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