1 //===- ARMISelLowering.cpp - ARM DAG Lowering Implementation --------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the interfaces that ARM uses to lower LLVM code into a 10 // selection DAG. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "ARMISelLowering.h" 15 #include "ARMBaseInstrInfo.h" 16 #include "ARMBaseRegisterInfo.h" 17 #include "ARMCallingConv.h" 18 #include "ARMConstantPoolValue.h" 19 #include "ARMMachineFunctionInfo.h" 20 #include "ARMPerfectShuffle.h" 21 #include "ARMRegisterInfo.h" 22 #include "ARMSelectionDAGInfo.h" 23 #include "ARMSubtarget.h" 24 #include "MCTargetDesc/ARMAddressingModes.h" 25 #include "MCTargetDesc/ARMBaseInfo.h" 26 #include "Utils/ARMBaseInfo.h" 27 #include "llvm/ADT/APFloat.h" 28 #include "llvm/ADT/APInt.h" 29 #include "llvm/ADT/ArrayRef.h" 30 #include "llvm/ADT/BitVector.h" 31 #include "llvm/ADT/DenseMap.h" 32 #include "llvm/ADT/STLExtras.h" 33 #include "llvm/ADT/SmallPtrSet.h" 34 #include "llvm/ADT/SmallVector.h" 35 #include "llvm/ADT/Statistic.h" 36 #include "llvm/ADT/StringExtras.h" 37 #include "llvm/ADT/StringRef.h" 38 #include "llvm/ADT/StringSwitch.h" 39 #include "llvm/ADT/Triple.h" 40 #include "llvm/ADT/Twine.h" 41 #include "llvm/Analysis/VectorUtils.h" 42 #include "llvm/CodeGen/CallingConvLower.h" 43 #include "llvm/CodeGen/ISDOpcodes.h" 44 #include "llvm/CodeGen/IntrinsicLowering.h" 45 #include "llvm/CodeGen/MachineBasicBlock.h" 46 #include "llvm/CodeGen/MachineConstantPool.h" 47 #include "llvm/CodeGen/MachineFrameInfo.h" 48 #include "llvm/CodeGen/MachineFunction.h" 49 #include "llvm/CodeGen/MachineInstr.h" 50 #include "llvm/CodeGen/MachineInstrBuilder.h" 51 #include "llvm/CodeGen/MachineJumpTableInfo.h" 52 #include "llvm/CodeGen/MachineMemOperand.h" 53 #include "llvm/CodeGen/MachineOperand.h" 54 #include "llvm/CodeGen/MachineRegisterInfo.h" 55 #include "llvm/CodeGen/RuntimeLibcalls.h" 56 #include "llvm/CodeGen/SelectionDAG.h" 57 #include "llvm/CodeGen/SelectionDAGNodes.h" 58 #include "llvm/CodeGen/TargetInstrInfo.h" 59 #include "llvm/CodeGen/TargetLowering.h" 60 #include "llvm/CodeGen/TargetOpcodes.h" 61 #include "llvm/CodeGen/TargetRegisterInfo.h" 62 #include "llvm/CodeGen/TargetSubtargetInfo.h" 63 #include "llvm/CodeGen/ValueTypes.h" 64 #include "llvm/IR/Attributes.h" 65 #include "llvm/IR/CallingConv.h" 66 #include "llvm/IR/Constant.h" 67 #include "llvm/IR/Constants.h" 68 #include "llvm/IR/DataLayout.h" 69 #include "llvm/IR/DebugLoc.h" 70 #include "llvm/IR/DerivedTypes.h" 71 #include "llvm/IR/Function.h" 72 #include "llvm/IR/GlobalAlias.h" 73 #include "llvm/IR/GlobalValue.h" 74 #include "llvm/IR/GlobalVariable.h" 75 #include "llvm/IR/IRBuilder.h" 76 #include "llvm/IR/InlineAsm.h" 77 #include "llvm/IR/Instruction.h" 78 #include "llvm/IR/Instructions.h" 79 #include "llvm/IR/IntrinsicInst.h" 80 #include "llvm/IR/Intrinsics.h" 81 #include "llvm/IR/IntrinsicsARM.h" 82 #include "llvm/IR/Module.h" 83 #include "llvm/IR/PatternMatch.h" 84 #include "llvm/IR/Type.h" 85 #include "llvm/IR/User.h" 86 #include "llvm/IR/Value.h" 87 #include "llvm/MC/MCInstrDesc.h" 88 #include "llvm/MC/MCInstrItineraries.h" 89 #include "llvm/MC/MCRegisterInfo.h" 90 #include "llvm/MC/MCSchedule.h" 91 #include "llvm/Support/AtomicOrdering.h" 92 #include "llvm/Support/BranchProbability.h" 93 #include "llvm/Support/Casting.h" 94 #include "llvm/Support/CodeGen.h" 95 #include "llvm/Support/CommandLine.h" 96 #include "llvm/Support/Compiler.h" 97 #include "llvm/Support/Debug.h" 98 #include "llvm/Support/ErrorHandling.h" 99 #include "llvm/Support/KnownBits.h" 100 #include "llvm/Support/MachineValueType.h" 101 #include "llvm/Support/MathExtras.h" 102 #include "llvm/Support/raw_ostream.h" 103 #include "llvm/Target/TargetMachine.h" 104 #include "llvm/Target/TargetOptions.h" 105 #include <algorithm> 106 #include <cassert> 107 #include <cstdint> 108 #include <cstdlib> 109 #include <iterator> 110 #include <limits> 111 #include <string> 112 #include <tuple> 113 #include <utility> 114 #include <vector> 115 116 using namespace llvm; 117 using namespace llvm::PatternMatch; 118 119 #define DEBUG_TYPE "arm-isel" 120 121 STATISTIC(NumTailCalls, "Number of tail calls"); 122 STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt"); 123 STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments"); 124 STATISTIC(NumConstpoolPromoted, 125 "Number of constants with their storage promoted into constant pools"); 126 127 static cl::opt<bool> 128 ARMInterworking("arm-interworking", cl::Hidden, 129 cl::desc("Enable / disable ARM interworking (for debugging only)"), 130 cl::init(true)); 131 132 static cl::opt<bool> EnableConstpoolPromotion( 133 "arm-promote-constant", cl::Hidden, 134 cl::desc("Enable / disable promotion of unnamed_addr constants into " 135 "constant pools"), 136 cl::init(false)); // FIXME: set to true by default once PR32780 is fixed 137 static cl::opt<unsigned> ConstpoolPromotionMaxSize( 138 "arm-promote-constant-max-size", cl::Hidden, 139 cl::desc("Maximum size of constant to promote into a constant pool"), 140 cl::init(64)); 141 static cl::opt<unsigned> ConstpoolPromotionMaxTotal( 142 "arm-promote-constant-max-total", cl::Hidden, 143 cl::desc("Maximum size of ALL constants to promote into a constant pool"), 144 cl::init(128)); 145 146 static cl::opt<unsigned> 147 MVEMaxSupportedInterleaveFactor("mve-max-interleave-factor", cl::Hidden, 148 cl::desc("Maximum interleave factor for MVE VLDn to generate."), 149 cl::init(2)); 150 151 // The APCS parameter registers. 152 static const MCPhysReg GPRArgRegs[] = { 153 ARM::R0, ARM::R1, ARM::R2, ARM::R3 154 }; 155 156 void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT, 157 MVT PromotedBitwiseVT) { 158 if (VT != PromotedLdStVT) { 159 setOperationAction(ISD::LOAD, VT, Promote); 160 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT); 161 162 setOperationAction(ISD::STORE, VT, Promote); 163 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT); 164 } 165 166 MVT ElemTy = VT.getVectorElementType(); 167 if (ElemTy != MVT::f64) 168 setOperationAction(ISD::SETCC, VT, Custom); 169 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); 170 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); 171 if (ElemTy == MVT::i32) { 172 setOperationAction(ISD::SINT_TO_FP, VT, Custom); 173 setOperationAction(ISD::UINT_TO_FP, VT, Custom); 174 setOperationAction(ISD::FP_TO_SINT, VT, Custom); 175 setOperationAction(ISD::FP_TO_UINT, VT, Custom); 176 } else { 177 setOperationAction(ISD::SINT_TO_FP, VT, Expand); 178 setOperationAction(ISD::UINT_TO_FP, VT, Expand); 179 setOperationAction(ISD::FP_TO_SINT, VT, Expand); 180 setOperationAction(ISD::FP_TO_UINT, VT, Expand); 181 } 182 setOperationAction(ISD::BUILD_VECTOR, VT, Custom); 183 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); 184 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal); 185 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal); 186 setOperationAction(ISD::SELECT, VT, Expand); 187 setOperationAction(ISD::SELECT_CC, VT, Expand); 188 setOperationAction(ISD::VSELECT, VT, Expand); 189 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); 190 if (VT.isInteger()) { 191 setOperationAction(ISD::SHL, VT, Custom); 192 setOperationAction(ISD::SRA, VT, Custom); 193 setOperationAction(ISD::SRL, VT, Custom); 194 } 195 196 // Promote all bit-wise operations. 197 if (VT.isInteger() && VT != PromotedBitwiseVT) { 198 setOperationAction(ISD::AND, VT, Promote); 199 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT); 200 setOperationAction(ISD::OR, VT, Promote); 201 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT); 202 setOperationAction(ISD::XOR, VT, Promote); 203 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT); 204 } 205 206 // Neon does not support vector divide/remainder operations. 207 setOperationAction(ISD::SDIV, VT, Expand); 208 setOperationAction(ISD::UDIV, VT, Expand); 209 setOperationAction(ISD::FDIV, VT, Expand); 210 setOperationAction(ISD::SREM, VT, Expand); 211 setOperationAction(ISD::UREM, VT, Expand); 212 setOperationAction(ISD::FREM, VT, Expand); 213 214 if (!VT.isFloatingPoint() && 215 VT != MVT::v2i64 && VT != MVT::v1i64) 216 for (auto Opcode : {ISD::ABS, ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX}) 217 setOperationAction(Opcode, VT, Legal); 218 if (!VT.isFloatingPoint()) 219 for (auto Opcode : {ISD::SADDSAT, ISD::UADDSAT, ISD::SSUBSAT, ISD::USUBSAT}) 220 setOperationAction(Opcode, VT, Legal); 221 } 222 223 void ARMTargetLowering::addDRTypeForNEON(MVT VT) { 224 addRegisterClass(VT, &ARM::DPRRegClass); 225 addTypeForNEON(VT, MVT::f64, MVT::v2i32); 226 } 227 228 void ARMTargetLowering::addQRTypeForNEON(MVT VT) { 229 addRegisterClass(VT, &ARM::DPairRegClass); 230 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32); 231 } 232 233 void ARMTargetLowering::setAllExpand(MVT VT) { 234 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc) 235 setOperationAction(Opc, VT, Expand); 236 237 // We support these really simple operations even on types where all 238 // the actual arithmetic has to be broken down into simpler 239 // operations or turned into library calls. 240 setOperationAction(ISD::BITCAST, VT, Legal); 241 setOperationAction(ISD::LOAD, VT, Legal); 242 setOperationAction(ISD::STORE, VT, Legal); 243 setOperationAction(ISD::UNDEF, VT, Legal); 244 } 245 246 void ARMTargetLowering::addAllExtLoads(const MVT From, const MVT To, 247 LegalizeAction Action) { 248 setLoadExtAction(ISD::EXTLOAD, From, To, Action); 249 setLoadExtAction(ISD::ZEXTLOAD, From, To, Action); 250 setLoadExtAction(ISD::SEXTLOAD, From, To, Action); 251 } 252 253 void ARMTargetLowering::addMVEVectorTypes(bool HasMVEFP) { 254 const MVT IntTypes[] = { MVT::v16i8, MVT::v8i16, MVT::v4i32 }; 255 256 for (auto VT : IntTypes) { 257 addRegisterClass(VT, &ARM::MQPRRegClass); 258 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); 259 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); 260 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); 261 setOperationAction(ISD::BUILD_VECTOR, VT, Custom); 262 setOperationAction(ISD::SHL, VT, Custom); 263 setOperationAction(ISD::SRA, VT, Custom); 264 setOperationAction(ISD::SRL, VT, Custom); 265 setOperationAction(ISD::SMIN, VT, Legal); 266 setOperationAction(ISD::SMAX, VT, Legal); 267 setOperationAction(ISD::UMIN, VT, Legal); 268 setOperationAction(ISD::UMAX, VT, Legal); 269 setOperationAction(ISD::ABS, VT, Legal); 270 setOperationAction(ISD::SETCC, VT, Custom); 271 setOperationAction(ISD::MLOAD, VT, Custom); 272 setOperationAction(ISD::MSTORE, VT, Legal); 273 setOperationAction(ISD::CTLZ, VT, Legal); 274 setOperationAction(ISD::CTTZ, VT, Custom); 275 setOperationAction(ISD::BITREVERSE, VT, Legal); 276 setOperationAction(ISD::BSWAP, VT, Legal); 277 setOperationAction(ISD::SADDSAT, VT, Legal); 278 setOperationAction(ISD::UADDSAT, VT, Legal); 279 setOperationAction(ISD::SSUBSAT, VT, Legal); 280 setOperationAction(ISD::USUBSAT, VT, Legal); 281 282 // No native support for these. 283 setOperationAction(ISD::UDIV, VT, Expand); 284 setOperationAction(ISD::SDIV, VT, Expand); 285 setOperationAction(ISD::UREM, VT, Expand); 286 setOperationAction(ISD::SREM, VT, Expand); 287 setOperationAction(ISD::CTPOP, VT, Expand); 288 289 // Vector reductions 290 setOperationAction(ISD::VECREDUCE_ADD, VT, Legal); 291 setOperationAction(ISD::VECREDUCE_SMAX, VT, Legal); 292 setOperationAction(ISD::VECREDUCE_UMAX, VT, Legal); 293 setOperationAction(ISD::VECREDUCE_SMIN, VT, Legal); 294 setOperationAction(ISD::VECREDUCE_UMIN, VT, Legal); 295 296 if (!HasMVEFP) { 297 setOperationAction(ISD::SINT_TO_FP, VT, Expand); 298 setOperationAction(ISD::UINT_TO_FP, VT, Expand); 299 setOperationAction(ISD::FP_TO_SINT, VT, Expand); 300 setOperationAction(ISD::FP_TO_UINT, VT, Expand); 301 } 302 303 // Pre and Post inc are supported on loads and stores 304 for (unsigned im = (unsigned)ISD::PRE_INC; 305 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) { 306 setIndexedLoadAction(im, VT, Legal); 307 setIndexedStoreAction(im, VT, Legal); 308 setIndexedMaskedLoadAction(im, VT, Legal); 309 setIndexedMaskedStoreAction(im, VT, Legal); 310 } 311 } 312 313 const MVT FloatTypes[] = { MVT::v8f16, MVT::v4f32 }; 314 for (auto VT : FloatTypes) { 315 addRegisterClass(VT, &ARM::MQPRRegClass); 316 if (!HasMVEFP) 317 setAllExpand(VT); 318 319 // These are legal or custom whether we have MVE.fp or not 320 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); 321 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); 322 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getVectorElementType(), Custom); 323 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); 324 setOperationAction(ISD::BUILD_VECTOR, VT, Custom); 325 setOperationAction(ISD::BUILD_VECTOR, VT.getVectorElementType(), Custom); 326 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Legal); 327 setOperationAction(ISD::SETCC, VT, Custom); 328 setOperationAction(ISD::MLOAD, VT, Custom); 329 setOperationAction(ISD::MSTORE, VT, Legal); 330 331 // Pre and Post inc are supported on loads and stores 332 for (unsigned im = (unsigned)ISD::PRE_INC; 333 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) { 334 setIndexedLoadAction(im, VT, Legal); 335 setIndexedStoreAction(im, VT, Legal); 336 setIndexedMaskedLoadAction(im, VT, Legal); 337 setIndexedMaskedStoreAction(im, VT, Legal); 338 } 339 340 if (HasMVEFP) { 341 setOperationAction(ISD::FMINNUM, VT, Legal); 342 setOperationAction(ISD::FMAXNUM, VT, Legal); 343 setOperationAction(ISD::FROUND, VT, Legal); 344 345 // No native support for these. 346 setOperationAction(ISD::FDIV, VT, Expand); 347 setOperationAction(ISD::FREM, VT, Expand); 348 setOperationAction(ISD::FSQRT, VT, Expand); 349 setOperationAction(ISD::FSIN, VT, Expand); 350 setOperationAction(ISD::FCOS, VT, Expand); 351 setOperationAction(ISD::FPOW, VT, Expand); 352 setOperationAction(ISD::FLOG, VT, Expand); 353 setOperationAction(ISD::FLOG2, VT, Expand); 354 setOperationAction(ISD::FLOG10, VT, Expand); 355 setOperationAction(ISD::FEXP, VT, Expand); 356 setOperationAction(ISD::FEXP2, VT, Expand); 357 setOperationAction(ISD::FNEARBYINT, VT, Expand); 358 } 359 } 360 361 // We 'support' these types up to bitcast/load/store level, regardless of 362 // MVE integer-only / float support. Only doing FP data processing on the FP 363 // vector types is inhibited at integer-only level. 364 const MVT LongTypes[] = { MVT::v2i64, MVT::v2f64 }; 365 for (auto VT : LongTypes) { 366 addRegisterClass(VT, &ARM::MQPRRegClass); 367 setAllExpand(VT); 368 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); 369 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); 370 setOperationAction(ISD::BUILD_VECTOR, VT, Custom); 371 } 372 // We can do bitwise operations on v2i64 vectors 373 setOperationAction(ISD::AND, MVT::v2i64, Legal); 374 setOperationAction(ISD::OR, MVT::v2i64, Legal); 375 setOperationAction(ISD::XOR, MVT::v2i64, Legal); 376 377 // It is legal to extload from v4i8 to v4i16 or v4i32. 378 addAllExtLoads(MVT::v8i16, MVT::v8i8, Legal); 379 addAllExtLoads(MVT::v4i32, MVT::v4i16, Legal); 380 addAllExtLoads(MVT::v4i32, MVT::v4i8, Legal); 381 382 // It is legal to sign extend from v4i8/v4i16 to v4i32 or v8i8 to v8i16. 383 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Legal); 384 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Legal); 385 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i32, Legal); 386 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v8i8, Legal); 387 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v8i16, Legal); 388 389 // Some truncating stores are legal too. 390 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Legal); 391 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Legal); 392 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Legal); 393 394 // Pre and Post inc on these are legal, given the correct extends 395 for (unsigned im = (unsigned)ISD::PRE_INC; 396 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) { 397 for (auto VT : {MVT::v8i8, MVT::v4i8, MVT::v4i16}) { 398 setIndexedLoadAction(im, VT, Legal); 399 setIndexedStoreAction(im, VT, Legal); 400 setIndexedMaskedLoadAction(im, VT, Legal); 401 setIndexedMaskedStoreAction(im, VT, Legal); 402 } 403 } 404 405 // Predicate types 406 const MVT pTypes[] = {MVT::v16i1, MVT::v8i1, MVT::v4i1}; 407 for (auto VT : pTypes) { 408 addRegisterClass(VT, &ARM::VCCRRegClass); 409 setOperationAction(ISD::BUILD_VECTOR, VT, Custom); 410 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); 411 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom); 412 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom); 413 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); 414 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); 415 setOperationAction(ISD::SETCC, VT, Custom); 416 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand); 417 setOperationAction(ISD::LOAD, VT, Custom); 418 setOperationAction(ISD::STORE, VT, Custom); 419 } 420 } 421 422 ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM, 423 const ARMSubtarget &STI) 424 : TargetLowering(TM), Subtarget(&STI) { 425 RegInfo = Subtarget->getRegisterInfo(); 426 Itins = Subtarget->getInstrItineraryData(); 427 428 setBooleanContents(ZeroOrOneBooleanContent); 429 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 430 431 if (!Subtarget->isTargetDarwin() && !Subtarget->isTargetIOS() && 432 !Subtarget->isTargetWatchOS()) { 433 bool IsHFTarget = TM.Options.FloatABIType == FloatABI::Hard; 434 for (int LCID = 0; LCID < RTLIB::UNKNOWN_LIBCALL; ++LCID) 435 setLibcallCallingConv(static_cast<RTLIB::Libcall>(LCID), 436 IsHFTarget ? CallingConv::ARM_AAPCS_VFP 437 : CallingConv::ARM_AAPCS); 438 } 439 440 if (Subtarget->isTargetMachO()) { 441 // Uses VFP for Thumb libfuncs if available. 442 if (Subtarget->isThumb() && Subtarget->hasVFP2Base() && 443 Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) { 444 static const struct { 445 const RTLIB::Libcall Op; 446 const char * const Name; 447 const ISD::CondCode Cond; 448 } LibraryCalls[] = { 449 // Single-precision floating-point arithmetic. 450 { RTLIB::ADD_F32, "__addsf3vfp", ISD::SETCC_INVALID }, 451 { RTLIB::SUB_F32, "__subsf3vfp", ISD::SETCC_INVALID }, 452 { RTLIB::MUL_F32, "__mulsf3vfp", ISD::SETCC_INVALID }, 453 { RTLIB::DIV_F32, "__divsf3vfp", ISD::SETCC_INVALID }, 454 455 // Double-precision floating-point arithmetic. 456 { RTLIB::ADD_F64, "__adddf3vfp", ISD::SETCC_INVALID }, 457 { RTLIB::SUB_F64, "__subdf3vfp", ISD::SETCC_INVALID }, 458 { RTLIB::MUL_F64, "__muldf3vfp", ISD::SETCC_INVALID }, 459 { RTLIB::DIV_F64, "__divdf3vfp", ISD::SETCC_INVALID }, 460 461 // Single-precision comparisons. 462 { RTLIB::OEQ_F32, "__eqsf2vfp", ISD::SETNE }, 463 { RTLIB::UNE_F32, "__nesf2vfp", ISD::SETNE }, 464 { RTLIB::OLT_F32, "__ltsf2vfp", ISD::SETNE }, 465 { RTLIB::OLE_F32, "__lesf2vfp", ISD::SETNE }, 466 { RTLIB::OGE_F32, "__gesf2vfp", ISD::SETNE }, 467 { RTLIB::OGT_F32, "__gtsf2vfp", ISD::SETNE }, 468 { RTLIB::UO_F32, "__unordsf2vfp", ISD::SETNE }, 469 470 // Double-precision comparisons. 471 { RTLIB::OEQ_F64, "__eqdf2vfp", ISD::SETNE }, 472 { RTLIB::UNE_F64, "__nedf2vfp", ISD::SETNE }, 473 { RTLIB::OLT_F64, "__ltdf2vfp", ISD::SETNE }, 474 { RTLIB::OLE_F64, "__ledf2vfp", ISD::SETNE }, 475 { RTLIB::OGE_F64, "__gedf2vfp", ISD::SETNE }, 476 { RTLIB::OGT_F64, "__gtdf2vfp", ISD::SETNE }, 477 { RTLIB::UO_F64, "__unorddf2vfp", ISD::SETNE }, 478 479 // Floating-point to integer conversions. 480 // i64 conversions are done via library routines even when generating VFP 481 // instructions, so use the same ones. 482 { RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp", ISD::SETCC_INVALID }, 483 { RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp", ISD::SETCC_INVALID }, 484 { RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp", ISD::SETCC_INVALID }, 485 { RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp", ISD::SETCC_INVALID }, 486 487 // Conversions between floating types. 488 { RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp", ISD::SETCC_INVALID }, 489 { RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp", ISD::SETCC_INVALID }, 490 491 // Integer to floating-point conversions. 492 // i64 conversions are done via library routines even when generating VFP 493 // instructions, so use the same ones. 494 // FIXME: There appears to be some naming inconsistency in ARM libgcc: 495 // e.g., __floatunsidf vs. __floatunssidfvfp. 496 { RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp", ISD::SETCC_INVALID }, 497 { RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp", ISD::SETCC_INVALID }, 498 { RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp", ISD::SETCC_INVALID }, 499 { RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp", ISD::SETCC_INVALID }, 500 }; 501 502 for (const auto &LC : LibraryCalls) { 503 setLibcallName(LC.Op, LC.Name); 504 if (LC.Cond != ISD::SETCC_INVALID) 505 setCmpLibcallCC(LC.Op, LC.Cond); 506 } 507 } 508 } 509 510 // These libcalls are not available in 32-bit. 511 setLibcallName(RTLIB::SHL_I128, nullptr); 512 setLibcallName(RTLIB::SRL_I128, nullptr); 513 setLibcallName(RTLIB::SRA_I128, nullptr); 514 515 // RTLIB 516 if (Subtarget->isAAPCS_ABI() && 517 (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() || 518 Subtarget->isTargetMuslAEABI() || Subtarget->isTargetAndroid())) { 519 static const struct { 520 const RTLIB::Libcall Op; 521 const char * const Name; 522 const CallingConv::ID CC; 523 const ISD::CondCode Cond; 524 } LibraryCalls[] = { 525 // Double-precision floating-point arithmetic helper functions 526 // RTABI chapter 4.1.2, Table 2 527 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 528 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 529 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 530 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 531 532 // Double-precision floating-point comparison helper functions 533 // RTABI chapter 4.1.2, Table 3 534 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE }, 535 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ }, 536 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE }, 537 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE }, 538 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE }, 539 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE }, 540 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE }, 541 542 // Single-precision floating-point arithmetic helper functions 543 // RTABI chapter 4.1.2, Table 4 544 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 545 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 546 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 547 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 548 549 // Single-precision floating-point comparison helper functions 550 // RTABI chapter 4.1.2, Table 5 551 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE }, 552 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ }, 553 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE }, 554 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE }, 555 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE }, 556 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE }, 557 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE }, 558 559 // Floating-point to integer conversions. 560 // RTABI chapter 4.1.2, Table 6 561 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 562 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 563 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 564 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 565 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 566 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 567 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 568 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 569 570 // Conversions between floating types. 571 // RTABI chapter 4.1.2, Table 7 572 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 573 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 574 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 575 576 // Integer to floating-point conversions. 577 // RTABI chapter 4.1.2, Table 8 578 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 579 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 580 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 581 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 582 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 583 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 584 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 585 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 586 587 // Long long helper functions 588 // RTABI chapter 4.2, Table 9 589 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 590 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 591 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 592 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 593 594 // Integer division functions 595 // RTABI chapter 4.3.1 596 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 597 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 598 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 599 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 600 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 601 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 602 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 603 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 604 }; 605 606 for (const auto &LC : LibraryCalls) { 607 setLibcallName(LC.Op, LC.Name); 608 setLibcallCallingConv(LC.Op, LC.CC); 609 if (LC.Cond != ISD::SETCC_INVALID) 610 setCmpLibcallCC(LC.Op, LC.Cond); 611 } 612 613 // EABI dependent RTLIB 614 if (TM.Options.EABIVersion == EABI::EABI4 || 615 TM.Options.EABIVersion == EABI::EABI5) { 616 static const struct { 617 const RTLIB::Libcall Op; 618 const char *const Name; 619 const CallingConv::ID CC; 620 const ISD::CondCode Cond; 621 } MemOpsLibraryCalls[] = { 622 // Memory operations 623 // RTABI chapter 4.3.4 624 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 625 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 626 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 627 }; 628 629 for (const auto &LC : MemOpsLibraryCalls) { 630 setLibcallName(LC.Op, LC.Name); 631 setLibcallCallingConv(LC.Op, LC.CC); 632 if (LC.Cond != ISD::SETCC_INVALID) 633 setCmpLibcallCC(LC.Op, LC.Cond); 634 } 635 } 636 } 637 638 if (Subtarget->isTargetWindows()) { 639 static const struct { 640 const RTLIB::Libcall Op; 641 const char * const Name; 642 const CallingConv::ID CC; 643 } LibraryCalls[] = { 644 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP }, 645 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP }, 646 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP }, 647 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP }, 648 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP }, 649 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP }, 650 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP }, 651 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP }, 652 }; 653 654 for (const auto &LC : LibraryCalls) { 655 setLibcallName(LC.Op, LC.Name); 656 setLibcallCallingConv(LC.Op, LC.CC); 657 } 658 } 659 660 // Use divmod compiler-rt calls for iOS 5.0 and later. 661 if (Subtarget->isTargetMachO() && 662 !(Subtarget->isTargetIOS() && 663 Subtarget->getTargetTriple().isOSVersionLT(5, 0))) { 664 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4"); 665 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4"); 666 } 667 668 // The half <-> float conversion functions are always soft-float on 669 // non-watchos platforms, but are needed for some targets which use a 670 // hard-float calling convention by default. 671 if (!Subtarget->isTargetWatchABI()) { 672 if (Subtarget->isAAPCS_ABI()) { 673 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS); 674 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS); 675 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS); 676 } else { 677 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS); 678 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS); 679 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS); 680 } 681 } 682 683 // In EABI, these functions have an __aeabi_ prefix, but in GNUEABI they have 684 // a __gnu_ prefix (which is the default). 685 if (Subtarget->isTargetAEABI()) { 686 static const struct { 687 const RTLIB::Libcall Op; 688 const char * const Name; 689 const CallingConv::ID CC; 690 } LibraryCalls[] = { 691 { RTLIB::FPROUND_F32_F16, "__aeabi_f2h", CallingConv::ARM_AAPCS }, 692 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS }, 693 { RTLIB::FPEXT_F16_F32, "__aeabi_h2f", CallingConv::ARM_AAPCS }, 694 }; 695 696 for (const auto &LC : LibraryCalls) { 697 setLibcallName(LC.Op, LC.Name); 698 setLibcallCallingConv(LC.Op, LC.CC); 699 } 700 } 701 702 if (Subtarget->isThumb1Only()) 703 addRegisterClass(MVT::i32, &ARM::tGPRRegClass); 704 else 705 addRegisterClass(MVT::i32, &ARM::GPRRegClass); 706 707 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only() && 708 Subtarget->hasFPRegs()) { 709 addRegisterClass(MVT::f32, &ARM::SPRRegClass); 710 addRegisterClass(MVT::f64, &ARM::DPRRegClass); 711 if (!Subtarget->hasVFP2Base()) 712 setAllExpand(MVT::f32); 713 if (!Subtarget->hasFP64()) 714 setAllExpand(MVT::f64); 715 } 716 717 if (Subtarget->hasFullFP16()) { 718 addRegisterClass(MVT::f16, &ARM::HPRRegClass); 719 setOperationAction(ISD::BITCAST, MVT::i16, Custom); 720 setOperationAction(ISD::BITCAST, MVT::i32, Custom); 721 setOperationAction(ISD::BITCAST, MVT::f16, Custom); 722 723 setOperationAction(ISD::FMINNUM, MVT::f16, Legal); 724 setOperationAction(ISD::FMAXNUM, MVT::f16, Legal); 725 } 726 727 for (MVT VT : MVT::fixedlen_vector_valuetypes()) { 728 for (MVT InnerVT : MVT::fixedlen_vector_valuetypes()) { 729 setTruncStoreAction(VT, InnerVT, Expand); 730 addAllExtLoads(VT, InnerVT, Expand); 731 } 732 733 setOperationAction(ISD::MULHS, VT, Expand); 734 setOperationAction(ISD::SMUL_LOHI, VT, Expand); 735 setOperationAction(ISD::MULHU, VT, Expand); 736 setOperationAction(ISD::UMUL_LOHI, VT, Expand); 737 738 setOperationAction(ISD::BSWAP, VT, Expand); 739 } 740 741 setOperationAction(ISD::ConstantFP, MVT::f32, Custom); 742 setOperationAction(ISD::ConstantFP, MVT::f64, Custom); 743 744 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom); 745 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom); 746 747 if (Subtarget->hasMVEIntegerOps()) 748 addMVEVectorTypes(Subtarget->hasMVEFloatOps()); 749 750 // Combine low-overhead loop intrinsics so that we can lower i1 types. 751 if (Subtarget->hasLOB()) { 752 setTargetDAGCombine(ISD::BRCOND); 753 setTargetDAGCombine(ISD::BR_CC); 754 } 755 756 if (Subtarget->hasNEON()) { 757 addDRTypeForNEON(MVT::v2f32); 758 addDRTypeForNEON(MVT::v8i8); 759 addDRTypeForNEON(MVT::v4i16); 760 addDRTypeForNEON(MVT::v2i32); 761 addDRTypeForNEON(MVT::v1i64); 762 763 addQRTypeForNEON(MVT::v4f32); 764 addQRTypeForNEON(MVT::v2f64); 765 addQRTypeForNEON(MVT::v16i8); 766 addQRTypeForNEON(MVT::v8i16); 767 addQRTypeForNEON(MVT::v4i32); 768 addQRTypeForNEON(MVT::v2i64); 769 770 if (Subtarget->hasFullFP16()) { 771 addQRTypeForNEON(MVT::v8f16); 772 addDRTypeForNEON(MVT::v4f16); 773 } 774 } 775 776 if (Subtarget->hasMVEIntegerOps() || Subtarget->hasNEON()) { 777 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but 778 // none of Neon, MVE or VFP supports any arithmetic operations on it. 779 setOperationAction(ISD::FADD, MVT::v2f64, Expand); 780 setOperationAction(ISD::FSUB, MVT::v2f64, Expand); 781 setOperationAction(ISD::FMUL, MVT::v2f64, Expand); 782 // FIXME: Code duplication: FDIV and FREM are expanded always, see 783 // ARMTargetLowering::addTypeForNEON method for details. 784 setOperationAction(ISD::FDIV, MVT::v2f64, Expand); 785 setOperationAction(ISD::FREM, MVT::v2f64, Expand); 786 // FIXME: Create unittest. 787 // In another words, find a way when "copysign" appears in DAG with vector 788 // operands. 789 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand); 790 // FIXME: Code duplication: SETCC has custom operation action, see 791 // ARMTargetLowering::addTypeForNEON method for details. 792 setOperationAction(ISD::SETCC, MVT::v2f64, Expand); 793 // FIXME: Create unittest for FNEG and for FABS. 794 setOperationAction(ISD::FNEG, MVT::v2f64, Expand); 795 setOperationAction(ISD::FABS, MVT::v2f64, Expand); 796 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand); 797 setOperationAction(ISD::FSIN, MVT::v2f64, Expand); 798 setOperationAction(ISD::FCOS, MVT::v2f64, Expand); 799 setOperationAction(ISD::FPOW, MVT::v2f64, Expand); 800 setOperationAction(ISD::FLOG, MVT::v2f64, Expand); 801 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand); 802 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand); 803 setOperationAction(ISD::FEXP, MVT::v2f64, Expand); 804 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand); 805 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR. 806 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand); 807 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand); 808 setOperationAction(ISD::FRINT, MVT::v2f64, Expand); 809 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand); 810 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand); 811 setOperationAction(ISD::FMA, MVT::v2f64, Expand); 812 } 813 814 if (Subtarget->hasNEON()) { 815 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively 816 // supported for v4f32. 817 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand); 818 setOperationAction(ISD::FSIN, MVT::v4f32, Expand); 819 setOperationAction(ISD::FCOS, MVT::v4f32, Expand); 820 setOperationAction(ISD::FPOW, MVT::v4f32, Expand); 821 setOperationAction(ISD::FLOG, MVT::v4f32, Expand); 822 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand); 823 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand); 824 setOperationAction(ISD::FEXP, MVT::v4f32, Expand); 825 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand); 826 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand); 827 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand); 828 setOperationAction(ISD::FRINT, MVT::v4f32, Expand); 829 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand); 830 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand); 831 832 // Mark v2f32 intrinsics. 833 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand); 834 setOperationAction(ISD::FSIN, MVT::v2f32, Expand); 835 setOperationAction(ISD::FCOS, MVT::v2f32, Expand); 836 setOperationAction(ISD::FPOW, MVT::v2f32, Expand); 837 setOperationAction(ISD::FLOG, MVT::v2f32, Expand); 838 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand); 839 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand); 840 setOperationAction(ISD::FEXP, MVT::v2f32, Expand); 841 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand); 842 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand); 843 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand); 844 setOperationAction(ISD::FRINT, MVT::v2f32, Expand); 845 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand); 846 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand); 847 848 // Neon does not support some operations on v1i64 and v2i64 types. 849 setOperationAction(ISD::MUL, MVT::v1i64, Expand); 850 // Custom handling for some quad-vector types to detect VMULL. 851 setOperationAction(ISD::MUL, MVT::v8i16, Custom); 852 setOperationAction(ISD::MUL, MVT::v4i32, Custom); 853 setOperationAction(ISD::MUL, MVT::v2i64, Custom); 854 // Custom handling for some vector types to avoid expensive expansions 855 setOperationAction(ISD::SDIV, MVT::v4i16, Custom); 856 setOperationAction(ISD::SDIV, MVT::v8i8, Custom); 857 setOperationAction(ISD::UDIV, MVT::v4i16, Custom); 858 setOperationAction(ISD::UDIV, MVT::v8i8, Custom); 859 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with 860 // a destination type that is wider than the source, and nor does 861 // it have a FP_TO_[SU]INT instruction with a narrower destination than 862 // source. 863 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom); 864 setOperationAction(ISD::SINT_TO_FP, MVT::v8i16, Custom); 865 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom); 866 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom); 867 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom); 868 setOperationAction(ISD::FP_TO_UINT, MVT::v8i16, Custom); 869 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom); 870 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom); 871 872 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand); 873 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand); 874 875 // NEON does not have single instruction CTPOP for vectors with element 876 // types wider than 8-bits. However, custom lowering can leverage the 877 // v8i8/v16i8 vcnt instruction. 878 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom); 879 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom); 880 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom); 881 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom); 882 setOperationAction(ISD::CTPOP, MVT::v1i64, Custom); 883 setOperationAction(ISD::CTPOP, MVT::v2i64, Custom); 884 885 setOperationAction(ISD::CTLZ, MVT::v1i64, Expand); 886 setOperationAction(ISD::CTLZ, MVT::v2i64, Expand); 887 888 // NEON does not have single instruction CTTZ for vectors. 889 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom); 890 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom); 891 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom); 892 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom); 893 894 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom); 895 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom); 896 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom); 897 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom); 898 899 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom); 900 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom); 901 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom); 902 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom); 903 904 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom); 905 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom); 906 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom); 907 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom); 908 909 // NEON only has FMA instructions as of VFP4. 910 if (!Subtarget->hasVFP4Base()) { 911 setOperationAction(ISD::FMA, MVT::v2f32, Expand); 912 setOperationAction(ISD::FMA, MVT::v4f32, Expand); 913 } 914 915 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); 916 setTargetDAGCombine(ISD::SHL); 917 setTargetDAGCombine(ISD::SRL); 918 setTargetDAGCombine(ISD::SRA); 919 setTargetDAGCombine(ISD::FP_TO_SINT); 920 setTargetDAGCombine(ISD::FP_TO_UINT); 921 setTargetDAGCombine(ISD::FDIV); 922 setTargetDAGCombine(ISD::LOAD); 923 924 // It is legal to extload from v4i8 to v4i16 or v4i32. 925 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16, 926 MVT::v2i32}) { 927 for (MVT VT : MVT::integer_fixedlen_vector_valuetypes()) { 928 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal); 929 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal); 930 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal); 931 } 932 } 933 } 934 935 if (Subtarget->hasNEON() || Subtarget->hasMVEIntegerOps()) { 936 setTargetDAGCombine(ISD::BUILD_VECTOR); 937 setTargetDAGCombine(ISD::VECTOR_SHUFFLE); 938 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT); 939 setTargetDAGCombine(ISD::STORE); 940 setTargetDAGCombine(ISD::SIGN_EXTEND); 941 setTargetDAGCombine(ISD::ZERO_EXTEND); 942 setTargetDAGCombine(ISD::ANY_EXTEND); 943 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN); 944 setTargetDAGCombine(ISD::INTRINSIC_VOID); 945 setTargetDAGCombine(ISD::VECREDUCE_ADD); 946 setTargetDAGCombine(ISD::ADD); 947 } 948 949 if (!Subtarget->hasFP64()) { 950 // When targeting a floating-point unit with only single-precision 951 // operations, f64 is legal for the few double-precision instructions which 952 // are present However, no double-precision operations other than moves, 953 // loads and stores are provided by the hardware. 954 setOperationAction(ISD::FADD, MVT::f64, Expand); 955 setOperationAction(ISD::FSUB, MVT::f64, Expand); 956 setOperationAction(ISD::FMUL, MVT::f64, Expand); 957 setOperationAction(ISD::FMA, MVT::f64, Expand); 958 setOperationAction(ISD::FDIV, MVT::f64, Expand); 959 setOperationAction(ISD::FREM, MVT::f64, Expand); 960 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 961 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand); 962 setOperationAction(ISD::FNEG, MVT::f64, Expand); 963 setOperationAction(ISD::FABS, MVT::f64, Expand); 964 setOperationAction(ISD::FSQRT, MVT::f64, Expand); 965 setOperationAction(ISD::FSIN, MVT::f64, Expand); 966 setOperationAction(ISD::FCOS, MVT::f64, Expand); 967 setOperationAction(ISD::FPOW, MVT::f64, Expand); 968 setOperationAction(ISD::FLOG, MVT::f64, Expand); 969 setOperationAction(ISD::FLOG2, MVT::f64, Expand); 970 setOperationAction(ISD::FLOG10, MVT::f64, Expand); 971 setOperationAction(ISD::FEXP, MVT::f64, Expand); 972 setOperationAction(ISD::FEXP2, MVT::f64, Expand); 973 setOperationAction(ISD::FCEIL, MVT::f64, Expand); 974 setOperationAction(ISD::FTRUNC, MVT::f64, Expand); 975 setOperationAction(ISD::FRINT, MVT::f64, Expand); 976 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand); 977 setOperationAction(ISD::FFLOOR, MVT::f64, Expand); 978 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 979 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); 980 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 981 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); 982 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom); 983 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom); 984 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom); 985 setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::i32, Custom); 986 setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i32, Custom); 987 setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::f64, Custom); 988 setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::f64, Custom); 989 setOperationAction(ISD::STRICT_FP_ROUND, MVT::f32, Custom); 990 } 991 992 if (!Subtarget->hasFP64() || !Subtarget->hasFPARMv8Base()) { 993 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom); 994 setOperationAction(ISD::STRICT_FP_EXTEND, MVT::f64, Custom); 995 if (Subtarget->hasFullFP16()) { 996 setOperationAction(ISD::FP_ROUND, MVT::f16, Custom); 997 setOperationAction(ISD::STRICT_FP_ROUND, MVT::f16, Custom); 998 } 999 } 1000 1001 if (!Subtarget->hasFP16()) { 1002 setOperationAction(ISD::FP_EXTEND, MVT::f32, Custom); 1003 setOperationAction(ISD::STRICT_FP_EXTEND, MVT::f32, Custom); 1004 } 1005 1006 computeRegisterProperties(Subtarget->getRegisterInfo()); 1007 1008 // ARM does not have floating-point extending loads. 1009 for (MVT VT : MVT::fp_valuetypes()) { 1010 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand); 1011 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand); 1012 } 1013 1014 // ... or truncating stores 1015 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 1016 setTruncStoreAction(MVT::f32, MVT::f16, Expand); 1017 setTruncStoreAction(MVT::f64, MVT::f16, Expand); 1018 1019 // ARM does not have i1 sign extending load. 1020 for (MVT VT : MVT::integer_valuetypes()) 1021 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 1022 1023 // ARM supports all 4 flavors of integer indexed load / store. 1024 if (!Subtarget->isThumb1Only()) { 1025 for (unsigned im = (unsigned)ISD::PRE_INC; 1026 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) { 1027 setIndexedLoadAction(im, MVT::i1, Legal); 1028 setIndexedLoadAction(im, MVT::i8, Legal); 1029 setIndexedLoadAction(im, MVT::i16, Legal); 1030 setIndexedLoadAction(im, MVT::i32, Legal); 1031 setIndexedStoreAction(im, MVT::i1, Legal); 1032 setIndexedStoreAction(im, MVT::i8, Legal); 1033 setIndexedStoreAction(im, MVT::i16, Legal); 1034 setIndexedStoreAction(im, MVT::i32, Legal); 1035 } 1036 } else { 1037 // Thumb-1 has limited post-inc load/store support - LDM r0!, {r1}. 1038 setIndexedLoadAction(ISD::POST_INC, MVT::i32, Legal); 1039 setIndexedStoreAction(ISD::POST_INC, MVT::i32, Legal); 1040 } 1041 1042 setOperationAction(ISD::SADDO, MVT::i32, Custom); 1043 setOperationAction(ISD::UADDO, MVT::i32, Custom); 1044 setOperationAction(ISD::SSUBO, MVT::i32, Custom); 1045 setOperationAction(ISD::USUBO, MVT::i32, Custom); 1046 1047 setOperationAction(ISD::ADDCARRY, MVT::i32, Custom); 1048 setOperationAction(ISD::SUBCARRY, MVT::i32, Custom); 1049 if (Subtarget->hasDSP()) { 1050 setOperationAction(ISD::SADDSAT, MVT::i8, Custom); 1051 setOperationAction(ISD::SSUBSAT, MVT::i8, Custom); 1052 setOperationAction(ISD::SADDSAT, MVT::i16, Custom); 1053 setOperationAction(ISD::SSUBSAT, MVT::i16, Custom); 1054 } 1055 if (Subtarget->hasBaseDSP()) { 1056 setOperationAction(ISD::SADDSAT, MVT::i32, Legal); 1057 setOperationAction(ISD::SSUBSAT, MVT::i32, Legal); 1058 } 1059 1060 // i64 operation support. 1061 setOperationAction(ISD::MUL, MVT::i64, Expand); 1062 setOperationAction(ISD::MULHU, MVT::i32, Expand); 1063 if (Subtarget->isThumb1Only()) { 1064 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); 1065 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); 1066 } 1067 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops() 1068 || (Subtarget->isThumb2() && !Subtarget->hasDSP())) 1069 setOperationAction(ISD::MULHS, MVT::i32, Expand); 1070 1071 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); 1072 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); 1073 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); 1074 setOperationAction(ISD::SRL, MVT::i64, Custom); 1075 setOperationAction(ISD::SRA, MVT::i64, Custom); 1076 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom); 1077 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom); 1078 1079 // MVE lowers 64 bit shifts to lsll and lsrl 1080 // assuming that ISD::SRL and SRA of i64 are already marked custom 1081 if (Subtarget->hasMVEIntegerOps()) 1082 setOperationAction(ISD::SHL, MVT::i64, Custom); 1083 1084 // Expand to __aeabi_l{lsl,lsr,asr} calls for Thumb1. 1085 if (Subtarget->isThumb1Only()) { 1086 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand); 1087 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand); 1088 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand); 1089 } 1090 1091 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) 1092 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal); 1093 1094 // ARM does not have ROTL. 1095 setOperationAction(ISD::ROTL, MVT::i32, Expand); 1096 for (MVT VT : MVT::fixedlen_vector_valuetypes()) { 1097 setOperationAction(ISD::ROTL, VT, Expand); 1098 setOperationAction(ISD::ROTR, VT, Expand); 1099 } 1100 setOperationAction(ISD::CTTZ, MVT::i32, Custom); 1101 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 1102 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only()) { 1103 setOperationAction(ISD::CTLZ, MVT::i32, Expand); 1104 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, LibCall); 1105 } 1106 1107 // @llvm.readcyclecounter requires the Performance Monitors extension. 1108 // Default to the 0 expansion on unsupported platforms. 1109 // FIXME: Technically there are older ARM CPUs that have 1110 // implementation-specific ways of obtaining this information. 1111 if (Subtarget->hasPerfMon()) 1112 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom); 1113 1114 // Only ARMv6 has BSWAP. 1115 if (!Subtarget->hasV6Ops()) 1116 setOperationAction(ISD::BSWAP, MVT::i32, Expand); 1117 1118 bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivideInThumbMode() 1119 : Subtarget->hasDivideInARMMode(); 1120 if (!hasDivide) { 1121 // These are expanded into libcalls if the cpu doesn't have HW divider. 1122 setOperationAction(ISD::SDIV, MVT::i32, LibCall); 1123 setOperationAction(ISD::UDIV, MVT::i32, LibCall); 1124 } 1125 1126 if (Subtarget->isTargetWindows() && !Subtarget->hasDivideInThumbMode()) { 1127 setOperationAction(ISD::SDIV, MVT::i32, Custom); 1128 setOperationAction(ISD::UDIV, MVT::i32, Custom); 1129 1130 setOperationAction(ISD::SDIV, MVT::i64, Custom); 1131 setOperationAction(ISD::UDIV, MVT::i64, Custom); 1132 } 1133 1134 setOperationAction(ISD::SREM, MVT::i32, Expand); 1135 setOperationAction(ISD::UREM, MVT::i32, Expand); 1136 1137 // Register based DivRem for AEABI (RTABI 4.2) 1138 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() || 1139 Subtarget->isTargetGNUAEABI() || Subtarget->isTargetMuslAEABI() || 1140 Subtarget->isTargetWindows()) { 1141 setOperationAction(ISD::SREM, MVT::i64, Custom); 1142 setOperationAction(ISD::UREM, MVT::i64, Custom); 1143 HasStandaloneRem = false; 1144 1145 if (Subtarget->isTargetWindows()) { 1146 const struct { 1147 const RTLIB::Libcall Op; 1148 const char * const Name; 1149 const CallingConv::ID CC; 1150 } LibraryCalls[] = { 1151 { RTLIB::SDIVREM_I8, "__rt_sdiv", CallingConv::ARM_AAPCS }, 1152 { RTLIB::SDIVREM_I16, "__rt_sdiv", CallingConv::ARM_AAPCS }, 1153 { RTLIB::SDIVREM_I32, "__rt_sdiv", CallingConv::ARM_AAPCS }, 1154 { RTLIB::SDIVREM_I64, "__rt_sdiv64", CallingConv::ARM_AAPCS }, 1155 1156 { RTLIB::UDIVREM_I8, "__rt_udiv", CallingConv::ARM_AAPCS }, 1157 { RTLIB::UDIVREM_I16, "__rt_udiv", CallingConv::ARM_AAPCS }, 1158 { RTLIB::UDIVREM_I32, "__rt_udiv", CallingConv::ARM_AAPCS }, 1159 { RTLIB::UDIVREM_I64, "__rt_udiv64", CallingConv::ARM_AAPCS }, 1160 }; 1161 1162 for (const auto &LC : LibraryCalls) { 1163 setLibcallName(LC.Op, LC.Name); 1164 setLibcallCallingConv(LC.Op, LC.CC); 1165 } 1166 } else { 1167 const struct { 1168 const RTLIB::Libcall Op; 1169 const char * const Name; 1170 const CallingConv::ID CC; 1171 } LibraryCalls[] = { 1172 { RTLIB::SDIVREM_I8, "__aeabi_idivmod", CallingConv::ARM_AAPCS }, 1173 { RTLIB::SDIVREM_I16, "__aeabi_idivmod", CallingConv::ARM_AAPCS }, 1174 { RTLIB::SDIVREM_I32, "__aeabi_idivmod", CallingConv::ARM_AAPCS }, 1175 { RTLIB::SDIVREM_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS }, 1176 1177 { RTLIB::UDIVREM_I8, "__aeabi_uidivmod", CallingConv::ARM_AAPCS }, 1178 { RTLIB::UDIVREM_I16, "__aeabi_uidivmod", CallingConv::ARM_AAPCS }, 1179 { RTLIB::UDIVREM_I32, "__aeabi_uidivmod", CallingConv::ARM_AAPCS }, 1180 { RTLIB::UDIVREM_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS }, 1181 }; 1182 1183 for (const auto &LC : LibraryCalls) { 1184 setLibcallName(LC.Op, LC.Name); 1185 setLibcallCallingConv(LC.Op, LC.CC); 1186 } 1187 } 1188 1189 setOperationAction(ISD::SDIVREM, MVT::i32, Custom); 1190 setOperationAction(ISD::UDIVREM, MVT::i32, Custom); 1191 setOperationAction(ISD::SDIVREM, MVT::i64, Custom); 1192 setOperationAction(ISD::UDIVREM, MVT::i64, Custom); 1193 } else { 1194 setOperationAction(ISD::SDIVREM, MVT::i32, Expand); 1195 setOperationAction(ISD::UDIVREM, MVT::i32, Expand); 1196 } 1197 1198 if (Subtarget->getTargetTriple().isOSMSVCRT()) { 1199 // MSVCRT doesn't have powi; fall back to pow 1200 setLibcallName(RTLIB::POWI_F32, nullptr); 1201 setLibcallName(RTLIB::POWI_F64, nullptr); 1202 } 1203 1204 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 1205 setOperationAction(ISD::ConstantPool, MVT::i32, Custom); 1206 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); 1207 setOperationAction(ISD::BlockAddress, MVT::i32, Custom); 1208 1209 setOperationAction(ISD::TRAP, MVT::Other, Legal); 1210 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal); 1211 1212 // Use the default implementation. 1213 setOperationAction(ISD::VASTART, MVT::Other, Custom); 1214 setOperationAction(ISD::VAARG, MVT::Other, Expand); 1215 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 1216 setOperationAction(ISD::VAEND, MVT::Other, Expand); 1217 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 1218 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 1219 1220 if (Subtarget->isTargetWindows()) 1221 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom); 1222 else 1223 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); 1224 1225 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use 1226 // the default expansion. 1227 InsertFencesForAtomic = false; 1228 if (Subtarget->hasAnyDataBarrier() && 1229 (!Subtarget->isThumb() || Subtarget->hasV8MBaselineOps())) { 1230 // ATOMIC_FENCE needs custom lowering; the others should have been expanded 1231 // to ldrex/strex loops already. 1232 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom); 1233 if (!Subtarget->isThumb() || !Subtarget->isMClass()) 1234 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom); 1235 1236 // On v8, we have particularly efficient implementations of atomic fences 1237 // if they can be combined with nearby atomic loads and stores. 1238 if (!Subtarget->hasAcquireRelease() || 1239 getTargetMachine().getOptLevel() == 0) { 1240 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc. 1241 InsertFencesForAtomic = true; 1242 } 1243 } else { 1244 // If there's anything we can use as a barrier, go through custom lowering 1245 // for ATOMIC_FENCE. 1246 // If target has DMB in thumb, Fences can be inserted. 1247 if (Subtarget->hasDataBarrier()) 1248 InsertFencesForAtomic = true; 1249 1250 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, 1251 Subtarget->hasAnyDataBarrier() ? Custom : Expand); 1252 1253 // Set them all for expansion, which will force libcalls. 1254 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand); 1255 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand); 1256 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand); 1257 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand); 1258 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand); 1259 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand); 1260 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand); 1261 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand); 1262 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand); 1263 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand); 1264 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand); 1265 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand); 1266 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the 1267 // Unordered/Monotonic case. 1268 if (!InsertFencesForAtomic) { 1269 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom); 1270 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom); 1271 } 1272 } 1273 1274 setOperationAction(ISD::PREFETCH, MVT::Other, Custom); 1275 1276 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes. 1277 if (!Subtarget->hasV6Ops()) { 1278 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); 1279 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand); 1280 } 1281 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 1282 1283 if (!Subtarget->useSoftFloat() && Subtarget->hasFPRegs() && 1284 !Subtarget->isThumb1Only()) { 1285 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR 1286 // iff target supports vfp2. 1287 setOperationAction(ISD::BITCAST, MVT::i64, Custom); 1288 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom); 1289 } 1290 1291 // We want to custom lower some of our intrinsics. 1292 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 1293 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom); 1294 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom); 1295 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom); 1296 if (Subtarget->useSjLjEH()) 1297 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume"); 1298 1299 setOperationAction(ISD::SETCC, MVT::i32, Expand); 1300 setOperationAction(ISD::SETCC, MVT::f32, Expand); 1301 setOperationAction(ISD::SETCC, MVT::f64, Expand); 1302 setOperationAction(ISD::SELECT, MVT::i32, Custom); 1303 setOperationAction(ISD::SELECT, MVT::f32, Custom); 1304 setOperationAction(ISD::SELECT, MVT::f64, Custom); 1305 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); 1306 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 1307 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); 1308 if (Subtarget->hasFullFP16()) { 1309 setOperationAction(ISD::SETCC, MVT::f16, Expand); 1310 setOperationAction(ISD::SELECT, MVT::f16, Custom); 1311 setOperationAction(ISD::SELECT_CC, MVT::f16, Custom); 1312 } 1313 1314 setOperationAction(ISD::SETCCCARRY, MVT::i32, Custom); 1315 1316 setOperationAction(ISD::BRCOND, MVT::Other, Custom); 1317 setOperationAction(ISD::BR_CC, MVT::i32, Custom); 1318 if (Subtarget->hasFullFP16()) 1319 setOperationAction(ISD::BR_CC, MVT::f16, Custom); 1320 setOperationAction(ISD::BR_CC, MVT::f32, Custom); 1321 setOperationAction(ISD::BR_CC, MVT::f64, Custom); 1322 setOperationAction(ISD::BR_JT, MVT::Other, Custom); 1323 1324 // We don't support sin/cos/fmod/copysign/pow 1325 setOperationAction(ISD::FSIN, MVT::f64, Expand); 1326 setOperationAction(ISD::FSIN, MVT::f32, Expand); 1327 setOperationAction(ISD::FCOS, MVT::f32, Expand); 1328 setOperationAction(ISD::FCOS, MVT::f64, Expand); 1329 setOperationAction(ISD::FSINCOS, MVT::f64, Expand); 1330 setOperationAction(ISD::FSINCOS, MVT::f32, Expand); 1331 setOperationAction(ISD::FREM, MVT::f64, Expand); 1332 setOperationAction(ISD::FREM, MVT::f32, Expand); 1333 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2Base() && 1334 !Subtarget->isThumb1Only()) { 1335 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); 1336 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); 1337 } 1338 setOperationAction(ISD::FPOW, MVT::f64, Expand); 1339 setOperationAction(ISD::FPOW, MVT::f32, Expand); 1340 1341 if (!Subtarget->hasVFP4Base()) { 1342 setOperationAction(ISD::FMA, MVT::f64, Expand); 1343 setOperationAction(ISD::FMA, MVT::f32, Expand); 1344 } 1345 1346 // Various VFP goodness 1347 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) { 1348 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded. 1349 if (!Subtarget->hasFPARMv8Base() || !Subtarget->hasFP64()) { 1350 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand); 1351 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand); 1352 } 1353 1354 // fp16 is a special v7 extension that adds f16 <-> f32 conversions. 1355 if (!Subtarget->hasFP16()) { 1356 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand); 1357 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand); 1358 } 1359 1360 // Strict floating-point comparisons need custom lowering. 1361 setOperationAction(ISD::STRICT_FSETCC, MVT::f16, Custom); 1362 setOperationAction(ISD::STRICT_FSETCCS, MVT::f16, Custom); 1363 setOperationAction(ISD::STRICT_FSETCC, MVT::f32, Custom); 1364 setOperationAction(ISD::STRICT_FSETCCS, MVT::f32, Custom); 1365 setOperationAction(ISD::STRICT_FSETCC, MVT::f64, Custom); 1366 setOperationAction(ISD::STRICT_FSETCCS, MVT::f64, Custom); 1367 } 1368 1369 // Use __sincos_stret if available. 1370 if (getLibcallName(RTLIB::SINCOS_STRET_F32) != nullptr && 1371 getLibcallName(RTLIB::SINCOS_STRET_F64) != nullptr) { 1372 setOperationAction(ISD::FSINCOS, MVT::f64, Custom); 1373 setOperationAction(ISD::FSINCOS, MVT::f32, Custom); 1374 } 1375 1376 // FP-ARMv8 implements a lot of rounding-like FP operations. 1377 if (Subtarget->hasFPARMv8Base()) { 1378 setOperationAction(ISD::FFLOOR, MVT::f32, Legal); 1379 setOperationAction(ISD::FCEIL, MVT::f32, Legal); 1380 setOperationAction(ISD::FROUND, MVT::f32, Legal); 1381 setOperationAction(ISD::FTRUNC, MVT::f32, Legal); 1382 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal); 1383 setOperationAction(ISD::FRINT, MVT::f32, Legal); 1384 setOperationAction(ISD::FMINNUM, MVT::f32, Legal); 1385 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal); 1386 if (Subtarget->hasNEON()) { 1387 setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal); 1388 setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal); 1389 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal); 1390 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal); 1391 } 1392 1393 if (Subtarget->hasFP64()) { 1394 setOperationAction(ISD::FFLOOR, MVT::f64, Legal); 1395 setOperationAction(ISD::FCEIL, MVT::f64, Legal); 1396 setOperationAction(ISD::FROUND, MVT::f64, Legal); 1397 setOperationAction(ISD::FTRUNC, MVT::f64, Legal); 1398 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal); 1399 setOperationAction(ISD::FRINT, MVT::f64, Legal); 1400 setOperationAction(ISD::FMINNUM, MVT::f64, Legal); 1401 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal); 1402 } 1403 } 1404 1405 // FP16 often need to be promoted to call lib functions 1406 if (Subtarget->hasFullFP16()) { 1407 setOperationAction(ISD::FREM, MVT::f16, Promote); 1408 setOperationAction(ISD::FCOPYSIGN, MVT::f16, Expand); 1409 setOperationAction(ISD::FSIN, MVT::f16, Promote); 1410 setOperationAction(ISD::FCOS, MVT::f16, Promote); 1411 setOperationAction(ISD::FSINCOS, MVT::f16, Promote); 1412 setOperationAction(ISD::FPOWI, MVT::f16, Promote); 1413 setOperationAction(ISD::FPOW, MVT::f16, Promote); 1414 setOperationAction(ISD::FEXP, MVT::f16, Promote); 1415 setOperationAction(ISD::FEXP2, MVT::f16, Promote); 1416 setOperationAction(ISD::FLOG, MVT::f16, Promote); 1417 setOperationAction(ISD::FLOG10, MVT::f16, Promote); 1418 setOperationAction(ISD::FLOG2, MVT::f16, Promote); 1419 1420 setOperationAction(ISD::FROUND, MVT::f16, Legal); 1421 } 1422 1423 if (Subtarget->hasNEON()) { 1424 // vmin and vmax aren't available in a scalar form, so we use 1425 // a NEON instruction with an undef lane instead. 1426 setOperationAction(ISD::FMINIMUM, MVT::f16, Legal); 1427 setOperationAction(ISD::FMAXIMUM, MVT::f16, Legal); 1428 setOperationAction(ISD::FMINIMUM, MVT::f32, Legal); 1429 setOperationAction(ISD::FMAXIMUM, MVT::f32, Legal); 1430 setOperationAction(ISD::FMINIMUM, MVT::v2f32, Legal); 1431 setOperationAction(ISD::FMAXIMUM, MVT::v2f32, Legal); 1432 setOperationAction(ISD::FMINIMUM, MVT::v4f32, Legal); 1433 setOperationAction(ISD::FMAXIMUM, MVT::v4f32, Legal); 1434 1435 if (Subtarget->hasFullFP16()) { 1436 setOperationAction(ISD::FMINNUM, MVT::v4f16, Legal); 1437 setOperationAction(ISD::FMAXNUM, MVT::v4f16, Legal); 1438 setOperationAction(ISD::FMINNUM, MVT::v8f16, Legal); 1439 setOperationAction(ISD::FMAXNUM, MVT::v8f16, Legal); 1440 1441 setOperationAction(ISD::FMINIMUM, MVT::v4f16, Legal); 1442 setOperationAction(ISD::FMAXIMUM, MVT::v4f16, Legal); 1443 setOperationAction(ISD::FMINIMUM, MVT::v8f16, Legal); 1444 setOperationAction(ISD::FMAXIMUM, MVT::v8f16, Legal); 1445 } 1446 } 1447 1448 // We have target-specific dag combine patterns for the following nodes: 1449 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine 1450 setTargetDAGCombine(ISD::ADD); 1451 setTargetDAGCombine(ISD::SUB); 1452 setTargetDAGCombine(ISD::MUL); 1453 setTargetDAGCombine(ISD::AND); 1454 setTargetDAGCombine(ISD::OR); 1455 setTargetDAGCombine(ISD::XOR); 1456 1457 if (Subtarget->hasV6Ops()) 1458 setTargetDAGCombine(ISD::SRL); 1459 if (Subtarget->isThumb1Only()) 1460 setTargetDAGCombine(ISD::SHL); 1461 1462 setStackPointerRegisterToSaveRestore(ARM::SP); 1463 1464 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() || 1465 !Subtarget->hasVFP2Base() || Subtarget->hasMinSize()) 1466 setSchedulingPreference(Sched::RegPressure); 1467 else 1468 setSchedulingPreference(Sched::Hybrid); 1469 1470 //// temporary - rewrite interface to use type 1471 MaxStoresPerMemset = 8; 1472 MaxStoresPerMemsetOptSize = 4; 1473 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores 1474 MaxStoresPerMemcpyOptSize = 2; 1475 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores 1476 MaxStoresPerMemmoveOptSize = 2; 1477 1478 // On ARM arguments smaller than 4 bytes are extended, so all arguments 1479 // are at least 4 bytes aligned. 1480 setMinStackArgumentAlignment(Align(4)); 1481 1482 // Prefer likely predicted branches to selects on out-of-order cores. 1483 PredictableSelectIsExpensive = Subtarget->getSchedModel().isOutOfOrder(); 1484 1485 setPrefLoopAlignment(Align(1ULL << Subtarget->getPrefLoopLogAlignment())); 1486 1487 setMinFunctionAlignment(Subtarget->isThumb() ? Align(2) : Align(4)); 1488 1489 if (Subtarget->isThumb() || Subtarget->isThumb2()) 1490 setTargetDAGCombine(ISD::ABS); 1491 } 1492 1493 bool ARMTargetLowering::useSoftFloat() const { 1494 return Subtarget->useSoftFloat(); 1495 } 1496 1497 // FIXME: It might make sense to define the representative register class as the 1498 // nearest super-register that has a non-null superset. For example, DPR_VFP2 is 1499 // a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently, 1500 // SPR's representative would be DPR_VFP2. This should work well if register 1501 // pressure tracking were modified such that a register use would increment the 1502 // pressure of the register class's representative and all of it's super 1503 // classes' representatives transitively. We have not implemented this because 1504 // of the difficulty prior to coalescing of modeling operand register classes 1505 // due to the common occurrence of cross class copies and subregister insertions 1506 // and extractions. 1507 std::pair<const TargetRegisterClass *, uint8_t> 1508 ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI, 1509 MVT VT) const { 1510 const TargetRegisterClass *RRC = nullptr; 1511 uint8_t Cost = 1; 1512 switch (VT.SimpleTy) { 1513 default: 1514 return TargetLowering::findRepresentativeClass(TRI, VT); 1515 // Use DPR as representative register class for all floating point 1516 // and vector types. Since there are 32 SPR registers and 32 DPR registers so 1517 // the cost is 1 for both f32 and f64. 1518 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16: 1519 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32: 1520 RRC = &ARM::DPRRegClass; 1521 // When NEON is used for SP, only half of the register file is available 1522 // because operations that define both SP and DP results will be constrained 1523 // to the VFP2 class (D0-D15). We currently model this constraint prior to 1524 // coalescing by double-counting the SP regs. See the FIXME above. 1525 if (Subtarget->useNEONForSinglePrecisionFP()) 1526 Cost = 2; 1527 break; 1528 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64: 1529 case MVT::v4f32: case MVT::v2f64: 1530 RRC = &ARM::DPRRegClass; 1531 Cost = 2; 1532 break; 1533 case MVT::v4i64: 1534 RRC = &ARM::DPRRegClass; 1535 Cost = 4; 1536 break; 1537 case MVT::v8i64: 1538 RRC = &ARM::DPRRegClass; 1539 Cost = 8; 1540 break; 1541 } 1542 return std::make_pair(RRC, Cost); 1543 } 1544 1545 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const { 1546 switch ((ARMISD::NodeType)Opcode) { 1547 case ARMISD::FIRST_NUMBER: break; 1548 case ARMISD::Wrapper: return "ARMISD::Wrapper"; 1549 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC"; 1550 case ARMISD::WrapperJT: return "ARMISD::WrapperJT"; 1551 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL"; 1552 case ARMISD::CALL: return "ARMISD::CALL"; 1553 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED"; 1554 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK"; 1555 case ARMISD::BRCOND: return "ARMISD::BRCOND"; 1556 case ARMISD::BR_JT: return "ARMISD::BR_JT"; 1557 case ARMISD::BR2_JT: return "ARMISD::BR2_JT"; 1558 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG"; 1559 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG"; 1560 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD"; 1561 case ARMISD::CMP: return "ARMISD::CMP"; 1562 case ARMISD::CMN: return "ARMISD::CMN"; 1563 case ARMISD::CMPZ: return "ARMISD::CMPZ"; 1564 case ARMISD::CMPFP: return "ARMISD::CMPFP"; 1565 case ARMISD::CMPFPE: return "ARMISD::CMPFPE"; 1566 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0"; 1567 case ARMISD::CMPFPEw0: return "ARMISD::CMPFPEw0"; 1568 case ARMISD::BCC_i64: return "ARMISD::BCC_i64"; 1569 case ARMISD::FMSTAT: return "ARMISD::FMSTAT"; 1570 1571 case ARMISD::CMOV: return "ARMISD::CMOV"; 1572 case ARMISD::SUBS: return "ARMISD::SUBS"; 1573 1574 case ARMISD::SSAT: return "ARMISD::SSAT"; 1575 case ARMISD::USAT: return "ARMISD::USAT"; 1576 1577 case ARMISD::ASRL: return "ARMISD::ASRL"; 1578 case ARMISD::LSRL: return "ARMISD::LSRL"; 1579 case ARMISD::LSLL: return "ARMISD::LSLL"; 1580 1581 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG"; 1582 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG"; 1583 case ARMISD::RRX: return "ARMISD::RRX"; 1584 1585 case ARMISD::ADDC: return "ARMISD::ADDC"; 1586 case ARMISD::ADDE: return "ARMISD::ADDE"; 1587 case ARMISD::SUBC: return "ARMISD::SUBC"; 1588 case ARMISD::SUBE: return "ARMISD::SUBE"; 1589 case ARMISD::LSLS: return "ARMISD::LSLS"; 1590 1591 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD"; 1592 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR"; 1593 case ARMISD::VMOVhr: return "ARMISD::VMOVhr"; 1594 case ARMISD::VMOVrh: return "ARMISD::VMOVrh"; 1595 case ARMISD::VMOVSR: return "ARMISD::VMOVSR"; 1596 1597 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP"; 1598 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP"; 1599 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH"; 1600 1601 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN"; 1602 1603 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER"; 1604 1605 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC"; 1606 1607 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR"; 1608 1609 case ARMISD::PRELOAD: return "ARMISD::PRELOAD"; 1610 1611 case ARMISD::WIN__CHKSTK: return "ARMISD::WIN__CHKSTK"; 1612 case ARMISD::WIN__DBZCHK: return "ARMISD::WIN__DBZCHK"; 1613 1614 case ARMISD::PREDICATE_CAST: return "ARMISD::PREDICATE_CAST"; 1615 case ARMISD::VECTOR_REG_CAST: return "ARMISD::VECTOR_REG_CAST"; 1616 case ARMISD::VCMP: return "ARMISD::VCMP"; 1617 case ARMISD::VCMPZ: return "ARMISD::VCMPZ"; 1618 case ARMISD::VTST: return "ARMISD::VTST"; 1619 1620 case ARMISD::VSHLs: return "ARMISD::VSHLs"; 1621 case ARMISD::VSHLu: return "ARMISD::VSHLu"; 1622 case ARMISD::VSHLIMM: return "ARMISD::VSHLIMM"; 1623 case ARMISD::VSHRsIMM: return "ARMISD::VSHRsIMM"; 1624 case ARMISD::VSHRuIMM: return "ARMISD::VSHRuIMM"; 1625 case ARMISD::VRSHRsIMM: return "ARMISD::VRSHRsIMM"; 1626 case ARMISD::VRSHRuIMM: return "ARMISD::VRSHRuIMM"; 1627 case ARMISD::VRSHRNIMM: return "ARMISD::VRSHRNIMM"; 1628 case ARMISD::VQSHLsIMM: return "ARMISD::VQSHLsIMM"; 1629 case ARMISD::VQSHLuIMM: return "ARMISD::VQSHLuIMM"; 1630 case ARMISD::VQSHLsuIMM: return "ARMISD::VQSHLsuIMM"; 1631 case ARMISD::VQSHRNsIMM: return "ARMISD::VQSHRNsIMM"; 1632 case ARMISD::VQSHRNuIMM: return "ARMISD::VQSHRNuIMM"; 1633 case ARMISD::VQSHRNsuIMM: return "ARMISD::VQSHRNsuIMM"; 1634 case ARMISD::VQRSHRNsIMM: return "ARMISD::VQRSHRNsIMM"; 1635 case ARMISD::VQRSHRNuIMM: return "ARMISD::VQRSHRNuIMM"; 1636 case ARMISD::VQRSHRNsuIMM: return "ARMISD::VQRSHRNsuIMM"; 1637 case ARMISD::VSLIIMM: return "ARMISD::VSLIIMM"; 1638 case ARMISD::VSRIIMM: return "ARMISD::VSRIIMM"; 1639 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu"; 1640 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs"; 1641 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM"; 1642 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM"; 1643 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM"; 1644 case ARMISD::VDUP: return "ARMISD::VDUP"; 1645 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE"; 1646 case ARMISD::VEXT: return "ARMISD::VEXT"; 1647 case ARMISD::VREV64: return "ARMISD::VREV64"; 1648 case ARMISD::VREV32: return "ARMISD::VREV32"; 1649 case ARMISD::VREV16: return "ARMISD::VREV16"; 1650 case ARMISD::VZIP: return "ARMISD::VZIP"; 1651 case ARMISD::VUZP: return "ARMISD::VUZP"; 1652 case ARMISD::VTRN: return "ARMISD::VTRN"; 1653 case ARMISD::VTBL1: return "ARMISD::VTBL1"; 1654 case ARMISD::VTBL2: return "ARMISD::VTBL2"; 1655 case ARMISD::VMOVN: return "ARMISD::VMOVN"; 1656 case ARMISD::VMULLs: return "ARMISD::VMULLs"; 1657 case ARMISD::VMULLu: return "ARMISD::VMULLu"; 1658 case ARMISD::VADDVs: return "ARMISD::VADDVs"; 1659 case ARMISD::VADDVu: return "ARMISD::VADDVu"; 1660 case ARMISD::VADDLVs: return "ARMISD::VADDLVs"; 1661 case ARMISD::VADDLVu: return "ARMISD::VADDLVu"; 1662 case ARMISD::VADDLVAs: return "ARMISD::VADDLVAs"; 1663 case ARMISD::VADDLVAu: return "ARMISD::VADDLVAu"; 1664 case ARMISD::VMLAVs: return "ARMISD::VMLAVs"; 1665 case ARMISD::VMLAVu: return "ARMISD::VMLAVu"; 1666 case ARMISD::VMLALVs: return "ARMISD::VMLALVs"; 1667 case ARMISD::VMLALVu: return "ARMISD::VMLALVu"; 1668 case ARMISD::VMLALVAs: return "ARMISD::VMLALVAs"; 1669 case ARMISD::VMLALVAu: return "ARMISD::VMLALVAu"; 1670 case ARMISD::UMAAL: return "ARMISD::UMAAL"; 1671 case ARMISD::UMLAL: return "ARMISD::UMLAL"; 1672 case ARMISD::SMLAL: return "ARMISD::SMLAL"; 1673 case ARMISD::SMLALBB: return "ARMISD::SMLALBB"; 1674 case ARMISD::SMLALBT: return "ARMISD::SMLALBT"; 1675 case ARMISD::SMLALTB: return "ARMISD::SMLALTB"; 1676 case ARMISD::SMLALTT: return "ARMISD::SMLALTT"; 1677 case ARMISD::SMULWB: return "ARMISD::SMULWB"; 1678 case ARMISD::SMULWT: return "ARMISD::SMULWT"; 1679 case ARMISD::SMLALD: return "ARMISD::SMLALD"; 1680 case ARMISD::SMLALDX: return "ARMISD::SMLALDX"; 1681 case ARMISD::SMLSLD: return "ARMISD::SMLSLD"; 1682 case ARMISD::SMLSLDX: return "ARMISD::SMLSLDX"; 1683 case ARMISD::SMMLAR: return "ARMISD::SMMLAR"; 1684 case ARMISD::SMMLSR: return "ARMISD::SMMLSR"; 1685 case ARMISD::QADD16b: return "ARMISD::QADD16b"; 1686 case ARMISD::QSUB16b: return "ARMISD::QSUB16b"; 1687 case ARMISD::QADD8b: return "ARMISD::QADD8b"; 1688 case ARMISD::QSUB8b: return "ARMISD::QSUB8b"; 1689 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR"; 1690 case ARMISD::BFI: return "ARMISD::BFI"; 1691 case ARMISD::VORRIMM: return "ARMISD::VORRIMM"; 1692 case ARMISD::VBICIMM: return "ARMISD::VBICIMM"; 1693 case ARMISD::VBSL: return "ARMISD::VBSL"; 1694 case ARMISD::MEMCPY: return "ARMISD::MEMCPY"; 1695 case ARMISD::VLD1DUP: return "ARMISD::VLD1DUP"; 1696 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP"; 1697 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP"; 1698 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP"; 1699 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD"; 1700 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD"; 1701 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD"; 1702 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD"; 1703 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD"; 1704 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD"; 1705 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD"; 1706 case ARMISD::VLD1DUP_UPD: return "ARMISD::VLD1DUP_UPD"; 1707 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD"; 1708 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD"; 1709 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD"; 1710 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD"; 1711 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD"; 1712 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD"; 1713 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD"; 1714 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD"; 1715 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD"; 1716 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD"; 1717 case ARMISD::WLS: return "ARMISD::WLS"; 1718 case ARMISD::LE: return "ARMISD::LE"; 1719 case ARMISD::LOOP_DEC: return "ARMISD::LOOP_DEC"; 1720 case ARMISD::CSINV: return "ARMISD::CSINV"; 1721 case ARMISD::CSNEG: return "ARMISD::CSNEG"; 1722 case ARMISD::CSINC: return "ARMISD::CSINC"; 1723 } 1724 return nullptr; 1725 } 1726 1727 EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &, 1728 EVT VT) const { 1729 if (!VT.isVector()) 1730 return getPointerTy(DL); 1731 1732 // MVE has a predicate register. 1733 if (Subtarget->hasMVEIntegerOps() && 1734 (VT == MVT::v4i32 || VT == MVT::v8i16 || VT == MVT::v16i8)) 1735 return MVT::getVectorVT(MVT::i1, VT.getVectorElementCount()); 1736 return VT.changeVectorElementTypeToInteger(); 1737 } 1738 1739 /// getRegClassFor - Return the register class that should be used for the 1740 /// specified value type. 1741 const TargetRegisterClass * 1742 ARMTargetLowering::getRegClassFor(MVT VT, bool isDivergent) const { 1743 (void)isDivergent; 1744 // Map v4i64 to QQ registers but do not make the type legal. Similarly map 1745 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to 1746 // load / store 4 to 8 consecutive NEON D registers, or 2 to 4 consecutive 1747 // MVE Q registers. 1748 if (Subtarget->hasNEON() || Subtarget->hasMVEIntegerOps()) { 1749 if (VT == MVT::v4i64) 1750 return &ARM::QQPRRegClass; 1751 if (VT == MVT::v8i64) 1752 return &ARM::QQQQPRRegClass; 1753 } 1754 return TargetLowering::getRegClassFor(VT); 1755 } 1756 1757 // memcpy, and other memory intrinsics, typically tries to use LDM/STM if the 1758 // source/dest is aligned and the copy size is large enough. We therefore want 1759 // to align such objects passed to memory intrinsics. 1760 bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize, 1761 unsigned &PrefAlign) const { 1762 if (!isa<MemIntrinsic>(CI)) 1763 return false; 1764 MinSize = 8; 1765 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1 1766 // cycle faster than 4-byte aligned LDM. 1767 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4); 1768 return true; 1769 } 1770 1771 // Create a fast isel object. 1772 FastISel * 1773 ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo, 1774 const TargetLibraryInfo *libInfo) const { 1775 return ARM::createFastISel(funcInfo, libInfo); 1776 } 1777 1778 Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const { 1779 unsigned NumVals = N->getNumValues(); 1780 if (!NumVals) 1781 return Sched::RegPressure; 1782 1783 for (unsigned i = 0; i != NumVals; ++i) { 1784 EVT VT = N->getValueType(i); 1785 if (VT == MVT::Glue || VT == MVT::Other) 1786 continue; 1787 if (VT.isFloatingPoint() || VT.isVector()) 1788 return Sched::ILP; 1789 } 1790 1791 if (!N->isMachineOpcode()) 1792 return Sched::RegPressure; 1793 1794 // Load are scheduled for latency even if there instruction itinerary 1795 // is not available. 1796 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 1797 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode()); 1798 1799 if (MCID.getNumDefs() == 0) 1800 return Sched::RegPressure; 1801 if (!Itins->isEmpty() && 1802 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2) 1803 return Sched::ILP; 1804 1805 return Sched::RegPressure; 1806 } 1807 1808 //===----------------------------------------------------------------------===// 1809 // Lowering Code 1810 //===----------------------------------------------------------------------===// 1811 1812 static bool isSRL16(const SDValue &Op) { 1813 if (Op.getOpcode() != ISD::SRL) 1814 return false; 1815 if (auto Const = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 1816 return Const->getZExtValue() == 16; 1817 return false; 1818 } 1819 1820 static bool isSRA16(const SDValue &Op) { 1821 if (Op.getOpcode() != ISD::SRA) 1822 return false; 1823 if (auto Const = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 1824 return Const->getZExtValue() == 16; 1825 return false; 1826 } 1827 1828 static bool isSHL16(const SDValue &Op) { 1829 if (Op.getOpcode() != ISD::SHL) 1830 return false; 1831 if (auto Const = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 1832 return Const->getZExtValue() == 16; 1833 return false; 1834 } 1835 1836 // Check for a signed 16-bit value. We special case SRA because it makes it 1837 // more simple when also looking for SRAs that aren't sign extending a 1838 // smaller value. Without the check, we'd need to take extra care with 1839 // checking order for some operations. 1840 static bool isS16(const SDValue &Op, SelectionDAG &DAG) { 1841 if (isSRA16(Op)) 1842 return isSHL16(Op.getOperand(0)); 1843 return DAG.ComputeNumSignBits(Op) == 17; 1844 } 1845 1846 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC 1847 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) { 1848 switch (CC) { 1849 default: llvm_unreachable("Unknown condition code!"); 1850 case ISD::SETNE: return ARMCC::NE; 1851 case ISD::SETEQ: return ARMCC::EQ; 1852 case ISD::SETGT: return ARMCC::GT; 1853 case ISD::SETGE: return ARMCC::GE; 1854 case ISD::SETLT: return ARMCC::LT; 1855 case ISD::SETLE: return ARMCC::LE; 1856 case ISD::SETUGT: return ARMCC::HI; 1857 case ISD::SETUGE: return ARMCC::HS; 1858 case ISD::SETULT: return ARMCC::LO; 1859 case ISD::SETULE: return ARMCC::LS; 1860 } 1861 } 1862 1863 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. 1864 static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 1865 ARMCC::CondCodes &CondCode2) { 1866 CondCode2 = ARMCC::AL; 1867 switch (CC) { 1868 default: llvm_unreachable("Unknown FP condition!"); 1869 case ISD::SETEQ: 1870 case ISD::SETOEQ: CondCode = ARMCC::EQ; break; 1871 case ISD::SETGT: 1872 case ISD::SETOGT: CondCode = ARMCC::GT; break; 1873 case ISD::SETGE: 1874 case ISD::SETOGE: CondCode = ARMCC::GE; break; 1875 case ISD::SETOLT: CondCode = ARMCC::MI; break; 1876 case ISD::SETOLE: CondCode = ARMCC::LS; break; 1877 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break; 1878 case ISD::SETO: CondCode = ARMCC::VC; break; 1879 case ISD::SETUO: CondCode = ARMCC::VS; break; 1880 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break; 1881 case ISD::SETUGT: CondCode = ARMCC::HI; break; 1882 case ISD::SETUGE: CondCode = ARMCC::PL; break; 1883 case ISD::SETLT: 1884 case ISD::SETULT: CondCode = ARMCC::LT; break; 1885 case ISD::SETLE: 1886 case ISD::SETULE: CondCode = ARMCC::LE; break; 1887 case ISD::SETNE: 1888 case ISD::SETUNE: CondCode = ARMCC::NE; break; 1889 } 1890 } 1891 1892 //===----------------------------------------------------------------------===// 1893 // Calling Convention Implementation 1894 //===----------------------------------------------------------------------===// 1895 1896 /// getEffectiveCallingConv - Get the effective calling convention, taking into 1897 /// account presence of floating point hardware and calling convention 1898 /// limitations, such as support for variadic functions. 1899 CallingConv::ID 1900 ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC, 1901 bool isVarArg) const { 1902 switch (CC) { 1903 default: 1904 report_fatal_error("Unsupported calling convention"); 1905 case CallingConv::ARM_AAPCS: 1906 case CallingConv::ARM_APCS: 1907 case CallingConv::GHC: 1908 case CallingConv::CFGuard_Check: 1909 return CC; 1910 case CallingConv::PreserveMost: 1911 return CallingConv::PreserveMost; 1912 case CallingConv::ARM_AAPCS_VFP: 1913 case CallingConv::Swift: 1914 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP; 1915 case CallingConv::C: 1916 if (!Subtarget->isAAPCS_ABI()) 1917 return CallingConv::ARM_APCS; 1918 else if (Subtarget->hasVFP2Base() && !Subtarget->isThumb1Only() && 1919 getTargetMachine().Options.FloatABIType == FloatABI::Hard && 1920 !isVarArg) 1921 return CallingConv::ARM_AAPCS_VFP; 1922 else 1923 return CallingConv::ARM_AAPCS; 1924 case CallingConv::Fast: 1925 case CallingConv::CXX_FAST_TLS: 1926 if (!Subtarget->isAAPCS_ABI()) { 1927 if (Subtarget->hasVFP2Base() && !Subtarget->isThumb1Only() && !isVarArg) 1928 return CallingConv::Fast; 1929 return CallingConv::ARM_APCS; 1930 } else if (Subtarget->hasVFP2Base() && 1931 !Subtarget->isThumb1Only() && !isVarArg) 1932 return CallingConv::ARM_AAPCS_VFP; 1933 else 1934 return CallingConv::ARM_AAPCS; 1935 } 1936 } 1937 1938 CCAssignFn *ARMTargetLowering::CCAssignFnForCall(CallingConv::ID CC, 1939 bool isVarArg) const { 1940 return CCAssignFnForNode(CC, false, isVarArg); 1941 } 1942 1943 CCAssignFn *ARMTargetLowering::CCAssignFnForReturn(CallingConv::ID CC, 1944 bool isVarArg) const { 1945 return CCAssignFnForNode(CC, true, isVarArg); 1946 } 1947 1948 /// CCAssignFnForNode - Selects the correct CCAssignFn for the given 1949 /// CallingConvention. 1950 CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC, 1951 bool Return, 1952 bool isVarArg) const { 1953 switch (getEffectiveCallingConv(CC, isVarArg)) { 1954 default: 1955 report_fatal_error("Unsupported calling convention"); 1956 case CallingConv::ARM_APCS: 1957 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS); 1958 case CallingConv::ARM_AAPCS: 1959 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1960 case CallingConv::ARM_AAPCS_VFP: 1961 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1962 case CallingConv::Fast: 1963 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS); 1964 case CallingConv::GHC: 1965 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC); 1966 case CallingConv::PreserveMost: 1967 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1968 case CallingConv::CFGuard_Check: 1969 return (Return ? RetCC_ARM_AAPCS : CC_ARM_Win32_CFGuard_Check); 1970 } 1971 } 1972 1973 /// LowerCallResult - Lower the result values of a call into the 1974 /// appropriate copies out of appropriate physical registers. 1975 SDValue ARMTargetLowering::LowerCallResult( 1976 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg, 1977 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, 1978 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, bool isThisReturn, 1979 SDValue ThisVal) const { 1980 // Assign locations to each value returned by this call. 1981 SmallVector<CCValAssign, 16> RVLocs; 1982 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 1983 *DAG.getContext()); 1984 CCInfo.AnalyzeCallResult(Ins, CCAssignFnForReturn(CallConv, isVarArg)); 1985 1986 // Copy all of the result registers out of their specified physreg. 1987 for (unsigned i = 0; i != RVLocs.size(); ++i) { 1988 CCValAssign VA = RVLocs[i]; 1989 1990 // Pass 'this' value directly from the argument to return value, to avoid 1991 // reg unit interference 1992 if (i == 0 && isThisReturn) { 1993 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 && 1994 "unexpected return calling convention register assignment"); 1995 InVals.push_back(ThisVal); 1996 continue; 1997 } 1998 1999 SDValue Val; 2000 if (VA.needsCustom()) { 2001 // Handle f64 or half of a v2f64. 2002 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 2003 InFlag); 2004 Chain = Lo.getValue(1); 2005 InFlag = Lo.getValue(2); 2006 VA = RVLocs[++i]; // skip ahead to next loc 2007 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 2008 InFlag); 2009 Chain = Hi.getValue(1); 2010 InFlag = Hi.getValue(2); 2011 if (!Subtarget->isLittle()) 2012 std::swap (Lo, Hi); 2013 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 2014 2015 if (VA.getLocVT() == MVT::v2f64) { 2016 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 2017 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 2018 DAG.getConstant(0, dl, MVT::i32)); 2019 2020 VA = RVLocs[++i]; // skip ahead to next loc 2021 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 2022 Chain = Lo.getValue(1); 2023 InFlag = Lo.getValue(2); 2024 VA = RVLocs[++i]; // skip ahead to next loc 2025 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 2026 Chain = Hi.getValue(1); 2027 InFlag = Hi.getValue(2); 2028 if (!Subtarget->isLittle()) 2029 std::swap (Lo, Hi); 2030 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 2031 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 2032 DAG.getConstant(1, dl, MVT::i32)); 2033 } 2034 } else { 2035 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(), 2036 InFlag); 2037 Chain = Val.getValue(1); 2038 InFlag = Val.getValue(2); 2039 } 2040 2041 switch (VA.getLocInfo()) { 2042 default: llvm_unreachable("Unknown loc info!"); 2043 case CCValAssign::Full: break; 2044 case CCValAssign::BCvt: 2045 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val); 2046 break; 2047 } 2048 2049 InVals.push_back(Val); 2050 } 2051 2052 return Chain; 2053 } 2054 2055 /// LowerMemOpCallTo - Store the argument to the stack. 2056 SDValue ARMTargetLowering::LowerMemOpCallTo(SDValue Chain, SDValue StackPtr, 2057 SDValue Arg, const SDLoc &dl, 2058 SelectionDAG &DAG, 2059 const CCValAssign &VA, 2060 ISD::ArgFlagsTy Flags) const { 2061 unsigned LocMemOffset = VA.getLocMemOffset(); 2062 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); 2063 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()), 2064 StackPtr, PtrOff); 2065 return DAG.getStore( 2066 Chain, dl, Arg, PtrOff, 2067 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset)); 2068 } 2069 2070 void ARMTargetLowering::PassF64ArgInRegs(const SDLoc &dl, SelectionDAG &DAG, 2071 SDValue Chain, SDValue &Arg, 2072 RegsToPassVector &RegsToPass, 2073 CCValAssign &VA, CCValAssign &NextVA, 2074 SDValue &StackPtr, 2075 SmallVectorImpl<SDValue> &MemOpChains, 2076 ISD::ArgFlagsTy Flags) const { 2077 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 2078 DAG.getVTList(MVT::i32, MVT::i32), Arg); 2079 unsigned id = Subtarget->isLittle() ? 0 : 1; 2080 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id))); 2081 2082 if (NextVA.isRegLoc()) 2083 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id))); 2084 else { 2085 assert(NextVA.isMemLoc()); 2086 if (!StackPtr.getNode()) 2087 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, 2088 getPointerTy(DAG.getDataLayout())); 2089 2090 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id), 2091 dl, DAG, NextVA, 2092 Flags)); 2093 } 2094 } 2095 2096 /// LowerCall - Lowering a call into a callseq_start <- 2097 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter 2098 /// nodes. 2099 SDValue 2100 ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 2101 SmallVectorImpl<SDValue> &InVals) const { 2102 SelectionDAG &DAG = CLI.DAG; 2103 SDLoc &dl = CLI.DL; 2104 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 2105 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 2106 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 2107 SDValue Chain = CLI.Chain; 2108 SDValue Callee = CLI.Callee; 2109 bool &isTailCall = CLI.IsTailCall; 2110 CallingConv::ID CallConv = CLI.CallConv; 2111 bool doesNotRet = CLI.DoesNotReturn; 2112 bool isVarArg = CLI.IsVarArg; 2113 2114 MachineFunction &MF = DAG.getMachineFunction(); 2115 MachineFunction::CallSiteInfo CSInfo; 2116 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet(); 2117 bool isThisReturn = false; 2118 bool PreferIndirect = false; 2119 2120 // Disable tail calls if they're not supported. 2121 if (!Subtarget->supportsTailCall()) 2122 isTailCall = false; 2123 2124 if (isa<GlobalAddressSDNode>(Callee)) { 2125 // If we're optimizing for minimum size and the function is called three or 2126 // more times in this block, we can improve codesize by calling indirectly 2127 // as BLXr has a 16-bit encoding. 2128 auto *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal(); 2129 if (CLI.CS) { 2130 auto *BB = CLI.CS.getParent(); 2131 PreferIndirect = Subtarget->isThumb() && Subtarget->hasMinSize() && 2132 count_if(GV->users(), [&BB](const User *U) { 2133 return isa<Instruction>(U) && 2134 cast<Instruction>(U)->getParent() == BB; 2135 }) > 2; 2136 } 2137 } 2138 if (isTailCall) { 2139 // Check if it's really possible to do a tail call. 2140 isTailCall = IsEligibleForTailCallOptimization( 2141 Callee, CallConv, isVarArg, isStructRet, 2142 MF.getFunction().hasStructRetAttr(), Outs, OutVals, Ins, DAG, 2143 PreferIndirect); 2144 if (!isTailCall && CLI.CS && CLI.CS.isMustTailCall()) 2145 report_fatal_error("failed to perform tail call elimination on a call " 2146 "site marked musttail"); 2147 // We don't support GuaranteedTailCallOpt for ARM, only automatically 2148 // detected sibcalls. 2149 if (isTailCall) 2150 ++NumTailCalls; 2151 } 2152 2153 // Analyze operands of the call, assigning locations to each operand. 2154 SmallVector<CCValAssign, 16> ArgLocs; 2155 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 2156 *DAG.getContext()); 2157 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CallConv, isVarArg)); 2158 2159 // Get a count of how many bytes are to be pushed on the stack. 2160 unsigned NumBytes = CCInfo.getNextStackOffset(); 2161 2162 if (isTailCall) { 2163 // For tail calls, memory operands are available in our caller's stack. 2164 NumBytes = 0; 2165 } else { 2166 // Adjust the stack pointer for the new arguments... 2167 // These operations are automatically eliminated by the prolog/epilog pass 2168 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl); 2169 } 2170 2171 SDValue StackPtr = 2172 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout())); 2173 2174 RegsToPassVector RegsToPass; 2175 SmallVector<SDValue, 8> MemOpChains; 2176 2177 // Walk the register/memloc assignments, inserting copies/loads. In the case 2178 // of tail call optimization, arguments are handled later. 2179 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 2180 i != e; 2181 ++i, ++realArgIdx) { 2182 CCValAssign &VA = ArgLocs[i]; 2183 SDValue Arg = OutVals[realArgIdx]; 2184 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 2185 bool isByVal = Flags.isByVal(); 2186 2187 // Promote the value if needed. 2188 switch (VA.getLocInfo()) { 2189 default: llvm_unreachable("Unknown loc info!"); 2190 case CCValAssign::Full: break; 2191 case CCValAssign::SExt: 2192 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 2193 break; 2194 case CCValAssign::ZExt: 2195 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 2196 break; 2197 case CCValAssign::AExt: 2198 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 2199 break; 2200 case CCValAssign::BCvt: 2201 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 2202 break; 2203 } 2204 2205 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces 2206 if (VA.needsCustom()) { 2207 if (VA.getLocVT() == MVT::v2f64) { 2208 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2209 DAG.getConstant(0, dl, MVT::i32)); 2210 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2211 DAG.getConstant(1, dl, MVT::i32)); 2212 2213 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass, 2214 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 2215 2216 VA = ArgLocs[++i]; // skip ahead to next loc 2217 if (VA.isRegLoc()) { 2218 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass, 2219 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 2220 } else { 2221 assert(VA.isMemLoc()); 2222 2223 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1, 2224 dl, DAG, VA, Flags)); 2225 } 2226 } else { 2227 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i], 2228 StackPtr, MemOpChains, Flags); 2229 } 2230 } else if (VA.isRegLoc()) { 2231 if (realArgIdx == 0 && Flags.isReturned() && !Flags.isSwiftSelf() && 2232 Outs[0].VT == MVT::i32) { 2233 assert(VA.getLocVT() == MVT::i32 && 2234 "unexpected calling convention register assignment"); 2235 assert(!Ins.empty() && Ins[0].VT == MVT::i32 && 2236 "unexpected use of 'returned'"); 2237 isThisReturn = true; 2238 } 2239 const TargetOptions &Options = DAG.getTarget().Options; 2240 if (Options.EnableDebugEntryValues) 2241 CSInfo.emplace_back(VA.getLocReg(), i); 2242 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 2243 } else if (isByVal) { 2244 assert(VA.isMemLoc()); 2245 unsigned offset = 0; 2246 2247 // True if this byval aggregate will be split between registers 2248 // and memory. 2249 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount(); 2250 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed(); 2251 2252 if (CurByValIdx < ByValArgsCount) { 2253 2254 unsigned RegBegin, RegEnd; 2255 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd); 2256 2257 EVT PtrVT = 2258 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 2259 unsigned int i, j; 2260 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) { 2261 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32); 2262 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); 2263 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, 2264 MachinePointerInfo(), 2265 DAG.InferPtrAlignment(AddArg)); 2266 MemOpChains.push_back(Load.getValue(1)); 2267 RegsToPass.push_back(std::make_pair(j, Load)); 2268 } 2269 2270 // If parameter size outsides register area, "offset" value 2271 // helps us to calculate stack slot for remained part properly. 2272 offset = RegEnd - RegBegin; 2273 2274 CCInfo.nextInRegsParam(); 2275 } 2276 2277 if (Flags.getByValSize() > 4*offset) { 2278 auto PtrVT = getPointerTy(DAG.getDataLayout()); 2279 unsigned LocMemOffset = VA.getLocMemOffset(); 2280 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); 2281 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff); 2282 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl); 2283 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset); 2284 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl, 2285 MVT::i32); 2286 SDValue AlignNode = 2287 DAG.getConstant(Flags.getNonZeroByValAlign().value(), dl, MVT::i32); 2288 2289 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue); 2290 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode}; 2291 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs, 2292 Ops)); 2293 } 2294 } else if (!isTailCall) { 2295 assert(VA.isMemLoc()); 2296 2297 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg, 2298 dl, DAG, VA, Flags)); 2299 } 2300 } 2301 2302 if (!MemOpChains.empty()) 2303 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 2304 2305 // Build a sequence of copy-to-reg nodes chained together with token chain 2306 // and flag operands which copy the outgoing args into the appropriate regs. 2307 SDValue InFlag; 2308 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 2309 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 2310 RegsToPass[i].second, InFlag); 2311 InFlag = Chain.getValue(1); 2312 } 2313 2314 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every 2315 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 2316 // node so that legalize doesn't hack it. 2317 bool isDirect = false; 2318 2319 const TargetMachine &TM = getTargetMachine(); 2320 const Module *Mod = MF.getFunction().getParent(); 2321 const GlobalValue *GV = nullptr; 2322 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 2323 GV = G->getGlobal(); 2324 bool isStub = 2325 !TM.shouldAssumeDSOLocal(*Mod, GV) && Subtarget->isTargetMachO(); 2326 2327 bool isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass()); 2328 bool isLocalARMFunc = false; 2329 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2330 auto PtrVt = getPointerTy(DAG.getDataLayout()); 2331 2332 if (Subtarget->genLongCalls()) { 2333 assert((!isPositionIndependent() || Subtarget->isTargetWindows()) && 2334 "long-calls codegen is not position independent!"); 2335 // Handle a global address or an external symbol. If it's not one of 2336 // those, the target's already in a register, so we don't need to do 2337 // anything extra. 2338 if (isa<GlobalAddressSDNode>(Callee)) { 2339 // Create a constant pool entry for the callee address 2340 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2341 ARMConstantPoolValue *CPV = 2342 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0); 2343 2344 // Get the address of the callee into a register 2345 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4); 2346 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2347 Callee = DAG.getLoad( 2348 PtrVt, dl, DAG.getEntryNode(), CPAddr, 2349 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2350 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) { 2351 const char *Sym = S->getSymbol(); 2352 2353 // Create a constant pool entry for the callee address 2354 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2355 ARMConstantPoolValue *CPV = 2356 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 2357 ARMPCLabelIndex, 0); 2358 // Get the address of the callee into a register 2359 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4); 2360 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2361 Callee = DAG.getLoad( 2362 PtrVt, dl, DAG.getEntryNode(), CPAddr, 2363 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2364 } 2365 } else if (isa<GlobalAddressSDNode>(Callee)) { 2366 if (!PreferIndirect) { 2367 isDirect = true; 2368 bool isDef = GV->isStrongDefinitionForLinker(); 2369 2370 // ARM call to a local ARM function is predicable. 2371 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking); 2372 // tBX takes a register source operand. 2373 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 2374 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?"); 2375 Callee = DAG.getNode( 2376 ARMISD::WrapperPIC, dl, PtrVt, 2377 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY)); 2378 Callee = DAG.getLoad( 2379 PtrVt, dl, DAG.getEntryNode(), Callee, 2380 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 2381 /* Alignment = */ 0, MachineMemOperand::MODereferenceable | 2382 MachineMemOperand::MOInvariant); 2383 } else if (Subtarget->isTargetCOFF()) { 2384 assert(Subtarget->isTargetWindows() && 2385 "Windows is the only supported COFF target"); 2386 unsigned TargetFlags = ARMII::MO_NO_FLAG; 2387 if (GV->hasDLLImportStorageClass()) 2388 TargetFlags = ARMII::MO_DLLIMPORT; 2389 else if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV)) 2390 TargetFlags = ARMII::MO_COFFSTUB; 2391 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*offset=*/0, 2392 TargetFlags); 2393 if (TargetFlags & (ARMII::MO_DLLIMPORT | ARMII::MO_COFFSTUB)) 2394 Callee = 2395 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), 2396 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee), 2397 MachinePointerInfo::getGOT(DAG.getMachineFunction())); 2398 } else { 2399 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, 0); 2400 } 2401 } 2402 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 2403 isDirect = true; 2404 // tBX takes a register source operand. 2405 const char *Sym = S->getSymbol(); 2406 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 2407 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2408 ARMConstantPoolValue *CPV = 2409 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 2410 ARMPCLabelIndex, 4); 2411 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4); 2412 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2413 Callee = DAG.getLoad( 2414 PtrVt, dl, DAG.getEntryNode(), CPAddr, 2415 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2416 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 2417 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel); 2418 } else { 2419 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, 0); 2420 } 2421 } 2422 2423 // FIXME: handle tail calls differently. 2424 unsigned CallOpc; 2425 if (Subtarget->isThumb()) { 2426 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps()) 2427 CallOpc = ARMISD::CALL_NOLINK; 2428 else 2429 CallOpc = ARMISD::CALL; 2430 } else { 2431 if (!isDirect && !Subtarget->hasV5TOps()) 2432 CallOpc = ARMISD::CALL_NOLINK; 2433 else if (doesNotRet && isDirect && Subtarget->hasRetAddrStack() && 2434 // Emit regular call when code size is the priority 2435 !Subtarget->hasMinSize()) 2436 // "mov lr, pc; b _foo" to avoid confusing the RSP 2437 CallOpc = ARMISD::CALL_NOLINK; 2438 else 2439 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL; 2440 } 2441 2442 std::vector<SDValue> Ops; 2443 Ops.push_back(Chain); 2444 Ops.push_back(Callee); 2445 2446 // Add argument registers to the end of the list so that they are known live 2447 // into the call. 2448 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 2449 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 2450 RegsToPass[i].second.getValueType())); 2451 2452 // Add a register mask operand representing the call-preserved registers. 2453 if (!isTailCall) { 2454 const uint32_t *Mask; 2455 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo(); 2456 if (isThisReturn) { 2457 // For 'this' returns, use the R0-preserving mask if applicable 2458 Mask = ARI->getThisReturnPreservedMask(MF, CallConv); 2459 if (!Mask) { 2460 // Set isThisReturn to false if the calling convention is not one that 2461 // allows 'returned' to be modeled in this way, so LowerCallResult does 2462 // not try to pass 'this' straight through 2463 isThisReturn = false; 2464 Mask = ARI->getCallPreservedMask(MF, CallConv); 2465 } 2466 } else 2467 Mask = ARI->getCallPreservedMask(MF, CallConv); 2468 2469 assert(Mask && "Missing call preserved mask for calling convention"); 2470 Ops.push_back(DAG.getRegisterMask(Mask)); 2471 } 2472 2473 if (InFlag.getNode()) 2474 Ops.push_back(InFlag); 2475 2476 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 2477 if (isTailCall) { 2478 MF.getFrameInfo().setHasTailCall(); 2479 SDValue Ret = DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops); 2480 DAG.addCallSiteInfo(Ret.getNode(), std::move(CSInfo)); 2481 return Ret; 2482 } 2483 2484 // Returns a chain and a flag for retval copy to use. 2485 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops); 2486 InFlag = Chain.getValue(1); 2487 DAG.addCallSiteInfo(Chain.getNode(), std::move(CSInfo)); 2488 2489 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), 2490 DAG.getIntPtrConstant(0, dl, true), InFlag, dl); 2491 if (!Ins.empty()) 2492 InFlag = Chain.getValue(1); 2493 2494 // Handle result values, copying them out of physregs into vregs that we 2495 // return. 2496 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG, 2497 InVals, isThisReturn, 2498 isThisReturn ? OutVals[0] : SDValue()); 2499 } 2500 2501 /// HandleByVal - Every parameter *after* a byval parameter is passed 2502 /// on the stack. Remember the next parameter register to allocate, 2503 /// and then confiscate the rest of the parameter registers to insure 2504 /// this. 2505 void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size, 2506 unsigned Align) const { 2507 // Byval (as with any stack) slots are always at least 4 byte aligned. 2508 Align = std::max(Align, 4U); 2509 2510 unsigned Reg = State->AllocateReg(GPRArgRegs); 2511 if (!Reg) 2512 return; 2513 2514 unsigned AlignInRegs = Align / 4; 2515 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs; 2516 for (unsigned i = 0; i < Waste; ++i) 2517 Reg = State->AllocateReg(GPRArgRegs); 2518 2519 if (!Reg) 2520 return; 2521 2522 unsigned Excess = 4 * (ARM::R4 - Reg); 2523 2524 // Special case when NSAA != SP and parameter size greater than size of 2525 // all remained GPR regs. In that case we can't split parameter, we must 2526 // send it to stack. We also must set NCRN to R4, so waste all 2527 // remained registers. 2528 const unsigned NSAAOffset = State->getNextStackOffset(); 2529 if (NSAAOffset != 0 && Size > Excess) { 2530 while (State->AllocateReg(GPRArgRegs)) 2531 ; 2532 return; 2533 } 2534 2535 // First register for byval parameter is the first register that wasn't 2536 // allocated before this method call, so it would be "reg". 2537 // If parameter is small enough to be saved in range [reg, r4), then 2538 // the end (first after last) register would be reg + param-size-in-regs, 2539 // else parameter would be splitted between registers and stack, 2540 // end register would be r4 in this case. 2541 unsigned ByValRegBegin = Reg; 2542 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4); 2543 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd); 2544 // Note, first register is allocated in the beginning of function already, 2545 // allocate remained amount of registers we need. 2546 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i) 2547 State->AllocateReg(GPRArgRegs); 2548 // A byval parameter that is split between registers and memory needs its 2549 // size truncated here. 2550 // In the case where the entire structure fits in registers, we set the 2551 // size in memory to zero. 2552 Size = std::max<int>(Size - Excess, 0); 2553 } 2554 2555 /// MatchingStackOffset - Return true if the given stack call argument is 2556 /// already available in the same position (relatively) of the caller's 2557 /// incoming argument stack. 2558 static 2559 bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags, 2560 MachineFrameInfo &MFI, const MachineRegisterInfo *MRI, 2561 const TargetInstrInfo *TII) { 2562 unsigned Bytes = Arg.getValueSizeInBits() / 8; 2563 int FI = std::numeric_limits<int>::max(); 2564 if (Arg.getOpcode() == ISD::CopyFromReg) { 2565 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg(); 2566 if (!Register::isVirtualRegister(VR)) 2567 return false; 2568 MachineInstr *Def = MRI->getVRegDef(VR); 2569 if (!Def) 2570 return false; 2571 if (!Flags.isByVal()) { 2572 if (!TII->isLoadFromStackSlot(*Def, FI)) 2573 return false; 2574 } else { 2575 return false; 2576 } 2577 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) { 2578 if (Flags.isByVal()) 2579 // ByVal argument is passed in as a pointer but it's now being 2580 // dereferenced. e.g. 2581 // define @foo(%struct.X* %A) { 2582 // tail call @bar(%struct.X* byval %A) 2583 // } 2584 return false; 2585 SDValue Ptr = Ld->getBasePtr(); 2586 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr); 2587 if (!FINode) 2588 return false; 2589 FI = FINode->getIndex(); 2590 } else 2591 return false; 2592 2593 assert(FI != std::numeric_limits<int>::max()); 2594 if (!MFI.isFixedObjectIndex(FI)) 2595 return false; 2596 return Offset == MFI.getObjectOffset(FI) && Bytes == MFI.getObjectSize(FI); 2597 } 2598 2599 /// IsEligibleForTailCallOptimization - Check whether the call is eligible 2600 /// for tail call optimization. Targets which want to do tail call 2601 /// optimization should implement this function. 2602 bool ARMTargetLowering::IsEligibleForTailCallOptimization( 2603 SDValue Callee, CallingConv::ID CalleeCC, bool isVarArg, 2604 bool isCalleeStructRet, bool isCallerStructRet, 2605 const SmallVectorImpl<ISD::OutputArg> &Outs, 2606 const SmallVectorImpl<SDValue> &OutVals, 2607 const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG, 2608 const bool isIndirect) const { 2609 MachineFunction &MF = DAG.getMachineFunction(); 2610 const Function &CallerF = MF.getFunction(); 2611 CallingConv::ID CallerCC = CallerF.getCallingConv(); 2612 2613 assert(Subtarget->supportsTailCall()); 2614 2615 // Indirect tail calls cannot be optimized for Thumb1 if the args 2616 // to the call take up r0-r3. The reason is that there are no legal registers 2617 // left to hold the pointer to the function to be called. 2618 if (Subtarget->isThumb1Only() && Outs.size() >= 4 && 2619 (!isa<GlobalAddressSDNode>(Callee.getNode()) || isIndirect)) 2620 return false; 2621 2622 // Look for obvious safe cases to perform tail call optimization that do not 2623 // require ABI changes. This is what gcc calls sibcall. 2624 2625 // Exception-handling functions need a special set of instructions to indicate 2626 // a return to the hardware. Tail-calling another function would probably 2627 // break this. 2628 if (CallerF.hasFnAttribute("interrupt")) 2629 return false; 2630 2631 // Also avoid sibcall optimization if either caller or callee uses struct 2632 // return semantics. 2633 if (isCalleeStructRet || isCallerStructRet) 2634 return false; 2635 2636 // Externally-defined functions with weak linkage should not be 2637 // tail-called on ARM when the OS does not support dynamic 2638 // pre-emption of symbols, as the AAELF spec requires normal calls 2639 // to undefined weak functions to be replaced with a NOP or jump to the 2640 // next instruction. The behaviour of branch instructions in this 2641 // situation (as used for tail calls) is implementation-defined, so we 2642 // cannot rely on the linker replacing the tail call with a return. 2643 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 2644 const GlobalValue *GV = G->getGlobal(); 2645 const Triple &TT = getTargetMachine().getTargetTriple(); 2646 if (GV->hasExternalWeakLinkage() && 2647 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO())) 2648 return false; 2649 } 2650 2651 // Check that the call results are passed in the same way. 2652 LLVMContext &C = *DAG.getContext(); 2653 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins, 2654 CCAssignFnForReturn(CalleeCC, isVarArg), 2655 CCAssignFnForReturn(CallerCC, isVarArg))) 2656 return false; 2657 // The callee has to preserve all registers the caller needs to preserve. 2658 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo(); 2659 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC); 2660 if (CalleeCC != CallerCC) { 2661 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC); 2662 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved)) 2663 return false; 2664 } 2665 2666 // If Caller's vararg or byval argument has been split between registers and 2667 // stack, do not perform tail call, since part of the argument is in caller's 2668 // local frame. 2669 const ARMFunctionInfo *AFI_Caller = MF.getInfo<ARMFunctionInfo>(); 2670 if (AFI_Caller->getArgRegsSaveSize()) 2671 return false; 2672 2673 // If the callee takes no arguments then go on to check the results of the 2674 // call. 2675 if (!Outs.empty()) { 2676 // Check if stack adjustment is needed. For now, do not do this if any 2677 // argument is passed on the stack. 2678 SmallVector<CCValAssign, 16> ArgLocs; 2679 CCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C); 2680 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, isVarArg)); 2681 if (CCInfo.getNextStackOffset()) { 2682 // Check if the arguments are already laid out in the right way as 2683 // the caller's fixed stack objects. 2684 MachineFrameInfo &MFI = MF.getFrameInfo(); 2685 const MachineRegisterInfo *MRI = &MF.getRegInfo(); 2686 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 2687 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 2688 i != e; 2689 ++i, ++realArgIdx) { 2690 CCValAssign &VA = ArgLocs[i]; 2691 EVT RegVT = VA.getLocVT(); 2692 SDValue Arg = OutVals[realArgIdx]; 2693 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 2694 if (VA.getLocInfo() == CCValAssign::Indirect) 2695 return false; 2696 if (VA.needsCustom()) { 2697 // f64 and vector types are split into multiple registers or 2698 // register/stack-slot combinations. The types will not match 2699 // the registers; give up on memory f64 refs until we figure 2700 // out what to do about this. 2701 if (!VA.isRegLoc()) 2702 return false; 2703 if (!ArgLocs[++i].isRegLoc()) 2704 return false; 2705 if (RegVT == MVT::v2f64) { 2706 if (!ArgLocs[++i].isRegLoc()) 2707 return false; 2708 if (!ArgLocs[++i].isRegLoc()) 2709 return false; 2710 } 2711 } else if (!VA.isRegLoc()) { 2712 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags, 2713 MFI, MRI, TII)) 2714 return false; 2715 } 2716 } 2717 } 2718 2719 const MachineRegisterInfo &MRI = MF.getRegInfo(); 2720 if (!parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals)) 2721 return false; 2722 } 2723 2724 return true; 2725 } 2726 2727 bool 2728 ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv, 2729 MachineFunction &MF, bool isVarArg, 2730 const SmallVectorImpl<ISD::OutputArg> &Outs, 2731 LLVMContext &Context) const { 2732 SmallVector<CCValAssign, 16> RVLocs; 2733 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context); 2734 return CCInfo.CheckReturn(Outs, CCAssignFnForReturn(CallConv, isVarArg)); 2735 } 2736 2737 static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps, 2738 const SDLoc &DL, SelectionDAG &DAG) { 2739 const MachineFunction &MF = DAG.getMachineFunction(); 2740 const Function &F = MF.getFunction(); 2741 2742 StringRef IntKind = F.getFnAttribute("interrupt").getValueAsString(); 2743 2744 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset 2745 // version of the "preferred return address". These offsets affect the return 2746 // instruction if this is a return from PL1 without hypervisor extensions. 2747 // IRQ/FIQ: +4 "subs pc, lr, #4" 2748 // SWI: 0 "subs pc, lr, #0" 2749 // ABORT: +4 "subs pc, lr, #4" 2750 // UNDEF: +4/+2 "subs pc, lr, #0" 2751 // UNDEF varies depending on where the exception came from ARM or Thumb 2752 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0. 2753 2754 int64_t LROffset; 2755 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" || 2756 IntKind == "ABORT") 2757 LROffset = 4; 2758 else if (IntKind == "SWI" || IntKind == "UNDEF") 2759 LROffset = 0; 2760 else 2761 report_fatal_error("Unsupported interrupt attribute. If present, value " 2762 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF"); 2763 2764 RetOps.insert(RetOps.begin() + 1, 2765 DAG.getConstant(LROffset, DL, MVT::i32, false)); 2766 2767 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps); 2768 } 2769 2770 SDValue 2771 ARMTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 2772 bool isVarArg, 2773 const SmallVectorImpl<ISD::OutputArg> &Outs, 2774 const SmallVectorImpl<SDValue> &OutVals, 2775 const SDLoc &dl, SelectionDAG &DAG) const { 2776 // CCValAssign - represent the assignment of the return value to a location. 2777 SmallVector<CCValAssign, 16> RVLocs; 2778 2779 // CCState - Info about the registers and stack slots. 2780 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 2781 *DAG.getContext()); 2782 2783 // Analyze outgoing return values. 2784 CCInfo.AnalyzeReturn(Outs, CCAssignFnForReturn(CallConv, isVarArg)); 2785 2786 SDValue Flag; 2787 SmallVector<SDValue, 4> RetOps; 2788 RetOps.push_back(Chain); // Operand #0 = Chain (updated below) 2789 bool isLittleEndian = Subtarget->isLittle(); 2790 2791 MachineFunction &MF = DAG.getMachineFunction(); 2792 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2793 AFI->setReturnRegsCount(RVLocs.size()); 2794 2795 // Copy the result values into the output registers. 2796 for (unsigned i = 0, realRVLocIdx = 0; 2797 i != RVLocs.size(); 2798 ++i, ++realRVLocIdx) { 2799 CCValAssign &VA = RVLocs[i]; 2800 assert(VA.isRegLoc() && "Can only return in registers!"); 2801 2802 SDValue Arg = OutVals[realRVLocIdx]; 2803 bool ReturnF16 = false; 2804 2805 if (Subtarget->hasFullFP16() && Subtarget->isTargetHardFloat()) { 2806 // Half-precision return values can be returned like this: 2807 // 2808 // t11 f16 = fadd ... 2809 // t12: i16 = bitcast t11 2810 // t13: i32 = zero_extend t12 2811 // t14: f32 = bitcast t13 <~~~~~~~ Arg 2812 // 2813 // to avoid code generation for bitcasts, we simply set Arg to the node 2814 // that produces the f16 value, t11 in this case. 2815 // 2816 if (Arg.getValueType() == MVT::f32 && Arg.getOpcode() == ISD::BITCAST) { 2817 SDValue ZE = Arg.getOperand(0); 2818 if (ZE.getOpcode() == ISD::ZERO_EXTEND && ZE.getValueType() == MVT::i32) { 2819 SDValue BC = ZE.getOperand(0); 2820 if (BC.getOpcode() == ISD::BITCAST && BC.getValueType() == MVT::i16) { 2821 Arg = BC.getOperand(0); 2822 ReturnF16 = true; 2823 } 2824 } 2825 } 2826 } 2827 2828 switch (VA.getLocInfo()) { 2829 default: llvm_unreachable("Unknown loc info!"); 2830 case CCValAssign::Full: break; 2831 case CCValAssign::BCvt: 2832 if (!ReturnF16) 2833 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 2834 break; 2835 } 2836 2837 if (VA.needsCustom()) { 2838 if (VA.getLocVT() == MVT::v2f64) { 2839 // Extract the first half and return it in two registers. 2840 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2841 DAG.getConstant(0, dl, MVT::i32)); 2842 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl, 2843 DAG.getVTList(MVT::i32, MVT::i32), Half); 2844 2845 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2846 HalfGPRs.getValue(isLittleEndian ? 0 : 1), 2847 Flag); 2848 Flag = Chain.getValue(1); 2849 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2850 VA = RVLocs[++i]; // skip ahead to next loc 2851 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2852 HalfGPRs.getValue(isLittleEndian ? 1 : 0), 2853 Flag); 2854 Flag = Chain.getValue(1); 2855 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2856 VA = RVLocs[++i]; // skip ahead to next loc 2857 2858 // Extract the 2nd half and fall through to handle it as an f64 value. 2859 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2860 DAG.getConstant(1, dl, MVT::i32)); 2861 } 2862 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is 2863 // available. 2864 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 2865 DAG.getVTList(MVT::i32, MVT::i32), Arg); 2866 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2867 fmrrd.getValue(isLittleEndian ? 0 : 1), 2868 Flag); 2869 Flag = Chain.getValue(1); 2870 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2871 VA = RVLocs[++i]; // skip ahead to next loc 2872 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2873 fmrrd.getValue(isLittleEndian ? 1 : 0), 2874 Flag); 2875 } else 2876 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag); 2877 2878 // Guarantee that all emitted copies are 2879 // stuck together, avoiding something bad. 2880 Flag = Chain.getValue(1); 2881 RetOps.push_back(DAG.getRegister(VA.getLocReg(), 2882 ReturnF16 ? MVT::f16 : VA.getLocVT())); 2883 } 2884 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo(); 2885 const MCPhysReg *I = 2886 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction()); 2887 if (I) { 2888 for (; *I; ++I) { 2889 if (ARM::GPRRegClass.contains(*I)) 2890 RetOps.push_back(DAG.getRegister(*I, MVT::i32)); 2891 else if (ARM::DPRRegClass.contains(*I)) 2892 RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64))); 2893 else 2894 llvm_unreachable("Unexpected register class in CSRsViaCopy!"); 2895 } 2896 } 2897 2898 // Update chain and glue. 2899 RetOps[0] = Chain; 2900 if (Flag.getNode()) 2901 RetOps.push_back(Flag); 2902 2903 // CPUs which aren't M-class use a special sequence to return from 2904 // exceptions (roughly, any instruction setting pc and cpsr simultaneously, 2905 // though we use "subs pc, lr, #N"). 2906 // 2907 // M-class CPUs actually use a normal return sequence with a special 2908 // (hardware-provided) value in LR, so the normal code path works. 2909 if (DAG.getMachineFunction().getFunction().hasFnAttribute("interrupt") && 2910 !Subtarget->isMClass()) { 2911 if (Subtarget->isThumb1Only()) 2912 report_fatal_error("interrupt attribute is not supported in Thumb1"); 2913 return LowerInterruptReturn(RetOps, dl, DAG); 2914 } 2915 2916 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps); 2917 } 2918 2919 bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const { 2920 if (N->getNumValues() != 1) 2921 return false; 2922 if (!N->hasNUsesOfValue(1, 0)) 2923 return false; 2924 2925 SDValue TCChain = Chain; 2926 SDNode *Copy = *N->use_begin(); 2927 if (Copy->getOpcode() == ISD::CopyToReg) { 2928 // If the copy has a glue operand, we conservatively assume it isn't safe to 2929 // perform a tail call. 2930 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) 2931 return false; 2932 TCChain = Copy->getOperand(0); 2933 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) { 2934 SDNode *VMov = Copy; 2935 // f64 returned in a pair of GPRs. 2936 SmallPtrSet<SDNode*, 2> Copies; 2937 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 2938 UI != UE; ++UI) { 2939 if (UI->getOpcode() != ISD::CopyToReg) 2940 return false; 2941 Copies.insert(*UI); 2942 } 2943 if (Copies.size() > 2) 2944 return false; 2945 2946 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 2947 UI != UE; ++UI) { 2948 SDValue UseChain = UI->getOperand(0); 2949 if (Copies.count(UseChain.getNode())) 2950 // Second CopyToReg 2951 Copy = *UI; 2952 else { 2953 // We are at the top of this chain. 2954 // If the copy has a glue operand, we conservatively assume it 2955 // isn't safe to perform a tail call. 2956 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue) 2957 return false; 2958 // First CopyToReg 2959 TCChain = UseChain; 2960 } 2961 } 2962 } else if (Copy->getOpcode() == ISD::BITCAST) { 2963 // f32 returned in a single GPR. 2964 if (!Copy->hasOneUse()) 2965 return false; 2966 Copy = *Copy->use_begin(); 2967 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0)) 2968 return false; 2969 // If the copy has a glue operand, we conservatively assume it isn't safe to 2970 // perform a tail call. 2971 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) 2972 return false; 2973 TCChain = Copy->getOperand(0); 2974 } else { 2975 return false; 2976 } 2977 2978 bool HasRet = false; 2979 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end(); 2980 UI != UE; ++UI) { 2981 if (UI->getOpcode() != ARMISD::RET_FLAG && 2982 UI->getOpcode() != ARMISD::INTRET_FLAG) 2983 return false; 2984 HasRet = true; 2985 } 2986 2987 if (!HasRet) 2988 return false; 2989 2990 Chain = TCChain; 2991 return true; 2992 } 2993 2994 bool ARMTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const { 2995 if (!Subtarget->supportsTailCall()) 2996 return false; 2997 2998 if (!CI->isTailCall()) 2999 return false; 3000 3001 return true; 3002 } 3003 3004 // Trying to write a 64 bit value so need to split into two 32 bit values first, 3005 // and pass the lower and high parts through. 3006 static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) { 3007 SDLoc DL(Op); 3008 SDValue WriteValue = Op->getOperand(2); 3009 3010 // This function is only supposed to be called for i64 type argument. 3011 assert(WriteValue.getValueType() == MVT::i64 3012 && "LowerWRITE_REGISTER called for non-i64 type argument."); 3013 3014 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue, 3015 DAG.getConstant(0, DL, MVT::i32)); 3016 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue, 3017 DAG.getConstant(1, DL, MVT::i32)); 3018 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi }; 3019 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops); 3020 } 3021 3022 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as 3023 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is 3024 // one of the above mentioned nodes. It has to be wrapped because otherwise 3025 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only 3026 // be used to form addressing mode. These wrapped nodes will be selected 3027 // into MOVi. 3028 SDValue ARMTargetLowering::LowerConstantPool(SDValue Op, 3029 SelectionDAG &DAG) const { 3030 EVT PtrVT = Op.getValueType(); 3031 // FIXME there is no actual debug info here 3032 SDLoc dl(Op); 3033 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 3034 SDValue Res; 3035 3036 // When generating execute-only code Constant Pools must be promoted to the 3037 // global data section. It's a bit ugly that we can't share them across basic 3038 // blocks, but this way we guarantee that execute-only behaves correct with 3039 // position-independent addressing modes. 3040 if (Subtarget->genExecuteOnly()) { 3041 auto AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>(); 3042 auto T = const_cast<Type*>(CP->getType()); 3043 auto C = const_cast<Constant*>(CP->getConstVal()); 3044 auto M = const_cast<Module*>(DAG.getMachineFunction(). 3045 getFunction().getParent()); 3046 auto GV = new GlobalVariable( 3047 *M, T, /*isConstant=*/true, GlobalVariable::InternalLinkage, C, 3048 Twine(DAG.getDataLayout().getPrivateGlobalPrefix()) + "CP" + 3049 Twine(DAG.getMachineFunction().getFunctionNumber()) + "_" + 3050 Twine(AFI->createPICLabelUId()) 3051 ); 3052 SDValue GA = DAG.getTargetGlobalAddress(dyn_cast<GlobalValue>(GV), 3053 dl, PtrVT); 3054 return LowerGlobalAddress(GA, DAG); 3055 } 3056 3057 if (CP->isMachineConstantPoolEntry()) 3058 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, 3059 CP->getAlignment()); 3060 else 3061 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, 3062 CP->getAlignment()); 3063 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res); 3064 } 3065 3066 unsigned ARMTargetLowering::getJumpTableEncoding() const { 3067 return MachineJumpTableInfo::EK_Inline; 3068 } 3069 3070 SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, 3071 SelectionDAG &DAG) const { 3072 MachineFunction &MF = DAG.getMachineFunction(); 3073 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3074 unsigned ARMPCLabelIndex = 0; 3075 SDLoc DL(Op); 3076 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3077 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); 3078 SDValue CPAddr; 3079 bool IsPositionIndependent = isPositionIndependent() || Subtarget->isROPI(); 3080 if (!IsPositionIndependent) { 3081 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4); 3082 } else { 3083 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 3084 ARMPCLabelIndex = AFI->createPICLabelUId(); 3085 ARMConstantPoolValue *CPV = 3086 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex, 3087 ARMCP::CPBlockAddress, PCAdj); 3088 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 3089 } 3090 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr); 3091 SDValue Result = DAG.getLoad( 3092 PtrVT, DL, DAG.getEntryNode(), CPAddr, 3093 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3094 if (!IsPositionIndependent) 3095 return Result; 3096 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32); 3097 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel); 3098 } 3099 3100 /// Convert a TLS address reference into the correct sequence of loads 3101 /// and calls to compute the variable's address for Darwin, and return an 3102 /// SDValue containing the final node. 3103 3104 /// Darwin only has one TLS scheme which must be capable of dealing with the 3105 /// fully general situation, in the worst case. This means: 3106 /// + "extern __thread" declaration. 3107 /// + Defined in a possibly unknown dynamic library. 3108 /// 3109 /// The general system is that each __thread variable has a [3 x i32] descriptor 3110 /// which contains information used by the runtime to calculate the address. The 3111 /// only part of this the compiler needs to know about is the first word, which 3112 /// contains a function pointer that must be called with the address of the 3113 /// entire descriptor in "r0". 3114 /// 3115 /// Since this descriptor may be in a different unit, in general access must 3116 /// proceed along the usual ARM rules. A common sequence to produce is: 3117 /// 3118 /// movw rT1, :lower16:_var$non_lazy_ptr 3119 /// movt rT1, :upper16:_var$non_lazy_ptr 3120 /// ldr r0, [rT1] 3121 /// ldr rT2, [r0] 3122 /// blx rT2 3123 /// [...address now in r0...] 3124 SDValue 3125 ARMTargetLowering::LowerGlobalTLSAddressDarwin(SDValue Op, 3126 SelectionDAG &DAG) const { 3127 assert(Subtarget->isTargetDarwin() && 3128 "This function expects a Darwin target"); 3129 SDLoc DL(Op); 3130 3131 // First step is to get the address of the actua global symbol. This is where 3132 // the TLS descriptor lives. 3133 SDValue DescAddr = LowerGlobalAddressDarwin(Op, DAG); 3134 3135 // The first entry in the descriptor is a function pointer that we must call 3136 // to obtain the address of the variable. 3137 SDValue Chain = DAG.getEntryNode(); 3138 SDValue FuncTLVGet = DAG.getLoad( 3139 MVT::i32, DL, Chain, DescAddr, 3140 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 3141 /* Alignment = */ 4, 3142 MachineMemOperand::MONonTemporal | MachineMemOperand::MODereferenceable | 3143 MachineMemOperand::MOInvariant); 3144 Chain = FuncTLVGet.getValue(1); 3145 3146 MachineFunction &F = DAG.getMachineFunction(); 3147 MachineFrameInfo &MFI = F.getFrameInfo(); 3148 MFI.setAdjustsStack(true); 3149 3150 // TLS calls preserve all registers except those that absolutely must be 3151 // trashed: R0 (it takes an argument), LR (it's a call) and CPSR (let's not be 3152 // silly). 3153 auto TRI = 3154 getTargetMachine().getSubtargetImpl(F.getFunction())->getRegisterInfo(); 3155 auto ARI = static_cast<const ARMRegisterInfo *>(TRI); 3156 const uint32_t *Mask = ARI->getTLSCallPreservedMask(DAG.getMachineFunction()); 3157 3158 // Finally, we can make the call. This is just a degenerate version of a 3159 // normal AArch64 call node: r0 takes the address of the descriptor, and 3160 // returns the address of the variable in this thread. 3161 Chain = DAG.getCopyToReg(Chain, DL, ARM::R0, DescAddr, SDValue()); 3162 Chain = 3163 DAG.getNode(ARMISD::CALL, DL, DAG.getVTList(MVT::Other, MVT::Glue), 3164 Chain, FuncTLVGet, DAG.getRegister(ARM::R0, MVT::i32), 3165 DAG.getRegisterMask(Mask), Chain.getValue(1)); 3166 return DAG.getCopyFromReg(Chain, DL, ARM::R0, MVT::i32, Chain.getValue(1)); 3167 } 3168 3169 SDValue 3170 ARMTargetLowering::LowerGlobalTLSAddressWindows(SDValue Op, 3171 SelectionDAG &DAG) const { 3172 assert(Subtarget->isTargetWindows() && "Windows specific TLS lowering"); 3173 3174 SDValue Chain = DAG.getEntryNode(); 3175 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3176 SDLoc DL(Op); 3177 3178 // Load the current TEB (thread environment block) 3179 SDValue Ops[] = {Chain, 3180 DAG.getTargetConstant(Intrinsic::arm_mrc, DL, MVT::i32), 3181 DAG.getTargetConstant(15, DL, MVT::i32), 3182 DAG.getTargetConstant(0, DL, MVT::i32), 3183 DAG.getTargetConstant(13, DL, MVT::i32), 3184 DAG.getTargetConstant(0, DL, MVT::i32), 3185 DAG.getTargetConstant(2, DL, MVT::i32)}; 3186 SDValue CurrentTEB = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL, 3187 DAG.getVTList(MVT::i32, MVT::Other), Ops); 3188 3189 SDValue TEB = CurrentTEB.getValue(0); 3190 Chain = CurrentTEB.getValue(1); 3191 3192 // Load the ThreadLocalStoragePointer from the TEB 3193 // A pointer to the TLS array is located at offset 0x2c from the TEB. 3194 SDValue TLSArray = 3195 DAG.getNode(ISD::ADD, DL, PtrVT, TEB, DAG.getIntPtrConstant(0x2c, DL)); 3196 TLSArray = DAG.getLoad(PtrVT, DL, Chain, TLSArray, MachinePointerInfo()); 3197 3198 // The pointer to the thread's TLS data area is at the TLS Index scaled by 4 3199 // offset into the TLSArray. 3200 3201 // Load the TLS index from the C runtime 3202 SDValue TLSIndex = 3203 DAG.getTargetExternalSymbol("_tls_index", PtrVT, ARMII::MO_NO_FLAG); 3204 TLSIndex = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, TLSIndex); 3205 TLSIndex = DAG.getLoad(PtrVT, DL, Chain, TLSIndex, MachinePointerInfo()); 3206 3207 SDValue Slot = DAG.getNode(ISD::SHL, DL, PtrVT, TLSIndex, 3208 DAG.getConstant(2, DL, MVT::i32)); 3209 SDValue TLS = DAG.getLoad(PtrVT, DL, Chain, 3210 DAG.getNode(ISD::ADD, DL, PtrVT, TLSArray, Slot), 3211 MachinePointerInfo()); 3212 3213 // Get the offset of the start of the .tls section (section base) 3214 const auto *GA = cast<GlobalAddressSDNode>(Op); 3215 auto *CPV = ARMConstantPoolConstant::Create(GA->getGlobal(), ARMCP::SECREL); 3216 SDValue Offset = DAG.getLoad( 3217 PtrVT, DL, Chain, DAG.getNode(ARMISD::Wrapper, DL, MVT::i32, 3218 DAG.getTargetConstantPool(CPV, PtrVT, 4)), 3219 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3220 3221 return DAG.getNode(ISD::ADD, DL, PtrVT, TLS, Offset); 3222 } 3223 3224 // Lower ISD::GlobalTLSAddress using the "general dynamic" model 3225 SDValue 3226 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, 3227 SelectionDAG &DAG) const { 3228 SDLoc dl(GA); 3229 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3230 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 3231 MachineFunction &MF = DAG.getMachineFunction(); 3232 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3233 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 3234 ARMConstantPoolValue *CPV = 3235 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 3236 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true); 3237 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4); 3238 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument); 3239 Argument = DAG.getLoad( 3240 PtrVT, dl, DAG.getEntryNode(), Argument, 3241 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3242 SDValue Chain = Argument.getValue(1); 3243 3244 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 3245 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel); 3246 3247 // call __tls_get_addr. 3248 ArgListTy Args; 3249 ArgListEntry Entry; 3250 Entry.Node = Argument; 3251 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext()); 3252 Args.push_back(Entry); 3253 3254 // FIXME: is there useful debug info available here? 3255 TargetLowering::CallLoweringInfo CLI(DAG); 3256 CLI.setDebugLoc(dl).setChain(Chain).setLibCallee( 3257 CallingConv::C, Type::getInt32Ty(*DAG.getContext()), 3258 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args)); 3259 3260 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 3261 return CallResult.first; 3262 } 3263 3264 // Lower ISD::GlobalTLSAddress using the "initial exec" or 3265 // "local exec" model. 3266 SDValue 3267 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA, 3268 SelectionDAG &DAG, 3269 TLSModel::Model model) const { 3270 const GlobalValue *GV = GA->getGlobal(); 3271 SDLoc dl(GA); 3272 SDValue Offset; 3273 SDValue Chain = DAG.getEntryNode(); 3274 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3275 // Get the Thread Pointer 3276 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 3277 3278 if (model == TLSModel::InitialExec) { 3279 MachineFunction &MF = DAG.getMachineFunction(); 3280 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3281 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 3282 // Initial exec model. 3283 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 3284 ARMConstantPoolValue *CPV = 3285 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 3286 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, 3287 true); 3288 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 3289 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 3290 Offset = DAG.getLoad( 3291 PtrVT, dl, Chain, Offset, 3292 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3293 Chain = Offset.getValue(1); 3294 3295 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 3296 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel); 3297 3298 Offset = DAG.getLoad( 3299 PtrVT, dl, Chain, Offset, 3300 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3301 } else { 3302 // local exec model 3303 assert(model == TLSModel::LocalExec); 3304 ARMConstantPoolValue *CPV = 3305 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF); 3306 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 3307 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 3308 Offset = DAG.getLoad( 3309 PtrVT, dl, Chain, Offset, 3310 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3311 } 3312 3313 // The address of the thread local variable is the add of the thread 3314 // pointer with the offset of the variable. 3315 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset); 3316 } 3317 3318 SDValue 3319 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const { 3320 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 3321 if (DAG.getTarget().useEmulatedTLS()) 3322 return LowerToTLSEmulatedModel(GA, DAG); 3323 3324 if (Subtarget->isTargetDarwin()) 3325 return LowerGlobalTLSAddressDarwin(Op, DAG); 3326 3327 if (Subtarget->isTargetWindows()) 3328 return LowerGlobalTLSAddressWindows(Op, DAG); 3329 3330 // TODO: implement the "local dynamic" model 3331 assert(Subtarget->isTargetELF() && "Only ELF implemented here"); 3332 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal()); 3333 3334 switch (model) { 3335 case TLSModel::GeneralDynamic: 3336 case TLSModel::LocalDynamic: 3337 return LowerToTLSGeneralDynamicModel(GA, DAG); 3338 case TLSModel::InitialExec: 3339 case TLSModel::LocalExec: 3340 return LowerToTLSExecModels(GA, DAG, model); 3341 } 3342 llvm_unreachable("bogus TLS model"); 3343 } 3344 3345 /// Return true if all users of V are within function F, looking through 3346 /// ConstantExprs. 3347 static bool allUsersAreInFunction(const Value *V, const Function *F) { 3348 SmallVector<const User*,4> Worklist; 3349 for (auto *U : V->users()) 3350 Worklist.push_back(U); 3351 while (!Worklist.empty()) { 3352 auto *U = Worklist.pop_back_val(); 3353 if (isa<ConstantExpr>(U)) { 3354 for (auto *UU : U->users()) 3355 Worklist.push_back(UU); 3356 continue; 3357 } 3358 3359 auto *I = dyn_cast<Instruction>(U); 3360 if (!I || I->getParent()->getParent() != F) 3361 return false; 3362 } 3363 return true; 3364 } 3365 3366 static SDValue promoteToConstantPool(const ARMTargetLowering *TLI, 3367 const GlobalValue *GV, SelectionDAG &DAG, 3368 EVT PtrVT, const SDLoc &dl) { 3369 // If we're creating a pool entry for a constant global with unnamed address, 3370 // and the global is small enough, we can emit it inline into the constant pool 3371 // to save ourselves an indirection. 3372 // 3373 // This is a win if the constant is only used in one function (so it doesn't 3374 // need to be duplicated) or duplicating the constant wouldn't increase code 3375 // size (implying the constant is no larger than 4 bytes). 3376 const Function &F = DAG.getMachineFunction().getFunction(); 3377 3378 // We rely on this decision to inline being idemopotent and unrelated to the 3379 // use-site. We know that if we inline a variable at one use site, we'll 3380 // inline it elsewhere too (and reuse the constant pool entry). Fast-isel 3381 // doesn't know about this optimization, so bail out if it's enabled else 3382 // we could decide to inline here (and thus never emit the GV) but require 3383 // the GV from fast-isel generated code. 3384 if (!EnableConstpoolPromotion || 3385 DAG.getMachineFunction().getTarget().Options.EnableFastISel) 3386 return SDValue(); 3387 3388 auto *GVar = dyn_cast<GlobalVariable>(GV); 3389 if (!GVar || !GVar->hasInitializer() || 3390 !GVar->isConstant() || !GVar->hasGlobalUnnamedAddr() || 3391 !GVar->hasLocalLinkage()) 3392 return SDValue(); 3393 3394 // If we inline a value that contains relocations, we move the relocations 3395 // from .data to .text. This is not allowed in position-independent code. 3396 auto *Init = GVar->getInitializer(); 3397 if ((TLI->isPositionIndependent() || TLI->getSubtarget()->isROPI()) && 3398 Init->needsRelocation()) 3399 return SDValue(); 3400 3401 // The constant islands pass can only really deal with alignment requests 3402 // <= 4 bytes and cannot pad constants itself. Therefore we cannot promote 3403 // any type wanting greater alignment requirements than 4 bytes. We also 3404 // can only promote constants that are multiples of 4 bytes in size or 3405 // are paddable to a multiple of 4. Currently we only try and pad constants 3406 // that are strings for simplicity. 3407 auto *CDAInit = dyn_cast<ConstantDataArray>(Init); 3408 unsigned Size = DAG.getDataLayout().getTypeAllocSize(Init->getType()); 3409 unsigned Align = DAG.getDataLayout().getPreferredAlignment(GVar); 3410 unsigned RequiredPadding = 4 - (Size % 4); 3411 bool PaddingPossible = 3412 RequiredPadding == 4 || (CDAInit && CDAInit->isString()); 3413 if (!PaddingPossible || Align > 4 || Size > ConstpoolPromotionMaxSize || 3414 Size == 0) 3415 return SDValue(); 3416 3417 unsigned PaddedSize = Size + ((RequiredPadding == 4) ? 0 : RequiredPadding); 3418 MachineFunction &MF = DAG.getMachineFunction(); 3419 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3420 3421 // We can't bloat the constant pool too much, else the ConstantIslands pass 3422 // may fail to converge. If we haven't promoted this global yet (it may have 3423 // multiple uses), and promoting it would increase the constant pool size (Sz 3424 // > 4), ensure we have space to do so up to MaxTotal. 3425 if (!AFI->getGlobalsPromotedToConstantPool().count(GVar) && Size > 4) 3426 if (AFI->getPromotedConstpoolIncrease() + PaddedSize - 4 >= 3427 ConstpoolPromotionMaxTotal) 3428 return SDValue(); 3429 3430 // This is only valid if all users are in a single function; we can't clone 3431 // the constant in general. The LLVM IR unnamed_addr allows merging 3432 // constants, but not cloning them. 3433 // 3434 // We could potentially allow cloning if we could prove all uses of the 3435 // constant in the current function don't care about the address, like 3436 // printf format strings. But that isn't implemented for now. 3437 if (!allUsersAreInFunction(GVar, &F)) 3438 return SDValue(); 3439 3440 // We're going to inline this global. Pad it out if needed. 3441 if (RequiredPadding != 4) { 3442 StringRef S = CDAInit->getAsString(); 3443 3444 SmallVector<uint8_t,16> V(S.size()); 3445 std::copy(S.bytes_begin(), S.bytes_end(), V.begin()); 3446 while (RequiredPadding--) 3447 V.push_back(0); 3448 Init = ConstantDataArray::get(*DAG.getContext(), V); 3449 } 3450 3451 auto CPVal = ARMConstantPoolConstant::Create(GVar, Init); 3452 SDValue CPAddr = 3453 DAG.getTargetConstantPool(CPVal, PtrVT, /*Align=*/4); 3454 if (!AFI->getGlobalsPromotedToConstantPool().count(GVar)) { 3455 AFI->markGlobalAsPromotedToConstantPool(GVar); 3456 AFI->setPromotedConstpoolIncrease(AFI->getPromotedConstpoolIncrease() + 3457 PaddedSize - 4); 3458 } 3459 ++NumConstpoolPromoted; 3460 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 3461 } 3462 3463 bool ARMTargetLowering::isReadOnly(const GlobalValue *GV) const { 3464 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) 3465 if (!(GV = GA->getBaseObject())) 3466 return false; 3467 if (const auto *V = dyn_cast<GlobalVariable>(GV)) 3468 return V->isConstant(); 3469 return isa<Function>(GV); 3470 } 3471 3472 SDValue ARMTargetLowering::LowerGlobalAddress(SDValue Op, 3473 SelectionDAG &DAG) const { 3474 switch (Subtarget->getTargetTriple().getObjectFormat()) { 3475 default: llvm_unreachable("unknown object format"); 3476 case Triple::COFF: 3477 return LowerGlobalAddressWindows(Op, DAG); 3478 case Triple::ELF: 3479 return LowerGlobalAddressELF(Op, DAG); 3480 case Triple::MachO: 3481 return LowerGlobalAddressDarwin(Op, DAG); 3482 } 3483 } 3484 3485 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op, 3486 SelectionDAG &DAG) const { 3487 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3488 SDLoc dl(Op); 3489 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 3490 const TargetMachine &TM = getTargetMachine(); 3491 bool IsRO = isReadOnly(GV); 3492 3493 // promoteToConstantPool only if not generating XO text section 3494 if (TM.shouldAssumeDSOLocal(*GV->getParent(), GV) && !Subtarget->genExecuteOnly()) 3495 if (SDValue V = promoteToConstantPool(this, GV, DAG, PtrVT, dl)) 3496 return V; 3497 3498 if (isPositionIndependent()) { 3499 bool UseGOT_PREL = !TM.shouldAssumeDSOLocal(*GV->getParent(), GV); 3500 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 3501 UseGOT_PREL ? ARMII::MO_GOT : 0); 3502 SDValue Result = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVT, G); 3503 if (UseGOT_PREL) 3504 Result = 3505 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result, 3506 MachinePointerInfo::getGOT(DAG.getMachineFunction())); 3507 return Result; 3508 } else if (Subtarget->isROPI() && IsRO) { 3509 // PC-relative. 3510 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT); 3511 SDValue Result = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVT, G); 3512 return Result; 3513 } else if (Subtarget->isRWPI() && !IsRO) { 3514 // SB-relative. 3515 SDValue RelAddr; 3516 if (Subtarget->useMovt()) { 3517 ++NumMovwMovt; 3518 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_SBREL); 3519 RelAddr = DAG.getNode(ARMISD::Wrapper, dl, PtrVT, G); 3520 } else { // use literal pool for address constant 3521 ARMConstantPoolValue *CPV = 3522 ARMConstantPoolConstant::Create(GV, ARMCP::SBREL); 3523 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 3524 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 3525 RelAddr = DAG.getLoad( 3526 PtrVT, dl, DAG.getEntryNode(), CPAddr, 3527 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3528 } 3529 SDValue SB = DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::R9, PtrVT); 3530 SDValue Result = DAG.getNode(ISD::ADD, dl, PtrVT, SB, RelAddr); 3531 return Result; 3532 } 3533 3534 // If we have T2 ops, we can materialize the address directly via movt/movw 3535 // pair. This is always cheaper. 3536 if (Subtarget->useMovt()) { 3537 ++NumMovwMovt; 3538 // FIXME: Once remat is capable of dealing with instructions with register 3539 // operands, expand this into two nodes. 3540 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT, 3541 DAG.getTargetGlobalAddress(GV, dl, PtrVT)); 3542 } else { 3543 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4); 3544 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 3545 return DAG.getLoad( 3546 PtrVT, dl, DAG.getEntryNode(), CPAddr, 3547 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3548 } 3549 } 3550 3551 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op, 3552 SelectionDAG &DAG) const { 3553 assert(!Subtarget->isROPI() && !Subtarget->isRWPI() && 3554 "ROPI/RWPI not currently supported for Darwin"); 3555 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3556 SDLoc dl(Op); 3557 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 3558 3559 if (Subtarget->useMovt()) 3560 ++NumMovwMovt; 3561 3562 // FIXME: Once remat is capable of dealing with instructions with register 3563 // operands, expand this into multiple nodes 3564 unsigned Wrapper = 3565 isPositionIndependent() ? ARMISD::WrapperPIC : ARMISD::Wrapper; 3566 3567 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY); 3568 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G); 3569 3570 if (Subtarget->isGVIndirectSymbol(GV)) 3571 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result, 3572 MachinePointerInfo::getGOT(DAG.getMachineFunction())); 3573 return Result; 3574 } 3575 3576 SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op, 3577 SelectionDAG &DAG) const { 3578 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported"); 3579 assert(Subtarget->useMovt() && 3580 "Windows on ARM expects to use movw/movt"); 3581 assert(!Subtarget->isROPI() && !Subtarget->isRWPI() && 3582 "ROPI/RWPI not currently supported for Windows"); 3583 3584 const TargetMachine &TM = getTargetMachine(); 3585 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 3586 ARMII::TOF TargetFlags = ARMII::MO_NO_FLAG; 3587 if (GV->hasDLLImportStorageClass()) 3588 TargetFlags = ARMII::MO_DLLIMPORT; 3589 else if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV)) 3590 TargetFlags = ARMII::MO_COFFSTUB; 3591 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3592 SDValue Result; 3593 SDLoc DL(Op); 3594 3595 ++NumMovwMovt; 3596 3597 // FIXME: Once remat is capable of dealing with instructions with register 3598 // operands, expand this into two nodes. 3599 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, 3600 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*offset=*/0, 3601 TargetFlags)); 3602 if (TargetFlags & (ARMII::MO_DLLIMPORT | ARMII::MO_COFFSTUB)) 3603 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result, 3604 MachinePointerInfo::getGOT(DAG.getMachineFunction())); 3605 return Result; 3606 } 3607 3608 SDValue 3609 ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const { 3610 SDLoc dl(Op); 3611 SDValue Val = DAG.getConstant(0, dl, MVT::i32); 3612 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, 3613 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0), 3614 Op.getOperand(1), Val); 3615 } 3616 3617 SDValue 3618 ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const { 3619 SDLoc dl(Op); 3620 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0), 3621 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32)); 3622 } 3623 3624 SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op, 3625 SelectionDAG &DAG) const { 3626 SDLoc dl(Op); 3627 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other, 3628 Op.getOperand(0)); 3629 } 3630 3631 SDValue ARMTargetLowering::LowerINTRINSIC_VOID( 3632 SDValue Op, SelectionDAG &DAG, const ARMSubtarget *Subtarget) const { 3633 unsigned IntNo = 3634 cast<ConstantSDNode>( 3635 Op.getOperand(Op.getOperand(0).getValueType() == MVT::Other)) 3636 ->getZExtValue(); 3637 switch (IntNo) { 3638 default: 3639 return SDValue(); // Don't custom lower most intrinsics. 3640 case Intrinsic::arm_gnu_eabi_mcount: { 3641 MachineFunction &MF = DAG.getMachineFunction(); 3642 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3643 SDLoc dl(Op); 3644 SDValue Chain = Op.getOperand(0); 3645 // call "\01__gnu_mcount_nc" 3646 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo(); 3647 const uint32_t *Mask = 3648 ARI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C); 3649 assert(Mask && "Missing call preserved mask for calling convention"); 3650 // Mark LR an implicit live-in. 3651 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32)); 3652 SDValue ReturnAddress = 3653 DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, PtrVT); 3654 constexpr EVT ResultTys[] = {MVT::Other, MVT::Glue}; 3655 SDValue Callee = 3656 DAG.getTargetExternalSymbol("\01__gnu_mcount_nc", PtrVT, 0); 3657 SDValue RegisterMask = DAG.getRegisterMask(Mask); 3658 if (Subtarget->isThumb()) 3659 return SDValue( 3660 DAG.getMachineNode( 3661 ARM::tBL_PUSHLR, dl, ResultTys, 3662 {ReturnAddress, DAG.getTargetConstant(ARMCC::AL, dl, PtrVT), 3663 DAG.getRegister(0, PtrVT), Callee, RegisterMask, Chain}), 3664 0); 3665 return SDValue( 3666 DAG.getMachineNode(ARM::BL_PUSHLR, dl, ResultTys, 3667 {ReturnAddress, Callee, RegisterMask, Chain}), 3668 0); 3669 } 3670 } 3671 } 3672 3673 SDValue 3674 ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG, 3675 const ARMSubtarget *Subtarget) const { 3676 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 3677 SDLoc dl(Op); 3678 switch (IntNo) { 3679 default: return SDValue(); // Don't custom lower most intrinsics. 3680 case Intrinsic::thread_pointer: { 3681 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3682 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 3683 } 3684 case Intrinsic::arm_cls: { 3685 const SDValue &Operand = Op.getOperand(1); 3686 const EVT VTy = Op.getValueType(); 3687 SDValue SRA = 3688 DAG.getNode(ISD::SRA, dl, VTy, Operand, DAG.getConstant(31, dl, VTy)); 3689 SDValue XOR = DAG.getNode(ISD::XOR, dl, VTy, SRA, Operand); 3690 SDValue SHL = 3691 DAG.getNode(ISD::SHL, dl, VTy, XOR, DAG.getConstant(1, dl, VTy)); 3692 SDValue OR = 3693 DAG.getNode(ISD::OR, dl, VTy, SHL, DAG.getConstant(1, dl, VTy)); 3694 SDValue Result = DAG.getNode(ISD::CTLZ, dl, VTy, OR); 3695 return Result; 3696 } 3697 case Intrinsic::arm_cls64: { 3698 // cls(x) = if cls(hi(x)) != 31 then cls(hi(x)) 3699 // else 31 + clz(if hi(x) == 0 then lo(x) else not(lo(x))) 3700 const SDValue &Operand = Op.getOperand(1); 3701 const EVT VTy = Op.getValueType(); 3702 3703 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VTy, Operand, 3704 DAG.getConstant(1, dl, VTy)); 3705 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VTy, Operand, 3706 DAG.getConstant(0, dl, VTy)); 3707 SDValue Constant0 = DAG.getConstant(0, dl, VTy); 3708 SDValue Constant1 = DAG.getConstant(1, dl, VTy); 3709 SDValue Constant31 = DAG.getConstant(31, dl, VTy); 3710 SDValue SRAHi = DAG.getNode(ISD::SRA, dl, VTy, Hi, Constant31); 3711 SDValue XORHi = DAG.getNode(ISD::XOR, dl, VTy, SRAHi, Hi); 3712 SDValue SHLHi = DAG.getNode(ISD::SHL, dl, VTy, XORHi, Constant1); 3713 SDValue ORHi = DAG.getNode(ISD::OR, dl, VTy, SHLHi, Constant1); 3714 SDValue CLSHi = DAG.getNode(ISD::CTLZ, dl, VTy, ORHi); 3715 SDValue CheckLo = 3716 DAG.getSetCC(dl, MVT::i1, CLSHi, Constant31, ISD::CondCode::SETEQ); 3717 SDValue HiIsZero = 3718 DAG.getSetCC(dl, MVT::i1, Hi, Constant0, ISD::CondCode::SETEQ); 3719 SDValue AdjustedLo = 3720 DAG.getSelect(dl, VTy, HiIsZero, Lo, DAG.getNOT(dl, Lo, VTy)); 3721 SDValue CLZAdjustedLo = DAG.getNode(ISD::CTLZ, dl, VTy, AdjustedLo); 3722 SDValue Result = 3723 DAG.getSelect(dl, VTy, CheckLo, 3724 DAG.getNode(ISD::ADD, dl, VTy, CLZAdjustedLo, Constant31), CLSHi); 3725 return Result; 3726 } 3727 case Intrinsic::eh_sjlj_lsda: { 3728 MachineFunction &MF = DAG.getMachineFunction(); 3729 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3730 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 3731 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3732 SDValue CPAddr; 3733 bool IsPositionIndependent = isPositionIndependent(); 3734 unsigned PCAdj = IsPositionIndependent ? (Subtarget->isThumb() ? 4 : 8) : 0; 3735 ARMConstantPoolValue *CPV = 3736 ARMConstantPoolConstant::Create(&MF.getFunction(), ARMPCLabelIndex, 3737 ARMCP::CPLSDA, PCAdj); 3738 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 3739 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 3740 SDValue Result = DAG.getLoad( 3741 PtrVT, dl, DAG.getEntryNode(), CPAddr, 3742 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3743 3744 if (IsPositionIndependent) { 3745 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 3746 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 3747 } 3748 return Result; 3749 } 3750 case Intrinsic::arm_neon_vabs: 3751 return DAG.getNode(ISD::ABS, SDLoc(Op), Op.getValueType(), 3752 Op.getOperand(1)); 3753 case Intrinsic::arm_neon_vmulls: 3754 case Intrinsic::arm_neon_vmullu: { 3755 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls) 3756 ? ARMISD::VMULLs : ARMISD::VMULLu; 3757 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 3758 Op.getOperand(1), Op.getOperand(2)); 3759 } 3760 case Intrinsic::arm_neon_vminnm: 3761 case Intrinsic::arm_neon_vmaxnm: { 3762 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm) 3763 ? ISD::FMINNUM : ISD::FMAXNUM; 3764 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 3765 Op.getOperand(1), Op.getOperand(2)); 3766 } 3767 case Intrinsic::arm_neon_vminu: 3768 case Intrinsic::arm_neon_vmaxu: { 3769 if (Op.getValueType().isFloatingPoint()) 3770 return SDValue(); 3771 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu) 3772 ? ISD::UMIN : ISD::UMAX; 3773 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 3774 Op.getOperand(1), Op.getOperand(2)); 3775 } 3776 case Intrinsic::arm_neon_vmins: 3777 case Intrinsic::arm_neon_vmaxs: { 3778 // v{min,max}s is overloaded between signed integers and floats. 3779 if (!Op.getValueType().isFloatingPoint()) { 3780 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins) 3781 ? ISD::SMIN : ISD::SMAX; 3782 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 3783 Op.getOperand(1), Op.getOperand(2)); 3784 } 3785 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins) 3786 ? ISD::FMINIMUM : ISD::FMAXIMUM; 3787 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 3788 Op.getOperand(1), Op.getOperand(2)); 3789 } 3790 case Intrinsic::arm_neon_vtbl1: 3791 return DAG.getNode(ARMISD::VTBL1, SDLoc(Op), Op.getValueType(), 3792 Op.getOperand(1), Op.getOperand(2)); 3793 case Intrinsic::arm_neon_vtbl2: 3794 return DAG.getNode(ARMISD::VTBL2, SDLoc(Op), Op.getValueType(), 3795 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 3796 case Intrinsic::arm_mve_pred_i2v: 3797 case Intrinsic::arm_mve_pred_v2i: 3798 return DAG.getNode(ARMISD::PREDICATE_CAST, SDLoc(Op), Op.getValueType(), 3799 Op.getOperand(1)); 3800 case Intrinsic::arm_mve_vreinterpretq: 3801 return DAG.getNode(ARMISD::VECTOR_REG_CAST, SDLoc(Op), Op.getValueType(), 3802 Op.getOperand(1)); 3803 } 3804 } 3805 3806 static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG, 3807 const ARMSubtarget *Subtarget) { 3808 SDLoc dl(Op); 3809 ConstantSDNode *SSIDNode = cast<ConstantSDNode>(Op.getOperand(2)); 3810 auto SSID = static_cast<SyncScope::ID>(SSIDNode->getZExtValue()); 3811 if (SSID == SyncScope::SingleThread) 3812 return Op; 3813 3814 if (!Subtarget->hasDataBarrier()) { 3815 // Some ARMv6 cpus can support data barriers with an mcr instruction. 3816 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 3817 // here. 3818 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() && 3819 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!"); 3820 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0), 3821 DAG.getConstant(0, dl, MVT::i32)); 3822 } 3823 3824 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1)); 3825 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue()); 3826 ARM_MB::MemBOpt Domain = ARM_MB::ISH; 3827 if (Subtarget->isMClass()) { 3828 // Only a full system barrier exists in the M-class architectures. 3829 Domain = ARM_MB::SY; 3830 } else if (Subtarget->preferISHSTBarriers() && 3831 Ord == AtomicOrdering::Release) { 3832 // Swift happens to implement ISHST barriers in a way that's compatible with 3833 // Release semantics but weaker than ISH so we'd be fools not to use 3834 // it. Beware: other processors probably don't! 3835 Domain = ARM_MB::ISHST; 3836 } 3837 3838 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0), 3839 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32), 3840 DAG.getConstant(Domain, dl, MVT::i32)); 3841 } 3842 3843 static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG, 3844 const ARMSubtarget *Subtarget) { 3845 // ARM pre v5TE and Thumb1 does not have preload instructions. 3846 if (!(Subtarget->isThumb2() || 3847 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps()))) 3848 // Just preserve the chain. 3849 return Op.getOperand(0); 3850 3851 SDLoc dl(Op); 3852 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1; 3853 if (!isRead && 3854 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension())) 3855 // ARMv7 with MP extension has PLDW. 3856 return Op.getOperand(0); 3857 3858 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); 3859 if (Subtarget->isThumb()) { 3860 // Invert the bits. 3861 isRead = ~isRead & 1; 3862 isData = ~isData & 1; 3863 } 3864 3865 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0), 3866 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32), 3867 DAG.getConstant(isData, dl, MVT::i32)); 3868 } 3869 3870 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) { 3871 MachineFunction &MF = DAG.getMachineFunction(); 3872 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>(); 3873 3874 // vastart just stores the address of the VarArgsFrameIndex slot into the 3875 // memory location argument. 3876 SDLoc dl(Op); 3877 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 3878 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 3879 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 3880 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), 3881 MachinePointerInfo(SV)); 3882 } 3883 3884 SDValue ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, 3885 CCValAssign &NextVA, 3886 SDValue &Root, 3887 SelectionDAG &DAG, 3888 const SDLoc &dl) const { 3889 MachineFunction &MF = DAG.getMachineFunction(); 3890 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3891 3892 const TargetRegisterClass *RC; 3893 if (AFI->isThumb1OnlyFunction()) 3894 RC = &ARM::tGPRRegClass; 3895 else 3896 RC = &ARM::GPRRegClass; 3897 3898 // Transform the arguments stored in physical registers into virtual ones. 3899 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 3900 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 3901 3902 SDValue ArgValue2; 3903 if (NextVA.isMemLoc()) { 3904 MachineFrameInfo &MFI = MF.getFrameInfo(); 3905 int FI = MFI.CreateFixedObject(4, NextVA.getLocMemOffset(), true); 3906 3907 // Create load node to retrieve arguments from the stack. 3908 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 3909 ArgValue2 = DAG.getLoad( 3910 MVT::i32, dl, Root, FIN, 3911 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI)); 3912 } else { 3913 Reg = MF.addLiveIn(NextVA.getLocReg(), RC); 3914 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 3915 } 3916 if (!Subtarget->isLittle()) 3917 std::swap (ArgValue, ArgValue2); 3918 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2); 3919 } 3920 3921 // The remaining GPRs hold either the beginning of variable-argument 3922 // data, or the beginning of an aggregate passed by value (usually 3923 // byval). Either way, we allocate stack slots adjacent to the data 3924 // provided by our caller, and store the unallocated registers there. 3925 // If this is a variadic function, the va_list pointer will begin with 3926 // these values; otherwise, this reassembles a (byval) structure that 3927 // was split between registers and memory. 3928 // Return: The frame index registers were stored into. 3929 int ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG, 3930 const SDLoc &dl, SDValue &Chain, 3931 const Value *OrigArg, 3932 unsigned InRegsParamRecordIdx, 3933 int ArgOffset, unsigned ArgSize) const { 3934 // Currently, two use-cases possible: 3935 // Case #1. Non-var-args function, and we meet first byval parameter. 3936 // Setup first unallocated register as first byval register; 3937 // eat all remained registers 3938 // (these two actions are performed by HandleByVal method). 3939 // Then, here, we initialize stack frame with 3940 // "store-reg" instructions. 3941 // Case #2. Var-args function, that doesn't contain byval parameters. 3942 // The same: eat all remained unallocated registers, 3943 // initialize stack frame. 3944 3945 MachineFunction &MF = DAG.getMachineFunction(); 3946 MachineFrameInfo &MFI = MF.getFrameInfo(); 3947 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3948 unsigned RBegin, REnd; 3949 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) { 3950 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd); 3951 } else { 3952 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs); 3953 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx]; 3954 REnd = ARM::R4; 3955 } 3956 3957 if (REnd != RBegin) 3958 ArgOffset = -4 * (ARM::R4 - RBegin); 3959 3960 auto PtrVT = getPointerTy(DAG.getDataLayout()); 3961 int FrameIndex = MFI.CreateFixedObject(ArgSize, ArgOffset, false); 3962 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT); 3963 3964 SmallVector<SDValue, 4> MemOps; 3965 const TargetRegisterClass *RC = 3966 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass; 3967 3968 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) { 3969 unsigned VReg = MF.addLiveIn(Reg, RC); 3970 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); 3971 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, 3972 MachinePointerInfo(OrigArg, 4 * i)); 3973 MemOps.push_back(Store); 3974 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT)); 3975 } 3976 3977 if (!MemOps.empty()) 3978 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); 3979 return FrameIndex; 3980 } 3981 3982 // Setup stack frame, the va_list pointer will start from. 3983 void ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG, 3984 const SDLoc &dl, SDValue &Chain, 3985 unsigned ArgOffset, 3986 unsigned TotalArgRegsSaveSize, 3987 bool ForceMutable) const { 3988 MachineFunction &MF = DAG.getMachineFunction(); 3989 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3990 3991 // Try to store any remaining integer argument regs 3992 // to their spots on the stack so that they may be loaded by dereferencing 3993 // the result of va_next. 3994 // If there is no regs to be stored, just point address after last 3995 // argument passed via stack. 3996 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr, 3997 CCInfo.getInRegsParamsCount(), 3998 CCInfo.getNextStackOffset(), 3999 std::max(4U, TotalArgRegsSaveSize)); 4000 AFI->setVarArgsFrameIndex(FrameIndex); 4001 } 4002 4003 SDValue ARMTargetLowering::LowerFormalArguments( 4004 SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 4005 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, 4006 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 4007 MachineFunction &MF = DAG.getMachineFunction(); 4008 MachineFrameInfo &MFI = MF.getFrameInfo(); 4009 4010 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 4011 4012 // Assign locations to all of the incoming arguments. 4013 SmallVector<CCValAssign, 16> ArgLocs; 4014 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 4015 *DAG.getContext()); 4016 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForCall(CallConv, isVarArg)); 4017 4018 SmallVector<SDValue, 16> ArgValues; 4019 SDValue ArgValue; 4020 Function::const_arg_iterator CurOrigArg = MF.getFunction().arg_begin(); 4021 unsigned CurArgIdx = 0; 4022 4023 // Initially ArgRegsSaveSize is zero. 4024 // Then we increase this value each time we meet byval parameter. 4025 // We also increase this value in case of varargs function. 4026 AFI->setArgRegsSaveSize(0); 4027 4028 // Calculate the amount of stack space that we need to allocate to store 4029 // byval and variadic arguments that are passed in registers. 4030 // We need to know this before we allocate the first byval or variadic 4031 // argument, as they will be allocated a stack slot below the CFA (Canonical 4032 // Frame Address, the stack pointer at entry to the function). 4033 unsigned ArgRegBegin = ARM::R4; 4034 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 4035 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount()) 4036 break; 4037 4038 CCValAssign &VA = ArgLocs[i]; 4039 unsigned Index = VA.getValNo(); 4040 ISD::ArgFlagsTy Flags = Ins[Index].Flags; 4041 if (!Flags.isByVal()) 4042 continue; 4043 4044 assert(VA.isMemLoc() && "unexpected byval pointer in reg"); 4045 unsigned RBegin, REnd; 4046 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd); 4047 ArgRegBegin = std::min(ArgRegBegin, RBegin); 4048 4049 CCInfo.nextInRegsParam(); 4050 } 4051 CCInfo.rewindByValRegsInfo(); 4052 4053 int lastInsIndex = -1; 4054 if (isVarArg && MFI.hasVAStart()) { 4055 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs); 4056 if (RegIdx != array_lengthof(GPRArgRegs)) 4057 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]); 4058 } 4059 4060 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin); 4061 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize); 4062 auto PtrVT = getPointerTy(DAG.getDataLayout()); 4063 4064 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 4065 CCValAssign &VA = ArgLocs[i]; 4066 if (Ins[VA.getValNo()].isOrigArg()) { 4067 std::advance(CurOrigArg, 4068 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx); 4069 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex(); 4070 } 4071 // Arguments stored in registers. 4072 if (VA.isRegLoc()) { 4073 EVT RegVT = VA.getLocVT(); 4074 4075 if (VA.needsCustom()) { 4076 // f64 and vector types are split up into multiple registers or 4077 // combinations of registers and stack slots. 4078 if (VA.getLocVT() == MVT::v2f64) { 4079 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i], 4080 Chain, DAG, dl); 4081 VA = ArgLocs[++i]; // skip ahead to next loc 4082 SDValue ArgValue2; 4083 if (VA.isMemLoc()) { 4084 int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), true); 4085 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 4086 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN, 4087 MachinePointerInfo::getFixedStack( 4088 DAG.getMachineFunction(), FI)); 4089 } else { 4090 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i], 4091 Chain, DAG, dl); 4092 } 4093 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 4094 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 4095 ArgValue, ArgValue1, 4096 DAG.getIntPtrConstant(0, dl)); 4097 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 4098 ArgValue, ArgValue2, 4099 DAG.getIntPtrConstant(1, dl)); 4100 } else 4101 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl); 4102 } else { 4103 const TargetRegisterClass *RC; 4104 4105 4106 if (RegVT == MVT::f16) 4107 RC = &ARM::HPRRegClass; 4108 else if (RegVT == MVT::f32) 4109 RC = &ARM::SPRRegClass; 4110 else if (RegVT == MVT::f64 || RegVT == MVT::v4f16) 4111 RC = &ARM::DPRRegClass; 4112 else if (RegVT == MVT::v2f64 || RegVT == MVT::v8f16) 4113 RC = &ARM::QPRRegClass; 4114 else if (RegVT == MVT::i32) 4115 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass 4116 : &ARM::GPRRegClass; 4117 else 4118 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering"); 4119 4120 // Transform the arguments in physical registers into virtual ones. 4121 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 4122 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); 4123 4124 // If this value is passed in r0 and has the returned attribute (e.g. 4125 // C++ 'structors), record this fact for later use. 4126 if (VA.getLocReg() == ARM::R0 && Ins[VA.getValNo()].Flags.isReturned()) { 4127 AFI->setPreservesR0(); 4128 } 4129 } 4130 4131 // If this is an 8 or 16-bit value, it is really passed promoted 4132 // to 32 bits. Insert an assert[sz]ext to capture this, then 4133 // truncate to the right size. 4134 switch (VA.getLocInfo()) { 4135 default: llvm_unreachable("Unknown loc info!"); 4136 case CCValAssign::Full: break; 4137 case CCValAssign::BCvt: 4138 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue); 4139 break; 4140 case CCValAssign::SExt: 4141 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, 4142 DAG.getValueType(VA.getValVT())); 4143 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 4144 break; 4145 case CCValAssign::ZExt: 4146 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, 4147 DAG.getValueType(VA.getValVT())); 4148 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 4149 break; 4150 } 4151 4152 InVals.push_back(ArgValue); 4153 } else { // VA.isRegLoc() 4154 // sanity check 4155 assert(VA.isMemLoc()); 4156 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered"); 4157 4158 int index = VA.getValNo(); 4159 4160 // Some Ins[] entries become multiple ArgLoc[] entries. 4161 // Process them only once. 4162 if (index != lastInsIndex) 4163 { 4164 ISD::ArgFlagsTy Flags = Ins[index].Flags; 4165 // FIXME: For now, all byval parameter objects are marked mutable. 4166 // This can be changed with more analysis. 4167 // In case of tail call optimization mark all arguments mutable. 4168 // Since they could be overwritten by lowering of arguments in case of 4169 // a tail call. 4170 if (Flags.isByVal()) { 4171 assert(Ins[index].isOrigArg() && 4172 "Byval arguments cannot be implicit"); 4173 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed(); 4174 4175 int FrameIndex = StoreByValRegs( 4176 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex, 4177 VA.getLocMemOffset(), Flags.getByValSize()); 4178 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT)); 4179 CCInfo.nextInRegsParam(); 4180 } else { 4181 unsigned FIOffset = VA.getLocMemOffset(); 4182 int FI = MFI.CreateFixedObject(VA.getLocVT().getSizeInBits()/8, 4183 FIOffset, true); 4184 4185 // Create load nodes to retrieve arguments from the stack. 4186 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 4187 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, 4188 MachinePointerInfo::getFixedStack( 4189 DAG.getMachineFunction(), FI))); 4190 } 4191 lastInsIndex = index; 4192 } 4193 } 4194 } 4195 4196 // varargs 4197 if (isVarArg && MFI.hasVAStart()) 4198 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 4199 CCInfo.getNextStackOffset(), 4200 TotalArgRegsSaveSize); 4201 4202 AFI->setArgumentStackSize(CCInfo.getNextStackOffset()); 4203 4204 return Chain; 4205 } 4206 4207 /// isFloatingPointZero - Return true if this is +0.0. 4208 static bool isFloatingPointZero(SDValue Op) { 4209 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) 4210 return CFP->getValueAPF().isPosZero(); 4211 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) { 4212 // Maybe this has already been legalized into the constant pool? 4213 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) { 4214 SDValue WrapperOp = Op.getOperand(1).getOperand(0); 4215 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp)) 4216 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) 4217 return CFP->getValueAPF().isPosZero(); 4218 } 4219 } else if (Op->getOpcode() == ISD::BITCAST && 4220 Op->getValueType(0) == MVT::f64) { 4221 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64) 4222 // created by LowerConstantFP(). 4223 SDValue BitcastOp = Op->getOperand(0); 4224 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM && 4225 isNullConstant(BitcastOp->getOperand(0))) 4226 return true; 4227 } 4228 return false; 4229 } 4230 4231 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for 4232 /// the given operands. 4233 SDValue ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC, 4234 SDValue &ARMcc, SelectionDAG &DAG, 4235 const SDLoc &dl) const { 4236 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) { 4237 unsigned C = RHSC->getZExtValue(); 4238 if (!isLegalICmpImmediate((int32_t)C)) { 4239 // Constant does not fit, try adjusting it by one. 4240 switch (CC) { 4241 default: break; 4242 case ISD::SETLT: 4243 case ISD::SETGE: 4244 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) { 4245 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT; 4246 RHS = DAG.getConstant(C - 1, dl, MVT::i32); 4247 } 4248 break; 4249 case ISD::SETULT: 4250 case ISD::SETUGE: 4251 if (C != 0 && isLegalICmpImmediate(C-1)) { 4252 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT; 4253 RHS = DAG.getConstant(C - 1, dl, MVT::i32); 4254 } 4255 break; 4256 case ISD::SETLE: 4257 case ISD::SETGT: 4258 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) { 4259 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE; 4260 RHS = DAG.getConstant(C + 1, dl, MVT::i32); 4261 } 4262 break; 4263 case ISD::SETULE: 4264 case ISD::SETUGT: 4265 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) { 4266 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE; 4267 RHS = DAG.getConstant(C + 1, dl, MVT::i32); 4268 } 4269 break; 4270 } 4271 } 4272 } else if ((ARM_AM::getShiftOpcForNode(LHS.getOpcode()) != ARM_AM::no_shift) && 4273 (ARM_AM::getShiftOpcForNode(RHS.getOpcode()) == ARM_AM::no_shift)) { 4274 // In ARM and Thumb-2, the compare instructions can shift their second 4275 // operand. 4276 CC = ISD::getSetCCSwappedOperands(CC); 4277 std::swap(LHS, RHS); 4278 } 4279 4280 // Thumb1 has very limited immediate modes, so turning an "and" into a 4281 // shift can save multiple instructions. 4282 // 4283 // If we have (x & C1), and C1 is an appropriate mask, we can transform it 4284 // into "((x << n) >> n)". But that isn't necessarily profitable on its 4285 // own. If it's the operand to an unsigned comparison with an immediate, 4286 // we can eliminate one of the shifts: we transform 4287 // "((x << n) >> n) == C2" to "(x << n) == (C2 << n)". 4288 // 4289 // We avoid transforming cases which aren't profitable due to encoding 4290 // details: 4291 // 4292 // 1. C2 fits into the immediate field of a cmp, and the transformed version 4293 // would not; in that case, we're essentially trading one immediate load for 4294 // another. 4295 // 2. C1 is 255 or 65535, so we can use uxtb or uxth. 4296 // 3. C2 is zero; we have other code for this special case. 4297 // 4298 // FIXME: Figure out profitability for Thumb2; we usually can't save an 4299 // instruction, since the AND is always one instruction anyway, but we could 4300 // use narrow instructions in some cases. 4301 if (Subtarget->isThumb1Only() && LHS->getOpcode() == ISD::AND && 4302 LHS->hasOneUse() && isa<ConstantSDNode>(LHS.getOperand(1)) && 4303 LHS.getValueType() == MVT::i32 && isa<ConstantSDNode>(RHS) && 4304 !isSignedIntSetCC(CC)) { 4305 unsigned Mask = cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue(); 4306 auto *RHSC = cast<ConstantSDNode>(RHS.getNode()); 4307 uint64_t RHSV = RHSC->getZExtValue(); 4308 if (isMask_32(Mask) && (RHSV & ~Mask) == 0 && Mask != 255 && Mask != 65535) { 4309 unsigned ShiftBits = countLeadingZeros(Mask); 4310 if (RHSV && (RHSV > 255 || (RHSV << ShiftBits) <= 255)) { 4311 SDValue ShiftAmt = DAG.getConstant(ShiftBits, dl, MVT::i32); 4312 LHS = DAG.getNode(ISD::SHL, dl, MVT::i32, LHS.getOperand(0), ShiftAmt); 4313 RHS = DAG.getConstant(RHSV << ShiftBits, dl, MVT::i32); 4314 } 4315 } 4316 } 4317 4318 // The specific comparison "(x<<c) > 0x80000000U" can be optimized to a 4319 // single "lsls x, c+1". The shift sets the "C" and "Z" flags the same 4320 // way a cmp would. 4321 // FIXME: Add support for ARM/Thumb2; this would need isel patterns, and 4322 // some tweaks to the heuristics for the previous and->shift transform. 4323 // FIXME: Optimize cases where the LHS isn't a shift. 4324 if (Subtarget->isThumb1Only() && LHS->getOpcode() == ISD::SHL && 4325 isa<ConstantSDNode>(RHS) && 4326 cast<ConstantSDNode>(RHS)->getZExtValue() == 0x80000000U && 4327 CC == ISD::SETUGT && isa<ConstantSDNode>(LHS.getOperand(1)) && 4328 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() < 31) { 4329 unsigned ShiftAmt = 4330 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() + 1; 4331 SDValue Shift = DAG.getNode(ARMISD::LSLS, dl, 4332 DAG.getVTList(MVT::i32, MVT::i32), 4333 LHS.getOperand(0), 4334 DAG.getConstant(ShiftAmt, dl, MVT::i32)); 4335 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, ARM::CPSR, 4336 Shift.getValue(1), SDValue()); 4337 ARMcc = DAG.getConstant(ARMCC::HI, dl, MVT::i32); 4338 return Chain.getValue(1); 4339 } 4340 4341 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 4342 4343 // If the RHS is a constant zero then the V (overflow) flag will never be 4344 // set. This can allow us to simplify GE to PL or LT to MI, which can be 4345 // simpler for other passes (like the peephole optimiser) to deal with. 4346 if (isNullConstant(RHS)) { 4347 switch (CondCode) { 4348 default: break; 4349 case ARMCC::GE: 4350 CondCode = ARMCC::PL; 4351 break; 4352 case ARMCC::LT: 4353 CondCode = ARMCC::MI; 4354 break; 4355 } 4356 } 4357 4358 ARMISD::NodeType CompareType; 4359 switch (CondCode) { 4360 default: 4361 CompareType = ARMISD::CMP; 4362 break; 4363 case ARMCC::EQ: 4364 case ARMCC::NE: 4365 // Uses only Z Flag 4366 CompareType = ARMISD::CMPZ; 4367 break; 4368 } 4369 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 4370 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS); 4371 } 4372 4373 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands. 4374 SDValue ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, 4375 SelectionDAG &DAG, const SDLoc &dl, 4376 bool Signaling) const { 4377 assert(Subtarget->hasFP64() || RHS.getValueType() != MVT::f64); 4378 SDValue Cmp; 4379 if (!isFloatingPointZero(RHS)) 4380 Cmp = DAG.getNode(Signaling ? ARMISD::CMPFPE : ARMISD::CMPFP, 4381 dl, MVT::Glue, LHS, RHS); 4382 else 4383 Cmp = DAG.getNode(Signaling ? ARMISD::CMPFPEw0 : ARMISD::CMPFPw0, 4384 dl, MVT::Glue, LHS); 4385 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp); 4386 } 4387 4388 /// duplicateCmp - Glue values can have only one use, so this function 4389 /// duplicates a comparison node. 4390 SDValue 4391 ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const { 4392 unsigned Opc = Cmp.getOpcode(); 4393 SDLoc DL(Cmp); 4394 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ) 4395 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 4396 4397 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation"); 4398 Cmp = Cmp.getOperand(0); 4399 Opc = Cmp.getOpcode(); 4400 if (Opc == ARMISD::CMPFP) 4401 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 4402 else { 4403 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT"); 4404 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0)); 4405 } 4406 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp); 4407 } 4408 4409 // This function returns three things: the arithmetic computation itself 4410 // (Value), a comparison (OverflowCmp), and a condition code (ARMcc). The 4411 // comparison and the condition code define the case in which the arithmetic 4412 // computation *does not* overflow. 4413 std::pair<SDValue, SDValue> 4414 ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG, 4415 SDValue &ARMcc) const { 4416 assert(Op.getValueType() == MVT::i32 && "Unsupported value type"); 4417 4418 SDValue Value, OverflowCmp; 4419 SDValue LHS = Op.getOperand(0); 4420 SDValue RHS = Op.getOperand(1); 4421 SDLoc dl(Op); 4422 4423 // FIXME: We are currently always generating CMPs because we don't support 4424 // generating CMN through the backend. This is not as good as the natural 4425 // CMP case because it causes a register dependency and cannot be folded 4426 // later. 4427 4428 switch (Op.getOpcode()) { 4429 default: 4430 llvm_unreachable("Unknown overflow instruction!"); 4431 case ISD::SADDO: 4432 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32); 4433 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS); 4434 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS); 4435 break; 4436 case ISD::UADDO: 4437 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32); 4438 // We use ADDC here to correspond to its use in LowerUnsignedALUO. 4439 // We do not use it in the USUBO case as Value may not be used. 4440 Value = DAG.getNode(ARMISD::ADDC, dl, 4441 DAG.getVTList(Op.getValueType(), MVT::i32), LHS, RHS) 4442 .getValue(0); 4443 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS); 4444 break; 4445 case ISD::SSUBO: 4446 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32); 4447 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS); 4448 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS); 4449 break; 4450 case ISD::USUBO: 4451 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32); 4452 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS); 4453 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS); 4454 break; 4455 case ISD::UMULO: 4456 // We generate a UMUL_LOHI and then check if the high word is 0. 4457 ARMcc = DAG.getConstant(ARMCC::EQ, dl, MVT::i32); 4458 Value = DAG.getNode(ISD::UMUL_LOHI, dl, 4459 DAG.getVTList(Op.getValueType(), Op.getValueType()), 4460 LHS, RHS); 4461 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value.getValue(1), 4462 DAG.getConstant(0, dl, MVT::i32)); 4463 Value = Value.getValue(0); // We only want the low 32 bits for the result. 4464 break; 4465 case ISD::SMULO: 4466 // We generate a SMUL_LOHI and then check if all the bits of the high word 4467 // are the same as the sign bit of the low word. 4468 ARMcc = DAG.getConstant(ARMCC::EQ, dl, MVT::i32); 4469 Value = DAG.getNode(ISD::SMUL_LOHI, dl, 4470 DAG.getVTList(Op.getValueType(), Op.getValueType()), 4471 LHS, RHS); 4472 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value.getValue(1), 4473 DAG.getNode(ISD::SRA, dl, Op.getValueType(), 4474 Value.getValue(0), 4475 DAG.getConstant(31, dl, MVT::i32))); 4476 Value = Value.getValue(0); // We only want the low 32 bits for the result. 4477 break; 4478 } // switch (...) 4479 4480 return std::make_pair(Value, OverflowCmp); 4481 } 4482 4483 SDValue 4484 ARMTargetLowering::LowerSignedALUO(SDValue Op, SelectionDAG &DAG) const { 4485 // Let legalize expand this if it isn't a legal type yet. 4486 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType())) 4487 return SDValue(); 4488 4489 SDValue Value, OverflowCmp; 4490 SDValue ARMcc; 4491 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc); 4492 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4493 SDLoc dl(Op); 4494 // We use 0 and 1 as false and true values. 4495 SDValue TVal = DAG.getConstant(1, dl, MVT::i32); 4496 SDValue FVal = DAG.getConstant(0, dl, MVT::i32); 4497 EVT VT = Op.getValueType(); 4498 4499 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal, 4500 ARMcc, CCR, OverflowCmp); 4501 4502 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); 4503 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow); 4504 } 4505 4506 static SDValue ConvertBooleanCarryToCarryFlag(SDValue BoolCarry, 4507 SelectionDAG &DAG) { 4508 SDLoc DL(BoolCarry); 4509 EVT CarryVT = BoolCarry.getValueType(); 4510 4511 // This converts the boolean value carry into the carry flag by doing 4512 // ARMISD::SUBC Carry, 1 4513 SDValue Carry = DAG.getNode(ARMISD::SUBC, DL, 4514 DAG.getVTList(CarryVT, MVT::i32), 4515 BoolCarry, DAG.getConstant(1, DL, CarryVT)); 4516 return Carry.getValue(1); 4517 } 4518 4519 static SDValue ConvertCarryFlagToBooleanCarry(SDValue Flags, EVT VT, 4520 SelectionDAG &DAG) { 4521 SDLoc DL(Flags); 4522 4523 // Now convert the carry flag into a boolean carry. We do this 4524 // using ARMISD:ADDE 0, 0, Carry 4525 return DAG.getNode(ARMISD::ADDE, DL, DAG.getVTList(VT, MVT::i32), 4526 DAG.getConstant(0, DL, MVT::i32), 4527 DAG.getConstant(0, DL, MVT::i32), Flags); 4528 } 4529 4530 SDValue ARMTargetLowering::LowerUnsignedALUO(SDValue Op, 4531 SelectionDAG &DAG) const { 4532 // Let legalize expand this if it isn't a legal type yet. 4533 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType())) 4534 return SDValue(); 4535 4536 SDValue LHS = Op.getOperand(0); 4537 SDValue RHS = Op.getOperand(1); 4538 SDLoc dl(Op); 4539 4540 EVT VT = Op.getValueType(); 4541 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 4542 SDValue Value; 4543 SDValue Overflow; 4544 switch (Op.getOpcode()) { 4545 default: 4546 llvm_unreachable("Unknown overflow instruction!"); 4547 case ISD::UADDO: 4548 Value = DAG.getNode(ARMISD::ADDC, dl, VTs, LHS, RHS); 4549 // Convert the carry flag into a boolean value. 4550 Overflow = ConvertCarryFlagToBooleanCarry(Value.getValue(1), VT, DAG); 4551 break; 4552 case ISD::USUBO: { 4553 Value = DAG.getNode(ARMISD::SUBC, dl, VTs, LHS, RHS); 4554 // Convert the carry flag into a boolean value. 4555 Overflow = ConvertCarryFlagToBooleanCarry(Value.getValue(1), VT, DAG); 4556 // ARMISD::SUBC returns 0 when we have to borrow, so make it an overflow 4557 // value. So compute 1 - C. 4558 Overflow = DAG.getNode(ISD::SUB, dl, MVT::i32, 4559 DAG.getConstant(1, dl, MVT::i32), Overflow); 4560 break; 4561 } 4562 } 4563 4564 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow); 4565 } 4566 4567 static SDValue LowerSADDSUBSAT(SDValue Op, SelectionDAG &DAG, 4568 const ARMSubtarget *Subtarget) { 4569 EVT VT = Op.getValueType(); 4570 if (!Subtarget->hasDSP()) 4571 return SDValue(); 4572 if (!VT.isSimple()) 4573 return SDValue(); 4574 4575 unsigned NewOpcode; 4576 bool IsAdd = Op->getOpcode() == ISD::SADDSAT; 4577 switch (VT.getSimpleVT().SimpleTy) { 4578 default: 4579 return SDValue(); 4580 case MVT::i8: 4581 NewOpcode = IsAdd ? ARMISD::QADD8b : ARMISD::QSUB8b; 4582 break; 4583 case MVT::i16: 4584 NewOpcode = IsAdd ? ARMISD::QADD16b : ARMISD::QSUB16b; 4585 break; 4586 } 4587 4588 SDLoc dl(Op); 4589 SDValue Add = 4590 DAG.getNode(NewOpcode, dl, MVT::i32, 4591 DAG.getSExtOrTrunc(Op->getOperand(0), dl, MVT::i32), 4592 DAG.getSExtOrTrunc(Op->getOperand(1), dl, MVT::i32)); 4593 return DAG.getNode(ISD::TRUNCATE, dl, VT, Add); 4594 } 4595 4596 SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 4597 SDValue Cond = Op.getOperand(0); 4598 SDValue SelectTrue = Op.getOperand(1); 4599 SDValue SelectFalse = Op.getOperand(2); 4600 SDLoc dl(Op); 4601 unsigned Opc = Cond.getOpcode(); 4602 4603 if (Cond.getResNo() == 1 && 4604 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO || 4605 Opc == ISD::USUBO)) { 4606 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0))) 4607 return SDValue(); 4608 4609 SDValue Value, OverflowCmp; 4610 SDValue ARMcc; 4611 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc); 4612 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4613 EVT VT = Op.getValueType(); 4614 4615 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR, 4616 OverflowCmp, DAG); 4617 } 4618 4619 // Convert: 4620 // 4621 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond) 4622 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond) 4623 // 4624 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) { 4625 const ConstantSDNode *CMOVTrue = 4626 dyn_cast<ConstantSDNode>(Cond.getOperand(0)); 4627 const ConstantSDNode *CMOVFalse = 4628 dyn_cast<ConstantSDNode>(Cond.getOperand(1)); 4629 4630 if (CMOVTrue && CMOVFalse) { 4631 unsigned CMOVTrueVal = CMOVTrue->getZExtValue(); 4632 unsigned CMOVFalseVal = CMOVFalse->getZExtValue(); 4633 4634 SDValue True; 4635 SDValue False; 4636 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) { 4637 True = SelectTrue; 4638 False = SelectFalse; 4639 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) { 4640 True = SelectFalse; 4641 False = SelectTrue; 4642 } 4643 4644 if (True.getNode() && False.getNode()) { 4645 EVT VT = Op.getValueType(); 4646 SDValue ARMcc = Cond.getOperand(2); 4647 SDValue CCR = Cond.getOperand(3); 4648 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG); 4649 assert(True.getValueType() == VT); 4650 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG); 4651 } 4652 } 4653 } 4654 4655 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the 4656 // undefined bits before doing a full-word comparison with zero. 4657 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond, 4658 DAG.getConstant(1, dl, Cond.getValueType())); 4659 4660 return DAG.getSelectCC(dl, Cond, 4661 DAG.getConstant(0, dl, Cond.getValueType()), 4662 SelectTrue, SelectFalse, ISD::SETNE); 4663 } 4664 4665 static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 4666 bool &swpCmpOps, bool &swpVselOps) { 4667 // Start by selecting the GE condition code for opcodes that return true for 4668 // 'equality' 4669 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE || 4670 CC == ISD::SETULE || CC == ISD::SETGE || CC == ISD::SETLE) 4671 CondCode = ARMCC::GE; 4672 4673 // and GT for opcodes that return false for 'equality'. 4674 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT || 4675 CC == ISD::SETULT || CC == ISD::SETGT || CC == ISD::SETLT) 4676 CondCode = ARMCC::GT; 4677 4678 // Since we are constrained to GE/GT, if the opcode contains 'less', we need 4679 // to swap the compare operands. 4680 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT || 4681 CC == ISD::SETULT || CC == ISD::SETLE || CC == ISD::SETLT) 4682 swpCmpOps = true; 4683 4684 // Both GT and GE are ordered comparisons, and return false for 'unordered'. 4685 // If we have an unordered opcode, we need to swap the operands to the VSEL 4686 // instruction (effectively negating the condition). 4687 // 4688 // This also has the effect of swapping which one of 'less' or 'greater' 4689 // returns true, so we also swap the compare operands. It also switches 4690 // whether we return true for 'equality', so we compensate by picking the 4691 // opposite condition code to our original choice. 4692 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE || 4693 CC == ISD::SETUGT) { 4694 swpCmpOps = !swpCmpOps; 4695 swpVselOps = !swpVselOps; 4696 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT; 4697 } 4698 4699 // 'ordered' is 'anything but unordered', so use the VS condition code and 4700 // swap the VSEL operands. 4701 if (CC == ISD::SETO) { 4702 CondCode = ARMCC::VS; 4703 swpVselOps = true; 4704 } 4705 4706 // 'unordered or not equal' is 'anything but equal', so use the EQ condition 4707 // code and swap the VSEL operands. Also do this if we don't care about the 4708 // unordered case. 4709 if (CC == ISD::SETUNE || CC == ISD::SETNE) { 4710 CondCode = ARMCC::EQ; 4711 swpVselOps = true; 4712 } 4713 } 4714 4715 SDValue ARMTargetLowering::getCMOV(const SDLoc &dl, EVT VT, SDValue FalseVal, 4716 SDValue TrueVal, SDValue ARMcc, SDValue CCR, 4717 SDValue Cmp, SelectionDAG &DAG) const { 4718 if (!Subtarget->hasFP64() && VT == MVT::f64) { 4719 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl, 4720 DAG.getVTList(MVT::i32, MVT::i32), FalseVal); 4721 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl, 4722 DAG.getVTList(MVT::i32, MVT::i32), TrueVal); 4723 4724 SDValue TrueLow = TrueVal.getValue(0); 4725 SDValue TrueHigh = TrueVal.getValue(1); 4726 SDValue FalseLow = FalseVal.getValue(0); 4727 SDValue FalseHigh = FalseVal.getValue(1); 4728 4729 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow, 4730 ARMcc, CCR, Cmp); 4731 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh, 4732 ARMcc, CCR, duplicateCmp(Cmp, DAG)); 4733 4734 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High); 4735 } else { 4736 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR, 4737 Cmp); 4738 } 4739 } 4740 4741 static bool isGTorGE(ISD::CondCode CC) { 4742 return CC == ISD::SETGT || CC == ISD::SETGE; 4743 } 4744 4745 static bool isLTorLE(ISD::CondCode CC) { 4746 return CC == ISD::SETLT || CC == ISD::SETLE; 4747 } 4748 4749 // See if a conditional (LHS CC RHS ? TrueVal : FalseVal) is lower-saturating. 4750 // All of these conditions (and their <= and >= counterparts) will do: 4751 // x < k ? k : x 4752 // x > k ? x : k 4753 // k < x ? x : k 4754 // k > x ? k : x 4755 static bool isLowerSaturate(const SDValue LHS, const SDValue RHS, 4756 const SDValue TrueVal, const SDValue FalseVal, 4757 const ISD::CondCode CC, const SDValue K) { 4758 return (isGTorGE(CC) && 4759 ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal))) || 4760 (isLTorLE(CC) && 4761 ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal))); 4762 } 4763 4764 // Similar to isLowerSaturate(), but checks for upper-saturating conditions. 4765 static bool isUpperSaturate(const SDValue LHS, const SDValue RHS, 4766 const SDValue TrueVal, const SDValue FalseVal, 4767 const ISD::CondCode CC, const SDValue K) { 4768 return (isGTorGE(CC) && 4769 ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal))) || 4770 (isLTorLE(CC) && 4771 ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal))); 4772 } 4773 4774 // Check if two chained conditionals could be converted into SSAT or USAT. 4775 // 4776 // SSAT can replace a set of two conditional selectors that bound a number to an 4777 // interval of type [k, ~k] when k + 1 is a power of 2. Here are some examples: 4778 // 4779 // x < -k ? -k : (x > k ? k : x) 4780 // x < -k ? -k : (x < k ? x : k) 4781 // x > -k ? (x > k ? k : x) : -k 4782 // x < k ? (x < -k ? -k : x) : k 4783 // etc. 4784 // 4785 // USAT works similarily to SSAT but bounds on the interval [0, k] where k + 1 is 4786 // a power of 2. 4787 // 4788 // It returns true if the conversion can be done, false otherwise. 4789 // Additionally, the variable is returned in parameter V, the constant in K and 4790 // usat is set to true if the conditional represents an unsigned saturation 4791 static bool isSaturatingConditional(const SDValue &Op, SDValue &V, 4792 uint64_t &K, bool &usat) { 4793 SDValue LHS1 = Op.getOperand(0); 4794 SDValue RHS1 = Op.getOperand(1); 4795 SDValue TrueVal1 = Op.getOperand(2); 4796 SDValue FalseVal1 = Op.getOperand(3); 4797 ISD::CondCode CC1 = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 4798 4799 const SDValue Op2 = isa<ConstantSDNode>(TrueVal1) ? FalseVal1 : TrueVal1; 4800 if (Op2.getOpcode() != ISD::SELECT_CC) 4801 return false; 4802 4803 SDValue LHS2 = Op2.getOperand(0); 4804 SDValue RHS2 = Op2.getOperand(1); 4805 SDValue TrueVal2 = Op2.getOperand(2); 4806 SDValue FalseVal2 = Op2.getOperand(3); 4807 ISD::CondCode CC2 = cast<CondCodeSDNode>(Op2.getOperand(4))->get(); 4808 4809 // Find out which are the constants and which are the variables 4810 // in each conditional 4811 SDValue *K1 = isa<ConstantSDNode>(LHS1) ? &LHS1 : isa<ConstantSDNode>(RHS1) 4812 ? &RHS1 4813 : nullptr; 4814 SDValue *K2 = isa<ConstantSDNode>(LHS2) ? &LHS2 : isa<ConstantSDNode>(RHS2) 4815 ? &RHS2 4816 : nullptr; 4817 SDValue K2Tmp = isa<ConstantSDNode>(TrueVal2) ? TrueVal2 : FalseVal2; 4818 SDValue V1Tmp = (K1 && *K1 == LHS1) ? RHS1 : LHS1; 4819 SDValue V2Tmp = (K2 && *K2 == LHS2) ? RHS2 : LHS2; 4820 SDValue V2 = (K2Tmp == TrueVal2) ? FalseVal2 : TrueVal2; 4821 4822 // We must detect cases where the original operations worked with 16- or 4823 // 8-bit values. In such case, V2Tmp != V2 because the comparison operations 4824 // must work with sign-extended values but the select operations return 4825 // the original non-extended value. 4826 SDValue V2TmpReg = V2Tmp; 4827 if (V2Tmp->getOpcode() == ISD::SIGN_EXTEND_INREG) 4828 V2TmpReg = V2Tmp->getOperand(0); 4829 4830 // Check that the registers and the constants have the correct values 4831 // in both conditionals 4832 if (!K1 || !K2 || *K1 == Op2 || *K2 != K2Tmp || V1Tmp != V2Tmp || 4833 V2TmpReg != V2) 4834 return false; 4835 4836 // Figure out which conditional is saturating the lower/upper bound. 4837 const SDValue *LowerCheckOp = 4838 isLowerSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1) 4839 ? &Op 4840 : isLowerSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2) 4841 ? &Op2 4842 : nullptr; 4843 const SDValue *UpperCheckOp = 4844 isUpperSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1) 4845 ? &Op 4846 : isUpperSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2) 4847 ? &Op2 4848 : nullptr; 4849 4850 if (!UpperCheckOp || !LowerCheckOp || LowerCheckOp == UpperCheckOp) 4851 return false; 4852 4853 // Check that the constant in the lower-bound check is 4854 // the opposite of the constant in the upper-bound check 4855 // in 1's complement. 4856 int64_t Val1 = cast<ConstantSDNode>(*K1)->getSExtValue(); 4857 int64_t Val2 = cast<ConstantSDNode>(*K2)->getSExtValue(); 4858 int64_t PosVal = std::max(Val1, Val2); 4859 int64_t NegVal = std::min(Val1, Val2); 4860 4861 if (((Val1 > Val2 && UpperCheckOp == &Op) || 4862 (Val1 < Val2 && UpperCheckOp == &Op2)) && 4863 isPowerOf2_64(PosVal + 1)) { 4864 4865 // Handle the difference between USAT (unsigned) and SSAT (signed) saturation 4866 if (Val1 == ~Val2) 4867 usat = false; 4868 else if (NegVal == 0) 4869 usat = true; 4870 else 4871 return false; 4872 4873 V = V2; 4874 K = (uint64_t)PosVal; // At this point, PosVal is guaranteed to be positive 4875 4876 return true; 4877 } 4878 4879 return false; 4880 } 4881 4882 // Check if a condition of the type x < k ? k : x can be converted into a 4883 // bit operation instead of conditional moves. 4884 // Currently this is allowed given: 4885 // - The conditions and values match up 4886 // - k is 0 or -1 (all ones) 4887 // This function will not check the last condition, thats up to the caller 4888 // It returns true if the transformation can be made, and in such case 4889 // returns x in V, and k in SatK. 4890 static bool isLowerSaturatingConditional(const SDValue &Op, SDValue &V, 4891 SDValue &SatK) 4892 { 4893 SDValue LHS = Op.getOperand(0); 4894 SDValue RHS = Op.getOperand(1); 4895 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 4896 SDValue TrueVal = Op.getOperand(2); 4897 SDValue FalseVal = Op.getOperand(3); 4898 4899 SDValue *K = isa<ConstantSDNode>(LHS) ? &LHS : isa<ConstantSDNode>(RHS) 4900 ? &RHS 4901 : nullptr; 4902 4903 // No constant operation in comparison, early out 4904 if (!K) 4905 return false; 4906 4907 SDValue KTmp = isa<ConstantSDNode>(TrueVal) ? TrueVal : FalseVal; 4908 V = (KTmp == TrueVal) ? FalseVal : TrueVal; 4909 SDValue VTmp = (K && *K == LHS) ? RHS : LHS; 4910 4911 // If the constant on left and right side, or variable on left and right, 4912 // does not match, early out 4913 if (*K != KTmp || V != VTmp) 4914 return false; 4915 4916 if (isLowerSaturate(LHS, RHS, TrueVal, FalseVal, CC, *K)) { 4917 SatK = *K; 4918 return true; 4919 } 4920 4921 return false; 4922 } 4923 4924 bool ARMTargetLowering::isUnsupportedFloatingType(EVT VT) const { 4925 if (VT == MVT::f32) 4926 return !Subtarget->hasVFP2Base(); 4927 if (VT == MVT::f64) 4928 return !Subtarget->hasFP64(); 4929 if (VT == MVT::f16) 4930 return !Subtarget->hasFullFP16(); 4931 return false; 4932 } 4933 4934 SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { 4935 EVT VT = Op.getValueType(); 4936 SDLoc dl(Op); 4937 4938 // Try to convert two saturating conditional selects into a single SSAT 4939 SDValue SatValue; 4940 uint64_t SatConstant; 4941 bool SatUSat; 4942 if (((!Subtarget->isThumb() && Subtarget->hasV6Ops()) || Subtarget->isThumb2()) && 4943 isSaturatingConditional(Op, SatValue, SatConstant, SatUSat)) { 4944 if (SatUSat) 4945 return DAG.getNode(ARMISD::USAT, dl, VT, SatValue, 4946 DAG.getConstant(countTrailingOnes(SatConstant), dl, VT)); 4947 else 4948 return DAG.getNode(ARMISD::SSAT, dl, VT, SatValue, 4949 DAG.getConstant(countTrailingOnes(SatConstant), dl, VT)); 4950 } 4951 4952 // Try to convert expressions of the form x < k ? k : x (and similar forms) 4953 // into more efficient bit operations, which is possible when k is 0 or -1 4954 // On ARM and Thumb-2 which have flexible operand 2 this will result in 4955 // single instructions. On Thumb the shift and the bit operation will be two 4956 // instructions. 4957 // Only allow this transformation on full-width (32-bit) operations 4958 SDValue LowerSatConstant; 4959 if (VT == MVT::i32 && 4960 isLowerSaturatingConditional(Op, SatValue, LowerSatConstant)) { 4961 SDValue ShiftV = DAG.getNode(ISD::SRA, dl, VT, SatValue, 4962 DAG.getConstant(31, dl, VT)); 4963 if (isNullConstant(LowerSatConstant)) { 4964 SDValue NotShiftV = DAG.getNode(ISD::XOR, dl, VT, ShiftV, 4965 DAG.getAllOnesConstant(dl, VT)); 4966 return DAG.getNode(ISD::AND, dl, VT, SatValue, NotShiftV); 4967 } else if (isAllOnesConstant(LowerSatConstant)) 4968 return DAG.getNode(ISD::OR, dl, VT, SatValue, ShiftV); 4969 } 4970 4971 SDValue LHS = Op.getOperand(0); 4972 SDValue RHS = Op.getOperand(1); 4973 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 4974 SDValue TrueVal = Op.getOperand(2); 4975 SDValue FalseVal = Op.getOperand(3); 4976 ConstantSDNode *CFVal = dyn_cast<ConstantSDNode>(FalseVal); 4977 ConstantSDNode *CTVal = dyn_cast<ConstantSDNode>(TrueVal); 4978 4979 if (Subtarget->hasV8_1MMainlineOps() && CFVal && CTVal && 4980 LHS.getValueType() == MVT::i32 && RHS.getValueType() == MVT::i32) { 4981 unsigned TVal = CTVal->getZExtValue(); 4982 unsigned FVal = CFVal->getZExtValue(); 4983 unsigned Opcode = 0; 4984 4985 if (TVal == ~FVal) { 4986 Opcode = ARMISD::CSINV; 4987 } else if (TVal == ~FVal + 1) { 4988 Opcode = ARMISD::CSNEG; 4989 } else if (TVal + 1 == FVal) { 4990 Opcode = ARMISD::CSINC; 4991 } else if (TVal == FVal + 1) { 4992 Opcode = ARMISD::CSINC; 4993 std::swap(TrueVal, FalseVal); 4994 std::swap(TVal, FVal); 4995 CC = ISD::getSetCCInverse(CC, LHS.getValueType()); 4996 } 4997 4998 if (Opcode) { 4999 // If one of the constants is cheaper than another, materialise the 5000 // cheaper one and let the csel generate the other. 5001 if (Opcode != ARMISD::CSINC && 5002 HasLowerConstantMaterializationCost(FVal, TVal, Subtarget)) { 5003 std::swap(TrueVal, FalseVal); 5004 std::swap(TVal, FVal); 5005 CC = ISD::getSetCCInverse(CC, LHS.getValueType()); 5006 } 5007 5008 // Attempt to use ZR checking TVal is 0, possibly inverting the condition 5009 // to get there. CSINC not is invertable like the other two (~(~a) == a, 5010 // -(-a) == a, but (a+1)+1 != a). 5011 if (FVal == 0 && Opcode != ARMISD::CSINC) { 5012 std::swap(TrueVal, FalseVal); 5013 std::swap(TVal, FVal); 5014 CC = ISD::getSetCCInverse(CC, LHS.getValueType()); 5015 } 5016 if (TVal == 0) 5017 TrueVal = DAG.getRegister(ARM::ZR, MVT::i32); 5018 5019 // Drops F's value because we can get it by inverting/negating TVal. 5020 FalseVal = TrueVal; 5021 5022 SDValue ARMcc; 5023 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 5024 EVT VT = TrueVal.getValueType(); 5025 return DAG.getNode(Opcode, dl, VT, TrueVal, FalseVal, ARMcc, Cmp); 5026 } 5027 } 5028 5029 if (isUnsupportedFloatingType(LHS.getValueType())) { 5030 DAG.getTargetLoweringInfo().softenSetCCOperands( 5031 DAG, LHS.getValueType(), LHS, RHS, CC, dl, LHS, RHS); 5032 5033 // If softenSetCCOperands only returned one value, we should compare it to 5034 // zero. 5035 if (!RHS.getNode()) { 5036 RHS = DAG.getConstant(0, dl, LHS.getValueType()); 5037 CC = ISD::SETNE; 5038 } 5039 } 5040 5041 if (LHS.getValueType() == MVT::i32) { 5042 // Try to generate VSEL on ARMv8. 5043 // The VSEL instruction can't use all the usual ARM condition 5044 // codes: it only has two bits to select the condition code, so it's 5045 // constrained to use only GE, GT, VS and EQ. 5046 // 5047 // To implement all the various ISD::SETXXX opcodes, we sometimes need to 5048 // swap the operands of the previous compare instruction (effectively 5049 // inverting the compare condition, swapping 'less' and 'greater') and 5050 // sometimes need to swap the operands to the VSEL (which inverts the 5051 // condition in the sense of firing whenever the previous condition didn't) 5052 if (Subtarget->hasFPARMv8Base() && (TrueVal.getValueType() == MVT::f16 || 5053 TrueVal.getValueType() == MVT::f32 || 5054 TrueVal.getValueType() == MVT::f64)) { 5055 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 5056 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE || 5057 CondCode == ARMCC::VC || CondCode == ARMCC::NE) { 5058 CC = ISD::getSetCCInverse(CC, LHS.getValueType()); 5059 std::swap(TrueVal, FalseVal); 5060 } 5061 } 5062 5063 SDValue ARMcc; 5064 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 5065 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 5066 // Choose GE over PL, which vsel does now support 5067 if (cast<ConstantSDNode>(ARMcc)->getZExtValue() == ARMCC::PL) 5068 ARMcc = DAG.getConstant(ARMCC::GE, dl, MVT::i32); 5069 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG); 5070 } 5071 5072 ARMCC::CondCodes CondCode, CondCode2; 5073 FPCCToARMCC(CC, CondCode, CondCode2); 5074 5075 // Normalize the fp compare. If RHS is zero we prefer to keep it there so we 5076 // match CMPFPw0 instead of CMPFP, though we don't do this for f16 because we 5077 // must use VSEL (limited condition codes), due to not having conditional f16 5078 // moves. 5079 if (Subtarget->hasFPARMv8Base() && 5080 !(isFloatingPointZero(RHS) && TrueVal.getValueType() != MVT::f16) && 5081 (TrueVal.getValueType() == MVT::f16 || 5082 TrueVal.getValueType() == MVT::f32 || 5083 TrueVal.getValueType() == MVT::f64)) { 5084 bool swpCmpOps = false; 5085 bool swpVselOps = false; 5086 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps); 5087 5088 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE || 5089 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) { 5090 if (swpCmpOps) 5091 std::swap(LHS, RHS); 5092 if (swpVselOps) 5093 std::swap(TrueVal, FalseVal); 5094 } 5095 } 5096 5097 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 5098 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl); 5099 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 5100 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG); 5101 if (CondCode2 != ARMCC::AL) { 5102 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32); 5103 // FIXME: Needs another CMP because flag can have but one use. 5104 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl); 5105 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG); 5106 } 5107 return Result; 5108 } 5109 5110 /// canChangeToInt - Given the fp compare operand, return true if it is suitable 5111 /// to morph to an integer compare sequence. 5112 static bool canChangeToInt(SDValue Op, bool &SeenZero, 5113 const ARMSubtarget *Subtarget) { 5114 SDNode *N = Op.getNode(); 5115 if (!N->hasOneUse()) 5116 // Otherwise it requires moving the value from fp to integer registers. 5117 return false; 5118 if (!N->getNumValues()) 5119 return false; 5120 EVT VT = Op.getValueType(); 5121 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow()) 5122 // f32 case is generally profitable. f64 case only makes sense when vcmpe + 5123 // vmrs are very slow, e.g. cortex-a8. 5124 return false; 5125 5126 if (isFloatingPointZero(Op)) { 5127 SeenZero = true; 5128 return true; 5129 } 5130 return ISD::isNormalLoad(N); 5131 } 5132 5133 static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) { 5134 if (isFloatingPointZero(Op)) 5135 return DAG.getConstant(0, SDLoc(Op), MVT::i32); 5136 5137 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) 5138 return DAG.getLoad(MVT::i32, SDLoc(Op), Ld->getChain(), Ld->getBasePtr(), 5139 Ld->getPointerInfo(), Ld->getAlignment(), 5140 Ld->getMemOperand()->getFlags()); 5141 5142 llvm_unreachable("Unknown VFP cmp argument!"); 5143 } 5144 5145 static void expandf64Toi32(SDValue Op, SelectionDAG &DAG, 5146 SDValue &RetVal1, SDValue &RetVal2) { 5147 SDLoc dl(Op); 5148 5149 if (isFloatingPointZero(Op)) { 5150 RetVal1 = DAG.getConstant(0, dl, MVT::i32); 5151 RetVal2 = DAG.getConstant(0, dl, MVT::i32); 5152 return; 5153 } 5154 5155 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) { 5156 SDValue Ptr = Ld->getBasePtr(); 5157 RetVal1 = 5158 DAG.getLoad(MVT::i32, dl, Ld->getChain(), Ptr, Ld->getPointerInfo(), 5159 Ld->getAlignment(), Ld->getMemOperand()->getFlags()); 5160 5161 EVT PtrType = Ptr.getValueType(); 5162 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4); 5163 SDValue NewPtr = DAG.getNode(ISD::ADD, dl, 5164 PtrType, Ptr, DAG.getConstant(4, dl, PtrType)); 5165 RetVal2 = DAG.getLoad(MVT::i32, dl, Ld->getChain(), NewPtr, 5166 Ld->getPointerInfo().getWithOffset(4), NewAlign, 5167 Ld->getMemOperand()->getFlags()); 5168 return; 5169 } 5170 5171 llvm_unreachable("Unknown VFP cmp argument!"); 5172 } 5173 5174 /// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some 5175 /// f32 and even f64 comparisons to integer ones. 5176 SDValue 5177 ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const { 5178 SDValue Chain = Op.getOperand(0); 5179 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 5180 SDValue LHS = Op.getOperand(2); 5181 SDValue RHS = Op.getOperand(3); 5182 SDValue Dest = Op.getOperand(4); 5183 SDLoc dl(Op); 5184 5185 bool LHSSeenZero = false; 5186 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget); 5187 bool RHSSeenZero = false; 5188 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget); 5189 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) { 5190 // If unsafe fp math optimization is enabled and there are no other uses of 5191 // the CMP operands, and the condition code is EQ or NE, we can optimize it 5192 // to an integer comparison. 5193 if (CC == ISD::SETOEQ) 5194 CC = ISD::SETEQ; 5195 else if (CC == ISD::SETUNE) 5196 CC = ISD::SETNE; 5197 5198 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32); 5199 SDValue ARMcc; 5200 if (LHS.getValueType() == MVT::f32) { 5201 LHS = DAG.getNode(ISD::AND, dl, MVT::i32, 5202 bitcastf32Toi32(LHS, DAG), Mask); 5203 RHS = DAG.getNode(ISD::AND, dl, MVT::i32, 5204 bitcastf32Toi32(RHS, DAG), Mask); 5205 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 5206 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 5207 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 5208 Chain, Dest, ARMcc, CCR, Cmp); 5209 } 5210 5211 SDValue LHS1, LHS2; 5212 SDValue RHS1, RHS2; 5213 expandf64Toi32(LHS, DAG, LHS1, LHS2); 5214 expandf64Toi32(RHS, DAG, RHS1, RHS2); 5215 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask); 5216 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask); 5217 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 5218 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 5219 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 5220 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest }; 5221 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops); 5222 } 5223 5224 return SDValue(); 5225 } 5226 5227 SDValue ARMTargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const { 5228 SDValue Chain = Op.getOperand(0); 5229 SDValue Cond = Op.getOperand(1); 5230 SDValue Dest = Op.getOperand(2); 5231 SDLoc dl(Op); 5232 5233 // Optimize {s|u}{add|sub|mul}.with.overflow feeding into a branch 5234 // instruction. 5235 unsigned Opc = Cond.getOpcode(); 5236 bool OptimizeMul = (Opc == ISD::SMULO || Opc == ISD::UMULO) && 5237 !Subtarget->isThumb1Only(); 5238 if (Cond.getResNo() == 1 && 5239 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO || 5240 Opc == ISD::USUBO || OptimizeMul)) { 5241 // Only lower legal XALUO ops. 5242 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0))) 5243 return SDValue(); 5244 5245 // The actual operation with overflow check. 5246 SDValue Value, OverflowCmp; 5247 SDValue ARMcc; 5248 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc); 5249 5250 // Reverse the condition code. 5251 ARMCC::CondCodes CondCode = 5252 (ARMCC::CondCodes)cast<const ConstantSDNode>(ARMcc)->getZExtValue(); 5253 CondCode = ARMCC::getOppositeCondition(CondCode); 5254 ARMcc = DAG.getConstant(CondCode, SDLoc(ARMcc), MVT::i32); 5255 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 5256 5257 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, Chain, Dest, ARMcc, CCR, 5258 OverflowCmp); 5259 } 5260 5261 return SDValue(); 5262 } 5263 5264 SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 5265 SDValue Chain = Op.getOperand(0); 5266 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 5267 SDValue LHS = Op.getOperand(2); 5268 SDValue RHS = Op.getOperand(3); 5269 SDValue Dest = Op.getOperand(4); 5270 SDLoc dl(Op); 5271 5272 if (isUnsupportedFloatingType(LHS.getValueType())) { 5273 DAG.getTargetLoweringInfo().softenSetCCOperands( 5274 DAG, LHS.getValueType(), LHS, RHS, CC, dl, LHS, RHS); 5275 5276 // If softenSetCCOperands only returned one value, we should compare it to 5277 // zero. 5278 if (!RHS.getNode()) { 5279 RHS = DAG.getConstant(0, dl, LHS.getValueType()); 5280 CC = ISD::SETNE; 5281 } 5282 } 5283 5284 // Optimize {s|u}{add|sub|mul}.with.overflow feeding into a branch 5285 // instruction. 5286 unsigned Opc = LHS.getOpcode(); 5287 bool OptimizeMul = (Opc == ISD::SMULO || Opc == ISD::UMULO) && 5288 !Subtarget->isThumb1Only(); 5289 if (LHS.getResNo() == 1 && (isOneConstant(RHS) || isNullConstant(RHS)) && 5290 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO || 5291 Opc == ISD::USUBO || OptimizeMul) && 5292 (CC == ISD::SETEQ || CC == ISD::SETNE)) { 5293 // Only lower legal XALUO ops. 5294 if (!DAG.getTargetLoweringInfo().isTypeLegal(LHS->getValueType(0))) 5295 return SDValue(); 5296 5297 // The actual operation with overflow check. 5298 SDValue Value, OverflowCmp; 5299 SDValue ARMcc; 5300 std::tie(Value, OverflowCmp) = getARMXALUOOp(LHS.getValue(0), DAG, ARMcc); 5301 5302 if ((CC == ISD::SETNE) != isOneConstant(RHS)) { 5303 // Reverse the condition code. 5304 ARMCC::CondCodes CondCode = 5305 (ARMCC::CondCodes)cast<const ConstantSDNode>(ARMcc)->getZExtValue(); 5306 CondCode = ARMCC::getOppositeCondition(CondCode); 5307 ARMcc = DAG.getConstant(CondCode, SDLoc(ARMcc), MVT::i32); 5308 } 5309 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 5310 5311 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, Chain, Dest, ARMcc, CCR, 5312 OverflowCmp); 5313 } 5314 5315 if (LHS.getValueType() == MVT::i32) { 5316 SDValue ARMcc; 5317 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 5318 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 5319 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 5320 Chain, Dest, ARMcc, CCR, Cmp); 5321 } 5322 5323 if (getTargetMachine().Options.UnsafeFPMath && 5324 (CC == ISD::SETEQ || CC == ISD::SETOEQ || 5325 CC == ISD::SETNE || CC == ISD::SETUNE)) { 5326 if (SDValue Result = OptimizeVFPBrcond(Op, DAG)) 5327 return Result; 5328 } 5329 5330 ARMCC::CondCodes CondCode, CondCode2; 5331 FPCCToARMCC(CC, CondCode, CondCode2); 5332 5333 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 5334 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl); 5335 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 5336 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 5337 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp }; 5338 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops); 5339 if (CondCode2 != ARMCC::AL) { 5340 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32); 5341 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) }; 5342 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops); 5343 } 5344 return Res; 5345 } 5346 5347 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const { 5348 SDValue Chain = Op.getOperand(0); 5349 SDValue Table = Op.getOperand(1); 5350 SDValue Index = Op.getOperand(2); 5351 SDLoc dl(Op); 5352 5353 EVT PTy = getPointerTy(DAG.getDataLayout()); 5354 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table); 5355 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy); 5356 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI); 5357 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy)); 5358 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Table, Index); 5359 if (Subtarget->isThumb2() || (Subtarget->hasV8MBaselineOps() && Subtarget->isThumb())) { 5360 // Thumb2 and ARMv8-M use a two-level jump. That is, it jumps into the jump table 5361 // which does another jump to the destination. This also makes it easier 5362 // to translate it to TBB / TBH later (Thumb2 only). 5363 // FIXME: This might not work if the function is extremely large. 5364 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain, 5365 Addr, Op.getOperand(2), JTI); 5366 } 5367 if (isPositionIndependent() || Subtarget->isROPI()) { 5368 Addr = 5369 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, 5370 MachinePointerInfo::getJumpTable(DAG.getMachineFunction())); 5371 Chain = Addr.getValue(1); 5372 Addr = DAG.getNode(ISD::ADD, dl, PTy, Table, Addr); 5373 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI); 5374 } else { 5375 Addr = 5376 DAG.getLoad(PTy, dl, Chain, Addr, 5377 MachinePointerInfo::getJumpTable(DAG.getMachineFunction())); 5378 Chain = Addr.getValue(1); 5379 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI); 5380 } 5381 } 5382 5383 static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) { 5384 EVT VT = Op.getValueType(); 5385 SDLoc dl(Op); 5386 5387 if (Op.getValueType().getVectorElementType() == MVT::i32) { 5388 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32) 5389 return Op; 5390 return DAG.UnrollVectorOp(Op.getNode()); 5391 } 5392 5393 const bool HasFullFP16 = 5394 static_cast<const ARMSubtarget&>(DAG.getSubtarget()).hasFullFP16(); 5395 5396 EVT NewTy; 5397 const EVT OpTy = Op.getOperand(0).getValueType(); 5398 if (OpTy == MVT::v4f32) 5399 NewTy = MVT::v4i32; 5400 else if (OpTy == MVT::v4f16 && HasFullFP16) 5401 NewTy = MVT::v4i16; 5402 else if (OpTy == MVT::v8f16 && HasFullFP16) 5403 NewTy = MVT::v8i16; 5404 else 5405 llvm_unreachable("Invalid type for custom lowering!"); 5406 5407 if (VT != MVT::v4i16 && VT != MVT::v8i16) 5408 return DAG.UnrollVectorOp(Op.getNode()); 5409 5410 Op = DAG.getNode(Op.getOpcode(), dl, NewTy, Op.getOperand(0)); 5411 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op); 5412 } 5413 5414 SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const { 5415 EVT VT = Op.getValueType(); 5416 if (VT.isVector()) 5417 return LowerVectorFP_TO_INT(Op, DAG); 5418 5419 bool IsStrict = Op->isStrictFPOpcode(); 5420 SDValue SrcVal = Op.getOperand(IsStrict ? 1 : 0); 5421 5422 if (isUnsupportedFloatingType(SrcVal.getValueType())) { 5423 RTLIB::Libcall LC; 5424 if (Op.getOpcode() == ISD::FP_TO_SINT || 5425 Op.getOpcode() == ISD::STRICT_FP_TO_SINT) 5426 LC = RTLIB::getFPTOSINT(SrcVal.getValueType(), 5427 Op.getValueType()); 5428 else 5429 LC = RTLIB::getFPTOUINT(SrcVal.getValueType(), 5430 Op.getValueType()); 5431 SDLoc Loc(Op); 5432 MakeLibCallOptions CallOptions; 5433 SDValue Chain = IsStrict ? Op.getOperand(0) : SDValue(); 5434 SDValue Result; 5435 std::tie(Result, Chain) = makeLibCall(DAG, LC, Op.getValueType(), SrcVal, 5436 CallOptions, Loc, Chain); 5437 return IsStrict ? DAG.getMergeValues({Result, Chain}, Loc) : Result; 5438 } 5439 5440 // FIXME: Remove this when we have strict fp instruction selection patterns 5441 if (IsStrict) { 5442 SDLoc Loc(Op); 5443 SDValue Result = 5444 DAG.getNode(Op.getOpcode() == ISD::STRICT_FP_TO_SINT ? ISD::FP_TO_SINT 5445 : ISD::FP_TO_UINT, 5446 Loc, Op.getValueType(), SrcVal); 5447 return DAG.getMergeValues({Result, Op.getOperand(0)}, Loc); 5448 } 5449 5450 return Op; 5451 } 5452 5453 static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 5454 EVT VT = Op.getValueType(); 5455 SDLoc dl(Op); 5456 5457 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) { 5458 if (VT.getVectorElementType() == MVT::f32) 5459 return Op; 5460 return DAG.UnrollVectorOp(Op.getNode()); 5461 } 5462 5463 assert((Op.getOperand(0).getValueType() == MVT::v4i16 || 5464 Op.getOperand(0).getValueType() == MVT::v8i16) && 5465 "Invalid type for custom lowering!"); 5466 5467 const bool HasFullFP16 = 5468 static_cast<const ARMSubtarget&>(DAG.getSubtarget()).hasFullFP16(); 5469 5470 EVT DestVecType; 5471 if (VT == MVT::v4f32) 5472 DestVecType = MVT::v4i32; 5473 else if (VT == MVT::v4f16 && HasFullFP16) 5474 DestVecType = MVT::v4i16; 5475 else if (VT == MVT::v8f16 && HasFullFP16) 5476 DestVecType = MVT::v8i16; 5477 else 5478 return DAG.UnrollVectorOp(Op.getNode()); 5479 5480 unsigned CastOpc; 5481 unsigned Opc; 5482 switch (Op.getOpcode()) { 5483 default: llvm_unreachable("Invalid opcode!"); 5484 case ISD::SINT_TO_FP: 5485 CastOpc = ISD::SIGN_EXTEND; 5486 Opc = ISD::SINT_TO_FP; 5487 break; 5488 case ISD::UINT_TO_FP: 5489 CastOpc = ISD::ZERO_EXTEND; 5490 Opc = ISD::UINT_TO_FP; 5491 break; 5492 } 5493 5494 Op = DAG.getNode(CastOpc, dl, DestVecType, Op.getOperand(0)); 5495 return DAG.getNode(Opc, dl, VT, Op); 5496 } 5497 5498 SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const { 5499 EVT VT = Op.getValueType(); 5500 if (VT.isVector()) 5501 return LowerVectorINT_TO_FP(Op, DAG); 5502 if (isUnsupportedFloatingType(VT)) { 5503 RTLIB::Libcall LC; 5504 if (Op.getOpcode() == ISD::SINT_TO_FP) 5505 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(), 5506 Op.getValueType()); 5507 else 5508 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(), 5509 Op.getValueType()); 5510 MakeLibCallOptions CallOptions; 5511 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0), 5512 CallOptions, SDLoc(Op)).first; 5513 } 5514 5515 return Op; 5516 } 5517 5518 SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const { 5519 // Implement fcopysign with a fabs and a conditional fneg. 5520 SDValue Tmp0 = Op.getOperand(0); 5521 SDValue Tmp1 = Op.getOperand(1); 5522 SDLoc dl(Op); 5523 EVT VT = Op.getValueType(); 5524 EVT SrcVT = Tmp1.getValueType(); 5525 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST || 5526 Tmp0.getOpcode() == ARMISD::VMOVDRR; 5527 bool UseNEON = !InGPR && Subtarget->hasNEON(); 5528 5529 if (UseNEON) { 5530 // Use VBSL to copy the sign bit. 5531 unsigned EncodedVal = ARM_AM::createVMOVModImm(0x6, 0x80); 5532 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32, 5533 DAG.getTargetConstant(EncodedVal, dl, MVT::i32)); 5534 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64; 5535 if (VT == MVT::f64) 5536 Mask = DAG.getNode(ARMISD::VSHLIMM, dl, OpVT, 5537 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask), 5538 DAG.getConstant(32, dl, MVT::i32)); 5539 else /*if (VT == MVT::f32)*/ 5540 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0); 5541 if (SrcVT == MVT::f32) { 5542 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1); 5543 if (VT == MVT::f64) 5544 Tmp1 = DAG.getNode(ARMISD::VSHLIMM, dl, OpVT, 5545 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1), 5546 DAG.getConstant(32, dl, MVT::i32)); 5547 } else if (VT == MVT::f32) 5548 Tmp1 = DAG.getNode(ARMISD::VSHRuIMM, dl, MVT::v1i64, 5549 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1), 5550 DAG.getConstant(32, dl, MVT::i32)); 5551 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0); 5552 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1); 5553 5554 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createVMOVModImm(0xe, 0xff), 5555 dl, MVT::i32); 5556 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes); 5557 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask, 5558 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes)); 5559 5560 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT, 5561 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask), 5562 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot)); 5563 if (VT == MVT::f32) { 5564 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res); 5565 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res, 5566 DAG.getConstant(0, dl, MVT::i32)); 5567 } else { 5568 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res); 5569 } 5570 5571 return Res; 5572 } 5573 5574 // Bitcast operand 1 to i32. 5575 if (SrcVT == MVT::f64) 5576 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 5577 Tmp1).getValue(1); 5578 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1); 5579 5580 // Or in the signbit with integer operations. 5581 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32); 5582 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32); 5583 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1); 5584 if (VT == MVT::f32) { 5585 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32, 5586 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2); 5587 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, 5588 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1)); 5589 } 5590 5591 // f64: Or the high part with signbit and then combine two parts. 5592 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 5593 Tmp0); 5594 SDValue Lo = Tmp0.getValue(0); 5595 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2); 5596 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1); 5597 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 5598 } 5599 5600 SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{ 5601 MachineFunction &MF = DAG.getMachineFunction(); 5602 MachineFrameInfo &MFI = MF.getFrameInfo(); 5603 MFI.setReturnAddressIsTaken(true); 5604 5605 if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 5606 return SDValue(); 5607 5608 EVT VT = Op.getValueType(); 5609 SDLoc dl(Op); 5610 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 5611 if (Depth) { 5612 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 5613 SDValue Offset = DAG.getConstant(4, dl, MVT::i32); 5614 return DAG.getLoad(VT, dl, DAG.getEntryNode(), 5615 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset), 5616 MachinePointerInfo()); 5617 } 5618 5619 // Return LR, which contains the return address. Mark it an implicit live-in. 5620 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32)); 5621 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT); 5622 } 5623 5624 SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { 5625 const ARMBaseRegisterInfo &ARI = 5626 *static_cast<const ARMBaseRegisterInfo*>(RegInfo); 5627 MachineFunction &MF = DAG.getMachineFunction(); 5628 MachineFrameInfo &MFI = MF.getFrameInfo(); 5629 MFI.setFrameAddressIsTaken(true); 5630 5631 EVT VT = Op.getValueType(); 5632 SDLoc dl(Op); // FIXME probably not meaningful 5633 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 5634 Register FrameReg = ARI.getFrameRegister(MF); 5635 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); 5636 while (Depth--) 5637 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, 5638 MachinePointerInfo()); 5639 return FrameAddr; 5640 } 5641 5642 // FIXME? Maybe this could be a TableGen attribute on some registers and 5643 // this table could be generated automatically from RegInfo. 5644 Register ARMTargetLowering::getRegisterByName(const char* RegName, LLT VT, 5645 const MachineFunction &MF) const { 5646 Register Reg = StringSwitch<unsigned>(RegName) 5647 .Case("sp", ARM::SP) 5648 .Default(0); 5649 if (Reg) 5650 return Reg; 5651 report_fatal_error(Twine("Invalid register name \"" 5652 + StringRef(RegName) + "\".")); 5653 } 5654 5655 // Result is 64 bit value so split into two 32 bit values and return as a 5656 // pair of values. 5657 static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results, 5658 SelectionDAG &DAG) { 5659 SDLoc DL(N); 5660 5661 // This function is only supposed to be called for i64 type destination. 5662 assert(N->getValueType(0) == MVT::i64 5663 && "ExpandREAD_REGISTER called for non-i64 type result."); 5664 5665 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL, 5666 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other), 5667 N->getOperand(0), 5668 N->getOperand(1)); 5669 5670 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0), 5671 Read.getValue(1))); 5672 Results.push_back(Read.getOperand(0)); 5673 } 5674 5675 /// \p BC is a bitcast that is about to be turned into a VMOVDRR. 5676 /// When \p DstVT, the destination type of \p BC, is on the vector 5677 /// register bank and the source of bitcast, \p Op, operates on the same bank, 5678 /// it might be possible to combine them, such that everything stays on the 5679 /// vector register bank. 5680 /// \p return The node that would replace \p BT, if the combine 5681 /// is possible. 5682 static SDValue CombineVMOVDRRCandidateWithVecOp(const SDNode *BC, 5683 SelectionDAG &DAG) { 5684 SDValue Op = BC->getOperand(0); 5685 EVT DstVT = BC->getValueType(0); 5686 5687 // The only vector instruction that can produce a scalar (remember, 5688 // since the bitcast was about to be turned into VMOVDRR, the source 5689 // type is i64) from a vector is EXTRACT_VECTOR_ELT. 5690 // Moreover, we can do this combine only if there is one use. 5691 // Finally, if the destination type is not a vector, there is not 5692 // much point on forcing everything on the vector bank. 5693 if (!DstVT.isVector() || Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT || 5694 !Op.hasOneUse()) 5695 return SDValue(); 5696 5697 // If the index is not constant, we will introduce an additional 5698 // multiply that will stick. 5699 // Give up in that case. 5700 ConstantSDNode *Index = dyn_cast<ConstantSDNode>(Op.getOperand(1)); 5701 if (!Index) 5702 return SDValue(); 5703 unsigned DstNumElt = DstVT.getVectorNumElements(); 5704 5705 // Compute the new index. 5706 const APInt &APIntIndex = Index->getAPIntValue(); 5707 APInt NewIndex(APIntIndex.getBitWidth(), DstNumElt); 5708 NewIndex *= APIntIndex; 5709 // Check if the new constant index fits into i32. 5710 if (NewIndex.getBitWidth() > 32) 5711 return SDValue(); 5712 5713 // vMTy bitcast(i64 extractelt vNi64 src, i32 index) -> 5714 // vMTy extractsubvector vNxMTy (bitcast vNi64 src), i32 index*M) 5715 SDLoc dl(Op); 5716 SDValue ExtractSrc = Op.getOperand(0); 5717 EVT VecVT = EVT::getVectorVT( 5718 *DAG.getContext(), DstVT.getScalarType(), 5719 ExtractSrc.getValueType().getVectorNumElements() * DstNumElt); 5720 SDValue BitCast = DAG.getNode(ISD::BITCAST, dl, VecVT, ExtractSrc); 5721 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DstVT, BitCast, 5722 DAG.getConstant(NewIndex.getZExtValue(), dl, MVT::i32)); 5723 } 5724 5725 /// ExpandBITCAST - If the target supports VFP, this function is called to 5726 /// expand a bit convert where either the source or destination type is i64 to 5727 /// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64 5728 /// operand type is illegal (e.g., v2f32 for a target that doesn't support 5729 /// vectors), since the legalizer won't know what to do with that. 5730 static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG, 5731 const ARMSubtarget *Subtarget) { 5732 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 5733 SDLoc dl(N); 5734 SDValue Op = N->getOperand(0); 5735 5736 // This function is only supposed to be called for i64 types, either as the 5737 // source or destination of the bit convert. 5738 EVT SrcVT = Op.getValueType(); 5739 EVT DstVT = N->getValueType(0); 5740 const bool HasFullFP16 = Subtarget->hasFullFP16(); 5741 5742 if (SrcVT == MVT::f32 && DstVT == MVT::i32) { 5743 // FullFP16: half values are passed in S-registers, and we don't 5744 // need any of the bitcast and moves: 5745 // 5746 // t2: f32,ch = CopyFromReg t0, Register:f32 %0 5747 // t5: i32 = bitcast t2 5748 // t18: f16 = ARMISD::VMOVhr t5 5749 if (Op.getOpcode() != ISD::CopyFromReg || 5750 Op.getValueType() != MVT::f32) 5751 return SDValue(); 5752 5753 auto Move = N->use_begin(); 5754 if (Move->getOpcode() != ARMISD::VMOVhr) 5755 return SDValue(); 5756 5757 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) }; 5758 SDValue Copy = DAG.getNode(ISD::CopyFromReg, SDLoc(Op), MVT::f16, Ops); 5759 DAG.ReplaceAllUsesWith(*Move, &Copy); 5760 return Copy; 5761 } 5762 5763 if (SrcVT == MVT::i16 && DstVT == MVT::f16) { 5764 if (!HasFullFP16) 5765 return SDValue(); 5766 // SoftFP: read half-precision arguments: 5767 // 5768 // t2: i32,ch = ... 5769 // t7: i16 = truncate t2 <~~~~ Op 5770 // t8: f16 = bitcast t7 <~~~~ N 5771 // 5772 if (Op.getOperand(0).getValueType() == MVT::i32) 5773 return DAG.getNode(ARMISD::VMOVhr, SDLoc(Op), 5774 MVT::f16, Op.getOperand(0)); 5775 5776 return SDValue(); 5777 } 5778 5779 // Half-precision return values 5780 if (SrcVT == MVT::f16 && DstVT == MVT::i16) { 5781 if (!HasFullFP16) 5782 return SDValue(); 5783 // 5784 // t11: f16 = fadd t8, t10 5785 // t12: i16 = bitcast t11 <~~~ SDNode N 5786 // t13: i32 = zero_extend t12 5787 // t16: ch,glue = CopyToReg t0, Register:i32 %r0, t13 5788 // t17: ch = ARMISD::RET_FLAG t16, Register:i32 %r0, t16:1 5789 // 5790 // transform this into: 5791 // 5792 // t20: i32 = ARMISD::VMOVrh t11 5793 // t16: ch,glue = CopyToReg t0, Register:i32 %r0, t20 5794 // 5795 auto ZeroExtend = N->use_begin(); 5796 if (N->use_size() != 1 || ZeroExtend->getOpcode() != ISD::ZERO_EXTEND || 5797 ZeroExtend->getValueType(0) != MVT::i32) 5798 return SDValue(); 5799 5800 auto Copy = ZeroExtend->use_begin(); 5801 if (Copy->getOpcode() == ISD::CopyToReg && 5802 Copy->use_begin()->getOpcode() == ARMISD::RET_FLAG) { 5803 SDValue Cvt = DAG.getNode(ARMISD::VMOVrh, SDLoc(Op), MVT::i32, Op); 5804 DAG.ReplaceAllUsesWith(*ZeroExtend, &Cvt); 5805 return Cvt; 5806 } 5807 return SDValue(); 5808 } 5809 5810 if (!(SrcVT == MVT::i64 || DstVT == MVT::i64)) 5811 return SDValue(); 5812 5813 // Turn i64->f64 into VMOVDRR. 5814 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) { 5815 // Do not force values to GPRs (this is what VMOVDRR does for the inputs) 5816 // if we can combine the bitcast with its source. 5817 if (SDValue Val = CombineVMOVDRRCandidateWithVecOp(N, DAG)) 5818 return Val; 5819 5820 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 5821 DAG.getConstant(0, dl, MVT::i32)); 5822 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 5823 DAG.getConstant(1, dl, MVT::i32)); 5824 return DAG.getNode(ISD::BITCAST, dl, DstVT, 5825 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi)); 5826 } 5827 5828 // Turn f64->i64 into VMOVRRD. 5829 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) { 5830 SDValue Cvt; 5831 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() && 5832 SrcVT.getVectorNumElements() > 1) 5833 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl, 5834 DAG.getVTList(MVT::i32, MVT::i32), 5835 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op)); 5836 else 5837 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl, 5838 DAG.getVTList(MVT::i32, MVT::i32), Op); 5839 // Merge the pieces into a single i64 value. 5840 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1)); 5841 } 5842 5843 return SDValue(); 5844 } 5845 5846 /// getZeroVector - Returns a vector of specified type with all zero elements. 5847 /// Zero vectors are used to represent vector negation and in those cases 5848 /// will be implemented with the NEON VNEG instruction. However, VNEG does 5849 /// not support i64 elements, so sometimes the zero vectors will need to be 5850 /// explicitly constructed. Regardless, use a canonical VMOV to create the 5851 /// zero vector. 5852 static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, const SDLoc &dl) { 5853 assert(VT.isVector() && "Expected a vector type"); 5854 // The canonical modified immediate encoding of a zero vector is....0! 5855 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32); 5856 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 5857 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal); 5858 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 5859 } 5860 5861 /// LowerShiftRightParts - Lower SRA_PARTS, which returns two 5862 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 5863 SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op, 5864 SelectionDAG &DAG) const { 5865 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 5866 EVT VT = Op.getValueType(); 5867 unsigned VTBits = VT.getSizeInBits(); 5868 SDLoc dl(Op); 5869 SDValue ShOpLo = Op.getOperand(0); 5870 SDValue ShOpHi = Op.getOperand(1); 5871 SDValue ShAmt = Op.getOperand(2); 5872 SDValue ARMcc; 5873 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 5874 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL; 5875 5876 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS); 5877 5878 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 5879 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt); 5880 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt); 5881 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 5882 DAG.getConstant(VTBits, dl, MVT::i32)); 5883 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt); 5884 SDValue LoSmallShift = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 5885 SDValue LoBigShift = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt); 5886 SDValue CmpLo = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 5887 ISD::SETGE, ARMcc, DAG, dl); 5888 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, LoSmallShift, LoBigShift, 5889 ARMcc, CCR, CmpLo); 5890 5891 SDValue HiSmallShift = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt); 5892 SDValue HiBigShift = Opc == ISD::SRA 5893 ? DAG.getNode(Opc, dl, VT, ShOpHi, 5894 DAG.getConstant(VTBits - 1, dl, VT)) 5895 : DAG.getConstant(0, dl, VT); 5896 SDValue CmpHi = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 5897 ISD::SETGE, ARMcc, DAG, dl); 5898 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, HiSmallShift, HiBigShift, 5899 ARMcc, CCR, CmpHi); 5900 5901 SDValue Ops[2] = { Lo, Hi }; 5902 return DAG.getMergeValues(Ops, dl); 5903 } 5904 5905 /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two 5906 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 5907 SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op, 5908 SelectionDAG &DAG) const { 5909 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 5910 EVT VT = Op.getValueType(); 5911 unsigned VTBits = VT.getSizeInBits(); 5912 SDLoc dl(Op); 5913 SDValue ShOpLo = Op.getOperand(0); 5914 SDValue ShOpHi = Op.getOperand(1); 5915 SDValue ShAmt = Op.getOperand(2); 5916 SDValue ARMcc; 5917 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 5918 5919 assert(Op.getOpcode() == ISD::SHL_PARTS); 5920 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 5921 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt); 5922 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt); 5923 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt); 5924 SDValue HiSmallShift = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 5925 5926 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 5927 DAG.getConstant(VTBits, dl, MVT::i32)); 5928 SDValue HiBigShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt); 5929 SDValue CmpHi = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 5930 ISD::SETGE, ARMcc, DAG, dl); 5931 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, HiSmallShift, HiBigShift, 5932 ARMcc, CCR, CmpHi); 5933 5934 SDValue CmpLo = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 5935 ISD::SETGE, ARMcc, DAG, dl); 5936 SDValue LoSmallShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt); 5937 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, LoSmallShift, 5938 DAG.getConstant(0, dl, VT), ARMcc, CCR, CmpLo); 5939 5940 SDValue Ops[2] = { Lo, Hi }; 5941 return DAG.getMergeValues(Ops, dl); 5942 } 5943 5944 SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op, 5945 SelectionDAG &DAG) const { 5946 // The rounding mode is in bits 23:22 of the FPSCR. 5947 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0 5948 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3) 5949 // so that the shift + and get folded into a bitfield extract. 5950 SDLoc dl(Op); 5951 SDValue Ops[] = { DAG.getEntryNode(), 5952 DAG.getConstant(Intrinsic::arm_get_fpscr, dl, MVT::i32) }; 5953 5954 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_W_CHAIN, dl, MVT::i32, Ops); 5955 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR, 5956 DAG.getConstant(1U << 22, dl, MVT::i32)); 5957 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds, 5958 DAG.getConstant(22, dl, MVT::i32)); 5959 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE, 5960 DAG.getConstant(3, dl, MVT::i32)); 5961 } 5962 5963 static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG, 5964 const ARMSubtarget *ST) { 5965 SDLoc dl(N); 5966 EVT VT = N->getValueType(0); 5967 if (VT.isVector() && ST->hasNEON()) { 5968 5969 // Compute the least significant set bit: LSB = X & -X 5970 SDValue X = N->getOperand(0); 5971 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X); 5972 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX); 5973 5974 EVT ElemTy = VT.getVectorElementType(); 5975 5976 if (ElemTy == MVT::i8) { 5977 // Compute with: cttz(x) = ctpop(lsb - 1) 5978 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT, 5979 DAG.getTargetConstant(1, dl, ElemTy)); 5980 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One); 5981 return DAG.getNode(ISD::CTPOP, dl, VT, Bits); 5982 } 5983 5984 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) && 5985 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) { 5986 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0 5987 unsigned NumBits = ElemTy.getSizeInBits(); 5988 SDValue WidthMinus1 = 5989 DAG.getNode(ARMISD::VMOVIMM, dl, VT, 5990 DAG.getTargetConstant(NumBits - 1, dl, ElemTy)); 5991 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB); 5992 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ); 5993 } 5994 5995 // Compute with: cttz(x) = ctpop(lsb - 1) 5996 5997 // Compute LSB - 1. 5998 SDValue Bits; 5999 if (ElemTy == MVT::i64) { 6000 // Load constant 0xffff'ffff'ffff'ffff to register. 6001 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT, 6002 DAG.getTargetConstant(0x1eff, dl, MVT::i32)); 6003 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF); 6004 } else { 6005 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT, 6006 DAG.getTargetConstant(1, dl, ElemTy)); 6007 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One); 6008 } 6009 return DAG.getNode(ISD::CTPOP, dl, VT, Bits); 6010 } 6011 6012 if (!ST->hasV6T2Ops()) 6013 return SDValue(); 6014 6015 SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, VT, N->getOperand(0)); 6016 return DAG.getNode(ISD::CTLZ, dl, VT, rbit); 6017 } 6018 6019 static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG, 6020 const ARMSubtarget *ST) { 6021 EVT VT = N->getValueType(0); 6022 SDLoc DL(N); 6023 6024 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON."); 6025 assert((VT == MVT::v1i64 || VT == MVT::v2i64 || VT == MVT::v2i32 || 6026 VT == MVT::v4i32 || VT == MVT::v4i16 || VT == MVT::v8i16) && 6027 "Unexpected type for custom ctpop lowering"); 6028 6029 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 6030 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8; 6031 SDValue Res = DAG.getBitcast(VT8Bit, N->getOperand(0)); 6032 Res = DAG.getNode(ISD::CTPOP, DL, VT8Bit, Res); 6033 6034 // Widen v8i8/v16i8 CTPOP result to VT by repeatedly widening pairwise adds. 6035 unsigned EltSize = 8; 6036 unsigned NumElts = VT.is64BitVector() ? 8 : 16; 6037 while (EltSize != VT.getScalarSizeInBits()) { 6038 SmallVector<SDValue, 8> Ops; 6039 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddlu, DL, 6040 TLI.getPointerTy(DAG.getDataLayout()))); 6041 Ops.push_back(Res); 6042 6043 EltSize *= 2; 6044 NumElts /= 2; 6045 MVT WidenVT = MVT::getVectorVT(MVT::getIntegerVT(EltSize), NumElts); 6046 Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, WidenVT, Ops); 6047 } 6048 6049 return Res; 6050 } 6051 6052 /// Getvshiftimm - Check if this is a valid build_vector for the immediate 6053 /// operand of a vector shift operation, where all the elements of the 6054 /// build_vector must have the same constant integer value. 6055 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) { 6056 // Ignore bit_converts. 6057 while (Op.getOpcode() == ISD::BITCAST) 6058 Op = Op.getOperand(0); 6059 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); 6060 APInt SplatBits, SplatUndef; 6061 unsigned SplatBitSize; 6062 bool HasAnyUndefs; 6063 if (!BVN || 6064 !BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs, 6065 ElementBits) || 6066 SplatBitSize > ElementBits) 6067 return false; 6068 Cnt = SplatBits.getSExtValue(); 6069 return true; 6070 } 6071 6072 /// isVShiftLImm - Check if this is a valid build_vector for the immediate 6073 /// operand of a vector shift left operation. That value must be in the range: 6074 /// 0 <= Value < ElementBits for a left shift; or 6075 /// 0 <= Value <= ElementBits for a long left shift. 6076 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) { 6077 assert(VT.isVector() && "vector shift count is not a vector type"); 6078 int64_t ElementBits = VT.getScalarSizeInBits(); 6079 if (!getVShiftImm(Op, ElementBits, Cnt)) 6080 return false; 6081 return (Cnt >= 0 && (isLong ? Cnt - 1 : Cnt) < ElementBits); 6082 } 6083 6084 /// isVShiftRImm - Check if this is a valid build_vector for the immediate 6085 /// operand of a vector shift right operation. For a shift opcode, the value 6086 /// is positive, but for an intrinsic the value count must be negative. The 6087 /// absolute value must be in the range: 6088 /// 1 <= |Value| <= ElementBits for a right shift; or 6089 /// 1 <= |Value| <= ElementBits/2 for a narrow right shift. 6090 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic, 6091 int64_t &Cnt) { 6092 assert(VT.isVector() && "vector shift count is not a vector type"); 6093 int64_t ElementBits = VT.getScalarSizeInBits(); 6094 if (!getVShiftImm(Op, ElementBits, Cnt)) 6095 return false; 6096 if (!isIntrinsic) 6097 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits / 2 : ElementBits)); 6098 if (Cnt >= -(isNarrow ? ElementBits / 2 : ElementBits) && Cnt <= -1) { 6099 Cnt = -Cnt; 6100 return true; 6101 } 6102 return false; 6103 } 6104 6105 static SDValue LowerShift(SDNode *N, SelectionDAG &DAG, 6106 const ARMSubtarget *ST) { 6107 EVT VT = N->getValueType(0); 6108 SDLoc dl(N); 6109 int64_t Cnt; 6110 6111 if (!VT.isVector()) 6112 return SDValue(); 6113 6114 // We essentially have two forms here. Shift by an immediate and shift by a 6115 // vector register (there are also shift by a gpr, but that is just handled 6116 // with a tablegen pattern). We cannot easily match shift by an immediate in 6117 // tablegen so we do that here and generate a VSHLIMM/VSHRsIMM/VSHRuIMM. 6118 // For shifting by a vector, we don't have VSHR, only VSHL (which can be 6119 // signed or unsigned, and a negative shift indicates a shift right). 6120 if (N->getOpcode() == ISD::SHL) { 6121 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) 6122 return DAG.getNode(ARMISD::VSHLIMM, dl, VT, N->getOperand(0), 6123 DAG.getConstant(Cnt, dl, MVT::i32)); 6124 return DAG.getNode(ARMISD::VSHLu, dl, VT, N->getOperand(0), 6125 N->getOperand(1)); 6126 } 6127 6128 assert((N->getOpcode() == ISD::SRA || N->getOpcode() == ISD::SRL) && 6129 "unexpected vector shift opcode"); 6130 6131 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) { 6132 unsigned VShiftOpc = 6133 (N->getOpcode() == ISD::SRA ? ARMISD::VSHRsIMM : ARMISD::VSHRuIMM); 6134 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0), 6135 DAG.getConstant(Cnt, dl, MVT::i32)); 6136 } 6137 6138 // Other right shifts we don't have operations for (we use a shift left by a 6139 // negative number). 6140 EVT ShiftVT = N->getOperand(1).getValueType(); 6141 SDValue NegatedCount = DAG.getNode( 6142 ISD::SUB, dl, ShiftVT, getZeroVector(ShiftVT, DAG, dl), N->getOperand(1)); 6143 unsigned VShiftOpc = 6144 (N->getOpcode() == ISD::SRA ? ARMISD::VSHLs : ARMISD::VSHLu); 6145 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0), NegatedCount); 6146 } 6147 6148 static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG, 6149 const ARMSubtarget *ST) { 6150 EVT VT = N->getValueType(0); 6151 SDLoc dl(N); 6152 6153 // We can get here for a node like i32 = ISD::SHL i32, i64 6154 if (VT != MVT::i64) 6155 return SDValue(); 6156 6157 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA || 6158 N->getOpcode() == ISD::SHL) && 6159 "Unknown shift to lower!"); 6160 6161 unsigned ShOpc = N->getOpcode(); 6162 if (ST->hasMVEIntegerOps()) { 6163 SDValue ShAmt = N->getOperand(1); 6164 unsigned ShPartsOpc = ARMISD::LSLL; 6165 ConstantSDNode *Con = dyn_cast<ConstantSDNode>(ShAmt); 6166 6167 // If the shift amount is greater than 32 or has a greater bitwidth than 64 6168 // then do the default optimisation 6169 if (ShAmt->getValueType(0).getSizeInBits() > 64 || 6170 (Con && (Con->getZExtValue() == 0 || Con->getZExtValue() >= 32))) 6171 return SDValue(); 6172 6173 // Extract the lower 32 bits of the shift amount if it's not an i32 6174 if (ShAmt->getValueType(0) != MVT::i32) 6175 ShAmt = DAG.getZExtOrTrunc(ShAmt, dl, MVT::i32); 6176 6177 if (ShOpc == ISD::SRL) { 6178 if (!Con) 6179 // There is no t2LSRLr instruction so negate and perform an lsll if the 6180 // shift amount is in a register, emulating a right shift. 6181 ShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 6182 DAG.getConstant(0, dl, MVT::i32), ShAmt); 6183 else 6184 // Else generate an lsrl on the immediate shift amount 6185 ShPartsOpc = ARMISD::LSRL; 6186 } else if (ShOpc == ISD::SRA) 6187 ShPartsOpc = ARMISD::ASRL; 6188 6189 // Lower 32 bits of the destination/source 6190 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 6191 DAG.getConstant(0, dl, MVT::i32)); 6192 // Upper 32 bits of the destination/source 6193 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 6194 DAG.getConstant(1, dl, MVT::i32)); 6195 6196 // Generate the shift operation as computed above 6197 Lo = DAG.getNode(ShPartsOpc, dl, DAG.getVTList(MVT::i32, MVT::i32), Lo, Hi, 6198 ShAmt); 6199 // The upper 32 bits come from the second return value of lsll 6200 Hi = SDValue(Lo.getNode(), 1); 6201 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi); 6202 } 6203 6204 // We only lower SRA, SRL of 1 here, all others use generic lowering. 6205 if (!isOneConstant(N->getOperand(1)) || N->getOpcode() == ISD::SHL) 6206 return SDValue(); 6207 6208 // If we are in thumb mode, we don't have RRX. 6209 if (ST->isThumb1Only()) 6210 return SDValue(); 6211 6212 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr. 6213 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 6214 DAG.getConstant(0, dl, MVT::i32)); 6215 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 6216 DAG.getConstant(1, dl, MVT::i32)); 6217 6218 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and 6219 // captures the result into a carry flag. 6220 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG; 6221 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi); 6222 6223 // The low part is an ARMISD::RRX operand, which shifts the carry in. 6224 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1)); 6225 6226 // Merge the pieces into a single i64 value. 6227 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi); 6228 } 6229 6230 static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG, 6231 const ARMSubtarget *ST) { 6232 bool Invert = false; 6233 bool Swap = false; 6234 unsigned Opc = ARMCC::AL; 6235 6236 SDValue Op0 = Op.getOperand(0); 6237 SDValue Op1 = Op.getOperand(1); 6238 SDValue CC = Op.getOperand(2); 6239 EVT VT = Op.getValueType(); 6240 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); 6241 SDLoc dl(Op); 6242 6243 EVT CmpVT; 6244 if (ST->hasNEON()) 6245 CmpVT = Op0.getValueType().changeVectorElementTypeToInteger(); 6246 else { 6247 assert(ST->hasMVEIntegerOps() && 6248 "No hardware support for integer vector comparison!"); 6249 6250 if (Op.getValueType().getVectorElementType() != MVT::i1) 6251 return SDValue(); 6252 6253 // Make sure we expand floating point setcc to scalar if we do not have 6254 // mve.fp, so that we can handle them from there. 6255 if (Op0.getValueType().isFloatingPoint() && !ST->hasMVEFloatOps()) 6256 return SDValue(); 6257 6258 CmpVT = VT; 6259 } 6260 6261 if (Op0.getValueType().getVectorElementType() == MVT::i64 && 6262 (SetCCOpcode == ISD::SETEQ || SetCCOpcode == ISD::SETNE)) { 6263 // Special-case integer 64-bit equality comparisons. They aren't legal, 6264 // but they can be lowered with a few vector instructions. 6265 unsigned CmpElements = CmpVT.getVectorNumElements() * 2; 6266 EVT SplitVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, CmpElements); 6267 SDValue CastOp0 = DAG.getNode(ISD::BITCAST, dl, SplitVT, Op0); 6268 SDValue CastOp1 = DAG.getNode(ISD::BITCAST, dl, SplitVT, Op1); 6269 SDValue Cmp = DAG.getNode(ISD::SETCC, dl, SplitVT, CastOp0, CastOp1, 6270 DAG.getCondCode(ISD::SETEQ)); 6271 SDValue Reversed = DAG.getNode(ARMISD::VREV64, dl, SplitVT, Cmp); 6272 SDValue Merged = DAG.getNode(ISD::AND, dl, SplitVT, Cmp, Reversed); 6273 Merged = DAG.getNode(ISD::BITCAST, dl, CmpVT, Merged); 6274 if (SetCCOpcode == ISD::SETNE) 6275 Merged = DAG.getNOT(dl, Merged, CmpVT); 6276 Merged = DAG.getSExtOrTrunc(Merged, dl, VT); 6277 return Merged; 6278 } 6279 6280 if (CmpVT.getVectorElementType() == MVT::i64) 6281 // 64-bit comparisons are not legal in general. 6282 return SDValue(); 6283 6284 if (Op1.getValueType().isFloatingPoint()) { 6285 switch (SetCCOpcode) { 6286 default: llvm_unreachable("Illegal FP comparison"); 6287 case ISD::SETUNE: 6288 case ISD::SETNE: 6289 if (ST->hasMVEFloatOps()) { 6290 Opc = ARMCC::NE; break; 6291 } else { 6292 Invert = true; LLVM_FALLTHROUGH; 6293 } 6294 case ISD::SETOEQ: 6295 case ISD::SETEQ: Opc = ARMCC::EQ; break; 6296 case ISD::SETOLT: 6297 case ISD::SETLT: Swap = true; LLVM_FALLTHROUGH; 6298 case ISD::SETOGT: 6299 case ISD::SETGT: Opc = ARMCC::GT; break; 6300 case ISD::SETOLE: 6301 case ISD::SETLE: Swap = true; LLVM_FALLTHROUGH; 6302 case ISD::SETOGE: 6303 case ISD::SETGE: Opc = ARMCC::GE; break; 6304 case ISD::SETUGE: Swap = true; LLVM_FALLTHROUGH; 6305 case ISD::SETULE: Invert = true; Opc = ARMCC::GT; break; 6306 case ISD::SETUGT: Swap = true; LLVM_FALLTHROUGH; 6307 case ISD::SETULT: Invert = true; Opc = ARMCC::GE; break; 6308 case ISD::SETUEQ: Invert = true; LLVM_FALLTHROUGH; 6309 case ISD::SETONE: { 6310 // Expand this to (OLT | OGT). 6311 SDValue TmpOp0 = DAG.getNode(ARMISD::VCMP, dl, CmpVT, Op1, Op0, 6312 DAG.getConstant(ARMCC::GT, dl, MVT::i32)); 6313 SDValue TmpOp1 = DAG.getNode(ARMISD::VCMP, dl, CmpVT, Op0, Op1, 6314 DAG.getConstant(ARMCC::GT, dl, MVT::i32)); 6315 SDValue Result = DAG.getNode(ISD::OR, dl, CmpVT, TmpOp0, TmpOp1); 6316 if (Invert) 6317 Result = DAG.getNOT(dl, Result, VT); 6318 return Result; 6319 } 6320 case ISD::SETUO: Invert = true; LLVM_FALLTHROUGH; 6321 case ISD::SETO: { 6322 // Expand this to (OLT | OGE). 6323 SDValue TmpOp0 = DAG.getNode(ARMISD::VCMP, dl, CmpVT, Op1, Op0, 6324 DAG.getConstant(ARMCC::GT, dl, MVT::i32)); 6325 SDValue TmpOp1 = DAG.getNode(ARMISD::VCMP, dl, CmpVT, Op0, Op1, 6326 DAG.getConstant(ARMCC::GE, dl, MVT::i32)); 6327 SDValue Result = DAG.getNode(ISD::OR, dl, CmpVT, TmpOp0, TmpOp1); 6328 if (Invert) 6329 Result = DAG.getNOT(dl, Result, VT); 6330 return Result; 6331 } 6332 } 6333 } else { 6334 // Integer comparisons. 6335 switch (SetCCOpcode) { 6336 default: llvm_unreachable("Illegal integer comparison"); 6337 case ISD::SETNE: 6338 if (ST->hasMVEIntegerOps()) { 6339 Opc = ARMCC::NE; break; 6340 } else { 6341 Invert = true; LLVM_FALLTHROUGH; 6342 } 6343 case ISD::SETEQ: Opc = ARMCC::EQ; break; 6344 case ISD::SETLT: Swap = true; LLVM_FALLTHROUGH; 6345 case ISD::SETGT: Opc = ARMCC::GT; break; 6346 case ISD::SETLE: Swap = true; LLVM_FALLTHROUGH; 6347 case ISD::SETGE: Opc = ARMCC::GE; break; 6348 case ISD::SETULT: Swap = true; LLVM_FALLTHROUGH; 6349 case ISD::SETUGT: Opc = ARMCC::HI; break; 6350 case ISD::SETULE: Swap = true; LLVM_FALLTHROUGH; 6351 case ISD::SETUGE: Opc = ARMCC::HS; break; 6352 } 6353 6354 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero). 6355 if (ST->hasNEON() && Opc == ARMCC::EQ) { 6356 SDValue AndOp; 6357 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 6358 AndOp = Op0; 6359 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) 6360 AndOp = Op1; 6361 6362 // Ignore bitconvert. 6363 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST) 6364 AndOp = AndOp.getOperand(0); 6365 6366 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) { 6367 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0)); 6368 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1)); 6369 SDValue Result = DAG.getNode(ARMISD::VTST, dl, CmpVT, Op0, Op1); 6370 if (!Invert) 6371 Result = DAG.getNOT(dl, Result, VT); 6372 return Result; 6373 } 6374 } 6375 } 6376 6377 if (Swap) 6378 std::swap(Op0, Op1); 6379 6380 // If one of the operands is a constant vector zero, attempt to fold the 6381 // comparison to a specialized compare-against-zero form. 6382 SDValue SingleOp; 6383 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 6384 SingleOp = Op0; 6385 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) { 6386 if (Opc == ARMCC::GE) 6387 Opc = ARMCC::LE; 6388 else if (Opc == ARMCC::GT) 6389 Opc = ARMCC::LT; 6390 SingleOp = Op1; 6391 } 6392 6393 SDValue Result; 6394 if (SingleOp.getNode()) { 6395 Result = DAG.getNode(ARMISD::VCMPZ, dl, CmpVT, SingleOp, 6396 DAG.getConstant(Opc, dl, MVT::i32)); 6397 } else { 6398 Result = DAG.getNode(ARMISD::VCMP, dl, CmpVT, Op0, Op1, 6399 DAG.getConstant(Opc, dl, MVT::i32)); 6400 } 6401 6402 Result = DAG.getSExtOrTrunc(Result, dl, VT); 6403 6404 if (Invert) 6405 Result = DAG.getNOT(dl, Result, VT); 6406 6407 return Result; 6408 } 6409 6410 static SDValue LowerSETCCCARRY(SDValue Op, SelectionDAG &DAG) { 6411 SDValue LHS = Op.getOperand(0); 6412 SDValue RHS = Op.getOperand(1); 6413 SDValue Carry = Op.getOperand(2); 6414 SDValue Cond = Op.getOperand(3); 6415 SDLoc DL(Op); 6416 6417 assert(LHS.getSimpleValueType().isInteger() && "SETCCCARRY is integer only."); 6418 6419 // ARMISD::SUBE expects a carry not a borrow like ISD::SUBCARRY so we 6420 // have to invert the carry first. 6421 Carry = DAG.getNode(ISD::SUB, DL, MVT::i32, 6422 DAG.getConstant(1, DL, MVT::i32), Carry); 6423 // This converts the boolean value carry into the carry flag. 6424 Carry = ConvertBooleanCarryToCarryFlag(Carry, DAG); 6425 6426 SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32); 6427 SDValue Cmp = DAG.getNode(ARMISD::SUBE, DL, VTs, LHS, RHS, Carry); 6428 6429 SDValue FVal = DAG.getConstant(0, DL, MVT::i32); 6430 SDValue TVal = DAG.getConstant(1, DL, MVT::i32); 6431 SDValue ARMcc = DAG.getConstant( 6432 IntCCToARMCC(cast<CondCodeSDNode>(Cond)->get()), DL, MVT::i32); 6433 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 6434 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, ARM::CPSR, 6435 Cmp.getValue(1), SDValue()); 6436 return DAG.getNode(ARMISD::CMOV, DL, Op.getValueType(), FVal, TVal, ARMcc, 6437 CCR, Chain.getValue(1)); 6438 } 6439 6440 /// isVMOVModifiedImm - Check if the specified splat value corresponds to a 6441 /// valid vector constant for a NEON or MVE instruction with a "modified 6442 /// immediate" operand (e.g., VMOV). If so, return the encoded value. 6443 static SDValue isVMOVModifiedImm(uint64_t SplatBits, uint64_t SplatUndef, 6444 unsigned SplatBitSize, SelectionDAG &DAG, 6445 const SDLoc &dl, EVT &VT, bool is128Bits, 6446 VMOVModImmType type) { 6447 unsigned OpCmode, Imm; 6448 6449 // SplatBitSize is set to the smallest size that splats the vector, so a 6450 // zero vector will always have SplatBitSize == 8. However, NEON modified 6451 // immediate instructions others than VMOV do not support the 8-bit encoding 6452 // of a zero vector, and the default encoding of zero is supposed to be the 6453 // 32-bit version. 6454 if (SplatBits == 0) 6455 SplatBitSize = 32; 6456 6457 switch (SplatBitSize) { 6458 case 8: 6459 if (type != VMOVModImm) 6460 return SDValue(); 6461 // Any 1-byte value is OK. Op=0, Cmode=1110. 6462 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big"); 6463 OpCmode = 0xe; 6464 Imm = SplatBits; 6465 VT = is128Bits ? MVT::v16i8 : MVT::v8i8; 6466 break; 6467 6468 case 16: 6469 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero. 6470 VT = is128Bits ? MVT::v8i16 : MVT::v4i16; 6471 if ((SplatBits & ~0xff) == 0) { 6472 // Value = 0x00nn: Op=x, Cmode=100x. 6473 OpCmode = 0x8; 6474 Imm = SplatBits; 6475 break; 6476 } 6477 if ((SplatBits & ~0xff00) == 0) { 6478 // Value = 0xnn00: Op=x, Cmode=101x. 6479 OpCmode = 0xa; 6480 Imm = SplatBits >> 8; 6481 break; 6482 } 6483 return SDValue(); 6484 6485 case 32: 6486 // NEON's 32-bit VMOV supports splat values where: 6487 // * only one byte is nonzero, or 6488 // * the least significant byte is 0xff and the second byte is nonzero, or 6489 // * the least significant 2 bytes are 0xff and the third is nonzero. 6490 VT = is128Bits ? MVT::v4i32 : MVT::v2i32; 6491 if ((SplatBits & ~0xff) == 0) { 6492 // Value = 0x000000nn: Op=x, Cmode=000x. 6493 OpCmode = 0; 6494 Imm = SplatBits; 6495 break; 6496 } 6497 if ((SplatBits & ~0xff00) == 0) { 6498 // Value = 0x0000nn00: Op=x, Cmode=001x. 6499 OpCmode = 0x2; 6500 Imm = SplatBits >> 8; 6501 break; 6502 } 6503 if ((SplatBits & ~0xff0000) == 0) { 6504 // Value = 0x00nn0000: Op=x, Cmode=010x. 6505 OpCmode = 0x4; 6506 Imm = SplatBits >> 16; 6507 break; 6508 } 6509 if ((SplatBits & ~0xff000000) == 0) { 6510 // Value = 0xnn000000: Op=x, Cmode=011x. 6511 OpCmode = 0x6; 6512 Imm = SplatBits >> 24; 6513 break; 6514 } 6515 6516 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC 6517 if (type == OtherModImm) return SDValue(); 6518 6519 if ((SplatBits & ~0xffff) == 0 && 6520 ((SplatBits | SplatUndef) & 0xff) == 0xff) { 6521 // Value = 0x0000nnff: Op=x, Cmode=1100. 6522 OpCmode = 0xc; 6523 Imm = SplatBits >> 8; 6524 break; 6525 } 6526 6527 // cmode == 0b1101 is not supported for MVE VMVN 6528 if (type == MVEVMVNModImm) 6529 return SDValue(); 6530 6531 if ((SplatBits & ~0xffffff) == 0 && 6532 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) { 6533 // Value = 0x00nnffff: Op=x, Cmode=1101. 6534 OpCmode = 0xd; 6535 Imm = SplatBits >> 16; 6536 break; 6537 } 6538 6539 // Note: there are a few 32-bit splat values (specifically: 00ffff00, 6540 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not 6541 // VMOV.I32. A (very) minor optimization would be to replicate the value 6542 // and fall through here to test for a valid 64-bit splat. But, then the 6543 // caller would also need to check and handle the change in size. 6544 return SDValue(); 6545 6546 case 64: { 6547 if (type != VMOVModImm) 6548 return SDValue(); 6549 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff. 6550 uint64_t BitMask = 0xff; 6551 uint64_t Val = 0; 6552 unsigned ImmMask = 1; 6553 Imm = 0; 6554 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) { 6555 if (((SplatBits | SplatUndef) & BitMask) == BitMask) { 6556 Val |= BitMask; 6557 Imm |= ImmMask; 6558 } else if ((SplatBits & BitMask) != 0) { 6559 return SDValue(); 6560 } 6561 BitMask <<= 8; 6562 ImmMask <<= 1; 6563 } 6564 6565 if (DAG.getDataLayout().isBigEndian()) 6566 // swap higher and lower 32 bit word 6567 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4); 6568 6569 // Op=1, Cmode=1110. 6570 OpCmode = 0x1e; 6571 VT = is128Bits ? MVT::v2i64 : MVT::v1i64; 6572 break; 6573 } 6574 6575 default: 6576 llvm_unreachable("unexpected size for isVMOVModifiedImm"); 6577 } 6578 6579 unsigned EncodedVal = ARM_AM::createVMOVModImm(OpCmode, Imm); 6580 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32); 6581 } 6582 6583 SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG, 6584 const ARMSubtarget *ST) const { 6585 EVT VT = Op.getValueType(); 6586 bool IsDouble = (VT == MVT::f64); 6587 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op); 6588 const APFloat &FPVal = CFP->getValueAPF(); 6589 6590 // Prevent floating-point constants from using literal loads 6591 // when execute-only is enabled. 6592 if (ST->genExecuteOnly()) { 6593 // If we can represent the constant as an immediate, don't lower it 6594 if (isFPImmLegal(FPVal, VT)) 6595 return Op; 6596 // Otherwise, construct as integer, and move to float register 6597 APInt INTVal = FPVal.bitcastToAPInt(); 6598 SDLoc DL(CFP); 6599 switch (VT.getSimpleVT().SimpleTy) { 6600 default: 6601 llvm_unreachable("Unknown floating point type!"); 6602 break; 6603 case MVT::f64: { 6604 SDValue Lo = DAG.getConstant(INTVal.trunc(32), DL, MVT::i32); 6605 SDValue Hi = DAG.getConstant(INTVal.lshr(32).trunc(32), DL, MVT::i32); 6606 if (!ST->isLittle()) 6607 std::swap(Lo, Hi); 6608 return DAG.getNode(ARMISD::VMOVDRR, DL, MVT::f64, Lo, Hi); 6609 } 6610 case MVT::f32: 6611 return DAG.getNode(ARMISD::VMOVSR, DL, VT, 6612 DAG.getConstant(INTVal, DL, MVT::i32)); 6613 } 6614 } 6615 6616 if (!ST->hasVFP3Base()) 6617 return SDValue(); 6618 6619 // Use the default (constant pool) lowering for double constants when we have 6620 // an SP-only FPU 6621 if (IsDouble && !Subtarget->hasFP64()) 6622 return SDValue(); 6623 6624 // Try splatting with a VMOV.f32... 6625 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal); 6626 6627 if (ImmVal != -1) { 6628 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) { 6629 // We have code in place to select a valid ConstantFP already, no need to 6630 // do any mangling. 6631 return Op; 6632 } 6633 6634 // It's a float and we are trying to use NEON operations where 6635 // possible. Lower it to a splat followed by an extract. 6636 SDLoc DL(Op); 6637 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32); 6638 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32, 6639 NewVal); 6640 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant, 6641 DAG.getConstant(0, DL, MVT::i32)); 6642 } 6643 6644 // The rest of our options are NEON only, make sure that's allowed before 6645 // proceeding.. 6646 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP())) 6647 return SDValue(); 6648 6649 EVT VMovVT; 6650 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue(); 6651 6652 // It wouldn't really be worth bothering for doubles except for one very 6653 // important value, which does happen to match: 0.0. So make sure we don't do 6654 // anything stupid. 6655 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32)) 6656 return SDValue(); 6657 6658 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too). 6659 SDValue NewVal = isVMOVModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), 6660 VMovVT, false, VMOVModImm); 6661 if (NewVal != SDValue()) { 6662 SDLoc DL(Op); 6663 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT, 6664 NewVal); 6665 if (IsDouble) 6666 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant); 6667 6668 // It's a float: cast and extract a vector element. 6669 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 6670 VecConstant); 6671 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 6672 DAG.getConstant(0, DL, MVT::i32)); 6673 } 6674 6675 // Finally, try a VMVN.i32 6676 NewVal = isVMOVModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT, 6677 false, VMVNModImm); 6678 if (NewVal != SDValue()) { 6679 SDLoc DL(Op); 6680 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal); 6681 6682 if (IsDouble) 6683 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant); 6684 6685 // It's a float: cast and extract a vector element. 6686 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 6687 VecConstant); 6688 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 6689 DAG.getConstant(0, DL, MVT::i32)); 6690 } 6691 6692 return SDValue(); 6693 } 6694 6695 // check if an VEXT instruction can handle the shuffle mask when the 6696 // vector sources of the shuffle are the same. 6697 static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) { 6698 unsigned NumElts = VT.getVectorNumElements(); 6699 6700 // Assume that the first shuffle index is not UNDEF. Fail if it is. 6701 if (M[0] < 0) 6702 return false; 6703 6704 Imm = M[0]; 6705 6706 // If this is a VEXT shuffle, the immediate value is the index of the first 6707 // element. The other shuffle indices must be the successive elements after 6708 // the first one. 6709 unsigned ExpectedElt = Imm; 6710 for (unsigned i = 1; i < NumElts; ++i) { 6711 // Increment the expected index. If it wraps around, just follow it 6712 // back to index zero and keep going. 6713 ++ExpectedElt; 6714 if (ExpectedElt == NumElts) 6715 ExpectedElt = 0; 6716 6717 if (M[i] < 0) continue; // ignore UNDEF indices 6718 if (ExpectedElt != static_cast<unsigned>(M[i])) 6719 return false; 6720 } 6721 6722 return true; 6723 } 6724 6725 static bool isVEXTMask(ArrayRef<int> M, EVT VT, 6726 bool &ReverseVEXT, unsigned &Imm) { 6727 unsigned NumElts = VT.getVectorNumElements(); 6728 ReverseVEXT = false; 6729 6730 // Assume that the first shuffle index is not UNDEF. Fail if it is. 6731 if (M[0] < 0) 6732 return false; 6733 6734 Imm = M[0]; 6735 6736 // If this is a VEXT shuffle, the immediate value is the index of the first 6737 // element. The other shuffle indices must be the successive elements after 6738 // the first one. 6739 unsigned ExpectedElt = Imm; 6740 for (unsigned i = 1; i < NumElts; ++i) { 6741 // Increment the expected index. If it wraps around, it may still be 6742 // a VEXT but the source vectors must be swapped. 6743 ExpectedElt += 1; 6744 if (ExpectedElt == NumElts * 2) { 6745 ExpectedElt = 0; 6746 ReverseVEXT = true; 6747 } 6748 6749 if (M[i] < 0) continue; // ignore UNDEF indices 6750 if (ExpectedElt != static_cast<unsigned>(M[i])) 6751 return false; 6752 } 6753 6754 // Adjust the index value if the source operands will be swapped. 6755 if (ReverseVEXT) 6756 Imm -= NumElts; 6757 6758 return true; 6759 } 6760 6761 /// isVREVMask - Check if a vector shuffle corresponds to a VREV 6762 /// instruction with the specified blocksize. (The order of the elements 6763 /// within each block of the vector is reversed.) 6764 static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) { 6765 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) && 6766 "Only possible block sizes for VREV are: 16, 32, 64"); 6767 6768 unsigned EltSz = VT.getScalarSizeInBits(); 6769 if (EltSz == 64) 6770 return false; 6771 6772 unsigned NumElts = VT.getVectorNumElements(); 6773 unsigned BlockElts = M[0] + 1; 6774 // If the first shuffle index is UNDEF, be optimistic. 6775 if (M[0] < 0) 6776 BlockElts = BlockSize / EltSz; 6777 6778 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz) 6779 return false; 6780 6781 for (unsigned i = 0; i < NumElts; ++i) { 6782 if (M[i] < 0) continue; // ignore UNDEF indices 6783 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts)) 6784 return false; 6785 } 6786 6787 return true; 6788 } 6789 6790 static bool isVTBLMask(ArrayRef<int> M, EVT VT) { 6791 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of 6792 // range, then 0 is placed into the resulting vector. So pretty much any mask 6793 // of 8 elements can work here. 6794 return VT == MVT::v8i8 && M.size() == 8; 6795 } 6796 6797 static unsigned SelectPairHalf(unsigned Elements, ArrayRef<int> Mask, 6798 unsigned Index) { 6799 if (Mask.size() == Elements * 2) 6800 return Index / Elements; 6801 return Mask[Index] == 0 ? 0 : 1; 6802 } 6803 6804 // Checks whether the shuffle mask represents a vector transpose (VTRN) by 6805 // checking that pairs of elements in the shuffle mask represent the same index 6806 // in each vector, incrementing the expected index by 2 at each step. 6807 // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6] 6808 // v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g} 6809 // v2={e,f,g,h} 6810 // WhichResult gives the offset for each element in the mask based on which 6811 // of the two results it belongs to. 6812 // 6813 // The transpose can be represented either as: 6814 // result1 = shufflevector v1, v2, result1_shuffle_mask 6815 // result2 = shufflevector v1, v2, result2_shuffle_mask 6816 // where v1/v2 and the shuffle masks have the same number of elements 6817 // (here WhichResult (see below) indicates which result is being checked) 6818 // 6819 // or as: 6820 // results = shufflevector v1, v2, shuffle_mask 6821 // where both results are returned in one vector and the shuffle mask has twice 6822 // as many elements as v1/v2 (here WhichResult will always be 0 if true) here we 6823 // want to check the low half and high half of the shuffle mask as if it were 6824 // the other case 6825 static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 6826 unsigned EltSz = VT.getScalarSizeInBits(); 6827 if (EltSz == 64) 6828 return false; 6829 6830 unsigned NumElts = VT.getVectorNumElements(); 6831 if (M.size() != NumElts && M.size() != NumElts*2) 6832 return false; 6833 6834 // If the mask is twice as long as the input vector then we need to check the 6835 // upper and lower parts of the mask with a matching value for WhichResult 6836 // FIXME: A mask with only even values will be rejected in case the first 6837 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only 6838 // M[0] is used to determine WhichResult 6839 for (unsigned i = 0; i < M.size(); i += NumElts) { 6840 WhichResult = SelectPairHalf(NumElts, M, i); 6841 for (unsigned j = 0; j < NumElts; j += 2) { 6842 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) || 6843 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult)) 6844 return false; 6845 } 6846 } 6847 6848 if (M.size() == NumElts*2) 6849 WhichResult = 0; 6850 6851 return true; 6852 } 6853 6854 /// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of 6855 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 6856 /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>. 6857 static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 6858 unsigned EltSz = VT.getScalarSizeInBits(); 6859 if (EltSz == 64) 6860 return false; 6861 6862 unsigned NumElts = VT.getVectorNumElements(); 6863 if (M.size() != NumElts && M.size() != NumElts*2) 6864 return false; 6865 6866 for (unsigned i = 0; i < M.size(); i += NumElts) { 6867 WhichResult = SelectPairHalf(NumElts, M, i); 6868 for (unsigned j = 0; j < NumElts; j += 2) { 6869 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) || 6870 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult)) 6871 return false; 6872 } 6873 } 6874 6875 if (M.size() == NumElts*2) 6876 WhichResult = 0; 6877 6878 return true; 6879 } 6880 6881 // Checks whether the shuffle mask represents a vector unzip (VUZP) by checking 6882 // that the mask elements are either all even and in steps of size 2 or all odd 6883 // and in steps of size 2. 6884 // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6] 6885 // v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g} 6886 // v2={e,f,g,h} 6887 // Requires similar checks to that of isVTRNMask with 6888 // respect the how results are returned. 6889 static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 6890 unsigned EltSz = VT.getScalarSizeInBits(); 6891 if (EltSz == 64) 6892 return false; 6893 6894 unsigned NumElts = VT.getVectorNumElements(); 6895 if (M.size() != NumElts && M.size() != NumElts*2) 6896 return false; 6897 6898 for (unsigned i = 0; i < M.size(); i += NumElts) { 6899 WhichResult = SelectPairHalf(NumElts, M, i); 6900 for (unsigned j = 0; j < NumElts; ++j) { 6901 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult) 6902 return false; 6903 } 6904 } 6905 6906 if (M.size() == NumElts*2) 6907 WhichResult = 0; 6908 6909 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 6910 if (VT.is64BitVector() && EltSz == 32) 6911 return false; 6912 6913 return true; 6914 } 6915 6916 /// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of 6917 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 6918 /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>, 6919 static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 6920 unsigned EltSz = VT.getScalarSizeInBits(); 6921 if (EltSz == 64) 6922 return false; 6923 6924 unsigned NumElts = VT.getVectorNumElements(); 6925 if (M.size() != NumElts && M.size() != NumElts*2) 6926 return false; 6927 6928 unsigned Half = NumElts / 2; 6929 for (unsigned i = 0; i < M.size(); i += NumElts) { 6930 WhichResult = SelectPairHalf(NumElts, M, i); 6931 for (unsigned j = 0; j < NumElts; j += Half) { 6932 unsigned Idx = WhichResult; 6933 for (unsigned k = 0; k < Half; ++k) { 6934 int MIdx = M[i + j + k]; 6935 if (MIdx >= 0 && (unsigned) MIdx != Idx) 6936 return false; 6937 Idx += 2; 6938 } 6939 } 6940 } 6941 6942 if (M.size() == NumElts*2) 6943 WhichResult = 0; 6944 6945 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 6946 if (VT.is64BitVector() && EltSz == 32) 6947 return false; 6948 6949 return true; 6950 } 6951 6952 // Checks whether the shuffle mask represents a vector zip (VZIP) by checking 6953 // that pairs of elements of the shufflemask represent the same index in each 6954 // vector incrementing sequentially through the vectors. 6955 // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5] 6956 // v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f} 6957 // v2={e,f,g,h} 6958 // Requires similar checks to that of isVTRNMask with respect the how results 6959 // are returned. 6960 static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 6961 unsigned EltSz = VT.getScalarSizeInBits(); 6962 if (EltSz == 64) 6963 return false; 6964 6965 unsigned NumElts = VT.getVectorNumElements(); 6966 if (M.size() != NumElts && M.size() != NumElts*2) 6967 return false; 6968 6969 for (unsigned i = 0; i < M.size(); i += NumElts) { 6970 WhichResult = SelectPairHalf(NumElts, M, i); 6971 unsigned Idx = WhichResult * NumElts / 2; 6972 for (unsigned j = 0; j < NumElts; j += 2) { 6973 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) || 6974 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts)) 6975 return false; 6976 Idx += 1; 6977 } 6978 } 6979 6980 if (M.size() == NumElts*2) 6981 WhichResult = 0; 6982 6983 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 6984 if (VT.is64BitVector() && EltSz == 32) 6985 return false; 6986 6987 return true; 6988 } 6989 6990 /// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of 6991 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 6992 /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>. 6993 static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 6994 unsigned EltSz = VT.getScalarSizeInBits(); 6995 if (EltSz == 64) 6996 return false; 6997 6998 unsigned NumElts = VT.getVectorNumElements(); 6999 if (M.size() != NumElts && M.size() != NumElts*2) 7000 return false; 7001 7002 for (unsigned i = 0; i < M.size(); i += NumElts) { 7003 WhichResult = SelectPairHalf(NumElts, M, i); 7004 unsigned Idx = WhichResult * NumElts / 2; 7005 for (unsigned j = 0; j < NumElts; j += 2) { 7006 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) || 7007 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx)) 7008 return false; 7009 Idx += 1; 7010 } 7011 } 7012 7013 if (M.size() == NumElts*2) 7014 WhichResult = 0; 7015 7016 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 7017 if (VT.is64BitVector() && EltSz == 32) 7018 return false; 7019 7020 return true; 7021 } 7022 7023 /// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN), 7024 /// and return the corresponding ARMISD opcode if it is, or 0 if it isn't. 7025 static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT, 7026 unsigned &WhichResult, 7027 bool &isV_UNDEF) { 7028 isV_UNDEF = false; 7029 if (isVTRNMask(ShuffleMask, VT, WhichResult)) 7030 return ARMISD::VTRN; 7031 if (isVUZPMask(ShuffleMask, VT, WhichResult)) 7032 return ARMISD::VUZP; 7033 if (isVZIPMask(ShuffleMask, VT, WhichResult)) 7034 return ARMISD::VZIP; 7035 7036 isV_UNDEF = true; 7037 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult)) 7038 return ARMISD::VTRN; 7039 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 7040 return ARMISD::VUZP; 7041 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 7042 return ARMISD::VZIP; 7043 7044 return 0; 7045 } 7046 7047 /// \return true if this is a reverse operation on an vector. 7048 static bool isReverseMask(ArrayRef<int> M, EVT VT) { 7049 unsigned NumElts = VT.getVectorNumElements(); 7050 // Make sure the mask has the right size. 7051 if (NumElts != M.size()) 7052 return false; 7053 7054 // Look for <15, ..., 3, -1, 1, 0>. 7055 for (unsigned i = 0; i != NumElts; ++i) 7056 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i)) 7057 return false; 7058 7059 return true; 7060 } 7061 7062 static bool isVMOVNMask(ArrayRef<int> M, EVT VT, bool Top) { 7063 unsigned NumElts = VT.getVectorNumElements(); 7064 // Make sure the mask has the right size. 7065 if (NumElts != M.size() || (VT != MVT::v8i16 && VT != MVT::v16i8)) 7066 return false; 7067 7068 // If Top 7069 // Look for <0, N, 2, N+2, 4, N+4, ..>. 7070 // This inserts Input2 into Input1 7071 // else if not Top 7072 // Look for <0, N+1, 2, N+3, 4, N+5, ..> 7073 // This inserts Input1 into Input2 7074 unsigned Offset = Top ? 0 : 1; 7075 for (unsigned i = 0; i < NumElts; i+=2) { 7076 if (M[i] >= 0 && M[i] != (int)i) 7077 return false; 7078 if (M[i+1] >= 0 && M[i+1] != (int)(NumElts + i + Offset)) 7079 return false; 7080 } 7081 7082 return true; 7083 } 7084 7085 // If N is an integer constant that can be moved into a register in one 7086 // instruction, return an SDValue of such a constant (will become a MOV 7087 // instruction). Otherwise return null. 7088 static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG, 7089 const ARMSubtarget *ST, const SDLoc &dl) { 7090 uint64_t Val; 7091 if (!isa<ConstantSDNode>(N)) 7092 return SDValue(); 7093 Val = cast<ConstantSDNode>(N)->getZExtValue(); 7094 7095 if (ST->isThumb1Only()) { 7096 if (Val <= 255 || ~Val <= 255) 7097 return DAG.getConstant(Val, dl, MVT::i32); 7098 } else { 7099 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1) 7100 return DAG.getConstant(Val, dl, MVT::i32); 7101 } 7102 return SDValue(); 7103 } 7104 7105 static SDValue LowerBUILD_VECTOR_i1(SDValue Op, SelectionDAG &DAG, 7106 const ARMSubtarget *ST) { 7107 SDLoc dl(Op); 7108 EVT VT = Op.getValueType(); 7109 7110 assert(ST->hasMVEIntegerOps() && "LowerBUILD_VECTOR_i1 called without MVE!"); 7111 7112 unsigned NumElts = VT.getVectorNumElements(); 7113 unsigned BoolMask; 7114 unsigned BitsPerBool; 7115 if (NumElts == 4) { 7116 BitsPerBool = 4; 7117 BoolMask = 0xf; 7118 } else if (NumElts == 8) { 7119 BitsPerBool = 2; 7120 BoolMask = 0x3; 7121 } else if (NumElts == 16) { 7122 BitsPerBool = 1; 7123 BoolMask = 0x1; 7124 } else 7125 return SDValue(); 7126 7127 // If this is a single value copied into all lanes (a splat), we can just sign 7128 // extend that single value 7129 SDValue FirstOp = Op.getOperand(0); 7130 if (!isa<ConstantSDNode>(FirstOp) && 7131 std::all_of(std::next(Op->op_begin()), Op->op_end(), 7132 [&FirstOp](SDUse &U) { 7133 return U.get().isUndef() || U.get() == FirstOp; 7134 })) { 7135 SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::i32, FirstOp, 7136 DAG.getValueType(MVT::i1)); 7137 return DAG.getNode(ARMISD::PREDICATE_CAST, dl, Op.getValueType(), Ext); 7138 } 7139 7140 // First create base with bits set where known 7141 unsigned Bits32 = 0; 7142 for (unsigned i = 0; i < NumElts; ++i) { 7143 SDValue V = Op.getOperand(i); 7144 if (!isa<ConstantSDNode>(V) && !V.isUndef()) 7145 continue; 7146 bool BitSet = V.isUndef() ? false : cast<ConstantSDNode>(V)->getZExtValue(); 7147 if (BitSet) 7148 Bits32 |= BoolMask << (i * BitsPerBool); 7149 } 7150 7151 // Add in unknown nodes 7152 SDValue Base = DAG.getNode(ARMISD::PREDICATE_CAST, dl, VT, 7153 DAG.getConstant(Bits32, dl, MVT::i32)); 7154 for (unsigned i = 0; i < NumElts; ++i) { 7155 SDValue V = Op.getOperand(i); 7156 if (isa<ConstantSDNode>(V) || V.isUndef()) 7157 continue; 7158 Base = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Base, V, 7159 DAG.getConstant(i, dl, MVT::i32)); 7160 } 7161 7162 return Base; 7163 } 7164 7165 // If this is a case we can't handle, return null and let the default 7166 // expansion code take care of it. 7167 SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG, 7168 const ARMSubtarget *ST) const { 7169 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode()); 7170 SDLoc dl(Op); 7171 EVT VT = Op.getValueType(); 7172 7173 if (ST->hasMVEIntegerOps() && VT.getScalarSizeInBits() == 1) 7174 return LowerBUILD_VECTOR_i1(Op, DAG, ST); 7175 7176 APInt SplatBits, SplatUndef; 7177 unsigned SplatBitSize; 7178 bool HasAnyUndefs; 7179 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 7180 if (SplatUndef.isAllOnesValue()) 7181 return DAG.getUNDEF(VT); 7182 7183 if ((ST->hasNEON() && SplatBitSize <= 64) || 7184 (ST->hasMVEIntegerOps() && SplatBitSize <= 32)) { 7185 // Check if an immediate VMOV works. 7186 EVT VmovVT; 7187 SDValue Val = isVMOVModifiedImm(SplatBits.getZExtValue(), 7188 SplatUndef.getZExtValue(), SplatBitSize, 7189 DAG, dl, VmovVT, VT.is128BitVector(), 7190 VMOVModImm); 7191 7192 if (Val.getNode()) { 7193 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val); 7194 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 7195 } 7196 7197 // Try an immediate VMVN. 7198 uint64_t NegatedImm = (~SplatBits).getZExtValue(); 7199 Val = isVMOVModifiedImm( 7200 NegatedImm, SplatUndef.getZExtValue(), SplatBitSize, 7201 DAG, dl, VmovVT, VT.is128BitVector(), 7202 ST->hasMVEIntegerOps() ? MVEVMVNModImm : VMVNModImm); 7203 if (Val.getNode()) { 7204 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val); 7205 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 7206 } 7207 7208 // Use vmov.f32 to materialize other v2f32 and v4f32 splats. 7209 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) { 7210 int ImmVal = ARM_AM::getFP32Imm(SplatBits); 7211 if (ImmVal != -1) { 7212 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32); 7213 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val); 7214 } 7215 } 7216 } 7217 } 7218 7219 // Scan through the operands to see if only one value is used. 7220 // 7221 // As an optimisation, even if more than one value is used it may be more 7222 // profitable to splat with one value then change some lanes. 7223 // 7224 // Heuristically we decide to do this if the vector has a "dominant" value, 7225 // defined as splatted to more than half of the lanes. 7226 unsigned NumElts = VT.getVectorNumElements(); 7227 bool isOnlyLowElement = true; 7228 bool usesOnlyOneValue = true; 7229 bool hasDominantValue = false; 7230 bool isConstant = true; 7231 7232 // Map of the number of times a particular SDValue appears in the 7233 // element list. 7234 DenseMap<SDValue, unsigned> ValueCounts; 7235 SDValue Value; 7236 for (unsigned i = 0; i < NumElts; ++i) { 7237 SDValue V = Op.getOperand(i); 7238 if (V.isUndef()) 7239 continue; 7240 if (i > 0) 7241 isOnlyLowElement = false; 7242 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 7243 isConstant = false; 7244 7245 ValueCounts.insert(std::make_pair(V, 0)); 7246 unsigned &Count = ValueCounts[V]; 7247 7248 // Is this value dominant? (takes up more than half of the lanes) 7249 if (++Count > (NumElts / 2)) { 7250 hasDominantValue = true; 7251 Value = V; 7252 } 7253 } 7254 if (ValueCounts.size() != 1) 7255 usesOnlyOneValue = false; 7256 if (!Value.getNode() && !ValueCounts.empty()) 7257 Value = ValueCounts.begin()->first; 7258 7259 if (ValueCounts.empty()) 7260 return DAG.getUNDEF(VT); 7261 7262 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR. 7263 // Keep going if we are hitting this case. 7264 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode())) 7265 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value); 7266 7267 unsigned EltSize = VT.getScalarSizeInBits(); 7268 7269 // Use VDUP for non-constant splats. For f32 constant splats, reduce to 7270 // i32 and try again. 7271 if (hasDominantValue && EltSize <= 32) { 7272 if (!isConstant) { 7273 SDValue N; 7274 7275 // If we are VDUPing a value that comes directly from a vector, that will 7276 // cause an unnecessary move to and from a GPR, where instead we could 7277 // just use VDUPLANE. We can only do this if the lane being extracted 7278 // is at a constant index, as the VDUP from lane instructions only have 7279 // constant-index forms. 7280 ConstantSDNode *constIndex; 7281 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT && 7282 (constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)))) { 7283 // We need to create a new undef vector to use for the VDUPLANE if the 7284 // size of the vector from which we get the value is different than the 7285 // size of the vector that we need to create. We will insert the element 7286 // such that the register coalescer will remove unnecessary copies. 7287 if (VT != Value->getOperand(0).getValueType()) { 7288 unsigned index = constIndex->getAPIntValue().getLimitedValue() % 7289 VT.getVectorNumElements(); 7290 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT, 7291 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT), 7292 Value, DAG.getConstant(index, dl, MVT::i32)), 7293 DAG.getConstant(index, dl, MVT::i32)); 7294 } else 7295 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT, 7296 Value->getOperand(0), Value->getOperand(1)); 7297 } else 7298 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value); 7299 7300 if (!usesOnlyOneValue) { 7301 // The dominant value was splatted as 'N', but we now have to insert 7302 // all differing elements. 7303 for (unsigned I = 0; I < NumElts; ++I) { 7304 if (Op.getOperand(I) == Value) 7305 continue; 7306 SmallVector<SDValue, 3> Ops; 7307 Ops.push_back(N); 7308 Ops.push_back(Op.getOperand(I)); 7309 Ops.push_back(DAG.getConstant(I, dl, MVT::i32)); 7310 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops); 7311 } 7312 } 7313 return N; 7314 } 7315 if (VT.getVectorElementType().isFloatingPoint()) { 7316 SmallVector<SDValue, 8> Ops; 7317 MVT FVT = VT.getVectorElementType().getSimpleVT(); 7318 assert(FVT == MVT::f32 || FVT == MVT::f16); 7319 MVT IVT = (FVT == MVT::f32) ? MVT::i32 : MVT::i16; 7320 for (unsigned i = 0; i < NumElts; ++i) 7321 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, IVT, 7322 Op.getOperand(i))); 7323 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), IVT, NumElts); 7324 SDValue Val = DAG.getBuildVector(VecVT, dl, Ops); 7325 Val = LowerBUILD_VECTOR(Val, DAG, ST); 7326 if (Val.getNode()) 7327 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 7328 } 7329 if (usesOnlyOneValue) { 7330 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl); 7331 if (isConstant && Val.getNode()) 7332 return DAG.getNode(ARMISD::VDUP, dl, VT, Val); 7333 } 7334 } 7335 7336 // If all elements are constants and the case above didn't get hit, fall back 7337 // to the default expansion, which will generate a load from the constant 7338 // pool. 7339 if (isConstant) 7340 return SDValue(); 7341 7342 // Empirical tests suggest this is rarely worth it for vectors of length <= 2. 7343 if (NumElts >= 4) { 7344 SDValue shuffle = ReconstructShuffle(Op, DAG); 7345 if (shuffle != SDValue()) 7346 return shuffle; 7347 } 7348 7349 if (ST->hasNEON() && VT.is128BitVector() && VT != MVT::v2f64 && VT != MVT::v4f32) { 7350 // If we haven't found an efficient lowering, try splitting a 128-bit vector 7351 // into two 64-bit vectors; we might discover a better way to lower it. 7352 SmallVector<SDValue, 64> Ops(Op->op_begin(), Op->op_begin() + NumElts); 7353 EVT ExtVT = VT.getVectorElementType(); 7354 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElts / 2); 7355 SDValue Lower = 7356 DAG.getBuildVector(HVT, dl, makeArrayRef(&Ops[0], NumElts / 2)); 7357 if (Lower.getOpcode() == ISD::BUILD_VECTOR) 7358 Lower = LowerBUILD_VECTOR(Lower, DAG, ST); 7359 SDValue Upper = DAG.getBuildVector( 7360 HVT, dl, makeArrayRef(&Ops[NumElts / 2], NumElts / 2)); 7361 if (Upper.getOpcode() == ISD::BUILD_VECTOR) 7362 Upper = LowerBUILD_VECTOR(Upper, DAG, ST); 7363 if (Lower && Upper) 7364 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Lower, Upper); 7365 } 7366 7367 // Vectors with 32- or 64-bit elements can be built by directly assigning 7368 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands 7369 // will be legalized. 7370 if (EltSize >= 32) { 7371 // Do the expansion with floating-point types, since that is what the VFP 7372 // registers are defined to use, and since i64 is not legal. 7373 EVT EltVT = EVT::getFloatingPointVT(EltSize); 7374 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 7375 SmallVector<SDValue, 8> Ops; 7376 for (unsigned i = 0; i < NumElts; ++i) 7377 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i))); 7378 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops); 7379 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 7380 } 7381 7382 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we 7383 // know the default expansion would otherwise fall back on something even 7384 // worse. For a vector with one or two non-undef values, that's 7385 // scalar_to_vector for the elements followed by a shuffle (provided the 7386 // shuffle is valid for the target) and materialization element by element 7387 // on the stack followed by a load for everything else. 7388 if (!isConstant && !usesOnlyOneValue) { 7389 SDValue Vec = DAG.getUNDEF(VT); 7390 for (unsigned i = 0 ; i < NumElts; ++i) { 7391 SDValue V = Op.getOperand(i); 7392 if (V.isUndef()) 7393 continue; 7394 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32); 7395 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx); 7396 } 7397 return Vec; 7398 } 7399 7400 return SDValue(); 7401 } 7402 7403 // Gather data to see if the operation can be modelled as a 7404 // shuffle in combination with VEXTs. 7405 SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op, 7406 SelectionDAG &DAG) const { 7407 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!"); 7408 SDLoc dl(Op); 7409 EVT VT = Op.getValueType(); 7410 unsigned NumElts = VT.getVectorNumElements(); 7411 7412 struct ShuffleSourceInfo { 7413 SDValue Vec; 7414 unsigned MinElt = std::numeric_limits<unsigned>::max(); 7415 unsigned MaxElt = 0; 7416 7417 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to 7418 // be compatible with the shuffle we intend to construct. As a result 7419 // ShuffleVec will be some sliding window into the original Vec. 7420 SDValue ShuffleVec; 7421 7422 // Code should guarantee that element i in Vec starts at element "WindowBase 7423 // + i * WindowScale in ShuffleVec". 7424 int WindowBase = 0; 7425 int WindowScale = 1; 7426 7427 ShuffleSourceInfo(SDValue Vec) : Vec(Vec), ShuffleVec(Vec) {} 7428 7429 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; } 7430 }; 7431 7432 // First gather all vectors used as an immediate source for this BUILD_VECTOR 7433 // node. 7434 SmallVector<ShuffleSourceInfo, 2> Sources; 7435 for (unsigned i = 0; i < NumElts; ++i) { 7436 SDValue V = Op.getOperand(i); 7437 if (V.isUndef()) 7438 continue; 7439 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) { 7440 // A shuffle can only come from building a vector from various 7441 // elements of other vectors. 7442 return SDValue(); 7443 } else if (!isa<ConstantSDNode>(V.getOperand(1))) { 7444 // Furthermore, shuffles require a constant mask, whereas extractelts 7445 // accept variable indices. 7446 return SDValue(); 7447 } 7448 7449 // Add this element source to the list if it's not already there. 7450 SDValue SourceVec = V.getOperand(0); 7451 auto Source = llvm::find(Sources, SourceVec); 7452 if (Source == Sources.end()) 7453 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec)); 7454 7455 // Update the minimum and maximum lane number seen. 7456 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue(); 7457 Source->MinElt = std::min(Source->MinElt, EltNo); 7458 Source->MaxElt = std::max(Source->MaxElt, EltNo); 7459 } 7460 7461 // Currently only do something sane when at most two source vectors 7462 // are involved. 7463 if (Sources.size() > 2) 7464 return SDValue(); 7465 7466 // Find out the smallest element size among result and two sources, and use 7467 // it as element size to build the shuffle_vector. 7468 EVT SmallestEltTy = VT.getVectorElementType(); 7469 for (auto &Source : Sources) { 7470 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType(); 7471 if (SrcEltTy.bitsLT(SmallestEltTy)) 7472 SmallestEltTy = SrcEltTy; 7473 } 7474 unsigned ResMultiplier = 7475 VT.getScalarSizeInBits() / SmallestEltTy.getSizeInBits(); 7476 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits(); 7477 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts); 7478 7479 // If the source vector is too wide or too narrow, we may nevertheless be able 7480 // to construct a compatible shuffle either by concatenating it with UNDEF or 7481 // extracting a suitable range of elements. 7482 for (auto &Src : Sources) { 7483 EVT SrcVT = Src.ShuffleVec.getValueType(); 7484 7485 if (SrcVT.getSizeInBits() == VT.getSizeInBits()) 7486 continue; 7487 7488 // This stage of the search produces a source with the same element type as 7489 // the original, but with a total width matching the BUILD_VECTOR output. 7490 EVT EltVT = SrcVT.getVectorElementType(); 7491 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits(); 7492 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts); 7493 7494 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) { 7495 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits()) 7496 return SDValue(); 7497 // We can pad out the smaller vector for free, so if it's part of a 7498 // shuffle... 7499 Src.ShuffleVec = 7500 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec, 7501 DAG.getUNDEF(Src.ShuffleVec.getValueType())); 7502 continue; 7503 } 7504 7505 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits()) 7506 return SDValue(); 7507 7508 if (Src.MaxElt - Src.MinElt >= NumSrcElts) { 7509 // Span too large for a VEXT to cope 7510 return SDValue(); 7511 } 7512 7513 if (Src.MinElt >= NumSrcElts) { 7514 // The extraction can just take the second half 7515 Src.ShuffleVec = 7516 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 7517 DAG.getConstant(NumSrcElts, dl, MVT::i32)); 7518 Src.WindowBase = -NumSrcElts; 7519 } else if (Src.MaxElt < NumSrcElts) { 7520 // The extraction can just take the first half 7521 Src.ShuffleVec = 7522 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 7523 DAG.getConstant(0, dl, MVT::i32)); 7524 } else { 7525 // An actual VEXT is needed 7526 SDValue VEXTSrc1 = 7527 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 7528 DAG.getConstant(0, dl, MVT::i32)); 7529 SDValue VEXTSrc2 = 7530 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 7531 DAG.getConstant(NumSrcElts, dl, MVT::i32)); 7532 7533 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1, 7534 VEXTSrc2, 7535 DAG.getConstant(Src.MinElt, dl, MVT::i32)); 7536 Src.WindowBase = -Src.MinElt; 7537 } 7538 } 7539 7540 // Another possible incompatibility occurs from the vector element types. We 7541 // can fix this by bitcasting the source vectors to the same type we intend 7542 // for the shuffle. 7543 for (auto &Src : Sources) { 7544 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType(); 7545 if (SrcEltTy == SmallestEltTy) 7546 continue; 7547 assert(ShuffleVT.getVectorElementType() == SmallestEltTy); 7548 Src.ShuffleVec = DAG.getNode(ARMISD::VECTOR_REG_CAST, dl, ShuffleVT, Src.ShuffleVec); 7549 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits(); 7550 Src.WindowBase *= Src.WindowScale; 7551 } 7552 7553 // Final sanity check before we try to actually produce a shuffle. 7554 LLVM_DEBUG(for (auto Src 7555 : Sources) 7556 assert(Src.ShuffleVec.getValueType() == ShuffleVT);); 7557 7558 // The stars all align, our next step is to produce the mask for the shuffle. 7559 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1); 7560 int BitsPerShuffleLane = ShuffleVT.getScalarSizeInBits(); 7561 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) { 7562 SDValue Entry = Op.getOperand(i); 7563 if (Entry.isUndef()) 7564 continue; 7565 7566 auto Src = llvm::find(Sources, Entry.getOperand(0)); 7567 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue(); 7568 7569 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit 7570 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this 7571 // segment. 7572 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType(); 7573 int BitsDefined = std::min(OrigEltTy.getSizeInBits(), 7574 VT.getScalarSizeInBits()); 7575 int LanesDefined = BitsDefined / BitsPerShuffleLane; 7576 7577 // This source is expected to fill ResMultiplier lanes of the final shuffle, 7578 // starting at the appropriate offset. 7579 int *LaneMask = &Mask[i * ResMultiplier]; 7580 7581 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase; 7582 ExtractBase += NumElts * (Src - Sources.begin()); 7583 for (int j = 0; j < LanesDefined; ++j) 7584 LaneMask[j] = ExtractBase + j; 7585 } 7586 7587 7588 // We can't handle more than two sources. This should have already 7589 // been checked before this point. 7590 assert(Sources.size() <= 2 && "Too many sources!"); 7591 7592 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) }; 7593 for (unsigned i = 0; i < Sources.size(); ++i) 7594 ShuffleOps[i] = Sources[i].ShuffleVec; 7595 7596 SDValue Shuffle = buildLegalVectorShuffle(ShuffleVT, dl, ShuffleOps[0], 7597 ShuffleOps[1], Mask, DAG); 7598 if (!Shuffle) 7599 return SDValue(); 7600 return DAG.getNode(ARMISD::VECTOR_REG_CAST, dl, VT, Shuffle); 7601 } 7602 7603 enum ShuffleOpCodes { 7604 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3> 7605 OP_VREV, 7606 OP_VDUP0, 7607 OP_VDUP1, 7608 OP_VDUP2, 7609 OP_VDUP3, 7610 OP_VEXT1, 7611 OP_VEXT2, 7612 OP_VEXT3, 7613 OP_VUZPL, // VUZP, left result 7614 OP_VUZPR, // VUZP, right result 7615 OP_VZIPL, // VZIP, left result 7616 OP_VZIPR, // VZIP, right result 7617 OP_VTRNL, // VTRN, left result 7618 OP_VTRNR // VTRN, right result 7619 }; 7620 7621 static bool isLegalMVEShuffleOp(unsigned PFEntry) { 7622 unsigned OpNum = (PFEntry >> 26) & 0x0F; 7623 switch (OpNum) { 7624 case OP_COPY: 7625 case OP_VREV: 7626 case OP_VDUP0: 7627 case OP_VDUP1: 7628 case OP_VDUP2: 7629 case OP_VDUP3: 7630 return true; 7631 } 7632 return false; 7633 } 7634 7635 /// isShuffleMaskLegal - Targets can use this to indicate that they only 7636 /// support *some* VECTOR_SHUFFLE operations, those with specific masks. 7637 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values 7638 /// are assumed to be legal. 7639 bool ARMTargetLowering::isShuffleMaskLegal(ArrayRef<int> M, EVT VT) const { 7640 if (VT.getVectorNumElements() == 4 && 7641 (VT.is128BitVector() || VT.is64BitVector())) { 7642 unsigned PFIndexes[4]; 7643 for (unsigned i = 0; i != 4; ++i) { 7644 if (M[i] < 0) 7645 PFIndexes[i] = 8; 7646 else 7647 PFIndexes[i] = M[i]; 7648 } 7649 7650 // Compute the index in the perfect shuffle table. 7651 unsigned PFTableIndex = 7652 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 7653 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 7654 unsigned Cost = (PFEntry >> 30); 7655 7656 if (Cost <= 4 && (Subtarget->hasNEON() || isLegalMVEShuffleOp(PFEntry))) 7657 return true; 7658 } 7659 7660 bool ReverseVEXT, isV_UNDEF; 7661 unsigned Imm, WhichResult; 7662 7663 unsigned EltSize = VT.getScalarSizeInBits(); 7664 if (EltSize >= 32 || 7665 ShuffleVectorSDNode::isSplatMask(&M[0], VT) || 7666 ShuffleVectorInst::isIdentityMask(M) || 7667 isVREVMask(M, VT, 64) || 7668 isVREVMask(M, VT, 32) || 7669 isVREVMask(M, VT, 16)) 7670 return true; 7671 else if (Subtarget->hasNEON() && 7672 (isVEXTMask(M, VT, ReverseVEXT, Imm) || 7673 isVTBLMask(M, VT) || 7674 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF))) 7675 return true; 7676 else if (Subtarget->hasNEON() && (VT == MVT::v8i16 || VT == MVT::v16i8) && 7677 isReverseMask(M, VT)) 7678 return true; 7679 else if (Subtarget->hasMVEIntegerOps() && 7680 (isVMOVNMask(M, VT, 0) || isVMOVNMask(M, VT, 1))) 7681 return true; 7682 else 7683 return false; 7684 } 7685 7686 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit 7687 /// the specified operations to build the shuffle. 7688 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, 7689 SDValue RHS, SelectionDAG &DAG, 7690 const SDLoc &dl) { 7691 unsigned OpNum = (PFEntry >> 26) & 0x0F; 7692 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1); 7693 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1); 7694 7695 if (OpNum == OP_COPY) { 7696 if (LHSID == (1*9+2)*9+3) return LHS; 7697 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!"); 7698 return RHS; 7699 } 7700 7701 SDValue OpLHS, OpRHS; 7702 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl); 7703 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl); 7704 EVT VT = OpLHS.getValueType(); 7705 7706 switch (OpNum) { 7707 default: llvm_unreachable("Unknown shuffle opcode!"); 7708 case OP_VREV: 7709 // VREV divides the vector in half and swaps within the half. 7710 if (VT.getVectorElementType() == MVT::i32 || 7711 VT.getVectorElementType() == MVT::f32) 7712 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS); 7713 // vrev <4 x i16> -> VREV32 7714 if (VT.getVectorElementType() == MVT::i16) 7715 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS); 7716 // vrev <4 x i8> -> VREV16 7717 assert(VT.getVectorElementType() == MVT::i8); 7718 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS); 7719 case OP_VDUP0: 7720 case OP_VDUP1: 7721 case OP_VDUP2: 7722 case OP_VDUP3: 7723 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, 7724 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32)); 7725 case OP_VEXT1: 7726 case OP_VEXT2: 7727 case OP_VEXT3: 7728 return DAG.getNode(ARMISD::VEXT, dl, VT, 7729 OpLHS, OpRHS, 7730 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32)); 7731 case OP_VUZPL: 7732 case OP_VUZPR: 7733 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 7734 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL); 7735 case OP_VZIPL: 7736 case OP_VZIPR: 7737 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 7738 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL); 7739 case OP_VTRNL: 7740 case OP_VTRNR: 7741 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 7742 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL); 7743 } 7744 } 7745 7746 static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op, 7747 ArrayRef<int> ShuffleMask, 7748 SelectionDAG &DAG) { 7749 // Check to see if we can use the VTBL instruction. 7750 SDValue V1 = Op.getOperand(0); 7751 SDValue V2 = Op.getOperand(1); 7752 SDLoc DL(Op); 7753 7754 SmallVector<SDValue, 8> VTBLMask; 7755 for (ArrayRef<int>::iterator 7756 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I) 7757 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32)); 7758 7759 if (V2.getNode()->isUndef()) 7760 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1, 7761 DAG.getBuildVector(MVT::v8i8, DL, VTBLMask)); 7762 7763 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2, 7764 DAG.getBuildVector(MVT::v8i8, DL, VTBLMask)); 7765 } 7766 7767 static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op, 7768 SelectionDAG &DAG) { 7769 SDLoc DL(Op); 7770 SDValue OpLHS = Op.getOperand(0); 7771 EVT VT = OpLHS.getValueType(); 7772 7773 assert((VT == MVT::v8i16 || VT == MVT::v16i8) && 7774 "Expect an v8i16/v16i8 type"); 7775 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS); 7776 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now, 7777 // extract the first 8 bytes into the top double word and the last 8 bytes 7778 // into the bottom double word. The v8i16 case is similar. 7779 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4; 7780 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS, 7781 DAG.getConstant(ExtractNum, DL, MVT::i32)); 7782 } 7783 7784 static EVT getVectorTyFromPredicateVector(EVT VT) { 7785 switch (VT.getSimpleVT().SimpleTy) { 7786 case MVT::v4i1: 7787 return MVT::v4i32; 7788 case MVT::v8i1: 7789 return MVT::v8i16; 7790 case MVT::v16i1: 7791 return MVT::v16i8; 7792 default: 7793 llvm_unreachable("Unexpected vector predicate type"); 7794 } 7795 } 7796 7797 static SDValue PromoteMVEPredVector(SDLoc dl, SDValue Pred, EVT VT, 7798 SelectionDAG &DAG) { 7799 // Converting from boolean predicates to integers involves creating a vector 7800 // of all ones or all zeroes and selecting the lanes based upon the real 7801 // predicate. 7802 SDValue AllOnes = 7803 DAG.getTargetConstant(ARM_AM::createVMOVModImm(0xe, 0xff), dl, MVT::i32); 7804 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v16i8, AllOnes); 7805 7806 SDValue AllZeroes = 7807 DAG.getTargetConstant(ARM_AM::createVMOVModImm(0xe, 0x0), dl, MVT::i32); 7808 AllZeroes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v16i8, AllZeroes); 7809 7810 // Get full vector type from predicate type 7811 EVT NewVT = getVectorTyFromPredicateVector(VT); 7812 7813 SDValue RecastV1; 7814 // If the real predicate is an v8i1 or v4i1 (not v16i1) then we need to recast 7815 // this to a v16i1. This cannot be done with an ordinary bitcast because the 7816 // sizes are not the same. We have to use a MVE specific PREDICATE_CAST node, 7817 // since we know in hardware the sizes are really the same. 7818 if (VT != MVT::v16i1) 7819 RecastV1 = DAG.getNode(ARMISD::PREDICATE_CAST, dl, MVT::v16i1, Pred); 7820 else 7821 RecastV1 = Pred; 7822 7823 // Select either all ones or zeroes depending upon the real predicate bits. 7824 SDValue PredAsVector = 7825 DAG.getNode(ISD::VSELECT, dl, MVT::v16i8, RecastV1, AllOnes, AllZeroes); 7826 7827 // Recast our new predicate-as-integer v16i8 vector into something 7828 // appropriate for the shuffle, i.e. v4i32 for a real v4i1 predicate. 7829 return DAG.getNode(ISD::BITCAST, dl, NewVT, PredAsVector); 7830 } 7831 7832 static SDValue LowerVECTOR_SHUFFLE_i1(SDValue Op, SelectionDAG &DAG, 7833 const ARMSubtarget *ST) { 7834 EVT VT = Op.getValueType(); 7835 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode()); 7836 ArrayRef<int> ShuffleMask = SVN->getMask(); 7837 7838 assert(ST->hasMVEIntegerOps() && 7839 "No support for vector shuffle of boolean predicates"); 7840 7841 SDValue V1 = Op.getOperand(0); 7842 SDLoc dl(Op); 7843 if (isReverseMask(ShuffleMask, VT)) { 7844 SDValue cast = DAG.getNode(ARMISD::PREDICATE_CAST, dl, MVT::i32, V1); 7845 SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, MVT::i32, cast); 7846 SDValue srl = DAG.getNode(ISD::SRL, dl, MVT::i32, rbit, 7847 DAG.getConstant(16, dl, MVT::i32)); 7848 return DAG.getNode(ARMISD::PREDICATE_CAST, dl, VT, srl); 7849 } 7850 7851 // Until we can come up with optimised cases for every single vector 7852 // shuffle in existence we have chosen the least painful strategy. This is 7853 // to essentially promote the boolean predicate to a 8-bit integer, where 7854 // each predicate represents a byte. Then we fall back on a normal integer 7855 // vector shuffle and convert the result back into a predicate vector. In 7856 // many cases the generated code might be even better than scalar code 7857 // operating on bits. Just imagine trying to shuffle 8 arbitrary 2-bit 7858 // fields in a register into 8 other arbitrary 2-bit fields! 7859 SDValue PredAsVector = PromoteMVEPredVector(dl, V1, VT, DAG); 7860 EVT NewVT = PredAsVector.getValueType(); 7861 7862 // Do the shuffle! 7863 SDValue Shuffled = DAG.getVectorShuffle(NewVT, dl, PredAsVector, 7864 DAG.getUNDEF(NewVT), ShuffleMask); 7865 7866 // Now return the result of comparing the shuffled vector with zero, 7867 // which will generate a real predicate, i.e. v4i1, v8i1 or v16i1. 7868 return DAG.getNode(ARMISD::VCMPZ, dl, VT, Shuffled, 7869 DAG.getConstant(ARMCC::NE, dl, MVT::i32)); 7870 } 7871 7872 static SDValue LowerVECTOR_SHUFFLEUsingMovs(SDValue Op, 7873 ArrayRef<int> ShuffleMask, 7874 SelectionDAG &DAG) { 7875 // Attempt to lower the vector shuffle using as many whole register movs as 7876 // possible. This is useful for types smaller than 32bits, which would 7877 // often otherwise become a series for grp movs. 7878 SDLoc dl(Op); 7879 EVT VT = Op.getValueType(); 7880 if (VT.getScalarSizeInBits() >= 32) 7881 return SDValue(); 7882 7883 assert((VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v16i8) && 7884 "Unexpected vector type"); 7885 int NumElts = VT.getVectorNumElements(); 7886 int QuarterSize = NumElts / 4; 7887 // The four final parts of the vector, as i32's 7888 SDValue Parts[4]; 7889 7890 // Look for full lane vmovs like <0,1,2,3> or <u,5,6,7> etc, (but not 7891 // <u,u,u,u>), returning the vmov lane index 7892 auto getMovIdx = [](ArrayRef<int> ShuffleMask, int Start, int Length) { 7893 // Detect which mov lane this would be from the first non-undef element. 7894 int MovIdx = -1; 7895 for (int i = 0; i < Length; i++) { 7896 if (ShuffleMask[Start + i] >= 0) { 7897 if (ShuffleMask[Start + i] % Length != i) 7898 return -1; 7899 MovIdx = ShuffleMask[Start + i] / Length; 7900 break; 7901 } 7902 } 7903 // If all items are undef, leave this for other combines 7904 if (MovIdx == -1) 7905 return -1; 7906 // Check the remaining values are the correct part of the same mov 7907 for (int i = 1; i < Length; i++) { 7908 if (ShuffleMask[Start + i] >= 0 && 7909 (ShuffleMask[Start + i] / Length != MovIdx || 7910 ShuffleMask[Start + i] % Length != i)) 7911 return -1; 7912 } 7913 return MovIdx; 7914 }; 7915 7916 for (int Part = 0; Part < 4; ++Part) { 7917 // Does this part look like a mov 7918 int Elt = getMovIdx(ShuffleMask, Part * QuarterSize, QuarterSize); 7919 if (Elt != -1) { 7920 SDValue Input = Op->getOperand(0); 7921 if (Elt >= 4) { 7922 Input = Op->getOperand(1); 7923 Elt -= 4; 7924 } 7925 SDValue BitCast = DAG.getBitcast(MVT::v4i32, Input); 7926 Parts[Part] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, BitCast, 7927 DAG.getConstant(Elt, dl, MVT::i32)); 7928 } 7929 } 7930 7931 // Nothing interesting found, just return 7932 if (!Parts[0] && !Parts[1] && !Parts[2] && !Parts[3]) 7933 return SDValue(); 7934 7935 // The other parts need to be built with the old shuffle vector, cast to a 7936 // v4i32 and extract_vector_elts 7937 if (!Parts[0] || !Parts[1] || !Parts[2] || !Parts[3]) { 7938 SmallVector<int, 16> NewShuffleMask; 7939 for (int Part = 0; Part < 4; ++Part) 7940 for (int i = 0; i < QuarterSize; i++) 7941 NewShuffleMask.push_back( 7942 Parts[Part] ? -1 : ShuffleMask[Part * QuarterSize + i]); 7943 SDValue NewShuffle = DAG.getVectorShuffle( 7944 VT, dl, Op->getOperand(0), Op->getOperand(1), NewShuffleMask); 7945 SDValue BitCast = DAG.getBitcast(MVT::v4i32, NewShuffle); 7946 7947 for (int Part = 0; Part < 4; ++Part) 7948 if (!Parts[Part]) 7949 Parts[Part] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, 7950 BitCast, DAG.getConstant(Part, dl, MVT::i32)); 7951 } 7952 // Build a vector out of the various parts and bitcast it back to the original 7953 // type. 7954 SDValue NewVec = DAG.getBuildVector(MVT::v4i32, dl, Parts); 7955 return DAG.getBitcast(VT, NewVec); 7956 } 7957 7958 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG, 7959 const ARMSubtarget *ST) { 7960 SDValue V1 = Op.getOperand(0); 7961 SDValue V2 = Op.getOperand(1); 7962 SDLoc dl(Op); 7963 EVT VT = Op.getValueType(); 7964 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode()); 7965 unsigned EltSize = VT.getScalarSizeInBits(); 7966 7967 if (ST->hasMVEIntegerOps() && EltSize == 1) 7968 return LowerVECTOR_SHUFFLE_i1(Op, DAG, ST); 7969 7970 // Convert shuffles that are directly supported on NEON to target-specific 7971 // DAG nodes, instead of keeping them as shuffles and matching them again 7972 // during code selection. This is more efficient and avoids the possibility 7973 // of inconsistencies between legalization and selection. 7974 // FIXME: floating-point vectors should be canonicalized to integer vectors 7975 // of the same time so that they get CSEd properly. 7976 ArrayRef<int> ShuffleMask = SVN->getMask(); 7977 7978 if (EltSize <= 32) { 7979 if (SVN->isSplat()) { 7980 int Lane = SVN->getSplatIndex(); 7981 // If this is undef splat, generate it via "just" vdup, if possible. 7982 if (Lane == -1) Lane = 0; 7983 7984 // Test if V1 is a SCALAR_TO_VECTOR. 7985 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) { 7986 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 7987 } 7988 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR 7989 // (and probably will turn into a SCALAR_TO_VECTOR once legalization 7990 // reaches it). 7991 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR && 7992 !isa<ConstantSDNode>(V1.getOperand(0))) { 7993 bool IsScalarToVector = true; 7994 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) 7995 if (!V1.getOperand(i).isUndef()) { 7996 IsScalarToVector = false; 7997 break; 7998 } 7999 if (IsScalarToVector) 8000 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 8001 } 8002 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1, 8003 DAG.getConstant(Lane, dl, MVT::i32)); 8004 } 8005 8006 bool ReverseVEXT = false; 8007 unsigned Imm = 0; 8008 if (ST->hasNEON() && isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) { 8009 if (ReverseVEXT) 8010 std::swap(V1, V2); 8011 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2, 8012 DAG.getConstant(Imm, dl, MVT::i32)); 8013 } 8014 8015 if (isVREVMask(ShuffleMask, VT, 64)) 8016 return DAG.getNode(ARMISD::VREV64, dl, VT, V1); 8017 if (isVREVMask(ShuffleMask, VT, 32)) 8018 return DAG.getNode(ARMISD::VREV32, dl, VT, V1); 8019 if (isVREVMask(ShuffleMask, VT, 16)) 8020 return DAG.getNode(ARMISD::VREV16, dl, VT, V1); 8021 8022 if (ST->hasNEON() && V2->isUndef() && isSingletonVEXTMask(ShuffleMask, VT, Imm)) { 8023 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1, 8024 DAG.getConstant(Imm, dl, MVT::i32)); 8025 } 8026 8027 // Check for Neon shuffles that modify both input vectors in place. 8028 // If both results are used, i.e., if there are two shuffles with the same 8029 // source operands and with masks corresponding to both results of one of 8030 // these operations, DAG memoization will ensure that a single node is 8031 // used for both shuffles. 8032 unsigned WhichResult = 0; 8033 bool isV_UNDEF = false; 8034 if (ST->hasNEON()) { 8035 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask( 8036 ShuffleMask, VT, WhichResult, isV_UNDEF)) { 8037 if (isV_UNDEF) 8038 V2 = V1; 8039 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2) 8040 .getValue(WhichResult); 8041 } 8042 } 8043 if (ST->hasMVEIntegerOps()) { 8044 if (isVMOVNMask(ShuffleMask, VT, 0)) 8045 return DAG.getNode(ARMISD::VMOVN, dl, VT, V2, V1, 8046 DAG.getConstant(0, dl, MVT::i32)); 8047 if (isVMOVNMask(ShuffleMask, VT, 1)) 8048 return DAG.getNode(ARMISD::VMOVN, dl, VT, V1, V2, 8049 DAG.getConstant(1, dl, MVT::i32)); 8050 } 8051 8052 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize 8053 // shuffles that produce a result larger than their operands with: 8054 // shuffle(concat(v1, undef), concat(v2, undef)) 8055 // -> 8056 // shuffle(concat(v1, v2), undef) 8057 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine). 8058 // 8059 // This is useful in the general case, but there are special cases where 8060 // native shuffles produce larger results: the two-result ops. 8061 // 8062 // Look through the concat when lowering them: 8063 // shuffle(concat(v1, v2), undef) 8064 // -> 8065 // concat(VZIP(v1, v2):0, :1) 8066 // 8067 if (ST->hasNEON() && V1->getOpcode() == ISD::CONCAT_VECTORS && V2->isUndef()) { 8068 SDValue SubV1 = V1->getOperand(0); 8069 SDValue SubV2 = V1->getOperand(1); 8070 EVT SubVT = SubV1.getValueType(); 8071 8072 // We expect these to have been canonicalized to -1. 8073 assert(llvm::all_of(ShuffleMask, [&](int i) { 8074 return i < (int)VT.getVectorNumElements(); 8075 }) && "Unexpected shuffle index into UNDEF operand!"); 8076 8077 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask( 8078 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) { 8079 if (isV_UNDEF) 8080 SubV2 = SubV1; 8081 assert((WhichResult == 0) && 8082 "In-place shuffle of concat can only have one result!"); 8083 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT), 8084 SubV1, SubV2); 8085 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0), 8086 Res.getValue(1)); 8087 } 8088 } 8089 } 8090 8091 // If the shuffle is not directly supported and it has 4 elements, use 8092 // the PerfectShuffle-generated table to synthesize it from other shuffles. 8093 unsigned NumElts = VT.getVectorNumElements(); 8094 if (NumElts == 4) { 8095 unsigned PFIndexes[4]; 8096 for (unsigned i = 0; i != 4; ++i) { 8097 if (ShuffleMask[i] < 0) 8098 PFIndexes[i] = 8; 8099 else 8100 PFIndexes[i] = ShuffleMask[i]; 8101 } 8102 8103 // Compute the index in the perfect shuffle table. 8104 unsigned PFTableIndex = 8105 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 8106 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 8107 unsigned Cost = (PFEntry >> 30); 8108 8109 if (Cost <= 4) { 8110 if (ST->hasNEON()) 8111 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); 8112 else if (isLegalMVEShuffleOp(PFEntry)) { 8113 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1); 8114 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1); 8115 unsigned PFEntryLHS = PerfectShuffleTable[LHSID]; 8116 unsigned PFEntryRHS = PerfectShuffleTable[RHSID]; 8117 if (isLegalMVEShuffleOp(PFEntryLHS) && isLegalMVEShuffleOp(PFEntryRHS)) 8118 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); 8119 } 8120 } 8121 } 8122 8123 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs. 8124 if (EltSize >= 32) { 8125 // Do the expansion with floating-point types, since that is what the VFP 8126 // registers are defined to use, and since i64 is not legal. 8127 EVT EltVT = EVT::getFloatingPointVT(EltSize); 8128 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 8129 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1); 8130 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2); 8131 SmallVector<SDValue, 8> Ops; 8132 for (unsigned i = 0; i < NumElts; ++i) { 8133 if (ShuffleMask[i] < 0) 8134 Ops.push_back(DAG.getUNDEF(EltVT)); 8135 else 8136 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, 8137 ShuffleMask[i] < (int)NumElts ? V1 : V2, 8138 DAG.getConstant(ShuffleMask[i] & (NumElts-1), 8139 dl, MVT::i32))); 8140 } 8141 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops); 8142 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 8143 } 8144 8145 if (ST->hasNEON() && (VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT)) 8146 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG); 8147 8148 if (ST->hasNEON() && VT == MVT::v8i8) 8149 if (SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG)) 8150 return NewOp; 8151 8152 if (ST->hasMVEIntegerOps()) 8153 if (SDValue NewOp = LowerVECTOR_SHUFFLEUsingMovs(Op, ShuffleMask, DAG)) 8154 return NewOp; 8155 8156 return SDValue(); 8157 } 8158 8159 static SDValue LowerINSERT_VECTOR_ELT_i1(SDValue Op, SelectionDAG &DAG, 8160 const ARMSubtarget *ST) { 8161 EVT VecVT = Op.getOperand(0).getValueType(); 8162 SDLoc dl(Op); 8163 8164 assert(ST->hasMVEIntegerOps() && 8165 "LowerINSERT_VECTOR_ELT_i1 called without MVE!"); 8166 8167 SDValue Conv = 8168 DAG.getNode(ARMISD::PREDICATE_CAST, dl, MVT::i32, Op->getOperand(0)); 8169 unsigned Lane = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue(); 8170 unsigned LaneWidth = 8171 getVectorTyFromPredicateVector(VecVT).getScalarSizeInBits() / 8; 8172 unsigned Mask = ((1 << LaneWidth) - 1) << Lane * LaneWidth; 8173 SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::i32, 8174 Op.getOperand(1), DAG.getValueType(MVT::i1)); 8175 SDValue BFI = DAG.getNode(ARMISD::BFI, dl, MVT::i32, Conv, Ext, 8176 DAG.getConstant(~Mask, dl, MVT::i32)); 8177 return DAG.getNode(ARMISD::PREDICATE_CAST, dl, Op.getValueType(), BFI); 8178 } 8179 8180 SDValue ARMTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, 8181 SelectionDAG &DAG) const { 8182 // INSERT_VECTOR_ELT is legal only for immediate indexes. 8183 SDValue Lane = Op.getOperand(2); 8184 if (!isa<ConstantSDNode>(Lane)) 8185 return SDValue(); 8186 8187 SDValue Elt = Op.getOperand(1); 8188 EVT EltVT = Elt.getValueType(); 8189 8190 if (Subtarget->hasMVEIntegerOps() && 8191 Op.getValueType().getScalarSizeInBits() == 1) 8192 return LowerINSERT_VECTOR_ELT_i1(Op, DAG, Subtarget); 8193 8194 if (getTypeAction(*DAG.getContext(), EltVT) == 8195 TargetLowering::TypePromoteFloat) { 8196 // INSERT_VECTOR_ELT doesn't want f16 operands promoting to f32, 8197 // but the type system will try to do that if we don't intervene. 8198 // Reinterpret any such vector-element insertion as one with the 8199 // corresponding integer types. 8200 8201 SDLoc dl(Op); 8202 8203 EVT IEltVT = MVT::getIntegerVT(EltVT.getScalarSizeInBits()); 8204 assert(getTypeAction(*DAG.getContext(), IEltVT) != 8205 TargetLowering::TypePromoteFloat); 8206 8207 SDValue VecIn = Op.getOperand(0); 8208 EVT VecVT = VecIn.getValueType(); 8209 EVT IVecVT = EVT::getVectorVT(*DAG.getContext(), IEltVT, 8210 VecVT.getVectorNumElements()); 8211 8212 SDValue IElt = DAG.getNode(ISD::BITCAST, dl, IEltVT, Elt); 8213 SDValue IVecIn = DAG.getNode(ISD::BITCAST, dl, IVecVT, VecIn); 8214 SDValue IVecOut = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, IVecVT, 8215 IVecIn, IElt, Lane); 8216 return DAG.getNode(ISD::BITCAST, dl, VecVT, IVecOut); 8217 } 8218 8219 return Op; 8220 } 8221 8222 static SDValue LowerEXTRACT_VECTOR_ELT_i1(SDValue Op, SelectionDAG &DAG, 8223 const ARMSubtarget *ST) { 8224 EVT VecVT = Op.getOperand(0).getValueType(); 8225 SDLoc dl(Op); 8226 8227 assert(ST->hasMVEIntegerOps() && 8228 "LowerINSERT_VECTOR_ELT_i1 called without MVE!"); 8229 8230 SDValue Conv = 8231 DAG.getNode(ARMISD::PREDICATE_CAST, dl, MVT::i32, Op->getOperand(0)); 8232 unsigned Lane = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 8233 unsigned LaneWidth = 8234 getVectorTyFromPredicateVector(VecVT).getScalarSizeInBits() / 8; 8235 SDValue Shift = DAG.getNode(ISD::SRL, dl, MVT::i32, Conv, 8236 DAG.getConstant(Lane * LaneWidth, dl, MVT::i32)); 8237 return Shift; 8238 } 8239 8240 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG, 8241 const ARMSubtarget *ST) { 8242 // EXTRACT_VECTOR_ELT is legal only for immediate indexes. 8243 SDValue Lane = Op.getOperand(1); 8244 if (!isa<ConstantSDNode>(Lane)) 8245 return SDValue(); 8246 8247 SDValue Vec = Op.getOperand(0); 8248 EVT VT = Vec.getValueType(); 8249 8250 if (ST->hasMVEIntegerOps() && VT.getScalarSizeInBits() == 1) 8251 return LowerEXTRACT_VECTOR_ELT_i1(Op, DAG, ST); 8252 8253 if (Op.getValueType() == MVT::i32 && Vec.getScalarValueSizeInBits() < 32) { 8254 SDLoc dl(Op); 8255 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane); 8256 } 8257 8258 return Op; 8259 } 8260 8261 static SDValue LowerCONCAT_VECTORS_i1(SDValue Op, SelectionDAG &DAG, 8262 const ARMSubtarget *ST) { 8263 SDValue V1 = Op.getOperand(0); 8264 SDValue V2 = Op.getOperand(1); 8265 SDLoc dl(Op); 8266 EVT VT = Op.getValueType(); 8267 EVT Op1VT = V1.getValueType(); 8268 EVT Op2VT = V2.getValueType(); 8269 unsigned NumElts = VT.getVectorNumElements(); 8270 8271 assert(Op1VT == Op2VT && "Operand types don't match!"); 8272 assert(VT.getScalarSizeInBits() == 1 && 8273 "Unexpected custom CONCAT_VECTORS lowering"); 8274 assert(ST->hasMVEIntegerOps() && 8275 "CONCAT_VECTORS lowering only supported for MVE"); 8276 8277 SDValue NewV1 = PromoteMVEPredVector(dl, V1, Op1VT, DAG); 8278 SDValue NewV2 = PromoteMVEPredVector(dl, V2, Op2VT, DAG); 8279 8280 // We now have Op1 + Op2 promoted to vectors of integers, where v8i1 gets 8281 // promoted to v8i16, etc. 8282 8283 MVT ElType = getVectorTyFromPredicateVector(VT).getScalarType().getSimpleVT(); 8284 8285 // Extract the vector elements from Op1 and Op2 one by one and truncate them 8286 // to be the right size for the destination. For example, if Op1 is v4i1 then 8287 // the promoted vector is v4i32. The result of concatentation gives a v8i1, 8288 // which when promoted is v8i16. That means each i32 element from Op1 needs 8289 // truncating to i16 and inserting in the result. 8290 EVT ConcatVT = MVT::getVectorVT(ElType, NumElts); 8291 SDValue ConVec = DAG.getNode(ISD::UNDEF, dl, ConcatVT); 8292 auto ExractInto = [&DAG, &dl](SDValue NewV, SDValue ConVec, unsigned &j) { 8293 EVT NewVT = NewV.getValueType(); 8294 EVT ConcatVT = ConVec.getValueType(); 8295 for (unsigned i = 0, e = NewVT.getVectorNumElements(); i < e; i++, j++) { 8296 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, NewV, 8297 DAG.getIntPtrConstant(i, dl)); 8298 ConVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ConcatVT, ConVec, Elt, 8299 DAG.getConstant(j, dl, MVT::i32)); 8300 } 8301 return ConVec; 8302 }; 8303 unsigned j = 0; 8304 ConVec = ExractInto(NewV1, ConVec, j); 8305 ConVec = ExractInto(NewV2, ConVec, j); 8306 8307 // Now return the result of comparing the subvector with zero, 8308 // which will generate a real predicate, i.e. v4i1, v8i1 or v16i1. 8309 return DAG.getNode(ARMISD::VCMPZ, dl, VT, ConVec, 8310 DAG.getConstant(ARMCC::NE, dl, MVT::i32)); 8311 } 8312 8313 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG, 8314 const ARMSubtarget *ST) { 8315 EVT VT = Op->getValueType(0); 8316 if (ST->hasMVEIntegerOps() && VT.getScalarSizeInBits() == 1) 8317 return LowerCONCAT_VECTORS_i1(Op, DAG, ST); 8318 8319 // The only time a CONCAT_VECTORS operation can have legal types is when 8320 // two 64-bit vectors are concatenated to a 128-bit vector. 8321 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 && 8322 "unexpected CONCAT_VECTORS"); 8323 SDLoc dl(Op); 8324 SDValue Val = DAG.getUNDEF(MVT::v2f64); 8325 SDValue Op0 = Op.getOperand(0); 8326 SDValue Op1 = Op.getOperand(1); 8327 if (!Op0.isUndef()) 8328 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 8329 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0), 8330 DAG.getIntPtrConstant(0, dl)); 8331 if (!Op1.isUndef()) 8332 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 8333 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1), 8334 DAG.getIntPtrConstant(1, dl)); 8335 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val); 8336 } 8337 8338 static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG, 8339 const ARMSubtarget *ST) { 8340 SDValue V1 = Op.getOperand(0); 8341 SDValue V2 = Op.getOperand(1); 8342 SDLoc dl(Op); 8343 EVT VT = Op.getValueType(); 8344 EVT Op1VT = V1.getValueType(); 8345 unsigned NumElts = VT.getVectorNumElements(); 8346 unsigned Index = cast<ConstantSDNode>(V2)->getZExtValue(); 8347 8348 assert(VT.getScalarSizeInBits() == 1 && 8349 "Unexpected custom EXTRACT_SUBVECTOR lowering"); 8350 assert(ST->hasMVEIntegerOps() && 8351 "EXTRACT_SUBVECTOR lowering only supported for MVE"); 8352 8353 SDValue NewV1 = PromoteMVEPredVector(dl, V1, Op1VT, DAG); 8354 8355 // We now have Op1 promoted to a vector of integers, where v8i1 gets 8356 // promoted to v8i16, etc. 8357 8358 MVT ElType = getVectorTyFromPredicateVector(VT).getScalarType().getSimpleVT(); 8359 8360 EVT SubVT = MVT::getVectorVT(ElType, NumElts); 8361 SDValue SubVec = DAG.getNode(ISD::UNDEF, dl, SubVT); 8362 for (unsigned i = Index, j = 0; i < (Index + NumElts); i++, j++) { 8363 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, NewV1, 8364 DAG.getIntPtrConstant(i, dl)); 8365 SubVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubVT, SubVec, Elt, 8366 DAG.getConstant(j, dl, MVT::i32)); 8367 } 8368 8369 // Now return the result of comparing the subvector with zero, 8370 // which will generate a real predicate, i.e. v4i1, v8i1 or v16i1. 8371 return DAG.getNode(ARMISD::VCMPZ, dl, VT, SubVec, 8372 DAG.getConstant(ARMCC::NE, dl, MVT::i32)); 8373 } 8374 8375 /// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each 8376 /// element has been zero/sign-extended, depending on the isSigned parameter, 8377 /// from an integer type half its size. 8378 static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG, 8379 bool isSigned) { 8380 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32. 8381 EVT VT = N->getValueType(0); 8382 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) { 8383 SDNode *BVN = N->getOperand(0).getNode(); 8384 if (BVN->getValueType(0) != MVT::v4i32 || 8385 BVN->getOpcode() != ISD::BUILD_VECTOR) 8386 return false; 8387 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0; 8388 unsigned HiElt = 1 - LoElt; 8389 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt)); 8390 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt)); 8391 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2)); 8392 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2)); 8393 if (!Lo0 || !Hi0 || !Lo1 || !Hi1) 8394 return false; 8395 if (isSigned) { 8396 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 && 8397 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32) 8398 return true; 8399 } else { 8400 if (Hi0->isNullValue() && Hi1->isNullValue()) 8401 return true; 8402 } 8403 return false; 8404 } 8405 8406 if (N->getOpcode() != ISD::BUILD_VECTOR) 8407 return false; 8408 8409 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 8410 SDNode *Elt = N->getOperand(i).getNode(); 8411 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) { 8412 unsigned EltSize = VT.getScalarSizeInBits(); 8413 unsigned HalfSize = EltSize / 2; 8414 if (isSigned) { 8415 if (!isIntN(HalfSize, C->getSExtValue())) 8416 return false; 8417 } else { 8418 if (!isUIntN(HalfSize, C->getZExtValue())) 8419 return false; 8420 } 8421 continue; 8422 } 8423 return false; 8424 } 8425 8426 return true; 8427 } 8428 8429 /// isSignExtended - Check if a node is a vector value that is sign-extended 8430 /// or a constant BUILD_VECTOR with sign-extended elements. 8431 static bool isSignExtended(SDNode *N, SelectionDAG &DAG) { 8432 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N)) 8433 return true; 8434 if (isExtendedBUILD_VECTOR(N, DAG, true)) 8435 return true; 8436 return false; 8437 } 8438 8439 /// isZeroExtended - Check if a node is a vector value that is zero-extended 8440 /// or a constant BUILD_VECTOR with zero-extended elements. 8441 static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) { 8442 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N)) 8443 return true; 8444 if (isExtendedBUILD_VECTOR(N, DAG, false)) 8445 return true; 8446 return false; 8447 } 8448 8449 static EVT getExtensionTo64Bits(const EVT &OrigVT) { 8450 if (OrigVT.getSizeInBits() >= 64) 8451 return OrigVT; 8452 8453 assert(OrigVT.isSimple() && "Expecting a simple value type"); 8454 8455 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy; 8456 switch (OrigSimpleTy) { 8457 default: llvm_unreachable("Unexpected Vector Type"); 8458 case MVT::v2i8: 8459 case MVT::v2i16: 8460 return MVT::v2i32; 8461 case MVT::v4i8: 8462 return MVT::v4i16; 8463 } 8464 } 8465 8466 /// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total 8467 /// value size to 64 bits. We need a 64-bit D register as an operand to VMULL. 8468 /// We insert the required extension here to get the vector to fill a D register. 8469 static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG, 8470 const EVT &OrigTy, 8471 const EVT &ExtTy, 8472 unsigned ExtOpcode) { 8473 // The vector originally had a size of OrigTy. It was then extended to ExtTy. 8474 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than 8475 // 64-bits we need to insert a new extension so that it will be 64-bits. 8476 assert(ExtTy.is128BitVector() && "Unexpected extension size"); 8477 if (OrigTy.getSizeInBits() >= 64) 8478 return N; 8479 8480 // Must extend size to at least 64 bits to be used as an operand for VMULL. 8481 EVT NewVT = getExtensionTo64Bits(OrigTy); 8482 8483 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N); 8484 } 8485 8486 /// SkipLoadExtensionForVMULL - return a load of the original vector size that 8487 /// does not do any sign/zero extension. If the original vector is less 8488 /// than 64 bits, an appropriate extension will be added after the load to 8489 /// reach a total size of 64 bits. We have to add the extension separately 8490 /// because ARM does not have a sign/zero extending load for vectors. 8491 static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) { 8492 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT()); 8493 8494 // The load already has the right type. 8495 if (ExtendedTy == LD->getMemoryVT()) 8496 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(), 8497 LD->getBasePtr(), LD->getPointerInfo(), 8498 LD->getAlignment(), LD->getMemOperand()->getFlags()); 8499 8500 // We need to create a zextload/sextload. We cannot just create a load 8501 // followed by a zext/zext node because LowerMUL is also run during normal 8502 // operation legalization where we can't create illegal types. 8503 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy, 8504 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(), 8505 LD->getMemoryVT(), LD->getAlignment(), 8506 LD->getMemOperand()->getFlags()); 8507 } 8508 8509 /// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND, 8510 /// extending load, or BUILD_VECTOR with extended elements, return the 8511 /// unextended value. The unextended vector should be 64 bits so that it can 8512 /// be used as an operand to a VMULL instruction. If the original vector size 8513 /// before extension is less than 64 bits we add a an extension to resize 8514 /// the vector to 64 bits. 8515 static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) { 8516 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND) 8517 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG, 8518 N->getOperand(0)->getValueType(0), 8519 N->getValueType(0), 8520 N->getOpcode()); 8521 8522 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 8523 assert((ISD::isSEXTLoad(LD) || ISD::isZEXTLoad(LD)) && 8524 "Expected extending load"); 8525 8526 SDValue newLoad = SkipLoadExtensionForVMULL(LD, DAG); 8527 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), newLoad.getValue(1)); 8528 unsigned Opcode = ISD::isSEXTLoad(LD) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 8529 SDValue extLoad = 8530 DAG.getNode(Opcode, SDLoc(newLoad), LD->getValueType(0), newLoad); 8531 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 0), extLoad); 8532 8533 return newLoad; 8534 } 8535 8536 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will 8537 // have been legalized as a BITCAST from v4i32. 8538 if (N->getOpcode() == ISD::BITCAST) { 8539 SDNode *BVN = N->getOperand(0).getNode(); 8540 assert(BVN->getOpcode() == ISD::BUILD_VECTOR && 8541 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR"); 8542 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0; 8543 return DAG.getBuildVector( 8544 MVT::v2i32, SDLoc(N), 8545 {BVN->getOperand(LowElt), BVN->getOperand(LowElt + 2)}); 8546 } 8547 // Construct a new BUILD_VECTOR with elements truncated to half the size. 8548 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR"); 8549 EVT VT = N->getValueType(0); 8550 unsigned EltSize = VT.getScalarSizeInBits() / 2; 8551 unsigned NumElts = VT.getVectorNumElements(); 8552 MVT TruncVT = MVT::getIntegerVT(EltSize); 8553 SmallVector<SDValue, 8> Ops; 8554 SDLoc dl(N); 8555 for (unsigned i = 0; i != NumElts; ++i) { 8556 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i)); 8557 const APInt &CInt = C->getAPIntValue(); 8558 // Element types smaller than 32 bits are not legal, so use i32 elements. 8559 // The values are implicitly truncated so sext vs. zext doesn't matter. 8560 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32)); 8561 } 8562 return DAG.getBuildVector(MVT::getVectorVT(TruncVT, NumElts), dl, Ops); 8563 } 8564 8565 static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) { 8566 unsigned Opcode = N->getOpcode(); 8567 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 8568 SDNode *N0 = N->getOperand(0).getNode(); 8569 SDNode *N1 = N->getOperand(1).getNode(); 8570 return N0->hasOneUse() && N1->hasOneUse() && 8571 isSignExtended(N0, DAG) && isSignExtended(N1, DAG); 8572 } 8573 return false; 8574 } 8575 8576 static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) { 8577 unsigned Opcode = N->getOpcode(); 8578 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 8579 SDNode *N0 = N->getOperand(0).getNode(); 8580 SDNode *N1 = N->getOperand(1).getNode(); 8581 return N0->hasOneUse() && N1->hasOneUse() && 8582 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG); 8583 } 8584 return false; 8585 } 8586 8587 static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) { 8588 // Multiplications are only custom-lowered for 128-bit vectors so that 8589 // VMULL can be detected. Otherwise v2i64 multiplications are not legal. 8590 EVT VT = Op.getValueType(); 8591 assert(VT.is128BitVector() && VT.isInteger() && 8592 "unexpected type for custom-lowering ISD::MUL"); 8593 SDNode *N0 = Op.getOperand(0).getNode(); 8594 SDNode *N1 = Op.getOperand(1).getNode(); 8595 unsigned NewOpc = 0; 8596 bool isMLA = false; 8597 bool isN0SExt = isSignExtended(N0, DAG); 8598 bool isN1SExt = isSignExtended(N1, DAG); 8599 if (isN0SExt && isN1SExt) 8600 NewOpc = ARMISD::VMULLs; 8601 else { 8602 bool isN0ZExt = isZeroExtended(N0, DAG); 8603 bool isN1ZExt = isZeroExtended(N1, DAG); 8604 if (isN0ZExt && isN1ZExt) 8605 NewOpc = ARMISD::VMULLu; 8606 else if (isN1SExt || isN1ZExt) { 8607 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these 8608 // into (s/zext A * s/zext C) + (s/zext B * s/zext C) 8609 if (isN1SExt && isAddSubSExt(N0, DAG)) { 8610 NewOpc = ARMISD::VMULLs; 8611 isMLA = true; 8612 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) { 8613 NewOpc = ARMISD::VMULLu; 8614 isMLA = true; 8615 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) { 8616 std::swap(N0, N1); 8617 NewOpc = ARMISD::VMULLu; 8618 isMLA = true; 8619 } 8620 } 8621 8622 if (!NewOpc) { 8623 if (VT == MVT::v2i64) 8624 // Fall through to expand this. It is not legal. 8625 return SDValue(); 8626 else 8627 // Other vector multiplications are legal. 8628 return Op; 8629 } 8630 } 8631 8632 // Legalize to a VMULL instruction. 8633 SDLoc DL(Op); 8634 SDValue Op0; 8635 SDValue Op1 = SkipExtensionForVMULL(N1, DAG); 8636 if (!isMLA) { 8637 Op0 = SkipExtensionForVMULL(N0, DAG); 8638 assert(Op0.getValueType().is64BitVector() && 8639 Op1.getValueType().is64BitVector() && 8640 "unexpected types for extended operands to VMULL"); 8641 return DAG.getNode(NewOpc, DL, VT, Op0, Op1); 8642 } 8643 8644 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during 8645 // isel lowering to take advantage of no-stall back to back vmul + vmla. 8646 // vmull q0, d4, d6 8647 // vmlal q0, d5, d6 8648 // is faster than 8649 // vaddl q0, d4, d5 8650 // vmovl q1, d6 8651 // vmul q0, q0, q1 8652 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG); 8653 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG); 8654 EVT Op1VT = Op1.getValueType(); 8655 return DAG.getNode(N0->getOpcode(), DL, VT, 8656 DAG.getNode(NewOpc, DL, VT, 8657 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1), 8658 DAG.getNode(NewOpc, DL, VT, 8659 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1)); 8660 } 8661 8662 static SDValue LowerSDIV_v4i8(SDValue X, SDValue Y, const SDLoc &dl, 8663 SelectionDAG &DAG) { 8664 // TODO: Should this propagate fast-math-flags? 8665 8666 // Convert to float 8667 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo)); 8668 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo)); 8669 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X); 8670 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y); 8671 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X); 8672 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y); 8673 // Get reciprocal estimate. 8674 // float4 recip = vrecpeq_f32(yf); 8675 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 8676 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32), 8677 Y); 8678 // Because char has a smaller range than uchar, we can actually get away 8679 // without any newton steps. This requires that we use a weird bias 8680 // of 0xb000, however (again, this has been exhaustively tested). 8681 // float4 result = as_float4(as_int4(xf*recip) + 0xb000); 8682 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y); 8683 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X); 8684 Y = DAG.getConstant(0xb000, dl, MVT::v4i32); 8685 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y); 8686 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X); 8687 // Convert back to short. 8688 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X); 8689 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X); 8690 return X; 8691 } 8692 8693 static SDValue LowerSDIV_v4i16(SDValue N0, SDValue N1, const SDLoc &dl, 8694 SelectionDAG &DAG) { 8695 // TODO: Should this propagate fast-math-flags? 8696 8697 SDValue N2; 8698 // Convert to float. 8699 // float4 yf = vcvt_f32_s32(vmovl_s16(y)); 8700 // float4 xf = vcvt_f32_s32(vmovl_s16(x)); 8701 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0); 8702 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1); 8703 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 8704 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 8705 8706 // Use reciprocal estimate and one refinement step. 8707 // float4 recip = vrecpeq_f32(yf); 8708 // recip *= vrecpsq_f32(yf, recip); 8709 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 8710 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32), 8711 N1); 8712 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 8713 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32), 8714 N1, N2); 8715 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 8716 // Because short has a smaller range than ushort, we can actually get away 8717 // with only a single newton step. This requires that we use a weird bias 8718 // of 89, however (again, this has been exhaustively tested). 8719 // float4 result = as_float4(as_int4(xf*recip) + 0x89); 8720 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 8721 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 8722 N1 = DAG.getConstant(0x89, dl, MVT::v4i32); 8723 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 8724 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 8725 // Convert back to integer and return. 8726 // return vmovn_s32(vcvt_s32_f32(result)); 8727 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 8728 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 8729 return N0; 8730 } 8731 8732 static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG, 8733 const ARMSubtarget *ST) { 8734 EVT VT = Op.getValueType(); 8735 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 8736 "unexpected type for custom-lowering ISD::SDIV"); 8737 8738 SDLoc dl(Op); 8739 SDValue N0 = Op.getOperand(0); 8740 SDValue N1 = Op.getOperand(1); 8741 SDValue N2, N3; 8742 8743 if (VT == MVT::v8i8) { 8744 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0); 8745 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1); 8746 8747 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 8748 DAG.getIntPtrConstant(4, dl)); 8749 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 8750 DAG.getIntPtrConstant(4, dl)); 8751 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 8752 DAG.getIntPtrConstant(0, dl)); 8753 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 8754 DAG.getIntPtrConstant(0, dl)); 8755 8756 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16 8757 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16 8758 8759 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 8760 N0 = LowerCONCAT_VECTORS(N0, DAG, ST); 8761 8762 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0); 8763 return N0; 8764 } 8765 return LowerSDIV_v4i16(N0, N1, dl, DAG); 8766 } 8767 8768 static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG, 8769 const ARMSubtarget *ST) { 8770 // TODO: Should this propagate fast-math-flags? 8771 EVT VT = Op.getValueType(); 8772 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 8773 "unexpected type for custom-lowering ISD::UDIV"); 8774 8775 SDLoc dl(Op); 8776 SDValue N0 = Op.getOperand(0); 8777 SDValue N1 = Op.getOperand(1); 8778 SDValue N2, N3; 8779 8780 if (VT == MVT::v8i8) { 8781 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0); 8782 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1); 8783 8784 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 8785 DAG.getIntPtrConstant(4, dl)); 8786 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 8787 DAG.getIntPtrConstant(4, dl)); 8788 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 8789 DAG.getIntPtrConstant(0, dl)); 8790 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 8791 DAG.getIntPtrConstant(0, dl)); 8792 8793 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16 8794 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16 8795 8796 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 8797 N0 = LowerCONCAT_VECTORS(N0, DAG, ST); 8798 8799 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8, 8800 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl, 8801 MVT::i32), 8802 N0); 8803 return N0; 8804 } 8805 8806 // v4i16 sdiv ... Convert to float. 8807 // float4 yf = vcvt_f32_s32(vmovl_u16(y)); 8808 // float4 xf = vcvt_f32_s32(vmovl_u16(x)); 8809 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0); 8810 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1); 8811 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 8812 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 8813 8814 // Use reciprocal estimate and two refinement steps. 8815 // float4 recip = vrecpeq_f32(yf); 8816 // recip *= vrecpsq_f32(yf, recip); 8817 // recip *= vrecpsq_f32(yf, recip); 8818 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 8819 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32), 8820 BN1); 8821 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 8822 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32), 8823 BN1, N2); 8824 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 8825 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 8826 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32), 8827 BN1, N2); 8828 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 8829 // Simply multiplying by the reciprocal estimate can leave us a few ulps 8830 // too low, so we add 2 ulps (exhaustive testing shows that this is enough, 8831 // and that it will never cause us to return an answer too large). 8832 // float4 result = as_float4(as_int4(xf*recip) + 2); 8833 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 8834 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 8835 N1 = DAG.getConstant(2, dl, MVT::v4i32); 8836 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 8837 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 8838 // Convert back to integer and return. 8839 // return vmovn_u32(vcvt_s32_f32(result)); 8840 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 8841 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 8842 return N0; 8843 } 8844 8845 static SDValue LowerADDSUBCARRY(SDValue Op, SelectionDAG &DAG) { 8846 SDNode *N = Op.getNode(); 8847 EVT VT = N->getValueType(0); 8848 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 8849 8850 SDValue Carry = Op.getOperand(2); 8851 8852 SDLoc DL(Op); 8853 8854 SDValue Result; 8855 if (Op.getOpcode() == ISD::ADDCARRY) { 8856 // This converts the boolean value carry into the carry flag. 8857 Carry = ConvertBooleanCarryToCarryFlag(Carry, DAG); 8858 8859 // Do the addition proper using the carry flag we wanted. 8860 Result = DAG.getNode(ARMISD::ADDE, DL, VTs, Op.getOperand(0), 8861 Op.getOperand(1), Carry); 8862 8863 // Now convert the carry flag into a boolean value. 8864 Carry = ConvertCarryFlagToBooleanCarry(Result.getValue(1), VT, DAG); 8865 } else { 8866 // ARMISD::SUBE expects a carry not a borrow like ISD::SUBCARRY so we 8867 // have to invert the carry first. 8868 Carry = DAG.getNode(ISD::SUB, DL, MVT::i32, 8869 DAG.getConstant(1, DL, MVT::i32), Carry); 8870 // This converts the boolean value carry into the carry flag. 8871 Carry = ConvertBooleanCarryToCarryFlag(Carry, DAG); 8872 8873 // Do the subtraction proper using the carry flag we wanted. 8874 Result = DAG.getNode(ARMISD::SUBE, DL, VTs, Op.getOperand(0), 8875 Op.getOperand(1), Carry); 8876 8877 // Now convert the carry flag into a boolean value. 8878 Carry = ConvertCarryFlagToBooleanCarry(Result.getValue(1), VT, DAG); 8879 // But the carry returned by ARMISD::SUBE is not a borrow as expected 8880 // by ISD::SUBCARRY, so compute 1 - C. 8881 Carry = DAG.getNode(ISD::SUB, DL, MVT::i32, 8882 DAG.getConstant(1, DL, MVT::i32), Carry); 8883 } 8884 8885 // Return both values. 8886 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Result, Carry); 8887 } 8888 8889 SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const { 8890 assert(Subtarget->isTargetDarwin()); 8891 8892 // For iOS, we want to call an alternative entry point: __sincos_stret, 8893 // return values are passed via sret. 8894 SDLoc dl(Op); 8895 SDValue Arg = Op.getOperand(0); 8896 EVT ArgVT = Arg.getValueType(); 8897 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 8898 auto PtrVT = getPointerTy(DAG.getDataLayout()); 8899 8900 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 8901 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8902 8903 // Pair of floats / doubles used to pass the result. 8904 Type *RetTy = StructType::get(ArgTy, ArgTy); 8905 auto &DL = DAG.getDataLayout(); 8906 8907 ArgListTy Args; 8908 bool ShouldUseSRet = Subtarget->isAPCS_ABI(); 8909 SDValue SRet; 8910 if (ShouldUseSRet) { 8911 // Create stack object for sret. 8912 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy); 8913 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy); 8914 int FrameIdx = MFI.CreateStackObject(ByteSize, StackAlign, false); 8915 SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy(DL)); 8916 8917 ArgListEntry Entry; 8918 Entry.Node = SRet; 8919 Entry.Ty = RetTy->getPointerTo(); 8920 Entry.IsSExt = false; 8921 Entry.IsZExt = false; 8922 Entry.IsSRet = true; 8923 Args.push_back(Entry); 8924 RetTy = Type::getVoidTy(*DAG.getContext()); 8925 } 8926 8927 ArgListEntry Entry; 8928 Entry.Node = Arg; 8929 Entry.Ty = ArgTy; 8930 Entry.IsSExt = false; 8931 Entry.IsZExt = false; 8932 Args.push_back(Entry); 8933 8934 RTLIB::Libcall LC = 8935 (ArgVT == MVT::f64) ? RTLIB::SINCOS_STRET_F64 : RTLIB::SINCOS_STRET_F32; 8936 const char *LibcallName = getLibcallName(LC); 8937 CallingConv::ID CC = getLibcallCallingConv(LC); 8938 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL)); 8939 8940 TargetLowering::CallLoweringInfo CLI(DAG); 8941 CLI.setDebugLoc(dl) 8942 .setChain(DAG.getEntryNode()) 8943 .setCallee(CC, RetTy, Callee, std::move(Args)) 8944 .setDiscardResult(ShouldUseSRet); 8945 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 8946 8947 if (!ShouldUseSRet) 8948 return CallResult.first; 8949 8950 SDValue LoadSin = 8951 DAG.getLoad(ArgVT, dl, CallResult.second, SRet, MachinePointerInfo()); 8952 8953 // Address of cos field. 8954 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet, 8955 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl)); 8956 SDValue LoadCos = 8957 DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add, MachinePointerInfo()); 8958 8959 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT); 8960 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, 8961 LoadSin.getValue(0), LoadCos.getValue(0)); 8962 } 8963 8964 SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG, 8965 bool Signed, 8966 SDValue &Chain) const { 8967 EVT VT = Op.getValueType(); 8968 assert((VT == MVT::i32 || VT == MVT::i64) && 8969 "unexpected type for custom lowering DIV"); 8970 SDLoc dl(Op); 8971 8972 const auto &DL = DAG.getDataLayout(); 8973 const auto &TLI = DAG.getTargetLoweringInfo(); 8974 8975 const char *Name = nullptr; 8976 if (Signed) 8977 Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64"; 8978 else 8979 Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64"; 8980 8981 SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL)); 8982 8983 ARMTargetLowering::ArgListTy Args; 8984 8985 for (auto AI : {1, 0}) { 8986 ArgListEntry Arg; 8987 Arg.Node = Op.getOperand(AI); 8988 Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext()); 8989 Args.push_back(Arg); 8990 } 8991 8992 CallLoweringInfo CLI(DAG); 8993 CLI.setDebugLoc(dl) 8994 .setChain(Chain) 8995 .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()), 8996 ES, std::move(Args)); 8997 8998 return LowerCallTo(CLI).first; 8999 } 9000 9001 // This is a code size optimisation: return the original SDIV node to 9002 // DAGCombiner when we don't want to expand SDIV into a sequence of 9003 // instructions, and an empty node otherwise which will cause the 9004 // SDIV to be expanded in DAGCombine. 9005 SDValue 9006 ARMTargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor, 9007 SelectionDAG &DAG, 9008 SmallVectorImpl<SDNode *> &Created) const { 9009 // TODO: Support SREM 9010 if (N->getOpcode() != ISD::SDIV) 9011 return SDValue(); 9012 9013 const auto &ST = static_cast<const ARMSubtarget&>(DAG.getSubtarget()); 9014 const bool MinSize = ST.hasMinSize(); 9015 const bool HasDivide = ST.isThumb() ? ST.hasDivideInThumbMode() 9016 : ST.hasDivideInARMMode(); 9017 9018 // Don't touch vector types; rewriting this may lead to scalarizing 9019 // the int divs. 9020 if (N->getOperand(0).getValueType().isVector()) 9021 return SDValue(); 9022 9023 // Bail if MinSize is not set, and also for both ARM and Thumb mode we need 9024 // hwdiv support for this to be really profitable. 9025 if (!(MinSize && HasDivide)) 9026 return SDValue(); 9027 9028 // ARM mode is a bit simpler than Thumb: we can handle large power 9029 // of 2 immediates with 1 mov instruction; no further checks required, 9030 // just return the sdiv node. 9031 if (!ST.isThumb()) 9032 return SDValue(N, 0); 9033 9034 // In Thumb mode, immediates larger than 128 need a wide 4-byte MOV, 9035 // and thus lose the code size benefits of a MOVS that requires only 2. 9036 // TargetTransformInfo and 'getIntImmCodeSizeCost' could be helpful here, 9037 // but as it's doing exactly this, it's not worth the trouble to get TTI. 9038 if (Divisor.sgt(128)) 9039 return SDValue(); 9040 9041 return SDValue(N, 0); 9042 } 9043 9044 SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG, 9045 bool Signed) const { 9046 assert(Op.getValueType() == MVT::i32 && 9047 "unexpected type for custom lowering DIV"); 9048 SDLoc dl(Op); 9049 9050 SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other, 9051 DAG.getEntryNode(), Op.getOperand(1)); 9052 9053 return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK); 9054 } 9055 9056 static SDValue WinDBZCheckDenominator(SelectionDAG &DAG, SDNode *N, SDValue InChain) { 9057 SDLoc DL(N); 9058 SDValue Op = N->getOperand(1); 9059 if (N->getValueType(0) == MVT::i32) 9060 return DAG.getNode(ARMISD::WIN__DBZCHK, DL, MVT::Other, InChain, Op); 9061 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Op, 9062 DAG.getConstant(0, DL, MVT::i32)); 9063 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Op, 9064 DAG.getConstant(1, DL, MVT::i32)); 9065 return DAG.getNode(ARMISD::WIN__DBZCHK, DL, MVT::Other, InChain, 9066 DAG.getNode(ISD::OR, DL, MVT::i32, Lo, Hi)); 9067 } 9068 9069 void ARMTargetLowering::ExpandDIV_Windows( 9070 SDValue Op, SelectionDAG &DAG, bool Signed, 9071 SmallVectorImpl<SDValue> &Results) const { 9072 const auto &DL = DAG.getDataLayout(); 9073 const auto &TLI = DAG.getTargetLoweringInfo(); 9074 9075 assert(Op.getValueType() == MVT::i64 && 9076 "unexpected type for custom lowering DIV"); 9077 SDLoc dl(Op); 9078 9079 SDValue DBZCHK = WinDBZCheckDenominator(DAG, Op.getNode(), DAG.getEntryNode()); 9080 9081 SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK); 9082 9083 SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result); 9084 SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result, 9085 DAG.getConstant(32, dl, TLI.getPointerTy(DL))); 9086 Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper); 9087 9088 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lower, Upper)); 9089 } 9090 9091 static SDValue LowerPredicateLoad(SDValue Op, SelectionDAG &DAG) { 9092 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode()); 9093 EVT MemVT = LD->getMemoryVT(); 9094 assert((MemVT == MVT::v4i1 || MemVT == MVT::v8i1 || MemVT == MVT::v16i1) && 9095 "Expected a predicate type!"); 9096 assert(MemVT == Op.getValueType()); 9097 assert(LD->getExtensionType() == ISD::NON_EXTLOAD && 9098 "Expected a non-extending load"); 9099 assert(LD->isUnindexed() && "Expected a unindexed load"); 9100 9101 // The basic MVE VLDR on a v4i1/v8i1 actually loads the entire 16bit 9102 // predicate, with the "v4i1" bits spread out over the 16 bits loaded. We 9103 // need to make sure that 8/4 bits are actually loaded into the correct 9104 // place, which means loading the value and then shuffling the values into 9105 // the bottom bits of the predicate. 9106 // Equally, VLDR for an v16i1 will actually load 32bits (so will be incorrect 9107 // for BE). 9108 9109 SDLoc dl(Op); 9110 SDValue Load = DAG.getExtLoad( 9111 ISD::EXTLOAD, dl, MVT::i32, LD->getChain(), LD->getBasePtr(), 9112 EVT::getIntegerVT(*DAG.getContext(), MemVT.getSizeInBits()), 9113 LD->getMemOperand()); 9114 SDValue Pred = DAG.getNode(ARMISD::PREDICATE_CAST, dl, MVT::v16i1, Load); 9115 if (MemVT != MVT::v16i1) 9116 Pred = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MemVT, Pred, 9117 DAG.getConstant(0, dl, MVT::i32)); 9118 return DAG.getMergeValues({Pred, Load.getValue(1)}, dl); 9119 } 9120 9121 static SDValue LowerPredicateStore(SDValue Op, SelectionDAG &DAG) { 9122 StoreSDNode *ST = cast<StoreSDNode>(Op.getNode()); 9123 EVT MemVT = ST->getMemoryVT(); 9124 assert((MemVT == MVT::v4i1 || MemVT == MVT::v8i1 || MemVT == MVT::v16i1) && 9125 "Expected a predicate type!"); 9126 assert(MemVT == ST->getValue().getValueType()); 9127 assert(!ST->isTruncatingStore() && "Expected a non-extending store"); 9128 assert(ST->isUnindexed() && "Expected a unindexed store"); 9129 9130 // Only store the v4i1 or v8i1 worth of bits, via a buildvector with top bits 9131 // unset and a scalar store. 9132 SDLoc dl(Op); 9133 SDValue Build = ST->getValue(); 9134 if (MemVT != MVT::v16i1) { 9135 SmallVector<SDValue, 16> Ops; 9136 for (unsigned I = 0; I < MemVT.getVectorNumElements(); I++) 9137 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, Build, 9138 DAG.getConstant(I, dl, MVT::i32))); 9139 for (unsigned I = MemVT.getVectorNumElements(); I < 16; I++) 9140 Ops.push_back(DAG.getUNDEF(MVT::i32)); 9141 Build = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i1, Ops); 9142 } 9143 SDValue GRP = DAG.getNode(ARMISD::PREDICATE_CAST, dl, MVT::i32, Build); 9144 return DAG.getTruncStore( 9145 ST->getChain(), dl, GRP, ST->getBasePtr(), 9146 EVT::getIntegerVT(*DAG.getContext(), MemVT.getSizeInBits()), 9147 ST->getMemOperand()); 9148 } 9149 9150 static bool isZeroVector(SDValue N) { 9151 return (ISD::isBuildVectorAllZeros(N.getNode()) || 9152 (N->getOpcode() == ARMISD::VMOVIMM && 9153 isNullConstant(N->getOperand(0)))); 9154 } 9155 9156 static SDValue LowerMLOAD(SDValue Op, SelectionDAG &DAG) { 9157 MaskedLoadSDNode *N = cast<MaskedLoadSDNode>(Op.getNode()); 9158 MVT VT = Op.getSimpleValueType(); 9159 SDValue Mask = N->getMask(); 9160 SDValue PassThru = N->getPassThru(); 9161 SDLoc dl(Op); 9162 9163 if (isZeroVector(PassThru)) 9164 return Op; 9165 9166 // MVE Masked loads use zero as the passthru value. Here we convert undef to 9167 // zero too, and other values are lowered to a select. 9168 SDValue ZeroVec = DAG.getNode(ARMISD::VMOVIMM, dl, VT, 9169 DAG.getTargetConstant(0, dl, MVT::i32)); 9170 SDValue NewLoad = DAG.getMaskedLoad( 9171 VT, dl, N->getChain(), N->getBasePtr(), N->getOffset(), Mask, ZeroVec, 9172 N->getMemoryVT(), N->getMemOperand(), N->getAddressingMode(), 9173 N->getExtensionType(), N->isExpandingLoad()); 9174 SDValue Combo = NewLoad; 9175 if (!PassThru.isUndef() && 9176 (PassThru.getOpcode() != ISD::BITCAST || 9177 !isZeroVector(PassThru->getOperand(0)))) 9178 Combo = DAG.getNode(ISD::VSELECT, dl, VT, Mask, NewLoad, PassThru); 9179 return DAG.getMergeValues({Combo, NewLoad.getValue(1)}, dl); 9180 } 9181 9182 static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) { 9183 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering())) 9184 // Acquire/Release load/store is not legal for targets without a dmb or 9185 // equivalent available. 9186 return SDValue(); 9187 9188 // Monotonic load/store is legal for all targets. 9189 return Op; 9190 } 9191 9192 static void ReplaceREADCYCLECOUNTER(SDNode *N, 9193 SmallVectorImpl<SDValue> &Results, 9194 SelectionDAG &DAG, 9195 const ARMSubtarget *Subtarget) { 9196 SDLoc DL(N); 9197 // Under Power Management extensions, the cycle-count is: 9198 // mrc p15, #0, <Rt>, c9, c13, #0 9199 SDValue Ops[] = { N->getOperand(0), // Chain 9200 DAG.getTargetConstant(Intrinsic::arm_mrc, DL, MVT::i32), 9201 DAG.getTargetConstant(15, DL, MVT::i32), 9202 DAG.getTargetConstant(0, DL, MVT::i32), 9203 DAG.getTargetConstant(9, DL, MVT::i32), 9204 DAG.getTargetConstant(13, DL, MVT::i32), 9205 DAG.getTargetConstant(0, DL, MVT::i32) 9206 }; 9207 9208 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL, 9209 DAG.getVTList(MVT::i32, MVT::Other), Ops); 9210 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32, 9211 DAG.getConstant(0, DL, MVT::i32))); 9212 Results.push_back(Cycles32.getValue(1)); 9213 } 9214 9215 static SDValue createGPRPairNode(SelectionDAG &DAG, SDValue V) { 9216 SDLoc dl(V.getNode()); 9217 SDValue VLo = DAG.getAnyExtOrTrunc(V, dl, MVT::i32); 9218 SDValue VHi = DAG.getAnyExtOrTrunc( 9219 DAG.getNode(ISD::SRL, dl, MVT::i64, V, DAG.getConstant(32, dl, MVT::i32)), 9220 dl, MVT::i32); 9221 bool isBigEndian = DAG.getDataLayout().isBigEndian(); 9222 if (isBigEndian) 9223 std::swap (VLo, VHi); 9224 SDValue RegClass = 9225 DAG.getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32); 9226 SDValue SubReg0 = DAG.getTargetConstant(ARM::gsub_0, dl, MVT::i32); 9227 SDValue SubReg1 = DAG.getTargetConstant(ARM::gsub_1, dl, MVT::i32); 9228 const SDValue Ops[] = { RegClass, VLo, SubReg0, VHi, SubReg1 }; 9229 return SDValue( 9230 DAG.getMachineNode(TargetOpcode::REG_SEQUENCE, dl, MVT::Untyped, Ops), 0); 9231 } 9232 9233 static void ReplaceCMP_SWAP_64Results(SDNode *N, 9234 SmallVectorImpl<SDValue> & Results, 9235 SelectionDAG &DAG) { 9236 assert(N->getValueType(0) == MVT::i64 && 9237 "AtomicCmpSwap on types less than 64 should be legal"); 9238 SDValue Ops[] = {N->getOperand(1), 9239 createGPRPairNode(DAG, N->getOperand(2)), 9240 createGPRPairNode(DAG, N->getOperand(3)), 9241 N->getOperand(0)}; 9242 SDNode *CmpSwap = DAG.getMachineNode( 9243 ARM::CMP_SWAP_64, SDLoc(N), 9244 DAG.getVTList(MVT::Untyped, MVT::i32, MVT::Other), Ops); 9245 9246 MachineMemOperand *MemOp = cast<MemSDNode>(N)->getMemOperand(); 9247 DAG.setNodeMemRefs(cast<MachineSDNode>(CmpSwap), {MemOp}); 9248 9249 bool isBigEndian = DAG.getDataLayout().isBigEndian(); 9250 9251 SDValue Lo = 9252 DAG.getTargetExtractSubreg(isBigEndian ? ARM::gsub_1 : ARM::gsub_0, 9253 SDLoc(N), MVT::i32, SDValue(CmpSwap, 0)); 9254 SDValue Hi = 9255 DAG.getTargetExtractSubreg(isBigEndian ? ARM::gsub_0 : ARM::gsub_1, 9256 SDLoc(N), MVT::i32, SDValue(CmpSwap, 0)); 9257 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, SDLoc(N), MVT::i64, Lo, Hi)); 9258 Results.push_back(SDValue(CmpSwap, 2)); 9259 } 9260 9261 SDValue ARMTargetLowering::LowerFSETCC(SDValue Op, SelectionDAG &DAG) const { 9262 SDLoc dl(Op); 9263 EVT VT = Op.getValueType(); 9264 SDValue Chain = Op.getOperand(0); 9265 SDValue LHS = Op.getOperand(1); 9266 SDValue RHS = Op.getOperand(2); 9267 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(3))->get(); 9268 bool IsSignaling = Op.getOpcode() == ISD::STRICT_FSETCCS; 9269 9270 // If we don't have instructions of this float type then soften to a libcall 9271 // and use SETCC instead. 9272 if (isUnsupportedFloatingType(LHS.getValueType())) { 9273 DAG.getTargetLoweringInfo().softenSetCCOperands( 9274 DAG, LHS.getValueType(), LHS, RHS, CC, dl, LHS, RHS, Chain, IsSignaling); 9275 if (!RHS.getNode()) { 9276 RHS = DAG.getConstant(0, dl, LHS.getValueType()); 9277 CC = ISD::SETNE; 9278 } 9279 SDValue Result = DAG.getNode(ISD::SETCC, dl, VT, LHS, RHS, 9280 DAG.getCondCode(CC)); 9281 return DAG.getMergeValues({Result, Chain}, dl); 9282 } 9283 9284 ARMCC::CondCodes CondCode, CondCode2; 9285 FPCCToARMCC(CC, CondCode, CondCode2); 9286 9287 // FIXME: Chain is not handled correctly here. Currently the FPSCR is implicit 9288 // in CMPFP and CMPFPE, but instead it should be made explicit by these 9289 // instructions using a chain instead of glue. This would also fix the problem 9290 // here (and also in LowerSELECT_CC) where we generate two comparisons when 9291 // CondCode2 != AL. 9292 SDValue True = DAG.getConstant(1, dl, VT); 9293 SDValue False = DAG.getConstant(0, dl, VT); 9294 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 9295 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 9296 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl, IsSignaling); 9297 SDValue Result = getCMOV(dl, VT, False, True, ARMcc, CCR, Cmp, DAG); 9298 if (CondCode2 != ARMCC::AL) { 9299 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32); 9300 Cmp = getVFPCmp(LHS, RHS, DAG, dl, IsSignaling); 9301 Result = getCMOV(dl, VT, Result, True, ARMcc, CCR, Cmp, DAG); 9302 } 9303 return DAG.getMergeValues({Result, Chain}, dl); 9304 } 9305 9306 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 9307 LLVM_DEBUG(dbgs() << "Lowering node: "; Op.dump()); 9308 switch (Op.getOpcode()) { 9309 default: llvm_unreachable("Don't know how to custom lower this!"); 9310 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG); 9311 case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 9312 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); 9313 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); 9314 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); 9315 case ISD::SELECT: return LowerSELECT(Op, DAG); 9316 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 9317 case ISD::BRCOND: return LowerBRCOND(Op, DAG); 9318 case ISD::BR_CC: return LowerBR_CC(Op, DAG); 9319 case ISD::BR_JT: return LowerBR_JT(Op, DAG); 9320 case ISD::VASTART: return LowerVASTART(Op, DAG); 9321 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget); 9322 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget); 9323 case ISD::SINT_TO_FP: 9324 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG); 9325 case ISD::STRICT_FP_TO_SINT: 9326 case ISD::STRICT_FP_TO_UINT: 9327 case ISD::FP_TO_SINT: 9328 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG); 9329 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); 9330 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 9331 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 9332 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG); 9333 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG); 9334 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG); 9335 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG, Subtarget); 9336 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG, 9337 Subtarget); 9338 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG, Subtarget); 9339 case ISD::SHL: 9340 case ISD::SRL: 9341 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget); 9342 case ISD::SREM: return LowerREM(Op.getNode(), DAG); 9343 case ISD::UREM: return LowerREM(Op.getNode(), DAG); 9344 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG); 9345 case ISD::SRL_PARTS: 9346 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG); 9347 case ISD::CTTZ: 9348 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget); 9349 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget); 9350 case ISD::SETCC: return LowerVSETCC(Op, DAG, Subtarget); 9351 case ISD::SETCCCARRY: return LowerSETCCCARRY(Op, DAG); 9352 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget); 9353 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget); 9354 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG, Subtarget); 9355 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG, Subtarget); 9356 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); 9357 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG, Subtarget); 9358 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG, Subtarget); 9359 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); 9360 case ISD::MUL: return LowerMUL(Op, DAG); 9361 case ISD::SDIV: 9362 if (Subtarget->isTargetWindows() && !Op.getValueType().isVector()) 9363 return LowerDIV_Windows(Op, DAG, /* Signed */ true); 9364 return LowerSDIV(Op, DAG, Subtarget); 9365 case ISD::UDIV: 9366 if (Subtarget->isTargetWindows() && !Op.getValueType().isVector()) 9367 return LowerDIV_Windows(Op, DAG, /* Signed */ false); 9368 return LowerUDIV(Op, DAG, Subtarget); 9369 case ISD::ADDCARRY: 9370 case ISD::SUBCARRY: return LowerADDSUBCARRY(Op, DAG); 9371 case ISD::SADDO: 9372 case ISD::SSUBO: 9373 return LowerSignedALUO(Op, DAG); 9374 case ISD::UADDO: 9375 case ISD::USUBO: 9376 return LowerUnsignedALUO(Op, DAG); 9377 case ISD::SADDSAT: 9378 case ISD::SSUBSAT: 9379 return LowerSADDSUBSAT(Op, DAG, Subtarget); 9380 case ISD::LOAD: 9381 return LowerPredicateLoad(Op, DAG); 9382 case ISD::STORE: 9383 return LowerPredicateStore(Op, DAG); 9384 case ISD::MLOAD: 9385 return LowerMLOAD(Op, DAG); 9386 case ISD::ATOMIC_LOAD: 9387 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG); 9388 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG); 9389 case ISD::SDIVREM: 9390 case ISD::UDIVREM: return LowerDivRem(Op, DAG); 9391 case ISD::DYNAMIC_STACKALLOC: 9392 if (Subtarget->isTargetWindows()) 9393 return LowerDYNAMIC_STACKALLOC(Op, DAG); 9394 llvm_unreachable("Don't know how to custom lower this!"); 9395 case ISD::STRICT_FP_ROUND: 9396 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG); 9397 case ISD::STRICT_FP_EXTEND: 9398 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG); 9399 case ISD::STRICT_FSETCC: 9400 case ISD::STRICT_FSETCCS: return LowerFSETCC(Op, DAG); 9401 case ARMISD::WIN__DBZCHK: return SDValue(); 9402 } 9403 } 9404 9405 static void ReplaceLongIntrinsic(SDNode *N, SmallVectorImpl<SDValue> &Results, 9406 SelectionDAG &DAG) { 9407 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 9408 unsigned Opc = 0; 9409 if (IntNo == Intrinsic::arm_smlald) 9410 Opc = ARMISD::SMLALD; 9411 else if (IntNo == Intrinsic::arm_smlaldx) 9412 Opc = ARMISD::SMLALDX; 9413 else if (IntNo == Intrinsic::arm_smlsld) 9414 Opc = ARMISD::SMLSLD; 9415 else if (IntNo == Intrinsic::arm_smlsldx) 9416 Opc = ARMISD::SMLSLDX; 9417 else 9418 return; 9419 9420 SDLoc dl(N); 9421 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 9422 N->getOperand(3), 9423 DAG.getConstant(0, dl, MVT::i32)); 9424 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 9425 N->getOperand(3), 9426 DAG.getConstant(1, dl, MVT::i32)); 9427 9428 SDValue LongMul = DAG.getNode(Opc, dl, 9429 DAG.getVTList(MVT::i32, MVT::i32), 9430 N->getOperand(1), N->getOperand(2), 9431 Lo, Hi); 9432 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, 9433 LongMul.getValue(0), LongMul.getValue(1))); 9434 } 9435 9436 /// ReplaceNodeResults - Replace the results of node with an illegal result 9437 /// type with new values built out of custom code. 9438 void ARMTargetLowering::ReplaceNodeResults(SDNode *N, 9439 SmallVectorImpl<SDValue> &Results, 9440 SelectionDAG &DAG) const { 9441 SDValue Res; 9442 switch (N->getOpcode()) { 9443 default: 9444 llvm_unreachable("Don't know how to custom expand this!"); 9445 case ISD::READ_REGISTER: 9446 ExpandREAD_REGISTER(N, Results, DAG); 9447 break; 9448 case ISD::BITCAST: 9449 Res = ExpandBITCAST(N, DAG, Subtarget); 9450 break; 9451 case ISD::SRL: 9452 case ISD::SRA: 9453 case ISD::SHL: 9454 Res = Expand64BitShift(N, DAG, Subtarget); 9455 break; 9456 case ISD::SREM: 9457 case ISD::UREM: 9458 Res = LowerREM(N, DAG); 9459 break; 9460 case ISD::SDIVREM: 9461 case ISD::UDIVREM: 9462 Res = LowerDivRem(SDValue(N, 0), DAG); 9463 assert(Res.getNumOperands() == 2 && "DivRem needs two values"); 9464 Results.push_back(Res.getValue(0)); 9465 Results.push_back(Res.getValue(1)); 9466 return; 9467 case ISD::SADDSAT: 9468 case ISD::SSUBSAT: 9469 Res = LowerSADDSUBSAT(SDValue(N, 0), DAG, Subtarget); 9470 break; 9471 case ISD::READCYCLECOUNTER: 9472 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget); 9473 return; 9474 case ISD::UDIV: 9475 case ISD::SDIV: 9476 assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows"); 9477 return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV, 9478 Results); 9479 case ISD::ATOMIC_CMP_SWAP: 9480 ReplaceCMP_SWAP_64Results(N, Results, DAG); 9481 return; 9482 case ISD::INTRINSIC_WO_CHAIN: 9483 return ReplaceLongIntrinsic(N, Results, DAG); 9484 case ISD::ABS: 9485 lowerABS(N, Results, DAG); 9486 return ; 9487 9488 } 9489 if (Res.getNode()) 9490 Results.push_back(Res); 9491 } 9492 9493 //===----------------------------------------------------------------------===// 9494 // ARM Scheduler Hooks 9495 //===----------------------------------------------------------------------===// 9496 9497 /// SetupEntryBlockForSjLj - Insert code into the entry block that creates and 9498 /// registers the function context. 9499 void ARMTargetLowering::SetupEntryBlockForSjLj(MachineInstr &MI, 9500 MachineBasicBlock *MBB, 9501 MachineBasicBlock *DispatchBB, 9502 int FI) const { 9503 assert(!Subtarget->isROPI() && !Subtarget->isRWPI() && 9504 "ROPI/RWPI not currently supported with SjLj"); 9505 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 9506 DebugLoc dl = MI.getDebugLoc(); 9507 MachineFunction *MF = MBB->getParent(); 9508 MachineRegisterInfo *MRI = &MF->getRegInfo(); 9509 MachineConstantPool *MCP = MF->getConstantPool(); 9510 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>(); 9511 const Function &F = MF->getFunction(); 9512 9513 bool isThumb = Subtarget->isThumb(); 9514 bool isThumb2 = Subtarget->isThumb2(); 9515 9516 unsigned PCLabelId = AFI->createPICLabelUId(); 9517 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8; 9518 ARMConstantPoolValue *CPV = 9519 ARMConstantPoolMBB::Create(F.getContext(), DispatchBB, PCLabelId, PCAdj); 9520 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4); 9521 9522 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass 9523 : &ARM::GPRRegClass; 9524 9525 // Grab constant pool and fixed stack memory operands. 9526 MachineMemOperand *CPMMO = 9527 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF), 9528 MachineMemOperand::MOLoad, 4, 4); 9529 9530 MachineMemOperand *FIMMOSt = 9531 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI), 9532 MachineMemOperand::MOStore, 4, 4); 9533 9534 // Load the address of the dispatch MBB into the jump buffer. 9535 if (isThumb2) { 9536 // Incoming value: jbuf 9537 // ldr.n r5, LCPI1_1 9538 // orr r5, r5, #1 9539 // add r5, pc 9540 // str r5, [$jbuf, #+4] ; &jbuf[1] 9541 Register NewVReg1 = MRI->createVirtualRegister(TRC); 9542 BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1) 9543 .addConstantPoolIndex(CPI) 9544 .addMemOperand(CPMMO) 9545 .add(predOps(ARMCC::AL)); 9546 // Set the low bit because of thumb mode. 9547 Register NewVReg2 = MRI->createVirtualRegister(TRC); 9548 BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2) 9549 .addReg(NewVReg1, RegState::Kill) 9550 .addImm(0x01) 9551 .add(predOps(ARMCC::AL)) 9552 .add(condCodeOp()); 9553 Register NewVReg3 = MRI->createVirtualRegister(TRC); 9554 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3) 9555 .addReg(NewVReg2, RegState::Kill) 9556 .addImm(PCLabelId); 9557 BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12)) 9558 .addReg(NewVReg3, RegState::Kill) 9559 .addFrameIndex(FI) 9560 .addImm(36) // &jbuf[1] :: pc 9561 .addMemOperand(FIMMOSt) 9562 .add(predOps(ARMCC::AL)); 9563 } else if (isThumb) { 9564 // Incoming value: jbuf 9565 // ldr.n r1, LCPI1_4 9566 // add r1, pc 9567 // mov r2, #1 9568 // orrs r1, r2 9569 // add r2, $jbuf, #+4 ; &jbuf[1] 9570 // str r1, [r2] 9571 Register NewVReg1 = MRI->createVirtualRegister(TRC); 9572 BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1) 9573 .addConstantPoolIndex(CPI) 9574 .addMemOperand(CPMMO) 9575 .add(predOps(ARMCC::AL)); 9576 Register NewVReg2 = MRI->createVirtualRegister(TRC); 9577 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2) 9578 .addReg(NewVReg1, RegState::Kill) 9579 .addImm(PCLabelId); 9580 // Set the low bit because of thumb mode. 9581 Register NewVReg3 = MRI->createVirtualRegister(TRC); 9582 BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3) 9583 .addReg(ARM::CPSR, RegState::Define) 9584 .addImm(1) 9585 .add(predOps(ARMCC::AL)); 9586 Register NewVReg4 = MRI->createVirtualRegister(TRC); 9587 BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4) 9588 .addReg(ARM::CPSR, RegState::Define) 9589 .addReg(NewVReg2, RegState::Kill) 9590 .addReg(NewVReg3, RegState::Kill) 9591 .add(predOps(ARMCC::AL)); 9592 Register NewVReg5 = MRI->createVirtualRegister(TRC); 9593 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5) 9594 .addFrameIndex(FI) 9595 .addImm(36); // &jbuf[1] :: pc 9596 BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi)) 9597 .addReg(NewVReg4, RegState::Kill) 9598 .addReg(NewVReg5, RegState::Kill) 9599 .addImm(0) 9600 .addMemOperand(FIMMOSt) 9601 .add(predOps(ARMCC::AL)); 9602 } else { 9603 // Incoming value: jbuf 9604 // ldr r1, LCPI1_1 9605 // add r1, pc, r1 9606 // str r1, [$jbuf, #+4] ; &jbuf[1] 9607 Register NewVReg1 = MRI->createVirtualRegister(TRC); 9608 BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1) 9609 .addConstantPoolIndex(CPI) 9610 .addImm(0) 9611 .addMemOperand(CPMMO) 9612 .add(predOps(ARMCC::AL)); 9613 Register NewVReg2 = MRI->createVirtualRegister(TRC); 9614 BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2) 9615 .addReg(NewVReg1, RegState::Kill) 9616 .addImm(PCLabelId) 9617 .add(predOps(ARMCC::AL)); 9618 BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12)) 9619 .addReg(NewVReg2, RegState::Kill) 9620 .addFrameIndex(FI) 9621 .addImm(36) // &jbuf[1] :: pc 9622 .addMemOperand(FIMMOSt) 9623 .add(predOps(ARMCC::AL)); 9624 } 9625 } 9626 9627 void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr &MI, 9628 MachineBasicBlock *MBB) const { 9629 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 9630 DebugLoc dl = MI.getDebugLoc(); 9631 MachineFunction *MF = MBB->getParent(); 9632 MachineRegisterInfo *MRI = &MF->getRegInfo(); 9633 MachineFrameInfo &MFI = MF->getFrameInfo(); 9634 int FI = MFI.getFunctionContextIndex(); 9635 9636 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass 9637 : &ARM::GPRnopcRegClass; 9638 9639 // Get a mapping of the call site numbers to all of the landing pads they're 9640 // associated with. 9641 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2>> CallSiteNumToLPad; 9642 unsigned MaxCSNum = 0; 9643 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; 9644 ++BB) { 9645 if (!BB->isEHPad()) continue; 9646 9647 // FIXME: We should assert that the EH_LABEL is the first MI in the landing 9648 // pad. 9649 for (MachineBasicBlock::iterator 9650 II = BB->begin(), IE = BB->end(); II != IE; ++II) { 9651 if (!II->isEHLabel()) continue; 9652 9653 MCSymbol *Sym = II->getOperand(0).getMCSymbol(); 9654 if (!MF->hasCallSiteLandingPad(Sym)) continue; 9655 9656 SmallVectorImpl<unsigned> &CallSiteIdxs = MF->getCallSiteLandingPad(Sym); 9657 for (SmallVectorImpl<unsigned>::iterator 9658 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end(); 9659 CSI != CSE; ++CSI) { 9660 CallSiteNumToLPad[*CSI].push_back(&*BB); 9661 MaxCSNum = std::max(MaxCSNum, *CSI); 9662 } 9663 break; 9664 } 9665 } 9666 9667 // Get an ordered list of the machine basic blocks for the jump table. 9668 std::vector<MachineBasicBlock*> LPadList; 9669 SmallPtrSet<MachineBasicBlock*, 32> InvokeBBs; 9670 LPadList.reserve(CallSiteNumToLPad.size()); 9671 for (unsigned I = 1; I <= MaxCSNum; ++I) { 9672 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I]; 9673 for (SmallVectorImpl<MachineBasicBlock*>::iterator 9674 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) { 9675 LPadList.push_back(*II); 9676 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end()); 9677 } 9678 } 9679 9680 assert(!LPadList.empty() && 9681 "No landing pad destinations for the dispatch jump table!"); 9682 9683 // Create the jump table and associated information. 9684 MachineJumpTableInfo *JTI = 9685 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline); 9686 unsigned MJTI = JTI->createJumpTableIndex(LPadList); 9687 9688 // Create the MBBs for the dispatch code. 9689 9690 // Shove the dispatch's address into the return slot in the function context. 9691 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock(); 9692 DispatchBB->setIsEHPad(); 9693 9694 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock(); 9695 unsigned trap_opcode; 9696 if (Subtarget->isThumb()) 9697 trap_opcode = ARM::tTRAP; 9698 else 9699 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP; 9700 9701 BuildMI(TrapBB, dl, TII->get(trap_opcode)); 9702 DispatchBB->addSuccessor(TrapBB); 9703 9704 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock(); 9705 DispatchBB->addSuccessor(DispContBB); 9706 9707 // Insert and MBBs. 9708 MF->insert(MF->end(), DispatchBB); 9709 MF->insert(MF->end(), DispContBB); 9710 MF->insert(MF->end(), TrapBB); 9711 9712 // Insert code into the entry block that creates and registers the function 9713 // context. 9714 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI); 9715 9716 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand( 9717 MachinePointerInfo::getFixedStack(*MF, FI), 9718 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4); 9719 9720 MachineInstrBuilder MIB; 9721 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup)); 9722 9723 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII); 9724 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo(); 9725 9726 // Add a register mask with no preserved registers. This results in all 9727 // registers being marked as clobbered. This can't work if the dispatch block 9728 // is in a Thumb1 function and is linked with ARM code which uses the FP 9729 // registers, as there is no way to preserve the FP registers in Thumb1 mode. 9730 MIB.addRegMask(RI.getSjLjDispatchPreservedMask(*MF)); 9731 9732 bool IsPositionIndependent = isPositionIndependent(); 9733 unsigned NumLPads = LPadList.size(); 9734 if (Subtarget->isThumb2()) { 9735 Register NewVReg1 = MRI->createVirtualRegister(TRC); 9736 BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1) 9737 .addFrameIndex(FI) 9738 .addImm(4) 9739 .addMemOperand(FIMMOLd) 9740 .add(predOps(ARMCC::AL)); 9741 9742 if (NumLPads < 256) { 9743 BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri)) 9744 .addReg(NewVReg1) 9745 .addImm(LPadList.size()) 9746 .add(predOps(ARMCC::AL)); 9747 } else { 9748 Register VReg1 = MRI->createVirtualRegister(TRC); 9749 BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1) 9750 .addImm(NumLPads & 0xFFFF) 9751 .add(predOps(ARMCC::AL)); 9752 9753 unsigned VReg2 = VReg1; 9754 if ((NumLPads & 0xFFFF0000) != 0) { 9755 VReg2 = MRI->createVirtualRegister(TRC); 9756 BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2) 9757 .addReg(VReg1) 9758 .addImm(NumLPads >> 16) 9759 .add(predOps(ARMCC::AL)); 9760 } 9761 9762 BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr)) 9763 .addReg(NewVReg1) 9764 .addReg(VReg2) 9765 .add(predOps(ARMCC::AL)); 9766 } 9767 9768 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc)) 9769 .addMBB(TrapBB) 9770 .addImm(ARMCC::HI) 9771 .addReg(ARM::CPSR); 9772 9773 Register NewVReg3 = MRI->createVirtualRegister(TRC); 9774 BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT), NewVReg3) 9775 .addJumpTableIndex(MJTI) 9776 .add(predOps(ARMCC::AL)); 9777 9778 Register NewVReg4 = MRI->createVirtualRegister(TRC); 9779 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4) 9780 .addReg(NewVReg3, RegState::Kill) 9781 .addReg(NewVReg1) 9782 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)) 9783 .add(predOps(ARMCC::AL)) 9784 .add(condCodeOp()); 9785 9786 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT)) 9787 .addReg(NewVReg4, RegState::Kill) 9788 .addReg(NewVReg1) 9789 .addJumpTableIndex(MJTI); 9790 } else if (Subtarget->isThumb()) { 9791 Register NewVReg1 = MRI->createVirtualRegister(TRC); 9792 BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1) 9793 .addFrameIndex(FI) 9794 .addImm(1) 9795 .addMemOperand(FIMMOLd) 9796 .add(predOps(ARMCC::AL)); 9797 9798 if (NumLPads < 256) { 9799 BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8)) 9800 .addReg(NewVReg1) 9801 .addImm(NumLPads) 9802 .add(predOps(ARMCC::AL)); 9803 } else { 9804 MachineConstantPool *ConstantPool = MF->getConstantPool(); 9805 Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext()); 9806 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 9807 9808 // MachineConstantPool wants an explicit alignment. 9809 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty); 9810 if (Align == 0) 9811 Align = MF->getDataLayout().getTypeAllocSize(C->getType()); 9812 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 9813 9814 Register VReg1 = MRI->createVirtualRegister(TRC); 9815 BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci)) 9816 .addReg(VReg1, RegState::Define) 9817 .addConstantPoolIndex(Idx) 9818 .add(predOps(ARMCC::AL)); 9819 BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr)) 9820 .addReg(NewVReg1) 9821 .addReg(VReg1) 9822 .add(predOps(ARMCC::AL)); 9823 } 9824 9825 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc)) 9826 .addMBB(TrapBB) 9827 .addImm(ARMCC::HI) 9828 .addReg(ARM::CPSR); 9829 9830 Register NewVReg2 = MRI->createVirtualRegister(TRC); 9831 BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2) 9832 .addReg(ARM::CPSR, RegState::Define) 9833 .addReg(NewVReg1) 9834 .addImm(2) 9835 .add(predOps(ARMCC::AL)); 9836 9837 Register NewVReg3 = MRI->createVirtualRegister(TRC); 9838 BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3) 9839 .addJumpTableIndex(MJTI) 9840 .add(predOps(ARMCC::AL)); 9841 9842 Register NewVReg4 = MRI->createVirtualRegister(TRC); 9843 BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4) 9844 .addReg(ARM::CPSR, RegState::Define) 9845 .addReg(NewVReg2, RegState::Kill) 9846 .addReg(NewVReg3) 9847 .add(predOps(ARMCC::AL)); 9848 9849 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand( 9850 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4); 9851 9852 Register NewVReg5 = MRI->createVirtualRegister(TRC); 9853 BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5) 9854 .addReg(NewVReg4, RegState::Kill) 9855 .addImm(0) 9856 .addMemOperand(JTMMOLd) 9857 .add(predOps(ARMCC::AL)); 9858 9859 unsigned NewVReg6 = NewVReg5; 9860 if (IsPositionIndependent) { 9861 NewVReg6 = MRI->createVirtualRegister(TRC); 9862 BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6) 9863 .addReg(ARM::CPSR, RegState::Define) 9864 .addReg(NewVReg5, RegState::Kill) 9865 .addReg(NewVReg3) 9866 .add(predOps(ARMCC::AL)); 9867 } 9868 9869 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr)) 9870 .addReg(NewVReg6, RegState::Kill) 9871 .addJumpTableIndex(MJTI); 9872 } else { 9873 Register NewVReg1 = MRI->createVirtualRegister(TRC); 9874 BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1) 9875 .addFrameIndex(FI) 9876 .addImm(4) 9877 .addMemOperand(FIMMOLd) 9878 .add(predOps(ARMCC::AL)); 9879 9880 if (NumLPads < 256) { 9881 BuildMI(DispatchBB, dl, TII->get(ARM::CMPri)) 9882 .addReg(NewVReg1) 9883 .addImm(NumLPads) 9884 .add(predOps(ARMCC::AL)); 9885 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) { 9886 Register VReg1 = MRI->createVirtualRegister(TRC); 9887 BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1) 9888 .addImm(NumLPads & 0xFFFF) 9889 .add(predOps(ARMCC::AL)); 9890 9891 unsigned VReg2 = VReg1; 9892 if ((NumLPads & 0xFFFF0000) != 0) { 9893 VReg2 = MRI->createVirtualRegister(TRC); 9894 BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2) 9895 .addReg(VReg1) 9896 .addImm(NumLPads >> 16) 9897 .add(predOps(ARMCC::AL)); 9898 } 9899 9900 BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 9901 .addReg(NewVReg1) 9902 .addReg(VReg2) 9903 .add(predOps(ARMCC::AL)); 9904 } else { 9905 MachineConstantPool *ConstantPool = MF->getConstantPool(); 9906 Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext()); 9907 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 9908 9909 // MachineConstantPool wants an explicit alignment. 9910 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty); 9911 if (Align == 0) 9912 Align = MF->getDataLayout().getTypeAllocSize(C->getType()); 9913 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 9914 9915 Register VReg1 = MRI->createVirtualRegister(TRC); 9916 BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp)) 9917 .addReg(VReg1, RegState::Define) 9918 .addConstantPoolIndex(Idx) 9919 .addImm(0) 9920 .add(predOps(ARMCC::AL)); 9921 BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 9922 .addReg(NewVReg1) 9923 .addReg(VReg1, RegState::Kill) 9924 .add(predOps(ARMCC::AL)); 9925 } 9926 9927 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc)) 9928 .addMBB(TrapBB) 9929 .addImm(ARMCC::HI) 9930 .addReg(ARM::CPSR); 9931 9932 Register NewVReg3 = MRI->createVirtualRegister(TRC); 9933 BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3) 9934 .addReg(NewVReg1) 9935 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)) 9936 .add(predOps(ARMCC::AL)) 9937 .add(condCodeOp()); 9938 Register NewVReg4 = MRI->createVirtualRegister(TRC); 9939 BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4) 9940 .addJumpTableIndex(MJTI) 9941 .add(predOps(ARMCC::AL)); 9942 9943 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand( 9944 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4); 9945 Register NewVReg5 = MRI->createVirtualRegister(TRC); 9946 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5) 9947 .addReg(NewVReg3, RegState::Kill) 9948 .addReg(NewVReg4) 9949 .addImm(0) 9950 .addMemOperand(JTMMOLd) 9951 .add(predOps(ARMCC::AL)); 9952 9953 if (IsPositionIndependent) { 9954 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd)) 9955 .addReg(NewVReg5, RegState::Kill) 9956 .addReg(NewVReg4) 9957 .addJumpTableIndex(MJTI); 9958 } else { 9959 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr)) 9960 .addReg(NewVReg5, RegState::Kill) 9961 .addJumpTableIndex(MJTI); 9962 } 9963 } 9964 9965 // Add the jump table entries as successors to the MBB. 9966 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs; 9967 for (std::vector<MachineBasicBlock*>::iterator 9968 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) { 9969 MachineBasicBlock *CurMBB = *I; 9970 if (SeenMBBs.insert(CurMBB).second) 9971 DispContBB->addSuccessor(CurMBB); 9972 } 9973 9974 // N.B. the order the invoke BBs are processed in doesn't matter here. 9975 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF); 9976 SmallVector<MachineBasicBlock*, 64> MBBLPads; 9977 for (MachineBasicBlock *BB : InvokeBBs) { 9978 9979 // Remove the landing pad successor from the invoke block and replace it 9980 // with the new dispatch block. 9981 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(), 9982 BB->succ_end()); 9983 while (!Successors.empty()) { 9984 MachineBasicBlock *SMBB = Successors.pop_back_val(); 9985 if (SMBB->isEHPad()) { 9986 BB->removeSuccessor(SMBB); 9987 MBBLPads.push_back(SMBB); 9988 } 9989 } 9990 9991 BB->addSuccessor(DispatchBB, BranchProbability::getZero()); 9992 BB->normalizeSuccProbs(); 9993 9994 // Find the invoke call and mark all of the callee-saved registers as 9995 // 'implicit defined' so that they're spilled. This prevents code from 9996 // moving instructions to before the EH block, where they will never be 9997 // executed. 9998 for (MachineBasicBlock::reverse_iterator 9999 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) { 10000 if (!II->isCall()) continue; 10001 10002 DenseMap<unsigned, bool> DefRegs; 10003 for (MachineInstr::mop_iterator 10004 OI = II->operands_begin(), OE = II->operands_end(); 10005 OI != OE; ++OI) { 10006 if (!OI->isReg()) continue; 10007 DefRegs[OI->getReg()] = true; 10008 } 10009 10010 MachineInstrBuilder MIB(*MF, &*II); 10011 10012 for (unsigned i = 0; SavedRegs[i] != 0; ++i) { 10013 unsigned Reg = SavedRegs[i]; 10014 if (Subtarget->isThumb2() && 10015 !ARM::tGPRRegClass.contains(Reg) && 10016 !ARM::hGPRRegClass.contains(Reg)) 10017 continue; 10018 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg)) 10019 continue; 10020 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg)) 10021 continue; 10022 if (!DefRegs[Reg]) 10023 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead); 10024 } 10025 10026 break; 10027 } 10028 } 10029 10030 // Mark all former landing pads as non-landing pads. The dispatch is the only 10031 // landing pad now. 10032 for (SmallVectorImpl<MachineBasicBlock*>::iterator 10033 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I) 10034 (*I)->setIsEHPad(false); 10035 10036 // The instruction is gone now. 10037 MI.eraseFromParent(); 10038 } 10039 10040 static 10041 MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) { 10042 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), 10043 E = MBB->succ_end(); I != E; ++I) 10044 if (*I != Succ) 10045 return *I; 10046 llvm_unreachable("Expecting a BB with two successors!"); 10047 } 10048 10049 /// Return the load opcode for a given load size. If load size >= 8, 10050 /// neon opcode will be returned. 10051 static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) { 10052 if (LdSize >= 8) 10053 return LdSize == 16 ? ARM::VLD1q32wb_fixed 10054 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0; 10055 if (IsThumb1) 10056 return LdSize == 4 ? ARM::tLDRi 10057 : LdSize == 2 ? ARM::tLDRHi 10058 : LdSize == 1 ? ARM::tLDRBi : 0; 10059 if (IsThumb2) 10060 return LdSize == 4 ? ARM::t2LDR_POST 10061 : LdSize == 2 ? ARM::t2LDRH_POST 10062 : LdSize == 1 ? ARM::t2LDRB_POST : 0; 10063 return LdSize == 4 ? ARM::LDR_POST_IMM 10064 : LdSize == 2 ? ARM::LDRH_POST 10065 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0; 10066 } 10067 10068 /// Return the store opcode for a given store size. If store size >= 8, 10069 /// neon opcode will be returned. 10070 static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) { 10071 if (StSize >= 8) 10072 return StSize == 16 ? ARM::VST1q32wb_fixed 10073 : StSize == 8 ? ARM::VST1d32wb_fixed : 0; 10074 if (IsThumb1) 10075 return StSize == 4 ? ARM::tSTRi 10076 : StSize == 2 ? ARM::tSTRHi 10077 : StSize == 1 ? ARM::tSTRBi : 0; 10078 if (IsThumb2) 10079 return StSize == 4 ? ARM::t2STR_POST 10080 : StSize == 2 ? ARM::t2STRH_POST 10081 : StSize == 1 ? ARM::t2STRB_POST : 0; 10082 return StSize == 4 ? ARM::STR_POST_IMM 10083 : StSize == 2 ? ARM::STRH_POST 10084 : StSize == 1 ? ARM::STRB_POST_IMM : 0; 10085 } 10086 10087 /// Emit a post-increment load operation with given size. The instructions 10088 /// will be added to BB at Pos. 10089 static void emitPostLd(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos, 10090 const TargetInstrInfo *TII, const DebugLoc &dl, 10091 unsigned LdSize, unsigned Data, unsigned AddrIn, 10092 unsigned AddrOut, bool IsThumb1, bool IsThumb2) { 10093 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2); 10094 assert(LdOpc != 0 && "Should have a load opcode"); 10095 if (LdSize >= 8) { 10096 BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 10097 .addReg(AddrOut, RegState::Define) 10098 .addReg(AddrIn) 10099 .addImm(0) 10100 .add(predOps(ARMCC::AL)); 10101 } else if (IsThumb1) { 10102 // load + update AddrIn 10103 BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 10104 .addReg(AddrIn) 10105 .addImm(0) 10106 .add(predOps(ARMCC::AL)); 10107 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut) 10108 .add(t1CondCodeOp()) 10109 .addReg(AddrIn) 10110 .addImm(LdSize) 10111 .add(predOps(ARMCC::AL)); 10112 } else if (IsThumb2) { 10113 BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 10114 .addReg(AddrOut, RegState::Define) 10115 .addReg(AddrIn) 10116 .addImm(LdSize) 10117 .add(predOps(ARMCC::AL)); 10118 } else { // arm 10119 BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 10120 .addReg(AddrOut, RegState::Define) 10121 .addReg(AddrIn) 10122 .addReg(0) 10123 .addImm(LdSize) 10124 .add(predOps(ARMCC::AL)); 10125 } 10126 } 10127 10128 /// Emit a post-increment store operation with given size. The instructions 10129 /// will be added to BB at Pos. 10130 static void emitPostSt(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos, 10131 const TargetInstrInfo *TII, const DebugLoc &dl, 10132 unsigned StSize, unsigned Data, unsigned AddrIn, 10133 unsigned AddrOut, bool IsThumb1, bool IsThumb2) { 10134 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2); 10135 assert(StOpc != 0 && "Should have a store opcode"); 10136 if (StSize >= 8) { 10137 BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 10138 .addReg(AddrIn) 10139 .addImm(0) 10140 .addReg(Data) 10141 .add(predOps(ARMCC::AL)); 10142 } else if (IsThumb1) { 10143 // store + update AddrIn 10144 BuildMI(*BB, Pos, dl, TII->get(StOpc)) 10145 .addReg(Data) 10146 .addReg(AddrIn) 10147 .addImm(0) 10148 .add(predOps(ARMCC::AL)); 10149 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut) 10150 .add(t1CondCodeOp()) 10151 .addReg(AddrIn) 10152 .addImm(StSize) 10153 .add(predOps(ARMCC::AL)); 10154 } else if (IsThumb2) { 10155 BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 10156 .addReg(Data) 10157 .addReg(AddrIn) 10158 .addImm(StSize) 10159 .add(predOps(ARMCC::AL)); 10160 } else { // arm 10161 BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 10162 .addReg(Data) 10163 .addReg(AddrIn) 10164 .addReg(0) 10165 .addImm(StSize) 10166 .add(predOps(ARMCC::AL)); 10167 } 10168 } 10169 10170 MachineBasicBlock * 10171 ARMTargetLowering::EmitStructByval(MachineInstr &MI, 10172 MachineBasicBlock *BB) const { 10173 // This pseudo instruction has 3 operands: dst, src, size 10174 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold(). 10175 // Otherwise, we will generate unrolled scalar copies. 10176 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 10177 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 10178 MachineFunction::iterator It = ++BB->getIterator(); 10179 10180 Register dest = MI.getOperand(0).getReg(); 10181 Register src = MI.getOperand(1).getReg(); 10182 unsigned SizeVal = MI.getOperand(2).getImm(); 10183 unsigned Align = MI.getOperand(3).getImm(); 10184 DebugLoc dl = MI.getDebugLoc(); 10185 10186 MachineFunction *MF = BB->getParent(); 10187 MachineRegisterInfo &MRI = MF->getRegInfo(); 10188 unsigned UnitSize = 0; 10189 const TargetRegisterClass *TRC = nullptr; 10190 const TargetRegisterClass *VecTRC = nullptr; 10191 10192 bool IsThumb1 = Subtarget->isThumb1Only(); 10193 bool IsThumb2 = Subtarget->isThumb2(); 10194 bool IsThumb = Subtarget->isThumb(); 10195 10196 if (Align & 1) { 10197 UnitSize = 1; 10198 } else if (Align & 2) { 10199 UnitSize = 2; 10200 } else { 10201 // Check whether we can use NEON instructions. 10202 if (!MF->getFunction().hasFnAttribute(Attribute::NoImplicitFloat) && 10203 Subtarget->hasNEON()) { 10204 if ((Align % 16 == 0) && SizeVal >= 16) 10205 UnitSize = 16; 10206 else if ((Align % 8 == 0) && SizeVal >= 8) 10207 UnitSize = 8; 10208 } 10209 // Can't use NEON instructions. 10210 if (UnitSize == 0) 10211 UnitSize = 4; 10212 } 10213 10214 // Select the correct opcode and register class for unit size load/store 10215 bool IsNeon = UnitSize >= 8; 10216 TRC = IsThumb ? &ARM::tGPRRegClass : &ARM::GPRRegClass; 10217 if (IsNeon) 10218 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass 10219 : UnitSize == 8 ? &ARM::DPRRegClass 10220 : nullptr; 10221 10222 unsigned BytesLeft = SizeVal % UnitSize; 10223 unsigned LoopSize = SizeVal - BytesLeft; 10224 10225 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) { 10226 // Use LDR and STR to copy. 10227 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize) 10228 // [destOut] = STR_POST(scratch, destIn, UnitSize) 10229 unsigned srcIn = src; 10230 unsigned destIn = dest; 10231 for (unsigned i = 0; i < LoopSize; i+=UnitSize) { 10232 Register srcOut = MRI.createVirtualRegister(TRC); 10233 Register destOut = MRI.createVirtualRegister(TRC); 10234 Register scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC); 10235 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut, 10236 IsThumb1, IsThumb2); 10237 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut, 10238 IsThumb1, IsThumb2); 10239 srcIn = srcOut; 10240 destIn = destOut; 10241 } 10242 10243 // Handle the leftover bytes with LDRB and STRB. 10244 // [scratch, srcOut] = LDRB_POST(srcIn, 1) 10245 // [destOut] = STRB_POST(scratch, destIn, 1) 10246 for (unsigned i = 0; i < BytesLeft; i++) { 10247 Register srcOut = MRI.createVirtualRegister(TRC); 10248 Register destOut = MRI.createVirtualRegister(TRC); 10249 Register scratch = MRI.createVirtualRegister(TRC); 10250 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut, 10251 IsThumb1, IsThumb2); 10252 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut, 10253 IsThumb1, IsThumb2); 10254 srcIn = srcOut; 10255 destIn = destOut; 10256 } 10257 MI.eraseFromParent(); // The instruction is gone now. 10258 return BB; 10259 } 10260 10261 // Expand the pseudo op to a loop. 10262 // thisMBB: 10263 // ... 10264 // movw varEnd, # --> with thumb2 10265 // movt varEnd, # 10266 // ldrcp varEnd, idx --> without thumb2 10267 // fallthrough --> loopMBB 10268 // loopMBB: 10269 // PHI varPhi, varEnd, varLoop 10270 // PHI srcPhi, src, srcLoop 10271 // PHI destPhi, dst, destLoop 10272 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 10273 // [destLoop] = STR_POST(scratch, destPhi, UnitSize) 10274 // subs varLoop, varPhi, #UnitSize 10275 // bne loopMBB 10276 // fallthrough --> exitMBB 10277 // exitMBB: 10278 // epilogue to handle left-over bytes 10279 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 10280 // [destOut] = STRB_POST(scratch, destLoop, 1) 10281 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 10282 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 10283 MF->insert(It, loopMBB); 10284 MF->insert(It, exitMBB); 10285 10286 // Transfer the remainder of BB and its successor edges to exitMBB. 10287 exitMBB->splice(exitMBB->begin(), BB, 10288 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 10289 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 10290 10291 // Load an immediate to varEnd. 10292 Register varEnd = MRI.createVirtualRegister(TRC); 10293 if (Subtarget->useMovt()) { 10294 unsigned Vtmp = varEnd; 10295 if ((LoopSize & 0xFFFF0000) != 0) 10296 Vtmp = MRI.createVirtualRegister(TRC); 10297 BuildMI(BB, dl, TII->get(IsThumb ? ARM::t2MOVi16 : ARM::MOVi16), Vtmp) 10298 .addImm(LoopSize & 0xFFFF) 10299 .add(predOps(ARMCC::AL)); 10300 10301 if ((LoopSize & 0xFFFF0000) != 0) 10302 BuildMI(BB, dl, TII->get(IsThumb ? ARM::t2MOVTi16 : ARM::MOVTi16), varEnd) 10303 .addReg(Vtmp) 10304 .addImm(LoopSize >> 16) 10305 .add(predOps(ARMCC::AL)); 10306 } else { 10307 MachineConstantPool *ConstantPool = MF->getConstantPool(); 10308 Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext()); 10309 const Constant *C = ConstantInt::get(Int32Ty, LoopSize); 10310 10311 // MachineConstantPool wants an explicit alignment. 10312 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty); 10313 if (Align == 0) 10314 Align = MF->getDataLayout().getTypeAllocSize(C->getType()); 10315 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 10316 MachineMemOperand *CPMMO = 10317 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF), 10318 MachineMemOperand::MOLoad, 4, 4); 10319 10320 if (IsThumb) 10321 BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)) 10322 .addReg(varEnd, RegState::Define) 10323 .addConstantPoolIndex(Idx) 10324 .add(predOps(ARMCC::AL)) 10325 .addMemOperand(CPMMO); 10326 else 10327 BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)) 10328 .addReg(varEnd, RegState::Define) 10329 .addConstantPoolIndex(Idx) 10330 .addImm(0) 10331 .add(predOps(ARMCC::AL)) 10332 .addMemOperand(CPMMO); 10333 } 10334 BB->addSuccessor(loopMBB); 10335 10336 // Generate the loop body: 10337 // varPhi = PHI(varLoop, varEnd) 10338 // srcPhi = PHI(srcLoop, src) 10339 // destPhi = PHI(destLoop, dst) 10340 MachineBasicBlock *entryBB = BB; 10341 BB = loopMBB; 10342 Register varLoop = MRI.createVirtualRegister(TRC); 10343 Register varPhi = MRI.createVirtualRegister(TRC); 10344 Register srcLoop = MRI.createVirtualRegister(TRC); 10345 Register srcPhi = MRI.createVirtualRegister(TRC); 10346 Register destLoop = MRI.createVirtualRegister(TRC); 10347 Register destPhi = MRI.createVirtualRegister(TRC); 10348 10349 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi) 10350 .addReg(varLoop).addMBB(loopMBB) 10351 .addReg(varEnd).addMBB(entryBB); 10352 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi) 10353 .addReg(srcLoop).addMBB(loopMBB) 10354 .addReg(src).addMBB(entryBB); 10355 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi) 10356 .addReg(destLoop).addMBB(loopMBB) 10357 .addReg(dest).addMBB(entryBB); 10358 10359 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 10360 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz) 10361 Register scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC); 10362 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop, 10363 IsThumb1, IsThumb2); 10364 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop, 10365 IsThumb1, IsThumb2); 10366 10367 // Decrement loop variable by UnitSize. 10368 if (IsThumb1) { 10369 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop) 10370 .add(t1CondCodeOp()) 10371 .addReg(varPhi) 10372 .addImm(UnitSize) 10373 .add(predOps(ARMCC::AL)); 10374 } else { 10375 MachineInstrBuilder MIB = 10376 BuildMI(*BB, BB->end(), dl, 10377 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop); 10378 MIB.addReg(varPhi) 10379 .addImm(UnitSize) 10380 .add(predOps(ARMCC::AL)) 10381 .add(condCodeOp()); 10382 MIB->getOperand(5).setReg(ARM::CPSR); 10383 MIB->getOperand(5).setIsDef(true); 10384 } 10385 BuildMI(*BB, BB->end(), dl, 10386 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc)) 10387 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 10388 10389 // loopMBB can loop back to loopMBB or fall through to exitMBB. 10390 BB->addSuccessor(loopMBB); 10391 BB->addSuccessor(exitMBB); 10392 10393 // Add epilogue to handle BytesLeft. 10394 BB = exitMBB; 10395 auto StartOfExit = exitMBB->begin(); 10396 10397 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 10398 // [destOut] = STRB_POST(scratch, destLoop, 1) 10399 unsigned srcIn = srcLoop; 10400 unsigned destIn = destLoop; 10401 for (unsigned i = 0; i < BytesLeft; i++) { 10402 Register srcOut = MRI.createVirtualRegister(TRC); 10403 Register destOut = MRI.createVirtualRegister(TRC); 10404 Register scratch = MRI.createVirtualRegister(TRC); 10405 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut, 10406 IsThumb1, IsThumb2); 10407 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut, 10408 IsThumb1, IsThumb2); 10409 srcIn = srcOut; 10410 destIn = destOut; 10411 } 10412 10413 MI.eraseFromParent(); // The instruction is gone now. 10414 return BB; 10415 } 10416 10417 MachineBasicBlock * 10418 ARMTargetLowering::EmitLowered__chkstk(MachineInstr &MI, 10419 MachineBasicBlock *MBB) const { 10420 const TargetMachine &TM = getTargetMachine(); 10421 const TargetInstrInfo &TII = *Subtarget->getInstrInfo(); 10422 DebugLoc DL = MI.getDebugLoc(); 10423 10424 assert(Subtarget->isTargetWindows() && 10425 "__chkstk is only supported on Windows"); 10426 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode"); 10427 10428 // __chkstk takes the number of words to allocate on the stack in R4, and 10429 // returns the stack adjustment in number of bytes in R4. This will not 10430 // clober any other registers (other than the obvious lr). 10431 // 10432 // Although, technically, IP should be considered a register which may be 10433 // clobbered, the call itself will not touch it. Windows on ARM is a pure 10434 // thumb-2 environment, so there is no interworking required. As a result, we 10435 // do not expect a veneer to be emitted by the linker, clobbering IP. 10436 // 10437 // Each module receives its own copy of __chkstk, so no import thunk is 10438 // required, again, ensuring that IP is not clobbered. 10439 // 10440 // Finally, although some linkers may theoretically provide a trampoline for 10441 // out of range calls (which is quite common due to a 32M range limitation of 10442 // branches for Thumb), we can generate the long-call version via 10443 // -mcmodel=large, alleviating the need for the trampoline which may clobber 10444 // IP. 10445 10446 switch (TM.getCodeModel()) { 10447 case CodeModel::Tiny: 10448 llvm_unreachable("Tiny code model not available on ARM."); 10449 case CodeModel::Small: 10450 case CodeModel::Medium: 10451 case CodeModel::Kernel: 10452 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL)) 10453 .add(predOps(ARMCC::AL)) 10454 .addExternalSymbol("__chkstk") 10455 .addReg(ARM::R4, RegState::Implicit | RegState::Kill) 10456 .addReg(ARM::R4, RegState::Implicit | RegState::Define) 10457 .addReg(ARM::R12, 10458 RegState::Implicit | RegState::Define | RegState::Dead) 10459 .addReg(ARM::CPSR, 10460 RegState::Implicit | RegState::Define | RegState::Dead); 10461 break; 10462 case CodeModel::Large: { 10463 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 10464 Register Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass); 10465 10466 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg) 10467 .addExternalSymbol("__chkstk"); 10468 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr)) 10469 .add(predOps(ARMCC::AL)) 10470 .addReg(Reg, RegState::Kill) 10471 .addReg(ARM::R4, RegState::Implicit | RegState::Kill) 10472 .addReg(ARM::R4, RegState::Implicit | RegState::Define) 10473 .addReg(ARM::R12, 10474 RegState::Implicit | RegState::Define | RegState::Dead) 10475 .addReg(ARM::CPSR, 10476 RegState::Implicit | RegState::Define | RegState::Dead); 10477 break; 10478 } 10479 } 10480 10481 BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr), ARM::SP) 10482 .addReg(ARM::SP, RegState::Kill) 10483 .addReg(ARM::R4, RegState::Kill) 10484 .setMIFlags(MachineInstr::FrameSetup) 10485 .add(predOps(ARMCC::AL)) 10486 .add(condCodeOp()); 10487 10488 MI.eraseFromParent(); 10489 return MBB; 10490 } 10491 10492 MachineBasicBlock * 10493 ARMTargetLowering::EmitLowered__dbzchk(MachineInstr &MI, 10494 MachineBasicBlock *MBB) const { 10495 DebugLoc DL = MI.getDebugLoc(); 10496 MachineFunction *MF = MBB->getParent(); 10497 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 10498 10499 MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock(); 10500 MF->insert(++MBB->getIterator(), ContBB); 10501 ContBB->splice(ContBB->begin(), MBB, 10502 std::next(MachineBasicBlock::iterator(MI)), MBB->end()); 10503 ContBB->transferSuccessorsAndUpdatePHIs(MBB); 10504 MBB->addSuccessor(ContBB); 10505 10506 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock(); 10507 BuildMI(TrapBB, DL, TII->get(ARM::t__brkdiv0)); 10508 MF->push_back(TrapBB); 10509 MBB->addSuccessor(TrapBB); 10510 10511 BuildMI(*MBB, MI, DL, TII->get(ARM::tCMPi8)) 10512 .addReg(MI.getOperand(0).getReg()) 10513 .addImm(0) 10514 .add(predOps(ARMCC::AL)); 10515 BuildMI(*MBB, MI, DL, TII->get(ARM::t2Bcc)) 10516 .addMBB(TrapBB) 10517 .addImm(ARMCC::EQ) 10518 .addReg(ARM::CPSR); 10519 10520 MI.eraseFromParent(); 10521 return ContBB; 10522 } 10523 10524 // The CPSR operand of SelectItr might be missing a kill marker 10525 // because there were multiple uses of CPSR, and ISel didn't know 10526 // which to mark. Figure out whether SelectItr should have had a 10527 // kill marker, and set it if it should. Returns the correct kill 10528 // marker value. 10529 static bool checkAndUpdateCPSRKill(MachineBasicBlock::iterator SelectItr, 10530 MachineBasicBlock* BB, 10531 const TargetRegisterInfo* TRI) { 10532 // Scan forward through BB for a use/def of CPSR. 10533 MachineBasicBlock::iterator miI(std::next(SelectItr)); 10534 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) { 10535 const MachineInstr& mi = *miI; 10536 if (mi.readsRegister(ARM::CPSR)) 10537 return false; 10538 if (mi.definesRegister(ARM::CPSR)) 10539 break; // Should have kill-flag - update below. 10540 } 10541 10542 // If we hit the end of the block, check whether CPSR is live into a 10543 // successor. 10544 if (miI == BB->end()) { 10545 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(), 10546 sEnd = BB->succ_end(); 10547 sItr != sEnd; ++sItr) { 10548 MachineBasicBlock* succ = *sItr; 10549 if (succ->isLiveIn(ARM::CPSR)) 10550 return false; 10551 } 10552 } 10553 10554 // We found a def, or hit the end of the basic block and CPSR wasn't live 10555 // out. SelectMI should have a kill flag on CPSR. 10556 SelectItr->addRegisterKilled(ARM::CPSR, TRI); 10557 return true; 10558 } 10559 10560 MachineBasicBlock * 10561 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, 10562 MachineBasicBlock *BB) const { 10563 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 10564 DebugLoc dl = MI.getDebugLoc(); 10565 bool isThumb2 = Subtarget->isThumb2(); 10566 switch (MI.getOpcode()) { 10567 default: { 10568 MI.print(errs()); 10569 llvm_unreachable("Unexpected instr type to insert"); 10570 } 10571 10572 // Thumb1 post-indexed loads are really just single-register LDMs. 10573 case ARM::tLDR_postidx: { 10574 MachineOperand Def(MI.getOperand(1)); 10575 BuildMI(*BB, MI, dl, TII->get(ARM::tLDMIA_UPD)) 10576 .add(Def) // Rn_wb 10577 .add(MI.getOperand(2)) // Rn 10578 .add(MI.getOperand(3)) // PredImm 10579 .add(MI.getOperand(4)) // PredReg 10580 .add(MI.getOperand(0)) // Rt 10581 .cloneMemRefs(MI); 10582 MI.eraseFromParent(); 10583 return BB; 10584 } 10585 10586 // The Thumb2 pre-indexed stores have the same MI operands, they just 10587 // define them differently in the .td files from the isel patterns, so 10588 // they need pseudos. 10589 case ARM::t2STR_preidx: 10590 MI.setDesc(TII->get(ARM::t2STR_PRE)); 10591 return BB; 10592 case ARM::t2STRB_preidx: 10593 MI.setDesc(TII->get(ARM::t2STRB_PRE)); 10594 return BB; 10595 case ARM::t2STRH_preidx: 10596 MI.setDesc(TII->get(ARM::t2STRH_PRE)); 10597 return BB; 10598 10599 case ARM::STRi_preidx: 10600 case ARM::STRBi_preidx: { 10601 unsigned NewOpc = MI.getOpcode() == ARM::STRi_preidx ? ARM::STR_PRE_IMM 10602 : ARM::STRB_PRE_IMM; 10603 // Decode the offset. 10604 unsigned Offset = MI.getOperand(4).getImm(); 10605 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub; 10606 Offset = ARM_AM::getAM2Offset(Offset); 10607 if (isSub) 10608 Offset = -Offset; 10609 10610 MachineMemOperand *MMO = *MI.memoperands_begin(); 10611 BuildMI(*BB, MI, dl, TII->get(NewOpc)) 10612 .add(MI.getOperand(0)) // Rn_wb 10613 .add(MI.getOperand(1)) // Rt 10614 .add(MI.getOperand(2)) // Rn 10615 .addImm(Offset) // offset (skip GPR==zero_reg) 10616 .add(MI.getOperand(5)) // pred 10617 .add(MI.getOperand(6)) 10618 .addMemOperand(MMO); 10619 MI.eraseFromParent(); 10620 return BB; 10621 } 10622 case ARM::STRr_preidx: 10623 case ARM::STRBr_preidx: 10624 case ARM::STRH_preidx: { 10625 unsigned NewOpc; 10626 switch (MI.getOpcode()) { 10627 default: llvm_unreachable("unexpected opcode!"); 10628 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break; 10629 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break; 10630 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break; 10631 } 10632 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc)); 10633 for (unsigned i = 0; i < MI.getNumOperands(); ++i) 10634 MIB.add(MI.getOperand(i)); 10635 MI.eraseFromParent(); 10636 return BB; 10637 } 10638 10639 case ARM::tMOVCCr_pseudo: { 10640 // To "insert" a SELECT_CC instruction, we actually have to insert the 10641 // diamond control-flow pattern. The incoming instruction knows the 10642 // destination vreg to set, the condition code register to branch on, the 10643 // true/false values to select between, and a branch opcode to use. 10644 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 10645 MachineFunction::iterator It = ++BB->getIterator(); 10646 10647 // thisMBB: 10648 // ... 10649 // TrueVal = ... 10650 // cmpTY ccX, r1, r2 10651 // bCC copy1MBB 10652 // fallthrough --> copy0MBB 10653 MachineBasicBlock *thisMBB = BB; 10654 MachineFunction *F = BB->getParent(); 10655 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 10656 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 10657 F->insert(It, copy0MBB); 10658 F->insert(It, sinkMBB); 10659 10660 // Check whether CPSR is live past the tMOVCCr_pseudo. 10661 const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo(); 10662 if (!MI.killsRegister(ARM::CPSR) && 10663 !checkAndUpdateCPSRKill(MI, thisMBB, TRI)) { 10664 copy0MBB->addLiveIn(ARM::CPSR); 10665 sinkMBB->addLiveIn(ARM::CPSR); 10666 } 10667 10668 // Transfer the remainder of BB and its successor edges to sinkMBB. 10669 sinkMBB->splice(sinkMBB->begin(), BB, 10670 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 10671 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 10672 10673 BB->addSuccessor(copy0MBB); 10674 BB->addSuccessor(sinkMBB); 10675 10676 BuildMI(BB, dl, TII->get(ARM::tBcc)) 10677 .addMBB(sinkMBB) 10678 .addImm(MI.getOperand(3).getImm()) 10679 .addReg(MI.getOperand(4).getReg()); 10680 10681 // copy0MBB: 10682 // %FalseValue = ... 10683 // # fallthrough to sinkMBB 10684 BB = copy0MBB; 10685 10686 // Update machine-CFG edges 10687 BB->addSuccessor(sinkMBB); 10688 10689 // sinkMBB: 10690 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 10691 // ... 10692 BB = sinkMBB; 10693 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), MI.getOperand(0).getReg()) 10694 .addReg(MI.getOperand(1).getReg()) 10695 .addMBB(copy0MBB) 10696 .addReg(MI.getOperand(2).getReg()) 10697 .addMBB(thisMBB); 10698 10699 MI.eraseFromParent(); // The pseudo instruction is gone now. 10700 return BB; 10701 } 10702 10703 case ARM::BCCi64: 10704 case ARM::BCCZi64: { 10705 // If there is an unconditional branch to the other successor, remove it. 10706 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end()); 10707 10708 // Compare both parts that make up the double comparison separately for 10709 // equality. 10710 bool RHSisZero = MI.getOpcode() == ARM::BCCZi64; 10711 10712 Register LHS1 = MI.getOperand(1).getReg(); 10713 Register LHS2 = MI.getOperand(2).getReg(); 10714 if (RHSisZero) { 10715 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 10716 .addReg(LHS1) 10717 .addImm(0) 10718 .add(predOps(ARMCC::AL)); 10719 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 10720 .addReg(LHS2).addImm(0) 10721 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 10722 } else { 10723 Register RHS1 = MI.getOperand(3).getReg(); 10724 Register RHS2 = MI.getOperand(4).getReg(); 10725 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 10726 .addReg(LHS1) 10727 .addReg(RHS1) 10728 .add(predOps(ARMCC::AL)); 10729 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 10730 .addReg(LHS2).addReg(RHS2) 10731 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 10732 } 10733 10734 MachineBasicBlock *destMBB = MI.getOperand(RHSisZero ? 3 : 5).getMBB(); 10735 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB); 10736 if (MI.getOperand(0).getImm() == ARMCC::NE) 10737 std::swap(destMBB, exitMBB); 10738 10739 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 10740 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR); 10741 if (isThumb2) 10742 BuildMI(BB, dl, TII->get(ARM::t2B)) 10743 .addMBB(exitMBB) 10744 .add(predOps(ARMCC::AL)); 10745 else 10746 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB); 10747 10748 MI.eraseFromParent(); // The pseudo instruction is gone now. 10749 return BB; 10750 } 10751 10752 case ARM::Int_eh_sjlj_setjmp: 10753 case ARM::Int_eh_sjlj_setjmp_nofp: 10754 case ARM::tInt_eh_sjlj_setjmp: 10755 case ARM::t2Int_eh_sjlj_setjmp: 10756 case ARM::t2Int_eh_sjlj_setjmp_nofp: 10757 return BB; 10758 10759 case ARM::Int_eh_sjlj_setup_dispatch: 10760 EmitSjLjDispatchBlock(MI, BB); 10761 return BB; 10762 10763 case ARM::ABS: 10764 case ARM::t2ABS: { 10765 // To insert an ABS instruction, we have to insert the 10766 // diamond control-flow pattern. The incoming instruction knows the 10767 // source vreg to test against 0, the destination vreg to set, 10768 // the condition code register to branch on, the 10769 // true/false values to select between, and a branch opcode to use. 10770 // It transforms 10771 // V1 = ABS V0 10772 // into 10773 // V2 = MOVS V0 10774 // BCC (branch to SinkBB if V0 >= 0) 10775 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0) 10776 // SinkBB: V1 = PHI(V2, V3) 10777 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 10778 MachineFunction::iterator BBI = ++BB->getIterator(); 10779 MachineFunction *Fn = BB->getParent(); 10780 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB); 10781 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB); 10782 Fn->insert(BBI, RSBBB); 10783 Fn->insert(BBI, SinkBB); 10784 10785 Register ABSSrcReg = MI.getOperand(1).getReg(); 10786 Register ABSDstReg = MI.getOperand(0).getReg(); 10787 bool ABSSrcKIll = MI.getOperand(1).isKill(); 10788 bool isThumb2 = Subtarget->isThumb2(); 10789 MachineRegisterInfo &MRI = Fn->getRegInfo(); 10790 // In Thumb mode S must not be specified if source register is the SP or 10791 // PC and if destination register is the SP, so restrict register class 10792 Register NewRsbDstReg = MRI.createVirtualRegister( 10793 isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass); 10794 10795 // Transfer the remainder of BB and its successor edges to sinkMBB. 10796 SinkBB->splice(SinkBB->begin(), BB, 10797 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 10798 SinkBB->transferSuccessorsAndUpdatePHIs(BB); 10799 10800 BB->addSuccessor(RSBBB); 10801 BB->addSuccessor(SinkBB); 10802 10803 // fall through to SinkMBB 10804 RSBBB->addSuccessor(SinkBB); 10805 10806 // insert a cmp at the end of BB 10807 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 10808 .addReg(ABSSrcReg) 10809 .addImm(0) 10810 .add(predOps(ARMCC::AL)); 10811 10812 // insert a bcc with opposite CC to ARMCC::MI at the end of BB 10813 BuildMI(BB, dl, 10814 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB) 10815 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR); 10816 10817 // insert rsbri in RSBBB 10818 // Note: BCC and rsbri will be converted into predicated rsbmi 10819 // by if-conversion pass 10820 BuildMI(*RSBBB, RSBBB->begin(), dl, 10821 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg) 10822 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0) 10823 .addImm(0) 10824 .add(predOps(ARMCC::AL)) 10825 .add(condCodeOp()); 10826 10827 // insert PHI in SinkBB, 10828 // reuse ABSDstReg to not change uses of ABS instruction 10829 BuildMI(*SinkBB, SinkBB->begin(), dl, 10830 TII->get(ARM::PHI), ABSDstReg) 10831 .addReg(NewRsbDstReg).addMBB(RSBBB) 10832 .addReg(ABSSrcReg).addMBB(BB); 10833 10834 // remove ABS instruction 10835 MI.eraseFromParent(); 10836 10837 // return last added BB 10838 return SinkBB; 10839 } 10840 case ARM::COPY_STRUCT_BYVAL_I32: 10841 ++NumLoopByVals; 10842 return EmitStructByval(MI, BB); 10843 case ARM::WIN__CHKSTK: 10844 return EmitLowered__chkstk(MI, BB); 10845 case ARM::WIN__DBZCHK: 10846 return EmitLowered__dbzchk(MI, BB); 10847 } 10848 } 10849 10850 /// Attaches vregs to MEMCPY that it will use as scratch registers 10851 /// when it is expanded into LDM/STM. This is done as a post-isel lowering 10852 /// instead of as a custom inserter because we need the use list from the SDNode. 10853 static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget, 10854 MachineInstr &MI, const SDNode *Node) { 10855 bool isThumb1 = Subtarget->isThumb1Only(); 10856 10857 DebugLoc DL = MI.getDebugLoc(); 10858 MachineFunction *MF = MI.getParent()->getParent(); 10859 MachineRegisterInfo &MRI = MF->getRegInfo(); 10860 MachineInstrBuilder MIB(*MF, MI); 10861 10862 // If the new dst/src is unused mark it as dead. 10863 if (!Node->hasAnyUseOfValue(0)) { 10864 MI.getOperand(0).setIsDead(true); 10865 } 10866 if (!Node->hasAnyUseOfValue(1)) { 10867 MI.getOperand(1).setIsDead(true); 10868 } 10869 10870 // The MEMCPY both defines and kills the scratch registers. 10871 for (unsigned I = 0; I != MI.getOperand(4).getImm(); ++I) { 10872 Register TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass 10873 : &ARM::GPRRegClass); 10874 MIB.addReg(TmpReg, RegState::Define|RegState::Dead); 10875 } 10876 } 10877 10878 void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI, 10879 SDNode *Node) const { 10880 if (MI.getOpcode() == ARM::MEMCPY) { 10881 attachMEMCPYScratchRegs(Subtarget, MI, Node); 10882 return; 10883 } 10884 10885 const MCInstrDesc *MCID = &MI.getDesc(); 10886 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB, 10887 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional 10888 // operand is still set to noreg. If needed, set the optional operand's 10889 // register to CPSR, and remove the redundant implicit def. 10890 // 10891 // e.g. ADCS (..., implicit-def CPSR) -> ADC (... opt:def CPSR). 10892 10893 // Rename pseudo opcodes. 10894 unsigned NewOpc = convertAddSubFlagsOpcode(MI.getOpcode()); 10895 unsigned ccOutIdx; 10896 if (NewOpc) { 10897 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo(); 10898 MCID = &TII->get(NewOpc); 10899 10900 assert(MCID->getNumOperands() == 10901 MI.getDesc().getNumOperands() + 5 - MI.getDesc().getSize() 10902 && "converted opcode should be the same except for cc_out" 10903 " (and, on Thumb1, pred)"); 10904 10905 MI.setDesc(*MCID); 10906 10907 // Add the optional cc_out operand 10908 MI.addOperand(MachineOperand::CreateReg(0, /*isDef=*/true)); 10909 10910 // On Thumb1, move all input operands to the end, then add the predicate 10911 if (Subtarget->isThumb1Only()) { 10912 for (unsigned c = MCID->getNumOperands() - 4; c--;) { 10913 MI.addOperand(MI.getOperand(1)); 10914 MI.RemoveOperand(1); 10915 } 10916 10917 // Restore the ties 10918 for (unsigned i = MI.getNumOperands(); i--;) { 10919 const MachineOperand& op = MI.getOperand(i); 10920 if (op.isReg() && op.isUse()) { 10921 int DefIdx = MCID->getOperandConstraint(i, MCOI::TIED_TO); 10922 if (DefIdx != -1) 10923 MI.tieOperands(DefIdx, i); 10924 } 10925 } 10926 10927 MI.addOperand(MachineOperand::CreateImm(ARMCC::AL)); 10928 MI.addOperand(MachineOperand::CreateReg(0, /*isDef=*/false)); 10929 ccOutIdx = 1; 10930 } else 10931 ccOutIdx = MCID->getNumOperands() - 1; 10932 } else 10933 ccOutIdx = MCID->getNumOperands() - 1; 10934 10935 // Any ARM instruction that sets the 's' bit should specify an optional 10936 // "cc_out" operand in the last operand position. 10937 if (!MI.hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) { 10938 assert(!NewOpc && "Optional cc_out operand required"); 10939 return; 10940 } 10941 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it 10942 // since we already have an optional CPSR def. 10943 bool definesCPSR = false; 10944 bool deadCPSR = false; 10945 for (unsigned i = MCID->getNumOperands(), e = MI.getNumOperands(); i != e; 10946 ++i) { 10947 const MachineOperand &MO = MI.getOperand(i); 10948 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) { 10949 definesCPSR = true; 10950 if (MO.isDead()) 10951 deadCPSR = true; 10952 MI.RemoveOperand(i); 10953 break; 10954 } 10955 } 10956 if (!definesCPSR) { 10957 assert(!NewOpc && "Optional cc_out operand required"); 10958 return; 10959 } 10960 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag"); 10961 if (deadCPSR) { 10962 assert(!MI.getOperand(ccOutIdx).getReg() && 10963 "expect uninitialized optional cc_out operand"); 10964 // Thumb1 instructions must have the S bit even if the CPSR is dead. 10965 if (!Subtarget->isThumb1Only()) 10966 return; 10967 } 10968 10969 // If this instruction was defined with an optional CPSR def and its dag node 10970 // had a live implicit CPSR def, then activate the optional CPSR def. 10971 MachineOperand &MO = MI.getOperand(ccOutIdx); 10972 MO.setReg(ARM::CPSR); 10973 MO.setIsDef(true); 10974 } 10975 10976 //===----------------------------------------------------------------------===// 10977 // ARM Optimization Hooks 10978 //===----------------------------------------------------------------------===// 10979 10980 // Helper function that checks if N is a null or all ones constant. 10981 static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) { 10982 return AllOnes ? isAllOnesConstant(N) : isNullConstant(N); 10983 } 10984 10985 // Return true if N is conditionally 0 or all ones. 10986 // Detects these expressions where cc is an i1 value: 10987 // 10988 // (select cc 0, y) [AllOnes=0] 10989 // (select cc y, 0) [AllOnes=0] 10990 // (zext cc) [AllOnes=0] 10991 // (sext cc) [AllOnes=0/1] 10992 // (select cc -1, y) [AllOnes=1] 10993 // (select cc y, -1) [AllOnes=1] 10994 // 10995 // Invert is set when N is the null/all ones constant when CC is false. 10996 // OtherOp is set to the alternative value of N. 10997 static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes, 10998 SDValue &CC, bool &Invert, 10999 SDValue &OtherOp, 11000 SelectionDAG &DAG) { 11001 switch (N->getOpcode()) { 11002 default: return false; 11003 case ISD::SELECT: { 11004 CC = N->getOperand(0); 11005 SDValue N1 = N->getOperand(1); 11006 SDValue N2 = N->getOperand(2); 11007 if (isZeroOrAllOnes(N1, AllOnes)) { 11008 Invert = false; 11009 OtherOp = N2; 11010 return true; 11011 } 11012 if (isZeroOrAllOnes(N2, AllOnes)) { 11013 Invert = true; 11014 OtherOp = N1; 11015 return true; 11016 } 11017 return false; 11018 } 11019 case ISD::ZERO_EXTEND: 11020 // (zext cc) can never be the all ones value. 11021 if (AllOnes) 11022 return false; 11023 LLVM_FALLTHROUGH; 11024 case ISD::SIGN_EXTEND: { 11025 SDLoc dl(N); 11026 EVT VT = N->getValueType(0); 11027 CC = N->getOperand(0); 11028 if (CC.getValueType() != MVT::i1 || CC.getOpcode() != ISD::SETCC) 11029 return false; 11030 Invert = !AllOnes; 11031 if (AllOnes) 11032 // When looking for an AllOnes constant, N is an sext, and the 'other' 11033 // value is 0. 11034 OtherOp = DAG.getConstant(0, dl, VT); 11035 else if (N->getOpcode() == ISD::ZERO_EXTEND) 11036 // When looking for a 0 constant, N can be zext or sext. 11037 OtherOp = DAG.getConstant(1, dl, VT); 11038 else 11039 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl, 11040 VT); 11041 return true; 11042 } 11043 } 11044 } 11045 11046 // Combine a constant select operand into its use: 11047 // 11048 // (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 11049 // (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 11050 // (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1] 11051 // (or (select cc, 0, c), x) -> (select cc, x, (or, x, c)) 11052 // (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c)) 11053 // 11054 // The transform is rejected if the select doesn't have a constant operand that 11055 // is null, or all ones when AllOnes is set. 11056 // 11057 // Also recognize sext/zext from i1: 11058 // 11059 // (add (zext cc), x) -> (select cc (add x, 1), x) 11060 // (add (sext cc), x) -> (select cc (add x, -1), x) 11061 // 11062 // These transformations eventually create predicated instructions. 11063 // 11064 // @param N The node to transform. 11065 // @param Slct The N operand that is a select. 11066 // @param OtherOp The other N operand (x above). 11067 // @param DCI Context. 11068 // @param AllOnes Require the select constant to be all ones instead of null. 11069 // @returns The new node, or SDValue() on failure. 11070 static 11071 SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp, 11072 TargetLowering::DAGCombinerInfo &DCI, 11073 bool AllOnes = false) { 11074 SelectionDAG &DAG = DCI.DAG; 11075 EVT VT = N->getValueType(0); 11076 SDValue NonConstantVal; 11077 SDValue CCOp; 11078 bool SwapSelectOps; 11079 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps, 11080 NonConstantVal, DAG)) 11081 return SDValue(); 11082 11083 // Slct is now know to be the desired identity constant when CC is true. 11084 SDValue TrueVal = OtherOp; 11085 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT, 11086 OtherOp, NonConstantVal); 11087 // Unless SwapSelectOps says CC should be false. 11088 if (SwapSelectOps) 11089 std::swap(TrueVal, FalseVal); 11090 11091 return DAG.getNode(ISD::SELECT, SDLoc(N), VT, 11092 CCOp, TrueVal, FalseVal); 11093 } 11094 11095 // Attempt combineSelectAndUse on each operand of a commutative operator N. 11096 static 11097 SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes, 11098 TargetLowering::DAGCombinerInfo &DCI) { 11099 SDValue N0 = N->getOperand(0); 11100 SDValue N1 = N->getOperand(1); 11101 if (N0.getNode()->hasOneUse()) 11102 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes)) 11103 return Result; 11104 if (N1.getNode()->hasOneUse()) 11105 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes)) 11106 return Result; 11107 return SDValue(); 11108 } 11109 11110 static bool IsVUZPShuffleNode(SDNode *N) { 11111 // VUZP shuffle node. 11112 if (N->getOpcode() == ARMISD::VUZP) 11113 return true; 11114 11115 // "VUZP" on i32 is an alias for VTRN. 11116 if (N->getOpcode() == ARMISD::VTRN && N->getValueType(0) == MVT::v2i32) 11117 return true; 11118 11119 return false; 11120 } 11121 11122 static SDValue AddCombineToVPADD(SDNode *N, SDValue N0, SDValue N1, 11123 TargetLowering::DAGCombinerInfo &DCI, 11124 const ARMSubtarget *Subtarget) { 11125 // Look for ADD(VUZP.0, VUZP.1). 11126 if (!IsVUZPShuffleNode(N0.getNode()) || N0.getNode() != N1.getNode() || 11127 N0 == N1) 11128 return SDValue(); 11129 11130 // Make sure the ADD is a 64-bit add; there is no 128-bit VPADD. 11131 if (!N->getValueType(0).is64BitVector()) 11132 return SDValue(); 11133 11134 // Generate vpadd. 11135 SelectionDAG &DAG = DCI.DAG; 11136 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 11137 SDLoc dl(N); 11138 SDNode *Unzip = N0.getNode(); 11139 EVT VT = N->getValueType(0); 11140 11141 SmallVector<SDValue, 8> Ops; 11142 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpadd, dl, 11143 TLI.getPointerTy(DAG.getDataLayout()))); 11144 Ops.push_back(Unzip->getOperand(0)); 11145 Ops.push_back(Unzip->getOperand(1)); 11146 11147 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, Ops); 11148 } 11149 11150 static SDValue AddCombineVUZPToVPADDL(SDNode *N, SDValue N0, SDValue N1, 11151 TargetLowering::DAGCombinerInfo &DCI, 11152 const ARMSubtarget *Subtarget) { 11153 // Check for two extended operands. 11154 if (!(N0.getOpcode() == ISD::SIGN_EXTEND && 11155 N1.getOpcode() == ISD::SIGN_EXTEND) && 11156 !(N0.getOpcode() == ISD::ZERO_EXTEND && 11157 N1.getOpcode() == ISD::ZERO_EXTEND)) 11158 return SDValue(); 11159 11160 SDValue N00 = N0.getOperand(0); 11161 SDValue N10 = N1.getOperand(0); 11162 11163 // Look for ADD(SEXT(VUZP.0), SEXT(VUZP.1)) 11164 if (!IsVUZPShuffleNode(N00.getNode()) || N00.getNode() != N10.getNode() || 11165 N00 == N10) 11166 return SDValue(); 11167 11168 // We only recognize Q register paddl here; this can't be reached until 11169 // after type legalization. 11170 if (!N00.getValueType().is64BitVector() || 11171 !N0.getValueType().is128BitVector()) 11172 return SDValue(); 11173 11174 // Generate vpaddl. 11175 SelectionDAG &DAG = DCI.DAG; 11176 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 11177 SDLoc dl(N); 11178 EVT VT = N->getValueType(0); 11179 11180 SmallVector<SDValue, 8> Ops; 11181 // Form vpaddl.sN or vpaddl.uN depending on the kind of extension. 11182 unsigned Opcode; 11183 if (N0.getOpcode() == ISD::SIGN_EXTEND) 11184 Opcode = Intrinsic::arm_neon_vpaddls; 11185 else 11186 Opcode = Intrinsic::arm_neon_vpaddlu; 11187 Ops.push_back(DAG.getConstant(Opcode, dl, 11188 TLI.getPointerTy(DAG.getDataLayout()))); 11189 EVT ElemTy = N00.getValueType().getVectorElementType(); 11190 unsigned NumElts = VT.getVectorNumElements(); 11191 EVT ConcatVT = EVT::getVectorVT(*DAG.getContext(), ElemTy, NumElts * 2); 11192 SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), ConcatVT, 11193 N00.getOperand(0), N00.getOperand(1)); 11194 Ops.push_back(Concat); 11195 11196 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, Ops); 11197 } 11198 11199 // FIXME: This function shouldn't be necessary; if we lower BUILD_VECTOR in 11200 // an appropriate manner, we end up with ADD(VUZP(ZEXT(N))), which is 11201 // much easier to match. 11202 static SDValue 11203 AddCombineBUILD_VECTORToVPADDL(SDNode *N, SDValue N0, SDValue N1, 11204 TargetLowering::DAGCombinerInfo &DCI, 11205 const ARMSubtarget *Subtarget) { 11206 // Only perform optimization if after legalize, and if NEON is available. We 11207 // also expected both operands to be BUILD_VECTORs. 11208 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON() 11209 || N0.getOpcode() != ISD::BUILD_VECTOR 11210 || N1.getOpcode() != ISD::BUILD_VECTOR) 11211 return SDValue(); 11212 11213 // Check output type since VPADDL operand elements can only be 8, 16, or 32. 11214 EVT VT = N->getValueType(0); 11215 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64) 11216 return SDValue(); 11217 11218 // Check that the vector operands are of the right form. 11219 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR 11220 // operands, where N is the size of the formed vector. 11221 // Each EXTRACT_VECTOR should have the same input vector and odd or even 11222 // index such that we have a pair wise add pattern. 11223 11224 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing. 11225 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 11226 return SDValue(); 11227 SDValue Vec = N0->getOperand(0)->getOperand(0); 11228 SDNode *V = Vec.getNode(); 11229 unsigned nextIndex = 0; 11230 11231 // For each operands to the ADD which are BUILD_VECTORs, 11232 // check to see if each of their operands are an EXTRACT_VECTOR with 11233 // the same vector and appropriate index. 11234 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) { 11235 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT 11236 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 11237 11238 SDValue ExtVec0 = N0->getOperand(i); 11239 SDValue ExtVec1 = N1->getOperand(i); 11240 11241 // First operand is the vector, verify its the same. 11242 if (V != ExtVec0->getOperand(0).getNode() || 11243 V != ExtVec1->getOperand(0).getNode()) 11244 return SDValue(); 11245 11246 // Second is the constant, verify its correct. 11247 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1)); 11248 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1)); 11249 11250 // For the constant, we want to see all the even or all the odd. 11251 if (!C0 || !C1 || C0->getZExtValue() != nextIndex 11252 || C1->getZExtValue() != nextIndex+1) 11253 return SDValue(); 11254 11255 // Increment index. 11256 nextIndex+=2; 11257 } else 11258 return SDValue(); 11259 } 11260 11261 // Don't generate vpaddl+vmovn; we'll match it to vpadd later. Also make sure 11262 // we're using the entire input vector, otherwise there's a size/legality 11263 // mismatch somewhere. 11264 if (nextIndex != Vec.getValueType().getVectorNumElements() || 11265 Vec.getValueType().getVectorElementType() == VT.getVectorElementType()) 11266 return SDValue(); 11267 11268 // Create VPADDL node. 11269 SelectionDAG &DAG = DCI.DAG; 11270 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 11271 11272 SDLoc dl(N); 11273 11274 // Build operand list. 11275 SmallVector<SDValue, 8> Ops; 11276 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl, 11277 TLI.getPointerTy(DAG.getDataLayout()))); 11278 11279 // Input is the vector. 11280 Ops.push_back(Vec); 11281 11282 // Get widened type and narrowed type. 11283 MVT widenType; 11284 unsigned numElem = VT.getVectorNumElements(); 11285 11286 EVT inputLaneType = Vec.getValueType().getVectorElementType(); 11287 switch (inputLaneType.getSimpleVT().SimpleTy) { 11288 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break; 11289 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break; 11290 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break; 11291 default: 11292 llvm_unreachable("Invalid vector element type for padd optimization."); 11293 } 11294 11295 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops); 11296 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE; 11297 return DAG.getNode(ExtOp, dl, VT, tmp); 11298 } 11299 11300 static SDValue findMUL_LOHI(SDValue V) { 11301 if (V->getOpcode() == ISD::UMUL_LOHI || 11302 V->getOpcode() == ISD::SMUL_LOHI) 11303 return V; 11304 return SDValue(); 11305 } 11306 11307 static SDValue AddCombineTo64BitSMLAL16(SDNode *AddcNode, SDNode *AddeNode, 11308 TargetLowering::DAGCombinerInfo &DCI, 11309 const ARMSubtarget *Subtarget) { 11310 if (!Subtarget->hasBaseDSP()) 11311 return SDValue(); 11312 11313 // SMLALBB, SMLALBT, SMLALTB, SMLALTT multiply two 16-bit values and 11314 // accumulates the product into a 64-bit value. The 16-bit values will 11315 // be sign extended somehow or SRA'd into 32-bit values 11316 // (addc (adde (mul 16bit, 16bit), lo), hi) 11317 SDValue Mul = AddcNode->getOperand(0); 11318 SDValue Lo = AddcNode->getOperand(1); 11319 if (Mul.getOpcode() != ISD::MUL) { 11320 Lo = AddcNode->getOperand(0); 11321 Mul = AddcNode->getOperand(1); 11322 if (Mul.getOpcode() != ISD::MUL) 11323 return SDValue(); 11324 } 11325 11326 SDValue SRA = AddeNode->getOperand(0); 11327 SDValue Hi = AddeNode->getOperand(1); 11328 if (SRA.getOpcode() != ISD::SRA) { 11329 SRA = AddeNode->getOperand(1); 11330 Hi = AddeNode->getOperand(0); 11331 if (SRA.getOpcode() != ISD::SRA) 11332 return SDValue(); 11333 } 11334 if (auto Const = dyn_cast<ConstantSDNode>(SRA.getOperand(1))) { 11335 if (Const->getZExtValue() != 31) 11336 return SDValue(); 11337 } else 11338 return SDValue(); 11339 11340 if (SRA.getOperand(0) != Mul) 11341 return SDValue(); 11342 11343 SelectionDAG &DAG = DCI.DAG; 11344 SDLoc dl(AddcNode); 11345 unsigned Opcode = 0; 11346 SDValue Op0; 11347 SDValue Op1; 11348 11349 if (isS16(Mul.getOperand(0), DAG) && isS16(Mul.getOperand(1), DAG)) { 11350 Opcode = ARMISD::SMLALBB; 11351 Op0 = Mul.getOperand(0); 11352 Op1 = Mul.getOperand(1); 11353 } else if (isS16(Mul.getOperand(0), DAG) && isSRA16(Mul.getOperand(1))) { 11354 Opcode = ARMISD::SMLALBT; 11355 Op0 = Mul.getOperand(0); 11356 Op1 = Mul.getOperand(1).getOperand(0); 11357 } else if (isSRA16(Mul.getOperand(0)) && isS16(Mul.getOperand(1), DAG)) { 11358 Opcode = ARMISD::SMLALTB; 11359 Op0 = Mul.getOperand(0).getOperand(0); 11360 Op1 = Mul.getOperand(1); 11361 } else if (isSRA16(Mul.getOperand(0)) && isSRA16(Mul.getOperand(1))) { 11362 Opcode = ARMISD::SMLALTT; 11363 Op0 = Mul->getOperand(0).getOperand(0); 11364 Op1 = Mul->getOperand(1).getOperand(0); 11365 } 11366 11367 if (!Op0 || !Op1) 11368 return SDValue(); 11369 11370 SDValue SMLAL = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32), 11371 Op0, Op1, Lo, Hi); 11372 // Replace the ADDs' nodes uses by the MLA node's values. 11373 SDValue HiMLALResult(SMLAL.getNode(), 1); 11374 SDValue LoMLALResult(SMLAL.getNode(), 0); 11375 11376 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult); 11377 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult); 11378 11379 // Return original node to notify the driver to stop replacing. 11380 SDValue resNode(AddcNode, 0); 11381 return resNode; 11382 } 11383 11384 static SDValue AddCombineTo64bitMLAL(SDNode *AddeSubeNode, 11385 TargetLowering::DAGCombinerInfo &DCI, 11386 const ARMSubtarget *Subtarget) { 11387 // Look for multiply add opportunities. 11388 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where 11389 // each add nodes consumes a value from ISD::UMUL_LOHI and there is 11390 // a glue link from the first add to the second add. 11391 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by 11392 // a S/UMLAL instruction. 11393 // UMUL_LOHI 11394 // / :lo \ :hi 11395 // V \ [no multiline comment] 11396 // loAdd -> ADDC | 11397 // \ :carry / 11398 // V V 11399 // ADDE <- hiAdd 11400 // 11401 // In the special case where only the higher part of a signed result is used 11402 // and the add to the low part of the result of ISD::UMUL_LOHI adds or subtracts 11403 // a constant with the exact value of 0x80000000, we recognize we are dealing 11404 // with a "rounded multiply and add" (or subtract) and transform it into 11405 // either a ARMISD::SMMLAR or ARMISD::SMMLSR respectively. 11406 11407 assert((AddeSubeNode->getOpcode() == ARMISD::ADDE || 11408 AddeSubeNode->getOpcode() == ARMISD::SUBE) && 11409 "Expect an ADDE or SUBE"); 11410 11411 assert(AddeSubeNode->getNumOperands() == 3 && 11412 AddeSubeNode->getOperand(2).getValueType() == MVT::i32 && 11413 "ADDE node has the wrong inputs"); 11414 11415 // Check that we are chained to the right ADDC or SUBC node. 11416 SDNode *AddcSubcNode = AddeSubeNode->getOperand(2).getNode(); 11417 if ((AddeSubeNode->getOpcode() == ARMISD::ADDE && 11418 AddcSubcNode->getOpcode() != ARMISD::ADDC) || 11419 (AddeSubeNode->getOpcode() == ARMISD::SUBE && 11420 AddcSubcNode->getOpcode() != ARMISD::SUBC)) 11421 return SDValue(); 11422 11423 SDValue AddcSubcOp0 = AddcSubcNode->getOperand(0); 11424 SDValue AddcSubcOp1 = AddcSubcNode->getOperand(1); 11425 11426 // Check if the two operands are from the same mul_lohi node. 11427 if (AddcSubcOp0.getNode() == AddcSubcOp1.getNode()) 11428 return SDValue(); 11429 11430 assert(AddcSubcNode->getNumValues() == 2 && 11431 AddcSubcNode->getValueType(0) == MVT::i32 && 11432 "Expect ADDC with two result values. First: i32"); 11433 11434 // Check that the ADDC adds the low result of the S/UMUL_LOHI. If not, it 11435 // maybe a SMLAL which multiplies two 16-bit values. 11436 if (AddeSubeNode->getOpcode() == ARMISD::ADDE && 11437 AddcSubcOp0->getOpcode() != ISD::UMUL_LOHI && 11438 AddcSubcOp0->getOpcode() != ISD::SMUL_LOHI && 11439 AddcSubcOp1->getOpcode() != ISD::UMUL_LOHI && 11440 AddcSubcOp1->getOpcode() != ISD::SMUL_LOHI) 11441 return AddCombineTo64BitSMLAL16(AddcSubcNode, AddeSubeNode, DCI, Subtarget); 11442 11443 // Check for the triangle shape. 11444 SDValue AddeSubeOp0 = AddeSubeNode->getOperand(0); 11445 SDValue AddeSubeOp1 = AddeSubeNode->getOperand(1); 11446 11447 // Make sure that the ADDE/SUBE operands are not coming from the same node. 11448 if (AddeSubeOp0.getNode() == AddeSubeOp1.getNode()) 11449 return SDValue(); 11450 11451 // Find the MUL_LOHI node walking up ADDE/SUBE's operands. 11452 bool IsLeftOperandMUL = false; 11453 SDValue MULOp = findMUL_LOHI(AddeSubeOp0); 11454 if (MULOp == SDValue()) 11455 MULOp = findMUL_LOHI(AddeSubeOp1); 11456 else 11457 IsLeftOperandMUL = true; 11458 if (MULOp == SDValue()) 11459 return SDValue(); 11460 11461 // Figure out the right opcode. 11462 unsigned Opc = MULOp->getOpcode(); 11463 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL; 11464 11465 // Figure out the high and low input values to the MLAL node. 11466 SDValue *HiAddSub = nullptr; 11467 SDValue *LoMul = nullptr; 11468 SDValue *LowAddSub = nullptr; 11469 11470 // Ensure that ADDE/SUBE is from high result of ISD::xMUL_LOHI. 11471 if ((AddeSubeOp0 != MULOp.getValue(1)) && (AddeSubeOp1 != MULOp.getValue(1))) 11472 return SDValue(); 11473 11474 if (IsLeftOperandMUL) 11475 HiAddSub = &AddeSubeOp1; 11476 else 11477 HiAddSub = &AddeSubeOp0; 11478 11479 // Ensure that LoMul and LowAddSub are taken from correct ISD::SMUL_LOHI node 11480 // whose low result is fed to the ADDC/SUBC we are checking. 11481 11482 if (AddcSubcOp0 == MULOp.getValue(0)) { 11483 LoMul = &AddcSubcOp0; 11484 LowAddSub = &AddcSubcOp1; 11485 } 11486 if (AddcSubcOp1 == MULOp.getValue(0)) { 11487 LoMul = &AddcSubcOp1; 11488 LowAddSub = &AddcSubcOp0; 11489 } 11490 11491 if (!LoMul) 11492 return SDValue(); 11493 11494 // If HiAddSub is the same node as ADDC/SUBC or is a predecessor of ADDC/SUBC 11495 // the replacement below will create a cycle. 11496 if (AddcSubcNode == HiAddSub->getNode() || 11497 AddcSubcNode->isPredecessorOf(HiAddSub->getNode())) 11498 return SDValue(); 11499 11500 // Create the merged node. 11501 SelectionDAG &DAG = DCI.DAG; 11502 11503 // Start building operand list. 11504 SmallVector<SDValue, 8> Ops; 11505 Ops.push_back(LoMul->getOperand(0)); 11506 Ops.push_back(LoMul->getOperand(1)); 11507 11508 // Check whether we can use SMMLAR, SMMLSR or SMMULR instead. For this to be 11509 // the case, we must be doing signed multiplication and only use the higher 11510 // part of the result of the MLAL, furthermore the LowAddSub must be a constant 11511 // addition or subtraction with the value of 0x800000. 11512 if (Subtarget->hasV6Ops() && Subtarget->hasDSP() && Subtarget->useMulOps() && 11513 FinalOpc == ARMISD::SMLAL && !AddeSubeNode->hasAnyUseOfValue(1) && 11514 LowAddSub->getNode()->getOpcode() == ISD::Constant && 11515 static_cast<ConstantSDNode *>(LowAddSub->getNode())->getZExtValue() == 11516 0x80000000) { 11517 Ops.push_back(*HiAddSub); 11518 if (AddcSubcNode->getOpcode() == ARMISD::SUBC) { 11519 FinalOpc = ARMISD::SMMLSR; 11520 } else { 11521 FinalOpc = ARMISD::SMMLAR; 11522 } 11523 SDValue NewNode = DAG.getNode(FinalOpc, SDLoc(AddcSubcNode), MVT::i32, Ops); 11524 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeSubeNode, 0), NewNode); 11525 11526 return SDValue(AddeSubeNode, 0); 11527 } else if (AddcSubcNode->getOpcode() == ARMISD::SUBC) 11528 // SMMLS is generated during instruction selection and the rest of this 11529 // function can not handle the case where AddcSubcNode is a SUBC. 11530 return SDValue(); 11531 11532 // Finish building the operand list for {U/S}MLAL 11533 Ops.push_back(*LowAddSub); 11534 Ops.push_back(*HiAddSub); 11535 11536 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcSubcNode), 11537 DAG.getVTList(MVT::i32, MVT::i32), Ops); 11538 11539 // Replace the ADDs' nodes uses by the MLA node's values. 11540 SDValue HiMLALResult(MLALNode.getNode(), 1); 11541 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeSubeNode, 0), HiMLALResult); 11542 11543 SDValue LoMLALResult(MLALNode.getNode(), 0); 11544 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcSubcNode, 0), LoMLALResult); 11545 11546 // Return original node to notify the driver to stop replacing. 11547 return SDValue(AddeSubeNode, 0); 11548 } 11549 11550 static SDValue AddCombineTo64bitUMAAL(SDNode *AddeNode, 11551 TargetLowering::DAGCombinerInfo &DCI, 11552 const ARMSubtarget *Subtarget) { 11553 // UMAAL is similar to UMLAL except that it adds two unsigned values. 11554 // While trying to combine for the other MLAL nodes, first search for the 11555 // chance to use UMAAL. Check if Addc uses a node which has already 11556 // been combined into a UMLAL. The other pattern is UMLAL using Addc/Adde 11557 // as the addend, and it's handled in PerformUMLALCombine. 11558 11559 if (!Subtarget->hasV6Ops() || !Subtarget->hasDSP()) 11560 return AddCombineTo64bitMLAL(AddeNode, DCI, Subtarget); 11561 11562 // Check that we have a glued ADDC node. 11563 SDNode* AddcNode = AddeNode->getOperand(2).getNode(); 11564 if (AddcNode->getOpcode() != ARMISD::ADDC) 11565 return SDValue(); 11566 11567 // Find the converted UMAAL or quit if it doesn't exist. 11568 SDNode *UmlalNode = nullptr; 11569 SDValue AddHi; 11570 if (AddcNode->getOperand(0).getOpcode() == ARMISD::UMLAL) { 11571 UmlalNode = AddcNode->getOperand(0).getNode(); 11572 AddHi = AddcNode->getOperand(1); 11573 } else if (AddcNode->getOperand(1).getOpcode() == ARMISD::UMLAL) { 11574 UmlalNode = AddcNode->getOperand(1).getNode(); 11575 AddHi = AddcNode->getOperand(0); 11576 } else { 11577 return AddCombineTo64bitMLAL(AddeNode, DCI, Subtarget); 11578 } 11579 11580 // The ADDC should be glued to an ADDE node, which uses the same UMLAL as 11581 // the ADDC as well as Zero. 11582 if (!isNullConstant(UmlalNode->getOperand(3))) 11583 return SDValue(); 11584 11585 if ((isNullConstant(AddeNode->getOperand(0)) && 11586 AddeNode->getOperand(1).getNode() == UmlalNode) || 11587 (AddeNode->getOperand(0).getNode() == UmlalNode && 11588 isNullConstant(AddeNode->getOperand(1)))) { 11589 SelectionDAG &DAG = DCI.DAG; 11590 SDValue Ops[] = { UmlalNode->getOperand(0), UmlalNode->getOperand(1), 11591 UmlalNode->getOperand(2), AddHi }; 11592 SDValue UMAAL = DAG.getNode(ARMISD::UMAAL, SDLoc(AddcNode), 11593 DAG.getVTList(MVT::i32, MVT::i32), Ops); 11594 11595 // Replace the ADDs' nodes uses by the UMAAL node's values. 11596 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), SDValue(UMAAL.getNode(), 1)); 11597 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), SDValue(UMAAL.getNode(), 0)); 11598 11599 // Return original node to notify the driver to stop replacing. 11600 return SDValue(AddeNode, 0); 11601 } 11602 return SDValue(); 11603 } 11604 11605 static SDValue PerformUMLALCombine(SDNode *N, SelectionDAG &DAG, 11606 const ARMSubtarget *Subtarget) { 11607 if (!Subtarget->hasV6Ops() || !Subtarget->hasDSP()) 11608 return SDValue(); 11609 11610 // Check that we have a pair of ADDC and ADDE as operands. 11611 // Both addends of the ADDE must be zero. 11612 SDNode* AddcNode = N->getOperand(2).getNode(); 11613 SDNode* AddeNode = N->getOperand(3).getNode(); 11614 if ((AddcNode->getOpcode() == ARMISD::ADDC) && 11615 (AddeNode->getOpcode() == ARMISD::ADDE) && 11616 isNullConstant(AddeNode->getOperand(0)) && 11617 isNullConstant(AddeNode->getOperand(1)) && 11618 (AddeNode->getOperand(2).getNode() == AddcNode)) 11619 return DAG.getNode(ARMISD::UMAAL, SDLoc(N), 11620 DAG.getVTList(MVT::i32, MVT::i32), 11621 {N->getOperand(0), N->getOperand(1), 11622 AddcNode->getOperand(0), AddcNode->getOperand(1)}); 11623 else 11624 return SDValue(); 11625 } 11626 11627 static SDValue PerformAddcSubcCombine(SDNode *N, 11628 TargetLowering::DAGCombinerInfo &DCI, 11629 const ARMSubtarget *Subtarget) { 11630 SelectionDAG &DAG(DCI.DAG); 11631 11632 if (N->getOpcode() == ARMISD::SUBC) { 11633 // (SUBC (ADDE 0, 0, C), 1) -> C 11634 SDValue LHS = N->getOperand(0); 11635 SDValue RHS = N->getOperand(1); 11636 if (LHS->getOpcode() == ARMISD::ADDE && 11637 isNullConstant(LHS->getOperand(0)) && 11638 isNullConstant(LHS->getOperand(1)) && isOneConstant(RHS)) { 11639 return DCI.CombineTo(N, SDValue(N, 0), LHS->getOperand(2)); 11640 } 11641 } 11642 11643 if (Subtarget->isThumb1Only()) { 11644 SDValue RHS = N->getOperand(1); 11645 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) { 11646 int32_t imm = C->getSExtValue(); 11647 if (imm < 0 && imm > std::numeric_limits<int>::min()) { 11648 SDLoc DL(N); 11649 RHS = DAG.getConstant(-imm, DL, MVT::i32); 11650 unsigned Opcode = (N->getOpcode() == ARMISD::ADDC) ? ARMISD::SUBC 11651 : ARMISD::ADDC; 11652 return DAG.getNode(Opcode, DL, N->getVTList(), N->getOperand(0), RHS); 11653 } 11654 } 11655 } 11656 11657 return SDValue(); 11658 } 11659 11660 static SDValue PerformAddeSubeCombine(SDNode *N, 11661 TargetLowering::DAGCombinerInfo &DCI, 11662 const ARMSubtarget *Subtarget) { 11663 if (Subtarget->isThumb1Only()) { 11664 SelectionDAG &DAG = DCI.DAG; 11665 SDValue RHS = N->getOperand(1); 11666 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) { 11667 int64_t imm = C->getSExtValue(); 11668 if (imm < 0) { 11669 SDLoc DL(N); 11670 11671 // The with-carry-in form matches bitwise not instead of the negation. 11672 // Effectively, the inverse interpretation of the carry flag already 11673 // accounts for part of the negation. 11674 RHS = DAG.getConstant(~imm, DL, MVT::i32); 11675 11676 unsigned Opcode = (N->getOpcode() == ARMISD::ADDE) ? ARMISD::SUBE 11677 : ARMISD::ADDE; 11678 return DAG.getNode(Opcode, DL, N->getVTList(), 11679 N->getOperand(0), RHS, N->getOperand(2)); 11680 } 11681 } 11682 } else if (N->getOperand(1)->getOpcode() == ISD::SMUL_LOHI) { 11683 return AddCombineTo64bitMLAL(N, DCI, Subtarget); 11684 } 11685 return SDValue(); 11686 } 11687 11688 static SDValue PerformABSCombine(SDNode *N, 11689 TargetLowering::DAGCombinerInfo &DCI, 11690 const ARMSubtarget *Subtarget) { 11691 SDValue res; 11692 SelectionDAG &DAG = DCI.DAG; 11693 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 11694 11695 if (TLI.isOperationLegal(N->getOpcode(), N->getValueType(0))) 11696 return SDValue(); 11697 11698 if (!TLI.expandABS(N, res, DAG)) 11699 return SDValue(); 11700 11701 return res; 11702 } 11703 11704 /// PerformADDECombine - Target-specific dag combine transform from 11705 /// ARMISD::ADDC, ARMISD::ADDE, and ISD::MUL_LOHI to MLAL or 11706 /// ARMISD::ADDC, ARMISD::ADDE and ARMISD::UMLAL to ARMISD::UMAAL 11707 static SDValue PerformADDECombine(SDNode *N, 11708 TargetLowering::DAGCombinerInfo &DCI, 11709 const ARMSubtarget *Subtarget) { 11710 // Only ARM and Thumb2 support UMLAL/SMLAL. 11711 if (Subtarget->isThumb1Only()) 11712 return PerformAddeSubeCombine(N, DCI, Subtarget); 11713 11714 // Only perform the checks after legalize when the pattern is available. 11715 if (DCI.isBeforeLegalize()) return SDValue(); 11716 11717 return AddCombineTo64bitUMAAL(N, DCI, Subtarget); 11718 } 11719 11720 /// PerformADDCombineWithOperands - Try DAG combinations for an ADD with 11721 /// operands N0 and N1. This is a helper for PerformADDCombine that is 11722 /// called with the default operands, and if that fails, with commuted 11723 /// operands. 11724 static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1, 11725 TargetLowering::DAGCombinerInfo &DCI, 11726 const ARMSubtarget *Subtarget){ 11727 // Attempt to create vpadd for this add. 11728 if (SDValue Result = AddCombineToVPADD(N, N0, N1, DCI, Subtarget)) 11729 return Result; 11730 11731 // Attempt to create vpaddl for this add. 11732 if (SDValue Result = AddCombineVUZPToVPADDL(N, N0, N1, DCI, Subtarget)) 11733 return Result; 11734 if (SDValue Result = AddCombineBUILD_VECTORToVPADDL(N, N0, N1, DCI, 11735 Subtarget)) 11736 return Result; 11737 11738 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 11739 if (N0.getNode()->hasOneUse()) 11740 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI)) 11741 return Result; 11742 return SDValue(); 11743 } 11744 11745 static SDValue PerformADDVecReduce(SDNode *N, 11746 TargetLowering::DAGCombinerInfo &DCI, 11747 const ARMSubtarget *Subtarget) { 11748 if (!Subtarget->hasMVEIntegerOps() || N->getValueType(0) != MVT::i64) 11749 return SDValue(); 11750 11751 SDValue N0 = N->getOperand(0); 11752 SDValue N1 = N->getOperand(1); 11753 11754 // We are looking for a i64 add of a VADDLVx. Due to these being i64's, this 11755 // will look like: 11756 // t1: i32,i32 = ARMISD::VADDLVs x 11757 // t2: i64 = build_pair t1, t1:1 11758 // t3: i64 = add t2, y 11759 // We also need to check for sext / zext and commutitive adds. 11760 auto MakeVecReduce = [&](unsigned Opcode, unsigned OpcodeA, SDValue NA, 11761 SDValue NB) { 11762 if (NB->getOpcode() != ISD::BUILD_PAIR) 11763 return SDValue(); 11764 SDValue VecRed = NB->getOperand(0); 11765 if (VecRed->getOpcode() != Opcode || VecRed.getResNo() != 0 || 11766 NB->getOperand(1) != SDValue(VecRed.getNode(), 1)) 11767 return SDValue(); 11768 11769 SDLoc dl(N); 11770 SDValue Lo = DCI.DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, NA, 11771 DCI.DAG.getConstant(0, dl, MVT::i32)); 11772 SDValue Hi = DCI.DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, NA, 11773 DCI.DAG.getConstant(1, dl, MVT::i32)); 11774 SDValue Red = 11775 VecRed->getNumOperands() == 1 11776 ? DCI.DAG.getNode(OpcodeA, dl, 11777 DCI.DAG.getVTList({MVT::i32, MVT::i32}), Lo, Hi, 11778 VecRed->getOperand(0)) 11779 : DCI.DAG.getNode(OpcodeA, dl, 11780 DCI.DAG.getVTList({MVT::i32, MVT::i32}), Lo, Hi, 11781 VecRed->getOperand(0), VecRed->getOperand(1)); 11782 return DCI.DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Red, 11783 SDValue(Red.getNode(), 1)); 11784 }; 11785 11786 if (SDValue M = MakeVecReduce(ARMISD::VADDLVs, ARMISD::VADDLVAs, N0, N1)) 11787 return M; 11788 if (SDValue M = MakeVecReduce(ARMISD::VADDLVu, ARMISD::VADDLVAu, N0, N1)) 11789 return M; 11790 if (SDValue M = MakeVecReduce(ARMISD::VADDLVs, ARMISD::VADDLVAs, N1, N0)) 11791 return M; 11792 if (SDValue M = MakeVecReduce(ARMISD::VADDLVu, ARMISD::VADDLVAu, N1, N0)) 11793 return M; 11794 if (SDValue M = MakeVecReduce(ARMISD::VMLALVs, ARMISD::VMLALVAs, N0, N1)) 11795 return M; 11796 if (SDValue M = MakeVecReduce(ARMISD::VMLALVu, ARMISD::VMLALVAu, N0, N1)) 11797 return M; 11798 if (SDValue M = MakeVecReduce(ARMISD::VMLALVs, ARMISD::VMLALVAs, N1, N0)) 11799 return M; 11800 if (SDValue M = MakeVecReduce(ARMISD::VMLALVu, ARMISD::VMLALVAu, N1, N0)) 11801 return M; 11802 return SDValue(); 11803 } 11804 11805 bool 11806 ARMTargetLowering::isDesirableToCommuteWithShift(const SDNode *N, 11807 CombineLevel Level) const { 11808 if (Level == BeforeLegalizeTypes) 11809 return true; 11810 11811 if (N->getOpcode() != ISD::SHL) 11812 return true; 11813 11814 if (Subtarget->isThumb1Only()) { 11815 // Avoid making expensive immediates by commuting shifts. (This logic 11816 // only applies to Thumb1 because ARM and Thumb2 immediates can be shifted 11817 // for free.) 11818 if (N->getOpcode() != ISD::SHL) 11819 return true; 11820 SDValue N1 = N->getOperand(0); 11821 if (N1->getOpcode() != ISD::ADD && N1->getOpcode() != ISD::AND && 11822 N1->getOpcode() != ISD::OR && N1->getOpcode() != ISD::XOR) 11823 return true; 11824 if (auto *Const = dyn_cast<ConstantSDNode>(N1->getOperand(1))) { 11825 if (Const->getAPIntValue().ult(256)) 11826 return false; 11827 if (N1->getOpcode() == ISD::ADD && Const->getAPIntValue().slt(0) && 11828 Const->getAPIntValue().sgt(-256)) 11829 return false; 11830 } 11831 return true; 11832 } 11833 11834 // Turn off commute-with-shift transform after legalization, so it doesn't 11835 // conflict with PerformSHLSimplify. (We could try to detect when 11836 // PerformSHLSimplify would trigger more precisely, but it isn't 11837 // really necessary.) 11838 return false; 11839 } 11840 11841 bool ARMTargetLowering::shouldFoldConstantShiftPairToMask( 11842 const SDNode *N, CombineLevel Level) const { 11843 if (!Subtarget->isThumb1Only()) 11844 return true; 11845 11846 if (Level == BeforeLegalizeTypes) 11847 return true; 11848 11849 return false; 11850 } 11851 11852 bool ARMTargetLowering::preferIncOfAddToSubOfNot(EVT VT) const { 11853 if (!Subtarget->hasNEON()) { 11854 if (Subtarget->isThumb1Only()) 11855 return VT.getScalarSizeInBits() <= 32; 11856 return true; 11857 } 11858 return VT.isScalarInteger(); 11859 } 11860 11861 static SDValue PerformSHLSimplify(SDNode *N, 11862 TargetLowering::DAGCombinerInfo &DCI, 11863 const ARMSubtarget *ST) { 11864 // Allow the generic combiner to identify potential bswaps. 11865 if (DCI.isBeforeLegalize()) 11866 return SDValue(); 11867 11868 // DAG combiner will fold: 11869 // (shl (add x, c1), c2) -> (add (shl x, c2), c1 << c2) 11870 // (shl (or x, c1), c2) -> (or (shl x, c2), c1 << c2 11871 // Other code patterns that can be also be modified have the following form: 11872 // b + ((a << 1) | 510) 11873 // b + ((a << 1) & 510) 11874 // b + ((a << 1) ^ 510) 11875 // b + ((a << 1) + 510) 11876 11877 // Many instructions can perform the shift for free, but it requires both 11878 // the operands to be registers. If c1 << c2 is too large, a mov immediate 11879 // instruction will needed. So, unfold back to the original pattern if: 11880 // - if c1 and c2 are small enough that they don't require mov imms. 11881 // - the user(s) of the node can perform an shl 11882 11883 // No shifted operands for 16-bit instructions. 11884 if (ST->isThumb() && ST->isThumb1Only()) 11885 return SDValue(); 11886 11887 // Check that all the users could perform the shl themselves. 11888 for (auto U : N->uses()) { 11889 switch(U->getOpcode()) { 11890 default: 11891 return SDValue(); 11892 case ISD::SUB: 11893 case ISD::ADD: 11894 case ISD::AND: 11895 case ISD::OR: 11896 case ISD::XOR: 11897 case ISD::SETCC: 11898 case ARMISD::CMP: 11899 // Check that the user isn't already using a constant because there 11900 // aren't any instructions that support an immediate operand and a 11901 // shifted operand. 11902 if (isa<ConstantSDNode>(U->getOperand(0)) || 11903 isa<ConstantSDNode>(U->getOperand(1))) 11904 return SDValue(); 11905 11906 // Check that it's not already using a shift. 11907 if (U->getOperand(0).getOpcode() == ISD::SHL || 11908 U->getOperand(1).getOpcode() == ISD::SHL) 11909 return SDValue(); 11910 break; 11911 } 11912 } 11913 11914 if (N->getOpcode() != ISD::ADD && N->getOpcode() != ISD::OR && 11915 N->getOpcode() != ISD::XOR && N->getOpcode() != ISD::AND) 11916 return SDValue(); 11917 11918 if (N->getOperand(0).getOpcode() != ISD::SHL) 11919 return SDValue(); 11920 11921 SDValue SHL = N->getOperand(0); 11922 11923 auto *C1ShlC2 = dyn_cast<ConstantSDNode>(N->getOperand(1)); 11924 auto *C2 = dyn_cast<ConstantSDNode>(SHL.getOperand(1)); 11925 if (!C1ShlC2 || !C2) 11926 return SDValue(); 11927 11928 APInt C2Int = C2->getAPIntValue(); 11929 APInt C1Int = C1ShlC2->getAPIntValue(); 11930 11931 // Check that performing a lshr will not lose any information. 11932 APInt Mask = APInt::getHighBitsSet(C2Int.getBitWidth(), 11933 C2Int.getBitWidth() - C2->getZExtValue()); 11934 if ((C1Int & Mask) != C1Int) 11935 return SDValue(); 11936 11937 // Shift the first constant. 11938 C1Int.lshrInPlace(C2Int); 11939 11940 // The immediates are encoded as an 8-bit value that can be rotated. 11941 auto LargeImm = [](const APInt &Imm) { 11942 unsigned Zeros = Imm.countLeadingZeros() + Imm.countTrailingZeros(); 11943 return Imm.getBitWidth() - Zeros > 8; 11944 }; 11945 11946 if (LargeImm(C1Int) || LargeImm(C2Int)) 11947 return SDValue(); 11948 11949 SelectionDAG &DAG = DCI.DAG; 11950 SDLoc dl(N); 11951 SDValue X = SHL.getOperand(0); 11952 SDValue BinOp = DAG.getNode(N->getOpcode(), dl, MVT::i32, X, 11953 DAG.getConstant(C1Int, dl, MVT::i32)); 11954 // Shift left to compensate for the lshr of C1Int. 11955 SDValue Res = DAG.getNode(ISD::SHL, dl, MVT::i32, BinOp, SHL.getOperand(1)); 11956 11957 LLVM_DEBUG(dbgs() << "Simplify shl use:\n"; SHL.getOperand(0).dump(); 11958 SHL.dump(); N->dump()); 11959 LLVM_DEBUG(dbgs() << "Into:\n"; X.dump(); BinOp.dump(); Res.dump()); 11960 return Res; 11961 } 11962 11963 11964 /// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD. 11965 /// 11966 static SDValue PerformADDCombine(SDNode *N, 11967 TargetLowering::DAGCombinerInfo &DCI, 11968 const ARMSubtarget *Subtarget) { 11969 SDValue N0 = N->getOperand(0); 11970 SDValue N1 = N->getOperand(1); 11971 11972 // Only works one way, because it needs an immediate operand. 11973 if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget)) 11974 return Result; 11975 11976 if (SDValue Result = PerformADDVecReduce(N, DCI, Subtarget)) 11977 return Result; 11978 11979 // First try with the default operand order. 11980 if (SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget)) 11981 return Result; 11982 11983 // If that didn't work, try again with the operands commuted. 11984 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget); 11985 } 11986 11987 /// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB. 11988 /// 11989 static SDValue PerformSUBCombine(SDNode *N, 11990 TargetLowering::DAGCombinerInfo &DCI, 11991 const ARMSubtarget *Subtarget) { 11992 SDValue N0 = N->getOperand(0); 11993 SDValue N1 = N->getOperand(1); 11994 11995 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 11996 if (N1.getNode()->hasOneUse()) 11997 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI)) 11998 return Result; 11999 12000 if (!Subtarget->hasMVEIntegerOps() || !N->getValueType(0).isVector()) 12001 return SDValue(); 12002 12003 // Fold (sub (ARMvmovImm 0), (ARMvdup x)) -> (ARMvdup (sub 0, x)) 12004 // so that we can readily pattern match more mve instructions which can use 12005 // a scalar operand. 12006 SDValue VDup = N->getOperand(1); 12007 if (VDup->getOpcode() != ARMISD::VDUP) 12008 return SDValue(); 12009 12010 SDValue VMov = N->getOperand(0); 12011 if (VMov->getOpcode() == ISD::BITCAST) 12012 VMov = VMov->getOperand(0); 12013 12014 if (VMov->getOpcode() != ARMISD::VMOVIMM || !isZeroVector(VMov)) 12015 return SDValue(); 12016 12017 SDLoc dl(N); 12018 SDValue Negate = DCI.DAG.getNode(ISD::SUB, dl, MVT::i32, 12019 DCI.DAG.getConstant(0, dl, MVT::i32), 12020 VDup->getOperand(0)); 12021 return DCI.DAG.getNode(ARMISD::VDUP, dl, N->getValueType(0), Negate); 12022 } 12023 12024 /// PerformVMULCombine 12025 /// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the 12026 /// special multiplier accumulator forwarding. 12027 /// vmul d3, d0, d2 12028 /// vmla d3, d1, d2 12029 /// is faster than 12030 /// vadd d3, d0, d1 12031 /// vmul d3, d3, d2 12032 // However, for (A + B) * (A + B), 12033 // vadd d2, d0, d1 12034 // vmul d3, d0, d2 12035 // vmla d3, d1, d2 12036 // is slower than 12037 // vadd d2, d0, d1 12038 // vmul d3, d2, d2 12039 static SDValue PerformVMULCombine(SDNode *N, 12040 TargetLowering::DAGCombinerInfo &DCI, 12041 const ARMSubtarget *Subtarget) { 12042 if (!Subtarget->hasVMLxForwarding()) 12043 return SDValue(); 12044 12045 SelectionDAG &DAG = DCI.DAG; 12046 SDValue N0 = N->getOperand(0); 12047 SDValue N1 = N->getOperand(1); 12048 unsigned Opcode = N0.getOpcode(); 12049 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 12050 Opcode != ISD::FADD && Opcode != ISD::FSUB) { 12051 Opcode = N1.getOpcode(); 12052 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 12053 Opcode != ISD::FADD && Opcode != ISD::FSUB) 12054 return SDValue(); 12055 std::swap(N0, N1); 12056 } 12057 12058 if (N0 == N1) 12059 return SDValue(); 12060 12061 EVT VT = N->getValueType(0); 12062 SDLoc DL(N); 12063 SDValue N00 = N0->getOperand(0); 12064 SDValue N01 = N0->getOperand(1); 12065 return DAG.getNode(Opcode, DL, VT, 12066 DAG.getNode(ISD::MUL, DL, VT, N00, N1), 12067 DAG.getNode(ISD::MUL, DL, VT, N01, N1)); 12068 } 12069 12070 static SDValue PerformMULCombine(SDNode *N, 12071 TargetLowering::DAGCombinerInfo &DCI, 12072 const ARMSubtarget *Subtarget) { 12073 SelectionDAG &DAG = DCI.DAG; 12074 12075 if (Subtarget->isThumb1Only()) 12076 return SDValue(); 12077 12078 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 12079 return SDValue(); 12080 12081 EVT VT = N->getValueType(0); 12082 if (VT.is64BitVector() || VT.is128BitVector()) 12083 return PerformVMULCombine(N, DCI, Subtarget); 12084 if (VT != MVT::i32) 12085 return SDValue(); 12086 12087 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 12088 if (!C) 12089 return SDValue(); 12090 12091 int64_t MulAmt = C->getSExtValue(); 12092 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt); 12093 12094 ShiftAmt = ShiftAmt & (32 - 1); 12095 SDValue V = N->getOperand(0); 12096 SDLoc DL(N); 12097 12098 SDValue Res; 12099 MulAmt >>= ShiftAmt; 12100 12101 if (MulAmt >= 0) { 12102 if (isPowerOf2_32(MulAmt - 1)) { 12103 // (mul x, 2^N + 1) => (add (shl x, N), x) 12104 Res = DAG.getNode(ISD::ADD, DL, VT, 12105 V, 12106 DAG.getNode(ISD::SHL, DL, VT, 12107 V, 12108 DAG.getConstant(Log2_32(MulAmt - 1), DL, 12109 MVT::i32))); 12110 } else if (isPowerOf2_32(MulAmt + 1)) { 12111 // (mul x, 2^N - 1) => (sub (shl x, N), x) 12112 Res = DAG.getNode(ISD::SUB, DL, VT, 12113 DAG.getNode(ISD::SHL, DL, VT, 12114 V, 12115 DAG.getConstant(Log2_32(MulAmt + 1), DL, 12116 MVT::i32)), 12117 V); 12118 } else 12119 return SDValue(); 12120 } else { 12121 uint64_t MulAmtAbs = -MulAmt; 12122 if (isPowerOf2_32(MulAmtAbs + 1)) { 12123 // (mul x, -(2^N - 1)) => (sub x, (shl x, N)) 12124 Res = DAG.getNode(ISD::SUB, DL, VT, 12125 V, 12126 DAG.getNode(ISD::SHL, DL, VT, 12127 V, 12128 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL, 12129 MVT::i32))); 12130 } else if (isPowerOf2_32(MulAmtAbs - 1)) { 12131 // (mul x, -(2^N + 1)) => - (add (shl x, N), x) 12132 Res = DAG.getNode(ISD::ADD, DL, VT, 12133 V, 12134 DAG.getNode(ISD::SHL, DL, VT, 12135 V, 12136 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL, 12137 MVT::i32))); 12138 Res = DAG.getNode(ISD::SUB, DL, VT, 12139 DAG.getConstant(0, DL, MVT::i32), Res); 12140 } else 12141 return SDValue(); 12142 } 12143 12144 if (ShiftAmt != 0) 12145 Res = DAG.getNode(ISD::SHL, DL, VT, 12146 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32)); 12147 12148 // Do not add new nodes to DAG combiner worklist. 12149 DCI.CombineTo(N, Res, false); 12150 return SDValue(); 12151 } 12152 12153 static SDValue CombineANDShift(SDNode *N, 12154 TargetLowering::DAGCombinerInfo &DCI, 12155 const ARMSubtarget *Subtarget) { 12156 // Allow DAGCombine to pattern-match before we touch the canonical form. 12157 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 12158 return SDValue(); 12159 12160 if (N->getValueType(0) != MVT::i32) 12161 return SDValue(); 12162 12163 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 12164 if (!N1C) 12165 return SDValue(); 12166 12167 uint32_t C1 = (uint32_t)N1C->getZExtValue(); 12168 // Don't transform uxtb/uxth. 12169 if (C1 == 255 || C1 == 65535) 12170 return SDValue(); 12171 12172 SDNode *N0 = N->getOperand(0).getNode(); 12173 if (!N0->hasOneUse()) 12174 return SDValue(); 12175 12176 if (N0->getOpcode() != ISD::SHL && N0->getOpcode() != ISD::SRL) 12177 return SDValue(); 12178 12179 bool LeftShift = N0->getOpcode() == ISD::SHL; 12180 12181 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0->getOperand(1)); 12182 if (!N01C) 12183 return SDValue(); 12184 12185 uint32_t C2 = (uint32_t)N01C->getZExtValue(); 12186 if (!C2 || C2 >= 32) 12187 return SDValue(); 12188 12189 // Clear irrelevant bits in the mask. 12190 if (LeftShift) 12191 C1 &= (-1U << C2); 12192 else 12193 C1 &= (-1U >> C2); 12194 12195 SelectionDAG &DAG = DCI.DAG; 12196 SDLoc DL(N); 12197 12198 // We have a pattern of the form "(and (shl x, c2) c1)" or 12199 // "(and (srl x, c2) c1)", where c1 is a shifted mask. Try to 12200 // transform to a pair of shifts, to save materializing c1. 12201 12202 // First pattern: right shift, then mask off leading bits. 12203 // FIXME: Use demanded bits? 12204 if (!LeftShift && isMask_32(C1)) { 12205 uint32_t C3 = countLeadingZeros(C1); 12206 if (C2 < C3) { 12207 SDValue SHL = DAG.getNode(ISD::SHL, DL, MVT::i32, N0->getOperand(0), 12208 DAG.getConstant(C3 - C2, DL, MVT::i32)); 12209 return DAG.getNode(ISD::SRL, DL, MVT::i32, SHL, 12210 DAG.getConstant(C3, DL, MVT::i32)); 12211 } 12212 } 12213 12214 // First pattern, reversed: left shift, then mask off trailing bits. 12215 if (LeftShift && isMask_32(~C1)) { 12216 uint32_t C3 = countTrailingZeros(C1); 12217 if (C2 < C3) { 12218 SDValue SHL = DAG.getNode(ISD::SRL, DL, MVT::i32, N0->getOperand(0), 12219 DAG.getConstant(C3 - C2, DL, MVT::i32)); 12220 return DAG.getNode(ISD::SHL, DL, MVT::i32, SHL, 12221 DAG.getConstant(C3, DL, MVT::i32)); 12222 } 12223 } 12224 12225 // Second pattern: left shift, then mask off leading bits. 12226 // FIXME: Use demanded bits? 12227 if (LeftShift && isShiftedMask_32(C1)) { 12228 uint32_t Trailing = countTrailingZeros(C1); 12229 uint32_t C3 = countLeadingZeros(C1); 12230 if (Trailing == C2 && C2 + C3 < 32) { 12231 SDValue SHL = DAG.getNode(ISD::SHL, DL, MVT::i32, N0->getOperand(0), 12232 DAG.getConstant(C2 + C3, DL, MVT::i32)); 12233 return DAG.getNode(ISD::SRL, DL, MVT::i32, SHL, 12234 DAG.getConstant(C3, DL, MVT::i32)); 12235 } 12236 } 12237 12238 // Second pattern, reversed: right shift, then mask off trailing bits. 12239 // FIXME: Handle other patterns of known/demanded bits. 12240 if (!LeftShift && isShiftedMask_32(C1)) { 12241 uint32_t Leading = countLeadingZeros(C1); 12242 uint32_t C3 = countTrailingZeros(C1); 12243 if (Leading == C2 && C2 + C3 < 32) { 12244 SDValue SHL = DAG.getNode(ISD::SRL, DL, MVT::i32, N0->getOperand(0), 12245 DAG.getConstant(C2 + C3, DL, MVT::i32)); 12246 return DAG.getNode(ISD::SHL, DL, MVT::i32, SHL, 12247 DAG.getConstant(C3, DL, MVT::i32)); 12248 } 12249 } 12250 12251 // FIXME: Transform "(and (shl x, c2) c1)" -> 12252 // "(shl (and x, c1>>c2), c2)" if "c1 >> c2" is a cheaper immediate than 12253 // c1. 12254 return SDValue(); 12255 } 12256 12257 static SDValue PerformANDCombine(SDNode *N, 12258 TargetLowering::DAGCombinerInfo &DCI, 12259 const ARMSubtarget *Subtarget) { 12260 // Attempt to use immediate-form VBIC 12261 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 12262 SDLoc dl(N); 12263 EVT VT = N->getValueType(0); 12264 SelectionDAG &DAG = DCI.DAG; 12265 12266 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 12267 return SDValue(); 12268 12269 APInt SplatBits, SplatUndef; 12270 unsigned SplatBitSize; 12271 bool HasAnyUndefs; 12272 if (BVN && (Subtarget->hasNEON() || Subtarget->hasMVEIntegerOps()) && 12273 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 12274 if (SplatBitSize <= 64) { 12275 EVT VbicVT; 12276 SDValue Val = isVMOVModifiedImm((~SplatBits).getZExtValue(), 12277 SplatUndef.getZExtValue(), SplatBitSize, 12278 DAG, dl, VbicVT, VT.is128BitVector(), 12279 OtherModImm); 12280 if (Val.getNode()) { 12281 SDValue Input = 12282 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0)); 12283 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val); 12284 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic); 12285 } 12286 } 12287 } 12288 12289 if (!Subtarget->isThumb1Only()) { 12290 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) 12291 if (SDValue Result = combineSelectAndUseCommutative(N, true, DCI)) 12292 return Result; 12293 12294 if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget)) 12295 return Result; 12296 } 12297 12298 if (Subtarget->isThumb1Only()) 12299 if (SDValue Result = CombineANDShift(N, DCI, Subtarget)) 12300 return Result; 12301 12302 return SDValue(); 12303 } 12304 12305 // Try combining OR nodes to SMULWB, SMULWT. 12306 static SDValue PerformORCombineToSMULWBT(SDNode *OR, 12307 TargetLowering::DAGCombinerInfo &DCI, 12308 const ARMSubtarget *Subtarget) { 12309 if (!Subtarget->hasV6Ops() || 12310 (Subtarget->isThumb() && 12311 (!Subtarget->hasThumb2() || !Subtarget->hasDSP()))) 12312 return SDValue(); 12313 12314 SDValue SRL = OR->getOperand(0); 12315 SDValue SHL = OR->getOperand(1); 12316 12317 if (SRL.getOpcode() != ISD::SRL || SHL.getOpcode() != ISD::SHL) { 12318 SRL = OR->getOperand(1); 12319 SHL = OR->getOperand(0); 12320 } 12321 if (!isSRL16(SRL) || !isSHL16(SHL)) 12322 return SDValue(); 12323 12324 // The first operands to the shifts need to be the two results from the 12325 // same smul_lohi node. 12326 if ((SRL.getOperand(0).getNode() != SHL.getOperand(0).getNode()) || 12327 SRL.getOperand(0).getOpcode() != ISD::SMUL_LOHI) 12328 return SDValue(); 12329 12330 SDNode *SMULLOHI = SRL.getOperand(0).getNode(); 12331 if (SRL.getOperand(0) != SDValue(SMULLOHI, 0) || 12332 SHL.getOperand(0) != SDValue(SMULLOHI, 1)) 12333 return SDValue(); 12334 12335 // Now we have: 12336 // (or (srl (smul_lohi ?, ?), 16), (shl (smul_lohi ?, ?), 16))) 12337 // For SMUL[B|T] smul_lohi will take a 32-bit and a 16-bit arguments. 12338 // For SMUWB the 16-bit value will signed extended somehow. 12339 // For SMULWT only the SRA is required. 12340 // Check both sides of SMUL_LOHI 12341 SDValue OpS16 = SMULLOHI->getOperand(0); 12342 SDValue OpS32 = SMULLOHI->getOperand(1); 12343 12344 SelectionDAG &DAG = DCI.DAG; 12345 if (!isS16(OpS16, DAG) && !isSRA16(OpS16)) { 12346 OpS16 = OpS32; 12347 OpS32 = SMULLOHI->getOperand(0); 12348 } 12349 12350 SDLoc dl(OR); 12351 unsigned Opcode = 0; 12352 if (isS16(OpS16, DAG)) 12353 Opcode = ARMISD::SMULWB; 12354 else if (isSRA16(OpS16)) { 12355 Opcode = ARMISD::SMULWT; 12356 OpS16 = OpS16->getOperand(0); 12357 } 12358 else 12359 return SDValue(); 12360 12361 SDValue Res = DAG.getNode(Opcode, dl, MVT::i32, OpS32, OpS16); 12362 DAG.ReplaceAllUsesOfValueWith(SDValue(OR, 0), Res); 12363 return SDValue(OR, 0); 12364 } 12365 12366 static SDValue PerformORCombineToBFI(SDNode *N, 12367 TargetLowering::DAGCombinerInfo &DCI, 12368 const ARMSubtarget *Subtarget) { 12369 // BFI is only available on V6T2+ 12370 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops()) 12371 return SDValue(); 12372 12373 EVT VT = N->getValueType(0); 12374 SDValue N0 = N->getOperand(0); 12375 SDValue N1 = N->getOperand(1); 12376 SelectionDAG &DAG = DCI.DAG; 12377 SDLoc DL(N); 12378 // 1) or (and A, mask), val => ARMbfi A, val, mask 12379 // iff (val & mask) == val 12380 // 12381 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 12382 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2) 12383 // && mask == ~mask2 12384 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2) 12385 // && ~mask == mask2 12386 // (i.e., copy a bitfield value into another bitfield of the same width) 12387 12388 if (VT != MVT::i32) 12389 return SDValue(); 12390 12391 SDValue N00 = N0.getOperand(0); 12392 12393 // The value and the mask need to be constants so we can verify this is 12394 // actually a bitfield set. If the mask is 0xffff, we can do better 12395 // via a movt instruction, so don't use BFI in that case. 12396 SDValue MaskOp = N0.getOperand(1); 12397 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp); 12398 if (!MaskC) 12399 return SDValue(); 12400 unsigned Mask = MaskC->getZExtValue(); 12401 if (Mask == 0xffff) 12402 return SDValue(); 12403 SDValue Res; 12404 // Case (1): or (and A, mask), val => ARMbfi A, val, mask 12405 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); 12406 if (N1C) { 12407 unsigned Val = N1C->getZExtValue(); 12408 if ((Val & ~Mask) != Val) 12409 return SDValue(); 12410 12411 if (ARM::isBitFieldInvertedMask(Mask)) { 12412 Val >>= countTrailingZeros(~Mask); 12413 12414 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, 12415 DAG.getConstant(Val, DL, MVT::i32), 12416 DAG.getConstant(Mask, DL, MVT::i32)); 12417 12418 DCI.CombineTo(N, Res, false); 12419 // Return value from the original node to inform the combiner than N is 12420 // now dead. 12421 return SDValue(N, 0); 12422 } 12423 } else if (N1.getOpcode() == ISD::AND) { 12424 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 12425 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 12426 if (!N11C) 12427 return SDValue(); 12428 unsigned Mask2 = N11C->getZExtValue(); 12429 12430 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern 12431 // as is to match. 12432 if (ARM::isBitFieldInvertedMask(Mask) && 12433 (Mask == ~Mask2)) { 12434 // The pack halfword instruction works better for masks that fit it, 12435 // so use that when it's available. 12436 if (Subtarget->hasDSP() && 12437 (Mask == 0xffff || Mask == 0xffff0000)) 12438 return SDValue(); 12439 // 2a 12440 unsigned amt = countTrailingZeros(Mask2); 12441 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0), 12442 DAG.getConstant(amt, DL, MVT::i32)); 12443 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res, 12444 DAG.getConstant(Mask, DL, MVT::i32)); 12445 DCI.CombineTo(N, Res, false); 12446 // Return value from the original node to inform the combiner than N is 12447 // now dead. 12448 return SDValue(N, 0); 12449 } else if (ARM::isBitFieldInvertedMask(~Mask) && 12450 (~Mask == Mask2)) { 12451 // The pack halfword instruction works better for masks that fit it, 12452 // so use that when it's available. 12453 if (Subtarget->hasDSP() && 12454 (Mask2 == 0xffff || Mask2 == 0xffff0000)) 12455 return SDValue(); 12456 // 2b 12457 unsigned lsb = countTrailingZeros(Mask); 12458 Res = DAG.getNode(ISD::SRL, DL, VT, N00, 12459 DAG.getConstant(lsb, DL, MVT::i32)); 12460 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res, 12461 DAG.getConstant(Mask2, DL, MVT::i32)); 12462 DCI.CombineTo(N, Res, false); 12463 // Return value from the original node to inform the combiner than N is 12464 // now dead. 12465 return SDValue(N, 0); 12466 } 12467 } 12468 12469 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) && 12470 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) && 12471 ARM::isBitFieldInvertedMask(~Mask)) { 12472 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask 12473 // where lsb(mask) == #shamt and masked bits of B are known zero. 12474 SDValue ShAmt = N00.getOperand(1); 12475 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue(); 12476 unsigned LSB = countTrailingZeros(Mask); 12477 if (ShAmtC != LSB) 12478 return SDValue(); 12479 12480 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0), 12481 DAG.getConstant(~Mask, DL, MVT::i32)); 12482 12483 DCI.CombineTo(N, Res, false); 12484 // Return value from the original node to inform the combiner than N is 12485 // now dead. 12486 return SDValue(N, 0); 12487 } 12488 12489 return SDValue(); 12490 } 12491 12492 static bool isValidMVECond(unsigned CC, bool IsFloat) { 12493 switch (CC) { 12494 case ARMCC::EQ: 12495 case ARMCC::NE: 12496 case ARMCC::LE: 12497 case ARMCC::GT: 12498 case ARMCC::GE: 12499 case ARMCC::LT: 12500 return true; 12501 case ARMCC::HS: 12502 case ARMCC::HI: 12503 return !IsFloat; 12504 default: 12505 return false; 12506 }; 12507 } 12508 12509 static SDValue PerformORCombine_i1(SDNode *N, 12510 TargetLowering::DAGCombinerInfo &DCI, 12511 const ARMSubtarget *Subtarget) { 12512 // Try to invert "or A, B" -> "and ~A, ~B", as the "and" is easier to chain 12513 // together with predicates 12514 EVT VT = N->getValueType(0); 12515 SDValue N0 = N->getOperand(0); 12516 SDValue N1 = N->getOperand(1); 12517 12518 ARMCC::CondCodes CondCode0 = ARMCC::AL; 12519 ARMCC::CondCodes CondCode1 = ARMCC::AL; 12520 if (N0->getOpcode() == ARMISD::VCMP) 12521 CondCode0 = (ARMCC::CondCodes)cast<const ConstantSDNode>(N0->getOperand(2)) 12522 ->getZExtValue(); 12523 else if (N0->getOpcode() == ARMISD::VCMPZ) 12524 CondCode0 = (ARMCC::CondCodes)cast<const ConstantSDNode>(N0->getOperand(1)) 12525 ->getZExtValue(); 12526 if (N1->getOpcode() == ARMISD::VCMP) 12527 CondCode1 = (ARMCC::CondCodes)cast<const ConstantSDNode>(N1->getOperand(2)) 12528 ->getZExtValue(); 12529 else if (N1->getOpcode() == ARMISD::VCMPZ) 12530 CondCode1 = (ARMCC::CondCodes)cast<const ConstantSDNode>(N1->getOperand(1)) 12531 ->getZExtValue(); 12532 12533 if (CondCode0 == ARMCC::AL || CondCode1 == ARMCC::AL) 12534 return SDValue(); 12535 12536 unsigned Opposite0 = ARMCC::getOppositeCondition(CondCode0); 12537 unsigned Opposite1 = ARMCC::getOppositeCondition(CondCode1); 12538 12539 if (!isValidMVECond(Opposite0, 12540 N0->getOperand(0)->getValueType(0).isFloatingPoint()) || 12541 !isValidMVECond(Opposite1, 12542 N1->getOperand(0)->getValueType(0).isFloatingPoint())) 12543 return SDValue(); 12544 12545 SmallVector<SDValue, 4> Ops0; 12546 Ops0.push_back(N0->getOperand(0)); 12547 if (N0->getOpcode() == ARMISD::VCMP) 12548 Ops0.push_back(N0->getOperand(1)); 12549 Ops0.push_back(DCI.DAG.getConstant(Opposite0, SDLoc(N0), MVT::i32)); 12550 SmallVector<SDValue, 4> Ops1; 12551 Ops1.push_back(N1->getOperand(0)); 12552 if (N1->getOpcode() == ARMISD::VCMP) 12553 Ops1.push_back(N1->getOperand(1)); 12554 Ops1.push_back(DCI.DAG.getConstant(Opposite1, SDLoc(N1), MVT::i32)); 12555 12556 SDValue NewN0 = DCI.DAG.getNode(N0->getOpcode(), SDLoc(N0), VT, Ops0); 12557 SDValue NewN1 = DCI.DAG.getNode(N1->getOpcode(), SDLoc(N1), VT, Ops1); 12558 SDValue And = DCI.DAG.getNode(ISD::AND, SDLoc(N), VT, NewN0, NewN1); 12559 return DCI.DAG.getNode(ISD::XOR, SDLoc(N), VT, And, 12560 DCI.DAG.getAllOnesConstant(SDLoc(N), VT)); 12561 } 12562 12563 /// PerformORCombine - Target-specific dag combine xforms for ISD::OR 12564 static SDValue PerformORCombine(SDNode *N, 12565 TargetLowering::DAGCombinerInfo &DCI, 12566 const ARMSubtarget *Subtarget) { 12567 // Attempt to use immediate-form VORR 12568 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 12569 SDLoc dl(N); 12570 EVT VT = N->getValueType(0); 12571 SelectionDAG &DAG = DCI.DAG; 12572 12573 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 12574 return SDValue(); 12575 12576 APInt SplatBits, SplatUndef; 12577 unsigned SplatBitSize; 12578 bool HasAnyUndefs; 12579 if (BVN && (Subtarget->hasNEON() || Subtarget->hasMVEIntegerOps()) && 12580 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 12581 if (SplatBitSize <= 64) { 12582 EVT VorrVT; 12583 SDValue Val = isVMOVModifiedImm(SplatBits.getZExtValue(), 12584 SplatUndef.getZExtValue(), SplatBitSize, 12585 DAG, dl, VorrVT, VT.is128BitVector(), 12586 OtherModImm); 12587 if (Val.getNode()) { 12588 SDValue Input = 12589 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0)); 12590 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val); 12591 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr); 12592 } 12593 } 12594 } 12595 12596 if (!Subtarget->isThumb1Only()) { 12597 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c)) 12598 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI)) 12599 return Result; 12600 if (SDValue Result = PerformORCombineToSMULWBT(N, DCI, Subtarget)) 12601 return Result; 12602 } 12603 12604 SDValue N0 = N->getOperand(0); 12605 SDValue N1 = N->getOperand(1); 12606 12607 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant. 12608 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() && 12609 DAG.getTargetLoweringInfo().isTypeLegal(VT)) { 12610 12611 // The code below optimizes (or (and X, Y), Z). 12612 // The AND operand needs to have a single user to make these optimizations 12613 // profitable. 12614 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse()) 12615 return SDValue(); 12616 12617 APInt SplatUndef; 12618 unsigned SplatBitSize; 12619 bool HasAnyUndefs; 12620 12621 APInt SplatBits0, SplatBits1; 12622 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1)); 12623 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1)); 12624 // Ensure that the second operand of both ands are constants 12625 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize, 12626 HasAnyUndefs) && !HasAnyUndefs) { 12627 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize, 12628 HasAnyUndefs) && !HasAnyUndefs) { 12629 // Ensure that the bit width of the constants are the same and that 12630 // the splat arguments are logical inverses as per the pattern we 12631 // are trying to simplify. 12632 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() && 12633 SplatBits0 == ~SplatBits1) { 12634 // Canonicalize the vector type to make instruction selection 12635 // simpler. 12636 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 12637 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT, 12638 N0->getOperand(1), 12639 N0->getOperand(0), 12640 N1->getOperand(0)); 12641 return DAG.getNode(ISD::BITCAST, dl, VT, Result); 12642 } 12643 } 12644 } 12645 } 12646 12647 if (Subtarget->hasMVEIntegerOps() && 12648 (VT == MVT::v4i1 || VT == MVT::v8i1 || VT == MVT::v16i1)) 12649 return PerformORCombine_i1(N, DCI, Subtarget); 12650 12651 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when 12652 // reasonable. 12653 if (N0.getOpcode() == ISD::AND && N0.hasOneUse()) { 12654 if (SDValue Res = PerformORCombineToBFI(N, DCI, Subtarget)) 12655 return Res; 12656 } 12657 12658 if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget)) 12659 return Result; 12660 12661 return SDValue(); 12662 } 12663 12664 static SDValue PerformXORCombine(SDNode *N, 12665 TargetLowering::DAGCombinerInfo &DCI, 12666 const ARMSubtarget *Subtarget) { 12667 EVT VT = N->getValueType(0); 12668 SelectionDAG &DAG = DCI.DAG; 12669 12670 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 12671 return SDValue(); 12672 12673 if (!Subtarget->isThumb1Only()) { 12674 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c)) 12675 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI)) 12676 return Result; 12677 12678 if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget)) 12679 return Result; 12680 } 12681 12682 return SDValue(); 12683 } 12684 12685 // ParseBFI - given a BFI instruction in N, extract the "from" value (Rn) and return it, 12686 // and fill in FromMask and ToMask with (consecutive) bits in "from" to be extracted and 12687 // their position in "to" (Rd). 12688 static SDValue ParseBFI(SDNode *N, APInt &ToMask, APInt &FromMask) { 12689 assert(N->getOpcode() == ARMISD::BFI); 12690 12691 SDValue From = N->getOperand(1); 12692 ToMask = ~cast<ConstantSDNode>(N->getOperand(2))->getAPIntValue(); 12693 FromMask = APInt::getLowBitsSet(ToMask.getBitWidth(), ToMask.countPopulation()); 12694 12695 // If the Base came from a SHR #C, we can deduce that it is really testing bit 12696 // #C in the base of the SHR. 12697 if (From->getOpcode() == ISD::SRL && 12698 isa<ConstantSDNode>(From->getOperand(1))) { 12699 APInt Shift = cast<ConstantSDNode>(From->getOperand(1))->getAPIntValue(); 12700 assert(Shift.getLimitedValue() < 32 && "Shift too large!"); 12701 FromMask <<= Shift.getLimitedValue(31); 12702 From = From->getOperand(0); 12703 } 12704 12705 return From; 12706 } 12707 12708 // If A and B contain one contiguous set of bits, does A | B == A . B? 12709 // 12710 // Neither A nor B must be zero. 12711 static bool BitsProperlyConcatenate(const APInt &A, const APInt &B) { 12712 unsigned LastActiveBitInA = A.countTrailingZeros(); 12713 unsigned FirstActiveBitInB = B.getBitWidth() - B.countLeadingZeros() - 1; 12714 return LastActiveBitInA - 1 == FirstActiveBitInB; 12715 } 12716 12717 static SDValue FindBFIToCombineWith(SDNode *N) { 12718 // We have a BFI in N. Follow a possible chain of BFIs and find a BFI it can combine with, 12719 // if one exists. 12720 APInt ToMask, FromMask; 12721 SDValue From = ParseBFI(N, ToMask, FromMask); 12722 SDValue To = N->getOperand(0); 12723 12724 // Now check for a compatible BFI to merge with. We can pass through BFIs that 12725 // aren't compatible, but not if they set the same bit in their destination as 12726 // we do (or that of any BFI we're going to combine with). 12727 SDValue V = To; 12728 APInt CombinedToMask = ToMask; 12729 while (V.getOpcode() == ARMISD::BFI) { 12730 APInt NewToMask, NewFromMask; 12731 SDValue NewFrom = ParseBFI(V.getNode(), NewToMask, NewFromMask); 12732 if (NewFrom != From) { 12733 // This BFI has a different base. Keep going. 12734 CombinedToMask |= NewToMask; 12735 V = V.getOperand(0); 12736 continue; 12737 } 12738 12739 // Do the written bits conflict with any we've seen so far? 12740 if ((NewToMask & CombinedToMask).getBoolValue()) 12741 // Conflicting bits - bail out because going further is unsafe. 12742 return SDValue(); 12743 12744 // Are the new bits contiguous when combined with the old bits? 12745 if (BitsProperlyConcatenate(ToMask, NewToMask) && 12746 BitsProperlyConcatenate(FromMask, NewFromMask)) 12747 return V; 12748 if (BitsProperlyConcatenate(NewToMask, ToMask) && 12749 BitsProperlyConcatenate(NewFromMask, FromMask)) 12750 return V; 12751 12752 // We've seen a write to some bits, so track it. 12753 CombinedToMask |= NewToMask; 12754 // Keep going... 12755 V = V.getOperand(0); 12756 } 12757 12758 return SDValue(); 12759 } 12760 12761 static SDValue PerformBFICombine(SDNode *N, 12762 TargetLowering::DAGCombinerInfo &DCI) { 12763 SDValue N1 = N->getOperand(1); 12764 if (N1.getOpcode() == ISD::AND) { 12765 // (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff 12766 // the bits being cleared by the AND are not demanded by the BFI. 12767 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 12768 if (!N11C) 12769 return SDValue(); 12770 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue(); 12771 unsigned LSB = countTrailingZeros(~InvMask); 12772 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB; 12773 assert(Width < 12774 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) && 12775 "undefined behavior"); 12776 unsigned Mask = (1u << Width) - 1; 12777 unsigned Mask2 = N11C->getZExtValue(); 12778 if ((Mask & (~Mask2)) == 0) 12779 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0), 12780 N->getOperand(0), N1.getOperand(0), 12781 N->getOperand(2)); 12782 } else if (N->getOperand(0).getOpcode() == ARMISD::BFI) { 12783 // We have a BFI of a BFI. Walk up the BFI chain to see how long it goes. 12784 // Keep track of any consecutive bits set that all come from the same base 12785 // value. We can combine these together into a single BFI. 12786 SDValue CombineBFI = FindBFIToCombineWith(N); 12787 if (CombineBFI == SDValue()) 12788 return SDValue(); 12789 12790 // We've found a BFI. 12791 APInt ToMask1, FromMask1; 12792 SDValue From1 = ParseBFI(N, ToMask1, FromMask1); 12793 12794 APInt ToMask2, FromMask2; 12795 SDValue From2 = ParseBFI(CombineBFI.getNode(), ToMask2, FromMask2); 12796 assert(From1 == From2); 12797 (void)From2; 12798 12799 // First, unlink CombineBFI. 12800 DCI.DAG.ReplaceAllUsesWith(CombineBFI, CombineBFI.getOperand(0)); 12801 // Then create a new BFI, combining the two together. 12802 APInt NewFromMask = FromMask1 | FromMask2; 12803 APInt NewToMask = ToMask1 | ToMask2; 12804 12805 EVT VT = N->getValueType(0); 12806 SDLoc dl(N); 12807 12808 if (NewFromMask[0] == 0) 12809 From1 = DCI.DAG.getNode( 12810 ISD::SRL, dl, VT, From1, 12811 DCI.DAG.getConstant(NewFromMask.countTrailingZeros(), dl, VT)); 12812 return DCI.DAG.getNode(ARMISD::BFI, dl, VT, N->getOperand(0), From1, 12813 DCI.DAG.getConstant(~NewToMask, dl, VT)); 12814 } 12815 return SDValue(); 12816 } 12817 12818 /// PerformVMOVRRDCombine - Target-specific dag combine xforms for 12819 /// ARMISD::VMOVRRD. 12820 static SDValue PerformVMOVRRDCombine(SDNode *N, 12821 TargetLowering::DAGCombinerInfo &DCI, 12822 const ARMSubtarget *Subtarget) { 12823 // vmovrrd(vmovdrr x, y) -> x,y 12824 SDValue InDouble = N->getOperand(0); 12825 if (InDouble.getOpcode() == ARMISD::VMOVDRR && Subtarget->hasFP64()) 12826 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1)); 12827 12828 // vmovrrd(load f64) -> (load i32), (load i32) 12829 SDNode *InNode = InDouble.getNode(); 12830 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() && 12831 InNode->getValueType(0) == MVT::f64 && 12832 InNode->getOperand(1).getOpcode() == ISD::FrameIndex && 12833 !cast<LoadSDNode>(InNode)->isVolatile()) { 12834 // TODO: Should this be done for non-FrameIndex operands? 12835 LoadSDNode *LD = cast<LoadSDNode>(InNode); 12836 12837 SelectionDAG &DAG = DCI.DAG; 12838 SDLoc DL(LD); 12839 SDValue BasePtr = LD->getBasePtr(); 12840 SDValue NewLD1 = 12841 DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr, LD->getPointerInfo(), 12842 LD->getAlignment(), LD->getMemOperand()->getFlags()); 12843 12844 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 12845 DAG.getConstant(4, DL, MVT::i32)); 12846 12847 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, LD->getChain(), OffsetPtr, 12848 LD->getPointerInfo().getWithOffset(4), 12849 std::min(4U, LD->getAlignment()), 12850 LD->getMemOperand()->getFlags()); 12851 12852 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1)); 12853 if (DCI.DAG.getDataLayout().isBigEndian()) 12854 std::swap (NewLD1, NewLD2); 12855 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2); 12856 return Result; 12857 } 12858 12859 return SDValue(); 12860 } 12861 12862 /// PerformVMOVDRRCombine - Target-specific dag combine xforms for 12863 /// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands. 12864 static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) { 12865 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X) 12866 SDValue Op0 = N->getOperand(0); 12867 SDValue Op1 = N->getOperand(1); 12868 if (Op0.getOpcode() == ISD::BITCAST) 12869 Op0 = Op0.getOperand(0); 12870 if (Op1.getOpcode() == ISD::BITCAST) 12871 Op1 = Op1.getOperand(0); 12872 if (Op0.getOpcode() == ARMISD::VMOVRRD && 12873 Op0.getNode() == Op1.getNode() && 12874 Op0.getResNo() == 0 && Op1.getResNo() == 1) 12875 return DAG.getNode(ISD::BITCAST, SDLoc(N), 12876 N->getValueType(0), Op0.getOperand(0)); 12877 return SDValue(); 12878 } 12879 12880 /// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node 12881 /// are normal, non-volatile loads. If so, it is profitable to bitcast an 12882 /// i64 vector to have f64 elements, since the value can then be loaded 12883 /// directly into a VFP register. 12884 static bool hasNormalLoadOperand(SDNode *N) { 12885 unsigned NumElts = N->getValueType(0).getVectorNumElements(); 12886 for (unsigned i = 0; i < NumElts; ++i) { 12887 SDNode *Elt = N->getOperand(i).getNode(); 12888 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile()) 12889 return true; 12890 } 12891 return false; 12892 } 12893 12894 /// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for 12895 /// ISD::BUILD_VECTOR. 12896 static SDValue PerformBUILD_VECTORCombine(SDNode *N, 12897 TargetLowering::DAGCombinerInfo &DCI, 12898 const ARMSubtarget *Subtarget) { 12899 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X): 12900 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value 12901 // into a pair of GPRs, which is fine when the value is used as a scalar, 12902 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD. 12903 SelectionDAG &DAG = DCI.DAG; 12904 if (N->getNumOperands() == 2) 12905 if (SDValue RV = PerformVMOVDRRCombine(N, DAG)) 12906 return RV; 12907 12908 // Load i64 elements as f64 values so that type legalization does not split 12909 // them up into i32 values. 12910 EVT VT = N->getValueType(0); 12911 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N)) 12912 return SDValue(); 12913 SDLoc dl(N); 12914 SmallVector<SDValue, 8> Ops; 12915 unsigned NumElts = VT.getVectorNumElements(); 12916 for (unsigned i = 0; i < NumElts; ++i) { 12917 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i)); 12918 Ops.push_back(V); 12919 // Make the DAGCombiner fold the bitcast. 12920 DCI.AddToWorklist(V.getNode()); 12921 } 12922 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts); 12923 SDValue BV = DAG.getBuildVector(FloatVT, dl, Ops); 12924 return DAG.getNode(ISD::BITCAST, dl, VT, BV); 12925 } 12926 12927 /// Target-specific dag combine xforms for ARMISD::BUILD_VECTOR. 12928 static SDValue 12929 PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 12930 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR. 12931 // At that time, we may have inserted bitcasts from integer to float. 12932 // If these bitcasts have survived DAGCombine, change the lowering of this 12933 // BUILD_VECTOR in something more vector friendly, i.e., that does not 12934 // force to use floating point types. 12935 12936 // Make sure we can change the type of the vector. 12937 // This is possible iff: 12938 // 1. The vector is only used in a bitcast to a integer type. I.e., 12939 // 1.1. Vector is used only once. 12940 // 1.2. Use is a bit convert to an integer type. 12941 // 2. The size of its operands are 32-bits (64-bits are not legal). 12942 EVT VT = N->getValueType(0); 12943 EVT EltVT = VT.getVectorElementType(); 12944 12945 // Check 1.1. and 2. 12946 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse()) 12947 return SDValue(); 12948 12949 // By construction, the input type must be float. 12950 assert(EltVT == MVT::f32 && "Unexpected type!"); 12951 12952 // Check 1.2. 12953 SDNode *Use = *N->use_begin(); 12954 if (Use->getOpcode() != ISD::BITCAST || 12955 Use->getValueType(0).isFloatingPoint()) 12956 return SDValue(); 12957 12958 // Check profitability. 12959 // Model is, if more than half of the relevant operands are bitcast from 12960 // i32, turn the build_vector into a sequence of insert_vector_elt. 12961 // Relevant operands are everything that is not statically 12962 // (i.e., at compile time) bitcasted. 12963 unsigned NumOfBitCastedElts = 0; 12964 unsigned NumElts = VT.getVectorNumElements(); 12965 unsigned NumOfRelevantElts = NumElts; 12966 for (unsigned Idx = 0; Idx < NumElts; ++Idx) { 12967 SDValue Elt = N->getOperand(Idx); 12968 if (Elt->getOpcode() == ISD::BITCAST) { 12969 // Assume only bit cast to i32 will go away. 12970 if (Elt->getOperand(0).getValueType() == MVT::i32) 12971 ++NumOfBitCastedElts; 12972 } else if (Elt.isUndef() || isa<ConstantSDNode>(Elt)) 12973 // Constants are statically casted, thus do not count them as 12974 // relevant operands. 12975 --NumOfRelevantElts; 12976 } 12977 12978 // Check if more than half of the elements require a non-free bitcast. 12979 if (NumOfBitCastedElts <= NumOfRelevantElts / 2) 12980 return SDValue(); 12981 12982 SelectionDAG &DAG = DCI.DAG; 12983 // Create the new vector type. 12984 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); 12985 // Check if the type is legal. 12986 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 12987 if (!TLI.isTypeLegal(VecVT)) 12988 return SDValue(); 12989 12990 // Combine: 12991 // ARMISD::BUILD_VECTOR E1, E2, ..., EN. 12992 // => BITCAST INSERT_VECTOR_ELT 12993 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1), 12994 // (BITCAST EN), N. 12995 SDValue Vec = DAG.getUNDEF(VecVT); 12996 SDLoc dl(N); 12997 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) { 12998 SDValue V = N->getOperand(Idx); 12999 if (V.isUndef()) 13000 continue; 13001 if (V.getOpcode() == ISD::BITCAST && 13002 V->getOperand(0).getValueType() == MVT::i32) 13003 // Fold obvious case. 13004 V = V.getOperand(0); 13005 else { 13006 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V); 13007 // Make the DAGCombiner fold the bitcasts. 13008 DCI.AddToWorklist(V.getNode()); 13009 } 13010 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32); 13011 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx); 13012 } 13013 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec); 13014 // Make the DAGCombiner fold the bitcasts. 13015 DCI.AddToWorklist(Vec.getNode()); 13016 return Vec; 13017 } 13018 13019 static SDValue 13020 PerformPREDICATE_CASTCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 13021 EVT VT = N->getValueType(0); 13022 SDValue Op = N->getOperand(0); 13023 SDLoc dl(N); 13024 13025 // PREDICATE_CAST(PREDICATE_CAST(x)) == PREDICATE_CAST(x) 13026 if (Op->getOpcode() == ARMISD::PREDICATE_CAST) { 13027 // If the valuetypes are the same, we can remove the cast entirely. 13028 if (Op->getOperand(0).getValueType() == VT) 13029 return Op->getOperand(0); 13030 return DCI.DAG.getNode(ARMISD::PREDICATE_CAST, dl, 13031 Op->getOperand(0).getValueType(), Op->getOperand(0)); 13032 } 13033 13034 return SDValue(); 13035 } 13036 13037 static SDValue 13038 PerformVECTOR_REG_CASTCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, 13039 const ARMSubtarget *ST) { 13040 EVT VT = N->getValueType(0); 13041 SDValue Op = N->getOperand(0); 13042 SDLoc dl(N); 13043 13044 // Under Little endian, a VECTOR_REG_CAST is equivalent to a BITCAST 13045 if (ST->isLittle()) 13046 return DCI.DAG.getNode(ISD::BITCAST, dl, VT, Op); 13047 13048 // VECTOR_REG_CAST(VECTOR_REG_CAST(x)) == VECTOR_REG_CAST(x) 13049 if (Op->getOpcode() == ARMISD::VECTOR_REG_CAST) { 13050 // If the valuetypes are the same, we can remove the cast entirely. 13051 if (Op->getOperand(0).getValueType() == VT) 13052 return Op->getOperand(0); 13053 return DCI.DAG.getNode(ARMISD::VECTOR_REG_CAST, dl, VT, Op->getOperand(0)); 13054 } 13055 13056 return SDValue(); 13057 } 13058 13059 static SDValue PerformVCMPCombine(SDNode *N, 13060 TargetLowering::DAGCombinerInfo &DCI, 13061 const ARMSubtarget *Subtarget) { 13062 if (!Subtarget->hasMVEIntegerOps()) 13063 return SDValue(); 13064 13065 EVT VT = N->getValueType(0); 13066 SDValue Op0 = N->getOperand(0); 13067 SDValue Op1 = N->getOperand(1); 13068 ARMCC::CondCodes Cond = 13069 (ARMCC::CondCodes)cast<ConstantSDNode>(N->getOperand(2))->getZExtValue(); 13070 SDLoc dl(N); 13071 13072 // vcmp X, 0, cc -> vcmpz X, cc 13073 if (isZeroVector(Op1)) 13074 return DCI.DAG.getNode(ARMISD::VCMPZ, dl, VT, Op0, 13075 N->getOperand(2)); 13076 13077 unsigned SwappedCond = getSwappedCondition(Cond); 13078 if (isValidMVECond(SwappedCond, VT.isFloatingPoint())) { 13079 // vcmp 0, X, cc -> vcmpz X, reversed(cc) 13080 if (isZeroVector(Op0)) 13081 return DCI.DAG.getNode(ARMISD::VCMPZ, dl, VT, Op1, 13082 DCI.DAG.getConstant(SwappedCond, dl, MVT::i32)); 13083 // vcmp vdup(Y), X, cc -> vcmp X, vdup(Y), reversed(cc) 13084 if (Op0->getOpcode() == ARMISD::VDUP && Op1->getOpcode() != ARMISD::VDUP) 13085 return DCI.DAG.getNode(ARMISD::VCMP, dl, VT, Op1, Op0, 13086 DCI.DAG.getConstant(SwappedCond, dl, MVT::i32)); 13087 } 13088 13089 return SDValue(); 13090 } 13091 13092 /// PerformInsertEltCombine - Target-specific dag combine xforms for 13093 /// ISD::INSERT_VECTOR_ELT. 13094 static SDValue PerformInsertEltCombine(SDNode *N, 13095 TargetLowering::DAGCombinerInfo &DCI) { 13096 // Bitcast an i64 load inserted into a vector to f64. 13097 // Otherwise, the i64 value will be legalized to a pair of i32 values. 13098 EVT VT = N->getValueType(0); 13099 SDNode *Elt = N->getOperand(1).getNode(); 13100 if (VT.getVectorElementType() != MVT::i64 || 13101 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile()) 13102 return SDValue(); 13103 13104 SelectionDAG &DAG = DCI.DAG; 13105 SDLoc dl(N); 13106 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 13107 VT.getVectorNumElements()); 13108 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0)); 13109 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1)); 13110 // Make the DAGCombiner fold the bitcasts. 13111 DCI.AddToWorklist(Vec.getNode()); 13112 DCI.AddToWorklist(V.getNode()); 13113 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT, 13114 Vec, V, N->getOperand(2)); 13115 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt); 13116 } 13117 13118 /// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for 13119 /// ISD::VECTOR_SHUFFLE. 13120 static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) { 13121 // The LLVM shufflevector instruction does not require the shuffle mask 13122 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does 13123 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the 13124 // operands do not match the mask length, they are extended by concatenating 13125 // them with undef vectors. That is probably the right thing for other 13126 // targets, but for NEON it is better to concatenate two double-register 13127 // size vector operands into a single quad-register size vector. Do that 13128 // transformation here: 13129 // shuffle(concat(v1, undef), concat(v2, undef)) -> 13130 // shuffle(concat(v1, v2), undef) 13131 SDValue Op0 = N->getOperand(0); 13132 SDValue Op1 = N->getOperand(1); 13133 if (Op0.getOpcode() != ISD::CONCAT_VECTORS || 13134 Op1.getOpcode() != ISD::CONCAT_VECTORS || 13135 Op0.getNumOperands() != 2 || 13136 Op1.getNumOperands() != 2) 13137 return SDValue(); 13138 SDValue Concat0Op1 = Op0.getOperand(1); 13139 SDValue Concat1Op1 = Op1.getOperand(1); 13140 if (!Concat0Op1.isUndef() || !Concat1Op1.isUndef()) 13141 return SDValue(); 13142 // Skip the transformation if any of the types are illegal. 13143 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 13144 EVT VT = N->getValueType(0); 13145 if (!TLI.isTypeLegal(VT) || 13146 !TLI.isTypeLegal(Concat0Op1.getValueType()) || 13147 !TLI.isTypeLegal(Concat1Op1.getValueType())) 13148 return SDValue(); 13149 13150 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, 13151 Op0.getOperand(0), Op1.getOperand(0)); 13152 // Translate the shuffle mask. 13153 SmallVector<int, 16> NewMask; 13154 unsigned NumElts = VT.getVectorNumElements(); 13155 unsigned HalfElts = NumElts/2; 13156 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 13157 for (unsigned n = 0; n < NumElts; ++n) { 13158 int MaskElt = SVN->getMaskElt(n); 13159 int NewElt = -1; 13160 if (MaskElt < (int)HalfElts) 13161 NewElt = MaskElt; 13162 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts)) 13163 NewElt = HalfElts + MaskElt - NumElts; 13164 NewMask.push_back(NewElt); 13165 } 13166 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat, 13167 DAG.getUNDEF(VT), NewMask); 13168 } 13169 13170 /// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP, 13171 /// NEON load/store intrinsics, and generic vector load/stores, to merge 13172 /// base address updates. 13173 /// For generic load/stores, the memory type is assumed to be a vector. 13174 /// The caller is assumed to have checked legality. 13175 static SDValue CombineBaseUpdate(SDNode *N, 13176 TargetLowering::DAGCombinerInfo &DCI) { 13177 SelectionDAG &DAG = DCI.DAG; 13178 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID || 13179 N->getOpcode() == ISD::INTRINSIC_W_CHAIN); 13180 const bool isStore = N->getOpcode() == ISD::STORE; 13181 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1); 13182 SDValue Addr = N->getOperand(AddrOpIdx); 13183 MemSDNode *MemN = cast<MemSDNode>(N); 13184 SDLoc dl(N); 13185 13186 // Search for a use of the address operand that is an increment. 13187 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), 13188 UE = Addr.getNode()->use_end(); UI != UE; ++UI) { 13189 SDNode *User = *UI; 13190 if (User->getOpcode() != ISD::ADD || 13191 UI.getUse().getResNo() != Addr.getResNo()) 13192 continue; 13193 13194 // Check that the add is independent of the load/store. Otherwise, folding 13195 // it would create a cycle. We can avoid searching through Addr as it's a 13196 // predecessor to both. 13197 SmallPtrSet<const SDNode *, 32> Visited; 13198 SmallVector<const SDNode *, 16> Worklist; 13199 Visited.insert(Addr.getNode()); 13200 Worklist.push_back(N); 13201 Worklist.push_back(User); 13202 if (SDNode::hasPredecessorHelper(N, Visited, Worklist) || 13203 SDNode::hasPredecessorHelper(User, Visited, Worklist)) 13204 continue; 13205 13206 // Find the new opcode for the updating load/store. 13207 bool isLoadOp = true; 13208 bool isLaneOp = false; 13209 unsigned NewOpc = 0; 13210 unsigned NumVecs = 0; 13211 if (isIntrinsic) { 13212 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 13213 switch (IntNo) { 13214 default: llvm_unreachable("unexpected intrinsic for Neon base update"); 13215 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD; 13216 NumVecs = 1; break; 13217 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD; 13218 NumVecs = 2; break; 13219 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD; 13220 NumVecs = 3; break; 13221 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD; 13222 NumVecs = 4; break; 13223 case Intrinsic::arm_neon_vld2dup: 13224 case Intrinsic::arm_neon_vld3dup: 13225 case Intrinsic::arm_neon_vld4dup: 13226 // TODO: Support updating VLDxDUP nodes. For now, we just skip 13227 // combining base updates for such intrinsics. 13228 continue; 13229 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD; 13230 NumVecs = 2; isLaneOp = true; break; 13231 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD; 13232 NumVecs = 3; isLaneOp = true; break; 13233 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD; 13234 NumVecs = 4; isLaneOp = true; break; 13235 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD; 13236 NumVecs = 1; isLoadOp = false; break; 13237 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD; 13238 NumVecs = 2; isLoadOp = false; break; 13239 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD; 13240 NumVecs = 3; isLoadOp = false; break; 13241 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD; 13242 NumVecs = 4; isLoadOp = false; break; 13243 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD; 13244 NumVecs = 2; isLoadOp = false; isLaneOp = true; break; 13245 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD; 13246 NumVecs = 3; isLoadOp = false; isLaneOp = true; break; 13247 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD; 13248 NumVecs = 4; isLoadOp = false; isLaneOp = true; break; 13249 } 13250 } else { 13251 isLaneOp = true; 13252 switch (N->getOpcode()) { 13253 default: llvm_unreachable("unexpected opcode for Neon base update"); 13254 case ARMISD::VLD1DUP: NewOpc = ARMISD::VLD1DUP_UPD; NumVecs = 1; break; 13255 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break; 13256 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break; 13257 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break; 13258 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD; 13259 NumVecs = 1; isLaneOp = false; break; 13260 case ISD::STORE: NewOpc = ARMISD::VST1_UPD; 13261 NumVecs = 1; isLaneOp = false; isLoadOp = false; break; 13262 } 13263 } 13264 13265 // Find the size of memory referenced by the load/store. 13266 EVT VecTy; 13267 if (isLoadOp) { 13268 VecTy = N->getValueType(0); 13269 } else if (isIntrinsic) { 13270 VecTy = N->getOperand(AddrOpIdx+1).getValueType(); 13271 } else { 13272 assert(isStore && "Node has to be a load, a store, or an intrinsic!"); 13273 VecTy = N->getOperand(1).getValueType(); 13274 } 13275 13276 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8; 13277 if (isLaneOp) 13278 NumBytes /= VecTy.getVectorNumElements(); 13279 13280 // If the increment is a constant, it must match the memory ref size. 13281 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0); 13282 ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode()); 13283 if (NumBytes >= 3 * 16 && (!CInc || CInc->getZExtValue() != NumBytes)) { 13284 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two 13285 // separate instructions that make it harder to use a non-constant update. 13286 continue; 13287 } 13288 13289 // OK, we found an ADD we can fold into the base update. 13290 // Now, create a _UPD node, taking care of not breaking alignment. 13291 13292 EVT AlignedVecTy = VecTy; 13293 unsigned Alignment = MemN->getAlignment(); 13294 13295 // If this is a less-than-standard-aligned load/store, change the type to 13296 // match the standard alignment. 13297 // The alignment is overlooked when selecting _UPD variants; and it's 13298 // easier to introduce bitcasts here than fix that. 13299 // There are 3 ways to get to this base-update combine: 13300 // - intrinsics: they are assumed to be properly aligned (to the standard 13301 // alignment of the memory type), so we don't need to do anything. 13302 // - ARMISD::VLDx nodes: they are only generated from the aforementioned 13303 // intrinsics, so, likewise, there's nothing to do. 13304 // - generic load/store instructions: the alignment is specified as an 13305 // explicit operand, rather than implicitly as the standard alignment 13306 // of the memory type (like the intrisics). We need to change the 13307 // memory type to match the explicit alignment. That way, we don't 13308 // generate non-standard-aligned ARMISD::VLDx nodes. 13309 if (isa<LSBaseSDNode>(N)) { 13310 if (Alignment == 0) 13311 Alignment = 1; 13312 if (Alignment < VecTy.getScalarSizeInBits() / 8) { 13313 MVT EltTy = MVT::getIntegerVT(Alignment * 8); 13314 assert(NumVecs == 1 && "Unexpected multi-element generic load/store."); 13315 assert(!isLaneOp && "Unexpected generic load/store lane."); 13316 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8); 13317 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts); 13318 } 13319 // Don't set an explicit alignment on regular load/stores that we want 13320 // to transform to VLD/VST 1_UPD nodes. 13321 // This matches the behavior of regular load/stores, which only get an 13322 // explicit alignment if the MMO alignment is larger than the standard 13323 // alignment of the memory type. 13324 // Intrinsics, however, always get an explicit alignment, set to the 13325 // alignment of the MMO. 13326 Alignment = 1; 13327 } 13328 13329 // Create the new updating load/store node. 13330 // First, create an SDVTList for the new updating node's results. 13331 EVT Tys[6]; 13332 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0); 13333 unsigned n; 13334 for (n = 0; n < NumResultVecs; ++n) 13335 Tys[n] = AlignedVecTy; 13336 Tys[n++] = MVT::i32; 13337 Tys[n] = MVT::Other; 13338 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2)); 13339 13340 // Then, gather the new node's operands. 13341 SmallVector<SDValue, 8> Ops; 13342 Ops.push_back(N->getOperand(0)); // incoming chain 13343 Ops.push_back(N->getOperand(AddrOpIdx)); 13344 Ops.push_back(Inc); 13345 13346 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) { 13347 // Try to match the intrinsic's signature 13348 Ops.push_back(StN->getValue()); 13349 } else { 13350 // Loads (and of course intrinsics) match the intrinsics' signature, 13351 // so just add all but the alignment operand. 13352 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i) 13353 Ops.push_back(N->getOperand(i)); 13354 } 13355 13356 // For all node types, the alignment operand is always the last one. 13357 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32)); 13358 13359 // If this is a non-standard-aligned STORE, the penultimate operand is the 13360 // stored value. Bitcast it to the aligned type. 13361 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) { 13362 SDValue &StVal = Ops[Ops.size()-2]; 13363 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal); 13364 } 13365 13366 EVT LoadVT = isLaneOp ? VecTy.getVectorElementType() : AlignedVecTy; 13367 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys, Ops, LoadVT, 13368 MemN->getMemOperand()); 13369 13370 // Update the uses. 13371 SmallVector<SDValue, 5> NewResults; 13372 for (unsigned i = 0; i < NumResultVecs; ++i) 13373 NewResults.push_back(SDValue(UpdN.getNode(), i)); 13374 13375 // If this is an non-standard-aligned LOAD, the first result is the loaded 13376 // value. Bitcast it to the expected result type. 13377 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) { 13378 SDValue &LdVal = NewResults[0]; 13379 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal); 13380 } 13381 13382 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain 13383 DCI.CombineTo(N, NewResults); 13384 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs)); 13385 13386 break; 13387 } 13388 return SDValue(); 13389 } 13390 13391 static SDValue PerformVLDCombine(SDNode *N, 13392 TargetLowering::DAGCombinerInfo &DCI) { 13393 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 13394 return SDValue(); 13395 13396 return CombineBaseUpdate(N, DCI); 13397 } 13398 13399 static SDValue PerformMVEVLDCombine(SDNode *N, 13400 TargetLowering::DAGCombinerInfo &DCI) { 13401 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 13402 return SDValue(); 13403 13404 SelectionDAG &DAG = DCI.DAG; 13405 SDValue Addr = N->getOperand(2); 13406 MemSDNode *MemN = cast<MemSDNode>(N); 13407 SDLoc dl(N); 13408 13409 // For the stores, where there are multiple intrinsics we only actually want 13410 // to post-inc the last of the them. 13411 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 13412 if (IntNo == Intrinsic::arm_mve_vst2q && 13413 cast<ConstantSDNode>(N->getOperand(5))->getZExtValue() != 1) 13414 return SDValue(); 13415 if (IntNo == Intrinsic::arm_mve_vst4q && 13416 cast<ConstantSDNode>(N->getOperand(7))->getZExtValue() != 3) 13417 return SDValue(); 13418 13419 // Search for a use of the address operand that is an increment. 13420 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), 13421 UE = Addr.getNode()->use_end(); 13422 UI != UE; ++UI) { 13423 SDNode *User = *UI; 13424 if (User->getOpcode() != ISD::ADD || 13425 UI.getUse().getResNo() != Addr.getResNo()) 13426 continue; 13427 13428 // Check that the add is independent of the load/store. Otherwise, folding 13429 // it would create a cycle. We can avoid searching through Addr as it's a 13430 // predecessor to both. 13431 SmallPtrSet<const SDNode *, 32> Visited; 13432 SmallVector<const SDNode *, 16> Worklist; 13433 Visited.insert(Addr.getNode()); 13434 Worklist.push_back(N); 13435 Worklist.push_back(User); 13436 if (SDNode::hasPredecessorHelper(N, Visited, Worklist) || 13437 SDNode::hasPredecessorHelper(User, Visited, Worklist)) 13438 continue; 13439 13440 // Find the new opcode for the updating load/store. 13441 bool isLoadOp = true; 13442 unsigned NewOpc = 0; 13443 unsigned NumVecs = 0; 13444 switch (IntNo) { 13445 default: 13446 llvm_unreachable("unexpected intrinsic for MVE VLDn combine"); 13447 case Intrinsic::arm_mve_vld2q: 13448 NewOpc = ARMISD::VLD2_UPD; 13449 NumVecs = 2; 13450 break; 13451 case Intrinsic::arm_mve_vld4q: 13452 NewOpc = ARMISD::VLD4_UPD; 13453 NumVecs = 4; 13454 break; 13455 case Intrinsic::arm_mve_vst2q: 13456 NewOpc = ARMISD::VST2_UPD; 13457 NumVecs = 2; 13458 isLoadOp = false; 13459 break; 13460 case Intrinsic::arm_mve_vst4q: 13461 NewOpc = ARMISD::VST4_UPD; 13462 NumVecs = 4; 13463 isLoadOp = false; 13464 break; 13465 } 13466 13467 // Find the size of memory referenced by the load/store. 13468 EVT VecTy; 13469 if (isLoadOp) { 13470 VecTy = N->getValueType(0); 13471 } else { 13472 VecTy = N->getOperand(3).getValueType(); 13473 } 13474 13475 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8; 13476 13477 // If the increment is a constant, it must match the memory ref size. 13478 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0); 13479 ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode()); 13480 if (!CInc || CInc->getZExtValue() != NumBytes) 13481 continue; 13482 13483 // Create the new updating load/store node. 13484 // First, create an SDVTList for the new updating node's results. 13485 EVT Tys[6]; 13486 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0); 13487 unsigned n; 13488 for (n = 0; n < NumResultVecs; ++n) 13489 Tys[n] = VecTy; 13490 Tys[n++] = MVT::i32; 13491 Tys[n] = MVT::Other; 13492 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs + 2)); 13493 13494 // Then, gather the new node's operands. 13495 SmallVector<SDValue, 8> Ops; 13496 Ops.push_back(N->getOperand(0)); // incoming chain 13497 Ops.push_back(N->getOperand(2)); // ptr 13498 Ops.push_back(Inc); 13499 13500 for (unsigned i = 3; i < N->getNumOperands(); ++i) 13501 Ops.push_back(N->getOperand(i)); 13502 13503 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys, Ops, VecTy, 13504 MemN->getMemOperand()); 13505 13506 // Update the uses. 13507 SmallVector<SDValue, 5> NewResults; 13508 for (unsigned i = 0; i < NumResultVecs; ++i) 13509 NewResults.push_back(SDValue(UpdN.getNode(), i)); 13510 13511 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs + 1)); // chain 13512 DCI.CombineTo(N, NewResults); 13513 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs)); 13514 13515 break; 13516 } 13517 13518 return SDValue(); 13519 } 13520 13521 /// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a 13522 /// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic 13523 /// are also VDUPLANEs. If so, combine them to a vldN-dup operation and 13524 /// return true. 13525 static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 13526 SelectionDAG &DAG = DCI.DAG; 13527 EVT VT = N->getValueType(0); 13528 // vldN-dup instructions only support 64-bit vectors for N > 1. 13529 if (!VT.is64BitVector()) 13530 return false; 13531 13532 // Check if the VDUPLANE operand is a vldN-dup intrinsic. 13533 SDNode *VLD = N->getOperand(0).getNode(); 13534 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN) 13535 return false; 13536 unsigned NumVecs = 0; 13537 unsigned NewOpc = 0; 13538 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue(); 13539 if (IntNo == Intrinsic::arm_neon_vld2lane) { 13540 NumVecs = 2; 13541 NewOpc = ARMISD::VLD2DUP; 13542 } else if (IntNo == Intrinsic::arm_neon_vld3lane) { 13543 NumVecs = 3; 13544 NewOpc = ARMISD::VLD3DUP; 13545 } else if (IntNo == Intrinsic::arm_neon_vld4lane) { 13546 NumVecs = 4; 13547 NewOpc = ARMISD::VLD4DUP; 13548 } else { 13549 return false; 13550 } 13551 13552 // First check that all the vldN-lane uses are VDUPLANEs and that the lane 13553 // numbers match the load. 13554 unsigned VLDLaneNo = 13555 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue(); 13556 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 13557 UI != UE; ++UI) { 13558 // Ignore uses of the chain result. 13559 if (UI.getUse().getResNo() == NumVecs) 13560 continue; 13561 SDNode *User = *UI; 13562 if (User->getOpcode() != ARMISD::VDUPLANE || 13563 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue()) 13564 return false; 13565 } 13566 13567 // Create the vldN-dup node. 13568 EVT Tys[5]; 13569 unsigned n; 13570 for (n = 0; n < NumVecs; ++n) 13571 Tys[n] = VT; 13572 Tys[n] = MVT::Other; 13573 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1)); 13574 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) }; 13575 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD); 13576 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys, 13577 Ops, VLDMemInt->getMemoryVT(), 13578 VLDMemInt->getMemOperand()); 13579 13580 // Update the uses. 13581 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 13582 UI != UE; ++UI) { 13583 unsigned ResNo = UI.getUse().getResNo(); 13584 // Ignore uses of the chain result. 13585 if (ResNo == NumVecs) 13586 continue; 13587 SDNode *User = *UI; 13588 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo)); 13589 } 13590 13591 // Now the vldN-lane intrinsic is dead except for its chain result. 13592 // Update uses of the chain. 13593 std::vector<SDValue> VLDDupResults; 13594 for (unsigned n = 0; n < NumVecs; ++n) 13595 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n)); 13596 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs)); 13597 DCI.CombineTo(VLD, VLDDupResults); 13598 13599 return true; 13600 } 13601 13602 /// PerformVDUPLANECombine - Target-specific dag combine xforms for 13603 /// ARMISD::VDUPLANE. 13604 static SDValue PerformVDUPLANECombine(SDNode *N, 13605 TargetLowering::DAGCombinerInfo &DCI) { 13606 SDValue Op = N->getOperand(0); 13607 13608 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses 13609 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation. 13610 if (CombineVLDDUP(N, DCI)) 13611 return SDValue(N, 0); 13612 13613 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is 13614 // redundant. Ignore bit_converts for now; element sizes are checked below. 13615 while (Op.getOpcode() == ISD::BITCAST) 13616 Op = Op.getOperand(0); 13617 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM) 13618 return SDValue(); 13619 13620 // Make sure the VMOV element size is not bigger than the VDUPLANE elements. 13621 unsigned EltSize = Op.getScalarValueSizeInBits(); 13622 // The canonical VMOV for a zero vector uses a 32-bit element size. 13623 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 13624 unsigned EltBits; 13625 if (ARM_AM::decodeVMOVModImm(Imm, EltBits) == 0) 13626 EltSize = 8; 13627 EVT VT = N->getValueType(0); 13628 if (EltSize > VT.getScalarSizeInBits()) 13629 return SDValue(); 13630 13631 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op); 13632 } 13633 13634 /// PerformVDUPCombine - Target-specific dag combine xforms for ARMISD::VDUP. 13635 static SDValue PerformVDUPCombine(SDNode *N, 13636 TargetLowering::DAGCombinerInfo &DCI, 13637 const ARMSubtarget *Subtarget) { 13638 SelectionDAG &DAG = DCI.DAG; 13639 SDValue Op = N->getOperand(0); 13640 13641 if (!Subtarget->hasNEON()) 13642 return SDValue(); 13643 13644 // Match VDUP(LOAD) -> VLD1DUP. 13645 // We match this pattern here rather than waiting for isel because the 13646 // transform is only legal for unindexed loads. 13647 LoadSDNode *LD = dyn_cast<LoadSDNode>(Op.getNode()); 13648 if (LD && Op.hasOneUse() && LD->isUnindexed() && 13649 LD->getMemoryVT() == N->getValueType(0).getVectorElementType()) { 13650 SDValue Ops[] = { LD->getOperand(0), LD->getOperand(1), 13651 DAG.getConstant(LD->getAlignment(), SDLoc(N), MVT::i32) }; 13652 SDVTList SDTys = DAG.getVTList(N->getValueType(0), MVT::Other); 13653 SDValue VLDDup = DAG.getMemIntrinsicNode(ARMISD::VLD1DUP, SDLoc(N), SDTys, 13654 Ops, LD->getMemoryVT(), 13655 LD->getMemOperand()); 13656 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), VLDDup.getValue(1)); 13657 return VLDDup; 13658 } 13659 13660 return SDValue(); 13661 } 13662 13663 static SDValue PerformLOADCombine(SDNode *N, 13664 TargetLowering::DAGCombinerInfo &DCI) { 13665 EVT VT = N->getValueType(0); 13666 13667 // If this is a legal vector load, try to combine it into a VLD1_UPD. 13668 if (ISD::isNormalLoad(N) && VT.isVector() && 13669 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT)) 13670 return CombineBaseUpdate(N, DCI); 13671 13672 return SDValue(); 13673 } 13674 13675 // Optimize trunc store (of multiple scalars) to shuffle and store. First, 13676 // pack all of the elements in one place. Next, store to memory in fewer 13677 // chunks. 13678 static SDValue PerformTruncatingStoreCombine(StoreSDNode *St, 13679 SelectionDAG &DAG) { 13680 SDValue StVal = St->getValue(); 13681 EVT VT = StVal.getValueType(); 13682 if (!St->isTruncatingStore() || !VT.isVector()) 13683 return SDValue(); 13684 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 13685 EVT StVT = St->getMemoryVT(); 13686 unsigned NumElems = VT.getVectorNumElements(); 13687 assert(StVT != VT && "Cannot truncate to the same type"); 13688 unsigned FromEltSz = VT.getScalarSizeInBits(); 13689 unsigned ToEltSz = StVT.getScalarSizeInBits(); 13690 13691 // From, To sizes and ElemCount must be pow of two 13692 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) 13693 return SDValue(); 13694 13695 // We are going to use the original vector elt for storing. 13696 // Accumulated smaller vector elements must be a multiple of the store size. 13697 if (0 != (NumElems * FromEltSz) % ToEltSz) 13698 return SDValue(); 13699 13700 unsigned SizeRatio = FromEltSz / ToEltSz; 13701 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits()); 13702 13703 // Create a type on which we perform the shuffle. 13704 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(), 13705 NumElems * SizeRatio); 13706 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits()); 13707 13708 SDLoc DL(St); 13709 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal); 13710 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1); 13711 for (unsigned i = 0; i < NumElems; ++i) 13712 ShuffleVec[i] = DAG.getDataLayout().isBigEndian() ? (i + 1) * SizeRatio - 1 13713 : i * SizeRatio; 13714 13715 // Can't shuffle using an illegal type. 13716 if (!TLI.isTypeLegal(WideVecVT)) 13717 return SDValue(); 13718 13719 SDValue Shuff = DAG.getVectorShuffle( 13720 WideVecVT, DL, WideVec, DAG.getUNDEF(WideVec.getValueType()), ShuffleVec); 13721 // At this point all of the data is stored at the bottom of the 13722 // register. We now need to save it to mem. 13723 13724 // Find the largest store unit 13725 MVT StoreType = MVT::i8; 13726 for (MVT Tp : MVT::integer_valuetypes()) { 13727 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz) 13728 StoreType = Tp; 13729 } 13730 // Didn't find a legal store type. 13731 if (!TLI.isTypeLegal(StoreType)) 13732 return SDValue(); 13733 13734 // Bitcast the original vector into a vector of store-size units 13735 EVT StoreVecVT = 13736 EVT::getVectorVT(*DAG.getContext(), StoreType, 13737 VT.getSizeInBits() / EVT(StoreType).getSizeInBits()); 13738 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits()); 13739 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff); 13740 SmallVector<SDValue, 8> Chains; 13741 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL, 13742 TLI.getPointerTy(DAG.getDataLayout())); 13743 SDValue BasePtr = St->getBasePtr(); 13744 13745 // Perform one or more big stores into memory. 13746 unsigned E = (ToEltSz * NumElems) / StoreType.getSizeInBits(); 13747 for (unsigned I = 0; I < E; I++) { 13748 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, StoreType, 13749 ShuffWide, DAG.getIntPtrConstant(I, DL)); 13750 SDValue Ch = 13751 DAG.getStore(St->getChain(), DL, SubVec, BasePtr, St->getPointerInfo(), 13752 St->getAlignment(), St->getMemOperand()->getFlags()); 13753 BasePtr = 13754 DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr, Increment); 13755 Chains.push_back(Ch); 13756 } 13757 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 13758 } 13759 13760 // Try taking a single vector store from an truncate (which would otherwise turn 13761 // into an expensive buildvector) and splitting it into a series of narrowing 13762 // stores. 13763 static SDValue PerformSplittingToNarrowingStores(StoreSDNode *St, 13764 SelectionDAG &DAG) { 13765 if (!St->isSimple() || St->isTruncatingStore() || !St->isUnindexed()) 13766 return SDValue(); 13767 SDValue Trunc = St->getValue(); 13768 if (Trunc->getOpcode() != ISD::TRUNCATE) 13769 return SDValue(); 13770 EVT FromVT = Trunc->getOperand(0).getValueType(); 13771 EVT ToVT = Trunc.getValueType(); 13772 if (!ToVT.isVector()) 13773 return SDValue(); 13774 assert(FromVT.getVectorNumElements() == ToVT.getVectorNumElements()); 13775 EVT ToEltVT = ToVT.getVectorElementType(); 13776 EVT FromEltVT = FromVT.getVectorElementType(); 13777 13778 unsigned NumElements = 0; 13779 if (FromEltVT == MVT::i32 && (ToEltVT == MVT::i16 || ToEltVT == MVT::i8)) 13780 NumElements = 4; 13781 if (FromEltVT == MVT::i16 && ToEltVT == MVT::i8) 13782 NumElements = 8; 13783 if (NumElements == 0 || FromVT.getVectorNumElements() == NumElements || 13784 FromVT.getVectorNumElements() % NumElements != 0) 13785 return SDValue(); 13786 13787 SDLoc DL(St); 13788 // Details about the old store 13789 SDValue Ch = St->getChain(); 13790 SDValue BasePtr = St->getBasePtr(); 13791 unsigned Alignment = St->getOriginalAlignment(); 13792 MachineMemOperand::Flags MMOFlags = St->getMemOperand()->getFlags(); 13793 AAMDNodes AAInfo = St->getAAInfo(); 13794 13795 EVT NewFromVT = EVT::getVectorVT(*DAG.getContext(), FromEltVT, NumElements); 13796 EVT NewToVT = EVT::getVectorVT(*DAG.getContext(), ToEltVT, NumElements); 13797 13798 SmallVector<SDValue, 4> Stores; 13799 for (unsigned i = 0; i < FromVT.getVectorNumElements() / NumElements; i++) { 13800 unsigned NewOffset = i * NumElements * ToEltVT.getSizeInBits() / 8; 13801 SDValue NewPtr = DAG.getObjectPtrOffset(DL, BasePtr, NewOffset); 13802 13803 SDValue Extract = 13804 DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, NewFromVT, Trunc.getOperand(0), 13805 DAG.getConstant(i * NumElements, DL, MVT::i32)); 13806 SDValue Store = DAG.getTruncStore( 13807 Ch, DL, Extract, NewPtr, St->getPointerInfo().getWithOffset(NewOffset), 13808 NewToVT, Alignment, MMOFlags, AAInfo); 13809 Stores.push_back(Store); 13810 } 13811 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores); 13812 } 13813 13814 /// PerformSTORECombine - Target-specific dag combine xforms for 13815 /// ISD::STORE. 13816 static SDValue PerformSTORECombine(SDNode *N, 13817 TargetLowering::DAGCombinerInfo &DCI, 13818 const ARMSubtarget *Subtarget) { 13819 StoreSDNode *St = cast<StoreSDNode>(N); 13820 if (St->isVolatile()) 13821 return SDValue(); 13822 SDValue StVal = St->getValue(); 13823 EVT VT = StVal.getValueType(); 13824 13825 if (Subtarget->hasNEON()) 13826 if (SDValue Store = PerformTruncatingStoreCombine(St, DCI.DAG)) 13827 return Store; 13828 13829 if (Subtarget->hasMVEIntegerOps()) 13830 if (SDValue NewToken = PerformSplittingToNarrowingStores(St, DCI.DAG)) 13831 return NewToken; 13832 13833 if (!ISD::isNormalStore(St)) 13834 return SDValue(); 13835 13836 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and 13837 // ARM stores of arguments in the same cache line. 13838 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR && 13839 StVal.getNode()->hasOneUse()) { 13840 SelectionDAG &DAG = DCI.DAG; 13841 bool isBigEndian = DAG.getDataLayout().isBigEndian(); 13842 SDLoc DL(St); 13843 SDValue BasePtr = St->getBasePtr(); 13844 SDValue NewST1 = DAG.getStore( 13845 St->getChain(), DL, StVal.getNode()->getOperand(isBigEndian ? 1 : 0), 13846 BasePtr, St->getPointerInfo(), St->getAlignment(), 13847 St->getMemOperand()->getFlags()); 13848 13849 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 13850 DAG.getConstant(4, DL, MVT::i32)); 13851 return DAG.getStore(NewST1.getValue(0), DL, 13852 StVal.getNode()->getOperand(isBigEndian ? 0 : 1), 13853 OffsetPtr, St->getPointerInfo(), 13854 std::min(4U, St->getAlignment() / 2), 13855 St->getMemOperand()->getFlags()); 13856 } 13857 13858 if (StVal.getValueType() == MVT::i64 && 13859 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 13860 13861 // Bitcast an i64 store extracted from a vector to f64. 13862 // Otherwise, the i64 value will be legalized to a pair of i32 values. 13863 SelectionDAG &DAG = DCI.DAG; 13864 SDLoc dl(StVal); 13865 SDValue IntVec = StVal.getOperand(0); 13866 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 13867 IntVec.getValueType().getVectorNumElements()); 13868 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec); 13869 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, 13870 Vec, StVal.getOperand(1)); 13871 dl = SDLoc(N); 13872 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt); 13873 // Make the DAGCombiner fold the bitcasts. 13874 DCI.AddToWorklist(Vec.getNode()); 13875 DCI.AddToWorklist(ExtElt.getNode()); 13876 DCI.AddToWorklist(V.getNode()); 13877 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(), 13878 St->getPointerInfo(), St->getAlignment(), 13879 St->getMemOperand()->getFlags(), St->getAAInfo()); 13880 } 13881 13882 // If this is a legal vector store, try to combine it into a VST1_UPD. 13883 if (Subtarget->hasNEON() && ISD::isNormalStore(N) && VT.isVector() && 13884 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT)) 13885 return CombineBaseUpdate(N, DCI); 13886 13887 return SDValue(); 13888 } 13889 13890 /// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD) 13891 /// can replace combinations of VMUL and VCVT (floating-point to integer) 13892 /// when the VMUL has a constant operand that is a power of 2. 13893 /// 13894 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 13895 /// vmul.f32 d16, d17, d16 13896 /// vcvt.s32.f32 d16, d16 13897 /// becomes: 13898 /// vcvt.s32.f32 d16, d16, #3 13899 static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG, 13900 const ARMSubtarget *Subtarget) { 13901 if (!Subtarget->hasNEON()) 13902 return SDValue(); 13903 13904 SDValue Op = N->getOperand(0); 13905 if (!Op.getValueType().isVector() || !Op.getValueType().isSimple() || 13906 Op.getOpcode() != ISD::FMUL) 13907 return SDValue(); 13908 13909 SDValue ConstVec = Op->getOperand(1); 13910 if (!isa<BuildVectorSDNode>(ConstVec)) 13911 return SDValue(); 13912 13913 MVT FloatTy = Op.getSimpleValueType().getVectorElementType(); 13914 uint32_t FloatBits = FloatTy.getSizeInBits(); 13915 MVT IntTy = N->getSimpleValueType(0).getVectorElementType(); 13916 uint32_t IntBits = IntTy.getSizeInBits(); 13917 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 13918 if (FloatBits != 32 || IntBits > 32 || (NumLanes != 4 && NumLanes != 2)) { 13919 // These instructions only exist converting from f32 to i32. We can handle 13920 // smaller integers by generating an extra truncate, but larger ones would 13921 // be lossy. We also can't handle anything other than 2 or 4 lanes, since 13922 // these intructions only support v2i32/v4i32 types. 13923 return SDValue(); 13924 } 13925 13926 BitVector UndefElements; 13927 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec); 13928 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33); 13929 if (C == -1 || C == 0 || C > 32) 13930 return SDValue(); 13931 13932 SDLoc dl(N); 13933 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT; 13934 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs : 13935 Intrinsic::arm_neon_vcvtfp2fxu; 13936 SDValue FixConv = DAG.getNode( 13937 ISD::INTRINSIC_WO_CHAIN, dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32, 13938 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), Op->getOperand(0), 13939 DAG.getConstant(C, dl, MVT::i32)); 13940 13941 if (IntBits < FloatBits) 13942 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv); 13943 13944 return FixConv; 13945 } 13946 13947 /// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD) 13948 /// can replace combinations of VCVT (integer to floating-point) and VDIV 13949 /// when the VDIV has a constant operand that is a power of 2. 13950 /// 13951 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 13952 /// vcvt.f32.s32 d16, d16 13953 /// vdiv.f32 d16, d17, d16 13954 /// becomes: 13955 /// vcvt.f32.s32 d16, d16, #3 13956 static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG, 13957 const ARMSubtarget *Subtarget) { 13958 if (!Subtarget->hasNEON()) 13959 return SDValue(); 13960 13961 SDValue Op = N->getOperand(0); 13962 unsigned OpOpcode = Op.getNode()->getOpcode(); 13963 if (!N->getValueType(0).isVector() || !N->getValueType(0).isSimple() || 13964 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP)) 13965 return SDValue(); 13966 13967 SDValue ConstVec = N->getOperand(1); 13968 if (!isa<BuildVectorSDNode>(ConstVec)) 13969 return SDValue(); 13970 13971 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType(); 13972 uint32_t FloatBits = FloatTy.getSizeInBits(); 13973 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType(); 13974 uint32_t IntBits = IntTy.getSizeInBits(); 13975 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 13976 if (FloatBits != 32 || IntBits > 32 || (NumLanes != 4 && NumLanes != 2)) { 13977 // These instructions only exist converting from i32 to f32. We can handle 13978 // smaller integers by generating an extra extend, but larger ones would 13979 // be lossy. We also can't handle anything other than 2 or 4 lanes, since 13980 // these intructions only support v2i32/v4i32 types. 13981 return SDValue(); 13982 } 13983 13984 BitVector UndefElements; 13985 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec); 13986 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33); 13987 if (C == -1 || C == 0 || C > 32) 13988 return SDValue(); 13989 13990 SDLoc dl(N); 13991 bool isSigned = OpOpcode == ISD::SINT_TO_FP; 13992 SDValue ConvInput = Op.getOperand(0); 13993 if (IntBits < FloatBits) 13994 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 13995 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32, 13996 ConvInput); 13997 13998 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp : 13999 Intrinsic::arm_neon_vcvtfxu2fp; 14000 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, 14001 Op.getValueType(), 14002 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), 14003 ConvInput, DAG.getConstant(C, dl, MVT::i32)); 14004 } 14005 14006 static SDValue PerformVECREDUCE_ADDCombine(SDNode *N, SelectionDAG &DAG, 14007 const ARMSubtarget *ST) { 14008 if (!ST->hasMVEIntegerOps()) 14009 return SDValue(); 14010 14011 assert(N->getOpcode() == ISD::VECREDUCE_ADD); 14012 EVT ResVT = N->getValueType(0); 14013 SDValue N0 = N->getOperand(0); 14014 SDLoc dl(N); 14015 14016 // We are looking for something that will have illegal types if left alone, 14017 // but that we can convert to a single instruction undef MVE. For example 14018 // vecreduce_add(sext(A, v8i32)) => VADDV.s16 A 14019 // or 14020 // vecreduce_add(mul(zext(A, v16i32), zext(B, v16i32))) => VMLADAV.u8 A, B 14021 14022 // Cases: 14023 // VADDV u/s 8/16/32 14024 // VMLAV u/s 8/16/32 14025 // VADDLV u/s 32 14026 // VMLALV u/s 16/32 14027 // TODOD: 14028 // VMLSV 14029 // VMLSLV 14030 14031 auto IsVADDV = [&](MVT RetTy, unsigned ExtendCode, ArrayRef<MVT> ExtTypes) { 14032 if (ResVT != RetTy || N0->getOpcode() != ExtendCode) 14033 return SDValue(); 14034 SDValue A = N0->getOperand(0); 14035 if (llvm::any_of(ExtTypes, [&A](MVT Ty) { return A.getValueType() == Ty; })) 14036 return A; 14037 return SDValue(); 14038 }; 14039 auto IsVMLAV = [&](MVT RetTy, unsigned ExtendCode, ArrayRef<MVT> ExtTypes, SDValue &A, SDValue &B) { 14040 if (ResVT != RetTy || N0->getOpcode() != ISD::MUL) 14041 return false; 14042 SDValue ExtA = N0->getOperand(0); 14043 SDValue ExtB = N0->getOperand(1); 14044 if (ExtA->getOpcode() != ExtendCode && ExtB->getOpcode() != ExtendCode) 14045 return false; 14046 A = ExtA->getOperand(0); 14047 B = ExtB->getOperand(0); 14048 if (A.getValueType() == B.getValueType() && 14049 llvm::any_of(ExtTypes, [&A](MVT Ty) { return A.getValueType() == Ty; })) 14050 return true; 14051 return false; 14052 }; 14053 auto Create64bitNode = [&](unsigned Opcode, ArrayRef<SDValue> Ops) { 14054 SDValue Node = DAG.getNode(Opcode, dl, {MVT::i32, MVT::i32}, Ops); 14055 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Node, SDValue(Node.getNode(), 1)); 14056 }; 14057 14058 if (SDValue A = IsVADDV(MVT::i32, ISD::SIGN_EXTEND, {MVT::v8i16, MVT::v16i8})) 14059 return DAG.getNode(ARMISD::VADDVs, dl, ResVT, A); 14060 if (SDValue A = IsVADDV(MVT::i32, ISD::ZERO_EXTEND, {MVT::v8i16, MVT::v16i8})) 14061 return DAG.getNode(ARMISD::VADDVu, dl, ResVT, A); 14062 if (SDValue A = IsVADDV(MVT::i64, ISD::SIGN_EXTEND, {MVT::v4i32})) 14063 return Create64bitNode(ARMISD::VADDLVs, {A}); 14064 if (SDValue A = IsVADDV(MVT::i64, ISD::ZERO_EXTEND, {MVT::v4i32})) 14065 return Create64bitNode(ARMISD::VADDLVu, {A}); 14066 14067 SDValue A, B; 14068 if (IsVMLAV(MVT::i32, ISD::SIGN_EXTEND, {MVT::v8i16, MVT::v16i8}, A, B)) 14069 return DAG.getNode(ARMISD::VMLAVs, dl, ResVT, A, B); 14070 if (IsVMLAV(MVT::i32, ISD::ZERO_EXTEND, {MVT::v8i16, MVT::v16i8}, A, B)) 14071 return DAG.getNode(ARMISD::VMLAVu, dl, ResVT, A, B); 14072 if (IsVMLAV(MVT::i64, ISD::SIGN_EXTEND, {MVT::v8i16, MVT::v4i32}, A, B)) 14073 return Create64bitNode(ARMISD::VMLALVs, {A, B}); 14074 if (IsVMLAV(MVT::i64, ISD::ZERO_EXTEND, {MVT::v8i16, MVT::v4i32}, A, B)) 14075 return Create64bitNode(ARMISD::VMLALVu, {A, B}); 14076 return SDValue(); 14077 } 14078 14079 /// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics. 14080 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) { 14081 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 14082 switch (IntNo) { 14083 default: 14084 // Don't do anything for most intrinsics. 14085 break; 14086 14087 // Vector shifts: check for immediate versions and lower them. 14088 // Note: This is done during DAG combining instead of DAG legalizing because 14089 // the build_vectors for 64-bit vector element shift counts are generally 14090 // not legal, and it is hard to see their values after they get legalized to 14091 // loads from a constant pool. 14092 case Intrinsic::arm_neon_vshifts: 14093 case Intrinsic::arm_neon_vshiftu: 14094 case Intrinsic::arm_neon_vrshifts: 14095 case Intrinsic::arm_neon_vrshiftu: 14096 case Intrinsic::arm_neon_vrshiftn: 14097 case Intrinsic::arm_neon_vqshifts: 14098 case Intrinsic::arm_neon_vqshiftu: 14099 case Intrinsic::arm_neon_vqshiftsu: 14100 case Intrinsic::arm_neon_vqshiftns: 14101 case Intrinsic::arm_neon_vqshiftnu: 14102 case Intrinsic::arm_neon_vqshiftnsu: 14103 case Intrinsic::arm_neon_vqrshiftns: 14104 case Intrinsic::arm_neon_vqrshiftnu: 14105 case Intrinsic::arm_neon_vqrshiftnsu: { 14106 EVT VT = N->getOperand(1).getValueType(); 14107 int64_t Cnt; 14108 unsigned VShiftOpc = 0; 14109 14110 switch (IntNo) { 14111 case Intrinsic::arm_neon_vshifts: 14112 case Intrinsic::arm_neon_vshiftu: 14113 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) { 14114 VShiftOpc = ARMISD::VSHLIMM; 14115 break; 14116 } 14117 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) { 14118 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ? ARMISD::VSHRsIMM 14119 : ARMISD::VSHRuIMM); 14120 break; 14121 } 14122 return SDValue(); 14123 14124 case Intrinsic::arm_neon_vrshifts: 14125 case Intrinsic::arm_neon_vrshiftu: 14126 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) 14127 break; 14128 return SDValue(); 14129 14130 case Intrinsic::arm_neon_vqshifts: 14131 case Intrinsic::arm_neon_vqshiftu: 14132 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 14133 break; 14134 return SDValue(); 14135 14136 case Intrinsic::arm_neon_vqshiftsu: 14137 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 14138 break; 14139 llvm_unreachable("invalid shift count for vqshlu intrinsic"); 14140 14141 case Intrinsic::arm_neon_vrshiftn: 14142 case Intrinsic::arm_neon_vqshiftns: 14143 case Intrinsic::arm_neon_vqshiftnu: 14144 case Intrinsic::arm_neon_vqshiftnsu: 14145 case Intrinsic::arm_neon_vqrshiftns: 14146 case Intrinsic::arm_neon_vqrshiftnu: 14147 case Intrinsic::arm_neon_vqrshiftnsu: 14148 // Narrowing shifts require an immediate right shift. 14149 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt)) 14150 break; 14151 llvm_unreachable("invalid shift count for narrowing vector shift " 14152 "intrinsic"); 14153 14154 default: 14155 llvm_unreachable("unhandled vector shift"); 14156 } 14157 14158 switch (IntNo) { 14159 case Intrinsic::arm_neon_vshifts: 14160 case Intrinsic::arm_neon_vshiftu: 14161 // Opcode already set above. 14162 break; 14163 case Intrinsic::arm_neon_vrshifts: 14164 VShiftOpc = ARMISD::VRSHRsIMM; 14165 break; 14166 case Intrinsic::arm_neon_vrshiftu: 14167 VShiftOpc = ARMISD::VRSHRuIMM; 14168 break; 14169 case Intrinsic::arm_neon_vrshiftn: 14170 VShiftOpc = ARMISD::VRSHRNIMM; 14171 break; 14172 case Intrinsic::arm_neon_vqshifts: 14173 VShiftOpc = ARMISD::VQSHLsIMM; 14174 break; 14175 case Intrinsic::arm_neon_vqshiftu: 14176 VShiftOpc = ARMISD::VQSHLuIMM; 14177 break; 14178 case Intrinsic::arm_neon_vqshiftsu: 14179 VShiftOpc = ARMISD::VQSHLsuIMM; 14180 break; 14181 case Intrinsic::arm_neon_vqshiftns: 14182 VShiftOpc = ARMISD::VQSHRNsIMM; 14183 break; 14184 case Intrinsic::arm_neon_vqshiftnu: 14185 VShiftOpc = ARMISD::VQSHRNuIMM; 14186 break; 14187 case Intrinsic::arm_neon_vqshiftnsu: 14188 VShiftOpc = ARMISD::VQSHRNsuIMM; 14189 break; 14190 case Intrinsic::arm_neon_vqrshiftns: 14191 VShiftOpc = ARMISD::VQRSHRNsIMM; 14192 break; 14193 case Intrinsic::arm_neon_vqrshiftnu: 14194 VShiftOpc = ARMISD::VQRSHRNuIMM; 14195 break; 14196 case Intrinsic::arm_neon_vqrshiftnsu: 14197 VShiftOpc = ARMISD::VQRSHRNsuIMM; 14198 break; 14199 } 14200 14201 SDLoc dl(N); 14202 return DAG.getNode(VShiftOpc, dl, N->getValueType(0), 14203 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32)); 14204 } 14205 14206 case Intrinsic::arm_neon_vshiftins: { 14207 EVT VT = N->getOperand(1).getValueType(); 14208 int64_t Cnt; 14209 unsigned VShiftOpc = 0; 14210 14211 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt)) 14212 VShiftOpc = ARMISD::VSLIIMM; 14213 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt)) 14214 VShiftOpc = ARMISD::VSRIIMM; 14215 else { 14216 llvm_unreachable("invalid shift count for vsli/vsri intrinsic"); 14217 } 14218 14219 SDLoc dl(N); 14220 return DAG.getNode(VShiftOpc, dl, N->getValueType(0), 14221 N->getOperand(1), N->getOperand(2), 14222 DAG.getConstant(Cnt, dl, MVT::i32)); 14223 } 14224 14225 case Intrinsic::arm_neon_vqrshifts: 14226 case Intrinsic::arm_neon_vqrshiftu: 14227 // No immediate versions of these to check for. 14228 break; 14229 } 14230 14231 return SDValue(); 14232 } 14233 14234 /// PerformShiftCombine - Checks for immediate versions of vector shifts and 14235 /// lowers them. As with the vector shift intrinsics, this is done during DAG 14236 /// combining instead of DAG legalizing because the build_vectors for 64-bit 14237 /// vector element shift counts are generally not legal, and it is hard to see 14238 /// their values after they get legalized to loads from a constant pool. 14239 static SDValue PerformShiftCombine(SDNode *N, 14240 TargetLowering::DAGCombinerInfo &DCI, 14241 const ARMSubtarget *ST) { 14242 SelectionDAG &DAG = DCI.DAG; 14243 EVT VT = N->getValueType(0); 14244 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) { 14245 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high 14246 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16. 14247 SDValue N1 = N->getOperand(1); 14248 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { 14249 SDValue N0 = N->getOperand(0); 14250 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP && 14251 DAG.MaskedValueIsZero(N0.getOperand(0), 14252 APInt::getHighBitsSet(32, 16))) 14253 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1); 14254 } 14255 } 14256 14257 if (ST->isThumb1Only() && N->getOpcode() == ISD::SHL && VT == MVT::i32 && 14258 N->getOperand(0)->getOpcode() == ISD::AND && 14259 N->getOperand(0)->hasOneUse()) { 14260 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 14261 return SDValue(); 14262 // Look for the pattern (shl (and x, AndMask), ShiftAmt). This doesn't 14263 // usually show up because instcombine prefers to canonicalize it to 14264 // (and (shl x, ShiftAmt) (shl AndMask, ShiftAmt)), but the shift can come 14265 // out of GEP lowering in some cases. 14266 SDValue N0 = N->getOperand(0); 14267 ConstantSDNode *ShiftAmtNode = dyn_cast<ConstantSDNode>(N->getOperand(1)); 14268 if (!ShiftAmtNode) 14269 return SDValue(); 14270 uint32_t ShiftAmt = static_cast<uint32_t>(ShiftAmtNode->getZExtValue()); 14271 ConstantSDNode *AndMaskNode = dyn_cast<ConstantSDNode>(N0->getOperand(1)); 14272 if (!AndMaskNode) 14273 return SDValue(); 14274 uint32_t AndMask = static_cast<uint32_t>(AndMaskNode->getZExtValue()); 14275 // Don't transform uxtb/uxth. 14276 if (AndMask == 255 || AndMask == 65535) 14277 return SDValue(); 14278 if (isMask_32(AndMask)) { 14279 uint32_t MaskedBits = countLeadingZeros(AndMask); 14280 if (MaskedBits > ShiftAmt) { 14281 SDLoc DL(N); 14282 SDValue SHL = DAG.getNode(ISD::SHL, DL, MVT::i32, N0->getOperand(0), 14283 DAG.getConstant(MaskedBits, DL, MVT::i32)); 14284 return DAG.getNode( 14285 ISD::SRL, DL, MVT::i32, SHL, 14286 DAG.getConstant(MaskedBits - ShiftAmt, DL, MVT::i32)); 14287 } 14288 } 14289 } 14290 14291 // Nothing to be done for scalar shifts. 14292 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 14293 if (!VT.isVector() || !TLI.isTypeLegal(VT)) 14294 return SDValue(); 14295 if (ST->hasMVEIntegerOps() && VT == MVT::v2i64) 14296 return SDValue(); 14297 14298 int64_t Cnt; 14299 14300 switch (N->getOpcode()) { 14301 default: llvm_unreachable("unexpected shift opcode"); 14302 14303 case ISD::SHL: 14304 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) { 14305 SDLoc dl(N); 14306 return DAG.getNode(ARMISD::VSHLIMM, dl, VT, N->getOperand(0), 14307 DAG.getConstant(Cnt, dl, MVT::i32)); 14308 } 14309 break; 14310 14311 case ISD::SRA: 14312 case ISD::SRL: 14313 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) { 14314 unsigned VShiftOpc = 14315 (N->getOpcode() == ISD::SRA ? ARMISD::VSHRsIMM : ARMISD::VSHRuIMM); 14316 SDLoc dl(N); 14317 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0), 14318 DAG.getConstant(Cnt, dl, MVT::i32)); 14319 } 14320 } 14321 return SDValue(); 14322 } 14323 14324 // Look for a sign/zero extend of a larger than legal load. This can be split 14325 // into two extending loads, which are simpler to deal with than an arbitrary 14326 // sign extend. 14327 static SDValue PerformSplittingToWideningLoad(SDNode *N, SelectionDAG &DAG) { 14328 SDValue N0 = N->getOperand(0); 14329 if (N0.getOpcode() != ISD::LOAD) 14330 return SDValue(); 14331 LoadSDNode *LD = cast<LoadSDNode>(N0.getNode()); 14332 if (!LD->isSimple() || !N0.hasOneUse() || LD->isIndexed() || 14333 LD->getExtensionType() != ISD::NON_EXTLOAD) 14334 return SDValue(); 14335 EVT FromVT = LD->getValueType(0); 14336 EVT ToVT = N->getValueType(0); 14337 if (!ToVT.isVector()) 14338 return SDValue(); 14339 assert(FromVT.getVectorNumElements() == ToVT.getVectorNumElements()); 14340 EVT ToEltVT = ToVT.getVectorElementType(); 14341 EVT FromEltVT = FromVT.getVectorElementType(); 14342 14343 unsigned NumElements = 0; 14344 if (ToEltVT == MVT::i32 && (FromEltVT == MVT::i16 || FromEltVT == MVT::i8)) 14345 NumElements = 4; 14346 if (ToEltVT == MVT::i16 && FromEltVT == MVT::i8) 14347 NumElements = 8; 14348 if (NumElements == 0 || 14349 FromVT.getVectorNumElements() == NumElements || 14350 FromVT.getVectorNumElements() % NumElements != 0 || 14351 !isPowerOf2_32(NumElements)) 14352 return SDValue(); 14353 14354 SDLoc DL(LD); 14355 // Details about the old load 14356 SDValue Ch = LD->getChain(); 14357 SDValue BasePtr = LD->getBasePtr(); 14358 unsigned Alignment = LD->getOriginalAlignment(); 14359 MachineMemOperand::Flags MMOFlags = LD->getMemOperand()->getFlags(); 14360 AAMDNodes AAInfo = LD->getAAInfo(); 14361 14362 ISD::LoadExtType NewExtType = 14363 N->getOpcode() == ISD::SIGN_EXTEND ? ISD::SEXTLOAD : ISD::ZEXTLOAD; 14364 SDValue Offset = DAG.getUNDEF(BasePtr.getValueType()); 14365 EVT NewFromVT = FromVT.getHalfNumVectorElementsVT(*DAG.getContext()); 14366 EVT NewToVT = ToVT.getHalfNumVectorElementsVT(*DAG.getContext()); 14367 unsigned NewOffset = NewFromVT.getSizeInBits() / 8; 14368 SDValue NewPtr = DAG.getObjectPtrOffset(DL, BasePtr, NewOffset); 14369 14370 // Split the load in half, each side of which is extended separately. This 14371 // is good enough, as legalisation will take it from there. They are either 14372 // already legal or they will be split further into something that is 14373 // legal. 14374 SDValue NewLoad1 = 14375 DAG.getLoad(ISD::UNINDEXED, NewExtType, NewToVT, DL, Ch, BasePtr, Offset, 14376 LD->getPointerInfo(), NewFromVT, Alignment, MMOFlags, AAInfo); 14377 SDValue NewLoad2 = 14378 DAG.getLoad(ISD::UNINDEXED, NewExtType, NewToVT, DL, Ch, NewPtr, Offset, 14379 LD->getPointerInfo().getWithOffset(NewOffset), NewFromVT, 14380 Alignment, MMOFlags, AAInfo); 14381 14382 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, 14383 SDValue(NewLoad1.getNode(), 1), 14384 SDValue(NewLoad2.getNode(), 1)); 14385 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewChain); 14386 return DAG.getNode(ISD::CONCAT_VECTORS, DL, ToVT, NewLoad1, NewLoad2); 14387 } 14388 14389 /// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND, 14390 /// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND. 14391 static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG, 14392 const ARMSubtarget *ST) { 14393 SDValue N0 = N->getOperand(0); 14394 14395 // Check for sign- and zero-extensions of vector extract operations of 8- and 14396 // 16-bit vector elements. NEON and MVE support these directly. They are 14397 // handled during DAG combining because type legalization will promote them 14398 // to 32-bit types and it is messy to recognize the operations after that. 14399 if ((ST->hasNEON() || ST->hasMVEIntegerOps()) && 14400 N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 14401 SDValue Vec = N0.getOperand(0); 14402 SDValue Lane = N0.getOperand(1); 14403 EVT VT = N->getValueType(0); 14404 EVT EltVT = N0.getValueType(); 14405 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 14406 14407 if (VT == MVT::i32 && 14408 (EltVT == MVT::i8 || EltVT == MVT::i16) && 14409 TLI.isTypeLegal(Vec.getValueType()) && 14410 isa<ConstantSDNode>(Lane)) { 14411 14412 unsigned Opc = 0; 14413 switch (N->getOpcode()) { 14414 default: llvm_unreachable("unexpected opcode"); 14415 case ISD::SIGN_EXTEND: 14416 Opc = ARMISD::VGETLANEs; 14417 break; 14418 case ISD::ZERO_EXTEND: 14419 case ISD::ANY_EXTEND: 14420 Opc = ARMISD::VGETLANEu; 14421 break; 14422 } 14423 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane); 14424 } 14425 } 14426 14427 if (ST->hasMVEIntegerOps()) 14428 if (SDValue NewLoad = PerformSplittingToWideningLoad(N, DAG)) 14429 return NewLoad; 14430 14431 return SDValue(); 14432 } 14433 14434 static const APInt *isPowerOf2Constant(SDValue V) { 14435 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V); 14436 if (!C) 14437 return nullptr; 14438 const APInt *CV = &C->getAPIntValue(); 14439 return CV->isPowerOf2() ? CV : nullptr; 14440 } 14441 14442 SDValue ARMTargetLowering::PerformCMOVToBFICombine(SDNode *CMOV, SelectionDAG &DAG) const { 14443 // If we have a CMOV, OR and AND combination such as: 14444 // if (x & CN) 14445 // y |= CM; 14446 // 14447 // And: 14448 // * CN is a single bit; 14449 // * All bits covered by CM are known zero in y 14450 // 14451 // Then we can convert this into a sequence of BFI instructions. This will 14452 // always be a win if CM is a single bit, will always be no worse than the 14453 // TST&OR sequence if CM is two bits, and for thumb will be no worse if CM is 14454 // three bits (due to the extra IT instruction). 14455 14456 SDValue Op0 = CMOV->getOperand(0); 14457 SDValue Op1 = CMOV->getOperand(1); 14458 auto CCNode = cast<ConstantSDNode>(CMOV->getOperand(2)); 14459 auto CC = CCNode->getAPIntValue().getLimitedValue(); 14460 SDValue CmpZ = CMOV->getOperand(4); 14461 14462 // The compare must be against zero. 14463 if (!isNullConstant(CmpZ->getOperand(1))) 14464 return SDValue(); 14465 14466 assert(CmpZ->getOpcode() == ARMISD::CMPZ); 14467 SDValue And = CmpZ->getOperand(0); 14468 if (And->getOpcode() != ISD::AND) 14469 return SDValue(); 14470 const APInt *AndC = isPowerOf2Constant(And->getOperand(1)); 14471 if (!AndC) 14472 return SDValue(); 14473 SDValue X = And->getOperand(0); 14474 14475 if (CC == ARMCC::EQ) { 14476 // We're performing an "equal to zero" compare. Swap the operands so we 14477 // canonicalize on a "not equal to zero" compare. 14478 std::swap(Op0, Op1); 14479 } else { 14480 assert(CC == ARMCC::NE && "How can a CMPZ node not be EQ or NE?"); 14481 } 14482 14483 if (Op1->getOpcode() != ISD::OR) 14484 return SDValue(); 14485 14486 ConstantSDNode *OrC = dyn_cast<ConstantSDNode>(Op1->getOperand(1)); 14487 if (!OrC) 14488 return SDValue(); 14489 SDValue Y = Op1->getOperand(0); 14490 14491 if (Op0 != Y) 14492 return SDValue(); 14493 14494 // Now, is it profitable to continue? 14495 APInt OrCI = OrC->getAPIntValue(); 14496 unsigned Heuristic = Subtarget->isThumb() ? 3 : 2; 14497 if (OrCI.countPopulation() > Heuristic) 14498 return SDValue(); 14499 14500 // Lastly, can we determine that the bits defined by OrCI 14501 // are zero in Y? 14502 KnownBits Known = DAG.computeKnownBits(Y); 14503 if ((OrCI & Known.Zero) != OrCI) 14504 return SDValue(); 14505 14506 // OK, we can do the combine. 14507 SDValue V = Y; 14508 SDLoc dl(X); 14509 EVT VT = X.getValueType(); 14510 unsigned BitInX = AndC->logBase2(); 14511 14512 if (BitInX != 0) { 14513 // We must shift X first. 14514 X = DAG.getNode(ISD::SRL, dl, VT, X, 14515 DAG.getConstant(BitInX, dl, VT)); 14516 } 14517 14518 for (unsigned BitInY = 0, NumActiveBits = OrCI.getActiveBits(); 14519 BitInY < NumActiveBits; ++BitInY) { 14520 if (OrCI[BitInY] == 0) 14521 continue; 14522 APInt Mask(VT.getSizeInBits(), 0); 14523 Mask.setBit(BitInY); 14524 V = DAG.getNode(ARMISD::BFI, dl, VT, V, X, 14525 // Confusingly, the operand is an *inverted* mask. 14526 DAG.getConstant(~Mask, dl, VT)); 14527 } 14528 14529 return V; 14530 } 14531 14532 // Given N, the value controlling the conditional branch, search for the loop 14533 // intrinsic, returning it, along with how the value is used. We need to handle 14534 // patterns such as the following: 14535 // (brcond (xor (setcc (loop.decrement), 0, ne), 1), exit) 14536 // (brcond (setcc (loop.decrement), 0, eq), exit) 14537 // (brcond (setcc (loop.decrement), 0, ne), header) 14538 static SDValue SearchLoopIntrinsic(SDValue N, ISD::CondCode &CC, int &Imm, 14539 bool &Negate) { 14540 switch (N->getOpcode()) { 14541 default: 14542 break; 14543 case ISD::XOR: { 14544 if (!isa<ConstantSDNode>(N.getOperand(1))) 14545 return SDValue(); 14546 if (!cast<ConstantSDNode>(N.getOperand(1))->isOne()) 14547 return SDValue(); 14548 Negate = !Negate; 14549 return SearchLoopIntrinsic(N.getOperand(0), CC, Imm, Negate); 14550 } 14551 case ISD::SETCC: { 14552 auto *Const = dyn_cast<ConstantSDNode>(N.getOperand(1)); 14553 if (!Const) 14554 return SDValue(); 14555 if (Const->isNullValue()) 14556 Imm = 0; 14557 else if (Const->isOne()) 14558 Imm = 1; 14559 else 14560 return SDValue(); 14561 CC = cast<CondCodeSDNode>(N.getOperand(2))->get(); 14562 return SearchLoopIntrinsic(N->getOperand(0), CC, Imm, Negate); 14563 } 14564 case ISD::INTRINSIC_W_CHAIN: { 14565 unsigned IntOp = cast<ConstantSDNode>(N.getOperand(1))->getZExtValue(); 14566 if (IntOp != Intrinsic::test_set_loop_iterations && 14567 IntOp != Intrinsic::loop_decrement_reg) 14568 return SDValue(); 14569 return N; 14570 } 14571 } 14572 return SDValue(); 14573 } 14574 14575 static SDValue PerformHWLoopCombine(SDNode *N, 14576 TargetLowering::DAGCombinerInfo &DCI, 14577 const ARMSubtarget *ST) { 14578 14579 // The hwloop intrinsics that we're interested are used for control-flow, 14580 // either for entering or exiting the loop: 14581 // - test.set.loop.iterations will test whether its operand is zero. If it 14582 // is zero, the proceeding branch should not enter the loop. 14583 // - loop.decrement.reg also tests whether its operand is zero. If it is 14584 // zero, the proceeding branch should not branch back to the beginning of 14585 // the loop. 14586 // So here, we need to check that how the brcond is using the result of each 14587 // of the intrinsics to ensure that we're branching to the right place at the 14588 // right time. 14589 14590 ISD::CondCode CC; 14591 SDValue Cond; 14592 int Imm = 1; 14593 bool Negate = false; 14594 SDValue Chain = N->getOperand(0); 14595 SDValue Dest; 14596 14597 if (N->getOpcode() == ISD::BRCOND) { 14598 CC = ISD::SETEQ; 14599 Cond = N->getOperand(1); 14600 Dest = N->getOperand(2); 14601 } else { 14602 assert(N->getOpcode() == ISD::BR_CC && "Expected BRCOND or BR_CC!"); 14603 CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); 14604 Cond = N->getOperand(2); 14605 Dest = N->getOperand(4); 14606 if (auto *Const = dyn_cast<ConstantSDNode>(N->getOperand(3))) { 14607 if (!Const->isOne() && !Const->isNullValue()) 14608 return SDValue(); 14609 Imm = Const->getZExtValue(); 14610 } else 14611 return SDValue(); 14612 } 14613 14614 SDValue Int = SearchLoopIntrinsic(Cond, CC, Imm, Negate); 14615 if (!Int) 14616 return SDValue(); 14617 14618 if (Negate) 14619 CC = ISD::getSetCCInverse(CC, /* Integer inverse */ MVT::i32); 14620 14621 auto IsTrueIfZero = [](ISD::CondCode CC, int Imm) { 14622 return (CC == ISD::SETEQ && Imm == 0) || 14623 (CC == ISD::SETNE && Imm == 1) || 14624 (CC == ISD::SETLT && Imm == 1) || 14625 (CC == ISD::SETULT && Imm == 1); 14626 }; 14627 14628 auto IsFalseIfZero = [](ISD::CondCode CC, int Imm) { 14629 return (CC == ISD::SETEQ && Imm == 1) || 14630 (CC == ISD::SETNE && Imm == 0) || 14631 (CC == ISD::SETGT && Imm == 0) || 14632 (CC == ISD::SETUGT && Imm == 0) || 14633 (CC == ISD::SETGE && Imm == 1) || 14634 (CC == ISD::SETUGE && Imm == 1); 14635 }; 14636 14637 assert((IsTrueIfZero(CC, Imm) || IsFalseIfZero(CC, Imm)) && 14638 "unsupported condition"); 14639 14640 SDLoc dl(Int); 14641 SelectionDAG &DAG = DCI.DAG; 14642 SDValue Elements = Int.getOperand(2); 14643 unsigned IntOp = cast<ConstantSDNode>(Int->getOperand(1))->getZExtValue(); 14644 assert((N->hasOneUse() && N->use_begin()->getOpcode() == ISD::BR) 14645 && "expected single br user"); 14646 SDNode *Br = *N->use_begin(); 14647 SDValue OtherTarget = Br->getOperand(1); 14648 14649 // Update the unconditional branch to branch to the given Dest. 14650 auto UpdateUncondBr = [](SDNode *Br, SDValue Dest, SelectionDAG &DAG) { 14651 SDValue NewBrOps[] = { Br->getOperand(0), Dest }; 14652 SDValue NewBr = DAG.getNode(ISD::BR, SDLoc(Br), MVT::Other, NewBrOps); 14653 DAG.ReplaceAllUsesOfValueWith(SDValue(Br, 0), NewBr); 14654 }; 14655 14656 if (IntOp == Intrinsic::test_set_loop_iterations) { 14657 SDValue Res; 14658 // We expect this 'instruction' to branch when the counter is zero. 14659 if (IsTrueIfZero(CC, Imm)) { 14660 SDValue Ops[] = { Chain, Elements, Dest }; 14661 Res = DAG.getNode(ARMISD::WLS, dl, MVT::Other, Ops); 14662 } else { 14663 // The logic is the reverse of what we need for WLS, so find the other 14664 // basic block target: the target of the proceeding br. 14665 UpdateUncondBr(Br, Dest, DAG); 14666 14667 SDValue Ops[] = { Chain, Elements, OtherTarget }; 14668 Res = DAG.getNode(ARMISD::WLS, dl, MVT::Other, Ops); 14669 } 14670 DAG.ReplaceAllUsesOfValueWith(Int.getValue(1), Int.getOperand(0)); 14671 return Res; 14672 } else { 14673 SDValue Size = DAG.getTargetConstant( 14674 cast<ConstantSDNode>(Int.getOperand(3))->getZExtValue(), dl, MVT::i32); 14675 SDValue Args[] = { Int.getOperand(0), Elements, Size, }; 14676 SDValue LoopDec = DAG.getNode(ARMISD::LOOP_DEC, dl, 14677 DAG.getVTList(MVT::i32, MVT::Other), Args); 14678 DAG.ReplaceAllUsesWith(Int.getNode(), LoopDec.getNode()); 14679 14680 // We expect this instruction to branch when the count is not zero. 14681 SDValue Target = IsFalseIfZero(CC, Imm) ? Dest : OtherTarget; 14682 14683 // Update the unconditional branch to target the loop preheader if we've 14684 // found the condition has been reversed. 14685 if (Target == OtherTarget) 14686 UpdateUncondBr(Br, Dest, DAG); 14687 14688 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 14689 SDValue(LoopDec.getNode(), 1), Chain); 14690 14691 SDValue EndArgs[] = { Chain, SDValue(LoopDec.getNode(), 0), Target }; 14692 return DAG.getNode(ARMISD::LE, dl, MVT::Other, EndArgs); 14693 } 14694 return SDValue(); 14695 } 14696 14697 /// PerformBRCONDCombine - Target-specific DAG combining for ARMISD::BRCOND. 14698 SDValue 14699 ARMTargetLowering::PerformBRCONDCombine(SDNode *N, SelectionDAG &DAG) const { 14700 SDValue Cmp = N->getOperand(4); 14701 if (Cmp.getOpcode() != ARMISD::CMPZ) 14702 // Only looking at NE cases. 14703 return SDValue(); 14704 14705 EVT VT = N->getValueType(0); 14706 SDLoc dl(N); 14707 SDValue LHS = Cmp.getOperand(0); 14708 SDValue RHS = Cmp.getOperand(1); 14709 SDValue Chain = N->getOperand(0); 14710 SDValue BB = N->getOperand(1); 14711 SDValue ARMcc = N->getOperand(2); 14712 ARMCC::CondCodes CC = 14713 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue(); 14714 14715 // (brcond Chain BB ne CPSR (cmpz (and (cmov 0 1 CC CPSR Cmp) 1) 0)) 14716 // -> (brcond Chain BB CC CPSR Cmp) 14717 if (CC == ARMCC::NE && LHS.getOpcode() == ISD::AND && LHS->hasOneUse() && 14718 LHS->getOperand(0)->getOpcode() == ARMISD::CMOV && 14719 LHS->getOperand(0)->hasOneUse()) { 14720 auto *LHS00C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(0)); 14721 auto *LHS01C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(1)); 14722 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1)); 14723 auto *RHSC = dyn_cast<ConstantSDNode>(RHS); 14724 if ((LHS00C && LHS00C->getZExtValue() == 0) && 14725 (LHS01C && LHS01C->getZExtValue() == 1) && 14726 (LHS1C && LHS1C->getZExtValue() == 1) && 14727 (RHSC && RHSC->getZExtValue() == 0)) { 14728 return DAG.getNode( 14729 ARMISD::BRCOND, dl, VT, Chain, BB, LHS->getOperand(0)->getOperand(2), 14730 LHS->getOperand(0)->getOperand(3), LHS->getOperand(0)->getOperand(4)); 14731 } 14732 } 14733 14734 return SDValue(); 14735 } 14736 14737 /// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV. 14738 SDValue 14739 ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const { 14740 SDValue Cmp = N->getOperand(4); 14741 if (Cmp.getOpcode() != ARMISD::CMPZ) 14742 // Only looking at EQ and NE cases. 14743 return SDValue(); 14744 14745 EVT VT = N->getValueType(0); 14746 SDLoc dl(N); 14747 SDValue LHS = Cmp.getOperand(0); 14748 SDValue RHS = Cmp.getOperand(1); 14749 SDValue FalseVal = N->getOperand(0); 14750 SDValue TrueVal = N->getOperand(1); 14751 SDValue ARMcc = N->getOperand(2); 14752 ARMCC::CondCodes CC = 14753 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue(); 14754 14755 // BFI is only available on V6T2+. 14756 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) { 14757 SDValue R = PerformCMOVToBFICombine(N, DAG); 14758 if (R) 14759 return R; 14760 } 14761 14762 // Simplify 14763 // mov r1, r0 14764 // cmp r1, x 14765 // mov r0, y 14766 // moveq r0, x 14767 // to 14768 // cmp r0, x 14769 // movne r0, y 14770 // 14771 // mov r1, r0 14772 // cmp r1, x 14773 // mov r0, x 14774 // movne r0, y 14775 // to 14776 // cmp r0, x 14777 // movne r0, y 14778 /// FIXME: Turn this into a target neutral optimization? 14779 SDValue Res; 14780 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) { 14781 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc, 14782 N->getOperand(3), Cmp); 14783 } else if (CC == ARMCC::EQ && TrueVal == RHS) { 14784 SDValue ARMcc; 14785 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl); 14786 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc, 14787 N->getOperand(3), NewCmp); 14788 } 14789 14790 // (cmov F T ne CPSR (cmpz (cmov 0 1 CC CPSR Cmp) 0)) 14791 // -> (cmov F T CC CPSR Cmp) 14792 if (CC == ARMCC::NE && LHS.getOpcode() == ARMISD::CMOV && LHS->hasOneUse()) { 14793 auto *LHS0C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)); 14794 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1)); 14795 auto *RHSC = dyn_cast<ConstantSDNode>(RHS); 14796 if ((LHS0C && LHS0C->getZExtValue() == 0) && 14797 (LHS1C && LHS1C->getZExtValue() == 1) && 14798 (RHSC && RHSC->getZExtValue() == 0)) { 14799 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, 14800 LHS->getOperand(2), LHS->getOperand(3), 14801 LHS->getOperand(4)); 14802 } 14803 } 14804 14805 if (!VT.isInteger()) 14806 return SDValue(); 14807 14808 // Materialize a boolean comparison for integers so we can avoid branching. 14809 if (isNullConstant(FalseVal)) { 14810 if (CC == ARMCC::EQ && isOneConstant(TrueVal)) { 14811 if (!Subtarget->isThumb1Only() && Subtarget->hasV5TOps()) { 14812 // If x == y then x - y == 0 and ARM's CLZ will return 32, shifting it 14813 // right 5 bits will make that 32 be 1, otherwise it will be 0. 14814 // CMOV 0, 1, ==, (CMPZ x, y) -> SRL (CTLZ (SUB x, y)), 5 14815 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, LHS, RHS); 14816 Res = DAG.getNode(ISD::SRL, dl, VT, DAG.getNode(ISD::CTLZ, dl, VT, Sub), 14817 DAG.getConstant(5, dl, MVT::i32)); 14818 } else { 14819 // CMOV 0, 1, ==, (CMPZ x, y) -> 14820 // (ADDCARRY (SUB x, y), t:0, t:1) 14821 // where t = (SUBCARRY 0, (SUB x, y), 0) 14822 // 14823 // The SUBCARRY computes 0 - (x - y) and this will give a borrow when 14824 // x != y. In other words, a carry C == 1 when x == y, C == 0 14825 // otherwise. 14826 // The final ADDCARRY computes 14827 // x - y + (0 - (x - y)) + C == C 14828 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, LHS, RHS); 14829 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 14830 SDValue Neg = DAG.getNode(ISD::USUBO, dl, VTs, FalseVal, Sub); 14831 // ISD::SUBCARRY returns a borrow but we want the carry here 14832 // actually. 14833 SDValue Carry = 14834 DAG.getNode(ISD::SUB, dl, MVT::i32, 14835 DAG.getConstant(1, dl, MVT::i32), Neg.getValue(1)); 14836 Res = DAG.getNode(ISD::ADDCARRY, dl, VTs, Sub, Neg, Carry); 14837 } 14838 } else if (CC == ARMCC::NE && !isNullConstant(RHS) && 14839 (!Subtarget->isThumb1Only() || isPowerOf2Constant(TrueVal))) { 14840 // This seems pointless but will allow us to combine it further below. 14841 // CMOV 0, z, !=, (CMPZ x, y) -> CMOV (SUBS x, y), z, !=, (SUBS x, y):1 14842 SDValue Sub = 14843 DAG.getNode(ARMISD::SUBS, dl, DAG.getVTList(VT, MVT::i32), LHS, RHS); 14844 SDValue CPSRGlue = DAG.getCopyToReg(DAG.getEntryNode(), dl, ARM::CPSR, 14845 Sub.getValue(1), SDValue()); 14846 Res = DAG.getNode(ARMISD::CMOV, dl, VT, Sub, TrueVal, ARMcc, 14847 N->getOperand(3), CPSRGlue.getValue(1)); 14848 FalseVal = Sub; 14849 } 14850 } else if (isNullConstant(TrueVal)) { 14851 if (CC == ARMCC::EQ && !isNullConstant(RHS) && 14852 (!Subtarget->isThumb1Only() || isPowerOf2Constant(FalseVal))) { 14853 // This seems pointless but will allow us to combine it further below 14854 // Note that we change == for != as this is the dual for the case above. 14855 // CMOV z, 0, ==, (CMPZ x, y) -> CMOV (SUBS x, y), z, !=, (SUBS x, y):1 14856 SDValue Sub = 14857 DAG.getNode(ARMISD::SUBS, dl, DAG.getVTList(VT, MVT::i32), LHS, RHS); 14858 SDValue CPSRGlue = DAG.getCopyToReg(DAG.getEntryNode(), dl, ARM::CPSR, 14859 Sub.getValue(1), SDValue()); 14860 Res = DAG.getNode(ARMISD::CMOV, dl, VT, Sub, FalseVal, 14861 DAG.getConstant(ARMCC::NE, dl, MVT::i32), 14862 N->getOperand(3), CPSRGlue.getValue(1)); 14863 FalseVal = Sub; 14864 } 14865 } 14866 14867 // On Thumb1, the DAG above may be further combined if z is a power of 2 14868 // (z == 2 ^ K). 14869 // CMOV (SUBS x, y), z, !=, (SUBS x, y):1 -> 14870 // t1 = (USUBO (SUB x, y), 1) 14871 // t2 = (SUBCARRY (SUB x, y), t1:0, t1:1) 14872 // Result = if K != 0 then (SHL t2:0, K) else t2:0 14873 // 14874 // This also handles the special case of comparing against zero; it's 14875 // essentially, the same pattern, except there's no SUBS: 14876 // CMOV x, z, !=, (CMPZ x, 0) -> 14877 // t1 = (USUBO x, 1) 14878 // t2 = (SUBCARRY x, t1:0, t1:1) 14879 // Result = if K != 0 then (SHL t2:0, K) else t2:0 14880 const APInt *TrueConst; 14881 if (Subtarget->isThumb1Only() && CC == ARMCC::NE && 14882 ((FalseVal.getOpcode() == ARMISD::SUBS && 14883 FalseVal.getOperand(0) == LHS && FalseVal.getOperand(1) == RHS) || 14884 (FalseVal == LHS && isNullConstant(RHS))) && 14885 (TrueConst = isPowerOf2Constant(TrueVal))) { 14886 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 14887 unsigned ShiftAmount = TrueConst->logBase2(); 14888 if (ShiftAmount) 14889 TrueVal = DAG.getConstant(1, dl, VT); 14890 SDValue Subc = DAG.getNode(ISD::USUBO, dl, VTs, FalseVal, TrueVal); 14891 Res = DAG.getNode(ISD::SUBCARRY, dl, VTs, FalseVal, Subc, Subc.getValue(1)); 14892 14893 if (ShiftAmount) 14894 Res = DAG.getNode(ISD::SHL, dl, VT, Res, 14895 DAG.getConstant(ShiftAmount, dl, MVT::i32)); 14896 } 14897 14898 if (Res.getNode()) { 14899 KnownBits Known = DAG.computeKnownBits(SDValue(N,0)); 14900 // Capture demanded bits information that would be otherwise lost. 14901 if (Known.Zero == 0xfffffffe) 14902 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 14903 DAG.getValueType(MVT::i1)); 14904 else if (Known.Zero == 0xffffff00) 14905 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 14906 DAG.getValueType(MVT::i8)); 14907 else if (Known.Zero == 0xffff0000) 14908 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 14909 DAG.getValueType(MVT::i16)); 14910 } 14911 14912 return Res; 14913 } 14914 14915 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N, 14916 DAGCombinerInfo &DCI) const { 14917 switch (N->getOpcode()) { 14918 default: break; 14919 case ISD::ABS: return PerformABSCombine(N, DCI, Subtarget); 14920 case ARMISD::ADDE: return PerformADDECombine(N, DCI, Subtarget); 14921 case ARMISD::UMLAL: return PerformUMLALCombine(N, DCI.DAG, Subtarget); 14922 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget); 14923 case ISD::SUB: return PerformSUBCombine(N, DCI, Subtarget); 14924 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget); 14925 case ISD::OR: return PerformORCombine(N, DCI, Subtarget); 14926 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget); 14927 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget); 14928 case ISD::BRCOND: 14929 case ISD::BR_CC: return PerformHWLoopCombine(N, DCI, Subtarget); 14930 case ARMISD::ADDC: 14931 case ARMISD::SUBC: return PerformAddcSubcCombine(N, DCI, Subtarget); 14932 case ARMISD::SUBE: return PerformAddeSubeCombine(N, DCI, Subtarget); 14933 case ARMISD::BFI: return PerformBFICombine(N, DCI); 14934 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget); 14935 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG); 14936 case ISD::STORE: return PerformSTORECombine(N, DCI, Subtarget); 14937 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget); 14938 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI); 14939 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG); 14940 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI); 14941 case ARMISD::VDUP: return PerformVDUPCombine(N, DCI, Subtarget); 14942 case ISD::FP_TO_SINT: 14943 case ISD::FP_TO_UINT: 14944 return PerformVCVTCombine(N, DCI.DAG, Subtarget); 14945 case ISD::FDIV: 14946 return PerformVDIVCombine(N, DCI.DAG, Subtarget); 14947 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG); 14948 case ISD::SHL: 14949 case ISD::SRA: 14950 case ISD::SRL: 14951 return PerformShiftCombine(N, DCI, Subtarget); 14952 case ISD::SIGN_EXTEND: 14953 case ISD::ZERO_EXTEND: 14954 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget); 14955 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG); 14956 case ARMISD::BRCOND: return PerformBRCONDCombine(N, DCI.DAG); 14957 case ISD::LOAD: return PerformLOADCombine(N, DCI); 14958 case ARMISD::VLD1DUP: 14959 case ARMISD::VLD2DUP: 14960 case ARMISD::VLD3DUP: 14961 case ARMISD::VLD4DUP: 14962 return PerformVLDCombine(N, DCI); 14963 case ARMISD::BUILD_VECTOR: 14964 return PerformARMBUILD_VECTORCombine(N, DCI); 14965 case ARMISD::PREDICATE_CAST: 14966 return PerformPREDICATE_CASTCombine(N, DCI); 14967 case ARMISD::VECTOR_REG_CAST: 14968 return PerformVECTOR_REG_CASTCombine(N, DCI, Subtarget); 14969 case ARMISD::VCMP: 14970 return PerformVCMPCombine(N, DCI, Subtarget); 14971 case ISD::VECREDUCE_ADD: 14972 return PerformVECREDUCE_ADDCombine(N, DCI.DAG, Subtarget); 14973 case ARMISD::SMULWB: { 14974 unsigned BitWidth = N->getValueType(0).getSizeInBits(); 14975 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, 16); 14976 if (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI)) 14977 return SDValue(); 14978 break; 14979 } 14980 case ARMISD::SMULWT: { 14981 unsigned BitWidth = N->getValueType(0).getSizeInBits(); 14982 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 16); 14983 if (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI)) 14984 return SDValue(); 14985 break; 14986 } 14987 case ARMISD::SMLALBB: 14988 case ARMISD::QADD16b: 14989 case ARMISD::QSUB16b: { 14990 unsigned BitWidth = N->getValueType(0).getSizeInBits(); 14991 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, 16); 14992 if ((SimplifyDemandedBits(N->getOperand(0), DemandedMask, DCI)) || 14993 (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI))) 14994 return SDValue(); 14995 break; 14996 } 14997 case ARMISD::SMLALBT: { 14998 unsigned LowWidth = N->getOperand(0).getValueType().getSizeInBits(); 14999 APInt LowMask = APInt::getLowBitsSet(LowWidth, 16); 15000 unsigned HighWidth = N->getOperand(1).getValueType().getSizeInBits(); 15001 APInt HighMask = APInt::getHighBitsSet(HighWidth, 16); 15002 if ((SimplifyDemandedBits(N->getOperand(0), LowMask, DCI)) || 15003 (SimplifyDemandedBits(N->getOperand(1), HighMask, DCI))) 15004 return SDValue(); 15005 break; 15006 } 15007 case ARMISD::SMLALTB: { 15008 unsigned HighWidth = N->getOperand(0).getValueType().getSizeInBits(); 15009 APInt HighMask = APInt::getHighBitsSet(HighWidth, 16); 15010 unsigned LowWidth = N->getOperand(1).getValueType().getSizeInBits(); 15011 APInt LowMask = APInt::getLowBitsSet(LowWidth, 16); 15012 if ((SimplifyDemandedBits(N->getOperand(0), HighMask, DCI)) || 15013 (SimplifyDemandedBits(N->getOperand(1), LowMask, DCI))) 15014 return SDValue(); 15015 break; 15016 } 15017 case ARMISD::SMLALTT: { 15018 unsigned BitWidth = N->getValueType(0).getSizeInBits(); 15019 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 16); 15020 if ((SimplifyDemandedBits(N->getOperand(0), DemandedMask, DCI)) || 15021 (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI))) 15022 return SDValue(); 15023 break; 15024 } 15025 case ARMISD::QADD8b: 15026 case ARMISD::QSUB8b: { 15027 unsigned BitWidth = N->getValueType(0).getSizeInBits(); 15028 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, 8); 15029 if ((SimplifyDemandedBits(N->getOperand(0), DemandedMask, DCI)) || 15030 (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI))) 15031 return SDValue(); 15032 break; 15033 } 15034 case ISD::INTRINSIC_VOID: 15035 case ISD::INTRINSIC_W_CHAIN: 15036 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 15037 case Intrinsic::arm_neon_vld1: 15038 case Intrinsic::arm_neon_vld1x2: 15039 case Intrinsic::arm_neon_vld1x3: 15040 case Intrinsic::arm_neon_vld1x4: 15041 case Intrinsic::arm_neon_vld2: 15042 case Intrinsic::arm_neon_vld3: 15043 case Intrinsic::arm_neon_vld4: 15044 case Intrinsic::arm_neon_vld2lane: 15045 case Intrinsic::arm_neon_vld3lane: 15046 case Intrinsic::arm_neon_vld4lane: 15047 case Intrinsic::arm_neon_vld2dup: 15048 case Intrinsic::arm_neon_vld3dup: 15049 case Intrinsic::arm_neon_vld4dup: 15050 case Intrinsic::arm_neon_vst1: 15051 case Intrinsic::arm_neon_vst1x2: 15052 case Intrinsic::arm_neon_vst1x3: 15053 case Intrinsic::arm_neon_vst1x4: 15054 case Intrinsic::arm_neon_vst2: 15055 case Intrinsic::arm_neon_vst3: 15056 case Intrinsic::arm_neon_vst4: 15057 case Intrinsic::arm_neon_vst2lane: 15058 case Intrinsic::arm_neon_vst3lane: 15059 case Intrinsic::arm_neon_vst4lane: 15060 return PerformVLDCombine(N, DCI); 15061 case Intrinsic::arm_mve_vld2q: 15062 case Intrinsic::arm_mve_vld4q: 15063 case Intrinsic::arm_mve_vst2q: 15064 case Intrinsic::arm_mve_vst4q: 15065 return PerformMVEVLDCombine(N, DCI); 15066 default: break; 15067 } 15068 break; 15069 } 15070 return SDValue(); 15071 } 15072 15073 bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc, 15074 EVT VT) const { 15075 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE); 15076 } 15077 15078 bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT, unsigned, 15079 unsigned Alignment, 15080 MachineMemOperand::Flags, 15081 bool *Fast) const { 15082 // Depends what it gets converted into if the type is weird. 15083 if (!VT.isSimple()) 15084 return false; 15085 15086 // The AllowsUnaligned flag models the SCTLR.A setting in ARM cpus 15087 bool AllowsUnaligned = Subtarget->allowsUnalignedMem(); 15088 auto Ty = VT.getSimpleVT().SimpleTy; 15089 15090 if (Ty == MVT::i8 || Ty == MVT::i16 || Ty == MVT::i32) { 15091 // Unaligned access can use (for example) LRDB, LRDH, LDR 15092 if (AllowsUnaligned) { 15093 if (Fast) 15094 *Fast = Subtarget->hasV7Ops(); 15095 return true; 15096 } 15097 } 15098 15099 if (Ty == MVT::f64 || Ty == MVT::v2f64) { 15100 // For any little-endian targets with neon, we can support unaligned ld/st 15101 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8. 15102 // A big-endian target may also explicitly support unaligned accesses 15103 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) { 15104 if (Fast) 15105 *Fast = true; 15106 return true; 15107 } 15108 } 15109 15110 if (!Subtarget->hasMVEIntegerOps()) 15111 return false; 15112 15113 // These are for predicates 15114 if ((Ty == MVT::v16i1 || Ty == MVT::v8i1 || Ty == MVT::v4i1)) { 15115 if (Fast) 15116 *Fast = true; 15117 return true; 15118 } 15119 15120 // These are for truncated stores/narrowing loads. They are fine so long as 15121 // the alignment is at least the size of the item being loaded 15122 if ((Ty == MVT::v4i8 || Ty == MVT::v8i8 || Ty == MVT::v4i16) && 15123 Alignment >= VT.getScalarSizeInBits() / 8) { 15124 if (Fast) 15125 *Fast = true; 15126 return true; 15127 } 15128 15129 // In little-endian MVE, the store instructions VSTRB.U8, VSTRH.U16 and 15130 // VSTRW.U32 all store the vector register in exactly the same format, and 15131 // differ only in the range of their immediate offset field and the required 15132 // alignment. So there is always a store that can be used, regardless of 15133 // actual type. 15134 // 15135 // For big endian, that is not the case. But can still emit a (VSTRB.U8; 15136 // VREV64.8) pair and get the same effect. This will likely be better than 15137 // aligning the vector through the stack. 15138 if (Ty == MVT::v16i8 || Ty == MVT::v8i16 || Ty == MVT::v8f16 || 15139 Ty == MVT::v4i32 || Ty == MVT::v4f32 || Ty == MVT::v2i64 || 15140 Ty == MVT::v2f64) { 15141 if (Fast) 15142 *Fast = true; 15143 return true; 15144 } 15145 15146 return false; 15147 } 15148 15149 15150 EVT ARMTargetLowering::getOptimalMemOpType( 15151 const MemOp &Op, const AttributeList &FuncAttributes) const { 15152 // See if we can use NEON instructions for this... 15153 if ((Op.isMemcpy() || Op.isZeroMemset()) && Subtarget->hasNEON() && 15154 !FuncAttributes.hasFnAttribute(Attribute::NoImplicitFloat)) { 15155 bool Fast; 15156 if (Op.size() >= 16 && 15157 (Op.isAligned(Align(16)) || 15158 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, 15159 MachineMemOperand::MONone, &Fast) && 15160 Fast))) { 15161 return MVT::v2f64; 15162 } else if (Op.size() >= 8 && 15163 (Op.isAligned(Align(8)) || 15164 (allowsMisalignedMemoryAccesses( 15165 MVT::f64, 0, 1, MachineMemOperand::MONone, &Fast) && 15166 Fast))) { 15167 return MVT::f64; 15168 } 15169 } 15170 15171 // Let the target-independent logic figure it out. 15172 return MVT::Other; 15173 } 15174 15175 // 64-bit integers are split into their high and low parts and held in two 15176 // different registers, so the trunc is free since the low register can just 15177 // be used. 15178 bool ARMTargetLowering::isTruncateFree(Type *SrcTy, Type *DstTy) const { 15179 if (!SrcTy->isIntegerTy() || !DstTy->isIntegerTy()) 15180 return false; 15181 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); 15182 unsigned DestBits = DstTy->getPrimitiveSizeInBits(); 15183 return (SrcBits == 64 && DestBits == 32); 15184 } 15185 15186 bool ARMTargetLowering::isTruncateFree(EVT SrcVT, EVT DstVT) const { 15187 if (SrcVT.isVector() || DstVT.isVector() || !SrcVT.isInteger() || 15188 !DstVT.isInteger()) 15189 return false; 15190 unsigned SrcBits = SrcVT.getSizeInBits(); 15191 unsigned DestBits = DstVT.getSizeInBits(); 15192 return (SrcBits == 64 && DestBits == 32); 15193 } 15194 15195 bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const { 15196 if (Val.getOpcode() != ISD::LOAD) 15197 return false; 15198 15199 EVT VT1 = Val.getValueType(); 15200 if (!VT1.isSimple() || !VT1.isInteger() || 15201 !VT2.isSimple() || !VT2.isInteger()) 15202 return false; 15203 15204 switch (VT1.getSimpleVT().SimpleTy) { 15205 default: break; 15206 case MVT::i1: 15207 case MVT::i8: 15208 case MVT::i16: 15209 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits. 15210 return true; 15211 } 15212 15213 return false; 15214 } 15215 15216 bool ARMTargetLowering::isFNegFree(EVT VT) const { 15217 if (!VT.isSimple()) 15218 return false; 15219 15220 // There are quite a few FP16 instructions (e.g. VNMLA, VNMLS, etc.) that 15221 // negate values directly (fneg is free). So, we don't want to let the DAG 15222 // combiner rewrite fneg into xors and some other instructions. For f16 and 15223 // FullFP16 argument passing, some bitcast nodes may be introduced, 15224 // triggering this DAG combine rewrite, so we are avoiding that with this. 15225 switch (VT.getSimpleVT().SimpleTy) { 15226 default: break; 15227 case MVT::f16: 15228 return Subtarget->hasFullFP16(); 15229 } 15230 15231 return false; 15232 } 15233 15234 /// Check if Ext1 and Ext2 are extends of the same type, doubling the bitwidth 15235 /// of the vector elements. 15236 static bool areExtractExts(Value *Ext1, Value *Ext2) { 15237 auto areExtDoubled = [](Instruction *Ext) { 15238 return Ext->getType()->getScalarSizeInBits() == 15239 2 * Ext->getOperand(0)->getType()->getScalarSizeInBits(); 15240 }; 15241 15242 if (!match(Ext1, m_ZExtOrSExt(m_Value())) || 15243 !match(Ext2, m_ZExtOrSExt(m_Value())) || 15244 !areExtDoubled(cast<Instruction>(Ext1)) || 15245 !areExtDoubled(cast<Instruction>(Ext2))) 15246 return false; 15247 15248 return true; 15249 } 15250 15251 /// Check if sinking \p I's operands to I's basic block is profitable, because 15252 /// the operands can be folded into a target instruction, e.g. 15253 /// sext/zext can be folded into vsubl. 15254 bool ARMTargetLowering::shouldSinkOperands(Instruction *I, 15255 SmallVectorImpl<Use *> &Ops) const { 15256 if (!I->getType()->isVectorTy()) 15257 return false; 15258 15259 if (Subtarget->hasNEON()) { 15260 switch (I->getOpcode()) { 15261 case Instruction::Sub: 15262 case Instruction::Add: { 15263 if (!areExtractExts(I->getOperand(0), I->getOperand(1))) 15264 return false; 15265 Ops.push_back(&I->getOperandUse(0)); 15266 Ops.push_back(&I->getOperandUse(1)); 15267 return true; 15268 } 15269 default: 15270 return false; 15271 } 15272 } 15273 15274 if (!Subtarget->hasMVEIntegerOps()) 15275 return false; 15276 15277 auto IsSinker = [](Instruction *I, int Operand) { 15278 switch (I->getOpcode()) { 15279 case Instruction::Add: 15280 case Instruction::Mul: 15281 case Instruction::ICmp: 15282 return true; 15283 case Instruction::Sub: 15284 case Instruction::Shl: 15285 case Instruction::LShr: 15286 case Instruction::AShr: 15287 return Operand == 1; 15288 default: 15289 return false; 15290 } 15291 }; 15292 15293 int Op = 0; 15294 if (!isa<ShuffleVectorInst>(I->getOperand(Op))) 15295 Op = 1; 15296 if (!IsSinker(I, Op)) 15297 return false; 15298 if (!match(I->getOperand(Op), 15299 m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(), m_ZeroInt()), 15300 m_Undef(), m_Zero()))) { 15301 return false; 15302 } 15303 Instruction *Shuffle = cast<Instruction>(I->getOperand(Op)); 15304 // All uses of the shuffle should be sunk to avoid duplicating it across gpr 15305 // and vector registers 15306 for (Use &U : Shuffle->uses()) { 15307 Instruction *Insn = cast<Instruction>(U.getUser()); 15308 if (!IsSinker(Insn, U.getOperandNo())) 15309 return false; 15310 } 15311 Ops.push_back(&Shuffle->getOperandUse(0)); 15312 Ops.push_back(&I->getOperandUse(Op)); 15313 return true; 15314 } 15315 15316 bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const { 15317 EVT VT = ExtVal.getValueType(); 15318 15319 if (!isTypeLegal(VT)) 15320 return false; 15321 15322 if (auto *Ld = dyn_cast<MaskedLoadSDNode>(ExtVal.getOperand(0))) { 15323 if (Ld->isExpandingLoad()) 15324 return false; 15325 } 15326 15327 // Don't create a loadext if we can fold the extension into a wide/long 15328 // instruction. 15329 // If there's more than one user instruction, the loadext is desirable no 15330 // matter what. There can be two uses by the same instruction. 15331 if (ExtVal->use_empty() || 15332 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode())) 15333 return true; 15334 15335 SDNode *U = *ExtVal->use_begin(); 15336 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB || 15337 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHLIMM)) 15338 return false; 15339 15340 return true; 15341 } 15342 15343 bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const { 15344 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) 15345 return false; 15346 15347 if (!isTypeLegal(EVT::getEVT(Ty1))) 15348 return false; 15349 15350 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop"); 15351 15352 // Assuming the caller doesn't have a zeroext or signext return parameter, 15353 // truncation all the way down to i1 is valid. 15354 return true; 15355 } 15356 15357 int ARMTargetLowering::getScalingFactorCost(const DataLayout &DL, 15358 const AddrMode &AM, Type *Ty, 15359 unsigned AS) const { 15360 if (isLegalAddressingMode(DL, AM, Ty, AS)) { 15361 if (Subtarget->hasFPAO()) 15362 return AM.Scale < 0 ? 1 : 0; // positive offsets execute faster 15363 return 0; 15364 } 15365 return -1; 15366 } 15367 15368 /// isFMAFasterThanFMulAndFAdd - Return true if an FMA operation is faster 15369 /// than a pair of fmul and fadd instructions. fmuladd intrinsics will be 15370 /// expanded to FMAs when this method returns true, otherwise fmuladd is 15371 /// expanded to fmul + fadd. 15372 /// 15373 /// ARM supports both fused and unfused multiply-add operations; we already 15374 /// lower a pair of fmul and fadd to the latter so it's not clear that there 15375 /// would be a gain or that the gain would be worthwhile enough to risk 15376 /// correctness bugs. 15377 /// 15378 /// For MVE, we set this to true as it helps simplify the need for some 15379 /// patterns (and we don't have the non-fused floating point instruction). 15380 bool ARMTargetLowering::isFMAFasterThanFMulAndFAdd(const MachineFunction &MF, 15381 EVT VT) const { 15382 if (!VT.isSimple()) 15383 return false; 15384 15385 switch (VT.getSimpleVT().SimpleTy) { 15386 case MVT::v4f32: 15387 case MVT::v8f16: 15388 return Subtarget->hasMVEFloatOps(); 15389 case MVT::f16: 15390 return Subtarget->useFPVFMx16(); 15391 case MVT::f32: 15392 return Subtarget->useFPVFMx(); 15393 case MVT::f64: 15394 return Subtarget->useFPVFMx64(); 15395 default: 15396 break; 15397 } 15398 15399 return false; 15400 } 15401 15402 static bool isLegalT1AddressImmediate(int64_t V, EVT VT) { 15403 if (V < 0) 15404 return false; 15405 15406 unsigned Scale = 1; 15407 switch (VT.getSimpleVT().SimpleTy) { 15408 case MVT::i1: 15409 case MVT::i8: 15410 // Scale == 1; 15411 break; 15412 case MVT::i16: 15413 // Scale == 2; 15414 Scale = 2; 15415 break; 15416 default: 15417 // On thumb1 we load most things (i32, i64, floats, etc) with a LDR 15418 // Scale == 4; 15419 Scale = 4; 15420 break; 15421 } 15422 15423 if ((V & (Scale - 1)) != 0) 15424 return false; 15425 return isUInt<5>(V / Scale); 15426 } 15427 15428 static bool isLegalT2AddressImmediate(int64_t V, EVT VT, 15429 const ARMSubtarget *Subtarget) { 15430 if (!VT.isInteger() && !VT.isFloatingPoint()) 15431 return false; 15432 if (VT.isVector() && Subtarget->hasNEON()) 15433 return false; 15434 if (VT.isVector() && VT.isFloatingPoint() && Subtarget->hasMVEIntegerOps() && 15435 !Subtarget->hasMVEFloatOps()) 15436 return false; 15437 15438 bool IsNeg = false; 15439 if (V < 0) { 15440 IsNeg = true; 15441 V = -V; 15442 } 15443 15444 unsigned NumBytes = std::max((unsigned)VT.getSizeInBits() / 8, 1U); 15445 15446 // MVE: size * imm7 15447 if (VT.isVector() && Subtarget->hasMVEIntegerOps()) { 15448 switch (VT.getSimpleVT().getVectorElementType().SimpleTy) { 15449 case MVT::i32: 15450 case MVT::f32: 15451 return isShiftedUInt<7,2>(V); 15452 case MVT::i16: 15453 case MVT::f16: 15454 return isShiftedUInt<7,1>(V); 15455 case MVT::i8: 15456 return isUInt<7>(V); 15457 default: 15458 return false; 15459 } 15460 } 15461 15462 // half VLDR: 2 * imm8 15463 if (VT.isFloatingPoint() && NumBytes == 2 && Subtarget->hasFPRegs16()) 15464 return isShiftedUInt<8, 1>(V); 15465 // VLDR and LDRD: 4 * imm8 15466 if ((VT.isFloatingPoint() && Subtarget->hasVFP2Base()) || NumBytes == 8) 15467 return isShiftedUInt<8, 2>(V); 15468 15469 if (NumBytes == 1 || NumBytes == 2 || NumBytes == 4) { 15470 // + imm12 or - imm8 15471 if (IsNeg) 15472 return isUInt<8>(V); 15473 return isUInt<12>(V); 15474 } 15475 15476 return false; 15477 } 15478 15479 /// isLegalAddressImmediate - Return true if the integer value can be used 15480 /// as the offset of the target addressing mode for load / store of the 15481 /// given type. 15482 static bool isLegalAddressImmediate(int64_t V, EVT VT, 15483 const ARMSubtarget *Subtarget) { 15484 if (V == 0) 15485 return true; 15486 15487 if (!VT.isSimple()) 15488 return false; 15489 15490 if (Subtarget->isThumb1Only()) 15491 return isLegalT1AddressImmediate(V, VT); 15492 else if (Subtarget->isThumb2()) 15493 return isLegalT2AddressImmediate(V, VT, Subtarget); 15494 15495 // ARM mode. 15496 if (V < 0) 15497 V = - V; 15498 switch (VT.getSimpleVT().SimpleTy) { 15499 default: return false; 15500 case MVT::i1: 15501 case MVT::i8: 15502 case MVT::i32: 15503 // +- imm12 15504 return isUInt<12>(V); 15505 case MVT::i16: 15506 // +- imm8 15507 return isUInt<8>(V); 15508 case MVT::f32: 15509 case MVT::f64: 15510 if (!Subtarget->hasVFP2Base()) // FIXME: NEON? 15511 return false; 15512 return isShiftedUInt<8, 2>(V); 15513 } 15514 } 15515 15516 bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM, 15517 EVT VT) const { 15518 int Scale = AM.Scale; 15519 if (Scale < 0) 15520 return false; 15521 15522 switch (VT.getSimpleVT().SimpleTy) { 15523 default: return false; 15524 case MVT::i1: 15525 case MVT::i8: 15526 case MVT::i16: 15527 case MVT::i32: 15528 if (Scale == 1) 15529 return true; 15530 // r + r << imm 15531 Scale = Scale & ~1; 15532 return Scale == 2 || Scale == 4 || Scale == 8; 15533 case MVT::i64: 15534 // FIXME: What are we trying to model here? ldrd doesn't have an r + r 15535 // version in Thumb mode. 15536 // r + r 15537 if (Scale == 1) 15538 return true; 15539 // r * 2 (this can be lowered to r + r). 15540 if (!AM.HasBaseReg && Scale == 2) 15541 return true; 15542 return false; 15543 case MVT::isVoid: 15544 // Note, we allow "void" uses (basically, uses that aren't loads or 15545 // stores), because arm allows folding a scale into many arithmetic 15546 // operations. This should be made more precise and revisited later. 15547 15548 // Allow r << imm, but the imm has to be a multiple of two. 15549 if (Scale & 1) return false; 15550 return isPowerOf2_32(Scale); 15551 } 15552 } 15553 15554 bool ARMTargetLowering::isLegalT1ScaledAddressingMode(const AddrMode &AM, 15555 EVT VT) const { 15556 const int Scale = AM.Scale; 15557 15558 // Negative scales are not supported in Thumb1. 15559 if (Scale < 0) 15560 return false; 15561 15562 // Thumb1 addressing modes do not support register scaling excepting the 15563 // following cases: 15564 // 1. Scale == 1 means no scaling. 15565 // 2. Scale == 2 this can be lowered to r + r if there is no base register. 15566 return (Scale == 1) || (!AM.HasBaseReg && Scale == 2); 15567 } 15568 15569 /// isLegalAddressingMode - Return true if the addressing mode represented 15570 /// by AM is legal for this target, for a load/store of the specified type. 15571 bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL, 15572 const AddrMode &AM, Type *Ty, 15573 unsigned AS, Instruction *I) const { 15574 EVT VT = getValueType(DL, Ty, true); 15575 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget)) 15576 return false; 15577 15578 // Can never fold addr of global into load/store. 15579 if (AM.BaseGV) 15580 return false; 15581 15582 switch (AM.Scale) { 15583 case 0: // no scale reg, must be "r+i" or "r", or "i". 15584 break; 15585 default: 15586 // ARM doesn't support any R+R*scale+imm addr modes. 15587 if (AM.BaseOffs) 15588 return false; 15589 15590 if (!VT.isSimple()) 15591 return false; 15592 15593 if (Subtarget->isThumb1Only()) 15594 return isLegalT1ScaledAddressingMode(AM, VT); 15595 15596 if (Subtarget->isThumb2()) 15597 return isLegalT2ScaledAddressingMode(AM, VT); 15598 15599 int Scale = AM.Scale; 15600 switch (VT.getSimpleVT().SimpleTy) { 15601 default: return false; 15602 case MVT::i1: 15603 case MVT::i8: 15604 case MVT::i32: 15605 if (Scale < 0) Scale = -Scale; 15606 if (Scale == 1) 15607 return true; 15608 // r + r << imm 15609 return isPowerOf2_32(Scale & ~1); 15610 case MVT::i16: 15611 case MVT::i64: 15612 // r +/- r 15613 if (Scale == 1 || (AM.HasBaseReg && Scale == -1)) 15614 return true; 15615 // r * 2 (this can be lowered to r + r). 15616 if (!AM.HasBaseReg && Scale == 2) 15617 return true; 15618 return false; 15619 15620 case MVT::isVoid: 15621 // Note, we allow "void" uses (basically, uses that aren't loads or 15622 // stores), because arm allows folding a scale into many arithmetic 15623 // operations. This should be made more precise and revisited later. 15624 15625 // Allow r << imm, but the imm has to be a multiple of two. 15626 if (Scale & 1) return false; 15627 return isPowerOf2_32(Scale); 15628 } 15629 } 15630 return true; 15631 } 15632 15633 /// isLegalICmpImmediate - Return true if the specified immediate is legal 15634 /// icmp immediate, that is the target has icmp instructions which can compare 15635 /// a register against the immediate without having to materialize the 15636 /// immediate into a register. 15637 bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const { 15638 // Thumb2 and ARM modes can use cmn for negative immediates. 15639 if (!Subtarget->isThumb()) 15640 return ARM_AM::getSOImmVal((uint32_t)Imm) != -1 || 15641 ARM_AM::getSOImmVal(-(uint32_t)Imm) != -1; 15642 if (Subtarget->isThumb2()) 15643 return ARM_AM::getT2SOImmVal((uint32_t)Imm) != -1 || 15644 ARM_AM::getT2SOImmVal(-(uint32_t)Imm) != -1; 15645 // Thumb1 doesn't have cmn, and only 8-bit immediates. 15646 return Imm >= 0 && Imm <= 255; 15647 } 15648 15649 /// isLegalAddImmediate - Return true if the specified immediate is a legal add 15650 /// *or sub* immediate, that is the target has add or sub instructions which can 15651 /// add a register with the immediate without having to materialize the 15652 /// immediate into a register. 15653 bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const { 15654 // Same encoding for add/sub, just flip the sign. 15655 int64_t AbsImm = std::abs(Imm); 15656 if (!Subtarget->isThumb()) 15657 return ARM_AM::getSOImmVal(AbsImm) != -1; 15658 if (Subtarget->isThumb2()) 15659 return ARM_AM::getT2SOImmVal(AbsImm) != -1; 15660 // Thumb1 only has 8-bit unsigned immediate. 15661 return AbsImm >= 0 && AbsImm <= 255; 15662 } 15663 15664 static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT, 15665 bool isSEXTLoad, SDValue &Base, 15666 SDValue &Offset, bool &isInc, 15667 SelectionDAG &DAG) { 15668 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 15669 return false; 15670 15671 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) { 15672 // AddressingMode 3 15673 Base = Ptr->getOperand(0); 15674 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 15675 int RHSC = (int)RHS->getZExtValue(); 15676 if (RHSC < 0 && RHSC > -256) { 15677 assert(Ptr->getOpcode() == ISD::ADD); 15678 isInc = false; 15679 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 15680 return true; 15681 } 15682 } 15683 isInc = (Ptr->getOpcode() == ISD::ADD); 15684 Offset = Ptr->getOperand(1); 15685 return true; 15686 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) { 15687 // AddressingMode 2 15688 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 15689 int RHSC = (int)RHS->getZExtValue(); 15690 if (RHSC < 0 && RHSC > -0x1000) { 15691 assert(Ptr->getOpcode() == ISD::ADD); 15692 isInc = false; 15693 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 15694 Base = Ptr->getOperand(0); 15695 return true; 15696 } 15697 } 15698 15699 if (Ptr->getOpcode() == ISD::ADD) { 15700 isInc = true; 15701 ARM_AM::ShiftOpc ShOpcVal= 15702 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode()); 15703 if (ShOpcVal != ARM_AM::no_shift) { 15704 Base = Ptr->getOperand(1); 15705 Offset = Ptr->getOperand(0); 15706 } else { 15707 Base = Ptr->getOperand(0); 15708 Offset = Ptr->getOperand(1); 15709 } 15710 return true; 15711 } 15712 15713 isInc = (Ptr->getOpcode() == ISD::ADD); 15714 Base = Ptr->getOperand(0); 15715 Offset = Ptr->getOperand(1); 15716 return true; 15717 } 15718 15719 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store. 15720 return false; 15721 } 15722 15723 static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT, 15724 bool isSEXTLoad, SDValue &Base, 15725 SDValue &Offset, bool &isInc, 15726 SelectionDAG &DAG) { 15727 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 15728 return false; 15729 15730 Base = Ptr->getOperand(0); 15731 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 15732 int RHSC = (int)RHS->getZExtValue(); 15733 if (RHSC < 0 && RHSC > -0x100) { // 8 bits. 15734 assert(Ptr->getOpcode() == ISD::ADD); 15735 isInc = false; 15736 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 15737 return true; 15738 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero. 15739 isInc = Ptr->getOpcode() == ISD::ADD; 15740 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0)); 15741 return true; 15742 } 15743 } 15744 15745 return false; 15746 } 15747 15748 static bool getMVEIndexedAddressParts(SDNode *Ptr, EVT VT, unsigned Align, 15749 bool isSEXTLoad, bool IsMasked, bool isLE, 15750 SDValue &Base, SDValue &Offset, 15751 bool &isInc, SelectionDAG &DAG) { 15752 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 15753 return false; 15754 if (!isa<ConstantSDNode>(Ptr->getOperand(1))) 15755 return false; 15756 15757 // We allow LE non-masked loads to change the type (for example use a vldrb.8 15758 // as opposed to a vldrw.32). This can allow extra addressing modes or 15759 // alignments for what is otherwise an equivalent instruction. 15760 bool CanChangeType = isLE && !IsMasked; 15761 15762 ConstantSDNode *RHS = cast<ConstantSDNode>(Ptr->getOperand(1)); 15763 int RHSC = (int)RHS->getZExtValue(); 15764 15765 auto IsInRange = [&](int RHSC, int Limit, int Scale) { 15766 if (RHSC < 0 && RHSC > -Limit * Scale && RHSC % Scale == 0) { 15767 assert(Ptr->getOpcode() == ISD::ADD); 15768 isInc = false; 15769 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 15770 return true; 15771 } else if (RHSC > 0 && RHSC < Limit * Scale && RHSC % Scale == 0) { 15772 isInc = Ptr->getOpcode() == ISD::ADD; 15773 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0)); 15774 return true; 15775 } 15776 return false; 15777 }; 15778 15779 // Try to find a matching instruction based on s/zext, Alignment, Offset and 15780 // (in BE/masked) type. 15781 Base = Ptr->getOperand(0); 15782 if (VT == MVT::v4i16) { 15783 if (Align >= 2 && IsInRange(RHSC, 0x80, 2)) 15784 return true; 15785 } else if (VT == MVT::v4i8 || VT == MVT::v8i8) { 15786 if (IsInRange(RHSC, 0x80, 1)) 15787 return true; 15788 } else if (Align >= 4 && 15789 (CanChangeType || VT == MVT::v4i32 || VT == MVT::v4f32) && 15790 IsInRange(RHSC, 0x80, 4)) 15791 return true; 15792 else if (Align >= 2 && 15793 (CanChangeType || VT == MVT::v8i16 || VT == MVT::v8f16) && 15794 IsInRange(RHSC, 0x80, 2)) 15795 return true; 15796 else if ((CanChangeType || VT == MVT::v16i8) && IsInRange(RHSC, 0x80, 1)) 15797 return true; 15798 return false; 15799 } 15800 15801 /// getPreIndexedAddressParts - returns true by value, base pointer and 15802 /// offset pointer and addressing mode by reference if the node's address 15803 /// can be legally represented as pre-indexed load / store address. 15804 bool 15805 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, 15806 SDValue &Offset, 15807 ISD::MemIndexedMode &AM, 15808 SelectionDAG &DAG) const { 15809 if (Subtarget->isThumb1Only()) 15810 return false; 15811 15812 EVT VT; 15813 SDValue Ptr; 15814 unsigned Align; 15815 bool isSEXTLoad = false; 15816 bool IsMasked = false; 15817 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 15818 Ptr = LD->getBasePtr(); 15819 VT = LD->getMemoryVT(); 15820 Align = LD->getAlignment(); 15821 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 15822 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 15823 Ptr = ST->getBasePtr(); 15824 VT = ST->getMemoryVT(); 15825 Align = ST->getAlignment(); 15826 } else if (MaskedLoadSDNode *LD = dyn_cast<MaskedLoadSDNode>(N)) { 15827 Ptr = LD->getBasePtr(); 15828 VT = LD->getMemoryVT(); 15829 Align = LD->getAlignment(); 15830 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 15831 IsMasked = true; 15832 } else if (MaskedStoreSDNode *ST = dyn_cast<MaskedStoreSDNode>(N)) { 15833 Ptr = ST->getBasePtr(); 15834 VT = ST->getMemoryVT(); 15835 Align = ST->getAlignment(); 15836 IsMasked = true; 15837 } else 15838 return false; 15839 15840 bool isInc; 15841 bool isLegal = false; 15842 if (VT.isVector()) 15843 isLegal = Subtarget->hasMVEIntegerOps() && 15844 getMVEIndexedAddressParts(Ptr.getNode(), VT, Align, isSEXTLoad, 15845 IsMasked, Subtarget->isLittle(), Base, 15846 Offset, isInc, DAG); 15847 else { 15848 if (Subtarget->isThumb2()) 15849 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 15850 Offset, isInc, DAG); 15851 else 15852 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 15853 Offset, isInc, DAG); 15854 } 15855 if (!isLegal) 15856 return false; 15857 15858 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC; 15859 return true; 15860 } 15861 15862 /// getPostIndexedAddressParts - returns true by value, base pointer and 15863 /// offset pointer and addressing mode by reference if this node can be 15864 /// combined with a load / store to form a post-indexed load / store. 15865 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op, 15866 SDValue &Base, 15867 SDValue &Offset, 15868 ISD::MemIndexedMode &AM, 15869 SelectionDAG &DAG) const { 15870 EVT VT; 15871 SDValue Ptr; 15872 unsigned Align; 15873 bool isSEXTLoad = false, isNonExt; 15874 bool IsMasked = false; 15875 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 15876 VT = LD->getMemoryVT(); 15877 Ptr = LD->getBasePtr(); 15878 Align = LD->getAlignment(); 15879 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 15880 isNonExt = LD->getExtensionType() == ISD::NON_EXTLOAD; 15881 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 15882 VT = ST->getMemoryVT(); 15883 Ptr = ST->getBasePtr(); 15884 Align = ST->getAlignment(); 15885 isNonExt = !ST->isTruncatingStore(); 15886 } else if (MaskedLoadSDNode *LD = dyn_cast<MaskedLoadSDNode>(N)) { 15887 VT = LD->getMemoryVT(); 15888 Ptr = LD->getBasePtr(); 15889 Align = LD->getAlignment(); 15890 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 15891 isNonExt = LD->getExtensionType() == ISD::NON_EXTLOAD; 15892 IsMasked = true; 15893 } else if (MaskedStoreSDNode *ST = dyn_cast<MaskedStoreSDNode>(N)) { 15894 VT = ST->getMemoryVT(); 15895 Ptr = ST->getBasePtr(); 15896 Align = ST->getAlignment(); 15897 isNonExt = !ST->isTruncatingStore(); 15898 IsMasked = true; 15899 } else 15900 return false; 15901 15902 if (Subtarget->isThumb1Only()) { 15903 // Thumb-1 can do a limited post-inc load or store as an updating LDM. It 15904 // must be non-extending/truncating, i32, with an offset of 4. 15905 assert(Op->getValueType(0) == MVT::i32 && "Non-i32 post-inc op?!"); 15906 if (Op->getOpcode() != ISD::ADD || !isNonExt) 15907 return false; 15908 auto *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1)); 15909 if (!RHS || RHS->getZExtValue() != 4) 15910 return false; 15911 15912 Offset = Op->getOperand(1); 15913 Base = Op->getOperand(0); 15914 AM = ISD::POST_INC; 15915 return true; 15916 } 15917 15918 bool isInc; 15919 bool isLegal = false; 15920 if (VT.isVector()) 15921 isLegal = Subtarget->hasMVEIntegerOps() && 15922 getMVEIndexedAddressParts(Op, VT, Align, isSEXTLoad, IsMasked, 15923 Subtarget->isLittle(), Base, Offset, 15924 isInc, DAG); 15925 else { 15926 if (Subtarget->isThumb2()) 15927 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 15928 isInc, DAG); 15929 else 15930 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 15931 isInc, DAG); 15932 } 15933 if (!isLegal) 15934 return false; 15935 15936 if (Ptr != Base) { 15937 // Swap base ptr and offset to catch more post-index load / store when 15938 // it's legal. In Thumb2 mode, offset must be an immediate. 15939 if (Ptr == Offset && Op->getOpcode() == ISD::ADD && 15940 !Subtarget->isThumb2()) 15941 std::swap(Base, Offset); 15942 15943 // Post-indexed load / store update the base pointer. 15944 if (Ptr != Base) 15945 return false; 15946 } 15947 15948 AM = isInc ? ISD::POST_INC : ISD::POST_DEC; 15949 return true; 15950 } 15951 15952 void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, 15953 KnownBits &Known, 15954 const APInt &DemandedElts, 15955 const SelectionDAG &DAG, 15956 unsigned Depth) const { 15957 unsigned BitWidth = Known.getBitWidth(); 15958 Known.resetAll(); 15959 switch (Op.getOpcode()) { 15960 default: break; 15961 case ARMISD::ADDC: 15962 case ARMISD::ADDE: 15963 case ARMISD::SUBC: 15964 case ARMISD::SUBE: 15965 // Special cases when we convert a carry to a boolean. 15966 if (Op.getResNo() == 0) { 15967 SDValue LHS = Op.getOperand(0); 15968 SDValue RHS = Op.getOperand(1); 15969 // (ADDE 0, 0, C) will give us a single bit. 15970 if (Op->getOpcode() == ARMISD::ADDE && isNullConstant(LHS) && 15971 isNullConstant(RHS)) { 15972 Known.Zero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1); 15973 return; 15974 } 15975 } 15976 break; 15977 case ARMISD::CMOV: { 15978 // Bits are known zero/one if known on the LHS and RHS. 15979 Known = DAG.computeKnownBits(Op.getOperand(0), Depth+1); 15980 if (Known.isUnknown()) 15981 return; 15982 15983 KnownBits KnownRHS = DAG.computeKnownBits(Op.getOperand(1), Depth+1); 15984 Known.Zero &= KnownRHS.Zero; 15985 Known.One &= KnownRHS.One; 15986 return; 15987 } 15988 case ISD::INTRINSIC_W_CHAIN: { 15989 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1)); 15990 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue()); 15991 switch (IntID) { 15992 default: return; 15993 case Intrinsic::arm_ldaex: 15994 case Intrinsic::arm_ldrex: { 15995 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT(); 15996 unsigned MemBits = VT.getScalarSizeInBits(); 15997 Known.Zero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits); 15998 return; 15999 } 16000 } 16001 } 16002 case ARMISD::BFI: { 16003 // Conservatively, we can recurse down the first operand 16004 // and just mask out all affected bits. 16005 Known = DAG.computeKnownBits(Op.getOperand(0), Depth + 1); 16006 16007 // The operand to BFI is already a mask suitable for removing the bits it 16008 // sets. 16009 ConstantSDNode *CI = cast<ConstantSDNode>(Op.getOperand(2)); 16010 const APInt &Mask = CI->getAPIntValue(); 16011 Known.Zero &= Mask; 16012 Known.One &= Mask; 16013 return; 16014 } 16015 case ARMISD::VGETLANEs: 16016 case ARMISD::VGETLANEu: { 16017 const SDValue &SrcSV = Op.getOperand(0); 16018 EVT VecVT = SrcSV.getValueType(); 16019 assert(VecVT.isVector() && "VGETLANE expected a vector type"); 16020 const unsigned NumSrcElts = VecVT.getVectorNumElements(); 16021 ConstantSDNode *Pos = cast<ConstantSDNode>(Op.getOperand(1).getNode()); 16022 assert(Pos->getAPIntValue().ult(NumSrcElts) && 16023 "VGETLANE index out of bounds"); 16024 unsigned Idx = Pos->getZExtValue(); 16025 APInt DemandedElt = APInt::getOneBitSet(NumSrcElts, Idx); 16026 Known = DAG.computeKnownBits(SrcSV, DemandedElt, Depth + 1); 16027 16028 EVT VT = Op.getValueType(); 16029 const unsigned DstSz = VT.getScalarSizeInBits(); 16030 const unsigned SrcSz = VecVT.getVectorElementType().getSizeInBits(); 16031 (void)SrcSz; 16032 assert(SrcSz == Known.getBitWidth()); 16033 assert(DstSz > SrcSz); 16034 if (Op.getOpcode() == ARMISD::VGETLANEs) 16035 Known = Known.sext(DstSz); 16036 else { 16037 Known = Known.zext(DstSz); 16038 } 16039 assert(DstSz == Known.getBitWidth()); 16040 break; 16041 } 16042 } 16043 } 16044 16045 bool 16046 ARMTargetLowering::targetShrinkDemandedConstant(SDValue Op, 16047 const APInt &DemandedAPInt, 16048 TargetLoweringOpt &TLO) const { 16049 // Delay optimization, so we don't have to deal with illegal types, or block 16050 // optimizations. 16051 if (!TLO.LegalOps) 16052 return false; 16053 16054 // Only optimize AND for now. 16055 if (Op.getOpcode() != ISD::AND) 16056 return false; 16057 16058 EVT VT = Op.getValueType(); 16059 16060 // Ignore vectors. 16061 if (VT.isVector()) 16062 return false; 16063 16064 assert(VT == MVT::i32 && "Unexpected integer type"); 16065 16066 // Make sure the RHS really is a constant. 16067 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); 16068 if (!C) 16069 return false; 16070 16071 unsigned Mask = C->getZExtValue(); 16072 16073 unsigned Demanded = DemandedAPInt.getZExtValue(); 16074 unsigned ShrunkMask = Mask & Demanded; 16075 unsigned ExpandedMask = Mask | ~Demanded; 16076 16077 // If the mask is all zeros, let the target-independent code replace the 16078 // result with zero. 16079 if (ShrunkMask == 0) 16080 return false; 16081 16082 // If the mask is all ones, erase the AND. (Currently, the target-independent 16083 // code won't do this, so we have to do it explicitly to avoid an infinite 16084 // loop in obscure cases.) 16085 if (ExpandedMask == ~0U) 16086 return TLO.CombineTo(Op, Op.getOperand(0)); 16087 16088 auto IsLegalMask = [ShrunkMask, ExpandedMask](unsigned Mask) -> bool { 16089 return (ShrunkMask & Mask) == ShrunkMask && (~ExpandedMask & Mask) == 0; 16090 }; 16091 auto UseMask = [Mask, Op, VT, &TLO](unsigned NewMask) -> bool { 16092 if (NewMask == Mask) 16093 return true; 16094 SDLoc DL(Op); 16095 SDValue NewC = TLO.DAG.getConstant(NewMask, DL, VT); 16096 SDValue NewOp = TLO.DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), NewC); 16097 return TLO.CombineTo(Op, NewOp); 16098 }; 16099 16100 // Prefer uxtb mask. 16101 if (IsLegalMask(0xFF)) 16102 return UseMask(0xFF); 16103 16104 // Prefer uxth mask. 16105 if (IsLegalMask(0xFFFF)) 16106 return UseMask(0xFFFF); 16107 16108 // [1, 255] is Thumb1 movs+ands, legal immediate for ARM/Thumb2. 16109 // FIXME: Prefer a contiguous sequence of bits for other optimizations. 16110 if (ShrunkMask < 256) 16111 return UseMask(ShrunkMask); 16112 16113 // [-256, -2] is Thumb1 movs+bics, legal immediate for ARM/Thumb2. 16114 // FIXME: Prefer a contiguous sequence of bits for other optimizations. 16115 if ((int)ExpandedMask <= -2 && (int)ExpandedMask >= -256) 16116 return UseMask(ExpandedMask); 16117 16118 // Potential improvements: 16119 // 16120 // We could try to recognize lsls+lsrs or lsrs+lsls pairs here. 16121 // We could try to prefer Thumb1 immediates which can be lowered to a 16122 // two-instruction sequence. 16123 // We could try to recognize more legal ARM/Thumb2 immediates here. 16124 16125 return false; 16126 } 16127 16128 16129 //===----------------------------------------------------------------------===// 16130 // ARM Inline Assembly Support 16131 //===----------------------------------------------------------------------===// 16132 16133 bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const { 16134 // Looking for "rev" which is V6+. 16135 if (!Subtarget->hasV6Ops()) 16136 return false; 16137 16138 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue()); 16139 std::string AsmStr = IA->getAsmString(); 16140 SmallVector<StringRef, 4> AsmPieces; 16141 SplitString(AsmStr, AsmPieces, ";\n"); 16142 16143 switch (AsmPieces.size()) { 16144 default: return false; 16145 case 1: 16146 AsmStr = std::string(AsmPieces[0]); 16147 AsmPieces.clear(); 16148 SplitString(AsmStr, AsmPieces, " \t,"); 16149 16150 // rev $0, $1 16151 if (AsmPieces.size() == 3 && 16152 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" && 16153 IA->getConstraintString().compare(0, 4, "=l,l") == 0) { 16154 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType()); 16155 if (Ty && Ty->getBitWidth() == 32) 16156 return IntrinsicLowering::LowerToByteSwap(CI); 16157 } 16158 break; 16159 } 16160 16161 return false; 16162 } 16163 16164 const char *ARMTargetLowering::LowerXConstraint(EVT ConstraintVT) const { 16165 // At this point, we have to lower this constraint to something else, so we 16166 // lower it to an "r" or "w". However, by doing this we will force the result 16167 // to be in register, while the X constraint is much more permissive. 16168 // 16169 // Although we are correct (we are free to emit anything, without 16170 // constraints), we might break use cases that would expect us to be more 16171 // efficient and emit something else. 16172 if (!Subtarget->hasVFP2Base()) 16173 return "r"; 16174 if (ConstraintVT.isFloatingPoint()) 16175 return "w"; 16176 if (ConstraintVT.isVector() && Subtarget->hasNEON() && 16177 (ConstraintVT.getSizeInBits() == 64 || 16178 ConstraintVT.getSizeInBits() == 128)) 16179 return "w"; 16180 16181 return "r"; 16182 } 16183 16184 /// getConstraintType - Given a constraint letter, return the type of 16185 /// constraint it is for this target. 16186 ARMTargetLowering::ConstraintType 16187 ARMTargetLowering::getConstraintType(StringRef Constraint) const { 16188 unsigned S = Constraint.size(); 16189 if (S == 1) { 16190 switch (Constraint[0]) { 16191 default: break; 16192 case 'l': return C_RegisterClass; 16193 case 'w': return C_RegisterClass; 16194 case 'h': return C_RegisterClass; 16195 case 'x': return C_RegisterClass; 16196 case 't': return C_RegisterClass; 16197 case 'j': return C_Immediate; // Constant for movw. 16198 // An address with a single base register. Due to the way we 16199 // currently handle addresses it is the same as an 'r' memory constraint. 16200 case 'Q': return C_Memory; 16201 } 16202 } else if (S == 2) { 16203 switch (Constraint[0]) { 16204 default: break; 16205 case 'T': return C_RegisterClass; 16206 // All 'U+' constraints are addresses. 16207 case 'U': return C_Memory; 16208 } 16209 } 16210 return TargetLowering::getConstraintType(Constraint); 16211 } 16212 16213 /// Examine constraint type and operand type and determine a weight value. 16214 /// This object must already have been set up with the operand type 16215 /// and the current alternative constraint selected. 16216 TargetLowering::ConstraintWeight 16217 ARMTargetLowering::getSingleConstraintMatchWeight( 16218 AsmOperandInfo &info, const char *constraint) const { 16219 ConstraintWeight weight = CW_Invalid; 16220 Value *CallOperandVal = info.CallOperandVal; 16221 // If we don't have a value, we can't do a match, 16222 // but allow it at the lowest weight. 16223 if (!CallOperandVal) 16224 return CW_Default; 16225 Type *type = CallOperandVal->getType(); 16226 // Look at the constraint type. 16227 switch (*constraint) { 16228 default: 16229 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 16230 break; 16231 case 'l': 16232 if (type->isIntegerTy()) { 16233 if (Subtarget->isThumb()) 16234 weight = CW_SpecificReg; 16235 else 16236 weight = CW_Register; 16237 } 16238 break; 16239 case 'w': 16240 if (type->isFloatingPointTy()) 16241 weight = CW_Register; 16242 break; 16243 } 16244 return weight; 16245 } 16246 16247 using RCPair = std::pair<unsigned, const TargetRegisterClass *>; 16248 16249 RCPair ARMTargetLowering::getRegForInlineAsmConstraint( 16250 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const { 16251 switch (Constraint.size()) { 16252 case 1: 16253 // GCC ARM Constraint Letters 16254 switch (Constraint[0]) { 16255 case 'l': // Low regs or general regs. 16256 if (Subtarget->isThumb()) 16257 return RCPair(0U, &ARM::tGPRRegClass); 16258 return RCPair(0U, &ARM::GPRRegClass); 16259 case 'h': // High regs or no regs. 16260 if (Subtarget->isThumb()) 16261 return RCPair(0U, &ARM::hGPRRegClass); 16262 break; 16263 case 'r': 16264 if (Subtarget->isThumb1Only()) 16265 return RCPair(0U, &ARM::tGPRRegClass); 16266 return RCPair(0U, &ARM::GPRRegClass); 16267 case 'w': 16268 if (VT == MVT::Other) 16269 break; 16270 if (VT == MVT::f32) 16271 return RCPair(0U, &ARM::SPRRegClass); 16272 if (VT.getSizeInBits() == 64) 16273 return RCPair(0U, &ARM::DPRRegClass); 16274 if (VT.getSizeInBits() == 128) 16275 return RCPair(0U, &ARM::QPRRegClass); 16276 break; 16277 case 'x': 16278 if (VT == MVT::Other) 16279 break; 16280 if (VT == MVT::f32) 16281 return RCPair(0U, &ARM::SPR_8RegClass); 16282 if (VT.getSizeInBits() == 64) 16283 return RCPair(0U, &ARM::DPR_8RegClass); 16284 if (VT.getSizeInBits() == 128) 16285 return RCPair(0U, &ARM::QPR_8RegClass); 16286 break; 16287 case 't': 16288 if (VT == MVT::Other) 16289 break; 16290 if (VT == MVT::f32 || VT == MVT::i32) 16291 return RCPair(0U, &ARM::SPRRegClass); 16292 if (VT.getSizeInBits() == 64) 16293 return RCPair(0U, &ARM::DPR_VFP2RegClass); 16294 if (VT.getSizeInBits() == 128) 16295 return RCPair(0U, &ARM::QPR_VFP2RegClass); 16296 break; 16297 } 16298 break; 16299 16300 case 2: 16301 if (Constraint[0] == 'T') { 16302 switch (Constraint[1]) { 16303 default: 16304 break; 16305 case 'e': 16306 return RCPair(0U, &ARM::tGPREvenRegClass); 16307 case 'o': 16308 return RCPair(0U, &ARM::tGPROddRegClass); 16309 } 16310 } 16311 break; 16312 16313 default: 16314 break; 16315 } 16316 16317 if (StringRef("{cc}").equals_lower(Constraint)) 16318 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass); 16319 16320 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 16321 } 16322 16323 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 16324 /// vector. If it is invalid, don't add anything to Ops. 16325 void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op, 16326 std::string &Constraint, 16327 std::vector<SDValue>&Ops, 16328 SelectionDAG &DAG) const { 16329 SDValue Result; 16330 16331 // Currently only support length 1 constraints. 16332 if (Constraint.length() != 1) return; 16333 16334 char ConstraintLetter = Constraint[0]; 16335 switch (ConstraintLetter) { 16336 default: break; 16337 case 'j': 16338 case 'I': case 'J': case 'K': case 'L': 16339 case 'M': case 'N': case 'O': 16340 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 16341 if (!C) 16342 return; 16343 16344 int64_t CVal64 = C->getSExtValue(); 16345 int CVal = (int) CVal64; 16346 // None of these constraints allow values larger than 32 bits. Check 16347 // that the value fits in an int. 16348 if (CVal != CVal64) 16349 return; 16350 16351 switch (ConstraintLetter) { 16352 case 'j': 16353 // Constant suitable for movw, must be between 0 and 16354 // 65535. 16355 if (Subtarget->hasV6T2Ops() || (Subtarget->hasV8MBaselineOps())) 16356 if (CVal >= 0 && CVal <= 65535) 16357 break; 16358 return; 16359 case 'I': 16360 if (Subtarget->isThumb1Only()) { 16361 // This must be a constant between 0 and 255, for ADD 16362 // immediates. 16363 if (CVal >= 0 && CVal <= 255) 16364 break; 16365 } else if (Subtarget->isThumb2()) { 16366 // A constant that can be used as an immediate value in a 16367 // data-processing instruction. 16368 if (ARM_AM::getT2SOImmVal(CVal) != -1) 16369 break; 16370 } else { 16371 // A constant that can be used as an immediate value in a 16372 // data-processing instruction. 16373 if (ARM_AM::getSOImmVal(CVal) != -1) 16374 break; 16375 } 16376 return; 16377 16378 case 'J': 16379 if (Subtarget->isThumb1Only()) { 16380 // This must be a constant between -255 and -1, for negated ADD 16381 // immediates. This can be used in GCC with an "n" modifier that 16382 // prints the negated value, for use with SUB instructions. It is 16383 // not useful otherwise but is implemented for compatibility. 16384 if (CVal >= -255 && CVal <= -1) 16385 break; 16386 } else { 16387 // This must be a constant between -4095 and 4095. It is not clear 16388 // what this constraint is intended for. Implemented for 16389 // compatibility with GCC. 16390 if (CVal >= -4095 && CVal <= 4095) 16391 break; 16392 } 16393 return; 16394 16395 case 'K': 16396 if (Subtarget->isThumb1Only()) { 16397 // A 32-bit value where only one byte has a nonzero value. Exclude 16398 // zero to match GCC. This constraint is used by GCC internally for 16399 // constants that can be loaded with a move/shift combination. 16400 // It is not useful otherwise but is implemented for compatibility. 16401 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal)) 16402 break; 16403 } else if (Subtarget->isThumb2()) { 16404 // A constant whose bitwise inverse can be used as an immediate 16405 // value in a data-processing instruction. This can be used in GCC 16406 // with a "B" modifier that prints the inverted value, for use with 16407 // BIC and MVN instructions. It is not useful otherwise but is 16408 // implemented for compatibility. 16409 if (ARM_AM::getT2SOImmVal(~CVal) != -1) 16410 break; 16411 } else { 16412 // A constant whose bitwise inverse can be used as an immediate 16413 // value in a data-processing instruction. This can be used in GCC 16414 // with a "B" modifier that prints the inverted value, for use with 16415 // BIC and MVN instructions. It is not useful otherwise but is 16416 // implemented for compatibility. 16417 if (ARM_AM::getSOImmVal(~CVal) != -1) 16418 break; 16419 } 16420 return; 16421 16422 case 'L': 16423 if (Subtarget->isThumb1Only()) { 16424 // This must be a constant between -7 and 7, 16425 // for 3-operand ADD/SUB immediate instructions. 16426 if (CVal >= -7 && CVal < 7) 16427 break; 16428 } else if (Subtarget->isThumb2()) { 16429 // A constant whose negation can be used as an immediate value in a 16430 // data-processing instruction. This can be used in GCC with an "n" 16431 // modifier that prints the negated value, for use with SUB 16432 // instructions. It is not useful otherwise but is implemented for 16433 // compatibility. 16434 if (ARM_AM::getT2SOImmVal(-CVal) != -1) 16435 break; 16436 } else { 16437 // A constant whose negation can be used as an immediate value in a 16438 // data-processing instruction. This can be used in GCC with an "n" 16439 // modifier that prints the negated value, for use with SUB 16440 // instructions. It is not useful otherwise but is implemented for 16441 // compatibility. 16442 if (ARM_AM::getSOImmVal(-CVal) != -1) 16443 break; 16444 } 16445 return; 16446 16447 case 'M': 16448 if (Subtarget->isThumb1Only()) { 16449 // This must be a multiple of 4 between 0 and 1020, for 16450 // ADD sp + immediate. 16451 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0)) 16452 break; 16453 } else { 16454 // A power of two or a constant between 0 and 32. This is used in 16455 // GCC for the shift amount on shifted register operands, but it is 16456 // useful in general for any shift amounts. 16457 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0)) 16458 break; 16459 } 16460 return; 16461 16462 case 'N': 16463 if (Subtarget->isThumb1Only()) { 16464 // This must be a constant between 0 and 31, for shift amounts. 16465 if (CVal >= 0 && CVal <= 31) 16466 break; 16467 } 16468 return; 16469 16470 case 'O': 16471 if (Subtarget->isThumb1Only()) { 16472 // This must be a multiple of 4 between -508 and 508, for 16473 // ADD/SUB sp = sp + immediate. 16474 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0)) 16475 break; 16476 } 16477 return; 16478 } 16479 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType()); 16480 break; 16481 } 16482 16483 if (Result.getNode()) { 16484 Ops.push_back(Result); 16485 return; 16486 } 16487 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 16488 } 16489 16490 static RTLIB::Libcall getDivRemLibcall( 16491 const SDNode *N, MVT::SimpleValueType SVT) { 16492 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM || 16493 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) && 16494 "Unhandled Opcode in getDivRemLibcall"); 16495 bool isSigned = N->getOpcode() == ISD::SDIVREM || 16496 N->getOpcode() == ISD::SREM; 16497 RTLIB::Libcall LC; 16498 switch (SVT) { 16499 default: llvm_unreachable("Unexpected request for libcall!"); 16500 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break; 16501 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break; 16502 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break; 16503 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break; 16504 } 16505 return LC; 16506 } 16507 16508 static TargetLowering::ArgListTy getDivRemArgList( 16509 const SDNode *N, LLVMContext *Context, const ARMSubtarget *Subtarget) { 16510 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM || 16511 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) && 16512 "Unhandled Opcode in getDivRemArgList"); 16513 bool isSigned = N->getOpcode() == ISD::SDIVREM || 16514 N->getOpcode() == ISD::SREM; 16515 TargetLowering::ArgListTy Args; 16516 TargetLowering::ArgListEntry Entry; 16517 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 16518 EVT ArgVT = N->getOperand(i).getValueType(); 16519 Type *ArgTy = ArgVT.getTypeForEVT(*Context); 16520 Entry.Node = N->getOperand(i); 16521 Entry.Ty = ArgTy; 16522 Entry.IsSExt = isSigned; 16523 Entry.IsZExt = !isSigned; 16524 Args.push_back(Entry); 16525 } 16526 if (Subtarget->isTargetWindows() && Args.size() >= 2) 16527 std::swap(Args[0], Args[1]); 16528 return Args; 16529 } 16530 16531 SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const { 16532 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() || 16533 Subtarget->isTargetGNUAEABI() || Subtarget->isTargetMuslAEABI() || 16534 Subtarget->isTargetWindows()) && 16535 "Register-based DivRem lowering only"); 16536 unsigned Opcode = Op->getOpcode(); 16537 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) && 16538 "Invalid opcode for Div/Rem lowering"); 16539 bool isSigned = (Opcode == ISD::SDIVREM); 16540 EVT VT = Op->getValueType(0); 16541 Type *Ty = VT.getTypeForEVT(*DAG.getContext()); 16542 SDLoc dl(Op); 16543 16544 // If the target has hardware divide, use divide + multiply + subtract: 16545 // div = a / b 16546 // rem = a - b * div 16547 // return {div, rem} 16548 // This should be lowered into UDIV/SDIV + MLS later on. 16549 bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivideInThumbMode() 16550 : Subtarget->hasDivideInARMMode(); 16551 if (hasDivide && Op->getValueType(0).isSimple() && 16552 Op->getSimpleValueType(0) == MVT::i32) { 16553 unsigned DivOpcode = isSigned ? ISD::SDIV : ISD::UDIV; 16554 const SDValue Dividend = Op->getOperand(0); 16555 const SDValue Divisor = Op->getOperand(1); 16556 SDValue Div = DAG.getNode(DivOpcode, dl, VT, Dividend, Divisor); 16557 SDValue Mul = DAG.getNode(ISD::MUL, dl, VT, Div, Divisor); 16558 SDValue Rem = DAG.getNode(ISD::SUB, dl, VT, Dividend, Mul); 16559 16560 SDValue Values[2] = {Div, Rem}; 16561 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(VT, VT), Values); 16562 } 16563 16564 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(), 16565 VT.getSimpleVT().SimpleTy); 16566 SDValue InChain = DAG.getEntryNode(); 16567 16568 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(), 16569 DAG.getContext(), 16570 Subtarget); 16571 16572 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), 16573 getPointerTy(DAG.getDataLayout())); 16574 16575 Type *RetTy = StructType::get(Ty, Ty); 16576 16577 if (Subtarget->isTargetWindows()) 16578 InChain = WinDBZCheckDenominator(DAG, Op.getNode(), InChain); 16579 16580 TargetLowering::CallLoweringInfo CLI(DAG); 16581 CLI.setDebugLoc(dl).setChain(InChain) 16582 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args)) 16583 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned); 16584 16585 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI); 16586 return CallInfo.first; 16587 } 16588 16589 // Lowers REM using divmod helpers 16590 // see RTABI section 4.2/4.3 16591 SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const { 16592 // Build return types (div and rem) 16593 std::vector<Type*> RetTyParams; 16594 Type *RetTyElement; 16595 16596 switch (N->getValueType(0).getSimpleVT().SimpleTy) { 16597 default: llvm_unreachable("Unexpected request for libcall!"); 16598 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break; 16599 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break; 16600 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break; 16601 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break; 16602 } 16603 16604 RetTyParams.push_back(RetTyElement); 16605 RetTyParams.push_back(RetTyElement); 16606 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams); 16607 Type *RetTy = StructType::get(*DAG.getContext(), ret); 16608 16609 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT(). 16610 SimpleTy); 16611 SDValue InChain = DAG.getEntryNode(); 16612 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext(), 16613 Subtarget); 16614 bool isSigned = N->getOpcode() == ISD::SREM; 16615 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), 16616 getPointerTy(DAG.getDataLayout())); 16617 16618 if (Subtarget->isTargetWindows()) 16619 InChain = WinDBZCheckDenominator(DAG, N, InChain); 16620 16621 // Lower call 16622 CallLoweringInfo CLI(DAG); 16623 CLI.setChain(InChain) 16624 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args)) 16625 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N)); 16626 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 16627 16628 // Return second (rem) result operand (first contains div) 16629 SDNode *ResNode = CallResult.first.getNode(); 16630 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands"); 16631 return ResNode->getOperand(1); 16632 } 16633 16634 SDValue 16635 ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const { 16636 assert(Subtarget->isTargetWindows() && "unsupported target platform"); 16637 SDLoc DL(Op); 16638 16639 // Get the inputs. 16640 SDValue Chain = Op.getOperand(0); 16641 SDValue Size = Op.getOperand(1); 16642 16643 if (DAG.getMachineFunction().getFunction().hasFnAttribute( 16644 "no-stack-arg-probe")) { 16645 unsigned Align = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue(); 16646 SDValue SP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32); 16647 Chain = SP.getValue(1); 16648 SP = DAG.getNode(ISD::SUB, DL, MVT::i32, SP, Size); 16649 if (Align) 16650 SP = DAG.getNode(ISD::AND, DL, MVT::i32, SP.getValue(0), 16651 DAG.getConstant(-(uint64_t)Align, DL, MVT::i32)); 16652 Chain = DAG.getCopyToReg(Chain, DL, ARM::SP, SP); 16653 SDValue Ops[2] = { SP, Chain }; 16654 return DAG.getMergeValues(Ops, DL); 16655 } 16656 16657 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size, 16658 DAG.getConstant(2, DL, MVT::i32)); 16659 16660 SDValue Flag; 16661 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag); 16662 Flag = Chain.getValue(1); 16663 16664 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 16665 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag); 16666 16667 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32); 16668 Chain = NewSP.getValue(1); 16669 16670 SDValue Ops[2] = { NewSP, Chain }; 16671 return DAG.getMergeValues(Ops, DL); 16672 } 16673 16674 SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const { 16675 bool IsStrict = Op->isStrictFPOpcode(); 16676 SDValue SrcVal = Op.getOperand(IsStrict ? 1 : 0); 16677 const unsigned DstSz = Op.getValueType().getSizeInBits(); 16678 const unsigned SrcSz = SrcVal.getValueType().getSizeInBits(); 16679 assert(DstSz > SrcSz && DstSz <= 64 && SrcSz >= 16 && 16680 "Unexpected type for custom-lowering FP_EXTEND"); 16681 16682 assert((!Subtarget->hasFP64() || !Subtarget->hasFPARMv8Base()) && 16683 "With both FP DP and 16, any FP conversion is legal!"); 16684 16685 assert(!(DstSz == 32 && Subtarget->hasFP16()) && 16686 "With FP16, 16 to 32 conversion is legal!"); 16687 16688 // Converting from 32 -> 64 is valid if we have FP64. 16689 if (SrcSz == 32 && DstSz == 64 && Subtarget->hasFP64()) { 16690 // FIXME: Remove this when we have strict fp instruction selection patterns 16691 if (IsStrict) { 16692 SDLoc Loc(Op); 16693 SDValue Result = DAG.getNode(ISD::FP_EXTEND, 16694 Loc, Op.getValueType(), SrcVal); 16695 return DAG.getMergeValues({Result, Op.getOperand(0)}, Loc); 16696 } 16697 return Op; 16698 } 16699 16700 // Either we are converting from 16 -> 64, without FP16 and/or 16701 // FP.double-precision or without Armv8-fp. So we must do it in two 16702 // steps. 16703 // Or we are converting from 32 -> 64 without fp.double-precision or 16 -> 32 16704 // without FP16. So we must do a function call. 16705 SDLoc Loc(Op); 16706 RTLIB::Libcall LC; 16707 MakeLibCallOptions CallOptions; 16708 SDValue Chain = IsStrict ? Op.getOperand(0) : SDValue(); 16709 for (unsigned Sz = SrcSz; Sz <= 32 && Sz < DstSz; Sz *= 2) { 16710 bool Supported = (Sz == 16 ? Subtarget->hasFP16() : Subtarget->hasFP64()); 16711 MVT SrcVT = (Sz == 16 ? MVT::f16 : MVT::f32); 16712 MVT DstVT = (Sz == 16 ? MVT::f32 : MVT::f64); 16713 if (Supported) { 16714 if (IsStrict) { 16715 SrcVal = DAG.getNode(ISD::STRICT_FP_EXTEND, Loc, 16716 {DstVT, MVT::Other}, {Chain, SrcVal}); 16717 Chain = SrcVal.getValue(1); 16718 } else { 16719 SrcVal = DAG.getNode(ISD::FP_EXTEND, Loc, DstVT, SrcVal); 16720 } 16721 } else { 16722 LC = RTLIB::getFPEXT(SrcVT, DstVT); 16723 assert(LC != RTLIB::UNKNOWN_LIBCALL && 16724 "Unexpected type for custom-lowering FP_EXTEND"); 16725 std::tie(SrcVal, Chain) = makeLibCall(DAG, LC, DstVT, SrcVal, CallOptions, 16726 Loc, Chain); 16727 } 16728 } 16729 16730 return IsStrict ? DAG.getMergeValues({SrcVal, Chain}, Loc) : SrcVal; 16731 } 16732 16733 SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const { 16734 bool IsStrict = Op->isStrictFPOpcode(); 16735 16736 SDValue SrcVal = Op.getOperand(IsStrict ? 1 : 0); 16737 EVT SrcVT = SrcVal.getValueType(); 16738 EVT DstVT = Op.getValueType(); 16739 const unsigned DstSz = Op.getValueType().getSizeInBits(); 16740 const unsigned SrcSz = SrcVT.getSizeInBits(); 16741 (void)DstSz; 16742 assert(DstSz < SrcSz && SrcSz <= 64 && DstSz >= 16 && 16743 "Unexpected type for custom-lowering FP_ROUND"); 16744 16745 assert((!Subtarget->hasFP64() || !Subtarget->hasFPARMv8Base()) && 16746 "With both FP DP and 16, any FP conversion is legal!"); 16747 16748 SDLoc Loc(Op); 16749 16750 // Instruction from 32 -> 16 if hasFP16 is valid 16751 if (SrcSz == 32 && Subtarget->hasFP16()) 16752 return Op; 16753 16754 // Lib call from 32 -> 16 / 64 -> [32, 16] 16755 RTLIB::Libcall LC = RTLIB::getFPROUND(SrcVT, DstVT); 16756 assert(LC != RTLIB::UNKNOWN_LIBCALL && 16757 "Unexpected type for custom-lowering FP_ROUND"); 16758 MakeLibCallOptions CallOptions; 16759 SDValue Chain = IsStrict ? Op.getOperand(0) : SDValue(); 16760 SDValue Result; 16761 std::tie(Result, Chain) = makeLibCall(DAG, LC, DstVT, SrcVal, CallOptions, 16762 Loc, Chain); 16763 return IsStrict ? DAG.getMergeValues({Result, Chain}, Loc) : Result; 16764 } 16765 16766 void ARMTargetLowering::lowerABS(SDNode *N, SmallVectorImpl<SDValue> &Results, 16767 SelectionDAG &DAG) const { 16768 assert(N->getValueType(0) == MVT::i64 && "Unexpected type (!= i64) on ABS."); 16769 MVT HalfT = MVT::i32; 16770 SDLoc dl(N); 16771 SDValue Hi, Lo, Tmp; 16772 16773 if (!isOperationLegalOrCustom(ISD::ADDCARRY, HalfT) || 16774 !isOperationLegalOrCustom(ISD::UADDO, HalfT)) 16775 return ; 16776 16777 unsigned OpTypeBits = HalfT.getScalarSizeInBits(); 16778 SDVTList VTList = DAG.getVTList(HalfT, MVT::i1); 16779 16780 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(0), 16781 DAG.getConstant(0, dl, HalfT)); 16782 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(0), 16783 DAG.getConstant(1, dl, HalfT)); 16784 16785 Tmp = DAG.getNode(ISD::SRA, dl, HalfT, Hi, 16786 DAG.getConstant(OpTypeBits - 1, dl, 16787 getShiftAmountTy(HalfT, DAG.getDataLayout()))); 16788 Lo = DAG.getNode(ISD::UADDO, dl, VTList, Tmp, Lo); 16789 Hi = DAG.getNode(ISD::ADDCARRY, dl, VTList, Tmp, Hi, 16790 SDValue(Lo.getNode(), 1)); 16791 Hi = DAG.getNode(ISD::XOR, dl, HalfT, Tmp, Hi); 16792 Lo = DAG.getNode(ISD::XOR, dl, HalfT, Tmp, Lo); 16793 16794 Results.push_back(Lo); 16795 Results.push_back(Hi); 16796 } 16797 16798 bool 16799 ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 16800 // The ARM target isn't yet aware of offsets. 16801 return false; 16802 } 16803 16804 bool ARM::isBitFieldInvertedMask(unsigned v) { 16805 if (v == 0xffffffff) 16806 return false; 16807 16808 // there can be 1's on either or both "outsides", all the "inside" 16809 // bits must be 0's 16810 return isShiftedMask_32(~v); 16811 } 16812 16813 /// isFPImmLegal - Returns true if the target can instruction select the 16814 /// specified FP immediate natively. If false, the legalizer will 16815 /// materialize the FP immediate as a load from a constant pool. 16816 bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT, 16817 bool ForCodeSize) const { 16818 if (!Subtarget->hasVFP3Base()) 16819 return false; 16820 if (VT == MVT::f16 && Subtarget->hasFullFP16()) 16821 return ARM_AM::getFP16Imm(Imm) != -1; 16822 if (VT == MVT::f32) 16823 return ARM_AM::getFP32Imm(Imm) != -1; 16824 if (VT == MVT::f64 && Subtarget->hasFP64()) 16825 return ARM_AM::getFP64Imm(Imm) != -1; 16826 return false; 16827 } 16828 16829 /// getTgtMemIntrinsic - Represent NEON load and store intrinsics as 16830 /// MemIntrinsicNodes. The associated MachineMemOperands record the alignment 16831 /// specified in the intrinsic calls. 16832 bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 16833 const CallInst &I, 16834 MachineFunction &MF, 16835 unsigned Intrinsic) const { 16836 switch (Intrinsic) { 16837 case Intrinsic::arm_neon_vld1: 16838 case Intrinsic::arm_neon_vld2: 16839 case Intrinsic::arm_neon_vld3: 16840 case Intrinsic::arm_neon_vld4: 16841 case Intrinsic::arm_neon_vld2lane: 16842 case Intrinsic::arm_neon_vld3lane: 16843 case Intrinsic::arm_neon_vld4lane: 16844 case Intrinsic::arm_neon_vld2dup: 16845 case Intrinsic::arm_neon_vld3dup: 16846 case Intrinsic::arm_neon_vld4dup: { 16847 Info.opc = ISD::INTRINSIC_W_CHAIN; 16848 // Conservatively set memVT to the entire set of vectors loaded. 16849 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 16850 uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64; 16851 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 16852 Info.ptrVal = I.getArgOperand(0); 16853 Info.offset = 0; 16854 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 16855 Info.align = MaybeAlign(cast<ConstantInt>(AlignArg)->getZExtValue()); 16856 // volatile loads with NEON intrinsics not supported 16857 Info.flags = MachineMemOperand::MOLoad; 16858 return true; 16859 } 16860 case Intrinsic::arm_neon_vld1x2: 16861 case Intrinsic::arm_neon_vld1x3: 16862 case Intrinsic::arm_neon_vld1x4: { 16863 Info.opc = ISD::INTRINSIC_W_CHAIN; 16864 // Conservatively set memVT to the entire set of vectors loaded. 16865 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 16866 uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64; 16867 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 16868 Info.ptrVal = I.getArgOperand(I.getNumArgOperands() - 1); 16869 Info.offset = 0; 16870 Info.align.reset(); 16871 // volatile loads with NEON intrinsics not supported 16872 Info.flags = MachineMemOperand::MOLoad; 16873 return true; 16874 } 16875 case Intrinsic::arm_neon_vst1: 16876 case Intrinsic::arm_neon_vst2: 16877 case Intrinsic::arm_neon_vst3: 16878 case Intrinsic::arm_neon_vst4: 16879 case Intrinsic::arm_neon_vst2lane: 16880 case Intrinsic::arm_neon_vst3lane: 16881 case Intrinsic::arm_neon_vst4lane: { 16882 Info.opc = ISD::INTRINSIC_VOID; 16883 // Conservatively set memVT to the entire set of vectors stored. 16884 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 16885 unsigned NumElts = 0; 16886 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) { 16887 Type *ArgTy = I.getArgOperand(ArgI)->getType(); 16888 if (!ArgTy->isVectorTy()) 16889 break; 16890 NumElts += DL.getTypeSizeInBits(ArgTy) / 64; 16891 } 16892 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 16893 Info.ptrVal = I.getArgOperand(0); 16894 Info.offset = 0; 16895 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 16896 Info.align = MaybeAlign(cast<ConstantInt>(AlignArg)->getZExtValue()); 16897 // volatile stores with NEON intrinsics not supported 16898 Info.flags = MachineMemOperand::MOStore; 16899 return true; 16900 } 16901 case Intrinsic::arm_neon_vst1x2: 16902 case Intrinsic::arm_neon_vst1x3: 16903 case Intrinsic::arm_neon_vst1x4: { 16904 Info.opc = ISD::INTRINSIC_VOID; 16905 // Conservatively set memVT to the entire set of vectors stored. 16906 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 16907 unsigned NumElts = 0; 16908 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) { 16909 Type *ArgTy = I.getArgOperand(ArgI)->getType(); 16910 if (!ArgTy->isVectorTy()) 16911 break; 16912 NumElts += DL.getTypeSizeInBits(ArgTy) / 64; 16913 } 16914 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 16915 Info.ptrVal = I.getArgOperand(0); 16916 Info.offset = 0; 16917 Info.align.reset(); 16918 // volatile stores with NEON intrinsics not supported 16919 Info.flags = MachineMemOperand::MOStore; 16920 return true; 16921 } 16922 case Intrinsic::arm_mve_vld2q: 16923 case Intrinsic::arm_mve_vld4q: { 16924 Info.opc = ISD::INTRINSIC_W_CHAIN; 16925 // Conservatively set memVT to the entire set of vectors loaded. 16926 Type *VecTy = cast<CompositeType>(I.getType())->getTypeAtIndex(1); 16927 unsigned Factor = Intrinsic == Intrinsic::arm_mve_vld2q ? 2 : 4; 16928 Info.memVT = EVT::getVectorVT(VecTy->getContext(), MVT::i64, Factor * 2); 16929 Info.ptrVal = I.getArgOperand(0); 16930 Info.offset = 0; 16931 Info.align = Align(VecTy->getScalarSizeInBits() / 8); 16932 // volatile loads with MVE intrinsics not supported 16933 Info.flags = MachineMemOperand::MOLoad; 16934 return true; 16935 } 16936 case Intrinsic::arm_mve_vst2q: 16937 case Intrinsic::arm_mve_vst4q: { 16938 Info.opc = ISD::INTRINSIC_VOID; 16939 // Conservatively set memVT to the entire set of vectors stored. 16940 Type *VecTy = I.getArgOperand(1)->getType(); 16941 unsigned Factor = Intrinsic == Intrinsic::arm_mve_vst2q ? 2 : 4; 16942 Info.memVT = EVT::getVectorVT(VecTy->getContext(), MVT::i64, Factor * 2); 16943 Info.ptrVal = I.getArgOperand(0); 16944 Info.offset = 0; 16945 Info.align = Align(VecTy->getScalarSizeInBits() / 8); 16946 // volatile stores with MVE intrinsics not supported 16947 Info.flags = MachineMemOperand::MOStore; 16948 return true; 16949 } 16950 case Intrinsic::arm_ldaex: 16951 case Intrinsic::arm_ldrex: { 16952 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 16953 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType()); 16954 Info.opc = ISD::INTRINSIC_W_CHAIN; 16955 Info.memVT = MVT::getVT(PtrTy->getElementType()); 16956 Info.ptrVal = I.getArgOperand(0); 16957 Info.offset = 0; 16958 Info.align = MaybeAlign(DL.getABITypeAlignment(PtrTy->getElementType())); 16959 Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile; 16960 return true; 16961 } 16962 case Intrinsic::arm_stlex: 16963 case Intrinsic::arm_strex: { 16964 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 16965 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType()); 16966 Info.opc = ISD::INTRINSIC_W_CHAIN; 16967 Info.memVT = MVT::getVT(PtrTy->getElementType()); 16968 Info.ptrVal = I.getArgOperand(1); 16969 Info.offset = 0; 16970 Info.align = MaybeAlign(DL.getABITypeAlignment(PtrTy->getElementType())); 16971 Info.flags = MachineMemOperand::MOStore | MachineMemOperand::MOVolatile; 16972 return true; 16973 } 16974 case Intrinsic::arm_stlexd: 16975 case Intrinsic::arm_strexd: 16976 Info.opc = ISD::INTRINSIC_W_CHAIN; 16977 Info.memVT = MVT::i64; 16978 Info.ptrVal = I.getArgOperand(2); 16979 Info.offset = 0; 16980 Info.align = Align(8); 16981 Info.flags = MachineMemOperand::MOStore | MachineMemOperand::MOVolatile; 16982 return true; 16983 16984 case Intrinsic::arm_ldaexd: 16985 case Intrinsic::arm_ldrexd: 16986 Info.opc = ISD::INTRINSIC_W_CHAIN; 16987 Info.memVT = MVT::i64; 16988 Info.ptrVal = I.getArgOperand(0); 16989 Info.offset = 0; 16990 Info.align = Align(8); 16991 Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile; 16992 return true; 16993 16994 default: 16995 break; 16996 } 16997 16998 return false; 16999 } 17000 17001 /// Returns true if it is beneficial to convert a load of a constant 17002 /// to just the constant itself. 17003 bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, 17004 Type *Ty) const { 17005 assert(Ty->isIntegerTy()); 17006 17007 unsigned Bits = Ty->getPrimitiveSizeInBits(); 17008 if (Bits == 0 || Bits > 32) 17009 return false; 17010 return true; 17011 } 17012 17013 bool ARMTargetLowering::isExtractSubvectorCheap(EVT ResVT, EVT SrcVT, 17014 unsigned Index) const { 17015 if (!isOperationLegalOrCustom(ISD::EXTRACT_SUBVECTOR, ResVT)) 17016 return false; 17017 17018 return (Index == 0 || Index == ResVT.getVectorNumElements()); 17019 } 17020 17021 Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder, 17022 ARM_MB::MemBOpt Domain) const { 17023 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 17024 17025 // First, if the target has no DMB, see what fallback we can use. 17026 if (!Subtarget->hasDataBarrier()) { 17027 // Some ARMv6 cpus can support data barriers with an mcr instruction. 17028 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 17029 // here. 17030 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) { 17031 Function *MCR = Intrinsic::getDeclaration(M, Intrinsic::arm_mcr); 17032 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0), 17033 Builder.getInt32(0), Builder.getInt32(7), 17034 Builder.getInt32(10), Builder.getInt32(5)}; 17035 return Builder.CreateCall(MCR, args); 17036 } else { 17037 // Instead of using barriers, atomic accesses on these subtargets use 17038 // libcalls. 17039 llvm_unreachable("makeDMB on a target so old that it has no barriers"); 17040 } 17041 } else { 17042 Function *DMB = Intrinsic::getDeclaration(M, Intrinsic::arm_dmb); 17043 // Only a full system barrier exists in the M-class architectures. 17044 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain; 17045 Constant *CDomain = Builder.getInt32(Domain); 17046 return Builder.CreateCall(DMB, CDomain); 17047 } 17048 } 17049 17050 // Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html 17051 Instruction *ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder, 17052 Instruction *Inst, 17053 AtomicOrdering Ord) const { 17054 switch (Ord) { 17055 case AtomicOrdering::NotAtomic: 17056 case AtomicOrdering::Unordered: 17057 llvm_unreachable("Invalid fence: unordered/non-atomic"); 17058 case AtomicOrdering::Monotonic: 17059 case AtomicOrdering::Acquire: 17060 return nullptr; // Nothing to do 17061 case AtomicOrdering::SequentiallyConsistent: 17062 if (!Inst->hasAtomicStore()) 17063 return nullptr; // Nothing to do 17064 LLVM_FALLTHROUGH; 17065 case AtomicOrdering::Release: 17066 case AtomicOrdering::AcquireRelease: 17067 if (Subtarget->preferISHSTBarriers()) 17068 return makeDMB(Builder, ARM_MB::ISHST); 17069 // FIXME: add a comment with a link to documentation justifying this. 17070 else 17071 return makeDMB(Builder, ARM_MB::ISH); 17072 } 17073 llvm_unreachable("Unknown fence ordering in emitLeadingFence"); 17074 } 17075 17076 Instruction *ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder, 17077 Instruction *Inst, 17078 AtomicOrdering Ord) const { 17079 switch (Ord) { 17080 case AtomicOrdering::NotAtomic: 17081 case AtomicOrdering::Unordered: 17082 llvm_unreachable("Invalid fence: unordered/not-atomic"); 17083 case AtomicOrdering::Monotonic: 17084 case AtomicOrdering::Release: 17085 return nullptr; // Nothing to do 17086 case AtomicOrdering::Acquire: 17087 case AtomicOrdering::AcquireRelease: 17088 case AtomicOrdering::SequentiallyConsistent: 17089 return makeDMB(Builder, ARM_MB::ISH); 17090 } 17091 llvm_unreachable("Unknown fence ordering in emitTrailingFence"); 17092 } 17093 17094 // Loads and stores less than 64-bits are already atomic; ones above that 17095 // are doomed anyway, so defer to the default libcall and blame the OS when 17096 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit 17097 // anything for those. 17098 bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const { 17099 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits(); 17100 return (Size == 64) && !Subtarget->isMClass(); 17101 } 17102 17103 // Loads and stores less than 64-bits are already atomic; ones above that 17104 // are doomed anyway, so defer to the default libcall and blame the OS when 17105 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit 17106 // anything for those. 17107 // FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that 17108 // guarantee, see DDI0406C ARM architecture reference manual, 17109 // sections A8.8.72-74 LDRD) 17110 TargetLowering::AtomicExpansionKind 17111 ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const { 17112 unsigned Size = LI->getType()->getPrimitiveSizeInBits(); 17113 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLOnly 17114 : AtomicExpansionKind::None; 17115 } 17116 17117 // For the real atomic operations, we have ldrex/strex up to 32 bits, 17118 // and up to 64 bits on the non-M profiles 17119 TargetLowering::AtomicExpansionKind 17120 ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const { 17121 if (AI->isFloatingPointOperation()) 17122 return AtomicExpansionKind::CmpXChg; 17123 17124 unsigned Size = AI->getType()->getPrimitiveSizeInBits(); 17125 bool hasAtomicRMW = !Subtarget->isThumb() || Subtarget->hasV8MBaselineOps(); 17126 return (Size <= (Subtarget->isMClass() ? 32U : 64U) && hasAtomicRMW) 17127 ? AtomicExpansionKind::LLSC 17128 : AtomicExpansionKind::None; 17129 } 17130 17131 TargetLowering::AtomicExpansionKind 17132 ARMTargetLowering::shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *AI) const { 17133 // At -O0, fast-regalloc cannot cope with the live vregs necessary to 17134 // implement cmpxchg without spilling. If the address being exchanged is also 17135 // on the stack and close enough to the spill slot, this can lead to a 17136 // situation where the monitor always gets cleared and the atomic operation 17137 // can never succeed. So at -O0 we need a late-expanded pseudo-inst instead. 17138 bool HasAtomicCmpXchg = 17139 !Subtarget->isThumb() || Subtarget->hasV8MBaselineOps(); 17140 if (getTargetMachine().getOptLevel() != 0 && HasAtomicCmpXchg) 17141 return AtomicExpansionKind::LLSC; 17142 return AtomicExpansionKind::None; 17143 } 17144 17145 bool ARMTargetLowering::shouldInsertFencesForAtomic( 17146 const Instruction *I) const { 17147 return InsertFencesForAtomic; 17148 } 17149 17150 // This has so far only been implemented for MachO. 17151 bool ARMTargetLowering::useLoadStackGuardNode() const { 17152 return Subtarget->isTargetMachO(); 17153 } 17154 17155 void ARMTargetLowering::insertSSPDeclarations(Module &M) const { 17156 if (!Subtarget->getTargetTriple().isWindowsMSVCEnvironment()) 17157 return TargetLowering::insertSSPDeclarations(M); 17158 17159 // MSVC CRT has a global variable holding security cookie. 17160 M.getOrInsertGlobal("__security_cookie", 17161 Type::getInt8PtrTy(M.getContext())); 17162 17163 // MSVC CRT has a function to validate security cookie. 17164 FunctionCallee SecurityCheckCookie = M.getOrInsertFunction( 17165 "__security_check_cookie", Type::getVoidTy(M.getContext()), 17166 Type::getInt8PtrTy(M.getContext())); 17167 if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee())) 17168 F->addAttribute(1, Attribute::AttrKind::InReg); 17169 } 17170 17171 Value *ARMTargetLowering::getSDagStackGuard(const Module &M) const { 17172 // MSVC CRT has a global variable holding security cookie. 17173 if (Subtarget->getTargetTriple().isWindowsMSVCEnvironment()) 17174 return M.getGlobalVariable("__security_cookie"); 17175 return TargetLowering::getSDagStackGuard(M); 17176 } 17177 17178 Function *ARMTargetLowering::getSSPStackGuardCheck(const Module &M) const { 17179 // MSVC CRT has a function to validate security cookie. 17180 if (Subtarget->getTargetTriple().isWindowsMSVCEnvironment()) 17181 return M.getFunction("__security_check_cookie"); 17182 return TargetLowering::getSSPStackGuardCheck(M); 17183 } 17184 17185 bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx, 17186 unsigned &Cost) const { 17187 // If we do not have NEON, vector types are not natively supported. 17188 if (!Subtarget->hasNEON()) 17189 return false; 17190 17191 // Floating point values and vector values map to the same register file. 17192 // Therefore, although we could do a store extract of a vector type, this is 17193 // better to leave at float as we have more freedom in the addressing mode for 17194 // those. 17195 if (VectorTy->isFPOrFPVectorTy()) 17196 return false; 17197 17198 // If the index is unknown at compile time, this is very expensive to lower 17199 // and it is not possible to combine the store with the extract. 17200 if (!isa<ConstantInt>(Idx)) 17201 return false; 17202 17203 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type"); 17204 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth(); 17205 // We can do a store + vector extract on any vector that fits perfectly in a D 17206 // or Q register. 17207 if (BitWidth == 64 || BitWidth == 128) { 17208 Cost = 0; 17209 return true; 17210 } 17211 return false; 17212 } 17213 17214 bool ARMTargetLowering::isCheapToSpeculateCttz() const { 17215 return Subtarget->hasV6T2Ops(); 17216 } 17217 17218 bool ARMTargetLowering::isCheapToSpeculateCtlz() const { 17219 return Subtarget->hasV6T2Ops(); 17220 } 17221 17222 bool ARMTargetLowering::shouldExpandShift(SelectionDAG &DAG, SDNode *N) const { 17223 return !Subtarget->hasMinSize(); 17224 } 17225 17226 Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr, 17227 AtomicOrdering Ord) const { 17228 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 17229 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType(); 17230 bool IsAcquire = isAcquireOrStronger(Ord); 17231 17232 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd 17233 // intrinsic must return {i32, i32} and we have to recombine them into a 17234 // single i64 here. 17235 if (ValTy->getPrimitiveSizeInBits() == 64) { 17236 Intrinsic::ID Int = 17237 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd; 17238 Function *Ldrex = Intrinsic::getDeclaration(M, Int); 17239 17240 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext())); 17241 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi"); 17242 17243 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo"); 17244 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi"); 17245 if (!Subtarget->isLittle()) 17246 std::swap (Lo, Hi); 17247 Lo = Builder.CreateZExt(Lo, ValTy, "lo64"); 17248 Hi = Builder.CreateZExt(Hi, ValTy, "hi64"); 17249 return Builder.CreateOr( 17250 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64"); 17251 } 17252 17253 Type *Tys[] = { Addr->getType() }; 17254 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex; 17255 Function *Ldrex = Intrinsic::getDeclaration(M, Int, Tys); 17256 17257 return Builder.CreateTruncOrBitCast( 17258 Builder.CreateCall(Ldrex, Addr), 17259 cast<PointerType>(Addr->getType())->getElementType()); 17260 } 17261 17262 void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance( 17263 IRBuilder<> &Builder) const { 17264 if (!Subtarget->hasV7Ops()) 17265 return; 17266 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 17267 Builder.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::arm_clrex)); 17268 } 17269 17270 Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val, 17271 Value *Addr, 17272 AtomicOrdering Ord) const { 17273 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 17274 bool IsRelease = isReleaseOrStronger(Ord); 17275 17276 // Since the intrinsics must have legal type, the i64 intrinsics take two 17277 // parameters: "i32, i32". We must marshal Val into the appropriate form 17278 // before the call. 17279 if (Val->getType()->getPrimitiveSizeInBits() == 64) { 17280 Intrinsic::ID Int = 17281 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd; 17282 Function *Strex = Intrinsic::getDeclaration(M, Int); 17283 Type *Int32Ty = Type::getInt32Ty(M->getContext()); 17284 17285 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo"); 17286 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi"); 17287 if (!Subtarget->isLittle()) 17288 std::swap(Lo, Hi); 17289 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext())); 17290 return Builder.CreateCall(Strex, {Lo, Hi, Addr}); 17291 } 17292 17293 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex; 17294 Type *Tys[] = { Addr->getType() }; 17295 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys); 17296 17297 return Builder.CreateCall( 17298 Strex, {Builder.CreateZExtOrBitCast( 17299 Val, Strex->getFunctionType()->getParamType(0)), 17300 Addr}); 17301 } 17302 17303 17304 bool ARMTargetLowering::alignLoopsWithOptSize() const { 17305 return Subtarget->isMClass(); 17306 } 17307 17308 /// A helper function for determining the number of interleaved accesses we 17309 /// will generate when lowering accesses of the given type. 17310 unsigned 17311 ARMTargetLowering::getNumInterleavedAccesses(VectorType *VecTy, 17312 const DataLayout &DL) const { 17313 return (DL.getTypeSizeInBits(VecTy) + 127) / 128; 17314 } 17315 17316 bool ARMTargetLowering::isLegalInterleavedAccessType( 17317 unsigned Factor, VectorType *VecTy, const DataLayout &DL) const { 17318 17319 unsigned VecSize = DL.getTypeSizeInBits(VecTy); 17320 unsigned ElSize = DL.getTypeSizeInBits(VecTy->getElementType()); 17321 17322 if (!Subtarget->hasNEON() && !Subtarget->hasMVEIntegerOps()) 17323 return false; 17324 17325 // Ensure the vector doesn't have f16 elements. Even though we could do an 17326 // i16 vldN, we can't hold the f16 vectors and will end up converting via 17327 // f32. 17328 if (Subtarget->hasNEON() && VecTy->getElementType()->isHalfTy()) 17329 return false; 17330 if (Subtarget->hasMVEIntegerOps() && Factor == 3) 17331 return false; 17332 17333 // Ensure the number of vector elements is greater than 1. 17334 if (VecTy->getNumElements() < 2) 17335 return false; 17336 17337 // Ensure the element type is legal. 17338 if (ElSize != 8 && ElSize != 16 && ElSize != 32) 17339 return false; 17340 17341 // Ensure the total vector size is 64 or a multiple of 128. Types larger than 17342 // 128 will be split into multiple interleaved accesses. 17343 if (Subtarget->hasNEON() && VecSize == 64) 17344 return true; 17345 return VecSize % 128 == 0; 17346 } 17347 17348 unsigned ARMTargetLowering::getMaxSupportedInterleaveFactor() const { 17349 if (Subtarget->hasNEON()) 17350 return 4; 17351 if (Subtarget->hasMVEIntegerOps()) 17352 return MVEMaxSupportedInterleaveFactor; 17353 return TargetLoweringBase::getMaxSupportedInterleaveFactor(); 17354 } 17355 17356 /// Lower an interleaved load into a vldN intrinsic. 17357 /// 17358 /// E.g. Lower an interleaved load (Factor = 2): 17359 /// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4 17360 /// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements 17361 /// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements 17362 /// 17363 /// Into: 17364 /// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4) 17365 /// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0 17366 /// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1 17367 bool ARMTargetLowering::lowerInterleavedLoad( 17368 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles, 17369 ArrayRef<unsigned> Indices, unsigned Factor) const { 17370 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() && 17371 "Invalid interleave factor"); 17372 assert(!Shuffles.empty() && "Empty shufflevector input"); 17373 assert(Shuffles.size() == Indices.size() && 17374 "Unmatched number of shufflevectors and indices"); 17375 17376 VectorType *VecTy = Shuffles[0]->getType(); 17377 Type *EltTy = VecTy->getVectorElementType(); 17378 17379 const DataLayout &DL = LI->getModule()->getDataLayout(); 17380 17381 // Skip if we do not have NEON and skip illegal vector types. We can 17382 // "legalize" wide vector types into multiple interleaved accesses as long as 17383 // the vector types are divisible by 128. 17384 if (!isLegalInterleavedAccessType(Factor, VecTy, DL)) 17385 return false; 17386 17387 unsigned NumLoads = getNumInterleavedAccesses(VecTy, DL); 17388 17389 // A pointer vector can not be the return type of the ldN intrinsics. Need to 17390 // load integer vectors first and then convert to pointer vectors. 17391 if (EltTy->isPointerTy()) 17392 VecTy = 17393 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements()); 17394 17395 IRBuilder<> Builder(LI); 17396 17397 // The base address of the load. 17398 Value *BaseAddr = LI->getPointerOperand(); 17399 17400 if (NumLoads > 1) { 17401 // If we're going to generate more than one load, reset the sub-vector type 17402 // to something legal. 17403 VecTy = VectorType::get(VecTy->getVectorElementType(), 17404 VecTy->getVectorNumElements() / NumLoads); 17405 17406 // We will compute the pointer operand of each load from the original base 17407 // address using GEPs. Cast the base address to a pointer to the scalar 17408 // element type. 17409 BaseAddr = Builder.CreateBitCast( 17410 BaseAddr, VecTy->getVectorElementType()->getPointerTo( 17411 LI->getPointerAddressSpace())); 17412 } 17413 17414 assert(isTypeLegal(EVT::getEVT(VecTy)) && "Illegal vldN vector type!"); 17415 17416 auto createLoadIntrinsic = [&](Value *BaseAddr) { 17417 if (Subtarget->hasNEON()) { 17418 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace()); 17419 Type *Tys[] = {VecTy, Int8Ptr}; 17420 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2, 17421 Intrinsic::arm_neon_vld3, 17422 Intrinsic::arm_neon_vld4}; 17423 Function *VldnFunc = 17424 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys); 17425 17426 SmallVector<Value *, 2> Ops; 17427 Ops.push_back(Builder.CreateBitCast(BaseAddr, Int8Ptr)); 17428 Ops.push_back(Builder.getInt32(LI->getAlignment())); 17429 17430 return Builder.CreateCall(VldnFunc, Ops, "vldN"); 17431 } else { 17432 assert((Factor == 2 || Factor == 4) && 17433 "expected interleave factor of 2 or 4 for MVE"); 17434 Intrinsic::ID LoadInts = 17435 Factor == 2 ? Intrinsic::arm_mve_vld2q : Intrinsic::arm_mve_vld4q; 17436 Type *VecEltTy = VecTy->getVectorElementType()->getPointerTo( 17437 LI->getPointerAddressSpace()); 17438 Type *Tys[] = {VecTy, VecEltTy}; 17439 Function *VldnFunc = 17440 Intrinsic::getDeclaration(LI->getModule(), LoadInts, Tys); 17441 17442 SmallVector<Value *, 2> Ops; 17443 Ops.push_back(Builder.CreateBitCast(BaseAddr, VecEltTy)); 17444 return Builder.CreateCall(VldnFunc, Ops, "vldN"); 17445 } 17446 }; 17447 17448 // Holds sub-vectors extracted from the load intrinsic return values. The 17449 // sub-vectors are associated with the shufflevector instructions they will 17450 // replace. 17451 DenseMap<ShuffleVectorInst *, SmallVector<Value *, 4>> SubVecs; 17452 17453 for (unsigned LoadCount = 0; LoadCount < NumLoads; ++LoadCount) { 17454 // If we're generating more than one load, compute the base address of 17455 // subsequent loads as an offset from the previous. 17456 if (LoadCount > 0) 17457 BaseAddr = 17458 Builder.CreateConstGEP1_32(VecTy->getVectorElementType(), BaseAddr, 17459 VecTy->getVectorNumElements() * Factor); 17460 17461 CallInst *VldN = createLoadIntrinsic(BaseAddr); 17462 17463 // Replace uses of each shufflevector with the corresponding vector loaded 17464 // by ldN. 17465 for (unsigned i = 0; i < Shuffles.size(); i++) { 17466 ShuffleVectorInst *SV = Shuffles[i]; 17467 unsigned Index = Indices[i]; 17468 17469 Value *SubVec = Builder.CreateExtractValue(VldN, Index); 17470 17471 // Convert the integer vector to pointer vector if the element is pointer. 17472 if (EltTy->isPointerTy()) 17473 SubVec = Builder.CreateIntToPtr( 17474 SubVec, VectorType::get(SV->getType()->getVectorElementType(), 17475 VecTy->getVectorNumElements())); 17476 17477 SubVecs[SV].push_back(SubVec); 17478 } 17479 } 17480 17481 // Replace uses of the shufflevector instructions with the sub-vectors 17482 // returned by the load intrinsic. If a shufflevector instruction is 17483 // associated with more than one sub-vector, those sub-vectors will be 17484 // concatenated into a single wide vector. 17485 for (ShuffleVectorInst *SVI : Shuffles) { 17486 auto &SubVec = SubVecs[SVI]; 17487 auto *WideVec = 17488 SubVec.size() > 1 ? concatenateVectors(Builder, SubVec) : SubVec[0]; 17489 SVI->replaceAllUsesWith(WideVec); 17490 } 17491 17492 return true; 17493 } 17494 17495 /// Lower an interleaved store into a vstN intrinsic. 17496 /// 17497 /// E.g. Lower an interleaved store (Factor = 3): 17498 /// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1, 17499 /// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11> 17500 /// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4 17501 /// 17502 /// Into: 17503 /// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3> 17504 /// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7> 17505 /// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11> 17506 /// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4) 17507 /// 17508 /// Note that the new shufflevectors will be removed and we'll only generate one 17509 /// vst3 instruction in CodeGen. 17510 /// 17511 /// Example for a more general valid mask (Factor 3). Lower: 17512 /// %i.vec = shuffle <32 x i32> %v0, <32 x i32> %v1, 17513 /// <4, 32, 16, 5, 33, 17, 6, 34, 18, 7, 35, 19> 17514 /// store <12 x i32> %i.vec, <12 x i32>* %ptr 17515 /// 17516 /// Into: 17517 /// %sub.v0 = shuffle <32 x i32> %v0, <32 x i32> v1, <4, 5, 6, 7> 17518 /// %sub.v1 = shuffle <32 x i32> %v0, <32 x i32> v1, <32, 33, 34, 35> 17519 /// %sub.v2 = shuffle <32 x i32> %v0, <32 x i32> v1, <16, 17, 18, 19> 17520 /// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4) 17521 bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI, 17522 ShuffleVectorInst *SVI, 17523 unsigned Factor) const { 17524 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() && 17525 "Invalid interleave factor"); 17526 17527 VectorType *VecTy = SVI->getType(); 17528 assert(VecTy->getVectorNumElements() % Factor == 0 && 17529 "Invalid interleaved store"); 17530 17531 unsigned LaneLen = VecTy->getVectorNumElements() / Factor; 17532 Type *EltTy = VecTy->getVectorElementType(); 17533 VectorType *SubVecTy = VectorType::get(EltTy, LaneLen); 17534 17535 const DataLayout &DL = SI->getModule()->getDataLayout(); 17536 17537 // Skip if we do not have NEON and skip illegal vector types. We can 17538 // "legalize" wide vector types into multiple interleaved accesses as long as 17539 // the vector types are divisible by 128. 17540 if (!isLegalInterleavedAccessType(Factor, SubVecTy, DL)) 17541 return false; 17542 17543 unsigned NumStores = getNumInterleavedAccesses(SubVecTy, DL); 17544 17545 Value *Op0 = SVI->getOperand(0); 17546 Value *Op1 = SVI->getOperand(1); 17547 IRBuilder<> Builder(SI); 17548 17549 // StN intrinsics don't support pointer vectors as arguments. Convert pointer 17550 // vectors to integer vectors. 17551 if (EltTy->isPointerTy()) { 17552 Type *IntTy = DL.getIntPtrType(EltTy); 17553 17554 // Convert to the corresponding integer vector. 17555 Type *IntVecTy = 17556 VectorType::get(IntTy, Op0->getType()->getVectorNumElements()); 17557 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy); 17558 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy); 17559 17560 SubVecTy = VectorType::get(IntTy, LaneLen); 17561 } 17562 17563 // The base address of the store. 17564 Value *BaseAddr = SI->getPointerOperand(); 17565 17566 if (NumStores > 1) { 17567 // If we're going to generate more than one store, reset the lane length 17568 // and sub-vector type to something legal. 17569 LaneLen /= NumStores; 17570 SubVecTy = VectorType::get(SubVecTy->getVectorElementType(), LaneLen); 17571 17572 // We will compute the pointer operand of each store from the original base 17573 // address using GEPs. Cast the base address to a pointer to the scalar 17574 // element type. 17575 BaseAddr = Builder.CreateBitCast( 17576 BaseAddr, SubVecTy->getVectorElementType()->getPointerTo( 17577 SI->getPointerAddressSpace())); 17578 } 17579 17580 assert(isTypeLegal(EVT::getEVT(SubVecTy)) && "Illegal vstN vector type!"); 17581 17582 auto Mask = SVI->getShuffleMask(); 17583 17584 auto createStoreIntrinsic = [&](Value *BaseAddr, 17585 SmallVectorImpl<Value *> &Shuffles) { 17586 if (Subtarget->hasNEON()) { 17587 static const Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2, 17588 Intrinsic::arm_neon_vst3, 17589 Intrinsic::arm_neon_vst4}; 17590 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace()); 17591 Type *Tys[] = {Int8Ptr, SubVecTy}; 17592 17593 Function *VstNFunc = Intrinsic::getDeclaration( 17594 SI->getModule(), StoreInts[Factor - 2], Tys); 17595 17596 SmallVector<Value *, 6> Ops; 17597 Ops.push_back(Builder.CreateBitCast(BaseAddr, Int8Ptr)); 17598 for (auto S : Shuffles) 17599 Ops.push_back(S); 17600 Ops.push_back(Builder.getInt32(SI->getAlignment())); 17601 Builder.CreateCall(VstNFunc, Ops); 17602 } else { 17603 assert((Factor == 2 || Factor == 4) && 17604 "expected interleave factor of 2 or 4 for MVE"); 17605 Intrinsic::ID StoreInts = 17606 Factor == 2 ? Intrinsic::arm_mve_vst2q : Intrinsic::arm_mve_vst4q; 17607 Type *EltPtrTy = SubVecTy->getVectorElementType()->getPointerTo( 17608 SI->getPointerAddressSpace()); 17609 Type *Tys[] = {EltPtrTy, SubVecTy}; 17610 Function *VstNFunc = 17611 Intrinsic::getDeclaration(SI->getModule(), StoreInts, Tys); 17612 17613 SmallVector<Value *, 6> Ops; 17614 Ops.push_back(Builder.CreateBitCast(BaseAddr, EltPtrTy)); 17615 for (auto S : Shuffles) 17616 Ops.push_back(S); 17617 for (unsigned F = 0; F < Factor; F++) { 17618 Ops.push_back(Builder.getInt32(F)); 17619 Builder.CreateCall(VstNFunc, Ops); 17620 Ops.pop_back(); 17621 } 17622 } 17623 }; 17624 17625 for (unsigned StoreCount = 0; StoreCount < NumStores; ++StoreCount) { 17626 // If we generating more than one store, we compute the base address of 17627 // subsequent stores as an offset from the previous. 17628 if (StoreCount > 0) 17629 BaseAddr = Builder.CreateConstGEP1_32(SubVecTy->getVectorElementType(), 17630 BaseAddr, LaneLen * Factor); 17631 17632 SmallVector<Value *, 4> Shuffles; 17633 17634 // Split the shufflevector operands into sub vectors for the new vstN call. 17635 for (unsigned i = 0; i < Factor; i++) { 17636 unsigned IdxI = StoreCount * LaneLen * Factor + i; 17637 if (Mask[IdxI] >= 0) { 17638 Shuffles.push_back(Builder.CreateShuffleVector( 17639 Op0, Op1, createSequentialMask(Builder, Mask[IdxI], LaneLen, 0))); 17640 } else { 17641 unsigned StartMask = 0; 17642 for (unsigned j = 1; j < LaneLen; j++) { 17643 unsigned IdxJ = StoreCount * LaneLen * Factor + j; 17644 if (Mask[IdxJ * Factor + IdxI] >= 0) { 17645 StartMask = Mask[IdxJ * Factor + IdxI] - IdxJ; 17646 break; 17647 } 17648 } 17649 // Note: If all elements in a chunk are undefs, StartMask=0! 17650 // Note: Filling undef gaps with random elements is ok, since 17651 // those elements were being written anyway (with undefs). 17652 // In the case of all undefs we're defaulting to using elems from 0 17653 // Note: StartMask cannot be negative, it's checked in 17654 // isReInterleaveMask 17655 Shuffles.push_back(Builder.CreateShuffleVector( 17656 Op0, Op1, createSequentialMask(Builder, StartMask, LaneLen, 0))); 17657 } 17658 } 17659 17660 createStoreIntrinsic(BaseAddr, Shuffles); 17661 } 17662 return true; 17663 } 17664 17665 enum HABaseType { 17666 HA_UNKNOWN = 0, 17667 HA_FLOAT, 17668 HA_DOUBLE, 17669 HA_VECT64, 17670 HA_VECT128 17671 }; 17672 17673 static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base, 17674 uint64_t &Members) { 17675 if (auto *ST = dyn_cast<StructType>(Ty)) { 17676 for (unsigned i = 0; i < ST->getNumElements(); ++i) { 17677 uint64_t SubMembers = 0; 17678 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers)) 17679 return false; 17680 Members += SubMembers; 17681 } 17682 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) { 17683 uint64_t SubMembers = 0; 17684 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers)) 17685 return false; 17686 Members += SubMembers * AT->getNumElements(); 17687 } else if (Ty->isFloatTy()) { 17688 if (Base != HA_UNKNOWN && Base != HA_FLOAT) 17689 return false; 17690 Members = 1; 17691 Base = HA_FLOAT; 17692 } else if (Ty->isDoubleTy()) { 17693 if (Base != HA_UNKNOWN && Base != HA_DOUBLE) 17694 return false; 17695 Members = 1; 17696 Base = HA_DOUBLE; 17697 } else if (auto *VT = dyn_cast<VectorType>(Ty)) { 17698 Members = 1; 17699 switch (Base) { 17700 case HA_FLOAT: 17701 case HA_DOUBLE: 17702 return false; 17703 case HA_VECT64: 17704 return VT->getBitWidth() == 64; 17705 case HA_VECT128: 17706 return VT->getBitWidth() == 128; 17707 case HA_UNKNOWN: 17708 switch (VT->getBitWidth()) { 17709 case 64: 17710 Base = HA_VECT64; 17711 return true; 17712 case 128: 17713 Base = HA_VECT128; 17714 return true; 17715 default: 17716 return false; 17717 } 17718 } 17719 } 17720 17721 return (Members > 0 && Members <= 4); 17722 } 17723 17724 /// Return the correct alignment for the current calling convention. 17725 Align ARMTargetLowering::getABIAlignmentForCallingConv(Type *ArgTy, 17726 DataLayout DL) const { 17727 const Align ABITypeAlign(DL.getABITypeAlignment(ArgTy)); 17728 if (!ArgTy->isVectorTy()) 17729 return ABITypeAlign; 17730 17731 // Avoid over-aligning vector parameters. It would require realigning the 17732 // stack and waste space for no real benefit. 17733 return std::min(ABITypeAlign, DL.getStackAlignment()); 17734 } 17735 17736 /// Return true if a type is an AAPCS-VFP homogeneous aggregate or one of 17737 /// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when 17738 /// passing according to AAPCS rules. 17739 bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters( 17740 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const { 17741 if (getEffectiveCallingConv(CallConv, isVarArg) != 17742 CallingConv::ARM_AAPCS_VFP) 17743 return false; 17744 17745 HABaseType Base = HA_UNKNOWN; 17746 uint64_t Members = 0; 17747 bool IsHA = isHomogeneousAggregate(Ty, Base, Members); 17748 LLVM_DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump()); 17749 17750 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy(); 17751 return IsHA || IsIntArray; 17752 } 17753 17754 unsigned ARMTargetLowering::getExceptionPointerRegister( 17755 const Constant *PersonalityFn) const { 17756 // Platforms which do not use SjLj EH may return values in these registers 17757 // via the personality function. 17758 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R0; 17759 } 17760 17761 unsigned ARMTargetLowering::getExceptionSelectorRegister( 17762 const Constant *PersonalityFn) const { 17763 // Platforms which do not use SjLj EH may return values in these registers 17764 // via the personality function. 17765 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R1; 17766 } 17767 17768 void ARMTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const { 17769 // Update IsSplitCSR in ARMFunctionInfo. 17770 ARMFunctionInfo *AFI = Entry->getParent()->getInfo<ARMFunctionInfo>(); 17771 AFI->setIsSplitCSR(true); 17772 } 17773 17774 void ARMTargetLowering::insertCopiesSplitCSR( 17775 MachineBasicBlock *Entry, 17776 const SmallVectorImpl<MachineBasicBlock *> &Exits) const { 17777 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo(); 17778 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent()); 17779 if (!IStart) 17780 return; 17781 17782 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 17783 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo(); 17784 MachineBasicBlock::iterator MBBI = Entry->begin(); 17785 for (const MCPhysReg *I = IStart; *I; ++I) { 17786 const TargetRegisterClass *RC = nullptr; 17787 if (ARM::GPRRegClass.contains(*I)) 17788 RC = &ARM::GPRRegClass; 17789 else if (ARM::DPRRegClass.contains(*I)) 17790 RC = &ARM::DPRRegClass; 17791 else 17792 llvm_unreachable("Unexpected register class in CSRsViaCopy!"); 17793 17794 Register NewVR = MRI->createVirtualRegister(RC); 17795 // Create copy from CSR to a virtual register. 17796 // FIXME: this currently does not emit CFI pseudo-instructions, it works 17797 // fine for CXX_FAST_TLS since the C++-style TLS access functions should be 17798 // nounwind. If we want to generalize this later, we may need to emit 17799 // CFI pseudo-instructions. 17800 assert(Entry->getParent()->getFunction().hasFnAttribute( 17801 Attribute::NoUnwind) && 17802 "Function should be nounwind in insertCopiesSplitCSR!"); 17803 Entry->addLiveIn(*I); 17804 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR) 17805 .addReg(*I); 17806 17807 // Insert the copy-back instructions right before the terminator. 17808 for (auto *Exit : Exits) 17809 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(), 17810 TII->get(TargetOpcode::COPY), *I) 17811 .addReg(NewVR); 17812 } 17813 } 17814 17815 void ARMTargetLowering::finalizeLowering(MachineFunction &MF) const { 17816 MF.getFrameInfo().computeMaxCallFrameSize(MF); 17817 TargetLoweringBase::finalizeLowering(MF); 17818 } 17819