1 //===-- AMDGPUISelLowering.cpp - AMDGPU Common DAG lowering functions -----===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 /// \file 10 /// This is the parent TargetLowering class for hardware code gen 11 /// targets. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "AMDGPUISelLowering.h" 16 #include "AMDGPU.h" 17 #include "AMDGPUInstrInfo.h" 18 #include "AMDGPUMachineFunction.h" 19 #include "GCNSubtarget.h" 20 #include "SIMachineFunctionInfo.h" 21 #include "llvm/CodeGen/Analysis.h" 22 #include "llvm/IR/DiagnosticInfo.h" 23 #include "llvm/IR/IntrinsicsAMDGPU.h" 24 #include "llvm/Support/CommandLine.h" 25 #include "llvm/Support/KnownBits.h" 26 #include "llvm/Target/TargetMachine.h" 27 28 using namespace llvm; 29 30 #include "AMDGPUGenCallingConv.inc" 31 32 static cl::opt<bool> AMDGPUBypassSlowDiv( 33 "amdgpu-bypass-slow-div", 34 cl::desc("Skip 64-bit divide for dynamic 32-bit values"), 35 cl::init(true)); 36 37 // Find a larger type to do a load / store of a vector with. 38 EVT AMDGPUTargetLowering::getEquivalentMemType(LLVMContext &Ctx, EVT VT) { 39 unsigned StoreSize = VT.getStoreSizeInBits(); 40 if (StoreSize <= 32) 41 return EVT::getIntegerVT(Ctx, StoreSize); 42 43 assert(StoreSize % 32 == 0 && "Store size not a multiple of 32"); 44 return EVT::getVectorVT(Ctx, MVT::i32, StoreSize / 32); 45 } 46 47 unsigned AMDGPUTargetLowering::numBitsUnsigned(SDValue Op, SelectionDAG &DAG) { 48 EVT VT = Op.getValueType(); 49 KnownBits Known = DAG.computeKnownBits(Op); 50 return VT.getSizeInBits() - Known.countMinLeadingZeros(); 51 } 52 53 unsigned AMDGPUTargetLowering::numBitsSigned(SDValue Op, SelectionDAG &DAG) { 54 EVT VT = Op.getValueType(); 55 56 // In order for this to be a signed 24-bit value, bit 23, must 57 // be a sign bit. 58 return VT.getSizeInBits() - DAG.ComputeNumSignBits(Op); 59 } 60 61 AMDGPUTargetLowering::AMDGPUTargetLowering(const TargetMachine &TM, 62 const AMDGPUSubtarget &STI) 63 : TargetLowering(TM), Subtarget(&STI) { 64 // Lower floating point store/load to integer store/load to reduce the number 65 // of patterns in tablegen. 66 setOperationAction(ISD::LOAD, MVT::f32, Promote); 67 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32); 68 69 setOperationAction(ISD::LOAD, MVT::v2f32, Promote); 70 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32); 71 72 setOperationAction(ISD::LOAD, MVT::v3f32, Promote); 73 AddPromotedToType(ISD::LOAD, MVT::v3f32, MVT::v3i32); 74 75 setOperationAction(ISD::LOAD, MVT::v4f32, Promote); 76 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32); 77 78 setOperationAction(ISD::LOAD, MVT::v5f32, Promote); 79 AddPromotedToType(ISD::LOAD, MVT::v5f32, MVT::v5i32); 80 81 setOperationAction(ISD::LOAD, MVT::v8f32, Promote); 82 AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32); 83 84 setOperationAction(ISD::LOAD, MVT::v16f32, Promote); 85 AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32); 86 87 setOperationAction(ISD::LOAD, MVT::v32f32, Promote); 88 AddPromotedToType(ISD::LOAD, MVT::v32f32, MVT::v32i32); 89 90 setOperationAction(ISD::LOAD, MVT::i64, Promote); 91 AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32); 92 93 setOperationAction(ISD::LOAD, MVT::v2i64, Promote); 94 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32); 95 96 setOperationAction(ISD::LOAD, MVT::f64, Promote); 97 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::v2i32); 98 99 setOperationAction(ISD::LOAD, MVT::v2f64, Promote); 100 AddPromotedToType(ISD::LOAD, MVT::v2f64, MVT::v4i32); 101 102 setOperationAction(ISD::LOAD, MVT::v4i64, Promote); 103 AddPromotedToType(ISD::LOAD, MVT::v4i64, MVT::v8i32); 104 105 setOperationAction(ISD::LOAD, MVT::v4f64, Promote); 106 AddPromotedToType(ISD::LOAD, MVT::v4f64, MVT::v8i32); 107 108 setOperationAction(ISD::LOAD, MVT::v8i64, Promote); 109 AddPromotedToType(ISD::LOAD, MVT::v8i64, MVT::v16i32); 110 111 setOperationAction(ISD::LOAD, MVT::v8f64, Promote); 112 AddPromotedToType(ISD::LOAD, MVT::v8f64, MVT::v16i32); 113 114 setOperationAction(ISD::LOAD, MVT::v16i64, Promote); 115 AddPromotedToType(ISD::LOAD, MVT::v16i64, MVT::v32i32); 116 117 setOperationAction(ISD::LOAD, MVT::v16f64, Promote); 118 AddPromotedToType(ISD::LOAD, MVT::v16f64, MVT::v32i32); 119 120 // There are no 64-bit extloads. These should be done as a 32-bit extload and 121 // an extension to 64-bit. 122 for (MVT VT : MVT::integer_valuetypes()) { 123 setLoadExtAction(ISD::EXTLOAD, MVT::i64, VT, Expand); 124 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, VT, Expand); 125 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, VT, Expand); 126 } 127 128 for (MVT VT : MVT::integer_valuetypes()) { 129 if (VT == MVT::i64) 130 continue; 131 132 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 133 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal); 134 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal); 135 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand); 136 137 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote); 138 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal); 139 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal); 140 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand); 141 142 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote); 143 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal); 144 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal); 145 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand); 146 } 147 148 for (MVT VT : MVT::integer_fixedlen_vector_valuetypes()) { 149 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i8, Expand); 150 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i8, Expand); 151 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i8, Expand); 152 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i8, Expand); 153 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i8, Expand); 154 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i8, Expand); 155 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i16, Expand); 156 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i16, Expand); 157 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i16, Expand); 158 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v3i16, Expand); 159 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v3i16, Expand); 160 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v3i16, Expand); 161 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i16, Expand); 162 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i16, Expand); 163 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i16, Expand); 164 } 165 166 setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand); 167 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, MVT::v2f16, Expand); 168 setLoadExtAction(ISD::EXTLOAD, MVT::v3f32, MVT::v3f16, Expand); 169 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, MVT::v4f16, Expand); 170 setLoadExtAction(ISD::EXTLOAD, MVT::v8f32, MVT::v8f16, Expand); 171 setLoadExtAction(ISD::EXTLOAD, MVT::v16f32, MVT::v16f16, Expand); 172 setLoadExtAction(ISD::EXTLOAD, MVT::v32f32, MVT::v32f16, Expand); 173 174 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand); 175 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand); 176 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Expand); 177 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f32, Expand); 178 setLoadExtAction(ISD::EXTLOAD, MVT::v16f64, MVT::v16f32, Expand); 179 180 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand); 181 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand); 182 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f16, Expand); 183 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f16, Expand); 184 setLoadExtAction(ISD::EXTLOAD, MVT::v16f64, MVT::v16f16, Expand); 185 186 setOperationAction(ISD::STORE, MVT::f32, Promote); 187 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32); 188 189 setOperationAction(ISD::STORE, MVT::v2f32, Promote); 190 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32); 191 192 setOperationAction(ISD::STORE, MVT::v3f32, Promote); 193 AddPromotedToType(ISD::STORE, MVT::v3f32, MVT::v3i32); 194 195 setOperationAction(ISD::STORE, MVT::v4f32, Promote); 196 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32); 197 198 setOperationAction(ISD::STORE, MVT::v5f32, Promote); 199 AddPromotedToType(ISD::STORE, MVT::v5f32, MVT::v5i32); 200 201 setOperationAction(ISD::STORE, MVT::v8f32, Promote); 202 AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32); 203 204 setOperationAction(ISD::STORE, MVT::v16f32, Promote); 205 AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32); 206 207 setOperationAction(ISD::STORE, MVT::v32f32, Promote); 208 AddPromotedToType(ISD::STORE, MVT::v32f32, MVT::v32i32); 209 210 setOperationAction(ISD::STORE, MVT::i64, Promote); 211 AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32); 212 213 setOperationAction(ISD::STORE, MVT::v2i64, Promote); 214 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32); 215 216 setOperationAction(ISD::STORE, MVT::f64, Promote); 217 AddPromotedToType(ISD::STORE, MVT::f64, MVT::v2i32); 218 219 setOperationAction(ISD::STORE, MVT::v2f64, Promote); 220 AddPromotedToType(ISD::STORE, MVT::v2f64, MVT::v4i32); 221 222 setOperationAction(ISD::STORE, MVT::v4i64, Promote); 223 AddPromotedToType(ISD::STORE, MVT::v4i64, MVT::v8i32); 224 225 setOperationAction(ISD::STORE, MVT::v4f64, Promote); 226 AddPromotedToType(ISD::STORE, MVT::v4f64, MVT::v8i32); 227 228 setOperationAction(ISD::STORE, MVT::v8i64, Promote); 229 AddPromotedToType(ISD::STORE, MVT::v8i64, MVT::v16i32); 230 231 setOperationAction(ISD::STORE, MVT::v8f64, Promote); 232 AddPromotedToType(ISD::STORE, MVT::v8f64, MVT::v16i32); 233 234 setOperationAction(ISD::STORE, MVT::v16i64, Promote); 235 AddPromotedToType(ISD::STORE, MVT::v16i64, MVT::v32i32); 236 237 setOperationAction(ISD::STORE, MVT::v16f64, Promote); 238 AddPromotedToType(ISD::STORE, MVT::v16f64, MVT::v32i32); 239 240 setTruncStoreAction(MVT::i64, MVT::i1, Expand); 241 setTruncStoreAction(MVT::i64, MVT::i8, Expand); 242 setTruncStoreAction(MVT::i64, MVT::i16, Expand); 243 setTruncStoreAction(MVT::i64, MVT::i32, Expand); 244 245 setTruncStoreAction(MVT::v2i64, MVT::v2i1, Expand); 246 setTruncStoreAction(MVT::v2i64, MVT::v2i8, Expand); 247 setTruncStoreAction(MVT::v2i64, MVT::v2i16, Expand); 248 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand); 249 250 setTruncStoreAction(MVT::f32, MVT::f16, Expand); 251 setTruncStoreAction(MVT::v2f32, MVT::v2f16, Expand); 252 setTruncStoreAction(MVT::v3f32, MVT::v3f16, Expand); 253 setTruncStoreAction(MVT::v4f32, MVT::v4f16, Expand); 254 setTruncStoreAction(MVT::v8f32, MVT::v8f16, Expand); 255 setTruncStoreAction(MVT::v16f32, MVT::v16f16, Expand); 256 setTruncStoreAction(MVT::v32f32, MVT::v32f16, Expand); 257 258 setTruncStoreAction(MVT::f64, MVT::f16, Expand); 259 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 260 261 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand); 262 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand); 263 264 setTruncStoreAction(MVT::v4i64, MVT::v4i32, Expand); 265 setTruncStoreAction(MVT::v4i64, MVT::v4i16, Expand); 266 setTruncStoreAction(MVT::v4f64, MVT::v4f32, Expand); 267 setTruncStoreAction(MVT::v4f64, MVT::v4f16, Expand); 268 269 setTruncStoreAction(MVT::v8f64, MVT::v8f32, Expand); 270 setTruncStoreAction(MVT::v8f64, MVT::v8f16, Expand); 271 272 setTruncStoreAction(MVT::v16f64, MVT::v16f32, Expand); 273 setTruncStoreAction(MVT::v16f64, MVT::v16f16, Expand); 274 setTruncStoreAction(MVT::v16i64, MVT::v16i16, Expand); 275 setTruncStoreAction(MVT::v16i64, MVT::v16i16, Expand); 276 setTruncStoreAction(MVT::v16i64, MVT::v16i8, Expand); 277 setTruncStoreAction(MVT::v16i64, MVT::v16i8, Expand); 278 setTruncStoreAction(MVT::v16i64, MVT::v16i1, Expand); 279 280 setOperationAction(ISD::Constant, MVT::i32, Legal); 281 setOperationAction(ISD::Constant, MVT::i64, Legal); 282 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); 283 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); 284 285 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 286 setOperationAction(ISD::BRIND, MVT::Other, Expand); 287 288 // This is totally unsupported, just custom lower to produce an error. 289 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom); 290 291 // Library functions. These default to Expand, but we have instructions 292 // for them. 293 setOperationAction(ISD::FCEIL, MVT::f32, Legal); 294 setOperationAction(ISD::FEXP2, MVT::f32, Legal); 295 setOperationAction(ISD::FPOW, MVT::f32, Legal); 296 setOperationAction(ISD::FLOG2, MVT::f32, Legal); 297 setOperationAction(ISD::FABS, MVT::f32, Legal); 298 setOperationAction(ISD::FFLOOR, MVT::f32, Legal); 299 setOperationAction(ISD::FRINT, MVT::f32, Legal); 300 setOperationAction(ISD::FTRUNC, MVT::f32, Legal); 301 setOperationAction(ISD::FMINNUM, MVT::f32, Legal); 302 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal); 303 304 setOperationAction(ISD::FROUND, MVT::f32, Custom); 305 setOperationAction(ISD::FROUND, MVT::f64, Custom); 306 307 setOperationAction(ISD::FLOG, MVT::f32, Custom); 308 setOperationAction(ISD::FLOG10, MVT::f32, Custom); 309 setOperationAction(ISD::FEXP, MVT::f32, Custom); 310 311 312 setOperationAction(ISD::FNEARBYINT, MVT::f32, Custom); 313 setOperationAction(ISD::FNEARBYINT, MVT::f64, Custom); 314 315 setOperationAction(ISD::FREM, MVT::f16, Custom); 316 setOperationAction(ISD::FREM, MVT::f32, Custom); 317 setOperationAction(ISD::FREM, MVT::f64, Custom); 318 319 // Expand to fneg + fadd. 320 setOperationAction(ISD::FSUB, MVT::f64, Expand); 321 322 setOperationAction(ISD::CONCAT_VECTORS, MVT::v3i32, Custom); 323 setOperationAction(ISD::CONCAT_VECTORS, MVT::v3f32, Custom); 324 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom); 325 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom); 326 setOperationAction(ISD::CONCAT_VECTORS, MVT::v5i32, Custom); 327 setOperationAction(ISD::CONCAT_VECTORS, MVT::v5f32, Custom); 328 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom); 329 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom); 330 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f16, Custom); 331 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i16, Custom); 332 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom); 333 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom); 334 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v3f32, Custom); 335 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v3i32, Custom); 336 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4f32, Custom); 337 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom); 338 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v5f32, Custom); 339 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v5i32, Custom); 340 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8f32, Custom); 341 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8i32, Custom); 342 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v16f32, Custom); 343 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v16i32, Custom); 344 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v32f32, Custom); 345 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v32i32, Custom); 346 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f64, Custom); 347 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i64, Custom); 348 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4f64, Custom); 349 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i64, Custom); 350 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8f64, Custom); 351 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8i64, Custom); 352 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v16f64, Custom); 353 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v16i64, Custom); 354 355 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand); 356 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Custom); 357 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Custom); 358 359 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 }; 360 for (MVT VT : ScalarIntVTs) { 361 // These should use [SU]DIVREM, so set them to expand 362 setOperationAction(ISD::SDIV, VT, Expand); 363 setOperationAction(ISD::UDIV, VT, Expand); 364 setOperationAction(ISD::SREM, VT, Expand); 365 setOperationAction(ISD::UREM, VT, Expand); 366 367 // GPU does not have divrem function for signed or unsigned. 368 setOperationAction(ISD::SDIVREM, VT, Custom); 369 setOperationAction(ISD::UDIVREM, VT, Custom); 370 371 // GPU does not have [S|U]MUL_LOHI functions as a single instruction. 372 setOperationAction(ISD::SMUL_LOHI, VT, Expand); 373 setOperationAction(ISD::UMUL_LOHI, VT, Expand); 374 375 setOperationAction(ISD::BSWAP, VT, Expand); 376 setOperationAction(ISD::CTTZ, VT, Expand); 377 setOperationAction(ISD::CTLZ, VT, Expand); 378 379 // AMDGPU uses ADDC/SUBC/ADDE/SUBE 380 setOperationAction(ISD::ADDC, VT, Legal); 381 setOperationAction(ISD::SUBC, VT, Legal); 382 setOperationAction(ISD::ADDE, VT, Legal); 383 setOperationAction(ISD::SUBE, VT, Legal); 384 } 385 386 // The hardware supports 32-bit FSHR, but not FSHL. 387 setOperationAction(ISD::FSHR, MVT::i32, Legal); 388 389 // The hardware supports 32-bit ROTR, but not ROTL. 390 setOperationAction(ISD::ROTL, MVT::i32, Expand); 391 setOperationAction(ISD::ROTL, MVT::i64, Expand); 392 setOperationAction(ISD::ROTR, MVT::i64, Expand); 393 394 setOperationAction(ISD::MULHU, MVT::i16, Expand); 395 setOperationAction(ISD::MULHS, MVT::i16, Expand); 396 397 setOperationAction(ISD::MUL, MVT::i64, Expand); 398 setOperationAction(ISD::MULHU, MVT::i64, Expand); 399 setOperationAction(ISD::MULHS, MVT::i64, Expand); 400 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom); 401 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); 402 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); 403 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom); 404 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand); 405 406 setOperationAction(ISD::SMIN, MVT::i32, Legal); 407 setOperationAction(ISD::UMIN, MVT::i32, Legal); 408 setOperationAction(ISD::SMAX, MVT::i32, Legal); 409 setOperationAction(ISD::UMAX, MVT::i32, Legal); 410 411 setOperationAction(ISD::CTTZ, MVT::i64, Custom); 412 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Custom); 413 setOperationAction(ISD::CTLZ, MVT::i64, Custom); 414 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom); 415 416 static const MVT::SimpleValueType VectorIntTypes[] = { 417 MVT::v2i32, MVT::v3i32, MVT::v4i32, MVT::v5i32 418 }; 419 420 for (MVT VT : VectorIntTypes) { 421 // Expand the following operations for the current type by default. 422 setOperationAction(ISD::ADD, VT, Expand); 423 setOperationAction(ISD::AND, VT, Expand); 424 setOperationAction(ISD::FP_TO_SINT, VT, Expand); 425 setOperationAction(ISD::FP_TO_UINT, VT, Expand); 426 setOperationAction(ISD::MUL, VT, Expand); 427 setOperationAction(ISD::MULHU, VT, Expand); 428 setOperationAction(ISD::MULHS, VT, Expand); 429 setOperationAction(ISD::OR, VT, Expand); 430 setOperationAction(ISD::SHL, VT, Expand); 431 setOperationAction(ISD::SRA, VT, Expand); 432 setOperationAction(ISD::SRL, VT, Expand); 433 setOperationAction(ISD::ROTL, VT, Expand); 434 setOperationAction(ISD::ROTR, VT, Expand); 435 setOperationAction(ISD::SUB, VT, Expand); 436 setOperationAction(ISD::SINT_TO_FP, VT, Expand); 437 setOperationAction(ISD::UINT_TO_FP, VT, Expand); 438 setOperationAction(ISD::SDIV, VT, Expand); 439 setOperationAction(ISD::UDIV, VT, Expand); 440 setOperationAction(ISD::SREM, VT, Expand); 441 setOperationAction(ISD::UREM, VT, Expand); 442 setOperationAction(ISD::SMUL_LOHI, VT, Expand); 443 setOperationAction(ISD::UMUL_LOHI, VT, Expand); 444 setOperationAction(ISD::SDIVREM, VT, Expand); 445 setOperationAction(ISD::UDIVREM, VT, Expand); 446 setOperationAction(ISD::SELECT, VT, Expand); 447 setOperationAction(ISD::VSELECT, VT, Expand); 448 setOperationAction(ISD::SELECT_CC, VT, Expand); 449 setOperationAction(ISD::XOR, VT, Expand); 450 setOperationAction(ISD::BSWAP, VT, Expand); 451 setOperationAction(ISD::CTPOP, VT, Expand); 452 setOperationAction(ISD::CTTZ, VT, Expand); 453 setOperationAction(ISD::CTLZ, VT, Expand); 454 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand); 455 setOperationAction(ISD::SETCC, VT, Expand); 456 } 457 458 static const MVT::SimpleValueType FloatVectorTypes[] = { 459 MVT::v2f32, MVT::v3f32, MVT::v4f32, MVT::v5f32 460 }; 461 462 for (MVT VT : FloatVectorTypes) { 463 setOperationAction(ISD::FABS, VT, Expand); 464 setOperationAction(ISD::FMINNUM, VT, Expand); 465 setOperationAction(ISD::FMAXNUM, VT, Expand); 466 setOperationAction(ISD::FADD, VT, Expand); 467 setOperationAction(ISD::FCEIL, VT, Expand); 468 setOperationAction(ISD::FCOS, VT, Expand); 469 setOperationAction(ISD::FDIV, VT, Expand); 470 setOperationAction(ISD::FEXP2, VT, Expand); 471 setOperationAction(ISD::FEXP, VT, Expand); 472 setOperationAction(ISD::FLOG2, VT, Expand); 473 setOperationAction(ISD::FREM, VT, Expand); 474 setOperationAction(ISD::FLOG, VT, Expand); 475 setOperationAction(ISD::FLOG10, VT, Expand); 476 setOperationAction(ISD::FPOW, VT, Expand); 477 setOperationAction(ISD::FFLOOR, VT, Expand); 478 setOperationAction(ISD::FTRUNC, VT, Expand); 479 setOperationAction(ISD::FMUL, VT, Expand); 480 setOperationAction(ISD::FMA, VT, Expand); 481 setOperationAction(ISD::FRINT, VT, Expand); 482 setOperationAction(ISD::FNEARBYINT, VT, Expand); 483 setOperationAction(ISD::FSQRT, VT, Expand); 484 setOperationAction(ISD::FSIN, VT, Expand); 485 setOperationAction(ISD::FSUB, VT, Expand); 486 setOperationAction(ISD::FNEG, VT, Expand); 487 setOperationAction(ISD::VSELECT, VT, Expand); 488 setOperationAction(ISD::SELECT_CC, VT, Expand); 489 setOperationAction(ISD::FCOPYSIGN, VT, Expand); 490 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand); 491 setOperationAction(ISD::SETCC, VT, Expand); 492 setOperationAction(ISD::FCANONICALIZE, VT, Expand); 493 } 494 495 // This causes using an unrolled select operation rather than expansion with 496 // bit operations. This is in general better, but the alternative using BFI 497 // instructions may be better if the select sources are SGPRs. 498 setOperationAction(ISD::SELECT, MVT::v2f32, Promote); 499 AddPromotedToType(ISD::SELECT, MVT::v2f32, MVT::v2i32); 500 501 setOperationAction(ISD::SELECT, MVT::v3f32, Promote); 502 AddPromotedToType(ISD::SELECT, MVT::v3f32, MVT::v3i32); 503 504 setOperationAction(ISD::SELECT, MVT::v4f32, Promote); 505 AddPromotedToType(ISD::SELECT, MVT::v4f32, MVT::v4i32); 506 507 setOperationAction(ISD::SELECT, MVT::v5f32, Promote); 508 AddPromotedToType(ISD::SELECT, MVT::v5f32, MVT::v5i32); 509 510 // There are no libcalls of any kind. 511 for (int I = 0; I < RTLIB::UNKNOWN_LIBCALL; ++I) 512 setLibcallName(static_cast<RTLIB::Libcall>(I), nullptr); 513 514 setSchedulingPreference(Sched::RegPressure); 515 setJumpIsExpensive(true); 516 517 // FIXME: This is only partially true. If we have to do vector compares, any 518 // SGPR pair can be a condition register. If we have a uniform condition, we 519 // are better off doing SALU operations, where there is only one SCC. For now, 520 // we don't have a way of knowing during instruction selection if a condition 521 // will be uniform and we always use vector compares. Assume we are using 522 // vector compares until that is fixed. 523 setHasMultipleConditionRegisters(true); 524 525 setMinCmpXchgSizeInBits(32); 526 setSupportsUnalignedAtomics(false); 527 528 PredictableSelectIsExpensive = false; 529 530 // We want to find all load dependencies for long chains of stores to enable 531 // merging into very wide vectors. The problem is with vectors with > 4 532 // elements. MergeConsecutiveStores will attempt to merge these because x8/x16 533 // vectors are a legal type, even though we have to split the loads 534 // usually. When we can more precisely specify load legality per address 535 // space, we should be able to make FindBetterChain/MergeConsecutiveStores 536 // smarter so that they can figure out what to do in 2 iterations without all 537 // N > 4 stores on the same chain. 538 GatherAllAliasesMaxDepth = 16; 539 540 // memcpy/memmove/memset are expanded in the IR, so we shouldn't need to worry 541 // about these during lowering. 542 MaxStoresPerMemcpy = 0xffffffff; 543 MaxStoresPerMemmove = 0xffffffff; 544 MaxStoresPerMemset = 0xffffffff; 545 546 // The expansion for 64-bit division is enormous. 547 if (AMDGPUBypassSlowDiv) 548 addBypassSlowDiv(64, 32); 549 550 setTargetDAGCombine(ISD::BITCAST); 551 setTargetDAGCombine(ISD::SHL); 552 setTargetDAGCombine(ISD::SRA); 553 setTargetDAGCombine(ISD::SRL); 554 setTargetDAGCombine(ISD::TRUNCATE); 555 setTargetDAGCombine(ISD::MUL); 556 setTargetDAGCombine(ISD::MULHU); 557 setTargetDAGCombine(ISD::MULHS); 558 setTargetDAGCombine(ISD::SELECT); 559 setTargetDAGCombine(ISD::SELECT_CC); 560 setTargetDAGCombine(ISD::STORE); 561 setTargetDAGCombine(ISD::FADD); 562 setTargetDAGCombine(ISD::FSUB); 563 setTargetDAGCombine(ISD::FNEG); 564 setTargetDAGCombine(ISD::FABS); 565 setTargetDAGCombine(ISD::AssertZext); 566 setTargetDAGCombine(ISD::AssertSext); 567 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); 568 } 569 570 bool AMDGPUTargetLowering::mayIgnoreSignedZero(SDValue Op) const { 571 if (getTargetMachine().Options.NoSignedZerosFPMath) 572 return true; 573 574 const auto Flags = Op.getNode()->getFlags(); 575 if (Flags.hasNoSignedZeros()) 576 return true; 577 578 return false; 579 } 580 581 //===----------------------------------------------------------------------===// 582 // Target Information 583 //===----------------------------------------------------------------------===// 584 585 LLVM_READNONE 586 static bool fnegFoldsIntoOp(unsigned Opc) { 587 switch (Opc) { 588 case ISD::FADD: 589 case ISD::FSUB: 590 case ISD::FMUL: 591 case ISD::FMA: 592 case ISD::FMAD: 593 case ISD::FMINNUM: 594 case ISD::FMAXNUM: 595 case ISD::FMINNUM_IEEE: 596 case ISD::FMAXNUM_IEEE: 597 case ISD::FSIN: 598 case ISD::FTRUNC: 599 case ISD::FRINT: 600 case ISD::FNEARBYINT: 601 case ISD::FCANONICALIZE: 602 case AMDGPUISD::RCP: 603 case AMDGPUISD::RCP_LEGACY: 604 case AMDGPUISD::RCP_IFLAG: 605 case AMDGPUISD::SIN_HW: 606 case AMDGPUISD::FMUL_LEGACY: 607 case AMDGPUISD::FMIN_LEGACY: 608 case AMDGPUISD::FMAX_LEGACY: 609 case AMDGPUISD::FMED3: 610 // TODO: handle llvm.amdgcn.fma.legacy 611 return true; 612 default: 613 return false; 614 } 615 } 616 617 /// \p returns true if the operation will definitely need to use a 64-bit 618 /// encoding, and thus will use a VOP3 encoding regardless of the source 619 /// modifiers. 620 LLVM_READONLY 621 static bool opMustUseVOP3Encoding(const SDNode *N, MVT VT) { 622 return N->getNumOperands() > 2 || VT == MVT::f64; 623 } 624 625 // Most FP instructions support source modifiers, but this could be refined 626 // slightly. 627 LLVM_READONLY 628 static bool hasSourceMods(const SDNode *N) { 629 if (isa<MemSDNode>(N)) 630 return false; 631 632 switch (N->getOpcode()) { 633 case ISD::CopyToReg: 634 case ISD::SELECT: 635 case ISD::FDIV: 636 case ISD::FREM: 637 case ISD::INLINEASM: 638 case ISD::INLINEASM_BR: 639 case AMDGPUISD::DIV_SCALE: 640 case ISD::INTRINSIC_W_CHAIN: 641 642 // TODO: Should really be looking at the users of the bitcast. These are 643 // problematic because bitcasts are used to legalize all stores to integer 644 // types. 645 case ISD::BITCAST: 646 return false; 647 case ISD::INTRINSIC_WO_CHAIN: { 648 switch (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue()) { 649 case Intrinsic::amdgcn_interp_p1: 650 case Intrinsic::amdgcn_interp_p2: 651 case Intrinsic::amdgcn_interp_mov: 652 case Intrinsic::amdgcn_interp_p1_f16: 653 case Intrinsic::amdgcn_interp_p2_f16: 654 return false; 655 default: 656 return true; 657 } 658 } 659 default: 660 return true; 661 } 662 } 663 664 bool AMDGPUTargetLowering::allUsesHaveSourceMods(const SDNode *N, 665 unsigned CostThreshold) { 666 // Some users (such as 3-operand FMA/MAD) must use a VOP3 encoding, and thus 667 // it is truly free to use a source modifier in all cases. If there are 668 // multiple users but for each one will necessitate using VOP3, there will be 669 // a code size increase. Try to avoid increasing code size unless we know it 670 // will save on the instruction count. 671 unsigned NumMayIncreaseSize = 0; 672 MVT VT = N->getValueType(0).getScalarType().getSimpleVT(); 673 674 // XXX - Should this limit number of uses to check? 675 for (const SDNode *U : N->uses()) { 676 if (!hasSourceMods(U)) 677 return false; 678 679 if (!opMustUseVOP3Encoding(U, VT)) { 680 if (++NumMayIncreaseSize > CostThreshold) 681 return false; 682 } 683 } 684 685 return true; 686 } 687 688 EVT AMDGPUTargetLowering::getTypeForExtReturn(LLVMContext &Context, EVT VT, 689 ISD::NodeType ExtendKind) const { 690 assert(!VT.isVector() && "only scalar expected"); 691 692 // Round to the next multiple of 32-bits. 693 unsigned Size = VT.getSizeInBits(); 694 if (Size <= 32) 695 return MVT::i32; 696 return EVT::getIntegerVT(Context, 32 * ((Size + 31) / 32)); 697 } 698 699 MVT AMDGPUTargetLowering::getVectorIdxTy(const DataLayout &) const { 700 return MVT::i32; 701 } 702 703 bool AMDGPUTargetLowering::isSelectSupported(SelectSupportKind SelType) const { 704 return true; 705 } 706 707 // The backend supports 32 and 64 bit floating point immediates. 708 // FIXME: Why are we reporting vectors of FP immediates as legal? 709 bool AMDGPUTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT, 710 bool ForCodeSize) const { 711 EVT ScalarVT = VT.getScalarType(); 712 return (ScalarVT == MVT::f32 || ScalarVT == MVT::f64 || 713 (ScalarVT == MVT::f16 && Subtarget->has16BitInsts())); 714 } 715 716 // We don't want to shrink f64 / f32 constants. 717 bool AMDGPUTargetLowering::ShouldShrinkFPConstant(EVT VT) const { 718 EVT ScalarVT = VT.getScalarType(); 719 return (ScalarVT != MVT::f32 && ScalarVT != MVT::f64); 720 } 721 722 bool AMDGPUTargetLowering::shouldReduceLoadWidth(SDNode *N, 723 ISD::LoadExtType ExtTy, 724 EVT NewVT) const { 725 // TODO: This may be worth removing. Check regression tests for diffs. 726 if (!TargetLoweringBase::shouldReduceLoadWidth(N, ExtTy, NewVT)) 727 return false; 728 729 unsigned NewSize = NewVT.getStoreSizeInBits(); 730 731 // If we are reducing to a 32-bit load or a smaller multi-dword load, 732 // this is always better. 733 if (NewSize >= 32) 734 return true; 735 736 EVT OldVT = N->getValueType(0); 737 unsigned OldSize = OldVT.getStoreSizeInBits(); 738 739 MemSDNode *MN = cast<MemSDNode>(N); 740 unsigned AS = MN->getAddressSpace(); 741 // Do not shrink an aligned scalar load to sub-dword. 742 // Scalar engine cannot do sub-dword loads. 743 if (OldSize >= 32 && NewSize < 32 && MN->getAlignment() >= 4 && 744 (AS == AMDGPUAS::CONSTANT_ADDRESS || 745 AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT || 746 (isa<LoadSDNode>(N) && 747 AS == AMDGPUAS::GLOBAL_ADDRESS && MN->isInvariant())) && 748 AMDGPUInstrInfo::isUniformMMO(MN->getMemOperand())) 749 return false; 750 751 // Don't produce extloads from sub 32-bit types. SI doesn't have scalar 752 // extloads, so doing one requires using a buffer_load. In cases where we 753 // still couldn't use a scalar load, using the wider load shouldn't really 754 // hurt anything. 755 756 // If the old size already had to be an extload, there's no harm in continuing 757 // to reduce the width. 758 return (OldSize < 32); 759 } 760 761 bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy, EVT CastTy, 762 const SelectionDAG &DAG, 763 const MachineMemOperand &MMO) const { 764 765 assert(LoadTy.getSizeInBits() == CastTy.getSizeInBits()); 766 767 if (LoadTy.getScalarType() == MVT::i32) 768 return false; 769 770 unsigned LScalarSize = LoadTy.getScalarSizeInBits(); 771 unsigned CastScalarSize = CastTy.getScalarSizeInBits(); 772 773 if ((LScalarSize >= CastScalarSize) && (CastScalarSize < 32)) 774 return false; 775 776 bool Fast = false; 777 return allowsMemoryAccessForAlignment(*DAG.getContext(), DAG.getDataLayout(), 778 CastTy, MMO, &Fast) && 779 Fast; 780 } 781 782 // SI+ has instructions for cttz / ctlz for 32-bit values. This is probably also 783 // profitable with the expansion for 64-bit since it's generally good to 784 // speculate things. 785 // FIXME: These should really have the size as a parameter. 786 bool AMDGPUTargetLowering::isCheapToSpeculateCttz() const { 787 return true; 788 } 789 790 bool AMDGPUTargetLowering::isCheapToSpeculateCtlz() const { 791 return true; 792 } 793 794 bool AMDGPUTargetLowering::isSDNodeAlwaysUniform(const SDNode *N) const { 795 switch (N->getOpcode()) { 796 case ISD::EntryToken: 797 case ISD::TokenFactor: 798 return true; 799 case ISD::INTRINSIC_WO_CHAIN: { 800 unsigned IntrID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 801 switch (IntrID) { 802 case Intrinsic::amdgcn_readfirstlane: 803 case Intrinsic::amdgcn_readlane: 804 return true; 805 } 806 return false; 807 } 808 case ISD::LOAD: 809 if (cast<LoadSDNode>(N)->getMemOperand()->getAddrSpace() == 810 AMDGPUAS::CONSTANT_ADDRESS_32BIT) 811 return true; 812 return false; 813 } 814 return false; 815 } 816 817 SDValue AMDGPUTargetLowering::getNegatedExpression( 818 SDValue Op, SelectionDAG &DAG, bool LegalOperations, bool ForCodeSize, 819 NegatibleCost &Cost, unsigned Depth) const { 820 821 switch (Op.getOpcode()) { 822 case ISD::FMA: 823 case ISD::FMAD: { 824 // Negating a fma is not free if it has users without source mods. 825 if (!allUsesHaveSourceMods(Op.getNode())) 826 return SDValue(); 827 break; 828 } 829 default: 830 break; 831 } 832 833 return TargetLowering::getNegatedExpression(Op, DAG, LegalOperations, 834 ForCodeSize, Cost, Depth); 835 } 836 837 //===---------------------------------------------------------------------===// 838 // Target Properties 839 //===---------------------------------------------------------------------===// 840 841 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const { 842 assert(VT.isFloatingPoint()); 843 844 // Packed operations do not have a fabs modifier. 845 return VT == MVT::f32 || VT == MVT::f64 || 846 (Subtarget->has16BitInsts() && VT == MVT::f16); 847 } 848 849 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const { 850 assert(VT.isFloatingPoint()); 851 return VT == MVT::f32 || VT == MVT::f64 || 852 (Subtarget->has16BitInsts() && VT == MVT::f16) || 853 (Subtarget->hasVOP3PInsts() && VT == MVT::v2f16); 854 } 855 856 bool AMDGPUTargetLowering:: storeOfVectorConstantIsCheap(EVT MemVT, 857 unsigned NumElem, 858 unsigned AS) const { 859 return true; 860 } 861 862 bool AMDGPUTargetLowering::aggressivelyPreferBuildVectorSources(EVT VecVT) const { 863 // There are few operations which truly have vector input operands. Any vector 864 // operation is going to involve operations on each component, and a 865 // build_vector will be a copy per element, so it always makes sense to use a 866 // build_vector input in place of the extracted element to avoid a copy into a 867 // super register. 868 // 869 // We should probably only do this if all users are extracts only, but this 870 // should be the common case. 871 return true; 872 } 873 874 bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const { 875 // Truncate is just accessing a subregister. 876 877 unsigned SrcSize = Source.getSizeInBits(); 878 unsigned DestSize = Dest.getSizeInBits(); 879 880 return DestSize < SrcSize && DestSize % 32 == 0 ; 881 } 882 883 bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const { 884 // Truncate is just accessing a subregister. 885 886 unsigned SrcSize = Source->getScalarSizeInBits(); 887 unsigned DestSize = Dest->getScalarSizeInBits(); 888 889 if (DestSize== 16 && Subtarget->has16BitInsts()) 890 return SrcSize >= 32; 891 892 return DestSize < SrcSize && DestSize % 32 == 0; 893 } 894 895 bool AMDGPUTargetLowering::isZExtFree(Type *Src, Type *Dest) const { 896 unsigned SrcSize = Src->getScalarSizeInBits(); 897 unsigned DestSize = Dest->getScalarSizeInBits(); 898 899 if (SrcSize == 16 && Subtarget->has16BitInsts()) 900 return DestSize >= 32; 901 902 return SrcSize == 32 && DestSize == 64; 903 } 904 905 bool AMDGPUTargetLowering::isZExtFree(EVT Src, EVT Dest) const { 906 // Any register load of a 64-bit value really requires 2 32-bit moves. For all 907 // practical purposes, the extra mov 0 to load a 64-bit is free. As used, 908 // this will enable reducing 64-bit operations the 32-bit, which is always 909 // good. 910 911 if (Src == MVT::i16) 912 return Dest == MVT::i32 ||Dest == MVT::i64 ; 913 914 return Src == MVT::i32 && Dest == MVT::i64; 915 } 916 917 bool AMDGPUTargetLowering::isZExtFree(SDValue Val, EVT VT2) const { 918 return isZExtFree(Val.getValueType(), VT2); 919 } 920 921 bool AMDGPUTargetLowering::isNarrowingProfitable(EVT SrcVT, EVT DestVT) const { 922 // There aren't really 64-bit registers, but pairs of 32-bit ones and only a 923 // limited number of native 64-bit operations. Shrinking an operation to fit 924 // in a single 32-bit register should always be helpful. As currently used, 925 // this is much less general than the name suggests, and is only used in 926 // places trying to reduce the sizes of loads. Shrinking loads to < 32-bits is 927 // not profitable, and may actually be harmful. 928 return SrcVT.getSizeInBits() > 32 && DestVT.getSizeInBits() == 32; 929 } 930 931 //===---------------------------------------------------------------------===// 932 // TargetLowering Callbacks 933 //===---------------------------------------------------------------------===// 934 935 CCAssignFn *AMDGPUCallLowering::CCAssignFnForCall(CallingConv::ID CC, 936 bool IsVarArg) { 937 switch (CC) { 938 case CallingConv::AMDGPU_VS: 939 case CallingConv::AMDGPU_GS: 940 case CallingConv::AMDGPU_PS: 941 case CallingConv::AMDGPU_CS: 942 case CallingConv::AMDGPU_HS: 943 case CallingConv::AMDGPU_ES: 944 case CallingConv::AMDGPU_LS: 945 return CC_AMDGPU; 946 case CallingConv::C: 947 case CallingConv::Fast: 948 case CallingConv::Cold: 949 return CC_AMDGPU_Func; 950 case CallingConv::AMDGPU_Gfx: 951 return CC_SI_Gfx; 952 case CallingConv::AMDGPU_KERNEL: 953 case CallingConv::SPIR_KERNEL: 954 default: 955 report_fatal_error("Unsupported calling convention for call"); 956 } 957 } 958 959 CCAssignFn *AMDGPUCallLowering::CCAssignFnForReturn(CallingConv::ID CC, 960 bool IsVarArg) { 961 switch (CC) { 962 case CallingConv::AMDGPU_KERNEL: 963 case CallingConv::SPIR_KERNEL: 964 llvm_unreachable("kernels should not be handled here"); 965 case CallingConv::AMDGPU_VS: 966 case CallingConv::AMDGPU_GS: 967 case CallingConv::AMDGPU_PS: 968 case CallingConv::AMDGPU_CS: 969 case CallingConv::AMDGPU_HS: 970 case CallingConv::AMDGPU_ES: 971 case CallingConv::AMDGPU_LS: 972 return RetCC_SI_Shader; 973 case CallingConv::AMDGPU_Gfx: 974 return RetCC_SI_Gfx; 975 case CallingConv::C: 976 case CallingConv::Fast: 977 case CallingConv::Cold: 978 return RetCC_AMDGPU_Func; 979 default: 980 report_fatal_error("Unsupported calling convention."); 981 } 982 } 983 984 /// The SelectionDAGBuilder will automatically promote function arguments 985 /// with illegal types. However, this does not work for the AMDGPU targets 986 /// since the function arguments are stored in memory as these illegal types. 987 /// In order to handle this properly we need to get the original types sizes 988 /// from the LLVM IR Function and fixup the ISD:InputArg values before 989 /// passing them to AnalyzeFormalArguments() 990 991 /// When the SelectionDAGBuilder computes the Ins, it takes care of splitting 992 /// input values across multiple registers. Each item in the Ins array 993 /// represents a single value that will be stored in registers. Ins[x].VT is 994 /// the value type of the value that will be stored in the register, so 995 /// whatever SDNode we lower the argument to needs to be this type. 996 /// 997 /// In order to correctly lower the arguments we need to know the size of each 998 /// argument. Since Ins[x].VT gives us the size of the register that will 999 /// hold the value, we need to look at Ins[x].ArgVT to see the 'real' type 1000 /// for the orignal function argument so that we can deduce the correct memory 1001 /// type to use for Ins[x]. In most cases the correct memory type will be 1002 /// Ins[x].ArgVT. However, this will not always be the case. If, for example, 1003 /// we have a kernel argument of type v8i8, this argument will be split into 1004 /// 8 parts and each part will be represented by its own item in the Ins array. 1005 /// For each part the Ins[x].ArgVT will be the v8i8, which is the full type of 1006 /// the argument before it was split. From this, we deduce that the memory type 1007 /// for each individual part is i8. We pass the memory type as LocVT to the 1008 /// calling convention analysis function and the register type (Ins[x].VT) as 1009 /// the ValVT. 1010 void AMDGPUTargetLowering::analyzeFormalArgumentsCompute( 1011 CCState &State, 1012 const SmallVectorImpl<ISD::InputArg> &Ins) const { 1013 const MachineFunction &MF = State.getMachineFunction(); 1014 const Function &Fn = MF.getFunction(); 1015 LLVMContext &Ctx = Fn.getParent()->getContext(); 1016 const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(MF); 1017 const unsigned ExplicitOffset = ST.getExplicitKernelArgOffset(Fn); 1018 CallingConv::ID CC = Fn.getCallingConv(); 1019 1020 Align MaxAlign = Align(1); 1021 uint64_t ExplicitArgOffset = 0; 1022 const DataLayout &DL = Fn.getParent()->getDataLayout(); 1023 1024 unsigned InIndex = 0; 1025 1026 for (const Argument &Arg : Fn.args()) { 1027 const bool IsByRef = Arg.hasByRefAttr(); 1028 Type *BaseArgTy = Arg.getType(); 1029 Type *MemArgTy = IsByRef ? Arg.getParamByRefType() : BaseArgTy; 1030 MaybeAlign Alignment = IsByRef ? Arg.getParamAlign() : None; 1031 if (!Alignment) 1032 Alignment = DL.getABITypeAlign(MemArgTy); 1033 MaxAlign = max(Alignment, MaxAlign); 1034 uint64_t AllocSize = DL.getTypeAllocSize(MemArgTy); 1035 1036 uint64_t ArgOffset = alignTo(ExplicitArgOffset, Alignment) + ExplicitOffset; 1037 ExplicitArgOffset = alignTo(ExplicitArgOffset, Alignment) + AllocSize; 1038 1039 // We're basically throwing away everything passed into us and starting over 1040 // to get accurate in-memory offsets. The "PartOffset" is completely useless 1041 // to us as computed in Ins. 1042 // 1043 // We also need to figure out what type legalization is trying to do to get 1044 // the correct memory offsets. 1045 1046 SmallVector<EVT, 16> ValueVTs; 1047 SmallVector<uint64_t, 16> Offsets; 1048 ComputeValueVTs(*this, DL, BaseArgTy, ValueVTs, &Offsets, ArgOffset); 1049 1050 for (unsigned Value = 0, NumValues = ValueVTs.size(); 1051 Value != NumValues; ++Value) { 1052 uint64_t BasePartOffset = Offsets[Value]; 1053 1054 EVT ArgVT = ValueVTs[Value]; 1055 EVT MemVT = ArgVT; 1056 MVT RegisterVT = getRegisterTypeForCallingConv(Ctx, CC, ArgVT); 1057 unsigned NumRegs = getNumRegistersForCallingConv(Ctx, CC, ArgVT); 1058 1059 if (NumRegs == 1) { 1060 // This argument is not split, so the IR type is the memory type. 1061 if (ArgVT.isExtended()) { 1062 // We have an extended type, like i24, so we should just use the 1063 // register type. 1064 MemVT = RegisterVT; 1065 } else { 1066 MemVT = ArgVT; 1067 } 1068 } else if (ArgVT.isVector() && RegisterVT.isVector() && 1069 ArgVT.getScalarType() == RegisterVT.getScalarType()) { 1070 assert(ArgVT.getVectorNumElements() > RegisterVT.getVectorNumElements()); 1071 // We have a vector value which has been split into a vector with 1072 // the same scalar type, but fewer elements. This should handle 1073 // all the floating-point vector types. 1074 MemVT = RegisterVT; 1075 } else if (ArgVT.isVector() && 1076 ArgVT.getVectorNumElements() == NumRegs) { 1077 // This arg has been split so that each element is stored in a separate 1078 // register. 1079 MemVT = ArgVT.getScalarType(); 1080 } else if (ArgVT.isExtended()) { 1081 // We have an extended type, like i65. 1082 MemVT = RegisterVT; 1083 } else { 1084 unsigned MemoryBits = ArgVT.getStoreSizeInBits() / NumRegs; 1085 assert(ArgVT.getStoreSizeInBits() % NumRegs == 0); 1086 if (RegisterVT.isInteger()) { 1087 MemVT = EVT::getIntegerVT(State.getContext(), MemoryBits); 1088 } else if (RegisterVT.isVector()) { 1089 assert(!RegisterVT.getScalarType().isFloatingPoint()); 1090 unsigned NumElements = RegisterVT.getVectorNumElements(); 1091 assert(MemoryBits % NumElements == 0); 1092 // This vector type has been split into another vector type with 1093 // a different elements size. 1094 EVT ScalarVT = EVT::getIntegerVT(State.getContext(), 1095 MemoryBits / NumElements); 1096 MemVT = EVT::getVectorVT(State.getContext(), ScalarVT, NumElements); 1097 } else { 1098 llvm_unreachable("cannot deduce memory type."); 1099 } 1100 } 1101 1102 // Convert one element vectors to scalar. 1103 if (MemVT.isVector() && MemVT.getVectorNumElements() == 1) 1104 MemVT = MemVT.getScalarType(); 1105 1106 // Round up vec3/vec5 argument. 1107 if (MemVT.isVector() && !MemVT.isPow2VectorType()) { 1108 assert(MemVT.getVectorNumElements() == 3 || 1109 MemVT.getVectorNumElements() == 5); 1110 MemVT = MemVT.getPow2VectorType(State.getContext()); 1111 } else if (!MemVT.isSimple() && !MemVT.isVector()) { 1112 MemVT = MemVT.getRoundIntegerType(State.getContext()); 1113 } 1114 1115 unsigned PartOffset = 0; 1116 for (unsigned i = 0; i != NumRegs; ++i) { 1117 State.addLoc(CCValAssign::getCustomMem(InIndex++, RegisterVT, 1118 BasePartOffset + PartOffset, 1119 MemVT.getSimpleVT(), 1120 CCValAssign::Full)); 1121 PartOffset += MemVT.getStoreSize(); 1122 } 1123 } 1124 } 1125 } 1126 1127 SDValue AMDGPUTargetLowering::LowerReturn( 1128 SDValue Chain, CallingConv::ID CallConv, 1129 bool isVarArg, 1130 const SmallVectorImpl<ISD::OutputArg> &Outs, 1131 const SmallVectorImpl<SDValue> &OutVals, 1132 const SDLoc &DL, SelectionDAG &DAG) const { 1133 // FIXME: Fails for r600 tests 1134 //assert(!isVarArg && Outs.empty() && OutVals.empty() && 1135 // "wave terminate should not have return values"); 1136 return DAG.getNode(AMDGPUISD::ENDPGM, DL, MVT::Other, Chain); 1137 } 1138 1139 //===---------------------------------------------------------------------===// 1140 // Target specific lowering 1141 //===---------------------------------------------------------------------===// 1142 1143 /// Selects the correct CCAssignFn for a given CallingConvention value. 1144 CCAssignFn *AMDGPUTargetLowering::CCAssignFnForCall(CallingConv::ID CC, 1145 bool IsVarArg) { 1146 return AMDGPUCallLowering::CCAssignFnForCall(CC, IsVarArg); 1147 } 1148 1149 CCAssignFn *AMDGPUTargetLowering::CCAssignFnForReturn(CallingConv::ID CC, 1150 bool IsVarArg) { 1151 return AMDGPUCallLowering::CCAssignFnForReturn(CC, IsVarArg); 1152 } 1153 1154 SDValue AMDGPUTargetLowering::addTokenForArgument(SDValue Chain, 1155 SelectionDAG &DAG, 1156 MachineFrameInfo &MFI, 1157 int ClobberedFI) const { 1158 SmallVector<SDValue, 8> ArgChains; 1159 int64_t FirstByte = MFI.getObjectOffset(ClobberedFI); 1160 int64_t LastByte = FirstByte + MFI.getObjectSize(ClobberedFI) - 1; 1161 1162 // Include the original chain at the beginning of the list. When this is 1163 // used by target LowerCall hooks, this helps legalize find the 1164 // CALLSEQ_BEGIN node. 1165 ArgChains.push_back(Chain); 1166 1167 // Add a chain value for each stack argument corresponding 1168 for (SDNode::use_iterator U = DAG.getEntryNode().getNode()->use_begin(), 1169 UE = DAG.getEntryNode().getNode()->use_end(); 1170 U != UE; ++U) { 1171 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U)) { 1172 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr())) { 1173 if (FI->getIndex() < 0) { 1174 int64_t InFirstByte = MFI.getObjectOffset(FI->getIndex()); 1175 int64_t InLastByte = InFirstByte; 1176 InLastByte += MFI.getObjectSize(FI->getIndex()) - 1; 1177 1178 if ((InFirstByte <= FirstByte && FirstByte <= InLastByte) || 1179 (FirstByte <= InFirstByte && InFirstByte <= LastByte)) 1180 ArgChains.push_back(SDValue(L, 1)); 1181 } 1182 } 1183 } 1184 } 1185 1186 // Build a tokenfactor for all the chains. 1187 return DAG.getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains); 1188 } 1189 1190 SDValue AMDGPUTargetLowering::lowerUnhandledCall(CallLoweringInfo &CLI, 1191 SmallVectorImpl<SDValue> &InVals, 1192 StringRef Reason) const { 1193 SDValue Callee = CLI.Callee; 1194 SelectionDAG &DAG = CLI.DAG; 1195 1196 const Function &Fn = DAG.getMachineFunction().getFunction(); 1197 1198 StringRef FuncName("<unknown>"); 1199 1200 if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee)) 1201 FuncName = G->getSymbol(); 1202 else if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 1203 FuncName = G->getGlobal()->getName(); 1204 1205 DiagnosticInfoUnsupported NoCalls( 1206 Fn, Reason + FuncName, CLI.DL.getDebugLoc()); 1207 DAG.getContext()->diagnose(NoCalls); 1208 1209 if (!CLI.IsTailCall) { 1210 for (unsigned I = 0, E = CLI.Ins.size(); I != E; ++I) 1211 InVals.push_back(DAG.getUNDEF(CLI.Ins[I].VT)); 1212 } 1213 1214 return DAG.getEntryNode(); 1215 } 1216 1217 SDValue AMDGPUTargetLowering::LowerCall(CallLoweringInfo &CLI, 1218 SmallVectorImpl<SDValue> &InVals) const { 1219 return lowerUnhandledCall(CLI, InVals, "unsupported call to function "); 1220 } 1221 1222 SDValue AMDGPUTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, 1223 SelectionDAG &DAG) const { 1224 const Function &Fn = DAG.getMachineFunction().getFunction(); 1225 1226 DiagnosticInfoUnsupported NoDynamicAlloca(Fn, "unsupported dynamic alloca", 1227 SDLoc(Op).getDebugLoc()); 1228 DAG.getContext()->diagnose(NoDynamicAlloca); 1229 auto Ops = {DAG.getConstant(0, SDLoc(), Op.getValueType()), Op.getOperand(0)}; 1230 return DAG.getMergeValues(Ops, SDLoc()); 1231 } 1232 1233 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op, 1234 SelectionDAG &DAG) const { 1235 switch (Op.getOpcode()) { 1236 default: 1237 Op->print(errs(), &DAG); 1238 llvm_unreachable("Custom lowering code for this " 1239 "instruction is not implemented yet!"); 1240 break; 1241 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG); 1242 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG); 1243 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG); 1244 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG); 1245 case ISD::SDIVREM: return LowerSDIVREM(Op, DAG); 1246 case ISD::FREM: return LowerFREM(Op, DAG); 1247 case ISD::FCEIL: return LowerFCEIL(Op, DAG); 1248 case ISD::FTRUNC: return LowerFTRUNC(Op, DAG); 1249 case ISD::FRINT: return LowerFRINT(Op, DAG); 1250 case ISD::FNEARBYINT: return LowerFNEARBYINT(Op, DAG); 1251 case ISD::FROUND: return LowerFROUND(Op, DAG); 1252 case ISD::FFLOOR: return LowerFFLOOR(Op, DAG); 1253 case ISD::FLOG: 1254 return LowerFLOG(Op, DAG, numbers::ln2f); 1255 case ISD::FLOG10: 1256 return LowerFLOG(Op, DAG, numbers::ln2f / numbers::ln10f); 1257 case ISD::FEXP: 1258 return lowerFEXP(Op, DAG); 1259 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG); 1260 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG); 1261 case ISD::FP_TO_FP16: return LowerFP_TO_FP16(Op, DAG); 1262 case ISD::FP_TO_SINT: 1263 case ISD::FP_TO_UINT: 1264 return LowerFP_TO_INT(Op, DAG); 1265 case ISD::CTTZ: 1266 case ISD::CTTZ_ZERO_UNDEF: 1267 case ISD::CTLZ: 1268 case ISD::CTLZ_ZERO_UNDEF: 1269 return LowerCTLZ_CTTZ(Op, DAG); 1270 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG); 1271 } 1272 return Op; 1273 } 1274 1275 void AMDGPUTargetLowering::ReplaceNodeResults(SDNode *N, 1276 SmallVectorImpl<SDValue> &Results, 1277 SelectionDAG &DAG) const { 1278 switch (N->getOpcode()) { 1279 case ISD::SIGN_EXTEND_INREG: 1280 // Different parts of legalization seem to interpret which type of 1281 // sign_extend_inreg is the one to check for custom lowering. The extended 1282 // from type is what really matters, but some places check for custom 1283 // lowering of the result type. This results in trying to use 1284 // ReplaceNodeResults to sext_in_reg to an illegal type, so we'll just do 1285 // nothing here and let the illegal result integer be handled normally. 1286 return; 1287 default: 1288 return; 1289 } 1290 } 1291 1292 bool AMDGPUTargetLowering::hasDefinedInitializer(const GlobalValue *GV) { 1293 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 1294 if (!GVar || !GVar->hasInitializer()) 1295 return false; 1296 1297 return !isa<UndefValue>(GVar->getInitializer()); 1298 } 1299 1300 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI, 1301 SDValue Op, 1302 SelectionDAG &DAG) const { 1303 1304 const DataLayout &DL = DAG.getDataLayout(); 1305 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op); 1306 const GlobalValue *GV = G->getGlobal(); 1307 1308 if (G->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS || 1309 G->getAddressSpace() == AMDGPUAS::REGION_ADDRESS) { 1310 if (!MFI->isModuleEntryFunction() && 1311 !GV->getName().equals("llvm.amdgcn.module.lds")) { 1312 SDLoc DL(Op); 1313 const Function &Fn = DAG.getMachineFunction().getFunction(); 1314 DiagnosticInfoUnsupported BadLDSDecl( 1315 Fn, "local memory global used by non-kernel function", 1316 DL.getDebugLoc(), DS_Warning); 1317 DAG.getContext()->diagnose(BadLDSDecl); 1318 1319 // We currently don't have a way to correctly allocate LDS objects that 1320 // aren't directly associated with a kernel. We do force inlining of 1321 // functions that use local objects. However, if these dead functions are 1322 // not eliminated, we don't want a compile time error. Just emit a warning 1323 // and a trap, since there should be no callable path here. 1324 SDValue Trap = DAG.getNode(ISD::TRAP, DL, MVT::Other, DAG.getEntryNode()); 1325 SDValue OutputChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, 1326 Trap, DAG.getRoot()); 1327 DAG.setRoot(OutputChain); 1328 return DAG.getUNDEF(Op.getValueType()); 1329 } 1330 1331 // XXX: What does the value of G->getOffset() mean? 1332 assert(G->getOffset() == 0 && 1333 "Do not know what to do with an non-zero offset"); 1334 1335 // TODO: We could emit code to handle the initialization somewhere. 1336 if (!hasDefinedInitializer(GV)) { 1337 unsigned Offset = MFI->allocateLDSGlobal(DL, *cast<GlobalVariable>(GV)); 1338 return DAG.getConstant(Offset, SDLoc(Op), Op.getValueType()); 1339 } 1340 } 1341 1342 const Function &Fn = DAG.getMachineFunction().getFunction(); 1343 DiagnosticInfoUnsupported BadInit( 1344 Fn, "unsupported initializer for address space", SDLoc(Op).getDebugLoc()); 1345 DAG.getContext()->diagnose(BadInit); 1346 return SDValue(); 1347 } 1348 1349 SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op, 1350 SelectionDAG &DAG) const { 1351 SmallVector<SDValue, 8> Args; 1352 1353 EVT VT = Op.getValueType(); 1354 if (VT == MVT::v4i16 || VT == MVT::v4f16) { 1355 SDLoc SL(Op); 1356 SDValue Lo = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Op.getOperand(0)); 1357 SDValue Hi = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Op.getOperand(1)); 1358 1359 SDValue BV = DAG.getBuildVector(MVT::v2i32, SL, { Lo, Hi }); 1360 return DAG.getNode(ISD::BITCAST, SL, VT, BV); 1361 } 1362 1363 for (const SDUse &U : Op->ops()) 1364 DAG.ExtractVectorElements(U.get(), Args); 1365 1366 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args); 1367 } 1368 1369 SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, 1370 SelectionDAG &DAG) const { 1371 1372 SmallVector<SDValue, 8> Args; 1373 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 1374 EVT VT = Op.getValueType(); 1375 EVT SrcVT = Op.getOperand(0).getValueType(); 1376 1377 // For these types, we have some TableGen patterns except if the index is 1 1378 if (((SrcVT == MVT::v4f16 && VT == MVT::v2f16) || 1379 (SrcVT == MVT::v4i16 && VT == MVT::v2i16)) && 1380 Start != 1) 1381 return Op; 1382 1383 DAG.ExtractVectorElements(Op.getOperand(0), Args, Start, 1384 VT.getVectorNumElements()); 1385 1386 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args); 1387 } 1388 1389 /// Generate Min/Max node 1390 SDValue AMDGPUTargetLowering::combineFMinMaxLegacy(const SDLoc &DL, EVT VT, 1391 SDValue LHS, SDValue RHS, 1392 SDValue True, SDValue False, 1393 SDValue CC, 1394 DAGCombinerInfo &DCI) const { 1395 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True)) 1396 return SDValue(); 1397 1398 SelectionDAG &DAG = DCI.DAG; 1399 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get(); 1400 switch (CCOpcode) { 1401 case ISD::SETOEQ: 1402 case ISD::SETONE: 1403 case ISD::SETUNE: 1404 case ISD::SETNE: 1405 case ISD::SETUEQ: 1406 case ISD::SETEQ: 1407 case ISD::SETFALSE: 1408 case ISD::SETFALSE2: 1409 case ISD::SETTRUE: 1410 case ISD::SETTRUE2: 1411 case ISD::SETUO: 1412 case ISD::SETO: 1413 break; 1414 case ISD::SETULE: 1415 case ISD::SETULT: { 1416 if (LHS == True) 1417 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS); 1418 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS); 1419 } 1420 case ISD::SETOLE: 1421 case ISD::SETOLT: 1422 case ISD::SETLE: 1423 case ISD::SETLT: { 1424 // Ordered. Assume ordered for undefined. 1425 1426 // Only do this after legalization to avoid interfering with other combines 1427 // which might occur. 1428 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG && 1429 !DCI.isCalledByLegalizer()) 1430 return SDValue(); 1431 1432 // We need to permute the operands to get the correct NaN behavior. The 1433 // selected operand is the second one based on the failing compare with NaN, 1434 // so permute it based on the compare type the hardware uses. 1435 if (LHS == True) 1436 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS); 1437 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS); 1438 } 1439 case ISD::SETUGE: 1440 case ISD::SETUGT: { 1441 if (LHS == True) 1442 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS); 1443 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS); 1444 } 1445 case ISD::SETGT: 1446 case ISD::SETGE: 1447 case ISD::SETOGE: 1448 case ISD::SETOGT: { 1449 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG && 1450 !DCI.isCalledByLegalizer()) 1451 return SDValue(); 1452 1453 if (LHS == True) 1454 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS); 1455 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS); 1456 } 1457 case ISD::SETCC_INVALID: 1458 llvm_unreachable("Invalid setcc condcode!"); 1459 } 1460 return SDValue(); 1461 } 1462 1463 std::pair<SDValue, SDValue> 1464 AMDGPUTargetLowering::split64BitValue(SDValue Op, SelectionDAG &DAG) const { 1465 SDLoc SL(Op); 1466 1467 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op); 1468 1469 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 1470 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 1471 1472 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero); 1473 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One); 1474 1475 return std::make_pair(Lo, Hi); 1476 } 1477 1478 SDValue AMDGPUTargetLowering::getLoHalf64(SDValue Op, SelectionDAG &DAG) const { 1479 SDLoc SL(Op); 1480 1481 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op); 1482 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 1483 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero); 1484 } 1485 1486 SDValue AMDGPUTargetLowering::getHiHalf64(SDValue Op, SelectionDAG &DAG) const { 1487 SDLoc SL(Op); 1488 1489 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op); 1490 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 1491 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One); 1492 } 1493 1494 // Split a vector type into two parts. The first part is a power of two vector. 1495 // The second part is whatever is left over, and is a scalar if it would 1496 // otherwise be a 1-vector. 1497 std::pair<EVT, EVT> 1498 AMDGPUTargetLowering::getSplitDestVTs(const EVT &VT, SelectionDAG &DAG) const { 1499 EVT LoVT, HiVT; 1500 EVT EltVT = VT.getVectorElementType(); 1501 unsigned NumElts = VT.getVectorNumElements(); 1502 unsigned LoNumElts = PowerOf2Ceil((NumElts + 1) / 2); 1503 LoVT = EVT::getVectorVT(*DAG.getContext(), EltVT, LoNumElts); 1504 HiVT = NumElts - LoNumElts == 1 1505 ? EltVT 1506 : EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts - LoNumElts); 1507 return std::make_pair(LoVT, HiVT); 1508 } 1509 1510 // Split a vector value into two parts of types LoVT and HiVT. HiVT could be 1511 // scalar. 1512 std::pair<SDValue, SDValue> 1513 AMDGPUTargetLowering::splitVector(const SDValue &N, const SDLoc &DL, 1514 const EVT &LoVT, const EVT &HiVT, 1515 SelectionDAG &DAG) const { 1516 assert(LoVT.getVectorNumElements() + 1517 (HiVT.isVector() ? HiVT.getVectorNumElements() : 1) <= 1518 N.getValueType().getVectorNumElements() && 1519 "More vector elements requested than available!"); 1520 SDValue Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N, 1521 DAG.getVectorIdxConstant(0, DL)); 1522 SDValue Hi = DAG.getNode( 1523 HiVT.isVector() ? ISD::EXTRACT_SUBVECTOR : ISD::EXTRACT_VECTOR_ELT, DL, 1524 HiVT, N, DAG.getVectorIdxConstant(LoVT.getVectorNumElements(), DL)); 1525 return std::make_pair(Lo, Hi); 1526 } 1527 1528 SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue Op, 1529 SelectionDAG &DAG) const { 1530 LoadSDNode *Load = cast<LoadSDNode>(Op); 1531 EVT VT = Op.getValueType(); 1532 SDLoc SL(Op); 1533 1534 1535 // If this is a 2 element vector, we really want to scalarize and not create 1536 // weird 1 element vectors. 1537 if (VT.getVectorNumElements() == 2) { 1538 SDValue Ops[2]; 1539 std::tie(Ops[0], Ops[1]) = scalarizeVectorLoad(Load, DAG); 1540 return DAG.getMergeValues(Ops, SL); 1541 } 1542 1543 SDValue BasePtr = Load->getBasePtr(); 1544 EVT MemVT = Load->getMemoryVT(); 1545 1546 const MachinePointerInfo &SrcValue = Load->getMemOperand()->getPointerInfo(); 1547 1548 EVT LoVT, HiVT; 1549 EVT LoMemVT, HiMemVT; 1550 SDValue Lo, Hi; 1551 1552 std::tie(LoVT, HiVT) = getSplitDestVTs(VT, DAG); 1553 std::tie(LoMemVT, HiMemVT) = getSplitDestVTs(MemVT, DAG); 1554 std::tie(Lo, Hi) = splitVector(Op, SL, LoVT, HiVT, DAG); 1555 1556 unsigned Size = LoMemVT.getStoreSize(); 1557 unsigned BaseAlign = Load->getAlignment(); 1558 unsigned HiAlign = MinAlign(BaseAlign, Size); 1559 1560 SDValue LoLoad = DAG.getExtLoad(Load->getExtensionType(), SL, LoVT, 1561 Load->getChain(), BasePtr, SrcValue, LoMemVT, 1562 BaseAlign, Load->getMemOperand()->getFlags()); 1563 SDValue HiPtr = DAG.getObjectPtrOffset(SL, BasePtr, TypeSize::Fixed(Size)); 1564 SDValue HiLoad = 1565 DAG.getExtLoad(Load->getExtensionType(), SL, HiVT, Load->getChain(), 1566 HiPtr, SrcValue.getWithOffset(LoMemVT.getStoreSize()), 1567 HiMemVT, HiAlign, Load->getMemOperand()->getFlags()); 1568 1569 SDValue Join; 1570 if (LoVT == HiVT) { 1571 // This is the case that the vector is power of two so was evenly split. 1572 Join = DAG.getNode(ISD::CONCAT_VECTORS, SL, VT, LoLoad, HiLoad); 1573 } else { 1574 Join = DAG.getNode(ISD::INSERT_SUBVECTOR, SL, VT, DAG.getUNDEF(VT), LoLoad, 1575 DAG.getVectorIdxConstant(0, SL)); 1576 Join = DAG.getNode( 1577 HiVT.isVector() ? ISD::INSERT_SUBVECTOR : ISD::INSERT_VECTOR_ELT, SL, 1578 VT, Join, HiLoad, 1579 DAG.getVectorIdxConstant(LoVT.getVectorNumElements(), SL)); 1580 } 1581 1582 SDValue Ops[] = {Join, DAG.getNode(ISD::TokenFactor, SL, MVT::Other, 1583 LoLoad.getValue(1), HiLoad.getValue(1))}; 1584 1585 return DAG.getMergeValues(Ops, SL); 1586 } 1587 1588 SDValue AMDGPUTargetLowering::WidenOrSplitVectorLoad(SDValue Op, 1589 SelectionDAG &DAG) const { 1590 LoadSDNode *Load = cast<LoadSDNode>(Op); 1591 EVT VT = Op.getValueType(); 1592 SDValue BasePtr = Load->getBasePtr(); 1593 EVT MemVT = Load->getMemoryVT(); 1594 SDLoc SL(Op); 1595 const MachinePointerInfo &SrcValue = Load->getMemOperand()->getPointerInfo(); 1596 unsigned BaseAlign = Load->getAlignment(); 1597 unsigned NumElements = MemVT.getVectorNumElements(); 1598 1599 // Widen from vec3 to vec4 when the load is at least 8-byte aligned 1600 // or 16-byte fully dereferenceable. Otherwise, split the vector load. 1601 if (NumElements != 3 || 1602 (BaseAlign < 8 && 1603 !SrcValue.isDereferenceable(16, *DAG.getContext(), DAG.getDataLayout()))) 1604 return SplitVectorLoad(Op, DAG); 1605 1606 assert(NumElements == 3); 1607 1608 EVT WideVT = 1609 EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(), 4); 1610 EVT WideMemVT = 1611 EVT::getVectorVT(*DAG.getContext(), MemVT.getVectorElementType(), 4); 1612 SDValue WideLoad = DAG.getExtLoad( 1613 Load->getExtensionType(), SL, WideVT, Load->getChain(), BasePtr, SrcValue, 1614 WideMemVT, BaseAlign, Load->getMemOperand()->getFlags()); 1615 return DAG.getMergeValues( 1616 {DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, VT, WideLoad, 1617 DAG.getVectorIdxConstant(0, SL)), 1618 WideLoad.getValue(1)}, 1619 SL); 1620 } 1621 1622 SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op, 1623 SelectionDAG &DAG) const { 1624 StoreSDNode *Store = cast<StoreSDNode>(Op); 1625 SDValue Val = Store->getValue(); 1626 EVT VT = Val.getValueType(); 1627 1628 // If this is a 2 element vector, we really want to scalarize and not create 1629 // weird 1 element vectors. 1630 if (VT.getVectorNumElements() == 2) 1631 return scalarizeVectorStore(Store, DAG); 1632 1633 EVT MemVT = Store->getMemoryVT(); 1634 SDValue Chain = Store->getChain(); 1635 SDValue BasePtr = Store->getBasePtr(); 1636 SDLoc SL(Op); 1637 1638 EVT LoVT, HiVT; 1639 EVT LoMemVT, HiMemVT; 1640 SDValue Lo, Hi; 1641 1642 std::tie(LoVT, HiVT) = getSplitDestVTs(VT, DAG); 1643 std::tie(LoMemVT, HiMemVT) = getSplitDestVTs(MemVT, DAG); 1644 std::tie(Lo, Hi) = splitVector(Val, SL, LoVT, HiVT, DAG); 1645 1646 SDValue HiPtr = DAG.getObjectPtrOffset(SL, BasePtr, LoMemVT.getStoreSize()); 1647 1648 const MachinePointerInfo &SrcValue = Store->getMemOperand()->getPointerInfo(); 1649 unsigned BaseAlign = Store->getAlignment(); 1650 unsigned Size = LoMemVT.getStoreSize(); 1651 unsigned HiAlign = MinAlign(BaseAlign, Size); 1652 1653 SDValue LoStore = 1654 DAG.getTruncStore(Chain, SL, Lo, BasePtr, SrcValue, LoMemVT, BaseAlign, 1655 Store->getMemOperand()->getFlags()); 1656 SDValue HiStore = 1657 DAG.getTruncStore(Chain, SL, Hi, HiPtr, SrcValue.getWithOffset(Size), 1658 HiMemVT, HiAlign, Store->getMemOperand()->getFlags()); 1659 1660 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoStore, HiStore); 1661 } 1662 1663 // This is a shortcut for integer division because we have fast i32<->f32 1664 // conversions, and fast f32 reciprocal instructions. The fractional part of a 1665 // float is enough to accurately represent up to a 24-bit signed integer. 1666 SDValue AMDGPUTargetLowering::LowerDIVREM24(SDValue Op, SelectionDAG &DAG, 1667 bool Sign) const { 1668 SDLoc DL(Op); 1669 EVT VT = Op.getValueType(); 1670 SDValue LHS = Op.getOperand(0); 1671 SDValue RHS = Op.getOperand(1); 1672 MVT IntVT = MVT::i32; 1673 MVT FltVT = MVT::f32; 1674 1675 unsigned LHSSignBits = DAG.ComputeNumSignBits(LHS); 1676 if (LHSSignBits < 9) 1677 return SDValue(); 1678 1679 unsigned RHSSignBits = DAG.ComputeNumSignBits(RHS); 1680 if (RHSSignBits < 9) 1681 return SDValue(); 1682 1683 unsigned BitSize = VT.getSizeInBits(); 1684 unsigned SignBits = std::min(LHSSignBits, RHSSignBits); 1685 unsigned DivBits = BitSize - SignBits; 1686 if (Sign) 1687 ++DivBits; 1688 1689 ISD::NodeType ToFp = Sign ? ISD::SINT_TO_FP : ISD::UINT_TO_FP; 1690 ISD::NodeType ToInt = Sign ? ISD::FP_TO_SINT : ISD::FP_TO_UINT; 1691 1692 SDValue jq = DAG.getConstant(1, DL, IntVT); 1693 1694 if (Sign) { 1695 // char|short jq = ia ^ ib; 1696 jq = DAG.getNode(ISD::XOR, DL, VT, LHS, RHS); 1697 1698 // jq = jq >> (bitsize - 2) 1699 jq = DAG.getNode(ISD::SRA, DL, VT, jq, 1700 DAG.getConstant(BitSize - 2, DL, VT)); 1701 1702 // jq = jq | 0x1 1703 jq = DAG.getNode(ISD::OR, DL, VT, jq, DAG.getConstant(1, DL, VT)); 1704 } 1705 1706 // int ia = (int)LHS; 1707 SDValue ia = LHS; 1708 1709 // int ib, (int)RHS; 1710 SDValue ib = RHS; 1711 1712 // float fa = (float)ia; 1713 SDValue fa = DAG.getNode(ToFp, DL, FltVT, ia); 1714 1715 // float fb = (float)ib; 1716 SDValue fb = DAG.getNode(ToFp, DL, FltVT, ib); 1717 1718 SDValue fq = DAG.getNode(ISD::FMUL, DL, FltVT, 1719 fa, DAG.getNode(AMDGPUISD::RCP, DL, FltVT, fb)); 1720 1721 // fq = trunc(fq); 1722 fq = DAG.getNode(ISD::FTRUNC, DL, FltVT, fq); 1723 1724 // float fqneg = -fq; 1725 SDValue fqneg = DAG.getNode(ISD::FNEG, DL, FltVT, fq); 1726 1727 MachineFunction &MF = DAG.getMachineFunction(); 1728 const AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>(); 1729 1730 // float fr = mad(fqneg, fb, fa); 1731 unsigned OpCode = !Subtarget->hasMadMacF32Insts() ? 1732 (unsigned)ISD::FMA : 1733 !MFI->getMode().allFP32Denormals() ? 1734 (unsigned)ISD::FMAD : 1735 (unsigned)AMDGPUISD::FMAD_FTZ; 1736 SDValue fr = DAG.getNode(OpCode, DL, FltVT, fqneg, fb, fa); 1737 1738 // int iq = (int)fq; 1739 SDValue iq = DAG.getNode(ToInt, DL, IntVT, fq); 1740 1741 // fr = fabs(fr); 1742 fr = DAG.getNode(ISD::FABS, DL, FltVT, fr); 1743 1744 // fb = fabs(fb); 1745 fb = DAG.getNode(ISD::FABS, DL, FltVT, fb); 1746 1747 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 1748 1749 // int cv = fr >= fb; 1750 SDValue cv = DAG.getSetCC(DL, SetCCVT, fr, fb, ISD::SETOGE); 1751 1752 // jq = (cv ? jq : 0); 1753 jq = DAG.getNode(ISD::SELECT, DL, VT, cv, jq, DAG.getConstant(0, DL, VT)); 1754 1755 // dst = iq + jq; 1756 SDValue Div = DAG.getNode(ISD::ADD, DL, VT, iq, jq); 1757 1758 // Rem needs compensation, it's easier to recompute it 1759 SDValue Rem = DAG.getNode(ISD::MUL, DL, VT, Div, RHS); 1760 Rem = DAG.getNode(ISD::SUB, DL, VT, LHS, Rem); 1761 1762 // Truncate to number of bits this divide really is. 1763 if (Sign) { 1764 SDValue InRegSize 1765 = DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), DivBits)); 1766 Div = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Div, InRegSize); 1767 Rem = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Rem, InRegSize); 1768 } else { 1769 SDValue TruncMask = DAG.getConstant((UINT64_C(1) << DivBits) - 1, DL, VT); 1770 Div = DAG.getNode(ISD::AND, DL, VT, Div, TruncMask); 1771 Rem = DAG.getNode(ISD::AND, DL, VT, Rem, TruncMask); 1772 } 1773 1774 return DAG.getMergeValues({ Div, Rem }, DL); 1775 } 1776 1777 void AMDGPUTargetLowering::LowerUDIVREM64(SDValue Op, 1778 SelectionDAG &DAG, 1779 SmallVectorImpl<SDValue> &Results) const { 1780 SDLoc DL(Op); 1781 EVT VT = Op.getValueType(); 1782 1783 assert(VT == MVT::i64 && "LowerUDIVREM64 expects an i64"); 1784 1785 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext()); 1786 1787 SDValue One = DAG.getConstant(1, DL, HalfVT); 1788 SDValue Zero = DAG.getConstant(0, DL, HalfVT); 1789 1790 //HiLo split 1791 SDValue LHS = Op.getOperand(0); 1792 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, Zero); 1793 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, One); 1794 1795 SDValue RHS = Op.getOperand(1); 1796 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, Zero); 1797 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, One); 1798 1799 if (DAG.MaskedValueIsZero(RHS, APInt::getHighBitsSet(64, 32)) && 1800 DAG.MaskedValueIsZero(LHS, APInt::getHighBitsSet(64, 32))) { 1801 1802 SDValue Res = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(HalfVT, HalfVT), 1803 LHS_Lo, RHS_Lo); 1804 1805 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(0), Zero}); 1806 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(1), Zero}); 1807 1808 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV)); 1809 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM)); 1810 return; 1811 } 1812 1813 if (isTypeLegal(MVT::i64)) { 1814 MachineFunction &MF = DAG.getMachineFunction(); 1815 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 1816 1817 // Compute denominator reciprocal. 1818 unsigned FMAD = !Subtarget->hasMadMacF32Insts() ? 1819 (unsigned)ISD::FMA : 1820 !MFI->getMode().allFP32Denormals() ? 1821 (unsigned)ISD::FMAD : 1822 (unsigned)AMDGPUISD::FMAD_FTZ; 1823 1824 SDValue Cvt_Lo = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, RHS_Lo); 1825 SDValue Cvt_Hi = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, RHS_Hi); 1826 SDValue Mad1 = DAG.getNode(FMAD, DL, MVT::f32, Cvt_Hi, 1827 DAG.getConstantFP(APInt(32, 0x4f800000).bitsToFloat(), DL, MVT::f32), 1828 Cvt_Lo); 1829 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, DL, MVT::f32, Mad1); 1830 SDValue Mul1 = DAG.getNode(ISD::FMUL, DL, MVT::f32, Rcp, 1831 DAG.getConstantFP(APInt(32, 0x5f7ffffc).bitsToFloat(), DL, MVT::f32)); 1832 SDValue Mul2 = DAG.getNode(ISD::FMUL, DL, MVT::f32, Mul1, 1833 DAG.getConstantFP(APInt(32, 0x2f800000).bitsToFloat(), DL, MVT::f32)); 1834 SDValue Trunc = DAG.getNode(ISD::FTRUNC, DL, MVT::f32, Mul2); 1835 SDValue Mad2 = DAG.getNode(FMAD, DL, MVT::f32, Trunc, 1836 DAG.getConstantFP(APInt(32, 0xcf800000).bitsToFloat(), DL, MVT::f32), 1837 Mul1); 1838 SDValue Rcp_Lo = DAG.getNode(ISD::FP_TO_UINT, DL, HalfVT, Mad2); 1839 SDValue Rcp_Hi = DAG.getNode(ISD::FP_TO_UINT, DL, HalfVT, Trunc); 1840 SDValue Rcp64 = DAG.getBitcast(VT, 1841 DAG.getBuildVector(MVT::v2i32, DL, {Rcp_Lo, Rcp_Hi})); 1842 1843 SDValue Zero64 = DAG.getConstant(0, DL, VT); 1844 SDValue One64 = DAG.getConstant(1, DL, VT); 1845 SDValue Zero1 = DAG.getConstant(0, DL, MVT::i1); 1846 SDVTList HalfCarryVT = DAG.getVTList(HalfVT, MVT::i1); 1847 1848 SDValue Neg_RHS = DAG.getNode(ISD::SUB, DL, VT, Zero64, RHS); 1849 SDValue Mullo1 = DAG.getNode(ISD::MUL, DL, VT, Neg_RHS, Rcp64); 1850 SDValue Mulhi1 = DAG.getNode(ISD::MULHU, DL, VT, Rcp64, Mullo1); 1851 SDValue Mulhi1_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, Mulhi1, 1852 Zero); 1853 SDValue Mulhi1_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, Mulhi1, 1854 One); 1855 1856 SDValue Add1_Lo = DAG.getNode(ISD::ADDCARRY, DL, HalfCarryVT, Rcp_Lo, 1857 Mulhi1_Lo, Zero1); 1858 SDValue Add1_Hi = DAG.getNode(ISD::ADDCARRY, DL, HalfCarryVT, Rcp_Hi, 1859 Mulhi1_Hi, Add1_Lo.getValue(1)); 1860 SDValue Add1_HiNc = DAG.getNode(ISD::ADD, DL, HalfVT, Rcp_Hi, Mulhi1_Hi); 1861 SDValue Add1 = DAG.getBitcast(VT, 1862 DAG.getBuildVector(MVT::v2i32, DL, {Add1_Lo, Add1_Hi})); 1863 1864 SDValue Mullo2 = DAG.getNode(ISD::MUL, DL, VT, Neg_RHS, Add1); 1865 SDValue Mulhi2 = DAG.getNode(ISD::MULHU, DL, VT, Add1, Mullo2); 1866 SDValue Mulhi2_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, Mulhi2, 1867 Zero); 1868 SDValue Mulhi2_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, Mulhi2, 1869 One); 1870 1871 SDValue Add2_Lo = DAG.getNode(ISD::ADDCARRY, DL, HalfCarryVT, Add1_Lo, 1872 Mulhi2_Lo, Zero1); 1873 SDValue Add2_HiC = DAG.getNode(ISD::ADDCARRY, DL, HalfCarryVT, Add1_HiNc, 1874 Mulhi2_Hi, Add1_Lo.getValue(1)); 1875 SDValue Add2_Hi = DAG.getNode(ISD::ADDCARRY, DL, HalfCarryVT, Add2_HiC, 1876 Zero, Add2_Lo.getValue(1)); 1877 SDValue Add2 = DAG.getBitcast(VT, 1878 DAG.getBuildVector(MVT::v2i32, DL, {Add2_Lo, Add2_Hi})); 1879 SDValue Mulhi3 = DAG.getNode(ISD::MULHU, DL, VT, LHS, Add2); 1880 1881 SDValue Mul3 = DAG.getNode(ISD::MUL, DL, VT, RHS, Mulhi3); 1882 1883 SDValue Mul3_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, Mul3, Zero); 1884 SDValue Mul3_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, Mul3, One); 1885 SDValue Sub1_Lo = DAG.getNode(ISD::SUBCARRY, DL, HalfCarryVT, LHS_Lo, 1886 Mul3_Lo, Zero1); 1887 SDValue Sub1_Hi = DAG.getNode(ISD::SUBCARRY, DL, HalfCarryVT, LHS_Hi, 1888 Mul3_Hi, Sub1_Lo.getValue(1)); 1889 SDValue Sub1_Mi = DAG.getNode(ISD::SUB, DL, HalfVT, LHS_Hi, Mul3_Hi); 1890 SDValue Sub1 = DAG.getBitcast(VT, 1891 DAG.getBuildVector(MVT::v2i32, DL, {Sub1_Lo, Sub1_Hi})); 1892 1893 SDValue MinusOne = DAG.getConstant(0xffffffffu, DL, HalfVT); 1894 SDValue C1 = DAG.getSelectCC(DL, Sub1_Hi, RHS_Hi, MinusOne, Zero, 1895 ISD::SETUGE); 1896 SDValue C2 = DAG.getSelectCC(DL, Sub1_Lo, RHS_Lo, MinusOne, Zero, 1897 ISD::SETUGE); 1898 SDValue C3 = DAG.getSelectCC(DL, Sub1_Hi, RHS_Hi, C2, C1, ISD::SETEQ); 1899 1900 // TODO: Here and below portions of the code can be enclosed into if/endif. 1901 // Currently control flow is unconditional and we have 4 selects after 1902 // potential endif to substitute PHIs. 1903 1904 // if C3 != 0 ... 1905 SDValue Sub2_Lo = DAG.getNode(ISD::SUBCARRY, DL, HalfCarryVT, Sub1_Lo, 1906 RHS_Lo, Zero1); 1907 SDValue Sub2_Mi = DAG.getNode(ISD::SUBCARRY, DL, HalfCarryVT, Sub1_Mi, 1908 RHS_Hi, Sub1_Lo.getValue(1)); 1909 SDValue Sub2_Hi = DAG.getNode(ISD::SUBCARRY, DL, HalfCarryVT, Sub2_Mi, 1910 Zero, Sub2_Lo.getValue(1)); 1911 SDValue Sub2 = DAG.getBitcast(VT, 1912 DAG.getBuildVector(MVT::v2i32, DL, {Sub2_Lo, Sub2_Hi})); 1913 1914 SDValue Add3 = DAG.getNode(ISD::ADD, DL, VT, Mulhi3, One64); 1915 1916 SDValue C4 = DAG.getSelectCC(DL, Sub2_Hi, RHS_Hi, MinusOne, Zero, 1917 ISD::SETUGE); 1918 SDValue C5 = DAG.getSelectCC(DL, Sub2_Lo, RHS_Lo, MinusOne, Zero, 1919 ISD::SETUGE); 1920 SDValue C6 = DAG.getSelectCC(DL, Sub2_Hi, RHS_Hi, C5, C4, ISD::SETEQ); 1921 1922 // if (C6 != 0) 1923 SDValue Add4 = DAG.getNode(ISD::ADD, DL, VT, Add3, One64); 1924 1925 SDValue Sub3_Lo = DAG.getNode(ISD::SUBCARRY, DL, HalfCarryVT, Sub2_Lo, 1926 RHS_Lo, Zero1); 1927 SDValue Sub3_Mi = DAG.getNode(ISD::SUBCARRY, DL, HalfCarryVT, Sub2_Mi, 1928 RHS_Hi, Sub2_Lo.getValue(1)); 1929 SDValue Sub3_Hi = DAG.getNode(ISD::SUBCARRY, DL, HalfCarryVT, Sub3_Mi, 1930 Zero, Sub3_Lo.getValue(1)); 1931 SDValue Sub3 = DAG.getBitcast(VT, 1932 DAG.getBuildVector(MVT::v2i32, DL, {Sub3_Lo, Sub3_Hi})); 1933 1934 // endif C6 1935 // endif C3 1936 1937 SDValue Sel1 = DAG.getSelectCC(DL, C6, Zero, Add4, Add3, ISD::SETNE); 1938 SDValue Div = DAG.getSelectCC(DL, C3, Zero, Sel1, Mulhi3, ISD::SETNE); 1939 1940 SDValue Sel2 = DAG.getSelectCC(DL, C6, Zero, Sub3, Sub2, ISD::SETNE); 1941 SDValue Rem = DAG.getSelectCC(DL, C3, Zero, Sel2, Sub1, ISD::SETNE); 1942 1943 Results.push_back(Div); 1944 Results.push_back(Rem); 1945 1946 return; 1947 } 1948 1949 // r600 expandion. 1950 // Get Speculative values 1951 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo); 1952 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo); 1953 1954 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, Zero, REM_Part, LHS_Hi, ISD::SETEQ); 1955 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {REM_Lo, Zero}); 1956 REM = DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM); 1957 1958 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, Zero, DIV_Part, Zero, ISD::SETEQ); 1959 SDValue DIV_Lo = Zero; 1960 1961 const unsigned halfBitWidth = HalfVT.getSizeInBits(); 1962 1963 for (unsigned i = 0; i < halfBitWidth; ++i) { 1964 const unsigned bitPos = halfBitWidth - i - 1; 1965 SDValue POS = DAG.getConstant(bitPos, DL, HalfVT); 1966 // Get value of high bit 1967 SDValue HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS); 1968 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, One); 1969 HBit = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, HBit); 1970 1971 // Shift 1972 REM = DAG.getNode(ISD::SHL, DL, VT, REM, DAG.getConstant(1, DL, VT)); 1973 // Add LHS high bit 1974 REM = DAG.getNode(ISD::OR, DL, VT, REM, HBit); 1975 1976 SDValue BIT = DAG.getConstant(1ULL << bitPos, DL, HalfVT); 1977 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, Zero, ISD::SETUGE); 1978 1979 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT); 1980 1981 // Update REM 1982 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS); 1983 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETUGE); 1984 } 1985 1986 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {DIV_Lo, DIV_Hi}); 1987 DIV = DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV); 1988 Results.push_back(DIV); 1989 Results.push_back(REM); 1990 } 1991 1992 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op, 1993 SelectionDAG &DAG) const { 1994 SDLoc DL(Op); 1995 EVT VT = Op.getValueType(); 1996 1997 if (VT == MVT::i64) { 1998 SmallVector<SDValue, 2> Results; 1999 LowerUDIVREM64(Op, DAG, Results); 2000 return DAG.getMergeValues(Results, DL); 2001 } 2002 2003 if (VT == MVT::i32) { 2004 if (SDValue Res = LowerDIVREM24(Op, DAG, false)) 2005 return Res; 2006 } 2007 2008 SDValue X = Op.getOperand(0); 2009 SDValue Y = Op.getOperand(1); 2010 2011 // See AMDGPUCodeGenPrepare::expandDivRem32 for a description of the 2012 // algorithm used here. 2013 2014 // Initial estimate of inv(y). 2015 SDValue Z = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Y); 2016 2017 // One round of UNR. 2018 SDValue NegY = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Y); 2019 SDValue NegYZ = DAG.getNode(ISD::MUL, DL, VT, NegY, Z); 2020 Z = DAG.getNode(ISD::ADD, DL, VT, Z, 2021 DAG.getNode(ISD::MULHU, DL, VT, Z, NegYZ)); 2022 2023 // Quotient/remainder estimate. 2024 SDValue Q = DAG.getNode(ISD::MULHU, DL, VT, X, Z); 2025 SDValue R = 2026 DAG.getNode(ISD::SUB, DL, VT, X, DAG.getNode(ISD::MUL, DL, VT, Q, Y)); 2027 2028 // First quotient/remainder refinement. 2029 EVT CCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 2030 SDValue One = DAG.getConstant(1, DL, VT); 2031 SDValue Cond = DAG.getSetCC(DL, CCVT, R, Y, ISD::SETUGE); 2032 Q = DAG.getNode(ISD::SELECT, DL, VT, Cond, 2033 DAG.getNode(ISD::ADD, DL, VT, Q, One), Q); 2034 R = DAG.getNode(ISD::SELECT, DL, VT, Cond, 2035 DAG.getNode(ISD::SUB, DL, VT, R, Y), R); 2036 2037 // Second quotient/remainder refinement. 2038 Cond = DAG.getSetCC(DL, CCVT, R, Y, ISD::SETUGE); 2039 Q = DAG.getNode(ISD::SELECT, DL, VT, Cond, 2040 DAG.getNode(ISD::ADD, DL, VT, Q, One), Q); 2041 R = DAG.getNode(ISD::SELECT, DL, VT, Cond, 2042 DAG.getNode(ISD::SUB, DL, VT, R, Y), R); 2043 2044 return DAG.getMergeValues({Q, R}, DL); 2045 } 2046 2047 SDValue AMDGPUTargetLowering::LowerSDIVREM(SDValue Op, 2048 SelectionDAG &DAG) const { 2049 SDLoc DL(Op); 2050 EVT VT = Op.getValueType(); 2051 2052 SDValue LHS = Op.getOperand(0); 2053 SDValue RHS = Op.getOperand(1); 2054 2055 SDValue Zero = DAG.getConstant(0, DL, VT); 2056 SDValue NegOne = DAG.getConstant(-1, DL, VT); 2057 2058 if (VT == MVT::i32) { 2059 if (SDValue Res = LowerDIVREM24(Op, DAG, true)) 2060 return Res; 2061 } 2062 2063 if (VT == MVT::i64 && 2064 DAG.ComputeNumSignBits(LHS) > 32 && 2065 DAG.ComputeNumSignBits(RHS) > 32) { 2066 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext()); 2067 2068 //HiLo split 2069 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, Zero); 2070 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, Zero); 2071 SDValue DIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(HalfVT, HalfVT), 2072 LHS_Lo, RHS_Lo); 2073 SDValue Res[2] = { 2074 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(0)), 2075 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(1)) 2076 }; 2077 return DAG.getMergeValues(Res, DL); 2078 } 2079 2080 SDValue LHSign = DAG.getSelectCC(DL, LHS, Zero, NegOne, Zero, ISD::SETLT); 2081 SDValue RHSign = DAG.getSelectCC(DL, RHS, Zero, NegOne, Zero, ISD::SETLT); 2082 SDValue DSign = DAG.getNode(ISD::XOR, DL, VT, LHSign, RHSign); 2083 SDValue RSign = LHSign; // Remainder sign is the same as LHS 2084 2085 LHS = DAG.getNode(ISD::ADD, DL, VT, LHS, LHSign); 2086 RHS = DAG.getNode(ISD::ADD, DL, VT, RHS, RHSign); 2087 2088 LHS = DAG.getNode(ISD::XOR, DL, VT, LHS, LHSign); 2089 RHS = DAG.getNode(ISD::XOR, DL, VT, RHS, RHSign); 2090 2091 SDValue Div = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT), LHS, RHS); 2092 SDValue Rem = Div.getValue(1); 2093 2094 Div = DAG.getNode(ISD::XOR, DL, VT, Div, DSign); 2095 Rem = DAG.getNode(ISD::XOR, DL, VT, Rem, RSign); 2096 2097 Div = DAG.getNode(ISD::SUB, DL, VT, Div, DSign); 2098 Rem = DAG.getNode(ISD::SUB, DL, VT, Rem, RSign); 2099 2100 SDValue Res[2] = { 2101 Div, 2102 Rem 2103 }; 2104 return DAG.getMergeValues(Res, DL); 2105 } 2106 2107 // (frem x, y) -> (fma (fneg (ftrunc (fdiv x, y))), y, x) 2108 SDValue AMDGPUTargetLowering::LowerFREM(SDValue Op, SelectionDAG &DAG) const { 2109 SDLoc SL(Op); 2110 EVT VT = Op.getValueType(); 2111 auto Flags = Op->getFlags(); 2112 SDValue X = Op.getOperand(0); 2113 SDValue Y = Op.getOperand(1); 2114 2115 SDValue Div = DAG.getNode(ISD::FDIV, SL, VT, X, Y, Flags); 2116 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, VT, Div, Flags); 2117 SDValue Neg = DAG.getNode(ISD::FNEG, SL, VT, Trunc, Flags); 2118 // TODO: For f32 use FMAD instead if !hasFastFMA32? 2119 return DAG.getNode(ISD::FMA, SL, VT, Neg, Y, X, Flags); 2120 } 2121 2122 SDValue AMDGPUTargetLowering::LowerFCEIL(SDValue Op, SelectionDAG &DAG) const { 2123 SDLoc SL(Op); 2124 SDValue Src = Op.getOperand(0); 2125 2126 // result = trunc(src) 2127 // if (src > 0.0 && src != result) 2128 // result += 1.0 2129 2130 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src); 2131 2132 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64); 2133 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64); 2134 2135 EVT SetCCVT = 2136 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64); 2137 2138 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOGT); 2139 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE); 2140 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc); 2141 2142 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, One, Zero); 2143 // TODO: Should this propagate fast-math-flags? 2144 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add); 2145 } 2146 2147 static SDValue extractF64Exponent(SDValue Hi, const SDLoc &SL, 2148 SelectionDAG &DAG) { 2149 const unsigned FractBits = 52; 2150 const unsigned ExpBits = 11; 2151 2152 SDValue ExpPart = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32, 2153 Hi, 2154 DAG.getConstant(FractBits - 32, SL, MVT::i32), 2155 DAG.getConstant(ExpBits, SL, MVT::i32)); 2156 SDValue Exp = DAG.getNode(ISD::SUB, SL, MVT::i32, ExpPart, 2157 DAG.getConstant(1023, SL, MVT::i32)); 2158 2159 return Exp; 2160 } 2161 2162 SDValue AMDGPUTargetLowering::LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const { 2163 SDLoc SL(Op); 2164 SDValue Src = Op.getOperand(0); 2165 2166 assert(Op.getValueType() == MVT::f64); 2167 2168 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 2169 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 2170 2171 SDValue VecSrc = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src); 2172 2173 // Extract the upper half, since this is where we will find the sign and 2174 // exponent. 2175 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecSrc, One); 2176 2177 SDValue Exp = extractF64Exponent(Hi, SL, DAG); 2178 2179 const unsigned FractBits = 52; 2180 2181 // Extract the sign bit. 2182 const SDValue SignBitMask = DAG.getConstant(UINT32_C(1) << 31, SL, MVT::i32); 2183 SDValue SignBit = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, SignBitMask); 2184 2185 // Extend back to 64-bits. 2186 SDValue SignBit64 = DAG.getBuildVector(MVT::v2i32, SL, {Zero, SignBit}); 2187 SignBit64 = DAG.getNode(ISD::BITCAST, SL, MVT::i64, SignBit64); 2188 2189 SDValue BcInt = DAG.getNode(ISD::BITCAST, SL, MVT::i64, Src); 2190 const SDValue FractMask 2191 = DAG.getConstant((UINT64_C(1) << FractBits) - 1, SL, MVT::i64); 2192 2193 SDValue Shr = DAG.getNode(ISD::SRA, SL, MVT::i64, FractMask, Exp); 2194 SDValue Not = DAG.getNOT(SL, Shr, MVT::i64); 2195 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, BcInt, Not); 2196 2197 EVT SetCCVT = 2198 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32); 2199 2200 const SDValue FiftyOne = DAG.getConstant(FractBits - 1, SL, MVT::i32); 2201 2202 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT); 2203 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT); 2204 2205 SDValue Tmp1 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpLt0, SignBit64, Tmp0); 2206 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpGt51, BcInt, Tmp1); 2207 2208 return DAG.getNode(ISD::BITCAST, SL, MVT::f64, Tmp2); 2209 } 2210 2211 SDValue AMDGPUTargetLowering::LowerFRINT(SDValue Op, SelectionDAG &DAG) const { 2212 SDLoc SL(Op); 2213 SDValue Src = Op.getOperand(0); 2214 2215 assert(Op.getValueType() == MVT::f64); 2216 2217 APFloat C1Val(APFloat::IEEEdouble(), "0x1.0p+52"); 2218 SDValue C1 = DAG.getConstantFP(C1Val, SL, MVT::f64); 2219 SDValue CopySign = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, C1, Src); 2220 2221 // TODO: Should this propagate fast-math-flags? 2222 2223 SDValue Tmp1 = DAG.getNode(ISD::FADD, SL, MVT::f64, Src, CopySign); 2224 SDValue Tmp2 = DAG.getNode(ISD::FSUB, SL, MVT::f64, Tmp1, CopySign); 2225 2226 SDValue Fabs = DAG.getNode(ISD::FABS, SL, MVT::f64, Src); 2227 2228 APFloat C2Val(APFloat::IEEEdouble(), "0x1.fffffffffffffp+51"); 2229 SDValue C2 = DAG.getConstantFP(C2Val, SL, MVT::f64); 2230 2231 EVT SetCCVT = 2232 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64); 2233 SDValue Cond = DAG.getSetCC(SL, SetCCVT, Fabs, C2, ISD::SETOGT); 2234 2235 return DAG.getSelect(SL, MVT::f64, Cond, Src, Tmp2); 2236 } 2237 2238 SDValue AMDGPUTargetLowering::LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const { 2239 // FNEARBYINT and FRINT are the same, except in their handling of FP 2240 // exceptions. Those aren't really meaningful for us, and OpenCL only has 2241 // rint, so just treat them as equivalent. 2242 return DAG.getNode(ISD::FRINT, SDLoc(Op), Op.getValueType(), Op.getOperand(0)); 2243 } 2244 2245 // XXX - May require not supporting f32 denormals? 2246 2247 // Don't handle v2f16. The extra instructions to scalarize and repack around the 2248 // compare and vselect end up producing worse code than scalarizing the whole 2249 // operation. 2250 SDValue AMDGPUTargetLowering::LowerFROUND(SDValue Op, SelectionDAG &DAG) const { 2251 SDLoc SL(Op); 2252 SDValue X = Op.getOperand(0); 2253 EVT VT = Op.getValueType(); 2254 2255 SDValue T = DAG.getNode(ISD::FTRUNC, SL, VT, X); 2256 2257 // TODO: Should this propagate fast-math-flags? 2258 2259 SDValue Diff = DAG.getNode(ISD::FSUB, SL, VT, X, T); 2260 2261 SDValue AbsDiff = DAG.getNode(ISD::FABS, SL, VT, Diff); 2262 2263 const SDValue Zero = DAG.getConstantFP(0.0, SL, VT); 2264 const SDValue One = DAG.getConstantFP(1.0, SL, VT); 2265 const SDValue Half = DAG.getConstantFP(0.5, SL, VT); 2266 2267 SDValue SignOne = DAG.getNode(ISD::FCOPYSIGN, SL, VT, One, X); 2268 2269 EVT SetCCVT = 2270 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 2271 2272 SDValue Cmp = DAG.getSetCC(SL, SetCCVT, AbsDiff, Half, ISD::SETOGE); 2273 2274 SDValue Sel = DAG.getNode(ISD::SELECT, SL, VT, Cmp, SignOne, Zero); 2275 2276 return DAG.getNode(ISD::FADD, SL, VT, T, Sel); 2277 } 2278 2279 SDValue AMDGPUTargetLowering::LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const { 2280 SDLoc SL(Op); 2281 SDValue Src = Op.getOperand(0); 2282 2283 // result = trunc(src); 2284 // if (src < 0.0 && src != result) 2285 // result += -1.0. 2286 2287 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src); 2288 2289 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64); 2290 const SDValue NegOne = DAG.getConstantFP(-1.0, SL, MVT::f64); 2291 2292 EVT SetCCVT = 2293 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64); 2294 2295 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOLT); 2296 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE); 2297 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc); 2298 2299 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, NegOne, Zero); 2300 // TODO: Should this propagate fast-math-flags? 2301 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add); 2302 } 2303 2304 SDValue AMDGPUTargetLowering::LowerFLOG(SDValue Op, SelectionDAG &DAG, 2305 double Log2BaseInverted) const { 2306 EVT VT = Op.getValueType(); 2307 2308 SDLoc SL(Op); 2309 SDValue Operand = Op.getOperand(0); 2310 SDValue Log2Operand = DAG.getNode(ISD::FLOG2, SL, VT, Operand); 2311 SDValue Log2BaseInvertedOperand = DAG.getConstantFP(Log2BaseInverted, SL, VT); 2312 2313 return DAG.getNode(ISD::FMUL, SL, VT, Log2Operand, Log2BaseInvertedOperand); 2314 } 2315 2316 // exp2(M_LOG2E_F * f); 2317 SDValue AMDGPUTargetLowering::lowerFEXP(SDValue Op, SelectionDAG &DAG) const { 2318 EVT VT = Op.getValueType(); 2319 SDLoc SL(Op); 2320 SDValue Src = Op.getOperand(0); 2321 2322 const SDValue K = DAG.getConstantFP(numbers::log2e, SL, VT); 2323 SDValue Mul = DAG.getNode(ISD::FMUL, SL, VT, Src, K, Op->getFlags()); 2324 return DAG.getNode(ISD::FEXP2, SL, VT, Mul, Op->getFlags()); 2325 } 2326 2327 static bool isCtlzOpc(unsigned Opc) { 2328 return Opc == ISD::CTLZ || Opc == ISD::CTLZ_ZERO_UNDEF; 2329 } 2330 2331 static bool isCttzOpc(unsigned Opc) { 2332 return Opc == ISD::CTTZ || Opc == ISD::CTTZ_ZERO_UNDEF; 2333 } 2334 2335 SDValue AMDGPUTargetLowering::LowerCTLZ_CTTZ(SDValue Op, SelectionDAG &DAG) const { 2336 SDLoc SL(Op); 2337 SDValue Src = Op.getOperand(0); 2338 bool ZeroUndef = Op.getOpcode() == ISD::CTTZ_ZERO_UNDEF || 2339 Op.getOpcode() == ISD::CTLZ_ZERO_UNDEF; 2340 2341 unsigned ISDOpc, NewOpc; 2342 if (isCtlzOpc(Op.getOpcode())) { 2343 ISDOpc = ISD::CTLZ_ZERO_UNDEF; 2344 NewOpc = AMDGPUISD::FFBH_U32; 2345 } else if (isCttzOpc(Op.getOpcode())) { 2346 ISDOpc = ISD::CTTZ_ZERO_UNDEF; 2347 NewOpc = AMDGPUISD::FFBL_B32; 2348 } else 2349 llvm_unreachable("Unexpected OPCode!!!"); 2350 2351 2352 if (ZeroUndef && Src.getValueType() == MVT::i32) 2353 return DAG.getNode(NewOpc, SL, MVT::i32, Src); 2354 2355 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src); 2356 2357 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 2358 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 2359 2360 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero); 2361 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One); 2362 2363 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), 2364 *DAG.getContext(), MVT::i32); 2365 2366 SDValue HiOrLo = isCtlzOpc(Op.getOpcode()) ? Hi : Lo; 2367 SDValue Hi0orLo0 = DAG.getSetCC(SL, SetCCVT, HiOrLo, Zero, ISD::SETEQ); 2368 2369 SDValue OprLo = DAG.getNode(ISDOpc, SL, MVT::i32, Lo); 2370 SDValue OprHi = DAG.getNode(ISDOpc, SL, MVT::i32, Hi); 2371 2372 const SDValue Bits32 = DAG.getConstant(32, SL, MVT::i32); 2373 SDValue Add, NewOpr; 2374 if (isCtlzOpc(Op.getOpcode())) { 2375 Add = DAG.getNode(ISD::ADD, SL, MVT::i32, OprLo, Bits32); 2376 // ctlz(x) = hi_32(x) == 0 ? ctlz(lo_32(x)) + 32 : ctlz(hi_32(x)) 2377 NewOpr = DAG.getNode(ISD::SELECT, SL, MVT::i32, Hi0orLo0, Add, OprHi); 2378 } else { 2379 Add = DAG.getNode(ISD::ADD, SL, MVT::i32, OprHi, Bits32); 2380 // cttz(x) = lo_32(x) == 0 ? cttz(hi_32(x)) + 32 : cttz(lo_32(x)) 2381 NewOpr = DAG.getNode(ISD::SELECT, SL, MVT::i32, Hi0orLo0, Add, OprLo); 2382 } 2383 2384 if (!ZeroUndef) { 2385 // Test if the full 64-bit input is zero. 2386 2387 // FIXME: DAG combines turn what should be an s_and_b64 into a v_or_b32, 2388 // which we probably don't want. 2389 SDValue LoOrHi = isCtlzOpc(Op.getOpcode()) ? Lo : Hi; 2390 SDValue Lo0OrHi0 = DAG.getSetCC(SL, SetCCVT, LoOrHi, Zero, ISD::SETEQ); 2391 SDValue SrcIsZero = DAG.getNode(ISD::AND, SL, SetCCVT, Lo0OrHi0, Hi0orLo0); 2392 2393 // TODO: If i64 setcc is half rate, it can result in 1 fewer instruction 2394 // with the same cycles, otherwise it is slower. 2395 // SDValue SrcIsZero = DAG.getSetCC(SL, SetCCVT, Src, 2396 // DAG.getConstant(0, SL, MVT::i64), ISD::SETEQ); 2397 2398 const SDValue Bits32 = DAG.getConstant(64, SL, MVT::i32); 2399 2400 // The instruction returns -1 for 0 input, but the defined intrinsic 2401 // behavior is to return the number of bits. 2402 NewOpr = DAG.getNode(ISD::SELECT, SL, MVT::i32, 2403 SrcIsZero, Bits32, NewOpr); 2404 } 2405 2406 return DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i64, NewOpr); 2407 } 2408 2409 SDValue AMDGPUTargetLowering::LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG, 2410 bool Signed) const { 2411 // Unsigned 2412 // cul2f(ulong u) 2413 //{ 2414 // uint lz = clz(u); 2415 // uint e = (u != 0) ? 127U + 63U - lz : 0; 2416 // u = (u << lz) & 0x7fffffffffffffffUL; 2417 // ulong t = u & 0xffffffffffUL; 2418 // uint v = (e << 23) | (uint)(u >> 40); 2419 // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U); 2420 // return as_float(v + r); 2421 //} 2422 // Signed 2423 // cl2f(long l) 2424 //{ 2425 // long s = l >> 63; 2426 // float r = cul2f((l + s) ^ s); 2427 // return s ? -r : r; 2428 //} 2429 2430 SDLoc SL(Op); 2431 SDValue Src = Op.getOperand(0); 2432 SDValue L = Src; 2433 2434 SDValue S; 2435 if (Signed) { 2436 const SDValue SignBit = DAG.getConstant(63, SL, MVT::i64); 2437 S = DAG.getNode(ISD::SRA, SL, MVT::i64, L, SignBit); 2438 2439 SDValue LPlusS = DAG.getNode(ISD::ADD, SL, MVT::i64, L, S); 2440 L = DAG.getNode(ISD::XOR, SL, MVT::i64, LPlusS, S); 2441 } 2442 2443 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), 2444 *DAG.getContext(), MVT::f32); 2445 2446 2447 SDValue ZeroI32 = DAG.getConstant(0, SL, MVT::i32); 2448 SDValue ZeroI64 = DAG.getConstant(0, SL, MVT::i64); 2449 SDValue LZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i64, L); 2450 LZ = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LZ); 2451 2452 SDValue K = DAG.getConstant(127U + 63U, SL, MVT::i32); 2453 SDValue E = DAG.getSelect(SL, MVT::i32, 2454 DAG.getSetCC(SL, SetCCVT, L, ZeroI64, ISD::SETNE), 2455 DAG.getNode(ISD::SUB, SL, MVT::i32, K, LZ), 2456 ZeroI32); 2457 2458 SDValue U = DAG.getNode(ISD::AND, SL, MVT::i64, 2459 DAG.getNode(ISD::SHL, SL, MVT::i64, L, LZ), 2460 DAG.getConstant((-1ULL) >> 1, SL, MVT::i64)); 2461 2462 SDValue T = DAG.getNode(ISD::AND, SL, MVT::i64, U, 2463 DAG.getConstant(0xffffffffffULL, SL, MVT::i64)); 2464 2465 SDValue UShl = DAG.getNode(ISD::SRL, SL, MVT::i64, 2466 U, DAG.getConstant(40, SL, MVT::i64)); 2467 2468 SDValue V = DAG.getNode(ISD::OR, SL, MVT::i32, 2469 DAG.getNode(ISD::SHL, SL, MVT::i32, E, DAG.getConstant(23, SL, MVT::i32)), 2470 DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, UShl)); 2471 2472 SDValue C = DAG.getConstant(0x8000000000ULL, SL, MVT::i64); 2473 SDValue RCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETUGT); 2474 SDValue TCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETEQ); 2475 2476 SDValue One = DAG.getConstant(1, SL, MVT::i32); 2477 2478 SDValue VTrunc1 = DAG.getNode(ISD::AND, SL, MVT::i32, V, One); 2479 2480 SDValue R = DAG.getSelect(SL, MVT::i32, 2481 RCmp, 2482 One, 2483 DAG.getSelect(SL, MVT::i32, TCmp, VTrunc1, ZeroI32)); 2484 R = DAG.getNode(ISD::ADD, SL, MVT::i32, V, R); 2485 R = DAG.getNode(ISD::BITCAST, SL, MVT::f32, R); 2486 2487 if (!Signed) 2488 return R; 2489 2490 SDValue RNeg = DAG.getNode(ISD::FNEG, SL, MVT::f32, R); 2491 return DAG.getSelect(SL, MVT::f32, DAG.getSExtOrTrunc(S, SL, SetCCVT), RNeg, R); 2492 } 2493 2494 SDValue AMDGPUTargetLowering::LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG, 2495 bool Signed) const { 2496 SDLoc SL(Op); 2497 SDValue Src = Op.getOperand(0); 2498 2499 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src); 2500 2501 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, 2502 DAG.getConstant(0, SL, MVT::i32)); 2503 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, 2504 DAG.getConstant(1, SL, MVT::i32)); 2505 2506 SDValue CvtHi = DAG.getNode(Signed ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, 2507 SL, MVT::f64, Hi); 2508 2509 SDValue CvtLo = DAG.getNode(ISD::UINT_TO_FP, SL, MVT::f64, Lo); 2510 2511 SDValue LdExp = DAG.getNode(AMDGPUISD::LDEXP, SL, MVT::f64, CvtHi, 2512 DAG.getConstant(32, SL, MVT::i32)); 2513 // TODO: Should this propagate fast-math-flags? 2514 return DAG.getNode(ISD::FADD, SL, MVT::f64, LdExp, CvtLo); 2515 } 2516 2517 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op, 2518 SelectionDAG &DAG) const { 2519 // TODO: Factor out code common with LowerSINT_TO_FP. 2520 EVT DestVT = Op.getValueType(); 2521 SDValue Src = Op.getOperand(0); 2522 EVT SrcVT = Src.getValueType(); 2523 2524 if (SrcVT == MVT::i16) { 2525 if (DestVT == MVT::f16) 2526 return Op; 2527 SDLoc DL(Op); 2528 2529 // Promote src to i32 2530 SDValue Ext = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Src); 2531 return DAG.getNode(ISD::UINT_TO_FP, DL, DestVT, Ext); 2532 } 2533 2534 assert(SrcVT == MVT::i64 && "operation should be legal"); 2535 2536 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) { 2537 SDLoc DL(Op); 2538 2539 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src); 2540 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op)); 2541 SDValue FPRound = 2542 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag); 2543 2544 return FPRound; 2545 } 2546 2547 if (DestVT == MVT::f32) 2548 return LowerINT_TO_FP32(Op, DAG, false); 2549 2550 assert(DestVT == MVT::f64); 2551 return LowerINT_TO_FP64(Op, DAG, false); 2552 } 2553 2554 SDValue AMDGPUTargetLowering::LowerSINT_TO_FP(SDValue Op, 2555 SelectionDAG &DAG) const { 2556 EVT DestVT = Op.getValueType(); 2557 2558 SDValue Src = Op.getOperand(0); 2559 EVT SrcVT = Src.getValueType(); 2560 2561 if (SrcVT == MVT::i16) { 2562 if (DestVT == MVT::f16) 2563 return Op; 2564 2565 SDLoc DL(Op); 2566 // Promote src to i32 2567 SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32, Src); 2568 return DAG.getNode(ISD::SINT_TO_FP, DL, DestVT, Ext); 2569 } 2570 2571 assert(SrcVT == MVT::i64 && "operation should be legal"); 2572 2573 // TODO: Factor out code common with LowerUINT_TO_FP. 2574 2575 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) { 2576 SDLoc DL(Op); 2577 SDValue Src = Op.getOperand(0); 2578 2579 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src); 2580 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op)); 2581 SDValue FPRound = 2582 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag); 2583 2584 return FPRound; 2585 } 2586 2587 if (DestVT == MVT::f32) 2588 return LowerINT_TO_FP32(Op, DAG, true); 2589 2590 assert(DestVT == MVT::f64); 2591 return LowerINT_TO_FP64(Op, DAG, true); 2592 } 2593 2594 SDValue AMDGPUTargetLowering::LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG, 2595 bool Signed) const { 2596 SDLoc SL(Op); 2597 2598 SDValue Src = Op.getOperand(0); 2599 2600 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src); 2601 2602 SDValue K0 = DAG.getConstantFP(BitsToDouble(UINT64_C(0x3df0000000000000)), SL, 2603 MVT::f64); 2604 SDValue K1 = DAG.getConstantFP(BitsToDouble(UINT64_C(0xc1f0000000000000)), SL, 2605 MVT::f64); 2606 // TODO: Should this propagate fast-math-flags? 2607 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, Trunc, K0); 2608 2609 SDValue FloorMul = DAG.getNode(ISD::FFLOOR, SL, MVT::f64, Mul); 2610 2611 2612 SDValue Fma = DAG.getNode(ISD::FMA, SL, MVT::f64, FloorMul, K1, Trunc); 2613 2614 SDValue Hi = DAG.getNode(Signed ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, SL, 2615 MVT::i32, FloorMul); 2616 SDValue Lo = DAG.getNode(ISD::FP_TO_UINT, SL, MVT::i32, Fma); 2617 2618 SDValue Result = DAG.getBuildVector(MVT::v2i32, SL, {Lo, Hi}); 2619 2620 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Result); 2621 } 2622 2623 SDValue AMDGPUTargetLowering::LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const { 2624 SDLoc DL(Op); 2625 SDValue N0 = Op.getOperand(0); 2626 2627 // Convert to target node to get known bits 2628 if (N0.getValueType() == MVT::f32) 2629 return DAG.getNode(AMDGPUISD::FP_TO_FP16, DL, Op.getValueType(), N0); 2630 2631 if (getTargetMachine().Options.UnsafeFPMath) { 2632 // There is a generic expand for FP_TO_FP16 with unsafe fast math. 2633 return SDValue(); 2634 } 2635 2636 assert(N0.getSimpleValueType() == MVT::f64); 2637 2638 // f64 -> f16 conversion using round-to-nearest-even rounding mode. 2639 const unsigned ExpMask = 0x7ff; 2640 const unsigned ExpBiasf64 = 1023; 2641 const unsigned ExpBiasf16 = 15; 2642 SDValue Zero = DAG.getConstant(0, DL, MVT::i32); 2643 SDValue One = DAG.getConstant(1, DL, MVT::i32); 2644 SDValue U = DAG.getNode(ISD::BITCAST, DL, MVT::i64, N0); 2645 SDValue UH = DAG.getNode(ISD::SRL, DL, MVT::i64, U, 2646 DAG.getConstant(32, DL, MVT::i64)); 2647 UH = DAG.getZExtOrTrunc(UH, DL, MVT::i32); 2648 U = DAG.getZExtOrTrunc(U, DL, MVT::i32); 2649 SDValue E = DAG.getNode(ISD::SRL, DL, MVT::i32, UH, 2650 DAG.getConstant(20, DL, MVT::i64)); 2651 E = DAG.getNode(ISD::AND, DL, MVT::i32, E, 2652 DAG.getConstant(ExpMask, DL, MVT::i32)); 2653 // Subtract the fp64 exponent bias (1023) to get the real exponent and 2654 // add the f16 bias (15) to get the biased exponent for the f16 format. 2655 E = DAG.getNode(ISD::ADD, DL, MVT::i32, E, 2656 DAG.getConstant(-ExpBiasf64 + ExpBiasf16, DL, MVT::i32)); 2657 2658 SDValue M = DAG.getNode(ISD::SRL, DL, MVT::i32, UH, 2659 DAG.getConstant(8, DL, MVT::i32)); 2660 M = DAG.getNode(ISD::AND, DL, MVT::i32, M, 2661 DAG.getConstant(0xffe, DL, MVT::i32)); 2662 2663 SDValue MaskedSig = DAG.getNode(ISD::AND, DL, MVT::i32, UH, 2664 DAG.getConstant(0x1ff, DL, MVT::i32)); 2665 MaskedSig = DAG.getNode(ISD::OR, DL, MVT::i32, MaskedSig, U); 2666 2667 SDValue Lo40Set = DAG.getSelectCC(DL, MaskedSig, Zero, Zero, One, ISD::SETEQ); 2668 M = DAG.getNode(ISD::OR, DL, MVT::i32, M, Lo40Set); 2669 2670 // (M != 0 ? 0x0200 : 0) | 0x7c00; 2671 SDValue I = DAG.getNode(ISD::OR, DL, MVT::i32, 2672 DAG.getSelectCC(DL, M, Zero, DAG.getConstant(0x0200, DL, MVT::i32), 2673 Zero, ISD::SETNE), DAG.getConstant(0x7c00, DL, MVT::i32)); 2674 2675 // N = M | (E << 12); 2676 SDValue N = DAG.getNode(ISD::OR, DL, MVT::i32, M, 2677 DAG.getNode(ISD::SHL, DL, MVT::i32, E, 2678 DAG.getConstant(12, DL, MVT::i32))); 2679 2680 // B = clamp(1-E, 0, 13); 2681 SDValue OneSubExp = DAG.getNode(ISD::SUB, DL, MVT::i32, 2682 One, E); 2683 SDValue B = DAG.getNode(ISD::SMAX, DL, MVT::i32, OneSubExp, Zero); 2684 B = DAG.getNode(ISD::SMIN, DL, MVT::i32, B, 2685 DAG.getConstant(13, DL, MVT::i32)); 2686 2687 SDValue SigSetHigh = DAG.getNode(ISD::OR, DL, MVT::i32, M, 2688 DAG.getConstant(0x1000, DL, MVT::i32)); 2689 2690 SDValue D = DAG.getNode(ISD::SRL, DL, MVT::i32, SigSetHigh, B); 2691 SDValue D0 = DAG.getNode(ISD::SHL, DL, MVT::i32, D, B); 2692 SDValue D1 = DAG.getSelectCC(DL, D0, SigSetHigh, One, Zero, ISD::SETNE); 2693 D = DAG.getNode(ISD::OR, DL, MVT::i32, D, D1); 2694 2695 SDValue V = DAG.getSelectCC(DL, E, One, D, N, ISD::SETLT); 2696 SDValue VLow3 = DAG.getNode(ISD::AND, DL, MVT::i32, V, 2697 DAG.getConstant(0x7, DL, MVT::i32)); 2698 V = DAG.getNode(ISD::SRL, DL, MVT::i32, V, 2699 DAG.getConstant(2, DL, MVT::i32)); 2700 SDValue V0 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(3, DL, MVT::i32), 2701 One, Zero, ISD::SETEQ); 2702 SDValue V1 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(5, DL, MVT::i32), 2703 One, Zero, ISD::SETGT); 2704 V1 = DAG.getNode(ISD::OR, DL, MVT::i32, V0, V1); 2705 V = DAG.getNode(ISD::ADD, DL, MVT::i32, V, V1); 2706 2707 V = DAG.getSelectCC(DL, E, DAG.getConstant(30, DL, MVT::i32), 2708 DAG.getConstant(0x7c00, DL, MVT::i32), V, ISD::SETGT); 2709 V = DAG.getSelectCC(DL, E, DAG.getConstant(1039, DL, MVT::i32), 2710 I, V, ISD::SETEQ); 2711 2712 // Extract the sign bit. 2713 SDValue Sign = DAG.getNode(ISD::SRL, DL, MVT::i32, UH, 2714 DAG.getConstant(16, DL, MVT::i32)); 2715 Sign = DAG.getNode(ISD::AND, DL, MVT::i32, Sign, 2716 DAG.getConstant(0x8000, DL, MVT::i32)); 2717 2718 V = DAG.getNode(ISD::OR, DL, MVT::i32, Sign, V); 2719 return DAG.getZExtOrTrunc(V, DL, Op.getValueType()); 2720 } 2721 2722 SDValue AMDGPUTargetLowering::LowerFP_TO_INT(SDValue Op, 2723 SelectionDAG &DAG) const { 2724 SDValue Src = Op.getOperand(0); 2725 unsigned OpOpcode = Op.getOpcode(); 2726 EVT SrcVT = Src.getValueType(); 2727 EVT DestVT = Op.getValueType(); 2728 2729 // Will be selected natively 2730 if (SrcVT == MVT::f16 && DestVT == MVT::i16) 2731 return Op; 2732 2733 // Promote i16 to i32 2734 if (DestVT == MVT::i16 && (SrcVT == MVT::f32 || SrcVT == MVT::f64)) { 2735 SDLoc DL(Op); 2736 2737 SDValue FpToInt32 = DAG.getNode(OpOpcode, DL, MVT::i32, Src); 2738 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FpToInt32); 2739 } 2740 2741 if (SrcVT == MVT::f16 || 2742 (SrcVT == MVT::f32 && Src.getOpcode() == ISD::FP16_TO_FP)) { 2743 SDLoc DL(Op); 2744 2745 SDValue FpToInt32 = DAG.getNode(OpOpcode, DL, MVT::i32, Src); 2746 unsigned Ext = 2747 OpOpcode == ISD::FP_TO_SINT ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 2748 return DAG.getNode(Ext, DL, MVT::i64, FpToInt32); 2749 } 2750 2751 if (DestVT == MVT::i64 && SrcVT == MVT::f64) 2752 return LowerFP64_TO_INT(Op, DAG, OpOpcode == ISD::FP_TO_SINT); 2753 2754 return SDValue(); 2755 } 2756 2757 SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, 2758 SelectionDAG &DAG) const { 2759 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 2760 MVT VT = Op.getSimpleValueType(); 2761 MVT ScalarVT = VT.getScalarType(); 2762 2763 assert(VT.isVector()); 2764 2765 SDValue Src = Op.getOperand(0); 2766 SDLoc DL(Op); 2767 2768 // TODO: Don't scalarize on Evergreen? 2769 unsigned NElts = VT.getVectorNumElements(); 2770 SmallVector<SDValue, 8> Args; 2771 DAG.ExtractVectorElements(Src, Args, 0, NElts); 2772 2773 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType()); 2774 for (unsigned I = 0; I < NElts; ++I) 2775 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp); 2776 2777 return DAG.getBuildVector(VT, DL, Args); 2778 } 2779 2780 //===----------------------------------------------------------------------===// 2781 // Custom DAG optimizations 2782 //===----------------------------------------------------------------------===// 2783 2784 static bool isU24(SDValue Op, SelectionDAG &DAG) { 2785 return AMDGPUTargetLowering::numBitsUnsigned(Op, DAG) <= 24; 2786 } 2787 2788 static bool isI24(SDValue Op, SelectionDAG &DAG) { 2789 EVT VT = Op.getValueType(); 2790 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated 2791 // as unsigned 24-bit values. 2792 AMDGPUTargetLowering::numBitsSigned(Op, DAG) < 24; 2793 } 2794 2795 static SDValue simplifyMul24(SDNode *Node24, 2796 TargetLowering::DAGCombinerInfo &DCI) { 2797 SelectionDAG &DAG = DCI.DAG; 2798 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 2799 bool IsIntrin = Node24->getOpcode() == ISD::INTRINSIC_WO_CHAIN; 2800 2801 SDValue LHS = IsIntrin ? Node24->getOperand(1) : Node24->getOperand(0); 2802 SDValue RHS = IsIntrin ? Node24->getOperand(2) : Node24->getOperand(1); 2803 unsigned NewOpcode = Node24->getOpcode(); 2804 if (IsIntrin) { 2805 unsigned IID = cast<ConstantSDNode>(Node24->getOperand(0))->getZExtValue(); 2806 NewOpcode = IID == Intrinsic::amdgcn_mul_i24 ? 2807 AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24; 2808 } 2809 2810 APInt Demanded = APInt::getLowBitsSet(LHS.getValueSizeInBits(), 24); 2811 2812 // First try to simplify using SimplifyMultipleUseDemandedBits which allows 2813 // the operands to have other uses, but will only perform simplifications that 2814 // involve bypassing some nodes for this user. 2815 SDValue DemandedLHS = TLI.SimplifyMultipleUseDemandedBits(LHS, Demanded, DAG); 2816 SDValue DemandedRHS = TLI.SimplifyMultipleUseDemandedBits(RHS, Demanded, DAG); 2817 if (DemandedLHS || DemandedRHS) 2818 return DAG.getNode(NewOpcode, SDLoc(Node24), Node24->getVTList(), 2819 DemandedLHS ? DemandedLHS : LHS, 2820 DemandedRHS ? DemandedRHS : RHS); 2821 2822 // Now try SimplifyDemandedBits which can simplify the nodes used by our 2823 // operands if this node is the only user. 2824 if (TLI.SimplifyDemandedBits(LHS, Demanded, DCI)) 2825 return SDValue(Node24, 0); 2826 if (TLI.SimplifyDemandedBits(RHS, Demanded, DCI)) 2827 return SDValue(Node24, 0); 2828 2829 return SDValue(); 2830 } 2831 2832 template <typename IntTy> 2833 static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0, uint32_t Offset, 2834 uint32_t Width, const SDLoc &DL) { 2835 if (Width + Offset < 32) { 2836 uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width); 2837 IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width); 2838 return DAG.getConstant(Result, DL, MVT::i32); 2839 } 2840 2841 return DAG.getConstant(Src0 >> Offset, DL, MVT::i32); 2842 } 2843 2844 static bool hasVolatileUser(SDNode *Val) { 2845 for (SDNode *U : Val->uses()) { 2846 if (MemSDNode *M = dyn_cast<MemSDNode>(U)) { 2847 if (M->isVolatile()) 2848 return true; 2849 } 2850 } 2851 2852 return false; 2853 } 2854 2855 bool AMDGPUTargetLowering::shouldCombineMemoryType(EVT VT) const { 2856 // i32 vectors are the canonical memory type. 2857 if (VT.getScalarType() == MVT::i32 || isTypeLegal(VT)) 2858 return false; 2859 2860 if (!VT.isByteSized()) 2861 return false; 2862 2863 unsigned Size = VT.getStoreSize(); 2864 2865 if ((Size == 1 || Size == 2 || Size == 4) && !VT.isVector()) 2866 return false; 2867 2868 if (Size == 3 || (Size > 4 && (Size % 4 != 0))) 2869 return false; 2870 2871 return true; 2872 } 2873 2874 // Replace load of an illegal type with a store of a bitcast to a friendlier 2875 // type. 2876 SDValue AMDGPUTargetLowering::performLoadCombine(SDNode *N, 2877 DAGCombinerInfo &DCI) const { 2878 if (!DCI.isBeforeLegalize()) 2879 return SDValue(); 2880 2881 LoadSDNode *LN = cast<LoadSDNode>(N); 2882 if (!LN->isSimple() || !ISD::isNormalLoad(LN) || hasVolatileUser(LN)) 2883 return SDValue(); 2884 2885 SDLoc SL(N); 2886 SelectionDAG &DAG = DCI.DAG; 2887 EVT VT = LN->getMemoryVT(); 2888 2889 unsigned Size = VT.getStoreSize(); 2890 Align Alignment = LN->getAlign(); 2891 if (Alignment < Size && isTypeLegal(VT)) { 2892 bool IsFast; 2893 unsigned AS = LN->getAddressSpace(); 2894 2895 // Expand unaligned loads earlier than legalization. Due to visitation order 2896 // problems during legalization, the emitted instructions to pack and unpack 2897 // the bytes again are not eliminated in the case of an unaligned copy. 2898 if (!allowsMisalignedMemoryAccesses( 2899 VT, AS, Alignment, LN->getMemOperand()->getFlags(), &IsFast)) { 2900 SDValue Ops[2]; 2901 2902 if (VT.isVector()) 2903 std::tie(Ops[0], Ops[1]) = scalarizeVectorLoad(LN, DAG); 2904 else 2905 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(LN, DAG); 2906 2907 return DAG.getMergeValues(Ops, SDLoc(N)); 2908 } 2909 2910 if (!IsFast) 2911 return SDValue(); 2912 } 2913 2914 if (!shouldCombineMemoryType(VT)) 2915 return SDValue(); 2916 2917 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT); 2918 2919 SDValue NewLoad 2920 = DAG.getLoad(NewVT, SL, LN->getChain(), 2921 LN->getBasePtr(), LN->getMemOperand()); 2922 2923 SDValue BC = DAG.getNode(ISD::BITCAST, SL, VT, NewLoad); 2924 DCI.CombineTo(N, BC, NewLoad.getValue(1)); 2925 return SDValue(N, 0); 2926 } 2927 2928 // Replace store of an illegal type with a store of a bitcast to a friendlier 2929 // type. 2930 SDValue AMDGPUTargetLowering::performStoreCombine(SDNode *N, 2931 DAGCombinerInfo &DCI) const { 2932 if (!DCI.isBeforeLegalize()) 2933 return SDValue(); 2934 2935 StoreSDNode *SN = cast<StoreSDNode>(N); 2936 if (!SN->isSimple() || !ISD::isNormalStore(SN)) 2937 return SDValue(); 2938 2939 EVT VT = SN->getMemoryVT(); 2940 unsigned Size = VT.getStoreSize(); 2941 2942 SDLoc SL(N); 2943 SelectionDAG &DAG = DCI.DAG; 2944 Align Alignment = SN->getAlign(); 2945 if (Alignment < Size && isTypeLegal(VT)) { 2946 bool IsFast; 2947 unsigned AS = SN->getAddressSpace(); 2948 2949 // Expand unaligned stores earlier than legalization. Due to visitation 2950 // order problems during legalization, the emitted instructions to pack and 2951 // unpack the bytes again are not eliminated in the case of an unaligned 2952 // copy. 2953 if (!allowsMisalignedMemoryAccesses( 2954 VT, AS, Alignment, SN->getMemOperand()->getFlags(), &IsFast)) { 2955 if (VT.isVector()) 2956 return scalarizeVectorStore(SN, DAG); 2957 2958 return expandUnalignedStore(SN, DAG); 2959 } 2960 2961 if (!IsFast) 2962 return SDValue(); 2963 } 2964 2965 if (!shouldCombineMemoryType(VT)) 2966 return SDValue(); 2967 2968 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT); 2969 SDValue Val = SN->getValue(); 2970 2971 //DCI.AddToWorklist(Val.getNode()); 2972 2973 bool OtherUses = !Val.hasOneUse(); 2974 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NewVT, Val); 2975 if (OtherUses) { 2976 SDValue CastBack = DAG.getNode(ISD::BITCAST, SL, VT, CastVal); 2977 DAG.ReplaceAllUsesOfValueWith(Val, CastBack); 2978 } 2979 2980 return DAG.getStore(SN->getChain(), SL, CastVal, 2981 SN->getBasePtr(), SN->getMemOperand()); 2982 } 2983 2984 // FIXME: This should go in generic DAG combiner with an isTruncateFree check, 2985 // but isTruncateFree is inaccurate for i16 now because of SALU vs. VALU 2986 // issues. 2987 SDValue AMDGPUTargetLowering::performAssertSZExtCombine(SDNode *N, 2988 DAGCombinerInfo &DCI) const { 2989 SelectionDAG &DAG = DCI.DAG; 2990 SDValue N0 = N->getOperand(0); 2991 2992 // (vt2 (assertzext (truncate vt0:x), vt1)) -> 2993 // (vt2 (truncate (assertzext vt0:x, vt1))) 2994 if (N0.getOpcode() == ISD::TRUNCATE) { 2995 SDValue N1 = N->getOperand(1); 2996 EVT ExtVT = cast<VTSDNode>(N1)->getVT(); 2997 SDLoc SL(N); 2998 2999 SDValue Src = N0.getOperand(0); 3000 EVT SrcVT = Src.getValueType(); 3001 if (SrcVT.bitsGE(ExtVT)) { 3002 SDValue NewInReg = DAG.getNode(N->getOpcode(), SL, SrcVT, Src, N1); 3003 return DAG.getNode(ISD::TRUNCATE, SL, N->getValueType(0), NewInReg); 3004 } 3005 } 3006 3007 return SDValue(); 3008 } 3009 3010 SDValue AMDGPUTargetLowering::performIntrinsicWOChainCombine( 3011 SDNode *N, DAGCombinerInfo &DCI) const { 3012 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 3013 switch (IID) { 3014 case Intrinsic::amdgcn_mul_i24: 3015 case Intrinsic::amdgcn_mul_u24: 3016 return simplifyMul24(N, DCI); 3017 case Intrinsic::amdgcn_fract: 3018 case Intrinsic::amdgcn_rsq: 3019 case Intrinsic::amdgcn_rcp_legacy: 3020 case Intrinsic::amdgcn_rsq_legacy: 3021 case Intrinsic::amdgcn_rsq_clamp: 3022 case Intrinsic::amdgcn_ldexp: { 3023 // FIXME: This is probably wrong. If src is an sNaN, it won't be quieted 3024 SDValue Src = N->getOperand(1); 3025 return Src.isUndef() ? Src : SDValue(); 3026 } 3027 default: 3028 return SDValue(); 3029 } 3030 } 3031 3032 /// Split the 64-bit value \p LHS into two 32-bit components, and perform the 3033 /// binary operation \p Opc to it with the corresponding constant operands. 3034 SDValue AMDGPUTargetLowering::splitBinaryBitConstantOpImpl( 3035 DAGCombinerInfo &DCI, const SDLoc &SL, 3036 unsigned Opc, SDValue LHS, 3037 uint32_t ValLo, uint32_t ValHi) const { 3038 SelectionDAG &DAG = DCI.DAG; 3039 SDValue Lo, Hi; 3040 std::tie(Lo, Hi) = split64BitValue(LHS, DAG); 3041 3042 SDValue LoRHS = DAG.getConstant(ValLo, SL, MVT::i32); 3043 SDValue HiRHS = DAG.getConstant(ValHi, SL, MVT::i32); 3044 3045 SDValue LoAnd = DAG.getNode(Opc, SL, MVT::i32, Lo, LoRHS); 3046 SDValue HiAnd = DAG.getNode(Opc, SL, MVT::i32, Hi, HiRHS); 3047 3048 // Re-visit the ands. It's possible we eliminated one of them and it could 3049 // simplify the vector. 3050 DCI.AddToWorklist(Lo.getNode()); 3051 DCI.AddToWorklist(Hi.getNode()); 3052 3053 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {LoAnd, HiAnd}); 3054 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 3055 } 3056 3057 SDValue AMDGPUTargetLowering::performShlCombine(SDNode *N, 3058 DAGCombinerInfo &DCI) const { 3059 EVT VT = N->getValueType(0); 3060 3061 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 3062 if (!RHS) 3063 return SDValue(); 3064 3065 SDValue LHS = N->getOperand(0); 3066 unsigned RHSVal = RHS->getZExtValue(); 3067 if (!RHSVal) 3068 return LHS; 3069 3070 SDLoc SL(N); 3071 SelectionDAG &DAG = DCI.DAG; 3072 3073 switch (LHS->getOpcode()) { 3074 default: 3075 break; 3076 case ISD::ZERO_EXTEND: 3077 case ISD::SIGN_EXTEND: 3078 case ISD::ANY_EXTEND: { 3079 SDValue X = LHS->getOperand(0); 3080 3081 if (VT == MVT::i32 && RHSVal == 16 && X.getValueType() == MVT::i16 && 3082 isOperationLegal(ISD::BUILD_VECTOR, MVT::v2i16)) { 3083 // Prefer build_vector as the canonical form if packed types are legal. 3084 // (shl ([asz]ext i16:x), 16 -> build_vector 0, x 3085 SDValue Vec = DAG.getBuildVector(MVT::v2i16, SL, 3086 { DAG.getConstant(0, SL, MVT::i16), LHS->getOperand(0) }); 3087 return DAG.getNode(ISD::BITCAST, SL, MVT::i32, Vec); 3088 } 3089 3090 // shl (ext x) => zext (shl x), if shift does not overflow int 3091 if (VT != MVT::i64) 3092 break; 3093 KnownBits Known = DAG.computeKnownBits(X); 3094 unsigned LZ = Known.countMinLeadingZeros(); 3095 if (LZ < RHSVal) 3096 break; 3097 EVT XVT = X.getValueType(); 3098 SDValue Shl = DAG.getNode(ISD::SHL, SL, XVT, X, SDValue(RHS, 0)); 3099 return DAG.getZExtOrTrunc(Shl, SL, VT); 3100 } 3101 } 3102 3103 if (VT != MVT::i64) 3104 return SDValue(); 3105 3106 // i64 (shl x, C) -> (build_pair 0, (shl x, C -32)) 3107 3108 // On some subtargets, 64-bit shift is a quarter rate instruction. In the 3109 // common case, splitting this into a move and a 32-bit shift is faster and 3110 // the same code size. 3111 if (RHSVal < 32) 3112 return SDValue(); 3113 3114 SDValue ShiftAmt = DAG.getConstant(RHSVal - 32, SL, MVT::i32); 3115 3116 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS); 3117 SDValue NewShift = DAG.getNode(ISD::SHL, SL, MVT::i32, Lo, ShiftAmt); 3118 3119 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 3120 3121 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {Zero, NewShift}); 3122 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 3123 } 3124 3125 SDValue AMDGPUTargetLowering::performSraCombine(SDNode *N, 3126 DAGCombinerInfo &DCI) const { 3127 if (N->getValueType(0) != MVT::i64) 3128 return SDValue(); 3129 3130 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 3131 if (!RHS) 3132 return SDValue(); 3133 3134 SelectionDAG &DAG = DCI.DAG; 3135 SDLoc SL(N); 3136 unsigned RHSVal = RHS->getZExtValue(); 3137 3138 // (sra i64:x, 32) -> build_pair x, (sra hi_32(x), 31) 3139 if (RHSVal == 32) { 3140 SDValue Hi = getHiHalf64(N->getOperand(0), DAG); 3141 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi, 3142 DAG.getConstant(31, SL, MVT::i32)); 3143 3144 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {Hi, NewShift}); 3145 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec); 3146 } 3147 3148 // (sra i64:x, 63) -> build_pair (sra hi_32(x), 31), (sra hi_32(x), 31) 3149 if (RHSVal == 63) { 3150 SDValue Hi = getHiHalf64(N->getOperand(0), DAG); 3151 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi, 3152 DAG.getConstant(31, SL, MVT::i32)); 3153 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, NewShift}); 3154 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec); 3155 } 3156 3157 return SDValue(); 3158 } 3159 3160 SDValue AMDGPUTargetLowering::performSrlCombine(SDNode *N, 3161 DAGCombinerInfo &DCI) const { 3162 auto *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 3163 if (!RHS) 3164 return SDValue(); 3165 3166 EVT VT = N->getValueType(0); 3167 SDValue LHS = N->getOperand(0); 3168 unsigned ShiftAmt = RHS->getZExtValue(); 3169 SelectionDAG &DAG = DCI.DAG; 3170 SDLoc SL(N); 3171 3172 // fold (srl (and x, c1 << c2), c2) -> (and (srl(x, c2), c1) 3173 // this improves the ability to match BFE patterns in isel. 3174 if (LHS.getOpcode() == ISD::AND) { 3175 if (auto *Mask = dyn_cast<ConstantSDNode>(LHS.getOperand(1))) { 3176 if (Mask->getAPIntValue().isShiftedMask() && 3177 Mask->getAPIntValue().countTrailingZeros() == ShiftAmt) { 3178 return DAG.getNode( 3179 ISD::AND, SL, VT, 3180 DAG.getNode(ISD::SRL, SL, VT, LHS.getOperand(0), N->getOperand(1)), 3181 DAG.getNode(ISD::SRL, SL, VT, LHS.getOperand(1), N->getOperand(1))); 3182 } 3183 } 3184 } 3185 3186 if (VT != MVT::i64) 3187 return SDValue(); 3188 3189 if (ShiftAmt < 32) 3190 return SDValue(); 3191 3192 // srl i64:x, C for C >= 32 3193 // => 3194 // build_pair (srl hi_32(x), C - 32), 0 3195 SDValue One = DAG.getConstant(1, SL, MVT::i32); 3196 SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 3197 3198 SDValue VecOp = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, LHS); 3199 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecOp, One); 3200 3201 SDValue NewConst = DAG.getConstant(ShiftAmt - 32, SL, MVT::i32); 3202 SDValue NewShift = DAG.getNode(ISD::SRL, SL, MVT::i32, Hi, NewConst); 3203 3204 SDValue BuildPair = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, Zero}); 3205 3206 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildPair); 3207 } 3208 3209 SDValue AMDGPUTargetLowering::performTruncateCombine( 3210 SDNode *N, DAGCombinerInfo &DCI) const { 3211 SDLoc SL(N); 3212 SelectionDAG &DAG = DCI.DAG; 3213 EVT VT = N->getValueType(0); 3214 SDValue Src = N->getOperand(0); 3215 3216 // vt1 (truncate (bitcast (build_vector vt0:x, ...))) -> vt1 (bitcast vt0:x) 3217 if (Src.getOpcode() == ISD::BITCAST && !VT.isVector()) { 3218 SDValue Vec = Src.getOperand(0); 3219 if (Vec.getOpcode() == ISD::BUILD_VECTOR) { 3220 SDValue Elt0 = Vec.getOperand(0); 3221 EVT EltVT = Elt0.getValueType(); 3222 if (VT.getFixedSizeInBits() <= EltVT.getFixedSizeInBits()) { 3223 if (EltVT.isFloatingPoint()) { 3224 Elt0 = DAG.getNode(ISD::BITCAST, SL, 3225 EltVT.changeTypeToInteger(), Elt0); 3226 } 3227 3228 return DAG.getNode(ISD::TRUNCATE, SL, VT, Elt0); 3229 } 3230 } 3231 } 3232 3233 // Equivalent of above for accessing the high element of a vector as an 3234 // integer operation. 3235 // trunc (srl (bitcast (build_vector x, y))), 16 -> trunc (bitcast y) 3236 if (Src.getOpcode() == ISD::SRL && !VT.isVector()) { 3237 if (auto K = isConstOrConstSplat(Src.getOperand(1))) { 3238 if (2 * K->getZExtValue() == Src.getValueType().getScalarSizeInBits()) { 3239 SDValue BV = stripBitcast(Src.getOperand(0)); 3240 if (BV.getOpcode() == ISD::BUILD_VECTOR && 3241 BV.getValueType().getVectorNumElements() == 2) { 3242 SDValue SrcElt = BV.getOperand(1); 3243 EVT SrcEltVT = SrcElt.getValueType(); 3244 if (SrcEltVT.isFloatingPoint()) { 3245 SrcElt = DAG.getNode(ISD::BITCAST, SL, 3246 SrcEltVT.changeTypeToInteger(), SrcElt); 3247 } 3248 3249 return DAG.getNode(ISD::TRUNCATE, SL, VT, SrcElt); 3250 } 3251 } 3252 } 3253 } 3254 3255 // Partially shrink 64-bit shifts to 32-bit if reduced to 16-bit. 3256 // 3257 // i16 (trunc (srl i64:x, K)), K <= 16 -> 3258 // i16 (trunc (srl (i32 (trunc x), K))) 3259 if (VT.getScalarSizeInBits() < 32) { 3260 EVT SrcVT = Src.getValueType(); 3261 if (SrcVT.getScalarSizeInBits() > 32 && 3262 (Src.getOpcode() == ISD::SRL || 3263 Src.getOpcode() == ISD::SRA || 3264 Src.getOpcode() == ISD::SHL)) { 3265 SDValue Amt = Src.getOperand(1); 3266 KnownBits Known = DAG.computeKnownBits(Amt); 3267 unsigned Size = VT.getScalarSizeInBits(); 3268 if ((Known.isConstant() && Known.getConstant().ule(Size)) || 3269 (Known.getBitWidth() - Known.countMinLeadingZeros() <= Log2_32(Size))) { 3270 EVT MidVT = VT.isVector() ? 3271 EVT::getVectorVT(*DAG.getContext(), MVT::i32, 3272 VT.getVectorNumElements()) : MVT::i32; 3273 3274 EVT NewShiftVT = getShiftAmountTy(MidVT, DAG.getDataLayout()); 3275 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SL, MidVT, 3276 Src.getOperand(0)); 3277 DCI.AddToWorklist(Trunc.getNode()); 3278 3279 if (Amt.getValueType() != NewShiftVT) { 3280 Amt = DAG.getZExtOrTrunc(Amt, SL, NewShiftVT); 3281 DCI.AddToWorklist(Amt.getNode()); 3282 } 3283 3284 SDValue ShrunkShift = DAG.getNode(Src.getOpcode(), SL, MidVT, 3285 Trunc, Amt); 3286 return DAG.getNode(ISD::TRUNCATE, SL, VT, ShrunkShift); 3287 } 3288 } 3289 } 3290 3291 return SDValue(); 3292 } 3293 3294 // We need to specifically handle i64 mul here to avoid unnecessary conversion 3295 // instructions. If we only match on the legalized i64 mul expansion, 3296 // SimplifyDemandedBits will be unable to remove them because there will be 3297 // multiple uses due to the separate mul + mulh[su]. 3298 static SDValue getMul24(SelectionDAG &DAG, const SDLoc &SL, 3299 SDValue N0, SDValue N1, unsigned Size, bool Signed) { 3300 if (Size <= 32) { 3301 unsigned MulOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24; 3302 return DAG.getNode(MulOpc, SL, MVT::i32, N0, N1); 3303 } 3304 3305 unsigned MulLoOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24; 3306 unsigned MulHiOpc = Signed ? AMDGPUISD::MULHI_I24 : AMDGPUISD::MULHI_U24; 3307 3308 SDValue MulLo = DAG.getNode(MulLoOpc, SL, MVT::i32, N0, N1); 3309 SDValue MulHi = DAG.getNode(MulHiOpc, SL, MVT::i32, N0, N1); 3310 3311 return DAG.getNode(ISD::BUILD_PAIR, SL, MVT::i64, MulLo, MulHi); 3312 } 3313 3314 SDValue AMDGPUTargetLowering::performMulCombine(SDNode *N, 3315 DAGCombinerInfo &DCI) const { 3316 EVT VT = N->getValueType(0); 3317 3318 // Don't generate 24-bit multiplies on values that are in SGPRs, since 3319 // we only have a 32-bit scalar multiply (avoid values being moved to VGPRs 3320 // unnecessarily). isDivergent() is used as an approximation of whether the 3321 // value is in an SGPR. 3322 if (!N->isDivergent()) 3323 return SDValue(); 3324 3325 unsigned Size = VT.getSizeInBits(); 3326 if (VT.isVector() || Size > 64) 3327 return SDValue(); 3328 3329 // There are i16 integer mul/mad. 3330 if (Subtarget->has16BitInsts() && VT.getScalarType().bitsLE(MVT::i16)) 3331 return SDValue(); 3332 3333 SelectionDAG &DAG = DCI.DAG; 3334 SDLoc DL(N); 3335 3336 SDValue N0 = N->getOperand(0); 3337 SDValue N1 = N->getOperand(1); 3338 3339 // SimplifyDemandedBits has the annoying habit of turning useful zero_extends 3340 // in the source into any_extends if the result of the mul is truncated. Since 3341 // we can assume the high bits are whatever we want, use the underlying value 3342 // to avoid the unknown high bits from interfering. 3343 if (N0.getOpcode() == ISD::ANY_EXTEND) 3344 N0 = N0.getOperand(0); 3345 3346 if (N1.getOpcode() == ISD::ANY_EXTEND) 3347 N1 = N1.getOperand(0); 3348 3349 SDValue Mul; 3350 3351 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) { 3352 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32); 3353 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32); 3354 Mul = getMul24(DAG, DL, N0, N1, Size, false); 3355 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) { 3356 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32); 3357 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32); 3358 Mul = getMul24(DAG, DL, N0, N1, Size, true); 3359 } else { 3360 return SDValue(); 3361 } 3362 3363 // We need to use sext even for MUL_U24, because MUL_U24 is used 3364 // for signed multiply of 8 and 16-bit types. 3365 return DAG.getSExtOrTrunc(Mul, DL, VT); 3366 } 3367 3368 SDValue AMDGPUTargetLowering::performMulhsCombine(SDNode *N, 3369 DAGCombinerInfo &DCI) const { 3370 EVT VT = N->getValueType(0); 3371 3372 if (!Subtarget->hasMulI24() || VT.isVector()) 3373 return SDValue(); 3374 3375 SelectionDAG &DAG = DCI.DAG; 3376 SDLoc DL(N); 3377 3378 SDValue N0 = N->getOperand(0); 3379 SDValue N1 = N->getOperand(1); 3380 3381 if (!isI24(N0, DAG) || !isI24(N1, DAG)) 3382 return SDValue(); 3383 3384 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32); 3385 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32); 3386 3387 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_I24, DL, MVT::i32, N0, N1); 3388 DCI.AddToWorklist(Mulhi.getNode()); 3389 return DAG.getSExtOrTrunc(Mulhi, DL, VT); 3390 } 3391 3392 SDValue AMDGPUTargetLowering::performMulhuCombine(SDNode *N, 3393 DAGCombinerInfo &DCI) const { 3394 EVT VT = N->getValueType(0); 3395 3396 if (!Subtarget->hasMulU24() || VT.isVector() || VT.getSizeInBits() > 32) 3397 return SDValue(); 3398 3399 SelectionDAG &DAG = DCI.DAG; 3400 SDLoc DL(N); 3401 3402 SDValue N0 = N->getOperand(0); 3403 SDValue N1 = N->getOperand(1); 3404 3405 if (!isU24(N0, DAG) || !isU24(N1, DAG)) 3406 return SDValue(); 3407 3408 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32); 3409 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32); 3410 3411 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_U24, DL, MVT::i32, N0, N1); 3412 DCI.AddToWorklist(Mulhi.getNode()); 3413 return DAG.getZExtOrTrunc(Mulhi, DL, VT); 3414 } 3415 3416 static bool isNegativeOne(SDValue Val) { 3417 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) 3418 return C->isAllOnesValue(); 3419 return false; 3420 } 3421 3422 SDValue AMDGPUTargetLowering::getFFBX_U32(SelectionDAG &DAG, 3423 SDValue Op, 3424 const SDLoc &DL, 3425 unsigned Opc) const { 3426 EVT VT = Op.getValueType(); 3427 EVT LegalVT = getTypeToTransformTo(*DAG.getContext(), VT); 3428 if (LegalVT != MVT::i32 && (Subtarget->has16BitInsts() && 3429 LegalVT != MVT::i16)) 3430 return SDValue(); 3431 3432 if (VT != MVT::i32) 3433 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Op); 3434 3435 SDValue FFBX = DAG.getNode(Opc, DL, MVT::i32, Op); 3436 if (VT != MVT::i32) 3437 FFBX = DAG.getNode(ISD::TRUNCATE, DL, VT, FFBX); 3438 3439 return FFBX; 3440 } 3441 3442 // The native instructions return -1 on 0 input. Optimize out a select that 3443 // produces -1 on 0. 3444 // 3445 // TODO: If zero is not undef, we could also do this if the output is compared 3446 // against the bitwidth. 3447 // 3448 // TODO: Should probably combine against FFBH_U32 instead of ctlz directly. 3449 SDValue AMDGPUTargetLowering::performCtlz_CttzCombine(const SDLoc &SL, SDValue Cond, 3450 SDValue LHS, SDValue RHS, 3451 DAGCombinerInfo &DCI) const { 3452 ConstantSDNode *CmpRhs = dyn_cast<ConstantSDNode>(Cond.getOperand(1)); 3453 if (!CmpRhs || !CmpRhs->isNullValue()) 3454 return SDValue(); 3455 3456 SelectionDAG &DAG = DCI.DAG; 3457 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); 3458 SDValue CmpLHS = Cond.getOperand(0); 3459 3460 // select (setcc x, 0, eq), -1, (ctlz_zero_undef x) -> ffbh_u32 x 3461 // select (setcc x, 0, eq), -1, (cttz_zero_undef x) -> ffbl_u32 x 3462 if (CCOpcode == ISD::SETEQ && 3463 (isCtlzOpc(RHS.getOpcode()) || isCttzOpc(RHS.getOpcode())) && 3464 RHS.getOperand(0) == CmpLHS && isNegativeOne(LHS)) { 3465 unsigned Opc = 3466 isCttzOpc(RHS.getOpcode()) ? AMDGPUISD::FFBL_B32 : AMDGPUISD::FFBH_U32; 3467 return getFFBX_U32(DAG, CmpLHS, SL, Opc); 3468 } 3469 3470 // select (setcc x, 0, ne), (ctlz_zero_undef x), -1 -> ffbh_u32 x 3471 // select (setcc x, 0, ne), (cttz_zero_undef x), -1 -> ffbl_u32 x 3472 if (CCOpcode == ISD::SETNE && 3473 (isCtlzOpc(LHS.getOpcode()) || isCttzOpc(LHS.getOpcode())) && 3474 LHS.getOperand(0) == CmpLHS && isNegativeOne(RHS)) { 3475 unsigned Opc = 3476 isCttzOpc(LHS.getOpcode()) ? AMDGPUISD::FFBL_B32 : AMDGPUISD::FFBH_U32; 3477 3478 return getFFBX_U32(DAG, CmpLHS, SL, Opc); 3479 } 3480 3481 return SDValue(); 3482 } 3483 3484 static SDValue distributeOpThroughSelect(TargetLowering::DAGCombinerInfo &DCI, 3485 unsigned Op, 3486 const SDLoc &SL, 3487 SDValue Cond, 3488 SDValue N1, 3489 SDValue N2) { 3490 SelectionDAG &DAG = DCI.DAG; 3491 EVT VT = N1.getValueType(); 3492 3493 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT, Cond, 3494 N1.getOperand(0), N2.getOperand(0)); 3495 DCI.AddToWorklist(NewSelect.getNode()); 3496 return DAG.getNode(Op, SL, VT, NewSelect); 3497 } 3498 3499 // Pull a free FP operation out of a select so it may fold into uses. 3500 // 3501 // select c, (fneg x), (fneg y) -> fneg (select c, x, y) 3502 // select c, (fneg x), k -> fneg (select c, x, (fneg k)) 3503 // 3504 // select c, (fabs x), (fabs y) -> fabs (select c, x, y) 3505 // select c, (fabs x), +k -> fabs (select c, x, k) 3506 static SDValue foldFreeOpFromSelect(TargetLowering::DAGCombinerInfo &DCI, 3507 SDValue N) { 3508 SelectionDAG &DAG = DCI.DAG; 3509 SDValue Cond = N.getOperand(0); 3510 SDValue LHS = N.getOperand(1); 3511 SDValue RHS = N.getOperand(2); 3512 3513 EVT VT = N.getValueType(); 3514 if ((LHS.getOpcode() == ISD::FABS && RHS.getOpcode() == ISD::FABS) || 3515 (LHS.getOpcode() == ISD::FNEG && RHS.getOpcode() == ISD::FNEG)) { 3516 return distributeOpThroughSelect(DCI, LHS.getOpcode(), 3517 SDLoc(N), Cond, LHS, RHS); 3518 } 3519 3520 bool Inv = false; 3521 if (RHS.getOpcode() == ISD::FABS || RHS.getOpcode() == ISD::FNEG) { 3522 std::swap(LHS, RHS); 3523 Inv = true; 3524 } 3525 3526 // TODO: Support vector constants. 3527 ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS); 3528 if ((LHS.getOpcode() == ISD::FNEG || LHS.getOpcode() == ISD::FABS) && CRHS) { 3529 SDLoc SL(N); 3530 // If one side is an fneg/fabs and the other is a constant, we can push the 3531 // fneg/fabs down. If it's an fabs, the constant needs to be non-negative. 3532 SDValue NewLHS = LHS.getOperand(0); 3533 SDValue NewRHS = RHS; 3534 3535 // Careful: if the neg can be folded up, don't try to pull it back down. 3536 bool ShouldFoldNeg = true; 3537 3538 if (NewLHS.hasOneUse()) { 3539 unsigned Opc = NewLHS.getOpcode(); 3540 if (LHS.getOpcode() == ISD::FNEG && fnegFoldsIntoOp(Opc)) 3541 ShouldFoldNeg = false; 3542 if (LHS.getOpcode() == ISD::FABS && Opc == ISD::FMUL) 3543 ShouldFoldNeg = false; 3544 } 3545 3546 if (ShouldFoldNeg) { 3547 if (LHS.getOpcode() == ISD::FNEG) 3548 NewRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3549 else if (CRHS->isNegative()) 3550 return SDValue(); 3551 3552 if (Inv) 3553 std::swap(NewLHS, NewRHS); 3554 3555 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT, 3556 Cond, NewLHS, NewRHS); 3557 DCI.AddToWorklist(NewSelect.getNode()); 3558 return DAG.getNode(LHS.getOpcode(), SL, VT, NewSelect); 3559 } 3560 } 3561 3562 return SDValue(); 3563 } 3564 3565 3566 SDValue AMDGPUTargetLowering::performSelectCombine(SDNode *N, 3567 DAGCombinerInfo &DCI) const { 3568 if (SDValue Folded = foldFreeOpFromSelect(DCI, SDValue(N, 0))) 3569 return Folded; 3570 3571 SDValue Cond = N->getOperand(0); 3572 if (Cond.getOpcode() != ISD::SETCC) 3573 return SDValue(); 3574 3575 EVT VT = N->getValueType(0); 3576 SDValue LHS = Cond.getOperand(0); 3577 SDValue RHS = Cond.getOperand(1); 3578 SDValue CC = Cond.getOperand(2); 3579 3580 SDValue True = N->getOperand(1); 3581 SDValue False = N->getOperand(2); 3582 3583 if (Cond.hasOneUse()) { // TODO: Look for multiple select uses. 3584 SelectionDAG &DAG = DCI.DAG; 3585 if (DAG.isConstantValueOfAnyType(True) && 3586 !DAG.isConstantValueOfAnyType(False)) { 3587 // Swap cmp + select pair to move constant to false input. 3588 // This will allow using VOPC cndmasks more often. 3589 // select (setcc x, y), k, x -> select (setccinv x, y), x, k 3590 3591 SDLoc SL(N); 3592 ISD::CondCode NewCC = 3593 getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), LHS.getValueType()); 3594 3595 SDValue NewCond = DAG.getSetCC(SL, Cond.getValueType(), LHS, RHS, NewCC); 3596 return DAG.getNode(ISD::SELECT, SL, VT, NewCond, False, True); 3597 } 3598 3599 if (VT == MVT::f32 && Subtarget->hasFminFmaxLegacy()) { 3600 SDValue MinMax 3601 = combineFMinMaxLegacy(SDLoc(N), VT, LHS, RHS, True, False, CC, DCI); 3602 // Revisit this node so we can catch min3/max3/med3 patterns. 3603 //DCI.AddToWorklist(MinMax.getNode()); 3604 return MinMax; 3605 } 3606 } 3607 3608 // There's no reason to not do this if the condition has other uses. 3609 return performCtlz_CttzCombine(SDLoc(N), Cond, True, False, DCI); 3610 } 3611 3612 static bool isInv2Pi(const APFloat &APF) { 3613 static const APFloat KF16(APFloat::IEEEhalf(), APInt(16, 0x3118)); 3614 static const APFloat KF32(APFloat::IEEEsingle(), APInt(32, 0x3e22f983)); 3615 static const APFloat KF64(APFloat::IEEEdouble(), APInt(64, 0x3fc45f306dc9c882)); 3616 3617 return APF.bitwiseIsEqual(KF16) || 3618 APF.bitwiseIsEqual(KF32) || 3619 APF.bitwiseIsEqual(KF64); 3620 } 3621 3622 // 0 and 1.0 / (0.5 * pi) do not have inline immmediates, so there is an 3623 // additional cost to negate them. 3624 bool AMDGPUTargetLowering::isConstantCostlierToNegate(SDValue N) const { 3625 if (const ConstantFPSDNode *C = isConstOrConstSplatFP(N)) { 3626 if (C->isZero() && !C->isNegative()) 3627 return true; 3628 3629 if (Subtarget->hasInv2PiInlineImm() && isInv2Pi(C->getValueAPF())) 3630 return true; 3631 } 3632 3633 return false; 3634 } 3635 3636 static unsigned inverseMinMax(unsigned Opc) { 3637 switch (Opc) { 3638 case ISD::FMAXNUM: 3639 return ISD::FMINNUM; 3640 case ISD::FMINNUM: 3641 return ISD::FMAXNUM; 3642 case ISD::FMAXNUM_IEEE: 3643 return ISD::FMINNUM_IEEE; 3644 case ISD::FMINNUM_IEEE: 3645 return ISD::FMAXNUM_IEEE; 3646 case AMDGPUISD::FMAX_LEGACY: 3647 return AMDGPUISD::FMIN_LEGACY; 3648 case AMDGPUISD::FMIN_LEGACY: 3649 return AMDGPUISD::FMAX_LEGACY; 3650 default: 3651 llvm_unreachable("invalid min/max opcode"); 3652 } 3653 } 3654 3655 SDValue AMDGPUTargetLowering::performFNegCombine(SDNode *N, 3656 DAGCombinerInfo &DCI) const { 3657 SelectionDAG &DAG = DCI.DAG; 3658 SDValue N0 = N->getOperand(0); 3659 EVT VT = N->getValueType(0); 3660 3661 unsigned Opc = N0.getOpcode(); 3662 3663 // If the input has multiple uses and we can either fold the negate down, or 3664 // the other uses cannot, give up. This both prevents unprofitable 3665 // transformations and infinite loops: we won't repeatedly try to fold around 3666 // a negate that has no 'good' form. 3667 if (N0.hasOneUse()) { 3668 // This may be able to fold into the source, but at a code size cost. Don't 3669 // fold if the fold into the user is free. 3670 if (allUsesHaveSourceMods(N, 0)) 3671 return SDValue(); 3672 } else { 3673 if (fnegFoldsIntoOp(Opc) && 3674 (allUsesHaveSourceMods(N) || !allUsesHaveSourceMods(N0.getNode()))) 3675 return SDValue(); 3676 } 3677 3678 SDLoc SL(N); 3679 switch (Opc) { 3680 case ISD::FADD: { 3681 if (!mayIgnoreSignedZero(N0)) 3682 return SDValue(); 3683 3684 // (fneg (fadd x, y)) -> (fadd (fneg x), (fneg y)) 3685 SDValue LHS = N0.getOperand(0); 3686 SDValue RHS = N0.getOperand(1); 3687 3688 if (LHS.getOpcode() != ISD::FNEG) 3689 LHS = DAG.getNode(ISD::FNEG, SL, VT, LHS); 3690 else 3691 LHS = LHS.getOperand(0); 3692 3693 if (RHS.getOpcode() != ISD::FNEG) 3694 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3695 else 3696 RHS = RHS.getOperand(0); 3697 3698 SDValue Res = DAG.getNode(ISD::FADD, SL, VT, LHS, RHS, N0->getFlags()); 3699 if (Res.getOpcode() != ISD::FADD) 3700 return SDValue(); // Op got folded away. 3701 if (!N0.hasOneUse()) 3702 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 3703 return Res; 3704 } 3705 case ISD::FMUL: 3706 case AMDGPUISD::FMUL_LEGACY: { 3707 // (fneg (fmul x, y)) -> (fmul x, (fneg y)) 3708 // (fneg (fmul_legacy x, y)) -> (fmul_legacy x, (fneg y)) 3709 SDValue LHS = N0.getOperand(0); 3710 SDValue RHS = N0.getOperand(1); 3711 3712 if (LHS.getOpcode() == ISD::FNEG) 3713 LHS = LHS.getOperand(0); 3714 else if (RHS.getOpcode() == ISD::FNEG) 3715 RHS = RHS.getOperand(0); 3716 else 3717 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3718 3719 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, RHS, N0->getFlags()); 3720 if (Res.getOpcode() != Opc) 3721 return SDValue(); // Op got folded away. 3722 if (!N0.hasOneUse()) 3723 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 3724 return Res; 3725 } 3726 case ISD::FMA: 3727 case ISD::FMAD: { 3728 // TODO: handle llvm.amdgcn.fma.legacy 3729 if (!mayIgnoreSignedZero(N0)) 3730 return SDValue(); 3731 3732 // (fneg (fma x, y, z)) -> (fma x, (fneg y), (fneg z)) 3733 SDValue LHS = N0.getOperand(0); 3734 SDValue MHS = N0.getOperand(1); 3735 SDValue RHS = N0.getOperand(2); 3736 3737 if (LHS.getOpcode() == ISD::FNEG) 3738 LHS = LHS.getOperand(0); 3739 else if (MHS.getOpcode() == ISD::FNEG) 3740 MHS = MHS.getOperand(0); 3741 else 3742 MHS = DAG.getNode(ISD::FNEG, SL, VT, MHS); 3743 3744 if (RHS.getOpcode() != ISD::FNEG) 3745 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3746 else 3747 RHS = RHS.getOperand(0); 3748 3749 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, MHS, RHS); 3750 if (Res.getOpcode() != Opc) 3751 return SDValue(); // Op got folded away. 3752 if (!N0.hasOneUse()) 3753 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 3754 return Res; 3755 } 3756 case ISD::FMAXNUM: 3757 case ISD::FMINNUM: 3758 case ISD::FMAXNUM_IEEE: 3759 case ISD::FMINNUM_IEEE: 3760 case AMDGPUISD::FMAX_LEGACY: 3761 case AMDGPUISD::FMIN_LEGACY: { 3762 // fneg (fmaxnum x, y) -> fminnum (fneg x), (fneg y) 3763 // fneg (fminnum x, y) -> fmaxnum (fneg x), (fneg y) 3764 // fneg (fmax_legacy x, y) -> fmin_legacy (fneg x), (fneg y) 3765 // fneg (fmin_legacy x, y) -> fmax_legacy (fneg x), (fneg y) 3766 3767 SDValue LHS = N0.getOperand(0); 3768 SDValue RHS = N0.getOperand(1); 3769 3770 // 0 doesn't have a negated inline immediate. 3771 // TODO: This constant check should be generalized to other operations. 3772 if (isConstantCostlierToNegate(RHS)) 3773 return SDValue(); 3774 3775 SDValue NegLHS = DAG.getNode(ISD::FNEG, SL, VT, LHS); 3776 SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3777 unsigned Opposite = inverseMinMax(Opc); 3778 3779 SDValue Res = DAG.getNode(Opposite, SL, VT, NegLHS, NegRHS, N0->getFlags()); 3780 if (Res.getOpcode() != Opposite) 3781 return SDValue(); // Op got folded away. 3782 if (!N0.hasOneUse()) 3783 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 3784 return Res; 3785 } 3786 case AMDGPUISD::FMED3: { 3787 SDValue Ops[3]; 3788 for (unsigned I = 0; I < 3; ++I) 3789 Ops[I] = DAG.getNode(ISD::FNEG, SL, VT, N0->getOperand(I), N0->getFlags()); 3790 3791 SDValue Res = DAG.getNode(AMDGPUISD::FMED3, SL, VT, Ops, N0->getFlags()); 3792 if (Res.getOpcode() != AMDGPUISD::FMED3) 3793 return SDValue(); // Op got folded away. 3794 3795 if (!N0.hasOneUse()) { 3796 SDValue Neg = DAG.getNode(ISD::FNEG, SL, VT, Res); 3797 DAG.ReplaceAllUsesWith(N0, Neg); 3798 3799 for (SDNode *U : Neg->uses()) 3800 DCI.AddToWorklist(U); 3801 } 3802 3803 return Res; 3804 } 3805 case ISD::FP_EXTEND: 3806 case ISD::FTRUNC: 3807 case ISD::FRINT: 3808 case ISD::FNEARBYINT: // XXX - Should fround be handled? 3809 case ISD::FSIN: 3810 case ISD::FCANONICALIZE: 3811 case AMDGPUISD::RCP: 3812 case AMDGPUISD::RCP_LEGACY: 3813 case AMDGPUISD::RCP_IFLAG: 3814 case AMDGPUISD::SIN_HW: { 3815 SDValue CvtSrc = N0.getOperand(0); 3816 if (CvtSrc.getOpcode() == ISD::FNEG) { 3817 // (fneg (fp_extend (fneg x))) -> (fp_extend x) 3818 // (fneg (rcp (fneg x))) -> (rcp x) 3819 return DAG.getNode(Opc, SL, VT, CvtSrc.getOperand(0)); 3820 } 3821 3822 if (!N0.hasOneUse()) 3823 return SDValue(); 3824 3825 // (fneg (fp_extend x)) -> (fp_extend (fneg x)) 3826 // (fneg (rcp x)) -> (rcp (fneg x)) 3827 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc); 3828 return DAG.getNode(Opc, SL, VT, Neg, N0->getFlags()); 3829 } 3830 case ISD::FP_ROUND: { 3831 SDValue CvtSrc = N0.getOperand(0); 3832 3833 if (CvtSrc.getOpcode() == ISD::FNEG) { 3834 // (fneg (fp_round (fneg x))) -> (fp_round x) 3835 return DAG.getNode(ISD::FP_ROUND, SL, VT, 3836 CvtSrc.getOperand(0), N0.getOperand(1)); 3837 } 3838 3839 if (!N0.hasOneUse()) 3840 return SDValue(); 3841 3842 // (fneg (fp_round x)) -> (fp_round (fneg x)) 3843 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc); 3844 return DAG.getNode(ISD::FP_ROUND, SL, VT, Neg, N0.getOperand(1)); 3845 } 3846 case ISD::FP16_TO_FP: { 3847 // v_cvt_f32_f16 supports source modifiers on pre-VI targets without legal 3848 // f16, but legalization of f16 fneg ends up pulling it out of the source. 3849 // Put the fneg back as a legal source operation that can be matched later. 3850 SDLoc SL(N); 3851 3852 SDValue Src = N0.getOperand(0); 3853 EVT SrcVT = Src.getValueType(); 3854 3855 // fneg (fp16_to_fp x) -> fp16_to_fp (xor x, 0x8000) 3856 SDValue IntFNeg = DAG.getNode(ISD::XOR, SL, SrcVT, Src, 3857 DAG.getConstant(0x8000, SL, SrcVT)); 3858 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFNeg); 3859 } 3860 default: 3861 return SDValue(); 3862 } 3863 } 3864 3865 SDValue AMDGPUTargetLowering::performFAbsCombine(SDNode *N, 3866 DAGCombinerInfo &DCI) const { 3867 SelectionDAG &DAG = DCI.DAG; 3868 SDValue N0 = N->getOperand(0); 3869 3870 if (!N0.hasOneUse()) 3871 return SDValue(); 3872 3873 switch (N0.getOpcode()) { 3874 case ISD::FP16_TO_FP: { 3875 assert(!Subtarget->has16BitInsts() && "should only see if f16 is illegal"); 3876 SDLoc SL(N); 3877 SDValue Src = N0.getOperand(0); 3878 EVT SrcVT = Src.getValueType(); 3879 3880 // fabs (fp16_to_fp x) -> fp16_to_fp (and x, 0x7fff) 3881 SDValue IntFAbs = DAG.getNode(ISD::AND, SL, SrcVT, Src, 3882 DAG.getConstant(0x7fff, SL, SrcVT)); 3883 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFAbs); 3884 } 3885 default: 3886 return SDValue(); 3887 } 3888 } 3889 3890 SDValue AMDGPUTargetLowering::performRcpCombine(SDNode *N, 3891 DAGCombinerInfo &DCI) const { 3892 const auto *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); 3893 if (!CFP) 3894 return SDValue(); 3895 3896 // XXX - Should this flush denormals? 3897 const APFloat &Val = CFP->getValueAPF(); 3898 APFloat One(Val.getSemantics(), "1.0"); 3899 return DCI.DAG.getConstantFP(One / Val, SDLoc(N), N->getValueType(0)); 3900 } 3901 3902 SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N, 3903 DAGCombinerInfo &DCI) const { 3904 SelectionDAG &DAG = DCI.DAG; 3905 SDLoc DL(N); 3906 3907 switch(N->getOpcode()) { 3908 default: 3909 break; 3910 case ISD::BITCAST: { 3911 EVT DestVT = N->getValueType(0); 3912 3913 // Push casts through vector builds. This helps avoid emitting a large 3914 // number of copies when materializing floating point vector constants. 3915 // 3916 // vNt1 bitcast (vNt0 (build_vector t0:x, t0:y)) => 3917 // vnt1 = build_vector (t1 (bitcast t0:x)), (t1 (bitcast t0:y)) 3918 if (DestVT.isVector()) { 3919 SDValue Src = N->getOperand(0); 3920 if (Src.getOpcode() == ISD::BUILD_VECTOR) { 3921 EVT SrcVT = Src.getValueType(); 3922 unsigned NElts = DestVT.getVectorNumElements(); 3923 3924 if (SrcVT.getVectorNumElements() == NElts) { 3925 EVT DestEltVT = DestVT.getVectorElementType(); 3926 3927 SmallVector<SDValue, 8> CastedElts; 3928 SDLoc SL(N); 3929 for (unsigned I = 0, E = SrcVT.getVectorNumElements(); I != E; ++I) { 3930 SDValue Elt = Src.getOperand(I); 3931 CastedElts.push_back(DAG.getNode(ISD::BITCAST, DL, DestEltVT, Elt)); 3932 } 3933 3934 return DAG.getBuildVector(DestVT, SL, CastedElts); 3935 } 3936 } 3937 } 3938 3939 if (DestVT.getSizeInBits() != 64 || !DestVT.isVector()) 3940 break; 3941 3942 // Fold bitcasts of constants. 3943 // 3944 // v2i32 (bitcast i64:k) -> build_vector lo_32(k), hi_32(k) 3945 // TODO: Generalize and move to DAGCombiner 3946 SDValue Src = N->getOperand(0); 3947 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src)) { 3948 SDLoc SL(N); 3949 uint64_t CVal = C->getZExtValue(); 3950 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, 3951 DAG.getConstant(Lo_32(CVal), SL, MVT::i32), 3952 DAG.getConstant(Hi_32(CVal), SL, MVT::i32)); 3953 return DAG.getNode(ISD::BITCAST, SL, DestVT, BV); 3954 } 3955 3956 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Src)) { 3957 const APInt &Val = C->getValueAPF().bitcastToAPInt(); 3958 SDLoc SL(N); 3959 uint64_t CVal = Val.getZExtValue(); 3960 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, 3961 DAG.getConstant(Lo_32(CVal), SL, MVT::i32), 3962 DAG.getConstant(Hi_32(CVal), SL, MVT::i32)); 3963 3964 return DAG.getNode(ISD::BITCAST, SL, DestVT, Vec); 3965 } 3966 3967 break; 3968 } 3969 case ISD::SHL: { 3970 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 3971 break; 3972 3973 return performShlCombine(N, DCI); 3974 } 3975 case ISD::SRL: { 3976 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 3977 break; 3978 3979 return performSrlCombine(N, DCI); 3980 } 3981 case ISD::SRA: { 3982 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 3983 break; 3984 3985 return performSraCombine(N, DCI); 3986 } 3987 case ISD::TRUNCATE: 3988 return performTruncateCombine(N, DCI); 3989 case ISD::MUL: 3990 return performMulCombine(N, DCI); 3991 case ISD::MULHS: 3992 return performMulhsCombine(N, DCI); 3993 case ISD::MULHU: 3994 return performMulhuCombine(N, DCI); 3995 case AMDGPUISD::MUL_I24: 3996 case AMDGPUISD::MUL_U24: 3997 case AMDGPUISD::MULHI_I24: 3998 case AMDGPUISD::MULHI_U24: 3999 return simplifyMul24(N, DCI); 4000 case ISD::SELECT: 4001 return performSelectCombine(N, DCI); 4002 case ISD::FNEG: 4003 return performFNegCombine(N, DCI); 4004 case ISD::FABS: 4005 return performFAbsCombine(N, DCI); 4006 case AMDGPUISD::BFE_I32: 4007 case AMDGPUISD::BFE_U32: { 4008 assert(!N->getValueType(0).isVector() && 4009 "Vector handling of BFE not implemented"); 4010 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2)); 4011 if (!Width) 4012 break; 4013 4014 uint32_t WidthVal = Width->getZExtValue() & 0x1f; 4015 if (WidthVal == 0) 4016 return DAG.getConstant(0, DL, MVT::i32); 4017 4018 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); 4019 if (!Offset) 4020 break; 4021 4022 SDValue BitsFrom = N->getOperand(0); 4023 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f; 4024 4025 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32; 4026 4027 if (OffsetVal == 0) { 4028 // This is already sign / zero extended, so try to fold away extra BFEs. 4029 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal); 4030 4031 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom); 4032 if (OpSignBits >= SignBits) 4033 return BitsFrom; 4034 4035 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal); 4036 if (Signed) { 4037 // This is a sign_extend_inreg. Replace it to take advantage of existing 4038 // DAG Combines. If not eliminated, we will match back to BFE during 4039 // selection. 4040 4041 // TODO: The sext_inreg of extended types ends, although we can could 4042 // handle them in a single BFE. 4043 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom, 4044 DAG.getValueType(SmallVT)); 4045 } 4046 4047 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT); 4048 } 4049 4050 if (ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(BitsFrom)) { 4051 if (Signed) { 4052 return constantFoldBFE<int32_t>(DAG, 4053 CVal->getSExtValue(), 4054 OffsetVal, 4055 WidthVal, 4056 DL); 4057 } 4058 4059 return constantFoldBFE<uint32_t>(DAG, 4060 CVal->getZExtValue(), 4061 OffsetVal, 4062 WidthVal, 4063 DL); 4064 } 4065 4066 if ((OffsetVal + WidthVal) >= 32 && 4067 !(Subtarget->hasSDWA() && OffsetVal == 16 && WidthVal == 16)) { 4068 SDValue ShiftVal = DAG.getConstant(OffsetVal, DL, MVT::i32); 4069 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32, 4070 BitsFrom, ShiftVal); 4071 } 4072 4073 if (BitsFrom.hasOneUse()) { 4074 APInt Demanded = APInt::getBitsSet(32, 4075 OffsetVal, 4076 OffsetVal + WidthVal); 4077 4078 KnownBits Known; 4079 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), 4080 !DCI.isBeforeLegalizeOps()); 4081 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4082 if (TLI.ShrinkDemandedConstant(BitsFrom, Demanded, TLO) || 4083 TLI.SimplifyDemandedBits(BitsFrom, Demanded, Known, TLO)) { 4084 DCI.CommitTargetLoweringOpt(TLO); 4085 } 4086 } 4087 4088 break; 4089 } 4090 case ISD::LOAD: 4091 return performLoadCombine(N, DCI); 4092 case ISD::STORE: 4093 return performStoreCombine(N, DCI); 4094 case AMDGPUISD::RCP: 4095 case AMDGPUISD::RCP_IFLAG: 4096 return performRcpCombine(N, DCI); 4097 case ISD::AssertZext: 4098 case ISD::AssertSext: 4099 return performAssertSZExtCombine(N, DCI); 4100 case ISD::INTRINSIC_WO_CHAIN: 4101 return performIntrinsicWOChainCombine(N, DCI); 4102 } 4103 return SDValue(); 4104 } 4105 4106 //===----------------------------------------------------------------------===// 4107 // Helper functions 4108 //===----------------------------------------------------------------------===// 4109 4110 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG, 4111 const TargetRegisterClass *RC, 4112 Register Reg, EVT VT, 4113 const SDLoc &SL, 4114 bool RawReg) const { 4115 MachineFunction &MF = DAG.getMachineFunction(); 4116 MachineRegisterInfo &MRI = MF.getRegInfo(); 4117 Register VReg; 4118 4119 if (!MRI.isLiveIn(Reg)) { 4120 VReg = MRI.createVirtualRegister(RC); 4121 MRI.addLiveIn(Reg, VReg); 4122 } else { 4123 VReg = MRI.getLiveInVirtReg(Reg); 4124 } 4125 4126 if (RawReg) 4127 return DAG.getRegister(VReg, VT); 4128 4129 return DAG.getCopyFromReg(DAG.getEntryNode(), SL, VReg, VT); 4130 } 4131 4132 // This may be called multiple times, and nothing prevents creating multiple 4133 // objects at the same offset. See if we already defined this object. 4134 static int getOrCreateFixedStackObject(MachineFrameInfo &MFI, unsigned Size, 4135 int64_t Offset) { 4136 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) { 4137 if (MFI.getObjectOffset(I) == Offset) { 4138 assert(MFI.getObjectSize(I) == Size); 4139 return I; 4140 } 4141 } 4142 4143 return MFI.CreateFixedObject(Size, Offset, true); 4144 } 4145 4146 SDValue AMDGPUTargetLowering::loadStackInputValue(SelectionDAG &DAG, 4147 EVT VT, 4148 const SDLoc &SL, 4149 int64_t Offset) const { 4150 MachineFunction &MF = DAG.getMachineFunction(); 4151 MachineFrameInfo &MFI = MF.getFrameInfo(); 4152 int FI = getOrCreateFixedStackObject(MFI, VT.getStoreSize(), Offset); 4153 4154 auto SrcPtrInfo = MachinePointerInfo::getStack(MF, Offset); 4155 SDValue Ptr = DAG.getFrameIndex(FI, MVT::i32); 4156 4157 return DAG.getLoad(VT, SL, DAG.getEntryNode(), Ptr, SrcPtrInfo, Align(4), 4158 MachineMemOperand::MODereferenceable | 4159 MachineMemOperand::MOInvariant); 4160 } 4161 4162 SDValue AMDGPUTargetLowering::storeStackInputValue(SelectionDAG &DAG, 4163 const SDLoc &SL, 4164 SDValue Chain, 4165 SDValue ArgVal, 4166 int64_t Offset) const { 4167 MachineFunction &MF = DAG.getMachineFunction(); 4168 MachinePointerInfo DstInfo = MachinePointerInfo::getStack(MF, Offset); 4169 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 4170 4171 SDValue Ptr = DAG.getConstant(Offset, SL, MVT::i32); 4172 // Stores to the argument stack area are relative to the stack pointer. 4173 SDValue SP = 4174 DAG.getCopyFromReg(Chain, SL, Info->getStackPtrOffsetReg(), MVT::i32); 4175 Ptr = DAG.getNode(ISD::ADD, SL, MVT::i32, SP, Ptr); 4176 SDValue Store = DAG.getStore(Chain, SL, ArgVal, Ptr, DstInfo, Align(4), 4177 MachineMemOperand::MODereferenceable); 4178 return Store; 4179 } 4180 4181 SDValue AMDGPUTargetLowering::loadInputValue(SelectionDAG &DAG, 4182 const TargetRegisterClass *RC, 4183 EVT VT, const SDLoc &SL, 4184 const ArgDescriptor &Arg) const { 4185 assert(Arg && "Attempting to load missing argument"); 4186 4187 SDValue V = Arg.isRegister() ? 4188 CreateLiveInRegister(DAG, RC, Arg.getRegister(), VT, SL) : 4189 loadStackInputValue(DAG, VT, SL, Arg.getStackOffset()); 4190 4191 if (!Arg.isMasked()) 4192 return V; 4193 4194 unsigned Mask = Arg.getMask(); 4195 unsigned Shift = countTrailingZeros<unsigned>(Mask); 4196 V = DAG.getNode(ISD::SRL, SL, VT, V, 4197 DAG.getShiftAmountConstant(Shift, VT, SL)); 4198 return DAG.getNode(ISD::AND, SL, VT, V, 4199 DAG.getConstant(Mask >> Shift, SL, VT)); 4200 } 4201 4202 uint32_t AMDGPUTargetLowering::getImplicitParameterOffset( 4203 const MachineFunction &MF, const ImplicitParameter Param) const { 4204 const AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>(); 4205 const AMDGPUSubtarget &ST = 4206 AMDGPUSubtarget::get(getTargetMachine(), MF.getFunction()); 4207 unsigned ExplicitArgOffset = ST.getExplicitKernelArgOffset(MF.getFunction()); 4208 const Align Alignment = ST.getAlignmentForImplicitArgPtr(); 4209 uint64_t ArgOffset = alignTo(MFI->getExplicitKernArgSize(), Alignment) + 4210 ExplicitArgOffset; 4211 switch (Param) { 4212 case GRID_DIM: 4213 return ArgOffset; 4214 case GRID_OFFSET: 4215 return ArgOffset + 4; 4216 } 4217 llvm_unreachable("unexpected implicit parameter type"); 4218 } 4219 4220 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node; 4221 4222 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const { 4223 switch ((AMDGPUISD::NodeType)Opcode) { 4224 case AMDGPUISD::FIRST_NUMBER: break; 4225 // AMDIL DAG nodes 4226 NODE_NAME_CASE(UMUL); 4227 NODE_NAME_CASE(BRANCH_COND); 4228 4229 // AMDGPU DAG nodes 4230 NODE_NAME_CASE(IF) 4231 NODE_NAME_CASE(ELSE) 4232 NODE_NAME_CASE(LOOP) 4233 NODE_NAME_CASE(CALL) 4234 NODE_NAME_CASE(TC_RETURN) 4235 NODE_NAME_CASE(TRAP) 4236 NODE_NAME_CASE(RET_FLAG) 4237 NODE_NAME_CASE(RETURN_TO_EPILOG) 4238 NODE_NAME_CASE(ENDPGM) 4239 NODE_NAME_CASE(DWORDADDR) 4240 NODE_NAME_CASE(FRACT) 4241 NODE_NAME_CASE(SETCC) 4242 NODE_NAME_CASE(SETREG) 4243 NODE_NAME_CASE(DENORM_MODE) 4244 NODE_NAME_CASE(FMA_W_CHAIN) 4245 NODE_NAME_CASE(FMUL_W_CHAIN) 4246 NODE_NAME_CASE(CLAMP) 4247 NODE_NAME_CASE(COS_HW) 4248 NODE_NAME_CASE(SIN_HW) 4249 NODE_NAME_CASE(FMAX_LEGACY) 4250 NODE_NAME_CASE(FMIN_LEGACY) 4251 NODE_NAME_CASE(FMAX3) 4252 NODE_NAME_CASE(SMAX3) 4253 NODE_NAME_CASE(UMAX3) 4254 NODE_NAME_CASE(FMIN3) 4255 NODE_NAME_CASE(SMIN3) 4256 NODE_NAME_CASE(UMIN3) 4257 NODE_NAME_CASE(FMED3) 4258 NODE_NAME_CASE(SMED3) 4259 NODE_NAME_CASE(UMED3) 4260 NODE_NAME_CASE(FDOT2) 4261 NODE_NAME_CASE(URECIP) 4262 NODE_NAME_CASE(DIV_SCALE) 4263 NODE_NAME_CASE(DIV_FMAS) 4264 NODE_NAME_CASE(DIV_FIXUP) 4265 NODE_NAME_CASE(FMAD_FTZ) 4266 NODE_NAME_CASE(RCP) 4267 NODE_NAME_CASE(RSQ) 4268 NODE_NAME_CASE(RCP_LEGACY) 4269 NODE_NAME_CASE(RCP_IFLAG) 4270 NODE_NAME_CASE(FMUL_LEGACY) 4271 NODE_NAME_CASE(RSQ_CLAMP) 4272 NODE_NAME_CASE(LDEXP) 4273 NODE_NAME_CASE(FP_CLASS) 4274 NODE_NAME_CASE(DOT4) 4275 NODE_NAME_CASE(CARRY) 4276 NODE_NAME_CASE(BORROW) 4277 NODE_NAME_CASE(BFE_U32) 4278 NODE_NAME_CASE(BFE_I32) 4279 NODE_NAME_CASE(BFI) 4280 NODE_NAME_CASE(BFM) 4281 NODE_NAME_CASE(FFBH_U32) 4282 NODE_NAME_CASE(FFBH_I32) 4283 NODE_NAME_CASE(FFBL_B32) 4284 NODE_NAME_CASE(MUL_U24) 4285 NODE_NAME_CASE(MUL_I24) 4286 NODE_NAME_CASE(MULHI_U24) 4287 NODE_NAME_CASE(MULHI_I24) 4288 NODE_NAME_CASE(MAD_U24) 4289 NODE_NAME_CASE(MAD_I24) 4290 NODE_NAME_CASE(MAD_I64_I32) 4291 NODE_NAME_CASE(MAD_U64_U32) 4292 NODE_NAME_CASE(PERM) 4293 NODE_NAME_CASE(TEXTURE_FETCH) 4294 NODE_NAME_CASE(R600_EXPORT) 4295 NODE_NAME_CASE(CONST_ADDRESS) 4296 NODE_NAME_CASE(REGISTER_LOAD) 4297 NODE_NAME_CASE(REGISTER_STORE) 4298 NODE_NAME_CASE(SAMPLE) 4299 NODE_NAME_CASE(SAMPLEB) 4300 NODE_NAME_CASE(SAMPLED) 4301 NODE_NAME_CASE(SAMPLEL) 4302 NODE_NAME_CASE(CVT_F32_UBYTE0) 4303 NODE_NAME_CASE(CVT_F32_UBYTE1) 4304 NODE_NAME_CASE(CVT_F32_UBYTE2) 4305 NODE_NAME_CASE(CVT_F32_UBYTE3) 4306 NODE_NAME_CASE(CVT_PKRTZ_F16_F32) 4307 NODE_NAME_CASE(CVT_PKNORM_I16_F32) 4308 NODE_NAME_CASE(CVT_PKNORM_U16_F32) 4309 NODE_NAME_CASE(CVT_PK_I16_I32) 4310 NODE_NAME_CASE(CVT_PK_U16_U32) 4311 NODE_NAME_CASE(FP_TO_FP16) 4312 NODE_NAME_CASE(FP16_ZEXT) 4313 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR) 4314 NODE_NAME_CASE(CONST_DATA_PTR) 4315 NODE_NAME_CASE(PC_ADD_REL_OFFSET) 4316 NODE_NAME_CASE(LDS) 4317 NODE_NAME_CASE(DUMMY_CHAIN) 4318 case AMDGPUISD::FIRST_MEM_OPCODE_NUMBER: break; 4319 NODE_NAME_CASE(LOAD_D16_HI) 4320 NODE_NAME_CASE(LOAD_D16_LO) 4321 NODE_NAME_CASE(LOAD_D16_HI_I8) 4322 NODE_NAME_CASE(LOAD_D16_HI_U8) 4323 NODE_NAME_CASE(LOAD_D16_LO_I8) 4324 NODE_NAME_CASE(LOAD_D16_LO_U8) 4325 NODE_NAME_CASE(STORE_MSKOR) 4326 NODE_NAME_CASE(LOAD_CONSTANT) 4327 NODE_NAME_CASE(TBUFFER_STORE_FORMAT) 4328 NODE_NAME_CASE(TBUFFER_STORE_FORMAT_D16) 4329 NODE_NAME_CASE(TBUFFER_LOAD_FORMAT) 4330 NODE_NAME_CASE(TBUFFER_LOAD_FORMAT_D16) 4331 NODE_NAME_CASE(DS_ORDERED_COUNT) 4332 NODE_NAME_CASE(ATOMIC_CMP_SWAP) 4333 NODE_NAME_CASE(ATOMIC_INC) 4334 NODE_NAME_CASE(ATOMIC_DEC) 4335 NODE_NAME_CASE(ATOMIC_LOAD_FMIN) 4336 NODE_NAME_CASE(ATOMIC_LOAD_FMAX) 4337 NODE_NAME_CASE(BUFFER_LOAD) 4338 NODE_NAME_CASE(BUFFER_LOAD_UBYTE) 4339 NODE_NAME_CASE(BUFFER_LOAD_USHORT) 4340 NODE_NAME_CASE(BUFFER_LOAD_BYTE) 4341 NODE_NAME_CASE(BUFFER_LOAD_SHORT) 4342 NODE_NAME_CASE(BUFFER_LOAD_FORMAT) 4343 NODE_NAME_CASE(BUFFER_LOAD_FORMAT_D16) 4344 NODE_NAME_CASE(SBUFFER_LOAD) 4345 NODE_NAME_CASE(BUFFER_STORE) 4346 NODE_NAME_CASE(BUFFER_STORE_BYTE) 4347 NODE_NAME_CASE(BUFFER_STORE_SHORT) 4348 NODE_NAME_CASE(BUFFER_STORE_FORMAT) 4349 NODE_NAME_CASE(BUFFER_STORE_FORMAT_D16) 4350 NODE_NAME_CASE(BUFFER_ATOMIC_SWAP) 4351 NODE_NAME_CASE(BUFFER_ATOMIC_ADD) 4352 NODE_NAME_CASE(BUFFER_ATOMIC_SUB) 4353 NODE_NAME_CASE(BUFFER_ATOMIC_SMIN) 4354 NODE_NAME_CASE(BUFFER_ATOMIC_UMIN) 4355 NODE_NAME_CASE(BUFFER_ATOMIC_SMAX) 4356 NODE_NAME_CASE(BUFFER_ATOMIC_UMAX) 4357 NODE_NAME_CASE(BUFFER_ATOMIC_AND) 4358 NODE_NAME_CASE(BUFFER_ATOMIC_OR) 4359 NODE_NAME_CASE(BUFFER_ATOMIC_XOR) 4360 NODE_NAME_CASE(BUFFER_ATOMIC_INC) 4361 NODE_NAME_CASE(BUFFER_ATOMIC_DEC) 4362 NODE_NAME_CASE(BUFFER_ATOMIC_CMPSWAP) 4363 NODE_NAME_CASE(BUFFER_ATOMIC_CSUB) 4364 NODE_NAME_CASE(BUFFER_ATOMIC_FADD) 4365 NODE_NAME_CASE(BUFFER_ATOMIC_FMIN) 4366 NODE_NAME_CASE(BUFFER_ATOMIC_FMAX) 4367 4368 case AMDGPUISD::LAST_AMDGPU_ISD_NUMBER: break; 4369 } 4370 return nullptr; 4371 } 4372 4373 SDValue AMDGPUTargetLowering::getSqrtEstimate(SDValue Operand, 4374 SelectionDAG &DAG, int Enabled, 4375 int &RefinementSteps, 4376 bool &UseOneConstNR, 4377 bool Reciprocal) const { 4378 EVT VT = Operand.getValueType(); 4379 4380 if (VT == MVT::f32) { 4381 RefinementSteps = 0; 4382 return DAG.getNode(AMDGPUISD::RSQ, SDLoc(Operand), VT, Operand); 4383 } 4384 4385 // TODO: There is also f64 rsq instruction, but the documentation is less 4386 // clear on its precision. 4387 4388 return SDValue(); 4389 } 4390 4391 SDValue AMDGPUTargetLowering::getRecipEstimate(SDValue Operand, 4392 SelectionDAG &DAG, int Enabled, 4393 int &RefinementSteps) const { 4394 EVT VT = Operand.getValueType(); 4395 4396 if (VT == MVT::f32) { 4397 // Reciprocal, < 1 ulp error. 4398 // 4399 // This reciprocal approximation converges to < 0.5 ulp error with one 4400 // newton rhapson performed with two fused multiple adds (FMAs). 4401 4402 RefinementSteps = 0; 4403 return DAG.getNode(AMDGPUISD::RCP, SDLoc(Operand), VT, Operand); 4404 } 4405 4406 // TODO: There is also f64 rcp instruction, but the documentation is less 4407 // clear on its precision. 4408 4409 return SDValue(); 4410 } 4411 4412 void AMDGPUTargetLowering::computeKnownBitsForTargetNode( 4413 const SDValue Op, KnownBits &Known, 4414 const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth) const { 4415 4416 Known.resetAll(); // Don't know anything. 4417 4418 unsigned Opc = Op.getOpcode(); 4419 4420 switch (Opc) { 4421 default: 4422 break; 4423 case AMDGPUISD::CARRY: 4424 case AMDGPUISD::BORROW: { 4425 Known.Zero = APInt::getHighBitsSet(32, 31); 4426 break; 4427 } 4428 4429 case AMDGPUISD::BFE_I32: 4430 case AMDGPUISD::BFE_U32: { 4431 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 4432 if (!CWidth) 4433 return; 4434 4435 uint32_t Width = CWidth->getZExtValue() & 0x1f; 4436 4437 if (Opc == AMDGPUISD::BFE_U32) 4438 Known.Zero = APInt::getHighBitsSet(32, 32 - Width); 4439 4440 break; 4441 } 4442 case AMDGPUISD::FP_TO_FP16: 4443 case AMDGPUISD::FP16_ZEXT: { 4444 unsigned BitWidth = Known.getBitWidth(); 4445 4446 // High bits are zero. 4447 Known.Zero = APInt::getHighBitsSet(BitWidth, BitWidth - 16); 4448 break; 4449 } 4450 case AMDGPUISD::MUL_U24: 4451 case AMDGPUISD::MUL_I24: { 4452 KnownBits LHSKnown = DAG.computeKnownBits(Op.getOperand(0), Depth + 1); 4453 KnownBits RHSKnown = DAG.computeKnownBits(Op.getOperand(1), Depth + 1); 4454 unsigned TrailZ = LHSKnown.countMinTrailingZeros() + 4455 RHSKnown.countMinTrailingZeros(); 4456 Known.Zero.setLowBits(std::min(TrailZ, 32u)); 4457 // Skip extra check if all bits are known zeros. 4458 if (TrailZ >= 32) 4459 break; 4460 4461 // Truncate to 24 bits. 4462 LHSKnown = LHSKnown.trunc(24); 4463 RHSKnown = RHSKnown.trunc(24); 4464 4465 if (Opc == AMDGPUISD::MUL_I24) { 4466 unsigned LHSValBits = 24 - LHSKnown.countMinSignBits(); 4467 unsigned RHSValBits = 24 - RHSKnown.countMinSignBits(); 4468 unsigned MaxValBits = std::min(LHSValBits + RHSValBits, 32u); 4469 if (MaxValBits >= 32) 4470 break; 4471 bool LHSNegative = LHSKnown.isNegative(); 4472 bool LHSNonNegative = LHSKnown.isNonNegative(); 4473 bool LHSPositive = LHSKnown.isStrictlyPositive(); 4474 bool RHSNegative = RHSKnown.isNegative(); 4475 bool RHSNonNegative = RHSKnown.isNonNegative(); 4476 bool RHSPositive = RHSKnown.isStrictlyPositive(); 4477 4478 if ((LHSNonNegative && RHSNonNegative) || (LHSNegative && RHSNegative)) 4479 Known.Zero.setHighBits(32 - MaxValBits); 4480 else if ((LHSNegative && RHSPositive) || (LHSPositive && RHSNegative)) 4481 Known.One.setHighBits(32 - MaxValBits); 4482 } else { 4483 unsigned LHSValBits = 24 - LHSKnown.countMinLeadingZeros(); 4484 unsigned RHSValBits = 24 - RHSKnown.countMinLeadingZeros(); 4485 unsigned MaxValBits = std::min(LHSValBits + RHSValBits, 32u); 4486 if (MaxValBits >= 32) 4487 break; 4488 Known.Zero.setHighBits(32 - MaxValBits); 4489 } 4490 break; 4491 } 4492 case AMDGPUISD::PERM: { 4493 ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 4494 if (!CMask) 4495 return; 4496 4497 KnownBits LHSKnown = DAG.computeKnownBits(Op.getOperand(0), Depth + 1); 4498 KnownBits RHSKnown = DAG.computeKnownBits(Op.getOperand(1), Depth + 1); 4499 unsigned Sel = CMask->getZExtValue(); 4500 4501 for (unsigned I = 0; I < 32; I += 8) { 4502 unsigned SelBits = Sel & 0xff; 4503 if (SelBits < 4) { 4504 SelBits *= 8; 4505 Known.One |= ((RHSKnown.One.getZExtValue() >> SelBits) & 0xff) << I; 4506 Known.Zero |= ((RHSKnown.Zero.getZExtValue() >> SelBits) & 0xff) << I; 4507 } else if (SelBits < 7) { 4508 SelBits = (SelBits & 3) * 8; 4509 Known.One |= ((LHSKnown.One.getZExtValue() >> SelBits) & 0xff) << I; 4510 Known.Zero |= ((LHSKnown.Zero.getZExtValue() >> SelBits) & 0xff) << I; 4511 } else if (SelBits == 0x0c) { 4512 Known.Zero |= 0xFFull << I; 4513 } else if (SelBits > 0x0c) { 4514 Known.One |= 0xFFull << I; 4515 } 4516 Sel >>= 8; 4517 } 4518 break; 4519 } 4520 case AMDGPUISD::BUFFER_LOAD_UBYTE: { 4521 Known.Zero.setHighBits(24); 4522 break; 4523 } 4524 case AMDGPUISD::BUFFER_LOAD_USHORT: { 4525 Known.Zero.setHighBits(16); 4526 break; 4527 } 4528 case AMDGPUISD::LDS: { 4529 auto GA = cast<GlobalAddressSDNode>(Op.getOperand(0).getNode()); 4530 Align Alignment = GA->getGlobal()->getPointerAlignment(DAG.getDataLayout()); 4531 4532 Known.Zero.setHighBits(16); 4533 Known.Zero.setLowBits(Log2(Alignment)); 4534 break; 4535 } 4536 case ISD::INTRINSIC_WO_CHAIN: { 4537 unsigned IID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4538 switch (IID) { 4539 case Intrinsic::amdgcn_mbcnt_lo: 4540 case Intrinsic::amdgcn_mbcnt_hi: { 4541 const GCNSubtarget &ST = 4542 DAG.getMachineFunction().getSubtarget<GCNSubtarget>(); 4543 // These return at most the wavefront size - 1. 4544 unsigned Size = Op.getValueType().getSizeInBits(); 4545 Known.Zero.setHighBits(Size - ST.getWavefrontSizeLog2()); 4546 break; 4547 } 4548 default: 4549 break; 4550 } 4551 } 4552 } 4553 } 4554 4555 unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode( 4556 SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, 4557 unsigned Depth) const { 4558 switch (Op.getOpcode()) { 4559 case AMDGPUISD::BFE_I32: { 4560 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 4561 if (!Width) 4562 return 1; 4563 4564 unsigned SignBits = 32 - Width->getZExtValue() + 1; 4565 if (!isNullConstant(Op.getOperand(1))) 4566 return SignBits; 4567 4568 // TODO: Could probably figure something out with non-0 offsets. 4569 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1); 4570 return std::max(SignBits, Op0SignBits); 4571 } 4572 4573 case AMDGPUISD::BFE_U32: { 4574 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 4575 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1; 4576 } 4577 4578 case AMDGPUISD::CARRY: 4579 case AMDGPUISD::BORROW: 4580 return 31; 4581 case AMDGPUISD::BUFFER_LOAD_BYTE: 4582 return 25; 4583 case AMDGPUISD::BUFFER_LOAD_SHORT: 4584 return 17; 4585 case AMDGPUISD::BUFFER_LOAD_UBYTE: 4586 return 24; 4587 case AMDGPUISD::BUFFER_LOAD_USHORT: 4588 return 16; 4589 case AMDGPUISD::FP_TO_FP16: 4590 case AMDGPUISD::FP16_ZEXT: 4591 return 16; 4592 default: 4593 return 1; 4594 } 4595 } 4596 4597 unsigned AMDGPUTargetLowering::computeNumSignBitsForTargetInstr( 4598 GISelKnownBits &Analysis, Register R, 4599 const APInt &DemandedElts, const MachineRegisterInfo &MRI, 4600 unsigned Depth) const { 4601 const MachineInstr *MI = MRI.getVRegDef(R); 4602 if (!MI) 4603 return 1; 4604 4605 // TODO: Check range metadata on MMO. 4606 switch (MI->getOpcode()) { 4607 case AMDGPU::G_AMDGPU_BUFFER_LOAD_SBYTE: 4608 return 25; 4609 case AMDGPU::G_AMDGPU_BUFFER_LOAD_SSHORT: 4610 return 17; 4611 case AMDGPU::G_AMDGPU_BUFFER_LOAD_UBYTE: 4612 return 24; 4613 case AMDGPU::G_AMDGPU_BUFFER_LOAD_USHORT: 4614 return 16; 4615 default: 4616 return 1; 4617 } 4618 } 4619 4620 bool AMDGPUTargetLowering::isKnownNeverNaNForTargetNode(SDValue Op, 4621 const SelectionDAG &DAG, 4622 bool SNaN, 4623 unsigned Depth) const { 4624 unsigned Opcode = Op.getOpcode(); 4625 switch (Opcode) { 4626 case AMDGPUISD::FMIN_LEGACY: 4627 case AMDGPUISD::FMAX_LEGACY: { 4628 if (SNaN) 4629 return true; 4630 4631 // TODO: Can check no nans on one of the operands for each one, but which 4632 // one? 4633 return false; 4634 } 4635 case AMDGPUISD::FMUL_LEGACY: 4636 case AMDGPUISD::CVT_PKRTZ_F16_F32: { 4637 if (SNaN) 4638 return true; 4639 return DAG.isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) && 4640 DAG.isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1); 4641 } 4642 case AMDGPUISD::FMED3: 4643 case AMDGPUISD::FMIN3: 4644 case AMDGPUISD::FMAX3: 4645 case AMDGPUISD::FMAD_FTZ: { 4646 if (SNaN) 4647 return true; 4648 return DAG.isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) && 4649 DAG.isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) && 4650 DAG.isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1); 4651 } 4652 case AMDGPUISD::CVT_F32_UBYTE0: 4653 case AMDGPUISD::CVT_F32_UBYTE1: 4654 case AMDGPUISD::CVT_F32_UBYTE2: 4655 case AMDGPUISD::CVT_F32_UBYTE3: 4656 return true; 4657 4658 case AMDGPUISD::RCP: 4659 case AMDGPUISD::RSQ: 4660 case AMDGPUISD::RCP_LEGACY: 4661 case AMDGPUISD::RSQ_CLAMP: { 4662 if (SNaN) 4663 return true; 4664 4665 // TODO: Need is known positive check. 4666 return false; 4667 } 4668 case AMDGPUISD::LDEXP: 4669 case AMDGPUISD::FRACT: { 4670 if (SNaN) 4671 return true; 4672 return DAG.isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1); 4673 } 4674 case AMDGPUISD::DIV_SCALE: 4675 case AMDGPUISD::DIV_FMAS: 4676 case AMDGPUISD::DIV_FIXUP: 4677 // TODO: Refine on operands. 4678 return SNaN; 4679 case AMDGPUISD::SIN_HW: 4680 case AMDGPUISD::COS_HW: { 4681 // TODO: Need check for infinity 4682 return SNaN; 4683 } 4684 case ISD::INTRINSIC_WO_CHAIN: { 4685 unsigned IntrinsicID 4686 = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4687 // TODO: Handle more intrinsics 4688 switch (IntrinsicID) { 4689 case Intrinsic::amdgcn_cubeid: 4690 return true; 4691 4692 case Intrinsic::amdgcn_frexp_mant: { 4693 if (SNaN) 4694 return true; 4695 return DAG.isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1); 4696 } 4697 case Intrinsic::amdgcn_cvt_pkrtz: { 4698 if (SNaN) 4699 return true; 4700 return DAG.isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) && 4701 DAG.isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1); 4702 } 4703 case Intrinsic::amdgcn_rcp: 4704 case Intrinsic::amdgcn_rsq: 4705 case Intrinsic::amdgcn_rcp_legacy: 4706 case Intrinsic::amdgcn_rsq_legacy: 4707 case Intrinsic::amdgcn_rsq_clamp: { 4708 if (SNaN) 4709 return true; 4710 4711 // TODO: Need is known positive check. 4712 return false; 4713 } 4714 case Intrinsic::amdgcn_trig_preop: 4715 case Intrinsic::amdgcn_fdot2: 4716 // TODO: Refine on operand 4717 return SNaN; 4718 case Intrinsic::amdgcn_fma_legacy: 4719 if (SNaN) 4720 return true; 4721 return DAG.isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) && 4722 DAG.isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1) && 4723 DAG.isKnownNeverNaN(Op.getOperand(3), SNaN, Depth + 1); 4724 default: 4725 return false; 4726 } 4727 } 4728 default: 4729 return false; 4730 } 4731 } 4732 4733 TargetLowering::AtomicExpansionKind 4734 AMDGPUTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *RMW) const { 4735 switch (RMW->getOperation()) { 4736 case AtomicRMWInst::Nand: 4737 case AtomicRMWInst::FAdd: 4738 case AtomicRMWInst::FSub: 4739 return AtomicExpansionKind::CmpXChg; 4740 default: 4741 return AtomicExpansionKind::None; 4742 } 4743 } 4744