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