1 //===-- AMDGPUISelLowering.cpp - AMDGPU Common DAG lowering functions -----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 /// \file 11 /// \brief This is the parent TargetLowering class for hardware code gen 12 /// targets. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "AMDGPUISelLowering.h" 17 #include "AMDGPU.h" 18 #include "AMDGPUCallLowering.h" 19 #include "AMDGPUFrameLowering.h" 20 #include "AMDGPUIntrinsicInfo.h" 21 #include "AMDGPURegisterInfo.h" 22 #include "AMDGPUSubtarget.h" 23 #include "R600MachineFunctionInfo.h" 24 #include "SIMachineFunctionInfo.h" 25 #include "llvm/CodeGen/CallingConvLower.h" 26 #include "llvm/CodeGen/MachineFunction.h" 27 #include "llvm/CodeGen/MachineRegisterInfo.h" 28 #include "llvm/CodeGen/SelectionDAG.h" 29 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 30 #include "llvm/IR/DataLayout.h" 31 #include "llvm/IR/DiagnosticInfo.h" 32 #include "SIInstrInfo.h" 33 using namespace llvm; 34 35 static bool allocateKernArg(unsigned ValNo, MVT ValVT, MVT LocVT, 36 CCValAssign::LocInfo LocInfo, 37 ISD::ArgFlagsTy ArgFlags, CCState &State) { 38 MachineFunction &MF = State.getMachineFunction(); 39 AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>(); 40 41 uint64_t Offset = MFI->allocateKernArg(LocVT.getStoreSize(), 42 ArgFlags.getOrigAlign()); 43 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT, Offset, LocVT, LocInfo)); 44 return true; 45 } 46 47 #include "AMDGPUGenCallingConv.inc" 48 49 // Find a larger type to do a load / store of a vector with. 50 EVT AMDGPUTargetLowering::getEquivalentMemType(LLVMContext &Ctx, EVT VT) { 51 unsigned StoreSize = VT.getStoreSizeInBits(); 52 if (StoreSize <= 32) 53 return EVT::getIntegerVT(Ctx, StoreSize); 54 55 assert(StoreSize % 32 == 0 && "Store size not a multiple of 32"); 56 return EVT::getVectorVT(Ctx, MVT::i32, StoreSize / 32); 57 } 58 59 AMDGPUTargetLowering::AMDGPUTargetLowering(const TargetMachine &TM, 60 const AMDGPUSubtarget &STI) 61 : TargetLowering(TM), Subtarget(&STI) { 62 // Lower floating point store/load to integer store/load to reduce the number 63 // of patterns in tablegen. 64 setOperationAction(ISD::LOAD, MVT::f32, Promote); 65 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32); 66 67 setOperationAction(ISD::LOAD, MVT::v2f32, Promote); 68 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32); 69 70 setOperationAction(ISD::LOAD, MVT::v4f32, Promote); 71 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32); 72 73 setOperationAction(ISD::LOAD, MVT::v8f32, Promote); 74 AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32); 75 76 setOperationAction(ISD::LOAD, MVT::v16f32, Promote); 77 AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32); 78 79 setOperationAction(ISD::LOAD, MVT::i64, Promote); 80 AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32); 81 82 setOperationAction(ISD::LOAD, MVT::v2i64, Promote); 83 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32); 84 85 setOperationAction(ISD::LOAD, MVT::f64, Promote); 86 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::v2i32); 87 88 setOperationAction(ISD::LOAD, MVT::v2f64, Promote); 89 AddPromotedToType(ISD::LOAD, MVT::v2f64, MVT::v4i32); 90 91 // There are no 64-bit extloads. These should be done as a 32-bit extload and 92 // an extension to 64-bit. 93 for (MVT VT : MVT::integer_valuetypes()) { 94 setLoadExtAction(ISD::EXTLOAD, MVT::i64, VT, Expand); 95 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, VT, Expand); 96 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, VT, Expand); 97 } 98 99 for (MVT VT : MVT::integer_valuetypes()) { 100 if (VT == MVT::i64) 101 continue; 102 103 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 104 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal); 105 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal); 106 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand); 107 108 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote); 109 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal); 110 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal); 111 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand); 112 113 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote); 114 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal); 115 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal); 116 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand); 117 } 118 119 for (MVT VT : MVT::integer_vector_valuetypes()) { 120 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i8, Expand); 121 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i8, Expand); 122 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i8, Expand); 123 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i8, Expand); 124 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i8, Expand); 125 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i8, Expand); 126 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i16, Expand); 127 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i16, Expand); 128 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i16, Expand); 129 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i16, Expand); 130 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i16, Expand); 131 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i16, Expand); 132 } 133 134 setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand); 135 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, MVT::v2f16, Expand); 136 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, MVT::v4f16, Expand); 137 setLoadExtAction(ISD::EXTLOAD, MVT::v8f32, MVT::v8f16, Expand); 138 139 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand); 140 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand); 141 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Expand); 142 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f32, Expand); 143 144 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand); 145 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand); 146 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f16, Expand); 147 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f16, Expand); 148 149 setOperationAction(ISD::STORE, MVT::f32, Promote); 150 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32); 151 152 setOperationAction(ISD::STORE, MVT::v2f32, Promote); 153 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32); 154 155 setOperationAction(ISD::STORE, MVT::v4f32, Promote); 156 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32); 157 158 setOperationAction(ISD::STORE, MVT::v8f32, Promote); 159 AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32); 160 161 setOperationAction(ISD::STORE, MVT::v16f32, Promote); 162 AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32); 163 164 setOperationAction(ISD::STORE, MVT::i64, Promote); 165 AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32); 166 167 setOperationAction(ISD::STORE, MVT::v2i64, Promote); 168 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32); 169 170 setOperationAction(ISD::STORE, MVT::f64, Promote); 171 AddPromotedToType(ISD::STORE, MVT::f64, MVT::v2i32); 172 173 setOperationAction(ISD::STORE, MVT::v2f64, Promote); 174 AddPromotedToType(ISD::STORE, MVT::v2f64, MVT::v4i32); 175 176 setTruncStoreAction(MVT::i64, MVT::i1, Expand); 177 setTruncStoreAction(MVT::i64, MVT::i8, Expand); 178 setTruncStoreAction(MVT::i64, MVT::i16, Expand); 179 setTruncStoreAction(MVT::i64, MVT::i32, Expand); 180 181 setTruncStoreAction(MVT::v2i64, MVT::v2i1, Expand); 182 setTruncStoreAction(MVT::v2i64, MVT::v2i8, Expand); 183 setTruncStoreAction(MVT::v2i64, MVT::v2i16, Expand); 184 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand); 185 186 setTruncStoreAction(MVT::f32, MVT::f16, Expand); 187 setTruncStoreAction(MVT::v2f32, MVT::v2f16, Expand); 188 setTruncStoreAction(MVT::v4f32, MVT::v4f16, Expand); 189 setTruncStoreAction(MVT::v8f32, MVT::v8f16, Expand); 190 191 setTruncStoreAction(MVT::f64, MVT::f16, Expand); 192 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 193 194 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand); 195 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand); 196 197 setTruncStoreAction(MVT::v4f64, MVT::v4f32, Expand); 198 setTruncStoreAction(MVT::v4f64, MVT::v4f16, Expand); 199 200 setTruncStoreAction(MVT::v8f64, MVT::v8f32, Expand); 201 setTruncStoreAction(MVT::v8f64, MVT::v8f16, Expand); 202 203 204 setOperationAction(ISD::Constant, MVT::i32, Legal); 205 setOperationAction(ISD::Constant, MVT::i64, Legal); 206 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); 207 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); 208 209 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 210 setOperationAction(ISD::BRIND, MVT::Other, Expand); 211 212 // This is totally unsupported, just custom lower to produce an error. 213 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom); 214 215 // We need to custom lower some of the intrinsics 216 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 217 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom); 218 219 // Library functions. These default to Expand, but we have instructions 220 // for them. 221 setOperationAction(ISD::FCEIL, MVT::f32, Legal); 222 setOperationAction(ISD::FEXP2, MVT::f32, Legal); 223 setOperationAction(ISD::FPOW, MVT::f32, Legal); 224 setOperationAction(ISD::FLOG2, MVT::f32, Legal); 225 setOperationAction(ISD::FABS, MVT::f32, Legal); 226 setOperationAction(ISD::FFLOOR, MVT::f32, Legal); 227 setOperationAction(ISD::FRINT, MVT::f32, Legal); 228 setOperationAction(ISD::FTRUNC, MVT::f32, Legal); 229 setOperationAction(ISD::FMINNUM, MVT::f32, Legal); 230 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal); 231 232 setOperationAction(ISD::FROUND, MVT::f32, Custom); 233 setOperationAction(ISD::FROUND, MVT::f64, Custom); 234 235 setOperationAction(ISD::FNEARBYINT, MVT::f32, Custom); 236 setOperationAction(ISD::FNEARBYINT, MVT::f64, Custom); 237 238 setOperationAction(ISD::FREM, MVT::f32, Custom); 239 setOperationAction(ISD::FREM, MVT::f64, Custom); 240 241 // v_mad_f32 does not support denormals according to some sources. 242 if (!Subtarget->hasFP32Denormals()) 243 setOperationAction(ISD::FMAD, MVT::f32, Legal); 244 245 // Expand to fneg + fadd. 246 setOperationAction(ISD::FSUB, MVT::f64, Expand); 247 248 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom); 249 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom); 250 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom); 251 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom); 252 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom); 253 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom); 254 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4f32, Custom); 255 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom); 256 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8f32, Custom); 257 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8i32, Custom); 258 259 if (Subtarget->getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) { 260 setOperationAction(ISD::FCEIL, MVT::f64, Custom); 261 setOperationAction(ISD::FTRUNC, MVT::f64, Custom); 262 setOperationAction(ISD::FRINT, MVT::f64, Custom); 263 setOperationAction(ISD::FFLOOR, MVT::f64, Custom); 264 } 265 266 if (!Subtarget->hasBFI()) { 267 // fcopysign can be done in a single instruction with BFI. 268 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); 269 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 270 } 271 272 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand); 273 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Custom); 274 275 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 }; 276 for (MVT VT : ScalarIntVTs) { 277 // These should use [SU]DIVREM, so set them to expand 278 setOperationAction(ISD::SDIV, VT, Expand); 279 setOperationAction(ISD::UDIV, VT, Expand); 280 setOperationAction(ISD::SREM, VT, Expand); 281 setOperationAction(ISD::UREM, VT, Expand); 282 283 // GPU does not have divrem function for signed or unsigned. 284 setOperationAction(ISD::SDIVREM, VT, Custom); 285 setOperationAction(ISD::UDIVREM, VT, Custom); 286 287 // GPU does not have [S|U]MUL_LOHI functions as a single instruction. 288 setOperationAction(ISD::SMUL_LOHI, VT, Expand); 289 setOperationAction(ISD::UMUL_LOHI, VT, Expand); 290 291 setOperationAction(ISD::BSWAP, VT, Expand); 292 setOperationAction(ISD::CTTZ, VT, Expand); 293 setOperationAction(ISD::CTLZ, VT, Expand); 294 } 295 296 if (!Subtarget->hasBCNT(32)) 297 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 298 299 if (!Subtarget->hasBCNT(64)) 300 setOperationAction(ISD::CTPOP, MVT::i64, Expand); 301 302 // The hardware supports 32-bit ROTR, but not ROTL. 303 setOperationAction(ISD::ROTL, MVT::i32, Expand); 304 setOperationAction(ISD::ROTL, MVT::i64, Expand); 305 setOperationAction(ISD::ROTR, MVT::i64, Expand); 306 307 setOperationAction(ISD::MUL, MVT::i64, Expand); 308 setOperationAction(ISD::MULHU, MVT::i64, Expand); 309 setOperationAction(ISD::MULHS, MVT::i64, Expand); 310 setOperationAction(ISD::UDIV, MVT::i32, Expand); 311 setOperationAction(ISD::UREM, MVT::i32, Expand); 312 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom); 313 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); 314 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); 315 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom); 316 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand); 317 318 setOperationAction(ISD::SMIN, MVT::i32, Legal); 319 setOperationAction(ISD::UMIN, MVT::i32, Legal); 320 setOperationAction(ISD::SMAX, MVT::i32, Legal); 321 setOperationAction(ISD::UMAX, MVT::i32, Legal); 322 323 if (Subtarget->hasFFBH()) 324 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Custom); 325 326 if (Subtarget->hasFFBL()) 327 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Legal); 328 329 setOperationAction(ISD::CTLZ, MVT::i64, Custom); 330 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom); 331 332 // We only really have 32-bit BFE instructions (and 16-bit on VI). 333 // 334 // On SI+ there are 64-bit BFEs, but they are scalar only and there isn't any 335 // effort to match them now. We want this to be false for i64 cases when the 336 // extraction isn't restricted to the upper or lower half. Ideally we would 337 // have some pass reduce 64-bit extracts to 32-bit if possible. Extracts that 338 // span the midpoint are probably relatively rare, so don't worry about them 339 // for now. 340 if (Subtarget->hasBFE()) 341 setHasExtractBitsInsn(true); 342 343 static const MVT::SimpleValueType VectorIntTypes[] = { 344 MVT::v2i32, MVT::v4i32 345 }; 346 347 for (MVT VT : VectorIntTypes) { 348 // Expand the following operations for the current type by default. 349 setOperationAction(ISD::ADD, VT, Expand); 350 setOperationAction(ISD::AND, VT, Expand); 351 setOperationAction(ISD::FP_TO_SINT, VT, Expand); 352 setOperationAction(ISD::FP_TO_UINT, VT, Expand); 353 setOperationAction(ISD::MUL, VT, Expand); 354 setOperationAction(ISD::MULHU, VT, Expand); 355 setOperationAction(ISD::MULHS, VT, Expand); 356 setOperationAction(ISD::OR, VT, Expand); 357 setOperationAction(ISD::SHL, VT, Expand); 358 setOperationAction(ISD::SRA, VT, Expand); 359 setOperationAction(ISD::SRL, VT, Expand); 360 setOperationAction(ISD::ROTL, VT, Expand); 361 setOperationAction(ISD::ROTR, VT, Expand); 362 setOperationAction(ISD::SUB, VT, Expand); 363 setOperationAction(ISD::SINT_TO_FP, VT, Expand); 364 setOperationAction(ISD::UINT_TO_FP, VT, Expand); 365 setOperationAction(ISD::SDIV, VT, Expand); 366 setOperationAction(ISD::UDIV, VT, Expand); 367 setOperationAction(ISD::SREM, VT, Expand); 368 setOperationAction(ISD::UREM, VT, Expand); 369 setOperationAction(ISD::SMUL_LOHI, VT, Expand); 370 setOperationAction(ISD::UMUL_LOHI, VT, Expand); 371 setOperationAction(ISD::SDIVREM, VT, Custom); 372 setOperationAction(ISD::UDIVREM, VT, Expand); 373 setOperationAction(ISD::ADDC, VT, Expand); 374 setOperationAction(ISD::SUBC, VT, Expand); 375 setOperationAction(ISD::ADDE, VT, Expand); 376 setOperationAction(ISD::SUBE, VT, Expand); 377 setOperationAction(ISD::SELECT, VT, Expand); 378 setOperationAction(ISD::VSELECT, VT, Expand); 379 setOperationAction(ISD::SELECT_CC, VT, Expand); 380 setOperationAction(ISD::XOR, VT, Expand); 381 setOperationAction(ISD::BSWAP, VT, Expand); 382 setOperationAction(ISD::CTPOP, VT, Expand); 383 setOperationAction(ISD::CTTZ, VT, Expand); 384 setOperationAction(ISD::CTLZ, VT, Expand); 385 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand); 386 } 387 388 static const MVT::SimpleValueType FloatVectorTypes[] = { 389 MVT::v2f32, MVT::v4f32 390 }; 391 392 for (MVT VT : FloatVectorTypes) { 393 setOperationAction(ISD::FABS, VT, Expand); 394 setOperationAction(ISD::FMINNUM, VT, Expand); 395 setOperationAction(ISD::FMAXNUM, VT, Expand); 396 setOperationAction(ISD::FADD, VT, Expand); 397 setOperationAction(ISD::FCEIL, VT, Expand); 398 setOperationAction(ISD::FCOS, VT, Expand); 399 setOperationAction(ISD::FDIV, VT, Expand); 400 setOperationAction(ISD::FEXP2, VT, Expand); 401 setOperationAction(ISD::FLOG2, VT, Expand); 402 setOperationAction(ISD::FREM, VT, Expand); 403 setOperationAction(ISD::FPOW, VT, Expand); 404 setOperationAction(ISD::FFLOOR, VT, Expand); 405 setOperationAction(ISD::FTRUNC, VT, Expand); 406 setOperationAction(ISD::FMUL, VT, Expand); 407 setOperationAction(ISD::FMA, VT, Expand); 408 setOperationAction(ISD::FRINT, VT, Expand); 409 setOperationAction(ISD::FNEARBYINT, VT, Expand); 410 setOperationAction(ISD::FSQRT, VT, Expand); 411 setOperationAction(ISD::FSIN, VT, Expand); 412 setOperationAction(ISD::FSUB, VT, Expand); 413 setOperationAction(ISD::FNEG, VT, Expand); 414 setOperationAction(ISD::VSELECT, VT, Expand); 415 setOperationAction(ISD::SELECT_CC, VT, Expand); 416 setOperationAction(ISD::FCOPYSIGN, VT, Expand); 417 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand); 418 } 419 420 // This causes using an unrolled select operation rather than expansion with 421 // bit operations. This is in general better, but the alternative using BFI 422 // instructions may be better if the select sources are SGPRs. 423 setOperationAction(ISD::SELECT, MVT::v2f32, Promote); 424 AddPromotedToType(ISD::SELECT, MVT::v2f32, MVT::v2i32); 425 426 setOperationAction(ISD::SELECT, MVT::v4f32, Promote); 427 AddPromotedToType(ISD::SELECT, MVT::v4f32, MVT::v4i32); 428 429 // There are no libcalls of any kind. 430 for (int I = 0; I < RTLIB::UNKNOWN_LIBCALL; ++I) 431 setLibcallName(static_cast<RTLIB::Libcall>(I), nullptr); 432 433 setBooleanContents(ZeroOrNegativeOneBooleanContent); 434 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 435 436 setSchedulingPreference(Sched::RegPressure); 437 setJumpIsExpensive(true); 438 439 // FIXME: This is only partially true. If we have to do vector compares, any 440 // SGPR pair can be a condition register. If we have a uniform condition, we 441 // are better off doing SALU operations, where there is only one SCC. For now, 442 // we don't have a way of knowing during instruction selection if a condition 443 // will be uniform and we always use vector compares. Assume we are using 444 // vector compares until that is fixed. 445 setHasMultipleConditionRegisters(true); 446 447 // SI at least has hardware support for floating point exceptions, but no way 448 // of using or handling them is implemented. They are also optional in OpenCL 449 // (Section 7.3) 450 setHasFloatingPointExceptions(Subtarget->hasFPExceptions()); 451 452 PredictableSelectIsExpensive = false; 453 454 // We want to find all load dependencies for long chains of stores to enable 455 // merging into very wide vectors. The problem is with vectors with > 4 456 // elements. MergeConsecutiveStores will attempt to merge these because x8/x16 457 // vectors are a legal type, even though we have to split the loads 458 // usually. When we can more precisely specify load legality per address 459 // space, we should be able to make FindBetterChain/MergeConsecutiveStores 460 // smarter so that they can figure out what to do in 2 iterations without all 461 // N > 4 stores on the same chain. 462 GatherAllAliasesMaxDepth = 16; 463 464 // FIXME: Need to really handle these. 465 MaxStoresPerMemcpy = 4096; 466 MaxStoresPerMemmove = 4096; 467 MaxStoresPerMemset = 4096; 468 469 setTargetDAGCombine(ISD::BITCAST); 470 setTargetDAGCombine(ISD::SHL); 471 setTargetDAGCombine(ISD::SRA); 472 setTargetDAGCombine(ISD::SRL); 473 setTargetDAGCombine(ISD::MUL); 474 setTargetDAGCombine(ISD::MULHU); 475 setTargetDAGCombine(ISD::MULHS); 476 setTargetDAGCombine(ISD::SELECT); 477 setTargetDAGCombine(ISD::SELECT_CC); 478 setTargetDAGCombine(ISD::STORE); 479 setTargetDAGCombine(ISD::FADD); 480 setTargetDAGCombine(ISD::FSUB); 481 setTargetDAGCombine(ISD::FNEG); 482 setTargetDAGCombine(ISD::FABS); 483 } 484 485 //===----------------------------------------------------------------------===// 486 // Target Information 487 //===----------------------------------------------------------------------===// 488 489 LLVM_READNONE 490 static bool fnegFoldsIntoOp(unsigned Opc) { 491 switch (Opc) { 492 case ISD::FADD: 493 case ISD::FSUB: 494 case ISD::FMUL: 495 case ISD::FMA: 496 case ISD::FMAD: 497 case ISD::FMINNUM: 498 case ISD::FMAXNUM: 499 case ISD::FSIN: 500 case ISD::FTRUNC: 501 case ISD::FRINT: 502 case ISD::FNEARBYINT: 503 case AMDGPUISD::RCP: 504 case AMDGPUISD::RCP_LEGACY: 505 case AMDGPUISD::SIN_HW: 506 case AMDGPUISD::FMUL_LEGACY: 507 case AMDGPUISD::FMIN_LEGACY: 508 case AMDGPUISD::FMAX_LEGACY: 509 return true; 510 default: 511 return false; 512 } 513 } 514 515 /// \p returns true if the operation will definitely need to use a 64-bit 516 /// encoding, and thus will use a VOP3 encoding regardless of the source 517 /// modifiers. 518 LLVM_READONLY 519 static bool opMustUseVOP3Encoding(const SDNode *N, MVT VT) { 520 return N->getNumOperands() > 2 || VT == MVT::f64; 521 } 522 523 // Most FP instructions support source modifiers, but this could be refined 524 // slightly. 525 LLVM_READONLY 526 static bool hasSourceMods(const SDNode *N) { 527 if (isa<MemSDNode>(N)) 528 return false; 529 530 switch (N->getOpcode()) { 531 case ISD::CopyToReg: 532 case ISD::SELECT: 533 case ISD::FDIV: 534 case ISD::FREM: 535 case ISD::INLINEASM: 536 case AMDGPUISD::INTERP_P1: 537 case AMDGPUISD::INTERP_P2: 538 case AMDGPUISD::DIV_SCALE: 539 return false; 540 default: 541 return true; 542 } 543 } 544 545 static bool allUsesHaveSourceMods(const SDNode *N, unsigned CostThreshold = 4) { 546 // Some users (such as 3-operand FMA/MAD) must use a VOP3 encoding, and thus 547 // it is truly free to use a source modifier in all cases. If there are 548 // multiple users but for each one will necessitate using VOP3, there will be 549 // a code size increase. Try to avoid increasing code size unless we know it 550 // will save on the instruction count. 551 unsigned NumMayIncreaseSize = 0; 552 MVT VT = N->getValueType(0).getScalarType().getSimpleVT(); 553 554 // XXX - Should this limit number of uses to check? 555 for (const SDNode *U : N->uses()) { 556 if (!hasSourceMods(U)) 557 return false; 558 559 if (!opMustUseVOP3Encoding(U, VT)) { 560 if (++NumMayIncreaseSize > CostThreshold) 561 return false; 562 } 563 } 564 565 return true; 566 } 567 568 MVT AMDGPUTargetLowering::getVectorIdxTy(const DataLayout &) const { 569 return MVT::i32; 570 } 571 572 bool AMDGPUTargetLowering::isSelectSupported(SelectSupportKind SelType) const { 573 return true; 574 } 575 576 // The backend supports 32 and 64 bit floating point immediates. 577 // FIXME: Why are we reporting vectors of FP immediates as legal? 578 bool AMDGPUTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { 579 EVT ScalarVT = VT.getScalarType(); 580 return (ScalarVT == MVT::f32 || ScalarVT == MVT::f64 || 581 (ScalarVT == MVT::f16 && Subtarget->has16BitInsts())); 582 } 583 584 // We don't want to shrink f64 / f32 constants. 585 bool AMDGPUTargetLowering::ShouldShrinkFPConstant(EVT VT) const { 586 EVT ScalarVT = VT.getScalarType(); 587 return (ScalarVT != MVT::f32 && ScalarVT != MVT::f64); 588 } 589 590 bool AMDGPUTargetLowering::shouldReduceLoadWidth(SDNode *N, 591 ISD::LoadExtType, 592 EVT NewVT) const { 593 594 unsigned NewSize = NewVT.getStoreSizeInBits(); 595 596 // If we are reducing to a 32-bit load, this is always better. 597 if (NewSize == 32) 598 return true; 599 600 EVT OldVT = N->getValueType(0); 601 unsigned OldSize = OldVT.getStoreSizeInBits(); 602 603 // Don't produce extloads from sub 32-bit types. SI doesn't have scalar 604 // extloads, so doing one requires using a buffer_load. In cases where we 605 // still couldn't use a scalar load, using the wider load shouldn't really 606 // hurt anything. 607 608 // If the old size already had to be an extload, there's no harm in continuing 609 // to reduce the width. 610 return (OldSize < 32); 611 } 612 613 bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy, 614 EVT CastTy) const { 615 616 assert(LoadTy.getSizeInBits() == CastTy.getSizeInBits()); 617 618 if (LoadTy.getScalarType() == MVT::i32) 619 return false; 620 621 unsigned LScalarSize = LoadTy.getScalarSizeInBits(); 622 unsigned CastScalarSize = CastTy.getScalarSizeInBits(); 623 624 return (LScalarSize < CastScalarSize) || 625 (CastScalarSize >= 32); 626 } 627 628 // SI+ has instructions for cttz / ctlz for 32-bit values. This is probably also 629 // profitable with the expansion for 64-bit since it's generally good to 630 // speculate things. 631 // FIXME: These should really have the size as a parameter. 632 bool AMDGPUTargetLowering::isCheapToSpeculateCttz() const { 633 return true; 634 } 635 636 bool AMDGPUTargetLowering::isCheapToSpeculateCtlz() const { 637 return true; 638 } 639 640 //===---------------------------------------------------------------------===// 641 // Target Properties 642 //===---------------------------------------------------------------------===// 643 644 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const { 645 assert(VT.isFloatingPoint()); 646 return VT == MVT::f32 || VT == MVT::f64 || (Subtarget->has16BitInsts() && 647 VT == MVT::f16); 648 } 649 650 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const { 651 return isFAbsFree(VT); 652 } 653 654 bool AMDGPUTargetLowering:: storeOfVectorConstantIsCheap(EVT MemVT, 655 unsigned NumElem, 656 unsigned AS) const { 657 return true; 658 } 659 660 bool AMDGPUTargetLowering::aggressivelyPreferBuildVectorSources(EVT VecVT) const { 661 // There are few operations which truly have vector input operands. Any vector 662 // operation is going to involve operations on each component, and a 663 // build_vector will be a copy per element, so it always makes sense to use a 664 // build_vector input in place of the extracted element to avoid a copy into a 665 // super register. 666 // 667 // We should probably only do this if all users are extracts only, but this 668 // should be the common case. 669 return true; 670 } 671 672 bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const { 673 // Truncate is just accessing a subregister. 674 675 unsigned SrcSize = Source.getSizeInBits(); 676 unsigned DestSize = Dest.getSizeInBits(); 677 678 return DestSize < SrcSize && DestSize % 32 == 0 ; 679 } 680 681 bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const { 682 // Truncate is just accessing a subregister. 683 684 unsigned SrcSize = Source->getScalarSizeInBits(); 685 unsigned DestSize = Dest->getScalarSizeInBits(); 686 687 if (DestSize== 16 && Subtarget->has16BitInsts()) 688 return SrcSize >= 32; 689 690 return DestSize < SrcSize && DestSize % 32 == 0; 691 } 692 693 bool AMDGPUTargetLowering::isZExtFree(Type *Src, Type *Dest) const { 694 unsigned SrcSize = Src->getScalarSizeInBits(); 695 unsigned DestSize = Dest->getScalarSizeInBits(); 696 697 if (SrcSize == 16 && Subtarget->has16BitInsts()) 698 return DestSize >= 32; 699 700 return SrcSize == 32 && DestSize == 64; 701 } 702 703 bool AMDGPUTargetLowering::isZExtFree(EVT Src, EVT Dest) const { 704 // Any register load of a 64-bit value really requires 2 32-bit moves. For all 705 // practical purposes, the extra mov 0 to load a 64-bit is free. As used, 706 // this will enable reducing 64-bit operations the 32-bit, which is always 707 // good. 708 709 if (Src == MVT::i16) 710 return Dest == MVT::i32 ||Dest == MVT::i64 ; 711 712 return Src == MVT::i32 && Dest == MVT::i64; 713 } 714 715 bool AMDGPUTargetLowering::isZExtFree(SDValue Val, EVT VT2) const { 716 return isZExtFree(Val.getValueType(), VT2); 717 } 718 719 bool AMDGPUTargetLowering::isNarrowingProfitable(EVT SrcVT, EVT DestVT) const { 720 // There aren't really 64-bit registers, but pairs of 32-bit ones and only a 721 // limited number of native 64-bit operations. Shrinking an operation to fit 722 // in a single 32-bit register should always be helpful. As currently used, 723 // this is much less general than the name suggests, and is only used in 724 // places trying to reduce the sizes of loads. Shrinking loads to < 32-bits is 725 // not profitable, and may actually be harmful. 726 return SrcVT.getSizeInBits() > 32 && DestVT.getSizeInBits() == 32; 727 } 728 729 //===---------------------------------------------------------------------===// 730 // TargetLowering Callbacks 731 //===---------------------------------------------------------------------===// 732 733 CCAssignFn *AMDGPUCallLowering::CCAssignFnForCall(CallingConv::ID CC, 734 bool IsVarArg) const { 735 return CC_AMDGPU; 736 } 737 738 /// The SelectionDAGBuilder will automatically promote function arguments 739 /// with illegal types. However, this does not work for the AMDGPU targets 740 /// since the function arguments are stored in memory as these illegal types. 741 /// In order to handle this properly we need to get the original types sizes 742 /// from the LLVM IR Function and fixup the ISD:InputArg values before 743 /// passing them to AnalyzeFormalArguments() 744 745 /// When the SelectionDAGBuilder computes the Ins, it takes care of splitting 746 /// input values across multiple registers. Each item in the Ins array 747 /// represents a single value that will be stored in regsters. Ins[x].VT is 748 /// the value type of the value that will be stored in the register, so 749 /// whatever SDNode we lower the argument to needs to be this type. 750 /// 751 /// In order to correctly lower the arguments we need to know the size of each 752 /// argument. Since Ins[x].VT gives us the size of the register that will 753 /// hold the value, we need to look at Ins[x].ArgVT to see the 'real' type 754 /// for the orignal function argument so that we can deduce the correct memory 755 /// type to use for Ins[x]. In most cases the correct memory type will be 756 /// Ins[x].ArgVT. However, this will not always be the case. If, for example, 757 /// we have a kernel argument of type v8i8, this argument will be split into 758 /// 8 parts and each part will be represented by its own item in the Ins array. 759 /// For each part the Ins[x].ArgVT will be the v8i8, which is the full type of 760 /// the argument before it was split. From this, we deduce that the memory type 761 /// for each individual part is i8. We pass the memory type as LocVT to the 762 /// calling convention analysis function and the register type (Ins[x].VT) as 763 /// the ValVT. 764 void AMDGPUTargetLowering::analyzeFormalArgumentsCompute(CCState &State, 765 const SmallVectorImpl<ISD::InputArg> &Ins) const { 766 for (unsigned i = 0, e = Ins.size(); i != e; ++i) { 767 const ISD::InputArg &In = Ins[i]; 768 EVT MemVT; 769 770 unsigned NumRegs = getNumRegisters(State.getContext(), In.ArgVT); 771 772 if (!Subtarget->isAmdHsaOS() && 773 (In.ArgVT == MVT::i16 || In.ArgVT == MVT::i8 || In.ArgVT == MVT::f16)) { 774 // The ABI says the caller will extend these values to 32-bits. 775 MemVT = In.ArgVT.isInteger() ? MVT::i32 : MVT::f32; 776 } else if (NumRegs == 1) { 777 // This argument is not split, so the IR type is the memory type. 778 assert(!In.Flags.isSplit()); 779 if (In.ArgVT.isExtended()) { 780 // We have an extended type, like i24, so we should just use the register type 781 MemVT = In.VT; 782 } else { 783 MemVT = In.ArgVT; 784 } 785 } else if (In.ArgVT.isVector() && In.VT.isVector() && 786 In.ArgVT.getScalarType() == In.VT.getScalarType()) { 787 assert(In.ArgVT.getVectorNumElements() > In.VT.getVectorNumElements()); 788 // We have a vector value which has been split into a vector with 789 // the same scalar type, but fewer elements. This should handle 790 // all the floating-point vector types. 791 MemVT = In.VT; 792 } else if (In.ArgVT.isVector() && 793 In.ArgVT.getVectorNumElements() == NumRegs) { 794 // This arg has been split so that each element is stored in a separate 795 // register. 796 MemVT = In.ArgVT.getScalarType(); 797 } else if (In.ArgVT.isExtended()) { 798 // We have an extended type, like i65. 799 MemVT = In.VT; 800 } else { 801 unsigned MemoryBits = In.ArgVT.getStoreSizeInBits() / NumRegs; 802 assert(In.ArgVT.getStoreSizeInBits() % NumRegs == 0); 803 if (In.VT.isInteger()) { 804 MemVT = EVT::getIntegerVT(State.getContext(), MemoryBits); 805 } else if (In.VT.isVector()) { 806 assert(!In.VT.getScalarType().isFloatingPoint()); 807 unsigned NumElements = In.VT.getVectorNumElements(); 808 assert(MemoryBits % NumElements == 0); 809 // This vector type has been split into another vector type with 810 // a different elements size. 811 EVT ScalarVT = EVT::getIntegerVT(State.getContext(), 812 MemoryBits / NumElements); 813 MemVT = EVT::getVectorVT(State.getContext(), ScalarVT, NumElements); 814 } else { 815 llvm_unreachable("cannot deduce memory type."); 816 } 817 } 818 819 // Convert one element vectors to scalar. 820 if (MemVT.isVector() && MemVT.getVectorNumElements() == 1) 821 MemVT = MemVT.getScalarType(); 822 823 if (MemVT.isExtended()) { 824 // This should really only happen if we have vec3 arguments 825 assert(MemVT.isVector() && MemVT.getVectorNumElements() == 3); 826 MemVT = MemVT.getPow2VectorType(State.getContext()); 827 } 828 829 assert(MemVT.isSimple()); 830 allocateKernArg(i, In.VT, MemVT.getSimpleVT(), CCValAssign::Full, In.Flags, 831 State); 832 } 833 } 834 835 void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State, 836 const SmallVectorImpl<ISD::InputArg> &Ins) const { 837 State.AnalyzeFormalArguments(Ins, CC_AMDGPU); 838 } 839 840 void AMDGPUTargetLowering::AnalyzeReturn(CCState &State, 841 const SmallVectorImpl<ISD::OutputArg> &Outs) const { 842 843 State.AnalyzeReturn(Outs, RetCC_SI); 844 } 845 846 SDValue 847 AMDGPUTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 848 bool isVarArg, 849 const SmallVectorImpl<ISD::OutputArg> &Outs, 850 const SmallVectorImpl<SDValue> &OutVals, 851 const SDLoc &DL, SelectionDAG &DAG) const { 852 return DAG.getNode(AMDGPUISD::ENDPGM, DL, MVT::Other, Chain); 853 } 854 855 //===---------------------------------------------------------------------===// 856 // Target specific lowering 857 //===---------------------------------------------------------------------===// 858 859 SDValue AMDGPUTargetLowering::LowerCall(CallLoweringInfo &CLI, 860 SmallVectorImpl<SDValue> &InVals) const { 861 SDValue Callee = CLI.Callee; 862 SelectionDAG &DAG = CLI.DAG; 863 864 const Function &Fn = *DAG.getMachineFunction().getFunction(); 865 866 StringRef FuncName("<unknown>"); 867 868 if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee)) 869 FuncName = G->getSymbol(); 870 else if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 871 FuncName = G->getGlobal()->getName(); 872 873 DiagnosticInfoUnsupported NoCalls( 874 Fn, "unsupported call to function " + FuncName, CLI.DL.getDebugLoc()); 875 DAG.getContext()->diagnose(NoCalls); 876 877 if (!CLI.IsTailCall) { 878 for (unsigned I = 0, E = CLI.Ins.size(); I != E; ++I) 879 InVals.push_back(DAG.getUNDEF(CLI.Ins[I].VT)); 880 } 881 882 return DAG.getEntryNode(); 883 } 884 885 SDValue AMDGPUTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, 886 SelectionDAG &DAG) const { 887 const Function &Fn = *DAG.getMachineFunction().getFunction(); 888 889 DiagnosticInfoUnsupported NoDynamicAlloca(Fn, "unsupported dynamic alloca", 890 SDLoc(Op).getDebugLoc()); 891 DAG.getContext()->diagnose(NoDynamicAlloca); 892 auto Ops = {DAG.getConstant(0, SDLoc(), Op.getValueType()), Op.getOperand(0)}; 893 return DAG.getMergeValues(Ops, SDLoc()); 894 } 895 896 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op, 897 SelectionDAG &DAG) const { 898 switch (Op.getOpcode()) { 899 default: 900 Op->print(errs(), &DAG); 901 llvm_unreachable("Custom lowering code for this" 902 "instruction is not implemented yet!"); 903 break; 904 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG); 905 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG); 906 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG); 907 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); 908 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG); 909 case ISD::SDIVREM: return LowerSDIVREM(Op, DAG); 910 case ISD::FREM: return LowerFREM(Op, DAG); 911 case ISD::FCEIL: return LowerFCEIL(Op, DAG); 912 case ISD::FTRUNC: return LowerFTRUNC(Op, DAG); 913 case ISD::FRINT: return LowerFRINT(Op, DAG); 914 case ISD::FNEARBYINT: return LowerFNEARBYINT(Op, DAG); 915 case ISD::FROUND: return LowerFROUND(Op, DAG); 916 case ISD::FFLOOR: return LowerFFLOOR(Op, DAG); 917 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG); 918 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG); 919 case ISD::FP_TO_FP16: return LowerFP_TO_FP16(Op, DAG); 920 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG); 921 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG); 922 case ISD::CTLZ: 923 case ISD::CTLZ_ZERO_UNDEF: 924 return LowerCTLZ(Op, DAG); 925 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG); 926 } 927 return Op; 928 } 929 930 void AMDGPUTargetLowering::ReplaceNodeResults(SDNode *N, 931 SmallVectorImpl<SDValue> &Results, 932 SelectionDAG &DAG) const { 933 switch (N->getOpcode()) { 934 case ISD::SIGN_EXTEND_INREG: 935 // Different parts of legalization seem to interpret which type of 936 // sign_extend_inreg is the one to check for custom lowering. The extended 937 // from type is what really matters, but some places check for custom 938 // lowering of the result type. This results in trying to use 939 // ReplaceNodeResults to sext_in_reg to an illegal type, so we'll just do 940 // nothing here and let the illegal result integer be handled normally. 941 return; 942 default: 943 return; 944 } 945 } 946 947 static bool hasDefinedInitializer(const GlobalValue *GV) { 948 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 949 if (!GVar || !GVar->hasInitializer()) 950 return false; 951 952 return !isa<UndefValue>(GVar->getInitializer()); 953 } 954 955 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI, 956 SDValue Op, 957 SelectionDAG &DAG) const { 958 959 const DataLayout &DL = DAG.getDataLayout(); 960 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op); 961 const GlobalValue *GV = G->getGlobal(); 962 963 switch (G->getAddressSpace()) { 964 case AMDGPUAS::LOCAL_ADDRESS: { 965 // XXX: What does the value of G->getOffset() mean? 966 assert(G->getOffset() == 0 && 967 "Do not know what to do with an non-zero offset"); 968 969 // TODO: We could emit code to handle the initialization somewhere. 970 if (hasDefinedInitializer(GV)) 971 break; 972 973 unsigned Offset = MFI->allocateLDSGlobal(DL, *GV); 974 return DAG.getConstant(Offset, SDLoc(Op), Op.getValueType()); 975 } 976 } 977 978 const Function &Fn = *DAG.getMachineFunction().getFunction(); 979 DiagnosticInfoUnsupported BadInit( 980 Fn, "unsupported initializer for address space", SDLoc(Op).getDebugLoc()); 981 DAG.getContext()->diagnose(BadInit); 982 return SDValue(); 983 } 984 985 SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op, 986 SelectionDAG &DAG) const { 987 SmallVector<SDValue, 8> Args; 988 989 for (const SDUse &U : Op->ops()) 990 DAG.ExtractVectorElements(U.get(), Args); 991 992 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args); 993 } 994 995 SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, 996 SelectionDAG &DAG) const { 997 998 SmallVector<SDValue, 8> Args; 999 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 1000 EVT VT = Op.getValueType(); 1001 DAG.ExtractVectorElements(Op.getOperand(0), Args, Start, 1002 VT.getVectorNumElements()); 1003 1004 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args); 1005 } 1006 1007 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, 1008 SelectionDAG &DAG) const { 1009 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 1010 SDLoc DL(Op); 1011 EVT VT = Op.getValueType(); 1012 1013 switch (IntrinsicID) { 1014 default: return Op; 1015 case AMDGPUIntrinsic::AMDGPU_clamp: // Legacy name. 1016 return DAG.getNode(AMDGPUISD::CLAMP, DL, VT, 1017 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 1018 1019 case AMDGPUIntrinsic::AMDGPU_bfe_i32: 1020 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT, 1021 Op.getOperand(1), 1022 Op.getOperand(2), 1023 Op.getOperand(3)); 1024 1025 case AMDGPUIntrinsic::AMDGPU_bfe_u32: 1026 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT, 1027 Op.getOperand(1), 1028 Op.getOperand(2), 1029 Op.getOperand(3)); 1030 } 1031 } 1032 1033 /// \brief Generate Min/Max node 1034 SDValue AMDGPUTargetLowering::combineFMinMaxLegacy(const SDLoc &DL, EVT VT, 1035 SDValue LHS, SDValue RHS, 1036 SDValue True, SDValue False, 1037 SDValue CC, 1038 DAGCombinerInfo &DCI) const { 1039 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True)) 1040 return SDValue(); 1041 1042 SelectionDAG &DAG = DCI.DAG; 1043 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get(); 1044 switch (CCOpcode) { 1045 case ISD::SETOEQ: 1046 case ISD::SETONE: 1047 case ISD::SETUNE: 1048 case ISD::SETNE: 1049 case ISD::SETUEQ: 1050 case ISD::SETEQ: 1051 case ISD::SETFALSE: 1052 case ISD::SETFALSE2: 1053 case ISD::SETTRUE: 1054 case ISD::SETTRUE2: 1055 case ISD::SETUO: 1056 case ISD::SETO: 1057 break; 1058 case ISD::SETULE: 1059 case ISD::SETULT: { 1060 if (LHS == True) 1061 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS); 1062 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS); 1063 } 1064 case ISD::SETOLE: 1065 case ISD::SETOLT: 1066 case ISD::SETLE: 1067 case ISD::SETLT: { 1068 // Ordered. Assume ordered for undefined. 1069 1070 // Only do this after legalization to avoid interfering with other combines 1071 // which might occur. 1072 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG && 1073 !DCI.isCalledByLegalizer()) 1074 return SDValue(); 1075 1076 // We need to permute the operands to get the correct NaN behavior. The 1077 // selected operand is the second one based on the failing compare with NaN, 1078 // so permute it based on the compare type the hardware uses. 1079 if (LHS == True) 1080 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS); 1081 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS); 1082 } 1083 case ISD::SETUGE: 1084 case ISD::SETUGT: { 1085 if (LHS == True) 1086 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS); 1087 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS); 1088 } 1089 case ISD::SETGT: 1090 case ISD::SETGE: 1091 case ISD::SETOGE: 1092 case ISD::SETOGT: { 1093 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG && 1094 !DCI.isCalledByLegalizer()) 1095 return SDValue(); 1096 1097 if (LHS == True) 1098 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS); 1099 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS); 1100 } 1101 case ISD::SETCC_INVALID: 1102 llvm_unreachable("Invalid setcc condcode!"); 1103 } 1104 return SDValue(); 1105 } 1106 1107 std::pair<SDValue, SDValue> 1108 AMDGPUTargetLowering::split64BitValue(SDValue Op, SelectionDAG &DAG) const { 1109 SDLoc SL(Op); 1110 1111 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op); 1112 1113 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 1114 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 1115 1116 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero); 1117 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One); 1118 1119 return std::make_pair(Lo, Hi); 1120 } 1121 1122 SDValue AMDGPUTargetLowering::getLoHalf64(SDValue Op, SelectionDAG &DAG) const { 1123 SDLoc SL(Op); 1124 1125 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op); 1126 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 1127 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero); 1128 } 1129 1130 SDValue AMDGPUTargetLowering::getHiHalf64(SDValue Op, SelectionDAG &DAG) const { 1131 SDLoc SL(Op); 1132 1133 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op); 1134 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 1135 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One); 1136 } 1137 1138 SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue Op, 1139 SelectionDAG &DAG) const { 1140 LoadSDNode *Load = cast<LoadSDNode>(Op); 1141 EVT VT = Op.getValueType(); 1142 1143 1144 // If this is a 2 element vector, we really want to scalarize and not create 1145 // weird 1 element vectors. 1146 if (VT.getVectorNumElements() == 2) 1147 return scalarizeVectorLoad(Load, DAG); 1148 1149 SDValue BasePtr = Load->getBasePtr(); 1150 EVT PtrVT = BasePtr.getValueType(); 1151 EVT MemVT = Load->getMemoryVT(); 1152 SDLoc SL(Op); 1153 1154 const MachinePointerInfo &SrcValue = Load->getMemOperand()->getPointerInfo(); 1155 1156 EVT LoVT, HiVT; 1157 EVT LoMemVT, HiMemVT; 1158 SDValue Lo, Hi; 1159 1160 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT); 1161 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT); 1162 std::tie(Lo, Hi) = DAG.SplitVector(Op, SL, LoVT, HiVT); 1163 1164 unsigned Size = LoMemVT.getStoreSize(); 1165 unsigned BaseAlign = Load->getAlignment(); 1166 unsigned HiAlign = MinAlign(BaseAlign, Size); 1167 1168 SDValue LoLoad = DAG.getExtLoad(Load->getExtensionType(), SL, LoVT, 1169 Load->getChain(), BasePtr, SrcValue, LoMemVT, 1170 BaseAlign, Load->getMemOperand()->getFlags()); 1171 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr, 1172 DAG.getConstant(Size, SL, PtrVT)); 1173 SDValue HiLoad = 1174 DAG.getExtLoad(Load->getExtensionType(), SL, HiVT, Load->getChain(), 1175 HiPtr, SrcValue.getWithOffset(LoMemVT.getStoreSize()), 1176 HiMemVT, HiAlign, Load->getMemOperand()->getFlags()); 1177 1178 SDValue Ops[] = { 1179 DAG.getNode(ISD::CONCAT_VECTORS, SL, VT, LoLoad, HiLoad), 1180 DAG.getNode(ISD::TokenFactor, SL, MVT::Other, 1181 LoLoad.getValue(1), HiLoad.getValue(1)) 1182 }; 1183 1184 return DAG.getMergeValues(Ops, SL); 1185 } 1186 1187 SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op, 1188 SelectionDAG &DAG) const { 1189 StoreSDNode *Store = cast<StoreSDNode>(Op); 1190 SDValue Val = Store->getValue(); 1191 EVT VT = Val.getValueType(); 1192 1193 // If this is a 2 element vector, we really want to scalarize and not create 1194 // weird 1 element vectors. 1195 if (VT.getVectorNumElements() == 2) 1196 return scalarizeVectorStore(Store, DAG); 1197 1198 EVT MemVT = Store->getMemoryVT(); 1199 SDValue Chain = Store->getChain(); 1200 SDValue BasePtr = Store->getBasePtr(); 1201 SDLoc SL(Op); 1202 1203 EVT LoVT, HiVT; 1204 EVT LoMemVT, HiMemVT; 1205 SDValue Lo, Hi; 1206 1207 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT); 1208 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT); 1209 std::tie(Lo, Hi) = DAG.SplitVector(Val, SL, LoVT, HiVT); 1210 1211 EVT PtrVT = BasePtr.getValueType(); 1212 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr, 1213 DAG.getConstant(LoMemVT.getStoreSize(), SL, 1214 PtrVT)); 1215 1216 const MachinePointerInfo &SrcValue = Store->getMemOperand()->getPointerInfo(); 1217 unsigned BaseAlign = Store->getAlignment(); 1218 unsigned Size = LoMemVT.getStoreSize(); 1219 unsigned HiAlign = MinAlign(BaseAlign, Size); 1220 1221 SDValue LoStore = 1222 DAG.getTruncStore(Chain, SL, Lo, BasePtr, SrcValue, LoMemVT, BaseAlign, 1223 Store->getMemOperand()->getFlags()); 1224 SDValue HiStore = 1225 DAG.getTruncStore(Chain, SL, Hi, HiPtr, SrcValue.getWithOffset(Size), 1226 HiMemVT, HiAlign, Store->getMemOperand()->getFlags()); 1227 1228 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoStore, HiStore); 1229 } 1230 1231 // This is a shortcut for integer division because we have fast i32<->f32 1232 // conversions, and fast f32 reciprocal instructions. The fractional part of a 1233 // float is enough to accurately represent up to a 24-bit signed integer. 1234 SDValue AMDGPUTargetLowering::LowerDIVREM24(SDValue Op, SelectionDAG &DAG, 1235 bool Sign) const { 1236 SDLoc DL(Op); 1237 EVT VT = Op.getValueType(); 1238 SDValue LHS = Op.getOperand(0); 1239 SDValue RHS = Op.getOperand(1); 1240 MVT IntVT = MVT::i32; 1241 MVT FltVT = MVT::f32; 1242 1243 unsigned LHSSignBits = DAG.ComputeNumSignBits(LHS); 1244 if (LHSSignBits < 9) 1245 return SDValue(); 1246 1247 unsigned RHSSignBits = DAG.ComputeNumSignBits(RHS); 1248 if (RHSSignBits < 9) 1249 return SDValue(); 1250 1251 unsigned BitSize = VT.getSizeInBits(); 1252 unsigned SignBits = std::min(LHSSignBits, RHSSignBits); 1253 unsigned DivBits = BitSize - SignBits; 1254 if (Sign) 1255 ++DivBits; 1256 1257 ISD::NodeType ToFp = Sign ? ISD::SINT_TO_FP : ISD::UINT_TO_FP; 1258 ISD::NodeType ToInt = Sign ? ISD::FP_TO_SINT : ISD::FP_TO_UINT; 1259 1260 SDValue jq = DAG.getConstant(1, DL, IntVT); 1261 1262 if (Sign) { 1263 // char|short jq = ia ^ ib; 1264 jq = DAG.getNode(ISD::XOR, DL, VT, LHS, RHS); 1265 1266 // jq = jq >> (bitsize - 2) 1267 jq = DAG.getNode(ISD::SRA, DL, VT, jq, 1268 DAG.getConstant(BitSize - 2, DL, VT)); 1269 1270 // jq = jq | 0x1 1271 jq = DAG.getNode(ISD::OR, DL, VT, jq, DAG.getConstant(1, DL, VT)); 1272 } 1273 1274 // int ia = (int)LHS; 1275 SDValue ia = LHS; 1276 1277 // int ib, (int)RHS; 1278 SDValue ib = RHS; 1279 1280 // float fa = (float)ia; 1281 SDValue fa = DAG.getNode(ToFp, DL, FltVT, ia); 1282 1283 // float fb = (float)ib; 1284 SDValue fb = DAG.getNode(ToFp, DL, FltVT, ib); 1285 1286 SDValue fq = DAG.getNode(ISD::FMUL, DL, FltVT, 1287 fa, DAG.getNode(AMDGPUISD::RCP, DL, FltVT, fb)); 1288 1289 // fq = trunc(fq); 1290 fq = DAG.getNode(ISD::FTRUNC, DL, FltVT, fq); 1291 1292 // float fqneg = -fq; 1293 SDValue fqneg = DAG.getNode(ISD::FNEG, DL, FltVT, fq); 1294 1295 // float fr = mad(fqneg, fb, fa); 1296 SDValue fr = DAG.getNode(ISD::FMAD, DL, FltVT, fqneg, fb, fa); 1297 1298 // int iq = (int)fq; 1299 SDValue iq = DAG.getNode(ToInt, DL, IntVT, fq); 1300 1301 // fr = fabs(fr); 1302 fr = DAG.getNode(ISD::FABS, DL, FltVT, fr); 1303 1304 // fb = fabs(fb); 1305 fb = DAG.getNode(ISD::FABS, DL, FltVT, fb); 1306 1307 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 1308 1309 // int cv = fr >= fb; 1310 SDValue cv = DAG.getSetCC(DL, SetCCVT, fr, fb, ISD::SETOGE); 1311 1312 // jq = (cv ? jq : 0); 1313 jq = DAG.getNode(ISD::SELECT, DL, VT, cv, jq, DAG.getConstant(0, DL, VT)); 1314 1315 // dst = iq + jq; 1316 SDValue Div = DAG.getNode(ISD::ADD, DL, VT, iq, jq); 1317 1318 // Rem needs compensation, it's easier to recompute it 1319 SDValue Rem = DAG.getNode(ISD::MUL, DL, VT, Div, RHS); 1320 Rem = DAG.getNode(ISD::SUB, DL, VT, LHS, Rem); 1321 1322 // Truncate to number of bits this divide really is. 1323 if (Sign) { 1324 SDValue InRegSize 1325 = DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), DivBits)); 1326 Div = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Div, InRegSize); 1327 Rem = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Rem, InRegSize); 1328 } else { 1329 SDValue TruncMask = DAG.getConstant((UINT64_C(1) << DivBits) - 1, DL, VT); 1330 Div = DAG.getNode(ISD::AND, DL, VT, Div, TruncMask); 1331 Rem = DAG.getNode(ISD::AND, DL, VT, Rem, TruncMask); 1332 } 1333 1334 return DAG.getMergeValues({ Div, Rem }, DL); 1335 } 1336 1337 void AMDGPUTargetLowering::LowerUDIVREM64(SDValue Op, 1338 SelectionDAG &DAG, 1339 SmallVectorImpl<SDValue> &Results) const { 1340 assert(Op.getValueType() == MVT::i64); 1341 1342 SDLoc DL(Op); 1343 EVT VT = Op.getValueType(); 1344 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext()); 1345 1346 SDValue one = DAG.getConstant(1, DL, HalfVT); 1347 SDValue zero = DAG.getConstant(0, DL, HalfVT); 1348 1349 //HiLo split 1350 SDValue LHS = Op.getOperand(0); 1351 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero); 1352 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one); 1353 1354 SDValue RHS = Op.getOperand(1); 1355 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero); 1356 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one); 1357 1358 if (VT == MVT::i64 && 1359 DAG.MaskedValueIsZero(RHS, APInt::getHighBitsSet(64, 32)) && 1360 DAG.MaskedValueIsZero(LHS, APInt::getHighBitsSet(64, 32))) { 1361 1362 SDValue Res = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(HalfVT, HalfVT), 1363 LHS_Lo, RHS_Lo); 1364 1365 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(0), zero}); 1366 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(1), zero}); 1367 1368 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV)); 1369 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM)); 1370 return; 1371 } 1372 1373 // Get Speculative values 1374 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo); 1375 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo); 1376 1377 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ); 1378 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {REM_Lo, zero}); 1379 REM = DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM); 1380 1381 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ); 1382 SDValue DIV_Lo = zero; 1383 1384 const unsigned halfBitWidth = HalfVT.getSizeInBits(); 1385 1386 for (unsigned i = 0; i < halfBitWidth; ++i) { 1387 const unsigned bitPos = halfBitWidth - i - 1; 1388 SDValue POS = DAG.getConstant(bitPos, DL, HalfVT); 1389 // Get value of high bit 1390 SDValue HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS); 1391 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one); 1392 HBit = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, HBit); 1393 1394 // Shift 1395 REM = DAG.getNode(ISD::SHL, DL, VT, REM, DAG.getConstant(1, DL, VT)); 1396 // Add LHS high bit 1397 REM = DAG.getNode(ISD::OR, DL, VT, REM, HBit); 1398 1399 SDValue BIT = DAG.getConstant(1ULL << bitPos, DL, HalfVT); 1400 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETUGE); 1401 1402 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT); 1403 1404 // Update REM 1405 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS); 1406 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETUGE); 1407 } 1408 1409 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {DIV_Lo, DIV_Hi}); 1410 DIV = DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV); 1411 Results.push_back(DIV); 1412 Results.push_back(REM); 1413 } 1414 1415 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op, 1416 SelectionDAG &DAG) const { 1417 SDLoc DL(Op); 1418 EVT VT = Op.getValueType(); 1419 1420 if (VT == MVT::i64) { 1421 SmallVector<SDValue, 2> Results; 1422 LowerUDIVREM64(Op, DAG, Results); 1423 return DAG.getMergeValues(Results, DL); 1424 } 1425 1426 if (VT == MVT::i32) { 1427 if (SDValue Res = LowerDIVREM24(Op, DAG, false)) 1428 return Res; 1429 } 1430 1431 SDValue Num = Op.getOperand(0); 1432 SDValue Den = Op.getOperand(1); 1433 1434 // RCP = URECIP(Den) = 2^32 / Den + e 1435 // e is rounding error. 1436 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den); 1437 1438 // RCP_LO = mul(RCP, Den) */ 1439 SDValue RCP_LO = DAG.getNode(ISD::MUL, DL, VT, RCP, Den); 1440 1441 // RCP_HI = mulhu (RCP, Den) */ 1442 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den); 1443 1444 // NEG_RCP_LO = -RCP_LO 1445 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), 1446 RCP_LO); 1447 1448 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO) 1449 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT), 1450 NEG_RCP_LO, RCP_LO, 1451 ISD::SETEQ); 1452 // Calculate the rounding error from the URECIP instruction 1453 // E = mulhu(ABS_RCP_LO, RCP) 1454 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP); 1455 1456 // RCP_A_E = RCP + E 1457 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E); 1458 1459 // RCP_S_E = RCP - E 1460 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E); 1461 1462 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E) 1463 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT), 1464 RCP_A_E, RCP_S_E, 1465 ISD::SETEQ); 1466 // Quotient = mulhu(Tmp0, Num) 1467 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num); 1468 1469 // Num_S_Remainder = Quotient * Den 1470 SDValue Num_S_Remainder = DAG.getNode(ISD::MUL, DL, VT, Quotient, Den); 1471 1472 // Remainder = Num - Num_S_Remainder 1473 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder); 1474 1475 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0) 1476 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den, 1477 DAG.getConstant(-1, DL, VT), 1478 DAG.getConstant(0, DL, VT), 1479 ISD::SETUGE); 1480 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0) 1481 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num, 1482 Num_S_Remainder, 1483 DAG.getConstant(-1, DL, VT), 1484 DAG.getConstant(0, DL, VT), 1485 ISD::SETUGE); 1486 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero 1487 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den, 1488 Remainder_GE_Zero); 1489 1490 // Calculate Division result: 1491 1492 // Quotient_A_One = Quotient + 1 1493 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient, 1494 DAG.getConstant(1, DL, VT)); 1495 1496 // Quotient_S_One = Quotient - 1 1497 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient, 1498 DAG.getConstant(1, DL, VT)); 1499 1500 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One) 1501 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT), 1502 Quotient, Quotient_A_One, ISD::SETEQ); 1503 1504 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div) 1505 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT), 1506 Quotient_S_One, Div, ISD::SETEQ); 1507 1508 // Calculate Rem result: 1509 1510 // Remainder_S_Den = Remainder - Den 1511 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den); 1512 1513 // Remainder_A_Den = Remainder + Den 1514 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den); 1515 1516 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den) 1517 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT), 1518 Remainder, Remainder_S_Den, ISD::SETEQ); 1519 1520 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem) 1521 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT), 1522 Remainder_A_Den, Rem, ISD::SETEQ); 1523 SDValue Ops[2] = { 1524 Div, 1525 Rem 1526 }; 1527 return DAG.getMergeValues(Ops, DL); 1528 } 1529 1530 SDValue AMDGPUTargetLowering::LowerSDIVREM(SDValue Op, 1531 SelectionDAG &DAG) const { 1532 SDLoc DL(Op); 1533 EVT VT = Op.getValueType(); 1534 1535 SDValue LHS = Op.getOperand(0); 1536 SDValue RHS = Op.getOperand(1); 1537 1538 SDValue Zero = DAG.getConstant(0, DL, VT); 1539 SDValue NegOne = DAG.getConstant(-1, DL, VT); 1540 1541 if (VT == MVT::i32) { 1542 if (SDValue Res = LowerDIVREM24(Op, DAG, true)) 1543 return Res; 1544 } 1545 1546 if (VT == MVT::i64 && 1547 DAG.ComputeNumSignBits(LHS) > 32 && 1548 DAG.ComputeNumSignBits(RHS) > 32) { 1549 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext()); 1550 1551 //HiLo split 1552 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, Zero); 1553 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, Zero); 1554 SDValue DIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(HalfVT, HalfVT), 1555 LHS_Lo, RHS_Lo); 1556 SDValue Res[2] = { 1557 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(0)), 1558 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(1)) 1559 }; 1560 return DAG.getMergeValues(Res, DL); 1561 } 1562 1563 SDValue LHSign = DAG.getSelectCC(DL, LHS, Zero, NegOne, Zero, ISD::SETLT); 1564 SDValue RHSign = DAG.getSelectCC(DL, RHS, Zero, NegOne, Zero, ISD::SETLT); 1565 SDValue DSign = DAG.getNode(ISD::XOR, DL, VT, LHSign, RHSign); 1566 SDValue RSign = LHSign; // Remainder sign is the same as LHS 1567 1568 LHS = DAG.getNode(ISD::ADD, DL, VT, LHS, LHSign); 1569 RHS = DAG.getNode(ISD::ADD, DL, VT, RHS, RHSign); 1570 1571 LHS = DAG.getNode(ISD::XOR, DL, VT, LHS, LHSign); 1572 RHS = DAG.getNode(ISD::XOR, DL, VT, RHS, RHSign); 1573 1574 SDValue Div = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT), LHS, RHS); 1575 SDValue Rem = Div.getValue(1); 1576 1577 Div = DAG.getNode(ISD::XOR, DL, VT, Div, DSign); 1578 Rem = DAG.getNode(ISD::XOR, DL, VT, Rem, RSign); 1579 1580 Div = DAG.getNode(ISD::SUB, DL, VT, Div, DSign); 1581 Rem = DAG.getNode(ISD::SUB, DL, VT, Rem, RSign); 1582 1583 SDValue Res[2] = { 1584 Div, 1585 Rem 1586 }; 1587 return DAG.getMergeValues(Res, DL); 1588 } 1589 1590 // (frem x, y) -> (fsub x, (fmul (ftrunc (fdiv x, y)), y)) 1591 SDValue AMDGPUTargetLowering::LowerFREM(SDValue Op, SelectionDAG &DAG) const { 1592 SDLoc SL(Op); 1593 EVT VT = Op.getValueType(); 1594 SDValue X = Op.getOperand(0); 1595 SDValue Y = Op.getOperand(1); 1596 1597 // TODO: Should this propagate fast-math-flags? 1598 1599 SDValue Div = DAG.getNode(ISD::FDIV, SL, VT, X, Y); 1600 SDValue Floor = DAG.getNode(ISD::FTRUNC, SL, VT, Div); 1601 SDValue Mul = DAG.getNode(ISD::FMUL, SL, VT, Floor, Y); 1602 1603 return DAG.getNode(ISD::FSUB, SL, VT, X, Mul); 1604 } 1605 1606 SDValue AMDGPUTargetLowering::LowerFCEIL(SDValue Op, SelectionDAG &DAG) const { 1607 SDLoc SL(Op); 1608 SDValue Src = Op.getOperand(0); 1609 1610 // result = trunc(src) 1611 // if (src > 0.0 && src != result) 1612 // result += 1.0 1613 1614 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src); 1615 1616 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64); 1617 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64); 1618 1619 EVT SetCCVT = 1620 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64); 1621 1622 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOGT); 1623 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE); 1624 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc); 1625 1626 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, One, Zero); 1627 // TODO: Should this propagate fast-math-flags? 1628 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add); 1629 } 1630 1631 static SDValue extractF64Exponent(SDValue Hi, const SDLoc &SL, 1632 SelectionDAG &DAG) { 1633 const unsigned FractBits = 52; 1634 const unsigned ExpBits = 11; 1635 1636 SDValue ExpPart = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32, 1637 Hi, 1638 DAG.getConstant(FractBits - 32, SL, MVT::i32), 1639 DAG.getConstant(ExpBits, SL, MVT::i32)); 1640 SDValue Exp = DAG.getNode(ISD::SUB, SL, MVT::i32, ExpPart, 1641 DAG.getConstant(1023, SL, MVT::i32)); 1642 1643 return Exp; 1644 } 1645 1646 SDValue AMDGPUTargetLowering::LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const { 1647 SDLoc SL(Op); 1648 SDValue Src = Op.getOperand(0); 1649 1650 assert(Op.getValueType() == MVT::f64); 1651 1652 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 1653 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 1654 1655 SDValue VecSrc = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src); 1656 1657 // Extract the upper half, since this is where we will find the sign and 1658 // exponent. 1659 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecSrc, One); 1660 1661 SDValue Exp = extractF64Exponent(Hi, SL, DAG); 1662 1663 const unsigned FractBits = 52; 1664 1665 // Extract the sign bit. 1666 const SDValue SignBitMask = DAG.getConstant(UINT32_C(1) << 31, SL, MVT::i32); 1667 SDValue SignBit = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, SignBitMask); 1668 1669 // Extend back to to 64-bits. 1670 SDValue SignBit64 = DAG.getBuildVector(MVT::v2i32, SL, {Zero, SignBit}); 1671 SignBit64 = DAG.getNode(ISD::BITCAST, SL, MVT::i64, SignBit64); 1672 1673 SDValue BcInt = DAG.getNode(ISD::BITCAST, SL, MVT::i64, Src); 1674 const SDValue FractMask 1675 = DAG.getConstant((UINT64_C(1) << FractBits) - 1, SL, MVT::i64); 1676 1677 SDValue Shr = DAG.getNode(ISD::SRA, SL, MVT::i64, FractMask, Exp); 1678 SDValue Not = DAG.getNOT(SL, Shr, MVT::i64); 1679 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, BcInt, Not); 1680 1681 EVT SetCCVT = 1682 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32); 1683 1684 const SDValue FiftyOne = DAG.getConstant(FractBits - 1, SL, MVT::i32); 1685 1686 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT); 1687 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT); 1688 1689 SDValue Tmp1 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpLt0, SignBit64, Tmp0); 1690 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpGt51, BcInt, Tmp1); 1691 1692 return DAG.getNode(ISD::BITCAST, SL, MVT::f64, Tmp2); 1693 } 1694 1695 SDValue AMDGPUTargetLowering::LowerFRINT(SDValue Op, SelectionDAG &DAG) const { 1696 SDLoc SL(Op); 1697 SDValue Src = Op.getOperand(0); 1698 1699 assert(Op.getValueType() == MVT::f64); 1700 1701 APFloat C1Val(APFloat::IEEEdouble(), "0x1.0p+52"); 1702 SDValue C1 = DAG.getConstantFP(C1Val, SL, MVT::f64); 1703 SDValue CopySign = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, C1, Src); 1704 1705 // TODO: Should this propagate fast-math-flags? 1706 1707 SDValue Tmp1 = DAG.getNode(ISD::FADD, SL, MVT::f64, Src, CopySign); 1708 SDValue Tmp2 = DAG.getNode(ISD::FSUB, SL, MVT::f64, Tmp1, CopySign); 1709 1710 SDValue Fabs = DAG.getNode(ISD::FABS, SL, MVT::f64, Src); 1711 1712 APFloat C2Val(APFloat::IEEEdouble(), "0x1.fffffffffffffp+51"); 1713 SDValue C2 = DAG.getConstantFP(C2Val, SL, MVT::f64); 1714 1715 EVT SetCCVT = 1716 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64); 1717 SDValue Cond = DAG.getSetCC(SL, SetCCVT, Fabs, C2, ISD::SETOGT); 1718 1719 return DAG.getSelect(SL, MVT::f64, Cond, Src, Tmp2); 1720 } 1721 1722 SDValue AMDGPUTargetLowering::LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const { 1723 // FNEARBYINT and FRINT are the same, except in their handling of FP 1724 // exceptions. Those aren't really meaningful for us, and OpenCL only has 1725 // rint, so just treat them as equivalent. 1726 return DAG.getNode(ISD::FRINT, SDLoc(Op), Op.getValueType(), Op.getOperand(0)); 1727 } 1728 1729 // XXX - May require not supporting f32 denormals? 1730 SDValue AMDGPUTargetLowering::LowerFROUND32(SDValue Op, SelectionDAG &DAG) const { 1731 SDLoc SL(Op); 1732 SDValue X = Op.getOperand(0); 1733 1734 SDValue T = DAG.getNode(ISD::FTRUNC, SL, MVT::f32, X); 1735 1736 // TODO: Should this propagate fast-math-flags? 1737 1738 SDValue Diff = DAG.getNode(ISD::FSUB, SL, MVT::f32, X, T); 1739 1740 SDValue AbsDiff = DAG.getNode(ISD::FABS, SL, MVT::f32, Diff); 1741 1742 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f32); 1743 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); 1744 const SDValue Half = DAG.getConstantFP(0.5, SL, MVT::f32); 1745 1746 SDValue SignOne = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f32, One, X); 1747 1748 EVT SetCCVT = 1749 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32); 1750 1751 SDValue Cmp = DAG.getSetCC(SL, SetCCVT, AbsDiff, Half, ISD::SETOGE); 1752 1753 SDValue Sel = DAG.getNode(ISD::SELECT, SL, MVT::f32, Cmp, SignOne, Zero); 1754 1755 return DAG.getNode(ISD::FADD, SL, MVT::f32, T, Sel); 1756 } 1757 1758 SDValue AMDGPUTargetLowering::LowerFROUND64(SDValue Op, SelectionDAG &DAG) const { 1759 SDLoc SL(Op); 1760 SDValue X = Op.getOperand(0); 1761 1762 SDValue L = DAG.getNode(ISD::BITCAST, SL, MVT::i64, X); 1763 1764 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 1765 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 1766 const SDValue NegOne = DAG.getConstant(-1, SL, MVT::i32); 1767 const SDValue FiftyOne = DAG.getConstant(51, SL, MVT::i32); 1768 EVT SetCCVT = 1769 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32); 1770 1771 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X); 1772 1773 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, One); 1774 1775 SDValue Exp = extractF64Exponent(Hi, SL, DAG); 1776 1777 const SDValue Mask = DAG.getConstant(INT64_C(0x000fffffffffffff), SL, 1778 MVT::i64); 1779 1780 SDValue M = DAG.getNode(ISD::SRA, SL, MVT::i64, Mask, Exp); 1781 SDValue D = DAG.getNode(ISD::SRA, SL, MVT::i64, 1782 DAG.getConstant(INT64_C(0x0008000000000000), SL, 1783 MVT::i64), 1784 Exp); 1785 1786 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, L, M); 1787 SDValue Tmp1 = DAG.getSetCC(SL, SetCCVT, 1788 DAG.getConstant(0, SL, MVT::i64), Tmp0, 1789 ISD::SETNE); 1790 1791 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, Tmp1, 1792 D, DAG.getConstant(0, SL, MVT::i64)); 1793 SDValue K = DAG.getNode(ISD::ADD, SL, MVT::i64, L, Tmp2); 1794 1795 K = DAG.getNode(ISD::AND, SL, MVT::i64, K, DAG.getNOT(SL, M, MVT::i64)); 1796 K = DAG.getNode(ISD::BITCAST, SL, MVT::f64, K); 1797 1798 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT); 1799 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT); 1800 SDValue ExpEqNegOne = DAG.getSetCC(SL, SetCCVT, NegOne, Exp, ISD::SETEQ); 1801 1802 SDValue Mag = DAG.getNode(ISD::SELECT, SL, MVT::f64, 1803 ExpEqNegOne, 1804 DAG.getConstantFP(1.0, SL, MVT::f64), 1805 DAG.getConstantFP(0.0, SL, MVT::f64)); 1806 1807 SDValue S = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, Mag, X); 1808 1809 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpLt0, S, K); 1810 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpGt51, X, K); 1811 1812 return K; 1813 } 1814 1815 SDValue AMDGPUTargetLowering::LowerFROUND(SDValue Op, SelectionDAG &DAG) const { 1816 EVT VT = Op.getValueType(); 1817 1818 if (VT == MVT::f32) 1819 return LowerFROUND32(Op, DAG); 1820 1821 if (VT == MVT::f64) 1822 return LowerFROUND64(Op, DAG); 1823 1824 llvm_unreachable("unhandled type"); 1825 } 1826 1827 SDValue AMDGPUTargetLowering::LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const { 1828 SDLoc SL(Op); 1829 SDValue Src = Op.getOperand(0); 1830 1831 // result = trunc(src); 1832 // if (src < 0.0 && src != result) 1833 // result += -1.0. 1834 1835 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src); 1836 1837 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64); 1838 const SDValue NegOne = DAG.getConstantFP(-1.0, SL, MVT::f64); 1839 1840 EVT SetCCVT = 1841 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64); 1842 1843 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOLT); 1844 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE); 1845 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc); 1846 1847 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, NegOne, Zero); 1848 // TODO: Should this propagate fast-math-flags? 1849 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add); 1850 } 1851 1852 SDValue AMDGPUTargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const { 1853 SDLoc SL(Op); 1854 SDValue Src = Op.getOperand(0); 1855 bool ZeroUndef = Op.getOpcode() == ISD::CTLZ_ZERO_UNDEF; 1856 1857 if (ZeroUndef && Src.getValueType() == MVT::i32) 1858 return DAG.getNode(AMDGPUISD::FFBH_U32, SL, MVT::i32, Src); 1859 1860 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src); 1861 1862 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 1863 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 1864 1865 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero); 1866 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One); 1867 1868 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), 1869 *DAG.getContext(), MVT::i32); 1870 1871 SDValue Hi0 = DAG.getSetCC(SL, SetCCVT, Hi, Zero, ISD::SETEQ); 1872 1873 SDValue CtlzLo = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Lo); 1874 SDValue CtlzHi = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Hi); 1875 1876 const SDValue Bits32 = DAG.getConstant(32, SL, MVT::i32); 1877 SDValue Add = DAG.getNode(ISD::ADD, SL, MVT::i32, CtlzLo, Bits32); 1878 1879 // ctlz(x) = hi_32(x) == 0 ? ctlz(lo_32(x)) + 32 : ctlz(hi_32(x)) 1880 SDValue NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32, Hi0, Add, CtlzHi); 1881 1882 if (!ZeroUndef) { 1883 // Test if the full 64-bit input is zero. 1884 1885 // FIXME: DAG combines turn what should be an s_and_b64 into a v_or_b32, 1886 // which we probably don't want. 1887 SDValue Lo0 = DAG.getSetCC(SL, SetCCVT, Lo, Zero, ISD::SETEQ); 1888 SDValue SrcIsZero = DAG.getNode(ISD::AND, SL, SetCCVT, Lo0, Hi0); 1889 1890 // TODO: If i64 setcc is half rate, it can result in 1 fewer instruction 1891 // with the same cycles, otherwise it is slower. 1892 // SDValue SrcIsZero = DAG.getSetCC(SL, SetCCVT, Src, 1893 // DAG.getConstant(0, SL, MVT::i64), ISD::SETEQ); 1894 1895 const SDValue Bits32 = DAG.getConstant(64, SL, MVT::i32); 1896 1897 // The instruction returns -1 for 0 input, but the defined intrinsic 1898 // behavior is to return the number of bits. 1899 NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32, 1900 SrcIsZero, Bits32, NewCtlz); 1901 } 1902 1903 return DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i64, NewCtlz); 1904 } 1905 1906 SDValue AMDGPUTargetLowering::LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG, 1907 bool Signed) const { 1908 // Unsigned 1909 // cul2f(ulong u) 1910 //{ 1911 // uint lz = clz(u); 1912 // uint e = (u != 0) ? 127U + 63U - lz : 0; 1913 // u = (u << lz) & 0x7fffffffffffffffUL; 1914 // ulong t = u & 0xffffffffffUL; 1915 // uint v = (e << 23) | (uint)(u >> 40); 1916 // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U); 1917 // return as_float(v + r); 1918 //} 1919 // Signed 1920 // cl2f(long l) 1921 //{ 1922 // long s = l >> 63; 1923 // float r = cul2f((l + s) ^ s); 1924 // return s ? -r : r; 1925 //} 1926 1927 SDLoc SL(Op); 1928 SDValue Src = Op.getOperand(0); 1929 SDValue L = Src; 1930 1931 SDValue S; 1932 if (Signed) { 1933 const SDValue SignBit = DAG.getConstant(63, SL, MVT::i64); 1934 S = DAG.getNode(ISD::SRA, SL, MVT::i64, L, SignBit); 1935 1936 SDValue LPlusS = DAG.getNode(ISD::ADD, SL, MVT::i64, L, S); 1937 L = DAG.getNode(ISD::XOR, SL, MVT::i64, LPlusS, S); 1938 } 1939 1940 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), 1941 *DAG.getContext(), MVT::f32); 1942 1943 1944 SDValue ZeroI32 = DAG.getConstant(0, SL, MVT::i32); 1945 SDValue ZeroI64 = DAG.getConstant(0, SL, MVT::i64); 1946 SDValue LZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i64, L); 1947 LZ = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LZ); 1948 1949 SDValue K = DAG.getConstant(127U + 63U, SL, MVT::i32); 1950 SDValue E = DAG.getSelect(SL, MVT::i32, 1951 DAG.getSetCC(SL, SetCCVT, L, ZeroI64, ISD::SETNE), 1952 DAG.getNode(ISD::SUB, SL, MVT::i32, K, LZ), 1953 ZeroI32); 1954 1955 SDValue U = DAG.getNode(ISD::AND, SL, MVT::i64, 1956 DAG.getNode(ISD::SHL, SL, MVT::i64, L, LZ), 1957 DAG.getConstant((-1ULL) >> 1, SL, MVT::i64)); 1958 1959 SDValue T = DAG.getNode(ISD::AND, SL, MVT::i64, U, 1960 DAG.getConstant(0xffffffffffULL, SL, MVT::i64)); 1961 1962 SDValue UShl = DAG.getNode(ISD::SRL, SL, MVT::i64, 1963 U, DAG.getConstant(40, SL, MVT::i64)); 1964 1965 SDValue V = DAG.getNode(ISD::OR, SL, MVT::i32, 1966 DAG.getNode(ISD::SHL, SL, MVT::i32, E, DAG.getConstant(23, SL, MVT::i32)), 1967 DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, UShl)); 1968 1969 SDValue C = DAG.getConstant(0x8000000000ULL, SL, MVT::i64); 1970 SDValue RCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETUGT); 1971 SDValue TCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETEQ); 1972 1973 SDValue One = DAG.getConstant(1, SL, MVT::i32); 1974 1975 SDValue VTrunc1 = DAG.getNode(ISD::AND, SL, MVT::i32, V, One); 1976 1977 SDValue R = DAG.getSelect(SL, MVT::i32, 1978 RCmp, 1979 One, 1980 DAG.getSelect(SL, MVT::i32, TCmp, VTrunc1, ZeroI32)); 1981 R = DAG.getNode(ISD::ADD, SL, MVT::i32, V, R); 1982 R = DAG.getNode(ISD::BITCAST, SL, MVT::f32, R); 1983 1984 if (!Signed) 1985 return R; 1986 1987 SDValue RNeg = DAG.getNode(ISD::FNEG, SL, MVT::f32, R); 1988 return DAG.getSelect(SL, MVT::f32, DAG.getSExtOrTrunc(S, SL, SetCCVT), RNeg, R); 1989 } 1990 1991 SDValue AMDGPUTargetLowering::LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG, 1992 bool Signed) const { 1993 SDLoc SL(Op); 1994 SDValue Src = Op.getOperand(0); 1995 1996 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src); 1997 1998 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, 1999 DAG.getConstant(0, SL, MVT::i32)); 2000 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, 2001 DAG.getConstant(1, SL, MVT::i32)); 2002 2003 SDValue CvtHi = DAG.getNode(Signed ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, 2004 SL, MVT::f64, Hi); 2005 2006 SDValue CvtLo = DAG.getNode(ISD::UINT_TO_FP, SL, MVT::f64, Lo); 2007 2008 SDValue LdExp = DAG.getNode(AMDGPUISD::LDEXP, SL, MVT::f64, CvtHi, 2009 DAG.getConstant(32, SL, MVT::i32)); 2010 // TODO: Should this propagate fast-math-flags? 2011 return DAG.getNode(ISD::FADD, SL, MVT::f64, LdExp, CvtLo); 2012 } 2013 2014 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op, 2015 SelectionDAG &DAG) const { 2016 assert(Op.getOperand(0).getValueType() == MVT::i64 && 2017 "operation should be legal"); 2018 2019 // TODO: Factor out code common with LowerSINT_TO_FP. 2020 2021 EVT DestVT = Op.getValueType(); 2022 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) { 2023 SDLoc DL(Op); 2024 SDValue Src = Op.getOperand(0); 2025 2026 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src); 2027 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op)); 2028 SDValue FPRound = 2029 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag); 2030 2031 return FPRound; 2032 } 2033 2034 if (DestVT == MVT::f32) 2035 return LowerINT_TO_FP32(Op, DAG, false); 2036 2037 assert(DestVT == MVT::f64); 2038 return LowerINT_TO_FP64(Op, DAG, false); 2039 } 2040 2041 SDValue AMDGPUTargetLowering::LowerSINT_TO_FP(SDValue Op, 2042 SelectionDAG &DAG) const { 2043 assert(Op.getOperand(0).getValueType() == MVT::i64 && 2044 "operation should be legal"); 2045 2046 // TODO: Factor out code common with LowerUINT_TO_FP. 2047 2048 EVT DestVT = Op.getValueType(); 2049 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) { 2050 SDLoc DL(Op); 2051 SDValue Src = Op.getOperand(0); 2052 2053 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src); 2054 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op)); 2055 SDValue FPRound = 2056 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag); 2057 2058 return FPRound; 2059 } 2060 2061 if (DestVT == MVT::f32) 2062 return LowerINT_TO_FP32(Op, DAG, true); 2063 2064 assert(DestVT == MVT::f64); 2065 return LowerINT_TO_FP64(Op, DAG, true); 2066 } 2067 2068 SDValue AMDGPUTargetLowering::LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG, 2069 bool Signed) const { 2070 SDLoc SL(Op); 2071 2072 SDValue Src = Op.getOperand(0); 2073 2074 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src); 2075 2076 SDValue K0 = DAG.getConstantFP(BitsToDouble(UINT64_C(0x3df0000000000000)), SL, 2077 MVT::f64); 2078 SDValue K1 = DAG.getConstantFP(BitsToDouble(UINT64_C(0xc1f0000000000000)), SL, 2079 MVT::f64); 2080 // TODO: Should this propagate fast-math-flags? 2081 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, Trunc, K0); 2082 2083 SDValue FloorMul = DAG.getNode(ISD::FFLOOR, SL, MVT::f64, Mul); 2084 2085 2086 SDValue Fma = DAG.getNode(ISD::FMA, SL, MVT::f64, FloorMul, K1, Trunc); 2087 2088 SDValue Hi = DAG.getNode(Signed ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, SL, 2089 MVT::i32, FloorMul); 2090 SDValue Lo = DAG.getNode(ISD::FP_TO_UINT, SL, MVT::i32, Fma); 2091 2092 SDValue Result = DAG.getBuildVector(MVT::v2i32, SL, {Lo, Hi}); 2093 2094 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Result); 2095 } 2096 2097 SDValue AMDGPUTargetLowering::LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const { 2098 2099 if (getTargetMachine().Options.UnsafeFPMath) { 2100 // There is a generic expand for FP_TO_FP16 with unsafe fast math. 2101 return SDValue(); 2102 } 2103 2104 SDLoc DL(Op); 2105 SDValue N0 = Op.getOperand(0); 2106 assert (N0.getSimpleValueType() == MVT::f64); 2107 2108 // f64 -> f16 conversion using round-to-nearest-even rounding mode. 2109 const unsigned ExpMask = 0x7ff; 2110 const unsigned ExpBiasf64 = 1023; 2111 const unsigned ExpBiasf16 = 15; 2112 SDValue Zero = DAG.getConstant(0, DL, MVT::i32); 2113 SDValue One = DAG.getConstant(1, DL, MVT::i32); 2114 SDValue U = DAG.getNode(ISD::BITCAST, DL, MVT::i64, N0); 2115 SDValue UH = DAG.getNode(ISD::SRL, DL, MVT::i64, U, 2116 DAG.getConstant(32, DL, MVT::i64)); 2117 UH = DAG.getZExtOrTrunc(UH, DL, MVT::i32); 2118 U = DAG.getZExtOrTrunc(U, DL, MVT::i32); 2119 SDValue E = DAG.getNode(ISD::SRL, DL, MVT::i32, UH, 2120 DAG.getConstant(20, DL, MVT::i64)); 2121 E = DAG.getNode(ISD::AND, DL, MVT::i32, E, 2122 DAG.getConstant(ExpMask, DL, MVT::i32)); 2123 // Subtract the fp64 exponent bias (1023) to get the real exponent and 2124 // add the f16 bias (15) to get the biased exponent for the f16 format. 2125 E = DAG.getNode(ISD::ADD, DL, MVT::i32, E, 2126 DAG.getConstant(-ExpBiasf64 + ExpBiasf16, DL, MVT::i32)); 2127 2128 SDValue M = DAG.getNode(ISD::SRL, DL, MVT::i32, UH, 2129 DAG.getConstant(8, DL, MVT::i32)); 2130 M = DAG.getNode(ISD::AND, DL, MVT::i32, M, 2131 DAG.getConstant(0xffe, DL, MVT::i32)); 2132 2133 SDValue MaskedSig = DAG.getNode(ISD::AND, DL, MVT::i32, UH, 2134 DAG.getConstant(0x1ff, DL, MVT::i32)); 2135 MaskedSig = DAG.getNode(ISD::OR, DL, MVT::i32, MaskedSig, U); 2136 2137 SDValue Lo40Set = DAG.getSelectCC(DL, MaskedSig, Zero, Zero, One, ISD::SETEQ); 2138 M = DAG.getNode(ISD::OR, DL, MVT::i32, M, Lo40Set); 2139 2140 // (M != 0 ? 0x0200 : 0) | 0x7c00; 2141 SDValue I = DAG.getNode(ISD::OR, DL, MVT::i32, 2142 DAG.getSelectCC(DL, M, Zero, DAG.getConstant(0x0200, DL, MVT::i32), 2143 Zero, ISD::SETNE), DAG.getConstant(0x7c00, DL, MVT::i32)); 2144 2145 // N = M | (E << 12); 2146 SDValue N = DAG.getNode(ISD::OR, DL, MVT::i32, M, 2147 DAG.getNode(ISD::SHL, DL, MVT::i32, E, 2148 DAG.getConstant(12, DL, MVT::i32))); 2149 2150 // B = clamp(1-E, 0, 13); 2151 SDValue OneSubExp = DAG.getNode(ISD::SUB, DL, MVT::i32, 2152 One, E); 2153 SDValue B = DAG.getNode(ISD::SMAX, DL, MVT::i32, OneSubExp, Zero); 2154 B = DAG.getNode(ISD::SMIN, DL, MVT::i32, B, 2155 DAG.getConstant(13, DL, MVT::i32)); 2156 2157 SDValue SigSetHigh = DAG.getNode(ISD::OR, DL, MVT::i32, M, 2158 DAG.getConstant(0x1000, DL, MVT::i32)); 2159 2160 SDValue D = DAG.getNode(ISD::SRL, DL, MVT::i32, SigSetHigh, B); 2161 SDValue D0 = DAG.getNode(ISD::SHL, DL, MVT::i32, D, B); 2162 SDValue D1 = DAG.getSelectCC(DL, D0, SigSetHigh, One, Zero, ISD::SETNE); 2163 D = DAG.getNode(ISD::OR, DL, MVT::i32, D, D1); 2164 2165 SDValue V = DAG.getSelectCC(DL, E, One, D, N, ISD::SETLT); 2166 SDValue VLow3 = DAG.getNode(ISD::AND, DL, MVT::i32, V, 2167 DAG.getConstant(0x7, DL, MVT::i32)); 2168 V = DAG.getNode(ISD::SRL, DL, MVT::i32, V, 2169 DAG.getConstant(2, DL, MVT::i32)); 2170 SDValue V0 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(3, DL, MVT::i32), 2171 One, Zero, ISD::SETEQ); 2172 SDValue V1 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(5, DL, MVT::i32), 2173 One, Zero, ISD::SETGT); 2174 V1 = DAG.getNode(ISD::OR, DL, MVT::i32, V0, V1); 2175 V = DAG.getNode(ISD::ADD, DL, MVT::i32, V, V1); 2176 2177 V = DAG.getSelectCC(DL, E, DAG.getConstant(30, DL, MVT::i32), 2178 DAG.getConstant(0x7c00, DL, MVT::i32), V, ISD::SETGT); 2179 V = DAG.getSelectCC(DL, E, DAG.getConstant(1039, DL, MVT::i32), 2180 I, V, ISD::SETEQ); 2181 2182 // Extract the sign bit. 2183 SDValue Sign = DAG.getNode(ISD::SRL, DL, MVT::i32, UH, 2184 DAG.getConstant(16, DL, MVT::i32)); 2185 Sign = DAG.getNode(ISD::AND, DL, MVT::i32, Sign, 2186 DAG.getConstant(0x8000, DL, MVT::i32)); 2187 2188 V = DAG.getNode(ISD::OR, DL, MVT::i32, Sign, V); 2189 return DAG.getZExtOrTrunc(V, DL, Op.getValueType()); 2190 } 2191 2192 SDValue AMDGPUTargetLowering::LowerFP_TO_SINT(SDValue Op, 2193 SelectionDAG &DAG) const { 2194 SDValue Src = Op.getOperand(0); 2195 2196 // TODO: Factor out code common with LowerFP_TO_UINT. 2197 2198 EVT SrcVT = Src.getValueType(); 2199 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) { 2200 SDLoc DL(Op); 2201 2202 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src); 2203 SDValue FpToInt32 = 2204 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend); 2205 2206 return FpToInt32; 2207 } 2208 2209 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64) 2210 return LowerFP64_TO_INT(Op, DAG, true); 2211 2212 return SDValue(); 2213 } 2214 2215 SDValue AMDGPUTargetLowering::LowerFP_TO_UINT(SDValue Op, 2216 SelectionDAG &DAG) const { 2217 SDValue Src = Op.getOperand(0); 2218 2219 // TODO: Factor out code common with LowerFP_TO_SINT. 2220 2221 EVT SrcVT = Src.getValueType(); 2222 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) { 2223 SDLoc DL(Op); 2224 2225 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src); 2226 SDValue FpToInt32 = 2227 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend); 2228 2229 return FpToInt32; 2230 } 2231 2232 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64) 2233 return LowerFP64_TO_INT(Op, DAG, false); 2234 2235 return SDValue(); 2236 } 2237 2238 SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, 2239 SelectionDAG &DAG) const { 2240 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 2241 MVT VT = Op.getSimpleValueType(); 2242 MVT ScalarVT = VT.getScalarType(); 2243 2244 assert(VT.isVector()); 2245 2246 SDValue Src = Op.getOperand(0); 2247 SDLoc DL(Op); 2248 2249 // TODO: Don't scalarize on Evergreen? 2250 unsigned NElts = VT.getVectorNumElements(); 2251 SmallVector<SDValue, 8> Args; 2252 DAG.ExtractVectorElements(Src, Args, 0, NElts); 2253 2254 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType()); 2255 for (unsigned I = 0; I < NElts; ++I) 2256 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp); 2257 2258 return DAG.getBuildVector(VT, DL, Args); 2259 } 2260 2261 //===----------------------------------------------------------------------===// 2262 // Custom DAG optimizations 2263 //===----------------------------------------------------------------------===// 2264 2265 static bool isU24(SDValue Op, SelectionDAG &DAG) { 2266 APInt KnownZero, KnownOne; 2267 EVT VT = Op.getValueType(); 2268 DAG.computeKnownBits(Op, KnownZero, KnownOne); 2269 2270 return (VT.getSizeInBits() - KnownZero.countLeadingOnes()) <= 24; 2271 } 2272 2273 static bool isI24(SDValue Op, SelectionDAG &DAG) { 2274 EVT VT = Op.getValueType(); 2275 2276 // In order for this to be a signed 24-bit value, bit 23, must 2277 // be a sign bit. 2278 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated 2279 // as unsigned 24-bit values. 2280 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24; 2281 } 2282 2283 static bool simplifyI24(SDNode *Node24, unsigned OpIdx, 2284 TargetLowering::DAGCombinerInfo &DCI) { 2285 2286 SelectionDAG &DAG = DCI.DAG; 2287 SDValue Op = Node24->getOperand(OpIdx); 2288 EVT VT = Op.getValueType(); 2289 2290 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24); 2291 APInt KnownZero, KnownOne; 2292 TargetLowering::TargetLoweringOpt TLO(DAG, true, true); 2293 if (TLO.SimplifyDemandedBits(Node24, OpIdx, Demanded, DCI)) 2294 return true; 2295 2296 return false; 2297 } 2298 2299 template <typename IntTy> 2300 static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0, uint32_t Offset, 2301 uint32_t Width, const SDLoc &DL) { 2302 if (Width + Offset < 32) { 2303 uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width); 2304 IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width); 2305 return DAG.getConstant(Result, DL, MVT::i32); 2306 } 2307 2308 return DAG.getConstant(Src0 >> Offset, DL, MVT::i32); 2309 } 2310 2311 static bool hasVolatileUser(SDNode *Val) { 2312 for (SDNode *U : Val->uses()) { 2313 if (MemSDNode *M = dyn_cast<MemSDNode>(U)) { 2314 if (M->isVolatile()) 2315 return true; 2316 } 2317 } 2318 2319 return false; 2320 } 2321 2322 bool AMDGPUTargetLowering::shouldCombineMemoryType(EVT VT) const { 2323 // i32 vectors are the canonical memory type. 2324 if (VT.getScalarType() == MVT::i32 || isTypeLegal(VT)) 2325 return false; 2326 2327 if (!VT.isByteSized()) 2328 return false; 2329 2330 unsigned Size = VT.getStoreSize(); 2331 2332 if ((Size == 1 || Size == 2 || Size == 4) && !VT.isVector()) 2333 return false; 2334 2335 if (Size == 3 || (Size > 4 && (Size % 4 != 0))) 2336 return false; 2337 2338 return true; 2339 } 2340 2341 // Replace load of an illegal type with a store of a bitcast to a friendlier 2342 // type. 2343 SDValue AMDGPUTargetLowering::performLoadCombine(SDNode *N, 2344 DAGCombinerInfo &DCI) const { 2345 if (!DCI.isBeforeLegalize()) 2346 return SDValue(); 2347 2348 LoadSDNode *LN = cast<LoadSDNode>(N); 2349 if (LN->isVolatile() || !ISD::isNormalLoad(LN) || hasVolatileUser(LN)) 2350 return SDValue(); 2351 2352 SDLoc SL(N); 2353 SelectionDAG &DAG = DCI.DAG; 2354 EVT VT = LN->getMemoryVT(); 2355 2356 unsigned Size = VT.getStoreSize(); 2357 unsigned Align = LN->getAlignment(); 2358 if (Align < Size && isTypeLegal(VT)) { 2359 bool IsFast; 2360 unsigned AS = LN->getAddressSpace(); 2361 2362 // Expand unaligned loads earlier than legalization. Due to visitation order 2363 // problems during legalization, the emitted instructions to pack and unpack 2364 // the bytes again are not eliminated in the case of an unaligned copy. 2365 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) { 2366 if (VT.isVector()) 2367 return scalarizeVectorLoad(LN, DAG); 2368 2369 SDValue Ops[2]; 2370 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(LN, DAG); 2371 return DAG.getMergeValues(Ops, SDLoc(N)); 2372 } 2373 2374 if (!IsFast) 2375 return SDValue(); 2376 } 2377 2378 if (!shouldCombineMemoryType(VT)) 2379 return SDValue(); 2380 2381 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT); 2382 2383 SDValue NewLoad 2384 = DAG.getLoad(NewVT, SL, LN->getChain(), 2385 LN->getBasePtr(), LN->getMemOperand()); 2386 2387 SDValue BC = DAG.getNode(ISD::BITCAST, SL, VT, NewLoad); 2388 DCI.CombineTo(N, BC, NewLoad.getValue(1)); 2389 return SDValue(N, 0); 2390 } 2391 2392 // Replace store of an illegal type with a store of a bitcast to a friendlier 2393 // type. 2394 SDValue AMDGPUTargetLowering::performStoreCombine(SDNode *N, 2395 DAGCombinerInfo &DCI) const { 2396 if (!DCI.isBeforeLegalize()) 2397 return SDValue(); 2398 2399 StoreSDNode *SN = cast<StoreSDNode>(N); 2400 if (SN->isVolatile() || !ISD::isNormalStore(SN)) 2401 return SDValue(); 2402 2403 EVT VT = SN->getMemoryVT(); 2404 unsigned Size = VT.getStoreSize(); 2405 2406 SDLoc SL(N); 2407 SelectionDAG &DAG = DCI.DAG; 2408 unsigned Align = SN->getAlignment(); 2409 if (Align < Size && isTypeLegal(VT)) { 2410 bool IsFast; 2411 unsigned AS = SN->getAddressSpace(); 2412 2413 // Expand unaligned stores earlier than legalization. Due to visitation 2414 // order problems during legalization, the emitted instructions to pack and 2415 // unpack the bytes again are not eliminated in the case of an unaligned 2416 // copy. 2417 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) { 2418 if (VT.isVector()) 2419 return scalarizeVectorStore(SN, DAG); 2420 2421 return expandUnalignedStore(SN, DAG); 2422 } 2423 2424 if (!IsFast) 2425 return SDValue(); 2426 } 2427 2428 if (!shouldCombineMemoryType(VT)) 2429 return SDValue(); 2430 2431 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT); 2432 SDValue Val = SN->getValue(); 2433 2434 //DCI.AddToWorklist(Val.getNode()); 2435 2436 bool OtherUses = !Val.hasOneUse(); 2437 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NewVT, Val); 2438 if (OtherUses) { 2439 SDValue CastBack = DAG.getNode(ISD::BITCAST, SL, VT, CastVal); 2440 DAG.ReplaceAllUsesOfValueWith(Val, CastBack); 2441 } 2442 2443 return DAG.getStore(SN->getChain(), SL, CastVal, 2444 SN->getBasePtr(), SN->getMemOperand()); 2445 } 2446 2447 /// Split the 64-bit value \p LHS into two 32-bit components, and perform the 2448 /// binary operation \p Opc to it with the corresponding constant operands. 2449 SDValue AMDGPUTargetLowering::splitBinaryBitConstantOpImpl( 2450 DAGCombinerInfo &DCI, const SDLoc &SL, 2451 unsigned Opc, SDValue LHS, 2452 uint32_t ValLo, uint32_t ValHi) const { 2453 SelectionDAG &DAG = DCI.DAG; 2454 SDValue Lo, Hi; 2455 std::tie(Lo, Hi) = split64BitValue(LHS, DAG); 2456 2457 SDValue LoRHS = DAG.getConstant(ValLo, SL, MVT::i32); 2458 SDValue HiRHS = DAG.getConstant(ValHi, SL, MVT::i32); 2459 2460 SDValue LoAnd = DAG.getNode(Opc, SL, MVT::i32, Lo, LoRHS); 2461 SDValue HiAnd = DAG.getNode(Opc, SL, MVT::i32, Hi, HiRHS); 2462 2463 // Re-visit the ands. It's possible we eliminated one of them and it could 2464 // simplify the vector. 2465 DCI.AddToWorklist(Lo.getNode()); 2466 DCI.AddToWorklist(Hi.getNode()); 2467 2468 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {LoAnd, HiAnd}); 2469 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 2470 } 2471 2472 SDValue AMDGPUTargetLowering::performShlCombine(SDNode *N, 2473 DAGCombinerInfo &DCI) const { 2474 if (N->getValueType(0) != MVT::i64) 2475 return SDValue(); 2476 2477 // i64 (shl x, C) -> (build_pair 0, (shl x, C -32)) 2478 2479 // On some subtargets, 64-bit shift is a quarter rate instruction. In the 2480 // common case, splitting this into a move and a 32-bit shift is faster and 2481 // the same code size. 2482 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2483 if (!RHS) 2484 return SDValue(); 2485 2486 unsigned RHSVal = RHS->getZExtValue(); 2487 if (RHSVal < 32) 2488 return SDValue(); 2489 2490 SDValue LHS = N->getOperand(0); 2491 2492 SDLoc SL(N); 2493 SelectionDAG &DAG = DCI.DAG; 2494 2495 SDValue ShiftAmt = DAG.getConstant(RHSVal - 32, SL, MVT::i32); 2496 2497 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS); 2498 SDValue NewShift = DAG.getNode(ISD::SHL, SL, MVT::i32, Lo, ShiftAmt); 2499 2500 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 2501 2502 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {Zero, NewShift}); 2503 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 2504 } 2505 2506 SDValue AMDGPUTargetLowering::performSraCombine(SDNode *N, 2507 DAGCombinerInfo &DCI) const { 2508 if (N->getValueType(0) != MVT::i64) 2509 return SDValue(); 2510 2511 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2512 if (!RHS) 2513 return SDValue(); 2514 2515 SelectionDAG &DAG = DCI.DAG; 2516 SDLoc SL(N); 2517 unsigned RHSVal = RHS->getZExtValue(); 2518 2519 // (sra i64:x, 32) -> build_pair x, (sra hi_32(x), 31) 2520 if (RHSVal == 32) { 2521 SDValue Hi = getHiHalf64(N->getOperand(0), DAG); 2522 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi, 2523 DAG.getConstant(31, SL, MVT::i32)); 2524 2525 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {Hi, NewShift}); 2526 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec); 2527 } 2528 2529 // (sra i64:x, 63) -> build_pair (sra hi_32(x), 31), (sra hi_32(x), 31) 2530 if (RHSVal == 63) { 2531 SDValue Hi = getHiHalf64(N->getOperand(0), DAG); 2532 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi, 2533 DAG.getConstant(31, SL, MVT::i32)); 2534 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, NewShift}); 2535 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec); 2536 } 2537 2538 return SDValue(); 2539 } 2540 2541 SDValue AMDGPUTargetLowering::performSrlCombine(SDNode *N, 2542 DAGCombinerInfo &DCI) const { 2543 if (N->getValueType(0) != MVT::i64) 2544 return SDValue(); 2545 2546 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2547 if (!RHS) 2548 return SDValue(); 2549 2550 unsigned ShiftAmt = RHS->getZExtValue(); 2551 if (ShiftAmt < 32) 2552 return SDValue(); 2553 2554 // srl i64:x, C for C >= 32 2555 // => 2556 // build_pair (srl hi_32(x), C - 32), 0 2557 2558 SelectionDAG &DAG = DCI.DAG; 2559 SDLoc SL(N); 2560 2561 SDValue One = DAG.getConstant(1, SL, MVT::i32); 2562 SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 2563 2564 SDValue VecOp = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, N->getOperand(0)); 2565 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, 2566 VecOp, One); 2567 2568 SDValue NewConst = DAG.getConstant(ShiftAmt - 32, SL, MVT::i32); 2569 SDValue NewShift = DAG.getNode(ISD::SRL, SL, MVT::i32, Hi, NewConst); 2570 2571 SDValue BuildPair = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, Zero}); 2572 2573 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildPair); 2574 } 2575 2576 // We need to specifically handle i64 mul here to avoid unnecessary conversion 2577 // instructions. If we only match on the legalized i64 mul expansion, 2578 // SimplifyDemandedBits will be unable to remove them because there will be 2579 // multiple uses due to the separate mul + mulh[su]. 2580 static SDValue getMul24(SelectionDAG &DAG, const SDLoc &SL, 2581 SDValue N0, SDValue N1, unsigned Size, bool Signed) { 2582 if (Size <= 32) { 2583 unsigned MulOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24; 2584 return DAG.getNode(MulOpc, SL, MVT::i32, N0, N1); 2585 } 2586 2587 // Because we want to eliminate extension instructions before the 2588 // operation, we need to create a single user here (i.e. not the separate 2589 // mul_lo + mul_hi) so that SimplifyDemandedBits will deal with it. 2590 2591 unsigned MulOpc = Signed ? AMDGPUISD::MUL_LOHI_I24 : AMDGPUISD::MUL_LOHI_U24; 2592 2593 SDValue Mul = DAG.getNode(MulOpc, SL, 2594 DAG.getVTList(MVT::i32, MVT::i32), N0, N1); 2595 2596 return DAG.getNode(ISD::BUILD_PAIR, SL, MVT::i64, 2597 Mul.getValue(0), Mul.getValue(1)); 2598 } 2599 2600 SDValue AMDGPUTargetLowering::performMulCombine(SDNode *N, 2601 DAGCombinerInfo &DCI) const { 2602 EVT VT = N->getValueType(0); 2603 2604 unsigned Size = VT.getSizeInBits(); 2605 if (VT.isVector() || Size > 64) 2606 return SDValue(); 2607 2608 // There are i16 integer mul/mad. 2609 if (Subtarget->has16BitInsts() && VT.getScalarType().bitsLE(MVT::i16)) 2610 return SDValue(); 2611 2612 SelectionDAG &DAG = DCI.DAG; 2613 SDLoc DL(N); 2614 2615 SDValue N0 = N->getOperand(0); 2616 SDValue N1 = N->getOperand(1); 2617 SDValue Mul; 2618 2619 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) { 2620 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32); 2621 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32); 2622 Mul = getMul24(DAG, DL, N0, N1, Size, false); 2623 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) { 2624 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32); 2625 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32); 2626 Mul = getMul24(DAG, DL, N0, N1, Size, true); 2627 } else { 2628 return SDValue(); 2629 } 2630 2631 // We need to use sext even for MUL_U24, because MUL_U24 is used 2632 // for signed multiply of 8 and 16-bit types. 2633 return DAG.getSExtOrTrunc(Mul, DL, VT); 2634 } 2635 2636 SDValue AMDGPUTargetLowering::performMulhsCombine(SDNode *N, 2637 DAGCombinerInfo &DCI) const { 2638 EVT VT = N->getValueType(0); 2639 2640 if (!Subtarget->hasMulI24() || VT.isVector()) 2641 return SDValue(); 2642 2643 SelectionDAG &DAG = DCI.DAG; 2644 SDLoc DL(N); 2645 2646 SDValue N0 = N->getOperand(0); 2647 SDValue N1 = N->getOperand(1); 2648 2649 if (!isI24(N0, DAG) || !isI24(N1, DAG)) 2650 return SDValue(); 2651 2652 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32); 2653 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32); 2654 2655 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_I24, DL, MVT::i32, N0, N1); 2656 DCI.AddToWorklist(Mulhi.getNode()); 2657 return DAG.getSExtOrTrunc(Mulhi, DL, VT); 2658 } 2659 2660 SDValue AMDGPUTargetLowering::performMulhuCombine(SDNode *N, 2661 DAGCombinerInfo &DCI) const { 2662 EVT VT = N->getValueType(0); 2663 2664 if (!Subtarget->hasMulU24() || VT.isVector() || VT.getSizeInBits() > 32) 2665 return SDValue(); 2666 2667 SelectionDAG &DAG = DCI.DAG; 2668 SDLoc DL(N); 2669 2670 SDValue N0 = N->getOperand(0); 2671 SDValue N1 = N->getOperand(1); 2672 2673 if (!isU24(N0, DAG) || !isU24(N1, DAG)) 2674 return SDValue(); 2675 2676 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32); 2677 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32); 2678 2679 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_U24, DL, MVT::i32, N0, N1); 2680 DCI.AddToWorklist(Mulhi.getNode()); 2681 return DAG.getZExtOrTrunc(Mulhi, DL, VT); 2682 } 2683 2684 SDValue AMDGPUTargetLowering::performMulLoHi24Combine( 2685 SDNode *N, DAGCombinerInfo &DCI) const { 2686 SelectionDAG &DAG = DCI.DAG; 2687 2688 // Simplify demanded bits before splitting into multiple users. 2689 if (simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI)) 2690 return SDValue(); 2691 2692 SDValue N0 = N->getOperand(0); 2693 SDValue N1 = N->getOperand(1); 2694 2695 bool Signed = (N->getOpcode() == AMDGPUISD::MUL_LOHI_I24); 2696 2697 unsigned MulLoOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24; 2698 unsigned MulHiOpc = Signed ? AMDGPUISD::MULHI_I24 : AMDGPUISD::MULHI_U24; 2699 2700 SDLoc SL(N); 2701 2702 SDValue MulLo = DAG.getNode(MulLoOpc, SL, MVT::i32, N0, N1); 2703 SDValue MulHi = DAG.getNode(MulHiOpc, SL, MVT::i32, N0, N1); 2704 return DAG.getMergeValues({ MulLo, MulHi }, SL); 2705 } 2706 2707 static bool isNegativeOne(SDValue Val) { 2708 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) 2709 return C->isAllOnesValue(); 2710 return false; 2711 } 2712 2713 static bool isCtlzOpc(unsigned Opc) { 2714 return Opc == ISD::CTLZ || Opc == ISD::CTLZ_ZERO_UNDEF; 2715 } 2716 2717 SDValue AMDGPUTargetLowering::getFFBH_U32(SelectionDAG &DAG, 2718 SDValue Op, 2719 const SDLoc &DL) const { 2720 EVT VT = Op.getValueType(); 2721 EVT LegalVT = getTypeToTransformTo(*DAG.getContext(), VT); 2722 if (LegalVT != MVT::i32 && (Subtarget->has16BitInsts() && 2723 LegalVT != MVT::i16)) 2724 return SDValue(); 2725 2726 if (VT != MVT::i32) 2727 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Op); 2728 2729 SDValue FFBH = DAG.getNode(AMDGPUISD::FFBH_U32, DL, MVT::i32, Op); 2730 if (VT != MVT::i32) 2731 FFBH = DAG.getNode(ISD::TRUNCATE, DL, VT, FFBH); 2732 2733 return FFBH; 2734 } 2735 2736 // The native instructions return -1 on 0 input. Optimize out a select that 2737 // produces -1 on 0. 2738 // 2739 // TODO: If zero is not undef, we could also do this if the output is compared 2740 // against the bitwidth. 2741 // 2742 // TODO: Should probably combine against FFBH_U32 instead of ctlz directly. 2743 SDValue AMDGPUTargetLowering::performCtlzCombine(const SDLoc &SL, SDValue Cond, 2744 SDValue LHS, SDValue RHS, 2745 DAGCombinerInfo &DCI) const { 2746 ConstantSDNode *CmpRhs = dyn_cast<ConstantSDNode>(Cond.getOperand(1)); 2747 if (!CmpRhs || !CmpRhs->isNullValue()) 2748 return SDValue(); 2749 2750 SelectionDAG &DAG = DCI.DAG; 2751 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); 2752 SDValue CmpLHS = Cond.getOperand(0); 2753 2754 // select (setcc x, 0, eq), -1, (ctlz_zero_undef x) -> ffbh_u32 x 2755 if (CCOpcode == ISD::SETEQ && 2756 isCtlzOpc(RHS.getOpcode()) && 2757 RHS.getOperand(0) == CmpLHS && 2758 isNegativeOne(LHS)) { 2759 return getFFBH_U32(DAG, CmpLHS, SL); 2760 } 2761 2762 // select (setcc x, 0, ne), (ctlz_zero_undef x), -1 -> ffbh_u32 x 2763 if (CCOpcode == ISD::SETNE && 2764 isCtlzOpc(LHS.getOpcode()) && 2765 LHS.getOperand(0) == CmpLHS && 2766 isNegativeOne(RHS)) { 2767 return getFFBH_U32(DAG, CmpLHS, SL); 2768 } 2769 2770 return SDValue(); 2771 } 2772 2773 static SDValue distributeOpThroughSelect(TargetLowering::DAGCombinerInfo &DCI, 2774 unsigned Op, 2775 const SDLoc &SL, 2776 SDValue Cond, 2777 SDValue N1, 2778 SDValue N2) { 2779 SelectionDAG &DAG = DCI.DAG; 2780 EVT VT = N1.getValueType(); 2781 2782 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT, Cond, 2783 N1.getOperand(0), N2.getOperand(0)); 2784 DCI.AddToWorklist(NewSelect.getNode()); 2785 return DAG.getNode(Op, SL, VT, NewSelect); 2786 } 2787 2788 // Pull a free FP operation out of a select so it may fold into uses. 2789 // 2790 // select c, (fneg x), (fneg y) -> fneg (select c, x, y) 2791 // select c, (fneg x), k -> fneg (select c, x, (fneg k)) 2792 // 2793 // select c, (fabs x), (fabs y) -> fabs (select c, x, y) 2794 // select c, (fabs x), +k -> fabs (select c, x, k) 2795 static SDValue foldFreeOpFromSelect(TargetLowering::DAGCombinerInfo &DCI, 2796 SDValue N) { 2797 SelectionDAG &DAG = DCI.DAG; 2798 SDValue Cond = N.getOperand(0); 2799 SDValue LHS = N.getOperand(1); 2800 SDValue RHS = N.getOperand(2); 2801 2802 EVT VT = N.getValueType(); 2803 if ((LHS.getOpcode() == ISD::FABS && RHS.getOpcode() == ISD::FABS) || 2804 (LHS.getOpcode() == ISD::FNEG && RHS.getOpcode() == ISD::FNEG)) { 2805 return distributeOpThroughSelect(DCI, LHS.getOpcode(), 2806 SDLoc(N), Cond, LHS, RHS); 2807 } 2808 2809 bool Inv = false; 2810 if (RHS.getOpcode() == ISD::FABS || RHS.getOpcode() == ISD::FNEG) { 2811 std::swap(LHS, RHS); 2812 Inv = true; 2813 } 2814 2815 // TODO: Support vector constants. 2816 ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS); 2817 if ((LHS.getOpcode() == ISD::FNEG || LHS.getOpcode() == ISD::FABS) && CRHS) { 2818 SDLoc SL(N); 2819 // If one side is an fneg/fabs and the other is a constant, we can push the 2820 // fneg/fabs down. If it's an fabs, the constant needs to be non-negative. 2821 SDValue NewLHS = LHS.getOperand(0); 2822 SDValue NewRHS = RHS; 2823 2824 // Careful: if the neg can be folded up, don't try to pull it back down. 2825 bool ShouldFoldNeg = true; 2826 2827 if (NewLHS.hasOneUse()) { 2828 unsigned Opc = NewLHS.getOpcode(); 2829 if (LHS.getOpcode() == ISD::FNEG && fnegFoldsIntoOp(Opc)) 2830 ShouldFoldNeg = false; 2831 if (LHS.getOpcode() == ISD::FABS && Opc == ISD::FMUL) 2832 ShouldFoldNeg = false; 2833 } 2834 2835 if (ShouldFoldNeg) { 2836 if (LHS.getOpcode() == ISD::FNEG) 2837 NewRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 2838 else if (CRHS->isNegative()) 2839 return SDValue(); 2840 2841 if (Inv) 2842 std::swap(NewLHS, NewRHS); 2843 2844 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT, 2845 Cond, NewLHS, NewRHS); 2846 DCI.AddToWorklist(NewSelect.getNode()); 2847 return DAG.getNode(LHS.getOpcode(), SL, VT, NewSelect); 2848 } 2849 } 2850 2851 return SDValue(); 2852 } 2853 2854 2855 SDValue AMDGPUTargetLowering::performSelectCombine(SDNode *N, 2856 DAGCombinerInfo &DCI) const { 2857 if (SDValue Folded = foldFreeOpFromSelect(DCI, SDValue(N, 0))) 2858 return Folded; 2859 2860 SDValue Cond = N->getOperand(0); 2861 if (Cond.getOpcode() != ISD::SETCC) 2862 return SDValue(); 2863 2864 EVT VT = N->getValueType(0); 2865 SDValue LHS = Cond.getOperand(0); 2866 SDValue RHS = Cond.getOperand(1); 2867 SDValue CC = Cond.getOperand(2); 2868 2869 SDValue True = N->getOperand(1); 2870 SDValue False = N->getOperand(2); 2871 2872 if (Cond.hasOneUse()) { // TODO: Look for multiple select uses. 2873 SelectionDAG &DAG = DCI.DAG; 2874 if ((DAG.isConstantValueOfAnyType(True) || 2875 DAG.isConstantValueOfAnyType(True)) && 2876 (!DAG.isConstantValueOfAnyType(False) && 2877 !DAG.isConstantValueOfAnyType(False))) { 2878 // Swap cmp + select pair to move constant to false input. 2879 // This will allow using VOPC cndmasks more often. 2880 // select (setcc x, y), k, x -> select (setcc y, x) x, x 2881 2882 SDLoc SL(N); 2883 ISD::CondCode NewCC = getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), 2884 LHS.getValueType().isInteger()); 2885 2886 SDValue NewCond = DAG.getSetCC(SL, Cond.getValueType(), LHS, RHS, NewCC); 2887 return DAG.getNode(ISD::SELECT, SL, VT, NewCond, False, True); 2888 } 2889 2890 if (VT == MVT::f32 && Subtarget->hasFminFmaxLegacy()) { 2891 SDValue MinMax 2892 = combineFMinMaxLegacy(SDLoc(N), VT, LHS, RHS, True, False, CC, DCI); 2893 // Revisit this node so we can catch min3/max3/med3 patterns. 2894 //DCI.AddToWorklist(MinMax.getNode()); 2895 return MinMax; 2896 } 2897 } 2898 2899 // There's no reason to not do this if the condition has other uses. 2900 return performCtlzCombine(SDLoc(N), Cond, True, False, DCI); 2901 } 2902 2903 static bool isConstantFPZero(SDValue N) { 2904 if (const ConstantFPSDNode *C = isConstOrConstSplatFP(N)) 2905 return C->isZero() && !C->isNegative(); 2906 return false; 2907 } 2908 2909 static unsigned inverseMinMax(unsigned Opc) { 2910 switch (Opc) { 2911 case ISD::FMAXNUM: 2912 return ISD::FMINNUM; 2913 case ISD::FMINNUM: 2914 return ISD::FMAXNUM; 2915 case AMDGPUISD::FMAX_LEGACY: 2916 return AMDGPUISD::FMIN_LEGACY; 2917 case AMDGPUISD::FMIN_LEGACY: 2918 return AMDGPUISD::FMAX_LEGACY; 2919 default: 2920 llvm_unreachable("invalid min/max opcode"); 2921 } 2922 } 2923 2924 SDValue AMDGPUTargetLowering::performFNegCombine(SDNode *N, 2925 DAGCombinerInfo &DCI) const { 2926 SelectionDAG &DAG = DCI.DAG; 2927 SDValue N0 = N->getOperand(0); 2928 EVT VT = N->getValueType(0); 2929 2930 unsigned Opc = N0.getOpcode(); 2931 2932 // If the input has multiple uses and we can either fold the negate down, or 2933 // the other uses cannot, give up. This both prevents unprofitable 2934 // transformations and infinite loops: we won't repeatedly try to fold around 2935 // a negate that has no 'good' form. 2936 if (N0.hasOneUse()) { 2937 // This may be able to fold into the source, but at a code size cost. Don't 2938 // fold if the fold into the user is free. 2939 if (allUsesHaveSourceMods(N, 0)) 2940 return SDValue(); 2941 } else { 2942 if (fnegFoldsIntoOp(Opc) && 2943 (allUsesHaveSourceMods(N) || !allUsesHaveSourceMods(N0.getNode()))) 2944 return SDValue(); 2945 } 2946 2947 SDLoc SL(N); 2948 switch (Opc) { 2949 case ISD::FADD: { 2950 if (!mayIgnoreSignedZero(N0)) 2951 return SDValue(); 2952 2953 // (fneg (fadd x, y)) -> (fadd (fneg x), (fneg y)) 2954 SDValue LHS = N0.getOperand(0); 2955 SDValue RHS = N0.getOperand(1); 2956 2957 if (LHS.getOpcode() != ISD::FNEG) 2958 LHS = DAG.getNode(ISD::FNEG, SL, VT, LHS); 2959 else 2960 LHS = LHS.getOperand(0); 2961 2962 if (RHS.getOpcode() != ISD::FNEG) 2963 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 2964 else 2965 RHS = RHS.getOperand(0); 2966 2967 SDValue Res = DAG.getNode(ISD::FADD, SL, VT, LHS, RHS, N0->getFlags()); 2968 if (!N0.hasOneUse()) 2969 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 2970 return Res; 2971 } 2972 case ISD::FMUL: 2973 case AMDGPUISD::FMUL_LEGACY: { 2974 // (fneg (fmul x, y)) -> (fmul x, (fneg y)) 2975 // (fneg (fmul_legacy x, y)) -> (fmul_legacy x, (fneg y)) 2976 SDValue LHS = N0.getOperand(0); 2977 SDValue RHS = N0.getOperand(1); 2978 2979 if (LHS.getOpcode() == ISD::FNEG) 2980 LHS = LHS.getOperand(0); 2981 else if (RHS.getOpcode() == ISD::FNEG) 2982 RHS = RHS.getOperand(0); 2983 else 2984 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 2985 2986 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, RHS, N0->getFlags()); 2987 if (!N0.hasOneUse()) 2988 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 2989 return Res; 2990 } 2991 case ISD::FMA: 2992 case ISD::FMAD: { 2993 if (!mayIgnoreSignedZero(N0)) 2994 return SDValue(); 2995 2996 // (fneg (fma x, y, z)) -> (fma x, (fneg y), (fneg z)) 2997 SDValue LHS = N0.getOperand(0); 2998 SDValue MHS = N0.getOperand(1); 2999 SDValue RHS = N0.getOperand(2); 3000 3001 if (LHS.getOpcode() == ISD::FNEG) 3002 LHS = LHS.getOperand(0); 3003 else if (MHS.getOpcode() == ISD::FNEG) 3004 MHS = MHS.getOperand(0); 3005 else 3006 MHS = DAG.getNode(ISD::FNEG, SL, VT, MHS); 3007 3008 if (RHS.getOpcode() != ISD::FNEG) 3009 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3010 else 3011 RHS = RHS.getOperand(0); 3012 3013 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, MHS, RHS); 3014 if (!N0.hasOneUse()) 3015 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 3016 return Res; 3017 } 3018 case ISD::FMAXNUM: 3019 case ISD::FMINNUM: 3020 case AMDGPUISD::FMAX_LEGACY: 3021 case AMDGPUISD::FMIN_LEGACY: { 3022 // fneg (fmaxnum x, y) -> fminnum (fneg x), (fneg y) 3023 // fneg (fminnum x, y) -> fmaxnum (fneg x), (fneg y) 3024 // fneg (fmax_legacy x, y) -> fmin_legacy (fneg x), (fneg y) 3025 // fneg (fmin_legacy x, y) -> fmax_legacy (fneg x), (fneg y) 3026 3027 SDValue LHS = N0.getOperand(0); 3028 SDValue RHS = N0.getOperand(1); 3029 3030 // 0 doesn't have a negated inline immediate. 3031 // TODO: Shouldn't fold 1/2pi either, and should be generalized to other 3032 // operations. 3033 if (isConstantFPZero(RHS)) 3034 return SDValue(); 3035 3036 SDValue NegLHS = DAG.getNode(ISD::FNEG, SL, VT, LHS); 3037 SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3038 unsigned Opposite = inverseMinMax(Opc); 3039 3040 SDValue Res = DAG.getNode(Opposite, SL, VT, NegLHS, NegRHS, N0->getFlags()); 3041 if (!N0.hasOneUse()) 3042 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 3043 return Res; 3044 } 3045 case ISD::FP_EXTEND: 3046 case ISD::FTRUNC: 3047 case ISD::FRINT: 3048 case ISD::FNEARBYINT: // XXX - Should fround be handled? 3049 case ISD::FSIN: 3050 case AMDGPUISD::RCP: 3051 case AMDGPUISD::RCP_LEGACY: 3052 case AMDGPUISD::SIN_HW: { 3053 SDValue CvtSrc = N0.getOperand(0); 3054 if (CvtSrc.getOpcode() == ISD::FNEG) { 3055 // (fneg (fp_extend (fneg x))) -> (fp_extend x) 3056 // (fneg (rcp (fneg x))) -> (rcp x) 3057 return DAG.getNode(Opc, SL, VT, CvtSrc.getOperand(0)); 3058 } 3059 3060 if (!N0.hasOneUse()) 3061 return SDValue(); 3062 3063 // (fneg (fp_extend x)) -> (fp_extend (fneg x)) 3064 // (fneg (rcp x)) -> (rcp (fneg x)) 3065 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc); 3066 return DAG.getNode(Opc, SL, VT, Neg, N0->getFlags()); 3067 } 3068 case ISD::FP_ROUND: { 3069 SDValue CvtSrc = N0.getOperand(0); 3070 3071 if (CvtSrc.getOpcode() == ISD::FNEG) { 3072 // (fneg (fp_round (fneg x))) -> (fp_round x) 3073 return DAG.getNode(ISD::FP_ROUND, SL, VT, 3074 CvtSrc.getOperand(0), N0.getOperand(1)); 3075 } 3076 3077 if (!N0.hasOneUse()) 3078 return SDValue(); 3079 3080 // (fneg (fp_round x)) -> (fp_round (fneg x)) 3081 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc); 3082 return DAG.getNode(ISD::FP_ROUND, SL, VT, Neg, N0.getOperand(1)); 3083 } 3084 case ISD::FP16_TO_FP: { 3085 // v_cvt_f32_f16 supports source modifiers on pre-VI targets without legal 3086 // f16, but legalization of f16 fneg ends up pulling it out of the source. 3087 // Put the fneg back as a legal source operation that can be matched later. 3088 SDLoc SL(N); 3089 3090 SDValue Src = N0.getOperand(0); 3091 EVT SrcVT = Src.getValueType(); 3092 3093 // fneg (fp16_to_fp x) -> fp16_to_fp (xor x, 0x8000) 3094 SDValue IntFNeg = DAG.getNode(ISD::XOR, SL, SrcVT, Src, 3095 DAG.getConstant(0x8000, SL, SrcVT)); 3096 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFNeg); 3097 } 3098 default: 3099 return SDValue(); 3100 } 3101 } 3102 3103 SDValue AMDGPUTargetLowering::performFAbsCombine(SDNode *N, 3104 DAGCombinerInfo &DCI) const { 3105 SelectionDAG &DAG = DCI.DAG; 3106 SDValue N0 = N->getOperand(0); 3107 3108 if (!N0.hasOneUse()) 3109 return SDValue(); 3110 3111 switch (N0.getOpcode()) { 3112 case ISD::FP16_TO_FP: { 3113 assert(!Subtarget->has16BitInsts() && "should only see if f16 is illegal"); 3114 SDLoc SL(N); 3115 SDValue Src = N0.getOperand(0); 3116 EVT SrcVT = Src.getValueType(); 3117 3118 // fabs (fp16_to_fp x) -> fp16_to_fp (and x, 0x7fff) 3119 SDValue IntFAbs = DAG.getNode(ISD::AND, SL, SrcVT, Src, 3120 DAG.getConstant(0x7fff, SL, SrcVT)); 3121 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFAbs); 3122 } 3123 default: 3124 return SDValue(); 3125 } 3126 } 3127 3128 SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N, 3129 DAGCombinerInfo &DCI) const { 3130 SelectionDAG &DAG = DCI.DAG; 3131 SDLoc DL(N); 3132 3133 switch(N->getOpcode()) { 3134 default: 3135 break; 3136 case ISD::BITCAST: { 3137 EVT DestVT = N->getValueType(0); 3138 3139 // Push casts through vector builds. This helps avoid emitting a large 3140 // number of copies when materializing floating point vector constants. 3141 // 3142 // vNt1 bitcast (vNt0 (build_vector t0:x, t0:y)) => 3143 // vnt1 = build_vector (t1 (bitcast t0:x)), (t1 (bitcast t0:y)) 3144 if (DestVT.isVector()) { 3145 SDValue Src = N->getOperand(0); 3146 if (Src.getOpcode() == ISD::BUILD_VECTOR) { 3147 EVT SrcVT = Src.getValueType(); 3148 unsigned NElts = DestVT.getVectorNumElements(); 3149 3150 if (SrcVT.getVectorNumElements() == NElts) { 3151 EVT DestEltVT = DestVT.getVectorElementType(); 3152 3153 SmallVector<SDValue, 8> CastedElts; 3154 SDLoc SL(N); 3155 for (unsigned I = 0, E = SrcVT.getVectorNumElements(); I != E; ++I) { 3156 SDValue Elt = Src.getOperand(I); 3157 CastedElts.push_back(DAG.getNode(ISD::BITCAST, DL, DestEltVT, Elt)); 3158 } 3159 3160 return DAG.getBuildVector(DestVT, SL, CastedElts); 3161 } 3162 } 3163 } 3164 3165 if (DestVT.getSizeInBits() != 64 && !DestVT.isVector()) 3166 break; 3167 3168 // Fold bitcasts of constants. 3169 // 3170 // v2i32 (bitcast i64:k) -> build_vector lo_32(k), hi_32(k) 3171 // TODO: Generalize and move to DAGCombiner 3172 SDValue Src = N->getOperand(0); 3173 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src)) { 3174 assert(Src.getValueType() == MVT::i64); 3175 SDLoc SL(N); 3176 uint64_t CVal = C->getZExtValue(); 3177 return DAG.getNode(ISD::BUILD_VECTOR, SL, DestVT, 3178 DAG.getConstant(Lo_32(CVal), SL, MVT::i32), 3179 DAG.getConstant(Hi_32(CVal), SL, MVT::i32)); 3180 } 3181 3182 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Src)) { 3183 const APInt &Val = C->getValueAPF().bitcastToAPInt(); 3184 SDLoc SL(N); 3185 uint64_t CVal = Val.getZExtValue(); 3186 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, 3187 DAG.getConstant(Lo_32(CVal), SL, MVT::i32), 3188 DAG.getConstant(Hi_32(CVal), SL, MVT::i32)); 3189 3190 return DAG.getNode(ISD::BITCAST, SL, DestVT, Vec); 3191 } 3192 3193 break; 3194 } 3195 case ISD::SHL: { 3196 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 3197 break; 3198 3199 return performShlCombine(N, DCI); 3200 } 3201 case ISD::SRL: { 3202 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 3203 break; 3204 3205 return performSrlCombine(N, DCI); 3206 } 3207 case ISD::SRA: { 3208 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 3209 break; 3210 3211 return performSraCombine(N, DCI); 3212 } 3213 case ISD::MUL: 3214 return performMulCombine(N, DCI); 3215 case ISD::MULHS: 3216 return performMulhsCombine(N, DCI); 3217 case ISD::MULHU: 3218 return performMulhuCombine(N, DCI); 3219 case AMDGPUISD::MUL_I24: 3220 case AMDGPUISD::MUL_U24: 3221 case AMDGPUISD::MULHI_I24: 3222 case AMDGPUISD::MULHI_U24: { 3223 // If the first call to simplify is successfull, then N may end up being 3224 // deleted, so we shouldn't call simplifyI24 again. 3225 simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI); 3226 return SDValue(); 3227 } 3228 case AMDGPUISD::MUL_LOHI_I24: 3229 case AMDGPUISD::MUL_LOHI_U24: 3230 return performMulLoHi24Combine(N, DCI); 3231 case ISD::SELECT: 3232 return performSelectCombine(N, DCI); 3233 case ISD::FNEG: 3234 return performFNegCombine(N, DCI); 3235 case ISD::FABS: 3236 return performFAbsCombine(N, DCI); 3237 case AMDGPUISD::BFE_I32: 3238 case AMDGPUISD::BFE_U32: { 3239 assert(!N->getValueType(0).isVector() && 3240 "Vector handling of BFE not implemented"); 3241 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2)); 3242 if (!Width) 3243 break; 3244 3245 uint32_t WidthVal = Width->getZExtValue() & 0x1f; 3246 if (WidthVal == 0) 3247 return DAG.getConstant(0, DL, MVT::i32); 3248 3249 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); 3250 if (!Offset) 3251 break; 3252 3253 SDValue BitsFrom = N->getOperand(0); 3254 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f; 3255 3256 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32; 3257 3258 if (OffsetVal == 0) { 3259 // This is already sign / zero extended, so try to fold away extra BFEs. 3260 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal); 3261 3262 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom); 3263 if (OpSignBits >= SignBits) 3264 return BitsFrom; 3265 3266 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal); 3267 if (Signed) { 3268 // This is a sign_extend_inreg. Replace it to take advantage of existing 3269 // DAG Combines. If not eliminated, we will match back to BFE during 3270 // selection. 3271 3272 // TODO: The sext_inreg of extended types ends, although we can could 3273 // handle them in a single BFE. 3274 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom, 3275 DAG.getValueType(SmallVT)); 3276 } 3277 3278 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT); 3279 } 3280 3281 if (ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(BitsFrom)) { 3282 if (Signed) { 3283 return constantFoldBFE<int32_t>(DAG, 3284 CVal->getSExtValue(), 3285 OffsetVal, 3286 WidthVal, 3287 DL); 3288 } 3289 3290 return constantFoldBFE<uint32_t>(DAG, 3291 CVal->getZExtValue(), 3292 OffsetVal, 3293 WidthVal, 3294 DL); 3295 } 3296 3297 if ((OffsetVal + WidthVal) >= 32) { 3298 SDValue ShiftVal = DAG.getConstant(OffsetVal, DL, MVT::i32); 3299 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32, 3300 BitsFrom, ShiftVal); 3301 } 3302 3303 if (BitsFrom.hasOneUse()) { 3304 APInt Demanded = APInt::getBitsSet(32, 3305 OffsetVal, 3306 OffsetVal + WidthVal); 3307 3308 APInt KnownZero, KnownOne; 3309 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), 3310 !DCI.isBeforeLegalizeOps()); 3311 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3312 if (TLO.ShrinkDemandedConstant(BitsFrom, Demanded) || 3313 TLI.SimplifyDemandedBits(BitsFrom, Demanded, 3314 KnownZero, KnownOne, TLO)) { 3315 DCI.CommitTargetLoweringOpt(TLO); 3316 } 3317 } 3318 3319 break; 3320 } 3321 case ISD::LOAD: 3322 return performLoadCombine(N, DCI); 3323 case ISD::STORE: 3324 return performStoreCombine(N, DCI); 3325 } 3326 return SDValue(); 3327 } 3328 3329 //===----------------------------------------------------------------------===// 3330 // Helper functions 3331 //===----------------------------------------------------------------------===// 3332 3333 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG, 3334 const TargetRegisterClass *RC, 3335 unsigned Reg, EVT VT) const { 3336 MachineFunction &MF = DAG.getMachineFunction(); 3337 MachineRegisterInfo &MRI = MF.getRegInfo(); 3338 unsigned VirtualRegister; 3339 if (!MRI.isLiveIn(Reg)) { 3340 VirtualRegister = MRI.createVirtualRegister(RC); 3341 MRI.addLiveIn(Reg, VirtualRegister); 3342 } else { 3343 VirtualRegister = MRI.getLiveInVirtReg(Reg); 3344 } 3345 return DAG.getRegister(VirtualRegister, VT); 3346 } 3347 3348 uint32_t AMDGPUTargetLowering::getImplicitParameterOffset( 3349 const AMDGPUMachineFunction *MFI, const ImplicitParameter Param) const { 3350 unsigned Alignment = Subtarget->getAlignmentForImplicitArgPtr(); 3351 uint64_t ArgOffset = alignTo(MFI->getABIArgOffset(), Alignment); 3352 switch (Param) { 3353 case GRID_DIM: 3354 return ArgOffset; 3355 case GRID_OFFSET: 3356 return ArgOffset + 4; 3357 } 3358 llvm_unreachable("unexpected implicit parameter type"); 3359 } 3360 3361 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node; 3362 3363 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const { 3364 switch ((AMDGPUISD::NodeType)Opcode) { 3365 case AMDGPUISD::FIRST_NUMBER: break; 3366 // AMDIL DAG nodes 3367 NODE_NAME_CASE(CALL); 3368 NODE_NAME_CASE(UMUL); 3369 NODE_NAME_CASE(BRANCH_COND); 3370 3371 // AMDGPU DAG nodes 3372 NODE_NAME_CASE(ENDPGM) 3373 NODE_NAME_CASE(RETURN) 3374 NODE_NAME_CASE(DWORDADDR) 3375 NODE_NAME_CASE(FRACT) 3376 NODE_NAME_CASE(SETCC) 3377 NODE_NAME_CASE(SETREG) 3378 NODE_NAME_CASE(FMA_W_CHAIN) 3379 NODE_NAME_CASE(FMUL_W_CHAIN) 3380 NODE_NAME_CASE(CLAMP) 3381 NODE_NAME_CASE(COS_HW) 3382 NODE_NAME_CASE(SIN_HW) 3383 NODE_NAME_CASE(FMAX_LEGACY) 3384 NODE_NAME_CASE(FMIN_LEGACY) 3385 NODE_NAME_CASE(FMAX3) 3386 NODE_NAME_CASE(SMAX3) 3387 NODE_NAME_CASE(UMAX3) 3388 NODE_NAME_CASE(FMIN3) 3389 NODE_NAME_CASE(SMIN3) 3390 NODE_NAME_CASE(UMIN3) 3391 NODE_NAME_CASE(FMED3) 3392 NODE_NAME_CASE(SMED3) 3393 NODE_NAME_CASE(UMED3) 3394 NODE_NAME_CASE(URECIP) 3395 NODE_NAME_CASE(DIV_SCALE) 3396 NODE_NAME_CASE(DIV_FMAS) 3397 NODE_NAME_CASE(DIV_FIXUP) 3398 NODE_NAME_CASE(TRIG_PREOP) 3399 NODE_NAME_CASE(RCP) 3400 NODE_NAME_CASE(RSQ) 3401 NODE_NAME_CASE(RCP_LEGACY) 3402 NODE_NAME_CASE(RSQ_LEGACY) 3403 NODE_NAME_CASE(FMUL_LEGACY) 3404 NODE_NAME_CASE(RSQ_CLAMP) 3405 NODE_NAME_CASE(LDEXP) 3406 NODE_NAME_CASE(FP_CLASS) 3407 NODE_NAME_CASE(DOT4) 3408 NODE_NAME_CASE(CARRY) 3409 NODE_NAME_CASE(BORROW) 3410 NODE_NAME_CASE(BFE_U32) 3411 NODE_NAME_CASE(BFE_I32) 3412 NODE_NAME_CASE(BFI) 3413 NODE_NAME_CASE(BFM) 3414 NODE_NAME_CASE(FFBH_U32) 3415 NODE_NAME_CASE(FFBH_I32) 3416 NODE_NAME_CASE(MUL_U24) 3417 NODE_NAME_CASE(MUL_I24) 3418 NODE_NAME_CASE(MULHI_U24) 3419 NODE_NAME_CASE(MULHI_I24) 3420 NODE_NAME_CASE(MUL_LOHI_U24) 3421 NODE_NAME_CASE(MUL_LOHI_I24) 3422 NODE_NAME_CASE(MAD_U24) 3423 NODE_NAME_CASE(MAD_I24) 3424 NODE_NAME_CASE(TEXTURE_FETCH) 3425 NODE_NAME_CASE(EXPORT) 3426 NODE_NAME_CASE(EXPORT_DONE) 3427 NODE_NAME_CASE(R600_EXPORT) 3428 NODE_NAME_CASE(CONST_ADDRESS) 3429 NODE_NAME_CASE(REGISTER_LOAD) 3430 NODE_NAME_CASE(REGISTER_STORE) 3431 NODE_NAME_CASE(LOAD_INPUT) 3432 NODE_NAME_CASE(SAMPLE) 3433 NODE_NAME_CASE(SAMPLEB) 3434 NODE_NAME_CASE(SAMPLED) 3435 NODE_NAME_CASE(SAMPLEL) 3436 NODE_NAME_CASE(CVT_F32_UBYTE0) 3437 NODE_NAME_CASE(CVT_F32_UBYTE1) 3438 NODE_NAME_CASE(CVT_F32_UBYTE2) 3439 NODE_NAME_CASE(CVT_F32_UBYTE3) 3440 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR) 3441 NODE_NAME_CASE(CONST_DATA_PTR) 3442 NODE_NAME_CASE(PC_ADD_REL_OFFSET) 3443 NODE_NAME_CASE(KILL) 3444 NODE_NAME_CASE(DUMMY_CHAIN) 3445 case AMDGPUISD::FIRST_MEM_OPCODE_NUMBER: break; 3446 NODE_NAME_CASE(SENDMSG) 3447 NODE_NAME_CASE(SENDMSGHALT) 3448 NODE_NAME_CASE(INTERP_MOV) 3449 NODE_NAME_CASE(INTERP_P1) 3450 NODE_NAME_CASE(INTERP_P2) 3451 NODE_NAME_CASE(STORE_MSKOR) 3452 NODE_NAME_CASE(LOAD_CONSTANT) 3453 NODE_NAME_CASE(TBUFFER_STORE_FORMAT) 3454 NODE_NAME_CASE(ATOMIC_CMP_SWAP) 3455 NODE_NAME_CASE(ATOMIC_INC) 3456 NODE_NAME_CASE(ATOMIC_DEC) 3457 NODE_NAME_CASE(BUFFER_LOAD) 3458 NODE_NAME_CASE(BUFFER_LOAD_FORMAT) 3459 case AMDGPUISD::LAST_AMDGPU_ISD_NUMBER: break; 3460 } 3461 return nullptr; 3462 } 3463 3464 SDValue AMDGPUTargetLowering::getSqrtEstimate(SDValue Operand, 3465 SelectionDAG &DAG, int Enabled, 3466 int &RefinementSteps, 3467 bool &UseOneConstNR, 3468 bool Reciprocal) const { 3469 EVT VT = Operand.getValueType(); 3470 3471 if (VT == MVT::f32) { 3472 RefinementSteps = 0; 3473 return DAG.getNode(AMDGPUISD::RSQ, SDLoc(Operand), VT, Operand); 3474 } 3475 3476 // TODO: There is also f64 rsq instruction, but the documentation is less 3477 // clear on its precision. 3478 3479 return SDValue(); 3480 } 3481 3482 SDValue AMDGPUTargetLowering::getRecipEstimate(SDValue Operand, 3483 SelectionDAG &DAG, int Enabled, 3484 int &RefinementSteps) const { 3485 EVT VT = Operand.getValueType(); 3486 3487 if (VT == MVT::f32) { 3488 // Reciprocal, < 1 ulp error. 3489 // 3490 // This reciprocal approximation converges to < 0.5 ulp error with one 3491 // newton rhapson performed with two fused multiple adds (FMAs). 3492 3493 RefinementSteps = 0; 3494 return DAG.getNode(AMDGPUISD::RCP, SDLoc(Operand), VT, Operand); 3495 } 3496 3497 // TODO: There is also f64 rcp instruction, but the documentation is less 3498 // clear on its precision. 3499 3500 return SDValue(); 3501 } 3502 3503 void AMDGPUTargetLowering::computeKnownBitsForTargetNode( 3504 const SDValue Op, 3505 APInt &KnownZero, 3506 APInt &KnownOne, 3507 const SelectionDAG &DAG, 3508 unsigned Depth) const { 3509 3510 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); // Don't know anything. 3511 3512 APInt KnownZero2; 3513 APInt KnownOne2; 3514 unsigned Opc = Op.getOpcode(); 3515 3516 switch (Opc) { 3517 default: 3518 break; 3519 case AMDGPUISD::CARRY: 3520 case AMDGPUISD::BORROW: { 3521 KnownZero = APInt::getHighBitsSet(32, 31); 3522 break; 3523 } 3524 3525 case AMDGPUISD::BFE_I32: 3526 case AMDGPUISD::BFE_U32: { 3527 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 3528 if (!CWidth) 3529 return; 3530 3531 unsigned BitWidth = 32; 3532 uint32_t Width = CWidth->getZExtValue() & 0x1f; 3533 3534 if (Opc == AMDGPUISD::BFE_U32) 3535 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - Width); 3536 3537 break; 3538 } 3539 } 3540 } 3541 3542 unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode( 3543 SDValue Op, 3544 const SelectionDAG &DAG, 3545 unsigned Depth) const { 3546 switch (Op.getOpcode()) { 3547 case AMDGPUISD::BFE_I32: { 3548 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 3549 if (!Width) 3550 return 1; 3551 3552 unsigned SignBits = 32 - Width->getZExtValue() + 1; 3553 if (!isNullConstant(Op.getOperand(1))) 3554 return SignBits; 3555 3556 // TODO: Could probably figure something out with non-0 offsets. 3557 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1); 3558 return std::max(SignBits, Op0SignBits); 3559 } 3560 3561 case AMDGPUISD::BFE_U32: { 3562 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 3563 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1; 3564 } 3565 3566 case AMDGPUISD::CARRY: 3567 case AMDGPUISD::BORROW: 3568 return 31; 3569 3570 default: 3571 return 1; 3572 } 3573 } 3574