1 //===-- AArch64ISelLowering.cpp - AArch64 DAG Lowering Implementation ----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the AArch64TargetLowering class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "AArch64ISelLowering.h" 15 #include "AArch64CallingConvention.h" 16 #include "AArch64MachineFunctionInfo.h" 17 #include "AArch64PerfectShuffle.h" 18 #include "AArch64Subtarget.h" 19 #include "AArch64TargetMachine.h" 20 #include "AArch64TargetObjectFile.h" 21 #include "MCTargetDesc/AArch64AddressingModes.h" 22 #include "llvm/ADT/Statistic.h" 23 #include "llvm/CodeGen/CallingConvLower.h" 24 #include "llvm/CodeGen/MachineFrameInfo.h" 25 #include "llvm/CodeGen/MachineInstrBuilder.h" 26 #include "llvm/CodeGen/MachineRegisterInfo.h" 27 #include "llvm/IR/Function.h" 28 #include "llvm/IR/GetElementPtrTypeIterator.h" 29 #include "llvm/IR/Intrinsics.h" 30 #include "llvm/IR/Type.h" 31 #include "llvm/Support/CommandLine.h" 32 #include "llvm/Support/Debug.h" 33 #include "llvm/Support/ErrorHandling.h" 34 #include "llvm/Support/raw_ostream.h" 35 #include "llvm/Target/TargetOptions.h" 36 using namespace llvm; 37 38 #define DEBUG_TYPE "aarch64-lower" 39 40 STATISTIC(NumTailCalls, "Number of tail calls"); 41 STATISTIC(NumShiftInserts, "Number of vector shift inserts"); 42 43 // Place holder until extr generation is tested fully. 44 static cl::opt<bool> 45 EnableAArch64ExtrGeneration("aarch64-extr-generation", cl::Hidden, 46 cl::desc("Allow AArch64 (or (shift)(shift))->extract"), 47 cl::init(true)); 48 49 static cl::opt<bool> 50 EnableAArch64SlrGeneration("aarch64-shift-insert-generation", cl::Hidden, 51 cl::desc("Allow AArch64 SLI/SRI formation"), 52 cl::init(false)); 53 54 // FIXME: The necessary dtprel relocations don't seem to be supported 55 // well in the GNU bfd and gold linkers at the moment. Therefore, by 56 // default, for now, fall back to GeneralDynamic code generation. 57 cl::opt<bool> EnableAArch64ELFLocalDynamicTLSGeneration( 58 "aarch64-elf-ldtls-generation", cl::Hidden, 59 cl::desc("Allow AArch64 Local Dynamic TLS code generation"), 60 cl::init(false)); 61 62 /// Value type used for condition codes. 63 static const MVT MVT_CC = MVT::i32; 64 65 AArch64TargetLowering::AArch64TargetLowering(const TargetMachine &TM, 66 const AArch64Subtarget &STI) 67 : TargetLowering(TM), Subtarget(&STI) { 68 69 // AArch64 doesn't have comparisons which set GPRs or setcc instructions, so 70 // we have to make something up. Arbitrarily, choose ZeroOrOne. 71 setBooleanContents(ZeroOrOneBooleanContent); 72 // When comparing vectors the result sets the different elements in the 73 // vector to all-one or all-zero. 74 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 75 76 // Set up the register classes. 77 addRegisterClass(MVT::i32, &AArch64::GPR32allRegClass); 78 addRegisterClass(MVT::i64, &AArch64::GPR64allRegClass); 79 80 if (Subtarget->hasFPARMv8()) { 81 addRegisterClass(MVT::f16, &AArch64::FPR16RegClass); 82 addRegisterClass(MVT::f32, &AArch64::FPR32RegClass); 83 addRegisterClass(MVT::f64, &AArch64::FPR64RegClass); 84 addRegisterClass(MVT::f128, &AArch64::FPR128RegClass); 85 } 86 87 if (Subtarget->hasNEON()) { 88 addRegisterClass(MVT::v16i8, &AArch64::FPR8RegClass); 89 addRegisterClass(MVT::v8i16, &AArch64::FPR16RegClass); 90 // Someone set us up the NEON. 91 addDRTypeForNEON(MVT::v2f32); 92 addDRTypeForNEON(MVT::v8i8); 93 addDRTypeForNEON(MVT::v4i16); 94 addDRTypeForNEON(MVT::v2i32); 95 addDRTypeForNEON(MVT::v1i64); 96 addDRTypeForNEON(MVT::v1f64); 97 addDRTypeForNEON(MVT::v4f16); 98 99 addQRTypeForNEON(MVT::v4f32); 100 addQRTypeForNEON(MVT::v2f64); 101 addQRTypeForNEON(MVT::v16i8); 102 addQRTypeForNEON(MVT::v8i16); 103 addQRTypeForNEON(MVT::v4i32); 104 addQRTypeForNEON(MVT::v2i64); 105 addQRTypeForNEON(MVT::v8f16); 106 } 107 108 // Compute derived properties from the register classes 109 computeRegisterProperties(Subtarget->getRegisterInfo()); 110 111 // Provide all sorts of operation actions 112 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); 113 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom); 114 setOperationAction(ISD::SETCC, MVT::i32, Custom); 115 setOperationAction(ISD::SETCC, MVT::i64, Custom); 116 setOperationAction(ISD::SETCC, MVT::f32, Custom); 117 setOperationAction(ISD::SETCC, MVT::f64, Custom); 118 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 119 setOperationAction(ISD::BR_CC, MVT::i32, Custom); 120 setOperationAction(ISD::BR_CC, MVT::i64, Custom); 121 setOperationAction(ISD::BR_CC, MVT::f32, Custom); 122 setOperationAction(ISD::BR_CC, MVT::f64, Custom); 123 setOperationAction(ISD::SELECT, MVT::i32, Custom); 124 setOperationAction(ISD::SELECT, MVT::i64, Custom); 125 setOperationAction(ISD::SELECT, MVT::f32, Custom); 126 setOperationAction(ISD::SELECT, MVT::f64, Custom); 127 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); 128 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom); 129 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 130 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); 131 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 132 setOperationAction(ISD::JumpTable, MVT::i64, Custom); 133 134 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom); 135 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom); 136 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom); 137 138 setOperationAction(ISD::FREM, MVT::f32, Expand); 139 setOperationAction(ISD::FREM, MVT::f64, Expand); 140 setOperationAction(ISD::FREM, MVT::f80, Expand); 141 142 // Custom lowering hooks are needed for XOR 143 // to fold it into CSINC/CSINV. 144 setOperationAction(ISD::XOR, MVT::i32, Custom); 145 setOperationAction(ISD::XOR, MVT::i64, Custom); 146 147 // Virtually no operation on f128 is legal, but LLVM can't expand them when 148 // there's a valid register class, so we need custom operations in most cases. 149 setOperationAction(ISD::FABS, MVT::f128, Expand); 150 setOperationAction(ISD::FADD, MVT::f128, Custom); 151 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand); 152 setOperationAction(ISD::FCOS, MVT::f128, Expand); 153 setOperationAction(ISD::FDIV, MVT::f128, Custom); 154 setOperationAction(ISD::FMA, MVT::f128, Expand); 155 setOperationAction(ISD::FMUL, MVT::f128, Custom); 156 setOperationAction(ISD::FNEG, MVT::f128, Expand); 157 setOperationAction(ISD::FPOW, MVT::f128, Expand); 158 setOperationAction(ISD::FREM, MVT::f128, Expand); 159 setOperationAction(ISD::FRINT, MVT::f128, Expand); 160 setOperationAction(ISD::FSIN, MVT::f128, Expand); 161 setOperationAction(ISD::FSINCOS, MVT::f128, Expand); 162 setOperationAction(ISD::FSQRT, MVT::f128, Expand); 163 setOperationAction(ISD::FSUB, MVT::f128, Custom); 164 setOperationAction(ISD::FTRUNC, MVT::f128, Expand); 165 setOperationAction(ISD::SETCC, MVT::f128, Custom); 166 setOperationAction(ISD::BR_CC, MVT::f128, Custom); 167 setOperationAction(ISD::SELECT, MVT::f128, Custom); 168 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom); 169 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom); 170 171 // Lowering for many of the conversions is actually specified by the non-f128 172 // type. The LowerXXX function will be trivial when f128 isn't involved. 173 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 174 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); 175 setOperationAction(ISD::FP_TO_SINT, MVT::i128, Custom); 176 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); 177 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom); 178 setOperationAction(ISD::FP_TO_UINT, MVT::i128, Custom); 179 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 180 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); 181 setOperationAction(ISD::SINT_TO_FP, MVT::i128, Custom); 182 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); 183 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom); 184 setOperationAction(ISD::UINT_TO_FP, MVT::i128, Custom); 185 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom); 186 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom); 187 188 // Variable arguments. 189 setOperationAction(ISD::VASTART, MVT::Other, Custom); 190 setOperationAction(ISD::VAARG, MVT::Other, Custom); 191 setOperationAction(ISD::VACOPY, MVT::Other, Custom); 192 setOperationAction(ISD::VAEND, MVT::Other, Expand); 193 194 // Variable-sized objects. 195 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 196 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 197 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand); 198 199 // Constant pool entries 200 setOperationAction(ISD::ConstantPool, MVT::i64, Custom); 201 202 // BlockAddress 203 setOperationAction(ISD::BlockAddress, MVT::i64, Custom); 204 205 // Add/Sub overflow ops with MVT::Glues are lowered to NZCV dependences. 206 setOperationAction(ISD::ADDC, MVT::i32, Custom); 207 setOperationAction(ISD::ADDE, MVT::i32, Custom); 208 setOperationAction(ISD::SUBC, MVT::i32, Custom); 209 setOperationAction(ISD::SUBE, MVT::i32, Custom); 210 setOperationAction(ISD::ADDC, MVT::i64, Custom); 211 setOperationAction(ISD::ADDE, MVT::i64, Custom); 212 setOperationAction(ISD::SUBC, MVT::i64, Custom); 213 setOperationAction(ISD::SUBE, MVT::i64, Custom); 214 215 // AArch64 lacks both left-rotate and popcount instructions. 216 setOperationAction(ISD::ROTL, MVT::i32, Expand); 217 setOperationAction(ISD::ROTL, MVT::i64, Expand); 218 for (MVT VT : MVT::vector_valuetypes()) { 219 setOperationAction(ISD::ROTL, VT, Expand); 220 setOperationAction(ISD::ROTR, VT, Expand); 221 } 222 223 // AArch64 doesn't have {U|S}MUL_LOHI. 224 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand); 225 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand); 226 227 228 // Expand the undefined-at-zero variants to cttz/ctlz to their defined-at-zero 229 // counterparts, which AArch64 supports directly. 230 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand); 231 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand); 232 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand); 233 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand); 234 235 setOperationAction(ISD::CTPOP, MVT::i32, Custom); 236 setOperationAction(ISD::CTPOP, MVT::i64, Custom); 237 238 setOperationAction(ISD::SDIVREM, MVT::i32, Expand); 239 setOperationAction(ISD::SDIVREM, MVT::i64, Expand); 240 setOperationAction(ISD::SREM, MVT::i32, Expand); 241 setOperationAction(ISD::SREM, MVT::i64, Expand); 242 setOperationAction(ISD::UDIVREM, MVT::i32, Expand); 243 setOperationAction(ISD::UDIVREM, MVT::i64, Expand); 244 setOperationAction(ISD::UREM, MVT::i32, Expand); 245 setOperationAction(ISD::UREM, MVT::i64, Expand); 246 247 // Custom lower Add/Sub/Mul with overflow. 248 setOperationAction(ISD::SADDO, MVT::i32, Custom); 249 setOperationAction(ISD::SADDO, MVT::i64, Custom); 250 setOperationAction(ISD::UADDO, MVT::i32, Custom); 251 setOperationAction(ISD::UADDO, MVT::i64, Custom); 252 setOperationAction(ISD::SSUBO, MVT::i32, Custom); 253 setOperationAction(ISD::SSUBO, MVT::i64, Custom); 254 setOperationAction(ISD::USUBO, MVT::i32, Custom); 255 setOperationAction(ISD::USUBO, MVT::i64, Custom); 256 setOperationAction(ISD::SMULO, MVT::i32, Custom); 257 setOperationAction(ISD::SMULO, MVT::i64, Custom); 258 setOperationAction(ISD::UMULO, MVT::i32, Custom); 259 setOperationAction(ISD::UMULO, MVT::i64, Custom); 260 261 setOperationAction(ISD::FSIN, MVT::f32, Expand); 262 setOperationAction(ISD::FSIN, MVT::f64, Expand); 263 setOperationAction(ISD::FCOS, MVT::f32, Expand); 264 setOperationAction(ISD::FCOS, MVT::f64, Expand); 265 setOperationAction(ISD::FPOW, MVT::f32, Expand); 266 setOperationAction(ISD::FPOW, MVT::f64, Expand); 267 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); 268 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); 269 270 // f16 is a storage-only type, always promote it to f32. 271 setOperationAction(ISD::SETCC, MVT::f16, Promote); 272 setOperationAction(ISD::BR_CC, MVT::f16, Promote); 273 setOperationAction(ISD::SELECT_CC, MVT::f16, Promote); 274 setOperationAction(ISD::SELECT, MVT::f16, Promote); 275 setOperationAction(ISD::FADD, MVT::f16, Promote); 276 setOperationAction(ISD::FSUB, MVT::f16, Promote); 277 setOperationAction(ISD::FMUL, MVT::f16, Promote); 278 setOperationAction(ISD::FDIV, MVT::f16, Promote); 279 setOperationAction(ISD::FREM, MVT::f16, Promote); 280 setOperationAction(ISD::FMA, MVT::f16, Promote); 281 setOperationAction(ISD::FNEG, MVT::f16, Promote); 282 setOperationAction(ISD::FABS, MVT::f16, Promote); 283 setOperationAction(ISD::FCEIL, MVT::f16, Promote); 284 setOperationAction(ISD::FCOPYSIGN, MVT::f16, Promote); 285 setOperationAction(ISD::FCOS, MVT::f16, Promote); 286 setOperationAction(ISD::FFLOOR, MVT::f16, Promote); 287 setOperationAction(ISD::FNEARBYINT, MVT::f16, Promote); 288 setOperationAction(ISD::FPOW, MVT::f16, Promote); 289 setOperationAction(ISD::FPOWI, MVT::f16, Promote); 290 setOperationAction(ISD::FRINT, MVT::f16, Promote); 291 setOperationAction(ISD::FSIN, MVT::f16, Promote); 292 setOperationAction(ISD::FSINCOS, MVT::f16, Promote); 293 setOperationAction(ISD::FSQRT, MVT::f16, Promote); 294 setOperationAction(ISD::FEXP, MVT::f16, Promote); 295 setOperationAction(ISD::FEXP2, MVT::f16, Promote); 296 setOperationAction(ISD::FLOG, MVT::f16, Promote); 297 setOperationAction(ISD::FLOG2, MVT::f16, Promote); 298 setOperationAction(ISD::FLOG10, MVT::f16, Promote); 299 setOperationAction(ISD::FROUND, MVT::f16, Promote); 300 setOperationAction(ISD::FTRUNC, MVT::f16, Promote); 301 setOperationAction(ISD::FMINNUM, MVT::f16, Promote); 302 setOperationAction(ISD::FMAXNUM, MVT::f16, Promote); 303 setOperationAction(ISD::FMINNAN, MVT::f16, Promote); 304 setOperationAction(ISD::FMAXNAN, MVT::f16, Promote); 305 306 // v4f16 is also a storage-only type, so promote it to v4f32 when that is 307 // known to be safe. 308 setOperationAction(ISD::FADD, MVT::v4f16, Promote); 309 setOperationAction(ISD::FSUB, MVT::v4f16, Promote); 310 setOperationAction(ISD::FMUL, MVT::v4f16, Promote); 311 setOperationAction(ISD::FDIV, MVT::v4f16, Promote); 312 setOperationAction(ISD::FP_EXTEND, MVT::v4f16, Promote); 313 setOperationAction(ISD::FP_ROUND, MVT::v4f16, Promote); 314 AddPromotedToType(ISD::FADD, MVT::v4f16, MVT::v4f32); 315 AddPromotedToType(ISD::FSUB, MVT::v4f16, MVT::v4f32); 316 AddPromotedToType(ISD::FMUL, MVT::v4f16, MVT::v4f32); 317 AddPromotedToType(ISD::FDIV, MVT::v4f16, MVT::v4f32); 318 AddPromotedToType(ISD::FP_EXTEND, MVT::v4f16, MVT::v4f32); 319 AddPromotedToType(ISD::FP_ROUND, MVT::v4f16, MVT::v4f32); 320 321 // Expand all other v4f16 operations. 322 // FIXME: We could generate better code by promoting some operations to 323 // a pair of v4f32s 324 setOperationAction(ISD::FABS, MVT::v4f16, Expand); 325 setOperationAction(ISD::FCEIL, MVT::v4f16, Expand); 326 setOperationAction(ISD::FCOPYSIGN, MVT::v4f16, Expand); 327 setOperationAction(ISD::FCOS, MVT::v4f16, Expand); 328 setOperationAction(ISD::FFLOOR, MVT::v4f16, Expand); 329 setOperationAction(ISD::FMA, MVT::v4f16, Expand); 330 setOperationAction(ISD::FNEARBYINT, MVT::v4f16, Expand); 331 setOperationAction(ISD::FNEG, MVT::v4f16, Expand); 332 setOperationAction(ISD::FPOW, MVT::v4f16, Expand); 333 setOperationAction(ISD::FPOWI, MVT::v4f16, Expand); 334 setOperationAction(ISD::FREM, MVT::v4f16, Expand); 335 setOperationAction(ISD::FROUND, MVT::v4f16, Expand); 336 setOperationAction(ISD::FRINT, MVT::v4f16, Expand); 337 setOperationAction(ISD::FSIN, MVT::v4f16, Expand); 338 setOperationAction(ISD::FSINCOS, MVT::v4f16, Expand); 339 setOperationAction(ISD::FSQRT, MVT::v4f16, Expand); 340 setOperationAction(ISD::FTRUNC, MVT::v4f16, Expand); 341 setOperationAction(ISD::SETCC, MVT::v4f16, Expand); 342 setOperationAction(ISD::BR_CC, MVT::v4f16, Expand); 343 setOperationAction(ISD::SELECT, MVT::v4f16, Expand); 344 setOperationAction(ISD::SELECT_CC, MVT::v4f16, Expand); 345 setOperationAction(ISD::FEXP, MVT::v4f16, Expand); 346 setOperationAction(ISD::FEXP2, MVT::v4f16, Expand); 347 setOperationAction(ISD::FLOG, MVT::v4f16, Expand); 348 setOperationAction(ISD::FLOG2, MVT::v4f16, Expand); 349 setOperationAction(ISD::FLOG10, MVT::v4f16, Expand); 350 351 352 // v8f16 is also a storage-only type, so expand it. 353 setOperationAction(ISD::FABS, MVT::v8f16, Expand); 354 setOperationAction(ISD::FADD, MVT::v8f16, Expand); 355 setOperationAction(ISD::FCEIL, MVT::v8f16, Expand); 356 setOperationAction(ISD::FCOPYSIGN, MVT::v8f16, Expand); 357 setOperationAction(ISD::FCOS, MVT::v8f16, Expand); 358 setOperationAction(ISD::FDIV, MVT::v8f16, Expand); 359 setOperationAction(ISD::FFLOOR, MVT::v8f16, Expand); 360 setOperationAction(ISD::FMA, MVT::v8f16, Expand); 361 setOperationAction(ISD::FMUL, MVT::v8f16, Expand); 362 setOperationAction(ISD::FNEARBYINT, MVT::v8f16, Expand); 363 setOperationAction(ISD::FNEG, MVT::v8f16, Expand); 364 setOperationAction(ISD::FPOW, MVT::v8f16, Expand); 365 setOperationAction(ISD::FPOWI, MVT::v8f16, Expand); 366 setOperationAction(ISD::FREM, MVT::v8f16, Expand); 367 setOperationAction(ISD::FROUND, MVT::v8f16, Expand); 368 setOperationAction(ISD::FRINT, MVT::v8f16, Expand); 369 setOperationAction(ISD::FSIN, MVT::v8f16, Expand); 370 setOperationAction(ISD::FSINCOS, MVT::v8f16, Expand); 371 setOperationAction(ISD::FSQRT, MVT::v8f16, Expand); 372 setOperationAction(ISD::FSUB, MVT::v8f16, Expand); 373 setOperationAction(ISD::FTRUNC, MVT::v8f16, Expand); 374 setOperationAction(ISD::SETCC, MVT::v8f16, Expand); 375 setOperationAction(ISD::BR_CC, MVT::v8f16, Expand); 376 setOperationAction(ISD::SELECT, MVT::v8f16, Expand); 377 setOperationAction(ISD::SELECT_CC, MVT::v8f16, Expand); 378 setOperationAction(ISD::FP_EXTEND, MVT::v8f16, Expand); 379 setOperationAction(ISD::FEXP, MVT::v8f16, Expand); 380 setOperationAction(ISD::FEXP2, MVT::v8f16, Expand); 381 setOperationAction(ISD::FLOG, MVT::v8f16, Expand); 382 setOperationAction(ISD::FLOG2, MVT::v8f16, Expand); 383 setOperationAction(ISD::FLOG10, MVT::v8f16, Expand); 384 385 // AArch64 has implementations of a lot of rounding-like FP operations. 386 for (MVT Ty : {MVT::f32, MVT::f64}) { 387 setOperationAction(ISD::FFLOOR, Ty, Legal); 388 setOperationAction(ISD::FNEARBYINT, Ty, Legal); 389 setOperationAction(ISD::FCEIL, Ty, Legal); 390 setOperationAction(ISD::FRINT, Ty, Legal); 391 setOperationAction(ISD::FTRUNC, Ty, Legal); 392 setOperationAction(ISD::FROUND, Ty, Legal); 393 setOperationAction(ISD::FMINNUM, Ty, Legal); 394 setOperationAction(ISD::FMAXNUM, Ty, Legal); 395 setOperationAction(ISD::FMINNAN, Ty, Legal); 396 setOperationAction(ISD::FMAXNAN, Ty, Legal); 397 } 398 399 setOperationAction(ISD::PREFETCH, MVT::Other, Custom); 400 401 // Lower READCYCLECOUNTER using an mrs from PMCCNTR_EL0. 402 // This requires the Performance Monitors extension. 403 if (Subtarget->hasPerfMon()) 404 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal); 405 406 if (Subtarget->isTargetMachO()) { 407 // For iOS, we don't want to the normal expansion of a libcall to 408 // sincos. We want to issue a libcall to __sincos_stret to avoid memory 409 // traffic. 410 setOperationAction(ISD::FSINCOS, MVT::f64, Custom); 411 setOperationAction(ISD::FSINCOS, MVT::f32, Custom); 412 } else { 413 setOperationAction(ISD::FSINCOS, MVT::f64, Expand); 414 setOperationAction(ISD::FSINCOS, MVT::f32, Expand); 415 } 416 417 // Make floating-point constants legal for the large code model, so they don't 418 // become loads from the constant pool. 419 if (Subtarget->isTargetMachO() && TM.getCodeModel() == CodeModel::Large) { 420 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); 421 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); 422 } 423 424 // AArch64 does not have floating-point extending loads, i1 sign-extending 425 // load, floating-point truncating stores, or v2i32->v2i16 truncating store. 426 for (MVT VT : MVT::fp_valuetypes()) { 427 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand); 428 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand); 429 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f64, Expand); 430 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand); 431 } 432 for (MVT VT : MVT::integer_valuetypes()) 433 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Expand); 434 435 setTruncStoreAction(MVT::f32, MVT::f16, Expand); 436 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 437 setTruncStoreAction(MVT::f64, MVT::f16, Expand); 438 setTruncStoreAction(MVT::f128, MVT::f80, Expand); 439 setTruncStoreAction(MVT::f128, MVT::f64, Expand); 440 setTruncStoreAction(MVT::f128, MVT::f32, Expand); 441 setTruncStoreAction(MVT::f128, MVT::f16, Expand); 442 443 setOperationAction(ISD::BITCAST, MVT::i16, Custom); 444 setOperationAction(ISD::BITCAST, MVT::f16, Custom); 445 446 // Indexed loads and stores are supported. 447 for (unsigned im = (unsigned)ISD::PRE_INC; 448 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) { 449 setIndexedLoadAction(im, MVT::i8, Legal); 450 setIndexedLoadAction(im, MVT::i16, Legal); 451 setIndexedLoadAction(im, MVT::i32, Legal); 452 setIndexedLoadAction(im, MVT::i64, Legal); 453 setIndexedLoadAction(im, MVT::f64, Legal); 454 setIndexedLoadAction(im, MVT::f32, Legal); 455 setIndexedLoadAction(im, MVT::f16, Legal); 456 setIndexedStoreAction(im, MVT::i8, Legal); 457 setIndexedStoreAction(im, MVT::i16, Legal); 458 setIndexedStoreAction(im, MVT::i32, Legal); 459 setIndexedStoreAction(im, MVT::i64, Legal); 460 setIndexedStoreAction(im, MVT::f64, Legal); 461 setIndexedStoreAction(im, MVT::f32, Legal); 462 setIndexedStoreAction(im, MVT::f16, Legal); 463 } 464 465 // Trap. 466 setOperationAction(ISD::TRAP, MVT::Other, Legal); 467 468 // We combine OR nodes for bitfield operations. 469 setTargetDAGCombine(ISD::OR); 470 471 // Vector add and sub nodes may conceal a high-half opportunity. 472 // Also, try to fold ADD into CSINC/CSINV.. 473 setTargetDAGCombine(ISD::ADD); 474 setTargetDAGCombine(ISD::SUB); 475 476 setTargetDAGCombine(ISD::XOR); 477 setTargetDAGCombine(ISD::SINT_TO_FP); 478 setTargetDAGCombine(ISD::UINT_TO_FP); 479 480 setTargetDAGCombine(ISD::FP_TO_SINT); 481 setTargetDAGCombine(ISD::FP_TO_UINT); 482 setTargetDAGCombine(ISD::FDIV); 483 484 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); 485 486 setTargetDAGCombine(ISD::ANY_EXTEND); 487 setTargetDAGCombine(ISD::ZERO_EXTEND); 488 setTargetDAGCombine(ISD::SIGN_EXTEND); 489 setTargetDAGCombine(ISD::BITCAST); 490 setTargetDAGCombine(ISD::CONCAT_VECTORS); 491 setTargetDAGCombine(ISD::STORE); 492 if (Subtarget->supportsAddressTopByteIgnored()) 493 setTargetDAGCombine(ISD::LOAD); 494 495 setTargetDAGCombine(ISD::MUL); 496 497 setTargetDAGCombine(ISD::SELECT); 498 setTargetDAGCombine(ISD::VSELECT); 499 500 setTargetDAGCombine(ISD::INTRINSIC_VOID); 501 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN); 502 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT); 503 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT); 504 505 MaxStoresPerMemset = MaxStoresPerMemsetOptSize = 8; 506 MaxStoresPerMemcpy = MaxStoresPerMemcpyOptSize = 4; 507 MaxStoresPerMemmove = MaxStoresPerMemmoveOptSize = 4; 508 509 setStackPointerRegisterToSaveRestore(AArch64::SP); 510 511 setSchedulingPreference(Sched::Hybrid); 512 513 // Enable TBZ/TBNZ 514 MaskAndBranchFoldingIsLegal = true; 515 EnableExtLdPromotion = true; 516 517 setMinFunctionAlignment(2); 518 519 setHasExtractBitsInsn(true); 520 521 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 522 523 if (Subtarget->hasNEON()) { 524 // FIXME: v1f64 shouldn't be legal if we can avoid it, because it leads to 525 // silliness like this: 526 setOperationAction(ISD::FABS, MVT::v1f64, Expand); 527 setOperationAction(ISD::FADD, MVT::v1f64, Expand); 528 setOperationAction(ISD::FCEIL, MVT::v1f64, Expand); 529 setOperationAction(ISD::FCOPYSIGN, MVT::v1f64, Expand); 530 setOperationAction(ISD::FCOS, MVT::v1f64, Expand); 531 setOperationAction(ISD::FDIV, MVT::v1f64, Expand); 532 setOperationAction(ISD::FFLOOR, MVT::v1f64, Expand); 533 setOperationAction(ISD::FMA, MVT::v1f64, Expand); 534 setOperationAction(ISD::FMUL, MVT::v1f64, Expand); 535 setOperationAction(ISD::FNEARBYINT, MVT::v1f64, Expand); 536 setOperationAction(ISD::FNEG, MVT::v1f64, Expand); 537 setOperationAction(ISD::FPOW, MVT::v1f64, Expand); 538 setOperationAction(ISD::FREM, MVT::v1f64, Expand); 539 setOperationAction(ISD::FROUND, MVT::v1f64, Expand); 540 setOperationAction(ISD::FRINT, MVT::v1f64, Expand); 541 setOperationAction(ISD::FSIN, MVT::v1f64, Expand); 542 setOperationAction(ISD::FSINCOS, MVT::v1f64, Expand); 543 setOperationAction(ISD::FSQRT, MVT::v1f64, Expand); 544 setOperationAction(ISD::FSUB, MVT::v1f64, Expand); 545 setOperationAction(ISD::FTRUNC, MVT::v1f64, Expand); 546 setOperationAction(ISD::SETCC, MVT::v1f64, Expand); 547 setOperationAction(ISD::BR_CC, MVT::v1f64, Expand); 548 setOperationAction(ISD::SELECT, MVT::v1f64, Expand); 549 setOperationAction(ISD::SELECT_CC, MVT::v1f64, Expand); 550 setOperationAction(ISD::FP_EXTEND, MVT::v1f64, Expand); 551 552 setOperationAction(ISD::FP_TO_SINT, MVT::v1i64, Expand); 553 setOperationAction(ISD::FP_TO_UINT, MVT::v1i64, Expand); 554 setOperationAction(ISD::SINT_TO_FP, MVT::v1i64, Expand); 555 setOperationAction(ISD::UINT_TO_FP, MVT::v1i64, Expand); 556 setOperationAction(ISD::FP_ROUND, MVT::v1f64, Expand); 557 558 setOperationAction(ISD::MUL, MVT::v1i64, Expand); 559 560 // AArch64 doesn't have a direct vector ->f32 conversion instructions for 561 // elements smaller than i32, so promote the input to i32 first. 562 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Promote); 563 setOperationAction(ISD::SINT_TO_FP, MVT::v4i8, Promote); 564 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Promote); 565 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Promote); 566 // i8 and i16 vector elements also need promotion to i32 for v8i8 or v8i16 567 // -> v8f16 conversions. 568 setOperationAction(ISD::SINT_TO_FP, MVT::v8i8, Promote); 569 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Promote); 570 setOperationAction(ISD::SINT_TO_FP, MVT::v8i16, Promote); 571 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Promote); 572 // Similarly, there is no direct i32 -> f64 vector conversion instruction. 573 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom); 574 setOperationAction(ISD::UINT_TO_FP, MVT::v2i32, Custom); 575 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Custom); 576 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Custom); 577 // Or, direct i32 -> f16 vector conversion. Set it so custom, so the 578 // conversion happens in two steps: v4i32 -> v4f32 -> v4f16 579 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Custom); 580 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Custom); 581 582 // AArch64 doesn't have MUL.2d: 583 setOperationAction(ISD::MUL, MVT::v2i64, Expand); 584 // Custom handling for some quad-vector types to detect MULL. 585 setOperationAction(ISD::MUL, MVT::v8i16, Custom); 586 setOperationAction(ISD::MUL, MVT::v4i32, Custom); 587 setOperationAction(ISD::MUL, MVT::v2i64, Custom); 588 589 setOperationAction(ISD::ANY_EXTEND, MVT::v4i32, Legal); 590 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Expand); 591 // Likewise, narrowing and extending vector loads/stores aren't handled 592 // directly. 593 for (MVT VT : MVT::vector_valuetypes()) { 594 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); 595 596 setOperationAction(ISD::MULHS, VT, Expand); 597 setOperationAction(ISD::SMUL_LOHI, VT, Expand); 598 setOperationAction(ISD::MULHU, VT, Expand); 599 setOperationAction(ISD::UMUL_LOHI, VT, Expand); 600 601 setOperationAction(ISD::BSWAP, VT, Expand); 602 603 for (MVT InnerVT : MVT::vector_valuetypes()) { 604 setTruncStoreAction(VT, InnerVT, Expand); 605 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand); 606 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand); 607 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand); 608 } 609 } 610 611 // AArch64 has implementations of a lot of rounding-like FP operations. 612 for (MVT Ty : {MVT::v2f32, MVT::v4f32, MVT::v2f64}) { 613 setOperationAction(ISD::FFLOOR, Ty, Legal); 614 setOperationAction(ISD::FNEARBYINT, Ty, Legal); 615 setOperationAction(ISD::FCEIL, Ty, Legal); 616 setOperationAction(ISD::FRINT, Ty, Legal); 617 setOperationAction(ISD::FTRUNC, Ty, Legal); 618 setOperationAction(ISD::FROUND, Ty, Legal); 619 } 620 } 621 622 // Prefer likely predicted branches to selects on out-of-order cores. 623 if (Subtarget->isCortexA57()) 624 PredictableSelectIsExpensive = true; 625 } 626 627 void AArch64TargetLowering::addTypeForNEON(EVT VT, EVT PromotedBitwiseVT) { 628 if (VT == MVT::v2f32 || VT == MVT::v4f16) { 629 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote); 630 AddPromotedToType(ISD::LOAD, VT.getSimpleVT(), MVT::v2i32); 631 632 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote); 633 AddPromotedToType(ISD::STORE, VT.getSimpleVT(), MVT::v2i32); 634 } else if (VT == MVT::v2f64 || VT == MVT::v4f32 || VT == MVT::v8f16) { 635 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote); 636 AddPromotedToType(ISD::LOAD, VT.getSimpleVT(), MVT::v2i64); 637 638 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote); 639 AddPromotedToType(ISD::STORE, VT.getSimpleVT(), MVT::v2i64); 640 } 641 642 // Mark vector float intrinsics as expand. 643 if (VT == MVT::v2f32 || VT == MVT::v4f32 || VT == MVT::v2f64) { 644 setOperationAction(ISD::FSIN, VT.getSimpleVT(), Expand); 645 setOperationAction(ISD::FCOS, VT.getSimpleVT(), Expand); 646 setOperationAction(ISD::FPOWI, VT.getSimpleVT(), Expand); 647 setOperationAction(ISD::FPOW, VT.getSimpleVT(), Expand); 648 setOperationAction(ISD::FLOG, VT.getSimpleVT(), Expand); 649 setOperationAction(ISD::FLOG2, VT.getSimpleVT(), Expand); 650 setOperationAction(ISD::FLOG10, VT.getSimpleVT(), Expand); 651 setOperationAction(ISD::FEXP, VT.getSimpleVT(), Expand); 652 setOperationAction(ISD::FEXP2, VT.getSimpleVT(), Expand); 653 654 // But we do support custom-lowering for FCOPYSIGN. 655 setOperationAction(ISD::FCOPYSIGN, VT.getSimpleVT(), Custom); 656 } 657 658 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom); 659 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom); 660 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom); 661 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom); 662 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Custom); 663 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom); 664 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom); 665 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom); 666 setOperationAction(ISD::AND, VT.getSimpleVT(), Custom); 667 setOperationAction(ISD::OR, VT.getSimpleVT(), Custom); 668 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom); 669 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal); 670 671 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand); 672 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand); 673 setOperationAction(ISD::VSELECT, VT.getSimpleVT(), Expand); 674 for (MVT InnerVT : MVT::all_valuetypes()) 675 setLoadExtAction(ISD::EXTLOAD, InnerVT, VT.getSimpleVT(), Expand); 676 677 // CNT supports only B element sizes. 678 if (VT != MVT::v8i8 && VT != MVT::v16i8) 679 setOperationAction(ISD::CTPOP, VT.getSimpleVT(), Expand); 680 681 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand); 682 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand); 683 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand); 684 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand); 685 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand); 686 687 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom); 688 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom); 689 690 // [SU][MIN|MAX] and [SU]ABSDIFF are available for all NEON types apart from 691 // i64. 692 if (!VT.isFloatingPoint() && 693 VT.getSimpleVT() != MVT::v2i64 && VT.getSimpleVT() != MVT::v1i64) 694 for (unsigned Opcode : {ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX, 695 ISD::SABSDIFF, ISD::UABSDIFF}) 696 setOperationAction(Opcode, VT.getSimpleVT(), Legal); 697 698 // F[MIN|MAX][NUM|NAN] are available for all FP NEON types (not f16 though!). 699 if (VT.isFloatingPoint() && VT.getVectorElementType() != MVT::f16) 700 for (unsigned Opcode : {ISD::FMINNAN, ISD::FMAXNAN, 701 ISD::FMINNUM, ISD::FMAXNUM}) 702 setOperationAction(Opcode, VT.getSimpleVT(), Legal); 703 704 if (Subtarget->isLittleEndian()) { 705 for (unsigned im = (unsigned)ISD::PRE_INC; 706 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) { 707 setIndexedLoadAction(im, VT.getSimpleVT(), Legal); 708 setIndexedStoreAction(im, VT.getSimpleVT(), Legal); 709 } 710 } 711 } 712 713 void AArch64TargetLowering::addDRTypeForNEON(MVT VT) { 714 addRegisterClass(VT, &AArch64::FPR64RegClass); 715 addTypeForNEON(VT, MVT::v2i32); 716 } 717 718 void AArch64TargetLowering::addQRTypeForNEON(MVT VT) { 719 addRegisterClass(VT, &AArch64::FPR128RegClass); 720 addTypeForNEON(VT, MVT::v4i32); 721 } 722 723 EVT AArch64TargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &, 724 EVT VT) const { 725 if (!VT.isVector()) 726 return MVT::i32; 727 return VT.changeVectorElementTypeToInteger(); 728 } 729 730 /// computeKnownBitsForTargetNode - Determine which of the bits specified in 731 /// Mask are known to be either zero or one and return them in the 732 /// KnownZero/KnownOne bitsets. 733 void AArch64TargetLowering::computeKnownBitsForTargetNode( 734 const SDValue Op, APInt &KnownZero, APInt &KnownOne, 735 const SelectionDAG &DAG, unsigned Depth) const { 736 switch (Op.getOpcode()) { 737 default: 738 break; 739 case AArch64ISD::CSEL: { 740 APInt KnownZero2, KnownOne2; 741 DAG.computeKnownBits(Op->getOperand(0), KnownZero, KnownOne, Depth + 1); 742 DAG.computeKnownBits(Op->getOperand(1), KnownZero2, KnownOne2, Depth + 1); 743 KnownZero &= KnownZero2; 744 KnownOne &= KnownOne2; 745 break; 746 } 747 case ISD::INTRINSIC_W_CHAIN: { 748 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1)); 749 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue()); 750 switch (IntID) { 751 default: return; 752 case Intrinsic::aarch64_ldaxr: 753 case Intrinsic::aarch64_ldxr: { 754 unsigned BitWidth = KnownOne.getBitWidth(); 755 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT(); 756 unsigned MemBits = VT.getScalarType().getSizeInBits(); 757 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits); 758 return; 759 } 760 } 761 break; 762 } 763 case ISD::INTRINSIC_WO_CHAIN: 764 case ISD::INTRINSIC_VOID: { 765 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 766 switch (IntNo) { 767 default: 768 break; 769 case Intrinsic::aarch64_neon_umaxv: 770 case Intrinsic::aarch64_neon_uminv: { 771 // Figure out the datatype of the vector operand. The UMINV instruction 772 // will zero extend the result, so we can mark as known zero all the 773 // bits larger than the element datatype. 32-bit or larget doesn't need 774 // this as those are legal types and will be handled by isel directly. 775 MVT VT = Op.getOperand(1).getValueType().getSimpleVT(); 776 unsigned BitWidth = KnownZero.getBitWidth(); 777 if (VT == MVT::v8i8 || VT == MVT::v16i8) { 778 assert(BitWidth >= 8 && "Unexpected width!"); 779 APInt Mask = APInt::getHighBitsSet(BitWidth, BitWidth - 8); 780 KnownZero |= Mask; 781 } else if (VT == MVT::v4i16 || VT == MVT::v8i16) { 782 assert(BitWidth >= 16 && "Unexpected width!"); 783 APInt Mask = APInt::getHighBitsSet(BitWidth, BitWidth - 16); 784 KnownZero |= Mask; 785 } 786 break; 787 } break; 788 } 789 } 790 } 791 } 792 793 MVT AArch64TargetLowering::getScalarShiftAmountTy(const DataLayout &DL, 794 EVT) const { 795 return MVT::i64; 796 } 797 798 bool AArch64TargetLowering::allowsMisalignedMemoryAccesses(EVT VT, 799 unsigned AddrSpace, 800 unsigned Align, 801 bool *Fast) const { 802 if (Subtarget->requiresStrictAlign()) 803 return false; 804 805 // FIXME: This is mostly true for Cyclone, but not necessarily others. 806 if (Fast) { 807 // FIXME: Define an attribute for slow unaligned accesses instead of 808 // relying on the CPU type as a proxy. 809 // On Cyclone, unaligned 128-bit stores are slow. 810 *Fast = !Subtarget->isCyclone() || VT.getStoreSize() != 16 || 811 // See comments in performSTORECombine() for more details about 812 // these conditions. 813 814 // Code that uses clang vector extensions can mark that it 815 // wants unaligned accesses to be treated as fast by 816 // underspecifying alignment to be 1 or 2. 817 Align <= 2 || 818 819 // Disregard v2i64. Memcpy lowering produces those and splitting 820 // them regresses performance on micro-benchmarks and olden/bh. 821 VT == MVT::v2i64; 822 } 823 return true; 824 } 825 826 FastISel * 827 AArch64TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo, 828 const TargetLibraryInfo *libInfo) const { 829 return AArch64::createFastISel(funcInfo, libInfo); 830 } 831 832 const char *AArch64TargetLowering::getTargetNodeName(unsigned Opcode) const { 833 switch ((AArch64ISD::NodeType)Opcode) { 834 case AArch64ISD::FIRST_NUMBER: break; 835 case AArch64ISD::CALL: return "AArch64ISD::CALL"; 836 case AArch64ISD::ADRP: return "AArch64ISD::ADRP"; 837 case AArch64ISD::ADDlow: return "AArch64ISD::ADDlow"; 838 case AArch64ISD::LOADgot: return "AArch64ISD::LOADgot"; 839 case AArch64ISD::RET_FLAG: return "AArch64ISD::RET_FLAG"; 840 case AArch64ISD::BRCOND: return "AArch64ISD::BRCOND"; 841 case AArch64ISD::CSEL: return "AArch64ISD::CSEL"; 842 case AArch64ISD::FCSEL: return "AArch64ISD::FCSEL"; 843 case AArch64ISD::CSINV: return "AArch64ISD::CSINV"; 844 case AArch64ISD::CSNEG: return "AArch64ISD::CSNEG"; 845 case AArch64ISD::CSINC: return "AArch64ISD::CSINC"; 846 case AArch64ISD::THREAD_POINTER: return "AArch64ISD::THREAD_POINTER"; 847 case AArch64ISD::TLSDESC_CALLSEQ: return "AArch64ISD::TLSDESC_CALLSEQ"; 848 case AArch64ISD::ADC: return "AArch64ISD::ADC"; 849 case AArch64ISD::SBC: return "AArch64ISD::SBC"; 850 case AArch64ISD::ADDS: return "AArch64ISD::ADDS"; 851 case AArch64ISD::SUBS: return "AArch64ISD::SUBS"; 852 case AArch64ISD::ADCS: return "AArch64ISD::ADCS"; 853 case AArch64ISD::SBCS: return "AArch64ISD::SBCS"; 854 case AArch64ISD::ANDS: return "AArch64ISD::ANDS"; 855 case AArch64ISD::CCMP: return "AArch64ISD::CCMP"; 856 case AArch64ISD::CCMN: return "AArch64ISD::CCMN"; 857 case AArch64ISD::FCCMP: return "AArch64ISD::FCCMP"; 858 case AArch64ISD::FCMP: return "AArch64ISD::FCMP"; 859 case AArch64ISD::DUP: return "AArch64ISD::DUP"; 860 case AArch64ISD::DUPLANE8: return "AArch64ISD::DUPLANE8"; 861 case AArch64ISD::DUPLANE16: return "AArch64ISD::DUPLANE16"; 862 case AArch64ISD::DUPLANE32: return "AArch64ISD::DUPLANE32"; 863 case AArch64ISD::DUPLANE64: return "AArch64ISD::DUPLANE64"; 864 case AArch64ISD::MOVI: return "AArch64ISD::MOVI"; 865 case AArch64ISD::MOVIshift: return "AArch64ISD::MOVIshift"; 866 case AArch64ISD::MOVIedit: return "AArch64ISD::MOVIedit"; 867 case AArch64ISD::MOVImsl: return "AArch64ISD::MOVImsl"; 868 case AArch64ISD::FMOV: return "AArch64ISD::FMOV"; 869 case AArch64ISD::MVNIshift: return "AArch64ISD::MVNIshift"; 870 case AArch64ISD::MVNImsl: return "AArch64ISD::MVNImsl"; 871 case AArch64ISD::BICi: return "AArch64ISD::BICi"; 872 case AArch64ISD::ORRi: return "AArch64ISD::ORRi"; 873 case AArch64ISD::BSL: return "AArch64ISD::BSL"; 874 case AArch64ISD::NEG: return "AArch64ISD::NEG"; 875 case AArch64ISD::EXTR: return "AArch64ISD::EXTR"; 876 case AArch64ISD::ZIP1: return "AArch64ISD::ZIP1"; 877 case AArch64ISD::ZIP2: return "AArch64ISD::ZIP2"; 878 case AArch64ISD::UZP1: return "AArch64ISD::UZP1"; 879 case AArch64ISD::UZP2: return "AArch64ISD::UZP2"; 880 case AArch64ISD::TRN1: return "AArch64ISD::TRN1"; 881 case AArch64ISD::TRN2: return "AArch64ISD::TRN2"; 882 case AArch64ISD::REV16: return "AArch64ISD::REV16"; 883 case AArch64ISD::REV32: return "AArch64ISD::REV32"; 884 case AArch64ISD::REV64: return "AArch64ISD::REV64"; 885 case AArch64ISD::EXT: return "AArch64ISD::EXT"; 886 case AArch64ISD::VSHL: return "AArch64ISD::VSHL"; 887 case AArch64ISD::VLSHR: return "AArch64ISD::VLSHR"; 888 case AArch64ISD::VASHR: return "AArch64ISD::VASHR"; 889 case AArch64ISD::CMEQ: return "AArch64ISD::CMEQ"; 890 case AArch64ISD::CMGE: return "AArch64ISD::CMGE"; 891 case AArch64ISD::CMGT: return "AArch64ISD::CMGT"; 892 case AArch64ISD::CMHI: return "AArch64ISD::CMHI"; 893 case AArch64ISD::CMHS: return "AArch64ISD::CMHS"; 894 case AArch64ISD::FCMEQ: return "AArch64ISD::FCMEQ"; 895 case AArch64ISD::FCMGE: return "AArch64ISD::FCMGE"; 896 case AArch64ISD::FCMGT: return "AArch64ISD::FCMGT"; 897 case AArch64ISD::CMEQz: return "AArch64ISD::CMEQz"; 898 case AArch64ISD::CMGEz: return "AArch64ISD::CMGEz"; 899 case AArch64ISD::CMGTz: return "AArch64ISD::CMGTz"; 900 case AArch64ISD::CMLEz: return "AArch64ISD::CMLEz"; 901 case AArch64ISD::CMLTz: return "AArch64ISD::CMLTz"; 902 case AArch64ISD::FCMEQz: return "AArch64ISD::FCMEQz"; 903 case AArch64ISD::FCMGEz: return "AArch64ISD::FCMGEz"; 904 case AArch64ISD::FCMGTz: return "AArch64ISD::FCMGTz"; 905 case AArch64ISD::FCMLEz: return "AArch64ISD::FCMLEz"; 906 case AArch64ISD::FCMLTz: return "AArch64ISD::FCMLTz"; 907 case AArch64ISD::SADDV: return "AArch64ISD::SADDV"; 908 case AArch64ISD::UADDV: return "AArch64ISD::UADDV"; 909 case AArch64ISD::SMINV: return "AArch64ISD::SMINV"; 910 case AArch64ISD::UMINV: return "AArch64ISD::UMINV"; 911 case AArch64ISD::SMAXV: return "AArch64ISD::SMAXV"; 912 case AArch64ISD::UMAXV: return "AArch64ISD::UMAXV"; 913 case AArch64ISD::NOT: return "AArch64ISD::NOT"; 914 case AArch64ISD::BIT: return "AArch64ISD::BIT"; 915 case AArch64ISD::CBZ: return "AArch64ISD::CBZ"; 916 case AArch64ISD::CBNZ: return "AArch64ISD::CBNZ"; 917 case AArch64ISD::TBZ: return "AArch64ISD::TBZ"; 918 case AArch64ISD::TBNZ: return "AArch64ISD::TBNZ"; 919 case AArch64ISD::TC_RETURN: return "AArch64ISD::TC_RETURN"; 920 case AArch64ISD::PREFETCH: return "AArch64ISD::PREFETCH"; 921 case AArch64ISD::SITOF: return "AArch64ISD::SITOF"; 922 case AArch64ISD::UITOF: return "AArch64ISD::UITOF"; 923 case AArch64ISD::NVCAST: return "AArch64ISD::NVCAST"; 924 case AArch64ISD::SQSHL_I: return "AArch64ISD::SQSHL_I"; 925 case AArch64ISD::UQSHL_I: return "AArch64ISD::UQSHL_I"; 926 case AArch64ISD::SRSHR_I: return "AArch64ISD::SRSHR_I"; 927 case AArch64ISD::URSHR_I: return "AArch64ISD::URSHR_I"; 928 case AArch64ISD::SQSHLU_I: return "AArch64ISD::SQSHLU_I"; 929 case AArch64ISD::WrapperLarge: return "AArch64ISD::WrapperLarge"; 930 case AArch64ISD::LD2post: return "AArch64ISD::LD2post"; 931 case AArch64ISD::LD3post: return "AArch64ISD::LD3post"; 932 case AArch64ISD::LD4post: return "AArch64ISD::LD4post"; 933 case AArch64ISD::ST2post: return "AArch64ISD::ST2post"; 934 case AArch64ISD::ST3post: return "AArch64ISD::ST3post"; 935 case AArch64ISD::ST4post: return "AArch64ISD::ST4post"; 936 case AArch64ISD::LD1x2post: return "AArch64ISD::LD1x2post"; 937 case AArch64ISD::LD1x3post: return "AArch64ISD::LD1x3post"; 938 case AArch64ISD::LD1x4post: return "AArch64ISD::LD1x4post"; 939 case AArch64ISD::ST1x2post: return "AArch64ISD::ST1x2post"; 940 case AArch64ISD::ST1x3post: return "AArch64ISD::ST1x3post"; 941 case AArch64ISD::ST1x4post: return "AArch64ISD::ST1x4post"; 942 case AArch64ISD::LD1DUPpost: return "AArch64ISD::LD1DUPpost"; 943 case AArch64ISD::LD2DUPpost: return "AArch64ISD::LD2DUPpost"; 944 case AArch64ISD::LD3DUPpost: return "AArch64ISD::LD3DUPpost"; 945 case AArch64ISD::LD4DUPpost: return "AArch64ISD::LD4DUPpost"; 946 case AArch64ISD::LD1LANEpost: return "AArch64ISD::LD1LANEpost"; 947 case AArch64ISD::LD2LANEpost: return "AArch64ISD::LD2LANEpost"; 948 case AArch64ISD::LD3LANEpost: return "AArch64ISD::LD3LANEpost"; 949 case AArch64ISD::LD4LANEpost: return "AArch64ISD::LD4LANEpost"; 950 case AArch64ISD::ST2LANEpost: return "AArch64ISD::ST2LANEpost"; 951 case AArch64ISD::ST3LANEpost: return "AArch64ISD::ST3LANEpost"; 952 case AArch64ISD::ST4LANEpost: return "AArch64ISD::ST4LANEpost"; 953 case AArch64ISD::SMULL: return "AArch64ISD::SMULL"; 954 case AArch64ISD::UMULL: return "AArch64ISD::UMULL"; 955 } 956 return nullptr; 957 } 958 959 MachineBasicBlock * 960 AArch64TargetLowering::EmitF128CSEL(MachineInstr *MI, 961 MachineBasicBlock *MBB) const { 962 // We materialise the F128CSEL pseudo-instruction as some control flow and a 963 // phi node: 964 965 // OrigBB: 966 // [... previous instrs leading to comparison ...] 967 // b.ne TrueBB 968 // b EndBB 969 // TrueBB: 970 // ; Fallthrough 971 // EndBB: 972 // Dest = PHI [IfTrue, TrueBB], [IfFalse, OrigBB] 973 974 MachineFunction *MF = MBB->getParent(); 975 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 976 const BasicBlock *LLVM_BB = MBB->getBasicBlock(); 977 DebugLoc DL = MI->getDebugLoc(); 978 MachineFunction::iterator It = ++MBB->getIterator(); 979 980 unsigned DestReg = MI->getOperand(0).getReg(); 981 unsigned IfTrueReg = MI->getOperand(1).getReg(); 982 unsigned IfFalseReg = MI->getOperand(2).getReg(); 983 unsigned CondCode = MI->getOperand(3).getImm(); 984 bool NZCVKilled = MI->getOperand(4).isKill(); 985 986 MachineBasicBlock *TrueBB = MF->CreateMachineBasicBlock(LLVM_BB); 987 MachineBasicBlock *EndBB = MF->CreateMachineBasicBlock(LLVM_BB); 988 MF->insert(It, TrueBB); 989 MF->insert(It, EndBB); 990 991 // Transfer rest of current basic-block to EndBB 992 EndBB->splice(EndBB->begin(), MBB, std::next(MachineBasicBlock::iterator(MI)), 993 MBB->end()); 994 EndBB->transferSuccessorsAndUpdatePHIs(MBB); 995 996 BuildMI(MBB, DL, TII->get(AArch64::Bcc)).addImm(CondCode).addMBB(TrueBB); 997 BuildMI(MBB, DL, TII->get(AArch64::B)).addMBB(EndBB); 998 MBB->addSuccessor(TrueBB); 999 MBB->addSuccessor(EndBB); 1000 1001 // TrueBB falls through to the end. 1002 TrueBB->addSuccessor(EndBB); 1003 1004 if (!NZCVKilled) { 1005 TrueBB->addLiveIn(AArch64::NZCV); 1006 EndBB->addLiveIn(AArch64::NZCV); 1007 } 1008 1009 BuildMI(*EndBB, EndBB->begin(), DL, TII->get(AArch64::PHI), DestReg) 1010 .addReg(IfTrueReg) 1011 .addMBB(TrueBB) 1012 .addReg(IfFalseReg) 1013 .addMBB(MBB); 1014 1015 MI->eraseFromParent(); 1016 return EndBB; 1017 } 1018 1019 MachineBasicBlock * 1020 AArch64TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 1021 MachineBasicBlock *BB) const { 1022 switch (MI->getOpcode()) { 1023 default: 1024 #ifndef NDEBUG 1025 MI->dump(); 1026 #endif 1027 llvm_unreachable("Unexpected instruction for custom inserter!"); 1028 1029 case AArch64::F128CSEL: 1030 return EmitF128CSEL(MI, BB); 1031 1032 case TargetOpcode::STACKMAP: 1033 case TargetOpcode::PATCHPOINT: 1034 return emitPatchPoint(MI, BB); 1035 } 1036 } 1037 1038 //===----------------------------------------------------------------------===// 1039 // AArch64 Lowering private implementation. 1040 //===----------------------------------------------------------------------===// 1041 1042 //===----------------------------------------------------------------------===// 1043 // Lowering Code 1044 //===----------------------------------------------------------------------===// 1045 1046 /// changeIntCCToAArch64CC - Convert a DAG integer condition code to an AArch64 1047 /// CC 1048 static AArch64CC::CondCode changeIntCCToAArch64CC(ISD::CondCode CC) { 1049 switch (CC) { 1050 default: 1051 llvm_unreachable("Unknown condition code!"); 1052 case ISD::SETNE: 1053 return AArch64CC::NE; 1054 case ISD::SETEQ: 1055 return AArch64CC::EQ; 1056 case ISD::SETGT: 1057 return AArch64CC::GT; 1058 case ISD::SETGE: 1059 return AArch64CC::GE; 1060 case ISD::SETLT: 1061 return AArch64CC::LT; 1062 case ISD::SETLE: 1063 return AArch64CC::LE; 1064 case ISD::SETUGT: 1065 return AArch64CC::HI; 1066 case ISD::SETUGE: 1067 return AArch64CC::HS; 1068 case ISD::SETULT: 1069 return AArch64CC::LO; 1070 case ISD::SETULE: 1071 return AArch64CC::LS; 1072 } 1073 } 1074 1075 /// changeFPCCToAArch64CC - Convert a DAG fp condition code to an AArch64 CC. 1076 static void changeFPCCToAArch64CC(ISD::CondCode CC, 1077 AArch64CC::CondCode &CondCode, 1078 AArch64CC::CondCode &CondCode2) { 1079 CondCode2 = AArch64CC::AL; 1080 switch (CC) { 1081 default: 1082 llvm_unreachable("Unknown FP condition!"); 1083 case ISD::SETEQ: 1084 case ISD::SETOEQ: 1085 CondCode = AArch64CC::EQ; 1086 break; 1087 case ISD::SETGT: 1088 case ISD::SETOGT: 1089 CondCode = AArch64CC::GT; 1090 break; 1091 case ISD::SETGE: 1092 case ISD::SETOGE: 1093 CondCode = AArch64CC::GE; 1094 break; 1095 case ISD::SETOLT: 1096 CondCode = AArch64CC::MI; 1097 break; 1098 case ISD::SETOLE: 1099 CondCode = AArch64CC::LS; 1100 break; 1101 case ISD::SETONE: 1102 CondCode = AArch64CC::MI; 1103 CondCode2 = AArch64CC::GT; 1104 break; 1105 case ISD::SETO: 1106 CondCode = AArch64CC::VC; 1107 break; 1108 case ISD::SETUO: 1109 CondCode = AArch64CC::VS; 1110 break; 1111 case ISD::SETUEQ: 1112 CondCode = AArch64CC::EQ; 1113 CondCode2 = AArch64CC::VS; 1114 break; 1115 case ISD::SETUGT: 1116 CondCode = AArch64CC::HI; 1117 break; 1118 case ISD::SETUGE: 1119 CondCode = AArch64CC::PL; 1120 break; 1121 case ISD::SETLT: 1122 case ISD::SETULT: 1123 CondCode = AArch64CC::LT; 1124 break; 1125 case ISD::SETLE: 1126 case ISD::SETULE: 1127 CondCode = AArch64CC::LE; 1128 break; 1129 case ISD::SETNE: 1130 case ISD::SETUNE: 1131 CondCode = AArch64CC::NE; 1132 break; 1133 } 1134 } 1135 1136 /// changeVectorFPCCToAArch64CC - Convert a DAG fp condition code to an AArch64 1137 /// CC usable with the vector instructions. Fewer operations are available 1138 /// without a real NZCV register, so we have to use less efficient combinations 1139 /// to get the same effect. 1140 static void changeVectorFPCCToAArch64CC(ISD::CondCode CC, 1141 AArch64CC::CondCode &CondCode, 1142 AArch64CC::CondCode &CondCode2, 1143 bool &Invert) { 1144 Invert = false; 1145 switch (CC) { 1146 default: 1147 // Mostly the scalar mappings work fine. 1148 changeFPCCToAArch64CC(CC, CondCode, CondCode2); 1149 break; 1150 case ISD::SETUO: 1151 Invert = true; // Fallthrough 1152 case ISD::SETO: 1153 CondCode = AArch64CC::MI; 1154 CondCode2 = AArch64CC::GE; 1155 break; 1156 case ISD::SETUEQ: 1157 case ISD::SETULT: 1158 case ISD::SETULE: 1159 case ISD::SETUGT: 1160 case ISD::SETUGE: 1161 // All of the compare-mask comparisons are ordered, but we can switch 1162 // between the two by a double inversion. E.g. ULE == !OGT. 1163 Invert = true; 1164 changeFPCCToAArch64CC(getSetCCInverse(CC, false), CondCode, CondCode2); 1165 break; 1166 } 1167 } 1168 1169 static bool isLegalArithImmed(uint64_t C) { 1170 // Matches AArch64DAGToDAGISel::SelectArithImmed(). 1171 return (C >> 12 == 0) || ((C & 0xFFFULL) == 0 && C >> 24 == 0); 1172 } 1173 1174 static SDValue emitComparison(SDValue LHS, SDValue RHS, ISD::CondCode CC, 1175 SDLoc dl, SelectionDAG &DAG) { 1176 EVT VT = LHS.getValueType(); 1177 1178 if (VT.isFloatingPoint()) 1179 return DAG.getNode(AArch64ISD::FCMP, dl, VT, LHS, RHS); 1180 1181 // The CMP instruction is just an alias for SUBS, and representing it as 1182 // SUBS means that it's possible to get CSE with subtract operations. 1183 // A later phase can perform the optimization of setting the destination 1184 // register to WZR/XZR if it ends up being unused. 1185 unsigned Opcode = AArch64ISD::SUBS; 1186 1187 if (RHS.getOpcode() == ISD::SUB && isa<ConstantSDNode>(RHS.getOperand(0)) && 1188 cast<ConstantSDNode>(RHS.getOperand(0))->getZExtValue() == 0 && 1189 (CC == ISD::SETEQ || CC == ISD::SETNE)) { 1190 // We'd like to combine a (CMP op1, (sub 0, op2) into a CMN instruction on 1191 // the grounds that "op1 - (-op2) == op1 + op2". However, the C and V flags 1192 // can be set differently by this operation. It comes down to whether 1193 // "SInt(~op2)+1 == SInt(~op2+1)" (and the same for UInt). If they are then 1194 // everything is fine. If not then the optimization is wrong. Thus general 1195 // comparisons are only valid if op2 != 0. 1196 1197 // So, finally, the only LLVM-native comparisons that don't mention C and V 1198 // are SETEQ and SETNE. They're the only ones we can safely use CMN for in 1199 // the absence of information about op2. 1200 Opcode = AArch64ISD::ADDS; 1201 RHS = RHS.getOperand(1); 1202 } else if (LHS.getOpcode() == ISD::AND && isa<ConstantSDNode>(RHS) && 1203 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 && 1204 !isUnsignedIntSetCC(CC)) { 1205 // Similarly, (CMP (and X, Y), 0) can be implemented with a TST 1206 // (a.k.a. ANDS) except that the flags are only guaranteed to work for one 1207 // of the signed comparisons. 1208 Opcode = AArch64ISD::ANDS; 1209 RHS = LHS.getOperand(1); 1210 LHS = LHS.getOperand(0); 1211 } 1212 1213 return DAG.getNode(Opcode, dl, DAG.getVTList(VT, MVT_CC), LHS, RHS) 1214 .getValue(1); 1215 } 1216 1217 /// \defgroup AArch64CCMP CMP;CCMP matching 1218 /// 1219 /// These functions deal with the formation of CMP;CCMP;... sequences. 1220 /// The CCMP/CCMN/FCCMP/FCCMPE instructions allow the conditional execution of 1221 /// a comparison. They set the NZCV flags to a predefined value if their 1222 /// predicate is false. This allows to express arbitrary conjunctions, for 1223 /// example "cmp 0 (and (setCA (cmp A)) (setCB (cmp B))))" 1224 /// expressed as: 1225 /// cmp A 1226 /// ccmp B, inv(CB), CA 1227 /// check for CB flags 1228 /// 1229 /// In general we can create code for arbitrary "... (and (and A B) C)" 1230 /// sequences. We can also implement some "or" expressions, because "(or A B)" 1231 /// is equivalent to "not (and (not A) (not B))" and we can implement some 1232 /// negation operations: 1233 /// We can negate the results of a single comparison by inverting the flags 1234 /// used when the predicate fails and inverting the flags tested in the next 1235 /// instruction; We can also negate the results of the whole previous 1236 /// conditional compare sequence by inverting the flags tested in the next 1237 /// instruction. However there is no way to negate the result of a partial 1238 /// sequence. 1239 /// 1240 /// Therefore on encountering an "or" expression we can negate the subtree on 1241 /// one side and have to be able to push the negate to the leafs of the subtree 1242 /// on the other side (see also the comments in code). As complete example: 1243 /// "or (or (setCA (cmp A)) (setCB (cmp B))) 1244 /// (and (setCC (cmp C)) (setCD (cmp D)))" 1245 /// is transformed to 1246 /// "not (and (not (and (setCC (cmp C)) (setCC (cmp D)))) 1247 /// (and (not (setCA (cmp A)) (not (setCB (cmp B))))))" 1248 /// and implemented as: 1249 /// cmp C 1250 /// ccmp D, inv(CD), CC 1251 /// ccmp A, CA, inv(CD) 1252 /// ccmp B, CB, inv(CA) 1253 /// check for CB flags 1254 /// A counterexample is "or (and A B) (and C D)" which cannot be implemented 1255 /// by conditional compare sequences. 1256 /// @{ 1257 1258 /// Create a conditional comparison; Use CCMP, CCMN or FCCMP as appropriate. 1259 static SDValue emitConditionalComparison(SDValue LHS, SDValue RHS, 1260 ISD::CondCode CC, SDValue CCOp, 1261 SDValue Condition, unsigned NZCV, 1262 SDLoc DL, SelectionDAG &DAG) { 1263 unsigned Opcode = 0; 1264 if (LHS.getValueType().isFloatingPoint()) 1265 Opcode = AArch64ISD::FCCMP; 1266 else if (RHS.getOpcode() == ISD::SUB) { 1267 SDValue SubOp0 = RHS.getOperand(0); 1268 if (const ConstantSDNode *SubOp0C = dyn_cast<ConstantSDNode>(SubOp0)) 1269 if (SubOp0C->isNullValue() && (CC == ISD::SETEQ || CC == ISD::SETNE)) { 1270 // See emitComparison() on why we can only do this for SETEQ and SETNE. 1271 Opcode = AArch64ISD::CCMN; 1272 RHS = RHS.getOperand(1); 1273 } 1274 } 1275 if (Opcode == 0) 1276 Opcode = AArch64ISD::CCMP; 1277 1278 SDValue NZCVOp = DAG.getConstant(NZCV, DL, MVT::i32); 1279 return DAG.getNode(Opcode, DL, MVT_CC, LHS, RHS, NZCVOp, Condition, CCOp); 1280 } 1281 1282 /// Returns true if @p Val is a tree of AND/OR/SETCC operations. 1283 /// CanPushNegate is set to true if we can push a negate operation through 1284 /// the tree in a was that we are left with AND operations and negate operations 1285 /// at the leafs only. i.e. "not (or (or x y) z)" can be changed to 1286 /// "and (and (not x) (not y)) (not z)"; "not (or (and x y) z)" cannot be 1287 /// brought into such a form. 1288 static bool isConjunctionDisjunctionTree(const SDValue Val, bool &CanPushNegate, 1289 unsigned Depth = 0) { 1290 if (!Val.hasOneUse()) 1291 return false; 1292 unsigned Opcode = Val->getOpcode(); 1293 if (Opcode == ISD::SETCC) { 1294 CanPushNegate = true; 1295 return true; 1296 } 1297 // Protect against stack overflow. 1298 if (Depth > 15) 1299 return false; 1300 if (Opcode == ISD::AND || Opcode == ISD::OR) { 1301 SDValue O0 = Val->getOperand(0); 1302 SDValue O1 = Val->getOperand(1); 1303 bool CanPushNegateL; 1304 if (!isConjunctionDisjunctionTree(O0, CanPushNegateL, Depth+1)) 1305 return false; 1306 bool CanPushNegateR; 1307 if (!isConjunctionDisjunctionTree(O1, CanPushNegateR, Depth+1)) 1308 return false; 1309 // We cannot push a negate through an AND operation (it would become an OR), 1310 // we can however change a (not (or x y)) to (and (not x) (not y)) if we can 1311 // push the negate through the x/y subtrees. 1312 CanPushNegate = (Opcode == ISD::OR) && CanPushNegateL && CanPushNegateR; 1313 return true; 1314 } 1315 return false; 1316 } 1317 1318 /// Emit conjunction or disjunction tree with the CMP/FCMP followed by a chain 1319 /// of CCMP/CFCMP ops. See @ref AArch64CCMP. 1320 /// Tries to transform the given i1 producing node @p Val to a series compare 1321 /// and conditional compare operations. @returns an NZCV flags producing node 1322 /// and sets @p OutCC to the flags that should be tested or returns SDValue() if 1323 /// transformation was not possible. 1324 /// On recursive invocations @p PushNegate may be set to true to have negation 1325 /// effects pushed to the tree leafs; @p Predicate is an NZCV flag predicate 1326 /// for the comparisons in the current subtree; @p Depth limits the search 1327 /// depth to avoid stack overflow. 1328 static SDValue emitConjunctionDisjunctionTree(SelectionDAG &DAG, SDValue Val, 1329 AArch64CC::CondCode &OutCC, bool PushNegate = false, 1330 SDValue CCOp = SDValue(), AArch64CC::CondCode Predicate = AArch64CC::AL, 1331 unsigned Depth = 0) { 1332 // We're at a tree leaf, produce a conditional comparison operation. 1333 unsigned Opcode = Val->getOpcode(); 1334 if (Opcode == ISD::SETCC) { 1335 SDValue LHS = Val->getOperand(0); 1336 SDValue RHS = Val->getOperand(1); 1337 ISD::CondCode CC = cast<CondCodeSDNode>(Val->getOperand(2))->get(); 1338 bool isInteger = LHS.getValueType().isInteger(); 1339 if (PushNegate) 1340 CC = getSetCCInverse(CC, isInteger); 1341 SDLoc DL(Val); 1342 // Determine OutCC and handle FP special case. 1343 if (isInteger) { 1344 OutCC = changeIntCCToAArch64CC(CC); 1345 } else { 1346 assert(LHS.getValueType().isFloatingPoint()); 1347 AArch64CC::CondCode ExtraCC; 1348 changeFPCCToAArch64CC(CC, OutCC, ExtraCC); 1349 // Surpisingly some floating point conditions can't be tested with a 1350 // single condition code. Construct an additional comparison in this case. 1351 // See comment below on how we deal with OR conditions. 1352 if (ExtraCC != AArch64CC::AL) { 1353 SDValue ExtraCmp; 1354 if (!CCOp.getNode()) 1355 ExtraCmp = emitComparison(LHS, RHS, CC, DL, DAG); 1356 else { 1357 SDValue ConditionOp = DAG.getConstant(Predicate, DL, MVT_CC); 1358 // Note that we want the inverse of ExtraCC, so NZCV is not inversed. 1359 unsigned NZCV = AArch64CC::getNZCVToSatisfyCondCode(ExtraCC); 1360 ExtraCmp = emitConditionalComparison(LHS, RHS, CC, CCOp, ConditionOp, 1361 NZCV, DL, DAG); 1362 } 1363 CCOp = ExtraCmp; 1364 Predicate = AArch64CC::getInvertedCondCode(ExtraCC); 1365 OutCC = AArch64CC::getInvertedCondCode(OutCC); 1366 } 1367 } 1368 1369 // Produce a normal comparison if we are first in the chain 1370 if (!CCOp.getNode()) 1371 return emitComparison(LHS, RHS, CC, DL, DAG); 1372 // Otherwise produce a ccmp. 1373 SDValue ConditionOp = DAG.getConstant(Predicate, DL, MVT_CC); 1374 AArch64CC::CondCode InvOutCC = AArch64CC::getInvertedCondCode(OutCC); 1375 unsigned NZCV = AArch64CC::getNZCVToSatisfyCondCode(InvOutCC); 1376 return emitConditionalComparison(LHS, RHS, CC, CCOp, ConditionOp, NZCV, DL, 1377 DAG); 1378 } else if ((Opcode != ISD::AND && Opcode != ISD::OR) || !Val->hasOneUse()) 1379 return SDValue(); 1380 1381 assert((Opcode == ISD::OR || !PushNegate) 1382 && "Can only push negate through OR operation"); 1383 1384 // Check if both sides can be transformed. 1385 SDValue LHS = Val->getOperand(0); 1386 SDValue RHS = Val->getOperand(1); 1387 bool CanPushNegateL; 1388 if (!isConjunctionDisjunctionTree(LHS, CanPushNegateL, Depth+1)) 1389 return SDValue(); 1390 bool CanPushNegateR; 1391 if (!isConjunctionDisjunctionTree(RHS, CanPushNegateR, Depth+1)) 1392 return SDValue(); 1393 1394 // Do we need to negate our operands? 1395 bool NegateOperands = Opcode == ISD::OR; 1396 // We can negate the results of all previous operations by inverting the 1397 // predicate flags giving us a free negation for one side. For the other side 1398 // we need to be able to push the negation to the leafs of the tree. 1399 if (NegateOperands) { 1400 if (!CanPushNegateL && !CanPushNegateR) 1401 return SDValue(); 1402 // Order the side where we can push the negate through to LHS. 1403 if (!CanPushNegateL && CanPushNegateR) 1404 std::swap(LHS, RHS); 1405 } else { 1406 bool NeedsNegOutL = LHS->getOpcode() == ISD::OR; 1407 bool NeedsNegOutR = RHS->getOpcode() == ISD::OR; 1408 if (NeedsNegOutL && NeedsNegOutR) 1409 return SDValue(); 1410 // Order the side where we need to negate the output flags to RHS so it 1411 // gets emitted first. 1412 if (NeedsNegOutL) 1413 std::swap(LHS, RHS); 1414 } 1415 1416 // Emit RHS. If we want to negate the tree we only need to push a negate 1417 // through if we are already in a PushNegate case, otherwise we can negate 1418 // the "flags to test" afterwards. 1419 AArch64CC::CondCode RHSCC; 1420 SDValue CmpR = emitConjunctionDisjunctionTree(DAG, RHS, RHSCC, PushNegate, 1421 CCOp, Predicate, Depth+1); 1422 if (NegateOperands && !PushNegate) 1423 RHSCC = AArch64CC::getInvertedCondCode(RHSCC); 1424 // Emit LHS. We must push the negate through if we need to negate it. 1425 SDValue CmpL = emitConjunctionDisjunctionTree(DAG, LHS, OutCC, NegateOperands, 1426 CmpR, RHSCC, Depth+1); 1427 // If we transformed an OR to and AND then we have to negate the result 1428 // (or absorb a PushNegate resulting in a double negation). 1429 if (Opcode == ISD::OR && !PushNegate) 1430 OutCC = AArch64CC::getInvertedCondCode(OutCC); 1431 return CmpL; 1432 } 1433 1434 /// @} 1435 1436 static SDValue getAArch64Cmp(SDValue LHS, SDValue RHS, ISD::CondCode CC, 1437 SDValue &AArch64cc, SelectionDAG &DAG, SDLoc dl) { 1438 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) { 1439 EVT VT = RHS.getValueType(); 1440 uint64_t C = RHSC->getZExtValue(); 1441 if (!isLegalArithImmed(C)) { 1442 // Constant does not fit, try adjusting it by one? 1443 switch (CC) { 1444 default: 1445 break; 1446 case ISD::SETLT: 1447 case ISD::SETGE: 1448 if ((VT == MVT::i32 && C != 0x80000000 && 1449 isLegalArithImmed((uint32_t)(C - 1))) || 1450 (VT == MVT::i64 && C != 0x80000000ULL && 1451 isLegalArithImmed(C - 1ULL))) { 1452 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT; 1453 C = (VT == MVT::i32) ? (uint32_t)(C - 1) : C - 1; 1454 RHS = DAG.getConstant(C, dl, VT); 1455 } 1456 break; 1457 case ISD::SETULT: 1458 case ISD::SETUGE: 1459 if ((VT == MVT::i32 && C != 0 && 1460 isLegalArithImmed((uint32_t)(C - 1))) || 1461 (VT == MVT::i64 && C != 0ULL && isLegalArithImmed(C - 1ULL))) { 1462 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT; 1463 C = (VT == MVT::i32) ? (uint32_t)(C - 1) : C - 1; 1464 RHS = DAG.getConstant(C, dl, VT); 1465 } 1466 break; 1467 case ISD::SETLE: 1468 case ISD::SETGT: 1469 if ((VT == MVT::i32 && C != INT32_MAX && 1470 isLegalArithImmed((uint32_t)(C + 1))) || 1471 (VT == MVT::i64 && C != INT64_MAX && 1472 isLegalArithImmed(C + 1ULL))) { 1473 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE; 1474 C = (VT == MVT::i32) ? (uint32_t)(C + 1) : C + 1; 1475 RHS = DAG.getConstant(C, dl, VT); 1476 } 1477 break; 1478 case ISD::SETULE: 1479 case ISD::SETUGT: 1480 if ((VT == MVT::i32 && C != UINT32_MAX && 1481 isLegalArithImmed((uint32_t)(C + 1))) || 1482 (VT == MVT::i64 && C != UINT64_MAX && 1483 isLegalArithImmed(C + 1ULL))) { 1484 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE; 1485 C = (VT == MVT::i32) ? (uint32_t)(C + 1) : C + 1; 1486 RHS = DAG.getConstant(C, dl, VT); 1487 } 1488 break; 1489 } 1490 } 1491 } 1492 SDValue Cmp; 1493 AArch64CC::CondCode AArch64CC; 1494 if ((CC == ISD::SETEQ || CC == ISD::SETNE) && isa<ConstantSDNode>(RHS)) { 1495 const ConstantSDNode *RHSC = cast<ConstantSDNode>(RHS); 1496 1497 // The imm operand of ADDS is an unsigned immediate, in the range 0 to 4095. 1498 // For the i8 operand, the largest immediate is 255, so this can be easily 1499 // encoded in the compare instruction. For the i16 operand, however, the 1500 // largest immediate cannot be encoded in the compare. 1501 // Therefore, use a sign extending load and cmn to avoid materializing the 1502 // -1 constant. For example, 1503 // movz w1, #65535 1504 // ldrh w0, [x0, #0] 1505 // cmp w0, w1 1506 // > 1507 // ldrsh w0, [x0, #0] 1508 // cmn w0, #1 1509 // Fundamental, we're relying on the property that (zext LHS) == (zext RHS) 1510 // if and only if (sext LHS) == (sext RHS). The checks are in place to 1511 // ensure both the LHS and RHS are truly zero extended and to make sure the 1512 // transformation is profitable. 1513 if ((RHSC->getZExtValue() >> 16 == 0) && isa<LoadSDNode>(LHS) && 1514 cast<LoadSDNode>(LHS)->getExtensionType() == ISD::ZEXTLOAD && 1515 cast<LoadSDNode>(LHS)->getMemoryVT() == MVT::i16 && 1516 LHS.getNode()->hasNUsesOfValue(1, 0)) { 1517 int16_t ValueofRHS = cast<ConstantSDNode>(RHS)->getZExtValue(); 1518 if (ValueofRHS < 0 && isLegalArithImmed(-ValueofRHS)) { 1519 SDValue SExt = 1520 DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, LHS.getValueType(), LHS, 1521 DAG.getValueType(MVT::i16)); 1522 Cmp = emitComparison(SExt, DAG.getConstant(ValueofRHS, dl, 1523 RHS.getValueType()), 1524 CC, dl, DAG); 1525 AArch64CC = changeIntCCToAArch64CC(CC); 1526 } 1527 } 1528 1529 if (!Cmp && (RHSC->isNullValue() || RHSC->isOne())) { 1530 if ((Cmp = emitConjunctionDisjunctionTree(DAG, LHS, AArch64CC))) { 1531 if ((CC == ISD::SETNE) ^ RHSC->isNullValue()) 1532 AArch64CC = AArch64CC::getInvertedCondCode(AArch64CC); 1533 } 1534 } 1535 } 1536 1537 if (!Cmp) { 1538 Cmp = emitComparison(LHS, RHS, CC, dl, DAG); 1539 AArch64CC = changeIntCCToAArch64CC(CC); 1540 } 1541 AArch64cc = DAG.getConstant(AArch64CC, dl, MVT_CC); 1542 return Cmp; 1543 } 1544 1545 static std::pair<SDValue, SDValue> 1546 getAArch64XALUOOp(AArch64CC::CondCode &CC, SDValue Op, SelectionDAG &DAG) { 1547 assert((Op.getValueType() == MVT::i32 || Op.getValueType() == MVT::i64) && 1548 "Unsupported value type"); 1549 SDValue Value, Overflow; 1550 SDLoc DL(Op); 1551 SDValue LHS = Op.getOperand(0); 1552 SDValue RHS = Op.getOperand(1); 1553 unsigned Opc = 0; 1554 switch (Op.getOpcode()) { 1555 default: 1556 llvm_unreachable("Unknown overflow instruction!"); 1557 case ISD::SADDO: 1558 Opc = AArch64ISD::ADDS; 1559 CC = AArch64CC::VS; 1560 break; 1561 case ISD::UADDO: 1562 Opc = AArch64ISD::ADDS; 1563 CC = AArch64CC::HS; 1564 break; 1565 case ISD::SSUBO: 1566 Opc = AArch64ISD::SUBS; 1567 CC = AArch64CC::VS; 1568 break; 1569 case ISD::USUBO: 1570 Opc = AArch64ISD::SUBS; 1571 CC = AArch64CC::LO; 1572 break; 1573 // Multiply needs a little bit extra work. 1574 case ISD::SMULO: 1575 case ISD::UMULO: { 1576 CC = AArch64CC::NE; 1577 bool IsSigned = Op.getOpcode() == ISD::SMULO; 1578 if (Op.getValueType() == MVT::i32) { 1579 unsigned ExtendOpc = IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 1580 // For a 32 bit multiply with overflow check we want the instruction 1581 // selector to generate a widening multiply (SMADDL/UMADDL). For that we 1582 // need to generate the following pattern: 1583 // (i64 add 0, (i64 mul (i64 sext|zext i32 %a), (i64 sext|zext i32 %b)) 1584 LHS = DAG.getNode(ExtendOpc, DL, MVT::i64, LHS); 1585 RHS = DAG.getNode(ExtendOpc, DL, MVT::i64, RHS); 1586 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, LHS, RHS); 1587 SDValue Add = DAG.getNode(ISD::ADD, DL, MVT::i64, Mul, 1588 DAG.getConstant(0, DL, MVT::i64)); 1589 // On AArch64 the upper 32 bits are always zero extended for a 32 bit 1590 // operation. We need to clear out the upper 32 bits, because we used a 1591 // widening multiply that wrote all 64 bits. In the end this should be a 1592 // noop. 1593 Value = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Add); 1594 if (IsSigned) { 1595 // The signed overflow check requires more than just a simple check for 1596 // any bit set in the upper 32 bits of the result. These bits could be 1597 // just the sign bits of a negative number. To perform the overflow 1598 // check we have to arithmetic shift right the 32nd bit of the result by 1599 // 31 bits. Then we compare the result to the upper 32 bits. 1600 SDValue UpperBits = DAG.getNode(ISD::SRL, DL, MVT::i64, Add, 1601 DAG.getConstant(32, DL, MVT::i64)); 1602 UpperBits = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, UpperBits); 1603 SDValue LowerBits = DAG.getNode(ISD::SRA, DL, MVT::i32, Value, 1604 DAG.getConstant(31, DL, MVT::i64)); 1605 // It is important that LowerBits is last, otherwise the arithmetic 1606 // shift will not be folded into the compare (SUBS). 1607 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32); 1608 Overflow = DAG.getNode(AArch64ISD::SUBS, DL, VTs, UpperBits, LowerBits) 1609 .getValue(1); 1610 } else { 1611 // The overflow check for unsigned multiply is easy. We only need to 1612 // check if any of the upper 32 bits are set. This can be done with a 1613 // CMP (shifted register). For that we need to generate the following 1614 // pattern: 1615 // (i64 AArch64ISD::SUBS i64 0, (i64 srl i64 %Mul, i64 32) 1616 SDValue UpperBits = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul, 1617 DAG.getConstant(32, DL, MVT::i64)); 1618 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::i32); 1619 Overflow = 1620 DAG.getNode(AArch64ISD::SUBS, DL, VTs, 1621 DAG.getConstant(0, DL, MVT::i64), 1622 UpperBits).getValue(1); 1623 } 1624 break; 1625 } 1626 assert(Op.getValueType() == MVT::i64 && "Expected an i64 value type"); 1627 // For the 64 bit multiply 1628 Value = DAG.getNode(ISD::MUL, DL, MVT::i64, LHS, RHS); 1629 if (IsSigned) { 1630 SDValue UpperBits = DAG.getNode(ISD::MULHS, DL, MVT::i64, LHS, RHS); 1631 SDValue LowerBits = DAG.getNode(ISD::SRA, DL, MVT::i64, Value, 1632 DAG.getConstant(63, DL, MVT::i64)); 1633 // It is important that LowerBits is last, otherwise the arithmetic 1634 // shift will not be folded into the compare (SUBS). 1635 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::i32); 1636 Overflow = DAG.getNode(AArch64ISD::SUBS, DL, VTs, UpperBits, LowerBits) 1637 .getValue(1); 1638 } else { 1639 SDValue UpperBits = DAG.getNode(ISD::MULHU, DL, MVT::i64, LHS, RHS); 1640 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::i32); 1641 Overflow = 1642 DAG.getNode(AArch64ISD::SUBS, DL, VTs, 1643 DAG.getConstant(0, DL, MVT::i64), 1644 UpperBits).getValue(1); 1645 } 1646 break; 1647 } 1648 } // switch (...) 1649 1650 if (Opc) { 1651 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::i32); 1652 1653 // Emit the AArch64 operation with overflow check. 1654 Value = DAG.getNode(Opc, DL, VTs, LHS, RHS); 1655 Overflow = Value.getValue(1); 1656 } 1657 return std::make_pair(Value, Overflow); 1658 } 1659 1660 SDValue AArch64TargetLowering::LowerF128Call(SDValue Op, SelectionDAG &DAG, 1661 RTLIB::Libcall Call) const { 1662 SmallVector<SDValue, 2> Ops(Op->op_begin(), Op->op_end()); 1663 return makeLibCall(DAG, Call, MVT::f128, Ops, false, SDLoc(Op)).first; 1664 } 1665 1666 static SDValue LowerXOR(SDValue Op, SelectionDAG &DAG) { 1667 SDValue Sel = Op.getOperand(0); 1668 SDValue Other = Op.getOperand(1); 1669 1670 // If neither operand is a SELECT_CC, give up. 1671 if (Sel.getOpcode() != ISD::SELECT_CC) 1672 std::swap(Sel, Other); 1673 if (Sel.getOpcode() != ISD::SELECT_CC) 1674 return Op; 1675 1676 // The folding we want to perform is: 1677 // (xor x, (select_cc a, b, cc, 0, -1) ) 1678 // --> 1679 // (csel x, (xor x, -1), cc ...) 1680 // 1681 // The latter will get matched to a CSINV instruction. 1682 1683 ISD::CondCode CC = cast<CondCodeSDNode>(Sel.getOperand(4))->get(); 1684 SDValue LHS = Sel.getOperand(0); 1685 SDValue RHS = Sel.getOperand(1); 1686 SDValue TVal = Sel.getOperand(2); 1687 SDValue FVal = Sel.getOperand(3); 1688 SDLoc dl(Sel); 1689 1690 // FIXME: This could be generalized to non-integer comparisons. 1691 if (LHS.getValueType() != MVT::i32 && LHS.getValueType() != MVT::i64) 1692 return Op; 1693 1694 ConstantSDNode *CFVal = dyn_cast<ConstantSDNode>(FVal); 1695 ConstantSDNode *CTVal = dyn_cast<ConstantSDNode>(TVal); 1696 1697 // The values aren't constants, this isn't the pattern we're looking for. 1698 if (!CFVal || !CTVal) 1699 return Op; 1700 1701 // We can commute the SELECT_CC by inverting the condition. This 1702 // might be needed to make this fit into a CSINV pattern. 1703 if (CTVal->isAllOnesValue() && CFVal->isNullValue()) { 1704 std::swap(TVal, FVal); 1705 std::swap(CTVal, CFVal); 1706 CC = ISD::getSetCCInverse(CC, true); 1707 } 1708 1709 // If the constants line up, perform the transform! 1710 if (CTVal->isNullValue() && CFVal->isAllOnesValue()) { 1711 SDValue CCVal; 1712 SDValue Cmp = getAArch64Cmp(LHS, RHS, CC, CCVal, DAG, dl); 1713 1714 FVal = Other; 1715 TVal = DAG.getNode(ISD::XOR, dl, Other.getValueType(), Other, 1716 DAG.getConstant(-1ULL, dl, Other.getValueType())); 1717 1718 return DAG.getNode(AArch64ISD::CSEL, dl, Sel.getValueType(), FVal, TVal, 1719 CCVal, Cmp); 1720 } 1721 1722 return Op; 1723 } 1724 1725 static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) { 1726 EVT VT = Op.getValueType(); 1727 1728 // Let legalize expand this if it isn't a legal type yet. 1729 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 1730 return SDValue(); 1731 1732 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 1733 1734 unsigned Opc; 1735 bool ExtraOp = false; 1736 switch (Op.getOpcode()) { 1737 default: 1738 llvm_unreachable("Invalid code"); 1739 case ISD::ADDC: 1740 Opc = AArch64ISD::ADDS; 1741 break; 1742 case ISD::SUBC: 1743 Opc = AArch64ISD::SUBS; 1744 break; 1745 case ISD::ADDE: 1746 Opc = AArch64ISD::ADCS; 1747 ExtraOp = true; 1748 break; 1749 case ISD::SUBE: 1750 Opc = AArch64ISD::SBCS; 1751 ExtraOp = true; 1752 break; 1753 } 1754 1755 if (!ExtraOp) 1756 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), Op.getOperand(1)); 1757 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), Op.getOperand(1), 1758 Op.getOperand(2)); 1759 } 1760 1761 static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) { 1762 // Let legalize expand this if it isn't a legal type yet. 1763 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType())) 1764 return SDValue(); 1765 1766 SDLoc dl(Op); 1767 AArch64CC::CondCode CC; 1768 // The actual operation that sets the overflow or carry flag. 1769 SDValue Value, Overflow; 1770 std::tie(Value, Overflow) = getAArch64XALUOOp(CC, Op, DAG); 1771 1772 // We use 0 and 1 as false and true values. 1773 SDValue TVal = DAG.getConstant(1, dl, MVT::i32); 1774 SDValue FVal = DAG.getConstant(0, dl, MVT::i32); 1775 1776 // We use an inverted condition, because the conditional select is inverted 1777 // too. This will allow it to be selected to a single instruction: 1778 // CSINC Wd, WZR, WZR, invert(cond). 1779 SDValue CCVal = DAG.getConstant(getInvertedCondCode(CC), dl, MVT::i32); 1780 Overflow = DAG.getNode(AArch64ISD::CSEL, dl, MVT::i32, FVal, TVal, 1781 CCVal, Overflow); 1782 1783 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); 1784 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow); 1785 } 1786 1787 // Prefetch operands are: 1788 // 1: Address to prefetch 1789 // 2: bool isWrite 1790 // 3: int locality (0 = no locality ... 3 = extreme locality) 1791 // 4: bool isDataCache 1792 static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG) { 1793 SDLoc DL(Op); 1794 unsigned IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue(); 1795 unsigned Locality = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue(); 1796 unsigned IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); 1797 1798 bool IsStream = !Locality; 1799 // When the locality number is set 1800 if (Locality) { 1801 // The front-end should have filtered out the out-of-range values 1802 assert(Locality <= 3 && "Prefetch locality out-of-range"); 1803 // The locality degree is the opposite of the cache speed. 1804 // Put the number the other way around. 1805 // The encoding starts at 0 for level 1 1806 Locality = 3 - Locality; 1807 } 1808 1809 // built the mask value encoding the expected behavior. 1810 unsigned PrfOp = (IsWrite << 4) | // Load/Store bit 1811 (!IsData << 3) | // IsDataCache bit 1812 (Locality << 1) | // Cache level bits 1813 (unsigned)IsStream; // Stream bit 1814 return DAG.getNode(AArch64ISD::PREFETCH, DL, MVT::Other, Op.getOperand(0), 1815 DAG.getConstant(PrfOp, DL, MVT::i32), Op.getOperand(1)); 1816 } 1817 1818 SDValue AArch64TargetLowering::LowerFP_EXTEND(SDValue Op, 1819 SelectionDAG &DAG) const { 1820 assert(Op.getValueType() == MVT::f128 && "Unexpected lowering"); 1821 1822 RTLIB::Libcall LC; 1823 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType()); 1824 1825 return LowerF128Call(Op, DAG, LC); 1826 } 1827 1828 SDValue AArch64TargetLowering::LowerFP_ROUND(SDValue Op, 1829 SelectionDAG &DAG) const { 1830 if (Op.getOperand(0).getValueType() != MVT::f128) { 1831 // It's legal except when f128 is involved 1832 return Op; 1833 } 1834 1835 RTLIB::Libcall LC; 1836 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType()); 1837 1838 // FP_ROUND node has a second operand indicating whether it is known to be 1839 // precise. That doesn't take part in the LibCall so we can't directly use 1840 // LowerF128Call. 1841 SDValue SrcVal = Op.getOperand(0); 1842 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false, 1843 SDLoc(Op)).first; 1844 } 1845 1846 static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) { 1847 // Warning: We maintain cost tables in AArch64TargetTransformInfo.cpp. 1848 // Any additional optimization in this function should be recorded 1849 // in the cost tables. 1850 EVT InVT = Op.getOperand(0).getValueType(); 1851 EVT VT = Op.getValueType(); 1852 1853 if (VT.getSizeInBits() < InVT.getSizeInBits()) { 1854 SDLoc dl(Op); 1855 SDValue Cv = 1856 DAG.getNode(Op.getOpcode(), dl, InVT.changeVectorElementTypeToInteger(), 1857 Op.getOperand(0)); 1858 return DAG.getNode(ISD::TRUNCATE, dl, VT, Cv); 1859 } 1860 1861 if (VT.getSizeInBits() > InVT.getSizeInBits()) { 1862 SDLoc dl(Op); 1863 MVT ExtVT = 1864 MVT::getVectorVT(MVT::getFloatingPointVT(VT.getScalarSizeInBits()), 1865 VT.getVectorNumElements()); 1866 SDValue Ext = DAG.getNode(ISD::FP_EXTEND, dl, ExtVT, Op.getOperand(0)); 1867 return DAG.getNode(Op.getOpcode(), dl, VT, Ext); 1868 } 1869 1870 // Type changing conversions are illegal. 1871 return Op; 1872 } 1873 1874 SDValue AArch64TargetLowering::LowerFP_TO_INT(SDValue Op, 1875 SelectionDAG &DAG) const { 1876 if (Op.getOperand(0).getValueType().isVector()) 1877 return LowerVectorFP_TO_INT(Op, DAG); 1878 1879 // f16 conversions are promoted to f32. 1880 if (Op.getOperand(0).getValueType() == MVT::f16) { 1881 SDLoc dl(Op); 1882 return DAG.getNode( 1883 Op.getOpcode(), dl, Op.getValueType(), 1884 DAG.getNode(ISD::FP_EXTEND, dl, MVT::f32, Op.getOperand(0))); 1885 } 1886 1887 if (Op.getOperand(0).getValueType() != MVT::f128) { 1888 // It's legal except when f128 is involved 1889 return Op; 1890 } 1891 1892 RTLIB::Libcall LC; 1893 if (Op.getOpcode() == ISD::FP_TO_SINT) 1894 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(), Op.getValueType()); 1895 else 1896 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(), Op.getValueType()); 1897 1898 SmallVector<SDValue, 2> Ops(Op->op_begin(), Op->op_end()); 1899 return makeLibCall(DAG, LC, Op.getValueType(), Ops, false, SDLoc(Op)).first; 1900 } 1901 1902 static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 1903 // Warning: We maintain cost tables in AArch64TargetTransformInfo.cpp. 1904 // Any additional optimization in this function should be recorded 1905 // in the cost tables. 1906 EVT VT = Op.getValueType(); 1907 SDLoc dl(Op); 1908 SDValue In = Op.getOperand(0); 1909 EVT InVT = In.getValueType(); 1910 1911 if (VT.getSizeInBits() < InVT.getSizeInBits()) { 1912 MVT CastVT = 1913 MVT::getVectorVT(MVT::getFloatingPointVT(InVT.getScalarSizeInBits()), 1914 InVT.getVectorNumElements()); 1915 In = DAG.getNode(Op.getOpcode(), dl, CastVT, In); 1916 return DAG.getNode(ISD::FP_ROUND, dl, VT, In, DAG.getIntPtrConstant(0, dl)); 1917 } 1918 1919 if (VT.getSizeInBits() > InVT.getSizeInBits()) { 1920 unsigned CastOpc = 1921 Op.getOpcode() == ISD::SINT_TO_FP ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 1922 EVT CastVT = VT.changeVectorElementTypeToInteger(); 1923 In = DAG.getNode(CastOpc, dl, CastVT, In); 1924 return DAG.getNode(Op.getOpcode(), dl, VT, In); 1925 } 1926 1927 return Op; 1928 } 1929 1930 SDValue AArch64TargetLowering::LowerINT_TO_FP(SDValue Op, 1931 SelectionDAG &DAG) const { 1932 if (Op.getValueType().isVector()) 1933 return LowerVectorINT_TO_FP(Op, DAG); 1934 1935 // f16 conversions are promoted to f32. 1936 if (Op.getValueType() == MVT::f16) { 1937 SDLoc dl(Op); 1938 return DAG.getNode( 1939 ISD::FP_ROUND, dl, MVT::f16, 1940 DAG.getNode(Op.getOpcode(), dl, MVT::f32, Op.getOperand(0)), 1941 DAG.getIntPtrConstant(0, dl)); 1942 } 1943 1944 // i128 conversions are libcalls. 1945 if (Op.getOperand(0).getValueType() == MVT::i128) 1946 return SDValue(); 1947 1948 // Other conversions are legal, unless it's to the completely software-based 1949 // fp128. 1950 if (Op.getValueType() != MVT::f128) 1951 return Op; 1952 1953 RTLIB::Libcall LC; 1954 if (Op.getOpcode() == ISD::SINT_TO_FP) 1955 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(), Op.getValueType()); 1956 else 1957 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(), Op.getValueType()); 1958 1959 return LowerF128Call(Op, DAG, LC); 1960 } 1961 1962 SDValue AArch64TargetLowering::LowerFSINCOS(SDValue Op, 1963 SelectionDAG &DAG) const { 1964 // For iOS, we want to call an alternative entry point: __sincos_stret, 1965 // which returns the values in two S / D registers. 1966 SDLoc dl(Op); 1967 SDValue Arg = Op.getOperand(0); 1968 EVT ArgVT = Arg.getValueType(); 1969 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 1970 1971 ArgListTy Args; 1972 ArgListEntry Entry; 1973 1974 Entry.Node = Arg; 1975 Entry.Ty = ArgTy; 1976 Entry.isSExt = false; 1977 Entry.isZExt = false; 1978 Args.push_back(Entry); 1979 1980 const char *LibcallName = 1981 (ArgVT == MVT::f64) ? "__sincos_stret" : "__sincosf_stret"; 1982 SDValue Callee = 1983 DAG.getExternalSymbol(LibcallName, getPointerTy(DAG.getDataLayout())); 1984 1985 StructType *RetTy = StructType::get(ArgTy, ArgTy, nullptr); 1986 TargetLowering::CallLoweringInfo CLI(DAG); 1987 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode()) 1988 .setCallee(CallingConv::Fast, RetTy, Callee, std::move(Args), 0); 1989 1990 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 1991 return CallResult.first; 1992 } 1993 1994 static SDValue LowerBITCAST(SDValue Op, SelectionDAG &DAG) { 1995 if (Op.getValueType() != MVT::f16) 1996 return SDValue(); 1997 1998 assert(Op.getOperand(0).getValueType() == MVT::i16); 1999 SDLoc DL(Op); 2000 2001 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Op.getOperand(0)); 2002 Op = DAG.getNode(ISD::BITCAST, DL, MVT::f32, Op); 2003 return SDValue( 2004 DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL, MVT::f16, Op, 2005 DAG.getTargetConstant(AArch64::hsub, DL, MVT::i32)), 2006 0); 2007 } 2008 2009 static EVT getExtensionTo64Bits(const EVT &OrigVT) { 2010 if (OrigVT.getSizeInBits() >= 64) 2011 return OrigVT; 2012 2013 assert(OrigVT.isSimple() && "Expecting a simple value type"); 2014 2015 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy; 2016 switch (OrigSimpleTy) { 2017 default: llvm_unreachable("Unexpected Vector Type"); 2018 case MVT::v2i8: 2019 case MVT::v2i16: 2020 return MVT::v2i32; 2021 case MVT::v4i8: 2022 return MVT::v4i16; 2023 } 2024 } 2025 2026 static SDValue addRequiredExtensionForVectorMULL(SDValue N, SelectionDAG &DAG, 2027 const EVT &OrigTy, 2028 const EVT &ExtTy, 2029 unsigned ExtOpcode) { 2030 // The vector originally had a size of OrigTy. It was then extended to ExtTy. 2031 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than 2032 // 64-bits we need to insert a new extension so that it will be 64-bits. 2033 assert(ExtTy.is128BitVector() && "Unexpected extension size"); 2034 if (OrigTy.getSizeInBits() >= 64) 2035 return N; 2036 2037 // Must extend size to at least 64 bits to be used as an operand for VMULL. 2038 EVT NewVT = getExtensionTo64Bits(OrigTy); 2039 2040 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N); 2041 } 2042 2043 static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG, 2044 bool isSigned) { 2045 EVT VT = N->getValueType(0); 2046 2047 if (N->getOpcode() != ISD::BUILD_VECTOR) 2048 return false; 2049 2050 for (const SDValue &Elt : N->op_values()) { 2051 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) { 2052 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 2053 unsigned HalfSize = EltSize / 2; 2054 if (isSigned) { 2055 if (!isIntN(HalfSize, C->getSExtValue())) 2056 return false; 2057 } else { 2058 if (!isUIntN(HalfSize, C->getZExtValue())) 2059 return false; 2060 } 2061 continue; 2062 } 2063 return false; 2064 } 2065 2066 return true; 2067 } 2068 2069 static SDValue skipExtensionForVectorMULL(SDNode *N, SelectionDAG &DAG) { 2070 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND) 2071 return addRequiredExtensionForVectorMULL(N->getOperand(0), DAG, 2072 N->getOperand(0)->getValueType(0), 2073 N->getValueType(0), 2074 N->getOpcode()); 2075 2076 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR"); 2077 EVT VT = N->getValueType(0); 2078 SDLoc dl(N); 2079 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2; 2080 unsigned NumElts = VT.getVectorNumElements(); 2081 MVT TruncVT = MVT::getIntegerVT(EltSize); 2082 SmallVector<SDValue, 8> Ops; 2083 for (unsigned i = 0; i != NumElts; ++i) { 2084 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i)); 2085 const APInt &CInt = C->getAPIntValue(); 2086 // Element types smaller than 32 bits are not legal, so use i32 elements. 2087 // The values are implicitly truncated so sext vs. zext doesn't matter. 2088 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32)); 2089 } 2090 return DAG.getNode(ISD::BUILD_VECTOR, dl, 2091 MVT::getVectorVT(TruncVT, NumElts), Ops); 2092 } 2093 2094 static bool isSignExtended(SDNode *N, SelectionDAG &DAG) { 2095 if (N->getOpcode() == ISD::SIGN_EXTEND) 2096 return true; 2097 if (isExtendedBUILD_VECTOR(N, DAG, true)) 2098 return true; 2099 return false; 2100 } 2101 2102 static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) { 2103 if (N->getOpcode() == ISD::ZERO_EXTEND) 2104 return true; 2105 if (isExtendedBUILD_VECTOR(N, DAG, false)) 2106 return true; 2107 return false; 2108 } 2109 2110 static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) { 2111 unsigned Opcode = N->getOpcode(); 2112 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 2113 SDNode *N0 = N->getOperand(0).getNode(); 2114 SDNode *N1 = N->getOperand(1).getNode(); 2115 return N0->hasOneUse() && N1->hasOneUse() && 2116 isSignExtended(N0, DAG) && isSignExtended(N1, DAG); 2117 } 2118 return false; 2119 } 2120 2121 static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) { 2122 unsigned Opcode = N->getOpcode(); 2123 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 2124 SDNode *N0 = N->getOperand(0).getNode(); 2125 SDNode *N1 = N->getOperand(1).getNode(); 2126 return N0->hasOneUse() && N1->hasOneUse() && 2127 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG); 2128 } 2129 return false; 2130 } 2131 2132 static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) { 2133 // Multiplications are only custom-lowered for 128-bit vectors so that 2134 // VMULL can be detected. Otherwise v2i64 multiplications are not legal. 2135 EVT VT = Op.getValueType(); 2136 assert(VT.is128BitVector() && VT.isInteger() && 2137 "unexpected type for custom-lowering ISD::MUL"); 2138 SDNode *N0 = Op.getOperand(0).getNode(); 2139 SDNode *N1 = Op.getOperand(1).getNode(); 2140 unsigned NewOpc = 0; 2141 bool isMLA = false; 2142 bool isN0SExt = isSignExtended(N0, DAG); 2143 bool isN1SExt = isSignExtended(N1, DAG); 2144 if (isN0SExt && isN1SExt) 2145 NewOpc = AArch64ISD::SMULL; 2146 else { 2147 bool isN0ZExt = isZeroExtended(N0, DAG); 2148 bool isN1ZExt = isZeroExtended(N1, DAG); 2149 if (isN0ZExt && isN1ZExt) 2150 NewOpc = AArch64ISD::UMULL; 2151 else if (isN1SExt || isN1ZExt) { 2152 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these 2153 // into (s/zext A * s/zext C) + (s/zext B * s/zext C) 2154 if (isN1SExt && isAddSubSExt(N0, DAG)) { 2155 NewOpc = AArch64ISD::SMULL; 2156 isMLA = true; 2157 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) { 2158 NewOpc = AArch64ISD::UMULL; 2159 isMLA = true; 2160 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) { 2161 std::swap(N0, N1); 2162 NewOpc = AArch64ISD::UMULL; 2163 isMLA = true; 2164 } 2165 } 2166 2167 if (!NewOpc) { 2168 if (VT == MVT::v2i64) 2169 // Fall through to expand this. It is not legal. 2170 return SDValue(); 2171 else 2172 // Other vector multiplications are legal. 2173 return Op; 2174 } 2175 } 2176 2177 // Legalize to a S/UMULL instruction 2178 SDLoc DL(Op); 2179 SDValue Op0; 2180 SDValue Op1 = skipExtensionForVectorMULL(N1, DAG); 2181 if (!isMLA) { 2182 Op0 = skipExtensionForVectorMULL(N0, DAG); 2183 assert(Op0.getValueType().is64BitVector() && 2184 Op1.getValueType().is64BitVector() && 2185 "unexpected types for extended operands to VMULL"); 2186 return DAG.getNode(NewOpc, DL, VT, Op0, Op1); 2187 } 2188 // Optimizing (zext A + zext B) * C, to (S/UMULL A, C) + (S/UMULL B, C) during 2189 // isel lowering to take advantage of no-stall back to back s/umul + s/umla. 2190 // This is true for CPUs with accumulate forwarding such as Cortex-A53/A57 2191 SDValue N00 = skipExtensionForVectorMULL(N0->getOperand(0).getNode(), DAG); 2192 SDValue N01 = skipExtensionForVectorMULL(N0->getOperand(1).getNode(), DAG); 2193 EVT Op1VT = Op1.getValueType(); 2194 return DAG.getNode(N0->getOpcode(), DL, VT, 2195 DAG.getNode(NewOpc, DL, VT, 2196 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1), 2197 DAG.getNode(NewOpc, DL, VT, 2198 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1)); 2199 } 2200 2201 SDValue AArch64TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, 2202 SelectionDAG &DAG) const { 2203 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 2204 SDLoc dl(Op); 2205 switch (IntNo) { 2206 default: return SDValue(); // Don't custom lower most intrinsics. 2207 case Intrinsic::aarch64_thread_pointer: { 2208 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2209 return DAG.getNode(AArch64ISD::THREAD_POINTER, dl, PtrVT); 2210 } 2211 case Intrinsic::aarch64_neon_smax: 2212 return DAG.getNode(ISD::SMAX, dl, Op.getValueType(), 2213 Op.getOperand(1), Op.getOperand(2)); 2214 case Intrinsic::aarch64_neon_umax: 2215 return DAG.getNode(ISD::UMAX, dl, Op.getValueType(), 2216 Op.getOperand(1), Op.getOperand(2)); 2217 case Intrinsic::aarch64_neon_smin: 2218 return DAG.getNode(ISD::SMIN, dl, Op.getValueType(), 2219 Op.getOperand(1), Op.getOperand(2)); 2220 case Intrinsic::aarch64_neon_umin: 2221 return DAG.getNode(ISD::UMIN, dl, Op.getValueType(), 2222 Op.getOperand(1), Op.getOperand(2)); 2223 } 2224 } 2225 2226 SDValue AArch64TargetLowering::LowerOperation(SDValue Op, 2227 SelectionDAG &DAG) const { 2228 switch (Op.getOpcode()) { 2229 default: 2230 llvm_unreachable("unimplemented operand"); 2231 return SDValue(); 2232 case ISD::BITCAST: 2233 return LowerBITCAST(Op, DAG); 2234 case ISD::GlobalAddress: 2235 return LowerGlobalAddress(Op, DAG); 2236 case ISD::GlobalTLSAddress: 2237 return LowerGlobalTLSAddress(Op, DAG); 2238 case ISD::SETCC: 2239 return LowerSETCC(Op, DAG); 2240 case ISD::BR_CC: 2241 return LowerBR_CC(Op, DAG); 2242 case ISD::SELECT: 2243 return LowerSELECT(Op, DAG); 2244 case ISD::SELECT_CC: 2245 return LowerSELECT_CC(Op, DAG); 2246 case ISD::JumpTable: 2247 return LowerJumpTable(Op, DAG); 2248 case ISD::ConstantPool: 2249 return LowerConstantPool(Op, DAG); 2250 case ISD::BlockAddress: 2251 return LowerBlockAddress(Op, DAG); 2252 case ISD::VASTART: 2253 return LowerVASTART(Op, DAG); 2254 case ISD::VACOPY: 2255 return LowerVACOPY(Op, DAG); 2256 case ISD::VAARG: 2257 return LowerVAARG(Op, DAG); 2258 case ISD::ADDC: 2259 case ISD::ADDE: 2260 case ISD::SUBC: 2261 case ISD::SUBE: 2262 return LowerADDC_ADDE_SUBC_SUBE(Op, DAG); 2263 case ISD::SADDO: 2264 case ISD::UADDO: 2265 case ISD::SSUBO: 2266 case ISD::USUBO: 2267 case ISD::SMULO: 2268 case ISD::UMULO: 2269 return LowerXALUO(Op, DAG); 2270 case ISD::FADD: 2271 return LowerF128Call(Op, DAG, RTLIB::ADD_F128); 2272 case ISD::FSUB: 2273 return LowerF128Call(Op, DAG, RTLIB::SUB_F128); 2274 case ISD::FMUL: 2275 return LowerF128Call(Op, DAG, RTLIB::MUL_F128); 2276 case ISD::FDIV: 2277 return LowerF128Call(Op, DAG, RTLIB::DIV_F128); 2278 case ISD::FP_ROUND: 2279 return LowerFP_ROUND(Op, DAG); 2280 case ISD::FP_EXTEND: 2281 return LowerFP_EXTEND(Op, DAG); 2282 case ISD::FRAMEADDR: 2283 return LowerFRAMEADDR(Op, DAG); 2284 case ISD::RETURNADDR: 2285 return LowerRETURNADDR(Op, DAG); 2286 case ISD::INSERT_VECTOR_ELT: 2287 return LowerINSERT_VECTOR_ELT(Op, DAG); 2288 case ISD::EXTRACT_VECTOR_ELT: 2289 return LowerEXTRACT_VECTOR_ELT(Op, DAG); 2290 case ISD::BUILD_VECTOR: 2291 return LowerBUILD_VECTOR(Op, DAG); 2292 case ISD::VECTOR_SHUFFLE: 2293 return LowerVECTOR_SHUFFLE(Op, DAG); 2294 case ISD::EXTRACT_SUBVECTOR: 2295 return LowerEXTRACT_SUBVECTOR(Op, DAG); 2296 case ISD::SRA: 2297 case ISD::SRL: 2298 case ISD::SHL: 2299 return LowerVectorSRA_SRL_SHL(Op, DAG); 2300 case ISD::SHL_PARTS: 2301 return LowerShiftLeftParts(Op, DAG); 2302 case ISD::SRL_PARTS: 2303 case ISD::SRA_PARTS: 2304 return LowerShiftRightParts(Op, DAG); 2305 case ISD::CTPOP: 2306 return LowerCTPOP(Op, DAG); 2307 case ISD::FCOPYSIGN: 2308 return LowerFCOPYSIGN(Op, DAG); 2309 case ISD::AND: 2310 return LowerVectorAND(Op, DAG); 2311 case ISD::OR: 2312 return LowerVectorOR(Op, DAG); 2313 case ISD::XOR: 2314 return LowerXOR(Op, DAG); 2315 case ISD::PREFETCH: 2316 return LowerPREFETCH(Op, DAG); 2317 case ISD::SINT_TO_FP: 2318 case ISD::UINT_TO_FP: 2319 return LowerINT_TO_FP(Op, DAG); 2320 case ISD::FP_TO_SINT: 2321 case ISD::FP_TO_UINT: 2322 return LowerFP_TO_INT(Op, DAG); 2323 case ISD::FSINCOS: 2324 return LowerFSINCOS(Op, DAG); 2325 case ISD::MUL: 2326 return LowerMUL(Op, DAG); 2327 case ISD::INTRINSIC_WO_CHAIN: 2328 return LowerINTRINSIC_WO_CHAIN(Op, DAG); 2329 } 2330 } 2331 2332 /// getFunctionAlignment - Return the Log2 alignment of this function. 2333 unsigned AArch64TargetLowering::getFunctionAlignment(const Function *F) const { 2334 return 2; 2335 } 2336 2337 //===----------------------------------------------------------------------===// 2338 // Calling Convention Implementation 2339 //===----------------------------------------------------------------------===// 2340 2341 #include "AArch64GenCallingConv.inc" 2342 2343 /// Selects the correct CCAssignFn for a given CallingConvention value. 2344 CCAssignFn *AArch64TargetLowering::CCAssignFnForCall(CallingConv::ID CC, 2345 bool IsVarArg) const { 2346 switch (CC) { 2347 default: 2348 llvm_unreachable("Unsupported calling convention."); 2349 case CallingConv::WebKit_JS: 2350 return CC_AArch64_WebKit_JS; 2351 case CallingConv::GHC: 2352 return CC_AArch64_GHC; 2353 case CallingConv::C: 2354 case CallingConv::Fast: 2355 if (!Subtarget->isTargetDarwin()) 2356 return CC_AArch64_AAPCS; 2357 return IsVarArg ? CC_AArch64_DarwinPCS_VarArg : CC_AArch64_DarwinPCS; 2358 } 2359 } 2360 2361 SDValue AArch64TargetLowering::LowerFormalArguments( 2362 SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 2363 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG, 2364 SmallVectorImpl<SDValue> &InVals) const { 2365 MachineFunction &MF = DAG.getMachineFunction(); 2366 MachineFrameInfo *MFI = MF.getFrameInfo(); 2367 2368 // Assign locations to all of the incoming arguments. 2369 SmallVector<CCValAssign, 16> ArgLocs; 2370 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 2371 *DAG.getContext()); 2372 2373 // At this point, Ins[].VT may already be promoted to i32. To correctly 2374 // handle passing i8 as i8 instead of i32 on stack, we pass in both i32 and 2375 // i8 to CC_AArch64_AAPCS with i32 being ValVT and i8 being LocVT. 2376 // Since AnalyzeFormalArguments uses Ins[].VT for both ValVT and LocVT, here 2377 // we use a special version of AnalyzeFormalArguments to pass in ValVT and 2378 // LocVT. 2379 unsigned NumArgs = Ins.size(); 2380 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin(); 2381 unsigned CurArgIdx = 0; 2382 for (unsigned i = 0; i != NumArgs; ++i) { 2383 MVT ValVT = Ins[i].VT; 2384 if (Ins[i].isOrigArg()) { 2385 std::advance(CurOrigArg, Ins[i].getOrigArgIndex() - CurArgIdx); 2386 CurArgIdx = Ins[i].getOrigArgIndex(); 2387 2388 // Get type of the original argument. 2389 EVT ActualVT = getValueType(DAG.getDataLayout(), CurOrigArg->getType(), 2390 /*AllowUnknown*/ true); 2391 MVT ActualMVT = ActualVT.isSimple() ? ActualVT.getSimpleVT() : MVT::Other; 2392 // If ActualMVT is i1/i8/i16, we should set LocVT to i8/i8/i16. 2393 if (ActualMVT == MVT::i1 || ActualMVT == MVT::i8) 2394 ValVT = MVT::i8; 2395 else if (ActualMVT == MVT::i16) 2396 ValVT = MVT::i16; 2397 } 2398 CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, /*IsVarArg=*/false); 2399 bool Res = 2400 AssignFn(i, ValVT, ValVT, CCValAssign::Full, Ins[i].Flags, CCInfo); 2401 assert(!Res && "Call operand has unhandled type"); 2402 (void)Res; 2403 } 2404 assert(ArgLocs.size() == Ins.size()); 2405 SmallVector<SDValue, 16> ArgValues; 2406 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 2407 CCValAssign &VA = ArgLocs[i]; 2408 2409 if (Ins[i].Flags.isByVal()) { 2410 // Byval is used for HFAs in the PCS, but the system should work in a 2411 // non-compliant manner for larger structs. 2412 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2413 int Size = Ins[i].Flags.getByValSize(); 2414 unsigned NumRegs = (Size + 7) / 8; 2415 2416 // FIXME: This works on big-endian for composite byvals, which are the common 2417 // case. It should also work for fundamental types too. 2418 unsigned FrameIdx = 2419 MFI->CreateFixedObject(8 * NumRegs, VA.getLocMemOffset(), false); 2420 SDValue FrameIdxN = DAG.getFrameIndex(FrameIdx, PtrVT); 2421 InVals.push_back(FrameIdxN); 2422 2423 continue; 2424 } 2425 2426 if (VA.isRegLoc()) { 2427 // Arguments stored in registers. 2428 EVT RegVT = VA.getLocVT(); 2429 2430 SDValue ArgValue; 2431 const TargetRegisterClass *RC; 2432 2433 if (RegVT == MVT::i32) 2434 RC = &AArch64::GPR32RegClass; 2435 else if (RegVT == MVT::i64) 2436 RC = &AArch64::GPR64RegClass; 2437 else if (RegVT == MVT::f16) 2438 RC = &AArch64::FPR16RegClass; 2439 else if (RegVT == MVT::f32) 2440 RC = &AArch64::FPR32RegClass; 2441 else if (RegVT == MVT::f64 || RegVT.is64BitVector()) 2442 RC = &AArch64::FPR64RegClass; 2443 else if (RegVT == MVT::f128 || RegVT.is128BitVector()) 2444 RC = &AArch64::FPR128RegClass; 2445 else 2446 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering"); 2447 2448 // Transform the arguments in physical registers into virtual ones. 2449 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 2450 ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT); 2451 2452 // If this is an 8, 16 or 32-bit value, it is really passed promoted 2453 // to 64 bits. Insert an assert[sz]ext to capture this, then 2454 // truncate to the right size. 2455 switch (VA.getLocInfo()) { 2456 default: 2457 llvm_unreachable("Unknown loc info!"); 2458 case CCValAssign::Full: 2459 break; 2460 case CCValAssign::BCvt: 2461 ArgValue = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), ArgValue); 2462 break; 2463 case CCValAssign::AExt: 2464 case CCValAssign::SExt: 2465 case CCValAssign::ZExt: 2466 // SelectionDAGBuilder will insert appropriate AssertZExt & AssertSExt 2467 // nodes after our lowering. 2468 assert(RegVT == Ins[i].VT && "incorrect register location selected"); 2469 break; 2470 } 2471 2472 InVals.push_back(ArgValue); 2473 2474 } else { // VA.isRegLoc() 2475 assert(VA.isMemLoc() && "CCValAssign is neither reg nor mem"); 2476 unsigned ArgOffset = VA.getLocMemOffset(); 2477 unsigned ArgSize = VA.getValVT().getSizeInBits() / 8; 2478 2479 uint32_t BEAlign = 0; 2480 if (!Subtarget->isLittleEndian() && ArgSize < 8 && 2481 !Ins[i].Flags.isInConsecutiveRegs()) 2482 BEAlign = 8 - ArgSize; 2483 2484 int FI = MFI->CreateFixedObject(ArgSize, ArgOffset + BEAlign, true); 2485 2486 // Create load nodes to retrieve arguments from the stack. 2487 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 2488 SDValue ArgValue; 2489 2490 // For NON_EXTLOAD, generic code in getLoad assert(ValVT == MemVT) 2491 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD; 2492 MVT MemVT = VA.getValVT(); 2493 2494 switch (VA.getLocInfo()) { 2495 default: 2496 break; 2497 case CCValAssign::BCvt: 2498 MemVT = VA.getLocVT(); 2499 break; 2500 case CCValAssign::SExt: 2501 ExtType = ISD::SEXTLOAD; 2502 break; 2503 case CCValAssign::ZExt: 2504 ExtType = ISD::ZEXTLOAD; 2505 break; 2506 case CCValAssign::AExt: 2507 ExtType = ISD::EXTLOAD; 2508 break; 2509 } 2510 2511 ArgValue = DAG.getExtLoad( 2512 ExtType, DL, VA.getLocVT(), Chain, FIN, 2513 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), 2514 MemVT, false, false, false, 0); 2515 2516 InVals.push_back(ArgValue); 2517 } 2518 } 2519 2520 // varargs 2521 if (isVarArg) { 2522 if (!Subtarget->isTargetDarwin()) { 2523 // The AAPCS variadic function ABI is identical to the non-variadic 2524 // one. As a result there may be more arguments in registers and we should 2525 // save them for future reference. 2526 saveVarArgRegisters(CCInfo, DAG, DL, Chain); 2527 } 2528 2529 AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); 2530 // This will point to the next argument passed via stack. 2531 unsigned StackOffset = CCInfo.getNextStackOffset(); 2532 // We currently pass all varargs at 8-byte alignment. 2533 StackOffset = ((StackOffset + 7) & ~7); 2534 AFI->setVarArgsStackIndex(MFI->CreateFixedObject(4, StackOffset, true)); 2535 } 2536 2537 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>(); 2538 unsigned StackArgSize = CCInfo.getNextStackOffset(); 2539 bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt; 2540 if (DoesCalleeRestoreStack(CallConv, TailCallOpt)) { 2541 // This is a non-standard ABI so by fiat I say we're allowed to make full 2542 // use of the stack area to be popped, which must be aligned to 16 bytes in 2543 // any case: 2544 StackArgSize = RoundUpToAlignment(StackArgSize, 16); 2545 2546 // If we're expected to restore the stack (e.g. fastcc) then we'll be adding 2547 // a multiple of 16. 2548 FuncInfo->setArgumentStackToRestore(StackArgSize); 2549 2550 // This realignment carries over to the available bytes below. Our own 2551 // callers will guarantee the space is free by giving an aligned value to 2552 // CALLSEQ_START. 2553 } 2554 // Even if we're not expected to free up the space, it's useful to know how 2555 // much is there while considering tail calls (because we can reuse it). 2556 FuncInfo->setBytesInStackArgArea(StackArgSize); 2557 2558 return Chain; 2559 } 2560 2561 void AArch64TargetLowering::saveVarArgRegisters(CCState &CCInfo, 2562 SelectionDAG &DAG, SDLoc DL, 2563 SDValue &Chain) const { 2564 MachineFunction &MF = DAG.getMachineFunction(); 2565 MachineFrameInfo *MFI = MF.getFrameInfo(); 2566 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>(); 2567 auto PtrVT = getPointerTy(DAG.getDataLayout()); 2568 2569 SmallVector<SDValue, 8> MemOps; 2570 2571 static const MCPhysReg GPRArgRegs[] = { AArch64::X0, AArch64::X1, AArch64::X2, 2572 AArch64::X3, AArch64::X4, AArch64::X5, 2573 AArch64::X6, AArch64::X7 }; 2574 static const unsigned NumGPRArgRegs = array_lengthof(GPRArgRegs); 2575 unsigned FirstVariadicGPR = CCInfo.getFirstUnallocated(GPRArgRegs); 2576 2577 unsigned GPRSaveSize = 8 * (NumGPRArgRegs - FirstVariadicGPR); 2578 int GPRIdx = 0; 2579 if (GPRSaveSize != 0) { 2580 GPRIdx = MFI->CreateStackObject(GPRSaveSize, 8, false); 2581 2582 SDValue FIN = DAG.getFrameIndex(GPRIdx, PtrVT); 2583 2584 for (unsigned i = FirstVariadicGPR; i < NumGPRArgRegs; ++i) { 2585 unsigned VReg = MF.addLiveIn(GPRArgRegs[i], &AArch64::GPR64RegClass); 2586 SDValue Val = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64); 2587 SDValue Store = DAG.getStore( 2588 Val.getValue(1), DL, Val, FIN, 2589 MachinePointerInfo::getStack(DAG.getMachineFunction(), i * 8), false, 2590 false, 0); 2591 MemOps.push_back(Store); 2592 FIN = 2593 DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getConstant(8, DL, PtrVT)); 2594 } 2595 } 2596 FuncInfo->setVarArgsGPRIndex(GPRIdx); 2597 FuncInfo->setVarArgsGPRSize(GPRSaveSize); 2598 2599 if (Subtarget->hasFPARMv8()) { 2600 static const MCPhysReg FPRArgRegs[] = { 2601 AArch64::Q0, AArch64::Q1, AArch64::Q2, AArch64::Q3, 2602 AArch64::Q4, AArch64::Q5, AArch64::Q6, AArch64::Q7}; 2603 static const unsigned NumFPRArgRegs = array_lengthof(FPRArgRegs); 2604 unsigned FirstVariadicFPR = CCInfo.getFirstUnallocated(FPRArgRegs); 2605 2606 unsigned FPRSaveSize = 16 * (NumFPRArgRegs - FirstVariadicFPR); 2607 int FPRIdx = 0; 2608 if (FPRSaveSize != 0) { 2609 FPRIdx = MFI->CreateStackObject(FPRSaveSize, 16, false); 2610 2611 SDValue FIN = DAG.getFrameIndex(FPRIdx, PtrVT); 2612 2613 for (unsigned i = FirstVariadicFPR; i < NumFPRArgRegs; ++i) { 2614 unsigned VReg = MF.addLiveIn(FPRArgRegs[i], &AArch64::FPR128RegClass); 2615 SDValue Val = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f128); 2616 2617 SDValue Store = DAG.getStore( 2618 Val.getValue(1), DL, Val, FIN, 2619 MachinePointerInfo::getStack(DAG.getMachineFunction(), i * 16), 2620 false, false, 0); 2621 MemOps.push_back(Store); 2622 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, 2623 DAG.getConstant(16, DL, PtrVT)); 2624 } 2625 } 2626 FuncInfo->setVarArgsFPRIndex(FPRIdx); 2627 FuncInfo->setVarArgsFPRSize(FPRSaveSize); 2628 } 2629 2630 if (!MemOps.empty()) { 2631 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps); 2632 } 2633 } 2634 2635 /// LowerCallResult - Lower the result values of a call into the 2636 /// appropriate copies out of appropriate physical registers. 2637 SDValue AArch64TargetLowering::LowerCallResult( 2638 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg, 2639 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG, 2640 SmallVectorImpl<SDValue> &InVals, bool isThisReturn, 2641 SDValue ThisVal) const { 2642 CCAssignFn *RetCC = CallConv == CallingConv::WebKit_JS 2643 ? RetCC_AArch64_WebKit_JS 2644 : RetCC_AArch64_AAPCS; 2645 // Assign locations to each value returned by this call. 2646 SmallVector<CCValAssign, 16> RVLocs; 2647 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 2648 *DAG.getContext()); 2649 CCInfo.AnalyzeCallResult(Ins, RetCC); 2650 2651 // Copy all of the result registers out of their specified physreg. 2652 for (unsigned i = 0; i != RVLocs.size(); ++i) { 2653 CCValAssign VA = RVLocs[i]; 2654 2655 // Pass 'this' value directly from the argument to return value, to avoid 2656 // reg unit interference 2657 if (i == 0 && isThisReturn) { 2658 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i64 && 2659 "unexpected return calling convention register assignment"); 2660 InVals.push_back(ThisVal); 2661 continue; 2662 } 2663 2664 SDValue Val = 2665 DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), InFlag); 2666 Chain = Val.getValue(1); 2667 InFlag = Val.getValue(2); 2668 2669 switch (VA.getLocInfo()) { 2670 default: 2671 llvm_unreachable("Unknown loc info!"); 2672 case CCValAssign::Full: 2673 break; 2674 case CCValAssign::BCvt: 2675 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val); 2676 break; 2677 } 2678 2679 InVals.push_back(Val); 2680 } 2681 2682 return Chain; 2683 } 2684 2685 bool AArch64TargetLowering::isEligibleForTailCallOptimization( 2686 SDValue Callee, CallingConv::ID CalleeCC, bool isVarArg, 2687 bool isCalleeStructRet, bool isCallerStructRet, 2688 const SmallVectorImpl<ISD::OutputArg> &Outs, 2689 const SmallVectorImpl<SDValue> &OutVals, 2690 const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const { 2691 // For CallingConv::C this function knows whether the ABI needs 2692 // changing. That's not true for other conventions so they will have to opt in 2693 // manually. 2694 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C) 2695 return false; 2696 2697 const MachineFunction &MF = DAG.getMachineFunction(); 2698 const Function *CallerF = MF.getFunction(); 2699 CallingConv::ID CallerCC = CallerF->getCallingConv(); 2700 bool CCMatch = CallerCC == CalleeCC; 2701 2702 // Byval parameters hand the function a pointer directly into the stack area 2703 // we want to reuse during a tail call. Working around this *is* possible (see 2704 // X86) but less efficient and uglier in LowerCall. 2705 for (Function::const_arg_iterator i = CallerF->arg_begin(), 2706 e = CallerF->arg_end(); 2707 i != e; ++i) 2708 if (i->hasByValAttr()) 2709 return false; 2710 2711 if (getTargetMachine().Options.GuaranteedTailCallOpt) { 2712 if (IsTailCallConvention(CalleeCC) && CCMatch) 2713 return true; 2714 return false; 2715 } 2716 2717 // Externally-defined functions with weak linkage should not be 2718 // tail-called on AArch64 when the OS does not support dynamic 2719 // pre-emption of symbols, as the AAELF spec requires normal calls 2720 // to undefined weak functions to be replaced with a NOP or jump to the 2721 // next instruction. The behaviour of branch instructions in this 2722 // situation (as used for tail calls) is implementation-defined, so we 2723 // cannot rely on the linker replacing the tail call with a return. 2724 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 2725 const GlobalValue *GV = G->getGlobal(); 2726 const Triple &TT = getTargetMachine().getTargetTriple(); 2727 if (GV->hasExternalWeakLinkage() && 2728 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO())) 2729 return false; 2730 } 2731 2732 // Now we search for cases where we can use a tail call without changing the 2733 // ABI. Sibcall is used in some places (particularly gcc) to refer to this 2734 // concept. 2735 2736 // I want anyone implementing a new calling convention to think long and hard 2737 // about this assert. 2738 assert((!isVarArg || CalleeCC == CallingConv::C) && 2739 "Unexpected variadic calling convention"); 2740 2741 if (isVarArg && !Outs.empty()) { 2742 // At least two cases here: if caller is fastcc then we can't have any 2743 // memory arguments (we'd be expected to clean up the stack afterwards). If 2744 // caller is C then we could potentially use its argument area. 2745 2746 // FIXME: for now we take the most conservative of these in both cases: 2747 // disallow all variadic memory operands. 2748 SmallVector<CCValAssign, 16> ArgLocs; 2749 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs, 2750 *DAG.getContext()); 2751 2752 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, true)); 2753 for (const CCValAssign &ArgLoc : ArgLocs) 2754 if (!ArgLoc.isRegLoc()) 2755 return false; 2756 } 2757 2758 // If the calling conventions do not match, then we'd better make sure the 2759 // results are returned in the same way as what the caller expects. 2760 if (!CCMatch) { 2761 SmallVector<CCValAssign, 16> RVLocs1; 2762 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1, 2763 *DAG.getContext()); 2764 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForCall(CalleeCC, isVarArg)); 2765 2766 SmallVector<CCValAssign, 16> RVLocs2; 2767 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2, 2768 *DAG.getContext()); 2769 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForCall(CallerCC, isVarArg)); 2770 2771 if (RVLocs1.size() != RVLocs2.size()) 2772 return false; 2773 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) { 2774 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc()) 2775 return false; 2776 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo()) 2777 return false; 2778 if (RVLocs1[i].isRegLoc()) { 2779 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg()) 2780 return false; 2781 } else { 2782 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset()) 2783 return false; 2784 } 2785 } 2786 } 2787 2788 // Nothing more to check if the callee is taking no arguments 2789 if (Outs.empty()) 2790 return true; 2791 2792 SmallVector<CCValAssign, 16> ArgLocs; 2793 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs, 2794 *DAG.getContext()); 2795 2796 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, isVarArg)); 2797 2798 const AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>(); 2799 2800 // If the stack arguments for this call would fit into our own save area then 2801 // the call can be made tail. 2802 return CCInfo.getNextStackOffset() <= FuncInfo->getBytesInStackArgArea(); 2803 } 2804 2805 SDValue AArch64TargetLowering::addTokenForArgument(SDValue Chain, 2806 SelectionDAG &DAG, 2807 MachineFrameInfo *MFI, 2808 int ClobberedFI) const { 2809 SmallVector<SDValue, 8> ArgChains; 2810 int64_t FirstByte = MFI->getObjectOffset(ClobberedFI); 2811 int64_t LastByte = FirstByte + MFI->getObjectSize(ClobberedFI) - 1; 2812 2813 // Include the original chain at the beginning of the list. When this is 2814 // used by target LowerCall hooks, this helps legalize find the 2815 // CALLSEQ_BEGIN node. 2816 ArgChains.push_back(Chain); 2817 2818 // Add a chain value for each stack argument corresponding 2819 for (SDNode::use_iterator U = DAG.getEntryNode().getNode()->use_begin(), 2820 UE = DAG.getEntryNode().getNode()->use_end(); 2821 U != UE; ++U) 2822 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U)) 2823 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr())) 2824 if (FI->getIndex() < 0) { 2825 int64_t InFirstByte = MFI->getObjectOffset(FI->getIndex()); 2826 int64_t InLastByte = InFirstByte; 2827 InLastByte += MFI->getObjectSize(FI->getIndex()) - 1; 2828 2829 if ((InFirstByte <= FirstByte && FirstByte <= InLastByte) || 2830 (FirstByte <= InFirstByte && InFirstByte <= LastByte)) 2831 ArgChains.push_back(SDValue(L, 1)); 2832 } 2833 2834 // Build a tokenfactor for all the chains. 2835 return DAG.getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains); 2836 } 2837 2838 bool AArch64TargetLowering::DoesCalleeRestoreStack(CallingConv::ID CallCC, 2839 bool TailCallOpt) const { 2840 return CallCC == CallingConv::Fast && TailCallOpt; 2841 } 2842 2843 bool AArch64TargetLowering::IsTailCallConvention(CallingConv::ID CallCC) const { 2844 return CallCC == CallingConv::Fast; 2845 } 2846 2847 /// LowerCall - Lower a call to a callseq_start + CALL + callseq_end chain, 2848 /// and add input and output parameter nodes. 2849 SDValue 2850 AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI, 2851 SmallVectorImpl<SDValue> &InVals) const { 2852 SelectionDAG &DAG = CLI.DAG; 2853 SDLoc &DL = CLI.DL; 2854 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs; 2855 SmallVector<SDValue, 32> &OutVals = CLI.OutVals; 2856 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins; 2857 SDValue Chain = CLI.Chain; 2858 SDValue Callee = CLI.Callee; 2859 bool &IsTailCall = CLI.IsTailCall; 2860 CallingConv::ID CallConv = CLI.CallConv; 2861 bool IsVarArg = CLI.IsVarArg; 2862 2863 MachineFunction &MF = DAG.getMachineFunction(); 2864 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet(); 2865 bool IsThisReturn = false; 2866 2867 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>(); 2868 bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt; 2869 bool IsSibCall = false; 2870 2871 if (IsTailCall) { 2872 // Check if it's really possible to do a tail call. 2873 IsTailCall = isEligibleForTailCallOptimization( 2874 Callee, CallConv, IsVarArg, IsStructRet, 2875 MF.getFunction()->hasStructRetAttr(), Outs, OutVals, Ins, DAG); 2876 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall()) 2877 report_fatal_error("failed to perform tail call elimination on a call " 2878 "site marked musttail"); 2879 2880 // A sibling call is one where we're under the usual C ABI and not planning 2881 // to change that but can still do a tail call: 2882 if (!TailCallOpt && IsTailCall) 2883 IsSibCall = true; 2884 2885 if (IsTailCall) 2886 ++NumTailCalls; 2887 } 2888 2889 // Analyze operands of the call, assigning locations to each operand. 2890 SmallVector<CCValAssign, 16> ArgLocs; 2891 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, 2892 *DAG.getContext()); 2893 2894 if (IsVarArg) { 2895 // Handle fixed and variable vector arguments differently. 2896 // Variable vector arguments always go into memory. 2897 unsigned NumArgs = Outs.size(); 2898 2899 for (unsigned i = 0; i != NumArgs; ++i) { 2900 MVT ArgVT = Outs[i].VT; 2901 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; 2902 CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, 2903 /*IsVarArg=*/ !Outs[i].IsFixed); 2904 bool Res = AssignFn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo); 2905 assert(!Res && "Call operand has unhandled type"); 2906 (void)Res; 2907 } 2908 } else { 2909 // At this point, Outs[].VT may already be promoted to i32. To correctly 2910 // handle passing i8 as i8 instead of i32 on stack, we pass in both i32 and 2911 // i8 to CC_AArch64_AAPCS with i32 being ValVT and i8 being LocVT. 2912 // Since AnalyzeCallOperands uses Ins[].VT for both ValVT and LocVT, here 2913 // we use a special version of AnalyzeCallOperands to pass in ValVT and 2914 // LocVT. 2915 unsigned NumArgs = Outs.size(); 2916 for (unsigned i = 0; i != NumArgs; ++i) { 2917 MVT ValVT = Outs[i].VT; 2918 // Get type of the original argument. 2919 EVT ActualVT = getValueType(DAG.getDataLayout(), 2920 CLI.getArgs()[Outs[i].OrigArgIndex].Ty, 2921 /*AllowUnknown*/ true); 2922 MVT ActualMVT = ActualVT.isSimple() ? ActualVT.getSimpleVT() : ValVT; 2923 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; 2924 // If ActualMVT is i1/i8/i16, we should set LocVT to i8/i8/i16. 2925 if (ActualMVT == MVT::i1 || ActualMVT == MVT::i8) 2926 ValVT = MVT::i8; 2927 else if (ActualMVT == MVT::i16) 2928 ValVT = MVT::i16; 2929 2930 CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, /*IsVarArg=*/false); 2931 bool Res = AssignFn(i, ValVT, ValVT, CCValAssign::Full, ArgFlags, CCInfo); 2932 assert(!Res && "Call operand has unhandled type"); 2933 (void)Res; 2934 } 2935 } 2936 2937 // Get a count of how many bytes are to be pushed on the stack. 2938 unsigned NumBytes = CCInfo.getNextStackOffset(); 2939 2940 if (IsSibCall) { 2941 // Since we're not changing the ABI to make this a tail call, the memory 2942 // operands are already available in the caller's incoming argument space. 2943 NumBytes = 0; 2944 } 2945 2946 // FPDiff is the byte offset of the call's argument area from the callee's. 2947 // Stores to callee stack arguments will be placed in FixedStackSlots offset 2948 // by this amount for a tail call. In a sibling call it must be 0 because the 2949 // caller will deallocate the entire stack and the callee still expects its 2950 // arguments to begin at SP+0. Completely unused for non-tail calls. 2951 int FPDiff = 0; 2952 2953 if (IsTailCall && !IsSibCall) { 2954 unsigned NumReusableBytes = FuncInfo->getBytesInStackArgArea(); 2955 2956 // Since callee will pop argument stack as a tail call, we must keep the 2957 // popped size 16-byte aligned. 2958 NumBytes = RoundUpToAlignment(NumBytes, 16); 2959 2960 // FPDiff will be negative if this tail call requires more space than we 2961 // would automatically have in our incoming argument space. Positive if we 2962 // can actually shrink the stack. 2963 FPDiff = NumReusableBytes - NumBytes; 2964 2965 // The stack pointer must be 16-byte aligned at all times it's used for a 2966 // memory operation, which in practice means at *all* times and in 2967 // particular across call boundaries. Therefore our own arguments started at 2968 // a 16-byte aligned SP and the delta applied for the tail call should 2969 // satisfy the same constraint. 2970 assert(FPDiff % 16 == 0 && "unaligned stack on tail call"); 2971 } 2972 2973 // Adjust the stack pointer for the new arguments... 2974 // These operations are automatically eliminated by the prolog/epilog pass 2975 if (!IsSibCall) 2976 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, DL, 2977 true), 2978 DL); 2979 2980 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL, AArch64::SP, 2981 getPointerTy(DAG.getDataLayout())); 2982 2983 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; 2984 SmallVector<SDValue, 8> MemOpChains; 2985 auto PtrVT = getPointerTy(DAG.getDataLayout()); 2986 2987 // Walk the register/memloc assignments, inserting copies/loads. 2988 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); i != e; 2989 ++i, ++realArgIdx) { 2990 CCValAssign &VA = ArgLocs[i]; 2991 SDValue Arg = OutVals[realArgIdx]; 2992 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 2993 2994 // Promote the value if needed. 2995 switch (VA.getLocInfo()) { 2996 default: 2997 llvm_unreachable("Unknown loc info!"); 2998 case CCValAssign::Full: 2999 break; 3000 case CCValAssign::SExt: 3001 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg); 3002 break; 3003 case CCValAssign::ZExt: 3004 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg); 3005 break; 3006 case CCValAssign::AExt: 3007 if (Outs[realArgIdx].ArgVT == MVT::i1) { 3008 // AAPCS requires i1 to be zero-extended to 8-bits by the caller. 3009 Arg = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Arg); 3010 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i8, Arg); 3011 } 3012 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg); 3013 break; 3014 case CCValAssign::BCvt: 3015 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg); 3016 break; 3017 case CCValAssign::FPExt: 3018 Arg = DAG.getNode(ISD::FP_EXTEND, DL, VA.getLocVT(), Arg); 3019 break; 3020 } 3021 3022 if (VA.isRegLoc()) { 3023 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i64) { 3024 assert(VA.getLocVT() == MVT::i64 && 3025 "unexpected calling convention register assignment"); 3026 assert(!Ins.empty() && Ins[0].VT == MVT::i64 && 3027 "unexpected use of 'returned'"); 3028 IsThisReturn = true; 3029 } 3030 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 3031 } else { 3032 assert(VA.isMemLoc()); 3033 3034 SDValue DstAddr; 3035 MachinePointerInfo DstInfo; 3036 3037 // FIXME: This works on big-endian for composite byvals, which are the 3038 // common case. It should also work for fundamental types too. 3039 uint32_t BEAlign = 0; 3040 unsigned OpSize = Flags.isByVal() ? Flags.getByValSize() * 8 3041 : VA.getValVT().getSizeInBits(); 3042 OpSize = (OpSize + 7) / 8; 3043 if (!Subtarget->isLittleEndian() && !Flags.isByVal() && 3044 !Flags.isInConsecutiveRegs()) { 3045 if (OpSize < 8) 3046 BEAlign = 8 - OpSize; 3047 } 3048 unsigned LocMemOffset = VA.getLocMemOffset(); 3049 int32_t Offset = LocMemOffset + BEAlign; 3050 SDValue PtrOff = DAG.getIntPtrConstant(Offset, DL); 3051 PtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff); 3052 3053 if (IsTailCall) { 3054 Offset = Offset + FPDiff; 3055 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true); 3056 3057 DstAddr = DAG.getFrameIndex(FI, PtrVT); 3058 DstInfo = 3059 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI); 3060 3061 // Make sure any stack arguments overlapping with where we're storing 3062 // are loaded before this eventual operation. Otherwise they'll be 3063 // clobbered. 3064 Chain = addTokenForArgument(Chain, DAG, MF.getFrameInfo(), FI); 3065 } else { 3066 SDValue PtrOff = DAG.getIntPtrConstant(Offset, DL); 3067 3068 DstAddr = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff); 3069 DstInfo = MachinePointerInfo::getStack(DAG.getMachineFunction(), 3070 LocMemOffset); 3071 } 3072 3073 if (Outs[i].Flags.isByVal()) { 3074 SDValue SizeNode = 3075 DAG.getConstant(Outs[i].Flags.getByValSize(), DL, MVT::i64); 3076 SDValue Cpy = DAG.getMemcpy( 3077 Chain, DL, DstAddr, Arg, SizeNode, Outs[i].Flags.getByValAlign(), 3078 /*isVol = */ false, /*AlwaysInline = */ false, 3079 /*isTailCall = */ false, 3080 DstInfo, MachinePointerInfo()); 3081 3082 MemOpChains.push_back(Cpy); 3083 } else { 3084 // Since we pass i1/i8/i16 as i1/i8/i16 on stack and Arg is already 3085 // promoted to a legal register type i32, we should truncate Arg back to 3086 // i1/i8/i16. 3087 if (VA.getValVT() == MVT::i1 || VA.getValVT() == MVT::i8 || 3088 VA.getValVT() == MVT::i16) 3089 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg); 3090 3091 SDValue Store = 3092 DAG.getStore(Chain, DL, Arg, DstAddr, DstInfo, false, false, 0); 3093 MemOpChains.push_back(Store); 3094 } 3095 } 3096 } 3097 3098 if (!MemOpChains.empty()) 3099 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains); 3100 3101 // Build a sequence of copy-to-reg nodes chained together with token chain 3102 // and flag operands which copy the outgoing args into the appropriate regs. 3103 SDValue InFlag; 3104 for (auto &RegToPass : RegsToPass) { 3105 Chain = DAG.getCopyToReg(Chain, DL, RegToPass.first, 3106 RegToPass.second, InFlag); 3107 InFlag = Chain.getValue(1); 3108 } 3109 3110 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every 3111 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 3112 // node so that legalize doesn't hack it. 3113 if (getTargetMachine().getCodeModel() == CodeModel::Large && 3114 Subtarget->isTargetMachO()) { 3115 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 3116 const GlobalValue *GV = G->getGlobal(); 3117 bool InternalLinkage = GV->hasInternalLinkage(); 3118 if (InternalLinkage) 3119 Callee = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 0); 3120 else { 3121 Callee = 3122 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_GOT); 3123 Callee = DAG.getNode(AArch64ISD::LOADgot, DL, PtrVT, Callee); 3124 } 3125 } else if (ExternalSymbolSDNode *S = 3126 dyn_cast<ExternalSymbolSDNode>(Callee)) { 3127 const char *Sym = S->getSymbol(); 3128 Callee = DAG.getTargetExternalSymbol(Sym, PtrVT, AArch64II::MO_GOT); 3129 Callee = DAG.getNode(AArch64ISD::LOADgot, DL, PtrVT, Callee); 3130 } 3131 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 3132 const GlobalValue *GV = G->getGlobal(); 3133 Callee = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 0); 3134 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 3135 const char *Sym = S->getSymbol(); 3136 Callee = DAG.getTargetExternalSymbol(Sym, PtrVT, 0); 3137 } 3138 3139 // We don't usually want to end the call-sequence here because we would tidy 3140 // the frame up *after* the call, however in the ABI-changing tail-call case 3141 // we've carefully laid out the parameters so that when sp is reset they'll be 3142 // in the correct location. 3143 if (IsTailCall && !IsSibCall) { 3144 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, DL, true), 3145 DAG.getIntPtrConstant(0, DL, true), InFlag, DL); 3146 InFlag = Chain.getValue(1); 3147 } 3148 3149 std::vector<SDValue> Ops; 3150 Ops.push_back(Chain); 3151 Ops.push_back(Callee); 3152 3153 if (IsTailCall) { 3154 // Each tail call may have to adjust the stack by a different amount, so 3155 // this information must travel along with the operation for eventual 3156 // consumption by emitEpilogue. 3157 Ops.push_back(DAG.getTargetConstant(FPDiff, DL, MVT::i32)); 3158 } 3159 3160 // Add argument registers to the end of the list so that they are known live 3161 // into the call. 3162 for (auto &RegToPass : RegsToPass) 3163 Ops.push_back(DAG.getRegister(RegToPass.first, 3164 RegToPass.second.getValueType())); 3165 3166 // Add a register mask operand representing the call-preserved registers. 3167 const uint32_t *Mask; 3168 const AArch64RegisterInfo *TRI = Subtarget->getRegisterInfo(); 3169 if (IsThisReturn) { 3170 // For 'this' returns, use the X0-preserving mask if applicable 3171 Mask = TRI->getThisReturnPreservedMask(MF, CallConv); 3172 if (!Mask) { 3173 IsThisReturn = false; 3174 Mask = TRI->getCallPreservedMask(MF, CallConv); 3175 } 3176 } else 3177 Mask = TRI->getCallPreservedMask(MF, CallConv); 3178 3179 assert(Mask && "Missing call preserved mask for calling convention"); 3180 Ops.push_back(DAG.getRegisterMask(Mask)); 3181 3182 if (InFlag.getNode()) 3183 Ops.push_back(InFlag); 3184 3185 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 3186 3187 // If we're doing a tall call, use a TC_RETURN here rather than an 3188 // actual call instruction. 3189 if (IsTailCall) { 3190 MF.getFrameInfo()->setHasTailCall(); 3191 return DAG.getNode(AArch64ISD::TC_RETURN, DL, NodeTys, Ops); 3192 } 3193 3194 // Returns a chain and a flag for retval copy to use. 3195 Chain = DAG.getNode(AArch64ISD::CALL, DL, NodeTys, Ops); 3196 InFlag = Chain.getValue(1); 3197 3198 uint64_t CalleePopBytes = DoesCalleeRestoreStack(CallConv, TailCallOpt) 3199 ? RoundUpToAlignment(NumBytes, 16) 3200 : 0; 3201 3202 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, DL, true), 3203 DAG.getIntPtrConstant(CalleePopBytes, DL, true), 3204 InFlag, DL); 3205 if (!Ins.empty()) 3206 InFlag = Chain.getValue(1); 3207 3208 // Handle result values, copying them out of physregs into vregs that we 3209 // return. 3210 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG, 3211 InVals, IsThisReturn, 3212 IsThisReturn ? OutVals[0] : SDValue()); 3213 } 3214 3215 bool AArch64TargetLowering::CanLowerReturn( 3216 CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg, 3217 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const { 3218 CCAssignFn *RetCC = CallConv == CallingConv::WebKit_JS 3219 ? RetCC_AArch64_WebKit_JS 3220 : RetCC_AArch64_AAPCS; 3221 SmallVector<CCValAssign, 16> RVLocs; 3222 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context); 3223 return CCInfo.CheckReturn(Outs, RetCC); 3224 } 3225 3226 SDValue 3227 AArch64TargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 3228 bool isVarArg, 3229 const SmallVectorImpl<ISD::OutputArg> &Outs, 3230 const SmallVectorImpl<SDValue> &OutVals, 3231 SDLoc DL, SelectionDAG &DAG) const { 3232 CCAssignFn *RetCC = CallConv == CallingConv::WebKit_JS 3233 ? RetCC_AArch64_WebKit_JS 3234 : RetCC_AArch64_AAPCS; 3235 SmallVector<CCValAssign, 16> RVLocs; 3236 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 3237 *DAG.getContext()); 3238 CCInfo.AnalyzeReturn(Outs, RetCC); 3239 3240 // Copy the result values into the output registers. 3241 SDValue Flag; 3242 SmallVector<SDValue, 4> RetOps(1, Chain); 3243 for (unsigned i = 0, realRVLocIdx = 0; i != RVLocs.size(); 3244 ++i, ++realRVLocIdx) { 3245 CCValAssign &VA = RVLocs[i]; 3246 assert(VA.isRegLoc() && "Can only return in registers!"); 3247 SDValue Arg = OutVals[realRVLocIdx]; 3248 3249 switch (VA.getLocInfo()) { 3250 default: 3251 llvm_unreachable("Unknown loc info!"); 3252 case CCValAssign::Full: 3253 if (Outs[i].ArgVT == MVT::i1) { 3254 // AAPCS requires i1 to be zero-extended to i8 by the producer of the 3255 // value. This is strictly redundant on Darwin (which uses "zeroext 3256 // i1"), but will be optimised out before ISel. 3257 Arg = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Arg); 3258 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg); 3259 } 3260 break; 3261 case CCValAssign::BCvt: 3262 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg); 3263 break; 3264 } 3265 3266 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag); 3267 Flag = Chain.getValue(1); 3268 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 3269 } 3270 3271 RetOps[0] = Chain; // Update chain. 3272 3273 // Add the flag if we have it. 3274 if (Flag.getNode()) 3275 RetOps.push_back(Flag); 3276 3277 return DAG.getNode(AArch64ISD::RET_FLAG, DL, MVT::Other, RetOps); 3278 } 3279 3280 //===----------------------------------------------------------------------===// 3281 // Other Lowering Code 3282 //===----------------------------------------------------------------------===// 3283 3284 SDValue AArch64TargetLowering::LowerGlobalAddress(SDValue Op, 3285 SelectionDAG &DAG) const { 3286 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3287 SDLoc DL(Op); 3288 const GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Op); 3289 const GlobalValue *GV = GN->getGlobal(); 3290 unsigned char OpFlags = 3291 Subtarget->ClassifyGlobalReference(GV, getTargetMachine()); 3292 3293 assert(cast<GlobalAddressSDNode>(Op)->getOffset() == 0 && 3294 "unexpected offset in global node"); 3295 3296 // This also catched the large code model case for Darwin. 3297 if ((OpFlags & AArch64II::MO_GOT) != 0) { 3298 SDValue GotAddr = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, OpFlags); 3299 // FIXME: Once remat is capable of dealing with instructions with register 3300 // operands, expand this into two nodes instead of using a wrapper node. 3301 return DAG.getNode(AArch64ISD::LOADgot, DL, PtrVT, GotAddr); 3302 } 3303 3304 if ((OpFlags & AArch64II::MO_CONSTPOOL) != 0) { 3305 assert(getTargetMachine().getCodeModel() == CodeModel::Small && 3306 "use of MO_CONSTPOOL only supported on small model"); 3307 SDValue Hi = DAG.getTargetConstantPool(GV, PtrVT, 0, 0, AArch64II::MO_PAGE); 3308 SDValue ADRP = DAG.getNode(AArch64ISD::ADRP, DL, PtrVT, Hi); 3309 unsigned char LoFlags = AArch64II::MO_PAGEOFF | AArch64II::MO_NC; 3310 SDValue Lo = DAG.getTargetConstantPool(GV, PtrVT, 0, 0, LoFlags); 3311 SDValue PoolAddr = DAG.getNode(AArch64ISD::ADDlow, DL, PtrVT, ADRP, Lo); 3312 SDValue GlobalAddr = DAG.getLoad( 3313 PtrVT, DL, DAG.getEntryNode(), PoolAddr, 3314 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), 3315 /*isVolatile=*/false, 3316 /*isNonTemporal=*/true, 3317 /*isInvariant=*/true, 8); 3318 if (GN->getOffset() != 0) 3319 return DAG.getNode(ISD::ADD, DL, PtrVT, GlobalAddr, 3320 DAG.getConstant(GN->getOffset(), DL, PtrVT)); 3321 return GlobalAddr; 3322 } 3323 3324 if (getTargetMachine().getCodeModel() == CodeModel::Large) { 3325 const unsigned char MO_NC = AArch64II::MO_NC; 3326 return DAG.getNode( 3327 AArch64ISD::WrapperLarge, DL, PtrVT, 3328 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_G3), 3329 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_G2 | MO_NC), 3330 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_G1 | MO_NC), 3331 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_G0 | MO_NC)); 3332 } else { 3333 // Use ADRP/ADD or ADRP/LDR for everything else: the small model on ELF and 3334 // the only correct model on Darwin. 3335 SDValue Hi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 3336 OpFlags | AArch64II::MO_PAGE); 3337 unsigned char LoFlags = OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC; 3338 SDValue Lo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, LoFlags); 3339 3340 SDValue ADRP = DAG.getNode(AArch64ISD::ADRP, DL, PtrVT, Hi); 3341 return DAG.getNode(AArch64ISD::ADDlow, DL, PtrVT, ADRP, Lo); 3342 } 3343 } 3344 3345 /// \brief Convert a TLS address reference into the correct sequence of loads 3346 /// and calls to compute the variable's address (for Darwin, currently) and 3347 /// return an SDValue containing the final node. 3348 3349 /// Darwin only has one TLS scheme which must be capable of dealing with the 3350 /// fully general situation, in the worst case. This means: 3351 /// + "extern __thread" declaration. 3352 /// + Defined in a possibly unknown dynamic library. 3353 /// 3354 /// The general system is that each __thread variable has a [3 x i64] descriptor 3355 /// which contains information used by the runtime to calculate the address. The 3356 /// only part of this the compiler needs to know about is the first xword, which 3357 /// contains a function pointer that must be called with the address of the 3358 /// entire descriptor in "x0". 3359 /// 3360 /// Since this descriptor may be in a different unit, in general even the 3361 /// descriptor must be accessed via an indirect load. The "ideal" code sequence 3362 /// is: 3363 /// adrp x0, _var@TLVPPAGE 3364 /// ldr x0, [x0, _var@TLVPPAGEOFF] ; x0 now contains address of descriptor 3365 /// ldr x1, [x0] ; x1 contains 1st entry of descriptor, 3366 /// ; the function pointer 3367 /// blr x1 ; Uses descriptor address in x0 3368 /// ; Address of _var is now in x0. 3369 /// 3370 /// If the address of _var's descriptor *is* known to the linker, then it can 3371 /// change the first "ldr" instruction to an appropriate "add x0, x0, #imm" for 3372 /// a slight efficiency gain. 3373 SDValue 3374 AArch64TargetLowering::LowerDarwinGlobalTLSAddress(SDValue Op, 3375 SelectionDAG &DAG) const { 3376 assert(Subtarget->isTargetDarwin() && "TLS only supported on Darwin"); 3377 3378 SDLoc DL(Op); 3379 MVT PtrVT = getPointerTy(DAG.getDataLayout()); 3380 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 3381 3382 SDValue TLVPAddr = 3383 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_TLS); 3384 SDValue DescAddr = DAG.getNode(AArch64ISD::LOADgot, DL, PtrVT, TLVPAddr); 3385 3386 // The first entry in the descriptor is a function pointer that we must call 3387 // to obtain the address of the variable. 3388 SDValue Chain = DAG.getEntryNode(); 3389 SDValue FuncTLVGet = 3390 DAG.getLoad(MVT::i64, DL, Chain, DescAddr, 3391 MachinePointerInfo::getGOT(DAG.getMachineFunction()), false, 3392 true, true, 8); 3393 Chain = FuncTLVGet.getValue(1); 3394 3395 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 3396 MFI->setAdjustsStack(true); 3397 3398 // TLS calls preserve all registers except those that absolutely must be 3399 // trashed: X0 (it takes an argument), LR (it's a call) and NZCV (let's not be 3400 // silly). 3401 const uint32_t *Mask = 3402 Subtarget->getRegisterInfo()->getTLSCallPreservedMask(); 3403 3404 // Finally, we can make the call. This is just a degenerate version of a 3405 // normal AArch64 call node: x0 takes the address of the descriptor, and 3406 // returns the address of the variable in this thread. 3407 Chain = DAG.getCopyToReg(Chain, DL, AArch64::X0, DescAddr, SDValue()); 3408 Chain = 3409 DAG.getNode(AArch64ISD::CALL, DL, DAG.getVTList(MVT::Other, MVT::Glue), 3410 Chain, FuncTLVGet, DAG.getRegister(AArch64::X0, MVT::i64), 3411 DAG.getRegisterMask(Mask), Chain.getValue(1)); 3412 return DAG.getCopyFromReg(Chain, DL, AArch64::X0, PtrVT, Chain.getValue(1)); 3413 } 3414 3415 /// When accessing thread-local variables under either the general-dynamic or 3416 /// local-dynamic system, we make a "TLS-descriptor" call. The variable will 3417 /// have a descriptor, accessible via a PC-relative ADRP, and whose first entry 3418 /// is a function pointer to carry out the resolution. 3419 /// 3420 /// The sequence is: 3421 /// adrp x0, :tlsdesc:var 3422 /// ldr x1, [x0, #:tlsdesc_lo12:var] 3423 /// add x0, x0, #:tlsdesc_lo12:var 3424 /// .tlsdesccall var 3425 /// blr x1 3426 /// (TPIDR_EL0 offset now in x0) 3427 /// 3428 /// The above sequence must be produced unscheduled, to enable the linker to 3429 /// optimize/relax this sequence. 3430 /// Therefore, a pseudo-instruction (TLSDESC_CALLSEQ) is used to represent the 3431 /// above sequence, and expanded really late in the compilation flow, to ensure 3432 /// the sequence is produced as per above. 3433 SDValue AArch64TargetLowering::LowerELFTLSDescCallSeq(SDValue SymAddr, SDLoc DL, 3434 SelectionDAG &DAG) const { 3435 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3436 3437 SDValue Chain = DAG.getEntryNode(); 3438 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 3439 3440 SmallVector<SDValue, 2> Ops; 3441 Ops.push_back(Chain); 3442 Ops.push_back(SymAddr); 3443 3444 Chain = DAG.getNode(AArch64ISD::TLSDESC_CALLSEQ, DL, NodeTys, Ops); 3445 SDValue Glue = Chain.getValue(1); 3446 3447 return DAG.getCopyFromReg(Chain, DL, AArch64::X0, PtrVT, Glue); 3448 } 3449 3450 SDValue 3451 AArch64TargetLowering::LowerELFGlobalTLSAddress(SDValue Op, 3452 SelectionDAG &DAG) const { 3453 assert(Subtarget->isTargetELF() && "This function expects an ELF target"); 3454 assert(getTargetMachine().getCodeModel() == CodeModel::Small && 3455 "ELF TLS only supported in small memory model"); 3456 // Different choices can be made for the maximum size of the TLS area for a 3457 // module. For the small address model, the default TLS size is 16MiB and the 3458 // maximum TLS size is 4GiB. 3459 // FIXME: add -mtls-size command line option and make it control the 16MiB 3460 // vs. 4GiB code sequence generation. 3461 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 3462 3463 TLSModel::Model Model = getTargetMachine().getTLSModel(GA->getGlobal()); 3464 3465 if (DAG.getTarget().Options.EmulatedTLS) 3466 return LowerToTLSEmulatedModel(GA, DAG); 3467 3468 if (!EnableAArch64ELFLocalDynamicTLSGeneration) { 3469 if (Model == TLSModel::LocalDynamic) 3470 Model = TLSModel::GeneralDynamic; 3471 } 3472 3473 SDValue TPOff; 3474 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3475 SDLoc DL(Op); 3476 const GlobalValue *GV = GA->getGlobal(); 3477 3478 SDValue ThreadBase = DAG.getNode(AArch64ISD::THREAD_POINTER, DL, PtrVT); 3479 3480 if (Model == TLSModel::LocalExec) { 3481 SDValue HiVar = DAG.getTargetGlobalAddress( 3482 GV, DL, PtrVT, 0, AArch64II::MO_TLS | AArch64II::MO_HI12); 3483 SDValue LoVar = DAG.getTargetGlobalAddress( 3484 GV, DL, PtrVT, 0, 3485 AArch64II::MO_TLS | AArch64II::MO_PAGEOFF | AArch64II::MO_NC); 3486 3487 SDValue TPWithOff_lo = 3488 SDValue(DAG.getMachineNode(AArch64::ADDXri, DL, PtrVT, ThreadBase, 3489 HiVar, 3490 DAG.getTargetConstant(0, DL, MVT::i32)), 3491 0); 3492 SDValue TPWithOff = 3493 SDValue(DAG.getMachineNode(AArch64::ADDXri, DL, PtrVT, TPWithOff_lo, 3494 LoVar, 3495 DAG.getTargetConstant(0, DL, MVT::i32)), 3496 0); 3497 return TPWithOff; 3498 } else if (Model == TLSModel::InitialExec) { 3499 TPOff = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_TLS); 3500 TPOff = DAG.getNode(AArch64ISD::LOADgot, DL, PtrVT, TPOff); 3501 } else if (Model == TLSModel::LocalDynamic) { 3502 // Local-dynamic accesses proceed in two phases. A general-dynamic TLS 3503 // descriptor call against the special symbol _TLS_MODULE_BASE_ to calculate 3504 // the beginning of the module's TLS region, followed by a DTPREL offset 3505 // calculation. 3506 3507 // These accesses will need deduplicating if there's more than one. 3508 AArch64FunctionInfo *MFI = 3509 DAG.getMachineFunction().getInfo<AArch64FunctionInfo>(); 3510 MFI->incNumLocalDynamicTLSAccesses(); 3511 3512 // The call needs a relocation too for linker relaxation. It doesn't make 3513 // sense to call it MO_PAGE or MO_PAGEOFF though so we need another copy of 3514 // the address. 3515 SDValue SymAddr = DAG.getTargetExternalSymbol("_TLS_MODULE_BASE_", PtrVT, 3516 AArch64II::MO_TLS); 3517 3518 // Now we can calculate the offset from TPIDR_EL0 to this module's 3519 // thread-local area. 3520 TPOff = LowerELFTLSDescCallSeq(SymAddr, DL, DAG); 3521 3522 // Now use :dtprel_whatever: operations to calculate this variable's offset 3523 // in its thread-storage area. 3524 SDValue HiVar = DAG.getTargetGlobalAddress( 3525 GV, DL, MVT::i64, 0, AArch64II::MO_TLS | AArch64II::MO_HI12); 3526 SDValue LoVar = DAG.getTargetGlobalAddress( 3527 GV, DL, MVT::i64, 0, 3528 AArch64II::MO_TLS | AArch64II::MO_PAGEOFF | AArch64II::MO_NC); 3529 3530 TPOff = SDValue(DAG.getMachineNode(AArch64::ADDXri, DL, PtrVT, TPOff, HiVar, 3531 DAG.getTargetConstant(0, DL, MVT::i32)), 3532 0); 3533 TPOff = SDValue(DAG.getMachineNode(AArch64::ADDXri, DL, PtrVT, TPOff, LoVar, 3534 DAG.getTargetConstant(0, DL, MVT::i32)), 3535 0); 3536 } else if (Model == TLSModel::GeneralDynamic) { 3537 // The call needs a relocation too for linker relaxation. It doesn't make 3538 // sense to call it MO_PAGE or MO_PAGEOFF though so we need another copy of 3539 // the address. 3540 SDValue SymAddr = 3541 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_TLS); 3542 3543 // Finally we can make a call to calculate the offset from tpidr_el0. 3544 TPOff = LowerELFTLSDescCallSeq(SymAddr, DL, DAG); 3545 } else 3546 llvm_unreachable("Unsupported ELF TLS access model"); 3547 3548 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadBase, TPOff); 3549 } 3550 3551 SDValue AArch64TargetLowering::LowerGlobalTLSAddress(SDValue Op, 3552 SelectionDAG &DAG) const { 3553 if (Subtarget->isTargetDarwin()) 3554 return LowerDarwinGlobalTLSAddress(Op, DAG); 3555 else if (Subtarget->isTargetELF()) 3556 return LowerELFGlobalTLSAddress(Op, DAG); 3557 3558 llvm_unreachable("Unexpected platform trying to use TLS"); 3559 } 3560 SDValue AArch64TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 3561 SDValue Chain = Op.getOperand(0); 3562 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 3563 SDValue LHS = Op.getOperand(2); 3564 SDValue RHS = Op.getOperand(3); 3565 SDValue Dest = Op.getOperand(4); 3566 SDLoc dl(Op); 3567 3568 // Handle f128 first, since lowering it will result in comparing the return 3569 // value of a libcall against zero, which is just what the rest of LowerBR_CC 3570 // is expecting to deal with. 3571 if (LHS.getValueType() == MVT::f128) { 3572 softenSetCCOperands(DAG, MVT::f128, LHS, RHS, CC, dl); 3573 3574 // If softenSetCCOperands returned a scalar, we need to compare the result 3575 // against zero to select between true and false values. 3576 if (!RHS.getNode()) { 3577 RHS = DAG.getConstant(0, dl, LHS.getValueType()); 3578 CC = ISD::SETNE; 3579 } 3580 } 3581 3582 // Optimize {s|u}{add|sub|mul}.with.overflow feeding into a branch 3583 // instruction. 3584 unsigned Opc = LHS.getOpcode(); 3585 if (LHS.getResNo() == 1 && isa<ConstantSDNode>(RHS) && 3586 cast<ConstantSDNode>(RHS)->isOne() && 3587 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO || 3588 Opc == ISD::USUBO || Opc == ISD::SMULO || Opc == ISD::UMULO)) { 3589 assert((CC == ISD::SETEQ || CC == ISD::SETNE) && 3590 "Unexpected condition code."); 3591 // Only lower legal XALUO ops. 3592 if (!DAG.getTargetLoweringInfo().isTypeLegal(LHS->getValueType(0))) 3593 return SDValue(); 3594 3595 // The actual operation with overflow check. 3596 AArch64CC::CondCode OFCC; 3597 SDValue Value, Overflow; 3598 std::tie(Value, Overflow) = getAArch64XALUOOp(OFCC, LHS.getValue(0), DAG); 3599 3600 if (CC == ISD::SETNE) 3601 OFCC = getInvertedCondCode(OFCC); 3602 SDValue CCVal = DAG.getConstant(OFCC, dl, MVT::i32); 3603 3604 return DAG.getNode(AArch64ISD::BRCOND, dl, MVT::Other, Chain, Dest, CCVal, 3605 Overflow); 3606 } 3607 3608 if (LHS.getValueType().isInteger()) { 3609 assert((LHS.getValueType() == RHS.getValueType()) && 3610 (LHS.getValueType() == MVT::i32 || LHS.getValueType() == MVT::i64)); 3611 3612 // If the RHS of the comparison is zero, we can potentially fold this 3613 // to a specialized branch. 3614 const ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS); 3615 if (RHSC && RHSC->getZExtValue() == 0) { 3616 if (CC == ISD::SETEQ) { 3617 // See if we can use a TBZ to fold in an AND as well. 3618 // TBZ has a smaller branch displacement than CBZ. If the offset is 3619 // out of bounds, a late MI-layer pass rewrites branches. 3620 // 403.gcc is an example that hits this case. 3621 if (LHS.getOpcode() == ISD::AND && 3622 isa<ConstantSDNode>(LHS.getOperand(1)) && 3623 isPowerOf2_64(LHS.getConstantOperandVal(1))) { 3624 SDValue Test = LHS.getOperand(0); 3625 uint64_t Mask = LHS.getConstantOperandVal(1); 3626 return DAG.getNode(AArch64ISD::TBZ, dl, MVT::Other, Chain, Test, 3627 DAG.getConstant(Log2_64(Mask), dl, MVT::i64), 3628 Dest); 3629 } 3630 3631 return DAG.getNode(AArch64ISD::CBZ, dl, MVT::Other, Chain, LHS, Dest); 3632 } else if (CC == ISD::SETNE) { 3633 // See if we can use a TBZ to fold in an AND as well. 3634 // TBZ has a smaller branch displacement than CBZ. If the offset is 3635 // out of bounds, a late MI-layer pass rewrites branches. 3636 // 403.gcc is an example that hits this case. 3637 if (LHS.getOpcode() == ISD::AND && 3638 isa<ConstantSDNode>(LHS.getOperand(1)) && 3639 isPowerOf2_64(LHS.getConstantOperandVal(1))) { 3640 SDValue Test = LHS.getOperand(0); 3641 uint64_t Mask = LHS.getConstantOperandVal(1); 3642 return DAG.getNode(AArch64ISD::TBNZ, dl, MVT::Other, Chain, Test, 3643 DAG.getConstant(Log2_64(Mask), dl, MVT::i64), 3644 Dest); 3645 } 3646 3647 return DAG.getNode(AArch64ISD::CBNZ, dl, MVT::Other, Chain, LHS, Dest); 3648 } else if (CC == ISD::SETLT && LHS.getOpcode() != ISD::AND) { 3649 // Don't combine AND since emitComparison converts the AND to an ANDS 3650 // (a.k.a. TST) and the test in the test bit and branch instruction 3651 // becomes redundant. This would also increase register pressure. 3652 uint64_t Mask = LHS.getValueType().getSizeInBits() - 1; 3653 return DAG.getNode(AArch64ISD::TBNZ, dl, MVT::Other, Chain, LHS, 3654 DAG.getConstant(Mask, dl, MVT::i64), Dest); 3655 } 3656 } 3657 if (RHSC && RHSC->getSExtValue() == -1 && CC == ISD::SETGT && 3658 LHS.getOpcode() != ISD::AND) { 3659 // Don't combine AND since emitComparison converts the AND to an ANDS 3660 // (a.k.a. TST) and the test in the test bit and branch instruction 3661 // becomes redundant. This would also increase register pressure. 3662 uint64_t Mask = LHS.getValueType().getSizeInBits() - 1; 3663 return DAG.getNode(AArch64ISD::TBZ, dl, MVT::Other, Chain, LHS, 3664 DAG.getConstant(Mask, dl, MVT::i64), Dest); 3665 } 3666 3667 SDValue CCVal; 3668 SDValue Cmp = getAArch64Cmp(LHS, RHS, CC, CCVal, DAG, dl); 3669 return DAG.getNode(AArch64ISD::BRCOND, dl, MVT::Other, Chain, Dest, CCVal, 3670 Cmp); 3671 } 3672 3673 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64); 3674 3675 // Unfortunately, the mapping of LLVM FP CC's onto AArch64 CC's isn't totally 3676 // clean. Some of them require two branches to implement. 3677 SDValue Cmp = emitComparison(LHS, RHS, CC, dl, DAG); 3678 AArch64CC::CondCode CC1, CC2; 3679 changeFPCCToAArch64CC(CC, CC1, CC2); 3680 SDValue CC1Val = DAG.getConstant(CC1, dl, MVT::i32); 3681 SDValue BR1 = 3682 DAG.getNode(AArch64ISD::BRCOND, dl, MVT::Other, Chain, Dest, CC1Val, Cmp); 3683 if (CC2 != AArch64CC::AL) { 3684 SDValue CC2Val = DAG.getConstant(CC2, dl, MVT::i32); 3685 return DAG.getNode(AArch64ISD::BRCOND, dl, MVT::Other, BR1, Dest, CC2Val, 3686 Cmp); 3687 } 3688 3689 return BR1; 3690 } 3691 3692 SDValue AArch64TargetLowering::LowerFCOPYSIGN(SDValue Op, 3693 SelectionDAG &DAG) const { 3694 EVT VT = Op.getValueType(); 3695 SDLoc DL(Op); 3696 3697 SDValue In1 = Op.getOperand(0); 3698 SDValue In2 = Op.getOperand(1); 3699 EVT SrcVT = In2.getValueType(); 3700 3701 if (SrcVT.bitsLT(VT)) 3702 In2 = DAG.getNode(ISD::FP_EXTEND, DL, VT, In2); 3703 else if (SrcVT.bitsGT(VT)) 3704 In2 = DAG.getNode(ISD::FP_ROUND, DL, VT, In2, DAG.getIntPtrConstant(0, DL)); 3705 3706 EVT VecVT; 3707 EVT EltVT; 3708 uint64_t EltMask; 3709 SDValue VecVal1, VecVal2; 3710 if (VT == MVT::f32 || VT == MVT::v2f32 || VT == MVT::v4f32) { 3711 EltVT = MVT::i32; 3712 VecVT = (VT == MVT::v2f32 ? MVT::v2i32 : MVT::v4i32); 3713 EltMask = 0x80000000ULL; 3714 3715 if (!VT.isVector()) { 3716 VecVal1 = DAG.getTargetInsertSubreg(AArch64::ssub, DL, VecVT, 3717 DAG.getUNDEF(VecVT), In1); 3718 VecVal2 = DAG.getTargetInsertSubreg(AArch64::ssub, DL, VecVT, 3719 DAG.getUNDEF(VecVT), In2); 3720 } else { 3721 VecVal1 = DAG.getNode(ISD::BITCAST, DL, VecVT, In1); 3722 VecVal2 = DAG.getNode(ISD::BITCAST, DL, VecVT, In2); 3723 } 3724 } else if (VT == MVT::f64 || VT == MVT::v2f64) { 3725 EltVT = MVT::i64; 3726 VecVT = MVT::v2i64; 3727 3728 // We want to materialize a mask with the high bit set, but the AdvSIMD 3729 // immediate moves cannot materialize that in a single instruction for 3730 // 64-bit elements. Instead, materialize zero and then negate it. 3731 EltMask = 0; 3732 3733 if (!VT.isVector()) { 3734 VecVal1 = DAG.getTargetInsertSubreg(AArch64::dsub, DL, VecVT, 3735 DAG.getUNDEF(VecVT), In1); 3736 VecVal2 = DAG.getTargetInsertSubreg(AArch64::dsub, DL, VecVT, 3737 DAG.getUNDEF(VecVT), In2); 3738 } else { 3739 VecVal1 = DAG.getNode(ISD::BITCAST, DL, VecVT, In1); 3740 VecVal2 = DAG.getNode(ISD::BITCAST, DL, VecVT, In2); 3741 } 3742 } else { 3743 llvm_unreachable("Invalid type for copysign!"); 3744 } 3745 3746 SDValue BuildVec = DAG.getConstant(EltMask, DL, VecVT); 3747 3748 // If we couldn't materialize the mask above, then the mask vector will be 3749 // the zero vector, and we need to negate it here. 3750 if (VT == MVT::f64 || VT == MVT::v2f64) { 3751 BuildVec = DAG.getNode(ISD::BITCAST, DL, MVT::v2f64, BuildVec); 3752 BuildVec = DAG.getNode(ISD::FNEG, DL, MVT::v2f64, BuildVec); 3753 BuildVec = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, BuildVec); 3754 } 3755 3756 SDValue Sel = 3757 DAG.getNode(AArch64ISD::BIT, DL, VecVT, VecVal1, VecVal2, BuildVec); 3758 3759 if (VT == MVT::f32) 3760 return DAG.getTargetExtractSubreg(AArch64::ssub, DL, VT, Sel); 3761 else if (VT == MVT::f64) 3762 return DAG.getTargetExtractSubreg(AArch64::dsub, DL, VT, Sel); 3763 else 3764 return DAG.getNode(ISD::BITCAST, DL, VT, Sel); 3765 } 3766 3767 SDValue AArch64TargetLowering::LowerCTPOP(SDValue Op, SelectionDAG &DAG) const { 3768 if (DAG.getMachineFunction().getFunction()->hasFnAttribute( 3769 Attribute::NoImplicitFloat)) 3770 return SDValue(); 3771 3772 if (!Subtarget->hasNEON()) 3773 return SDValue(); 3774 3775 // While there is no integer popcount instruction, it can 3776 // be more efficiently lowered to the following sequence that uses 3777 // AdvSIMD registers/instructions as long as the copies to/from 3778 // the AdvSIMD registers are cheap. 3779 // FMOV D0, X0 // copy 64-bit int to vector, high bits zero'd 3780 // CNT V0.8B, V0.8B // 8xbyte pop-counts 3781 // ADDV B0, V0.8B // sum 8xbyte pop-counts 3782 // UMOV X0, V0.B[0] // copy byte result back to integer reg 3783 SDValue Val = Op.getOperand(0); 3784 SDLoc DL(Op); 3785 EVT VT = Op.getValueType(); 3786 3787 if (VT == MVT::i32) 3788 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Val); 3789 Val = DAG.getNode(ISD::BITCAST, DL, MVT::v8i8, Val); 3790 3791 SDValue CtPop = DAG.getNode(ISD::CTPOP, DL, MVT::v8i8, Val); 3792 SDValue UaddLV = DAG.getNode( 3793 ISD::INTRINSIC_WO_CHAIN, DL, MVT::i32, 3794 DAG.getConstant(Intrinsic::aarch64_neon_uaddlv, DL, MVT::i32), CtPop); 3795 3796 if (VT == MVT::i64) 3797 UaddLV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, UaddLV); 3798 return UaddLV; 3799 } 3800 3801 SDValue AArch64TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const { 3802 3803 if (Op.getValueType().isVector()) 3804 return LowerVSETCC(Op, DAG); 3805 3806 SDValue LHS = Op.getOperand(0); 3807 SDValue RHS = Op.getOperand(1); 3808 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 3809 SDLoc dl(Op); 3810 3811 // We chose ZeroOrOneBooleanContents, so use zero and one. 3812 EVT VT = Op.getValueType(); 3813 SDValue TVal = DAG.getConstant(1, dl, VT); 3814 SDValue FVal = DAG.getConstant(0, dl, VT); 3815 3816 // Handle f128 first, since one possible outcome is a normal integer 3817 // comparison which gets picked up by the next if statement. 3818 if (LHS.getValueType() == MVT::f128) { 3819 softenSetCCOperands(DAG, MVT::f128, LHS, RHS, CC, dl); 3820 3821 // If softenSetCCOperands returned a scalar, use it. 3822 if (!RHS.getNode()) { 3823 assert(LHS.getValueType() == Op.getValueType() && 3824 "Unexpected setcc expansion!"); 3825 return LHS; 3826 } 3827 } 3828 3829 if (LHS.getValueType().isInteger()) { 3830 SDValue CCVal; 3831 SDValue Cmp = 3832 getAArch64Cmp(LHS, RHS, ISD::getSetCCInverse(CC, true), CCVal, DAG, dl); 3833 3834 // Note that we inverted the condition above, so we reverse the order of 3835 // the true and false operands here. This will allow the setcc to be 3836 // matched to a single CSINC instruction. 3837 return DAG.getNode(AArch64ISD::CSEL, dl, VT, FVal, TVal, CCVal, Cmp); 3838 } 3839 3840 // Now we know we're dealing with FP values. 3841 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64); 3842 3843 // If that fails, we'll need to perform an FCMP + CSEL sequence. Go ahead 3844 // and do the comparison. 3845 SDValue Cmp = emitComparison(LHS, RHS, CC, dl, DAG); 3846 3847 AArch64CC::CondCode CC1, CC2; 3848 changeFPCCToAArch64CC(CC, CC1, CC2); 3849 if (CC2 == AArch64CC::AL) { 3850 changeFPCCToAArch64CC(ISD::getSetCCInverse(CC, false), CC1, CC2); 3851 SDValue CC1Val = DAG.getConstant(CC1, dl, MVT::i32); 3852 3853 // Note that we inverted the condition above, so we reverse the order of 3854 // the true and false operands here. This will allow the setcc to be 3855 // matched to a single CSINC instruction. 3856 return DAG.getNode(AArch64ISD::CSEL, dl, VT, FVal, TVal, CC1Val, Cmp); 3857 } else { 3858 // Unfortunately, the mapping of LLVM FP CC's onto AArch64 CC's isn't 3859 // totally clean. Some of them require two CSELs to implement. As is in 3860 // this case, we emit the first CSEL and then emit a second using the output 3861 // of the first as the RHS. We're effectively OR'ing the two CC's together. 3862 3863 // FIXME: It would be nice if we could match the two CSELs to two CSINCs. 3864 SDValue CC1Val = DAG.getConstant(CC1, dl, MVT::i32); 3865 SDValue CS1 = 3866 DAG.getNode(AArch64ISD::CSEL, dl, VT, TVal, FVal, CC1Val, Cmp); 3867 3868 SDValue CC2Val = DAG.getConstant(CC2, dl, MVT::i32); 3869 return DAG.getNode(AArch64ISD::CSEL, dl, VT, TVal, CS1, CC2Val, Cmp); 3870 } 3871 } 3872 3873 SDValue AArch64TargetLowering::LowerSELECT_CC(ISD::CondCode CC, SDValue LHS, 3874 SDValue RHS, SDValue TVal, 3875 SDValue FVal, SDLoc dl, 3876 SelectionDAG &DAG) const { 3877 // Handle f128 first, because it will result in a comparison of some RTLIB 3878 // call result against zero. 3879 if (LHS.getValueType() == MVT::f128) { 3880 softenSetCCOperands(DAG, MVT::f128, LHS, RHS, CC, dl); 3881 3882 // If softenSetCCOperands returned a scalar, we need to compare the result 3883 // against zero to select between true and false values. 3884 if (!RHS.getNode()) { 3885 RHS = DAG.getConstant(0, dl, LHS.getValueType()); 3886 CC = ISD::SETNE; 3887 } 3888 } 3889 3890 // Also handle f16, for which we need to do a f32 comparison. 3891 if (LHS.getValueType() == MVT::f16) { 3892 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f32, LHS); 3893 RHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f32, RHS); 3894 } 3895 3896 // Next, handle integers. 3897 if (LHS.getValueType().isInteger()) { 3898 assert((LHS.getValueType() == RHS.getValueType()) && 3899 (LHS.getValueType() == MVT::i32 || LHS.getValueType() == MVT::i64)); 3900 3901 unsigned Opcode = AArch64ISD::CSEL; 3902 3903 // If both the TVal and the FVal are constants, see if we can swap them in 3904 // order to for a CSINV or CSINC out of them. 3905 ConstantSDNode *CFVal = dyn_cast<ConstantSDNode>(FVal); 3906 ConstantSDNode *CTVal = dyn_cast<ConstantSDNode>(TVal); 3907 3908 if (CTVal && CFVal && CTVal->isAllOnesValue() && CFVal->isNullValue()) { 3909 std::swap(TVal, FVal); 3910 std::swap(CTVal, CFVal); 3911 CC = ISD::getSetCCInverse(CC, true); 3912 } else if (CTVal && CFVal && CTVal->isOne() && CFVal->isNullValue()) { 3913 std::swap(TVal, FVal); 3914 std::swap(CTVal, CFVal); 3915 CC = ISD::getSetCCInverse(CC, true); 3916 } else if (TVal.getOpcode() == ISD::XOR) { 3917 // If TVal is a NOT we want to swap TVal and FVal so that we can match 3918 // with a CSINV rather than a CSEL. 3919 ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(TVal.getOperand(1)); 3920 3921 if (CVal && CVal->isAllOnesValue()) { 3922 std::swap(TVal, FVal); 3923 std::swap(CTVal, CFVal); 3924 CC = ISD::getSetCCInverse(CC, true); 3925 } 3926 } else if (TVal.getOpcode() == ISD::SUB) { 3927 // If TVal is a negation (SUB from 0) we want to swap TVal and FVal so 3928 // that we can match with a CSNEG rather than a CSEL. 3929 ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(TVal.getOperand(0)); 3930 3931 if (CVal && CVal->isNullValue()) { 3932 std::swap(TVal, FVal); 3933 std::swap(CTVal, CFVal); 3934 CC = ISD::getSetCCInverse(CC, true); 3935 } 3936 } else if (CTVal && CFVal) { 3937 const int64_t TrueVal = CTVal->getSExtValue(); 3938 const int64_t FalseVal = CFVal->getSExtValue(); 3939 bool Swap = false; 3940 3941 // If both TVal and FVal are constants, see if FVal is the 3942 // inverse/negation/increment of TVal and generate a CSINV/CSNEG/CSINC 3943 // instead of a CSEL in that case. 3944 if (TrueVal == ~FalseVal) { 3945 Opcode = AArch64ISD::CSINV; 3946 } else if (TrueVal == -FalseVal) { 3947 Opcode = AArch64ISD::CSNEG; 3948 } else if (TVal.getValueType() == MVT::i32) { 3949 // If our operands are only 32-bit wide, make sure we use 32-bit 3950 // arithmetic for the check whether we can use CSINC. This ensures that 3951 // the addition in the check will wrap around properly in case there is 3952 // an overflow (which would not be the case if we do the check with 3953 // 64-bit arithmetic). 3954 const uint32_t TrueVal32 = CTVal->getZExtValue(); 3955 const uint32_t FalseVal32 = CFVal->getZExtValue(); 3956 3957 if ((TrueVal32 == FalseVal32 + 1) || (TrueVal32 + 1 == FalseVal32)) { 3958 Opcode = AArch64ISD::CSINC; 3959 3960 if (TrueVal32 > FalseVal32) { 3961 Swap = true; 3962 } 3963 } 3964 // 64-bit check whether we can use CSINC. 3965 } else if ((TrueVal == FalseVal + 1) || (TrueVal + 1 == FalseVal)) { 3966 Opcode = AArch64ISD::CSINC; 3967 3968 if (TrueVal > FalseVal) { 3969 Swap = true; 3970 } 3971 } 3972 3973 // Swap TVal and FVal if necessary. 3974 if (Swap) { 3975 std::swap(TVal, FVal); 3976 std::swap(CTVal, CFVal); 3977 CC = ISD::getSetCCInverse(CC, true); 3978 } 3979 3980 if (Opcode != AArch64ISD::CSEL) { 3981 // Drop FVal since we can get its value by simply inverting/negating 3982 // TVal. 3983 FVal = TVal; 3984 } 3985 } 3986 3987 SDValue CCVal; 3988 SDValue Cmp = getAArch64Cmp(LHS, RHS, CC, CCVal, DAG, dl); 3989 3990 EVT VT = TVal.getValueType(); 3991 return DAG.getNode(Opcode, dl, VT, TVal, FVal, CCVal, Cmp); 3992 } 3993 3994 // Now we know we're dealing with FP values. 3995 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64); 3996 assert(LHS.getValueType() == RHS.getValueType()); 3997 EVT VT = TVal.getValueType(); 3998 SDValue Cmp = emitComparison(LHS, RHS, CC, dl, DAG); 3999 4000 // Unfortunately, the mapping of LLVM FP CC's onto AArch64 CC's isn't totally 4001 // clean. Some of them require two CSELs to implement. 4002 AArch64CC::CondCode CC1, CC2; 4003 changeFPCCToAArch64CC(CC, CC1, CC2); 4004 SDValue CC1Val = DAG.getConstant(CC1, dl, MVT::i32); 4005 SDValue CS1 = DAG.getNode(AArch64ISD::CSEL, dl, VT, TVal, FVal, CC1Val, Cmp); 4006 4007 // If we need a second CSEL, emit it, using the output of the first as the 4008 // RHS. We're effectively OR'ing the two CC's together. 4009 if (CC2 != AArch64CC::AL) { 4010 SDValue CC2Val = DAG.getConstant(CC2, dl, MVT::i32); 4011 return DAG.getNode(AArch64ISD::CSEL, dl, VT, TVal, CS1, CC2Val, Cmp); 4012 } 4013 4014 // Otherwise, return the output of the first CSEL. 4015 return CS1; 4016 } 4017 4018 SDValue AArch64TargetLowering::LowerSELECT_CC(SDValue Op, 4019 SelectionDAG &DAG) const { 4020 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 4021 SDValue LHS = Op.getOperand(0); 4022 SDValue RHS = Op.getOperand(1); 4023 SDValue TVal = Op.getOperand(2); 4024 SDValue FVal = Op.getOperand(3); 4025 SDLoc DL(Op); 4026 return LowerSELECT_CC(CC, LHS, RHS, TVal, FVal, DL, DAG); 4027 } 4028 4029 SDValue AArch64TargetLowering::LowerSELECT(SDValue Op, 4030 SelectionDAG &DAG) const { 4031 SDValue CCVal = Op->getOperand(0); 4032 SDValue TVal = Op->getOperand(1); 4033 SDValue FVal = Op->getOperand(2); 4034 SDLoc DL(Op); 4035 4036 unsigned Opc = CCVal.getOpcode(); 4037 // Optimize {s|u}{add|sub|mul}.with.overflow feeding into a select 4038 // instruction. 4039 if (CCVal.getResNo() == 1 && 4040 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO || 4041 Opc == ISD::USUBO || Opc == ISD::SMULO || Opc == ISD::UMULO)) { 4042 // Only lower legal XALUO ops. 4043 if (!DAG.getTargetLoweringInfo().isTypeLegal(CCVal->getValueType(0))) 4044 return SDValue(); 4045 4046 AArch64CC::CondCode OFCC; 4047 SDValue Value, Overflow; 4048 std::tie(Value, Overflow) = getAArch64XALUOOp(OFCC, CCVal.getValue(0), DAG); 4049 SDValue CCVal = DAG.getConstant(OFCC, DL, MVT::i32); 4050 4051 return DAG.getNode(AArch64ISD::CSEL, DL, Op.getValueType(), TVal, FVal, 4052 CCVal, Overflow); 4053 } 4054 4055 // Lower it the same way as we would lower a SELECT_CC node. 4056 ISD::CondCode CC; 4057 SDValue LHS, RHS; 4058 if (CCVal.getOpcode() == ISD::SETCC) { 4059 LHS = CCVal.getOperand(0); 4060 RHS = CCVal.getOperand(1); 4061 CC = cast<CondCodeSDNode>(CCVal->getOperand(2))->get(); 4062 } else { 4063 LHS = CCVal; 4064 RHS = DAG.getConstant(0, DL, CCVal.getValueType()); 4065 CC = ISD::SETNE; 4066 } 4067 return LowerSELECT_CC(CC, LHS, RHS, TVal, FVal, DL, DAG); 4068 } 4069 4070 SDValue AArch64TargetLowering::LowerJumpTable(SDValue Op, 4071 SelectionDAG &DAG) const { 4072 // Jump table entries as PC relative offsets. No additional tweaking 4073 // is necessary here. Just get the address of the jump table. 4074 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); 4075 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 4076 SDLoc DL(Op); 4077 4078 if (getTargetMachine().getCodeModel() == CodeModel::Large && 4079 !Subtarget->isTargetMachO()) { 4080 const unsigned char MO_NC = AArch64II::MO_NC; 4081 return DAG.getNode( 4082 AArch64ISD::WrapperLarge, DL, PtrVT, 4083 DAG.getTargetJumpTable(JT->getIndex(), PtrVT, AArch64II::MO_G3), 4084 DAG.getTargetJumpTable(JT->getIndex(), PtrVT, AArch64II::MO_G2 | MO_NC), 4085 DAG.getTargetJumpTable(JT->getIndex(), PtrVT, AArch64II::MO_G1 | MO_NC), 4086 DAG.getTargetJumpTable(JT->getIndex(), PtrVT, 4087 AArch64II::MO_G0 | MO_NC)); 4088 } 4089 4090 SDValue Hi = 4091 DAG.getTargetJumpTable(JT->getIndex(), PtrVT, AArch64II::MO_PAGE); 4092 SDValue Lo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, 4093 AArch64II::MO_PAGEOFF | AArch64II::MO_NC); 4094 SDValue ADRP = DAG.getNode(AArch64ISD::ADRP, DL, PtrVT, Hi); 4095 return DAG.getNode(AArch64ISD::ADDlow, DL, PtrVT, ADRP, Lo); 4096 } 4097 4098 SDValue AArch64TargetLowering::LowerConstantPool(SDValue Op, 4099 SelectionDAG &DAG) const { 4100 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 4101 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 4102 SDLoc DL(Op); 4103 4104 if (getTargetMachine().getCodeModel() == CodeModel::Large) { 4105 // Use the GOT for the large code model on iOS. 4106 if (Subtarget->isTargetMachO()) { 4107 SDValue GotAddr = DAG.getTargetConstantPool( 4108 CP->getConstVal(), PtrVT, CP->getAlignment(), CP->getOffset(), 4109 AArch64II::MO_GOT); 4110 return DAG.getNode(AArch64ISD::LOADgot, DL, PtrVT, GotAddr); 4111 } 4112 4113 const unsigned char MO_NC = AArch64II::MO_NC; 4114 return DAG.getNode( 4115 AArch64ISD::WrapperLarge, DL, PtrVT, 4116 DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, CP->getAlignment(), 4117 CP->getOffset(), AArch64II::MO_G3), 4118 DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, CP->getAlignment(), 4119 CP->getOffset(), AArch64II::MO_G2 | MO_NC), 4120 DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, CP->getAlignment(), 4121 CP->getOffset(), AArch64II::MO_G1 | MO_NC), 4122 DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, CP->getAlignment(), 4123 CP->getOffset(), AArch64II::MO_G0 | MO_NC)); 4124 } else { 4125 // Use ADRP/ADD or ADRP/LDR for everything else: the small memory model on 4126 // ELF, the only valid one on Darwin. 4127 SDValue Hi = 4128 DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, CP->getAlignment(), 4129 CP->getOffset(), AArch64II::MO_PAGE); 4130 SDValue Lo = DAG.getTargetConstantPool( 4131 CP->getConstVal(), PtrVT, CP->getAlignment(), CP->getOffset(), 4132 AArch64II::MO_PAGEOFF | AArch64II::MO_NC); 4133 4134 SDValue ADRP = DAG.getNode(AArch64ISD::ADRP, DL, PtrVT, Hi); 4135 return DAG.getNode(AArch64ISD::ADDlow, DL, PtrVT, ADRP, Lo); 4136 } 4137 } 4138 4139 SDValue AArch64TargetLowering::LowerBlockAddress(SDValue Op, 4140 SelectionDAG &DAG) const { 4141 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); 4142 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 4143 SDLoc DL(Op); 4144 if (getTargetMachine().getCodeModel() == CodeModel::Large && 4145 !Subtarget->isTargetMachO()) { 4146 const unsigned char MO_NC = AArch64II::MO_NC; 4147 return DAG.getNode( 4148 AArch64ISD::WrapperLarge, DL, PtrVT, 4149 DAG.getTargetBlockAddress(BA, PtrVT, 0, AArch64II::MO_G3), 4150 DAG.getTargetBlockAddress(BA, PtrVT, 0, AArch64II::MO_G2 | MO_NC), 4151 DAG.getTargetBlockAddress(BA, PtrVT, 0, AArch64II::MO_G1 | MO_NC), 4152 DAG.getTargetBlockAddress(BA, PtrVT, 0, AArch64II::MO_G0 | MO_NC)); 4153 } else { 4154 SDValue Hi = DAG.getTargetBlockAddress(BA, PtrVT, 0, AArch64II::MO_PAGE); 4155 SDValue Lo = DAG.getTargetBlockAddress(BA, PtrVT, 0, AArch64II::MO_PAGEOFF | 4156 AArch64II::MO_NC); 4157 SDValue ADRP = DAG.getNode(AArch64ISD::ADRP, DL, PtrVT, Hi); 4158 return DAG.getNode(AArch64ISD::ADDlow, DL, PtrVT, ADRP, Lo); 4159 } 4160 } 4161 4162 SDValue AArch64TargetLowering::LowerDarwin_VASTART(SDValue Op, 4163 SelectionDAG &DAG) const { 4164 AArch64FunctionInfo *FuncInfo = 4165 DAG.getMachineFunction().getInfo<AArch64FunctionInfo>(); 4166 4167 SDLoc DL(Op); 4168 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsStackIndex(), 4169 getPointerTy(DAG.getDataLayout())); 4170 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 4171 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1), 4172 MachinePointerInfo(SV), false, false, 0); 4173 } 4174 4175 SDValue AArch64TargetLowering::LowerAAPCS_VASTART(SDValue Op, 4176 SelectionDAG &DAG) const { 4177 // The layout of the va_list struct is specified in the AArch64 Procedure Call 4178 // Standard, section B.3. 4179 MachineFunction &MF = DAG.getMachineFunction(); 4180 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>(); 4181 auto PtrVT = getPointerTy(DAG.getDataLayout()); 4182 SDLoc DL(Op); 4183 4184 SDValue Chain = Op.getOperand(0); 4185 SDValue VAList = Op.getOperand(1); 4186 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 4187 SmallVector<SDValue, 4> MemOps; 4188 4189 // void *__stack at offset 0 4190 SDValue Stack = DAG.getFrameIndex(FuncInfo->getVarArgsStackIndex(), PtrVT); 4191 MemOps.push_back(DAG.getStore(Chain, DL, Stack, VAList, 4192 MachinePointerInfo(SV), false, false, 8)); 4193 4194 // void *__gr_top at offset 8 4195 int GPRSize = FuncInfo->getVarArgsGPRSize(); 4196 if (GPRSize > 0) { 4197 SDValue GRTop, GRTopAddr; 4198 4199 GRTopAddr = 4200 DAG.getNode(ISD::ADD, DL, PtrVT, VAList, DAG.getConstant(8, DL, PtrVT)); 4201 4202 GRTop = DAG.getFrameIndex(FuncInfo->getVarArgsGPRIndex(), PtrVT); 4203 GRTop = DAG.getNode(ISD::ADD, DL, PtrVT, GRTop, 4204 DAG.getConstant(GPRSize, DL, PtrVT)); 4205 4206 MemOps.push_back(DAG.getStore(Chain, DL, GRTop, GRTopAddr, 4207 MachinePointerInfo(SV, 8), false, false, 8)); 4208 } 4209 4210 // void *__vr_top at offset 16 4211 int FPRSize = FuncInfo->getVarArgsFPRSize(); 4212 if (FPRSize > 0) { 4213 SDValue VRTop, VRTopAddr; 4214 VRTopAddr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList, 4215 DAG.getConstant(16, DL, PtrVT)); 4216 4217 VRTop = DAG.getFrameIndex(FuncInfo->getVarArgsFPRIndex(), PtrVT); 4218 VRTop = DAG.getNode(ISD::ADD, DL, PtrVT, VRTop, 4219 DAG.getConstant(FPRSize, DL, PtrVT)); 4220 4221 MemOps.push_back(DAG.getStore(Chain, DL, VRTop, VRTopAddr, 4222 MachinePointerInfo(SV, 16), false, false, 8)); 4223 } 4224 4225 // int __gr_offs at offset 24 4226 SDValue GROffsAddr = 4227 DAG.getNode(ISD::ADD, DL, PtrVT, VAList, DAG.getConstant(24, DL, PtrVT)); 4228 MemOps.push_back(DAG.getStore(Chain, DL, 4229 DAG.getConstant(-GPRSize, DL, MVT::i32), 4230 GROffsAddr, MachinePointerInfo(SV, 24), false, 4231 false, 4)); 4232 4233 // int __vr_offs at offset 28 4234 SDValue VROffsAddr = 4235 DAG.getNode(ISD::ADD, DL, PtrVT, VAList, DAG.getConstant(28, DL, PtrVT)); 4236 MemOps.push_back(DAG.getStore(Chain, DL, 4237 DAG.getConstant(-FPRSize, DL, MVT::i32), 4238 VROffsAddr, MachinePointerInfo(SV, 28), false, 4239 false, 4)); 4240 4241 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps); 4242 } 4243 4244 SDValue AArch64TargetLowering::LowerVASTART(SDValue Op, 4245 SelectionDAG &DAG) const { 4246 return Subtarget->isTargetDarwin() ? LowerDarwin_VASTART(Op, DAG) 4247 : LowerAAPCS_VASTART(Op, DAG); 4248 } 4249 4250 SDValue AArch64TargetLowering::LowerVACOPY(SDValue Op, 4251 SelectionDAG &DAG) const { 4252 // AAPCS has three pointers and two ints (= 32 bytes), Darwin has single 4253 // pointer. 4254 SDLoc DL(Op); 4255 unsigned VaListSize = Subtarget->isTargetDarwin() ? 8 : 32; 4256 const Value *DestSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue(); 4257 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); 4258 4259 return DAG.getMemcpy(Op.getOperand(0), DL, Op.getOperand(1), 4260 Op.getOperand(2), 4261 DAG.getConstant(VaListSize, DL, MVT::i32), 4262 8, false, false, false, MachinePointerInfo(DestSV), 4263 MachinePointerInfo(SrcSV)); 4264 } 4265 4266 SDValue AArch64TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const { 4267 assert(Subtarget->isTargetDarwin() && 4268 "automatic va_arg instruction only works on Darwin"); 4269 4270 const Value *V = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 4271 EVT VT = Op.getValueType(); 4272 SDLoc DL(Op); 4273 SDValue Chain = Op.getOperand(0); 4274 SDValue Addr = Op.getOperand(1); 4275 unsigned Align = Op.getConstantOperandVal(3); 4276 auto PtrVT = getPointerTy(DAG.getDataLayout()); 4277 4278 SDValue VAList = DAG.getLoad(PtrVT, DL, Chain, Addr, MachinePointerInfo(V), 4279 false, false, false, 0); 4280 Chain = VAList.getValue(1); 4281 4282 if (Align > 8) { 4283 assert(((Align & (Align - 1)) == 0) && "Expected Align to be a power of 2"); 4284 VAList = DAG.getNode(ISD::ADD, DL, PtrVT, VAList, 4285 DAG.getConstant(Align - 1, DL, PtrVT)); 4286 VAList = DAG.getNode(ISD::AND, DL, PtrVT, VAList, 4287 DAG.getConstant(-(int64_t)Align, DL, PtrVT)); 4288 } 4289 4290 Type *ArgTy = VT.getTypeForEVT(*DAG.getContext()); 4291 uint64_t ArgSize = DAG.getDataLayout().getTypeAllocSize(ArgTy); 4292 4293 // Scalar integer and FP values smaller than 64 bits are implicitly extended 4294 // up to 64 bits. At the very least, we have to increase the striding of the 4295 // vaargs list to match this, and for FP values we need to introduce 4296 // FP_ROUND nodes as well. 4297 if (VT.isInteger() && !VT.isVector()) 4298 ArgSize = 8; 4299 bool NeedFPTrunc = false; 4300 if (VT.isFloatingPoint() && !VT.isVector() && VT != MVT::f64) { 4301 ArgSize = 8; 4302 NeedFPTrunc = true; 4303 } 4304 4305 // Increment the pointer, VAList, to the next vaarg 4306 SDValue VANext = DAG.getNode(ISD::ADD, DL, PtrVT, VAList, 4307 DAG.getConstant(ArgSize, DL, PtrVT)); 4308 // Store the incremented VAList to the legalized pointer 4309 SDValue APStore = DAG.getStore(Chain, DL, VANext, Addr, MachinePointerInfo(V), 4310 false, false, 0); 4311 4312 // Load the actual argument out of the pointer VAList 4313 if (NeedFPTrunc) { 4314 // Load the value as an f64. 4315 SDValue WideFP = DAG.getLoad(MVT::f64, DL, APStore, VAList, 4316 MachinePointerInfo(), false, false, false, 0); 4317 // Round the value down to an f32. 4318 SDValue NarrowFP = DAG.getNode(ISD::FP_ROUND, DL, VT, WideFP.getValue(0), 4319 DAG.getIntPtrConstant(1, DL)); 4320 SDValue Ops[] = { NarrowFP, WideFP.getValue(1) }; 4321 // Merge the rounded value with the chain output of the load. 4322 return DAG.getMergeValues(Ops, DL); 4323 } 4324 4325 return DAG.getLoad(VT, DL, APStore, VAList, MachinePointerInfo(), false, 4326 false, false, 0); 4327 } 4328 4329 SDValue AArch64TargetLowering::LowerFRAMEADDR(SDValue Op, 4330 SelectionDAG &DAG) const { 4331 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 4332 MFI->setFrameAddressIsTaken(true); 4333 4334 EVT VT = Op.getValueType(); 4335 SDLoc DL(Op); 4336 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4337 SDValue FrameAddr = 4338 DAG.getCopyFromReg(DAG.getEntryNode(), DL, AArch64::FP, VT); 4339 while (Depth--) 4340 FrameAddr = DAG.getLoad(VT, DL, DAG.getEntryNode(), FrameAddr, 4341 MachinePointerInfo(), false, false, false, 0); 4342 return FrameAddr; 4343 } 4344 4345 // FIXME? Maybe this could be a TableGen attribute on some registers and 4346 // this table could be generated automatically from RegInfo. 4347 unsigned AArch64TargetLowering::getRegisterByName(const char* RegName, EVT VT, 4348 SelectionDAG &DAG) const { 4349 unsigned Reg = StringSwitch<unsigned>(RegName) 4350 .Case("sp", AArch64::SP) 4351 .Default(0); 4352 if (Reg) 4353 return Reg; 4354 report_fatal_error(Twine("Invalid register name \"" 4355 + StringRef(RegName) + "\".")); 4356 } 4357 4358 SDValue AArch64TargetLowering::LowerRETURNADDR(SDValue Op, 4359 SelectionDAG &DAG) const { 4360 MachineFunction &MF = DAG.getMachineFunction(); 4361 MachineFrameInfo *MFI = MF.getFrameInfo(); 4362 MFI->setReturnAddressIsTaken(true); 4363 4364 EVT VT = Op.getValueType(); 4365 SDLoc DL(Op); 4366 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4367 if (Depth) { 4368 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 4369 SDValue Offset = DAG.getConstant(8, DL, getPointerTy(DAG.getDataLayout())); 4370 return DAG.getLoad(VT, DL, DAG.getEntryNode(), 4371 DAG.getNode(ISD::ADD, DL, VT, FrameAddr, Offset), 4372 MachinePointerInfo(), false, false, false, 0); 4373 } 4374 4375 // Return LR, which contains the return address. Mark it an implicit live-in. 4376 unsigned Reg = MF.addLiveIn(AArch64::LR, &AArch64::GPR64RegClass); 4377 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, VT); 4378 } 4379 4380 /// LowerShiftRightParts - Lower SRA_PARTS, which returns two 4381 /// i64 values and take a 2 x i64 value to shift plus a shift amount. 4382 SDValue AArch64TargetLowering::LowerShiftRightParts(SDValue Op, 4383 SelectionDAG &DAG) const { 4384 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 4385 EVT VT = Op.getValueType(); 4386 unsigned VTBits = VT.getSizeInBits(); 4387 SDLoc dl(Op); 4388 SDValue ShOpLo = Op.getOperand(0); 4389 SDValue ShOpHi = Op.getOperand(1); 4390 SDValue ShAmt = Op.getOperand(2); 4391 SDValue ARMcc; 4392 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL; 4393 4394 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS); 4395 4396 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i64, 4397 DAG.getConstant(VTBits, dl, MVT::i64), ShAmt); 4398 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt); 4399 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i64, ShAmt, 4400 DAG.getConstant(VTBits, dl, MVT::i64)); 4401 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt); 4402 4403 SDValue Cmp = emitComparison(ExtraShAmt, DAG.getConstant(0, dl, MVT::i64), 4404 ISD::SETGE, dl, DAG); 4405 SDValue CCVal = DAG.getConstant(AArch64CC::GE, dl, MVT::i32); 4406 4407 SDValue FalseValLo = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 4408 SDValue TrueValLo = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt); 4409 SDValue Lo = 4410 DAG.getNode(AArch64ISD::CSEL, dl, VT, TrueValLo, FalseValLo, CCVal, Cmp); 4411 4412 // AArch64 shifts larger than the register width are wrapped rather than 4413 // clamped, so we can't just emit "hi >> x". 4414 SDValue FalseValHi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt); 4415 SDValue TrueValHi = Opc == ISD::SRA 4416 ? DAG.getNode(Opc, dl, VT, ShOpHi, 4417 DAG.getConstant(VTBits - 1, dl, 4418 MVT::i64)) 4419 : DAG.getConstant(0, dl, VT); 4420 SDValue Hi = 4421 DAG.getNode(AArch64ISD::CSEL, dl, VT, TrueValHi, FalseValHi, CCVal, Cmp); 4422 4423 SDValue Ops[2] = { Lo, Hi }; 4424 return DAG.getMergeValues(Ops, dl); 4425 } 4426 4427 /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two 4428 /// i64 values and take a 2 x i64 value to shift plus a shift amount. 4429 SDValue AArch64TargetLowering::LowerShiftLeftParts(SDValue Op, 4430 SelectionDAG &DAG) const { 4431 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 4432 EVT VT = Op.getValueType(); 4433 unsigned VTBits = VT.getSizeInBits(); 4434 SDLoc dl(Op); 4435 SDValue ShOpLo = Op.getOperand(0); 4436 SDValue ShOpHi = Op.getOperand(1); 4437 SDValue ShAmt = Op.getOperand(2); 4438 SDValue ARMcc; 4439 4440 assert(Op.getOpcode() == ISD::SHL_PARTS); 4441 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i64, 4442 DAG.getConstant(VTBits, dl, MVT::i64), ShAmt); 4443 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt); 4444 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i64, ShAmt, 4445 DAG.getConstant(VTBits, dl, MVT::i64)); 4446 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt); 4447 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt); 4448 4449 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 4450 4451 SDValue Cmp = emitComparison(ExtraShAmt, DAG.getConstant(0, dl, MVT::i64), 4452 ISD::SETGE, dl, DAG); 4453 SDValue CCVal = DAG.getConstant(AArch64CC::GE, dl, MVT::i32); 4454 SDValue Hi = 4455 DAG.getNode(AArch64ISD::CSEL, dl, VT, Tmp3, FalseVal, CCVal, Cmp); 4456 4457 // AArch64 shifts of larger than register sizes are wrapped rather than 4458 // clamped, so we can't just emit "lo << a" if a is too big. 4459 SDValue TrueValLo = DAG.getConstant(0, dl, VT); 4460 SDValue FalseValLo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt); 4461 SDValue Lo = 4462 DAG.getNode(AArch64ISD::CSEL, dl, VT, TrueValLo, FalseValLo, CCVal, Cmp); 4463 4464 SDValue Ops[2] = { Lo, Hi }; 4465 return DAG.getMergeValues(Ops, dl); 4466 } 4467 4468 bool AArch64TargetLowering::isOffsetFoldingLegal( 4469 const GlobalAddressSDNode *GA) const { 4470 // The AArch64 target doesn't support folding offsets into global addresses. 4471 return false; 4472 } 4473 4474 bool AArch64TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { 4475 // We can materialize #0.0 as fmov $Rd, XZR for 64-bit and 32-bit cases. 4476 // FIXME: We should be able to handle f128 as well with a clever lowering. 4477 if (Imm.isPosZero() && (VT == MVT::f64 || VT == MVT::f32)) 4478 return true; 4479 4480 if (VT == MVT::f64) 4481 return AArch64_AM::getFP64Imm(Imm) != -1; 4482 else if (VT == MVT::f32) 4483 return AArch64_AM::getFP32Imm(Imm) != -1; 4484 return false; 4485 } 4486 4487 //===----------------------------------------------------------------------===// 4488 // AArch64 Optimization Hooks 4489 //===----------------------------------------------------------------------===// 4490 4491 //===----------------------------------------------------------------------===// 4492 // AArch64 Inline Assembly Support 4493 //===----------------------------------------------------------------------===// 4494 4495 // Table of Constraints 4496 // TODO: This is the current set of constraints supported by ARM for the 4497 // compiler, not all of them may make sense, e.g. S may be difficult to support. 4498 // 4499 // r - A general register 4500 // w - An FP/SIMD register of some size in the range v0-v31 4501 // x - An FP/SIMD register of some size in the range v0-v15 4502 // I - Constant that can be used with an ADD instruction 4503 // J - Constant that can be used with a SUB instruction 4504 // K - Constant that can be used with a 32-bit logical instruction 4505 // L - Constant that can be used with a 64-bit logical instruction 4506 // M - Constant that can be used as a 32-bit MOV immediate 4507 // N - Constant that can be used as a 64-bit MOV immediate 4508 // Q - A memory reference with base register and no offset 4509 // S - A symbolic address 4510 // Y - Floating point constant zero 4511 // Z - Integer constant zero 4512 // 4513 // Note that general register operands will be output using their 64-bit x 4514 // register name, whatever the size of the variable, unless the asm operand 4515 // is prefixed by the %w modifier. Floating-point and SIMD register operands 4516 // will be output with the v prefix unless prefixed by the %b, %h, %s, %d or 4517 // %q modifier. 4518 4519 /// getConstraintType - Given a constraint letter, return the type of 4520 /// constraint it is for this target. 4521 AArch64TargetLowering::ConstraintType 4522 AArch64TargetLowering::getConstraintType(StringRef Constraint) const { 4523 if (Constraint.size() == 1) { 4524 switch (Constraint[0]) { 4525 default: 4526 break; 4527 case 'z': 4528 return C_Other; 4529 case 'x': 4530 case 'w': 4531 return C_RegisterClass; 4532 // An address with a single base register. Due to the way we 4533 // currently handle addresses it is the same as 'r'. 4534 case 'Q': 4535 return C_Memory; 4536 } 4537 } 4538 return TargetLowering::getConstraintType(Constraint); 4539 } 4540 4541 /// Examine constraint type and operand type and determine a weight value. 4542 /// This object must already have been set up with the operand type 4543 /// and the current alternative constraint selected. 4544 TargetLowering::ConstraintWeight 4545 AArch64TargetLowering::getSingleConstraintMatchWeight( 4546 AsmOperandInfo &info, const char *constraint) const { 4547 ConstraintWeight weight = CW_Invalid; 4548 Value *CallOperandVal = info.CallOperandVal; 4549 // If we don't have a value, we can't do a match, 4550 // but allow it at the lowest weight. 4551 if (!CallOperandVal) 4552 return CW_Default; 4553 Type *type = CallOperandVal->getType(); 4554 // Look at the constraint type. 4555 switch (*constraint) { 4556 default: 4557 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 4558 break; 4559 case 'x': 4560 case 'w': 4561 if (type->isFloatingPointTy() || type->isVectorTy()) 4562 weight = CW_Register; 4563 break; 4564 case 'z': 4565 weight = CW_Constant; 4566 break; 4567 } 4568 return weight; 4569 } 4570 4571 std::pair<unsigned, const TargetRegisterClass *> 4572 AArch64TargetLowering::getRegForInlineAsmConstraint( 4573 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const { 4574 if (Constraint.size() == 1) { 4575 switch (Constraint[0]) { 4576 case 'r': 4577 if (VT.getSizeInBits() == 64) 4578 return std::make_pair(0U, &AArch64::GPR64commonRegClass); 4579 return std::make_pair(0U, &AArch64::GPR32commonRegClass); 4580 case 'w': 4581 if (VT == MVT::f32) 4582 return std::make_pair(0U, &AArch64::FPR32RegClass); 4583 if (VT.getSizeInBits() == 64) 4584 return std::make_pair(0U, &AArch64::FPR64RegClass); 4585 if (VT.getSizeInBits() == 128) 4586 return std::make_pair(0U, &AArch64::FPR128RegClass); 4587 break; 4588 // The instructions that this constraint is designed for can 4589 // only take 128-bit registers so just use that regclass. 4590 case 'x': 4591 if (VT.getSizeInBits() == 128) 4592 return std::make_pair(0U, &AArch64::FPR128_loRegClass); 4593 break; 4594 } 4595 } 4596 if (StringRef("{cc}").equals_lower(Constraint)) 4597 return std::make_pair(unsigned(AArch64::NZCV), &AArch64::CCRRegClass); 4598 4599 // Use the default implementation in TargetLowering to convert the register 4600 // constraint into a member of a register class. 4601 std::pair<unsigned, const TargetRegisterClass *> Res; 4602 Res = TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 4603 4604 // Not found as a standard register? 4605 if (!Res.second) { 4606 unsigned Size = Constraint.size(); 4607 if ((Size == 4 || Size == 5) && Constraint[0] == '{' && 4608 tolower(Constraint[1]) == 'v' && Constraint[Size - 1] == '}') { 4609 int RegNo; 4610 bool Failed = Constraint.slice(2, Size - 1).getAsInteger(10, RegNo); 4611 if (!Failed && RegNo >= 0 && RegNo <= 31) { 4612 // v0 - v31 are aliases of q0 - q31. 4613 // By default we'll emit v0-v31 for this unless there's a modifier where 4614 // we'll emit the correct register as well. 4615 Res.first = AArch64::FPR128RegClass.getRegister(RegNo); 4616 Res.second = &AArch64::FPR128RegClass; 4617 } 4618 } 4619 } 4620 4621 return Res; 4622 } 4623 4624 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 4625 /// vector. If it is invalid, don't add anything to Ops. 4626 void AArch64TargetLowering::LowerAsmOperandForConstraint( 4627 SDValue Op, std::string &Constraint, std::vector<SDValue> &Ops, 4628 SelectionDAG &DAG) const { 4629 SDValue Result; 4630 4631 // Currently only support length 1 constraints. 4632 if (Constraint.length() != 1) 4633 return; 4634 4635 char ConstraintLetter = Constraint[0]; 4636 switch (ConstraintLetter) { 4637 default: 4638 break; 4639 4640 // This set of constraints deal with valid constants for various instructions. 4641 // Validate and return a target constant for them if we can. 4642 case 'z': { 4643 // 'z' maps to xzr or wzr so it needs an input of 0. 4644 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 4645 if (!C || C->getZExtValue() != 0) 4646 return; 4647 4648 if (Op.getValueType() == MVT::i64) 4649 Result = DAG.getRegister(AArch64::XZR, MVT::i64); 4650 else 4651 Result = DAG.getRegister(AArch64::WZR, MVT::i32); 4652 break; 4653 } 4654 4655 case 'I': 4656 case 'J': 4657 case 'K': 4658 case 'L': 4659 case 'M': 4660 case 'N': 4661 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 4662 if (!C) 4663 return; 4664 4665 // Grab the value and do some validation. 4666 uint64_t CVal = C->getZExtValue(); 4667 switch (ConstraintLetter) { 4668 // The I constraint applies only to simple ADD or SUB immediate operands: 4669 // i.e. 0 to 4095 with optional shift by 12 4670 // The J constraint applies only to ADD or SUB immediates that would be 4671 // valid when negated, i.e. if [an add pattern] were to be output as a SUB 4672 // instruction [or vice versa], in other words -1 to -4095 with optional 4673 // left shift by 12. 4674 case 'I': 4675 if (isUInt<12>(CVal) || isShiftedUInt<12, 12>(CVal)) 4676 break; 4677 return; 4678 case 'J': { 4679 uint64_t NVal = -C->getSExtValue(); 4680 if (isUInt<12>(NVal) || isShiftedUInt<12, 12>(NVal)) { 4681 CVal = C->getSExtValue(); 4682 break; 4683 } 4684 return; 4685 } 4686 // The K and L constraints apply *only* to logical immediates, including 4687 // what used to be the MOVI alias for ORR (though the MOVI alias has now 4688 // been removed and MOV should be used). So these constraints have to 4689 // distinguish between bit patterns that are valid 32-bit or 64-bit 4690 // "bitmask immediates": for example 0xaaaaaaaa is a valid bimm32 (K), but 4691 // not a valid bimm64 (L) where 0xaaaaaaaaaaaaaaaa would be valid, and vice 4692 // versa. 4693 case 'K': 4694 if (AArch64_AM::isLogicalImmediate(CVal, 32)) 4695 break; 4696 return; 4697 case 'L': 4698 if (AArch64_AM::isLogicalImmediate(CVal, 64)) 4699 break; 4700 return; 4701 // The M and N constraints are a superset of K and L respectively, for use 4702 // with the MOV (immediate) alias. As well as the logical immediates they 4703 // also match 32 or 64-bit immediates that can be loaded either using a 4704 // *single* MOVZ or MOVN , such as 32-bit 0x12340000, 0x00001234, 0xffffedca 4705 // (M) or 64-bit 0x1234000000000000 (N) etc. 4706 // As a note some of this code is liberally stolen from the asm parser. 4707 case 'M': { 4708 if (!isUInt<32>(CVal)) 4709 return; 4710 if (AArch64_AM::isLogicalImmediate(CVal, 32)) 4711 break; 4712 if ((CVal & 0xFFFF) == CVal) 4713 break; 4714 if ((CVal & 0xFFFF0000ULL) == CVal) 4715 break; 4716 uint64_t NCVal = ~(uint32_t)CVal; 4717 if ((NCVal & 0xFFFFULL) == NCVal) 4718 break; 4719 if ((NCVal & 0xFFFF0000ULL) == NCVal) 4720 break; 4721 return; 4722 } 4723 case 'N': { 4724 if (AArch64_AM::isLogicalImmediate(CVal, 64)) 4725 break; 4726 if ((CVal & 0xFFFFULL) == CVal) 4727 break; 4728 if ((CVal & 0xFFFF0000ULL) == CVal) 4729 break; 4730 if ((CVal & 0xFFFF00000000ULL) == CVal) 4731 break; 4732 if ((CVal & 0xFFFF000000000000ULL) == CVal) 4733 break; 4734 uint64_t NCVal = ~CVal; 4735 if ((NCVal & 0xFFFFULL) == NCVal) 4736 break; 4737 if ((NCVal & 0xFFFF0000ULL) == NCVal) 4738 break; 4739 if ((NCVal & 0xFFFF00000000ULL) == NCVal) 4740 break; 4741 if ((NCVal & 0xFFFF000000000000ULL) == NCVal) 4742 break; 4743 return; 4744 } 4745 default: 4746 return; 4747 } 4748 4749 // All assembler immediates are 64-bit integers. 4750 Result = DAG.getTargetConstant(CVal, SDLoc(Op), MVT::i64); 4751 break; 4752 } 4753 4754 if (Result.getNode()) { 4755 Ops.push_back(Result); 4756 return; 4757 } 4758 4759 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 4760 } 4761 4762 //===----------------------------------------------------------------------===// 4763 // AArch64 Advanced SIMD Support 4764 //===----------------------------------------------------------------------===// 4765 4766 /// WidenVector - Given a value in the V64 register class, produce the 4767 /// equivalent value in the V128 register class. 4768 static SDValue WidenVector(SDValue V64Reg, SelectionDAG &DAG) { 4769 EVT VT = V64Reg.getValueType(); 4770 unsigned NarrowSize = VT.getVectorNumElements(); 4771 MVT EltTy = VT.getVectorElementType().getSimpleVT(); 4772 MVT WideTy = MVT::getVectorVT(EltTy, 2 * NarrowSize); 4773 SDLoc DL(V64Reg); 4774 4775 return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, WideTy, DAG.getUNDEF(WideTy), 4776 V64Reg, DAG.getConstant(0, DL, MVT::i32)); 4777 } 4778 4779 /// getExtFactor - Determine the adjustment factor for the position when 4780 /// generating an "extract from vector registers" instruction. 4781 static unsigned getExtFactor(SDValue &V) { 4782 EVT EltType = V.getValueType().getVectorElementType(); 4783 return EltType.getSizeInBits() / 8; 4784 } 4785 4786 /// NarrowVector - Given a value in the V128 register class, produce the 4787 /// equivalent value in the V64 register class. 4788 static SDValue NarrowVector(SDValue V128Reg, SelectionDAG &DAG) { 4789 EVT VT = V128Reg.getValueType(); 4790 unsigned WideSize = VT.getVectorNumElements(); 4791 MVT EltTy = VT.getVectorElementType().getSimpleVT(); 4792 MVT NarrowTy = MVT::getVectorVT(EltTy, WideSize / 2); 4793 SDLoc DL(V128Reg); 4794 4795 return DAG.getTargetExtractSubreg(AArch64::dsub, DL, NarrowTy, V128Reg); 4796 } 4797 4798 // Gather data to see if the operation can be modelled as a 4799 // shuffle in combination with VEXTs. 4800 SDValue AArch64TargetLowering::ReconstructShuffle(SDValue Op, 4801 SelectionDAG &DAG) const { 4802 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!"); 4803 SDLoc dl(Op); 4804 EVT VT = Op.getValueType(); 4805 unsigned NumElts = VT.getVectorNumElements(); 4806 4807 struct ShuffleSourceInfo { 4808 SDValue Vec; 4809 unsigned MinElt; 4810 unsigned MaxElt; 4811 4812 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to 4813 // be compatible with the shuffle we intend to construct. As a result 4814 // ShuffleVec will be some sliding window into the original Vec. 4815 SDValue ShuffleVec; 4816 4817 // Code should guarantee that element i in Vec starts at element "WindowBase 4818 // + i * WindowScale in ShuffleVec". 4819 int WindowBase; 4820 int WindowScale; 4821 4822 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; } 4823 ShuffleSourceInfo(SDValue Vec) 4824 : Vec(Vec), MinElt(UINT_MAX), MaxElt(0), ShuffleVec(Vec), WindowBase(0), 4825 WindowScale(1) {} 4826 }; 4827 4828 // First gather all vectors used as an immediate source for this BUILD_VECTOR 4829 // node. 4830 SmallVector<ShuffleSourceInfo, 2> Sources; 4831 for (unsigned i = 0; i < NumElts; ++i) { 4832 SDValue V = Op.getOperand(i); 4833 if (V.getOpcode() == ISD::UNDEF) 4834 continue; 4835 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) { 4836 // A shuffle can only come from building a vector from various 4837 // elements of other vectors. 4838 return SDValue(); 4839 } 4840 4841 // Add this element source to the list if it's not already there. 4842 SDValue SourceVec = V.getOperand(0); 4843 auto Source = std::find(Sources.begin(), Sources.end(), SourceVec); 4844 if (Source == Sources.end()) 4845 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec)); 4846 4847 // Update the minimum and maximum lane number seen. 4848 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue(); 4849 Source->MinElt = std::min(Source->MinElt, EltNo); 4850 Source->MaxElt = std::max(Source->MaxElt, EltNo); 4851 } 4852 4853 // Currently only do something sane when at most two source vectors 4854 // are involved. 4855 if (Sources.size() > 2) 4856 return SDValue(); 4857 4858 // Find out the smallest element size among result and two sources, and use 4859 // it as element size to build the shuffle_vector. 4860 EVT SmallestEltTy = VT.getVectorElementType(); 4861 for (auto &Source : Sources) { 4862 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType(); 4863 if (SrcEltTy.bitsLT(SmallestEltTy)) { 4864 SmallestEltTy = SrcEltTy; 4865 } 4866 } 4867 unsigned ResMultiplier = 4868 VT.getVectorElementType().getSizeInBits() / SmallestEltTy.getSizeInBits(); 4869 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits(); 4870 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts); 4871 4872 // If the source vector is too wide or too narrow, we may nevertheless be able 4873 // to construct a compatible shuffle either by concatenating it with UNDEF or 4874 // extracting a suitable range of elements. 4875 for (auto &Src : Sources) { 4876 EVT SrcVT = Src.ShuffleVec.getValueType(); 4877 4878 if (SrcVT.getSizeInBits() == VT.getSizeInBits()) 4879 continue; 4880 4881 // This stage of the search produces a source with the same element type as 4882 // the original, but with a total width matching the BUILD_VECTOR output. 4883 EVT EltVT = SrcVT.getVectorElementType(); 4884 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits(); 4885 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts); 4886 4887 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) { 4888 assert(2 * SrcVT.getSizeInBits() == VT.getSizeInBits()); 4889 // We can pad out the smaller vector for free, so if it's part of a 4890 // shuffle... 4891 Src.ShuffleVec = 4892 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec, 4893 DAG.getUNDEF(Src.ShuffleVec.getValueType())); 4894 continue; 4895 } 4896 4897 assert(SrcVT.getSizeInBits() == 2 * VT.getSizeInBits()); 4898 4899 if (Src.MaxElt - Src.MinElt >= NumSrcElts) { 4900 // Span too large for a VEXT to cope 4901 return SDValue(); 4902 } 4903 4904 if (Src.MinElt >= NumSrcElts) { 4905 // The extraction can just take the second half 4906 Src.ShuffleVec = 4907 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 4908 DAG.getConstant(NumSrcElts, dl, MVT::i64)); 4909 Src.WindowBase = -NumSrcElts; 4910 } else if (Src.MaxElt < NumSrcElts) { 4911 // The extraction can just take the first half 4912 Src.ShuffleVec = 4913 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 4914 DAG.getConstant(0, dl, MVT::i64)); 4915 } else { 4916 // An actual VEXT is needed 4917 SDValue VEXTSrc1 = 4918 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 4919 DAG.getConstant(0, dl, MVT::i64)); 4920 SDValue VEXTSrc2 = 4921 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 4922 DAG.getConstant(NumSrcElts, dl, MVT::i64)); 4923 unsigned Imm = Src.MinElt * getExtFactor(VEXTSrc1); 4924 4925 Src.ShuffleVec = DAG.getNode(AArch64ISD::EXT, dl, DestVT, VEXTSrc1, 4926 VEXTSrc2, 4927 DAG.getConstant(Imm, dl, MVT::i32)); 4928 Src.WindowBase = -Src.MinElt; 4929 } 4930 } 4931 4932 // Another possible incompatibility occurs from the vector element types. We 4933 // can fix this by bitcasting the source vectors to the same type we intend 4934 // for the shuffle. 4935 for (auto &Src : Sources) { 4936 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType(); 4937 if (SrcEltTy == SmallestEltTy) 4938 continue; 4939 assert(ShuffleVT.getVectorElementType() == SmallestEltTy); 4940 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec); 4941 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits(); 4942 Src.WindowBase *= Src.WindowScale; 4943 } 4944 4945 // Final sanity check before we try to actually produce a shuffle. 4946 DEBUG( 4947 for (auto Src : Sources) 4948 assert(Src.ShuffleVec.getValueType() == ShuffleVT); 4949 ); 4950 4951 // The stars all align, our next step is to produce the mask for the shuffle. 4952 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1); 4953 int BitsPerShuffleLane = ShuffleVT.getVectorElementType().getSizeInBits(); 4954 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) { 4955 SDValue Entry = Op.getOperand(i); 4956 if (Entry.getOpcode() == ISD::UNDEF) 4957 continue; 4958 4959 auto Src = std::find(Sources.begin(), Sources.end(), Entry.getOperand(0)); 4960 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue(); 4961 4962 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit 4963 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this 4964 // segment. 4965 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType(); 4966 int BitsDefined = std::min(OrigEltTy.getSizeInBits(), 4967 VT.getVectorElementType().getSizeInBits()); 4968 int LanesDefined = BitsDefined / BitsPerShuffleLane; 4969 4970 // This source is expected to fill ResMultiplier lanes of the final shuffle, 4971 // starting at the appropriate offset. 4972 int *LaneMask = &Mask[i * ResMultiplier]; 4973 4974 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase; 4975 ExtractBase += NumElts * (Src - Sources.begin()); 4976 for (int j = 0; j < LanesDefined; ++j) 4977 LaneMask[j] = ExtractBase + j; 4978 } 4979 4980 // Final check before we try to produce nonsense... 4981 if (!isShuffleMaskLegal(Mask, ShuffleVT)) 4982 return SDValue(); 4983 4984 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) }; 4985 for (unsigned i = 0; i < Sources.size(); ++i) 4986 ShuffleOps[i] = Sources[i].ShuffleVec; 4987 4988 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0], 4989 ShuffleOps[1], &Mask[0]); 4990 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle); 4991 } 4992 4993 // check if an EXT instruction can handle the shuffle mask when the 4994 // vector sources of the shuffle are the same. 4995 static bool isSingletonEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) { 4996 unsigned NumElts = VT.getVectorNumElements(); 4997 4998 // Assume that the first shuffle index is not UNDEF. Fail if it is. 4999 if (M[0] < 0) 5000 return false; 5001 5002 Imm = M[0]; 5003 5004 // If this is a VEXT shuffle, the immediate value is the index of the first 5005 // element. The other shuffle indices must be the successive elements after 5006 // the first one. 5007 unsigned ExpectedElt = Imm; 5008 for (unsigned i = 1; i < NumElts; ++i) { 5009 // Increment the expected index. If it wraps around, just follow it 5010 // back to index zero and keep going. 5011 ++ExpectedElt; 5012 if (ExpectedElt == NumElts) 5013 ExpectedElt = 0; 5014 5015 if (M[i] < 0) 5016 continue; // ignore UNDEF indices 5017 if (ExpectedElt != static_cast<unsigned>(M[i])) 5018 return false; 5019 } 5020 5021 return true; 5022 } 5023 5024 // check if an EXT instruction can handle the shuffle mask when the 5025 // vector sources of the shuffle are different. 5026 static bool isEXTMask(ArrayRef<int> M, EVT VT, bool &ReverseEXT, 5027 unsigned &Imm) { 5028 // Look for the first non-undef element. 5029 const int *FirstRealElt = std::find_if(M.begin(), M.end(), 5030 [](int Elt) {return Elt >= 0;}); 5031 5032 // Benefit form APInt to handle overflow when calculating expected element. 5033 unsigned NumElts = VT.getVectorNumElements(); 5034 unsigned MaskBits = APInt(32, NumElts * 2).logBase2(); 5035 APInt ExpectedElt = APInt(MaskBits, *FirstRealElt + 1); 5036 // The following shuffle indices must be the successive elements after the 5037 // first real element. 5038 const int *FirstWrongElt = std::find_if(FirstRealElt + 1, M.end(), 5039 [&](int Elt) {return Elt != ExpectedElt++ && Elt != -1;}); 5040 if (FirstWrongElt != M.end()) 5041 return false; 5042 5043 // The index of an EXT is the first element if it is not UNDEF. 5044 // Watch out for the beginning UNDEFs. The EXT index should be the expected 5045 // value of the first element. E.g. 5046 // <-1, -1, 3, ...> is treated as <1, 2, 3, ...>. 5047 // <-1, -1, 0, 1, ...> is treated as <2*NumElts-2, 2*NumElts-1, 0, 1, ...>. 5048 // ExpectedElt is the last mask index plus 1. 5049 Imm = ExpectedElt.getZExtValue(); 5050 5051 // There are two difference cases requiring to reverse input vectors. 5052 // For example, for vector <4 x i32> we have the following cases, 5053 // Case 1: shufflevector(<4 x i32>,<4 x i32>,<-1, -1, -1, 0>) 5054 // Case 2: shufflevector(<4 x i32>,<4 x i32>,<-1, -1, 7, 0>) 5055 // For both cases, we finally use mask <5, 6, 7, 0>, which requires 5056 // to reverse two input vectors. 5057 if (Imm < NumElts) 5058 ReverseEXT = true; 5059 else 5060 Imm -= NumElts; 5061 5062 return true; 5063 } 5064 5065 /// isREVMask - Check if a vector shuffle corresponds to a REV 5066 /// instruction with the specified blocksize. (The order of the elements 5067 /// within each block of the vector is reversed.) 5068 static bool isREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) { 5069 assert((BlockSize == 16 || BlockSize == 32 || BlockSize == 64) && 5070 "Only possible block sizes for REV are: 16, 32, 64"); 5071 5072 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 5073 if (EltSz == 64) 5074 return false; 5075 5076 unsigned NumElts = VT.getVectorNumElements(); 5077 unsigned BlockElts = M[0] + 1; 5078 // If the first shuffle index is UNDEF, be optimistic. 5079 if (M[0] < 0) 5080 BlockElts = BlockSize / EltSz; 5081 5082 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz) 5083 return false; 5084 5085 for (unsigned i = 0; i < NumElts; ++i) { 5086 if (M[i] < 0) 5087 continue; // ignore UNDEF indices 5088 if ((unsigned)M[i] != (i - i % BlockElts) + (BlockElts - 1 - i % BlockElts)) 5089 return false; 5090 } 5091 5092 return true; 5093 } 5094 5095 static bool isZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 5096 unsigned NumElts = VT.getVectorNumElements(); 5097 WhichResult = (M[0] == 0 ? 0 : 1); 5098 unsigned Idx = WhichResult * NumElts / 2; 5099 for (unsigned i = 0; i != NumElts; i += 2) { 5100 if ((M[i] >= 0 && (unsigned)M[i] != Idx) || 5101 (M[i + 1] >= 0 && (unsigned)M[i + 1] != Idx + NumElts)) 5102 return false; 5103 Idx += 1; 5104 } 5105 5106 return true; 5107 } 5108 5109 static bool isUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 5110 unsigned NumElts = VT.getVectorNumElements(); 5111 WhichResult = (M[0] == 0 ? 0 : 1); 5112 for (unsigned i = 0; i != NumElts; ++i) { 5113 if (M[i] < 0) 5114 continue; // ignore UNDEF indices 5115 if ((unsigned)M[i] != 2 * i + WhichResult) 5116 return false; 5117 } 5118 5119 return true; 5120 } 5121 5122 static bool isTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 5123 unsigned NumElts = VT.getVectorNumElements(); 5124 WhichResult = (M[0] == 0 ? 0 : 1); 5125 for (unsigned i = 0; i < NumElts; i += 2) { 5126 if ((M[i] >= 0 && (unsigned)M[i] != i + WhichResult) || 5127 (M[i + 1] >= 0 && (unsigned)M[i + 1] != i + NumElts + WhichResult)) 5128 return false; 5129 } 5130 return true; 5131 } 5132 5133 /// isZIP_v_undef_Mask - Special case of isZIPMask for canonical form of 5134 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 5135 /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>. 5136 static bool isZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 5137 unsigned NumElts = VT.getVectorNumElements(); 5138 WhichResult = (M[0] == 0 ? 0 : 1); 5139 unsigned Idx = WhichResult * NumElts / 2; 5140 for (unsigned i = 0; i != NumElts; i += 2) { 5141 if ((M[i] >= 0 && (unsigned)M[i] != Idx) || 5142 (M[i + 1] >= 0 && (unsigned)M[i + 1] != Idx)) 5143 return false; 5144 Idx += 1; 5145 } 5146 5147 return true; 5148 } 5149 5150 /// isUZP_v_undef_Mask - Special case of isUZPMask for canonical form of 5151 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 5152 /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>, 5153 static bool isUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 5154 unsigned Half = VT.getVectorNumElements() / 2; 5155 WhichResult = (M[0] == 0 ? 0 : 1); 5156 for (unsigned j = 0; j != 2; ++j) { 5157 unsigned Idx = WhichResult; 5158 for (unsigned i = 0; i != Half; ++i) { 5159 int MIdx = M[i + j * Half]; 5160 if (MIdx >= 0 && (unsigned)MIdx != Idx) 5161 return false; 5162 Idx += 2; 5163 } 5164 } 5165 5166 return true; 5167 } 5168 5169 /// isTRN_v_undef_Mask - Special case of isTRNMask for canonical form of 5170 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 5171 /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>. 5172 static bool isTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 5173 unsigned NumElts = VT.getVectorNumElements(); 5174 WhichResult = (M[0] == 0 ? 0 : 1); 5175 for (unsigned i = 0; i < NumElts; i += 2) { 5176 if ((M[i] >= 0 && (unsigned)M[i] != i + WhichResult) || 5177 (M[i + 1] >= 0 && (unsigned)M[i + 1] != i + WhichResult)) 5178 return false; 5179 } 5180 return true; 5181 } 5182 5183 static bool isINSMask(ArrayRef<int> M, int NumInputElements, 5184 bool &DstIsLeft, int &Anomaly) { 5185 if (M.size() != static_cast<size_t>(NumInputElements)) 5186 return false; 5187 5188 int NumLHSMatch = 0, NumRHSMatch = 0; 5189 int LastLHSMismatch = -1, LastRHSMismatch = -1; 5190 5191 for (int i = 0; i < NumInputElements; ++i) { 5192 if (M[i] == -1) { 5193 ++NumLHSMatch; 5194 ++NumRHSMatch; 5195 continue; 5196 } 5197 5198 if (M[i] == i) 5199 ++NumLHSMatch; 5200 else 5201 LastLHSMismatch = i; 5202 5203 if (M[i] == i + NumInputElements) 5204 ++NumRHSMatch; 5205 else 5206 LastRHSMismatch = i; 5207 } 5208 5209 if (NumLHSMatch == NumInputElements - 1) { 5210 DstIsLeft = true; 5211 Anomaly = LastLHSMismatch; 5212 return true; 5213 } else if (NumRHSMatch == NumInputElements - 1) { 5214 DstIsLeft = false; 5215 Anomaly = LastRHSMismatch; 5216 return true; 5217 } 5218 5219 return false; 5220 } 5221 5222 static bool isConcatMask(ArrayRef<int> Mask, EVT VT, bool SplitLHS) { 5223 if (VT.getSizeInBits() != 128) 5224 return false; 5225 5226 unsigned NumElts = VT.getVectorNumElements(); 5227 5228 for (int I = 0, E = NumElts / 2; I != E; I++) { 5229 if (Mask[I] != I) 5230 return false; 5231 } 5232 5233 int Offset = NumElts / 2; 5234 for (int I = NumElts / 2, E = NumElts; I != E; I++) { 5235 if (Mask[I] != I + SplitLHS * Offset) 5236 return false; 5237 } 5238 5239 return true; 5240 } 5241 5242 static SDValue tryFormConcatFromShuffle(SDValue Op, SelectionDAG &DAG) { 5243 SDLoc DL(Op); 5244 EVT VT = Op.getValueType(); 5245 SDValue V0 = Op.getOperand(0); 5246 SDValue V1 = Op.getOperand(1); 5247 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Op)->getMask(); 5248 5249 if (VT.getVectorElementType() != V0.getValueType().getVectorElementType() || 5250 VT.getVectorElementType() != V1.getValueType().getVectorElementType()) 5251 return SDValue(); 5252 5253 bool SplitV0 = V0.getValueType().getSizeInBits() == 128; 5254 5255 if (!isConcatMask(Mask, VT, SplitV0)) 5256 return SDValue(); 5257 5258 EVT CastVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(), 5259 VT.getVectorNumElements() / 2); 5260 if (SplitV0) { 5261 V0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, CastVT, V0, 5262 DAG.getConstant(0, DL, MVT::i64)); 5263 } 5264 if (V1.getValueType().getSizeInBits() == 128) { 5265 V1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, CastVT, V1, 5266 DAG.getConstant(0, DL, MVT::i64)); 5267 } 5268 return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, V0, V1); 5269 } 5270 5271 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit 5272 /// the specified operations to build the shuffle. 5273 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, 5274 SDValue RHS, SelectionDAG &DAG, 5275 SDLoc dl) { 5276 unsigned OpNum = (PFEntry >> 26) & 0x0F; 5277 unsigned LHSID = (PFEntry >> 13) & ((1 << 13) - 1); 5278 unsigned RHSID = (PFEntry >> 0) & ((1 << 13) - 1); 5279 5280 enum { 5281 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3> 5282 OP_VREV, 5283 OP_VDUP0, 5284 OP_VDUP1, 5285 OP_VDUP2, 5286 OP_VDUP3, 5287 OP_VEXT1, 5288 OP_VEXT2, 5289 OP_VEXT3, 5290 OP_VUZPL, // VUZP, left result 5291 OP_VUZPR, // VUZP, right result 5292 OP_VZIPL, // VZIP, left result 5293 OP_VZIPR, // VZIP, right result 5294 OP_VTRNL, // VTRN, left result 5295 OP_VTRNR // VTRN, right result 5296 }; 5297 5298 if (OpNum == OP_COPY) { 5299 if (LHSID == (1 * 9 + 2) * 9 + 3) 5300 return LHS; 5301 assert(LHSID == ((4 * 9 + 5) * 9 + 6) * 9 + 7 && "Illegal OP_COPY!"); 5302 return RHS; 5303 } 5304 5305 SDValue OpLHS, OpRHS; 5306 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl); 5307 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl); 5308 EVT VT = OpLHS.getValueType(); 5309 5310 switch (OpNum) { 5311 default: 5312 llvm_unreachable("Unknown shuffle opcode!"); 5313 case OP_VREV: 5314 // VREV divides the vector in half and swaps within the half. 5315 if (VT.getVectorElementType() == MVT::i32 || 5316 VT.getVectorElementType() == MVT::f32) 5317 return DAG.getNode(AArch64ISD::REV64, dl, VT, OpLHS); 5318 // vrev <4 x i16> -> REV32 5319 if (VT.getVectorElementType() == MVT::i16 || 5320 VT.getVectorElementType() == MVT::f16) 5321 return DAG.getNode(AArch64ISD::REV32, dl, VT, OpLHS); 5322 // vrev <4 x i8> -> REV16 5323 assert(VT.getVectorElementType() == MVT::i8); 5324 return DAG.getNode(AArch64ISD::REV16, dl, VT, OpLHS); 5325 case OP_VDUP0: 5326 case OP_VDUP1: 5327 case OP_VDUP2: 5328 case OP_VDUP3: { 5329 EVT EltTy = VT.getVectorElementType(); 5330 unsigned Opcode; 5331 if (EltTy == MVT::i8) 5332 Opcode = AArch64ISD::DUPLANE8; 5333 else if (EltTy == MVT::i16 || EltTy == MVT::f16) 5334 Opcode = AArch64ISD::DUPLANE16; 5335 else if (EltTy == MVT::i32 || EltTy == MVT::f32) 5336 Opcode = AArch64ISD::DUPLANE32; 5337 else if (EltTy == MVT::i64 || EltTy == MVT::f64) 5338 Opcode = AArch64ISD::DUPLANE64; 5339 else 5340 llvm_unreachable("Invalid vector element type?"); 5341 5342 if (VT.getSizeInBits() == 64) 5343 OpLHS = WidenVector(OpLHS, DAG); 5344 SDValue Lane = DAG.getConstant(OpNum - OP_VDUP0, dl, MVT::i64); 5345 return DAG.getNode(Opcode, dl, VT, OpLHS, Lane); 5346 } 5347 case OP_VEXT1: 5348 case OP_VEXT2: 5349 case OP_VEXT3: { 5350 unsigned Imm = (OpNum - OP_VEXT1 + 1) * getExtFactor(OpLHS); 5351 return DAG.getNode(AArch64ISD::EXT, dl, VT, OpLHS, OpRHS, 5352 DAG.getConstant(Imm, dl, MVT::i32)); 5353 } 5354 case OP_VUZPL: 5355 return DAG.getNode(AArch64ISD::UZP1, dl, DAG.getVTList(VT, VT), OpLHS, 5356 OpRHS); 5357 case OP_VUZPR: 5358 return DAG.getNode(AArch64ISD::UZP2, dl, DAG.getVTList(VT, VT), OpLHS, 5359 OpRHS); 5360 case OP_VZIPL: 5361 return DAG.getNode(AArch64ISD::ZIP1, dl, DAG.getVTList(VT, VT), OpLHS, 5362 OpRHS); 5363 case OP_VZIPR: 5364 return DAG.getNode(AArch64ISD::ZIP2, dl, DAG.getVTList(VT, VT), OpLHS, 5365 OpRHS); 5366 case OP_VTRNL: 5367 return DAG.getNode(AArch64ISD::TRN1, dl, DAG.getVTList(VT, VT), OpLHS, 5368 OpRHS); 5369 case OP_VTRNR: 5370 return DAG.getNode(AArch64ISD::TRN2, dl, DAG.getVTList(VT, VT), OpLHS, 5371 OpRHS); 5372 } 5373 } 5374 5375 static SDValue GenerateTBL(SDValue Op, ArrayRef<int> ShuffleMask, 5376 SelectionDAG &DAG) { 5377 // Check to see if we can use the TBL instruction. 5378 SDValue V1 = Op.getOperand(0); 5379 SDValue V2 = Op.getOperand(1); 5380 SDLoc DL(Op); 5381 5382 EVT EltVT = Op.getValueType().getVectorElementType(); 5383 unsigned BytesPerElt = EltVT.getSizeInBits() / 8; 5384 5385 SmallVector<SDValue, 8> TBLMask; 5386 for (int Val : ShuffleMask) { 5387 for (unsigned Byte = 0; Byte < BytesPerElt; ++Byte) { 5388 unsigned Offset = Byte + Val * BytesPerElt; 5389 TBLMask.push_back(DAG.getConstant(Offset, DL, MVT::i32)); 5390 } 5391 } 5392 5393 MVT IndexVT = MVT::v8i8; 5394 unsigned IndexLen = 8; 5395 if (Op.getValueType().getSizeInBits() == 128) { 5396 IndexVT = MVT::v16i8; 5397 IndexLen = 16; 5398 } 5399 5400 SDValue V1Cst = DAG.getNode(ISD::BITCAST, DL, IndexVT, V1); 5401 SDValue V2Cst = DAG.getNode(ISD::BITCAST, DL, IndexVT, V2); 5402 5403 SDValue Shuffle; 5404 if (V2.getNode()->getOpcode() == ISD::UNDEF) { 5405 if (IndexLen == 8) 5406 V1Cst = DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v16i8, V1Cst, V1Cst); 5407 Shuffle = DAG.getNode( 5408 ISD::INTRINSIC_WO_CHAIN, DL, IndexVT, 5409 DAG.getConstant(Intrinsic::aarch64_neon_tbl1, DL, MVT::i32), V1Cst, 5410 DAG.getNode(ISD::BUILD_VECTOR, DL, IndexVT, 5411 makeArrayRef(TBLMask.data(), IndexLen))); 5412 } else { 5413 if (IndexLen == 8) { 5414 V1Cst = DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v16i8, V1Cst, V2Cst); 5415 Shuffle = DAG.getNode( 5416 ISD::INTRINSIC_WO_CHAIN, DL, IndexVT, 5417 DAG.getConstant(Intrinsic::aarch64_neon_tbl1, DL, MVT::i32), V1Cst, 5418 DAG.getNode(ISD::BUILD_VECTOR, DL, IndexVT, 5419 makeArrayRef(TBLMask.data(), IndexLen))); 5420 } else { 5421 // FIXME: We cannot, for the moment, emit a TBL2 instruction because we 5422 // cannot currently represent the register constraints on the input 5423 // table registers. 5424 // Shuffle = DAG.getNode(AArch64ISD::TBL2, DL, IndexVT, V1Cst, V2Cst, 5425 // DAG.getNode(ISD::BUILD_VECTOR, DL, IndexVT, 5426 // &TBLMask[0], IndexLen)); 5427 Shuffle = DAG.getNode( 5428 ISD::INTRINSIC_WO_CHAIN, DL, IndexVT, 5429 DAG.getConstant(Intrinsic::aarch64_neon_tbl2, DL, MVT::i32), 5430 V1Cst, V2Cst, 5431 DAG.getNode(ISD::BUILD_VECTOR, DL, IndexVT, 5432 makeArrayRef(TBLMask.data(), IndexLen))); 5433 } 5434 } 5435 return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Shuffle); 5436 } 5437 5438 static unsigned getDUPLANEOp(EVT EltType) { 5439 if (EltType == MVT::i8) 5440 return AArch64ISD::DUPLANE8; 5441 if (EltType == MVT::i16 || EltType == MVT::f16) 5442 return AArch64ISD::DUPLANE16; 5443 if (EltType == MVT::i32 || EltType == MVT::f32) 5444 return AArch64ISD::DUPLANE32; 5445 if (EltType == MVT::i64 || EltType == MVT::f64) 5446 return AArch64ISD::DUPLANE64; 5447 5448 llvm_unreachable("Invalid vector element type?"); 5449 } 5450 5451 SDValue AArch64TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, 5452 SelectionDAG &DAG) const { 5453 SDLoc dl(Op); 5454 EVT VT = Op.getValueType(); 5455 5456 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode()); 5457 5458 // Convert shuffles that are directly supported on NEON to target-specific 5459 // DAG nodes, instead of keeping them as shuffles and matching them again 5460 // during code selection. This is more efficient and avoids the possibility 5461 // of inconsistencies between legalization and selection. 5462 ArrayRef<int> ShuffleMask = SVN->getMask(); 5463 5464 SDValue V1 = Op.getOperand(0); 5465 SDValue V2 = Op.getOperand(1); 5466 5467 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], 5468 V1.getValueType().getSimpleVT())) { 5469 int Lane = SVN->getSplatIndex(); 5470 // If this is undef splat, generate it via "just" vdup, if possible. 5471 if (Lane == -1) 5472 Lane = 0; 5473 5474 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) 5475 return DAG.getNode(AArch64ISD::DUP, dl, V1.getValueType(), 5476 V1.getOperand(0)); 5477 // Test if V1 is a BUILD_VECTOR and the lane being referenced is a non- 5478 // constant. If so, we can just reference the lane's definition directly. 5479 if (V1.getOpcode() == ISD::BUILD_VECTOR && 5480 !isa<ConstantSDNode>(V1.getOperand(Lane))) 5481 return DAG.getNode(AArch64ISD::DUP, dl, VT, V1.getOperand(Lane)); 5482 5483 // Otherwise, duplicate from the lane of the input vector. 5484 unsigned Opcode = getDUPLANEOp(V1.getValueType().getVectorElementType()); 5485 5486 // SelectionDAGBuilder may have "helpfully" already extracted or conatenated 5487 // to make a vector of the same size as this SHUFFLE. We can ignore the 5488 // extract entirely, and canonicalise the concat using WidenVector. 5489 if (V1.getOpcode() == ISD::EXTRACT_SUBVECTOR) { 5490 Lane += cast<ConstantSDNode>(V1.getOperand(1))->getZExtValue(); 5491 V1 = V1.getOperand(0); 5492 } else if (V1.getOpcode() == ISD::CONCAT_VECTORS) { 5493 unsigned Idx = Lane >= (int)VT.getVectorNumElements() / 2; 5494 Lane -= Idx * VT.getVectorNumElements() / 2; 5495 V1 = WidenVector(V1.getOperand(Idx), DAG); 5496 } else if (VT.getSizeInBits() == 64) 5497 V1 = WidenVector(V1, DAG); 5498 5499 return DAG.getNode(Opcode, dl, VT, V1, DAG.getConstant(Lane, dl, MVT::i64)); 5500 } 5501 5502 if (isREVMask(ShuffleMask, VT, 64)) 5503 return DAG.getNode(AArch64ISD::REV64, dl, V1.getValueType(), V1, V2); 5504 if (isREVMask(ShuffleMask, VT, 32)) 5505 return DAG.getNode(AArch64ISD::REV32, dl, V1.getValueType(), V1, V2); 5506 if (isREVMask(ShuffleMask, VT, 16)) 5507 return DAG.getNode(AArch64ISD::REV16, dl, V1.getValueType(), V1, V2); 5508 5509 bool ReverseEXT = false; 5510 unsigned Imm; 5511 if (isEXTMask(ShuffleMask, VT, ReverseEXT, Imm)) { 5512 if (ReverseEXT) 5513 std::swap(V1, V2); 5514 Imm *= getExtFactor(V1); 5515 return DAG.getNode(AArch64ISD::EXT, dl, V1.getValueType(), V1, V2, 5516 DAG.getConstant(Imm, dl, MVT::i32)); 5517 } else if (V2->getOpcode() == ISD::UNDEF && 5518 isSingletonEXTMask(ShuffleMask, VT, Imm)) { 5519 Imm *= getExtFactor(V1); 5520 return DAG.getNode(AArch64ISD::EXT, dl, V1.getValueType(), V1, V1, 5521 DAG.getConstant(Imm, dl, MVT::i32)); 5522 } 5523 5524 unsigned WhichResult; 5525 if (isZIPMask(ShuffleMask, VT, WhichResult)) { 5526 unsigned Opc = (WhichResult == 0) ? AArch64ISD::ZIP1 : AArch64ISD::ZIP2; 5527 return DAG.getNode(Opc, dl, V1.getValueType(), V1, V2); 5528 } 5529 if (isUZPMask(ShuffleMask, VT, WhichResult)) { 5530 unsigned Opc = (WhichResult == 0) ? AArch64ISD::UZP1 : AArch64ISD::UZP2; 5531 return DAG.getNode(Opc, dl, V1.getValueType(), V1, V2); 5532 } 5533 if (isTRNMask(ShuffleMask, VT, WhichResult)) { 5534 unsigned Opc = (WhichResult == 0) ? AArch64ISD::TRN1 : AArch64ISD::TRN2; 5535 return DAG.getNode(Opc, dl, V1.getValueType(), V1, V2); 5536 } 5537 5538 if (isZIP_v_undef_Mask(ShuffleMask, VT, WhichResult)) { 5539 unsigned Opc = (WhichResult == 0) ? AArch64ISD::ZIP1 : AArch64ISD::ZIP2; 5540 return DAG.getNode(Opc, dl, V1.getValueType(), V1, V1); 5541 } 5542 if (isUZP_v_undef_Mask(ShuffleMask, VT, WhichResult)) { 5543 unsigned Opc = (WhichResult == 0) ? AArch64ISD::UZP1 : AArch64ISD::UZP2; 5544 return DAG.getNode(Opc, dl, V1.getValueType(), V1, V1); 5545 } 5546 if (isTRN_v_undef_Mask(ShuffleMask, VT, WhichResult)) { 5547 unsigned Opc = (WhichResult == 0) ? AArch64ISD::TRN1 : AArch64ISD::TRN2; 5548 return DAG.getNode(Opc, dl, V1.getValueType(), V1, V1); 5549 } 5550 5551 SDValue Concat = tryFormConcatFromShuffle(Op, DAG); 5552 if (Concat.getNode()) 5553 return Concat; 5554 5555 bool DstIsLeft; 5556 int Anomaly; 5557 int NumInputElements = V1.getValueType().getVectorNumElements(); 5558 if (isINSMask(ShuffleMask, NumInputElements, DstIsLeft, Anomaly)) { 5559 SDValue DstVec = DstIsLeft ? V1 : V2; 5560 SDValue DstLaneV = DAG.getConstant(Anomaly, dl, MVT::i64); 5561 5562 SDValue SrcVec = V1; 5563 int SrcLane = ShuffleMask[Anomaly]; 5564 if (SrcLane >= NumInputElements) { 5565 SrcVec = V2; 5566 SrcLane -= VT.getVectorNumElements(); 5567 } 5568 SDValue SrcLaneV = DAG.getConstant(SrcLane, dl, MVT::i64); 5569 5570 EVT ScalarVT = VT.getVectorElementType(); 5571 5572 if (ScalarVT.getSizeInBits() < 32 && ScalarVT.isInteger()) 5573 ScalarVT = MVT::i32; 5574 5575 return DAG.getNode( 5576 ISD::INSERT_VECTOR_ELT, dl, VT, DstVec, 5577 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ScalarVT, SrcVec, SrcLaneV), 5578 DstLaneV); 5579 } 5580 5581 // If the shuffle is not directly supported and it has 4 elements, use 5582 // the PerfectShuffle-generated table to synthesize it from other shuffles. 5583 unsigned NumElts = VT.getVectorNumElements(); 5584 if (NumElts == 4) { 5585 unsigned PFIndexes[4]; 5586 for (unsigned i = 0; i != 4; ++i) { 5587 if (ShuffleMask[i] < 0) 5588 PFIndexes[i] = 8; 5589 else 5590 PFIndexes[i] = ShuffleMask[i]; 5591 } 5592 5593 // Compute the index in the perfect shuffle table. 5594 unsigned PFTableIndex = PFIndexes[0] * 9 * 9 * 9 + PFIndexes[1] * 9 * 9 + 5595 PFIndexes[2] * 9 + PFIndexes[3]; 5596 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 5597 unsigned Cost = (PFEntry >> 30); 5598 5599 if (Cost <= 4) 5600 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); 5601 } 5602 5603 return GenerateTBL(Op, ShuffleMask, DAG); 5604 } 5605 5606 static bool resolveBuildVector(BuildVectorSDNode *BVN, APInt &CnstBits, 5607 APInt &UndefBits) { 5608 EVT VT = BVN->getValueType(0); 5609 APInt SplatBits, SplatUndef; 5610 unsigned SplatBitSize; 5611 bool HasAnyUndefs; 5612 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 5613 unsigned NumSplats = VT.getSizeInBits() / SplatBitSize; 5614 5615 for (unsigned i = 0; i < NumSplats; ++i) { 5616 CnstBits <<= SplatBitSize; 5617 UndefBits <<= SplatBitSize; 5618 CnstBits |= SplatBits.zextOrTrunc(VT.getSizeInBits()); 5619 UndefBits |= (SplatBits ^ SplatUndef).zextOrTrunc(VT.getSizeInBits()); 5620 } 5621 5622 return true; 5623 } 5624 5625 return false; 5626 } 5627 5628 SDValue AArch64TargetLowering::LowerVectorAND(SDValue Op, 5629 SelectionDAG &DAG) const { 5630 BuildVectorSDNode *BVN = 5631 dyn_cast<BuildVectorSDNode>(Op.getOperand(1).getNode()); 5632 SDValue LHS = Op.getOperand(0); 5633 SDLoc dl(Op); 5634 EVT VT = Op.getValueType(); 5635 5636 if (!BVN) 5637 return Op; 5638 5639 APInt CnstBits(VT.getSizeInBits(), 0); 5640 APInt UndefBits(VT.getSizeInBits(), 0); 5641 if (resolveBuildVector(BVN, CnstBits, UndefBits)) { 5642 // We only have BIC vector immediate instruction, which is and-not. 5643 CnstBits = ~CnstBits; 5644 5645 // We make use of a little bit of goto ickiness in order to avoid having to 5646 // duplicate the immediate matching logic for the undef toggled case. 5647 bool SecondTry = false; 5648 AttemptModImm: 5649 5650 if (CnstBits.getHiBits(64) == CnstBits.getLoBits(64)) { 5651 CnstBits = CnstBits.zextOrTrunc(64); 5652 uint64_t CnstVal = CnstBits.getZExtValue(); 5653 5654 if (AArch64_AM::isAdvSIMDModImmType1(CnstVal)) { 5655 CnstVal = AArch64_AM::encodeAdvSIMDModImmType1(CnstVal); 5656 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 5657 SDValue Mov = DAG.getNode(AArch64ISD::BICi, dl, MovTy, LHS, 5658 DAG.getConstant(CnstVal, dl, MVT::i32), 5659 DAG.getConstant(0, dl, MVT::i32)); 5660 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 5661 } 5662 5663 if (AArch64_AM::isAdvSIMDModImmType2(CnstVal)) { 5664 CnstVal = AArch64_AM::encodeAdvSIMDModImmType2(CnstVal); 5665 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 5666 SDValue Mov = DAG.getNode(AArch64ISD::BICi, dl, MovTy, LHS, 5667 DAG.getConstant(CnstVal, dl, MVT::i32), 5668 DAG.getConstant(8, dl, MVT::i32)); 5669 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 5670 } 5671 5672 if (AArch64_AM::isAdvSIMDModImmType3(CnstVal)) { 5673 CnstVal = AArch64_AM::encodeAdvSIMDModImmType3(CnstVal); 5674 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 5675 SDValue Mov = DAG.getNode(AArch64ISD::BICi, dl, MovTy, LHS, 5676 DAG.getConstant(CnstVal, dl, MVT::i32), 5677 DAG.getConstant(16, dl, MVT::i32)); 5678 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 5679 } 5680 5681 if (AArch64_AM::isAdvSIMDModImmType4(CnstVal)) { 5682 CnstVal = AArch64_AM::encodeAdvSIMDModImmType4(CnstVal); 5683 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 5684 SDValue Mov = DAG.getNode(AArch64ISD::BICi, dl, MovTy, LHS, 5685 DAG.getConstant(CnstVal, dl, MVT::i32), 5686 DAG.getConstant(24, dl, MVT::i32)); 5687 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 5688 } 5689 5690 if (AArch64_AM::isAdvSIMDModImmType5(CnstVal)) { 5691 CnstVal = AArch64_AM::encodeAdvSIMDModImmType5(CnstVal); 5692 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v8i16 : MVT::v4i16; 5693 SDValue Mov = DAG.getNode(AArch64ISD::BICi, dl, MovTy, LHS, 5694 DAG.getConstant(CnstVal, dl, MVT::i32), 5695 DAG.getConstant(0, dl, MVT::i32)); 5696 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 5697 } 5698 5699 if (AArch64_AM::isAdvSIMDModImmType6(CnstVal)) { 5700 CnstVal = AArch64_AM::encodeAdvSIMDModImmType6(CnstVal); 5701 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v8i16 : MVT::v4i16; 5702 SDValue Mov = DAG.getNode(AArch64ISD::BICi, dl, MovTy, LHS, 5703 DAG.getConstant(CnstVal, dl, MVT::i32), 5704 DAG.getConstant(8, dl, MVT::i32)); 5705 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 5706 } 5707 } 5708 5709 if (SecondTry) 5710 goto FailedModImm; 5711 SecondTry = true; 5712 CnstBits = ~UndefBits; 5713 goto AttemptModImm; 5714 } 5715 5716 // We can always fall back to a non-immediate AND. 5717 FailedModImm: 5718 return Op; 5719 } 5720 5721 // Specialized code to quickly find if PotentialBVec is a BuildVector that 5722 // consists of only the same constant int value, returned in reference arg 5723 // ConstVal 5724 static bool isAllConstantBuildVector(const SDValue &PotentialBVec, 5725 uint64_t &ConstVal) { 5726 BuildVectorSDNode *Bvec = dyn_cast<BuildVectorSDNode>(PotentialBVec); 5727 if (!Bvec) 5728 return false; 5729 ConstantSDNode *FirstElt = dyn_cast<ConstantSDNode>(Bvec->getOperand(0)); 5730 if (!FirstElt) 5731 return false; 5732 EVT VT = Bvec->getValueType(0); 5733 unsigned NumElts = VT.getVectorNumElements(); 5734 for (unsigned i = 1; i < NumElts; ++i) 5735 if (dyn_cast<ConstantSDNode>(Bvec->getOperand(i)) != FirstElt) 5736 return false; 5737 ConstVal = FirstElt->getZExtValue(); 5738 return true; 5739 } 5740 5741 static unsigned getIntrinsicID(const SDNode *N) { 5742 unsigned Opcode = N->getOpcode(); 5743 switch (Opcode) { 5744 default: 5745 return Intrinsic::not_intrinsic; 5746 case ISD::INTRINSIC_WO_CHAIN: { 5747 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 5748 if (IID < Intrinsic::num_intrinsics) 5749 return IID; 5750 return Intrinsic::not_intrinsic; 5751 } 5752 } 5753 } 5754 5755 // Attempt to form a vector S[LR]I from (or (and X, BvecC1), (lsl Y, C2)), 5756 // to (SLI X, Y, C2), where X and Y have matching vector types, BvecC1 is a 5757 // BUILD_VECTORs with constant element C1, C2 is a constant, and C1 == ~C2. 5758 // Also, logical shift right -> sri, with the same structure. 5759 static SDValue tryLowerToSLI(SDNode *N, SelectionDAG &DAG) { 5760 EVT VT = N->getValueType(0); 5761 5762 if (!VT.isVector()) 5763 return SDValue(); 5764 5765 SDLoc DL(N); 5766 5767 // Is the first op an AND? 5768 const SDValue And = N->getOperand(0); 5769 if (And.getOpcode() != ISD::AND) 5770 return SDValue(); 5771 5772 // Is the second op an shl or lshr? 5773 SDValue Shift = N->getOperand(1); 5774 // This will have been turned into: AArch64ISD::VSHL vector, #shift 5775 // or AArch64ISD::VLSHR vector, #shift 5776 unsigned ShiftOpc = Shift.getOpcode(); 5777 if ((ShiftOpc != AArch64ISD::VSHL && ShiftOpc != AArch64ISD::VLSHR)) 5778 return SDValue(); 5779 bool IsShiftRight = ShiftOpc == AArch64ISD::VLSHR; 5780 5781 // Is the shift amount constant? 5782 ConstantSDNode *C2node = dyn_cast<ConstantSDNode>(Shift.getOperand(1)); 5783 if (!C2node) 5784 return SDValue(); 5785 5786 // Is the and mask vector all constant? 5787 uint64_t C1; 5788 if (!isAllConstantBuildVector(And.getOperand(1), C1)) 5789 return SDValue(); 5790 5791 // Is C1 == ~C2, taking into account how much one can shift elements of a 5792 // particular size? 5793 uint64_t C2 = C2node->getZExtValue(); 5794 unsigned ElemSizeInBits = VT.getVectorElementType().getSizeInBits(); 5795 if (C2 > ElemSizeInBits) 5796 return SDValue(); 5797 unsigned ElemMask = (1 << ElemSizeInBits) - 1; 5798 if ((C1 & ElemMask) != (~C2 & ElemMask)) 5799 return SDValue(); 5800 5801 SDValue X = And.getOperand(0); 5802 SDValue Y = Shift.getOperand(0); 5803 5804 unsigned Intrin = 5805 IsShiftRight ? Intrinsic::aarch64_neon_vsri : Intrinsic::aarch64_neon_vsli; 5806 SDValue ResultSLI = 5807 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, 5808 DAG.getConstant(Intrin, DL, MVT::i32), X, Y, 5809 Shift.getOperand(1)); 5810 5811 DEBUG(dbgs() << "aarch64-lower: transformed: \n"); 5812 DEBUG(N->dump(&DAG)); 5813 DEBUG(dbgs() << "into: \n"); 5814 DEBUG(ResultSLI->dump(&DAG)); 5815 5816 ++NumShiftInserts; 5817 return ResultSLI; 5818 } 5819 5820 SDValue AArch64TargetLowering::LowerVectorOR(SDValue Op, 5821 SelectionDAG &DAG) const { 5822 // Attempt to form a vector S[LR]I from (or (and X, C1), (lsl Y, C2)) 5823 if (EnableAArch64SlrGeneration) { 5824 SDValue Res = tryLowerToSLI(Op.getNode(), DAG); 5825 if (Res.getNode()) 5826 return Res; 5827 } 5828 5829 BuildVectorSDNode *BVN = 5830 dyn_cast<BuildVectorSDNode>(Op.getOperand(0).getNode()); 5831 SDValue LHS = Op.getOperand(1); 5832 SDLoc dl(Op); 5833 EVT VT = Op.getValueType(); 5834 5835 // OR commutes, so try swapping the operands. 5836 if (!BVN) { 5837 LHS = Op.getOperand(0); 5838 BVN = dyn_cast<BuildVectorSDNode>(Op.getOperand(1).getNode()); 5839 } 5840 if (!BVN) 5841 return Op; 5842 5843 APInt CnstBits(VT.getSizeInBits(), 0); 5844 APInt UndefBits(VT.getSizeInBits(), 0); 5845 if (resolveBuildVector(BVN, CnstBits, UndefBits)) { 5846 // We make use of a little bit of goto ickiness in order to avoid having to 5847 // duplicate the immediate matching logic for the undef toggled case. 5848 bool SecondTry = false; 5849 AttemptModImm: 5850 5851 if (CnstBits.getHiBits(64) == CnstBits.getLoBits(64)) { 5852 CnstBits = CnstBits.zextOrTrunc(64); 5853 uint64_t CnstVal = CnstBits.getZExtValue(); 5854 5855 if (AArch64_AM::isAdvSIMDModImmType1(CnstVal)) { 5856 CnstVal = AArch64_AM::encodeAdvSIMDModImmType1(CnstVal); 5857 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 5858 SDValue Mov = DAG.getNode(AArch64ISD::ORRi, dl, MovTy, LHS, 5859 DAG.getConstant(CnstVal, dl, MVT::i32), 5860 DAG.getConstant(0, dl, MVT::i32)); 5861 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 5862 } 5863 5864 if (AArch64_AM::isAdvSIMDModImmType2(CnstVal)) { 5865 CnstVal = AArch64_AM::encodeAdvSIMDModImmType2(CnstVal); 5866 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 5867 SDValue Mov = DAG.getNode(AArch64ISD::ORRi, dl, MovTy, LHS, 5868 DAG.getConstant(CnstVal, dl, MVT::i32), 5869 DAG.getConstant(8, dl, MVT::i32)); 5870 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 5871 } 5872 5873 if (AArch64_AM::isAdvSIMDModImmType3(CnstVal)) { 5874 CnstVal = AArch64_AM::encodeAdvSIMDModImmType3(CnstVal); 5875 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 5876 SDValue Mov = DAG.getNode(AArch64ISD::ORRi, dl, MovTy, LHS, 5877 DAG.getConstant(CnstVal, dl, MVT::i32), 5878 DAG.getConstant(16, dl, MVT::i32)); 5879 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 5880 } 5881 5882 if (AArch64_AM::isAdvSIMDModImmType4(CnstVal)) { 5883 CnstVal = AArch64_AM::encodeAdvSIMDModImmType4(CnstVal); 5884 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 5885 SDValue Mov = DAG.getNode(AArch64ISD::ORRi, dl, MovTy, LHS, 5886 DAG.getConstant(CnstVal, dl, MVT::i32), 5887 DAG.getConstant(24, dl, MVT::i32)); 5888 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 5889 } 5890 5891 if (AArch64_AM::isAdvSIMDModImmType5(CnstVal)) { 5892 CnstVal = AArch64_AM::encodeAdvSIMDModImmType5(CnstVal); 5893 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v8i16 : MVT::v4i16; 5894 SDValue Mov = DAG.getNode(AArch64ISD::ORRi, dl, MovTy, LHS, 5895 DAG.getConstant(CnstVal, dl, MVT::i32), 5896 DAG.getConstant(0, dl, MVT::i32)); 5897 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 5898 } 5899 5900 if (AArch64_AM::isAdvSIMDModImmType6(CnstVal)) { 5901 CnstVal = AArch64_AM::encodeAdvSIMDModImmType6(CnstVal); 5902 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v8i16 : MVT::v4i16; 5903 SDValue Mov = DAG.getNode(AArch64ISD::ORRi, dl, MovTy, LHS, 5904 DAG.getConstant(CnstVal, dl, MVT::i32), 5905 DAG.getConstant(8, dl, MVT::i32)); 5906 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 5907 } 5908 } 5909 5910 if (SecondTry) 5911 goto FailedModImm; 5912 SecondTry = true; 5913 CnstBits = UndefBits; 5914 goto AttemptModImm; 5915 } 5916 5917 // We can always fall back to a non-immediate OR. 5918 FailedModImm: 5919 return Op; 5920 } 5921 5922 // Normalize the operands of BUILD_VECTOR. The value of constant operands will 5923 // be truncated to fit element width. 5924 static SDValue NormalizeBuildVector(SDValue Op, 5925 SelectionDAG &DAG) { 5926 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!"); 5927 SDLoc dl(Op); 5928 EVT VT = Op.getValueType(); 5929 EVT EltTy= VT.getVectorElementType(); 5930 5931 if (EltTy.isFloatingPoint() || EltTy.getSizeInBits() > 16) 5932 return Op; 5933 5934 SmallVector<SDValue, 16> Ops; 5935 for (SDValue Lane : Op->ops()) { 5936 if (auto *CstLane = dyn_cast<ConstantSDNode>(Lane)) { 5937 APInt LowBits(EltTy.getSizeInBits(), 5938 CstLane->getZExtValue()); 5939 Lane = DAG.getConstant(LowBits.getZExtValue(), dl, MVT::i32); 5940 } 5941 Ops.push_back(Lane); 5942 } 5943 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops); 5944 } 5945 5946 SDValue AArch64TargetLowering::LowerBUILD_VECTOR(SDValue Op, 5947 SelectionDAG &DAG) const { 5948 SDLoc dl(Op); 5949 EVT VT = Op.getValueType(); 5950 Op = NormalizeBuildVector(Op, DAG); 5951 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode()); 5952 5953 APInt CnstBits(VT.getSizeInBits(), 0); 5954 APInt UndefBits(VT.getSizeInBits(), 0); 5955 if (resolveBuildVector(BVN, CnstBits, UndefBits)) { 5956 // We make use of a little bit of goto ickiness in order to avoid having to 5957 // duplicate the immediate matching logic for the undef toggled case. 5958 bool SecondTry = false; 5959 AttemptModImm: 5960 5961 if (CnstBits.getHiBits(64) == CnstBits.getLoBits(64)) { 5962 CnstBits = CnstBits.zextOrTrunc(64); 5963 uint64_t CnstVal = CnstBits.getZExtValue(); 5964 5965 // Certain magic vector constants (used to express things like NOT 5966 // and NEG) are passed through unmodified. This allows codegen patterns 5967 // for these operations to match. Special-purpose patterns will lower 5968 // these immediates to MOVIs if it proves necessary. 5969 if (VT.isInteger() && (CnstVal == 0 || CnstVal == ~0ULL)) 5970 return Op; 5971 5972 // The many faces of MOVI... 5973 if (AArch64_AM::isAdvSIMDModImmType10(CnstVal)) { 5974 CnstVal = AArch64_AM::encodeAdvSIMDModImmType10(CnstVal); 5975 if (VT.getSizeInBits() == 128) { 5976 SDValue Mov = DAG.getNode(AArch64ISD::MOVIedit, dl, MVT::v2i64, 5977 DAG.getConstant(CnstVal, dl, MVT::i32)); 5978 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 5979 } 5980 5981 // Support the V64 version via subregister insertion. 5982 SDValue Mov = DAG.getNode(AArch64ISD::MOVIedit, dl, MVT::f64, 5983 DAG.getConstant(CnstVal, dl, MVT::i32)); 5984 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 5985 } 5986 5987 if (AArch64_AM::isAdvSIMDModImmType1(CnstVal)) { 5988 CnstVal = AArch64_AM::encodeAdvSIMDModImmType1(CnstVal); 5989 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 5990 SDValue Mov = DAG.getNode(AArch64ISD::MOVIshift, dl, MovTy, 5991 DAG.getConstant(CnstVal, dl, MVT::i32), 5992 DAG.getConstant(0, dl, MVT::i32)); 5993 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 5994 } 5995 5996 if (AArch64_AM::isAdvSIMDModImmType2(CnstVal)) { 5997 CnstVal = AArch64_AM::encodeAdvSIMDModImmType2(CnstVal); 5998 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 5999 SDValue Mov = DAG.getNode(AArch64ISD::MOVIshift, dl, MovTy, 6000 DAG.getConstant(CnstVal, dl, MVT::i32), 6001 DAG.getConstant(8, dl, MVT::i32)); 6002 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6003 } 6004 6005 if (AArch64_AM::isAdvSIMDModImmType3(CnstVal)) { 6006 CnstVal = AArch64_AM::encodeAdvSIMDModImmType3(CnstVal); 6007 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 6008 SDValue Mov = DAG.getNode(AArch64ISD::MOVIshift, dl, MovTy, 6009 DAG.getConstant(CnstVal, dl, MVT::i32), 6010 DAG.getConstant(16, dl, MVT::i32)); 6011 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6012 } 6013 6014 if (AArch64_AM::isAdvSIMDModImmType4(CnstVal)) { 6015 CnstVal = AArch64_AM::encodeAdvSIMDModImmType4(CnstVal); 6016 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 6017 SDValue Mov = DAG.getNode(AArch64ISD::MOVIshift, dl, MovTy, 6018 DAG.getConstant(CnstVal, dl, MVT::i32), 6019 DAG.getConstant(24, dl, MVT::i32)); 6020 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6021 } 6022 6023 if (AArch64_AM::isAdvSIMDModImmType5(CnstVal)) { 6024 CnstVal = AArch64_AM::encodeAdvSIMDModImmType5(CnstVal); 6025 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v8i16 : MVT::v4i16; 6026 SDValue Mov = DAG.getNode(AArch64ISD::MOVIshift, dl, MovTy, 6027 DAG.getConstant(CnstVal, dl, MVT::i32), 6028 DAG.getConstant(0, dl, MVT::i32)); 6029 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6030 } 6031 6032 if (AArch64_AM::isAdvSIMDModImmType6(CnstVal)) { 6033 CnstVal = AArch64_AM::encodeAdvSIMDModImmType6(CnstVal); 6034 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v8i16 : MVT::v4i16; 6035 SDValue Mov = DAG.getNode(AArch64ISD::MOVIshift, dl, MovTy, 6036 DAG.getConstant(CnstVal, dl, MVT::i32), 6037 DAG.getConstant(8, dl, MVT::i32)); 6038 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6039 } 6040 6041 if (AArch64_AM::isAdvSIMDModImmType7(CnstVal)) { 6042 CnstVal = AArch64_AM::encodeAdvSIMDModImmType7(CnstVal); 6043 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 6044 SDValue Mov = DAG.getNode(AArch64ISD::MOVImsl, dl, MovTy, 6045 DAG.getConstant(CnstVal, dl, MVT::i32), 6046 DAG.getConstant(264, dl, MVT::i32)); 6047 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6048 } 6049 6050 if (AArch64_AM::isAdvSIMDModImmType8(CnstVal)) { 6051 CnstVal = AArch64_AM::encodeAdvSIMDModImmType8(CnstVal); 6052 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 6053 SDValue Mov = DAG.getNode(AArch64ISD::MOVImsl, dl, MovTy, 6054 DAG.getConstant(CnstVal, dl, MVT::i32), 6055 DAG.getConstant(272, dl, MVT::i32)); 6056 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6057 } 6058 6059 if (AArch64_AM::isAdvSIMDModImmType9(CnstVal)) { 6060 CnstVal = AArch64_AM::encodeAdvSIMDModImmType9(CnstVal); 6061 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v16i8 : MVT::v8i8; 6062 SDValue Mov = DAG.getNode(AArch64ISD::MOVI, dl, MovTy, 6063 DAG.getConstant(CnstVal, dl, MVT::i32)); 6064 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6065 } 6066 6067 // The few faces of FMOV... 6068 if (AArch64_AM::isAdvSIMDModImmType11(CnstVal)) { 6069 CnstVal = AArch64_AM::encodeAdvSIMDModImmType11(CnstVal); 6070 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4f32 : MVT::v2f32; 6071 SDValue Mov = DAG.getNode(AArch64ISD::FMOV, dl, MovTy, 6072 DAG.getConstant(CnstVal, dl, MVT::i32)); 6073 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6074 } 6075 6076 if (AArch64_AM::isAdvSIMDModImmType12(CnstVal) && 6077 VT.getSizeInBits() == 128) { 6078 CnstVal = AArch64_AM::encodeAdvSIMDModImmType12(CnstVal); 6079 SDValue Mov = DAG.getNode(AArch64ISD::FMOV, dl, MVT::v2f64, 6080 DAG.getConstant(CnstVal, dl, MVT::i32)); 6081 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6082 } 6083 6084 // The many faces of MVNI... 6085 CnstVal = ~CnstVal; 6086 if (AArch64_AM::isAdvSIMDModImmType1(CnstVal)) { 6087 CnstVal = AArch64_AM::encodeAdvSIMDModImmType1(CnstVal); 6088 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 6089 SDValue Mov = DAG.getNode(AArch64ISD::MVNIshift, dl, MovTy, 6090 DAG.getConstant(CnstVal, dl, MVT::i32), 6091 DAG.getConstant(0, dl, MVT::i32)); 6092 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6093 } 6094 6095 if (AArch64_AM::isAdvSIMDModImmType2(CnstVal)) { 6096 CnstVal = AArch64_AM::encodeAdvSIMDModImmType2(CnstVal); 6097 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 6098 SDValue Mov = DAG.getNode(AArch64ISD::MVNIshift, dl, MovTy, 6099 DAG.getConstant(CnstVal, dl, MVT::i32), 6100 DAG.getConstant(8, dl, MVT::i32)); 6101 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6102 } 6103 6104 if (AArch64_AM::isAdvSIMDModImmType3(CnstVal)) { 6105 CnstVal = AArch64_AM::encodeAdvSIMDModImmType3(CnstVal); 6106 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 6107 SDValue Mov = DAG.getNode(AArch64ISD::MVNIshift, dl, MovTy, 6108 DAG.getConstant(CnstVal, dl, MVT::i32), 6109 DAG.getConstant(16, dl, MVT::i32)); 6110 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6111 } 6112 6113 if (AArch64_AM::isAdvSIMDModImmType4(CnstVal)) { 6114 CnstVal = AArch64_AM::encodeAdvSIMDModImmType4(CnstVal); 6115 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 6116 SDValue Mov = DAG.getNode(AArch64ISD::MVNIshift, dl, MovTy, 6117 DAG.getConstant(CnstVal, dl, MVT::i32), 6118 DAG.getConstant(24, dl, MVT::i32)); 6119 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6120 } 6121 6122 if (AArch64_AM::isAdvSIMDModImmType5(CnstVal)) { 6123 CnstVal = AArch64_AM::encodeAdvSIMDModImmType5(CnstVal); 6124 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v8i16 : MVT::v4i16; 6125 SDValue Mov = DAG.getNode(AArch64ISD::MVNIshift, dl, MovTy, 6126 DAG.getConstant(CnstVal, dl, MVT::i32), 6127 DAG.getConstant(0, dl, MVT::i32)); 6128 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6129 } 6130 6131 if (AArch64_AM::isAdvSIMDModImmType6(CnstVal)) { 6132 CnstVal = AArch64_AM::encodeAdvSIMDModImmType6(CnstVal); 6133 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v8i16 : MVT::v4i16; 6134 SDValue Mov = DAG.getNode(AArch64ISD::MVNIshift, dl, MovTy, 6135 DAG.getConstant(CnstVal, dl, MVT::i32), 6136 DAG.getConstant(8, dl, MVT::i32)); 6137 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6138 } 6139 6140 if (AArch64_AM::isAdvSIMDModImmType7(CnstVal)) { 6141 CnstVal = AArch64_AM::encodeAdvSIMDModImmType7(CnstVal); 6142 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 6143 SDValue Mov = DAG.getNode(AArch64ISD::MVNImsl, dl, MovTy, 6144 DAG.getConstant(CnstVal, dl, MVT::i32), 6145 DAG.getConstant(264, dl, MVT::i32)); 6146 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6147 } 6148 6149 if (AArch64_AM::isAdvSIMDModImmType8(CnstVal)) { 6150 CnstVal = AArch64_AM::encodeAdvSIMDModImmType8(CnstVal); 6151 MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32; 6152 SDValue Mov = DAG.getNode(AArch64ISD::MVNImsl, dl, MovTy, 6153 DAG.getConstant(CnstVal, dl, MVT::i32), 6154 DAG.getConstant(272, dl, MVT::i32)); 6155 return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov); 6156 } 6157 } 6158 6159 if (SecondTry) 6160 goto FailedModImm; 6161 SecondTry = true; 6162 CnstBits = UndefBits; 6163 goto AttemptModImm; 6164 } 6165 FailedModImm: 6166 6167 // Scan through the operands to find some interesting properties we can 6168 // exploit: 6169 // 1) If only one value is used, we can use a DUP, or 6170 // 2) if only the low element is not undef, we can just insert that, or 6171 // 3) if only one constant value is used (w/ some non-constant lanes), 6172 // we can splat the constant value into the whole vector then fill 6173 // in the non-constant lanes. 6174 // 4) FIXME: If different constant values are used, but we can intelligently 6175 // select the values we'll be overwriting for the non-constant 6176 // lanes such that we can directly materialize the vector 6177 // some other way (MOVI, e.g.), we can be sneaky. 6178 unsigned NumElts = VT.getVectorNumElements(); 6179 bool isOnlyLowElement = true; 6180 bool usesOnlyOneValue = true; 6181 bool usesOnlyOneConstantValue = true; 6182 bool isConstant = true; 6183 unsigned NumConstantLanes = 0; 6184 SDValue Value; 6185 SDValue ConstantValue; 6186 for (unsigned i = 0; i < NumElts; ++i) { 6187 SDValue V = Op.getOperand(i); 6188 if (V.getOpcode() == ISD::UNDEF) 6189 continue; 6190 if (i > 0) 6191 isOnlyLowElement = false; 6192 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 6193 isConstant = false; 6194 6195 if (isa<ConstantSDNode>(V) || isa<ConstantFPSDNode>(V)) { 6196 ++NumConstantLanes; 6197 if (!ConstantValue.getNode()) 6198 ConstantValue = V; 6199 else if (ConstantValue != V) 6200 usesOnlyOneConstantValue = false; 6201 } 6202 6203 if (!Value.getNode()) 6204 Value = V; 6205 else if (V != Value) 6206 usesOnlyOneValue = false; 6207 } 6208 6209 if (!Value.getNode()) 6210 return DAG.getUNDEF(VT); 6211 6212 if (isOnlyLowElement) 6213 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value); 6214 6215 // Use DUP for non-constant splats. For f32 constant splats, reduce to 6216 // i32 and try again. 6217 if (usesOnlyOneValue) { 6218 if (!isConstant) { 6219 if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT || 6220 Value.getValueType() != VT) 6221 return DAG.getNode(AArch64ISD::DUP, dl, VT, Value); 6222 6223 // This is actually a DUPLANExx operation, which keeps everything vectory. 6224 6225 // DUPLANE works on 128-bit vectors, widen it if necessary. 6226 SDValue Lane = Value.getOperand(1); 6227 Value = Value.getOperand(0); 6228 if (Value.getValueType().getSizeInBits() == 64) 6229 Value = WidenVector(Value, DAG); 6230 6231 unsigned Opcode = getDUPLANEOp(VT.getVectorElementType()); 6232 return DAG.getNode(Opcode, dl, VT, Value, Lane); 6233 } 6234 6235 if (VT.getVectorElementType().isFloatingPoint()) { 6236 SmallVector<SDValue, 8> Ops; 6237 EVT EltTy = VT.getVectorElementType(); 6238 assert ((EltTy == MVT::f16 || EltTy == MVT::f32 || EltTy == MVT::f64) && 6239 "Unsupported floating-point vector type"); 6240 MVT NewType = MVT::getIntegerVT(EltTy.getSizeInBits()); 6241 for (unsigned i = 0; i < NumElts; ++i) 6242 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, NewType, Op.getOperand(i))); 6243 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), NewType, NumElts); 6244 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops); 6245 Val = LowerBUILD_VECTOR(Val, DAG); 6246 if (Val.getNode()) 6247 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 6248 } 6249 } 6250 6251 // If there was only one constant value used and for more than one lane, 6252 // start by splatting that value, then replace the non-constant lanes. This 6253 // is better than the default, which will perform a separate initialization 6254 // for each lane. 6255 if (NumConstantLanes > 0 && usesOnlyOneConstantValue) { 6256 SDValue Val = DAG.getNode(AArch64ISD::DUP, dl, VT, ConstantValue); 6257 // Now insert the non-constant lanes. 6258 for (unsigned i = 0; i < NumElts; ++i) { 6259 SDValue V = Op.getOperand(i); 6260 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i64); 6261 if (!isa<ConstantSDNode>(V) && !isa<ConstantFPSDNode>(V)) { 6262 // Note that type legalization likely mucked about with the VT of the 6263 // source operand, so we may have to convert it here before inserting. 6264 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, V, LaneIdx); 6265 } 6266 } 6267 return Val; 6268 } 6269 6270 // If all elements are constants and the case above didn't get hit, fall back 6271 // to the default expansion, which will generate a load from the constant 6272 // pool. 6273 if (isConstant) 6274 return SDValue(); 6275 6276 // Empirical tests suggest this is rarely worth it for vectors of length <= 2. 6277 if (NumElts >= 4) { 6278 if (SDValue shuffle = ReconstructShuffle(Op, DAG)) 6279 return shuffle; 6280 } 6281 6282 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we 6283 // know the default expansion would otherwise fall back on something even 6284 // worse. For a vector with one or two non-undef values, that's 6285 // scalar_to_vector for the elements followed by a shuffle (provided the 6286 // shuffle is valid for the target) and materialization element by element 6287 // on the stack followed by a load for everything else. 6288 if (!isConstant && !usesOnlyOneValue) { 6289 SDValue Vec = DAG.getUNDEF(VT); 6290 SDValue Op0 = Op.getOperand(0); 6291 unsigned ElemSize = VT.getVectorElementType().getSizeInBits(); 6292 unsigned i = 0; 6293 // For 32 and 64 bit types, use INSERT_SUBREG for lane zero to 6294 // a) Avoid a RMW dependency on the full vector register, and 6295 // b) Allow the register coalescer to fold away the copy if the 6296 // value is already in an S or D register. 6297 // Do not do this for UNDEF/LOAD nodes because we have better patterns 6298 // for those avoiding the SCALAR_TO_VECTOR/BUILD_VECTOR. 6299 if (Op0.getOpcode() != ISD::UNDEF && Op0.getOpcode() != ISD::LOAD && 6300 (ElemSize == 32 || ElemSize == 64)) { 6301 unsigned SubIdx = ElemSize == 32 ? AArch64::ssub : AArch64::dsub; 6302 MachineSDNode *N = 6303 DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl, VT, Vec, Op0, 6304 DAG.getTargetConstant(SubIdx, dl, MVT::i32)); 6305 Vec = SDValue(N, 0); 6306 ++i; 6307 } 6308 for (; i < NumElts; ++i) { 6309 SDValue V = Op.getOperand(i); 6310 if (V.getOpcode() == ISD::UNDEF) 6311 continue; 6312 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i64); 6313 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx); 6314 } 6315 return Vec; 6316 } 6317 6318 // Just use the default expansion. We failed to find a better alternative. 6319 return SDValue(); 6320 } 6321 6322 SDValue AArch64TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, 6323 SelectionDAG &DAG) const { 6324 assert(Op.getOpcode() == ISD::INSERT_VECTOR_ELT && "Unknown opcode!"); 6325 6326 // Check for non-constant or out of range lane. 6327 EVT VT = Op.getOperand(0).getValueType(); 6328 ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 6329 if (!CI || CI->getZExtValue() >= VT.getVectorNumElements()) 6330 return SDValue(); 6331 6332 6333 // Insertion/extraction are legal for V128 types. 6334 if (VT == MVT::v16i8 || VT == MVT::v8i16 || VT == MVT::v4i32 || 6335 VT == MVT::v2i64 || VT == MVT::v4f32 || VT == MVT::v2f64 || 6336 VT == MVT::v8f16) 6337 return Op; 6338 6339 if (VT != MVT::v8i8 && VT != MVT::v4i16 && VT != MVT::v2i32 && 6340 VT != MVT::v1i64 && VT != MVT::v2f32 && VT != MVT::v4f16) 6341 return SDValue(); 6342 6343 // For V64 types, we perform insertion by expanding the value 6344 // to a V128 type and perform the insertion on that. 6345 SDLoc DL(Op); 6346 SDValue WideVec = WidenVector(Op.getOperand(0), DAG); 6347 EVT WideTy = WideVec.getValueType(); 6348 6349 SDValue Node = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, WideTy, WideVec, 6350 Op.getOperand(1), Op.getOperand(2)); 6351 // Re-narrow the resultant vector. 6352 return NarrowVector(Node, DAG); 6353 } 6354 6355 SDValue 6356 AArch64TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, 6357 SelectionDAG &DAG) const { 6358 assert(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT && "Unknown opcode!"); 6359 6360 // Check for non-constant or out of range lane. 6361 EVT VT = Op.getOperand(0).getValueType(); 6362 ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Op.getOperand(1)); 6363 if (!CI || CI->getZExtValue() >= VT.getVectorNumElements()) 6364 return SDValue(); 6365 6366 6367 // Insertion/extraction are legal for V128 types. 6368 if (VT == MVT::v16i8 || VT == MVT::v8i16 || VT == MVT::v4i32 || 6369 VT == MVT::v2i64 || VT == MVT::v4f32 || VT == MVT::v2f64 || 6370 VT == MVT::v8f16) 6371 return Op; 6372 6373 if (VT != MVT::v8i8 && VT != MVT::v4i16 && VT != MVT::v2i32 && 6374 VT != MVT::v1i64 && VT != MVT::v2f32 && VT != MVT::v4f16) 6375 return SDValue(); 6376 6377 // For V64 types, we perform extraction by expanding the value 6378 // to a V128 type and perform the extraction on that. 6379 SDLoc DL(Op); 6380 SDValue WideVec = WidenVector(Op.getOperand(0), DAG); 6381 EVT WideTy = WideVec.getValueType(); 6382 6383 EVT ExtrTy = WideTy.getVectorElementType(); 6384 if (ExtrTy == MVT::i16 || ExtrTy == MVT::i8) 6385 ExtrTy = MVT::i32; 6386 6387 // For extractions, we just return the result directly. 6388 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ExtrTy, WideVec, 6389 Op.getOperand(1)); 6390 } 6391 6392 SDValue AArch64TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, 6393 SelectionDAG &DAG) const { 6394 EVT VT = Op.getOperand(0).getValueType(); 6395 SDLoc dl(Op); 6396 // Just in case... 6397 if (!VT.isVector()) 6398 return SDValue(); 6399 6400 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op.getOperand(1)); 6401 if (!Cst) 6402 return SDValue(); 6403 unsigned Val = Cst->getZExtValue(); 6404 6405 unsigned Size = Op.getValueType().getSizeInBits(); 6406 6407 // This will get lowered to an appropriate EXTRACT_SUBREG in ISel. 6408 if (Val == 0) 6409 return Op; 6410 6411 // If this is extracting the upper 64-bits of a 128-bit vector, we match 6412 // that directly. 6413 if (Size == 64 && Val * VT.getVectorElementType().getSizeInBits() == 64) 6414 return Op; 6415 6416 return SDValue(); 6417 } 6418 6419 bool AArch64TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M, 6420 EVT VT) const { 6421 if (VT.getVectorNumElements() == 4 && 6422 (VT.is128BitVector() || VT.is64BitVector())) { 6423 unsigned PFIndexes[4]; 6424 for (unsigned i = 0; i != 4; ++i) { 6425 if (M[i] < 0) 6426 PFIndexes[i] = 8; 6427 else 6428 PFIndexes[i] = M[i]; 6429 } 6430 6431 // Compute the index in the perfect shuffle table. 6432 unsigned PFTableIndex = PFIndexes[0] * 9 * 9 * 9 + PFIndexes[1] * 9 * 9 + 6433 PFIndexes[2] * 9 + PFIndexes[3]; 6434 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 6435 unsigned Cost = (PFEntry >> 30); 6436 6437 if (Cost <= 4) 6438 return true; 6439 } 6440 6441 bool DummyBool; 6442 int DummyInt; 6443 unsigned DummyUnsigned; 6444 6445 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) || isREVMask(M, VT, 64) || 6446 isREVMask(M, VT, 32) || isREVMask(M, VT, 16) || 6447 isEXTMask(M, VT, DummyBool, DummyUnsigned) || 6448 // isTBLMask(M, VT) || // FIXME: Port TBL support from ARM. 6449 isTRNMask(M, VT, DummyUnsigned) || isUZPMask(M, VT, DummyUnsigned) || 6450 isZIPMask(M, VT, DummyUnsigned) || 6451 isTRN_v_undef_Mask(M, VT, DummyUnsigned) || 6452 isUZP_v_undef_Mask(M, VT, DummyUnsigned) || 6453 isZIP_v_undef_Mask(M, VT, DummyUnsigned) || 6454 isINSMask(M, VT.getVectorNumElements(), DummyBool, DummyInt) || 6455 isConcatMask(M, VT, VT.getSizeInBits() == 128)); 6456 } 6457 6458 /// getVShiftImm - Check if this is a valid build_vector for the immediate 6459 /// operand of a vector shift operation, where all the elements of the 6460 /// build_vector must have the same constant integer value. 6461 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) { 6462 // Ignore bit_converts. 6463 while (Op.getOpcode() == ISD::BITCAST) 6464 Op = Op.getOperand(0); 6465 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); 6466 APInt SplatBits, SplatUndef; 6467 unsigned SplatBitSize; 6468 bool HasAnyUndefs; 6469 if (!BVN || !BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, 6470 HasAnyUndefs, ElementBits) || 6471 SplatBitSize > ElementBits) 6472 return false; 6473 Cnt = SplatBits.getSExtValue(); 6474 return true; 6475 } 6476 6477 /// isVShiftLImm - Check if this is a valid build_vector for the immediate 6478 /// operand of a vector shift left operation. That value must be in the range: 6479 /// 0 <= Value < ElementBits for a left shift; or 6480 /// 0 <= Value <= ElementBits for a long left shift. 6481 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) { 6482 assert(VT.isVector() && "vector shift count is not a vector type"); 6483 int64_t ElementBits = VT.getVectorElementType().getSizeInBits(); 6484 if (!getVShiftImm(Op, ElementBits, Cnt)) 6485 return false; 6486 return (Cnt >= 0 && (isLong ? Cnt - 1 : Cnt) < ElementBits); 6487 } 6488 6489 /// isVShiftRImm - Check if this is a valid build_vector for the immediate 6490 /// operand of a vector shift right operation. The value must be in the range: 6491 /// 1 <= Value <= ElementBits for a right shift; or 6492 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, int64_t &Cnt) { 6493 assert(VT.isVector() && "vector shift count is not a vector type"); 6494 int64_t ElementBits = VT.getVectorElementType().getSizeInBits(); 6495 if (!getVShiftImm(Op, ElementBits, Cnt)) 6496 return false; 6497 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits / 2 : ElementBits)); 6498 } 6499 6500 SDValue AArch64TargetLowering::LowerVectorSRA_SRL_SHL(SDValue Op, 6501 SelectionDAG &DAG) const { 6502 EVT VT = Op.getValueType(); 6503 SDLoc DL(Op); 6504 int64_t Cnt; 6505 6506 if (!Op.getOperand(1).getValueType().isVector()) 6507 return Op; 6508 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 6509 6510 switch (Op.getOpcode()) { 6511 default: 6512 llvm_unreachable("unexpected shift opcode"); 6513 6514 case ISD::SHL: 6515 if (isVShiftLImm(Op.getOperand(1), VT, false, Cnt) && Cnt < EltSize) 6516 return DAG.getNode(AArch64ISD::VSHL, DL, VT, Op.getOperand(0), 6517 DAG.getConstant(Cnt, DL, MVT::i32)); 6518 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, 6519 DAG.getConstant(Intrinsic::aarch64_neon_ushl, DL, 6520 MVT::i32), 6521 Op.getOperand(0), Op.getOperand(1)); 6522 case ISD::SRA: 6523 case ISD::SRL: 6524 // Right shift immediate 6525 if (isVShiftRImm(Op.getOperand(1), VT, false, Cnt) && Cnt < EltSize) { 6526 unsigned Opc = 6527 (Op.getOpcode() == ISD::SRA) ? AArch64ISD::VASHR : AArch64ISD::VLSHR; 6528 return DAG.getNode(Opc, DL, VT, Op.getOperand(0), 6529 DAG.getConstant(Cnt, DL, MVT::i32)); 6530 } 6531 6532 // Right shift register. Note, there is not a shift right register 6533 // instruction, but the shift left register instruction takes a signed 6534 // value, where negative numbers specify a right shift. 6535 unsigned Opc = (Op.getOpcode() == ISD::SRA) ? Intrinsic::aarch64_neon_sshl 6536 : Intrinsic::aarch64_neon_ushl; 6537 // negate the shift amount 6538 SDValue NegShift = DAG.getNode(AArch64ISD::NEG, DL, VT, Op.getOperand(1)); 6539 SDValue NegShiftLeft = 6540 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, 6541 DAG.getConstant(Opc, DL, MVT::i32), Op.getOperand(0), 6542 NegShift); 6543 return NegShiftLeft; 6544 } 6545 6546 return SDValue(); 6547 } 6548 6549 static SDValue EmitVectorComparison(SDValue LHS, SDValue RHS, 6550 AArch64CC::CondCode CC, bool NoNans, EVT VT, 6551 SDLoc dl, SelectionDAG &DAG) { 6552 EVT SrcVT = LHS.getValueType(); 6553 assert(VT.getSizeInBits() == SrcVT.getSizeInBits() && 6554 "function only supposed to emit natural comparisons"); 6555 6556 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(RHS.getNode()); 6557 APInt CnstBits(VT.getSizeInBits(), 0); 6558 APInt UndefBits(VT.getSizeInBits(), 0); 6559 bool IsCnst = BVN && resolveBuildVector(BVN, CnstBits, UndefBits); 6560 bool IsZero = IsCnst && (CnstBits == 0); 6561 6562 if (SrcVT.getVectorElementType().isFloatingPoint()) { 6563 switch (CC) { 6564 default: 6565 return SDValue(); 6566 case AArch64CC::NE: { 6567 SDValue Fcmeq; 6568 if (IsZero) 6569 Fcmeq = DAG.getNode(AArch64ISD::FCMEQz, dl, VT, LHS); 6570 else 6571 Fcmeq = DAG.getNode(AArch64ISD::FCMEQ, dl, VT, LHS, RHS); 6572 return DAG.getNode(AArch64ISD::NOT, dl, VT, Fcmeq); 6573 } 6574 case AArch64CC::EQ: 6575 if (IsZero) 6576 return DAG.getNode(AArch64ISD::FCMEQz, dl, VT, LHS); 6577 return DAG.getNode(AArch64ISD::FCMEQ, dl, VT, LHS, RHS); 6578 case AArch64CC::GE: 6579 if (IsZero) 6580 return DAG.getNode(AArch64ISD::FCMGEz, dl, VT, LHS); 6581 return DAG.getNode(AArch64ISD::FCMGE, dl, VT, LHS, RHS); 6582 case AArch64CC::GT: 6583 if (IsZero) 6584 return DAG.getNode(AArch64ISD::FCMGTz, dl, VT, LHS); 6585 return DAG.getNode(AArch64ISD::FCMGT, dl, VT, LHS, RHS); 6586 case AArch64CC::LS: 6587 if (IsZero) 6588 return DAG.getNode(AArch64ISD::FCMLEz, dl, VT, LHS); 6589 return DAG.getNode(AArch64ISD::FCMGE, dl, VT, RHS, LHS); 6590 case AArch64CC::LT: 6591 if (!NoNans) 6592 return SDValue(); 6593 // If we ignore NaNs then we can use to the MI implementation. 6594 // Fallthrough. 6595 case AArch64CC::MI: 6596 if (IsZero) 6597 return DAG.getNode(AArch64ISD::FCMLTz, dl, VT, LHS); 6598 return DAG.getNode(AArch64ISD::FCMGT, dl, VT, RHS, LHS); 6599 } 6600 } 6601 6602 switch (CC) { 6603 default: 6604 return SDValue(); 6605 case AArch64CC::NE: { 6606 SDValue Cmeq; 6607 if (IsZero) 6608 Cmeq = DAG.getNode(AArch64ISD::CMEQz, dl, VT, LHS); 6609 else 6610 Cmeq = DAG.getNode(AArch64ISD::CMEQ, dl, VT, LHS, RHS); 6611 return DAG.getNode(AArch64ISD::NOT, dl, VT, Cmeq); 6612 } 6613 case AArch64CC::EQ: 6614 if (IsZero) 6615 return DAG.getNode(AArch64ISD::CMEQz, dl, VT, LHS); 6616 return DAG.getNode(AArch64ISD::CMEQ, dl, VT, LHS, RHS); 6617 case AArch64CC::GE: 6618 if (IsZero) 6619 return DAG.getNode(AArch64ISD::CMGEz, dl, VT, LHS); 6620 return DAG.getNode(AArch64ISD::CMGE, dl, VT, LHS, RHS); 6621 case AArch64CC::GT: 6622 if (IsZero) 6623 return DAG.getNode(AArch64ISD::CMGTz, dl, VT, LHS); 6624 return DAG.getNode(AArch64ISD::CMGT, dl, VT, LHS, RHS); 6625 case AArch64CC::LE: 6626 if (IsZero) 6627 return DAG.getNode(AArch64ISD::CMLEz, dl, VT, LHS); 6628 return DAG.getNode(AArch64ISD::CMGE, dl, VT, RHS, LHS); 6629 case AArch64CC::LS: 6630 return DAG.getNode(AArch64ISD::CMHS, dl, VT, RHS, LHS); 6631 case AArch64CC::LO: 6632 return DAG.getNode(AArch64ISD::CMHI, dl, VT, RHS, LHS); 6633 case AArch64CC::LT: 6634 if (IsZero) 6635 return DAG.getNode(AArch64ISD::CMLTz, dl, VT, LHS); 6636 return DAG.getNode(AArch64ISD::CMGT, dl, VT, RHS, LHS); 6637 case AArch64CC::HI: 6638 return DAG.getNode(AArch64ISD::CMHI, dl, VT, LHS, RHS); 6639 case AArch64CC::HS: 6640 return DAG.getNode(AArch64ISD::CMHS, dl, VT, LHS, RHS); 6641 } 6642 } 6643 6644 SDValue AArch64TargetLowering::LowerVSETCC(SDValue Op, 6645 SelectionDAG &DAG) const { 6646 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 6647 SDValue LHS = Op.getOperand(0); 6648 SDValue RHS = Op.getOperand(1); 6649 EVT CmpVT = LHS.getValueType().changeVectorElementTypeToInteger(); 6650 SDLoc dl(Op); 6651 6652 if (LHS.getValueType().getVectorElementType().isInteger()) { 6653 assert(LHS.getValueType() == RHS.getValueType()); 6654 AArch64CC::CondCode AArch64CC = changeIntCCToAArch64CC(CC); 6655 SDValue Cmp = 6656 EmitVectorComparison(LHS, RHS, AArch64CC, false, CmpVT, dl, DAG); 6657 return DAG.getSExtOrTrunc(Cmp, dl, Op.getValueType()); 6658 } 6659 6660 assert(LHS.getValueType().getVectorElementType() == MVT::f32 || 6661 LHS.getValueType().getVectorElementType() == MVT::f64); 6662 6663 // Unfortunately, the mapping of LLVM FP CC's onto AArch64 CC's isn't totally 6664 // clean. Some of them require two branches to implement. 6665 AArch64CC::CondCode CC1, CC2; 6666 bool ShouldInvert; 6667 changeVectorFPCCToAArch64CC(CC, CC1, CC2, ShouldInvert); 6668 6669 bool NoNaNs = getTargetMachine().Options.NoNaNsFPMath; 6670 SDValue Cmp = 6671 EmitVectorComparison(LHS, RHS, CC1, NoNaNs, CmpVT, dl, DAG); 6672 if (!Cmp.getNode()) 6673 return SDValue(); 6674 6675 if (CC2 != AArch64CC::AL) { 6676 SDValue Cmp2 = 6677 EmitVectorComparison(LHS, RHS, CC2, NoNaNs, CmpVT, dl, DAG); 6678 if (!Cmp2.getNode()) 6679 return SDValue(); 6680 6681 Cmp = DAG.getNode(ISD::OR, dl, CmpVT, Cmp, Cmp2); 6682 } 6683 6684 Cmp = DAG.getSExtOrTrunc(Cmp, dl, Op.getValueType()); 6685 6686 if (ShouldInvert) 6687 return Cmp = DAG.getNOT(dl, Cmp, Cmp.getValueType()); 6688 6689 return Cmp; 6690 } 6691 6692 /// getTgtMemIntrinsic - Represent NEON load and store intrinsics as 6693 /// MemIntrinsicNodes. The associated MachineMemOperands record the alignment 6694 /// specified in the intrinsic calls. 6695 bool AArch64TargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 6696 const CallInst &I, 6697 unsigned Intrinsic) const { 6698 auto &DL = I.getModule()->getDataLayout(); 6699 switch (Intrinsic) { 6700 case Intrinsic::aarch64_neon_ld2: 6701 case Intrinsic::aarch64_neon_ld3: 6702 case Intrinsic::aarch64_neon_ld4: 6703 case Intrinsic::aarch64_neon_ld1x2: 6704 case Intrinsic::aarch64_neon_ld1x3: 6705 case Intrinsic::aarch64_neon_ld1x4: 6706 case Intrinsic::aarch64_neon_ld2lane: 6707 case Intrinsic::aarch64_neon_ld3lane: 6708 case Intrinsic::aarch64_neon_ld4lane: 6709 case Intrinsic::aarch64_neon_ld2r: 6710 case Intrinsic::aarch64_neon_ld3r: 6711 case Intrinsic::aarch64_neon_ld4r: { 6712 Info.opc = ISD::INTRINSIC_W_CHAIN; 6713 // Conservatively set memVT to the entire set of vectors loaded. 6714 uint64_t NumElts = DL.getTypeAllocSize(I.getType()) / 8; 6715 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 6716 Info.ptrVal = I.getArgOperand(I.getNumArgOperands() - 1); 6717 Info.offset = 0; 6718 Info.align = 0; 6719 Info.vol = false; // volatile loads with NEON intrinsics not supported 6720 Info.readMem = true; 6721 Info.writeMem = false; 6722 return true; 6723 } 6724 case Intrinsic::aarch64_neon_st2: 6725 case Intrinsic::aarch64_neon_st3: 6726 case Intrinsic::aarch64_neon_st4: 6727 case Intrinsic::aarch64_neon_st1x2: 6728 case Intrinsic::aarch64_neon_st1x3: 6729 case Intrinsic::aarch64_neon_st1x4: 6730 case Intrinsic::aarch64_neon_st2lane: 6731 case Intrinsic::aarch64_neon_st3lane: 6732 case Intrinsic::aarch64_neon_st4lane: { 6733 Info.opc = ISD::INTRINSIC_VOID; 6734 // Conservatively set memVT to the entire set of vectors stored. 6735 unsigned NumElts = 0; 6736 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) { 6737 Type *ArgTy = I.getArgOperand(ArgI)->getType(); 6738 if (!ArgTy->isVectorTy()) 6739 break; 6740 NumElts += DL.getTypeAllocSize(ArgTy) / 8; 6741 } 6742 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 6743 Info.ptrVal = I.getArgOperand(I.getNumArgOperands() - 1); 6744 Info.offset = 0; 6745 Info.align = 0; 6746 Info.vol = false; // volatile stores with NEON intrinsics not supported 6747 Info.readMem = false; 6748 Info.writeMem = true; 6749 return true; 6750 } 6751 case Intrinsic::aarch64_ldaxr: 6752 case Intrinsic::aarch64_ldxr: { 6753 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType()); 6754 Info.opc = ISD::INTRINSIC_W_CHAIN; 6755 Info.memVT = MVT::getVT(PtrTy->getElementType()); 6756 Info.ptrVal = I.getArgOperand(0); 6757 Info.offset = 0; 6758 Info.align = DL.getABITypeAlignment(PtrTy->getElementType()); 6759 Info.vol = true; 6760 Info.readMem = true; 6761 Info.writeMem = false; 6762 return true; 6763 } 6764 case Intrinsic::aarch64_stlxr: 6765 case Intrinsic::aarch64_stxr: { 6766 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType()); 6767 Info.opc = ISD::INTRINSIC_W_CHAIN; 6768 Info.memVT = MVT::getVT(PtrTy->getElementType()); 6769 Info.ptrVal = I.getArgOperand(1); 6770 Info.offset = 0; 6771 Info.align = DL.getABITypeAlignment(PtrTy->getElementType()); 6772 Info.vol = true; 6773 Info.readMem = false; 6774 Info.writeMem = true; 6775 return true; 6776 } 6777 case Intrinsic::aarch64_ldaxp: 6778 case Intrinsic::aarch64_ldxp: { 6779 Info.opc = ISD::INTRINSIC_W_CHAIN; 6780 Info.memVT = MVT::i128; 6781 Info.ptrVal = I.getArgOperand(0); 6782 Info.offset = 0; 6783 Info.align = 16; 6784 Info.vol = true; 6785 Info.readMem = true; 6786 Info.writeMem = false; 6787 return true; 6788 } 6789 case Intrinsic::aarch64_stlxp: 6790 case Intrinsic::aarch64_stxp: { 6791 Info.opc = ISD::INTRINSIC_W_CHAIN; 6792 Info.memVT = MVT::i128; 6793 Info.ptrVal = I.getArgOperand(2); 6794 Info.offset = 0; 6795 Info.align = 16; 6796 Info.vol = true; 6797 Info.readMem = false; 6798 Info.writeMem = true; 6799 return true; 6800 } 6801 default: 6802 break; 6803 } 6804 6805 return false; 6806 } 6807 6808 // Truncations from 64-bit GPR to 32-bit GPR is free. 6809 bool AArch64TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const { 6810 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) 6811 return false; 6812 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits(); 6813 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits(); 6814 return NumBits1 > NumBits2; 6815 } 6816 bool AArch64TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const { 6817 if (VT1.isVector() || VT2.isVector() || !VT1.isInteger() || !VT2.isInteger()) 6818 return false; 6819 unsigned NumBits1 = VT1.getSizeInBits(); 6820 unsigned NumBits2 = VT2.getSizeInBits(); 6821 return NumBits1 > NumBits2; 6822 } 6823 6824 /// Check if it is profitable to hoist instruction in then/else to if. 6825 /// Not profitable if I and it's user can form a FMA instruction 6826 /// because we prefer FMSUB/FMADD. 6827 bool AArch64TargetLowering::isProfitableToHoist(Instruction *I) const { 6828 if (I->getOpcode() != Instruction::FMul) 6829 return true; 6830 6831 if (I->getNumUses() != 1) 6832 return true; 6833 6834 Instruction *User = I->user_back(); 6835 6836 if (User && 6837 !(User->getOpcode() == Instruction::FSub || 6838 User->getOpcode() == Instruction::FAdd)) 6839 return true; 6840 6841 const TargetOptions &Options = getTargetMachine().Options; 6842 const DataLayout &DL = I->getModule()->getDataLayout(); 6843 EVT VT = getValueType(DL, User->getOperand(0)->getType()); 6844 6845 if (isFMAFasterThanFMulAndFAdd(VT) && 6846 isOperationLegalOrCustom(ISD::FMA, VT) && 6847 (Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath)) 6848 return false; 6849 6850 return true; 6851 } 6852 6853 // All 32-bit GPR operations implicitly zero the high-half of the corresponding 6854 // 64-bit GPR. 6855 bool AArch64TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const { 6856 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) 6857 return false; 6858 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits(); 6859 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits(); 6860 return NumBits1 == 32 && NumBits2 == 64; 6861 } 6862 bool AArch64TargetLowering::isZExtFree(EVT VT1, EVT VT2) const { 6863 if (VT1.isVector() || VT2.isVector() || !VT1.isInteger() || !VT2.isInteger()) 6864 return false; 6865 unsigned NumBits1 = VT1.getSizeInBits(); 6866 unsigned NumBits2 = VT2.getSizeInBits(); 6867 return NumBits1 == 32 && NumBits2 == 64; 6868 } 6869 6870 bool AArch64TargetLowering::isZExtFree(SDValue Val, EVT VT2) const { 6871 EVT VT1 = Val.getValueType(); 6872 if (isZExtFree(VT1, VT2)) { 6873 return true; 6874 } 6875 6876 if (Val.getOpcode() != ISD::LOAD) 6877 return false; 6878 6879 // 8-, 16-, and 32-bit integer loads all implicitly zero-extend. 6880 return (VT1.isSimple() && !VT1.isVector() && VT1.isInteger() && 6881 VT2.isSimple() && !VT2.isVector() && VT2.isInteger() && 6882 VT1.getSizeInBits() <= 32); 6883 } 6884 6885 bool AArch64TargetLowering::isExtFreeImpl(const Instruction *Ext) const { 6886 if (isa<FPExtInst>(Ext)) 6887 return false; 6888 6889 // Vector types are next free. 6890 if (Ext->getType()->isVectorTy()) 6891 return false; 6892 6893 for (const Use &U : Ext->uses()) { 6894 // The extension is free if we can fold it with a left shift in an 6895 // addressing mode or an arithmetic operation: add, sub, and cmp. 6896 6897 // Is there a shift? 6898 const Instruction *Instr = cast<Instruction>(U.getUser()); 6899 6900 // Is this a constant shift? 6901 switch (Instr->getOpcode()) { 6902 case Instruction::Shl: 6903 if (!isa<ConstantInt>(Instr->getOperand(1))) 6904 return false; 6905 break; 6906 case Instruction::GetElementPtr: { 6907 gep_type_iterator GTI = gep_type_begin(Instr); 6908 auto &DL = Ext->getModule()->getDataLayout(); 6909 std::advance(GTI, U.getOperandNo()); 6910 Type *IdxTy = *GTI; 6911 // This extension will end up with a shift because of the scaling factor. 6912 // 8-bit sized types have a scaling factor of 1, thus a shift amount of 0. 6913 // Get the shift amount based on the scaling factor: 6914 // log2(sizeof(IdxTy)) - log2(8). 6915 uint64_t ShiftAmt = 6916 countTrailingZeros(DL.getTypeStoreSizeInBits(IdxTy)) - 3; 6917 // Is the constant foldable in the shift of the addressing mode? 6918 // I.e., shift amount is between 1 and 4 inclusive. 6919 if (ShiftAmt == 0 || ShiftAmt > 4) 6920 return false; 6921 break; 6922 } 6923 case Instruction::Trunc: 6924 // Check if this is a noop. 6925 // trunc(sext ty1 to ty2) to ty1. 6926 if (Instr->getType() == Ext->getOperand(0)->getType()) 6927 continue; 6928 // FALL THROUGH. 6929 default: 6930 return false; 6931 } 6932 6933 // At this point we can use the bfm family, so this extension is free 6934 // for that use. 6935 } 6936 return true; 6937 } 6938 6939 bool AArch64TargetLowering::hasPairedLoad(Type *LoadedType, 6940 unsigned &RequiredAligment) const { 6941 if (!LoadedType->isIntegerTy() && !LoadedType->isFloatTy()) 6942 return false; 6943 // Cyclone supports unaligned accesses. 6944 RequiredAligment = 0; 6945 unsigned NumBits = LoadedType->getPrimitiveSizeInBits(); 6946 return NumBits == 32 || NumBits == 64; 6947 } 6948 6949 bool AArch64TargetLowering::hasPairedLoad(EVT LoadedType, 6950 unsigned &RequiredAligment) const { 6951 if (!LoadedType.isSimple() || 6952 (!LoadedType.isInteger() && !LoadedType.isFloatingPoint())) 6953 return false; 6954 // Cyclone supports unaligned accesses. 6955 RequiredAligment = 0; 6956 unsigned NumBits = LoadedType.getSizeInBits(); 6957 return NumBits == 32 || NumBits == 64; 6958 } 6959 6960 /// \brief Lower an interleaved load into a ldN intrinsic. 6961 /// 6962 /// E.g. Lower an interleaved load (Factor = 2): 6963 /// %wide.vec = load <8 x i32>, <8 x i32>* %ptr 6964 /// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements 6965 /// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements 6966 /// 6967 /// Into: 6968 /// %ld2 = { <4 x i32>, <4 x i32> } call llvm.aarch64.neon.ld2(%ptr) 6969 /// %vec0 = extractelement { <4 x i32>, <4 x i32> } %ld2, i32 0 6970 /// %vec1 = extractelement { <4 x i32>, <4 x i32> } %ld2, i32 1 6971 bool AArch64TargetLowering::lowerInterleavedLoad( 6972 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles, 6973 ArrayRef<unsigned> Indices, unsigned Factor) const { 6974 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() && 6975 "Invalid interleave factor"); 6976 assert(!Shuffles.empty() && "Empty shufflevector input"); 6977 assert(Shuffles.size() == Indices.size() && 6978 "Unmatched number of shufflevectors and indices"); 6979 6980 const DataLayout &DL = LI->getModule()->getDataLayout(); 6981 6982 VectorType *VecTy = Shuffles[0]->getType(); 6983 unsigned VecSize = DL.getTypeAllocSizeInBits(VecTy); 6984 6985 // Skip if we do not have NEON and skip illegal vector types. 6986 if (!Subtarget->hasNEON() || (VecSize != 64 && VecSize != 128)) 6987 return false; 6988 6989 // A pointer vector can not be the return type of the ldN intrinsics. Need to 6990 // load integer vectors first and then convert to pointer vectors. 6991 Type *EltTy = VecTy->getVectorElementType(); 6992 if (EltTy->isPointerTy()) 6993 VecTy = 6994 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements()); 6995 6996 Type *PtrTy = VecTy->getPointerTo(LI->getPointerAddressSpace()); 6997 Type *Tys[2] = {VecTy, PtrTy}; 6998 static const Intrinsic::ID LoadInts[3] = {Intrinsic::aarch64_neon_ld2, 6999 Intrinsic::aarch64_neon_ld3, 7000 Intrinsic::aarch64_neon_ld4}; 7001 Function *LdNFunc = 7002 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys); 7003 7004 IRBuilder<> Builder(LI); 7005 Value *Ptr = Builder.CreateBitCast(LI->getPointerOperand(), PtrTy); 7006 7007 CallInst *LdN = Builder.CreateCall(LdNFunc, Ptr, "ldN"); 7008 7009 // Replace uses of each shufflevector with the corresponding vector loaded 7010 // by ldN. 7011 for (unsigned i = 0; i < Shuffles.size(); i++) { 7012 ShuffleVectorInst *SVI = Shuffles[i]; 7013 unsigned Index = Indices[i]; 7014 7015 Value *SubVec = Builder.CreateExtractValue(LdN, Index); 7016 7017 // Convert the integer vector to pointer vector if the element is pointer. 7018 if (EltTy->isPointerTy()) 7019 SubVec = Builder.CreateIntToPtr(SubVec, SVI->getType()); 7020 7021 SVI->replaceAllUsesWith(SubVec); 7022 } 7023 7024 return true; 7025 } 7026 7027 /// \brief Get a mask consisting of sequential integers starting from \p Start. 7028 /// 7029 /// I.e. <Start, Start + 1, ..., Start + NumElts - 1> 7030 static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start, 7031 unsigned NumElts) { 7032 SmallVector<Constant *, 16> Mask; 7033 for (unsigned i = 0; i < NumElts; i++) 7034 Mask.push_back(Builder.getInt32(Start + i)); 7035 7036 return ConstantVector::get(Mask); 7037 } 7038 7039 /// \brief Lower an interleaved store into a stN intrinsic. 7040 /// 7041 /// E.g. Lower an interleaved store (Factor = 3): 7042 /// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1, 7043 /// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11> 7044 /// store <12 x i32> %i.vec, <12 x i32>* %ptr 7045 /// 7046 /// Into: 7047 /// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3> 7048 /// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7> 7049 /// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11> 7050 /// call void llvm.aarch64.neon.st3(%sub.v0, %sub.v1, %sub.v2, %ptr) 7051 /// 7052 /// Note that the new shufflevectors will be removed and we'll only generate one 7053 /// st3 instruction in CodeGen. 7054 bool AArch64TargetLowering::lowerInterleavedStore(StoreInst *SI, 7055 ShuffleVectorInst *SVI, 7056 unsigned Factor) const { 7057 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() && 7058 "Invalid interleave factor"); 7059 7060 VectorType *VecTy = SVI->getType(); 7061 assert(VecTy->getVectorNumElements() % Factor == 0 && 7062 "Invalid interleaved store"); 7063 7064 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor; 7065 Type *EltTy = VecTy->getVectorElementType(); 7066 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts); 7067 7068 const DataLayout &DL = SI->getModule()->getDataLayout(); 7069 unsigned SubVecSize = DL.getTypeAllocSizeInBits(SubVecTy); 7070 7071 // Skip if we do not have NEON and skip illegal vector types. 7072 if (!Subtarget->hasNEON() || (SubVecSize != 64 && SubVecSize != 128)) 7073 return false; 7074 7075 Value *Op0 = SVI->getOperand(0); 7076 Value *Op1 = SVI->getOperand(1); 7077 IRBuilder<> Builder(SI); 7078 7079 // StN intrinsics don't support pointer vectors as arguments. Convert pointer 7080 // vectors to integer vectors. 7081 if (EltTy->isPointerTy()) { 7082 Type *IntTy = DL.getIntPtrType(EltTy); 7083 unsigned NumOpElts = 7084 dyn_cast<VectorType>(Op0->getType())->getVectorNumElements(); 7085 7086 // Convert to the corresponding integer vector. 7087 Type *IntVecTy = VectorType::get(IntTy, NumOpElts); 7088 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy); 7089 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy); 7090 7091 SubVecTy = VectorType::get(IntTy, NumSubElts); 7092 } 7093 7094 Type *PtrTy = SubVecTy->getPointerTo(SI->getPointerAddressSpace()); 7095 Type *Tys[2] = {SubVecTy, PtrTy}; 7096 static const Intrinsic::ID StoreInts[3] = {Intrinsic::aarch64_neon_st2, 7097 Intrinsic::aarch64_neon_st3, 7098 Intrinsic::aarch64_neon_st4}; 7099 Function *StNFunc = 7100 Intrinsic::getDeclaration(SI->getModule(), StoreInts[Factor - 2], Tys); 7101 7102 SmallVector<Value *, 5> Ops; 7103 7104 // Split the shufflevector operands into sub vectors for the new stN call. 7105 for (unsigned i = 0; i < Factor; i++) 7106 Ops.push_back(Builder.CreateShuffleVector( 7107 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts))); 7108 7109 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), PtrTy)); 7110 Builder.CreateCall(StNFunc, Ops); 7111 return true; 7112 } 7113 7114 static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign, 7115 unsigned AlignCheck) { 7116 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) && 7117 (DstAlign == 0 || DstAlign % AlignCheck == 0)); 7118 } 7119 7120 EVT AArch64TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign, 7121 unsigned SrcAlign, bool IsMemset, 7122 bool ZeroMemset, 7123 bool MemcpyStrSrc, 7124 MachineFunction &MF) const { 7125 // Don't use AdvSIMD to implement 16-byte memset. It would have taken one 7126 // instruction to materialize the v2i64 zero and one store (with restrictive 7127 // addressing mode). Just do two i64 store of zero-registers. 7128 bool Fast; 7129 const Function *F = MF.getFunction(); 7130 if (Subtarget->hasFPARMv8() && !IsMemset && Size >= 16 && 7131 !F->hasFnAttribute(Attribute::NoImplicitFloat) && 7132 (memOpAlign(SrcAlign, DstAlign, 16) || 7133 (allowsMisalignedMemoryAccesses(MVT::f128, 0, 1, &Fast) && Fast))) 7134 return MVT::f128; 7135 7136 if (Size >= 8 && 7137 (memOpAlign(SrcAlign, DstAlign, 8) || 7138 (allowsMisalignedMemoryAccesses(MVT::i64, 0, 1, &Fast) && Fast))) 7139 return MVT::i64; 7140 7141 if (Size >= 4 && 7142 (memOpAlign(SrcAlign, DstAlign, 4) || 7143 (allowsMisalignedMemoryAccesses(MVT::i32, 0, 1, &Fast) && Fast))) 7144 return MVT::i32; 7145 7146 return MVT::Other; 7147 } 7148 7149 // 12-bit optionally shifted immediates are legal for adds. 7150 bool AArch64TargetLowering::isLegalAddImmediate(int64_t Immed) const { 7151 if ((Immed >> 12) == 0 || ((Immed & 0xfff) == 0 && Immed >> 24 == 0)) 7152 return true; 7153 return false; 7154 } 7155 7156 // Integer comparisons are implemented with ADDS/SUBS, so the range of valid 7157 // immediates is the same as for an add or a sub. 7158 bool AArch64TargetLowering::isLegalICmpImmediate(int64_t Immed) const { 7159 if (Immed < 0) 7160 Immed *= -1; 7161 return isLegalAddImmediate(Immed); 7162 } 7163 7164 /// isLegalAddressingMode - Return true if the addressing mode represented 7165 /// by AM is legal for this target, for a load/store of the specified type. 7166 bool AArch64TargetLowering::isLegalAddressingMode(const DataLayout &DL, 7167 const AddrMode &AM, Type *Ty, 7168 unsigned AS) const { 7169 // AArch64 has five basic addressing modes: 7170 // reg 7171 // reg + 9-bit signed offset 7172 // reg + SIZE_IN_BYTES * 12-bit unsigned offset 7173 // reg1 + reg2 7174 // reg + SIZE_IN_BYTES * reg 7175 7176 // No global is ever allowed as a base. 7177 if (AM.BaseGV) 7178 return false; 7179 7180 // No reg+reg+imm addressing. 7181 if (AM.HasBaseReg && AM.BaseOffs && AM.Scale) 7182 return false; 7183 7184 // check reg + imm case: 7185 // i.e., reg + 0, reg + imm9, reg + SIZE_IN_BYTES * uimm12 7186 uint64_t NumBytes = 0; 7187 if (Ty->isSized()) { 7188 uint64_t NumBits = DL.getTypeSizeInBits(Ty); 7189 NumBytes = NumBits / 8; 7190 if (!isPowerOf2_64(NumBits)) 7191 NumBytes = 0; 7192 } 7193 7194 if (!AM.Scale) { 7195 int64_t Offset = AM.BaseOffs; 7196 7197 // 9-bit signed offset 7198 if (Offset >= -(1LL << 9) && Offset <= (1LL << 9) - 1) 7199 return true; 7200 7201 // 12-bit unsigned offset 7202 unsigned shift = Log2_64(NumBytes); 7203 if (NumBytes && Offset > 0 && (Offset / NumBytes) <= (1LL << 12) - 1 && 7204 // Must be a multiple of NumBytes (NumBytes is a power of 2) 7205 (Offset >> shift) << shift == Offset) 7206 return true; 7207 return false; 7208 } 7209 7210 // Check reg1 + SIZE_IN_BYTES * reg2 and reg1 + reg2 7211 7212 if (!AM.Scale || AM.Scale == 1 || 7213 (AM.Scale > 0 && (uint64_t)AM.Scale == NumBytes)) 7214 return true; 7215 return false; 7216 } 7217 7218 int AArch64TargetLowering::getScalingFactorCost(const DataLayout &DL, 7219 const AddrMode &AM, Type *Ty, 7220 unsigned AS) const { 7221 // Scaling factors are not free at all. 7222 // Operands | Rt Latency 7223 // ------------------------------------------- 7224 // Rt, [Xn, Xm] | 4 7225 // ------------------------------------------- 7226 // Rt, [Xn, Xm, lsl #imm] | Rn: 4 Rm: 5 7227 // Rt, [Xn, Wm, <extend> #imm] | 7228 if (isLegalAddressingMode(DL, AM, Ty, AS)) 7229 // Scale represents reg2 * scale, thus account for 1 if 7230 // it is not equal to 0 or 1. 7231 return AM.Scale != 0 && AM.Scale != 1; 7232 return -1; 7233 } 7234 7235 bool AArch64TargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { 7236 VT = VT.getScalarType(); 7237 7238 if (!VT.isSimple()) 7239 return false; 7240 7241 switch (VT.getSimpleVT().SimpleTy) { 7242 case MVT::f32: 7243 case MVT::f64: 7244 return true; 7245 default: 7246 break; 7247 } 7248 7249 return false; 7250 } 7251 7252 const MCPhysReg * 7253 AArch64TargetLowering::getScratchRegisters(CallingConv::ID) const { 7254 // LR is a callee-save register, but we must treat it as clobbered by any call 7255 // site. Hence we include LR in the scratch registers, which are in turn added 7256 // as implicit-defs for stackmaps and patchpoints. 7257 static const MCPhysReg ScratchRegs[] = { 7258 AArch64::X16, AArch64::X17, AArch64::LR, 0 7259 }; 7260 return ScratchRegs; 7261 } 7262 7263 bool 7264 AArch64TargetLowering::isDesirableToCommuteWithShift(const SDNode *N) const { 7265 EVT VT = N->getValueType(0); 7266 // If N is unsigned bit extraction: ((x >> C) & mask), then do not combine 7267 // it with shift to let it be lowered to UBFX. 7268 if (N->getOpcode() == ISD::AND && (VT == MVT::i32 || VT == MVT::i64) && 7269 isa<ConstantSDNode>(N->getOperand(1))) { 7270 uint64_t TruncMask = N->getConstantOperandVal(1); 7271 if (isMask_64(TruncMask) && 7272 N->getOperand(0).getOpcode() == ISD::SRL && 7273 isa<ConstantSDNode>(N->getOperand(0)->getOperand(1))) 7274 return false; 7275 } 7276 return true; 7277 } 7278 7279 bool AArch64TargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, 7280 Type *Ty) const { 7281 assert(Ty->isIntegerTy()); 7282 7283 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 7284 if (BitSize == 0) 7285 return false; 7286 7287 int64_t Val = Imm.getSExtValue(); 7288 if (Val == 0 || AArch64_AM::isLogicalImmediate(Val, BitSize)) 7289 return true; 7290 7291 if ((int64_t)Val < 0) 7292 Val = ~Val; 7293 if (BitSize == 32) 7294 Val &= (1LL << 32) - 1; 7295 7296 unsigned LZ = countLeadingZeros((uint64_t)Val); 7297 unsigned Shift = (63 - LZ) / 16; 7298 // MOVZ is free so return true for one or fewer MOVK. 7299 return Shift < 3; 7300 } 7301 7302 // Generate SUBS and CSEL for integer abs. 7303 static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) { 7304 EVT VT = N->getValueType(0); 7305 7306 SDValue N0 = N->getOperand(0); 7307 SDValue N1 = N->getOperand(1); 7308 SDLoc DL(N); 7309 7310 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1) 7311 // and change it to SUB and CSEL. 7312 if (VT.isInteger() && N->getOpcode() == ISD::XOR && 7313 N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1 && 7314 N1.getOpcode() == ISD::SRA && N1.getOperand(0) == N0.getOperand(0)) 7315 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) 7316 if (Y1C->getAPIntValue() == VT.getSizeInBits() - 1) { 7317 SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), 7318 N0.getOperand(0)); 7319 // Generate SUBS & CSEL. 7320 SDValue Cmp = 7321 DAG.getNode(AArch64ISD::SUBS, DL, DAG.getVTList(VT, MVT::i32), 7322 N0.getOperand(0), DAG.getConstant(0, DL, VT)); 7323 return DAG.getNode(AArch64ISD::CSEL, DL, VT, N0.getOperand(0), Neg, 7324 DAG.getConstant(AArch64CC::PL, DL, MVT::i32), 7325 SDValue(Cmp.getNode(), 1)); 7326 } 7327 return SDValue(); 7328 } 7329 7330 // performXorCombine - Attempts to handle integer ABS. 7331 static SDValue performXorCombine(SDNode *N, SelectionDAG &DAG, 7332 TargetLowering::DAGCombinerInfo &DCI, 7333 const AArch64Subtarget *Subtarget) { 7334 if (DCI.isBeforeLegalizeOps()) 7335 return SDValue(); 7336 7337 return performIntegerAbsCombine(N, DAG); 7338 } 7339 7340 SDValue 7341 AArch64TargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor, 7342 SelectionDAG &DAG, 7343 std::vector<SDNode *> *Created) const { 7344 // fold (sdiv X, pow2) 7345 EVT VT = N->getValueType(0); 7346 if ((VT != MVT::i32 && VT != MVT::i64) || 7347 !(Divisor.isPowerOf2() || (-Divisor).isPowerOf2())) 7348 return SDValue(); 7349 7350 SDLoc DL(N); 7351 SDValue N0 = N->getOperand(0); 7352 unsigned Lg2 = Divisor.countTrailingZeros(); 7353 SDValue Zero = DAG.getConstant(0, DL, VT); 7354 SDValue Pow2MinusOne = DAG.getConstant((1ULL << Lg2) - 1, DL, VT); 7355 7356 // Add (N0 < 0) ? Pow2 - 1 : 0; 7357 SDValue CCVal; 7358 SDValue Cmp = getAArch64Cmp(N0, Zero, ISD::SETLT, CCVal, DAG, DL); 7359 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, N0, Pow2MinusOne); 7360 SDValue CSel = DAG.getNode(AArch64ISD::CSEL, DL, VT, Add, N0, CCVal, Cmp); 7361 7362 if (Created) { 7363 Created->push_back(Cmp.getNode()); 7364 Created->push_back(Add.getNode()); 7365 Created->push_back(CSel.getNode()); 7366 } 7367 7368 // Divide by pow2. 7369 SDValue SRA = 7370 DAG.getNode(ISD::SRA, DL, VT, CSel, DAG.getConstant(Lg2, DL, MVT::i64)); 7371 7372 // If we're dividing by a positive value, we're done. Otherwise, we must 7373 // negate the result. 7374 if (Divisor.isNonNegative()) 7375 return SRA; 7376 7377 if (Created) 7378 Created->push_back(SRA.getNode()); 7379 return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), SRA); 7380 } 7381 7382 static SDValue performMulCombine(SDNode *N, SelectionDAG &DAG, 7383 TargetLowering::DAGCombinerInfo &DCI, 7384 const AArch64Subtarget *Subtarget) { 7385 if (DCI.isBeforeLegalizeOps()) 7386 return SDValue(); 7387 7388 // Multiplication of a power of two plus/minus one can be done more 7389 // cheaply as as shift+add/sub. For now, this is true unilaterally. If 7390 // future CPUs have a cheaper MADD instruction, this may need to be 7391 // gated on a subtarget feature. For Cyclone, 32-bit MADD is 4 cycles and 7392 // 64-bit is 5 cycles, so this is always a win. 7393 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) { 7394 APInt Value = C->getAPIntValue(); 7395 EVT VT = N->getValueType(0); 7396 SDLoc DL(N); 7397 if (Value.isNonNegative()) { 7398 // (mul x, 2^N + 1) => (add (shl x, N), x) 7399 APInt VM1 = Value - 1; 7400 if (VM1.isPowerOf2()) { 7401 SDValue ShiftedVal = 7402 DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), 7403 DAG.getConstant(VM1.logBase2(), DL, MVT::i64)); 7404 return DAG.getNode(ISD::ADD, DL, VT, ShiftedVal, 7405 N->getOperand(0)); 7406 } 7407 // (mul x, 2^N - 1) => (sub (shl x, N), x) 7408 APInt VP1 = Value + 1; 7409 if (VP1.isPowerOf2()) { 7410 SDValue ShiftedVal = 7411 DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), 7412 DAG.getConstant(VP1.logBase2(), DL, MVT::i64)); 7413 return DAG.getNode(ISD::SUB, DL, VT, ShiftedVal, 7414 N->getOperand(0)); 7415 } 7416 } else { 7417 // (mul x, -(2^N - 1)) => (sub x, (shl x, N)) 7418 APInt VNP1 = -Value + 1; 7419 if (VNP1.isPowerOf2()) { 7420 SDValue ShiftedVal = 7421 DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), 7422 DAG.getConstant(VNP1.logBase2(), DL, MVT::i64)); 7423 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), 7424 ShiftedVal); 7425 } 7426 // (mul x, -(2^N + 1)) => - (add (shl x, N), x) 7427 APInt VNM1 = -Value - 1; 7428 if (VNM1.isPowerOf2()) { 7429 SDValue ShiftedVal = 7430 DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), 7431 DAG.getConstant(VNM1.logBase2(), DL, MVT::i64)); 7432 SDValue Add = 7433 DAG.getNode(ISD::ADD, DL, VT, ShiftedVal, N->getOperand(0)); 7434 return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Add); 7435 } 7436 } 7437 } 7438 return SDValue(); 7439 } 7440 7441 static SDValue performVectorCompareAndMaskUnaryOpCombine(SDNode *N, 7442 SelectionDAG &DAG) { 7443 // Take advantage of vector comparisons producing 0 or -1 in each lane to 7444 // optimize away operation when it's from a constant. 7445 // 7446 // The general transformation is: 7447 // UNARYOP(AND(VECTOR_CMP(x,y), constant)) --> 7448 // AND(VECTOR_CMP(x,y), constant2) 7449 // constant2 = UNARYOP(constant) 7450 7451 // Early exit if this isn't a vector operation, the operand of the 7452 // unary operation isn't a bitwise AND, or if the sizes of the operations 7453 // aren't the same. 7454 EVT VT = N->getValueType(0); 7455 if (!VT.isVector() || N->getOperand(0)->getOpcode() != ISD::AND || 7456 N->getOperand(0)->getOperand(0)->getOpcode() != ISD::SETCC || 7457 VT.getSizeInBits() != N->getOperand(0)->getValueType(0).getSizeInBits()) 7458 return SDValue(); 7459 7460 // Now check that the other operand of the AND is a constant. We could 7461 // make the transformation for non-constant splats as well, but it's unclear 7462 // that would be a benefit as it would not eliminate any operations, just 7463 // perform one more step in scalar code before moving to the vector unit. 7464 if (BuildVectorSDNode *BV = 7465 dyn_cast<BuildVectorSDNode>(N->getOperand(0)->getOperand(1))) { 7466 // Bail out if the vector isn't a constant. 7467 if (!BV->isConstant()) 7468 return SDValue(); 7469 7470 // Everything checks out. Build up the new and improved node. 7471 SDLoc DL(N); 7472 EVT IntVT = BV->getValueType(0); 7473 // Create a new constant of the appropriate type for the transformed 7474 // DAG. 7475 SDValue SourceConst = DAG.getNode(N->getOpcode(), DL, VT, SDValue(BV, 0)); 7476 // The AND node needs bitcasts to/from an integer vector type around it. 7477 SDValue MaskConst = DAG.getNode(ISD::BITCAST, DL, IntVT, SourceConst); 7478 SDValue NewAnd = DAG.getNode(ISD::AND, DL, IntVT, 7479 N->getOperand(0)->getOperand(0), MaskConst); 7480 SDValue Res = DAG.getNode(ISD::BITCAST, DL, VT, NewAnd); 7481 return Res; 7482 } 7483 7484 return SDValue(); 7485 } 7486 7487 static SDValue performIntToFpCombine(SDNode *N, SelectionDAG &DAG, 7488 const AArch64Subtarget *Subtarget) { 7489 // First try to optimize away the conversion when it's conditionally from 7490 // a constant. Vectors only. 7491 if (SDValue Res = performVectorCompareAndMaskUnaryOpCombine(N, DAG)) 7492 return Res; 7493 7494 EVT VT = N->getValueType(0); 7495 if (VT != MVT::f32 && VT != MVT::f64) 7496 return SDValue(); 7497 7498 // Only optimize when the source and destination types have the same width. 7499 if (VT.getSizeInBits() != N->getOperand(0).getValueType().getSizeInBits()) 7500 return SDValue(); 7501 7502 // If the result of an integer load is only used by an integer-to-float 7503 // conversion, use a fp load instead and a AdvSIMD scalar {S|U}CVTF instead. 7504 // This eliminates an "integer-to-vector-move" UOP and improves throughput. 7505 SDValue N0 = N->getOperand(0); 7506 if (Subtarget->hasNEON() && ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && 7507 // Do not change the width of a volatile load. 7508 !cast<LoadSDNode>(N0)->isVolatile()) { 7509 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 7510 SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(), LN0->getBasePtr(), 7511 LN0->getPointerInfo(), LN0->isVolatile(), 7512 LN0->isNonTemporal(), LN0->isInvariant(), 7513 LN0->getAlignment()); 7514 7515 // Make sure successors of the original load stay after it by updating them 7516 // to use the new Chain. 7517 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), Load.getValue(1)); 7518 7519 unsigned Opcode = 7520 (N->getOpcode() == ISD::SINT_TO_FP) ? AArch64ISD::SITOF : AArch64ISD::UITOF; 7521 return DAG.getNode(Opcode, SDLoc(N), VT, Load); 7522 } 7523 7524 return SDValue(); 7525 } 7526 7527 /// Fold a floating-point multiply by power of two into floating-point to 7528 /// fixed-point conversion. 7529 static SDValue performFpToIntCombine(SDNode *N, SelectionDAG &DAG, 7530 const AArch64Subtarget *Subtarget) { 7531 if (!Subtarget->hasNEON()) 7532 return SDValue(); 7533 7534 SDValue Op = N->getOperand(0); 7535 if (!Op.getValueType().isVector() || Op.getOpcode() != ISD::FMUL) 7536 return SDValue(); 7537 7538 SDValue ConstVec = Op->getOperand(1); 7539 if (!isa<BuildVectorSDNode>(ConstVec)) 7540 return SDValue(); 7541 7542 MVT FloatTy = Op.getSimpleValueType().getVectorElementType(); 7543 uint32_t FloatBits = FloatTy.getSizeInBits(); 7544 if (FloatBits != 32 && FloatBits != 64) 7545 return SDValue(); 7546 7547 MVT IntTy = N->getSimpleValueType(0).getVectorElementType(); 7548 uint32_t IntBits = IntTy.getSizeInBits(); 7549 if (IntBits != 16 && IntBits != 32 && IntBits != 64) 7550 return SDValue(); 7551 7552 // Avoid conversions where iN is larger than the float (e.g., float -> i64). 7553 if (IntBits > FloatBits) 7554 return SDValue(); 7555 7556 BitVector UndefElements; 7557 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec); 7558 int32_t Bits = IntBits == 64 ? 64 : 32; 7559 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, Bits + 1); 7560 if (C == -1 || C == 0 || C > Bits) 7561 return SDValue(); 7562 7563 MVT ResTy; 7564 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 7565 switch (NumLanes) { 7566 default: 7567 return SDValue(); 7568 case 2: 7569 ResTy = FloatBits == 32 ? MVT::v2i32 : MVT::v2i64; 7570 break; 7571 case 4: 7572 ResTy = MVT::v4i32; 7573 break; 7574 } 7575 7576 SDLoc DL(N); 7577 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT; 7578 unsigned IntrinsicOpcode = IsSigned ? Intrinsic::aarch64_neon_vcvtfp2fxs 7579 : Intrinsic::aarch64_neon_vcvtfp2fxu; 7580 SDValue FixConv = 7581 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, ResTy, 7582 DAG.getConstant(IntrinsicOpcode, DL, MVT::i32), 7583 Op->getOperand(0), DAG.getConstant(C, DL, MVT::i32)); 7584 // We can handle smaller integers by generating an extra trunc. 7585 if (IntBits < FloatBits) 7586 FixConv = DAG.getNode(ISD::TRUNCATE, DL, N->getValueType(0), FixConv); 7587 7588 return FixConv; 7589 } 7590 7591 /// Fold a floating-point divide by power of two into fixed-point to 7592 /// floating-point conversion. 7593 static SDValue performFDivCombine(SDNode *N, SelectionDAG &DAG, 7594 const AArch64Subtarget *Subtarget) { 7595 if (!Subtarget->hasNEON()) 7596 return SDValue(); 7597 7598 SDValue Op = N->getOperand(0); 7599 unsigned Opc = Op->getOpcode(); 7600 if (!Op.getValueType().isVector() || 7601 (Opc != ISD::SINT_TO_FP && Opc != ISD::UINT_TO_FP)) 7602 return SDValue(); 7603 7604 SDValue ConstVec = N->getOperand(1); 7605 if (!isa<BuildVectorSDNode>(ConstVec)) 7606 return SDValue(); 7607 7608 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType(); 7609 int32_t IntBits = IntTy.getSizeInBits(); 7610 if (IntBits != 16 && IntBits != 32 && IntBits != 64) 7611 return SDValue(); 7612 7613 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType(); 7614 int32_t FloatBits = FloatTy.getSizeInBits(); 7615 if (FloatBits != 32 && FloatBits != 64) 7616 return SDValue(); 7617 7618 // Avoid conversions where iN is larger than the float (e.g., i64 -> float). 7619 if (IntBits > FloatBits) 7620 return SDValue(); 7621 7622 BitVector UndefElements; 7623 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec); 7624 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, FloatBits + 1); 7625 if (C == -1 || C == 0 || C > FloatBits) 7626 return SDValue(); 7627 7628 MVT ResTy; 7629 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 7630 switch (NumLanes) { 7631 default: 7632 return SDValue(); 7633 case 2: 7634 ResTy = FloatBits == 32 ? MVT::v2i32 : MVT::v2i64; 7635 break; 7636 case 4: 7637 ResTy = MVT::v4i32; 7638 break; 7639 } 7640 7641 SDLoc DL(N); 7642 SDValue ConvInput = Op.getOperand(0); 7643 bool IsSigned = Opc == ISD::SINT_TO_FP; 7644 if (IntBits < FloatBits) 7645 ConvInput = DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, DL, 7646 ResTy, ConvInput); 7647 7648 unsigned IntrinsicOpcode = IsSigned ? Intrinsic::aarch64_neon_vcvtfxs2fp 7649 : Intrinsic::aarch64_neon_vcvtfxu2fp; 7650 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, Op.getValueType(), 7651 DAG.getConstant(IntrinsicOpcode, DL, MVT::i32), ConvInput, 7652 DAG.getConstant(C, DL, MVT::i32)); 7653 } 7654 7655 /// An EXTR instruction is made up of two shifts, ORed together. This helper 7656 /// searches for and classifies those shifts. 7657 static bool findEXTRHalf(SDValue N, SDValue &Src, uint32_t &ShiftAmount, 7658 bool &FromHi) { 7659 if (N.getOpcode() == ISD::SHL) 7660 FromHi = false; 7661 else if (N.getOpcode() == ISD::SRL) 7662 FromHi = true; 7663 else 7664 return false; 7665 7666 if (!isa<ConstantSDNode>(N.getOperand(1))) 7667 return false; 7668 7669 ShiftAmount = N->getConstantOperandVal(1); 7670 Src = N->getOperand(0); 7671 return true; 7672 } 7673 7674 /// EXTR instruction extracts a contiguous chunk of bits from two existing 7675 /// registers viewed as a high/low pair. This function looks for the pattern: 7676 /// (or (shl VAL1, #N), (srl VAL2, #RegWidth-N)) and replaces it with an 7677 /// EXTR. Can't quite be done in TableGen because the two immediates aren't 7678 /// independent. 7679 static SDValue tryCombineToEXTR(SDNode *N, 7680 TargetLowering::DAGCombinerInfo &DCI) { 7681 SelectionDAG &DAG = DCI.DAG; 7682 SDLoc DL(N); 7683 EVT VT = N->getValueType(0); 7684 7685 assert(N->getOpcode() == ISD::OR && "Unexpected root"); 7686 7687 if (VT != MVT::i32 && VT != MVT::i64) 7688 return SDValue(); 7689 7690 SDValue LHS; 7691 uint32_t ShiftLHS = 0; 7692 bool LHSFromHi = 0; 7693 if (!findEXTRHalf(N->getOperand(0), LHS, ShiftLHS, LHSFromHi)) 7694 return SDValue(); 7695 7696 SDValue RHS; 7697 uint32_t ShiftRHS = 0; 7698 bool RHSFromHi = 0; 7699 if (!findEXTRHalf(N->getOperand(1), RHS, ShiftRHS, RHSFromHi)) 7700 return SDValue(); 7701 7702 // If they're both trying to come from the high part of the register, they're 7703 // not really an EXTR. 7704 if (LHSFromHi == RHSFromHi) 7705 return SDValue(); 7706 7707 if (ShiftLHS + ShiftRHS != VT.getSizeInBits()) 7708 return SDValue(); 7709 7710 if (LHSFromHi) { 7711 std::swap(LHS, RHS); 7712 std::swap(ShiftLHS, ShiftRHS); 7713 } 7714 7715 return DAG.getNode(AArch64ISD::EXTR, DL, VT, LHS, RHS, 7716 DAG.getConstant(ShiftRHS, DL, MVT::i64)); 7717 } 7718 7719 static SDValue tryCombineToBSL(SDNode *N, 7720 TargetLowering::DAGCombinerInfo &DCI) { 7721 EVT VT = N->getValueType(0); 7722 SelectionDAG &DAG = DCI.DAG; 7723 SDLoc DL(N); 7724 7725 if (!VT.isVector()) 7726 return SDValue(); 7727 7728 SDValue N0 = N->getOperand(0); 7729 if (N0.getOpcode() != ISD::AND) 7730 return SDValue(); 7731 7732 SDValue N1 = N->getOperand(1); 7733 if (N1.getOpcode() != ISD::AND) 7734 return SDValue(); 7735 7736 // We only have to look for constant vectors here since the general, variable 7737 // case can be handled in TableGen. 7738 unsigned Bits = VT.getVectorElementType().getSizeInBits(); 7739 uint64_t BitMask = Bits == 64 ? -1ULL : ((1ULL << Bits) - 1); 7740 for (int i = 1; i >= 0; --i) 7741 for (int j = 1; j >= 0; --j) { 7742 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(i)); 7743 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(j)); 7744 if (!BVN0 || !BVN1) 7745 continue; 7746 7747 bool FoundMatch = true; 7748 for (unsigned k = 0; k < VT.getVectorNumElements(); ++k) { 7749 ConstantSDNode *CN0 = dyn_cast<ConstantSDNode>(BVN0->getOperand(k)); 7750 ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(BVN1->getOperand(k)); 7751 if (!CN0 || !CN1 || 7752 CN0->getZExtValue() != (BitMask & ~CN1->getZExtValue())) { 7753 FoundMatch = false; 7754 break; 7755 } 7756 } 7757 7758 if (FoundMatch) 7759 return DAG.getNode(AArch64ISD::BSL, DL, VT, SDValue(BVN0, 0), 7760 N0->getOperand(1 - i), N1->getOperand(1 - j)); 7761 } 7762 7763 return SDValue(); 7764 } 7765 7766 static SDValue performORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, 7767 const AArch64Subtarget *Subtarget) { 7768 // Attempt to form an EXTR from (or (shl VAL1, #N), (srl VAL2, #RegWidth-N)) 7769 if (!EnableAArch64ExtrGeneration) 7770 return SDValue(); 7771 SelectionDAG &DAG = DCI.DAG; 7772 EVT VT = N->getValueType(0); 7773 7774 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 7775 return SDValue(); 7776 7777 SDValue Res = tryCombineToEXTR(N, DCI); 7778 if (Res.getNode()) 7779 return Res; 7780 7781 Res = tryCombineToBSL(N, DCI); 7782 if (Res.getNode()) 7783 return Res; 7784 7785 return SDValue(); 7786 } 7787 7788 static SDValue performBitcastCombine(SDNode *N, 7789 TargetLowering::DAGCombinerInfo &DCI, 7790 SelectionDAG &DAG) { 7791 // Wait 'til after everything is legalized to try this. That way we have 7792 // legal vector types and such. 7793 if (DCI.isBeforeLegalizeOps()) 7794 return SDValue(); 7795 7796 // Remove extraneous bitcasts around an extract_subvector. 7797 // For example, 7798 // (v4i16 (bitconvert 7799 // (extract_subvector (v2i64 (bitconvert (v8i16 ...)), (i64 1))))) 7800 // becomes 7801 // (extract_subvector ((v8i16 ...), (i64 4))) 7802 7803 // Only interested in 64-bit vectors as the ultimate result. 7804 EVT VT = N->getValueType(0); 7805 if (!VT.isVector()) 7806 return SDValue(); 7807 if (VT.getSimpleVT().getSizeInBits() != 64) 7808 return SDValue(); 7809 // Is the operand an extract_subvector starting at the beginning or halfway 7810 // point of the vector? A low half may also come through as an 7811 // EXTRACT_SUBREG, so look for that, too. 7812 SDValue Op0 = N->getOperand(0); 7813 if (Op0->getOpcode() != ISD::EXTRACT_SUBVECTOR && 7814 !(Op0->isMachineOpcode() && 7815 Op0->getMachineOpcode() == AArch64::EXTRACT_SUBREG)) 7816 return SDValue(); 7817 uint64_t idx = cast<ConstantSDNode>(Op0->getOperand(1))->getZExtValue(); 7818 if (Op0->getOpcode() == ISD::EXTRACT_SUBVECTOR) { 7819 if (Op0->getValueType(0).getVectorNumElements() != idx && idx != 0) 7820 return SDValue(); 7821 } else if (Op0->getMachineOpcode() == AArch64::EXTRACT_SUBREG) { 7822 if (idx != AArch64::dsub) 7823 return SDValue(); 7824 // The dsub reference is equivalent to a lane zero subvector reference. 7825 idx = 0; 7826 } 7827 // Look through the bitcast of the input to the extract. 7828 if (Op0->getOperand(0)->getOpcode() != ISD::BITCAST) 7829 return SDValue(); 7830 SDValue Source = Op0->getOperand(0)->getOperand(0); 7831 // If the source type has twice the number of elements as our destination 7832 // type, we know this is an extract of the high or low half of the vector. 7833 EVT SVT = Source->getValueType(0); 7834 if (SVT.getVectorNumElements() != VT.getVectorNumElements() * 2) 7835 return SDValue(); 7836 7837 DEBUG(dbgs() << "aarch64-lower: bitcast extract_subvector simplification\n"); 7838 7839 // Create the simplified form to just extract the low or high half of the 7840 // vector directly rather than bothering with the bitcasts. 7841 SDLoc dl(N); 7842 unsigned NumElements = VT.getVectorNumElements(); 7843 if (idx) { 7844 SDValue HalfIdx = DAG.getConstant(NumElements, dl, MVT::i64); 7845 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Source, HalfIdx); 7846 } else { 7847 SDValue SubReg = DAG.getTargetConstant(AArch64::dsub, dl, MVT::i32); 7848 return SDValue(DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl, VT, 7849 Source, SubReg), 7850 0); 7851 } 7852 } 7853 7854 static SDValue performConcatVectorsCombine(SDNode *N, 7855 TargetLowering::DAGCombinerInfo &DCI, 7856 SelectionDAG &DAG) { 7857 SDLoc dl(N); 7858 EVT VT = N->getValueType(0); 7859 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1); 7860 7861 // Optimize concat_vectors of truncated vectors, where the intermediate 7862 // type is illegal, to avoid said illegality, e.g., 7863 // (v4i16 (concat_vectors (v2i16 (truncate (v2i64))), 7864 // (v2i16 (truncate (v2i64))))) 7865 // -> 7866 // (v4i16 (truncate (vector_shuffle (v4i32 (bitcast (v2i64))), 7867 // (v4i32 (bitcast (v2i64))), 7868 // <0, 2, 4, 6>))) 7869 // This isn't really target-specific, but ISD::TRUNCATE legality isn't keyed 7870 // on both input and result type, so we might generate worse code. 7871 // On AArch64 we know it's fine for v2i64->v4i16 and v4i32->v8i8. 7872 if (N->getNumOperands() == 2 && 7873 N0->getOpcode() == ISD::TRUNCATE && 7874 N1->getOpcode() == ISD::TRUNCATE) { 7875 SDValue N00 = N0->getOperand(0); 7876 SDValue N10 = N1->getOperand(0); 7877 EVT N00VT = N00.getValueType(); 7878 7879 if (N00VT == N10.getValueType() && 7880 (N00VT == MVT::v2i64 || N00VT == MVT::v4i32) && 7881 N00VT.getScalarSizeInBits() == 4 * VT.getScalarSizeInBits()) { 7882 MVT MidVT = (N00VT == MVT::v2i64 ? MVT::v4i32 : MVT::v8i16); 7883 SmallVector<int, 8> Mask(MidVT.getVectorNumElements()); 7884 for (size_t i = 0; i < Mask.size(); ++i) 7885 Mask[i] = i * 2; 7886 return DAG.getNode(ISD::TRUNCATE, dl, VT, 7887 DAG.getVectorShuffle( 7888 MidVT, dl, 7889 DAG.getNode(ISD::BITCAST, dl, MidVT, N00), 7890 DAG.getNode(ISD::BITCAST, dl, MidVT, N10), Mask)); 7891 } 7892 } 7893 7894 // Wait 'til after everything is legalized to try this. That way we have 7895 // legal vector types and such. 7896 if (DCI.isBeforeLegalizeOps()) 7897 return SDValue(); 7898 7899 // If we see a (concat_vectors (v1x64 A), (v1x64 A)) it's really a vector 7900 // splat. The indexed instructions are going to be expecting a DUPLANE64, so 7901 // canonicalise to that. 7902 if (N0 == N1 && VT.getVectorNumElements() == 2) { 7903 assert(VT.getVectorElementType().getSizeInBits() == 64); 7904 return DAG.getNode(AArch64ISD::DUPLANE64, dl, VT, WidenVector(N0, DAG), 7905 DAG.getConstant(0, dl, MVT::i64)); 7906 } 7907 7908 // Canonicalise concat_vectors so that the right-hand vector has as few 7909 // bit-casts as possible before its real operation. The primary matching 7910 // destination for these operations will be the narrowing "2" instructions, 7911 // which depend on the operation being performed on this right-hand vector. 7912 // For example, 7913 // (concat_vectors LHS, (v1i64 (bitconvert (v4i16 RHS)))) 7914 // becomes 7915 // (bitconvert (concat_vectors (v4i16 (bitconvert LHS)), RHS)) 7916 7917 if (N1->getOpcode() != ISD::BITCAST) 7918 return SDValue(); 7919 SDValue RHS = N1->getOperand(0); 7920 MVT RHSTy = RHS.getValueType().getSimpleVT(); 7921 // If the RHS is not a vector, this is not the pattern we're looking for. 7922 if (!RHSTy.isVector()) 7923 return SDValue(); 7924 7925 DEBUG(dbgs() << "aarch64-lower: concat_vectors bitcast simplification\n"); 7926 7927 MVT ConcatTy = MVT::getVectorVT(RHSTy.getVectorElementType(), 7928 RHSTy.getVectorNumElements() * 2); 7929 return DAG.getNode(ISD::BITCAST, dl, VT, 7930 DAG.getNode(ISD::CONCAT_VECTORS, dl, ConcatTy, 7931 DAG.getNode(ISD::BITCAST, dl, RHSTy, N0), 7932 RHS)); 7933 } 7934 7935 static SDValue tryCombineFixedPointConvert(SDNode *N, 7936 TargetLowering::DAGCombinerInfo &DCI, 7937 SelectionDAG &DAG) { 7938 // Wait 'til after everything is legalized to try this. That way we have 7939 // legal vector types and such. 7940 if (DCI.isBeforeLegalizeOps()) 7941 return SDValue(); 7942 // Transform a scalar conversion of a value from a lane extract into a 7943 // lane extract of a vector conversion. E.g., from foo1 to foo2: 7944 // double foo1(int64x2_t a) { return vcvtd_n_f64_s64(a[1], 9); } 7945 // double foo2(int64x2_t a) { return vcvtq_n_f64_s64(a, 9)[1]; } 7946 // 7947 // The second form interacts better with instruction selection and the 7948 // register allocator to avoid cross-class register copies that aren't 7949 // coalescable due to a lane reference. 7950 7951 // Check the operand and see if it originates from a lane extract. 7952 SDValue Op1 = N->getOperand(1); 7953 if (Op1.getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 7954 // Yep, no additional predication needed. Perform the transform. 7955 SDValue IID = N->getOperand(0); 7956 SDValue Shift = N->getOperand(2); 7957 SDValue Vec = Op1.getOperand(0); 7958 SDValue Lane = Op1.getOperand(1); 7959 EVT ResTy = N->getValueType(0); 7960 EVT VecResTy; 7961 SDLoc DL(N); 7962 7963 // The vector width should be 128 bits by the time we get here, even 7964 // if it started as 64 bits (the extract_vector handling will have 7965 // done so). 7966 assert(Vec.getValueType().getSizeInBits() == 128 && 7967 "unexpected vector size on extract_vector_elt!"); 7968 if (Vec.getValueType() == MVT::v4i32) 7969 VecResTy = MVT::v4f32; 7970 else if (Vec.getValueType() == MVT::v2i64) 7971 VecResTy = MVT::v2f64; 7972 else 7973 llvm_unreachable("unexpected vector type!"); 7974 7975 SDValue Convert = 7976 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VecResTy, IID, Vec, Shift); 7977 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResTy, Convert, Lane); 7978 } 7979 return SDValue(); 7980 } 7981 7982 // AArch64 high-vector "long" operations are formed by performing the non-high 7983 // version on an extract_subvector of each operand which gets the high half: 7984 // 7985 // (longop2 LHS, RHS) == (longop (extract_high LHS), (extract_high RHS)) 7986 // 7987 // However, there are cases which don't have an extract_high explicitly, but 7988 // have another operation that can be made compatible with one for free. For 7989 // example: 7990 // 7991 // (dupv64 scalar) --> (extract_high (dup128 scalar)) 7992 // 7993 // This routine does the actual conversion of such DUPs, once outer routines 7994 // have determined that everything else is in order. 7995 // It also supports immediate DUP-like nodes (MOVI/MVNi), which we can fold 7996 // similarly here. 7997 static SDValue tryExtendDUPToExtractHigh(SDValue N, SelectionDAG &DAG) { 7998 switch (N.getOpcode()) { 7999 case AArch64ISD::DUP: 8000 case AArch64ISD::DUPLANE8: 8001 case AArch64ISD::DUPLANE16: 8002 case AArch64ISD::DUPLANE32: 8003 case AArch64ISD::DUPLANE64: 8004 case AArch64ISD::MOVI: 8005 case AArch64ISD::MOVIshift: 8006 case AArch64ISD::MOVIedit: 8007 case AArch64ISD::MOVImsl: 8008 case AArch64ISD::MVNIshift: 8009 case AArch64ISD::MVNImsl: 8010 break; 8011 default: 8012 // FMOV could be supported, but isn't very useful, as it would only occur 8013 // if you passed a bitcast' floating point immediate to an eligible long 8014 // integer op (addl, smull, ...). 8015 return SDValue(); 8016 } 8017 8018 MVT NarrowTy = N.getSimpleValueType(); 8019 if (!NarrowTy.is64BitVector()) 8020 return SDValue(); 8021 8022 MVT ElementTy = NarrowTy.getVectorElementType(); 8023 unsigned NumElems = NarrowTy.getVectorNumElements(); 8024 MVT NewVT = MVT::getVectorVT(ElementTy, NumElems * 2); 8025 8026 SDLoc dl(N); 8027 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NarrowTy, 8028 DAG.getNode(N->getOpcode(), dl, NewVT, N->ops()), 8029 DAG.getConstant(NumElems, dl, MVT::i64)); 8030 } 8031 8032 static bool isEssentiallyExtractSubvector(SDValue N) { 8033 if (N.getOpcode() == ISD::EXTRACT_SUBVECTOR) 8034 return true; 8035 8036 return N.getOpcode() == ISD::BITCAST && 8037 N.getOperand(0).getOpcode() == ISD::EXTRACT_SUBVECTOR; 8038 } 8039 8040 /// \brief Helper structure to keep track of ISD::SET_CC operands. 8041 struct GenericSetCCInfo { 8042 const SDValue *Opnd0; 8043 const SDValue *Opnd1; 8044 ISD::CondCode CC; 8045 }; 8046 8047 /// \brief Helper structure to keep track of a SET_CC lowered into AArch64 code. 8048 struct AArch64SetCCInfo { 8049 const SDValue *Cmp; 8050 AArch64CC::CondCode CC; 8051 }; 8052 8053 /// \brief Helper structure to keep track of SetCC information. 8054 union SetCCInfo { 8055 GenericSetCCInfo Generic; 8056 AArch64SetCCInfo AArch64; 8057 }; 8058 8059 /// \brief Helper structure to be able to read SetCC information. If set to 8060 /// true, IsAArch64 field, Info is a AArch64SetCCInfo, otherwise Info is a 8061 /// GenericSetCCInfo. 8062 struct SetCCInfoAndKind { 8063 SetCCInfo Info; 8064 bool IsAArch64; 8065 }; 8066 8067 /// \brief Check whether or not \p Op is a SET_CC operation, either a generic or 8068 /// an 8069 /// AArch64 lowered one. 8070 /// \p SetCCInfo is filled accordingly. 8071 /// \post SetCCInfo is meanginfull only when this function returns true. 8072 /// \return True when Op is a kind of SET_CC operation. 8073 static bool isSetCC(SDValue Op, SetCCInfoAndKind &SetCCInfo) { 8074 // If this is a setcc, this is straight forward. 8075 if (Op.getOpcode() == ISD::SETCC) { 8076 SetCCInfo.Info.Generic.Opnd0 = &Op.getOperand(0); 8077 SetCCInfo.Info.Generic.Opnd1 = &Op.getOperand(1); 8078 SetCCInfo.Info.Generic.CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 8079 SetCCInfo.IsAArch64 = false; 8080 return true; 8081 } 8082 // Otherwise, check if this is a matching csel instruction. 8083 // In other words: 8084 // - csel 1, 0, cc 8085 // - csel 0, 1, !cc 8086 if (Op.getOpcode() != AArch64ISD::CSEL) 8087 return false; 8088 // Set the information about the operands. 8089 // TODO: we want the operands of the Cmp not the csel 8090 SetCCInfo.Info.AArch64.Cmp = &Op.getOperand(3); 8091 SetCCInfo.IsAArch64 = true; 8092 SetCCInfo.Info.AArch64.CC = static_cast<AArch64CC::CondCode>( 8093 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue()); 8094 8095 // Check that the operands matches the constraints: 8096 // (1) Both operands must be constants. 8097 // (2) One must be 1 and the other must be 0. 8098 ConstantSDNode *TValue = dyn_cast<ConstantSDNode>(Op.getOperand(0)); 8099 ConstantSDNode *FValue = dyn_cast<ConstantSDNode>(Op.getOperand(1)); 8100 8101 // Check (1). 8102 if (!TValue || !FValue) 8103 return false; 8104 8105 // Check (2). 8106 if (!TValue->isOne()) { 8107 // Update the comparison when we are interested in !cc. 8108 std::swap(TValue, FValue); 8109 SetCCInfo.Info.AArch64.CC = 8110 AArch64CC::getInvertedCondCode(SetCCInfo.Info.AArch64.CC); 8111 } 8112 return TValue->isOne() && FValue->isNullValue(); 8113 } 8114 8115 // Returns true if Op is setcc or zext of setcc. 8116 static bool isSetCCOrZExtSetCC(const SDValue& Op, SetCCInfoAndKind &Info) { 8117 if (isSetCC(Op, Info)) 8118 return true; 8119 return ((Op.getOpcode() == ISD::ZERO_EXTEND) && 8120 isSetCC(Op->getOperand(0), Info)); 8121 } 8122 8123 // The folding we want to perform is: 8124 // (add x, [zext] (setcc cc ...) ) 8125 // --> 8126 // (csel x, (add x, 1), !cc ...) 8127 // 8128 // The latter will get matched to a CSINC instruction. 8129 static SDValue performSetccAddFolding(SDNode *Op, SelectionDAG &DAG) { 8130 assert(Op && Op->getOpcode() == ISD::ADD && "Unexpected operation!"); 8131 SDValue LHS = Op->getOperand(0); 8132 SDValue RHS = Op->getOperand(1); 8133 SetCCInfoAndKind InfoAndKind; 8134 8135 // If neither operand is a SET_CC, give up. 8136 if (!isSetCCOrZExtSetCC(LHS, InfoAndKind)) { 8137 std::swap(LHS, RHS); 8138 if (!isSetCCOrZExtSetCC(LHS, InfoAndKind)) 8139 return SDValue(); 8140 } 8141 8142 // FIXME: This could be generatized to work for FP comparisons. 8143 EVT CmpVT = InfoAndKind.IsAArch64 8144 ? InfoAndKind.Info.AArch64.Cmp->getOperand(0).getValueType() 8145 : InfoAndKind.Info.Generic.Opnd0->getValueType(); 8146 if (CmpVT != MVT::i32 && CmpVT != MVT::i64) 8147 return SDValue(); 8148 8149 SDValue CCVal; 8150 SDValue Cmp; 8151 SDLoc dl(Op); 8152 if (InfoAndKind.IsAArch64) { 8153 CCVal = DAG.getConstant( 8154 AArch64CC::getInvertedCondCode(InfoAndKind.Info.AArch64.CC), dl, 8155 MVT::i32); 8156 Cmp = *InfoAndKind.Info.AArch64.Cmp; 8157 } else 8158 Cmp = getAArch64Cmp(*InfoAndKind.Info.Generic.Opnd0, 8159 *InfoAndKind.Info.Generic.Opnd1, 8160 ISD::getSetCCInverse(InfoAndKind.Info.Generic.CC, true), 8161 CCVal, DAG, dl); 8162 8163 EVT VT = Op->getValueType(0); 8164 LHS = DAG.getNode(ISD::ADD, dl, VT, RHS, DAG.getConstant(1, dl, VT)); 8165 return DAG.getNode(AArch64ISD::CSEL, dl, VT, RHS, LHS, CCVal, Cmp); 8166 } 8167 8168 // The basic add/sub long vector instructions have variants with "2" on the end 8169 // which act on the high-half of their inputs. They are normally matched by 8170 // patterns like: 8171 // 8172 // (add (zeroext (extract_high LHS)), 8173 // (zeroext (extract_high RHS))) 8174 // -> uaddl2 vD, vN, vM 8175 // 8176 // However, if one of the extracts is something like a duplicate, this 8177 // instruction can still be used profitably. This function puts the DAG into a 8178 // more appropriate form for those patterns to trigger. 8179 static SDValue performAddSubLongCombine(SDNode *N, 8180 TargetLowering::DAGCombinerInfo &DCI, 8181 SelectionDAG &DAG) { 8182 if (DCI.isBeforeLegalizeOps()) 8183 return SDValue(); 8184 8185 MVT VT = N->getSimpleValueType(0); 8186 if (!VT.is128BitVector()) { 8187 if (N->getOpcode() == ISD::ADD) 8188 return performSetccAddFolding(N, DAG); 8189 return SDValue(); 8190 } 8191 8192 // Make sure both branches are extended in the same way. 8193 SDValue LHS = N->getOperand(0); 8194 SDValue RHS = N->getOperand(1); 8195 if ((LHS.getOpcode() != ISD::ZERO_EXTEND && 8196 LHS.getOpcode() != ISD::SIGN_EXTEND) || 8197 LHS.getOpcode() != RHS.getOpcode()) 8198 return SDValue(); 8199 8200 unsigned ExtType = LHS.getOpcode(); 8201 8202 // It's not worth doing if at least one of the inputs isn't already an 8203 // extract, but we don't know which it'll be so we have to try both. 8204 if (isEssentiallyExtractSubvector(LHS.getOperand(0))) { 8205 RHS = tryExtendDUPToExtractHigh(RHS.getOperand(0), DAG); 8206 if (!RHS.getNode()) 8207 return SDValue(); 8208 8209 RHS = DAG.getNode(ExtType, SDLoc(N), VT, RHS); 8210 } else if (isEssentiallyExtractSubvector(RHS.getOperand(0))) { 8211 LHS = tryExtendDUPToExtractHigh(LHS.getOperand(0), DAG); 8212 if (!LHS.getNode()) 8213 return SDValue(); 8214 8215 LHS = DAG.getNode(ExtType, SDLoc(N), VT, LHS); 8216 } 8217 8218 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, LHS, RHS); 8219 } 8220 8221 // Massage DAGs which we can use the high-half "long" operations on into 8222 // something isel will recognize better. E.g. 8223 // 8224 // (aarch64_neon_umull (extract_high vec) (dupv64 scalar)) --> 8225 // (aarch64_neon_umull (extract_high (v2i64 vec))) 8226 // (extract_high (v2i64 (dup128 scalar))))) 8227 // 8228 static SDValue tryCombineLongOpWithDup(SDNode *N, 8229 TargetLowering::DAGCombinerInfo &DCI, 8230 SelectionDAG &DAG) { 8231 if (DCI.isBeforeLegalizeOps()) 8232 return SDValue(); 8233 8234 bool IsIntrinsic = N->getOpcode() == ISD::INTRINSIC_WO_CHAIN; 8235 SDValue LHS = N->getOperand(IsIntrinsic ? 1 : 0); 8236 SDValue RHS = N->getOperand(IsIntrinsic ? 2 : 1); 8237 assert(LHS.getValueType().is64BitVector() && 8238 RHS.getValueType().is64BitVector() && 8239 "unexpected shape for long operation"); 8240 8241 // Either node could be a DUP, but it's not worth doing both of them (you'd 8242 // just as well use the non-high version) so look for a corresponding extract 8243 // operation on the other "wing". 8244 if (isEssentiallyExtractSubvector(LHS)) { 8245 RHS = tryExtendDUPToExtractHigh(RHS, DAG); 8246 if (!RHS.getNode()) 8247 return SDValue(); 8248 } else if (isEssentiallyExtractSubvector(RHS)) { 8249 LHS = tryExtendDUPToExtractHigh(LHS, DAG); 8250 if (!LHS.getNode()) 8251 return SDValue(); 8252 } 8253 8254 // N could either be an intrinsic or a sabsdiff/uabsdiff node. 8255 if (IsIntrinsic) 8256 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), N->getValueType(0), 8257 N->getOperand(0), LHS, RHS); 8258 else 8259 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0), 8260 LHS, RHS); 8261 } 8262 8263 static SDValue tryCombineShiftImm(unsigned IID, SDNode *N, SelectionDAG &DAG) { 8264 MVT ElemTy = N->getSimpleValueType(0).getScalarType(); 8265 unsigned ElemBits = ElemTy.getSizeInBits(); 8266 8267 int64_t ShiftAmount; 8268 if (BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(2))) { 8269 APInt SplatValue, SplatUndef; 8270 unsigned SplatBitSize; 8271 bool HasAnyUndefs; 8272 if (!BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, 8273 HasAnyUndefs, ElemBits) || 8274 SplatBitSize != ElemBits) 8275 return SDValue(); 8276 8277 ShiftAmount = SplatValue.getSExtValue(); 8278 } else if (ConstantSDNode *CVN = dyn_cast<ConstantSDNode>(N->getOperand(2))) { 8279 ShiftAmount = CVN->getSExtValue(); 8280 } else 8281 return SDValue(); 8282 8283 unsigned Opcode; 8284 bool IsRightShift; 8285 switch (IID) { 8286 default: 8287 llvm_unreachable("Unknown shift intrinsic"); 8288 case Intrinsic::aarch64_neon_sqshl: 8289 Opcode = AArch64ISD::SQSHL_I; 8290 IsRightShift = false; 8291 break; 8292 case Intrinsic::aarch64_neon_uqshl: 8293 Opcode = AArch64ISD::UQSHL_I; 8294 IsRightShift = false; 8295 break; 8296 case Intrinsic::aarch64_neon_srshl: 8297 Opcode = AArch64ISD::SRSHR_I; 8298 IsRightShift = true; 8299 break; 8300 case Intrinsic::aarch64_neon_urshl: 8301 Opcode = AArch64ISD::URSHR_I; 8302 IsRightShift = true; 8303 break; 8304 case Intrinsic::aarch64_neon_sqshlu: 8305 Opcode = AArch64ISD::SQSHLU_I; 8306 IsRightShift = false; 8307 break; 8308 } 8309 8310 if (IsRightShift && ShiftAmount <= -1 && ShiftAmount >= -(int)ElemBits) { 8311 SDLoc dl(N); 8312 return DAG.getNode(Opcode, dl, N->getValueType(0), N->getOperand(1), 8313 DAG.getConstant(-ShiftAmount, dl, MVT::i32)); 8314 } else if (!IsRightShift && ShiftAmount >= 0 && ShiftAmount < ElemBits) { 8315 SDLoc dl(N); 8316 return DAG.getNode(Opcode, dl, N->getValueType(0), N->getOperand(1), 8317 DAG.getConstant(ShiftAmount, dl, MVT::i32)); 8318 } 8319 8320 return SDValue(); 8321 } 8322 8323 // The CRC32[BH] instructions ignore the high bits of their data operand. Since 8324 // the intrinsics must be legal and take an i32, this means there's almost 8325 // certainly going to be a zext in the DAG which we can eliminate. 8326 static SDValue tryCombineCRC32(unsigned Mask, SDNode *N, SelectionDAG &DAG) { 8327 SDValue AndN = N->getOperand(2); 8328 if (AndN.getOpcode() != ISD::AND) 8329 return SDValue(); 8330 8331 ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(AndN.getOperand(1)); 8332 if (!CMask || CMask->getZExtValue() != Mask) 8333 return SDValue(); 8334 8335 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), MVT::i32, 8336 N->getOperand(0), N->getOperand(1), AndN.getOperand(0)); 8337 } 8338 8339 static SDValue combineAcrossLanesIntrinsic(unsigned Opc, SDNode *N, 8340 SelectionDAG &DAG) { 8341 SDLoc dl(N); 8342 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), 8343 DAG.getNode(Opc, dl, 8344 N->getOperand(1).getSimpleValueType(), 8345 N->getOperand(1)), 8346 DAG.getConstant(0, dl, MVT::i64)); 8347 } 8348 8349 static SDValue performIntrinsicCombine(SDNode *N, 8350 TargetLowering::DAGCombinerInfo &DCI, 8351 const AArch64Subtarget *Subtarget) { 8352 SelectionDAG &DAG = DCI.DAG; 8353 unsigned IID = getIntrinsicID(N); 8354 switch (IID) { 8355 default: 8356 break; 8357 case Intrinsic::aarch64_neon_vcvtfxs2fp: 8358 case Intrinsic::aarch64_neon_vcvtfxu2fp: 8359 return tryCombineFixedPointConvert(N, DCI, DAG); 8360 case Intrinsic::aarch64_neon_saddv: 8361 return combineAcrossLanesIntrinsic(AArch64ISD::SADDV, N, DAG); 8362 case Intrinsic::aarch64_neon_uaddv: 8363 return combineAcrossLanesIntrinsic(AArch64ISD::UADDV, N, DAG); 8364 case Intrinsic::aarch64_neon_sminv: 8365 return combineAcrossLanesIntrinsic(AArch64ISD::SMINV, N, DAG); 8366 case Intrinsic::aarch64_neon_uminv: 8367 return combineAcrossLanesIntrinsic(AArch64ISD::UMINV, N, DAG); 8368 case Intrinsic::aarch64_neon_smaxv: 8369 return combineAcrossLanesIntrinsic(AArch64ISD::SMAXV, N, DAG); 8370 case Intrinsic::aarch64_neon_umaxv: 8371 return combineAcrossLanesIntrinsic(AArch64ISD::UMAXV, N, DAG); 8372 case Intrinsic::aarch64_neon_fmax: 8373 return DAG.getNode(ISD::FMAXNAN, SDLoc(N), N->getValueType(0), 8374 N->getOperand(1), N->getOperand(2)); 8375 case Intrinsic::aarch64_neon_fmin: 8376 return DAG.getNode(ISD::FMINNAN, SDLoc(N), N->getValueType(0), 8377 N->getOperand(1), N->getOperand(2)); 8378 case Intrinsic::aarch64_neon_sabd: 8379 return DAG.getNode(ISD::SABSDIFF, SDLoc(N), N->getValueType(0), 8380 N->getOperand(1), N->getOperand(2)); 8381 case Intrinsic::aarch64_neon_uabd: 8382 return DAG.getNode(ISD::UABSDIFF, SDLoc(N), N->getValueType(0), 8383 N->getOperand(1), N->getOperand(2)); 8384 case Intrinsic::aarch64_neon_fmaxnm: 8385 return DAG.getNode(ISD::FMAXNUM, SDLoc(N), N->getValueType(0), 8386 N->getOperand(1), N->getOperand(2)); 8387 case Intrinsic::aarch64_neon_fminnm: 8388 return DAG.getNode(ISD::FMINNUM, SDLoc(N), N->getValueType(0), 8389 N->getOperand(1), N->getOperand(2)); 8390 case Intrinsic::aarch64_neon_smull: 8391 case Intrinsic::aarch64_neon_umull: 8392 case Intrinsic::aarch64_neon_pmull: 8393 case Intrinsic::aarch64_neon_sqdmull: 8394 return tryCombineLongOpWithDup(N, DCI, DAG); 8395 case Intrinsic::aarch64_neon_sqshl: 8396 case Intrinsic::aarch64_neon_uqshl: 8397 case Intrinsic::aarch64_neon_sqshlu: 8398 case Intrinsic::aarch64_neon_srshl: 8399 case Intrinsic::aarch64_neon_urshl: 8400 return tryCombineShiftImm(IID, N, DAG); 8401 case Intrinsic::aarch64_crc32b: 8402 case Intrinsic::aarch64_crc32cb: 8403 return tryCombineCRC32(0xff, N, DAG); 8404 case Intrinsic::aarch64_crc32h: 8405 case Intrinsic::aarch64_crc32ch: 8406 return tryCombineCRC32(0xffff, N, DAG); 8407 } 8408 return SDValue(); 8409 } 8410 8411 static SDValue performExtendCombine(SDNode *N, 8412 TargetLowering::DAGCombinerInfo &DCI, 8413 SelectionDAG &DAG) { 8414 // If we see something like (zext (sabd (extract_high ...), (DUP ...))) then 8415 // we can convert that DUP into another extract_high (of a bigger DUP), which 8416 // helps the backend to decide that an sabdl2 would be useful, saving a real 8417 // extract_high operation. 8418 if (!DCI.isBeforeLegalizeOps() && N->getOpcode() == ISD::ZERO_EXTEND && 8419 (N->getOperand(0).getOpcode() == ISD::SABSDIFF || 8420 N->getOperand(0).getOpcode() == ISD::UABSDIFF)) { 8421 SDNode *ABDNode = N->getOperand(0).getNode(); 8422 SDValue NewABD = tryCombineLongOpWithDup(ABDNode, DCI, DAG); 8423 if (!NewABD.getNode()) 8424 return SDValue(); 8425 8426 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), N->getValueType(0), 8427 NewABD); 8428 } 8429 8430 // This is effectively a custom type legalization for AArch64. 8431 // 8432 // Type legalization will split an extend of a small, legal, type to a larger 8433 // illegal type by first splitting the destination type, often creating 8434 // illegal source types, which then get legalized in isel-confusing ways, 8435 // leading to really terrible codegen. E.g., 8436 // %result = v8i32 sext v8i8 %value 8437 // becomes 8438 // %losrc = extract_subreg %value, ... 8439 // %hisrc = extract_subreg %value, ... 8440 // %lo = v4i32 sext v4i8 %losrc 8441 // %hi = v4i32 sext v4i8 %hisrc 8442 // Things go rapidly downhill from there. 8443 // 8444 // For AArch64, the [sz]ext vector instructions can only go up one element 8445 // size, so we can, e.g., extend from i8 to i16, but to go from i8 to i32 8446 // take two instructions. 8447 // 8448 // This implies that the most efficient way to do the extend from v8i8 8449 // to two v4i32 values is to first extend the v8i8 to v8i16, then do 8450 // the normal splitting to happen for the v8i16->v8i32. 8451 8452 // This is pre-legalization to catch some cases where the default 8453 // type legalization will create ill-tempered code. 8454 if (!DCI.isBeforeLegalizeOps()) 8455 return SDValue(); 8456 8457 // We're only interested in cleaning things up for non-legal vector types 8458 // here. If both the source and destination are legal, things will just 8459 // work naturally without any fiddling. 8460 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8461 EVT ResVT = N->getValueType(0); 8462 if (!ResVT.isVector() || TLI.isTypeLegal(ResVT)) 8463 return SDValue(); 8464 // If the vector type isn't a simple VT, it's beyond the scope of what 8465 // we're worried about here. Let legalization do its thing and hope for 8466 // the best. 8467 SDValue Src = N->getOperand(0); 8468 EVT SrcVT = Src->getValueType(0); 8469 if (!ResVT.isSimple() || !SrcVT.isSimple()) 8470 return SDValue(); 8471 8472 // If the source VT is a 64-bit vector, we can play games and get the 8473 // better results we want. 8474 if (SrcVT.getSizeInBits() != 64) 8475 return SDValue(); 8476 8477 unsigned SrcEltSize = SrcVT.getVectorElementType().getSizeInBits(); 8478 unsigned ElementCount = SrcVT.getVectorNumElements(); 8479 SrcVT = MVT::getVectorVT(MVT::getIntegerVT(SrcEltSize * 2), ElementCount); 8480 SDLoc DL(N); 8481 Src = DAG.getNode(N->getOpcode(), DL, SrcVT, Src); 8482 8483 // Now split the rest of the operation into two halves, each with a 64 8484 // bit source. 8485 EVT LoVT, HiVT; 8486 SDValue Lo, Hi; 8487 unsigned NumElements = ResVT.getVectorNumElements(); 8488 assert(!(NumElements & 1) && "Splitting vector, but not in half!"); 8489 LoVT = HiVT = EVT::getVectorVT(*DAG.getContext(), 8490 ResVT.getVectorElementType(), NumElements / 2); 8491 8492 EVT InNVT = EVT::getVectorVT(*DAG.getContext(), SrcVT.getVectorElementType(), 8493 LoVT.getVectorNumElements()); 8494 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, Src, 8495 DAG.getConstant(0, DL, MVT::i64)); 8496 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, Src, 8497 DAG.getConstant(InNVT.getVectorNumElements(), DL, MVT::i64)); 8498 Lo = DAG.getNode(N->getOpcode(), DL, LoVT, Lo); 8499 Hi = DAG.getNode(N->getOpcode(), DL, HiVT, Hi); 8500 8501 // Now combine the parts back together so we still have a single result 8502 // like the combiner expects. 8503 return DAG.getNode(ISD::CONCAT_VECTORS, DL, ResVT, Lo, Hi); 8504 } 8505 8506 /// Replace a splat of a scalar to a vector store by scalar stores of the scalar 8507 /// value. The load store optimizer pass will merge them to store pair stores. 8508 /// This has better performance than a splat of the scalar followed by a split 8509 /// vector store. Even if the stores are not merged it is four stores vs a dup, 8510 /// followed by an ext.b and two stores. 8511 static SDValue replaceSplatVectorStore(SelectionDAG &DAG, StoreSDNode *St) { 8512 SDValue StVal = St->getValue(); 8513 EVT VT = StVal.getValueType(); 8514 8515 // Don't replace floating point stores, they possibly won't be transformed to 8516 // stp because of the store pair suppress pass. 8517 if (VT.isFloatingPoint()) 8518 return SDValue(); 8519 8520 // Check for insert vector elements. 8521 if (StVal.getOpcode() != ISD::INSERT_VECTOR_ELT) 8522 return SDValue(); 8523 8524 // We can express a splat as store pair(s) for 2 or 4 elements. 8525 unsigned NumVecElts = VT.getVectorNumElements(); 8526 if (NumVecElts != 4 && NumVecElts != 2) 8527 return SDValue(); 8528 SDValue SplatVal = StVal.getOperand(1); 8529 unsigned RemainInsertElts = NumVecElts - 1; 8530 8531 // Check that this is a splat. 8532 while (--RemainInsertElts) { 8533 SDValue NextInsertElt = StVal.getOperand(0); 8534 if (NextInsertElt.getOpcode() != ISD::INSERT_VECTOR_ELT) 8535 return SDValue(); 8536 if (NextInsertElt.getOperand(1) != SplatVal) 8537 return SDValue(); 8538 StVal = NextInsertElt; 8539 } 8540 unsigned OrigAlignment = St->getAlignment(); 8541 unsigned EltOffset = NumVecElts == 4 ? 4 : 8; 8542 unsigned Alignment = std::min(OrigAlignment, EltOffset); 8543 8544 // Create scalar stores. This is at least as good as the code sequence for a 8545 // split unaligned store which is a dup.s, ext.b, and two stores. 8546 // Most of the time the three stores should be replaced by store pair 8547 // instructions (stp). 8548 SDLoc DL(St); 8549 SDValue BasePtr = St->getBasePtr(); 8550 SDValue NewST1 = 8551 DAG.getStore(St->getChain(), DL, SplatVal, BasePtr, St->getPointerInfo(), 8552 St->isVolatile(), St->isNonTemporal(), St->getAlignment()); 8553 8554 unsigned Offset = EltOffset; 8555 while (--NumVecElts) { 8556 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr, 8557 DAG.getConstant(Offset, DL, MVT::i64)); 8558 NewST1 = DAG.getStore(NewST1.getValue(0), DL, SplatVal, OffsetPtr, 8559 St->getPointerInfo(), St->isVolatile(), 8560 St->isNonTemporal(), Alignment); 8561 Offset += EltOffset; 8562 } 8563 return NewST1; 8564 } 8565 8566 static SDValue split16BStores(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, 8567 SelectionDAG &DAG, 8568 const AArch64Subtarget *Subtarget) { 8569 if (!DCI.isBeforeLegalize()) 8570 return SDValue(); 8571 8572 StoreSDNode *S = cast<StoreSDNode>(N); 8573 if (S->isVolatile()) 8574 return SDValue(); 8575 8576 // FIXME: The logic for deciding if an unaligned store should be split should 8577 // be included in TLI.allowsMisalignedMemoryAccesses(), and there should be 8578 // a call to that function here. 8579 8580 // Cyclone has bad performance on unaligned 16B stores when crossing line and 8581 // page boundaries. We want to split such stores. 8582 if (!Subtarget->isCyclone()) 8583 return SDValue(); 8584 8585 // Don't split at -Oz. 8586 if (DAG.getMachineFunction().getFunction()->optForMinSize()) 8587 return SDValue(); 8588 8589 SDValue StVal = S->getValue(); 8590 EVT VT = StVal.getValueType(); 8591 8592 // Don't split v2i64 vectors. Memcpy lowering produces those and splitting 8593 // those up regresses performance on micro-benchmarks and olden/bh. 8594 if (!VT.isVector() || VT.getVectorNumElements() < 2 || VT == MVT::v2i64) 8595 return SDValue(); 8596 8597 // Split unaligned 16B stores. They are terrible for performance. 8598 // Don't split stores with alignment of 1 or 2. Code that uses clang vector 8599 // extensions can use this to mark that it does not want splitting to happen 8600 // (by underspecifying alignment to be 1 or 2). Furthermore, the chance of 8601 // eliminating alignment hazards is only 1 in 8 for alignment of 2. 8602 if (VT.getSizeInBits() != 128 || S->getAlignment() >= 16 || 8603 S->getAlignment() <= 2) 8604 return SDValue(); 8605 8606 // If we get a splat of a scalar convert this vector store to a store of 8607 // scalars. They will be merged into store pairs thereby removing two 8608 // instructions. 8609 if (SDValue ReplacedSplat = replaceSplatVectorStore(DAG, S)) 8610 return ReplacedSplat; 8611 8612 SDLoc DL(S); 8613 unsigned NumElts = VT.getVectorNumElements() / 2; 8614 // Split VT into two. 8615 EVT HalfVT = 8616 EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(), NumElts); 8617 SDValue SubVector0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HalfVT, StVal, 8618 DAG.getConstant(0, DL, MVT::i64)); 8619 SDValue SubVector1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HalfVT, StVal, 8620 DAG.getConstant(NumElts, DL, MVT::i64)); 8621 SDValue BasePtr = S->getBasePtr(); 8622 SDValue NewST1 = 8623 DAG.getStore(S->getChain(), DL, SubVector0, BasePtr, S->getPointerInfo(), 8624 S->isVolatile(), S->isNonTemporal(), S->getAlignment()); 8625 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr, 8626 DAG.getConstant(8, DL, MVT::i64)); 8627 return DAG.getStore(NewST1.getValue(0), DL, SubVector1, OffsetPtr, 8628 S->getPointerInfo(), S->isVolatile(), S->isNonTemporal(), 8629 S->getAlignment()); 8630 } 8631 8632 /// Target-specific DAG combine function for post-increment LD1 (lane) and 8633 /// post-increment LD1R. 8634 static SDValue performPostLD1Combine(SDNode *N, 8635 TargetLowering::DAGCombinerInfo &DCI, 8636 bool IsLaneOp) { 8637 if (DCI.isBeforeLegalizeOps()) 8638 return SDValue(); 8639 8640 SelectionDAG &DAG = DCI.DAG; 8641 EVT VT = N->getValueType(0); 8642 8643 unsigned LoadIdx = IsLaneOp ? 1 : 0; 8644 SDNode *LD = N->getOperand(LoadIdx).getNode(); 8645 // If it is not LOAD, can not do such combine. 8646 if (LD->getOpcode() != ISD::LOAD) 8647 return SDValue(); 8648 8649 LoadSDNode *LoadSDN = cast<LoadSDNode>(LD); 8650 EVT MemVT = LoadSDN->getMemoryVT(); 8651 // Check if memory operand is the same type as the vector element. 8652 if (MemVT != VT.getVectorElementType()) 8653 return SDValue(); 8654 8655 // Check if there are other uses. If so, do not combine as it will introduce 8656 // an extra load. 8657 for (SDNode::use_iterator UI = LD->use_begin(), UE = LD->use_end(); UI != UE; 8658 ++UI) { 8659 if (UI.getUse().getResNo() == 1) // Ignore uses of the chain result. 8660 continue; 8661 if (*UI != N) 8662 return SDValue(); 8663 } 8664 8665 SDValue Addr = LD->getOperand(1); 8666 SDValue Vector = N->getOperand(0); 8667 // Search for a use of the address operand that is an increment. 8668 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), UE = 8669 Addr.getNode()->use_end(); UI != UE; ++UI) { 8670 SDNode *User = *UI; 8671 if (User->getOpcode() != ISD::ADD 8672 || UI.getUse().getResNo() != Addr.getResNo()) 8673 continue; 8674 8675 // Check that the add is independent of the load. Otherwise, folding it 8676 // would create a cycle. 8677 if (User->isPredecessorOf(LD) || LD->isPredecessorOf(User)) 8678 continue; 8679 // Also check that add is not used in the vector operand. This would also 8680 // create a cycle. 8681 if (User->isPredecessorOf(Vector.getNode())) 8682 continue; 8683 8684 // If the increment is a constant, it must match the memory ref size. 8685 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0); 8686 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) { 8687 uint32_t IncVal = CInc->getZExtValue(); 8688 unsigned NumBytes = VT.getScalarSizeInBits() / 8; 8689 if (IncVal != NumBytes) 8690 continue; 8691 Inc = DAG.getRegister(AArch64::XZR, MVT::i64); 8692 } 8693 8694 // Finally, check that the vector doesn't depend on the load. 8695 // Again, this would create a cycle. 8696 // The load depending on the vector is fine, as that's the case for the 8697 // LD1*post we'll eventually generate anyway. 8698 if (LoadSDN->isPredecessorOf(Vector.getNode())) 8699 continue; 8700 8701 SmallVector<SDValue, 8> Ops; 8702 Ops.push_back(LD->getOperand(0)); // Chain 8703 if (IsLaneOp) { 8704 Ops.push_back(Vector); // The vector to be inserted 8705 Ops.push_back(N->getOperand(2)); // The lane to be inserted in the vector 8706 } 8707 Ops.push_back(Addr); 8708 Ops.push_back(Inc); 8709 8710 EVT Tys[3] = { VT, MVT::i64, MVT::Other }; 8711 SDVTList SDTys = DAG.getVTList(Tys); 8712 unsigned NewOp = IsLaneOp ? AArch64ISD::LD1LANEpost : AArch64ISD::LD1DUPpost; 8713 SDValue UpdN = DAG.getMemIntrinsicNode(NewOp, SDLoc(N), SDTys, Ops, 8714 MemVT, 8715 LoadSDN->getMemOperand()); 8716 8717 // Update the uses. 8718 SmallVector<SDValue, 2> NewResults; 8719 NewResults.push_back(SDValue(LD, 0)); // The result of load 8720 NewResults.push_back(SDValue(UpdN.getNode(), 2)); // Chain 8721 DCI.CombineTo(LD, NewResults); 8722 DCI.CombineTo(N, SDValue(UpdN.getNode(), 0)); // Dup/Inserted Result 8723 DCI.CombineTo(User, SDValue(UpdN.getNode(), 1)); // Write back register 8724 8725 break; 8726 } 8727 return SDValue(); 8728 } 8729 8730 /// Simplify \Addr given that the top byte of it is ignored by HW during 8731 /// address translation. 8732 static bool performTBISimplification(SDValue Addr, 8733 TargetLowering::DAGCombinerInfo &DCI, 8734 SelectionDAG &DAG) { 8735 APInt DemandedMask = APInt::getLowBitsSet(64, 56); 8736 APInt KnownZero, KnownOne; 8737 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(), 8738 DCI.isBeforeLegalizeOps()); 8739 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8740 if (TLI.SimplifyDemandedBits(Addr, DemandedMask, KnownZero, KnownOne, TLO)) { 8741 DCI.CommitTargetLoweringOpt(TLO); 8742 return true; 8743 } 8744 return false; 8745 } 8746 8747 static SDValue performSTORECombine(SDNode *N, 8748 TargetLowering::DAGCombinerInfo &DCI, 8749 SelectionDAG &DAG, 8750 const AArch64Subtarget *Subtarget) { 8751 SDValue Split = split16BStores(N, DCI, DAG, Subtarget); 8752 if (Split.getNode()) 8753 return Split; 8754 8755 if (Subtarget->supportsAddressTopByteIgnored() && 8756 performTBISimplification(N->getOperand(2), DCI, DAG)) 8757 return SDValue(N, 0); 8758 8759 return SDValue(); 8760 } 8761 8762 /// This function handles the log2-shuffle pattern produced by the 8763 /// LoopVectorizer for the across vector reduction. It consists of 8764 /// log2(NumVectorElements) steps and, in each step, 2^(s) elements 8765 /// are reduced, where s is an induction variable from 0 to 8766 /// log2(NumVectorElements). 8767 static SDValue tryMatchAcrossLaneShuffleForReduction(SDNode *N, SDValue OpV, 8768 unsigned Op, 8769 SelectionDAG &DAG) { 8770 EVT VTy = OpV->getOperand(0).getValueType(); 8771 if (!VTy.isVector()) 8772 return SDValue(); 8773 8774 int NumVecElts = VTy.getVectorNumElements(); 8775 if (Op == ISD::FMAXNUM || Op == ISD::FMINNUM) { 8776 if (NumVecElts != 4) 8777 return SDValue(); 8778 } else { 8779 if (NumVecElts != 4 && NumVecElts != 8 && NumVecElts != 16) 8780 return SDValue(); 8781 } 8782 8783 int NumExpectedSteps = APInt(8, NumVecElts).logBase2(); 8784 SDValue PreOp = OpV; 8785 // Iterate over each step of the across vector reduction. 8786 for (int CurStep = 0; CurStep != NumExpectedSteps; ++CurStep) { 8787 SDValue CurOp = PreOp.getOperand(0); 8788 SDValue Shuffle = PreOp.getOperand(1); 8789 if (Shuffle.getOpcode() != ISD::VECTOR_SHUFFLE) { 8790 // Try to swap the 1st and 2nd operand as add and min/max instructions 8791 // are commutative. 8792 CurOp = PreOp.getOperand(1); 8793 Shuffle = PreOp.getOperand(0); 8794 if (Shuffle.getOpcode() != ISD::VECTOR_SHUFFLE) 8795 return SDValue(); 8796 } 8797 8798 // Check if the input vector is fed by the operator we want to handle, 8799 // except the last step; the very first input vector is not necessarily 8800 // the same operator we are handling. 8801 if (CurOp.getOpcode() != Op && (CurStep != (NumExpectedSteps - 1))) 8802 return SDValue(); 8803 8804 // Check if it forms one step of the across vector reduction. 8805 // E.g., 8806 // %cur = add %1, %0 8807 // %shuffle = vector_shuffle %cur, <2, 3, u, u> 8808 // %pre = add %cur, %shuffle 8809 if (Shuffle.getOperand(0) != CurOp) 8810 return SDValue(); 8811 8812 int NumMaskElts = 1 << CurStep; 8813 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Shuffle)->getMask(); 8814 // Check mask values in each step. 8815 // We expect the shuffle mask in each step follows a specific pattern 8816 // denoted here by the <M, U> form, where M is a sequence of integers 8817 // starting from NumMaskElts, increasing by 1, and the number integers 8818 // in M should be NumMaskElts. U is a sequence of UNDEFs and the number 8819 // of undef in U should be NumVecElts - NumMaskElts. 8820 // E.g., for <8 x i16>, mask values in each step should be : 8821 // step 0 : <1,u,u,u,u,u,u,u> 8822 // step 1 : <2,3,u,u,u,u,u,u> 8823 // step 2 : <4,5,6,7,u,u,u,u> 8824 for (int i = 0; i < NumVecElts; ++i) 8825 if ((i < NumMaskElts && Mask[i] != (NumMaskElts + i)) || 8826 (i >= NumMaskElts && !(Mask[i] < 0))) 8827 return SDValue(); 8828 8829 PreOp = CurOp; 8830 } 8831 unsigned Opcode; 8832 bool IsIntrinsic = false; 8833 8834 switch (Op) { 8835 default: 8836 llvm_unreachable("Unexpected operator for across vector reduction"); 8837 case ISD::ADD: 8838 Opcode = AArch64ISD::UADDV; 8839 break; 8840 case ISD::SMAX: 8841 Opcode = AArch64ISD::SMAXV; 8842 break; 8843 case ISD::UMAX: 8844 Opcode = AArch64ISD::UMAXV; 8845 break; 8846 case ISD::SMIN: 8847 Opcode = AArch64ISD::SMINV; 8848 break; 8849 case ISD::UMIN: 8850 Opcode = AArch64ISD::UMINV; 8851 break; 8852 case ISD::FMAXNUM: 8853 Opcode = Intrinsic::aarch64_neon_fmaxnmv; 8854 IsIntrinsic = true; 8855 break; 8856 case ISD::FMINNUM: 8857 Opcode = Intrinsic::aarch64_neon_fminnmv; 8858 IsIntrinsic = true; 8859 break; 8860 } 8861 SDLoc DL(N); 8862 8863 return IsIntrinsic 8864 ? DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, N->getValueType(0), 8865 DAG.getConstant(Opcode, DL, MVT::i32), PreOp) 8866 : DAG.getNode( 8867 ISD::EXTRACT_VECTOR_ELT, DL, N->getValueType(0), 8868 DAG.getNode(Opcode, DL, PreOp.getSimpleValueType(), PreOp), 8869 DAG.getConstant(0, DL, MVT::i64)); 8870 } 8871 8872 /// Target-specific DAG combine for the across vector min/max reductions. 8873 /// This function specifically handles the final clean-up step of the vector 8874 /// min/max reductions produced by the LoopVectorizer. It is the log2-shuffle 8875 /// pattern, which narrows down and finds the final min/max value from all 8876 /// elements of the vector. 8877 /// For example, for a <16 x i8> vector : 8878 /// svn0 = vector_shuffle %0, undef<8,9,10,11,12,13,14,15,u,u,u,u,u,u,u,u> 8879 /// %smax0 = smax %arr, svn0 8880 /// %svn1 = vector_shuffle %smax0, undef<4,5,6,7,u,u,u,u,u,u,u,u,u,u,u,u> 8881 /// %smax1 = smax %smax0, %svn1 8882 /// %svn2 = vector_shuffle %smax1, undef<2,3,u,u,u,u,u,u,u,u,u,u,u,u,u,u> 8883 /// %smax2 = smax %smax1, svn2 8884 /// %svn3 = vector_shuffle %smax2, undef<1,u,u,u,u,u,u,u,u,u,u,u,u,u,u,u> 8885 /// %sc = setcc %smax2, %svn3, gt 8886 /// %n0 = extract_vector_elt %sc, #0 8887 /// %n1 = extract_vector_elt %smax2, #0 8888 /// %n2 = extract_vector_elt $smax2, #1 8889 /// %result = select %n0, %n1, n2 8890 /// becomes : 8891 /// %1 = smaxv %0 8892 /// %result = extract_vector_elt %1, 0 8893 static SDValue 8894 performAcrossLaneMinMaxReductionCombine(SDNode *N, SelectionDAG &DAG, 8895 const AArch64Subtarget *Subtarget) { 8896 if (!Subtarget->hasNEON()) 8897 return SDValue(); 8898 8899 SDValue N0 = N->getOperand(0); 8900 SDValue IfTrue = N->getOperand(1); 8901 SDValue IfFalse = N->getOperand(2); 8902 8903 // Check if the SELECT merges up the final result of the min/max 8904 // from a vector. 8905 if (N0.getOpcode() != ISD::EXTRACT_VECTOR_ELT || 8906 IfTrue.getOpcode() != ISD::EXTRACT_VECTOR_ELT || 8907 IfFalse.getOpcode() != ISD::EXTRACT_VECTOR_ELT) 8908 return SDValue(); 8909 8910 // Expect N0 is fed by SETCC. 8911 SDValue SetCC = N0.getOperand(0); 8912 EVT SetCCVT = SetCC.getValueType(); 8913 if (SetCC.getOpcode() != ISD::SETCC || !SetCCVT.isVector() || 8914 SetCCVT.getVectorElementType() != MVT::i1) 8915 return SDValue(); 8916 8917 SDValue VectorOp = SetCC.getOperand(0); 8918 unsigned Op = VectorOp->getOpcode(); 8919 // Check if the input vector is fed by the operator we want to handle. 8920 if (Op != ISD::SMAX && Op != ISD::UMAX && Op != ISD::SMIN && 8921 Op != ISD::UMIN && Op != ISD::FMAXNUM && Op != ISD::FMINNUM) 8922 return SDValue(); 8923 8924 EVT VTy = VectorOp.getValueType(); 8925 if (!VTy.isVector()) 8926 return SDValue(); 8927 8928 if (VTy.getSizeInBits() < 64) 8929 return SDValue(); 8930 8931 EVT EltTy = VTy.getVectorElementType(); 8932 if (Op == ISD::FMAXNUM || Op == ISD::FMINNUM) { 8933 if (EltTy != MVT::f32) 8934 return SDValue(); 8935 } else { 8936 if (EltTy != MVT::i32 && EltTy != MVT::i16 && EltTy != MVT::i8) 8937 return SDValue(); 8938 } 8939 8940 // Check if extracting from the same vector. 8941 // For example, 8942 // %sc = setcc %vector, %svn1, gt 8943 // %n0 = extract_vector_elt %sc, #0 8944 // %n1 = extract_vector_elt %vector, #0 8945 // %n2 = extract_vector_elt $vector, #1 8946 if (!(VectorOp == IfTrue->getOperand(0) && 8947 VectorOp == IfFalse->getOperand(0))) 8948 return SDValue(); 8949 8950 // Check if the condition code is matched with the operator type. 8951 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get(); 8952 if ((Op == ISD::SMAX && CC != ISD::SETGT && CC != ISD::SETGE) || 8953 (Op == ISD::UMAX && CC != ISD::SETUGT && CC != ISD::SETUGE) || 8954 (Op == ISD::SMIN && CC != ISD::SETLT && CC != ISD::SETLE) || 8955 (Op == ISD::UMIN && CC != ISD::SETULT && CC != ISD::SETULE) || 8956 (Op == ISD::FMAXNUM && CC != ISD::SETOGT && CC != ISD::SETOGE && 8957 CC != ISD::SETUGT && CC != ISD::SETUGE && CC != ISD::SETGT && 8958 CC != ISD::SETGE) || 8959 (Op == ISD::FMINNUM && CC != ISD::SETOLT && CC != ISD::SETOLE && 8960 CC != ISD::SETULT && CC != ISD::SETULE && CC != ISD::SETLT && 8961 CC != ISD::SETLE)) 8962 return SDValue(); 8963 8964 // Expect to check only lane 0 from the vector SETCC. 8965 if (!isa<ConstantSDNode>(N0.getOperand(1)) || 8966 cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue() != 0) 8967 return SDValue(); 8968 8969 // Expect to extract the true value from lane 0. 8970 if (!isa<ConstantSDNode>(IfTrue.getOperand(1)) || 8971 cast<ConstantSDNode>(IfTrue.getOperand(1))->getZExtValue() != 0) 8972 return SDValue(); 8973 8974 // Expect to extract the false value from lane 1. 8975 if (!isa<ConstantSDNode>(IfFalse.getOperand(1)) || 8976 cast<ConstantSDNode>(IfFalse.getOperand(1))->getZExtValue() != 1) 8977 return SDValue(); 8978 8979 return tryMatchAcrossLaneShuffleForReduction(N, SetCC, Op, DAG); 8980 } 8981 8982 /// Target-specific DAG combine for the across vector add reduction. 8983 /// This function specifically handles the final clean-up step of the vector 8984 /// add reduction produced by the LoopVectorizer. It is the log2-shuffle 8985 /// pattern, which adds all elements of a vector together. 8986 /// For example, for a <4 x i32> vector : 8987 /// %1 = vector_shuffle %0, <2,3,u,u> 8988 /// %2 = add %0, %1 8989 /// %3 = vector_shuffle %2, <1,u,u,u> 8990 /// %4 = add %2, %3 8991 /// %result = extract_vector_elt %4, 0 8992 /// becomes : 8993 /// %0 = uaddv %0 8994 /// %result = extract_vector_elt %0, 0 8995 static SDValue 8996 performAcrossLaneAddReductionCombine(SDNode *N, SelectionDAG &DAG, 8997 const AArch64Subtarget *Subtarget) { 8998 if (!Subtarget->hasNEON()) 8999 return SDValue(); 9000 SDValue N0 = N->getOperand(0); 9001 SDValue N1 = N->getOperand(1); 9002 9003 // Check if the input vector is fed by the ADD. 9004 if (N0->getOpcode() != ISD::ADD) 9005 return SDValue(); 9006 9007 // The vector extract idx must constant zero because we only expect the final 9008 // result of the reduction is placed in lane 0. 9009 if (!isa<ConstantSDNode>(N1) || cast<ConstantSDNode>(N1)->getZExtValue() != 0) 9010 return SDValue(); 9011 9012 EVT VTy = N0.getValueType(); 9013 if (!VTy.isVector()) 9014 return SDValue(); 9015 9016 EVT EltTy = VTy.getVectorElementType(); 9017 if (EltTy != MVT::i32 && EltTy != MVT::i16 && EltTy != MVT::i8) 9018 return SDValue(); 9019 9020 if (VTy.getSizeInBits() < 64) 9021 return SDValue(); 9022 9023 return tryMatchAcrossLaneShuffleForReduction(N, N0, ISD::ADD, DAG); 9024 } 9025 9026 /// Target-specific DAG combine function for NEON load/store intrinsics 9027 /// to merge base address updates. 9028 static SDValue performNEONPostLDSTCombine(SDNode *N, 9029 TargetLowering::DAGCombinerInfo &DCI, 9030 SelectionDAG &DAG) { 9031 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 9032 return SDValue(); 9033 9034 unsigned AddrOpIdx = N->getNumOperands() - 1; 9035 SDValue Addr = N->getOperand(AddrOpIdx); 9036 9037 // Search for a use of the address operand that is an increment. 9038 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), 9039 UE = Addr.getNode()->use_end(); UI != UE; ++UI) { 9040 SDNode *User = *UI; 9041 if (User->getOpcode() != ISD::ADD || 9042 UI.getUse().getResNo() != Addr.getResNo()) 9043 continue; 9044 9045 // Check that the add is independent of the load/store. Otherwise, folding 9046 // it would create a cycle. 9047 if (User->isPredecessorOf(N) || N->isPredecessorOf(User)) 9048 continue; 9049 9050 // Find the new opcode for the updating load/store. 9051 bool IsStore = false; 9052 bool IsLaneOp = false; 9053 bool IsDupOp = false; 9054 unsigned NewOpc = 0; 9055 unsigned NumVecs = 0; 9056 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 9057 switch (IntNo) { 9058 default: llvm_unreachable("unexpected intrinsic for Neon base update"); 9059 case Intrinsic::aarch64_neon_ld2: NewOpc = AArch64ISD::LD2post; 9060 NumVecs = 2; break; 9061 case Intrinsic::aarch64_neon_ld3: NewOpc = AArch64ISD::LD3post; 9062 NumVecs = 3; break; 9063 case Intrinsic::aarch64_neon_ld4: NewOpc = AArch64ISD::LD4post; 9064 NumVecs = 4; break; 9065 case Intrinsic::aarch64_neon_st2: NewOpc = AArch64ISD::ST2post; 9066 NumVecs = 2; IsStore = true; break; 9067 case Intrinsic::aarch64_neon_st3: NewOpc = AArch64ISD::ST3post; 9068 NumVecs = 3; IsStore = true; break; 9069 case Intrinsic::aarch64_neon_st4: NewOpc = AArch64ISD::ST4post; 9070 NumVecs = 4; IsStore = true; break; 9071 case Intrinsic::aarch64_neon_ld1x2: NewOpc = AArch64ISD::LD1x2post; 9072 NumVecs = 2; break; 9073 case Intrinsic::aarch64_neon_ld1x3: NewOpc = AArch64ISD::LD1x3post; 9074 NumVecs = 3; break; 9075 case Intrinsic::aarch64_neon_ld1x4: NewOpc = AArch64ISD::LD1x4post; 9076 NumVecs = 4; break; 9077 case Intrinsic::aarch64_neon_st1x2: NewOpc = AArch64ISD::ST1x2post; 9078 NumVecs = 2; IsStore = true; break; 9079 case Intrinsic::aarch64_neon_st1x3: NewOpc = AArch64ISD::ST1x3post; 9080 NumVecs = 3; IsStore = true; break; 9081 case Intrinsic::aarch64_neon_st1x4: NewOpc = AArch64ISD::ST1x4post; 9082 NumVecs = 4; IsStore = true; break; 9083 case Intrinsic::aarch64_neon_ld2r: NewOpc = AArch64ISD::LD2DUPpost; 9084 NumVecs = 2; IsDupOp = true; break; 9085 case Intrinsic::aarch64_neon_ld3r: NewOpc = AArch64ISD::LD3DUPpost; 9086 NumVecs = 3; IsDupOp = true; break; 9087 case Intrinsic::aarch64_neon_ld4r: NewOpc = AArch64ISD::LD4DUPpost; 9088 NumVecs = 4; IsDupOp = true; break; 9089 case Intrinsic::aarch64_neon_ld2lane: NewOpc = AArch64ISD::LD2LANEpost; 9090 NumVecs = 2; IsLaneOp = true; break; 9091 case Intrinsic::aarch64_neon_ld3lane: NewOpc = AArch64ISD::LD3LANEpost; 9092 NumVecs = 3; IsLaneOp = true; break; 9093 case Intrinsic::aarch64_neon_ld4lane: NewOpc = AArch64ISD::LD4LANEpost; 9094 NumVecs = 4; IsLaneOp = true; break; 9095 case Intrinsic::aarch64_neon_st2lane: NewOpc = AArch64ISD::ST2LANEpost; 9096 NumVecs = 2; IsStore = true; IsLaneOp = true; break; 9097 case Intrinsic::aarch64_neon_st3lane: NewOpc = AArch64ISD::ST3LANEpost; 9098 NumVecs = 3; IsStore = true; IsLaneOp = true; break; 9099 case Intrinsic::aarch64_neon_st4lane: NewOpc = AArch64ISD::ST4LANEpost; 9100 NumVecs = 4; IsStore = true; IsLaneOp = true; break; 9101 } 9102 9103 EVT VecTy; 9104 if (IsStore) 9105 VecTy = N->getOperand(2).getValueType(); 9106 else 9107 VecTy = N->getValueType(0); 9108 9109 // If the increment is a constant, it must match the memory ref size. 9110 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0); 9111 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) { 9112 uint32_t IncVal = CInc->getZExtValue(); 9113 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8; 9114 if (IsLaneOp || IsDupOp) 9115 NumBytes /= VecTy.getVectorNumElements(); 9116 if (IncVal != NumBytes) 9117 continue; 9118 Inc = DAG.getRegister(AArch64::XZR, MVT::i64); 9119 } 9120 SmallVector<SDValue, 8> Ops; 9121 Ops.push_back(N->getOperand(0)); // Incoming chain 9122 // Load lane and store have vector list as input. 9123 if (IsLaneOp || IsStore) 9124 for (unsigned i = 2; i < AddrOpIdx; ++i) 9125 Ops.push_back(N->getOperand(i)); 9126 Ops.push_back(Addr); // Base register 9127 Ops.push_back(Inc); 9128 9129 // Return Types. 9130 EVT Tys[6]; 9131 unsigned NumResultVecs = (IsStore ? 0 : NumVecs); 9132 unsigned n; 9133 for (n = 0; n < NumResultVecs; ++n) 9134 Tys[n] = VecTy; 9135 Tys[n++] = MVT::i64; // Type of write back register 9136 Tys[n] = MVT::Other; // Type of the chain 9137 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs + 2)); 9138 9139 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N); 9140 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys, Ops, 9141 MemInt->getMemoryVT(), 9142 MemInt->getMemOperand()); 9143 9144 // Update the uses. 9145 std::vector<SDValue> NewResults; 9146 for (unsigned i = 0; i < NumResultVecs; ++i) { 9147 NewResults.push_back(SDValue(UpdN.getNode(), i)); 9148 } 9149 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs + 1)); 9150 DCI.CombineTo(N, NewResults); 9151 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs)); 9152 9153 break; 9154 } 9155 return SDValue(); 9156 } 9157 9158 // Checks to see if the value is the prescribed width and returns information 9159 // about its extension mode. 9160 static 9161 bool checkValueWidth(SDValue V, unsigned width, ISD::LoadExtType &ExtType) { 9162 ExtType = ISD::NON_EXTLOAD; 9163 switch(V.getNode()->getOpcode()) { 9164 default: 9165 return false; 9166 case ISD::LOAD: { 9167 LoadSDNode *LoadNode = cast<LoadSDNode>(V.getNode()); 9168 if ((LoadNode->getMemoryVT() == MVT::i8 && width == 8) 9169 || (LoadNode->getMemoryVT() == MVT::i16 && width == 16)) { 9170 ExtType = LoadNode->getExtensionType(); 9171 return true; 9172 } 9173 return false; 9174 } 9175 case ISD::AssertSext: { 9176 VTSDNode *TypeNode = cast<VTSDNode>(V.getNode()->getOperand(1)); 9177 if ((TypeNode->getVT() == MVT::i8 && width == 8) 9178 || (TypeNode->getVT() == MVT::i16 && width == 16)) { 9179 ExtType = ISD::SEXTLOAD; 9180 return true; 9181 } 9182 return false; 9183 } 9184 case ISD::AssertZext: { 9185 VTSDNode *TypeNode = cast<VTSDNode>(V.getNode()->getOperand(1)); 9186 if ((TypeNode->getVT() == MVT::i8 && width == 8) 9187 || (TypeNode->getVT() == MVT::i16 && width == 16)) { 9188 ExtType = ISD::ZEXTLOAD; 9189 return true; 9190 } 9191 return false; 9192 } 9193 case ISD::Constant: 9194 case ISD::TargetConstant: { 9195 if (std::abs(cast<ConstantSDNode>(V.getNode())->getSExtValue()) < 9196 1LL << (width - 1)) 9197 return true; 9198 return false; 9199 } 9200 } 9201 9202 return true; 9203 } 9204 9205 // This function does a whole lot of voodoo to determine if the tests are 9206 // equivalent without and with a mask. Essentially what happens is that given a 9207 // DAG resembling: 9208 // 9209 // +-------------+ +-------------+ +-------------+ +-------------+ 9210 // | Input | | AddConstant | | CompConstant| | CC | 9211 // +-------------+ +-------------+ +-------------+ +-------------+ 9212 // | | | | 9213 // V V | +----------+ 9214 // +-------------+ +----+ | | 9215 // | ADD | |0xff| | | 9216 // +-------------+ +----+ | | 9217 // | | | | 9218 // V V | | 9219 // +-------------+ | | 9220 // | AND | | | 9221 // +-------------+ | | 9222 // | | | 9223 // +-----+ | | 9224 // | | | 9225 // V V V 9226 // +-------------+ 9227 // | CMP | 9228 // +-------------+ 9229 // 9230 // The AND node may be safely removed for some combinations of inputs. In 9231 // particular we need to take into account the extension type of the Input, 9232 // the exact values of AddConstant, CompConstant, and CC, along with the nominal 9233 // width of the input (this can work for any width inputs, the above graph is 9234 // specific to 8 bits. 9235 // 9236 // The specific equations were worked out by generating output tables for each 9237 // AArch64CC value in terms of and AddConstant (w1), CompConstant(w2). The 9238 // problem was simplified by working with 4 bit inputs, which means we only 9239 // needed to reason about 24 distinct bit patterns: 8 patterns unique to zero 9240 // extension (8,15), 8 patterns unique to sign extensions (-8,-1), and 8 9241 // patterns present in both extensions (0,7). For every distinct set of 9242 // AddConstant and CompConstants bit patterns we can consider the masked and 9243 // unmasked versions to be equivalent if the result of this function is true for 9244 // all 16 distinct bit patterns of for the current extension type of Input (w0). 9245 // 9246 // sub w8, w0, w1 9247 // and w10, w8, #0x0f 9248 // cmp w8, w2 9249 // cset w9, AArch64CC 9250 // cmp w10, w2 9251 // cset w11, AArch64CC 9252 // cmp w9, w11 9253 // cset w0, eq 9254 // ret 9255 // 9256 // Since the above function shows when the outputs are equivalent it defines 9257 // when it is safe to remove the AND. Unfortunately it only runs on AArch64 and 9258 // would be expensive to run during compiles. The equations below were written 9259 // in a test harness that confirmed they gave equivalent outputs to the above 9260 // for all inputs function, so they can be used determine if the removal is 9261 // legal instead. 9262 // 9263 // isEquivalentMaskless() is the code for testing if the AND can be removed 9264 // factored out of the DAG recognition as the DAG can take several forms. 9265 9266 static 9267 bool isEquivalentMaskless(unsigned CC, unsigned width, 9268 ISD::LoadExtType ExtType, signed AddConstant, 9269 signed CompConstant) { 9270 // By being careful about our equations and only writing the in term 9271 // symbolic values and well known constants (0, 1, -1, MaxUInt) we can 9272 // make them generally applicable to all bit widths. 9273 signed MaxUInt = (1 << width); 9274 9275 // For the purposes of these comparisons sign extending the type is 9276 // equivalent to zero extending the add and displacing it by half the integer 9277 // width. Provided we are careful and make sure our equations are valid over 9278 // the whole range we can just adjust the input and avoid writing equations 9279 // for sign extended inputs. 9280 if (ExtType == ISD::SEXTLOAD) 9281 AddConstant -= (1 << (width-1)); 9282 9283 switch(CC) { 9284 case AArch64CC::LE: 9285 case AArch64CC::GT: { 9286 if ((AddConstant == 0) || 9287 (CompConstant == MaxUInt - 1 && AddConstant < 0) || 9288 (AddConstant >= 0 && CompConstant < 0) || 9289 (AddConstant <= 0 && CompConstant <= 0 && CompConstant < AddConstant)) 9290 return true; 9291 } break; 9292 case AArch64CC::LT: 9293 case AArch64CC::GE: { 9294 if ((AddConstant == 0) || 9295 (AddConstant >= 0 && CompConstant <= 0) || 9296 (AddConstant <= 0 && CompConstant <= 0 && CompConstant <= AddConstant)) 9297 return true; 9298 } break; 9299 case AArch64CC::HI: 9300 case AArch64CC::LS: { 9301 if ((AddConstant >= 0 && CompConstant < 0) || 9302 (AddConstant <= 0 && CompConstant >= -1 && 9303 CompConstant < AddConstant + MaxUInt)) 9304 return true; 9305 } break; 9306 case AArch64CC::PL: 9307 case AArch64CC::MI: { 9308 if ((AddConstant == 0) || 9309 (AddConstant > 0 && CompConstant <= 0) || 9310 (AddConstant < 0 && CompConstant <= AddConstant)) 9311 return true; 9312 } break; 9313 case AArch64CC::LO: 9314 case AArch64CC::HS: { 9315 if ((AddConstant >= 0 && CompConstant <= 0) || 9316 (AddConstant <= 0 && CompConstant >= 0 && 9317 CompConstant <= AddConstant + MaxUInt)) 9318 return true; 9319 } break; 9320 case AArch64CC::EQ: 9321 case AArch64CC::NE: { 9322 if ((AddConstant > 0 && CompConstant < 0) || 9323 (AddConstant < 0 && CompConstant >= 0 && 9324 CompConstant < AddConstant + MaxUInt) || 9325 (AddConstant >= 0 && CompConstant >= 0 && 9326 CompConstant >= AddConstant) || 9327 (AddConstant <= 0 && CompConstant < 0 && CompConstant < AddConstant)) 9328 9329 return true; 9330 } break; 9331 case AArch64CC::VS: 9332 case AArch64CC::VC: 9333 case AArch64CC::AL: 9334 case AArch64CC::NV: 9335 return true; 9336 case AArch64CC::Invalid: 9337 break; 9338 } 9339 9340 return false; 9341 } 9342 9343 static 9344 SDValue performCONDCombine(SDNode *N, 9345 TargetLowering::DAGCombinerInfo &DCI, 9346 SelectionDAG &DAG, unsigned CCIndex, 9347 unsigned CmpIndex) { 9348 unsigned CC = cast<ConstantSDNode>(N->getOperand(CCIndex))->getSExtValue(); 9349 SDNode *SubsNode = N->getOperand(CmpIndex).getNode(); 9350 unsigned CondOpcode = SubsNode->getOpcode(); 9351 9352 if (CondOpcode != AArch64ISD::SUBS) 9353 return SDValue(); 9354 9355 // There is a SUBS feeding this condition. Is it fed by a mask we can 9356 // use? 9357 9358 SDNode *AndNode = SubsNode->getOperand(0).getNode(); 9359 unsigned MaskBits = 0; 9360 9361 if (AndNode->getOpcode() != ISD::AND) 9362 return SDValue(); 9363 9364 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(AndNode->getOperand(1))) { 9365 uint32_t CNV = CN->getZExtValue(); 9366 if (CNV == 255) 9367 MaskBits = 8; 9368 else if (CNV == 65535) 9369 MaskBits = 16; 9370 } 9371 9372 if (!MaskBits) 9373 return SDValue(); 9374 9375 SDValue AddValue = AndNode->getOperand(0); 9376 9377 if (AddValue.getOpcode() != ISD::ADD) 9378 return SDValue(); 9379 9380 // The basic dag structure is correct, grab the inputs and validate them. 9381 9382 SDValue AddInputValue1 = AddValue.getNode()->getOperand(0); 9383 SDValue AddInputValue2 = AddValue.getNode()->getOperand(1); 9384 SDValue SubsInputValue = SubsNode->getOperand(1); 9385 9386 // The mask is present and the provenance of all the values is a smaller type, 9387 // lets see if the mask is superfluous. 9388 9389 if (!isa<ConstantSDNode>(AddInputValue2.getNode()) || 9390 !isa<ConstantSDNode>(SubsInputValue.getNode())) 9391 return SDValue(); 9392 9393 ISD::LoadExtType ExtType; 9394 9395 if (!checkValueWidth(SubsInputValue, MaskBits, ExtType) || 9396 !checkValueWidth(AddInputValue2, MaskBits, ExtType) || 9397 !checkValueWidth(AddInputValue1, MaskBits, ExtType) ) 9398 return SDValue(); 9399 9400 if(!isEquivalentMaskless(CC, MaskBits, ExtType, 9401 cast<ConstantSDNode>(AddInputValue2.getNode())->getSExtValue(), 9402 cast<ConstantSDNode>(SubsInputValue.getNode())->getSExtValue())) 9403 return SDValue(); 9404 9405 // The AND is not necessary, remove it. 9406 9407 SDVTList VTs = DAG.getVTList(SubsNode->getValueType(0), 9408 SubsNode->getValueType(1)); 9409 SDValue Ops[] = { AddValue, SubsNode->getOperand(1) }; 9410 9411 SDValue NewValue = DAG.getNode(CondOpcode, SDLoc(SubsNode), VTs, Ops); 9412 DAG.ReplaceAllUsesWith(SubsNode, NewValue.getNode()); 9413 9414 return SDValue(N, 0); 9415 } 9416 9417 // Optimize compare with zero and branch. 9418 static SDValue performBRCONDCombine(SDNode *N, 9419 TargetLowering::DAGCombinerInfo &DCI, 9420 SelectionDAG &DAG) { 9421 SDValue NV = performCONDCombine(N, DCI, DAG, 2, 3); 9422 if (NV.getNode()) 9423 N = NV.getNode(); 9424 SDValue Chain = N->getOperand(0); 9425 SDValue Dest = N->getOperand(1); 9426 SDValue CCVal = N->getOperand(2); 9427 SDValue Cmp = N->getOperand(3); 9428 9429 assert(isa<ConstantSDNode>(CCVal) && "Expected a ConstantSDNode here!"); 9430 unsigned CC = cast<ConstantSDNode>(CCVal)->getZExtValue(); 9431 if (CC != AArch64CC::EQ && CC != AArch64CC::NE) 9432 return SDValue(); 9433 9434 unsigned CmpOpc = Cmp.getOpcode(); 9435 if (CmpOpc != AArch64ISD::ADDS && CmpOpc != AArch64ISD::SUBS) 9436 return SDValue(); 9437 9438 // Only attempt folding if there is only one use of the flag and no use of the 9439 // value. 9440 if (!Cmp->hasNUsesOfValue(0, 0) || !Cmp->hasNUsesOfValue(1, 1)) 9441 return SDValue(); 9442 9443 SDValue LHS = Cmp.getOperand(0); 9444 SDValue RHS = Cmp.getOperand(1); 9445 9446 assert(LHS.getValueType() == RHS.getValueType() && 9447 "Expected the value type to be the same for both operands!"); 9448 if (LHS.getValueType() != MVT::i32 && LHS.getValueType() != MVT::i64) 9449 return SDValue(); 9450 9451 if (isa<ConstantSDNode>(LHS) && cast<ConstantSDNode>(LHS)->isNullValue()) 9452 std::swap(LHS, RHS); 9453 9454 if (!isa<ConstantSDNode>(RHS) || !cast<ConstantSDNode>(RHS)->isNullValue()) 9455 return SDValue(); 9456 9457 if (LHS.getOpcode() == ISD::SHL || LHS.getOpcode() == ISD::SRA || 9458 LHS.getOpcode() == ISD::SRL) 9459 return SDValue(); 9460 9461 // Fold the compare into the branch instruction. 9462 SDValue BR; 9463 if (CC == AArch64CC::EQ) 9464 BR = DAG.getNode(AArch64ISD::CBZ, SDLoc(N), MVT::Other, Chain, LHS, Dest); 9465 else 9466 BR = DAG.getNode(AArch64ISD::CBNZ, SDLoc(N), MVT::Other, Chain, LHS, Dest); 9467 9468 // Do not add new nodes to DAG combiner worklist. 9469 DCI.CombineTo(N, BR, false); 9470 9471 return SDValue(); 9472 } 9473 9474 // vselect (v1i1 setcc) -> 9475 // vselect (v1iXX setcc) (XX is the size of the compared operand type) 9476 // FIXME: Currently the type legalizer can't handle VSELECT having v1i1 as 9477 // condition. If it can legalize "VSELECT v1i1" correctly, no need to combine 9478 // such VSELECT. 9479 static SDValue performVSelectCombine(SDNode *N, SelectionDAG &DAG) { 9480 SDValue N0 = N->getOperand(0); 9481 EVT CCVT = N0.getValueType(); 9482 9483 if (N0.getOpcode() != ISD::SETCC || CCVT.getVectorNumElements() != 1 || 9484 CCVT.getVectorElementType() != MVT::i1) 9485 return SDValue(); 9486 9487 EVT ResVT = N->getValueType(0); 9488 EVT CmpVT = N0.getOperand(0).getValueType(); 9489 // Only combine when the result type is of the same size as the compared 9490 // operands. 9491 if (ResVT.getSizeInBits() != CmpVT.getSizeInBits()) 9492 return SDValue(); 9493 9494 SDValue IfTrue = N->getOperand(1); 9495 SDValue IfFalse = N->getOperand(2); 9496 SDValue SetCC = 9497 DAG.getSetCC(SDLoc(N), CmpVT.changeVectorElementTypeToInteger(), 9498 N0.getOperand(0), N0.getOperand(1), 9499 cast<CondCodeSDNode>(N0.getOperand(2))->get()); 9500 return DAG.getNode(ISD::VSELECT, SDLoc(N), ResVT, SetCC, 9501 IfTrue, IfFalse); 9502 } 9503 9504 /// A vector select: "(select vL, vR, (setcc LHS, RHS))" is best performed with 9505 /// the compare-mask instructions rather than going via NZCV, even if LHS and 9506 /// RHS are really scalar. This replaces any scalar setcc in the above pattern 9507 /// with a vector one followed by a DUP shuffle on the result. 9508 static SDValue performSelectCombine(SDNode *N, 9509 TargetLowering::DAGCombinerInfo &DCI) { 9510 SelectionDAG &DAG = DCI.DAG; 9511 SDValue N0 = N->getOperand(0); 9512 EVT ResVT = N->getValueType(0); 9513 9514 if (N0.getOpcode() != ISD::SETCC) 9515 return SDValue(); 9516 9517 // Make sure the SETCC result is either i1 (initial DAG), or i32, the lowered 9518 // scalar SetCCResultType. We also don't expect vectors, because we assume 9519 // that selects fed by vector SETCCs are canonicalized to VSELECT. 9520 assert((N0.getValueType() == MVT::i1 || N0.getValueType() == MVT::i32) && 9521 "Scalar-SETCC feeding SELECT has unexpected result type!"); 9522 9523 // If NumMaskElts == 0, the comparison is larger than select result. The 9524 // largest real NEON comparison is 64-bits per lane, which means the result is 9525 // at most 32-bits and an illegal vector. Just bail out for now. 9526 EVT SrcVT = N0.getOperand(0).getValueType(); 9527 9528 // Don't try to do this optimization when the setcc itself has i1 operands. 9529 // There are no legal vectors of i1, so this would be pointless. 9530 if (SrcVT == MVT::i1) 9531 return SDValue(); 9532 9533 int NumMaskElts = ResVT.getSizeInBits() / SrcVT.getSizeInBits(); 9534 if (!ResVT.isVector() || NumMaskElts == 0) 9535 return SDValue(); 9536 9537 SrcVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumMaskElts); 9538 EVT CCVT = SrcVT.changeVectorElementTypeToInteger(); 9539 9540 // Also bail out if the vector CCVT isn't the same size as ResVT. 9541 // This can happen if the SETCC operand size doesn't divide the ResVT size 9542 // (e.g., f64 vs v3f32). 9543 if (CCVT.getSizeInBits() != ResVT.getSizeInBits()) 9544 return SDValue(); 9545 9546 // Make sure we didn't create illegal types, if we're not supposed to. 9547 assert(DCI.isBeforeLegalize() || 9548 DAG.getTargetLoweringInfo().isTypeLegal(SrcVT)); 9549 9550 // First perform a vector comparison, where lane 0 is the one we're interested 9551 // in. 9552 SDLoc DL(N0); 9553 SDValue LHS = 9554 DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, SrcVT, N0.getOperand(0)); 9555 SDValue RHS = 9556 DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, SrcVT, N0.getOperand(1)); 9557 SDValue SetCC = DAG.getNode(ISD::SETCC, DL, CCVT, LHS, RHS, N0.getOperand(2)); 9558 9559 // Now duplicate the comparison mask we want across all other lanes. 9560 SmallVector<int, 8> DUPMask(CCVT.getVectorNumElements(), 0); 9561 SDValue Mask = DAG.getVectorShuffle(CCVT, DL, SetCC, SetCC, DUPMask.data()); 9562 Mask = DAG.getNode(ISD::BITCAST, DL, 9563 ResVT.changeVectorElementTypeToInteger(), Mask); 9564 9565 return DAG.getSelect(DL, ResVT, Mask, N->getOperand(1), N->getOperand(2)); 9566 } 9567 9568 /// Get rid of unnecessary NVCASTs (that don't change the type). 9569 static SDValue performNVCASTCombine(SDNode *N) { 9570 if (N->getValueType(0) == N->getOperand(0).getValueType()) 9571 return N->getOperand(0); 9572 9573 return SDValue(); 9574 } 9575 9576 SDValue AArch64TargetLowering::PerformDAGCombine(SDNode *N, 9577 DAGCombinerInfo &DCI) const { 9578 SelectionDAG &DAG = DCI.DAG; 9579 switch (N->getOpcode()) { 9580 default: 9581 break; 9582 case ISD::ADD: 9583 case ISD::SUB: 9584 return performAddSubLongCombine(N, DCI, DAG); 9585 case ISD::XOR: 9586 return performXorCombine(N, DAG, DCI, Subtarget); 9587 case ISD::MUL: 9588 return performMulCombine(N, DAG, DCI, Subtarget); 9589 case ISD::SINT_TO_FP: 9590 case ISD::UINT_TO_FP: 9591 return performIntToFpCombine(N, DAG, Subtarget); 9592 case ISD::FP_TO_SINT: 9593 case ISD::FP_TO_UINT: 9594 return performFpToIntCombine(N, DAG, Subtarget); 9595 case ISD::FDIV: 9596 return performFDivCombine(N, DAG, Subtarget); 9597 case ISD::OR: 9598 return performORCombine(N, DCI, Subtarget); 9599 case ISD::INTRINSIC_WO_CHAIN: 9600 return performIntrinsicCombine(N, DCI, Subtarget); 9601 case ISD::ANY_EXTEND: 9602 case ISD::ZERO_EXTEND: 9603 case ISD::SIGN_EXTEND: 9604 return performExtendCombine(N, DCI, DAG); 9605 case ISD::BITCAST: 9606 return performBitcastCombine(N, DCI, DAG); 9607 case ISD::CONCAT_VECTORS: 9608 return performConcatVectorsCombine(N, DCI, DAG); 9609 case ISD::SELECT: { 9610 SDValue RV = performSelectCombine(N, DCI); 9611 if (!RV.getNode()) 9612 RV = performAcrossLaneMinMaxReductionCombine(N, DAG, Subtarget); 9613 return RV; 9614 } 9615 case ISD::VSELECT: 9616 return performVSelectCombine(N, DCI.DAG); 9617 case ISD::LOAD: 9618 if (performTBISimplification(N->getOperand(1), DCI, DAG)) 9619 return SDValue(N, 0); 9620 break; 9621 case ISD::STORE: 9622 return performSTORECombine(N, DCI, DAG, Subtarget); 9623 case AArch64ISD::BRCOND: 9624 return performBRCONDCombine(N, DCI, DAG); 9625 case AArch64ISD::CSEL: 9626 return performCONDCombine(N, DCI, DAG, 2, 3); 9627 case AArch64ISD::DUP: 9628 return performPostLD1Combine(N, DCI, false); 9629 case AArch64ISD::NVCAST: 9630 return performNVCASTCombine(N); 9631 case ISD::INSERT_VECTOR_ELT: 9632 return performPostLD1Combine(N, DCI, true); 9633 case ISD::EXTRACT_VECTOR_ELT: 9634 return performAcrossLaneAddReductionCombine(N, DAG, Subtarget); 9635 case ISD::INTRINSIC_VOID: 9636 case ISD::INTRINSIC_W_CHAIN: 9637 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 9638 case Intrinsic::aarch64_neon_ld2: 9639 case Intrinsic::aarch64_neon_ld3: 9640 case Intrinsic::aarch64_neon_ld4: 9641 case Intrinsic::aarch64_neon_ld1x2: 9642 case Intrinsic::aarch64_neon_ld1x3: 9643 case Intrinsic::aarch64_neon_ld1x4: 9644 case Intrinsic::aarch64_neon_ld2lane: 9645 case Intrinsic::aarch64_neon_ld3lane: 9646 case Intrinsic::aarch64_neon_ld4lane: 9647 case Intrinsic::aarch64_neon_ld2r: 9648 case Intrinsic::aarch64_neon_ld3r: 9649 case Intrinsic::aarch64_neon_ld4r: 9650 case Intrinsic::aarch64_neon_st2: 9651 case Intrinsic::aarch64_neon_st3: 9652 case Intrinsic::aarch64_neon_st4: 9653 case Intrinsic::aarch64_neon_st1x2: 9654 case Intrinsic::aarch64_neon_st1x3: 9655 case Intrinsic::aarch64_neon_st1x4: 9656 case Intrinsic::aarch64_neon_st2lane: 9657 case Intrinsic::aarch64_neon_st3lane: 9658 case Intrinsic::aarch64_neon_st4lane: 9659 return performNEONPostLDSTCombine(N, DCI, DAG); 9660 default: 9661 break; 9662 } 9663 } 9664 return SDValue(); 9665 } 9666 9667 // Check if the return value is used as only a return value, as otherwise 9668 // we can't perform a tail-call. In particular, we need to check for 9669 // target ISD nodes that are returns and any other "odd" constructs 9670 // that the generic analysis code won't necessarily catch. 9671 bool AArch64TargetLowering::isUsedByReturnOnly(SDNode *N, 9672 SDValue &Chain) const { 9673 if (N->getNumValues() != 1) 9674 return false; 9675 if (!N->hasNUsesOfValue(1, 0)) 9676 return false; 9677 9678 SDValue TCChain = Chain; 9679 SDNode *Copy = *N->use_begin(); 9680 if (Copy->getOpcode() == ISD::CopyToReg) { 9681 // If the copy has a glue operand, we conservatively assume it isn't safe to 9682 // perform a tail call. 9683 if (Copy->getOperand(Copy->getNumOperands() - 1).getValueType() == 9684 MVT::Glue) 9685 return false; 9686 TCChain = Copy->getOperand(0); 9687 } else if (Copy->getOpcode() != ISD::FP_EXTEND) 9688 return false; 9689 9690 bool HasRet = false; 9691 for (SDNode *Node : Copy->uses()) { 9692 if (Node->getOpcode() != AArch64ISD::RET_FLAG) 9693 return false; 9694 HasRet = true; 9695 } 9696 9697 if (!HasRet) 9698 return false; 9699 9700 Chain = TCChain; 9701 return true; 9702 } 9703 9704 // Return whether the an instruction can potentially be optimized to a tail 9705 // call. This will cause the optimizers to attempt to move, or duplicate, 9706 // return instructions to help enable tail call optimizations for this 9707 // instruction. 9708 bool AArch64TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const { 9709 if (!CI->isTailCall()) 9710 return false; 9711 9712 return true; 9713 } 9714 9715 bool AArch64TargetLowering::getIndexedAddressParts(SDNode *Op, SDValue &Base, 9716 SDValue &Offset, 9717 ISD::MemIndexedMode &AM, 9718 bool &IsInc, 9719 SelectionDAG &DAG) const { 9720 if (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB) 9721 return false; 9722 9723 Base = Op->getOperand(0); 9724 // All of the indexed addressing mode instructions take a signed 9725 // 9 bit immediate offset. 9726 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) { 9727 int64_t RHSC = (int64_t)RHS->getZExtValue(); 9728 if (RHSC >= 256 || RHSC <= -256) 9729 return false; 9730 IsInc = (Op->getOpcode() == ISD::ADD); 9731 Offset = Op->getOperand(1); 9732 return true; 9733 } 9734 return false; 9735 } 9736 9737 bool AArch64TargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, 9738 SDValue &Offset, 9739 ISD::MemIndexedMode &AM, 9740 SelectionDAG &DAG) const { 9741 EVT VT; 9742 SDValue Ptr; 9743 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 9744 VT = LD->getMemoryVT(); 9745 Ptr = LD->getBasePtr(); 9746 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 9747 VT = ST->getMemoryVT(); 9748 Ptr = ST->getBasePtr(); 9749 } else 9750 return false; 9751 9752 bool IsInc; 9753 if (!getIndexedAddressParts(Ptr.getNode(), Base, Offset, AM, IsInc, DAG)) 9754 return false; 9755 AM = IsInc ? ISD::PRE_INC : ISD::PRE_DEC; 9756 return true; 9757 } 9758 9759 bool AArch64TargetLowering::getPostIndexedAddressParts( 9760 SDNode *N, SDNode *Op, SDValue &Base, SDValue &Offset, 9761 ISD::MemIndexedMode &AM, SelectionDAG &DAG) const { 9762 EVT VT; 9763 SDValue Ptr; 9764 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 9765 VT = LD->getMemoryVT(); 9766 Ptr = LD->getBasePtr(); 9767 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 9768 VT = ST->getMemoryVT(); 9769 Ptr = ST->getBasePtr(); 9770 } else 9771 return false; 9772 9773 bool IsInc; 9774 if (!getIndexedAddressParts(Op, Base, Offset, AM, IsInc, DAG)) 9775 return false; 9776 // Post-indexing updates the base, so it's not a valid transform 9777 // if that's not the same as the load's pointer. 9778 if (Ptr != Base) 9779 return false; 9780 AM = IsInc ? ISD::POST_INC : ISD::POST_DEC; 9781 return true; 9782 } 9783 9784 static void ReplaceBITCASTResults(SDNode *N, SmallVectorImpl<SDValue> &Results, 9785 SelectionDAG &DAG) { 9786 SDLoc DL(N); 9787 SDValue Op = N->getOperand(0); 9788 9789 if (N->getValueType(0) != MVT::i16 || Op.getValueType() != MVT::f16) 9790 return; 9791 9792 Op = SDValue( 9793 DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, DL, MVT::f32, 9794 DAG.getUNDEF(MVT::i32), Op, 9795 DAG.getTargetConstant(AArch64::hsub, DL, MVT::i32)), 9796 0); 9797 Op = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op); 9798 Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, Op)); 9799 } 9800 9801 static void ReplaceReductionResults(SDNode *N, 9802 SmallVectorImpl<SDValue> &Results, 9803 SelectionDAG &DAG, unsigned InterOp, 9804 unsigned AcrossOp) { 9805 EVT LoVT, HiVT; 9806 SDValue Lo, Hi; 9807 SDLoc dl(N); 9808 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0)); 9809 std::tie(Lo, Hi) = DAG.SplitVectorOperand(N, 0); 9810 SDValue InterVal = DAG.getNode(InterOp, dl, LoVT, Lo, Hi); 9811 SDValue SplitVal = DAG.getNode(AcrossOp, dl, LoVT, InterVal); 9812 Results.push_back(SplitVal); 9813 } 9814 9815 void AArch64TargetLowering::ReplaceNodeResults( 9816 SDNode *N, SmallVectorImpl<SDValue> &Results, SelectionDAG &DAG) const { 9817 switch (N->getOpcode()) { 9818 default: 9819 llvm_unreachable("Don't know how to custom expand this"); 9820 case ISD::BITCAST: 9821 ReplaceBITCASTResults(N, Results, DAG); 9822 return; 9823 case AArch64ISD::SADDV: 9824 ReplaceReductionResults(N, Results, DAG, ISD::ADD, AArch64ISD::SADDV); 9825 return; 9826 case AArch64ISD::UADDV: 9827 ReplaceReductionResults(N, Results, DAG, ISD::ADD, AArch64ISD::UADDV); 9828 return; 9829 case AArch64ISD::SMINV: 9830 ReplaceReductionResults(N, Results, DAG, ISD::SMIN, AArch64ISD::SMINV); 9831 return; 9832 case AArch64ISD::UMINV: 9833 ReplaceReductionResults(N, Results, DAG, ISD::UMIN, AArch64ISD::UMINV); 9834 return; 9835 case AArch64ISD::SMAXV: 9836 ReplaceReductionResults(N, Results, DAG, ISD::SMAX, AArch64ISD::SMAXV); 9837 return; 9838 case AArch64ISD::UMAXV: 9839 ReplaceReductionResults(N, Results, DAG, ISD::UMAX, AArch64ISD::UMAXV); 9840 return; 9841 case ISD::FP_TO_UINT: 9842 case ISD::FP_TO_SINT: 9843 assert(N->getValueType(0) == MVT::i128 && "unexpected illegal conversion"); 9844 // Let normal code take care of it by not adding anything to Results. 9845 return; 9846 } 9847 } 9848 9849 bool AArch64TargetLowering::useLoadStackGuardNode() const { 9850 return true; 9851 } 9852 9853 unsigned AArch64TargetLowering::combineRepeatedFPDivisors() const { 9854 // Combine multiple FDIVs with the same divisor into multiple FMULs by the 9855 // reciprocal if there are three or more FDIVs. 9856 return 3; 9857 } 9858 9859 TargetLoweringBase::LegalizeTypeAction 9860 AArch64TargetLowering::getPreferredVectorAction(EVT VT) const { 9861 MVT SVT = VT.getSimpleVT(); 9862 // During type legalization, we prefer to widen v1i8, v1i16, v1i32 to v8i8, 9863 // v4i16, v2i32 instead of to promote. 9864 if (SVT == MVT::v1i8 || SVT == MVT::v1i16 || SVT == MVT::v1i32 9865 || SVT == MVT::v1f32) 9866 return TypeWidenVector; 9867 9868 return TargetLoweringBase::getPreferredVectorAction(VT); 9869 } 9870 9871 // Loads and stores less than 128-bits are already atomic; ones above that 9872 // are doomed anyway, so defer to the default libcall and blame the OS when 9873 // things go wrong. 9874 bool AArch64TargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const { 9875 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits(); 9876 return Size == 128; 9877 } 9878 9879 // Loads and stores less than 128-bits are already atomic; ones above that 9880 // are doomed anyway, so defer to the default libcall and blame the OS when 9881 // things go wrong. 9882 TargetLowering::AtomicExpansionKind 9883 AArch64TargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const { 9884 unsigned Size = LI->getType()->getPrimitiveSizeInBits(); 9885 return Size == 128 ? AtomicExpansionKind::LLSC : AtomicExpansionKind::None; 9886 } 9887 9888 // For the real atomic operations, we have ldxr/stxr up to 128 bits, 9889 TargetLowering::AtomicExpansionKind 9890 AArch64TargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const { 9891 unsigned Size = AI->getType()->getPrimitiveSizeInBits(); 9892 return Size <= 128 ? AtomicExpansionKind::LLSC : AtomicExpansionKind::None; 9893 } 9894 9895 bool AArch64TargetLowering::shouldExpandAtomicCmpXchgInIR( 9896 AtomicCmpXchgInst *AI) const { 9897 return true; 9898 } 9899 9900 Value *AArch64TargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr, 9901 AtomicOrdering Ord) const { 9902 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 9903 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType(); 9904 bool IsAcquire = isAtLeastAcquire(Ord); 9905 9906 // Since i128 isn't legal and intrinsics don't get type-lowered, the ldrexd 9907 // intrinsic must return {i64, i64} and we have to recombine them into a 9908 // single i128 here. 9909 if (ValTy->getPrimitiveSizeInBits() == 128) { 9910 Intrinsic::ID Int = 9911 IsAcquire ? Intrinsic::aarch64_ldaxp : Intrinsic::aarch64_ldxp; 9912 Function *Ldxr = llvm::Intrinsic::getDeclaration(M, Int); 9913 9914 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext())); 9915 Value *LoHi = Builder.CreateCall(Ldxr, Addr, "lohi"); 9916 9917 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo"); 9918 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi"); 9919 Lo = Builder.CreateZExt(Lo, ValTy, "lo64"); 9920 Hi = Builder.CreateZExt(Hi, ValTy, "hi64"); 9921 return Builder.CreateOr( 9922 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 64)), "val64"); 9923 } 9924 9925 Type *Tys[] = { Addr->getType() }; 9926 Intrinsic::ID Int = 9927 IsAcquire ? Intrinsic::aarch64_ldaxr : Intrinsic::aarch64_ldxr; 9928 Function *Ldxr = llvm::Intrinsic::getDeclaration(M, Int, Tys); 9929 9930 return Builder.CreateTruncOrBitCast( 9931 Builder.CreateCall(Ldxr, Addr), 9932 cast<PointerType>(Addr->getType())->getElementType()); 9933 } 9934 9935 void AArch64TargetLowering::emitAtomicCmpXchgNoStoreLLBalance( 9936 IRBuilder<> &Builder) const { 9937 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 9938 Builder.CreateCall( 9939 llvm::Intrinsic::getDeclaration(M, Intrinsic::aarch64_clrex)); 9940 } 9941 9942 Value *AArch64TargetLowering::emitStoreConditional(IRBuilder<> &Builder, 9943 Value *Val, Value *Addr, 9944 AtomicOrdering Ord) const { 9945 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 9946 bool IsRelease = isAtLeastRelease(Ord); 9947 9948 // Since the intrinsics must have legal type, the i128 intrinsics take two 9949 // parameters: "i64, i64". We must marshal Val into the appropriate form 9950 // before the call. 9951 if (Val->getType()->getPrimitiveSizeInBits() == 128) { 9952 Intrinsic::ID Int = 9953 IsRelease ? Intrinsic::aarch64_stlxp : Intrinsic::aarch64_stxp; 9954 Function *Stxr = Intrinsic::getDeclaration(M, Int); 9955 Type *Int64Ty = Type::getInt64Ty(M->getContext()); 9956 9957 Value *Lo = Builder.CreateTrunc(Val, Int64Ty, "lo"); 9958 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 64), Int64Ty, "hi"); 9959 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext())); 9960 return Builder.CreateCall(Stxr, {Lo, Hi, Addr}); 9961 } 9962 9963 Intrinsic::ID Int = 9964 IsRelease ? Intrinsic::aarch64_stlxr : Intrinsic::aarch64_stxr; 9965 Type *Tys[] = { Addr->getType() }; 9966 Function *Stxr = Intrinsic::getDeclaration(M, Int, Tys); 9967 9968 return Builder.CreateCall(Stxr, 9969 {Builder.CreateZExtOrBitCast( 9970 Val, Stxr->getFunctionType()->getParamType(0)), 9971 Addr}); 9972 } 9973 9974 bool AArch64TargetLowering::functionArgumentNeedsConsecutiveRegisters( 9975 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const { 9976 return Ty->isArrayTy(); 9977 } 9978 9979 bool AArch64TargetLowering::shouldNormalizeToSelectSequence(LLVMContext &, 9980 EVT) const { 9981 return false; 9982 } 9983 9984 Value *AArch64TargetLowering::getSafeStackPointerLocation(IRBuilder<> &IRB) const { 9985 if (!Subtarget->isTargetAndroid()) 9986 return TargetLowering::getSafeStackPointerLocation(IRB); 9987 9988 // Android provides a fixed TLS slot for the SafeStack pointer. See the 9989 // definition of TLS_SLOT_SAFESTACK in 9990 // https://android.googlesource.com/platform/bionic/+/master/libc/private/bionic_tls.h 9991 const unsigned TlsOffset = 0x48; 9992 Module *M = IRB.GetInsertBlock()->getParent()->getParent(); 9993 Function *ThreadPointerFunc = 9994 Intrinsic::getDeclaration(M, Intrinsic::aarch64_thread_pointer); 9995 return IRB.CreatePointerCast( 9996 IRB.CreateConstGEP1_32(IRB.CreateCall(ThreadPointerFunc), TlsOffset), 9997 Type::getInt8PtrTy(IRB.getContext())->getPointerTo(0)); 9998 } 9999