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 // Report this based on the end legalized type. 852 VT = VT.getScalarType(); 853 return VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f16; 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::LowerFP_TO_INT64(SDValue Op, SelectionDAG &DAG, 2595 bool Signed) const { 2596 SDLoc SL(Op); 2597 2598 SDValue Src = Op.getOperand(0); 2599 EVT SrcVT = Src.getValueType(); 2600 2601 assert(SrcVT == MVT::f32 || SrcVT == MVT::f64); 2602 2603 // The basic idea of converting a floating point number into a pair of 32-bit 2604 // integers is illustrated as follows: 2605 // 2606 // tf := trunc(val); 2607 // hif := floor(tf * 2^-32); 2608 // lof := tf - hif * 2^32; // lof is always positive due to floor. 2609 // hi := fptoi(hif); 2610 // lo := fptoi(lof); 2611 // 2612 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, SrcVT, Src); 2613 SDValue Sign; 2614 if (Signed && SrcVT == MVT::f32) { 2615 // However, a 32-bit floating point number has only 23 bits mantissa and 2616 // it's not enough to hold all the significant bits of `lof` if val is 2617 // negative. To avoid the loss of precision, We need to take the absolute 2618 // value after truncating and flip the result back based on the original 2619 // signedness. 2620 Sign = DAG.getNode(ISD::SRA, SL, MVT::i32, 2621 DAG.getNode(ISD::BITCAST, SL, MVT::i32, Trunc), 2622 DAG.getConstant(31, SL, MVT::i32)); 2623 Trunc = DAG.getNode(ISD::FABS, SL, SrcVT, Trunc); 2624 } 2625 2626 SDValue K0, K1; 2627 if (SrcVT == MVT::f64) { 2628 K0 = DAG.getConstantFP(BitsToDouble(UINT64_C(/*2^-32*/ 0x3df0000000000000)), 2629 SL, SrcVT); 2630 K1 = DAG.getConstantFP(BitsToDouble(UINT64_C(/*-2^32*/ 0xc1f0000000000000)), 2631 SL, SrcVT); 2632 } else { 2633 K0 = DAG.getConstantFP(BitsToFloat(UINT32_C(/*2^-32*/ 0x2f800000)), SL, 2634 SrcVT); 2635 K1 = DAG.getConstantFP(BitsToFloat(UINT32_C(/*-2^32*/ 0xcf800000)), SL, 2636 SrcVT); 2637 } 2638 // TODO: Should this propagate fast-math-flags? 2639 SDValue Mul = DAG.getNode(ISD::FMUL, SL, SrcVT, Trunc, K0); 2640 2641 SDValue FloorMul = DAG.getNode(ISD::FFLOOR, SL, SrcVT, Mul); 2642 2643 SDValue Fma = DAG.getNode(ISD::FMA, SL, SrcVT, FloorMul, K1, Trunc); 2644 2645 SDValue Hi = DAG.getNode((Signed && SrcVT == MVT::f64) ? ISD::FP_TO_SINT 2646 : ISD::FP_TO_UINT, 2647 SL, MVT::i32, FloorMul); 2648 SDValue Lo = DAG.getNode(ISD::FP_TO_UINT, SL, MVT::i32, Fma); 2649 2650 SDValue Result = DAG.getNode(ISD::BITCAST, SL, MVT::i64, 2651 DAG.getBuildVector(MVT::v2i32, SL, {Lo, Hi})); 2652 2653 if (Signed && SrcVT == MVT::f32) { 2654 assert(Sign); 2655 // Flip the result based on the signedness, which is either all 0s or 1s. 2656 Sign = DAG.getNode(ISD::BITCAST, SL, MVT::i64, 2657 DAG.getBuildVector(MVT::v2i32, SL, {Sign, Sign})); 2658 // r := xor(r, sign) - sign; 2659 Result = 2660 DAG.getNode(ISD::SUB, SL, MVT::i64, 2661 DAG.getNode(ISD::XOR, SL, MVT::i64, Result, Sign), Sign); 2662 } 2663 2664 return Result; 2665 } 2666 2667 SDValue AMDGPUTargetLowering::LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const { 2668 SDLoc DL(Op); 2669 SDValue N0 = Op.getOperand(0); 2670 2671 // Convert to target node to get known bits 2672 if (N0.getValueType() == MVT::f32) 2673 return DAG.getNode(AMDGPUISD::FP_TO_FP16, DL, Op.getValueType(), N0); 2674 2675 if (getTargetMachine().Options.UnsafeFPMath) { 2676 // There is a generic expand for FP_TO_FP16 with unsafe fast math. 2677 return SDValue(); 2678 } 2679 2680 assert(N0.getSimpleValueType() == MVT::f64); 2681 2682 // f64 -> f16 conversion using round-to-nearest-even rounding mode. 2683 const unsigned ExpMask = 0x7ff; 2684 const unsigned ExpBiasf64 = 1023; 2685 const unsigned ExpBiasf16 = 15; 2686 SDValue Zero = DAG.getConstant(0, DL, MVT::i32); 2687 SDValue One = DAG.getConstant(1, DL, MVT::i32); 2688 SDValue U = DAG.getNode(ISD::BITCAST, DL, MVT::i64, N0); 2689 SDValue UH = DAG.getNode(ISD::SRL, DL, MVT::i64, U, 2690 DAG.getConstant(32, DL, MVT::i64)); 2691 UH = DAG.getZExtOrTrunc(UH, DL, MVT::i32); 2692 U = DAG.getZExtOrTrunc(U, DL, MVT::i32); 2693 SDValue E = DAG.getNode(ISD::SRL, DL, MVT::i32, UH, 2694 DAG.getConstant(20, DL, MVT::i64)); 2695 E = DAG.getNode(ISD::AND, DL, MVT::i32, E, 2696 DAG.getConstant(ExpMask, DL, MVT::i32)); 2697 // Subtract the fp64 exponent bias (1023) to get the real exponent and 2698 // add the f16 bias (15) to get the biased exponent for the f16 format. 2699 E = DAG.getNode(ISD::ADD, DL, MVT::i32, E, 2700 DAG.getConstant(-ExpBiasf64 + ExpBiasf16, DL, MVT::i32)); 2701 2702 SDValue M = DAG.getNode(ISD::SRL, DL, MVT::i32, UH, 2703 DAG.getConstant(8, DL, MVT::i32)); 2704 M = DAG.getNode(ISD::AND, DL, MVT::i32, M, 2705 DAG.getConstant(0xffe, DL, MVT::i32)); 2706 2707 SDValue MaskedSig = DAG.getNode(ISD::AND, DL, MVT::i32, UH, 2708 DAG.getConstant(0x1ff, DL, MVT::i32)); 2709 MaskedSig = DAG.getNode(ISD::OR, DL, MVT::i32, MaskedSig, U); 2710 2711 SDValue Lo40Set = DAG.getSelectCC(DL, MaskedSig, Zero, Zero, One, ISD::SETEQ); 2712 M = DAG.getNode(ISD::OR, DL, MVT::i32, M, Lo40Set); 2713 2714 // (M != 0 ? 0x0200 : 0) | 0x7c00; 2715 SDValue I = DAG.getNode(ISD::OR, DL, MVT::i32, 2716 DAG.getSelectCC(DL, M, Zero, DAG.getConstant(0x0200, DL, MVT::i32), 2717 Zero, ISD::SETNE), DAG.getConstant(0x7c00, DL, MVT::i32)); 2718 2719 // N = M | (E << 12); 2720 SDValue N = DAG.getNode(ISD::OR, DL, MVT::i32, M, 2721 DAG.getNode(ISD::SHL, DL, MVT::i32, E, 2722 DAG.getConstant(12, DL, MVT::i32))); 2723 2724 // B = clamp(1-E, 0, 13); 2725 SDValue OneSubExp = DAG.getNode(ISD::SUB, DL, MVT::i32, 2726 One, E); 2727 SDValue B = DAG.getNode(ISD::SMAX, DL, MVT::i32, OneSubExp, Zero); 2728 B = DAG.getNode(ISD::SMIN, DL, MVT::i32, B, 2729 DAG.getConstant(13, DL, MVT::i32)); 2730 2731 SDValue SigSetHigh = DAG.getNode(ISD::OR, DL, MVT::i32, M, 2732 DAG.getConstant(0x1000, DL, MVT::i32)); 2733 2734 SDValue D = DAG.getNode(ISD::SRL, DL, MVT::i32, SigSetHigh, B); 2735 SDValue D0 = DAG.getNode(ISD::SHL, DL, MVT::i32, D, B); 2736 SDValue D1 = DAG.getSelectCC(DL, D0, SigSetHigh, One, Zero, ISD::SETNE); 2737 D = DAG.getNode(ISD::OR, DL, MVT::i32, D, D1); 2738 2739 SDValue V = DAG.getSelectCC(DL, E, One, D, N, ISD::SETLT); 2740 SDValue VLow3 = DAG.getNode(ISD::AND, DL, MVT::i32, V, 2741 DAG.getConstant(0x7, DL, MVT::i32)); 2742 V = DAG.getNode(ISD::SRL, DL, MVT::i32, V, 2743 DAG.getConstant(2, DL, MVT::i32)); 2744 SDValue V0 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(3, DL, MVT::i32), 2745 One, Zero, ISD::SETEQ); 2746 SDValue V1 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(5, DL, MVT::i32), 2747 One, Zero, ISD::SETGT); 2748 V1 = DAG.getNode(ISD::OR, DL, MVT::i32, V0, V1); 2749 V = DAG.getNode(ISD::ADD, DL, MVT::i32, V, V1); 2750 2751 V = DAG.getSelectCC(DL, E, DAG.getConstant(30, DL, MVT::i32), 2752 DAG.getConstant(0x7c00, DL, MVT::i32), V, ISD::SETGT); 2753 V = DAG.getSelectCC(DL, E, DAG.getConstant(1039, DL, MVT::i32), 2754 I, V, ISD::SETEQ); 2755 2756 // Extract the sign bit. 2757 SDValue Sign = DAG.getNode(ISD::SRL, DL, MVT::i32, UH, 2758 DAG.getConstant(16, DL, MVT::i32)); 2759 Sign = DAG.getNode(ISD::AND, DL, MVT::i32, Sign, 2760 DAG.getConstant(0x8000, DL, MVT::i32)); 2761 2762 V = DAG.getNode(ISD::OR, DL, MVT::i32, Sign, V); 2763 return DAG.getZExtOrTrunc(V, DL, Op.getValueType()); 2764 } 2765 2766 SDValue AMDGPUTargetLowering::LowerFP_TO_INT(SDValue Op, 2767 SelectionDAG &DAG) const { 2768 SDValue Src = Op.getOperand(0); 2769 unsigned OpOpcode = Op.getOpcode(); 2770 EVT SrcVT = Src.getValueType(); 2771 EVT DestVT = Op.getValueType(); 2772 2773 // Will be selected natively 2774 if (SrcVT == MVT::f16 && DestVT == MVT::i16) 2775 return Op; 2776 2777 // Promote i16 to i32 2778 if (DestVT == MVT::i16 && (SrcVT == MVT::f32 || SrcVT == MVT::f64)) { 2779 SDLoc DL(Op); 2780 2781 SDValue FpToInt32 = DAG.getNode(OpOpcode, DL, MVT::i32, Src); 2782 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FpToInt32); 2783 } 2784 2785 if (SrcVT == MVT::f16 || 2786 (SrcVT == MVT::f32 && Src.getOpcode() == ISD::FP16_TO_FP)) { 2787 SDLoc DL(Op); 2788 2789 SDValue FpToInt32 = DAG.getNode(OpOpcode, DL, MVT::i32, Src); 2790 unsigned Ext = 2791 OpOpcode == ISD::FP_TO_SINT ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 2792 return DAG.getNode(Ext, DL, MVT::i64, FpToInt32); 2793 } 2794 2795 if (DestVT == MVT::i64 && (SrcVT == MVT::f32 || SrcVT == MVT::f64)) 2796 return LowerFP_TO_INT64(Op, DAG, OpOpcode == ISD::FP_TO_SINT); 2797 2798 return SDValue(); 2799 } 2800 2801 SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, 2802 SelectionDAG &DAG) const { 2803 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 2804 MVT VT = Op.getSimpleValueType(); 2805 MVT ScalarVT = VT.getScalarType(); 2806 2807 assert(VT.isVector()); 2808 2809 SDValue Src = Op.getOperand(0); 2810 SDLoc DL(Op); 2811 2812 // TODO: Don't scalarize on Evergreen? 2813 unsigned NElts = VT.getVectorNumElements(); 2814 SmallVector<SDValue, 8> Args; 2815 DAG.ExtractVectorElements(Src, Args, 0, NElts); 2816 2817 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType()); 2818 for (unsigned I = 0; I < NElts; ++I) 2819 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp); 2820 2821 return DAG.getBuildVector(VT, DL, Args); 2822 } 2823 2824 //===----------------------------------------------------------------------===// 2825 // Custom DAG optimizations 2826 //===----------------------------------------------------------------------===// 2827 2828 static bool isU24(SDValue Op, SelectionDAG &DAG) { 2829 return AMDGPUTargetLowering::numBitsUnsigned(Op, DAG) <= 24; 2830 } 2831 2832 static bool isI24(SDValue Op, SelectionDAG &DAG) { 2833 EVT VT = Op.getValueType(); 2834 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated 2835 // as unsigned 24-bit values. 2836 AMDGPUTargetLowering::numBitsSigned(Op, DAG) < 24; 2837 } 2838 2839 static SDValue simplifyMul24(SDNode *Node24, 2840 TargetLowering::DAGCombinerInfo &DCI) { 2841 SelectionDAG &DAG = DCI.DAG; 2842 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 2843 bool IsIntrin = Node24->getOpcode() == ISD::INTRINSIC_WO_CHAIN; 2844 2845 SDValue LHS = IsIntrin ? Node24->getOperand(1) : Node24->getOperand(0); 2846 SDValue RHS = IsIntrin ? Node24->getOperand(2) : Node24->getOperand(1); 2847 unsigned NewOpcode = Node24->getOpcode(); 2848 if (IsIntrin) { 2849 unsigned IID = cast<ConstantSDNode>(Node24->getOperand(0))->getZExtValue(); 2850 NewOpcode = IID == Intrinsic::amdgcn_mul_i24 ? 2851 AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24; 2852 } 2853 2854 APInt Demanded = APInt::getLowBitsSet(LHS.getValueSizeInBits(), 24); 2855 2856 // First try to simplify using SimplifyMultipleUseDemandedBits which allows 2857 // the operands to have other uses, but will only perform simplifications that 2858 // involve bypassing some nodes for this user. 2859 SDValue DemandedLHS = TLI.SimplifyMultipleUseDemandedBits(LHS, Demanded, DAG); 2860 SDValue DemandedRHS = TLI.SimplifyMultipleUseDemandedBits(RHS, Demanded, DAG); 2861 if (DemandedLHS || DemandedRHS) 2862 return DAG.getNode(NewOpcode, SDLoc(Node24), Node24->getVTList(), 2863 DemandedLHS ? DemandedLHS : LHS, 2864 DemandedRHS ? DemandedRHS : RHS); 2865 2866 // Now try SimplifyDemandedBits which can simplify the nodes used by our 2867 // operands if this node is the only user. 2868 if (TLI.SimplifyDemandedBits(LHS, Demanded, DCI)) 2869 return SDValue(Node24, 0); 2870 if (TLI.SimplifyDemandedBits(RHS, Demanded, DCI)) 2871 return SDValue(Node24, 0); 2872 2873 return SDValue(); 2874 } 2875 2876 template <typename IntTy> 2877 static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0, uint32_t Offset, 2878 uint32_t Width, const SDLoc &DL) { 2879 if (Width + Offset < 32) { 2880 uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width); 2881 IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width); 2882 return DAG.getConstant(Result, DL, MVT::i32); 2883 } 2884 2885 return DAG.getConstant(Src0 >> Offset, DL, MVT::i32); 2886 } 2887 2888 static bool hasVolatileUser(SDNode *Val) { 2889 for (SDNode *U : Val->uses()) { 2890 if (MemSDNode *M = dyn_cast<MemSDNode>(U)) { 2891 if (M->isVolatile()) 2892 return true; 2893 } 2894 } 2895 2896 return false; 2897 } 2898 2899 bool AMDGPUTargetLowering::shouldCombineMemoryType(EVT VT) const { 2900 // i32 vectors are the canonical memory type. 2901 if (VT.getScalarType() == MVT::i32 || isTypeLegal(VT)) 2902 return false; 2903 2904 if (!VT.isByteSized()) 2905 return false; 2906 2907 unsigned Size = VT.getStoreSize(); 2908 2909 if ((Size == 1 || Size == 2 || Size == 4) && !VT.isVector()) 2910 return false; 2911 2912 if (Size == 3 || (Size > 4 && (Size % 4 != 0))) 2913 return false; 2914 2915 return true; 2916 } 2917 2918 // Replace load of an illegal type with a store of a bitcast to a friendlier 2919 // type. 2920 SDValue AMDGPUTargetLowering::performLoadCombine(SDNode *N, 2921 DAGCombinerInfo &DCI) const { 2922 if (!DCI.isBeforeLegalize()) 2923 return SDValue(); 2924 2925 LoadSDNode *LN = cast<LoadSDNode>(N); 2926 if (!LN->isSimple() || !ISD::isNormalLoad(LN) || hasVolatileUser(LN)) 2927 return SDValue(); 2928 2929 SDLoc SL(N); 2930 SelectionDAG &DAG = DCI.DAG; 2931 EVT VT = LN->getMemoryVT(); 2932 2933 unsigned Size = VT.getStoreSize(); 2934 Align Alignment = LN->getAlign(); 2935 if (Alignment < Size && isTypeLegal(VT)) { 2936 bool IsFast; 2937 unsigned AS = LN->getAddressSpace(); 2938 2939 // Expand unaligned loads earlier than legalization. Due to visitation order 2940 // problems during legalization, the emitted instructions to pack and unpack 2941 // the bytes again are not eliminated in the case of an unaligned copy. 2942 if (!allowsMisalignedMemoryAccesses( 2943 VT, AS, Alignment, LN->getMemOperand()->getFlags(), &IsFast)) { 2944 SDValue Ops[2]; 2945 2946 if (VT.isVector()) 2947 std::tie(Ops[0], Ops[1]) = scalarizeVectorLoad(LN, DAG); 2948 else 2949 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(LN, DAG); 2950 2951 return DAG.getMergeValues(Ops, SDLoc(N)); 2952 } 2953 2954 if (!IsFast) 2955 return SDValue(); 2956 } 2957 2958 if (!shouldCombineMemoryType(VT)) 2959 return SDValue(); 2960 2961 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT); 2962 2963 SDValue NewLoad 2964 = DAG.getLoad(NewVT, SL, LN->getChain(), 2965 LN->getBasePtr(), LN->getMemOperand()); 2966 2967 SDValue BC = DAG.getNode(ISD::BITCAST, SL, VT, NewLoad); 2968 DCI.CombineTo(N, BC, NewLoad.getValue(1)); 2969 return SDValue(N, 0); 2970 } 2971 2972 // Replace store of an illegal type with a store of a bitcast to a friendlier 2973 // type. 2974 SDValue AMDGPUTargetLowering::performStoreCombine(SDNode *N, 2975 DAGCombinerInfo &DCI) const { 2976 if (!DCI.isBeforeLegalize()) 2977 return SDValue(); 2978 2979 StoreSDNode *SN = cast<StoreSDNode>(N); 2980 if (!SN->isSimple() || !ISD::isNormalStore(SN)) 2981 return SDValue(); 2982 2983 EVT VT = SN->getMemoryVT(); 2984 unsigned Size = VT.getStoreSize(); 2985 2986 SDLoc SL(N); 2987 SelectionDAG &DAG = DCI.DAG; 2988 Align Alignment = SN->getAlign(); 2989 if (Alignment < Size && isTypeLegal(VT)) { 2990 bool IsFast; 2991 unsigned AS = SN->getAddressSpace(); 2992 2993 // Expand unaligned stores earlier than legalization. Due to visitation 2994 // order problems during legalization, the emitted instructions to pack and 2995 // unpack the bytes again are not eliminated in the case of an unaligned 2996 // copy. 2997 if (!allowsMisalignedMemoryAccesses( 2998 VT, AS, Alignment, SN->getMemOperand()->getFlags(), &IsFast)) { 2999 if (VT.isVector()) 3000 return scalarizeVectorStore(SN, DAG); 3001 3002 return expandUnalignedStore(SN, DAG); 3003 } 3004 3005 if (!IsFast) 3006 return SDValue(); 3007 } 3008 3009 if (!shouldCombineMemoryType(VT)) 3010 return SDValue(); 3011 3012 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT); 3013 SDValue Val = SN->getValue(); 3014 3015 //DCI.AddToWorklist(Val.getNode()); 3016 3017 bool OtherUses = !Val.hasOneUse(); 3018 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NewVT, Val); 3019 if (OtherUses) { 3020 SDValue CastBack = DAG.getNode(ISD::BITCAST, SL, VT, CastVal); 3021 DAG.ReplaceAllUsesOfValueWith(Val, CastBack); 3022 } 3023 3024 return DAG.getStore(SN->getChain(), SL, CastVal, 3025 SN->getBasePtr(), SN->getMemOperand()); 3026 } 3027 3028 // FIXME: This should go in generic DAG combiner with an isTruncateFree check, 3029 // but isTruncateFree is inaccurate for i16 now because of SALU vs. VALU 3030 // issues. 3031 SDValue AMDGPUTargetLowering::performAssertSZExtCombine(SDNode *N, 3032 DAGCombinerInfo &DCI) const { 3033 SelectionDAG &DAG = DCI.DAG; 3034 SDValue N0 = N->getOperand(0); 3035 3036 // (vt2 (assertzext (truncate vt0:x), vt1)) -> 3037 // (vt2 (truncate (assertzext vt0:x, vt1))) 3038 if (N0.getOpcode() == ISD::TRUNCATE) { 3039 SDValue N1 = N->getOperand(1); 3040 EVT ExtVT = cast<VTSDNode>(N1)->getVT(); 3041 SDLoc SL(N); 3042 3043 SDValue Src = N0.getOperand(0); 3044 EVT SrcVT = Src.getValueType(); 3045 if (SrcVT.bitsGE(ExtVT)) { 3046 SDValue NewInReg = DAG.getNode(N->getOpcode(), SL, SrcVT, Src, N1); 3047 return DAG.getNode(ISD::TRUNCATE, SL, N->getValueType(0), NewInReg); 3048 } 3049 } 3050 3051 return SDValue(); 3052 } 3053 3054 SDValue AMDGPUTargetLowering::performIntrinsicWOChainCombine( 3055 SDNode *N, DAGCombinerInfo &DCI) const { 3056 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 3057 switch (IID) { 3058 case Intrinsic::amdgcn_mul_i24: 3059 case Intrinsic::amdgcn_mul_u24: 3060 return simplifyMul24(N, DCI); 3061 case Intrinsic::amdgcn_fract: 3062 case Intrinsic::amdgcn_rsq: 3063 case Intrinsic::amdgcn_rcp_legacy: 3064 case Intrinsic::amdgcn_rsq_legacy: 3065 case Intrinsic::amdgcn_rsq_clamp: 3066 case Intrinsic::amdgcn_ldexp: { 3067 // FIXME: This is probably wrong. If src is an sNaN, it won't be quieted 3068 SDValue Src = N->getOperand(1); 3069 return Src.isUndef() ? Src : SDValue(); 3070 } 3071 default: 3072 return SDValue(); 3073 } 3074 } 3075 3076 /// Split the 64-bit value \p LHS into two 32-bit components, and perform the 3077 /// binary operation \p Opc to it with the corresponding constant operands. 3078 SDValue AMDGPUTargetLowering::splitBinaryBitConstantOpImpl( 3079 DAGCombinerInfo &DCI, const SDLoc &SL, 3080 unsigned Opc, SDValue LHS, 3081 uint32_t ValLo, uint32_t ValHi) const { 3082 SelectionDAG &DAG = DCI.DAG; 3083 SDValue Lo, Hi; 3084 std::tie(Lo, Hi) = split64BitValue(LHS, DAG); 3085 3086 SDValue LoRHS = DAG.getConstant(ValLo, SL, MVT::i32); 3087 SDValue HiRHS = DAG.getConstant(ValHi, SL, MVT::i32); 3088 3089 SDValue LoAnd = DAG.getNode(Opc, SL, MVT::i32, Lo, LoRHS); 3090 SDValue HiAnd = DAG.getNode(Opc, SL, MVT::i32, Hi, HiRHS); 3091 3092 // Re-visit the ands. It's possible we eliminated one of them and it could 3093 // simplify the vector. 3094 DCI.AddToWorklist(Lo.getNode()); 3095 DCI.AddToWorklist(Hi.getNode()); 3096 3097 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {LoAnd, HiAnd}); 3098 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 3099 } 3100 3101 SDValue AMDGPUTargetLowering::performShlCombine(SDNode *N, 3102 DAGCombinerInfo &DCI) const { 3103 EVT VT = N->getValueType(0); 3104 3105 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 3106 if (!RHS) 3107 return SDValue(); 3108 3109 SDValue LHS = N->getOperand(0); 3110 unsigned RHSVal = RHS->getZExtValue(); 3111 if (!RHSVal) 3112 return LHS; 3113 3114 SDLoc SL(N); 3115 SelectionDAG &DAG = DCI.DAG; 3116 3117 switch (LHS->getOpcode()) { 3118 default: 3119 break; 3120 case ISD::ZERO_EXTEND: 3121 case ISD::SIGN_EXTEND: 3122 case ISD::ANY_EXTEND: { 3123 SDValue X = LHS->getOperand(0); 3124 3125 if (VT == MVT::i32 && RHSVal == 16 && X.getValueType() == MVT::i16 && 3126 isOperationLegal(ISD::BUILD_VECTOR, MVT::v2i16)) { 3127 // Prefer build_vector as the canonical form if packed types are legal. 3128 // (shl ([asz]ext i16:x), 16 -> build_vector 0, x 3129 SDValue Vec = DAG.getBuildVector(MVT::v2i16, SL, 3130 { DAG.getConstant(0, SL, MVT::i16), LHS->getOperand(0) }); 3131 return DAG.getNode(ISD::BITCAST, SL, MVT::i32, Vec); 3132 } 3133 3134 // shl (ext x) => zext (shl x), if shift does not overflow int 3135 if (VT != MVT::i64) 3136 break; 3137 KnownBits Known = DAG.computeKnownBits(X); 3138 unsigned LZ = Known.countMinLeadingZeros(); 3139 if (LZ < RHSVal) 3140 break; 3141 EVT XVT = X.getValueType(); 3142 SDValue Shl = DAG.getNode(ISD::SHL, SL, XVT, X, SDValue(RHS, 0)); 3143 return DAG.getZExtOrTrunc(Shl, SL, VT); 3144 } 3145 } 3146 3147 if (VT != MVT::i64) 3148 return SDValue(); 3149 3150 // i64 (shl x, C) -> (build_pair 0, (shl x, C -32)) 3151 3152 // On some subtargets, 64-bit shift is a quarter rate instruction. In the 3153 // common case, splitting this into a move and a 32-bit shift is faster and 3154 // the same code size. 3155 if (RHSVal < 32) 3156 return SDValue(); 3157 3158 SDValue ShiftAmt = DAG.getConstant(RHSVal - 32, SL, MVT::i32); 3159 3160 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS); 3161 SDValue NewShift = DAG.getNode(ISD::SHL, SL, MVT::i32, Lo, ShiftAmt); 3162 3163 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 3164 3165 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {Zero, NewShift}); 3166 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 3167 } 3168 3169 SDValue AMDGPUTargetLowering::performSraCombine(SDNode *N, 3170 DAGCombinerInfo &DCI) const { 3171 if (N->getValueType(0) != MVT::i64) 3172 return SDValue(); 3173 3174 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 3175 if (!RHS) 3176 return SDValue(); 3177 3178 SelectionDAG &DAG = DCI.DAG; 3179 SDLoc SL(N); 3180 unsigned RHSVal = RHS->getZExtValue(); 3181 3182 // (sra i64:x, 32) -> build_pair x, (sra hi_32(x), 31) 3183 if (RHSVal == 32) { 3184 SDValue Hi = getHiHalf64(N->getOperand(0), DAG); 3185 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi, 3186 DAG.getConstant(31, SL, MVT::i32)); 3187 3188 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {Hi, NewShift}); 3189 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec); 3190 } 3191 3192 // (sra i64:x, 63) -> build_pair (sra hi_32(x), 31), (sra hi_32(x), 31) 3193 if (RHSVal == 63) { 3194 SDValue Hi = getHiHalf64(N->getOperand(0), DAG); 3195 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi, 3196 DAG.getConstant(31, SL, MVT::i32)); 3197 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, NewShift}); 3198 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec); 3199 } 3200 3201 return SDValue(); 3202 } 3203 3204 SDValue AMDGPUTargetLowering::performSrlCombine(SDNode *N, 3205 DAGCombinerInfo &DCI) const { 3206 auto *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 3207 if (!RHS) 3208 return SDValue(); 3209 3210 EVT VT = N->getValueType(0); 3211 SDValue LHS = N->getOperand(0); 3212 unsigned ShiftAmt = RHS->getZExtValue(); 3213 SelectionDAG &DAG = DCI.DAG; 3214 SDLoc SL(N); 3215 3216 // fold (srl (and x, c1 << c2), c2) -> (and (srl(x, c2), c1) 3217 // this improves the ability to match BFE patterns in isel. 3218 if (LHS.getOpcode() == ISD::AND) { 3219 if (auto *Mask = dyn_cast<ConstantSDNode>(LHS.getOperand(1))) { 3220 if (Mask->getAPIntValue().isShiftedMask() && 3221 Mask->getAPIntValue().countTrailingZeros() == ShiftAmt) { 3222 return DAG.getNode( 3223 ISD::AND, SL, VT, 3224 DAG.getNode(ISD::SRL, SL, VT, LHS.getOperand(0), N->getOperand(1)), 3225 DAG.getNode(ISD::SRL, SL, VT, LHS.getOperand(1), N->getOperand(1))); 3226 } 3227 } 3228 } 3229 3230 if (VT != MVT::i64) 3231 return SDValue(); 3232 3233 if (ShiftAmt < 32) 3234 return SDValue(); 3235 3236 // srl i64:x, C for C >= 32 3237 // => 3238 // build_pair (srl hi_32(x), C - 32), 0 3239 SDValue One = DAG.getConstant(1, SL, MVT::i32); 3240 SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 3241 3242 SDValue VecOp = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, LHS); 3243 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecOp, One); 3244 3245 SDValue NewConst = DAG.getConstant(ShiftAmt - 32, SL, MVT::i32); 3246 SDValue NewShift = DAG.getNode(ISD::SRL, SL, MVT::i32, Hi, NewConst); 3247 3248 SDValue BuildPair = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, Zero}); 3249 3250 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildPair); 3251 } 3252 3253 SDValue AMDGPUTargetLowering::performTruncateCombine( 3254 SDNode *N, DAGCombinerInfo &DCI) const { 3255 SDLoc SL(N); 3256 SelectionDAG &DAG = DCI.DAG; 3257 EVT VT = N->getValueType(0); 3258 SDValue Src = N->getOperand(0); 3259 3260 // vt1 (truncate (bitcast (build_vector vt0:x, ...))) -> vt1 (bitcast vt0:x) 3261 if (Src.getOpcode() == ISD::BITCAST && !VT.isVector()) { 3262 SDValue Vec = Src.getOperand(0); 3263 if (Vec.getOpcode() == ISD::BUILD_VECTOR) { 3264 SDValue Elt0 = Vec.getOperand(0); 3265 EVT EltVT = Elt0.getValueType(); 3266 if (VT.getFixedSizeInBits() <= EltVT.getFixedSizeInBits()) { 3267 if (EltVT.isFloatingPoint()) { 3268 Elt0 = DAG.getNode(ISD::BITCAST, SL, 3269 EltVT.changeTypeToInteger(), Elt0); 3270 } 3271 3272 return DAG.getNode(ISD::TRUNCATE, SL, VT, Elt0); 3273 } 3274 } 3275 } 3276 3277 // Equivalent of above for accessing the high element of a vector as an 3278 // integer operation. 3279 // trunc (srl (bitcast (build_vector x, y))), 16 -> trunc (bitcast y) 3280 if (Src.getOpcode() == ISD::SRL && !VT.isVector()) { 3281 if (auto K = isConstOrConstSplat(Src.getOperand(1))) { 3282 if (2 * K->getZExtValue() == Src.getValueType().getScalarSizeInBits()) { 3283 SDValue BV = stripBitcast(Src.getOperand(0)); 3284 if (BV.getOpcode() == ISD::BUILD_VECTOR && 3285 BV.getValueType().getVectorNumElements() == 2) { 3286 SDValue SrcElt = BV.getOperand(1); 3287 EVT SrcEltVT = SrcElt.getValueType(); 3288 if (SrcEltVT.isFloatingPoint()) { 3289 SrcElt = DAG.getNode(ISD::BITCAST, SL, 3290 SrcEltVT.changeTypeToInteger(), SrcElt); 3291 } 3292 3293 return DAG.getNode(ISD::TRUNCATE, SL, VT, SrcElt); 3294 } 3295 } 3296 } 3297 } 3298 3299 // Partially shrink 64-bit shifts to 32-bit if reduced to 16-bit. 3300 // 3301 // i16 (trunc (srl i64:x, K)), K <= 16 -> 3302 // i16 (trunc (srl (i32 (trunc x), K))) 3303 if (VT.getScalarSizeInBits() < 32) { 3304 EVT SrcVT = Src.getValueType(); 3305 if (SrcVT.getScalarSizeInBits() > 32 && 3306 (Src.getOpcode() == ISD::SRL || 3307 Src.getOpcode() == ISD::SRA || 3308 Src.getOpcode() == ISD::SHL)) { 3309 SDValue Amt = Src.getOperand(1); 3310 KnownBits Known = DAG.computeKnownBits(Amt); 3311 unsigned Size = VT.getScalarSizeInBits(); 3312 if ((Known.isConstant() && Known.getConstant().ule(Size)) || 3313 (Known.getBitWidth() - Known.countMinLeadingZeros() <= Log2_32(Size))) { 3314 EVT MidVT = VT.isVector() ? 3315 EVT::getVectorVT(*DAG.getContext(), MVT::i32, 3316 VT.getVectorNumElements()) : MVT::i32; 3317 3318 EVT NewShiftVT = getShiftAmountTy(MidVT, DAG.getDataLayout()); 3319 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SL, MidVT, 3320 Src.getOperand(0)); 3321 DCI.AddToWorklist(Trunc.getNode()); 3322 3323 if (Amt.getValueType() != NewShiftVT) { 3324 Amt = DAG.getZExtOrTrunc(Amt, SL, NewShiftVT); 3325 DCI.AddToWorklist(Amt.getNode()); 3326 } 3327 3328 SDValue ShrunkShift = DAG.getNode(Src.getOpcode(), SL, MidVT, 3329 Trunc, Amt); 3330 return DAG.getNode(ISD::TRUNCATE, SL, VT, ShrunkShift); 3331 } 3332 } 3333 } 3334 3335 return SDValue(); 3336 } 3337 3338 // We need to specifically handle i64 mul here to avoid unnecessary conversion 3339 // instructions. If we only match on the legalized i64 mul expansion, 3340 // SimplifyDemandedBits will be unable to remove them because there will be 3341 // multiple uses due to the separate mul + mulh[su]. 3342 static SDValue getMul24(SelectionDAG &DAG, const SDLoc &SL, 3343 SDValue N0, SDValue N1, unsigned Size, bool Signed) { 3344 if (Size <= 32) { 3345 unsigned MulOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24; 3346 return DAG.getNode(MulOpc, SL, MVT::i32, N0, N1); 3347 } 3348 3349 unsigned MulLoOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24; 3350 unsigned MulHiOpc = Signed ? AMDGPUISD::MULHI_I24 : AMDGPUISD::MULHI_U24; 3351 3352 SDValue MulLo = DAG.getNode(MulLoOpc, SL, MVT::i32, N0, N1); 3353 SDValue MulHi = DAG.getNode(MulHiOpc, SL, MVT::i32, N0, N1); 3354 3355 return DAG.getNode(ISD::BUILD_PAIR, SL, MVT::i64, MulLo, MulHi); 3356 } 3357 3358 SDValue AMDGPUTargetLowering::performMulCombine(SDNode *N, 3359 DAGCombinerInfo &DCI) const { 3360 EVT VT = N->getValueType(0); 3361 3362 // Don't generate 24-bit multiplies on values that are in SGPRs, since 3363 // we only have a 32-bit scalar multiply (avoid values being moved to VGPRs 3364 // unnecessarily). isDivergent() is used as an approximation of whether the 3365 // value is in an SGPR. 3366 if (!N->isDivergent()) 3367 return SDValue(); 3368 3369 unsigned Size = VT.getSizeInBits(); 3370 if (VT.isVector() || Size > 64) 3371 return SDValue(); 3372 3373 // There are i16 integer mul/mad. 3374 if (Subtarget->has16BitInsts() && VT.getScalarType().bitsLE(MVT::i16)) 3375 return SDValue(); 3376 3377 SelectionDAG &DAG = DCI.DAG; 3378 SDLoc DL(N); 3379 3380 SDValue N0 = N->getOperand(0); 3381 SDValue N1 = N->getOperand(1); 3382 3383 // SimplifyDemandedBits has the annoying habit of turning useful zero_extends 3384 // in the source into any_extends if the result of the mul is truncated. Since 3385 // we can assume the high bits are whatever we want, use the underlying value 3386 // to avoid the unknown high bits from interfering. 3387 if (N0.getOpcode() == ISD::ANY_EXTEND) 3388 N0 = N0.getOperand(0); 3389 3390 if (N1.getOpcode() == ISD::ANY_EXTEND) 3391 N1 = N1.getOperand(0); 3392 3393 SDValue Mul; 3394 3395 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) { 3396 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32); 3397 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32); 3398 Mul = getMul24(DAG, DL, N0, N1, Size, false); 3399 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) { 3400 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32); 3401 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32); 3402 Mul = getMul24(DAG, DL, N0, N1, Size, true); 3403 } else { 3404 return SDValue(); 3405 } 3406 3407 // We need to use sext even for MUL_U24, because MUL_U24 is used 3408 // for signed multiply of 8 and 16-bit types. 3409 return DAG.getSExtOrTrunc(Mul, DL, VT); 3410 } 3411 3412 SDValue AMDGPUTargetLowering::performMulhsCombine(SDNode *N, 3413 DAGCombinerInfo &DCI) const { 3414 EVT VT = N->getValueType(0); 3415 3416 if (!Subtarget->hasMulI24() || VT.isVector()) 3417 return SDValue(); 3418 3419 SelectionDAG &DAG = DCI.DAG; 3420 SDLoc DL(N); 3421 3422 SDValue N0 = N->getOperand(0); 3423 SDValue N1 = N->getOperand(1); 3424 3425 if (!isI24(N0, DAG) || !isI24(N1, DAG)) 3426 return SDValue(); 3427 3428 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32); 3429 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32); 3430 3431 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_I24, DL, MVT::i32, N0, N1); 3432 DCI.AddToWorklist(Mulhi.getNode()); 3433 return DAG.getSExtOrTrunc(Mulhi, DL, VT); 3434 } 3435 3436 SDValue AMDGPUTargetLowering::performMulhuCombine(SDNode *N, 3437 DAGCombinerInfo &DCI) const { 3438 EVT VT = N->getValueType(0); 3439 3440 if (!Subtarget->hasMulU24() || VT.isVector() || VT.getSizeInBits() > 32) 3441 return SDValue(); 3442 3443 SelectionDAG &DAG = DCI.DAG; 3444 SDLoc DL(N); 3445 3446 SDValue N0 = N->getOperand(0); 3447 SDValue N1 = N->getOperand(1); 3448 3449 if (!isU24(N0, DAG) || !isU24(N1, DAG)) 3450 return SDValue(); 3451 3452 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32); 3453 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32); 3454 3455 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_U24, DL, MVT::i32, N0, N1); 3456 DCI.AddToWorklist(Mulhi.getNode()); 3457 return DAG.getZExtOrTrunc(Mulhi, DL, VT); 3458 } 3459 3460 static bool isNegativeOne(SDValue Val) { 3461 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) 3462 return C->isAllOnesValue(); 3463 return false; 3464 } 3465 3466 SDValue AMDGPUTargetLowering::getFFBX_U32(SelectionDAG &DAG, 3467 SDValue Op, 3468 const SDLoc &DL, 3469 unsigned Opc) const { 3470 EVT VT = Op.getValueType(); 3471 EVT LegalVT = getTypeToTransformTo(*DAG.getContext(), VT); 3472 if (LegalVT != MVT::i32 && (Subtarget->has16BitInsts() && 3473 LegalVT != MVT::i16)) 3474 return SDValue(); 3475 3476 if (VT != MVT::i32) 3477 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Op); 3478 3479 SDValue FFBX = DAG.getNode(Opc, DL, MVT::i32, Op); 3480 if (VT != MVT::i32) 3481 FFBX = DAG.getNode(ISD::TRUNCATE, DL, VT, FFBX); 3482 3483 return FFBX; 3484 } 3485 3486 // The native instructions return -1 on 0 input. Optimize out a select that 3487 // produces -1 on 0. 3488 // 3489 // TODO: If zero is not undef, we could also do this if the output is compared 3490 // against the bitwidth. 3491 // 3492 // TODO: Should probably combine against FFBH_U32 instead of ctlz directly. 3493 SDValue AMDGPUTargetLowering::performCtlz_CttzCombine(const SDLoc &SL, SDValue Cond, 3494 SDValue LHS, SDValue RHS, 3495 DAGCombinerInfo &DCI) const { 3496 ConstantSDNode *CmpRhs = dyn_cast<ConstantSDNode>(Cond.getOperand(1)); 3497 if (!CmpRhs || !CmpRhs->isNullValue()) 3498 return SDValue(); 3499 3500 SelectionDAG &DAG = DCI.DAG; 3501 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); 3502 SDValue CmpLHS = Cond.getOperand(0); 3503 3504 // select (setcc x, 0, eq), -1, (ctlz_zero_undef x) -> ffbh_u32 x 3505 // select (setcc x, 0, eq), -1, (cttz_zero_undef x) -> ffbl_u32 x 3506 if (CCOpcode == ISD::SETEQ && 3507 (isCtlzOpc(RHS.getOpcode()) || isCttzOpc(RHS.getOpcode())) && 3508 RHS.getOperand(0) == CmpLHS && isNegativeOne(LHS)) { 3509 unsigned Opc = 3510 isCttzOpc(RHS.getOpcode()) ? AMDGPUISD::FFBL_B32 : AMDGPUISD::FFBH_U32; 3511 return getFFBX_U32(DAG, CmpLHS, SL, Opc); 3512 } 3513 3514 // select (setcc x, 0, ne), (ctlz_zero_undef x), -1 -> ffbh_u32 x 3515 // select (setcc x, 0, ne), (cttz_zero_undef x), -1 -> ffbl_u32 x 3516 if (CCOpcode == ISD::SETNE && 3517 (isCtlzOpc(LHS.getOpcode()) || isCttzOpc(LHS.getOpcode())) && 3518 LHS.getOperand(0) == CmpLHS && isNegativeOne(RHS)) { 3519 unsigned Opc = 3520 isCttzOpc(LHS.getOpcode()) ? AMDGPUISD::FFBL_B32 : AMDGPUISD::FFBH_U32; 3521 3522 return getFFBX_U32(DAG, CmpLHS, SL, Opc); 3523 } 3524 3525 return SDValue(); 3526 } 3527 3528 static SDValue distributeOpThroughSelect(TargetLowering::DAGCombinerInfo &DCI, 3529 unsigned Op, 3530 const SDLoc &SL, 3531 SDValue Cond, 3532 SDValue N1, 3533 SDValue N2) { 3534 SelectionDAG &DAG = DCI.DAG; 3535 EVT VT = N1.getValueType(); 3536 3537 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT, Cond, 3538 N1.getOperand(0), N2.getOperand(0)); 3539 DCI.AddToWorklist(NewSelect.getNode()); 3540 return DAG.getNode(Op, SL, VT, NewSelect); 3541 } 3542 3543 // Pull a free FP operation out of a select so it may fold into uses. 3544 // 3545 // select c, (fneg x), (fneg y) -> fneg (select c, x, y) 3546 // select c, (fneg x), k -> fneg (select c, x, (fneg k)) 3547 // 3548 // select c, (fabs x), (fabs y) -> fabs (select c, x, y) 3549 // select c, (fabs x), +k -> fabs (select c, x, k) 3550 static SDValue foldFreeOpFromSelect(TargetLowering::DAGCombinerInfo &DCI, 3551 SDValue N) { 3552 SelectionDAG &DAG = DCI.DAG; 3553 SDValue Cond = N.getOperand(0); 3554 SDValue LHS = N.getOperand(1); 3555 SDValue RHS = N.getOperand(2); 3556 3557 EVT VT = N.getValueType(); 3558 if ((LHS.getOpcode() == ISD::FABS && RHS.getOpcode() == ISD::FABS) || 3559 (LHS.getOpcode() == ISD::FNEG && RHS.getOpcode() == ISD::FNEG)) { 3560 return distributeOpThroughSelect(DCI, LHS.getOpcode(), 3561 SDLoc(N), Cond, LHS, RHS); 3562 } 3563 3564 bool Inv = false; 3565 if (RHS.getOpcode() == ISD::FABS || RHS.getOpcode() == ISD::FNEG) { 3566 std::swap(LHS, RHS); 3567 Inv = true; 3568 } 3569 3570 // TODO: Support vector constants. 3571 ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS); 3572 if ((LHS.getOpcode() == ISD::FNEG || LHS.getOpcode() == ISD::FABS) && CRHS) { 3573 SDLoc SL(N); 3574 // If one side is an fneg/fabs and the other is a constant, we can push the 3575 // fneg/fabs down. If it's an fabs, the constant needs to be non-negative. 3576 SDValue NewLHS = LHS.getOperand(0); 3577 SDValue NewRHS = RHS; 3578 3579 // Careful: if the neg can be folded up, don't try to pull it back down. 3580 bool ShouldFoldNeg = true; 3581 3582 if (NewLHS.hasOneUse()) { 3583 unsigned Opc = NewLHS.getOpcode(); 3584 if (LHS.getOpcode() == ISD::FNEG && fnegFoldsIntoOp(Opc)) 3585 ShouldFoldNeg = false; 3586 if (LHS.getOpcode() == ISD::FABS && Opc == ISD::FMUL) 3587 ShouldFoldNeg = false; 3588 } 3589 3590 if (ShouldFoldNeg) { 3591 if (LHS.getOpcode() == ISD::FNEG) 3592 NewRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3593 else if (CRHS->isNegative()) 3594 return SDValue(); 3595 3596 if (Inv) 3597 std::swap(NewLHS, NewRHS); 3598 3599 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT, 3600 Cond, NewLHS, NewRHS); 3601 DCI.AddToWorklist(NewSelect.getNode()); 3602 return DAG.getNode(LHS.getOpcode(), SL, VT, NewSelect); 3603 } 3604 } 3605 3606 return SDValue(); 3607 } 3608 3609 3610 SDValue AMDGPUTargetLowering::performSelectCombine(SDNode *N, 3611 DAGCombinerInfo &DCI) const { 3612 if (SDValue Folded = foldFreeOpFromSelect(DCI, SDValue(N, 0))) 3613 return Folded; 3614 3615 SDValue Cond = N->getOperand(0); 3616 if (Cond.getOpcode() != ISD::SETCC) 3617 return SDValue(); 3618 3619 EVT VT = N->getValueType(0); 3620 SDValue LHS = Cond.getOperand(0); 3621 SDValue RHS = Cond.getOperand(1); 3622 SDValue CC = Cond.getOperand(2); 3623 3624 SDValue True = N->getOperand(1); 3625 SDValue False = N->getOperand(2); 3626 3627 if (Cond.hasOneUse()) { // TODO: Look for multiple select uses. 3628 SelectionDAG &DAG = DCI.DAG; 3629 if (DAG.isConstantValueOfAnyType(True) && 3630 !DAG.isConstantValueOfAnyType(False)) { 3631 // Swap cmp + select pair to move constant to false input. 3632 // This will allow using VOPC cndmasks more often. 3633 // select (setcc x, y), k, x -> select (setccinv x, y), x, k 3634 3635 SDLoc SL(N); 3636 ISD::CondCode NewCC = 3637 getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), LHS.getValueType()); 3638 3639 SDValue NewCond = DAG.getSetCC(SL, Cond.getValueType(), LHS, RHS, NewCC); 3640 return DAG.getNode(ISD::SELECT, SL, VT, NewCond, False, True); 3641 } 3642 3643 if (VT == MVT::f32 && Subtarget->hasFminFmaxLegacy()) { 3644 SDValue MinMax 3645 = combineFMinMaxLegacy(SDLoc(N), VT, LHS, RHS, True, False, CC, DCI); 3646 // Revisit this node so we can catch min3/max3/med3 patterns. 3647 //DCI.AddToWorklist(MinMax.getNode()); 3648 return MinMax; 3649 } 3650 } 3651 3652 // There's no reason to not do this if the condition has other uses. 3653 return performCtlz_CttzCombine(SDLoc(N), Cond, True, False, DCI); 3654 } 3655 3656 static bool isInv2Pi(const APFloat &APF) { 3657 static const APFloat KF16(APFloat::IEEEhalf(), APInt(16, 0x3118)); 3658 static const APFloat KF32(APFloat::IEEEsingle(), APInt(32, 0x3e22f983)); 3659 static const APFloat KF64(APFloat::IEEEdouble(), APInt(64, 0x3fc45f306dc9c882)); 3660 3661 return APF.bitwiseIsEqual(KF16) || 3662 APF.bitwiseIsEqual(KF32) || 3663 APF.bitwiseIsEqual(KF64); 3664 } 3665 3666 // 0 and 1.0 / (0.5 * pi) do not have inline immmediates, so there is an 3667 // additional cost to negate them. 3668 bool AMDGPUTargetLowering::isConstantCostlierToNegate(SDValue N) const { 3669 if (const ConstantFPSDNode *C = isConstOrConstSplatFP(N)) { 3670 if (C->isZero() && !C->isNegative()) 3671 return true; 3672 3673 if (Subtarget->hasInv2PiInlineImm() && isInv2Pi(C->getValueAPF())) 3674 return true; 3675 } 3676 3677 return false; 3678 } 3679 3680 static unsigned inverseMinMax(unsigned Opc) { 3681 switch (Opc) { 3682 case ISD::FMAXNUM: 3683 return ISD::FMINNUM; 3684 case ISD::FMINNUM: 3685 return ISD::FMAXNUM; 3686 case ISD::FMAXNUM_IEEE: 3687 return ISD::FMINNUM_IEEE; 3688 case ISD::FMINNUM_IEEE: 3689 return ISD::FMAXNUM_IEEE; 3690 case AMDGPUISD::FMAX_LEGACY: 3691 return AMDGPUISD::FMIN_LEGACY; 3692 case AMDGPUISD::FMIN_LEGACY: 3693 return AMDGPUISD::FMAX_LEGACY; 3694 default: 3695 llvm_unreachable("invalid min/max opcode"); 3696 } 3697 } 3698 3699 SDValue AMDGPUTargetLowering::performFNegCombine(SDNode *N, 3700 DAGCombinerInfo &DCI) const { 3701 SelectionDAG &DAG = DCI.DAG; 3702 SDValue N0 = N->getOperand(0); 3703 EVT VT = N->getValueType(0); 3704 3705 unsigned Opc = N0.getOpcode(); 3706 3707 // If the input has multiple uses and we can either fold the negate down, or 3708 // the other uses cannot, give up. This both prevents unprofitable 3709 // transformations and infinite loops: we won't repeatedly try to fold around 3710 // a negate that has no 'good' form. 3711 if (N0.hasOneUse()) { 3712 // This may be able to fold into the source, but at a code size cost. Don't 3713 // fold if the fold into the user is free. 3714 if (allUsesHaveSourceMods(N, 0)) 3715 return SDValue(); 3716 } else { 3717 if (fnegFoldsIntoOp(Opc) && 3718 (allUsesHaveSourceMods(N) || !allUsesHaveSourceMods(N0.getNode()))) 3719 return SDValue(); 3720 } 3721 3722 SDLoc SL(N); 3723 switch (Opc) { 3724 case ISD::FADD: { 3725 if (!mayIgnoreSignedZero(N0)) 3726 return SDValue(); 3727 3728 // (fneg (fadd x, y)) -> (fadd (fneg x), (fneg y)) 3729 SDValue LHS = N0.getOperand(0); 3730 SDValue RHS = N0.getOperand(1); 3731 3732 if (LHS.getOpcode() != ISD::FNEG) 3733 LHS = DAG.getNode(ISD::FNEG, SL, VT, LHS); 3734 else 3735 LHS = LHS.getOperand(0); 3736 3737 if (RHS.getOpcode() != ISD::FNEG) 3738 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3739 else 3740 RHS = RHS.getOperand(0); 3741 3742 SDValue Res = DAG.getNode(ISD::FADD, SL, VT, LHS, RHS, N0->getFlags()); 3743 if (Res.getOpcode() != ISD::FADD) 3744 return SDValue(); // Op got folded away. 3745 if (!N0.hasOneUse()) 3746 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 3747 return Res; 3748 } 3749 case ISD::FMUL: 3750 case AMDGPUISD::FMUL_LEGACY: { 3751 // (fneg (fmul x, y)) -> (fmul x, (fneg y)) 3752 // (fneg (fmul_legacy x, y)) -> (fmul_legacy x, (fneg y)) 3753 SDValue LHS = N0.getOperand(0); 3754 SDValue RHS = N0.getOperand(1); 3755 3756 if (LHS.getOpcode() == ISD::FNEG) 3757 LHS = LHS.getOperand(0); 3758 else if (RHS.getOpcode() == ISD::FNEG) 3759 RHS = RHS.getOperand(0); 3760 else 3761 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3762 3763 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, RHS, N0->getFlags()); 3764 if (Res.getOpcode() != Opc) 3765 return SDValue(); // Op got folded away. 3766 if (!N0.hasOneUse()) 3767 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 3768 return Res; 3769 } 3770 case ISD::FMA: 3771 case ISD::FMAD: { 3772 // TODO: handle llvm.amdgcn.fma.legacy 3773 if (!mayIgnoreSignedZero(N0)) 3774 return SDValue(); 3775 3776 // (fneg (fma x, y, z)) -> (fma x, (fneg y), (fneg z)) 3777 SDValue LHS = N0.getOperand(0); 3778 SDValue MHS = N0.getOperand(1); 3779 SDValue RHS = N0.getOperand(2); 3780 3781 if (LHS.getOpcode() == ISD::FNEG) 3782 LHS = LHS.getOperand(0); 3783 else if (MHS.getOpcode() == ISD::FNEG) 3784 MHS = MHS.getOperand(0); 3785 else 3786 MHS = DAG.getNode(ISD::FNEG, SL, VT, MHS); 3787 3788 if (RHS.getOpcode() != ISD::FNEG) 3789 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3790 else 3791 RHS = RHS.getOperand(0); 3792 3793 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, MHS, RHS); 3794 if (Res.getOpcode() != Opc) 3795 return SDValue(); // Op got folded away. 3796 if (!N0.hasOneUse()) 3797 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 3798 return Res; 3799 } 3800 case ISD::FMAXNUM: 3801 case ISD::FMINNUM: 3802 case ISD::FMAXNUM_IEEE: 3803 case ISD::FMINNUM_IEEE: 3804 case AMDGPUISD::FMAX_LEGACY: 3805 case AMDGPUISD::FMIN_LEGACY: { 3806 // fneg (fmaxnum x, y) -> fminnum (fneg x), (fneg y) 3807 // fneg (fminnum x, y) -> fmaxnum (fneg x), (fneg y) 3808 // fneg (fmax_legacy x, y) -> fmin_legacy (fneg x), (fneg y) 3809 // fneg (fmin_legacy x, y) -> fmax_legacy (fneg x), (fneg y) 3810 3811 SDValue LHS = N0.getOperand(0); 3812 SDValue RHS = N0.getOperand(1); 3813 3814 // 0 doesn't have a negated inline immediate. 3815 // TODO: This constant check should be generalized to other operations. 3816 if (isConstantCostlierToNegate(RHS)) 3817 return SDValue(); 3818 3819 SDValue NegLHS = DAG.getNode(ISD::FNEG, SL, VT, LHS); 3820 SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3821 unsigned Opposite = inverseMinMax(Opc); 3822 3823 SDValue Res = DAG.getNode(Opposite, SL, VT, NegLHS, NegRHS, N0->getFlags()); 3824 if (Res.getOpcode() != Opposite) 3825 return SDValue(); // Op got folded away. 3826 if (!N0.hasOneUse()) 3827 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 3828 return Res; 3829 } 3830 case AMDGPUISD::FMED3: { 3831 SDValue Ops[3]; 3832 for (unsigned I = 0; I < 3; ++I) 3833 Ops[I] = DAG.getNode(ISD::FNEG, SL, VT, N0->getOperand(I), N0->getFlags()); 3834 3835 SDValue Res = DAG.getNode(AMDGPUISD::FMED3, SL, VT, Ops, N0->getFlags()); 3836 if (Res.getOpcode() != AMDGPUISD::FMED3) 3837 return SDValue(); // Op got folded away. 3838 3839 if (!N0.hasOneUse()) { 3840 SDValue Neg = DAG.getNode(ISD::FNEG, SL, VT, Res); 3841 DAG.ReplaceAllUsesWith(N0, Neg); 3842 3843 for (SDNode *U : Neg->uses()) 3844 DCI.AddToWorklist(U); 3845 } 3846 3847 return Res; 3848 } 3849 case ISD::FP_EXTEND: 3850 case ISD::FTRUNC: 3851 case ISD::FRINT: 3852 case ISD::FNEARBYINT: // XXX - Should fround be handled? 3853 case ISD::FSIN: 3854 case ISD::FCANONICALIZE: 3855 case AMDGPUISD::RCP: 3856 case AMDGPUISD::RCP_LEGACY: 3857 case AMDGPUISD::RCP_IFLAG: 3858 case AMDGPUISD::SIN_HW: { 3859 SDValue CvtSrc = N0.getOperand(0); 3860 if (CvtSrc.getOpcode() == ISD::FNEG) { 3861 // (fneg (fp_extend (fneg x))) -> (fp_extend x) 3862 // (fneg (rcp (fneg x))) -> (rcp x) 3863 return DAG.getNode(Opc, SL, VT, CvtSrc.getOperand(0)); 3864 } 3865 3866 if (!N0.hasOneUse()) 3867 return SDValue(); 3868 3869 // (fneg (fp_extend x)) -> (fp_extend (fneg x)) 3870 // (fneg (rcp x)) -> (rcp (fneg x)) 3871 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc); 3872 return DAG.getNode(Opc, SL, VT, Neg, N0->getFlags()); 3873 } 3874 case ISD::FP_ROUND: { 3875 SDValue CvtSrc = N0.getOperand(0); 3876 3877 if (CvtSrc.getOpcode() == ISD::FNEG) { 3878 // (fneg (fp_round (fneg x))) -> (fp_round x) 3879 return DAG.getNode(ISD::FP_ROUND, SL, VT, 3880 CvtSrc.getOperand(0), N0.getOperand(1)); 3881 } 3882 3883 if (!N0.hasOneUse()) 3884 return SDValue(); 3885 3886 // (fneg (fp_round x)) -> (fp_round (fneg x)) 3887 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc); 3888 return DAG.getNode(ISD::FP_ROUND, SL, VT, Neg, N0.getOperand(1)); 3889 } 3890 case ISD::FP16_TO_FP: { 3891 // v_cvt_f32_f16 supports source modifiers on pre-VI targets without legal 3892 // f16, but legalization of f16 fneg ends up pulling it out of the source. 3893 // Put the fneg back as a legal source operation that can be matched later. 3894 SDLoc SL(N); 3895 3896 SDValue Src = N0.getOperand(0); 3897 EVT SrcVT = Src.getValueType(); 3898 3899 // fneg (fp16_to_fp x) -> fp16_to_fp (xor x, 0x8000) 3900 SDValue IntFNeg = DAG.getNode(ISD::XOR, SL, SrcVT, Src, 3901 DAG.getConstant(0x8000, SL, SrcVT)); 3902 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFNeg); 3903 } 3904 default: 3905 return SDValue(); 3906 } 3907 } 3908 3909 SDValue AMDGPUTargetLowering::performFAbsCombine(SDNode *N, 3910 DAGCombinerInfo &DCI) const { 3911 SelectionDAG &DAG = DCI.DAG; 3912 SDValue N0 = N->getOperand(0); 3913 3914 if (!N0.hasOneUse()) 3915 return SDValue(); 3916 3917 switch (N0.getOpcode()) { 3918 case ISD::FP16_TO_FP: { 3919 assert(!Subtarget->has16BitInsts() && "should only see if f16 is illegal"); 3920 SDLoc SL(N); 3921 SDValue Src = N0.getOperand(0); 3922 EVT SrcVT = Src.getValueType(); 3923 3924 // fabs (fp16_to_fp x) -> fp16_to_fp (and x, 0x7fff) 3925 SDValue IntFAbs = DAG.getNode(ISD::AND, SL, SrcVT, Src, 3926 DAG.getConstant(0x7fff, SL, SrcVT)); 3927 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFAbs); 3928 } 3929 default: 3930 return SDValue(); 3931 } 3932 } 3933 3934 SDValue AMDGPUTargetLowering::performRcpCombine(SDNode *N, 3935 DAGCombinerInfo &DCI) const { 3936 const auto *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); 3937 if (!CFP) 3938 return SDValue(); 3939 3940 // XXX - Should this flush denormals? 3941 const APFloat &Val = CFP->getValueAPF(); 3942 APFloat One(Val.getSemantics(), "1.0"); 3943 return DCI.DAG.getConstantFP(One / Val, SDLoc(N), N->getValueType(0)); 3944 } 3945 3946 SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N, 3947 DAGCombinerInfo &DCI) const { 3948 SelectionDAG &DAG = DCI.DAG; 3949 SDLoc DL(N); 3950 3951 switch(N->getOpcode()) { 3952 default: 3953 break; 3954 case ISD::BITCAST: { 3955 EVT DestVT = N->getValueType(0); 3956 3957 // Push casts through vector builds. This helps avoid emitting a large 3958 // number of copies when materializing floating point vector constants. 3959 // 3960 // vNt1 bitcast (vNt0 (build_vector t0:x, t0:y)) => 3961 // vnt1 = build_vector (t1 (bitcast t0:x)), (t1 (bitcast t0:y)) 3962 if (DestVT.isVector()) { 3963 SDValue Src = N->getOperand(0); 3964 if (Src.getOpcode() == ISD::BUILD_VECTOR) { 3965 EVT SrcVT = Src.getValueType(); 3966 unsigned NElts = DestVT.getVectorNumElements(); 3967 3968 if (SrcVT.getVectorNumElements() == NElts) { 3969 EVT DestEltVT = DestVT.getVectorElementType(); 3970 3971 SmallVector<SDValue, 8> CastedElts; 3972 SDLoc SL(N); 3973 for (unsigned I = 0, E = SrcVT.getVectorNumElements(); I != E; ++I) { 3974 SDValue Elt = Src.getOperand(I); 3975 CastedElts.push_back(DAG.getNode(ISD::BITCAST, DL, DestEltVT, Elt)); 3976 } 3977 3978 return DAG.getBuildVector(DestVT, SL, CastedElts); 3979 } 3980 } 3981 } 3982 3983 if (DestVT.getSizeInBits() != 64 || !DestVT.isVector()) 3984 break; 3985 3986 // Fold bitcasts of constants. 3987 // 3988 // v2i32 (bitcast i64:k) -> build_vector lo_32(k), hi_32(k) 3989 // TODO: Generalize and move to DAGCombiner 3990 SDValue Src = N->getOperand(0); 3991 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src)) { 3992 SDLoc SL(N); 3993 uint64_t CVal = C->getZExtValue(); 3994 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, 3995 DAG.getConstant(Lo_32(CVal), SL, MVT::i32), 3996 DAG.getConstant(Hi_32(CVal), SL, MVT::i32)); 3997 return DAG.getNode(ISD::BITCAST, SL, DestVT, BV); 3998 } 3999 4000 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Src)) { 4001 const APInt &Val = C->getValueAPF().bitcastToAPInt(); 4002 SDLoc SL(N); 4003 uint64_t CVal = Val.getZExtValue(); 4004 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, 4005 DAG.getConstant(Lo_32(CVal), SL, MVT::i32), 4006 DAG.getConstant(Hi_32(CVal), SL, MVT::i32)); 4007 4008 return DAG.getNode(ISD::BITCAST, SL, DestVT, Vec); 4009 } 4010 4011 break; 4012 } 4013 case ISD::SHL: { 4014 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 4015 break; 4016 4017 return performShlCombine(N, DCI); 4018 } 4019 case ISD::SRL: { 4020 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 4021 break; 4022 4023 return performSrlCombine(N, DCI); 4024 } 4025 case ISD::SRA: { 4026 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 4027 break; 4028 4029 return performSraCombine(N, DCI); 4030 } 4031 case ISD::TRUNCATE: 4032 return performTruncateCombine(N, DCI); 4033 case ISD::MUL: 4034 return performMulCombine(N, DCI); 4035 case ISD::MULHS: 4036 return performMulhsCombine(N, DCI); 4037 case ISD::MULHU: 4038 return performMulhuCombine(N, DCI); 4039 case AMDGPUISD::MUL_I24: 4040 case AMDGPUISD::MUL_U24: 4041 case AMDGPUISD::MULHI_I24: 4042 case AMDGPUISD::MULHI_U24: 4043 return simplifyMul24(N, DCI); 4044 case ISD::SELECT: 4045 return performSelectCombine(N, DCI); 4046 case ISD::FNEG: 4047 return performFNegCombine(N, DCI); 4048 case ISD::FABS: 4049 return performFAbsCombine(N, DCI); 4050 case AMDGPUISD::BFE_I32: 4051 case AMDGPUISD::BFE_U32: { 4052 assert(!N->getValueType(0).isVector() && 4053 "Vector handling of BFE not implemented"); 4054 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2)); 4055 if (!Width) 4056 break; 4057 4058 uint32_t WidthVal = Width->getZExtValue() & 0x1f; 4059 if (WidthVal == 0) 4060 return DAG.getConstant(0, DL, MVT::i32); 4061 4062 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); 4063 if (!Offset) 4064 break; 4065 4066 SDValue BitsFrom = N->getOperand(0); 4067 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f; 4068 4069 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32; 4070 4071 if (OffsetVal == 0) { 4072 // This is already sign / zero extended, so try to fold away extra BFEs. 4073 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal); 4074 4075 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom); 4076 if (OpSignBits >= SignBits) 4077 return BitsFrom; 4078 4079 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal); 4080 if (Signed) { 4081 // This is a sign_extend_inreg. Replace it to take advantage of existing 4082 // DAG Combines. If not eliminated, we will match back to BFE during 4083 // selection. 4084 4085 // TODO: The sext_inreg of extended types ends, although we can could 4086 // handle them in a single BFE. 4087 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom, 4088 DAG.getValueType(SmallVT)); 4089 } 4090 4091 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT); 4092 } 4093 4094 if (ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(BitsFrom)) { 4095 if (Signed) { 4096 return constantFoldBFE<int32_t>(DAG, 4097 CVal->getSExtValue(), 4098 OffsetVal, 4099 WidthVal, 4100 DL); 4101 } 4102 4103 return constantFoldBFE<uint32_t>(DAG, 4104 CVal->getZExtValue(), 4105 OffsetVal, 4106 WidthVal, 4107 DL); 4108 } 4109 4110 if ((OffsetVal + WidthVal) >= 32 && 4111 !(Subtarget->hasSDWA() && OffsetVal == 16 && WidthVal == 16)) { 4112 SDValue ShiftVal = DAG.getConstant(OffsetVal, DL, MVT::i32); 4113 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32, 4114 BitsFrom, ShiftVal); 4115 } 4116 4117 if (BitsFrom.hasOneUse()) { 4118 APInt Demanded = APInt::getBitsSet(32, 4119 OffsetVal, 4120 OffsetVal + WidthVal); 4121 4122 KnownBits Known; 4123 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), 4124 !DCI.isBeforeLegalizeOps()); 4125 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4126 if (TLI.ShrinkDemandedConstant(BitsFrom, Demanded, TLO) || 4127 TLI.SimplifyDemandedBits(BitsFrom, Demanded, Known, TLO)) { 4128 DCI.CommitTargetLoweringOpt(TLO); 4129 } 4130 } 4131 4132 break; 4133 } 4134 case ISD::LOAD: 4135 return performLoadCombine(N, DCI); 4136 case ISD::STORE: 4137 return performStoreCombine(N, DCI); 4138 case AMDGPUISD::RCP: 4139 case AMDGPUISD::RCP_IFLAG: 4140 return performRcpCombine(N, DCI); 4141 case ISD::AssertZext: 4142 case ISD::AssertSext: 4143 return performAssertSZExtCombine(N, DCI); 4144 case ISD::INTRINSIC_WO_CHAIN: 4145 return performIntrinsicWOChainCombine(N, DCI); 4146 } 4147 return SDValue(); 4148 } 4149 4150 //===----------------------------------------------------------------------===// 4151 // Helper functions 4152 //===----------------------------------------------------------------------===// 4153 4154 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG, 4155 const TargetRegisterClass *RC, 4156 Register Reg, EVT VT, 4157 const SDLoc &SL, 4158 bool RawReg) const { 4159 MachineFunction &MF = DAG.getMachineFunction(); 4160 MachineRegisterInfo &MRI = MF.getRegInfo(); 4161 Register VReg; 4162 4163 if (!MRI.isLiveIn(Reg)) { 4164 VReg = MRI.createVirtualRegister(RC); 4165 MRI.addLiveIn(Reg, VReg); 4166 } else { 4167 VReg = MRI.getLiveInVirtReg(Reg); 4168 } 4169 4170 if (RawReg) 4171 return DAG.getRegister(VReg, VT); 4172 4173 return DAG.getCopyFromReg(DAG.getEntryNode(), SL, VReg, VT); 4174 } 4175 4176 // This may be called multiple times, and nothing prevents creating multiple 4177 // objects at the same offset. See if we already defined this object. 4178 static int getOrCreateFixedStackObject(MachineFrameInfo &MFI, unsigned Size, 4179 int64_t Offset) { 4180 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) { 4181 if (MFI.getObjectOffset(I) == Offset) { 4182 assert(MFI.getObjectSize(I) == Size); 4183 return I; 4184 } 4185 } 4186 4187 return MFI.CreateFixedObject(Size, Offset, true); 4188 } 4189 4190 SDValue AMDGPUTargetLowering::loadStackInputValue(SelectionDAG &DAG, 4191 EVT VT, 4192 const SDLoc &SL, 4193 int64_t Offset) const { 4194 MachineFunction &MF = DAG.getMachineFunction(); 4195 MachineFrameInfo &MFI = MF.getFrameInfo(); 4196 int FI = getOrCreateFixedStackObject(MFI, VT.getStoreSize(), Offset); 4197 4198 auto SrcPtrInfo = MachinePointerInfo::getStack(MF, Offset); 4199 SDValue Ptr = DAG.getFrameIndex(FI, MVT::i32); 4200 4201 return DAG.getLoad(VT, SL, DAG.getEntryNode(), Ptr, SrcPtrInfo, Align(4), 4202 MachineMemOperand::MODereferenceable | 4203 MachineMemOperand::MOInvariant); 4204 } 4205 4206 SDValue AMDGPUTargetLowering::storeStackInputValue(SelectionDAG &DAG, 4207 const SDLoc &SL, 4208 SDValue Chain, 4209 SDValue ArgVal, 4210 int64_t Offset) const { 4211 MachineFunction &MF = DAG.getMachineFunction(); 4212 MachinePointerInfo DstInfo = MachinePointerInfo::getStack(MF, Offset); 4213 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 4214 4215 SDValue Ptr = DAG.getConstant(Offset, SL, MVT::i32); 4216 // Stores to the argument stack area are relative to the stack pointer. 4217 SDValue SP = 4218 DAG.getCopyFromReg(Chain, SL, Info->getStackPtrOffsetReg(), MVT::i32); 4219 Ptr = DAG.getNode(ISD::ADD, SL, MVT::i32, SP, Ptr); 4220 SDValue Store = DAG.getStore(Chain, SL, ArgVal, Ptr, DstInfo, Align(4), 4221 MachineMemOperand::MODereferenceable); 4222 return Store; 4223 } 4224 4225 SDValue AMDGPUTargetLowering::loadInputValue(SelectionDAG &DAG, 4226 const TargetRegisterClass *RC, 4227 EVT VT, const SDLoc &SL, 4228 const ArgDescriptor &Arg) const { 4229 assert(Arg && "Attempting to load missing argument"); 4230 4231 SDValue V = Arg.isRegister() ? 4232 CreateLiveInRegister(DAG, RC, Arg.getRegister(), VT, SL) : 4233 loadStackInputValue(DAG, VT, SL, Arg.getStackOffset()); 4234 4235 if (!Arg.isMasked()) 4236 return V; 4237 4238 unsigned Mask = Arg.getMask(); 4239 unsigned Shift = countTrailingZeros<unsigned>(Mask); 4240 V = DAG.getNode(ISD::SRL, SL, VT, V, 4241 DAG.getShiftAmountConstant(Shift, VT, SL)); 4242 return DAG.getNode(ISD::AND, SL, VT, V, 4243 DAG.getConstant(Mask >> Shift, SL, VT)); 4244 } 4245 4246 uint32_t AMDGPUTargetLowering::getImplicitParameterOffset( 4247 const MachineFunction &MF, const ImplicitParameter Param) const { 4248 const AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>(); 4249 const AMDGPUSubtarget &ST = 4250 AMDGPUSubtarget::get(getTargetMachine(), MF.getFunction()); 4251 unsigned ExplicitArgOffset = ST.getExplicitKernelArgOffset(MF.getFunction()); 4252 const Align Alignment = ST.getAlignmentForImplicitArgPtr(); 4253 uint64_t ArgOffset = alignTo(MFI->getExplicitKernArgSize(), Alignment) + 4254 ExplicitArgOffset; 4255 switch (Param) { 4256 case GRID_DIM: 4257 return ArgOffset; 4258 case GRID_OFFSET: 4259 return ArgOffset + 4; 4260 } 4261 llvm_unreachable("unexpected implicit parameter type"); 4262 } 4263 4264 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node; 4265 4266 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const { 4267 switch ((AMDGPUISD::NodeType)Opcode) { 4268 case AMDGPUISD::FIRST_NUMBER: break; 4269 // AMDIL DAG nodes 4270 NODE_NAME_CASE(UMUL); 4271 NODE_NAME_CASE(BRANCH_COND); 4272 4273 // AMDGPU DAG nodes 4274 NODE_NAME_CASE(IF) 4275 NODE_NAME_CASE(ELSE) 4276 NODE_NAME_CASE(LOOP) 4277 NODE_NAME_CASE(CALL) 4278 NODE_NAME_CASE(TC_RETURN) 4279 NODE_NAME_CASE(TRAP) 4280 NODE_NAME_CASE(RET_FLAG) 4281 NODE_NAME_CASE(RETURN_TO_EPILOG) 4282 NODE_NAME_CASE(ENDPGM) 4283 NODE_NAME_CASE(DWORDADDR) 4284 NODE_NAME_CASE(FRACT) 4285 NODE_NAME_CASE(SETCC) 4286 NODE_NAME_CASE(SETREG) 4287 NODE_NAME_CASE(DENORM_MODE) 4288 NODE_NAME_CASE(FMA_W_CHAIN) 4289 NODE_NAME_CASE(FMUL_W_CHAIN) 4290 NODE_NAME_CASE(CLAMP) 4291 NODE_NAME_CASE(COS_HW) 4292 NODE_NAME_CASE(SIN_HW) 4293 NODE_NAME_CASE(FMAX_LEGACY) 4294 NODE_NAME_CASE(FMIN_LEGACY) 4295 NODE_NAME_CASE(FMAX3) 4296 NODE_NAME_CASE(SMAX3) 4297 NODE_NAME_CASE(UMAX3) 4298 NODE_NAME_CASE(FMIN3) 4299 NODE_NAME_CASE(SMIN3) 4300 NODE_NAME_CASE(UMIN3) 4301 NODE_NAME_CASE(FMED3) 4302 NODE_NAME_CASE(SMED3) 4303 NODE_NAME_CASE(UMED3) 4304 NODE_NAME_CASE(FDOT2) 4305 NODE_NAME_CASE(URECIP) 4306 NODE_NAME_CASE(DIV_SCALE) 4307 NODE_NAME_CASE(DIV_FMAS) 4308 NODE_NAME_CASE(DIV_FIXUP) 4309 NODE_NAME_CASE(FMAD_FTZ) 4310 NODE_NAME_CASE(RCP) 4311 NODE_NAME_CASE(RSQ) 4312 NODE_NAME_CASE(RCP_LEGACY) 4313 NODE_NAME_CASE(RCP_IFLAG) 4314 NODE_NAME_CASE(FMUL_LEGACY) 4315 NODE_NAME_CASE(RSQ_CLAMP) 4316 NODE_NAME_CASE(LDEXP) 4317 NODE_NAME_CASE(FP_CLASS) 4318 NODE_NAME_CASE(DOT4) 4319 NODE_NAME_CASE(CARRY) 4320 NODE_NAME_CASE(BORROW) 4321 NODE_NAME_CASE(BFE_U32) 4322 NODE_NAME_CASE(BFE_I32) 4323 NODE_NAME_CASE(BFI) 4324 NODE_NAME_CASE(BFM) 4325 NODE_NAME_CASE(FFBH_U32) 4326 NODE_NAME_CASE(FFBH_I32) 4327 NODE_NAME_CASE(FFBL_B32) 4328 NODE_NAME_CASE(MUL_U24) 4329 NODE_NAME_CASE(MUL_I24) 4330 NODE_NAME_CASE(MULHI_U24) 4331 NODE_NAME_CASE(MULHI_I24) 4332 NODE_NAME_CASE(MAD_U24) 4333 NODE_NAME_CASE(MAD_I24) 4334 NODE_NAME_CASE(MAD_I64_I32) 4335 NODE_NAME_CASE(MAD_U64_U32) 4336 NODE_NAME_CASE(PERM) 4337 NODE_NAME_CASE(TEXTURE_FETCH) 4338 NODE_NAME_CASE(R600_EXPORT) 4339 NODE_NAME_CASE(CONST_ADDRESS) 4340 NODE_NAME_CASE(REGISTER_LOAD) 4341 NODE_NAME_CASE(REGISTER_STORE) 4342 NODE_NAME_CASE(SAMPLE) 4343 NODE_NAME_CASE(SAMPLEB) 4344 NODE_NAME_CASE(SAMPLED) 4345 NODE_NAME_CASE(SAMPLEL) 4346 NODE_NAME_CASE(CVT_F32_UBYTE0) 4347 NODE_NAME_CASE(CVT_F32_UBYTE1) 4348 NODE_NAME_CASE(CVT_F32_UBYTE2) 4349 NODE_NAME_CASE(CVT_F32_UBYTE3) 4350 NODE_NAME_CASE(CVT_PKRTZ_F16_F32) 4351 NODE_NAME_CASE(CVT_PKNORM_I16_F32) 4352 NODE_NAME_CASE(CVT_PKNORM_U16_F32) 4353 NODE_NAME_CASE(CVT_PK_I16_I32) 4354 NODE_NAME_CASE(CVT_PK_U16_U32) 4355 NODE_NAME_CASE(FP_TO_FP16) 4356 NODE_NAME_CASE(FP16_ZEXT) 4357 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR) 4358 NODE_NAME_CASE(CONST_DATA_PTR) 4359 NODE_NAME_CASE(PC_ADD_REL_OFFSET) 4360 NODE_NAME_CASE(LDS) 4361 NODE_NAME_CASE(DUMMY_CHAIN) 4362 case AMDGPUISD::FIRST_MEM_OPCODE_NUMBER: break; 4363 NODE_NAME_CASE(LOAD_D16_HI) 4364 NODE_NAME_CASE(LOAD_D16_LO) 4365 NODE_NAME_CASE(LOAD_D16_HI_I8) 4366 NODE_NAME_CASE(LOAD_D16_HI_U8) 4367 NODE_NAME_CASE(LOAD_D16_LO_I8) 4368 NODE_NAME_CASE(LOAD_D16_LO_U8) 4369 NODE_NAME_CASE(STORE_MSKOR) 4370 NODE_NAME_CASE(LOAD_CONSTANT) 4371 NODE_NAME_CASE(TBUFFER_STORE_FORMAT) 4372 NODE_NAME_CASE(TBUFFER_STORE_FORMAT_D16) 4373 NODE_NAME_CASE(TBUFFER_LOAD_FORMAT) 4374 NODE_NAME_CASE(TBUFFER_LOAD_FORMAT_D16) 4375 NODE_NAME_CASE(DS_ORDERED_COUNT) 4376 NODE_NAME_CASE(ATOMIC_CMP_SWAP) 4377 NODE_NAME_CASE(ATOMIC_INC) 4378 NODE_NAME_CASE(ATOMIC_DEC) 4379 NODE_NAME_CASE(ATOMIC_LOAD_FMIN) 4380 NODE_NAME_CASE(ATOMIC_LOAD_FMAX) 4381 NODE_NAME_CASE(BUFFER_LOAD) 4382 NODE_NAME_CASE(BUFFER_LOAD_UBYTE) 4383 NODE_NAME_CASE(BUFFER_LOAD_USHORT) 4384 NODE_NAME_CASE(BUFFER_LOAD_BYTE) 4385 NODE_NAME_CASE(BUFFER_LOAD_SHORT) 4386 NODE_NAME_CASE(BUFFER_LOAD_FORMAT) 4387 NODE_NAME_CASE(BUFFER_LOAD_FORMAT_D16) 4388 NODE_NAME_CASE(SBUFFER_LOAD) 4389 NODE_NAME_CASE(BUFFER_STORE) 4390 NODE_NAME_CASE(BUFFER_STORE_BYTE) 4391 NODE_NAME_CASE(BUFFER_STORE_SHORT) 4392 NODE_NAME_CASE(BUFFER_STORE_FORMAT) 4393 NODE_NAME_CASE(BUFFER_STORE_FORMAT_D16) 4394 NODE_NAME_CASE(BUFFER_ATOMIC_SWAP) 4395 NODE_NAME_CASE(BUFFER_ATOMIC_ADD) 4396 NODE_NAME_CASE(BUFFER_ATOMIC_SUB) 4397 NODE_NAME_CASE(BUFFER_ATOMIC_SMIN) 4398 NODE_NAME_CASE(BUFFER_ATOMIC_UMIN) 4399 NODE_NAME_CASE(BUFFER_ATOMIC_SMAX) 4400 NODE_NAME_CASE(BUFFER_ATOMIC_UMAX) 4401 NODE_NAME_CASE(BUFFER_ATOMIC_AND) 4402 NODE_NAME_CASE(BUFFER_ATOMIC_OR) 4403 NODE_NAME_CASE(BUFFER_ATOMIC_XOR) 4404 NODE_NAME_CASE(BUFFER_ATOMIC_INC) 4405 NODE_NAME_CASE(BUFFER_ATOMIC_DEC) 4406 NODE_NAME_CASE(BUFFER_ATOMIC_CMPSWAP) 4407 NODE_NAME_CASE(BUFFER_ATOMIC_CSUB) 4408 NODE_NAME_CASE(BUFFER_ATOMIC_FADD) 4409 NODE_NAME_CASE(BUFFER_ATOMIC_FMIN) 4410 NODE_NAME_CASE(BUFFER_ATOMIC_FMAX) 4411 4412 case AMDGPUISD::LAST_AMDGPU_ISD_NUMBER: break; 4413 } 4414 return nullptr; 4415 } 4416 4417 SDValue AMDGPUTargetLowering::getSqrtEstimate(SDValue Operand, 4418 SelectionDAG &DAG, int Enabled, 4419 int &RefinementSteps, 4420 bool &UseOneConstNR, 4421 bool Reciprocal) const { 4422 EVT VT = Operand.getValueType(); 4423 4424 if (VT == MVT::f32) { 4425 RefinementSteps = 0; 4426 return DAG.getNode(AMDGPUISD::RSQ, SDLoc(Operand), VT, Operand); 4427 } 4428 4429 // TODO: There is also f64 rsq instruction, but the documentation is less 4430 // clear on its precision. 4431 4432 return SDValue(); 4433 } 4434 4435 SDValue AMDGPUTargetLowering::getRecipEstimate(SDValue Operand, 4436 SelectionDAG &DAG, int Enabled, 4437 int &RefinementSteps) const { 4438 EVT VT = Operand.getValueType(); 4439 4440 if (VT == MVT::f32) { 4441 // Reciprocal, < 1 ulp error. 4442 // 4443 // This reciprocal approximation converges to < 0.5 ulp error with one 4444 // newton rhapson performed with two fused multiple adds (FMAs). 4445 4446 RefinementSteps = 0; 4447 return DAG.getNode(AMDGPUISD::RCP, SDLoc(Operand), VT, Operand); 4448 } 4449 4450 // TODO: There is also f64 rcp instruction, but the documentation is less 4451 // clear on its precision. 4452 4453 return SDValue(); 4454 } 4455 4456 void AMDGPUTargetLowering::computeKnownBitsForTargetNode( 4457 const SDValue Op, KnownBits &Known, 4458 const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth) const { 4459 4460 Known.resetAll(); // Don't know anything. 4461 4462 unsigned Opc = Op.getOpcode(); 4463 4464 switch (Opc) { 4465 default: 4466 break; 4467 case AMDGPUISD::CARRY: 4468 case AMDGPUISD::BORROW: { 4469 Known.Zero = APInt::getHighBitsSet(32, 31); 4470 break; 4471 } 4472 4473 case AMDGPUISD::BFE_I32: 4474 case AMDGPUISD::BFE_U32: { 4475 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 4476 if (!CWidth) 4477 return; 4478 4479 uint32_t Width = CWidth->getZExtValue() & 0x1f; 4480 4481 if (Opc == AMDGPUISD::BFE_U32) 4482 Known.Zero = APInt::getHighBitsSet(32, 32 - Width); 4483 4484 break; 4485 } 4486 case AMDGPUISD::FP_TO_FP16: 4487 case AMDGPUISD::FP16_ZEXT: { 4488 unsigned BitWidth = Known.getBitWidth(); 4489 4490 // High bits are zero. 4491 Known.Zero = APInt::getHighBitsSet(BitWidth, BitWidth - 16); 4492 break; 4493 } 4494 case AMDGPUISD::MUL_U24: 4495 case AMDGPUISD::MUL_I24: { 4496 KnownBits LHSKnown = DAG.computeKnownBits(Op.getOperand(0), Depth + 1); 4497 KnownBits RHSKnown = DAG.computeKnownBits(Op.getOperand(1), Depth + 1); 4498 unsigned TrailZ = LHSKnown.countMinTrailingZeros() + 4499 RHSKnown.countMinTrailingZeros(); 4500 Known.Zero.setLowBits(std::min(TrailZ, 32u)); 4501 // Skip extra check if all bits are known zeros. 4502 if (TrailZ >= 32) 4503 break; 4504 4505 // Truncate to 24 bits. 4506 LHSKnown = LHSKnown.trunc(24); 4507 RHSKnown = RHSKnown.trunc(24); 4508 4509 if (Opc == AMDGPUISD::MUL_I24) { 4510 unsigned LHSValBits = 24 - LHSKnown.countMinSignBits(); 4511 unsigned RHSValBits = 24 - RHSKnown.countMinSignBits(); 4512 unsigned MaxValBits = std::min(LHSValBits + RHSValBits, 32u); 4513 if (MaxValBits >= 32) 4514 break; 4515 bool LHSNegative = LHSKnown.isNegative(); 4516 bool LHSNonNegative = LHSKnown.isNonNegative(); 4517 bool LHSPositive = LHSKnown.isStrictlyPositive(); 4518 bool RHSNegative = RHSKnown.isNegative(); 4519 bool RHSNonNegative = RHSKnown.isNonNegative(); 4520 bool RHSPositive = RHSKnown.isStrictlyPositive(); 4521 4522 if ((LHSNonNegative && RHSNonNegative) || (LHSNegative && RHSNegative)) 4523 Known.Zero.setHighBits(32 - MaxValBits); 4524 else if ((LHSNegative && RHSPositive) || (LHSPositive && RHSNegative)) 4525 Known.One.setHighBits(32 - MaxValBits); 4526 } else { 4527 unsigned LHSValBits = 24 - LHSKnown.countMinLeadingZeros(); 4528 unsigned RHSValBits = 24 - RHSKnown.countMinLeadingZeros(); 4529 unsigned MaxValBits = std::min(LHSValBits + RHSValBits, 32u); 4530 if (MaxValBits >= 32) 4531 break; 4532 Known.Zero.setHighBits(32 - MaxValBits); 4533 } 4534 break; 4535 } 4536 case AMDGPUISD::PERM: { 4537 ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 4538 if (!CMask) 4539 return; 4540 4541 KnownBits LHSKnown = DAG.computeKnownBits(Op.getOperand(0), Depth + 1); 4542 KnownBits RHSKnown = DAG.computeKnownBits(Op.getOperand(1), Depth + 1); 4543 unsigned Sel = CMask->getZExtValue(); 4544 4545 for (unsigned I = 0; I < 32; I += 8) { 4546 unsigned SelBits = Sel & 0xff; 4547 if (SelBits < 4) { 4548 SelBits *= 8; 4549 Known.One |= ((RHSKnown.One.getZExtValue() >> SelBits) & 0xff) << I; 4550 Known.Zero |= ((RHSKnown.Zero.getZExtValue() >> SelBits) & 0xff) << I; 4551 } else if (SelBits < 7) { 4552 SelBits = (SelBits & 3) * 8; 4553 Known.One |= ((LHSKnown.One.getZExtValue() >> SelBits) & 0xff) << I; 4554 Known.Zero |= ((LHSKnown.Zero.getZExtValue() >> SelBits) & 0xff) << I; 4555 } else if (SelBits == 0x0c) { 4556 Known.Zero |= 0xFFull << I; 4557 } else if (SelBits > 0x0c) { 4558 Known.One |= 0xFFull << I; 4559 } 4560 Sel >>= 8; 4561 } 4562 break; 4563 } 4564 case AMDGPUISD::BUFFER_LOAD_UBYTE: { 4565 Known.Zero.setHighBits(24); 4566 break; 4567 } 4568 case AMDGPUISD::BUFFER_LOAD_USHORT: { 4569 Known.Zero.setHighBits(16); 4570 break; 4571 } 4572 case AMDGPUISD::LDS: { 4573 auto GA = cast<GlobalAddressSDNode>(Op.getOperand(0).getNode()); 4574 Align Alignment = GA->getGlobal()->getPointerAlignment(DAG.getDataLayout()); 4575 4576 Known.Zero.setHighBits(16); 4577 Known.Zero.setLowBits(Log2(Alignment)); 4578 break; 4579 } 4580 case ISD::INTRINSIC_WO_CHAIN: { 4581 unsigned IID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4582 switch (IID) { 4583 case Intrinsic::amdgcn_mbcnt_lo: 4584 case Intrinsic::amdgcn_mbcnt_hi: { 4585 const GCNSubtarget &ST = 4586 DAG.getMachineFunction().getSubtarget<GCNSubtarget>(); 4587 // These return at most the wavefront size - 1. 4588 unsigned Size = Op.getValueType().getSizeInBits(); 4589 Known.Zero.setHighBits(Size - ST.getWavefrontSizeLog2()); 4590 break; 4591 } 4592 default: 4593 break; 4594 } 4595 } 4596 } 4597 } 4598 4599 unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode( 4600 SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, 4601 unsigned Depth) const { 4602 switch (Op.getOpcode()) { 4603 case AMDGPUISD::BFE_I32: { 4604 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 4605 if (!Width) 4606 return 1; 4607 4608 unsigned SignBits = 32 - Width->getZExtValue() + 1; 4609 if (!isNullConstant(Op.getOperand(1))) 4610 return SignBits; 4611 4612 // TODO: Could probably figure something out with non-0 offsets. 4613 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1); 4614 return std::max(SignBits, Op0SignBits); 4615 } 4616 4617 case AMDGPUISD::BFE_U32: { 4618 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 4619 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1; 4620 } 4621 4622 case AMDGPUISD::CARRY: 4623 case AMDGPUISD::BORROW: 4624 return 31; 4625 case AMDGPUISD::BUFFER_LOAD_BYTE: 4626 return 25; 4627 case AMDGPUISD::BUFFER_LOAD_SHORT: 4628 return 17; 4629 case AMDGPUISD::BUFFER_LOAD_UBYTE: 4630 return 24; 4631 case AMDGPUISD::BUFFER_LOAD_USHORT: 4632 return 16; 4633 case AMDGPUISD::FP_TO_FP16: 4634 case AMDGPUISD::FP16_ZEXT: 4635 return 16; 4636 default: 4637 return 1; 4638 } 4639 } 4640 4641 unsigned AMDGPUTargetLowering::computeNumSignBitsForTargetInstr( 4642 GISelKnownBits &Analysis, Register R, 4643 const APInt &DemandedElts, const MachineRegisterInfo &MRI, 4644 unsigned Depth) const { 4645 const MachineInstr *MI = MRI.getVRegDef(R); 4646 if (!MI) 4647 return 1; 4648 4649 // TODO: Check range metadata on MMO. 4650 switch (MI->getOpcode()) { 4651 case AMDGPU::G_AMDGPU_BUFFER_LOAD_SBYTE: 4652 return 25; 4653 case AMDGPU::G_AMDGPU_BUFFER_LOAD_SSHORT: 4654 return 17; 4655 case AMDGPU::G_AMDGPU_BUFFER_LOAD_UBYTE: 4656 return 24; 4657 case AMDGPU::G_AMDGPU_BUFFER_LOAD_USHORT: 4658 return 16; 4659 default: 4660 return 1; 4661 } 4662 } 4663 4664 bool AMDGPUTargetLowering::isKnownNeverNaNForTargetNode(SDValue Op, 4665 const SelectionDAG &DAG, 4666 bool SNaN, 4667 unsigned Depth) const { 4668 unsigned Opcode = Op.getOpcode(); 4669 switch (Opcode) { 4670 case AMDGPUISD::FMIN_LEGACY: 4671 case AMDGPUISD::FMAX_LEGACY: { 4672 if (SNaN) 4673 return true; 4674 4675 // TODO: Can check no nans on one of the operands for each one, but which 4676 // one? 4677 return false; 4678 } 4679 case AMDGPUISD::FMUL_LEGACY: 4680 case AMDGPUISD::CVT_PKRTZ_F16_F32: { 4681 if (SNaN) 4682 return true; 4683 return DAG.isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) && 4684 DAG.isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1); 4685 } 4686 case AMDGPUISD::FMED3: 4687 case AMDGPUISD::FMIN3: 4688 case AMDGPUISD::FMAX3: 4689 case AMDGPUISD::FMAD_FTZ: { 4690 if (SNaN) 4691 return true; 4692 return DAG.isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) && 4693 DAG.isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) && 4694 DAG.isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1); 4695 } 4696 case AMDGPUISD::CVT_F32_UBYTE0: 4697 case AMDGPUISD::CVT_F32_UBYTE1: 4698 case AMDGPUISD::CVT_F32_UBYTE2: 4699 case AMDGPUISD::CVT_F32_UBYTE3: 4700 return true; 4701 4702 case AMDGPUISD::RCP: 4703 case AMDGPUISD::RSQ: 4704 case AMDGPUISD::RCP_LEGACY: 4705 case AMDGPUISD::RSQ_CLAMP: { 4706 if (SNaN) 4707 return true; 4708 4709 // TODO: Need is known positive check. 4710 return false; 4711 } 4712 case AMDGPUISD::LDEXP: 4713 case AMDGPUISD::FRACT: { 4714 if (SNaN) 4715 return true; 4716 return DAG.isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1); 4717 } 4718 case AMDGPUISD::DIV_SCALE: 4719 case AMDGPUISD::DIV_FMAS: 4720 case AMDGPUISD::DIV_FIXUP: 4721 // TODO: Refine on operands. 4722 return SNaN; 4723 case AMDGPUISD::SIN_HW: 4724 case AMDGPUISD::COS_HW: { 4725 // TODO: Need check for infinity 4726 return SNaN; 4727 } 4728 case ISD::INTRINSIC_WO_CHAIN: { 4729 unsigned IntrinsicID 4730 = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4731 // TODO: Handle more intrinsics 4732 switch (IntrinsicID) { 4733 case Intrinsic::amdgcn_cubeid: 4734 return true; 4735 4736 case Intrinsic::amdgcn_frexp_mant: { 4737 if (SNaN) 4738 return true; 4739 return DAG.isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1); 4740 } 4741 case Intrinsic::amdgcn_cvt_pkrtz: { 4742 if (SNaN) 4743 return true; 4744 return DAG.isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) && 4745 DAG.isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1); 4746 } 4747 case Intrinsic::amdgcn_rcp: 4748 case Intrinsic::amdgcn_rsq: 4749 case Intrinsic::amdgcn_rcp_legacy: 4750 case Intrinsic::amdgcn_rsq_legacy: 4751 case Intrinsic::amdgcn_rsq_clamp: { 4752 if (SNaN) 4753 return true; 4754 4755 // TODO: Need is known positive check. 4756 return false; 4757 } 4758 case Intrinsic::amdgcn_trig_preop: 4759 case Intrinsic::amdgcn_fdot2: 4760 // TODO: Refine on operand 4761 return SNaN; 4762 case Intrinsic::amdgcn_fma_legacy: 4763 if (SNaN) 4764 return true; 4765 return DAG.isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) && 4766 DAG.isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1) && 4767 DAG.isKnownNeverNaN(Op.getOperand(3), SNaN, Depth + 1); 4768 default: 4769 return false; 4770 } 4771 } 4772 default: 4773 return false; 4774 } 4775 } 4776 4777 TargetLowering::AtomicExpansionKind 4778 AMDGPUTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *RMW) const { 4779 switch (RMW->getOperation()) { 4780 case AtomicRMWInst::Nand: 4781 case AtomicRMWInst::FAdd: 4782 case AtomicRMWInst::FSub: 4783 return AtomicExpansionKind::CmpXChg; 4784 default: 4785 return AtomicExpansionKind::None; 4786 } 4787 } 4788