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 defines the interfaces that AArch64 uses to lower LLVM code into a 11 // selection DAG. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #define DEBUG_TYPE "aarch64-isel" 16 #include "AArch64.h" 17 #include "AArch64ISelLowering.h" 18 #include "AArch64MachineFunctionInfo.h" 19 #include "AArch64TargetMachine.h" 20 #include "AArch64TargetObjectFile.h" 21 #include "Utils/AArch64BaseInfo.h" 22 #include "llvm/CodeGen/Analysis.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/CodeGen/TargetLoweringObjectFileImpl.h" 28 #include "llvm/IR/CallingConv.h" 29 30 using namespace llvm; 31 32 static TargetLoweringObjectFile *createTLOF(AArch64TargetMachine &TM) { 33 assert (TM.getSubtarget<AArch64Subtarget>().isTargetELF() && 34 "unknown subtarget type"); 35 return new AArch64ElfTargetObjectFile(); 36 } 37 38 AArch64TargetLowering::AArch64TargetLowering(AArch64TargetMachine &TM) 39 : TargetLowering(TM, createTLOF(TM)), Itins(TM.getInstrItineraryData()) { 40 41 const AArch64Subtarget *Subtarget = &TM.getSubtarget<AArch64Subtarget>(); 42 43 // SIMD compares set the entire lane's bits to 1 44 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 45 46 // Scalar register <-> type mapping 47 addRegisterClass(MVT::i32, &AArch64::GPR32RegClass); 48 addRegisterClass(MVT::i64, &AArch64::GPR64RegClass); 49 50 if (Subtarget->hasFPARMv8()) { 51 addRegisterClass(MVT::f16, &AArch64::FPR16RegClass); 52 addRegisterClass(MVT::f32, &AArch64::FPR32RegClass); 53 addRegisterClass(MVT::f64, &AArch64::FPR64RegClass); 54 addRegisterClass(MVT::f128, &AArch64::FPR128RegClass); 55 } 56 57 if (Subtarget->hasNEON()) { 58 // And the vectors 59 addRegisterClass(MVT::v1i8, &AArch64::FPR8RegClass); 60 addRegisterClass(MVT::v1i16, &AArch64::FPR16RegClass); 61 addRegisterClass(MVT::v1i32, &AArch64::FPR32RegClass); 62 addRegisterClass(MVT::v1i64, &AArch64::FPR64RegClass); 63 addRegisterClass(MVT::v1f64, &AArch64::FPR64RegClass); 64 addRegisterClass(MVT::v8i8, &AArch64::FPR64RegClass); 65 addRegisterClass(MVT::v4i16, &AArch64::FPR64RegClass); 66 addRegisterClass(MVT::v2i32, &AArch64::FPR64RegClass); 67 addRegisterClass(MVT::v1i64, &AArch64::FPR64RegClass); 68 addRegisterClass(MVT::v2f32, &AArch64::FPR64RegClass); 69 addRegisterClass(MVT::v16i8, &AArch64::FPR128RegClass); 70 addRegisterClass(MVT::v8i16, &AArch64::FPR128RegClass); 71 addRegisterClass(MVT::v4i32, &AArch64::FPR128RegClass); 72 addRegisterClass(MVT::v2i64, &AArch64::FPR128RegClass); 73 addRegisterClass(MVT::v4f32, &AArch64::FPR128RegClass); 74 addRegisterClass(MVT::v2f64, &AArch64::FPR128RegClass); 75 } 76 77 computeRegisterProperties(); 78 79 // We combine OR nodes for bitfield and NEON BSL operations. 80 setTargetDAGCombine(ISD::OR); 81 82 setTargetDAGCombine(ISD::AND); 83 setTargetDAGCombine(ISD::SRA); 84 setTargetDAGCombine(ISD::SRL); 85 setTargetDAGCombine(ISD::SHL); 86 87 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); 88 setTargetDAGCombine(ISD::INTRINSIC_VOID); 89 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN); 90 91 // AArch64 does not have i1 loads, or much of anything for i1 really. 92 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); 93 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote); 94 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote); 95 96 setStackPointerRegisterToSaveRestore(AArch64::XSP); 97 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand); 98 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 99 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 100 101 // We'll lower globals to wrappers for selection. 102 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); 103 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom); 104 105 // A64 instructions have the comparison predicate attached to the user of the 106 // result, but having a separate comparison is valuable for matching. 107 setOperationAction(ISD::BR_CC, MVT::i32, Custom); 108 setOperationAction(ISD::BR_CC, MVT::i64, Custom); 109 setOperationAction(ISD::BR_CC, MVT::f32, Custom); 110 setOperationAction(ISD::BR_CC, MVT::f64, Custom); 111 112 setOperationAction(ISD::SELECT, MVT::i32, Custom); 113 setOperationAction(ISD::SELECT, MVT::i64, Custom); 114 setOperationAction(ISD::SELECT, MVT::f32, Custom); 115 setOperationAction(ISD::SELECT, MVT::f64, Custom); 116 117 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); 118 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom); 119 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 120 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); 121 122 setOperationAction(ISD::BRCOND, MVT::Other, Custom); 123 124 setOperationAction(ISD::SETCC, MVT::i32, Custom); 125 setOperationAction(ISD::SETCC, MVT::i64, Custom); 126 setOperationAction(ISD::SETCC, MVT::f32, Custom); 127 setOperationAction(ISD::SETCC, MVT::f64, Custom); 128 129 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 130 setOperationAction(ISD::JumpTable, MVT::i32, Custom); 131 setOperationAction(ISD::JumpTable, MVT::i64, Custom); 132 133 setOperationAction(ISD::VASTART, MVT::Other, Custom); 134 setOperationAction(ISD::VACOPY, MVT::Other, Custom); 135 setOperationAction(ISD::VAEND, MVT::Other, Expand); 136 setOperationAction(ISD::VAARG, MVT::Other, Expand); 137 138 setOperationAction(ISD::BlockAddress, MVT::i64, Custom); 139 setOperationAction(ISD::ConstantPool, MVT::i64, Custom); 140 141 setOperationAction(ISD::ROTL, MVT::i32, Expand); 142 setOperationAction(ISD::ROTL, MVT::i64, Expand); 143 144 setOperationAction(ISD::UREM, MVT::i32, Expand); 145 setOperationAction(ISD::UREM, MVT::i64, Expand); 146 setOperationAction(ISD::UDIVREM, MVT::i32, Expand); 147 setOperationAction(ISD::UDIVREM, MVT::i64, Expand); 148 149 setOperationAction(ISD::SREM, MVT::i32, Expand); 150 setOperationAction(ISD::SREM, MVT::i64, Expand); 151 setOperationAction(ISD::SDIVREM, MVT::i32, Expand); 152 setOperationAction(ISD::SDIVREM, MVT::i64, Expand); 153 154 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); 155 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand); 156 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); 157 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand); 158 159 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 160 setOperationAction(ISD::CTPOP, MVT::i64, Expand); 161 162 // Legal floating-point operations. 163 setOperationAction(ISD::FABS, MVT::f32, Legal); 164 setOperationAction(ISD::FABS, MVT::f64, Legal); 165 166 setOperationAction(ISD::FCEIL, MVT::f32, Legal); 167 setOperationAction(ISD::FCEIL, MVT::f64, Legal); 168 169 setOperationAction(ISD::FFLOOR, MVT::f32, Legal); 170 setOperationAction(ISD::FFLOOR, MVT::f64, Legal); 171 172 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal); 173 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal); 174 175 setOperationAction(ISD::FNEG, MVT::f32, Legal); 176 setOperationAction(ISD::FNEG, MVT::f64, Legal); 177 178 setOperationAction(ISD::FRINT, MVT::f32, Legal); 179 setOperationAction(ISD::FRINT, MVT::f64, Legal); 180 181 setOperationAction(ISD::FSQRT, MVT::f32, Legal); 182 setOperationAction(ISD::FSQRT, MVT::f64, Legal); 183 184 setOperationAction(ISD::FTRUNC, MVT::f32, Legal); 185 setOperationAction(ISD::FTRUNC, MVT::f64, Legal); 186 187 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); 188 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); 189 setOperationAction(ISD::ConstantFP, MVT::f128, Legal); 190 191 // Illegal floating-point operations. 192 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); 193 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 194 195 setOperationAction(ISD::FCOS, MVT::f32, Expand); 196 setOperationAction(ISD::FCOS, MVT::f64, Expand); 197 198 setOperationAction(ISD::FEXP, MVT::f32, Expand); 199 setOperationAction(ISD::FEXP, MVT::f64, Expand); 200 201 setOperationAction(ISD::FEXP2, MVT::f32, Expand); 202 setOperationAction(ISD::FEXP2, MVT::f64, Expand); 203 204 setOperationAction(ISD::FLOG, MVT::f32, Expand); 205 setOperationAction(ISD::FLOG, MVT::f64, Expand); 206 207 setOperationAction(ISD::FLOG2, MVT::f32, Expand); 208 setOperationAction(ISD::FLOG2, MVT::f64, Expand); 209 210 setOperationAction(ISD::FLOG10, MVT::f32, Expand); 211 setOperationAction(ISD::FLOG10, MVT::f64, Expand); 212 213 setOperationAction(ISD::FPOW, MVT::f32, Expand); 214 setOperationAction(ISD::FPOW, MVT::f64, Expand); 215 216 setOperationAction(ISD::FPOWI, MVT::f32, Expand); 217 setOperationAction(ISD::FPOWI, MVT::f64, Expand); 218 219 setOperationAction(ISD::FREM, MVT::f32, Expand); 220 setOperationAction(ISD::FREM, MVT::f64, Expand); 221 222 setOperationAction(ISD::FSIN, MVT::f32, Expand); 223 setOperationAction(ISD::FSIN, MVT::f64, Expand); 224 225 setOperationAction(ISD::FSINCOS, MVT::f32, Expand); 226 setOperationAction(ISD::FSINCOS, MVT::f64, Expand); 227 228 // Virtually no operation on f128 is legal, but LLVM can't expand them when 229 // there's a valid register class, so we need custom operations in most cases. 230 setOperationAction(ISD::FABS, MVT::f128, Expand); 231 setOperationAction(ISD::FADD, MVT::f128, Custom); 232 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand); 233 setOperationAction(ISD::FCOS, MVT::f128, Expand); 234 setOperationAction(ISD::FDIV, MVT::f128, Custom); 235 setOperationAction(ISD::FMA, MVT::f128, Expand); 236 setOperationAction(ISD::FMUL, MVT::f128, Custom); 237 setOperationAction(ISD::FNEG, MVT::f128, Expand); 238 setOperationAction(ISD::FP_EXTEND, MVT::f128, Expand); 239 setOperationAction(ISD::FP_ROUND, MVT::f128, Expand); 240 setOperationAction(ISD::FPOW, MVT::f128, Expand); 241 setOperationAction(ISD::FREM, MVT::f128, Expand); 242 setOperationAction(ISD::FRINT, MVT::f128, Expand); 243 setOperationAction(ISD::FSIN, MVT::f128, Expand); 244 setOperationAction(ISD::FSINCOS, MVT::f128, Expand); 245 setOperationAction(ISD::FSQRT, MVT::f128, Expand); 246 setOperationAction(ISD::FSUB, MVT::f128, Custom); 247 setOperationAction(ISD::FTRUNC, MVT::f128, Expand); 248 setOperationAction(ISD::SETCC, MVT::f128, Custom); 249 setOperationAction(ISD::BR_CC, MVT::f128, Custom); 250 setOperationAction(ISD::SELECT, MVT::f128, Expand); 251 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom); 252 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom); 253 254 // Lowering for many of the conversions is actually specified by the non-f128 255 // type. The LowerXXX function will be trivial when f128 isn't involved. 256 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 257 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); 258 setOperationAction(ISD::FP_TO_SINT, MVT::i128, Custom); 259 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); 260 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom); 261 setOperationAction(ISD::FP_TO_UINT, MVT::i128, Custom); 262 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 263 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); 264 setOperationAction(ISD::SINT_TO_FP, MVT::i128, Custom); 265 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); 266 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom); 267 setOperationAction(ISD::UINT_TO_FP, MVT::i128, Custom); 268 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom); 269 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom); 270 271 // This prevents LLVM trying to compress double constants into a floating 272 // constant-pool entry and trying to load from there. It's of doubtful benefit 273 // for A64: we'd need LDR followed by FCVT, I believe. 274 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand); 275 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); 276 setLoadExtAction(ISD::EXTLOAD, MVT::f16, Expand); 277 278 setTruncStoreAction(MVT::f128, MVT::f64, Expand); 279 setTruncStoreAction(MVT::f128, MVT::f32, Expand); 280 setTruncStoreAction(MVT::f128, MVT::f16, Expand); 281 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 282 setTruncStoreAction(MVT::f64, MVT::f16, Expand); 283 setTruncStoreAction(MVT::f32, MVT::f16, Expand); 284 285 setExceptionPointerRegister(AArch64::X0); 286 setExceptionSelectorRegister(AArch64::X1); 287 288 if (Subtarget->hasNEON()) { 289 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v8i8, Expand); 290 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Expand); 291 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Expand); 292 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v1i64, Expand); 293 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v16i8, Expand); 294 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v8i16, Expand); 295 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i32, Expand); 296 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Expand); 297 298 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i8, Custom); 299 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom); 300 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom); 301 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i16, Custom); 302 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom); 303 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom); 304 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i32, Custom); 305 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom); 306 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom); 307 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom); 308 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom); 309 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom); 310 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); 311 setOperationAction(ISD::BUILD_VECTOR, MVT::v1f64, Custom); 312 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom); 313 314 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom); 315 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom); 316 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom); 317 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i16, Custom); 318 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom); 319 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i32, Custom); 320 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom); 321 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom); 322 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f32, Custom); 323 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom); 324 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1f64, Custom); 325 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom); 326 327 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i32, Legal); 328 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Legal); 329 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Legal); 330 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Legal); 331 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Legal); 332 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Legal); 333 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Legal); 334 335 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i8, Custom); 336 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i16, Custom); 337 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom); 338 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom); 339 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom); 340 341 setOperationAction(ISD::SETCC, MVT::v8i8, Custom); 342 setOperationAction(ISD::SETCC, MVT::v16i8, Custom); 343 setOperationAction(ISD::SETCC, MVT::v4i16, Custom); 344 setOperationAction(ISD::SETCC, MVT::v8i16, Custom); 345 setOperationAction(ISD::SETCC, MVT::v2i32, Custom); 346 setOperationAction(ISD::SETCC, MVT::v4i32, Custom); 347 setOperationAction(ISD::SETCC, MVT::v1i64, Custom); 348 setOperationAction(ISD::SETCC, MVT::v2i64, Custom); 349 setOperationAction(ISD::SETCC, MVT::v2f32, Custom); 350 setOperationAction(ISD::SETCC, MVT::v4f32, Custom); 351 setOperationAction(ISD::SETCC, MVT::v1f64, Custom); 352 setOperationAction(ISD::SETCC, MVT::v2f64, Custom); 353 354 setOperationAction(ISD::FFLOOR, MVT::v2f32, Legal); 355 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal); 356 setOperationAction(ISD::FFLOOR, MVT::v1f64, Legal); 357 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal); 358 359 setOperationAction(ISD::FCEIL, MVT::v2f32, Legal); 360 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal); 361 setOperationAction(ISD::FCEIL, MVT::v1f64, Legal); 362 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal); 363 364 setOperationAction(ISD::FTRUNC, MVT::v2f32, Legal); 365 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal); 366 setOperationAction(ISD::FTRUNC, MVT::v1f64, Legal); 367 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal); 368 369 setOperationAction(ISD::FRINT, MVT::v2f32, Legal); 370 setOperationAction(ISD::FRINT, MVT::v4f32, Legal); 371 setOperationAction(ISD::FRINT, MVT::v1f64, Legal); 372 setOperationAction(ISD::FRINT, MVT::v2f64, Legal); 373 374 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Legal); 375 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal); 376 setOperationAction(ISD::FNEARBYINT, MVT::v1f64, Legal); 377 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal); 378 379 setOperationAction(ISD::FROUND, MVT::v2f32, Legal); 380 setOperationAction(ISD::FROUND, MVT::v4f32, Legal); 381 setOperationAction(ISD::FROUND, MVT::v1f64, Legal); 382 setOperationAction(ISD::FROUND, MVT::v2f64, Legal); 383 384 setOperationAction(ISD::SINT_TO_FP, MVT::v1i8, Custom); 385 setOperationAction(ISD::SINT_TO_FP, MVT::v1i16, Custom); 386 setOperationAction(ISD::SINT_TO_FP, MVT::v1i32, Custom); 387 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom); 388 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom); 389 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Custom); 390 391 setOperationAction(ISD::UINT_TO_FP, MVT::v1i8, Custom); 392 setOperationAction(ISD::UINT_TO_FP, MVT::v1i16, Custom); 393 setOperationAction(ISD::UINT_TO_FP, MVT::v1i32, Custom); 394 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom); 395 setOperationAction(ISD::UINT_TO_FP, MVT::v2i32, Custom); 396 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Custom); 397 398 setOperationAction(ISD::FP_TO_SINT, MVT::v1i8, Custom); 399 setOperationAction(ISD::FP_TO_SINT, MVT::v1i16, Custom); 400 setOperationAction(ISD::FP_TO_SINT, MVT::v1i32, Custom); 401 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom); 402 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom); 403 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Custom); 404 405 setOperationAction(ISD::FP_TO_UINT, MVT::v1i8, Custom); 406 setOperationAction(ISD::FP_TO_UINT, MVT::v1i16, Custom); 407 setOperationAction(ISD::FP_TO_UINT, MVT::v1i32, Custom); 408 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom); 409 setOperationAction(ISD::FP_TO_UINT, MVT::v2i32, Custom); 410 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Custom); 411 412 // Neon does not support vector divide/remainder operations except 413 // floating-point divide. 414 setOperationAction(ISD::SDIV, MVT::v1i8, Expand); 415 setOperationAction(ISD::SDIV, MVT::v8i8, Expand); 416 setOperationAction(ISD::SDIV, MVT::v16i8, Expand); 417 setOperationAction(ISD::SDIV, MVT::v1i16, Expand); 418 setOperationAction(ISD::SDIV, MVT::v4i16, Expand); 419 setOperationAction(ISD::SDIV, MVT::v8i16, Expand); 420 setOperationAction(ISD::SDIV, MVT::v1i32, Expand); 421 setOperationAction(ISD::SDIV, MVT::v2i32, Expand); 422 setOperationAction(ISD::SDIV, MVT::v4i32, Expand); 423 setOperationAction(ISD::SDIV, MVT::v1i64, Expand); 424 setOperationAction(ISD::SDIV, MVT::v2i64, Expand); 425 426 setOperationAction(ISD::UDIV, MVT::v1i8, Expand); 427 setOperationAction(ISD::UDIV, MVT::v8i8, Expand); 428 setOperationAction(ISD::UDIV, MVT::v16i8, Expand); 429 setOperationAction(ISD::UDIV, MVT::v1i16, Expand); 430 setOperationAction(ISD::UDIV, MVT::v4i16, Expand); 431 setOperationAction(ISD::UDIV, MVT::v8i16, Expand); 432 setOperationAction(ISD::UDIV, MVT::v1i32, Expand); 433 setOperationAction(ISD::UDIV, MVT::v2i32, Expand); 434 setOperationAction(ISD::UDIV, MVT::v4i32, Expand); 435 setOperationAction(ISD::UDIV, MVT::v1i64, Expand); 436 setOperationAction(ISD::UDIV, MVT::v2i64, Expand); 437 438 setOperationAction(ISD::SREM, MVT::v1i8, Expand); 439 setOperationAction(ISD::SREM, MVT::v8i8, Expand); 440 setOperationAction(ISD::SREM, MVT::v16i8, Expand); 441 setOperationAction(ISD::SREM, MVT::v1i16, Expand); 442 setOperationAction(ISD::SREM, MVT::v4i16, Expand); 443 setOperationAction(ISD::SREM, MVT::v8i16, Expand); 444 setOperationAction(ISD::SREM, MVT::v1i32, Expand); 445 setOperationAction(ISD::SREM, MVT::v2i32, Expand); 446 setOperationAction(ISD::SREM, MVT::v4i32, Expand); 447 setOperationAction(ISD::SREM, MVT::v1i64, Expand); 448 setOperationAction(ISD::SREM, MVT::v2i64, Expand); 449 450 setOperationAction(ISD::UREM, MVT::v1i8, Expand); 451 setOperationAction(ISD::UREM, MVT::v8i8, Expand); 452 setOperationAction(ISD::UREM, MVT::v16i8, Expand); 453 setOperationAction(ISD::UREM, MVT::v1i16, Expand); 454 setOperationAction(ISD::UREM, MVT::v4i16, Expand); 455 setOperationAction(ISD::UREM, MVT::v8i16, Expand); 456 setOperationAction(ISD::UREM, MVT::v1i32, Expand); 457 setOperationAction(ISD::UREM, MVT::v2i32, Expand); 458 setOperationAction(ISD::UREM, MVT::v4i32, Expand); 459 setOperationAction(ISD::UREM, MVT::v1i64, Expand); 460 setOperationAction(ISD::UREM, MVT::v2i64, Expand); 461 462 setOperationAction(ISD::FREM, MVT::v2f32, Expand); 463 setOperationAction(ISD::FREM, MVT::v4f32, Expand); 464 setOperationAction(ISD::FREM, MVT::v1f64, Expand); 465 setOperationAction(ISD::FREM, MVT::v2f64, Expand); 466 467 setOperationAction(ISD::SELECT, MVT::v8i8, Expand); 468 setOperationAction(ISD::SELECT, MVT::v16i8, Expand); 469 setOperationAction(ISD::SELECT, MVT::v4i16, Expand); 470 setOperationAction(ISD::SELECT, MVT::v8i16, Expand); 471 setOperationAction(ISD::SELECT, MVT::v2i32, Expand); 472 setOperationAction(ISD::SELECT, MVT::v4i32, Expand); 473 setOperationAction(ISD::SELECT, MVT::v1i64, Expand); 474 setOperationAction(ISD::SELECT, MVT::v2i64, Expand); 475 setOperationAction(ISD::SELECT, MVT::v2f32, Expand); 476 setOperationAction(ISD::SELECT, MVT::v4f32, Expand); 477 setOperationAction(ISD::SELECT, MVT::v1f64, Expand); 478 setOperationAction(ISD::SELECT, MVT::v2f64, Expand); 479 480 setOperationAction(ISD::SELECT_CC, MVT::v8i8, Custom); 481 setOperationAction(ISD::SELECT_CC, MVT::v16i8, Custom); 482 setOperationAction(ISD::SELECT_CC, MVT::v4i16, Custom); 483 setOperationAction(ISD::SELECT_CC, MVT::v8i16, Custom); 484 setOperationAction(ISD::SELECT_CC, MVT::v2i32, Custom); 485 setOperationAction(ISD::SELECT_CC, MVT::v4i32, Custom); 486 setOperationAction(ISD::SELECT_CC, MVT::v1i64, Custom); 487 setOperationAction(ISD::SELECT_CC, MVT::v2i64, Custom); 488 setOperationAction(ISD::SELECT_CC, MVT::v2f32, Custom); 489 setOperationAction(ISD::SELECT_CC, MVT::v4f32, Custom); 490 setOperationAction(ISD::SELECT_CC, MVT::v1f64, Custom); 491 setOperationAction(ISD::SELECT_CC, MVT::v2f64, Custom); 492 493 // Vector ExtLoad and TruncStore are expanded. 494 for (unsigned I = MVT::FIRST_VECTOR_VALUETYPE; 495 I <= MVT::LAST_VECTOR_VALUETYPE; ++I) { 496 MVT VT = (MVT::SimpleValueType) I; 497 setLoadExtAction(ISD::SEXTLOAD, VT, Expand); 498 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand); 499 setLoadExtAction(ISD::EXTLOAD, VT, Expand); 500 for (unsigned II = MVT::FIRST_VECTOR_VALUETYPE; 501 II <= MVT::LAST_VECTOR_VALUETYPE; ++II) { 502 MVT VT1 = (MVT::SimpleValueType) II; 503 // A TruncStore has two vector types of the same number of elements 504 // and different element sizes. 505 if (VT.getVectorNumElements() == VT1.getVectorNumElements() && 506 VT.getVectorElementType().getSizeInBits() 507 > VT1.getVectorElementType().getSizeInBits()) 508 setTruncStoreAction(VT, VT1, Expand); 509 } 510 } 511 512 // There is no v1i64/v2i64 multiply, expand v1i64/v2i64 to GPR i64 multiply. 513 // FIXME: For a v2i64 multiply, we copy VPR to GPR and do 2 i64 multiplies, 514 // and then copy back to VPR. This solution may be optimized by Following 3 515 // NEON instructions: 516 // pmull v2.1q, v0.1d, v1.1d 517 // pmull2 v3.1q, v0.2d, v1.2d 518 // ins v2.d[1], v3.d[0] 519 // As currently we can't verify the correctness of such assumption, we can 520 // do such optimization in the future. 521 setOperationAction(ISD::MUL, MVT::v1i64, Expand); 522 setOperationAction(ISD::MUL, MVT::v2i64, Expand); 523 524 setOperationAction(ISD::FCOS, MVT::v2f64, Expand); 525 setOperationAction(ISD::FCOS, MVT::v4f32, Expand); 526 setOperationAction(ISD::FCOS, MVT::v2f32, Expand); 527 setOperationAction(ISD::FSIN, MVT::v2f64, Expand); 528 setOperationAction(ISD::FSIN, MVT::v4f32, Expand); 529 setOperationAction(ISD::FSIN, MVT::v2f32, Expand); 530 setOperationAction(ISD::FPOW, MVT::v2f64, Expand); 531 setOperationAction(ISD::FPOW, MVT::v4f32, Expand); 532 setOperationAction(ISD::FPOW, MVT::v2f32, Expand); 533 } 534 535 setTargetDAGCombine(ISD::SETCC); 536 setTargetDAGCombine(ISD::SIGN_EXTEND); 537 setTargetDAGCombine(ISD::VSELECT); 538 } 539 540 EVT AArch64TargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const { 541 // It's reasonably important that this value matches the "natural" legal 542 // promotion from i1 for scalar types. Otherwise LegalizeTypes can get itself 543 // in a twist (e.g. inserting an any_extend which then becomes i64 -> i64). 544 if (!VT.isVector()) return MVT::i32; 545 return VT.changeVectorElementTypeToInteger(); 546 } 547 548 static void getExclusiveOperation(unsigned Size, AtomicOrdering Ord, 549 unsigned &LdrOpc, 550 unsigned &StrOpc) { 551 static const unsigned LoadBares[] = {AArch64::LDXR_byte, AArch64::LDXR_hword, 552 AArch64::LDXR_word, AArch64::LDXR_dword}; 553 static const unsigned LoadAcqs[] = {AArch64::LDAXR_byte, AArch64::LDAXR_hword, 554 AArch64::LDAXR_word, AArch64::LDAXR_dword}; 555 static const unsigned StoreBares[] = {AArch64::STXR_byte, AArch64::STXR_hword, 556 AArch64::STXR_word, AArch64::STXR_dword}; 557 static const unsigned StoreRels[] = {AArch64::STLXR_byte,AArch64::STLXR_hword, 558 AArch64::STLXR_word, AArch64::STLXR_dword}; 559 560 const unsigned *LoadOps, *StoreOps; 561 if (Ord == Acquire || Ord == AcquireRelease || Ord == SequentiallyConsistent) 562 LoadOps = LoadAcqs; 563 else 564 LoadOps = LoadBares; 565 566 if (Ord == Release || Ord == AcquireRelease || Ord == SequentiallyConsistent) 567 StoreOps = StoreRels; 568 else 569 StoreOps = StoreBares; 570 571 assert(isPowerOf2_32(Size) && Size <= 8 && 572 "unsupported size for atomic binary op!"); 573 574 LdrOpc = LoadOps[Log2_32(Size)]; 575 StrOpc = StoreOps[Log2_32(Size)]; 576 } 577 578 // FIXME: AArch64::DTripleRegClass and AArch64::QTripleRegClass don't really 579 // have value type mapped, and they are both being defined as MVT::untyped. 580 // Without knowing the MVT type, MachineLICM::getRegisterClassIDAndCost 581 // would fail to figure out the register pressure correctly. 582 std::pair<const TargetRegisterClass*, uint8_t> 583 AArch64TargetLowering::findRepresentativeClass(MVT VT) const{ 584 const TargetRegisterClass *RRC = 0; 585 uint8_t Cost = 1; 586 switch (VT.SimpleTy) { 587 default: 588 return TargetLowering::findRepresentativeClass(VT); 589 case MVT::v4i64: 590 RRC = &AArch64::QPairRegClass; 591 Cost = 2; 592 break; 593 case MVT::v8i64: 594 RRC = &AArch64::QQuadRegClass; 595 Cost = 4; 596 break; 597 } 598 return std::make_pair(RRC, Cost); 599 } 600 601 MachineBasicBlock * 602 AArch64TargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB, 603 unsigned Size, 604 unsigned BinOpcode) const { 605 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0. 606 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 607 608 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 609 MachineFunction *MF = BB->getParent(); 610 MachineFunction::iterator It = BB; 611 ++It; 612 613 unsigned dest = MI->getOperand(0).getReg(); 614 unsigned ptr = MI->getOperand(1).getReg(); 615 unsigned incr = MI->getOperand(2).getReg(); 616 AtomicOrdering Ord = static_cast<AtomicOrdering>(MI->getOperand(3).getImm()); 617 DebugLoc dl = MI->getDebugLoc(); 618 619 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 620 621 unsigned ldrOpc, strOpc; 622 getExclusiveOperation(Size, Ord, ldrOpc, strOpc); 623 624 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 625 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 626 MF->insert(It, loopMBB); 627 MF->insert(It, exitMBB); 628 629 // Transfer the remainder of BB and its successor edges to exitMBB. 630 exitMBB->splice(exitMBB->begin(), BB, 631 llvm::next(MachineBasicBlock::iterator(MI)), 632 BB->end()); 633 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 634 635 const TargetRegisterClass *TRC 636 = Size == 8 ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass; 637 unsigned scratch = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC); 638 639 // thisMBB: 640 // ... 641 // fallthrough --> loopMBB 642 BB->addSuccessor(loopMBB); 643 644 // loopMBB: 645 // ldxr dest, ptr 646 // <binop> scratch, dest, incr 647 // stxr stxr_status, scratch, ptr 648 // cbnz stxr_status, loopMBB 649 // fallthrough --> exitMBB 650 BB = loopMBB; 651 BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr); 652 if (BinOpcode) { 653 // All arithmetic operations we'll be creating are designed to take an extra 654 // shift or extend operand, which we can conveniently set to zero. 655 656 // Operand order needs to go the other way for NAND. 657 if (BinOpcode == AArch64::BICwww_lsl || BinOpcode == AArch64::BICxxx_lsl) 658 BuildMI(BB, dl, TII->get(BinOpcode), scratch) 659 .addReg(incr).addReg(dest).addImm(0); 660 else 661 BuildMI(BB, dl, TII->get(BinOpcode), scratch) 662 .addReg(dest).addReg(incr).addImm(0); 663 } 664 665 // From the stxr, the register is GPR32; from the cmp it's GPR32wsp 666 unsigned stxr_status = MRI.createVirtualRegister(&AArch64::GPR32RegClass); 667 MRI.constrainRegClass(stxr_status, &AArch64::GPR32wspRegClass); 668 669 BuildMI(BB, dl, TII->get(strOpc), stxr_status).addReg(scratch).addReg(ptr); 670 BuildMI(BB, dl, TII->get(AArch64::CBNZw)) 671 .addReg(stxr_status).addMBB(loopMBB); 672 673 BB->addSuccessor(loopMBB); 674 BB->addSuccessor(exitMBB); 675 676 // exitMBB: 677 // ... 678 BB = exitMBB; 679 680 MI->eraseFromParent(); // The instruction is gone now. 681 682 return BB; 683 } 684 685 MachineBasicBlock * 686 AArch64TargetLowering::emitAtomicBinaryMinMax(MachineInstr *MI, 687 MachineBasicBlock *BB, 688 unsigned Size, 689 unsigned CmpOp, 690 A64CC::CondCodes Cond) const { 691 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 692 693 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 694 MachineFunction *MF = BB->getParent(); 695 MachineFunction::iterator It = BB; 696 ++It; 697 698 unsigned dest = MI->getOperand(0).getReg(); 699 unsigned ptr = MI->getOperand(1).getReg(); 700 unsigned incr = MI->getOperand(2).getReg(); 701 AtomicOrdering Ord = static_cast<AtomicOrdering>(MI->getOperand(3).getImm()); 702 703 unsigned oldval = dest; 704 DebugLoc dl = MI->getDebugLoc(); 705 706 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 707 const TargetRegisterClass *TRC, *TRCsp; 708 if (Size == 8) { 709 TRC = &AArch64::GPR64RegClass; 710 TRCsp = &AArch64::GPR64xspRegClass; 711 } else { 712 TRC = &AArch64::GPR32RegClass; 713 TRCsp = &AArch64::GPR32wspRegClass; 714 } 715 716 unsigned ldrOpc, strOpc; 717 getExclusiveOperation(Size, Ord, ldrOpc, strOpc); 718 719 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 720 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 721 MF->insert(It, loopMBB); 722 MF->insert(It, exitMBB); 723 724 // Transfer the remainder of BB and its successor edges to exitMBB. 725 exitMBB->splice(exitMBB->begin(), BB, 726 llvm::next(MachineBasicBlock::iterator(MI)), 727 BB->end()); 728 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 729 730 unsigned scratch = MRI.createVirtualRegister(TRC); 731 MRI.constrainRegClass(scratch, TRCsp); 732 733 // thisMBB: 734 // ... 735 // fallthrough --> loopMBB 736 BB->addSuccessor(loopMBB); 737 738 // loopMBB: 739 // ldxr dest, ptr 740 // cmp incr, dest (, sign extend if necessary) 741 // csel scratch, dest, incr, cond 742 // stxr stxr_status, scratch, ptr 743 // cbnz stxr_status, loopMBB 744 // fallthrough --> exitMBB 745 BB = loopMBB; 746 BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr); 747 748 // Build compare and cmov instructions. 749 MRI.constrainRegClass(incr, TRCsp); 750 BuildMI(BB, dl, TII->get(CmpOp)) 751 .addReg(incr).addReg(oldval).addImm(0); 752 753 BuildMI(BB, dl, TII->get(Size == 8 ? AArch64::CSELxxxc : AArch64::CSELwwwc), 754 scratch) 755 .addReg(oldval).addReg(incr).addImm(Cond); 756 757 unsigned stxr_status = MRI.createVirtualRegister(&AArch64::GPR32RegClass); 758 MRI.constrainRegClass(stxr_status, &AArch64::GPR32wspRegClass); 759 760 BuildMI(BB, dl, TII->get(strOpc), stxr_status) 761 .addReg(scratch).addReg(ptr); 762 BuildMI(BB, dl, TII->get(AArch64::CBNZw)) 763 .addReg(stxr_status).addMBB(loopMBB); 764 765 BB->addSuccessor(loopMBB); 766 BB->addSuccessor(exitMBB); 767 768 // exitMBB: 769 // ... 770 BB = exitMBB; 771 772 MI->eraseFromParent(); // The instruction is gone now. 773 774 return BB; 775 } 776 777 MachineBasicBlock * 778 AArch64TargetLowering::emitAtomicCmpSwap(MachineInstr *MI, 779 MachineBasicBlock *BB, 780 unsigned Size) const { 781 unsigned dest = MI->getOperand(0).getReg(); 782 unsigned ptr = MI->getOperand(1).getReg(); 783 unsigned oldval = MI->getOperand(2).getReg(); 784 unsigned newval = MI->getOperand(3).getReg(); 785 AtomicOrdering Ord = static_cast<AtomicOrdering>(MI->getOperand(4).getImm()); 786 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 787 DebugLoc dl = MI->getDebugLoc(); 788 789 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 790 const TargetRegisterClass *TRCsp; 791 TRCsp = Size == 8 ? &AArch64::GPR64xspRegClass : &AArch64::GPR32wspRegClass; 792 793 unsigned ldrOpc, strOpc; 794 getExclusiveOperation(Size, Ord, ldrOpc, strOpc); 795 796 MachineFunction *MF = BB->getParent(); 797 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 798 MachineFunction::iterator It = BB; 799 ++It; // insert the new blocks after the current block 800 801 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB); 802 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB); 803 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 804 MF->insert(It, loop1MBB); 805 MF->insert(It, loop2MBB); 806 MF->insert(It, exitMBB); 807 808 // Transfer the remainder of BB and its successor edges to exitMBB. 809 exitMBB->splice(exitMBB->begin(), BB, 810 llvm::next(MachineBasicBlock::iterator(MI)), 811 BB->end()); 812 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 813 814 // thisMBB: 815 // ... 816 // fallthrough --> loop1MBB 817 BB->addSuccessor(loop1MBB); 818 819 // loop1MBB: 820 // ldxr dest, [ptr] 821 // cmp dest, oldval 822 // b.ne exitMBB 823 BB = loop1MBB; 824 BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr); 825 826 unsigned CmpOp = Size == 8 ? AArch64::CMPxx_lsl : AArch64::CMPww_lsl; 827 MRI.constrainRegClass(dest, TRCsp); 828 BuildMI(BB, dl, TII->get(CmpOp)) 829 .addReg(dest).addReg(oldval).addImm(0); 830 BuildMI(BB, dl, TII->get(AArch64::Bcc)) 831 .addImm(A64CC::NE).addMBB(exitMBB); 832 BB->addSuccessor(loop2MBB); 833 BB->addSuccessor(exitMBB); 834 835 // loop2MBB: 836 // strex stxr_status, newval, [ptr] 837 // cbnz stxr_status, loop1MBB 838 BB = loop2MBB; 839 unsigned stxr_status = MRI.createVirtualRegister(&AArch64::GPR32RegClass); 840 MRI.constrainRegClass(stxr_status, &AArch64::GPR32wspRegClass); 841 842 BuildMI(BB, dl, TII->get(strOpc), stxr_status).addReg(newval).addReg(ptr); 843 BuildMI(BB, dl, TII->get(AArch64::CBNZw)) 844 .addReg(stxr_status).addMBB(loop1MBB); 845 BB->addSuccessor(loop1MBB); 846 BB->addSuccessor(exitMBB); 847 848 // exitMBB: 849 // ... 850 BB = exitMBB; 851 852 MI->eraseFromParent(); // The instruction is gone now. 853 854 return BB; 855 } 856 857 MachineBasicBlock * 858 AArch64TargetLowering::EmitF128CSEL(MachineInstr *MI, 859 MachineBasicBlock *MBB) const { 860 // We materialise the F128CSEL pseudo-instruction using conditional branches 861 // and loads, giving an instruciton sequence like: 862 // str q0, [sp] 863 // b.ne IfTrue 864 // b Finish 865 // IfTrue: 866 // str q1, [sp] 867 // Finish: 868 // ldr q0, [sp] 869 // 870 // Using virtual registers would probably not be beneficial since COPY 871 // instructions are expensive for f128 (there's no actual instruction to 872 // implement them). 873 // 874 // An alternative would be to do an integer-CSEL on some address. E.g.: 875 // mov x0, sp 876 // add x1, sp, #16 877 // str q0, [x0] 878 // str q1, [x1] 879 // csel x0, x0, x1, ne 880 // ldr q0, [x0] 881 // 882 // It's unclear which approach is actually optimal. 883 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 884 MachineFunction *MF = MBB->getParent(); 885 const BasicBlock *LLVM_BB = MBB->getBasicBlock(); 886 DebugLoc DL = MI->getDebugLoc(); 887 MachineFunction::iterator It = MBB; 888 ++It; 889 890 unsigned DestReg = MI->getOperand(0).getReg(); 891 unsigned IfTrueReg = MI->getOperand(1).getReg(); 892 unsigned IfFalseReg = MI->getOperand(2).getReg(); 893 unsigned CondCode = MI->getOperand(3).getImm(); 894 bool NZCVKilled = MI->getOperand(4).isKill(); 895 896 MachineBasicBlock *TrueBB = MF->CreateMachineBasicBlock(LLVM_BB); 897 MachineBasicBlock *EndBB = MF->CreateMachineBasicBlock(LLVM_BB); 898 MF->insert(It, TrueBB); 899 MF->insert(It, EndBB); 900 901 // Transfer rest of current basic-block to EndBB 902 EndBB->splice(EndBB->begin(), MBB, 903 llvm::next(MachineBasicBlock::iterator(MI)), 904 MBB->end()); 905 EndBB->transferSuccessorsAndUpdatePHIs(MBB); 906 907 // We need somewhere to store the f128 value needed. 908 int ScratchFI = MF->getFrameInfo()->CreateSpillStackObject(16, 16); 909 910 // [... start of incoming MBB ...] 911 // str qIFFALSE, [sp] 912 // b.cc IfTrue 913 // b Done 914 BuildMI(MBB, DL, TII->get(AArch64::LSFP128_STR)) 915 .addReg(IfFalseReg) 916 .addFrameIndex(ScratchFI) 917 .addImm(0); 918 BuildMI(MBB, DL, TII->get(AArch64::Bcc)) 919 .addImm(CondCode) 920 .addMBB(TrueBB); 921 BuildMI(MBB, DL, TII->get(AArch64::Bimm)) 922 .addMBB(EndBB); 923 MBB->addSuccessor(TrueBB); 924 MBB->addSuccessor(EndBB); 925 926 if (!NZCVKilled) { 927 // NZCV is live-through TrueBB. 928 TrueBB->addLiveIn(AArch64::NZCV); 929 EndBB->addLiveIn(AArch64::NZCV); 930 } 931 932 // IfTrue: 933 // str qIFTRUE, [sp] 934 BuildMI(TrueBB, DL, TII->get(AArch64::LSFP128_STR)) 935 .addReg(IfTrueReg) 936 .addFrameIndex(ScratchFI) 937 .addImm(0); 938 939 // Note: fallthrough. We can rely on LLVM adding a branch if it reorders the 940 // blocks. 941 TrueBB->addSuccessor(EndBB); 942 943 // Done: 944 // ldr qDEST, [sp] 945 // [... rest of incoming MBB ...] 946 MachineInstr *StartOfEnd = EndBB->begin(); 947 BuildMI(*EndBB, StartOfEnd, DL, TII->get(AArch64::LSFP128_LDR), DestReg) 948 .addFrameIndex(ScratchFI) 949 .addImm(0); 950 951 MI->eraseFromParent(); 952 return EndBB; 953 } 954 955 MachineBasicBlock * 956 AArch64TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 957 MachineBasicBlock *MBB) const { 958 switch (MI->getOpcode()) { 959 default: llvm_unreachable("Unhandled instruction with custom inserter"); 960 case AArch64::F128CSEL: 961 return EmitF128CSEL(MI, MBB); 962 case AArch64::ATOMIC_LOAD_ADD_I8: 963 return emitAtomicBinary(MI, MBB, 1, AArch64::ADDwww_lsl); 964 case AArch64::ATOMIC_LOAD_ADD_I16: 965 return emitAtomicBinary(MI, MBB, 2, AArch64::ADDwww_lsl); 966 case AArch64::ATOMIC_LOAD_ADD_I32: 967 return emitAtomicBinary(MI, MBB, 4, AArch64::ADDwww_lsl); 968 case AArch64::ATOMIC_LOAD_ADD_I64: 969 return emitAtomicBinary(MI, MBB, 8, AArch64::ADDxxx_lsl); 970 971 case AArch64::ATOMIC_LOAD_SUB_I8: 972 return emitAtomicBinary(MI, MBB, 1, AArch64::SUBwww_lsl); 973 case AArch64::ATOMIC_LOAD_SUB_I16: 974 return emitAtomicBinary(MI, MBB, 2, AArch64::SUBwww_lsl); 975 case AArch64::ATOMIC_LOAD_SUB_I32: 976 return emitAtomicBinary(MI, MBB, 4, AArch64::SUBwww_lsl); 977 case AArch64::ATOMIC_LOAD_SUB_I64: 978 return emitAtomicBinary(MI, MBB, 8, AArch64::SUBxxx_lsl); 979 980 case AArch64::ATOMIC_LOAD_AND_I8: 981 return emitAtomicBinary(MI, MBB, 1, AArch64::ANDwww_lsl); 982 case AArch64::ATOMIC_LOAD_AND_I16: 983 return emitAtomicBinary(MI, MBB, 2, AArch64::ANDwww_lsl); 984 case AArch64::ATOMIC_LOAD_AND_I32: 985 return emitAtomicBinary(MI, MBB, 4, AArch64::ANDwww_lsl); 986 case AArch64::ATOMIC_LOAD_AND_I64: 987 return emitAtomicBinary(MI, MBB, 8, AArch64::ANDxxx_lsl); 988 989 case AArch64::ATOMIC_LOAD_OR_I8: 990 return emitAtomicBinary(MI, MBB, 1, AArch64::ORRwww_lsl); 991 case AArch64::ATOMIC_LOAD_OR_I16: 992 return emitAtomicBinary(MI, MBB, 2, AArch64::ORRwww_lsl); 993 case AArch64::ATOMIC_LOAD_OR_I32: 994 return emitAtomicBinary(MI, MBB, 4, AArch64::ORRwww_lsl); 995 case AArch64::ATOMIC_LOAD_OR_I64: 996 return emitAtomicBinary(MI, MBB, 8, AArch64::ORRxxx_lsl); 997 998 case AArch64::ATOMIC_LOAD_XOR_I8: 999 return emitAtomicBinary(MI, MBB, 1, AArch64::EORwww_lsl); 1000 case AArch64::ATOMIC_LOAD_XOR_I16: 1001 return emitAtomicBinary(MI, MBB, 2, AArch64::EORwww_lsl); 1002 case AArch64::ATOMIC_LOAD_XOR_I32: 1003 return emitAtomicBinary(MI, MBB, 4, AArch64::EORwww_lsl); 1004 case AArch64::ATOMIC_LOAD_XOR_I64: 1005 return emitAtomicBinary(MI, MBB, 8, AArch64::EORxxx_lsl); 1006 1007 case AArch64::ATOMIC_LOAD_NAND_I8: 1008 return emitAtomicBinary(MI, MBB, 1, AArch64::BICwww_lsl); 1009 case AArch64::ATOMIC_LOAD_NAND_I16: 1010 return emitAtomicBinary(MI, MBB, 2, AArch64::BICwww_lsl); 1011 case AArch64::ATOMIC_LOAD_NAND_I32: 1012 return emitAtomicBinary(MI, MBB, 4, AArch64::BICwww_lsl); 1013 case AArch64::ATOMIC_LOAD_NAND_I64: 1014 return emitAtomicBinary(MI, MBB, 8, AArch64::BICxxx_lsl); 1015 1016 case AArch64::ATOMIC_LOAD_MIN_I8: 1017 return emitAtomicBinaryMinMax(MI, MBB, 1, AArch64::CMPww_sxtb, A64CC::GT); 1018 case AArch64::ATOMIC_LOAD_MIN_I16: 1019 return emitAtomicBinaryMinMax(MI, MBB, 2, AArch64::CMPww_sxth, A64CC::GT); 1020 case AArch64::ATOMIC_LOAD_MIN_I32: 1021 return emitAtomicBinaryMinMax(MI, MBB, 4, AArch64::CMPww_lsl, A64CC::GT); 1022 case AArch64::ATOMIC_LOAD_MIN_I64: 1023 return emitAtomicBinaryMinMax(MI, MBB, 8, AArch64::CMPxx_lsl, A64CC::GT); 1024 1025 case AArch64::ATOMIC_LOAD_MAX_I8: 1026 return emitAtomicBinaryMinMax(MI, MBB, 1, AArch64::CMPww_sxtb, A64CC::LT); 1027 case AArch64::ATOMIC_LOAD_MAX_I16: 1028 return emitAtomicBinaryMinMax(MI, MBB, 2, AArch64::CMPww_sxth, A64CC::LT); 1029 case AArch64::ATOMIC_LOAD_MAX_I32: 1030 return emitAtomicBinaryMinMax(MI, MBB, 4, AArch64::CMPww_lsl, A64CC::LT); 1031 case AArch64::ATOMIC_LOAD_MAX_I64: 1032 return emitAtomicBinaryMinMax(MI, MBB, 8, AArch64::CMPxx_lsl, A64CC::LT); 1033 1034 case AArch64::ATOMIC_LOAD_UMIN_I8: 1035 return emitAtomicBinaryMinMax(MI, MBB, 1, AArch64::CMPww_uxtb, A64CC::HI); 1036 case AArch64::ATOMIC_LOAD_UMIN_I16: 1037 return emitAtomicBinaryMinMax(MI, MBB, 2, AArch64::CMPww_uxth, A64CC::HI); 1038 case AArch64::ATOMIC_LOAD_UMIN_I32: 1039 return emitAtomicBinaryMinMax(MI, MBB, 4, AArch64::CMPww_lsl, A64CC::HI); 1040 case AArch64::ATOMIC_LOAD_UMIN_I64: 1041 return emitAtomicBinaryMinMax(MI, MBB, 8, AArch64::CMPxx_lsl, A64CC::HI); 1042 1043 case AArch64::ATOMIC_LOAD_UMAX_I8: 1044 return emitAtomicBinaryMinMax(MI, MBB, 1, AArch64::CMPww_uxtb, A64CC::LO); 1045 case AArch64::ATOMIC_LOAD_UMAX_I16: 1046 return emitAtomicBinaryMinMax(MI, MBB, 2, AArch64::CMPww_uxth, A64CC::LO); 1047 case AArch64::ATOMIC_LOAD_UMAX_I32: 1048 return emitAtomicBinaryMinMax(MI, MBB, 4, AArch64::CMPww_lsl, A64CC::LO); 1049 case AArch64::ATOMIC_LOAD_UMAX_I64: 1050 return emitAtomicBinaryMinMax(MI, MBB, 8, AArch64::CMPxx_lsl, A64CC::LO); 1051 1052 case AArch64::ATOMIC_SWAP_I8: 1053 return emitAtomicBinary(MI, MBB, 1, 0); 1054 case AArch64::ATOMIC_SWAP_I16: 1055 return emitAtomicBinary(MI, MBB, 2, 0); 1056 case AArch64::ATOMIC_SWAP_I32: 1057 return emitAtomicBinary(MI, MBB, 4, 0); 1058 case AArch64::ATOMIC_SWAP_I64: 1059 return emitAtomicBinary(MI, MBB, 8, 0); 1060 1061 case AArch64::ATOMIC_CMP_SWAP_I8: 1062 return emitAtomicCmpSwap(MI, MBB, 1); 1063 case AArch64::ATOMIC_CMP_SWAP_I16: 1064 return emitAtomicCmpSwap(MI, MBB, 2); 1065 case AArch64::ATOMIC_CMP_SWAP_I32: 1066 return emitAtomicCmpSwap(MI, MBB, 4); 1067 case AArch64::ATOMIC_CMP_SWAP_I64: 1068 return emitAtomicCmpSwap(MI, MBB, 8); 1069 } 1070 } 1071 1072 1073 const char *AArch64TargetLowering::getTargetNodeName(unsigned Opcode) const { 1074 switch (Opcode) { 1075 case AArch64ISD::BR_CC: return "AArch64ISD::BR_CC"; 1076 case AArch64ISD::Call: return "AArch64ISD::Call"; 1077 case AArch64ISD::FPMOV: return "AArch64ISD::FPMOV"; 1078 case AArch64ISD::GOTLoad: return "AArch64ISD::GOTLoad"; 1079 case AArch64ISD::BFI: return "AArch64ISD::BFI"; 1080 case AArch64ISD::EXTR: return "AArch64ISD::EXTR"; 1081 case AArch64ISD::Ret: return "AArch64ISD::Ret"; 1082 case AArch64ISD::SBFX: return "AArch64ISD::SBFX"; 1083 case AArch64ISD::SELECT_CC: return "AArch64ISD::SELECT_CC"; 1084 case AArch64ISD::SETCC: return "AArch64ISD::SETCC"; 1085 case AArch64ISD::TC_RETURN: return "AArch64ISD::TC_RETURN"; 1086 case AArch64ISD::THREAD_POINTER: return "AArch64ISD::THREAD_POINTER"; 1087 case AArch64ISD::TLSDESCCALL: return "AArch64ISD::TLSDESCCALL"; 1088 case AArch64ISD::WrapperLarge: return "AArch64ISD::WrapperLarge"; 1089 case AArch64ISD::WrapperSmall: return "AArch64ISD::WrapperSmall"; 1090 1091 case AArch64ISD::NEON_MOVIMM: 1092 return "AArch64ISD::NEON_MOVIMM"; 1093 case AArch64ISD::NEON_MVNIMM: 1094 return "AArch64ISD::NEON_MVNIMM"; 1095 case AArch64ISD::NEON_FMOVIMM: 1096 return "AArch64ISD::NEON_FMOVIMM"; 1097 case AArch64ISD::NEON_CMP: 1098 return "AArch64ISD::NEON_CMP"; 1099 case AArch64ISD::NEON_CMPZ: 1100 return "AArch64ISD::NEON_CMPZ"; 1101 case AArch64ISD::NEON_TST: 1102 return "AArch64ISD::NEON_TST"; 1103 case AArch64ISD::NEON_QSHLs: 1104 return "AArch64ISD::NEON_QSHLs"; 1105 case AArch64ISD::NEON_QSHLu: 1106 return "AArch64ISD::NEON_QSHLu"; 1107 case AArch64ISD::NEON_VDUP: 1108 return "AArch64ISD::NEON_VDUP"; 1109 case AArch64ISD::NEON_VDUPLANE: 1110 return "AArch64ISD::NEON_VDUPLANE"; 1111 case AArch64ISD::NEON_REV16: 1112 return "AArch64ISD::NEON_REV16"; 1113 case AArch64ISD::NEON_REV32: 1114 return "AArch64ISD::NEON_REV32"; 1115 case AArch64ISD::NEON_REV64: 1116 return "AArch64ISD::NEON_REV64"; 1117 case AArch64ISD::NEON_UZP1: 1118 return "AArch64ISD::NEON_UZP1"; 1119 case AArch64ISD::NEON_UZP2: 1120 return "AArch64ISD::NEON_UZP2"; 1121 case AArch64ISD::NEON_ZIP1: 1122 return "AArch64ISD::NEON_ZIP1"; 1123 case AArch64ISD::NEON_ZIP2: 1124 return "AArch64ISD::NEON_ZIP2"; 1125 case AArch64ISD::NEON_TRN1: 1126 return "AArch64ISD::NEON_TRN1"; 1127 case AArch64ISD::NEON_TRN2: 1128 return "AArch64ISD::NEON_TRN2"; 1129 case AArch64ISD::NEON_LD1_UPD: 1130 return "AArch64ISD::NEON_LD1_UPD"; 1131 case AArch64ISD::NEON_LD2_UPD: 1132 return "AArch64ISD::NEON_LD2_UPD"; 1133 case AArch64ISD::NEON_LD3_UPD: 1134 return "AArch64ISD::NEON_LD3_UPD"; 1135 case AArch64ISD::NEON_LD4_UPD: 1136 return "AArch64ISD::NEON_LD4_UPD"; 1137 case AArch64ISD::NEON_ST1_UPD: 1138 return "AArch64ISD::NEON_ST1_UPD"; 1139 case AArch64ISD::NEON_ST2_UPD: 1140 return "AArch64ISD::NEON_ST2_UPD"; 1141 case AArch64ISD::NEON_ST3_UPD: 1142 return "AArch64ISD::NEON_ST3_UPD"; 1143 case AArch64ISD::NEON_ST4_UPD: 1144 return "AArch64ISD::NEON_ST4_UPD"; 1145 case AArch64ISD::NEON_LD1x2_UPD: 1146 return "AArch64ISD::NEON_LD1x2_UPD"; 1147 case AArch64ISD::NEON_LD1x3_UPD: 1148 return "AArch64ISD::NEON_LD1x3_UPD"; 1149 case AArch64ISD::NEON_LD1x4_UPD: 1150 return "AArch64ISD::NEON_LD1x4_UPD"; 1151 case AArch64ISD::NEON_ST1x2_UPD: 1152 return "AArch64ISD::NEON_ST1x2_UPD"; 1153 case AArch64ISD::NEON_ST1x3_UPD: 1154 return "AArch64ISD::NEON_ST1x3_UPD"; 1155 case AArch64ISD::NEON_ST1x4_UPD: 1156 return "AArch64ISD::NEON_ST1x4_UPD"; 1157 case AArch64ISD::NEON_LD2DUP: 1158 return "AArch64ISD::NEON_LD2DUP"; 1159 case AArch64ISD::NEON_LD3DUP: 1160 return "AArch64ISD::NEON_LD3DUP"; 1161 case AArch64ISD::NEON_LD4DUP: 1162 return "AArch64ISD::NEON_LD4DUP"; 1163 case AArch64ISD::NEON_LD2DUP_UPD: 1164 return "AArch64ISD::NEON_LD2DUP_UPD"; 1165 case AArch64ISD::NEON_LD3DUP_UPD: 1166 return "AArch64ISD::NEON_LD3DUP_UPD"; 1167 case AArch64ISD::NEON_LD4DUP_UPD: 1168 return "AArch64ISD::NEON_LD4DUP_UPD"; 1169 case AArch64ISD::NEON_LD2LN_UPD: 1170 return "AArch64ISD::NEON_LD2LN_UPD"; 1171 case AArch64ISD::NEON_LD3LN_UPD: 1172 return "AArch64ISD::NEON_LD3LN_UPD"; 1173 case AArch64ISD::NEON_LD4LN_UPD: 1174 return "AArch64ISD::NEON_LD4LN_UPD"; 1175 case AArch64ISD::NEON_ST2LN_UPD: 1176 return "AArch64ISD::NEON_ST2LN_UPD"; 1177 case AArch64ISD::NEON_ST3LN_UPD: 1178 return "AArch64ISD::NEON_ST3LN_UPD"; 1179 case AArch64ISD::NEON_ST4LN_UPD: 1180 return "AArch64ISD::NEON_ST4LN_UPD"; 1181 case AArch64ISD::NEON_VEXTRACT: 1182 return "AArch64ISD::NEON_VEXTRACT"; 1183 default: 1184 return NULL; 1185 } 1186 } 1187 1188 static const uint16_t AArch64FPRArgRegs[] = { 1189 AArch64::Q0, AArch64::Q1, AArch64::Q2, AArch64::Q3, 1190 AArch64::Q4, AArch64::Q5, AArch64::Q6, AArch64::Q7 1191 }; 1192 static const unsigned NumFPRArgRegs = llvm::array_lengthof(AArch64FPRArgRegs); 1193 1194 static const uint16_t AArch64ArgRegs[] = { 1195 AArch64::X0, AArch64::X1, AArch64::X2, AArch64::X3, 1196 AArch64::X4, AArch64::X5, AArch64::X6, AArch64::X7 1197 }; 1198 static const unsigned NumArgRegs = llvm::array_lengthof(AArch64ArgRegs); 1199 1200 static bool CC_AArch64NoMoreRegs(unsigned ValNo, MVT ValVT, MVT LocVT, 1201 CCValAssign::LocInfo LocInfo, 1202 ISD::ArgFlagsTy ArgFlags, CCState &State) { 1203 // Mark all remaining general purpose registers as allocated. We don't 1204 // backtrack: if (for example) an i128 gets put on the stack, no subsequent 1205 // i64 will go in registers (C.11). 1206 for (unsigned i = 0; i < NumArgRegs; ++i) 1207 State.AllocateReg(AArch64ArgRegs[i]); 1208 1209 return false; 1210 } 1211 1212 #include "AArch64GenCallingConv.inc" 1213 1214 CCAssignFn *AArch64TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const { 1215 1216 switch(CC) { 1217 default: llvm_unreachable("Unsupported calling convention"); 1218 case CallingConv::Fast: 1219 case CallingConv::C: 1220 return CC_A64_APCS; 1221 } 1222 } 1223 1224 void 1225 AArch64TargetLowering::SaveVarArgRegisters(CCState &CCInfo, SelectionDAG &DAG, 1226 SDLoc DL, SDValue &Chain) const { 1227 MachineFunction &MF = DAG.getMachineFunction(); 1228 MachineFrameInfo *MFI = MF.getFrameInfo(); 1229 AArch64MachineFunctionInfo *FuncInfo 1230 = MF.getInfo<AArch64MachineFunctionInfo>(); 1231 1232 SmallVector<SDValue, 8> MemOps; 1233 1234 unsigned FirstVariadicGPR = CCInfo.getFirstUnallocated(AArch64ArgRegs, 1235 NumArgRegs); 1236 unsigned FirstVariadicFPR = CCInfo.getFirstUnallocated(AArch64FPRArgRegs, 1237 NumFPRArgRegs); 1238 1239 unsigned GPRSaveSize = 8 * (NumArgRegs - FirstVariadicGPR); 1240 int GPRIdx = 0; 1241 if (GPRSaveSize != 0) { 1242 GPRIdx = MFI->CreateStackObject(GPRSaveSize, 8, false); 1243 1244 SDValue FIN = DAG.getFrameIndex(GPRIdx, getPointerTy()); 1245 1246 for (unsigned i = FirstVariadicGPR; i < NumArgRegs; ++i) { 1247 unsigned VReg = MF.addLiveIn(AArch64ArgRegs[i], &AArch64::GPR64RegClass); 1248 SDValue Val = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64); 1249 SDValue Store = DAG.getStore(Val.getValue(1), DL, Val, FIN, 1250 MachinePointerInfo::getStack(i * 8), 1251 false, false, 0); 1252 MemOps.push_back(Store); 1253 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(), FIN, 1254 DAG.getConstant(8, getPointerTy())); 1255 } 1256 } 1257 1258 if (getSubtarget()->hasFPARMv8()) { 1259 unsigned FPRSaveSize = 16 * (NumFPRArgRegs - FirstVariadicFPR); 1260 int FPRIdx = 0; 1261 // According to the AArch64 Procedure Call Standard, section B.1/B.3, we 1262 // can omit a register save area if we know we'll never use registers of 1263 // that class. 1264 if (FPRSaveSize != 0) { 1265 FPRIdx = MFI->CreateStackObject(FPRSaveSize, 16, false); 1266 1267 SDValue FIN = DAG.getFrameIndex(FPRIdx, getPointerTy()); 1268 1269 for (unsigned i = FirstVariadicFPR; i < NumFPRArgRegs; ++i) { 1270 unsigned VReg = MF.addLiveIn(AArch64FPRArgRegs[i], 1271 &AArch64::FPR128RegClass); 1272 SDValue Val = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f128); 1273 SDValue Store = DAG.getStore(Val.getValue(1), DL, Val, FIN, 1274 MachinePointerInfo::getStack(i * 16), 1275 false, false, 0); 1276 MemOps.push_back(Store); 1277 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(), FIN, 1278 DAG.getConstant(16, getPointerTy())); 1279 } 1280 } 1281 FuncInfo->setVariadicFPRIdx(FPRIdx); 1282 FuncInfo->setVariadicFPRSize(FPRSaveSize); 1283 } 1284 1285 int StackIdx = MFI->CreateFixedObject(8, CCInfo.getNextStackOffset(), true); 1286 1287 FuncInfo->setVariadicStackIdx(StackIdx); 1288 FuncInfo->setVariadicGPRIdx(GPRIdx); 1289 FuncInfo->setVariadicGPRSize(GPRSaveSize); 1290 1291 if (!MemOps.empty()) { 1292 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &MemOps[0], 1293 MemOps.size()); 1294 } 1295 } 1296 1297 1298 SDValue 1299 AArch64TargetLowering::LowerFormalArguments(SDValue Chain, 1300 CallingConv::ID CallConv, bool isVarArg, 1301 const SmallVectorImpl<ISD::InputArg> &Ins, 1302 SDLoc dl, SelectionDAG &DAG, 1303 SmallVectorImpl<SDValue> &InVals) const { 1304 MachineFunction &MF = DAG.getMachineFunction(); 1305 AArch64MachineFunctionInfo *FuncInfo 1306 = MF.getInfo<AArch64MachineFunctionInfo>(); 1307 MachineFrameInfo *MFI = MF.getFrameInfo(); 1308 bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt; 1309 1310 SmallVector<CCValAssign, 16> ArgLocs; 1311 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 1312 getTargetMachine(), ArgLocs, *DAG.getContext()); 1313 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv)); 1314 1315 SmallVector<SDValue, 16> ArgValues; 1316 1317 SDValue ArgValue; 1318 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 1319 CCValAssign &VA = ArgLocs[i]; 1320 ISD::ArgFlagsTy Flags = Ins[i].Flags; 1321 1322 if (Flags.isByVal()) { 1323 // Byval is used for small structs and HFAs in the PCS, but the system 1324 // should work in a non-compliant manner for larger structs. 1325 EVT PtrTy = getPointerTy(); 1326 int Size = Flags.getByValSize(); 1327 unsigned NumRegs = (Size + 7) / 8; 1328 1329 unsigned FrameIdx = MFI->CreateFixedObject(8 * NumRegs, 1330 VA.getLocMemOffset(), 1331 false); 1332 SDValue FrameIdxN = DAG.getFrameIndex(FrameIdx, PtrTy); 1333 InVals.push_back(FrameIdxN); 1334 1335 continue; 1336 } else if (VA.isRegLoc()) { 1337 MVT RegVT = VA.getLocVT(); 1338 const TargetRegisterClass *RC = getRegClassFor(RegVT); 1339 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 1340 1341 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); 1342 } else { // VA.isRegLoc() 1343 assert(VA.isMemLoc()); 1344 1345 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8, 1346 VA.getLocMemOffset(), true); 1347 1348 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 1349 ArgValue = DAG.getLoad(VA.getLocVT(), dl, Chain, FIN, 1350 MachinePointerInfo::getFixedStack(FI), 1351 false, false, false, 0); 1352 1353 1354 } 1355 1356 switch (VA.getLocInfo()) { 1357 default: llvm_unreachable("Unknown loc info!"); 1358 case CCValAssign::Full: break; 1359 case CCValAssign::BCvt: 1360 ArgValue = DAG.getNode(ISD::BITCAST,dl, VA.getValVT(), ArgValue); 1361 break; 1362 case CCValAssign::SExt: 1363 case CCValAssign::ZExt: 1364 case CCValAssign::AExt: 1365 case CCValAssign::FPExt: { 1366 unsigned DestSize = VA.getValVT().getSizeInBits(); 1367 unsigned DestSubReg; 1368 1369 switch (DestSize) { 1370 case 8: DestSubReg = AArch64::sub_8; break; 1371 case 16: DestSubReg = AArch64::sub_16; break; 1372 case 32: DestSubReg = AArch64::sub_32; break; 1373 case 64: DestSubReg = AArch64::sub_64; break; 1374 default: llvm_unreachable("Unexpected argument promotion"); 1375 } 1376 1377 ArgValue = SDValue(DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl, 1378 VA.getValVT(), ArgValue, 1379 DAG.getTargetConstant(DestSubReg, MVT::i32)), 1380 0); 1381 break; 1382 } 1383 } 1384 1385 InVals.push_back(ArgValue); 1386 } 1387 1388 if (isVarArg) 1389 SaveVarArgRegisters(CCInfo, DAG, dl, Chain); 1390 1391 unsigned StackArgSize = CCInfo.getNextStackOffset(); 1392 if (DoesCalleeRestoreStack(CallConv, TailCallOpt)) { 1393 // This is a non-standard ABI so by fiat I say we're allowed to make full 1394 // use of the stack area to be popped, which must be aligned to 16 bytes in 1395 // any case: 1396 StackArgSize = RoundUpToAlignment(StackArgSize, 16); 1397 1398 // If we're expected to restore the stack (e.g. fastcc) then we'll be adding 1399 // a multiple of 16. 1400 FuncInfo->setArgumentStackToRestore(StackArgSize); 1401 1402 // This realignment carries over to the available bytes below. Our own 1403 // callers will guarantee the space is free by giving an aligned value to 1404 // CALLSEQ_START. 1405 } 1406 // Even if we're not expected to free up the space, it's useful to know how 1407 // much is there while considering tail calls (because we can reuse it). 1408 FuncInfo->setBytesInStackArgArea(StackArgSize); 1409 1410 return Chain; 1411 } 1412 1413 SDValue 1414 AArch64TargetLowering::LowerReturn(SDValue Chain, 1415 CallingConv::ID CallConv, bool isVarArg, 1416 const SmallVectorImpl<ISD::OutputArg> &Outs, 1417 const SmallVectorImpl<SDValue> &OutVals, 1418 SDLoc dl, SelectionDAG &DAG) const { 1419 // CCValAssign - represent the assignment of the return value to a location. 1420 SmallVector<CCValAssign, 16> RVLocs; 1421 1422 // CCState - Info about the registers and stack slots. 1423 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 1424 getTargetMachine(), RVLocs, *DAG.getContext()); 1425 1426 // Analyze outgoing return values. 1427 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv)); 1428 1429 SDValue Flag; 1430 SmallVector<SDValue, 4> RetOps(1, Chain); 1431 1432 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) { 1433 // PCS: "If the type, T, of the result of a function is such that 1434 // void func(T arg) would require that arg be passed as a value in a 1435 // register (or set of registers) according to the rules in 5.4, then the 1436 // result is returned in the same registers as would be used for such an 1437 // argument. 1438 // 1439 // Otherwise, the caller shall reserve a block of memory of sufficient 1440 // size and alignment to hold the result. The address of the memory block 1441 // shall be passed as an additional argument to the function in x8." 1442 // 1443 // This is implemented in two places. The register-return values are dealt 1444 // with here, more complex returns are passed as an sret parameter, which 1445 // means we don't have to worry about it during actual return. 1446 CCValAssign &VA = RVLocs[i]; 1447 assert(VA.isRegLoc() && "Only register-returns should be created by PCS"); 1448 1449 1450 SDValue Arg = OutVals[i]; 1451 1452 // There's no convenient note in the ABI about this as there is for normal 1453 // arguments, but it says return values are passed in the same registers as 1454 // an argument would be. I believe that includes the comments about 1455 // unspecified higher bits, putting the burden of widening on the *caller* 1456 // for return values. 1457 switch (VA.getLocInfo()) { 1458 default: llvm_unreachable("Unknown loc info"); 1459 case CCValAssign::Full: break; 1460 case CCValAssign::SExt: 1461 case CCValAssign::ZExt: 1462 case CCValAssign::AExt: 1463 // Floating-point values should only be extended when they're going into 1464 // memory, which can't happen here so an integer extend is acceptable. 1465 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 1466 break; 1467 case CCValAssign::BCvt: 1468 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 1469 break; 1470 } 1471 1472 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag); 1473 Flag = Chain.getValue(1); 1474 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 1475 } 1476 1477 RetOps[0] = Chain; // Update chain. 1478 1479 // Add the flag if we have it. 1480 if (Flag.getNode()) 1481 RetOps.push_back(Flag); 1482 1483 return DAG.getNode(AArch64ISD::Ret, dl, MVT::Other, 1484 &RetOps[0], RetOps.size()); 1485 } 1486 1487 unsigned AArch64TargetLowering::getByValTypeAlignment(Type *Ty) const { 1488 // This is a new backend. For anything more precise than this a FE should 1489 // set an explicit alignment. 1490 return 4; 1491 } 1492 1493 SDValue 1494 AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI, 1495 SmallVectorImpl<SDValue> &InVals) const { 1496 SelectionDAG &DAG = CLI.DAG; 1497 SDLoc &dl = CLI.DL; 1498 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 1499 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 1500 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 1501 SDValue Chain = CLI.Chain; 1502 SDValue Callee = CLI.Callee; 1503 bool &IsTailCall = CLI.IsTailCall; 1504 CallingConv::ID CallConv = CLI.CallConv; 1505 bool IsVarArg = CLI.IsVarArg; 1506 1507 MachineFunction &MF = DAG.getMachineFunction(); 1508 AArch64MachineFunctionInfo *FuncInfo 1509 = MF.getInfo<AArch64MachineFunctionInfo>(); 1510 bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt; 1511 bool IsStructRet = !Outs.empty() && Outs[0].Flags.isSRet(); 1512 bool IsSibCall = false; 1513 1514 if (IsTailCall) { 1515 IsTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, 1516 IsVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(), 1517 Outs, OutVals, Ins, DAG); 1518 1519 // A sibling call is one where we're under the usual C ABI and not planning 1520 // to change that but can still do a tail call: 1521 if (!TailCallOpt && IsTailCall) 1522 IsSibCall = true; 1523 } 1524 1525 SmallVector<CCValAssign, 16> ArgLocs; 1526 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), 1527 getTargetMachine(), ArgLocs, *DAG.getContext()); 1528 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv)); 1529 1530 // On AArch64 (and all other architectures I'm aware of) the most this has to 1531 // do is adjust the stack pointer. 1532 unsigned NumBytes = RoundUpToAlignment(CCInfo.getNextStackOffset(), 16); 1533 if (IsSibCall) { 1534 // Since we're not changing the ABI to make this a tail call, the memory 1535 // operands are already available in the caller's incoming argument space. 1536 NumBytes = 0; 1537 } 1538 1539 // FPDiff is the byte offset of the call's argument area from the callee's. 1540 // Stores to callee stack arguments will be placed in FixedStackSlots offset 1541 // by this amount for a tail call. In a sibling call it must be 0 because the 1542 // caller will deallocate the entire stack and the callee still expects its 1543 // arguments to begin at SP+0. Completely unused for non-tail calls. 1544 int FPDiff = 0; 1545 1546 if (IsTailCall && !IsSibCall) { 1547 unsigned NumReusableBytes = FuncInfo->getBytesInStackArgArea(); 1548 1549 // FPDiff will be negative if this tail call requires more space than we 1550 // would automatically have in our incoming argument space. Positive if we 1551 // can actually shrink the stack. 1552 FPDiff = NumReusableBytes - NumBytes; 1553 1554 // The stack pointer must be 16-byte aligned at all times it's used for a 1555 // memory operation, which in practice means at *all* times and in 1556 // particular across call boundaries. Therefore our own arguments started at 1557 // a 16-byte aligned SP and the delta applied for the tail call should 1558 // satisfy the same constraint. 1559 assert(FPDiff % 16 == 0 && "unaligned stack on tail call"); 1560 } 1561 1562 if (!IsSibCall) 1563 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true), 1564 dl); 1565 1566 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, AArch64::XSP, 1567 getPointerTy()); 1568 1569 SmallVector<SDValue, 8> MemOpChains; 1570 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; 1571 1572 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 1573 CCValAssign &VA = ArgLocs[i]; 1574 ISD::ArgFlagsTy Flags = Outs[i].Flags; 1575 SDValue Arg = OutVals[i]; 1576 1577 // Callee does the actual widening, so all extensions just use an implicit 1578 // definition of the rest of the Loc. Aesthetically, this would be nicer as 1579 // an ANY_EXTEND, but that isn't valid for floating-point types and this 1580 // alternative works on integer types too. 1581 switch (VA.getLocInfo()) { 1582 default: llvm_unreachable("Unknown loc info!"); 1583 case CCValAssign::Full: break; 1584 case CCValAssign::SExt: 1585 case CCValAssign::ZExt: 1586 case CCValAssign::AExt: 1587 case CCValAssign::FPExt: { 1588 unsigned SrcSize = VA.getValVT().getSizeInBits(); 1589 unsigned SrcSubReg; 1590 1591 switch (SrcSize) { 1592 case 8: SrcSubReg = AArch64::sub_8; break; 1593 case 16: SrcSubReg = AArch64::sub_16; break; 1594 case 32: SrcSubReg = AArch64::sub_32; break; 1595 case 64: SrcSubReg = AArch64::sub_64; break; 1596 default: llvm_unreachable("Unexpected argument promotion"); 1597 } 1598 1599 Arg = SDValue(DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl, 1600 VA.getLocVT(), 1601 DAG.getUNDEF(VA.getLocVT()), 1602 Arg, 1603 DAG.getTargetConstant(SrcSubReg, MVT::i32)), 1604 0); 1605 1606 break; 1607 } 1608 case CCValAssign::BCvt: 1609 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 1610 break; 1611 } 1612 1613 if (VA.isRegLoc()) { 1614 // A normal register (sub-) argument. For now we just note it down because 1615 // we want to copy things into registers as late as possible to avoid 1616 // register-pressure (and possibly worse). 1617 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 1618 continue; 1619 } 1620 1621 assert(VA.isMemLoc() && "unexpected argument location"); 1622 1623 SDValue DstAddr; 1624 MachinePointerInfo DstInfo; 1625 if (IsTailCall) { 1626 uint32_t OpSize = Flags.isByVal() ? Flags.getByValSize() : 1627 VA.getLocVT().getSizeInBits(); 1628 OpSize = (OpSize + 7) / 8; 1629 int32_t Offset = VA.getLocMemOffset() + FPDiff; 1630 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true); 1631 1632 DstAddr = DAG.getFrameIndex(FI, getPointerTy()); 1633 DstInfo = MachinePointerInfo::getFixedStack(FI); 1634 1635 // Make sure any stack arguments overlapping with where we're storing are 1636 // loaded before this eventual operation. Otherwise they'll be clobbered. 1637 Chain = addTokenForArgument(Chain, DAG, MF.getFrameInfo(), FI); 1638 } else { 1639 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()); 1640 1641 DstAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff); 1642 DstInfo = MachinePointerInfo::getStack(VA.getLocMemOffset()); 1643 } 1644 1645 if (Flags.isByVal()) { 1646 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i64); 1647 SDValue Cpy = DAG.getMemcpy(Chain, dl, DstAddr, Arg, SizeNode, 1648 Flags.getByValAlign(), 1649 /*isVolatile = */ false, 1650 /*alwaysInline = */ false, 1651 DstInfo, MachinePointerInfo(0)); 1652 MemOpChains.push_back(Cpy); 1653 } else { 1654 // Normal stack argument, put it where it's needed. 1655 SDValue Store = DAG.getStore(Chain, dl, Arg, DstAddr, DstInfo, 1656 false, false, 0); 1657 MemOpChains.push_back(Store); 1658 } 1659 } 1660 1661 // The loads and stores generated above shouldn't clash with each 1662 // other. Combining them with this TokenFactor notes that fact for the rest of 1663 // the backend. 1664 if (!MemOpChains.empty()) 1665 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 1666 &MemOpChains[0], MemOpChains.size()); 1667 1668 // Most of the rest of the instructions need to be glued together; we don't 1669 // want assignments to actual registers used by a call to be rearranged by a 1670 // well-meaning scheduler. 1671 SDValue InFlag; 1672 1673 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1674 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1675 RegsToPass[i].second, InFlag); 1676 InFlag = Chain.getValue(1); 1677 } 1678 1679 // The linker is responsible for inserting veneers when necessary to put a 1680 // function call destination in range, so we don't need to bother with a 1681 // wrapper here. 1682 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1683 const GlobalValue *GV = G->getGlobal(); 1684 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy()); 1685 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 1686 const char *Sym = S->getSymbol(); 1687 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy()); 1688 } 1689 1690 // We don't usually want to end the call-sequence here because we would tidy 1691 // the frame up *after* the call, however in the ABI-changing tail-call case 1692 // we've carefully laid out the parameters so that when sp is reset they'll be 1693 // in the correct location. 1694 if (IsTailCall && !IsSibCall) { 1695 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), 1696 DAG.getIntPtrConstant(0, true), InFlag, dl); 1697 InFlag = Chain.getValue(1); 1698 } 1699 1700 // We produce the following DAG scheme for the actual call instruction: 1701 // (AArch64Call Chain, Callee, reg1, ..., regn, preserveMask, inflag? 1702 // 1703 // Most arguments aren't going to be used and just keep the values live as 1704 // far as LLVM is concerned. It's expected to be selected as simply "bl 1705 // callee" (for a direct, non-tail call). 1706 std::vector<SDValue> Ops; 1707 Ops.push_back(Chain); 1708 Ops.push_back(Callee); 1709 1710 if (IsTailCall) { 1711 // Each tail call may have to adjust the stack by a different amount, so 1712 // this information must travel along with the operation for eventual 1713 // consumption by emitEpilogue. 1714 Ops.push_back(DAG.getTargetConstant(FPDiff, MVT::i32)); 1715 } 1716 1717 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 1718 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 1719 RegsToPass[i].second.getValueType())); 1720 1721 1722 // Add a register mask operand representing the call-preserved registers. This 1723 // is used later in codegen to constrain register-allocation. 1724 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); 1725 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv); 1726 assert(Mask && "Missing call preserved mask for calling convention"); 1727 Ops.push_back(DAG.getRegisterMask(Mask)); 1728 1729 // If we needed glue, put it in as the last argument. 1730 if (InFlag.getNode()) 1731 Ops.push_back(InFlag); 1732 1733 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 1734 1735 if (IsTailCall) { 1736 return DAG.getNode(AArch64ISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size()); 1737 } 1738 1739 Chain = DAG.getNode(AArch64ISD::Call, dl, NodeTys, &Ops[0], Ops.size()); 1740 InFlag = Chain.getValue(1); 1741 1742 // Now we can reclaim the stack, just as well do it before working out where 1743 // our return value is. 1744 if (!IsSibCall) { 1745 uint64_t CalleePopBytes 1746 = DoesCalleeRestoreStack(CallConv, TailCallOpt) ? NumBytes : 0; 1747 1748 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), 1749 DAG.getIntPtrConstant(CalleePopBytes, true), 1750 InFlag, dl); 1751 InFlag = Chain.getValue(1); 1752 } 1753 1754 return LowerCallResult(Chain, InFlag, CallConv, 1755 IsVarArg, Ins, dl, DAG, InVals); 1756 } 1757 1758 SDValue 1759 AArch64TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, 1760 CallingConv::ID CallConv, bool IsVarArg, 1761 const SmallVectorImpl<ISD::InputArg> &Ins, 1762 SDLoc dl, SelectionDAG &DAG, 1763 SmallVectorImpl<SDValue> &InVals) const { 1764 // Assign locations to each value returned by this call. 1765 SmallVector<CCValAssign, 16> RVLocs; 1766 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), 1767 getTargetMachine(), RVLocs, *DAG.getContext()); 1768 CCInfo.AnalyzeCallResult(Ins, CCAssignFnForNode(CallConv)); 1769 1770 for (unsigned i = 0; i != RVLocs.size(); ++i) { 1771 CCValAssign VA = RVLocs[i]; 1772 1773 // Return values that are too big to fit into registers should use an sret 1774 // pointer, so this can be a lot simpler than the main argument code. 1775 assert(VA.isRegLoc() && "Memory locations not expected for call return"); 1776 1777 SDValue Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(), 1778 InFlag); 1779 Chain = Val.getValue(1); 1780 InFlag = Val.getValue(2); 1781 1782 switch (VA.getLocInfo()) { 1783 default: llvm_unreachable("Unknown loc info!"); 1784 case CCValAssign::Full: break; 1785 case CCValAssign::BCvt: 1786 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val); 1787 break; 1788 case CCValAssign::ZExt: 1789 case CCValAssign::SExt: 1790 case CCValAssign::AExt: 1791 // Floating-point arguments only get extended/truncated if they're going 1792 // in memory, so using the integer operation is acceptable here. 1793 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val); 1794 break; 1795 } 1796 1797 InVals.push_back(Val); 1798 } 1799 1800 return Chain; 1801 } 1802 1803 bool 1804 AArch64TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, 1805 CallingConv::ID CalleeCC, 1806 bool IsVarArg, 1807 bool IsCalleeStructRet, 1808 bool IsCallerStructRet, 1809 const SmallVectorImpl<ISD::OutputArg> &Outs, 1810 const SmallVectorImpl<SDValue> &OutVals, 1811 const SmallVectorImpl<ISD::InputArg> &Ins, 1812 SelectionDAG& DAG) const { 1813 1814 // For CallingConv::C this function knows whether the ABI needs 1815 // changing. That's not true for other conventions so they will have to opt in 1816 // manually. 1817 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C) 1818 return false; 1819 1820 const MachineFunction &MF = DAG.getMachineFunction(); 1821 const Function *CallerF = MF.getFunction(); 1822 CallingConv::ID CallerCC = CallerF->getCallingConv(); 1823 bool CCMatch = CallerCC == CalleeCC; 1824 1825 // Byval parameters hand the function a pointer directly into the stack area 1826 // we want to reuse during a tail call. Working around this *is* possible (see 1827 // X86) but less efficient and uglier in LowerCall. 1828 for (Function::const_arg_iterator i = CallerF->arg_begin(), 1829 e = CallerF->arg_end(); i != e; ++i) 1830 if (i->hasByValAttr()) 1831 return false; 1832 1833 if (getTargetMachine().Options.GuaranteedTailCallOpt) { 1834 if (IsTailCallConvention(CalleeCC) && CCMatch) 1835 return true; 1836 return false; 1837 } 1838 1839 // Now we search for cases where we can use a tail call without changing the 1840 // ABI. Sibcall is used in some places (particularly gcc) to refer to this 1841 // concept. 1842 1843 // I want anyone implementing a new calling convention to think long and hard 1844 // about this assert. 1845 assert((!IsVarArg || CalleeCC == CallingConv::C) 1846 && "Unexpected variadic calling convention"); 1847 1848 if (IsVarArg && !Outs.empty()) { 1849 // At least two cases here: if caller is fastcc then we can't have any 1850 // memory arguments (we'd be expected to clean up the stack afterwards). If 1851 // caller is C then we could potentially use its argument area. 1852 1853 // FIXME: for now we take the most conservative of these in both cases: 1854 // disallow all variadic memory operands. 1855 SmallVector<CCValAssign, 16> ArgLocs; 1856 CCState CCInfo(CalleeCC, IsVarArg, DAG.getMachineFunction(), 1857 getTargetMachine(), ArgLocs, *DAG.getContext()); 1858 1859 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC)); 1860 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) 1861 if (!ArgLocs[i].isRegLoc()) 1862 return false; 1863 } 1864 1865 // If the calling conventions do not match, then we'd better make sure the 1866 // results are returned in the same way as what the caller expects. 1867 if (!CCMatch) { 1868 SmallVector<CCValAssign, 16> RVLocs1; 1869 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), 1870 getTargetMachine(), RVLocs1, *DAG.getContext()); 1871 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC)); 1872 1873 SmallVector<CCValAssign, 16> RVLocs2; 1874 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), 1875 getTargetMachine(), RVLocs2, *DAG.getContext()); 1876 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC)); 1877 1878 if (RVLocs1.size() != RVLocs2.size()) 1879 return false; 1880 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) { 1881 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc()) 1882 return false; 1883 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo()) 1884 return false; 1885 if (RVLocs1[i].isRegLoc()) { 1886 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg()) 1887 return false; 1888 } else { 1889 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset()) 1890 return false; 1891 } 1892 } 1893 } 1894 1895 // Nothing more to check if the callee is taking no arguments 1896 if (Outs.empty()) 1897 return true; 1898 1899 SmallVector<CCValAssign, 16> ArgLocs; 1900 CCState CCInfo(CalleeCC, IsVarArg, DAG.getMachineFunction(), 1901 getTargetMachine(), ArgLocs, *DAG.getContext()); 1902 1903 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC)); 1904 1905 const AArch64MachineFunctionInfo *FuncInfo 1906 = MF.getInfo<AArch64MachineFunctionInfo>(); 1907 1908 // If the stack arguments for this call would fit into our own save area then 1909 // the call can be made tail. 1910 return CCInfo.getNextStackOffset() <= FuncInfo->getBytesInStackArgArea(); 1911 } 1912 1913 bool AArch64TargetLowering::DoesCalleeRestoreStack(CallingConv::ID CallCC, 1914 bool TailCallOpt) const { 1915 return CallCC == CallingConv::Fast && TailCallOpt; 1916 } 1917 1918 bool AArch64TargetLowering::IsTailCallConvention(CallingConv::ID CallCC) const { 1919 return CallCC == CallingConv::Fast; 1920 } 1921 1922 SDValue AArch64TargetLowering::addTokenForArgument(SDValue Chain, 1923 SelectionDAG &DAG, 1924 MachineFrameInfo *MFI, 1925 int ClobberedFI) const { 1926 SmallVector<SDValue, 8> ArgChains; 1927 int64_t FirstByte = MFI->getObjectOffset(ClobberedFI); 1928 int64_t LastByte = FirstByte + MFI->getObjectSize(ClobberedFI) - 1; 1929 1930 // Include the original chain at the beginning of the list. When this is 1931 // used by target LowerCall hooks, this helps legalize find the 1932 // CALLSEQ_BEGIN node. 1933 ArgChains.push_back(Chain); 1934 1935 // Add a chain value for each stack argument corresponding 1936 for (SDNode::use_iterator U = DAG.getEntryNode().getNode()->use_begin(), 1937 UE = DAG.getEntryNode().getNode()->use_end(); U != UE; ++U) 1938 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U)) 1939 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr())) 1940 if (FI->getIndex() < 0) { 1941 int64_t InFirstByte = MFI->getObjectOffset(FI->getIndex()); 1942 int64_t InLastByte = InFirstByte; 1943 InLastByte += MFI->getObjectSize(FI->getIndex()) - 1; 1944 1945 if ((InFirstByte <= FirstByte && FirstByte <= InLastByte) || 1946 (FirstByte <= InFirstByte && InFirstByte <= LastByte)) 1947 ArgChains.push_back(SDValue(L, 1)); 1948 } 1949 1950 // Build a tokenfactor for all the chains. 1951 return DAG.getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, 1952 &ArgChains[0], ArgChains.size()); 1953 } 1954 1955 static A64CC::CondCodes IntCCToA64CC(ISD::CondCode CC) { 1956 switch (CC) { 1957 case ISD::SETEQ: return A64CC::EQ; 1958 case ISD::SETGT: return A64CC::GT; 1959 case ISD::SETGE: return A64CC::GE; 1960 case ISD::SETLT: return A64CC::LT; 1961 case ISD::SETLE: return A64CC::LE; 1962 case ISD::SETNE: return A64CC::NE; 1963 case ISD::SETUGT: return A64CC::HI; 1964 case ISD::SETUGE: return A64CC::HS; 1965 case ISD::SETULT: return A64CC::LO; 1966 case ISD::SETULE: return A64CC::LS; 1967 default: llvm_unreachable("Unexpected condition code"); 1968 } 1969 } 1970 1971 bool AArch64TargetLowering::isLegalICmpImmediate(int64_t Val) const { 1972 // icmp is implemented using adds/subs immediate, which take an unsigned 1973 // 12-bit immediate, optionally shifted left by 12 bits. 1974 1975 // Symmetric by using adds/subs 1976 if (Val < 0) 1977 Val = -Val; 1978 1979 return (Val & ~0xfff) == 0 || (Val & ~0xfff000) == 0; 1980 } 1981 1982 SDValue AArch64TargetLowering::getSelectableIntSetCC(SDValue LHS, SDValue RHS, 1983 ISD::CondCode CC, SDValue &A64cc, 1984 SelectionDAG &DAG, SDLoc &dl) const { 1985 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) { 1986 int64_t C = 0; 1987 EVT VT = RHSC->getValueType(0); 1988 bool knownInvalid = false; 1989 1990 // I'm not convinced the rest of LLVM handles these edge cases properly, but 1991 // we can at least get it right. 1992 if (isSignedIntSetCC(CC)) { 1993 C = RHSC->getSExtValue(); 1994 } else if (RHSC->getZExtValue() > INT64_MAX) { 1995 // A 64-bit constant not representable by a signed 64-bit integer is far 1996 // too big to fit into a SUBS immediate anyway. 1997 knownInvalid = true; 1998 } else { 1999 C = RHSC->getZExtValue(); 2000 } 2001 2002 if (!knownInvalid && !isLegalICmpImmediate(C)) { 2003 // Constant does not fit, try adjusting it by one? 2004 switch (CC) { 2005 default: break; 2006 case ISD::SETLT: 2007 case ISD::SETGE: 2008 if (isLegalICmpImmediate(C-1)) { 2009 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT; 2010 RHS = DAG.getConstant(C-1, VT); 2011 } 2012 break; 2013 case ISD::SETULT: 2014 case ISD::SETUGE: 2015 if (isLegalICmpImmediate(C-1)) { 2016 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT; 2017 RHS = DAG.getConstant(C-1, VT); 2018 } 2019 break; 2020 case ISD::SETLE: 2021 case ISD::SETGT: 2022 if (isLegalICmpImmediate(C+1)) { 2023 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE; 2024 RHS = DAG.getConstant(C+1, VT); 2025 } 2026 break; 2027 case ISD::SETULE: 2028 case ISD::SETUGT: 2029 if (isLegalICmpImmediate(C+1)) { 2030 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE; 2031 RHS = DAG.getConstant(C+1, VT); 2032 } 2033 break; 2034 } 2035 } 2036 } 2037 2038 A64CC::CondCodes CondCode = IntCCToA64CC(CC); 2039 A64cc = DAG.getConstant(CondCode, MVT::i32); 2040 return DAG.getNode(AArch64ISD::SETCC, dl, MVT::i32, LHS, RHS, 2041 DAG.getCondCode(CC)); 2042 } 2043 2044 static A64CC::CondCodes FPCCToA64CC(ISD::CondCode CC, 2045 A64CC::CondCodes &Alternative) { 2046 A64CC::CondCodes CondCode = A64CC::Invalid; 2047 Alternative = A64CC::Invalid; 2048 2049 switch (CC) { 2050 default: llvm_unreachable("Unknown FP condition!"); 2051 case ISD::SETEQ: 2052 case ISD::SETOEQ: CondCode = A64CC::EQ; break; 2053 case ISD::SETGT: 2054 case ISD::SETOGT: CondCode = A64CC::GT; break; 2055 case ISD::SETGE: 2056 case ISD::SETOGE: CondCode = A64CC::GE; break; 2057 case ISD::SETOLT: CondCode = A64CC::MI; break; 2058 case ISD::SETOLE: CondCode = A64CC::LS; break; 2059 case ISD::SETONE: CondCode = A64CC::MI; Alternative = A64CC::GT; break; 2060 case ISD::SETO: CondCode = A64CC::VC; break; 2061 case ISD::SETUO: CondCode = A64CC::VS; break; 2062 case ISD::SETUEQ: CondCode = A64CC::EQ; Alternative = A64CC::VS; break; 2063 case ISD::SETUGT: CondCode = A64CC::HI; break; 2064 case ISD::SETUGE: CondCode = A64CC::PL; break; 2065 case ISD::SETLT: 2066 case ISD::SETULT: CondCode = A64CC::LT; break; 2067 case ISD::SETLE: 2068 case ISD::SETULE: CondCode = A64CC::LE; break; 2069 case ISD::SETNE: 2070 case ISD::SETUNE: CondCode = A64CC::NE; break; 2071 } 2072 return CondCode; 2073 } 2074 2075 SDValue 2076 AArch64TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const { 2077 SDLoc DL(Op); 2078 EVT PtrVT = getPointerTy(); 2079 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); 2080 2081 switch(getTargetMachine().getCodeModel()) { 2082 case CodeModel::Small: 2083 // The most efficient code is PC-relative anyway for the small memory model, 2084 // so we don't need to worry about relocation model. 2085 return DAG.getNode(AArch64ISD::WrapperSmall, DL, PtrVT, 2086 DAG.getTargetBlockAddress(BA, PtrVT, 0, 2087 AArch64II::MO_NO_FLAG), 2088 DAG.getTargetBlockAddress(BA, PtrVT, 0, 2089 AArch64II::MO_LO12), 2090 DAG.getConstant(/*Alignment=*/ 4, MVT::i32)); 2091 case CodeModel::Large: 2092 return DAG.getNode( 2093 AArch64ISD::WrapperLarge, DL, PtrVT, 2094 DAG.getTargetBlockAddress(BA, PtrVT, 0, AArch64II::MO_ABS_G3), 2095 DAG.getTargetBlockAddress(BA, PtrVT, 0, AArch64II::MO_ABS_G2_NC), 2096 DAG.getTargetBlockAddress(BA, PtrVT, 0, AArch64II::MO_ABS_G1_NC), 2097 DAG.getTargetBlockAddress(BA, PtrVT, 0, AArch64II::MO_ABS_G0_NC)); 2098 default: 2099 llvm_unreachable("Only small and large code models supported now"); 2100 } 2101 } 2102 2103 2104 // (BRCOND chain, val, dest) 2105 SDValue 2106 AArch64TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const { 2107 SDLoc dl(Op); 2108 SDValue Chain = Op.getOperand(0); 2109 SDValue TheBit = Op.getOperand(1); 2110 SDValue DestBB = Op.getOperand(2); 2111 2112 // AArch64 BooleanContents is the default UndefinedBooleanContent, which means 2113 // that as the consumer we are responsible for ignoring rubbish in higher 2114 // bits. 2115 TheBit = DAG.getNode(ISD::AND, dl, MVT::i32, TheBit, 2116 DAG.getConstant(1, MVT::i32)); 2117 2118 SDValue A64CMP = DAG.getNode(AArch64ISD::SETCC, dl, MVT::i32, TheBit, 2119 DAG.getConstant(0, TheBit.getValueType()), 2120 DAG.getCondCode(ISD::SETNE)); 2121 2122 return DAG.getNode(AArch64ISD::BR_CC, dl, MVT::Other, Chain, 2123 A64CMP, DAG.getConstant(A64CC::NE, MVT::i32), 2124 DestBB); 2125 } 2126 2127 // (BR_CC chain, condcode, lhs, rhs, dest) 2128 SDValue 2129 AArch64TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 2130 SDLoc dl(Op); 2131 SDValue Chain = Op.getOperand(0); 2132 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 2133 SDValue LHS = Op.getOperand(2); 2134 SDValue RHS = Op.getOperand(3); 2135 SDValue DestBB = Op.getOperand(4); 2136 2137 if (LHS.getValueType() == MVT::f128) { 2138 // f128 comparisons are lowered to runtime calls by a routine which sets 2139 // LHS, RHS and CC appropriately for the rest of this function to continue. 2140 softenSetCCOperands(DAG, MVT::f128, LHS, RHS, CC, dl); 2141 2142 // If softenSetCCOperands returned a scalar, we need to compare the result 2143 // against zero to select between true and false values. 2144 if (RHS.getNode() == 0) { 2145 RHS = DAG.getConstant(0, LHS.getValueType()); 2146 CC = ISD::SETNE; 2147 } 2148 } 2149 2150 if (LHS.getValueType().isInteger()) { 2151 SDValue A64cc; 2152 2153 // Integers are handled in a separate function because the combinations of 2154 // immediates and tests can get hairy and we may want to fiddle things. 2155 SDValue CmpOp = getSelectableIntSetCC(LHS, RHS, CC, A64cc, DAG, dl); 2156 2157 return DAG.getNode(AArch64ISD::BR_CC, dl, MVT::Other, 2158 Chain, CmpOp, A64cc, DestBB); 2159 } 2160 2161 // Note that some LLVM floating-point CondCodes can't be lowered to a single 2162 // conditional branch, hence FPCCToA64CC can set a second test, where either 2163 // passing is sufficient. 2164 A64CC::CondCodes CondCode, Alternative = A64CC::Invalid; 2165 CondCode = FPCCToA64CC(CC, Alternative); 2166 SDValue A64cc = DAG.getConstant(CondCode, MVT::i32); 2167 SDValue SetCC = DAG.getNode(AArch64ISD::SETCC, dl, MVT::i32, LHS, RHS, 2168 DAG.getCondCode(CC)); 2169 SDValue A64BR_CC = DAG.getNode(AArch64ISD::BR_CC, dl, MVT::Other, 2170 Chain, SetCC, A64cc, DestBB); 2171 2172 if (Alternative != A64CC::Invalid) { 2173 A64cc = DAG.getConstant(Alternative, MVT::i32); 2174 A64BR_CC = DAG.getNode(AArch64ISD::BR_CC, dl, MVT::Other, 2175 A64BR_CC, SetCC, A64cc, DestBB); 2176 2177 } 2178 2179 return A64BR_CC; 2180 } 2181 2182 SDValue 2183 AArch64TargetLowering::LowerF128ToCall(SDValue Op, SelectionDAG &DAG, 2184 RTLIB::Libcall Call) const { 2185 ArgListTy Args; 2186 ArgListEntry Entry; 2187 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) { 2188 EVT ArgVT = Op.getOperand(i).getValueType(); 2189 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 2190 Entry.Node = Op.getOperand(i); Entry.Ty = ArgTy; 2191 Entry.isSExt = false; 2192 Entry.isZExt = false; 2193 Args.push_back(Entry); 2194 } 2195 SDValue Callee = DAG.getExternalSymbol(getLibcallName(Call), getPointerTy()); 2196 2197 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext()); 2198 2199 // By default, the input chain to this libcall is the entry node of the 2200 // function. If the libcall is going to be emitted as a tail call then 2201 // isUsedByReturnOnly will change it to the right chain if the return 2202 // node which is being folded has a non-entry input chain. 2203 SDValue InChain = DAG.getEntryNode(); 2204 2205 // isTailCall may be true since the callee does not reference caller stack 2206 // frame. Check if it's in the right position. 2207 SDValue TCChain = InChain; 2208 bool isTailCall = isInTailCallPosition(DAG, Op.getNode(), TCChain); 2209 if (isTailCall) 2210 InChain = TCChain; 2211 2212 TargetLowering:: 2213 CallLoweringInfo CLI(InChain, RetTy, false, false, false, false, 2214 0, getLibcallCallingConv(Call), isTailCall, 2215 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true, 2216 Callee, Args, DAG, SDLoc(Op)); 2217 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI); 2218 2219 if (!CallInfo.second.getNode()) 2220 // It's a tailcall, return the chain (which is the DAG root). 2221 return DAG.getRoot(); 2222 2223 return CallInfo.first; 2224 } 2225 2226 SDValue 2227 AArch64TargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const { 2228 if (Op.getOperand(0).getValueType() != MVT::f128) { 2229 // It's legal except when f128 is involved 2230 return Op; 2231 } 2232 2233 RTLIB::Libcall LC; 2234 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType()); 2235 2236 SDValue SrcVal = Op.getOperand(0); 2237 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1, 2238 /*isSigned*/ false, SDLoc(Op)).first; 2239 } 2240 2241 SDValue 2242 AArch64TargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const { 2243 assert(Op.getValueType() == MVT::f128 && "Unexpected lowering"); 2244 2245 RTLIB::Libcall LC; 2246 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType()); 2247 2248 return LowerF128ToCall(Op, DAG, LC); 2249 } 2250 2251 static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG, 2252 bool IsSigned) { 2253 SDLoc dl(Op); 2254 EVT VT = Op.getValueType(); 2255 SDValue Vec = Op.getOperand(0); 2256 EVT OpVT = Vec.getValueType(); 2257 unsigned Opc = IsSigned ? ISD::FP_TO_SINT : ISD::FP_TO_UINT; 2258 2259 if (VT.getVectorNumElements() == 1) { 2260 assert(OpVT == MVT::v1f64 && "Unexpected vector type!"); 2261 if (VT.getSizeInBits() == OpVT.getSizeInBits()) 2262 return Op; 2263 return DAG.UnrollVectorOp(Op.getNode()); 2264 } 2265 2266 if (VT.getSizeInBits() > OpVT.getSizeInBits()) { 2267 assert(Vec.getValueType() == MVT::v2f32 && VT == MVT::v2i64 && 2268 "Unexpected vector type!"); 2269 Vec = DAG.getNode(ISD::FP_EXTEND, dl, MVT::v2f64, Vec); 2270 return DAG.getNode(Opc, dl, VT, Vec); 2271 } else if (VT.getSizeInBits() < OpVT.getSizeInBits()) { 2272 EVT CastVT = EVT::getIntegerVT(*DAG.getContext(), 2273 OpVT.getVectorElementType().getSizeInBits()); 2274 CastVT = 2275 EVT::getVectorVT(*DAG.getContext(), CastVT, VT.getVectorNumElements()); 2276 Vec = DAG.getNode(Opc, dl, CastVT, Vec); 2277 return DAG.getNode(ISD::TRUNCATE, dl, VT, Vec); 2278 } 2279 return DAG.getNode(Opc, dl, VT, Vec); 2280 } 2281 2282 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) { 2283 // We custom lower concat_vectors with 4, 8, or 16 operands that are all the 2284 // same operand and of type v1* using the DUP instruction. 2285 unsigned NumOps = Op->getNumOperands(); 2286 if (NumOps != 4 && NumOps != 8 && NumOps != 16) 2287 return Op; 2288 2289 // Must be a single value for VDUP. 2290 bool isConstant = true; 2291 SDValue Op0 = Op.getOperand(0); 2292 for (unsigned i = 1; i < NumOps; ++i) { 2293 SDValue OpN = Op.getOperand(i); 2294 if (Op0 != OpN) 2295 return Op; 2296 2297 if (!isa<ConstantSDNode>(OpN->getOperand(0))) 2298 isConstant = false; 2299 } 2300 2301 // Verify the value type. 2302 EVT EltVT = Op0.getValueType(); 2303 switch (NumOps) { 2304 default: llvm_unreachable("Unexpected number of operands"); 2305 case 4: 2306 if (EltVT != MVT::v1i16 && EltVT != MVT::v1i32) 2307 return Op; 2308 break; 2309 case 8: 2310 if (EltVT != MVT::v1i8 && EltVT != MVT::v1i16) 2311 return Op; 2312 break; 2313 case 16: 2314 if (EltVT != MVT::v1i8) 2315 return Op; 2316 break; 2317 } 2318 2319 SDLoc DL(Op); 2320 EVT VT = Op.getValueType(); 2321 // VDUP produces better code for constants. 2322 if (isConstant) 2323 return DAG.getNode(AArch64ISD::NEON_VDUP, DL, VT, Op0->getOperand(0)); 2324 return DAG.getNode(AArch64ISD::NEON_VDUPLANE, DL, VT, Op0, 2325 DAG.getConstant(0, MVT::i64)); 2326 } 2327 2328 SDValue 2329 AArch64TargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG, 2330 bool IsSigned) const { 2331 if (Op.getValueType().isVector()) 2332 return LowerVectorFP_TO_INT(Op, DAG, IsSigned); 2333 if (Op.getOperand(0).getValueType() != MVT::f128) { 2334 // It's legal except when f128 is involved 2335 return Op; 2336 } 2337 2338 RTLIB::Libcall LC; 2339 if (IsSigned) 2340 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(), Op.getValueType()); 2341 else 2342 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(), Op.getValueType()); 2343 2344 return LowerF128ToCall(Op, DAG, LC); 2345 } 2346 2347 SDValue AArch64TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{ 2348 MachineFunction &MF = DAG.getMachineFunction(); 2349 MachineFrameInfo *MFI = MF.getFrameInfo(); 2350 MFI->setReturnAddressIsTaken(true); 2351 2352 if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 2353 return SDValue(); 2354 2355 EVT VT = Op.getValueType(); 2356 SDLoc dl(Op); 2357 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 2358 if (Depth) { 2359 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 2360 SDValue Offset = DAG.getConstant(8, MVT::i64); 2361 return DAG.getLoad(VT, dl, DAG.getEntryNode(), 2362 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset), 2363 MachinePointerInfo(), false, false, false, 0); 2364 } 2365 2366 // Return X30, which contains the return address. Mark it an implicit live-in. 2367 unsigned Reg = MF.addLiveIn(AArch64::X30, getRegClassFor(MVT::i64)); 2368 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, MVT::i64); 2369 } 2370 2371 2372 SDValue AArch64TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) 2373 const { 2374 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 2375 MFI->setFrameAddressIsTaken(true); 2376 2377 EVT VT = Op.getValueType(); 2378 SDLoc dl(Op); 2379 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 2380 unsigned FrameReg = AArch64::X29; 2381 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); 2382 while (Depth--) 2383 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, 2384 MachinePointerInfo(), 2385 false, false, false, 0); 2386 return FrameAddr; 2387 } 2388 2389 SDValue 2390 AArch64TargetLowering::LowerGlobalAddressELFLarge(SDValue Op, 2391 SelectionDAG &DAG) const { 2392 assert(getTargetMachine().getCodeModel() == CodeModel::Large); 2393 assert(getTargetMachine().getRelocationModel() == Reloc::Static); 2394 2395 EVT PtrVT = getPointerTy(); 2396 SDLoc dl(Op); 2397 const GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Op); 2398 const GlobalValue *GV = GN->getGlobal(); 2399 2400 SDValue GlobalAddr = DAG.getNode( 2401 AArch64ISD::WrapperLarge, dl, PtrVT, 2402 DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, AArch64II::MO_ABS_G3), 2403 DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, AArch64II::MO_ABS_G2_NC), 2404 DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, AArch64II::MO_ABS_G1_NC), 2405 DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, AArch64II::MO_ABS_G0_NC)); 2406 2407 if (GN->getOffset() != 0) 2408 return DAG.getNode(ISD::ADD, dl, PtrVT, GlobalAddr, 2409 DAG.getConstant(GN->getOffset(), PtrVT)); 2410 2411 return GlobalAddr; 2412 } 2413 2414 SDValue 2415 AArch64TargetLowering::LowerGlobalAddressELFSmall(SDValue Op, 2416 SelectionDAG &DAG) const { 2417 assert(getTargetMachine().getCodeModel() == CodeModel::Small); 2418 2419 EVT PtrVT = getPointerTy(); 2420 SDLoc dl(Op); 2421 const GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Op); 2422 const GlobalValue *GV = GN->getGlobal(); 2423 unsigned Alignment = GV->getAlignment(); 2424 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2425 if (GV->isWeakForLinker() && GV->isDeclaration() && RelocM == Reloc::Static) { 2426 // Weak undefined symbols can't use ADRP/ADD pair since they should evaluate 2427 // to zero when they remain undefined. In PIC mode the GOT can take care of 2428 // this, but in absolute mode we use a constant pool load. 2429 SDValue PoolAddr; 2430 PoolAddr = DAG.getNode(AArch64ISD::WrapperSmall, dl, PtrVT, 2431 DAG.getTargetConstantPool(GV, PtrVT, 0, 0, 2432 AArch64II::MO_NO_FLAG), 2433 DAG.getTargetConstantPool(GV, PtrVT, 0, 0, 2434 AArch64II::MO_LO12), 2435 DAG.getConstant(8, MVT::i32)); 2436 SDValue GlobalAddr = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), PoolAddr, 2437 MachinePointerInfo::getConstantPool(), 2438 /*isVolatile=*/ false, 2439 /*isNonTemporal=*/ true, 2440 /*isInvariant=*/ true, 8); 2441 if (GN->getOffset() != 0) 2442 return DAG.getNode(ISD::ADD, dl, PtrVT, GlobalAddr, 2443 DAG.getConstant(GN->getOffset(), PtrVT)); 2444 2445 return GlobalAddr; 2446 } 2447 2448 if (Alignment == 0) { 2449 const PointerType *GVPtrTy = cast<PointerType>(GV->getType()); 2450 if (GVPtrTy->getElementType()->isSized()) { 2451 Alignment 2452 = getDataLayout()->getABITypeAlignment(GVPtrTy->getElementType()); 2453 } else { 2454 // Be conservative if we can't guess, not that it really matters: 2455 // functions and labels aren't valid for loads, and the methods used to 2456 // actually calculate an address work with any alignment. 2457 Alignment = 1; 2458 } 2459 } 2460 2461 unsigned char HiFixup, LoFixup; 2462 bool UseGOT = getSubtarget()->GVIsIndirectSymbol(GV, RelocM); 2463 2464 if (UseGOT) { 2465 HiFixup = AArch64II::MO_GOT; 2466 LoFixup = AArch64II::MO_GOT_LO12; 2467 Alignment = 8; 2468 } else { 2469 HiFixup = AArch64II::MO_NO_FLAG; 2470 LoFixup = AArch64II::MO_LO12; 2471 } 2472 2473 // AArch64's small model demands the following sequence: 2474 // ADRP x0, somewhere 2475 // ADD x0, x0, #:lo12:somewhere ; (or LDR directly). 2476 SDValue GlobalRef = DAG.getNode(AArch64ISD::WrapperSmall, dl, PtrVT, 2477 DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 2478 HiFixup), 2479 DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 2480 LoFixup), 2481 DAG.getConstant(Alignment, MVT::i32)); 2482 2483 if (UseGOT) { 2484 GlobalRef = DAG.getNode(AArch64ISD::GOTLoad, dl, PtrVT, DAG.getEntryNode(), 2485 GlobalRef); 2486 } 2487 2488 if (GN->getOffset() != 0) 2489 return DAG.getNode(ISD::ADD, dl, PtrVT, GlobalRef, 2490 DAG.getConstant(GN->getOffset(), PtrVT)); 2491 2492 return GlobalRef; 2493 } 2494 2495 SDValue 2496 AArch64TargetLowering::LowerGlobalAddressELF(SDValue Op, 2497 SelectionDAG &DAG) const { 2498 // TableGen doesn't have easy access to the CodeModel or RelocationModel, so 2499 // we make those distinctions here. 2500 2501 switch (getTargetMachine().getCodeModel()) { 2502 case CodeModel::Small: 2503 return LowerGlobalAddressELFSmall(Op, DAG); 2504 case CodeModel::Large: 2505 return LowerGlobalAddressELFLarge(Op, DAG); 2506 default: 2507 llvm_unreachable("Only small and large code models supported now"); 2508 } 2509 } 2510 2511 SDValue 2512 AArch64TargetLowering::LowerConstantPool(SDValue Op, 2513 SelectionDAG &DAG) const { 2514 SDLoc DL(Op); 2515 EVT PtrVT = getPointerTy(); 2516 ConstantPoolSDNode *CN = cast<ConstantPoolSDNode>(Op); 2517 const Constant *C = CN->getConstVal(); 2518 2519 switch(getTargetMachine().getCodeModel()) { 2520 case CodeModel::Small: 2521 // The most efficient code is PC-relative anyway for the small memory model, 2522 // so we don't need to worry about relocation model. 2523 return DAG.getNode(AArch64ISD::WrapperSmall, DL, PtrVT, 2524 DAG.getTargetConstantPool(C, PtrVT, 0, 0, 2525 AArch64II::MO_NO_FLAG), 2526 DAG.getTargetConstantPool(C, PtrVT, 0, 0, 2527 AArch64II::MO_LO12), 2528 DAG.getConstant(CN->getAlignment(), MVT::i32)); 2529 case CodeModel::Large: 2530 return DAG.getNode( 2531 AArch64ISD::WrapperLarge, DL, PtrVT, 2532 DAG.getTargetConstantPool(C, PtrVT, 0, 0, AArch64II::MO_ABS_G3), 2533 DAG.getTargetConstantPool(C, PtrVT, 0, 0, AArch64II::MO_ABS_G2_NC), 2534 DAG.getTargetConstantPool(C, PtrVT, 0, 0, AArch64II::MO_ABS_G1_NC), 2535 DAG.getTargetConstantPool(C, PtrVT, 0, 0, AArch64II::MO_ABS_G0_NC)); 2536 default: 2537 llvm_unreachable("Only small and large code models supported now"); 2538 } 2539 } 2540 2541 SDValue AArch64TargetLowering::LowerTLSDescCall(SDValue SymAddr, 2542 SDValue DescAddr, 2543 SDLoc DL, 2544 SelectionDAG &DAG) const { 2545 EVT PtrVT = getPointerTy(); 2546 2547 // The function we need to call is simply the first entry in the GOT for this 2548 // descriptor, load it in preparation. 2549 SDValue Func, Chain; 2550 Func = DAG.getNode(AArch64ISD::GOTLoad, DL, PtrVT, DAG.getEntryNode(), 2551 DescAddr); 2552 2553 // The function takes only one argument: the address of the descriptor itself 2554 // in X0. 2555 SDValue Glue; 2556 Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, AArch64::X0, DescAddr, Glue); 2557 Glue = Chain.getValue(1); 2558 2559 // Finally, there's a special calling-convention which means that the lookup 2560 // must preserve all registers (except X0, obviously). 2561 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); 2562 const AArch64RegisterInfo *A64RI 2563 = static_cast<const AArch64RegisterInfo *>(TRI); 2564 const uint32_t *Mask = A64RI->getTLSDescCallPreservedMask(); 2565 2566 // We're now ready to populate the argument list, as with a normal call: 2567 std::vector<SDValue> Ops; 2568 Ops.push_back(Chain); 2569 Ops.push_back(Func); 2570 Ops.push_back(SymAddr); 2571 Ops.push_back(DAG.getRegister(AArch64::X0, PtrVT)); 2572 Ops.push_back(DAG.getRegisterMask(Mask)); 2573 Ops.push_back(Glue); 2574 2575 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 2576 Chain = DAG.getNode(AArch64ISD::TLSDESCCALL, DL, NodeTys, &Ops[0], 2577 Ops.size()); 2578 Glue = Chain.getValue(1); 2579 2580 // After the call, the offset from TPIDR_EL0 is in X0, copy it out and pass it 2581 // back to the generic handling code. 2582 return DAG.getCopyFromReg(Chain, DL, AArch64::X0, PtrVT, Glue); 2583 } 2584 2585 SDValue 2586 AArch64TargetLowering::LowerGlobalTLSAddress(SDValue Op, 2587 SelectionDAG &DAG) const { 2588 assert(getSubtarget()->isTargetELF() && 2589 "TLS not implemented for non-ELF targets"); 2590 assert(getTargetMachine().getCodeModel() == CodeModel::Small 2591 && "TLS only supported in small memory model"); 2592 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 2593 2594 TLSModel::Model Model = getTargetMachine().getTLSModel(GA->getGlobal()); 2595 2596 SDValue TPOff; 2597 EVT PtrVT = getPointerTy(); 2598 SDLoc DL(Op); 2599 const GlobalValue *GV = GA->getGlobal(); 2600 2601 SDValue ThreadBase = DAG.getNode(AArch64ISD::THREAD_POINTER, DL, PtrVT); 2602 2603 if (Model == TLSModel::InitialExec) { 2604 TPOff = DAG.getNode(AArch64ISD::WrapperSmall, DL, PtrVT, 2605 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 2606 AArch64II::MO_GOTTPREL), 2607 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 2608 AArch64II::MO_GOTTPREL_LO12), 2609 DAG.getConstant(8, MVT::i32)); 2610 TPOff = DAG.getNode(AArch64ISD::GOTLoad, DL, PtrVT, DAG.getEntryNode(), 2611 TPOff); 2612 } else if (Model == TLSModel::LocalExec) { 2613 SDValue HiVar = DAG.getTargetGlobalAddress(GV, DL, MVT::i64, 0, 2614 AArch64II::MO_TPREL_G1); 2615 SDValue LoVar = DAG.getTargetGlobalAddress(GV, DL, MVT::i64, 0, 2616 AArch64II::MO_TPREL_G0_NC); 2617 2618 TPOff = SDValue(DAG.getMachineNode(AArch64::MOVZxii, DL, PtrVT, HiVar, 2619 DAG.getTargetConstant(1, MVT::i32)), 0); 2620 TPOff = SDValue(DAG.getMachineNode(AArch64::MOVKxii, DL, PtrVT, 2621 TPOff, LoVar, 2622 DAG.getTargetConstant(0, MVT::i32)), 0); 2623 } else if (Model == TLSModel::GeneralDynamic) { 2624 // Accesses used in this sequence go via the TLS descriptor which lives in 2625 // the GOT. Prepare an address we can use to handle this. 2626 SDValue HiDesc = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 2627 AArch64II::MO_TLSDESC); 2628 SDValue LoDesc = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 2629 AArch64II::MO_TLSDESC_LO12); 2630 SDValue DescAddr = DAG.getNode(AArch64ISD::WrapperSmall, DL, PtrVT, 2631 HiDesc, LoDesc, 2632 DAG.getConstant(8, MVT::i32)); 2633 SDValue SymAddr = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0); 2634 2635 TPOff = LowerTLSDescCall(SymAddr, DescAddr, DL, DAG); 2636 } else if (Model == TLSModel::LocalDynamic) { 2637 // Local-dynamic accesses proceed in two phases. A general-dynamic TLS 2638 // descriptor call against the special symbol _TLS_MODULE_BASE_ to calculate 2639 // the beginning of the module's TLS region, followed by a DTPREL offset 2640 // calculation. 2641 2642 // These accesses will need deduplicating if there's more than one. 2643 AArch64MachineFunctionInfo* MFI = DAG.getMachineFunction() 2644 .getInfo<AArch64MachineFunctionInfo>(); 2645 MFI->incNumLocalDynamicTLSAccesses(); 2646 2647 2648 // Get the location of _TLS_MODULE_BASE_: 2649 SDValue HiDesc = DAG.getTargetExternalSymbol("_TLS_MODULE_BASE_", PtrVT, 2650 AArch64II::MO_TLSDESC); 2651 SDValue LoDesc = DAG.getTargetExternalSymbol("_TLS_MODULE_BASE_", PtrVT, 2652 AArch64II::MO_TLSDESC_LO12); 2653 SDValue DescAddr = DAG.getNode(AArch64ISD::WrapperSmall, DL, PtrVT, 2654 HiDesc, LoDesc, 2655 DAG.getConstant(8, MVT::i32)); 2656 SDValue SymAddr = DAG.getTargetExternalSymbol("_TLS_MODULE_BASE_", PtrVT); 2657 2658 ThreadBase = LowerTLSDescCall(SymAddr, DescAddr, DL, DAG); 2659 2660 // Get the variable's offset from _TLS_MODULE_BASE_ 2661 SDValue HiVar = DAG.getTargetGlobalAddress(GV, DL, MVT::i64, 0, 2662 AArch64II::MO_DTPREL_G1); 2663 SDValue LoVar = DAG.getTargetGlobalAddress(GV, DL, MVT::i64, 0, 2664 AArch64II::MO_DTPREL_G0_NC); 2665 2666 TPOff = SDValue(DAG.getMachineNode(AArch64::MOVZxii, DL, PtrVT, HiVar, 2667 DAG.getTargetConstant(0, MVT::i32)), 0); 2668 TPOff = SDValue(DAG.getMachineNode(AArch64::MOVKxii, DL, PtrVT, 2669 TPOff, LoVar, 2670 DAG.getTargetConstant(0, MVT::i32)), 0); 2671 } else 2672 llvm_unreachable("Unsupported TLS access model"); 2673 2674 2675 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadBase, TPOff); 2676 } 2677 2678 static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG, 2679 bool IsSigned) { 2680 SDLoc dl(Op); 2681 EVT VT = Op.getValueType(); 2682 SDValue Vec = Op.getOperand(0); 2683 unsigned Opc = IsSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP; 2684 2685 if (VT.getVectorNumElements() == 1) { 2686 assert(VT == MVT::v1f64 && "Unexpected vector type!"); 2687 if (VT.getSizeInBits() == Vec.getValueSizeInBits()) 2688 return Op; 2689 return DAG.UnrollVectorOp(Op.getNode()); 2690 } 2691 2692 if (VT.getSizeInBits() < Vec.getValueSizeInBits()) { 2693 assert(Vec.getValueType() == MVT::v2i64 && VT == MVT::v2f32 && 2694 "Unexpected vector type!"); 2695 Vec = DAG.getNode(Opc, dl, MVT::v2f64, Vec); 2696 return DAG.getNode(ISD::FP_ROUND, dl, VT, Vec, DAG.getIntPtrConstant(0)); 2697 } else if (VT.getSizeInBits() > Vec.getValueSizeInBits()) { 2698 unsigned CastOpc = IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 2699 EVT CastVT = EVT::getIntegerVT(*DAG.getContext(), 2700 VT.getVectorElementType().getSizeInBits()); 2701 CastVT = 2702 EVT::getVectorVT(*DAG.getContext(), CastVT, VT.getVectorNumElements()); 2703 Vec = DAG.getNode(CastOpc, dl, CastVT, Vec); 2704 } 2705 2706 return DAG.getNode(Opc, dl, VT, Vec); 2707 } 2708 2709 SDValue 2710 AArch64TargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG, 2711 bool IsSigned) const { 2712 if (Op.getValueType().isVector()) 2713 return LowerVectorINT_TO_FP(Op, DAG, IsSigned); 2714 if (Op.getValueType() != MVT::f128) { 2715 // Legal for everything except f128. 2716 return Op; 2717 } 2718 2719 RTLIB::Libcall LC; 2720 if (IsSigned) 2721 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(), Op.getValueType()); 2722 else 2723 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(), Op.getValueType()); 2724 2725 return LowerF128ToCall(Op, DAG, LC); 2726 } 2727 2728 2729 SDValue 2730 AArch64TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const { 2731 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); 2732 SDLoc dl(JT); 2733 EVT PtrVT = getPointerTy(); 2734 2735 // When compiling PIC, jump tables get put in the code section so a static 2736 // relocation-style is acceptable for both cases. 2737 switch (getTargetMachine().getCodeModel()) { 2738 case CodeModel::Small: 2739 return DAG.getNode(AArch64ISD::WrapperSmall, dl, PtrVT, 2740 DAG.getTargetJumpTable(JT->getIndex(), PtrVT), 2741 DAG.getTargetJumpTable(JT->getIndex(), PtrVT, 2742 AArch64II::MO_LO12), 2743 DAG.getConstant(1, MVT::i32)); 2744 case CodeModel::Large: 2745 return DAG.getNode( 2746 AArch64ISD::WrapperLarge, dl, PtrVT, 2747 DAG.getTargetJumpTable(JT->getIndex(), PtrVT, AArch64II::MO_ABS_G3), 2748 DAG.getTargetJumpTable(JT->getIndex(), PtrVT, AArch64II::MO_ABS_G2_NC), 2749 DAG.getTargetJumpTable(JT->getIndex(), PtrVT, AArch64II::MO_ABS_G1_NC), 2750 DAG.getTargetJumpTable(JT->getIndex(), PtrVT, AArch64II::MO_ABS_G0_NC)); 2751 default: 2752 llvm_unreachable("Only small and large code models supported now"); 2753 } 2754 } 2755 2756 // (SELECT testbit, iftrue, iffalse) 2757 SDValue 2758 AArch64TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 2759 SDLoc dl(Op); 2760 SDValue TheBit = Op.getOperand(0); 2761 SDValue IfTrue = Op.getOperand(1); 2762 SDValue IfFalse = Op.getOperand(2); 2763 2764 // AArch64 BooleanContents is the default UndefinedBooleanContent, which means 2765 // that as the consumer we are responsible for ignoring rubbish in higher 2766 // bits. 2767 TheBit = DAG.getNode(ISD::AND, dl, MVT::i32, TheBit, 2768 DAG.getConstant(1, MVT::i32)); 2769 SDValue A64CMP = DAG.getNode(AArch64ISD::SETCC, dl, MVT::i32, TheBit, 2770 DAG.getConstant(0, TheBit.getValueType()), 2771 DAG.getCondCode(ISD::SETNE)); 2772 2773 return DAG.getNode(AArch64ISD::SELECT_CC, dl, Op.getValueType(), 2774 A64CMP, IfTrue, IfFalse, 2775 DAG.getConstant(A64CC::NE, MVT::i32)); 2776 } 2777 2778 static SDValue LowerVectorSETCC(SDValue Op, SelectionDAG &DAG) { 2779 SDLoc DL(Op); 2780 SDValue LHS = Op.getOperand(0); 2781 SDValue RHS = Op.getOperand(1); 2782 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 2783 EVT VT = Op.getValueType(); 2784 bool Invert = false; 2785 SDValue Op0, Op1; 2786 unsigned Opcode; 2787 2788 if (LHS.getValueType().isInteger()) { 2789 2790 // Attempt to use Vector Integer Compare Mask Test instruction. 2791 // TST = icmp ne (and (op0, op1), zero). 2792 if (CC == ISD::SETNE) { 2793 if (((LHS.getOpcode() == ISD::AND) && 2794 ISD::isBuildVectorAllZeros(RHS.getNode())) || 2795 ((RHS.getOpcode() == ISD::AND) && 2796 ISD::isBuildVectorAllZeros(LHS.getNode()))) { 2797 2798 SDValue AndOp = (LHS.getOpcode() == ISD::AND) ? LHS : RHS; 2799 SDValue NewLHS = DAG.getNode(ISD::BITCAST, DL, VT, AndOp.getOperand(0)); 2800 SDValue NewRHS = DAG.getNode(ISD::BITCAST, DL, VT, AndOp.getOperand(1)); 2801 return DAG.getNode(AArch64ISD::NEON_TST, DL, VT, NewLHS, NewRHS); 2802 } 2803 } 2804 2805 // Attempt to use Vector Integer Compare Mask against Zero instr (Signed). 2806 // Note: Compare against Zero does not support unsigned predicates. 2807 if ((ISD::isBuildVectorAllZeros(RHS.getNode()) || 2808 ISD::isBuildVectorAllZeros(LHS.getNode())) && 2809 !isUnsignedIntSetCC(CC)) { 2810 2811 // If LHS is the zero value, swap operands and CondCode. 2812 if (ISD::isBuildVectorAllZeros(LHS.getNode())) { 2813 CC = getSetCCSwappedOperands(CC); 2814 Op0 = RHS; 2815 } else 2816 Op0 = LHS; 2817 2818 // Ensure valid CondCode for Compare Mask against Zero instruction: 2819 // EQ, GE, GT, LE, LT. 2820 if (ISD::SETNE == CC) { 2821 Invert = true; 2822 CC = ISD::SETEQ; 2823 } 2824 2825 // Using constant type to differentiate integer and FP compares with zero. 2826 Op1 = DAG.getConstant(0, MVT::i32); 2827 Opcode = AArch64ISD::NEON_CMPZ; 2828 2829 } else { 2830 // Attempt to use Vector Integer Compare Mask instr (Signed/Unsigned). 2831 // Ensure valid CondCode for Compare Mask instr: EQ, GE, GT, UGE, UGT. 2832 bool Swap = false; 2833 switch (CC) { 2834 default: 2835 llvm_unreachable("Illegal integer comparison."); 2836 case ISD::SETEQ: 2837 case ISD::SETGT: 2838 case ISD::SETGE: 2839 case ISD::SETUGT: 2840 case ISD::SETUGE: 2841 break; 2842 case ISD::SETNE: 2843 Invert = true; 2844 CC = ISD::SETEQ; 2845 break; 2846 case ISD::SETULT: 2847 case ISD::SETULE: 2848 case ISD::SETLT: 2849 case ISD::SETLE: 2850 Swap = true; 2851 CC = getSetCCSwappedOperands(CC); 2852 } 2853 2854 if (Swap) 2855 std::swap(LHS, RHS); 2856 2857 Opcode = AArch64ISD::NEON_CMP; 2858 Op0 = LHS; 2859 Op1 = RHS; 2860 } 2861 2862 // Generate Compare Mask instr or Compare Mask against Zero instr. 2863 SDValue NeonCmp = 2864 DAG.getNode(Opcode, DL, VT, Op0, Op1, DAG.getCondCode(CC)); 2865 2866 if (Invert) 2867 NeonCmp = DAG.getNOT(DL, NeonCmp, VT); 2868 2869 return NeonCmp; 2870 } 2871 2872 // Now handle Floating Point cases. 2873 // Attempt to use Vector Floating Point Compare Mask against Zero instruction. 2874 if (ISD::isBuildVectorAllZeros(RHS.getNode()) || 2875 ISD::isBuildVectorAllZeros(LHS.getNode())) { 2876 2877 // If LHS is the zero value, swap operands and CondCode. 2878 if (ISD::isBuildVectorAllZeros(LHS.getNode())) { 2879 CC = getSetCCSwappedOperands(CC); 2880 Op0 = RHS; 2881 } else 2882 Op0 = LHS; 2883 2884 // Using constant type to differentiate integer and FP compares with zero. 2885 Op1 = DAG.getConstantFP(0, MVT::f32); 2886 Opcode = AArch64ISD::NEON_CMPZ; 2887 } else { 2888 // Attempt to use Vector Floating Point Compare Mask instruction. 2889 Op0 = LHS; 2890 Op1 = RHS; 2891 Opcode = AArch64ISD::NEON_CMP; 2892 } 2893 2894 SDValue NeonCmpAlt; 2895 // Some register compares have to be implemented with swapped CC and operands, 2896 // e.g.: OLT implemented as OGT with swapped operands. 2897 bool SwapIfRegArgs = false; 2898 2899 // Ensure valid CondCode for FP Compare Mask against Zero instruction: 2900 // EQ, GE, GT, LE, LT. 2901 // And ensure valid CondCode for FP Compare Mask instruction: EQ, GE, GT. 2902 switch (CC) { 2903 default: 2904 llvm_unreachable("Illegal FP comparison"); 2905 case ISD::SETUNE: 2906 case ISD::SETNE: 2907 Invert = true; // Fallthrough 2908 case ISD::SETOEQ: 2909 case ISD::SETEQ: 2910 CC = ISD::SETEQ; 2911 break; 2912 case ISD::SETOLT: 2913 case ISD::SETLT: 2914 CC = ISD::SETLT; 2915 SwapIfRegArgs = true; 2916 break; 2917 case ISD::SETOGT: 2918 case ISD::SETGT: 2919 CC = ISD::SETGT; 2920 break; 2921 case ISD::SETOLE: 2922 case ISD::SETLE: 2923 CC = ISD::SETLE; 2924 SwapIfRegArgs = true; 2925 break; 2926 case ISD::SETOGE: 2927 case ISD::SETGE: 2928 CC = ISD::SETGE; 2929 break; 2930 case ISD::SETUGE: 2931 Invert = true; 2932 CC = ISD::SETLT; 2933 SwapIfRegArgs = true; 2934 break; 2935 case ISD::SETULE: 2936 Invert = true; 2937 CC = ISD::SETGT; 2938 break; 2939 case ISD::SETUGT: 2940 Invert = true; 2941 CC = ISD::SETLE; 2942 SwapIfRegArgs = true; 2943 break; 2944 case ISD::SETULT: 2945 Invert = true; 2946 CC = ISD::SETGE; 2947 break; 2948 case ISD::SETUEQ: 2949 Invert = true; // Fallthrough 2950 case ISD::SETONE: 2951 // Expand this to (OGT |OLT). 2952 NeonCmpAlt = 2953 DAG.getNode(Opcode, DL, VT, Op0, Op1, DAG.getCondCode(ISD::SETGT)); 2954 CC = ISD::SETLT; 2955 SwapIfRegArgs = true; 2956 break; 2957 case ISD::SETUO: 2958 Invert = true; // Fallthrough 2959 case ISD::SETO: 2960 // Expand this to (OGE | OLT). 2961 NeonCmpAlt = 2962 DAG.getNode(Opcode, DL, VT, Op0, Op1, DAG.getCondCode(ISD::SETGE)); 2963 CC = ISD::SETLT; 2964 SwapIfRegArgs = true; 2965 break; 2966 } 2967 2968 if (Opcode == AArch64ISD::NEON_CMP && SwapIfRegArgs) { 2969 CC = getSetCCSwappedOperands(CC); 2970 std::swap(Op0, Op1); 2971 } 2972 2973 // Generate FP Compare Mask instr or FP Compare Mask against Zero instr 2974 SDValue NeonCmp = DAG.getNode(Opcode, DL, VT, Op0, Op1, DAG.getCondCode(CC)); 2975 2976 if (NeonCmpAlt.getNode()) 2977 NeonCmp = DAG.getNode(ISD::OR, DL, VT, NeonCmp, NeonCmpAlt); 2978 2979 if (Invert) 2980 NeonCmp = DAG.getNOT(DL, NeonCmp, VT); 2981 2982 return NeonCmp; 2983 } 2984 2985 // (SETCC lhs, rhs, condcode) 2986 SDValue 2987 AArch64TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const { 2988 SDLoc dl(Op); 2989 SDValue LHS = Op.getOperand(0); 2990 SDValue RHS = Op.getOperand(1); 2991 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 2992 EVT VT = Op.getValueType(); 2993 2994 if (VT.isVector()) 2995 return LowerVectorSETCC(Op, DAG); 2996 2997 if (LHS.getValueType() == MVT::f128) { 2998 // f128 comparisons will be lowered to libcalls giving a valid LHS and RHS 2999 // for the rest of the function (some i32 or i64 values). 3000 softenSetCCOperands(DAG, MVT::f128, LHS, RHS, CC, dl); 3001 3002 // If softenSetCCOperands returned a scalar, use it. 3003 if (RHS.getNode() == 0) { 3004 assert(LHS.getValueType() == Op.getValueType() && 3005 "Unexpected setcc expansion!"); 3006 return LHS; 3007 } 3008 } 3009 3010 if (LHS.getValueType().isInteger()) { 3011 SDValue A64cc; 3012 3013 // Integers are handled in a separate function because the combinations of 3014 // immediates and tests can get hairy and we may want to fiddle things. 3015 SDValue CmpOp = getSelectableIntSetCC(LHS, RHS, CC, A64cc, DAG, dl); 3016 3017 return DAG.getNode(AArch64ISD::SELECT_CC, dl, VT, 3018 CmpOp, DAG.getConstant(1, VT), DAG.getConstant(0, VT), 3019 A64cc); 3020 } 3021 3022 // Note that some LLVM floating-point CondCodes can't be lowered to a single 3023 // conditional branch, hence FPCCToA64CC can set a second test, where either 3024 // passing is sufficient. 3025 A64CC::CondCodes CondCode, Alternative = A64CC::Invalid; 3026 CondCode = FPCCToA64CC(CC, Alternative); 3027 SDValue A64cc = DAG.getConstant(CondCode, MVT::i32); 3028 SDValue CmpOp = DAG.getNode(AArch64ISD::SETCC, dl, MVT::i32, LHS, RHS, 3029 DAG.getCondCode(CC)); 3030 SDValue A64SELECT_CC = DAG.getNode(AArch64ISD::SELECT_CC, dl, VT, 3031 CmpOp, DAG.getConstant(1, VT), 3032 DAG.getConstant(0, VT), A64cc); 3033 3034 if (Alternative != A64CC::Invalid) { 3035 A64cc = DAG.getConstant(Alternative, MVT::i32); 3036 A64SELECT_CC = DAG.getNode(AArch64ISD::SELECT_CC, dl, VT, CmpOp, 3037 DAG.getConstant(1, VT), A64SELECT_CC, A64cc); 3038 } 3039 3040 return A64SELECT_CC; 3041 } 3042 3043 static SDValue LowerVectorSELECT_CC(SDValue Op, SelectionDAG &DAG) { 3044 SDLoc dl(Op); 3045 SDValue LHS = Op.getOperand(0); 3046 SDValue RHS = Op.getOperand(1); 3047 SDValue IfTrue = Op.getOperand(2); 3048 SDValue IfFalse = Op.getOperand(3); 3049 EVT IfTrueVT = IfTrue.getValueType(); 3050 EVT CondVT = IfTrueVT.changeVectorElementTypeToInteger(); 3051 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 3052 3053 // If LHS & RHS are floating point and IfTrue & IfFalse are vectors, we will 3054 // use NEON compare. 3055 if ((LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64)) { 3056 EVT EltVT = LHS.getValueType(); 3057 unsigned EltNum = 128 / EltVT.getSizeInBits(); 3058 EVT VT = EVT::getVectorVT(*DAG.getContext(), EltVT, EltNum); 3059 unsigned SubConstant = 3060 (LHS.getValueType() == MVT::f32) ? AArch64::sub_32 :AArch64::sub_64; 3061 EVT CEltT = (LHS.getValueType() == MVT::f32) ? MVT::i32 : MVT::i64; 3062 EVT CVT = EVT::getVectorVT(*DAG.getContext(), CEltT, EltNum); 3063 3064 LHS 3065 = SDValue(DAG.getMachineNode(TargetOpcode::SUBREG_TO_REG, dl, 3066 VT, DAG.getTargetConstant(0, MVT::i32), LHS, 3067 DAG.getTargetConstant(SubConstant, MVT::i32)), 0); 3068 RHS 3069 = SDValue(DAG.getMachineNode(TargetOpcode::SUBREG_TO_REG, dl, 3070 VT, DAG.getTargetConstant(0, MVT::i32), RHS, 3071 DAG.getTargetConstant(SubConstant, MVT::i32)), 0); 3072 3073 SDValue VSetCC = DAG.getSetCC(dl, CVT, LHS, RHS, CC); 3074 SDValue ResCC = LowerVectorSETCC(VSetCC, DAG); 3075 if (CEltT.getSizeInBits() < IfTrueVT.getSizeInBits()) { 3076 EVT DUPVT = 3077 EVT::getVectorVT(*DAG.getContext(), CEltT, 3078 IfTrueVT.getSizeInBits() / CEltT.getSizeInBits()); 3079 ResCC = DAG.getNode(AArch64ISD::NEON_VDUPLANE, dl, DUPVT, ResCC, 3080 DAG.getConstant(0, MVT::i64, false)); 3081 3082 ResCC = DAG.getNode(ISD::BITCAST, dl, CondVT, ResCC); 3083 } else { 3084 // FIXME: If IfTrue & IfFalse hold v1i8, v1i16 or v1i32, this function 3085 // can't handle them and will hit this assert. 3086 assert(CEltT.getSizeInBits() == IfTrueVT.getSizeInBits() && 3087 "Vector of IfTrue & IfFalse is too small."); 3088 3089 unsigned ExEltNum = 3090 EltNum * IfTrueVT.getSizeInBits() / ResCC.getValueSizeInBits(); 3091 EVT ExVT = EVT::getVectorVT(*DAG.getContext(), CEltT, ExEltNum); 3092 ResCC = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ExVT, ResCC, 3093 DAG.getConstant(0, MVT::i64, false)); 3094 ResCC = DAG.getNode(ISD::BITCAST, dl, CondVT, ResCC); 3095 } 3096 SDValue VSelect = DAG.getNode(ISD::VSELECT, dl, IfTrue.getValueType(), 3097 ResCC, IfTrue, IfFalse); 3098 return VSelect; 3099 } 3100 3101 // Here we handle the case that LHS & RHS are integer and IfTrue & IfFalse are 3102 // vectors. 3103 A64CC::CondCodes CondCode, Alternative = A64CC::Invalid; 3104 CondCode = FPCCToA64CC(CC, Alternative); 3105 SDValue A64cc = DAG.getConstant(CondCode, MVT::i32); 3106 SDValue SetCC = DAG.getNode(AArch64ISD::SETCC, dl, MVT::i32, LHS, RHS, 3107 DAG.getCondCode(CC)); 3108 EVT SEVT = MVT::i32; 3109 if (IfTrue.getValueType().getVectorElementType().getSizeInBits() > 32) 3110 SEVT = MVT::i64; 3111 SDValue AllOne = DAG.getConstant(-1, SEVT); 3112 SDValue AllZero = DAG.getConstant(0, SEVT); 3113 SDValue A64SELECT_CC = DAG.getNode(AArch64ISD::SELECT_CC, dl, SEVT, SetCC, 3114 AllOne, AllZero, A64cc); 3115 3116 if (Alternative != A64CC::Invalid) { 3117 A64cc = DAG.getConstant(Alternative, MVT::i32); 3118 A64SELECT_CC = DAG.getNode(AArch64ISD::SELECT_CC, dl, Op.getValueType(), 3119 SetCC, AllOne, A64SELECT_CC, A64cc); 3120 } 3121 SDValue VDup; 3122 if (IfTrue.getValueType().getVectorNumElements() == 1) 3123 VDup = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, CondVT, A64SELECT_CC); 3124 else 3125 VDup = DAG.getNode(AArch64ISD::NEON_VDUP, dl, CondVT, A64SELECT_CC); 3126 SDValue VSelect = DAG.getNode(ISD::VSELECT, dl, IfTrue.getValueType(), 3127 VDup, IfTrue, IfFalse); 3128 return VSelect; 3129 } 3130 3131 // (SELECT_CC lhs, rhs, iftrue, iffalse, condcode) 3132 SDValue 3133 AArch64TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { 3134 SDLoc dl(Op); 3135 SDValue LHS = Op.getOperand(0); 3136 SDValue RHS = Op.getOperand(1); 3137 SDValue IfTrue = Op.getOperand(2); 3138 SDValue IfFalse = Op.getOperand(3); 3139 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 3140 3141 if (IfTrue.getValueType().isVector()) 3142 return LowerVectorSELECT_CC(Op, DAG); 3143 3144 if (LHS.getValueType() == MVT::f128) { 3145 // f128 comparisons are lowered to libcalls, but slot in nicely here 3146 // afterwards. 3147 softenSetCCOperands(DAG, MVT::f128, LHS, RHS, CC, dl); 3148 3149 // If softenSetCCOperands returned a scalar, we need to compare the result 3150 // against zero to select between true and false values. 3151 if (RHS.getNode() == 0) { 3152 RHS = DAG.getConstant(0, LHS.getValueType()); 3153 CC = ISD::SETNE; 3154 } 3155 } 3156 3157 if (LHS.getValueType().isInteger()) { 3158 SDValue A64cc; 3159 3160 // Integers are handled in a separate function because the combinations of 3161 // immediates and tests can get hairy and we may want to fiddle things. 3162 SDValue CmpOp = getSelectableIntSetCC(LHS, RHS, CC, A64cc, DAG, dl); 3163 3164 return DAG.getNode(AArch64ISD::SELECT_CC, dl, Op.getValueType(), CmpOp, 3165 IfTrue, IfFalse, A64cc); 3166 } 3167 3168 // Note that some LLVM floating-point CondCodes can't be lowered to a single 3169 // conditional branch, hence FPCCToA64CC can set a second test, where either 3170 // passing is sufficient. 3171 A64CC::CondCodes CondCode, Alternative = A64CC::Invalid; 3172 CondCode = FPCCToA64CC(CC, Alternative); 3173 SDValue A64cc = DAG.getConstant(CondCode, MVT::i32); 3174 SDValue SetCC = DAG.getNode(AArch64ISD::SETCC, dl, MVT::i32, LHS, RHS, 3175 DAG.getCondCode(CC)); 3176 SDValue A64SELECT_CC = DAG.getNode(AArch64ISD::SELECT_CC, dl, 3177 Op.getValueType(), 3178 SetCC, IfTrue, IfFalse, A64cc); 3179 3180 if (Alternative != A64CC::Invalid) { 3181 A64cc = DAG.getConstant(Alternative, MVT::i32); 3182 A64SELECT_CC = DAG.getNode(AArch64ISD::SELECT_CC, dl, Op.getValueType(), 3183 SetCC, IfTrue, A64SELECT_CC, A64cc); 3184 3185 } 3186 3187 return A64SELECT_CC; 3188 } 3189 3190 SDValue 3191 AArch64TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const { 3192 const Value *DestSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue(); 3193 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); 3194 3195 // We have to make sure we copy the entire structure: 8+8+8+4+4 = 32 bytes 3196 // rather than just 8. 3197 return DAG.getMemcpy(Op.getOperand(0), SDLoc(Op), 3198 Op.getOperand(1), Op.getOperand(2), 3199 DAG.getConstant(32, MVT::i32), 8, false, false, 3200 MachinePointerInfo(DestSV), MachinePointerInfo(SrcSV)); 3201 } 3202 3203 SDValue 3204 AArch64TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const { 3205 // The layout of the va_list struct is specified in the AArch64 Procedure Call 3206 // Standard, section B.3. 3207 MachineFunction &MF = DAG.getMachineFunction(); 3208 AArch64MachineFunctionInfo *FuncInfo 3209 = MF.getInfo<AArch64MachineFunctionInfo>(); 3210 SDLoc DL(Op); 3211 3212 SDValue Chain = Op.getOperand(0); 3213 SDValue VAList = Op.getOperand(1); 3214 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 3215 SmallVector<SDValue, 4> MemOps; 3216 3217 // void *__stack at offset 0 3218 SDValue Stack = DAG.getFrameIndex(FuncInfo->getVariadicStackIdx(), 3219 getPointerTy()); 3220 MemOps.push_back(DAG.getStore(Chain, DL, Stack, VAList, 3221 MachinePointerInfo(SV), false, false, 0)); 3222 3223 // void *__gr_top at offset 8 3224 int GPRSize = FuncInfo->getVariadicGPRSize(); 3225 if (GPRSize > 0) { 3226 SDValue GRTop, GRTopAddr; 3227 3228 GRTopAddr = DAG.getNode(ISD::ADD, DL, getPointerTy(), VAList, 3229 DAG.getConstant(8, getPointerTy())); 3230 3231 GRTop = DAG.getFrameIndex(FuncInfo->getVariadicGPRIdx(), getPointerTy()); 3232 GRTop = DAG.getNode(ISD::ADD, DL, getPointerTy(), GRTop, 3233 DAG.getConstant(GPRSize, getPointerTy())); 3234 3235 MemOps.push_back(DAG.getStore(Chain, DL, GRTop, GRTopAddr, 3236 MachinePointerInfo(SV, 8), 3237 false, false, 0)); 3238 } 3239 3240 // void *__vr_top at offset 16 3241 int FPRSize = FuncInfo->getVariadicFPRSize(); 3242 if (FPRSize > 0) { 3243 SDValue VRTop, VRTopAddr; 3244 VRTopAddr = DAG.getNode(ISD::ADD, DL, getPointerTy(), VAList, 3245 DAG.getConstant(16, getPointerTy())); 3246 3247 VRTop = DAG.getFrameIndex(FuncInfo->getVariadicFPRIdx(), getPointerTy()); 3248 VRTop = DAG.getNode(ISD::ADD, DL, getPointerTy(), VRTop, 3249 DAG.getConstant(FPRSize, getPointerTy())); 3250 3251 MemOps.push_back(DAG.getStore(Chain, DL, VRTop, VRTopAddr, 3252 MachinePointerInfo(SV, 16), 3253 false, false, 0)); 3254 } 3255 3256 // int __gr_offs at offset 24 3257 SDValue GROffsAddr = DAG.getNode(ISD::ADD, DL, getPointerTy(), VAList, 3258 DAG.getConstant(24, getPointerTy())); 3259 MemOps.push_back(DAG.getStore(Chain, DL, DAG.getConstant(-GPRSize, MVT::i32), 3260 GROffsAddr, MachinePointerInfo(SV, 24), 3261 false, false, 0)); 3262 3263 // int __vr_offs at offset 28 3264 SDValue VROffsAddr = DAG.getNode(ISD::ADD, DL, getPointerTy(), VAList, 3265 DAG.getConstant(28, getPointerTy())); 3266 MemOps.push_back(DAG.getStore(Chain, DL, DAG.getConstant(-FPRSize, MVT::i32), 3267 VROffsAddr, MachinePointerInfo(SV, 28), 3268 false, false, 0)); 3269 3270 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &MemOps[0], 3271 MemOps.size()); 3272 } 3273 3274 SDValue 3275 AArch64TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 3276 switch (Op.getOpcode()) { 3277 default: llvm_unreachable("Don't know how to custom lower this!"); 3278 case ISD::FADD: return LowerF128ToCall(Op, DAG, RTLIB::ADD_F128); 3279 case ISD::FSUB: return LowerF128ToCall(Op, DAG, RTLIB::SUB_F128); 3280 case ISD::FMUL: return LowerF128ToCall(Op, DAG, RTLIB::MUL_F128); 3281 case ISD::FDIV: return LowerF128ToCall(Op, DAG, RTLIB::DIV_F128); 3282 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG, true); 3283 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG, false); 3284 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG, true); 3285 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG, false); 3286 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG); 3287 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG); 3288 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 3289 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 3290 3291 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); 3292 case ISD::BRCOND: return LowerBRCOND(Op, DAG); 3293 case ISD::BR_CC: return LowerBR_CC(Op, DAG); 3294 case ISD::GlobalAddress: return LowerGlobalAddressELF(Op, DAG); 3295 case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 3296 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); 3297 case ISD::JumpTable: return LowerJumpTable(Op, DAG); 3298 case ISD::SELECT: return LowerSELECT(Op, DAG); 3299 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 3300 case ISD::SETCC: return LowerSETCC(Op, DAG); 3301 case ISD::VACOPY: return LowerVACOPY(Op, DAG); 3302 case ISD::VASTART: return LowerVASTART(Op, DAG); 3303 case ISD::BUILD_VECTOR: 3304 return LowerBUILD_VECTOR(Op, DAG, getSubtarget()); 3305 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG); 3306 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); 3307 } 3308 3309 return SDValue(); 3310 } 3311 3312 /// Check if the specified splat value corresponds to a valid vector constant 3313 /// for a Neon instruction with a "modified immediate" operand (e.g., MOVI). If 3314 /// so, return the encoded 8-bit immediate and the OpCmode instruction fields 3315 /// values. 3316 static bool isNeonModifiedImm(uint64_t SplatBits, uint64_t SplatUndef, 3317 unsigned SplatBitSize, SelectionDAG &DAG, 3318 bool is128Bits, NeonModImmType type, EVT &VT, 3319 unsigned &Imm, unsigned &OpCmode) { 3320 switch (SplatBitSize) { 3321 default: 3322 llvm_unreachable("unexpected size for isNeonModifiedImm"); 3323 case 8: { 3324 if (type != Neon_Mov_Imm) 3325 return false; 3326 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big"); 3327 // Neon movi per byte: Op=0, Cmode=1110. 3328 OpCmode = 0xe; 3329 Imm = SplatBits; 3330 VT = is128Bits ? MVT::v16i8 : MVT::v8i8; 3331 break; 3332 } 3333 case 16: { 3334 // Neon move inst per halfword 3335 VT = is128Bits ? MVT::v8i16 : MVT::v4i16; 3336 if ((SplatBits & ~0xff) == 0) { 3337 // Value = 0x00nn is 0x00nn LSL 0 3338 // movi: Op=0, Cmode=1000; mvni: Op=1, Cmode=1000 3339 // bic: Op=1, Cmode=1001; orr: Op=0, Cmode=1001 3340 // Op=x, Cmode=100y 3341 Imm = SplatBits; 3342 OpCmode = 0x8; 3343 break; 3344 } 3345 if ((SplatBits & ~0xff00) == 0) { 3346 // Value = 0xnn00 is 0x00nn LSL 8 3347 // movi: Op=0, Cmode=1010; mvni: Op=1, Cmode=1010 3348 // bic: Op=1, Cmode=1011; orr: Op=0, Cmode=1011 3349 // Op=x, Cmode=101x 3350 Imm = SplatBits >> 8; 3351 OpCmode = 0xa; 3352 break; 3353 } 3354 // can't handle any other 3355 return false; 3356 } 3357 3358 case 32: { 3359 // First the LSL variants (MSL is unusable by some interested instructions). 3360 3361 // Neon move instr per word, shift zeros 3362 VT = is128Bits ? MVT::v4i32 : MVT::v2i32; 3363 if ((SplatBits & ~0xff) == 0) { 3364 // Value = 0x000000nn is 0x000000nn LSL 0 3365 // movi: Op=0, Cmode= 0000; mvni: Op=1, Cmode= 0000 3366 // bic: Op=1, Cmode= 0001; orr: Op=0, Cmode= 0001 3367 // Op=x, Cmode=000x 3368 Imm = SplatBits; 3369 OpCmode = 0; 3370 break; 3371 } 3372 if ((SplatBits & ~0xff00) == 0) { 3373 // Value = 0x0000nn00 is 0x000000nn LSL 8 3374 // movi: Op=0, Cmode= 0010; mvni: Op=1, Cmode= 0010 3375 // bic: Op=1, Cmode= 0011; orr : Op=0, Cmode= 0011 3376 // Op=x, Cmode=001x 3377 Imm = SplatBits >> 8; 3378 OpCmode = 0x2; 3379 break; 3380 } 3381 if ((SplatBits & ~0xff0000) == 0) { 3382 // Value = 0x00nn0000 is 0x000000nn LSL 16 3383 // movi: Op=0, Cmode= 0100; mvni: Op=1, Cmode= 0100 3384 // bic: Op=1, Cmode= 0101; orr: Op=0, Cmode= 0101 3385 // Op=x, Cmode=010x 3386 Imm = SplatBits >> 16; 3387 OpCmode = 0x4; 3388 break; 3389 } 3390 if ((SplatBits & ~0xff000000) == 0) { 3391 // Value = 0xnn000000 is 0x000000nn LSL 24 3392 // movi: Op=0, Cmode= 0110; mvni: Op=1, Cmode= 0110 3393 // bic: Op=1, Cmode= 0111; orr: Op=0, Cmode= 0111 3394 // Op=x, Cmode=011x 3395 Imm = SplatBits >> 24; 3396 OpCmode = 0x6; 3397 break; 3398 } 3399 3400 // Now the MSL immediates. 3401 3402 // Neon move instr per word, shift ones 3403 if ((SplatBits & ~0xffff) == 0 && 3404 ((SplatBits | SplatUndef) & 0xff) == 0xff) { 3405 // Value = 0x0000nnff is 0x000000nn MSL 8 3406 // movi: Op=0, Cmode= 1100; mvni: Op=1, Cmode= 1100 3407 // Op=x, Cmode=1100 3408 Imm = SplatBits >> 8; 3409 OpCmode = 0xc; 3410 break; 3411 } 3412 if ((SplatBits & ~0xffffff) == 0 && 3413 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) { 3414 // Value = 0x00nnffff is 0x000000nn MSL 16 3415 // movi: Op=1, Cmode= 1101; mvni: Op=1, Cmode= 1101 3416 // Op=x, Cmode=1101 3417 Imm = SplatBits >> 16; 3418 OpCmode = 0xd; 3419 break; 3420 } 3421 // can't handle any other 3422 return false; 3423 } 3424 3425 case 64: { 3426 if (type != Neon_Mov_Imm) 3427 return false; 3428 // Neon move instr bytemask, where each byte is either 0x00 or 0xff. 3429 // movi Op=1, Cmode=1110. 3430 OpCmode = 0x1e; 3431 uint64_t BitMask = 0xff; 3432 uint64_t Val = 0; 3433 unsigned ImmMask = 1; 3434 Imm = 0; 3435 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) { 3436 if (((SplatBits | SplatUndef) & BitMask) == BitMask) { 3437 Val |= BitMask; 3438 Imm |= ImmMask; 3439 } else if ((SplatBits & BitMask) != 0) { 3440 return false; 3441 } 3442 BitMask <<= 8; 3443 ImmMask <<= 1; 3444 } 3445 SplatBits = Val; 3446 VT = is128Bits ? MVT::v2i64 : MVT::v1i64; 3447 break; 3448 } 3449 } 3450 3451 return true; 3452 } 3453 3454 static SDValue PerformANDCombine(SDNode *N, 3455 TargetLowering::DAGCombinerInfo &DCI) { 3456 3457 SelectionDAG &DAG = DCI.DAG; 3458 SDLoc DL(N); 3459 EVT VT = N->getValueType(0); 3460 3461 // We're looking for an SRA/SHL pair which form an SBFX. 3462 3463 if (VT != MVT::i32 && VT != MVT::i64) 3464 return SDValue(); 3465 3466 if (!isa<ConstantSDNode>(N->getOperand(1))) 3467 return SDValue(); 3468 3469 uint64_t TruncMask = N->getConstantOperandVal(1); 3470 if (!isMask_64(TruncMask)) 3471 return SDValue(); 3472 3473 uint64_t Width = CountPopulation_64(TruncMask); 3474 SDValue Shift = N->getOperand(0); 3475 3476 if (Shift.getOpcode() != ISD::SRL) 3477 return SDValue(); 3478 3479 if (!isa<ConstantSDNode>(Shift->getOperand(1))) 3480 return SDValue(); 3481 uint64_t LSB = Shift->getConstantOperandVal(1); 3482 3483 if (LSB > VT.getSizeInBits() || Width > VT.getSizeInBits()) 3484 return SDValue(); 3485 3486 return DAG.getNode(AArch64ISD::UBFX, DL, VT, Shift.getOperand(0), 3487 DAG.getConstant(LSB, MVT::i64), 3488 DAG.getConstant(LSB + Width - 1, MVT::i64)); 3489 } 3490 3491 /// For a true bitfield insert, the bits getting into that contiguous mask 3492 /// should come from the low part of an existing value: they must be formed from 3493 /// a compatible SHL operation (unless they're already low). This function 3494 /// checks that condition and returns the least-significant bit that's 3495 /// intended. If the operation not a field preparation, -1 is returned. 3496 static int32_t getLSBForBFI(SelectionDAG &DAG, SDLoc DL, EVT VT, 3497 SDValue &MaskedVal, uint64_t Mask) { 3498 if (!isShiftedMask_64(Mask)) 3499 return -1; 3500 3501 // Now we need to alter MaskedVal so that it is an appropriate input for a BFI 3502 // instruction. BFI will do a left-shift by LSB before applying the mask we've 3503 // spotted, so in general we should pre-emptively "undo" that by making sure 3504 // the incoming bits have had a right-shift applied to them. 3505 // 3506 // This right shift, however, will combine with existing left/right shifts. In 3507 // the simplest case of a completely straight bitfield operation, it will be 3508 // expected to completely cancel out with an existing SHL. More complicated 3509 // cases (e.g. bitfield to bitfield copy) may still need a real shift before 3510 // the BFI. 3511 3512 uint64_t LSB = countTrailingZeros(Mask); 3513 int64_t ShiftRightRequired = LSB; 3514 if (MaskedVal.getOpcode() == ISD::SHL && 3515 isa<ConstantSDNode>(MaskedVal.getOperand(1))) { 3516 ShiftRightRequired -= MaskedVal.getConstantOperandVal(1); 3517 MaskedVal = MaskedVal.getOperand(0); 3518 } else if (MaskedVal.getOpcode() == ISD::SRL && 3519 isa<ConstantSDNode>(MaskedVal.getOperand(1))) { 3520 ShiftRightRequired += MaskedVal.getConstantOperandVal(1); 3521 MaskedVal = MaskedVal.getOperand(0); 3522 } 3523 3524 if (ShiftRightRequired > 0) 3525 MaskedVal = DAG.getNode(ISD::SRL, DL, VT, MaskedVal, 3526 DAG.getConstant(ShiftRightRequired, MVT::i64)); 3527 else if (ShiftRightRequired < 0) { 3528 // We could actually end up with a residual left shift, for example with 3529 // "struc.bitfield = val << 1". 3530 MaskedVal = DAG.getNode(ISD::SHL, DL, VT, MaskedVal, 3531 DAG.getConstant(-ShiftRightRequired, MVT::i64)); 3532 } 3533 3534 return LSB; 3535 } 3536 3537 /// Searches from N for an existing AArch64ISD::BFI node, possibly surrounded by 3538 /// a mask and an extension. Returns true if a BFI was found and provides 3539 /// information on its surroundings. 3540 static bool findMaskedBFI(SDValue N, SDValue &BFI, uint64_t &Mask, 3541 bool &Extended) { 3542 Extended = false; 3543 if (N.getOpcode() == ISD::ZERO_EXTEND) { 3544 Extended = true; 3545 N = N.getOperand(0); 3546 } 3547 3548 if (N.getOpcode() == ISD::AND && isa<ConstantSDNode>(N.getOperand(1))) { 3549 Mask = N->getConstantOperandVal(1); 3550 N = N.getOperand(0); 3551 } else { 3552 // Mask is the whole width. 3553 Mask = -1ULL >> (64 - N.getValueType().getSizeInBits()); 3554 } 3555 3556 if (N.getOpcode() == AArch64ISD::BFI) { 3557 BFI = N; 3558 return true; 3559 } 3560 3561 return false; 3562 } 3563 3564 /// Try to combine a subtree (rooted at an OR) into a "masked BFI" node, which 3565 /// is roughly equivalent to (and (BFI ...), mask). This form is used because it 3566 /// can often be further combined with a larger mask. Ultimately, we want mask 3567 /// to be 2^32-1 or 2^64-1 so the AND can be skipped. 3568 static SDValue tryCombineToBFI(SDNode *N, 3569 TargetLowering::DAGCombinerInfo &DCI, 3570 const AArch64Subtarget *Subtarget) { 3571 SelectionDAG &DAG = DCI.DAG; 3572 SDLoc DL(N); 3573 EVT VT = N->getValueType(0); 3574 3575 assert(N->getOpcode() == ISD::OR && "Unexpected root"); 3576 3577 // We need the LHS to be (and SOMETHING, MASK). Find out what that mask is or 3578 // abandon the effort. 3579 SDValue LHS = N->getOperand(0); 3580 if (LHS.getOpcode() != ISD::AND) 3581 return SDValue(); 3582 3583 uint64_t LHSMask; 3584 if (isa<ConstantSDNode>(LHS.getOperand(1))) 3585 LHSMask = LHS->getConstantOperandVal(1); 3586 else 3587 return SDValue(); 3588 3589 // We also need the RHS to be (and SOMETHING, MASK). Find out what that mask 3590 // is or abandon the effort. 3591 SDValue RHS = N->getOperand(1); 3592 if (RHS.getOpcode() != ISD::AND) 3593 return SDValue(); 3594 3595 uint64_t RHSMask; 3596 if (isa<ConstantSDNode>(RHS.getOperand(1))) 3597 RHSMask = RHS->getConstantOperandVal(1); 3598 else 3599 return SDValue(); 3600 3601 // Can't do anything if the masks are incompatible. 3602 if (LHSMask & RHSMask) 3603 return SDValue(); 3604 3605 // Now we need one of the masks to be a contiguous field. Without loss of 3606 // generality that should be the RHS one. 3607 SDValue Bitfield = LHS.getOperand(0); 3608 if (getLSBForBFI(DAG, DL, VT, Bitfield, LHSMask) != -1) { 3609 // We know that LHS is a candidate new value, and RHS isn't already a better 3610 // one. 3611 std::swap(LHS, RHS); 3612 std::swap(LHSMask, RHSMask); 3613 } 3614 3615 // We've done our best to put the right operands in the right places, all we 3616 // can do now is check whether a BFI exists. 3617 Bitfield = RHS.getOperand(0); 3618 int32_t LSB = getLSBForBFI(DAG, DL, VT, Bitfield, RHSMask); 3619 if (LSB == -1) 3620 return SDValue(); 3621 3622 uint32_t Width = CountPopulation_64(RHSMask); 3623 assert(Width && "Expected non-zero bitfield width"); 3624 3625 SDValue BFI = DAG.getNode(AArch64ISD::BFI, DL, VT, 3626 LHS.getOperand(0), Bitfield, 3627 DAG.getConstant(LSB, MVT::i64), 3628 DAG.getConstant(Width, MVT::i64)); 3629 3630 // Mask is trivial 3631 if ((LHSMask | RHSMask) == (-1ULL >> (64 - VT.getSizeInBits()))) 3632 return BFI; 3633 3634 return DAG.getNode(ISD::AND, DL, VT, BFI, 3635 DAG.getConstant(LHSMask | RHSMask, VT)); 3636 } 3637 3638 /// Search for the bitwise combining (with careful masks) of a MaskedBFI and its 3639 /// original input. This is surprisingly common because SROA splits things up 3640 /// into i8 chunks, so the originally detected MaskedBFI may actually only act 3641 /// on the low (say) byte of a word. This is then orred into the rest of the 3642 /// word afterwards. 3643 /// 3644 /// Basic input: (or (and OLDFIELD, MASK1), (MaskedBFI MASK2, OLDFIELD, ...)). 3645 /// 3646 /// If MASK1 and MASK2 are compatible, we can fold the whole thing into the 3647 /// MaskedBFI. We can also deal with a certain amount of extend/truncate being 3648 /// involved. 3649 static SDValue tryCombineToLargerBFI(SDNode *N, 3650 TargetLowering::DAGCombinerInfo &DCI, 3651 const AArch64Subtarget *Subtarget) { 3652 SelectionDAG &DAG = DCI.DAG; 3653 SDLoc DL(N); 3654 EVT VT = N->getValueType(0); 3655 3656 // First job is to hunt for a MaskedBFI on either the left or right. Swap 3657 // operands if it's actually on the right. 3658 SDValue BFI; 3659 SDValue PossExtraMask; 3660 uint64_t ExistingMask = 0; 3661 bool Extended = false; 3662 if (findMaskedBFI(N->getOperand(0), BFI, ExistingMask, Extended)) 3663 PossExtraMask = N->getOperand(1); 3664 else if (findMaskedBFI(N->getOperand(1), BFI, ExistingMask, Extended)) 3665 PossExtraMask = N->getOperand(0); 3666 else 3667 return SDValue(); 3668 3669 // We can only combine a BFI with another compatible mask. 3670 if (PossExtraMask.getOpcode() != ISD::AND || 3671 !isa<ConstantSDNode>(PossExtraMask.getOperand(1))) 3672 return SDValue(); 3673 3674 uint64_t ExtraMask = PossExtraMask->getConstantOperandVal(1); 3675 3676 // Masks must be compatible. 3677 if (ExtraMask & ExistingMask) 3678 return SDValue(); 3679 3680 SDValue OldBFIVal = BFI.getOperand(0); 3681 SDValue NewBFIVal = BFI.getOperand(1); 3682 if (Extended) { 3683 // We skipped a ZERO_EXTEND above, so the input to the MaskedBFIs should be 3684 // 32-bit and we'll be forming a 64-bit MaskedBFI. The MaskedBFI arguments 3685 // need to be made compatible. 3686 assert(VT == MVT::i64 && BFI.getValueType() == MVT::i32 3687 && "Invalid types for BFI"); 3688 OldBFIVal = DAG.getNode(ISD::ANY_EXTEND, DL, VT, OldBFIVal); 3689 NewBFIVal = DAG.getNode(ISD::ANY_EXTEND, DL, VT, NewBFIVal); 3690 } 3691 3692 // We need the MaskedBFI to be combined with a mask of the *same* value. 3693 if (PossExtraMask.getOperand(0) != OldBFIVal) 3694 return SDValue(); 3695 3696 BFI = DAG.getNode(AArch64ISD::BFI, DL, VT, 3697 OldBFIVal, NewBFIVal, 3698 BFI.getOperand(2), BFI.getOperand(3)); 3699 3700 // If the masking is trivial, we don't need to create it. 3701 if ((ExtraMask | ExistingMask) == (-1ULL >> (64 - VT.getSizeInBits()))) 3702 return BFI; 3703 3704 return DAG.getNode(ISD::AND, DL, VT, BFI, 3705 DAG.getConstant(ExtraMask | ExistingMask, VT)); 3706 } 3707 3708 /// An EXTR instruction is made up of two shifts, ORed together. This helper 3709 /// searches for and classifies those shifts. 3710 static bool findEXTRHalf(SDValue N, SDValue &Src, uint32_t &ShiftAmount, 3711 bool &FromHi) { 3712 if (N.getOpcode() == ISD::SHL) 3713 FromHi = false; 3714 else if (N.getOpcode() == ISD::SRL) 3715 FromHi = true; 3716 else 3717 return false; 3718 3719 if (!isa<ConstantSDNode>(N.getOperand(1))) 3720 return false; 3721 3722 ShiftAmount = N->getConstantOperandVal(1); 3723 Src = N->getOperand(0); 3724 return true; 3725 } 3726 3727 /// EXTR instruction extracts a contiguous chunk of bits from two existing 3728 /// registers viewed as a high/low pair. This function looks for the pattern: 3729 /// (or (shl VAL1, #N), (srl VAL2, #RegWidth-N)) and replaces it with an 3730 /// EXTR. Can't quite be done in TableGen because the two immediates aren't 3731 /// independent. 3732 static SDValue tryCombineToEXTR(SDNode *N, 3733 TargetLowering::DAGCombinerInfo &DCI) { 3734 SelectionDAG &DAG = DCI.DAG; 3735 SDLoc DL(N); 3736 EVT VT = N->getValueType(0); 3737 3738 assert(N->getOpcode() == ISD::OR && "Unexpected root"); 3739 3740 if (VT != MVT::i32 && VT != MVT::i64) 3741 return SDValue(); 3742 3743 SDValue LHS; 3744 uint32_t ShiftLHS = 0; 3745 bool LHSFromHi = 0; 3746 if (!findEXTRHalf(N->getOperand(0), LHS, ShiftLHS, LHSFromHi)) 3747 return SDValue(); 3748 3749 SDValue RHS; 3750 uint32_t ShiftRHS = 0; 3751 bool RHSFromHi = 0; 3752 if (!findEXTRHalf(N->getOperand(1), RHS, ShiftRHS, RHSFromHi)) 3753 return SDValue(); 3754 3755 // If they're both trying to come from the high part of the register, they're 3756 // not really an EXTR. 3757 if (LHSFromHi == RHSFromHi) 3758 return SDValue(); 3759 3760 if (ShiftLHS + ShiftRHS != VT.getSizeInBits()) 3761 return SDValue(); 3762 3763 if (LHSFromHi) { 3764 std::swap(LHS, RHS); 3765 std::swap(ShiftLHS, ShiftRHS); 3766 } 3767 3768 return DAG.getNode(AArch64ISD::EXTR, DL, VT, 3769 LHS, RHS, 3770 DAG.getConstant(ShiftRHS, MVT::i64)); 3771 } 3772 3773 /// Target-specific dag combine xforms for ISD::OR 3774 static SDValue PerformORCombine(SDNode *N, 3775 TargetLowering::DAGCombinerInfo &DCI, 3776 const AArch64Subtarget *Subtarget) { 3777 3778 SelectionDAG &DAG = DCI.DAG; 3779 SDLoc DL(N); 3780 EVT VT = N->getValueType(0); 3781 3782 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 3783 return SDValue(); 3784 3785 // Attempt to recognise bitfield-insert operations. 3786 SDValue Res = tryCombineToBFI(N, DCI, Subtarget); 3787 if (Res.getNode()) 3788 return Res; 3789 3790 // Attempt to combine an existing MaskedBFI operation into one with a larger 3791 // mask. 3792 Res = tryCombineToLargerBFI(N, DCI, Subtarget); 3793 if (Res.getNode()) 3794 return Res; 3795 3796 Res = tryCombineToEXTR(N, DCI); 3797 if (Res.getNode()) 3798 return Res; 3799 3800 if (!Subtarget->hasNEON()) 3801 return SDValue(); 3802 3803 // Attempt to use vector immediate-form BSL 3804 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant. 3805 3806 SDValue N0 = N->getOperand(0); 3807 if (N0.getOpcode() != ISD::AND) 3808 return SDValue(); 3809 3810 SDValue N1 = N->getOperand(1); 3811 if (N1.getOpcode() != ISD::AND) 3812 return SDValue(); 3813 3814 if (VT.isVector() && DAG.getTargetLoweringInfo().isTypeLegal(VT)) { 3815 APInt SplatUndef; 3816 unsigned SplatBitSize; 3817 bool HasAnyUndefs; 3818 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1)); 3819 APInt SplatBits0; 3820 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize, 3821 HasAnyUndefs) && 3822 !HasAnyUndefs) { 3823 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1)); 3824 APInt SplatBits1; 3825 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize, 3826 HasAnyUndefs) && !HasAnyUndefs && 3827 SplatBits0.getBitWidth() == SplatBits1.getBitWidth() && 3828 SplatBits0 == ~SplatBits1) { 3829 3830 return DAG.getNode(ISD::VSELECT, DL, VT, N0->getOperand(1), 3831 N0->getOperand(0), N1->getOperand(0)); 3832 } 3833 } 3834 } 3835 3836 return SDValue(); 3837 } 3838 3839 /// Target-specific dag combine xforms for ISD::SRA 3840 static SDValue PerformSRACombine(SDNode *N, 3841 TargetLowering::DAGCombinerInfo &DCI) { 3842 3843 SelectionDAG &DAG = DCI.DAG; 3844 SDLoc DL(N); 3845 EVT VT = N->getValueType(0); 3846 3847 // We're looking for an SRA/SHL pair which form an SBFX. 3848 3849 if (VT != MVT::i32 && VT != MVT::i64) 3850 return SDValue(); 3851 3852 if (!isa<ConstantSDNode>(N->getOperand(1))) 3853 return SDValue(); 3854 3855 uint64_t ExtraSignBits = N->getConstantOperandVal(1); 3856 SDValue Shift = N->getOperand(0); 3857 3858 if (Shift.getOpcode() != ISD::SHL) 3859 return SDValue(); 3860 3861 if (!isa<ConstantSDNode>(Shift->getOperand(1))) 3862 return SDValue(); 3863 3864 uint64_t BitsOnLeft = Shift->getConstantOperandVal(1); 3865 uint64_t Width = VT.getSizeInBits() - ExtraSignBits; 3866 uint64_t LSB = VT.getSizeInBits() - Width - BitsOnLeft; 3867 3868 if (LSB > VT.getSizeInBits() || Width > VT.getSizeInBits()) 3869 return SDValue(); 3870 3871 return DAG.getNode(AArch64ISD::SBFX, DL, VT, Shift.getOperand(0), 3872 DAG.getConstant(LSB, MVT::i64), 3873 DAG.getConstant(LSB + Width - 1, MVT::i64)); 3874 } 3875 3876 /// Check if this is a valid build_vector for the immediate operand of 3877 /// a vector shift operation, where all the elements of the build_vector 3878 /// must have the same constant integer value. 3879 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) { 3880 // Ignore bit_converts. 3881 while (Op.getOpcode() == ISD::BITCAST) 3882 Op = Op.getOperand(0); 3883 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); 3884 APInt SplatBits, SplatUndef; 3885 unsigned SplatBitSize; 3886 bool HasAnyUndefs; 3887 if (!BVN || !BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, 3888 HasAnyUndefs, ElementBits) || 3889 SplatBitSize > ElementBits) 3890 return false; 3891 Cnt = SplatBits.getSExtValue(); 3892 return true; 3893 } 3894 3895 /// Check if this is a valid build_vector for the immediate operand of 3896 /// a vector shift left operation. That value must be in the range: 3897 /// 0 <= Value < ElementBits 3898 static bool isVShiftLImm(SDValue Op, EVT VT, int64_t &Cnt) { 3899 assert(VT.isVector() && "vector shift count is not a vector type"); 3900 unsigned ElementBits = VT.getVectorElementType().getSizeInBits(); 3901 if (!getVShiftImm(Op, ElementBits, Cnt)) 3902 return false; 3903 return (Cnt >= 0 && Cnt < ElementBits); 3904 } 3905 3906 /// Check if this is a valid build_vector for the immediate operand of a 3907 /// vector shift right operation. The value must be in the range: 3908 /// 1 <= Value <= ElementBits 3909 static bool isVShiftRImm(SDValue Op, EVT VT, int64_t &Cnt) { 3910 assert(VT.isVector() && "vector shift count is not a vector type"); 3911 unsigned ElementBits = VT.getVectorElementType().getSizeInBits(); 3912 if (!getVShiftImm(Op, ElementBits, Cnt)) 3913 return false; 3914 return (Cnt >= 1 && Cnt <= ElementBits); 3915 } 3916 3917 static SDValue GenForSextInreg(SDNode *N, 3918 TargetLowering::DAGCombinerInfo &DCI, 3919 EVT SrcVT, EVT DestVT, EVT SubRegVT, 3920 const int *Mask, SDValue Src) { 3921 SelectionDAG &DAG = DCI.DAG; 3922 SDValue Bitcast 3923 = DAG.getNode(ISD::BITCAST, SDLoc(N), SrcVT, Src); 3924 SDValue Sext 3925 = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), DestVT, Bitcast); 3926 SDValue ShuffleVec 3927 = DAG.getVectorShuffle(DestVT, SDLoc(N), Sext, DAG.getUNDEF(DestVT), Mask); 3928 SDValue ExtractSubreg 3929 = SDValue(DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, SDLoc(N), 3930 SubRegVT, ShuffleVec, 3931 DAG.getTargetConstant(AArch64::sub_64, MVT::i32)), 0); 3932 return ExtractSubreg; 3933 } 3934 3935 /// Checks for vector shifts and lowers them. 3936 static SDValue PerformShiftCombine(SDNode *N, 3937 TargetLowering::DAGCombinerInfo &DCI, 3938 const AArch64Subtarget *ST) { 3939 SelectionDAG &DAG = DCI.DAG; 3940 EVT VT = N->getValueType(0); 3941 if (N->getOpcode() == ISD::SRA && (VT == MVT::i32 || VT == MVT::i64)) 3942 return PerformSRACombine(N, DCI); 3943 3944 // We're looking for an SRA/SHL pair to help generating instruction 3945 // sshll v0.8h, v0.8b, #0 3946 // The instruction STXL is also the alias of this instruction. 3947 // 3948 // For example, for DAG like below, 3949 // v2i32 = sra (v2i32 (shl v2i32, 16)), 16 3950 // we can transform it into 3951 // v2i32 = EXTRACT_SUBREG 3952 // (v4i32 (suffle_vector 3953 // (v4i32 (sext (v4i16 (bitcast v2i32))), 3954 // undef, (0, 2, u, u)), 3955 // sub_64 3956 // 3957 // With this transformation we expect to generate "SSHLL + UZIP1" 3958 // Sometimes UZIP1 can be optimized away by combining with other context. 3959 int64_t ShrCnt, ShlCnt; 3960 if (N->getOpcode() == ISD::SRA 3961 && (VT == MVT::v2i32 || VT == MVT::v4i16) 3962 && isVShiftRImm(N->getOperand(1), VT, ShrCnt) 3963 && N->getOperand(0).getOpcode() == ISD::SHL 3964 && isVShiftRImm(N->getOperand(0).getOperand(1), VT, ShlCnt)) { 3965 SDValue Src = N->getOperand(0).getOperand(0); 3966 if (VT == MVT::v2i32 && ShrCnt == 16 && ShlCnt == 16) { 3967 // sext_inreg(v2i32, v2i16) 3968 // We essentially only care the Mask {0, 2, u, u} 3969 int Mask[4] = {0, 2, 4, 6}; 3970 return GenForSextInreg(N, DCI, MVT::v4i16, MVT::v4i32, MVT::v2i32, 3971 Mask, Src); 3972 } 3973 else if (VT == MVT::v2i32 && ShrCnt == 24 && ShlCnt == 24) { 3974 // sext_inreg(v2i16, v2i8) 3975 // We essentially only care the Mask {0, u, 4, u, u, u, u, u, u, u, u, u} 3976 int Mask[8] = {0, 2, 4, 6, 8, 10, 12, 14}; 3977 return GenForSextInreg(N, DCI, MVT::v8i8, MVT::v8i16, MVT::v2i32, 3978 Mask, Src); 3979 } 3980 else if (VT == MVT::v4i16 && ShrCnt == 8 && ShlCnt == 8) { 3981 // sext_inreg(v4i16, v4i8) 3982 // We essentially only care the Mask {0, 2, 4, 6, u, u, u, u, u, u, u, u} 3983 int Mask[8] = {0, 2, 4, 6, 8, 10, 12, 14}; 3984 return GenForSextInreg(N, DCI, MVT::v8i8, MVT::v8i16, MVT::v4i16, 3985 Mask, Src); 3986 } 3987 } 3988 3989 // Nothing to be done for scalar shifts. 3990 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3991 if (!VT.isVector() || !TLI.isTypeLegal(VT)) 3992 return SDValue(); 3993 3994 assert(ST->hasNEON() && "unexpected vector shift"); 3995 int64_t Cnt; 3996 3997 switch (N->getOpcode()) { 3998 default: 3999 llvm_unreachable("unexpected shift opcode"); 4000 4001 case ISD::SHL: 4002 if (isVShiftLImm(N->getOperand(1), VT, Cnt)) { 4003 SDValue RHS = 4004 DAG.getNode(AArch64ISD::NEON_VDUP, SDLoc(N->getOperand(1)), VT, 4005 DAG.getConstant(Cnt, MVT::i32)); 4006 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N->getOperand(0), RHS); 4007 } 4008 break; 4009 4010 case ISD::SRA: 4011 case ISD::SRL: 4012 if (isVShiftRImm(N->getOperand(1), VT, Cnt)) { 4013 SDValue RHS = 4014 DAG.getNode(AArch64ISD::NEON_VDUP, SDLoc(N->getOperand(1)), VT, 4015 DAG.getConstant(Cnt, MVT::i32)); 4016 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, N->getOperand(0), RHS); 4017 } 4018 break; 4019 } 4020 4021 return SDValue(); 4022 } 4023 4024 /// ARM-specific DAG combining for intrinsics. 4025 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) { 4026 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 4027 4028 switch (IntNo) { 4029 default: 4030 // Don't do anything for most intrinsics. 4031 break; 4032 4033 case Intrinsic::arm_neon_vqshifts: 4034 case Intrinsic::arm_neon_vqshiftu: 4035 EVT VT = N->getOperand(1).getValueType(); 4036 int64_t Cnt; 4037 if (!isVShiftLImm(N->getOperand(2), VT, Cnt)) 4038 break; 4039 unsigned VShiftOpc = (IntNo == Intrinsic::arm_neon_vqshifts) 4040 ? AArch64ISD::NEON_QSHLs 4041 : AArch64ISD::NEON_QSHLu; 4042 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0), 4043 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32)); 4044 } 4045 4046 return SDValue(); 4047 } 4048 4049 /// Target-specific DAG combine function for NEON load/store intrinsics 4050 /// to merge base address updates. 4051 static SDValue CombineBaseUpdate(SDNode *N, 4052 TargetLowering::DAGCombinerInfo &DCI) { 4053 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 4054 return SDValue(); 4055 4056 SelectionDAG &DAG = DCI.DAG; 4057 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID || 4058 N->getOpcode() == ISD::INTRINSIC_W_CHAIN); 4059 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1); 4060 SDValue Addr = N->getOperand(AddrOpIdx); 4061 4062 // Search for a use of the address operand that is an increment. 4063 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), 4064 UE = Addr.getNode()->use_end(); UI != UE; ++UI) { 4065 SDNode *User = *UI; 4066 if (User->getOpcode() != ISD::ADD || 4067 UI.getUse().getResNo() != Addr.getResNo()) 4068 continue; 4069 4070 // Check that the add is independent of the load/store. Otherwise, folding 4071 // it would create a cycle. 4072 if (User->isPredecessorOf(N) || N->isPredecessorOf(User)) 4073 continue; 4074 4075 // Find the new opcode for the updating load/store. 4076 bool isLoad = true; 4077 bool isLaneOp = false; 4078 unsigned NewOpc = 0; 4079 unsigned NumVecs = 0; 4080 if (isIntrinsic) { 4081 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 4082 switch (IntNo) { 4083 default: llvm_unreachable("unexpected intrinsic for Neon base update"); 4084 case Intrinsic::arm_neon_vld1: NewOpc = AArch64ISD::NEON_LD1_UPD; 4085 NumVecs = 1; break; 4086 case Intrinsic::arm_neon_vld2: NewOpc = AArch64ISD::NEON_LD2_UPD; 4087 NumVecs = 2; break; 4088 case Intrinsic::arm_neon_vld3: NewOpc = AArch64ISD::NEON_LD3_UPD; 4089 NumVecs = 3; break; 4090 case Intrinsic::arm_neon_vld4: NewOpc = AArch64ISD::NEON_LD4_UPD; 4091 NumVecs = 4; break; 4092 case Intrinsic::arm_neon_vst1: NewOpc = AArch64ISD::NEON_ST1_UPD; 4093 NumVecs = 1; isLoad = false; break; 4094 case Intrinsic::arm_neon_vst2: NewOpc = AArch64ISD::NEON_ST2_UPD; 4095 NumVecs = 2; isLoad = false; break; 4096 case Intrinsic::arm_neon_vst3: NewOpc = AArch64ISD::NEON_ST3_UPD; 4097 NumVecs = 3; isLoad = false; break; 4098 case Intrinsic::arm_neon_vst4: NewOpc = AArch64ISD::NEON_ST4_UPD; 4099 NumVecs = 4; isLoad = false; break; 4100 case Intrinsic::aarch64_neon_vld1x2: NewOpc = AArch64ISD::NEON_LD1x2_UPD; 4101 NumVecs = 2; break; 4102 case Intrinsic::aarch64_neon_vld1x3: NewOpc = AArch64ISD::NEON_LD1x3_UPD; 4103 NumVecs = 3; break; 4104 case Intrinsic::aarch64_neon_vld1x4: NewOpc = AArch64ISD::NEON_LD1x4_UPD; 4105 NumVecs = 4; break; 4106 case Intrinsic::aarch64_neon_vst1x2: NewOpc = AArch64ISD::NEON_ST1x2_UPD; 4107 NumVecs = 2; isLoad = false; break; 4108 case Intrinsic::aarch64_neon_vst1x3: NewOpc = AArch64ISD::NEON_ST1x3_UPD; 4109 NumVecs = 3; isLoad = false; break; 4110 case Intrinsic::aarch64_neon_vst1x4: NewOpc = AArch64ISD::NEON_ST1x4_UPD; 4111 NumVecs = 4; isLoad = false; break; 4112 case Intrinsic::arm_neon_vld2lane: NewOpc = AArch64ISD::NEON_LD2LN_UPD; 4113 NumVecs = 2; isLaneOp = true; break; 4114 case Intrinsic::arm_neon_vld3lane: NewOpc = AArch64ISD::NEON_LD3LN_UPD; 4115 NumVecs = 3; isLaneOp = true; break; 4116 case Intrinsic::arm_neon_vld4lane: NewOpc = AArch64ISD::NEON_LD4LN_UPD; 4117 NumVecs = 4; isLaneOp = true; break; 4118 case Intrinsic::arm_neon_vst2lane: NewOpc = AArch64ISD::NEON_ST2LN_UPD; 4119 NumVecs = 2; isLoad = false; isLaneOp = true; break; 4120 case Intrinsic::arm_neon_vst3lane: NewOpc = AArch64ISD::NEON_ST3LN_UPD; 4121 NumVecs = 3; isLoad = false; isLaneOp = true; break; 4122 case Intrinsic::arm_neon_vst4lane: NewOpc = AArch64ISD::NEON_ST4LN_UPD; 4123 NumVecs = 4; isLoad = false; isLaneOp = true; break; 4124 } 4125 } else { 4126 isLaneOp = true; 4127 switch (N->getOpcode()) { 4128 default: llvm_unreachable("unexpected opcode for Neon base update"); 4129 case AArch64ISD::NEON_LD2DUP: NewOpc = AArch64ISD::NEON_LD2DUP_UPD; 4130 NumVecs = 2; break; 4131 case AArch64ISD::NEON_LD3DUP: NewOpc = AArch64ISD::NEON_LD3DUP_UPD; 4132 NumVecs = 3; break; 4133 case AArch64ISD::NEON_LD4DUP: NewOpc = AArch64ISD::NEON_LD4DUP_UPD; 4134 NumVecs = 4; break; 4135 } 4136 } 4137 4138 // Find the size of memory referenced by the load/store. 4139 EVT VecTy; 4140 if (isLoad) 4141 VecTy = N->getValueType(0); 4142 else 4143 VecTy = N->getOperand(AddrOpIdx + 1).getValueType(); 4144 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8; 4145 if (isLaneOp) 4146 NumBytes /= VecTy.getVectorNumElements(); 4147 4148 // If the increment is a constant, it must match the memory ref size. 4149 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0); 4150 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) { 4151 uint32_t IncVal = CInc->getZExtValue(); 4152 if (IncVal != NumBytes) 4153 continue; 4154 Inc = DAG.getTargetConstant(IncVal, MVT::i32); 4155 } 4156 4157 // Create the new updating load/store node. 4158 EVT Tys[6]; 4159 unsigned NumResultVecs = (isLoad ? NumVecs : 0); 4160 unsigned n; 4161 for (n = 0; n < NumResultVecs; ++n) 4162 Tys[n] = VecTy; 4163 Tys[n++] = MVT::i64; 4164 Tys[n] = MVT::Other; 4165 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs + 2); 4166 SmallVector<SDValue, 8> Ops; 4167 Ops.push_back(N->getOperand(0)); // incoming chain 4168 Ops.push_back(N->getOperand(AddrOpIdx)); 4169 Ops.push_back(Inc); 4170 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) { 4171 Ops.push_back(N->getOperand(i)); 4172 } 4173 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N); 4174 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys, 4175 Ops.data(), Ops.size(), 4176 MemInt->getMemoryVT(), 4177 MemInt->getMemOperand()); 4178 4179 // Update the uses. 4180 std::vector<SDValue> NewResults; 4181 for (unsigned i = 0; i < NumResultVecs; ++i) { 4182 NewResults.push_back(SDValue(UpdN.getNode(), i)); 4183 } 4184 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs + 1)); // chain 4185 DCI.CombineTo(N, NewResults); 4186 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs)); 4187 4188 break; 4189 } 4190 return SDValue(); 4191 } 4192 4193 /// For a VDUPLANE node N, check if its source operand is a vldN-lane (N > 1) 4194 /// intrinsic, and if all the other uses of that intrinsic are also VDUPLANEs. 4195 /// If so, combine them to a vldN-dup operation and return true. 4196 static SDValue CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 4197 SelectionDAG &DAG = DCI.DAG; 4198 EVT VT = N->getValueType(0); 4199 4200 // Check if the VDUPLANE operand is a vldN-dup intrinsic. 4201 SDNode *VLD = N->getOperand(0).getNode(); 4202 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN) 4203 return SDValue(); 4204 unsigned NumVecs = 0; 4205 unsigned NewOpc = 0; 4206 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue(); 4207 if (IntNo == Intrinsic::arm_neon_vld2lane) { 4208 NumVecs = 2; 4209 NewOpc = AArch64ISD::NEON_LD2DUP; 4210 } else if (IntNo == Intrinsic::arm_neon_vld3lane) { 4211 NumVecs = 3; 4212 NewOpc = AArch64ISD::NEON_LD3DUP; 4213 } else if (IntNo == Intrinsic::arm_neon_vld4lane) { 4214 NumVecs = 4; 4215 NewOpc = AArch64ISD::NEON_LD4DUP; 4216 } else { 4217 return SDValue(); 4218 } 4219 4220 // First check that all the vldN-lane uses are VDUPLANEs and that the lane 4221 // numbers match the load. 4222 unsigned VLDLaneNo = 4223 cast<ConstantSDNode>(VLD->getOperand(NumVecs + 3))->getZExtValue(); 4224 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 4225 UI != UE; ++UI) { 4226 // Ignore uses of the chain result. 4227 if (UI.getUse().getResNo() == NumVecs) 4228 continue; 4229 SDNode *User = *UI; 4230 if (User->getOpcode() != AArch64ISD::NEON_VDUPLANE || 4231 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue()) 4232 return SDValue(); 4233 } 4234 4235 // Create the vldN-dup node. 4236 EVT Tys[5]; 4237 unsigned n; 4238 for (n = 0; n < NumVecs; ++n) 4239 Tys[n] = VT; 4240 Tys[n] = MVT::Other; 4241 SDVTList SDTys = DAG.getVTList(Tys, NumVecs + 1); 4242 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) }; 4243 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD); 4244 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys, Ops, 2, 4245 VLDMemInt->getMemoryVT(), 4246 VLDMemInt->getMemOperand()); 4247 4248 // Update the uses. 4249 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 4250 UI != UE; ++UI) { 4251 unsigned ResNo = UI.getUse().getResNo(); 4252 // Ignore uses of the chain result. 4253 if (ResNo == NumVecs) 4254 continue; 4255 SDNode *User = *UI; 4256 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo)); 4257 } 4258 4259 // Now the vldN-lane intrinsic is dead except for its chain result. 4260 // Update uses of the chain. 4261 std::vector<SDValue> VLDDupResults; 4262 for (unsigned n = 0; n < NumVecs; ++n) 4263 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n)); 4264 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs)); 4265 DCI.CombineTo(VLD, VLDDupResults); 4266 4267 return SDValue(N, 0); 4268 } 4269 4270 // v1i1 setcc -> 4271 // v1i1 (bitcast (i1 setcc (extract_vector_elt, extract_vector_elt)) 4272 // FIXME: Currently the type legalizer can't handle SETCC having v1i1 as result. 4273 // If it can legalize "v1i1 SETCC" correctly, no need to combine such SETCC. 4274 static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) { 4275 EVT ResVT = N->getValueType(0); 4276 4277 if (!ResVT.isVector() || ResVT.getVectorNumElements() != 1 || 4278 ResVT.getVectorElementType() != MVT::i1) 4279 return SDValue(); 4280 4281 SDValue LHS = N->getOperand(0); 4282 SDValue RHS = N->getOperand(1); 4283 EVT CmpVT = LHS.getValueType(); 4284 LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), 4285 CmpVT.getVectorElementType(), LHS, 4286 DAG.getConstant(0, MVT::i64)); 4287 RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), 4288 CmpVT.getVectorElementType(), RHS, 4289 DAG.getConstant(0, MVT::i64)); 4290 SDValue SetCC = 4291 DAG.getSetCC(SDLoc(N), MVT::i1, LHS, RHS, 4292 cast<CondCodeSDNode>(N->getOperand(2))->get()); 4293 return DAG.getNode(ISD::BITCAST, SDLoc(N), ResVT, SetCC); 4294 } 4295 4296 // vselect (v1i1 setcc) -> 4297 // vselect (v1iXX setcc) (XX is the size of the compared operand type) 4298 // FIXME: Currently the type legalizer can't handle VSELECT having v1i1 as 4299 // condition. If it can legalize "VSELECT v1i1" correctly, no need to combine 4300 // such VSELECT. 4301 static SDValue PerformVSelectCombine(SDNode *N, SelectionDAG &DAG) { 4302 SDValue N0 = N->getOperand(0); 4303 EVT CCVT = N0.getValueType(); 4304 4305 if (N0.getOpcode() != ISD::SETCC || CCVT.getVectorNumElements() != 1 || 4306 CCVT.getVectorElementType() != MVT::i1) 4307 return SDValue(); 4308 4309 EVT ResVT = N->getValueType(0); 4310 EVT CmpVT = N0.getOperand(0).getValueType(); 4311 // Only combine when the result type is of the same size as the compared 4312 // operands. 4313 if (ResVT.getSizeInBits() != CmpVT.getSizeInBits()) 4314 return SDValue(); 4315 4316 SDValue IfTrue = N->getOperand(1); 4317 SDValue IfFalse = N->getOperand(2); 4318 SDValue SetCC = 4319 DAG.getSetCC(SDLoc(N), CmpVT.changeVectorElementTypeToInteger(), 4320 N0.getOperand(0), N0.getOperand(1), 4321 cast<CondCodeSDNode>(N0.getOperand(2))->get()); 4322 return DAG.getNode(ISD::VSELECT, SDLoc(N), ResVT, SetCC, 4323 IfTrue, IfFalse); 4324 } 4325 4326 // sign_extend (extract_vector_elt (v1i1 setcc)) -> 4327 // extract_vector_elt (v1iXX setcc) 4328 // (XX is the size of the compared operand type) 4329 static SDValue PerformSignExtendCombine(SDNode *N, SelectionDAG &DAG) { 4330 SDValue N0 = N->getOperand(0); 4331 SDValue Vec = N0.getOperand(0); 4332 4333 if (N0.getOpcode() != ISD::EXTRACT_VECTOR_ELT || 4334 Vec.getOpcode() != ISD::SETCC) 4335 return SDValue(); 4336 4337 EVT ResVT = N->getValueType(0); 4338 EVT CmpVT = Vec.getOperand(0).getValueType(); 4339 // Only optimize when the result type is of the same size as the element 4340 // type of the compared operand. 4341 if (ResVT.getSizeInBits() != CmpVT.getVectorElementType().getSizeInBits()) 4342 return SDValue(); 4343 4344 SDValue Lane = N0.getOperand(1); 4345 SDValue SetCC = 4346 DAG.getSetCC(SDLoc(N), CmpVT.changeVectorElementTypeToInteger(), 4347 Vec.getOperand(0), Vec.getOperand(1), 4348 cast<CondCodeSDNode>(Vec.getOperand(2))->get()); 4349 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), ResVT, 4350 SetCC, Lane); 4351 } 4352 4353 SDValue 4354 AArch64TargetLowering::PerformDAGCombine(SDNode *N, 4355 DAGCombinerInfo &DCI) const { 4356 switch (N->getOpcode()) { 4357 default: break; 4358 case ISD::AND: return PerformANDCombine(N, DCI); 4359 case ISD::OR: return PerformORCombine(N, DCI, getSubtarget()); 4360 case ISD::SHL: 4361 case ISD::SRA: 4362 case ISD::SRL: 4363 return PerformShiftCombine(N, DCI, getSubtarget()); 4364 case ISD::SETCC: return PerformSETCCCombine(N, DCI.DAG); 4365 case ISD::VSELECT: return PerformVSelectCombine(N, DCI.DAG); 4366 case ISD::SIGN_EXTEND: return PerformSignExtendCombine(N, DCI.DAG); 4367 case ISD::INTRINSIC_WO_CHAIN: 4368 return PerformIntrinsicCombine(N, DCI.DAG); 4369 case AArch64ISD::NEON_VDUPLANE: 4370 return CombineVLDDUP(N, DCI); 4371 case AArch64ISD::NEON_LD2DUP: 4372 case AArch64ISD::NEON_LD3DUP: 4373 case AArch64ISD::NEON_LD4DUP: 4374 return CombineBaseUpdate(N, DCI); 4375 case ISD::INTRINSIC_VOID: 4376 case ISD::INTRINSIC_W_CHAIN: 4377 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 4378 case Intrinsic::arm_neon_vld1: 4379 case Intrinsic::arm_neon_vld2: 4380 case Intrinsic::arm_neon_vld3: 4381 case Intrinsic::arm_neon_vld4: 4382 case Intrinsic::arm_neon_vst1: 4383 case Intrinsic::arm_neon_vst2: 4384 case Intrinsic::arm_neon_vst3: 4385 case Intrinsic::arm_neon_vst4: 4386 case Intrinsic::arm_neon_vld2lane: 4387 case Intrinsic::arm_neon_vld3lane: 4388 case Intrinsic::arm_neon_vld4lane: 4389 case Intrinsic::aarch64_neon_vld1x2: 4390 case Intrinsic::aarch64_neon_vld1x3: 4391 case Intrinsic::aarch64_neon_vld1x4: 4392 case Intrinsic::aarch64_neon_vst1x2: 4393 case Intrinsic::aarch64_neon_vst1x3: 4394 case Intrinsic::aarch64_neon_vst1x4: 4395 case Intrinsic::arm_neon_vst2lane: 4396 case Intrinsic::arm_neon_vst3lane: 4397 case Intrinsic::arm_neon_vst4lane: 4398 return CombineBaseUpdate(N, DCI); 4399 default: 4400 break; 4401 } 4402 } 4403 return SDValue(); 4404 } 4405 4406 bool 4407 AArch64TargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { 4408 VT = VT.getScalarType(); 4409 4410 if (!VT.isSimple()) 4411 return false; 4412 4413 switch (VT.getSimpleVT().SimpleTy) { 4414 case MVT::f16: 4415 case MVT::f32: 4416 case MVT::f64: 4417 return true; 4418 case MVT::f128: 4419 return false; 4420 default: 4421 break; 4422 } 4423 4424 return false; 4425 } 4426 // Check whether a shuffle_vector could be presented as concat_vector. 4427 bool AArch64TargetLowering::isConcatVector(SDValue Op, SelectionDAG &DAG, 4428 SDValue V0, SDValue V1, 4429 const int *Mask, 4430 SDValue &Res) const { 4431 SDLoc DL(Op); 4432 EVT VT = Op.getValueType(); 4433 if (VT.getSizeInBits() != 128) 4434 return false; 4435 if (VT.getVectorElementType() != V0.getValueType().getVectorElementType() || 4436 VT.getVectorElementType() != V1.getValueType().getVectorElementType()) 4437 return false; 4438 4439 unsigned NumElts = VT.getVectorNumElements(); 4440 bool isContactVector = true; 4441 bool splitV0 = false; 4442 if (V0.getValueType().getSizeInBits() == 128) 4443 splitV0 = true; 4444 4445 for (int I = 0, E = NumElts / 2; I != E; I++) { 4446 if (Mask[I] != I) { 4447 isContactVector = false; 4448 break; 4449 } 4450 } 4451 4452 if (isContactVector) { 4453 int offset = NumElts / 2; 4454 for (int I = NumElts / 2, E = NumElts; I != E; I++) { 4455 if (Mask[I] != I + splitV0 * offset) { 4456 isContactVector = false; 4457 break; 4458 } 4459 } 4460 } 4461 4462 if (isContactVector) { 4463 EVT CastVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(), 4464 NumElts / 2); 4465 if (splitV0) { 4466 V0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, CastVT, V0, 4467 DAG.getConstant(0, MVT::i64)); 4468 } 4469 if (V1.getValueType().getSizeInBits() == 128) { 4470 V1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, CastVT, V1, 4471 DAG.getConstant(0, MVT::i64)); 4472 } 4473 Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, V0, V1); 4474 return true; 4475 } 4476 return false; 4477 } 4478 4479 // Check whether a Build Vector could be presented as Shuffle Vector. 4480 // This Shuffle Vector maybe not legalized, so the length of its operand and 4481 // the length of result may not equal. 4482 bool AArch64TargetLowering::isKnownShuffleVector(SDValue Op, SelectionDAG &DAG, 4483 SDValue &V0, SDValue &V1, 4484 int *Mask) const { 4485 SDLoc DL(Op); 4486 EVT VT = Op.getValueType(); 4487 unsigned NumElts = VT.getVectorNumElements(); 4488 unsigned V0NumElts = 0; 4489 4490 // Check if all elements are extracted from less than 3 vectors. 4491 for (unsigned i = 0; i < NumElts; ++i) { 4492 SDValue Elt = Op.getOperand(i); 4493 if (Elt.getOpcode() != ISD::EXTRACT_VECTOR_ELT || 4494 Elt.getOperand(0).getValueType().getVectorElementType() != 4495 VT.getVectorElementType()) 4496 return false; 4497 4498 if (V0.getNode() == 0) { 4499 V0 = Elt.getOperand(0); 4500 V0NumElts = V0.getValueType().getVectorNumElements(); 4501 } 4502 if (Elt.getOperand(0) == V0) { 4503 Mask[i] = (cast<ConstantSDNode>(Elt->getOperand(1))->getZExtValue()); 4504 continue; 4505 } else if (V1.getNode() == 0) { 4506 V1 = Elt.getOperand(0); 4507 } 4508 if (Elt.getOperand(0) == V1) { 4509 unsigned Lane = cast<ConstantSDNode>(Elt->getOperand(1))->getZExtValue(); 4510 Mask[i] = (Lane + V0NumElts); 4511 continue; 4512 } else { 4513 return false; 4514 } 4515 } 4516 return true; 4517 } 4518 4519 // If this is a case we can't handle, return null and let the default 4520 // expansion code take care of it. 4521 SDValue 4522 AArch64TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG, 4523 const AArch64Subtarget *ST) const { 4524 4525 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode()); 4526 SDLoc DL(Op); 4527 EVT VT = Op.getValueType(); 4528 4529 APInt SplatBits, SplatUndef; 4530 unsigned SplatBitSize; 4531 bool HasAnyUndefs; 4532 4533 unsigned UseNeonMov = VT.getSizeInBits() >= 64; 4534 4535 // Note we favor lowering MOVI over MVNI. 4536 // This has implications on the definition of patterns in TableGen to select 4537 // BIC immediate instructions but not ORR immediate instructions. 4538 // If this lowering order is changed, TableGen patterns for BIC immediate and 4539 // ORR immediate instructions have to be updated. 4540 if (UseNeonMov && 4541 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 4542 if (SplatBitSize <= 64) { 4543 // First attempt to use vector immediate-form MOVI 4544 EVT NeonMovVT; 4545 unsigned Imm = 0; 4546 unsigned OpCmode = 0; 4547 4548 if (isNeonModifiedImm(SplatBits.getZExtValue(), SplatUndef.getZExtValue(), 4549 SplatBitSize, DAG, VT.is128BitVector(), 4550 Neon_Mov_Imm, NeonMovVT, Imm, OpCmode)) { 4551 SDValue ImmVal = DAG.getTargetConstant(Imm, MVT::i32); 4552 SDValue OpCmodeVal = DAG.getConstant(OpCmode, MVT::i32); 4553 4554 if (ImmVal.getNode() && OpCmodeVal.getNode()) { 4555 SDValue NeonMov = DAG.getNode(AArch64ISD::NEON_MOVIMM, DL, NeonMovVT, 4556 ImmVal, OpCmodeVal); 4557 return DAG.getNode(ISD::BITCAST, DL, VT, NeonMov); 4558 } 4559 } 4560 4561 // Then attempt to use vector immediate-form MVNI 4562 uint64_t NegatedImm = (~SplatBits).getZExtValue(); 4563 if (isNeonModifiedImm(NegatedImm, SplatUndef.getZExtValue(), SplatBitSize, 4564 DAG, VT.is128BitVector(), Neon_Mvn_Imm, NeonMovVT, 4565 Imm, OpCmode)) { 4566 SDValue ImmVal = DAG.getTargetConstant(Imm, MVT::i32); 4567 SDValue OpCmodeVal = DAG.getConstant(OpCmode, MVT::i32); 4568 if (ImmVal.getNode() && OpCmodeVal.getNode()) { 4569 SDValue NeonMov = DAG.getNode(AArch64ISD::NEON_MVNIMM, DL, NeonMovVT, 4570 ImmVal, OpCmodeVal); 4571 return DAG.getNode(ISD::BITCAST, DL, VT, NeonMov); 4572 } 4573 } 4574 4575 // Attempt to use vector immediate-form FMOV 4576 if (((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) || 4577 (VT == MVT::v2f64 && SplatBitSize == 64)) { 4578 APFloat RealVal( 4579 SplatBitSize == 32 ? APFloat::IEEEsingle : APFloat::IEEEdouble, 4580 SplatBits); 4581 uint32_t ImmVal; 4582 if (A64Imms::isFPImm(RealVal, ImmVal)) { 4583 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32); 4584 return DAG.getNode(AArch64ISD::NEON_FMOVIMM, DL, VT, Val); 4585 } 4586 } 4587 } 4588 } 4589 4590 unsigned NumElts = VT.getVectorNumElements(); 4591 bool isOnlyLowElement = true; 4592 bool usesOnlyOneValue = true; 4593 bool hasDominantValue = false; 4594 bool isConstant = true; 4595 4596 // Map of the number of times a particular SDValue appears in the 4597 // element list. 4598 DenseMap<SDValue, unsigned> ValueCounts; 4599 SDValue Value; 4600 for (unsigned i = 0; i < NumElts; ++i) { 4601 SDValue V = Op.getOperand(i); 4602 if (V.getOpcode() == ISD::UNDEF) 4603 continue; 4604 if (i > 0) 4605 isOnlyLowElement = false; 4606 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 4607 isConstant = false; 4608 4609 ValueCounts.insert(std::make_pair(V, 0)); 4610 unsigned &Count = ValueCounts[V]; 4611 4612 // Is this value dominant? (takes up more than half of the lanes) 4613 if (++Count > (NumElts / 2)) { 4614 hasDominantValue = true; 4615 Value = V; 4616 } 4617 } 4618 if (ValueCounts.size() != 1) 4619 usesOnlyOneValue = false; 4620 if (!Value.getNode() && ValueCounts.size() > 0) 4621 Value = ValueCounts.begin()->first; 4622 4623 if (ValueCounts.size() == 0) 4624 return DAG.getUNDEF(VT); 4625 4626 if (isOnlyLowElement) 4627 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value); 4628 4629 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 4630 if (hasDominantValue && EltSize <= 64) { 4631 // Use VDUP for non-constant splats. 4632 if (!isConstant) { 4633 SDValue N; 4634 4635 // If we are DUPing a value that comes directly from a vector, we could 4636 // just use DUPLANE. We can only do this if the lane being extracted 4637 // is at a constant index, as the DUP from lane instructions only have 4638 // constant-index forms. 4639 // 4640 // If there is a TRUNCATE between EXTRACT_VECTOR_ELT and DUP, we can 4641 // remove TRUNCATE for DUPLANE by apdating the source vector to 4642 // appropriate vector type and lane index. 4643 // 4644 // FIXME: for now we have v1i8, v1i16, v1i32 legal vector types, if they 4645 // are not legal any more, no need to check the type size in bits should 4646 // be large than 64. 4647 SDValue V = Value; 4648 if (Value->getOpcode() == ISD::TRUNCATE) 4649 V = Value->getOperand(0); 4650 if (V->getOpcode() == ISD::EXTRACT_VECTOR_ELT && 4651 isa<ConstantSDNode>(V->getOperand(1)) && 4652 V->getOperand(0).getValueType().getSizeInBits() >= 64) { 4653 4654 // If the element size of source vector is larger than DUPLANE 4655 // element size, we can do transformation by, 4656 // 1) bitcasting source register to smaller element vector 4657 // 2) mutiplying the lane index by SrcEltSize/ResEltSize 4658 // For example, we can lower 4659 // "v8i16 vdup_lane(v4i32, 1)" 4660 // to be 4661 // "v8i16 vdup_lane(v8i16 bitcast(v4i32), 2)". 4662 SDValue SrcVec = V->getOperand(0); 4663 unsigned SrcEltSize = 4664 SrcVec.getValueType().getVectorElementType().getSizeInBits(); 4665 unsigned ResEltSize = VT.getVectorElementType().getSizeInBits(); 4666 if (SrcEltSize > ResEltSize) { 4667 assert((SrcEltSize % ResEltSize == 0) && "Invalid element size"); 4668 SDValue BitCast; 4669 unsigned SrcSize = SrcVec.getValueType().getSizeInBits(); 4670 unsigned ResSize = VT.getSizeInBits(); 4671 4672 if (SrcSize > ResSize) { 4673 assert((SrcSize % ResSize == 0) && "Invalid vector size"); 4674 EVT CastVT = 4675 EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(), 4676 SrcSize / ResEltSize); 4677 BitCast = DAG.getNode(ISD::BITCAST, DL, CastVT, SrcVec); 4678 } else { 4679 assert((SrcSize == ResSize) && "Invalid vector size of source vec"); 4680 BitCast = DAG.getNode(ISD::BITCAST, DL, VT, SrcVec); 4681 } 4682 4683 unsigned LaneIdx = V->getConstantOperandVal(1); 4684 SDValue Lane = 4685 DAG.getConstant((SrcEltSize / ResEltSize) * LaneIdx, MVT::i64); 4686 N = DAG.getNode(AArch64ISD::NEON_VDUPLANE, DL, VT, BitCast, Lane); 4687 } else { 4688 assert((SrcEltSize == ResEltSize) && 4689 "Invalid element size of source vec"); 4690 N = DAG.getNode(AArch64ISD::NEON_VDUPLANE, DL, VT, V->getOperand(0), 4691 V->getOperand(1)); 4692 } 4693 } else 4694 N = DAG.getNode(AArch64ISD::NEON_VDUP, DL, VT, Value); 4695 4696 if (!usesOnlyOneValue) { 4697 // The dominant value was splatted as 'N', but we now have to insert 4698 // all differing elements. 4699 for (unsigned I = 0; I < NumElts; ++I) { 4700 if (Op.getOperand(I) == Value) 4701 continue; 4702 SmallVector<SDValue, 3> Ops; 4703 Ops.push_back(N); 4704 Ops.push_back(Op.getOperand(I)); 4705 Ops.push_back(DAG.getConstant(I, MVT::i64)); 4706 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, &Ops[0], 3); 4707 } 4708 } 4709 return N; 4710 } 4711 if (usesOnlyOneValue && isConstant) { 4712 return DAG.getNode(AArch64ISD::NEON_VDUP, DL, VT, Value); 4713 } 4714 } 4715 // If all elements are constants and the case above didn't get hit, fall back 4716 // to the default expansion, which will generate a load from the constant 4717 // pool. 4718 if (isConstant) 4719 return SDValue(); 4720 4721 // Try to lower this in lowering ShuffleVector way. 4722 SDValue V0, V1; 4723 int Mask[16]; 4724 if (isKnownShuffleVector(Op, DAG, V0, V1, Mask)) { 4725 unsigned V0NumElts = V0.getValueType().getVectorNumElements(); 4726 if (!V1.getNode() && V0NumElts == NumElts * 2) { 4727 V1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V0, 4728 DAG.getConstant(NumElts, MVT::i64)); 4729 V0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V0, 4730 DAG.getConstant(0, MVT::i64)); 4731 V0NumElts = V0.getValueType().getVectorNumElements(); 4732 } 4733 4734 if (V1.getNode() && NumElts == V0NumElts && 4735 V0NumElts == V1.getValueType().getVectorNumElements()) { 4736 SDValue Shuffle = DAG.getVectorShuffle(VT, DL, V0, V1, Mask); 4737 if (Shuffle.getOpcode() != ISD::VECTOR_SHUFFLE) 4738 return Shuffle; 4739 else 4740 return LowerVECTOR_SHUFFLE(Shuffle, DAG); 4741 } else { 4742 SDValue Res; 4743 if (isConcatVector(Op, DAG, V0, V1, Mask, Res)) 4744 return Res; 4745 } 4746 } 4747 4748 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we 4749 // know the default expansion would otherwise fall back on something even 4750 // worse. For a vector with one or two non-undef values, that's 4751 // scalar_to_vector for the elements followed by a shuffle (provided the 4752 // shuffle is valid for the target) and materialization element by element 4753 // on the stack followed by a load for everything else. 4754 if (!isConstant && !usesOnlyOneValue) { 4755 SDValue Vec = DAG.getUNDEF(VT); 4756 for (unsigned i = 0 ; i < NumElts; ++i) { 4757 SDValue V = Op.getOperand(i); 4758 if (V.getOpcode() == ISD::UNDEF) 4759 continue; 4760 SDValue LaneIdx = DAG.getConstant(i, MVT::i64); 4761 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Vec, V, LaneIdx); 4762 } 4763 return Vec; 4764 } 4765 return SDValue(); 4766 } 4767 4768 /// isREVMask - Check if a vector shuffle corresponds to a REV 4769 /// instruction with the specified blocksize. (The order of the elements 4770 /// within each block of the vector is reversed.) 4771 static bool isREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) { 4772 assert((BlockSize == 16 || BlockSize == 32 || BlockSize == 64) && 4773 "Only possible block sizes for REV are: 16, 32, 64"); 4774 4775 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4776 if (EltSz == 64) 4777 return false; 4778 4779 unsigned NumElts = VT.getVectorNumElements(); 4780 unsigned BlockElts = M[0] + 1; 4781 // If the first shuffle index is UNDEF, be optimistic. 4782 if (M[0] < 0) 4783 BlockElts = BlockSize / EltSz; 4784 4785 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz) 4786 return false; 4787 4788 for (unsigned i = 0; i < NumElts; ++i) { 4789 if (M[i] < 0) 4790 continue; // ignore UNDEF indices 4791 if ((unsigned)M[i] != (i - i % BlockElts) + (BlockElts - 1 - i % BlockElts)) 4792 return false; 4793 } 4794 4795 return true; 4796 } 4797 4798 // isPermuteMask - Check whether the vector shuffle matches to UZP, ZIP and 4799 // TRN instruction. 4800 static unsigned isPermuteMask(ArrayRef<int> M, EVT VT, bool isV2undef) { 4801 unsigned NumElts = VT.getVectorNumElements(); 4802 if (NumElts < 4) 4803 return 0; 4804 4805 bool ismatch = true; 4806 4807 // Check UZP1 4808 for (unsigned i = 0; i < NumElts; ++i) { 4809 unsigned answer = i * 2; 4810 if (isV2undef && answer >= NumElts) 4811 answer -= NumElts; 4812 if (M[i] != -1 && (unsigned)M[i] != answer) { 4813 ismatch = false; 4814 break; 4815 } 4816 } 4817 if (ismatch) 4818 return AArch64ISD::NEON_UZP1; 4819 4820 // Check UZP2 4821 ismatch = true; 4822 for (unsigned i = 0; i < NumElts; ++i) { 4823 unsigned answer = i * 2 + 1; 4824 if (isV2undef && answer >= NumElts) 4825 answer -= NumElts; 4826 if (M[i] != -1 && (unsigned)M[i] != answer) { 4827 ismatch = false; 4828 break; 4829 } 4830 } 4831 if (ismatch) 4832 return AArch64ISD::NEON_UZP2; 4833 4834 // Check ZIP1 4835 ismatch = true; 4836 for (unsigned i = 0; i < NumElts; ++i) { 4837 unsigned answer = i / 2 + NumElts * (i % 2); 4838 if (isV2undef && answer >= NumElts) 4839 answer -= NumElts; 4840 if (M[i] != -1 && (unsigned)M[i] != answer) { 4841 ismatch = false; 4842 break; 4843 } 4844 } 4845 if (ismatch) 4846 return AArch64ISD::NEON_ZIP1; 4847 4848 // Check ZIP2 4849 ismatch = true; 4850 for (unsigned i = 0; i < NumElts; ++i) { 4851 unsigned answer = (NumElts + i) / 2 + NumElts * (i % 2); 4852 if (isV2undef && answer >= NumElts) 4853 answer -= NumElts; 4854 if (M[i] != -1 && (unsigned)M[i] != answer) { 4855 ismatch = false; 4856 break; 4857 } 4858 } 4859 if (ismatch) 4860 return AArch64ISD::NEON_ZIP2; 4861 4862 // Check TRN1 4863 ismatch = true; 4864 for (unsigned i = 0; i < NumElts; ++i) { 4865 unsigned answer = i + (NumElts - 1) * (i % 2); 4866 if (isV2undef && answer >= NumElts) 4867 answer -= NumElts; 4868 if (M[i] != -1 && (unsigned)M[i] != answer) { 4869 ismatch = false; 4870 break; 4871 } 4872 } 4873 if (ismatch) 4874 return AArch64ISD::NEON_TRN1; 4875 4876 // Check TRN2 4877 ismatch = true; 4878 for (unsigned i = 0; i < NumElts; ++i) { 4879 unsigned answer = 1 + i + (NumElts - 1) * (i % 2); 4880 if (isV2undef && answer >= NumElts) 4881 answer -= NumElts; 4882 if (M[i] != -1 && (unsigned)M[i] != answer) { 4883 ismatch = false; 4884 break; 4885 } 4886 } 4887 if (ismatch) 4888 return AArch64ISD::NEON_TRN2; 4889 4890 return 0; 4891 } 4892 4893 SDValue 4894 AArch64TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, 4895 SelectionDAG &DAG) const { 4896 SDValue V1 = Op.getOperand(0); 4897 SDValue V2 = Op.getOperand(1); 4898 SDLoc dl(Op); 4899 EVT VT = Op.getValueType(); 4900 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode()); 4901 4902 // Convert shuffles that are directly supported on NEON to target-specific 4903 // DAG nodes, instead of keeping them as shuffles and matching them again 4904 // during code selection. This is more efficient and avoids the possibility 4905 // of inconsistencies between legalization and selection. 4906 ArrayRef<int> ShuffleMask = SVN->getMask(); 4907 4908 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 4909 if (EltSize > 64) 4910 return SDValue(); 4911 4912 if (isREVMask(ShuffleMask, VT, 64)) 4913 return DAG.getNode(AArch64ISD::NEON_REV64, dl, VT, V1); 4914 if (isREVMask(ShuffleMask, VT, 32)) 4915 return DAG.getNode(AArch64ISD::NEON_REV32, dl, VT, V1); 4916 if (isREVMask(ShuffleMask, VT, 16)) 4917 return DAG.getNode(AArch64ISD::NEON_REV16, dl, VT, V1); 4918 4919 unsigned ISDNo; 4920 if (V2.getOpcode() == ISD::UNDEF) 4921 ISDNo = isPermuteMask(ShuffleMask, VT, true); 4922 else 4923 ISDNo = isPermuteMask(ShuffleMask, VT, false); 4924 4925 if (ISDNo) { 4926 if (V2.getOpcode() == ISD::UNDEF) 4927 return DAG.getNode(ISDNo, dl, VT, V1, V1); 4928 else 4929 return DAG.getNode(ISDNo, dl, VT, V1, V2); 4930 } 4931 4932 SDValue Res; 4933 if (isConcatVector(Op, DAG, V1, V2, &ShuffleMask[0], Res)) 4934 return Res; 4935 4936 // If the element of shuffle mask are all the same constant, we can 4937 // transform it into either NEON_VDUP or NEON_VDUPLANE 4938 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) { 4939 int Lane = SVN->getSplatIndex(); 4940 // If this is undef splat, generate it via "just" vdup, if possible. 4941 if (Lane == -1) Lane = 0; 4942 4943 // Test if V1 is a SCALAR_TO_VECTOR. 4944 if (V1.getOpcode() == ISD::SCALAR_TO_VECTOR) { 4945 return DAG.getNode(AArch64ISD::NEON_VDUP, dl, VT, V1.getOperand(0)); 4946 } 4947 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR. 4948 if (V1.getOpcode() == ISD::BUILD_VECTOR) { 4949 bool IsScalarToVector = true; 4950 for (unsigned i = 0, e = V1.getNumOperands(); i != e; ++i) 4951 if (V1.getOperand(i).getOpcode() != ISD::UNDEF && 4952 i != (unsigned)Lane) { 4953 IsScalarToVector = false; 4954 break; 4955 } 4956 if (IsScalarToVector) 4957 return DAG.getNode(AArch64ISD::NEON_VDUP, dl, VT, 4958 V1.getOperand(Lane)); 4959 } 4960 4961 // Test if V1 is a EXTRACT_SUBVECTOR. 4962 if (V1.getOpcode() == ISD::EXTRACT_SUBVECTOR) { 4963 int ExtLane = cast<ConstantSDNode>(V1.getOperand(1))->getZExtValue(); 4964 return DAG.getNode(AArch64ISD::NEON_VDUPLANE, dl, VT, V1.getOperand(0), 4965 DAG.getConstant(Lane + ExtLane, MVT::i64)); 4966 } 4967 // Test if V1 is a CONCAT_VECTORS. 4968 if (V1.getOpcode() == ISD::CONCAT_VECTORS && 4969 V1.getOperand(1).getOpcode() == ISD::UNDEF) { 4970 SDValue Op0 = V1.getOperand(0); 4971 assert((unsigned)Lane < Op0.getValueType().getVectorNumElements() && 4972 "Invalid vector lane access"); 4973 return DAG.getNode(AArch64ISD::NEON_VDUPLANE, dl, VT, Op0, 4974 DAG.getConstant(Lane, MVT::i64)); 4975 } 4976 4977 return DAG.getNode(AArch64ISD::NEON_VDUPLANE, dl, VT, V1, 4978 DAG.getConstant(Lane, MVT::i64)); 4979 } 4980 4981 int Length = ShuffleMask.size(); 4982 int V1EltNum = V1.getValueType().getVectorNumElements(); 4983 4984 // If the number of v1 elements is the same as the number of shuffle mask 4985 // element and the shuffle masks are sequential values, we can transform 4986 // it into NEON_VEXTRACT. 4987 if (V1EltNum == Length) { 4988 // Check if the shuffle mask is sequential. 4989 int SkipUndef = 0; 4990 while (ShuffleMask[SkipUndef] == -1) { 4991 SkipUndef++; 4992 } 4993 int CurMask = ShuffleMask[SkipUndef]; 4994 if (CurMask >= SkipUndef) { 4995 bool IsSequential = true; 4996 for (int I = SkipUndef; I < Length; ++I) { 4997 if (ShuffleMask[I] != -1 && ShuffleMask[I] != CurMask) { 4998 IsSequential = false; 4999 break; 5000 } 5001 CurMask++; 5002 } 5003 if (IsSequential) { 5004 assert((EltSize % 8 == 0) && "Bitsize of vector element is incorrect"); 5005 unsigned VecSize = EltSize * V1EltNum; 5006 unsigned Index = (EltSize / 8) * (ShuffleMask[SkipUndef] - SkipUndef); 5007 if (VecSize == 64 || VecSize == 128) 5008 return DAG.getNode(AArch64ISD::NEON_VEXTRACT, dl, VT, V1, V2, 5009 DAG.getConstant(Index, MVT::i64)); 5010 } 5011 } 5012 } 5013 5014 // For shuffle mask like "0, 1, 2, 3, 4, 5, 13, 7", try to generate insert 5015 // by element from V2 to V1 . 5016 // If shuffle mask is like "0, 1, 10, 11, 12, 13, 14, 15", V2 would be a 5017 // better choice to be inserted than V1 as less insert needed, so we count 5018 // element to be inserted for both V1 and V2, and select less one as insert 5019 // target. 5020 5021 // Collect elements need to be inserted and their index. 5022 SmallVector<int, 8> NV1Elt; 5023 SmallVector<int, 8> N1Index; 5024 SmallVector<int, 8> NV2Elt; 5025 SmallVector<int, 8> N2Index; 5026 for (int I = 0; I != Length; ++I) { 5027 if (ShuffleMask[I] != I) { 5028 NV1Elt.push_back(ShuffleMask[I]); 5029 N1Index.push_back(I); 5030 } 5031 } 5032 for (int I = 0; I != Length; ++I) { 5033 if (ShuffleMask[I] != (I + V1EltNum)) { 5034 NV2Elt.push_back(ShuffleMask[I]); 5035 N2Index.push_back(I); 5036 } 5037 } 5038 5039 // Decide which to be inserted. If all lanes mismatch, neither V1 nor V2 5040 // will be inserted. 5041 SDValue InsV = V1; 5042 SmallVector<int, 8> InsMasks = NV1Elt; 5043 SmallVector<int, 8> InsIndex = N1Index; 5044 if ((int)NV1Elt.size() != Length || (int)NV2Elt.size() != Length) { 5045 if (NV1Elt.size() > NV2Elt.size()) { 5046 InsV = V2; 5047 InsMasks = NV2Elt; 5048 InsIndex = N2Index; 5049 } 5050 } else { 5051 InsV = DAG.getNode(ISD::UNDEF, dl, VT); 5052 } 5053 5054 for (int I = 0, E = InsMasks.size(); I != E; ++I) { 5055 SDValue ExtV = V1; 5056 int Mask = InsMasks[I]; 5057 if (Mask >= V1EltNum) { 5058 ExtV = V2; 5059 Mask -= V1EltNum; 5060 } 5061 // Any value type smaller than i32 is illegal in AArch64, and this lower 5062 // function is called after legalize pass, so we need to legalize 5063 // the result here. 5064 EVT EltVT; 5065 if (VT.getVectorElementType().isFloatingPoint()) 5066 EltVT = (EltSize == 64) ? MVT::f64 : MVT::f32; 5067 else 5068 EltVT = (EltSize == 64) ? MVT::i64 : MVT::i32; 5069 5070 if (Mask >= 0) { 5071 ExtV = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, ExtV, 5072 DAG.getConstant(Mask, MVT::i64)); 5073 InsV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, InsV, ExtV, 5074 DAG.getConstant(InsIndex[I], MVT::i64)); 5075 } 5076 } 5077 return InsV; 5078 } 5079 5080 AArch64TargetLowering::ConstraintType 5081 AArch64TargetLowering::getConstraintType(const std::string &Constraint) const { 5082 if (Constraint.size() == 1) { 5083 switch (Constraint[0]) { 5084 default: break; 5085 case 'w': // An FP/SIMD vector register 5086 return C_RegisterClass; 5087 case 'I': // Constant that can be used with an ADD instruction 5088 case 'J': // Constant that can be used with a SUB instruction 5089 case 'K': // Constant that can be used with a 32-bit logical instruction 5090 case 'L': // Constant that can be used with a 64-bit logical instruction 5091 case 'M': // Constant that can be used as a 32-bit MOV immediate 5092 case 'N': // Constant that can be used as a 64-bit MOV immediate 5093 case 'Y': // Floating point constant zero 5094 case 'Z': // Integer constant zero 5095 return C_Other; 5096 case 'Q': // A memory reference with base register and no offset 5097 return C_Memory; 5098 case 'S': // A symbolic address 5099 return C_Other; 5100 } 5101 } 5102 5103 // FIXME: Ump, Utf, Usa, Ush 5104 // Ump: A memory address suitable for ldp/stp in SI, DI, SF and DF modes, 5105 // whatever they may be 5106 // Utf: A memory address suitable for ldp/stp in TF mode, whatever it may be 5107 // Usa: An absolute symbolic address 5108 // Ush: The high part (bits 32:12) of a pc-relative symbolic address 5109 assert(Constraint != "Ump" && Constraint != "Utf" && Constraint != "Usa" 5110 && Constraint != "Ush" && "Unimplemented constraints"); 5111 5112 return TargetLowering::getConstraintType(Constraint); 5113 } 5114 5115 TargetLowering::ConstraintWeight 5116 AArch64TargetLowering::getSingleConstraintMatchWeight(AsmOperandInfo &Info, 5117 const char *Constraint) const { 5118 5119 llvm_unreachable("Constraint weight unimplemented"); 5120 } 5121 5122 void 5123 AArch64TargetLowering::LowerAsmOperandForConstraint(SDValue Op, 5124 std::string &Constraint, 5125 std::vector<SDValue> &Ops, 5126 SelectionDAG &DAG) const { 5127 SDValue Result(0, 0); 5128 5129 // Only length 1 constraints are C_Other. 5130 if (Constraint.size() != 1) return; 5131 5132 // Only C_Other constraints get lowered like this. That means constants for us 5133 // so return early if there's no hope the constraint can be lowered. 5134 5135 switch(Constraint[0]) { 5136 default: break; 5137 case 'I': case 'J': case 'K': case 'L': 5138 case 'M': case 'N': case 'Z': { 5139 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 5140 if (!C) 5141 return; 5142 5143 uint64_t CVal = C->getZExtValue(); 5144 uint32_t Bits; 5145 5146 switch (Constraint[0]) { 5147 default: 5148 // FIXME: 'M' and 'N' are MOV pseudo-insts -- unsupported in assembly. 'J' 5149 // is a peculiarly useless SUB constraint. 5150 llvm_unreachable("Unimplemented C_Other constraint"); 5151 case 'I': 5152 if (CVal <= 0xfff) 5153 break; 5154 return; 5155 case 'K': 5156 if (A64Imms::isLogicalImm(32, CVal, Bits)) 5157 break; 5158 return; 5159 case 'L': 5160 if (A64Imms::isLogicalImm(64, CVal, Bits)) 5161 break; 5162 return; 5163 case 'Z': 5164 if (CVal == 0) 5165 break; 5166 return; 5167 } 5168 5169 Result = DAG.getTargetConstant(CVal, Op.getValueType()); 5170 break; 5171 } 5172 case 'S': { 5173 // An absolute symbolic address or label reference. 5174 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) { 5175 Result = DAG.getTargetGlobalAddress(GA->getGlobal(), SDLoc(Op), 5176 GA->getValueType(0)); 5177 } else if (const BlockAddressSDNode *BA 5178 = dyn_cast<BlockAddressSDNode>(Op)) { 5179 Result = DAG.getTargetBlockAddress(BA->getBlockAddress(), 5180 BA->getValueType(0)); 5181 } else if (const ExternalSymbolSDNode *ES 5182 = dyn_cast<ExternalSymbolSDNode>(Op)) { 5183 Result = DAG.getTargetExternalSymbol(ES->getSymbol(), 5184 ES->getValueType(0)); 5185 } else 5186 return; 5187 break; 5188 } 5189 case 'Y': 5190 if (const ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) { 5191 if (CFP->isExactlyValue(0.0)) { 5192 Result = DAG.getTargetConstantFP(0.0, CFP->getValueType(0)); 5193 break; 5194 } 5195 } 5196 return; 5197 } 5198 5199 if (Result.getNode()) { 5200 Ops.push_back(Result); 5201 return; 5202 } 5203 5204 // It's an unknown constraint for us. Let generic code have a go. 5205 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 5206 } 5207 5208 std::pair<unsigned, const TargetRegisterClass*> 5209 AArch64TargetLowering::getRegForInlineAsmConstraint( 5210 const std::string &Constraint, 5211 MVT VT) const { 5212 if (Constraint.size() == 1) { 5213 switch (Constraint[0]) { 5214 case 'r': 5215 if (VT.getSizeInBits() <= 32) 5216 return std::make_pair(0U, &AArch64::GPR32RegClass); 5217 else if (VT == MVT::i64) 5218 return std::make_pair(0U, &AArch64::GPR64RegClass); 5219 break; 5220 case 'w': 5221 if (VT == MVT::f16) 5222 return std::make_pair(0U, &AArch64::FPR16RegClass); 5223 else if (VT == MVT::f32) 5224 return std::make_pair(0U, &AArch64::FPR32RegClass); 5225 else if (VT.getSizeInBits() == 64) 5226 return std::make_pair(0U, &AArch64::FPR64RegClass); 5227 else if (VT.getSizeInBits() == 128) 5228 return std::make_pair(0U, &AArch64::FPR128RegClass); 5229 break; 5230 } 5231 } 5232 5233 // Use the default implementation in TargetLowering to convert the register 5234 // constraint into a member of a register class. 5235 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); 5236 } 5237 5238 /// Represent NEON load and store intrinsics as MemIntrinsicNodes. 5239 /// The associated MachineMemOperands record the alignment specified 5240 /// in the intrinsic calls. 5241 bool AArch64TargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 5242 const CallInst &I, 5243 unsigned Intrinsic) const { 5244 switch (Intrinsic) { 5245 case Intrinsic::arm_neon_vld1: 5246 case Intrinsic::arm_neon_vld2: 5247 case Intrinsic::arm_neon_vld3: 5248 case Intrinsic::arm_neon_vld4: 5249 case Intrinsic::aarch64_neon_vld1x2: 5250 case Intrinsic::aarch64_neon_vld1x3: 5251 case Intrinsic::aarch64_neon_vld1x4: 5252 case Intrinsic::arm_neon_vld2lane: 5253 case Intrinsic::arm_neon_vld3lane: 5254 case Intrinsic::arm_neon_vld4lane: { 5255 Info.opc = ISD::INTRINSIC_W_CHAIN; 5256 // Conservatively set memVT to the entire set of vectors loaded. 5257 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8; 5258 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 5259 Info.ptrVal = I.getArgOperand(0); 5260 Info.offset = 0; 5261 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 5262 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 5263 Info.vol = false; // volatile loads with NEON intrinsics not supported 5264 Info.readMem = true; 5265 Info.writeMem = false; 5266 return true; 5267 } 5268 case Intrinsic::arm_neon_vst1: 5269 case Intrinsic::arm_neon_vst2: 5270 case Intrinsic::arm_neon_vst3: 5271 case Intrinsic::arm_neon_vst4: 5272 case Intrinsic::aarch64_neon_vst1x2: 5273 case Intrinsic::aarch64_neon_vst1x3: 5274 case Intrinsic::aarch64_neon_vst1x4: 5275 case Intrinsic::arm_neon_vst2lane: 5276 case Intrinsic::arm_neon_vst3lane: 5277 case Intrinsic::arm_neon_vst4lane: { 5278 Info.opc = ISD::INTRINSIC_VOID; 5279 // Conservatively set memVT to the entire set of vectors stored. 5280 unsigned NumElts = 0; 5281 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) { 5282 Type *ArgTy = I.getArgOperand(ArgI)->getType(); 5283 if (!ArgTy->isVectorTy()) 5284 break; 5285 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8; 5286 } 5287 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 5288 Info.ptrVal = I.getArgOperand(0); 5289 Info.offset = 0; 5290 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 5291 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 5292 Info.vol = false; // volatile stores with NEON intrinsics not supported 5293 Info.readMem = false; 5294 Info.writeMem = true; 5295 return true; 5296 } 5297 default: 5298 break; 5299 } 5300 5301 return false; 5302 } 5303