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