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