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