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 "AMDGPUFrameLowering.h" 19 #include "AMDGPUIntrinsicInfo.h" 20 #include "AMDGPURegisterInfo.h" 21 #include "AMDGPUSubtarget.h" 22 #include "R600MachineFunctionInfo.h" 23 #include "SIMachineFunctionInfo.h" 24 #include "llvm/CodeGen/CallingConvLower.h" 25 #include "llvm/CodeGen/MachineFunction.h" 26 #include "llvm/CodeGen/MachineRegisterInfo.h" 27 #include "llvm/CodeGen/SelectionDAG.h" 28 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 29 #include "llvm/IR/DataLayout.h" 30 #include "llvm/IR/DiagnosticInfo.h" 31 #include "SIInstrInfo.h" 32 using namespace llvm; 33 34 static bool allocateStack(unsigned ValNo, MVT ValVT, MVT LocVT, 35 CCValAssign::LocInfo LocInfo, 36 ISD::ArgFlagsTy ArgFlags, CCState &State) { 37 unsigned Offset = State.AllocateStack(ValVT.getStoreSize(), 38 ArgFlags.getOrigAlign()); 39 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo)); 40 41 return true; 42 } 43 44 #include "AMDGPUGenCallingConv.inc" 45 46 // Find a larger type to do a load / store of a vector with. 47 EVT AMDGPUTargetLowering::getEquivalentMemType(LLVMContext &Ctx, EVT VT) { 48 unsigned StoreSize = VT.getStoreSizeInBits(); 49 if (StoreSize <= 32) 50 return EVT::getIntegerVT(Ctx, StoreSize); 51 52 assert(StoreSize % 32 == 0 && "Store size not a multiple of 32"); 53 return EVT::getVectorVT(Ctx, MVT::i32, StoreSize / 32); 54 } 55 56 // Type for a vector that will be loaded to. 57 EVT AMDGPUTargetLowering::getEquivalentLoadRegType(LLVMContext &Ctx, EVT VT) { 58 unsigned StoreSize = VT.getStoreSizeInBits(); 59 if (StoreSize <= 32) 60 return EVT::getIntegerVT(Ctx, 32); 61 62 return EVT::getVectorVT(Ctx, MVT::i32, StoreSize / 32); 63 } 64 65 AMDGPUTargetLowering::AMDGPUTargetLowering(TargetMachine &TM, 66 const AMDGPUSubtarget &STI) 67 : TargetLowering(TM), Subtarget(&STI) { 68 setOperationAction(ISD::Constant, MVT::i32, Legal); 69 setOperationAction(ISD::Constant, MVT::i64, Legal); 70 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); 71 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); 72 73 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 74 setOperationAction(ISD::BRIND, MVT::Other, Expand); 75 76 // This is totally unsupported, just custom lower to produce an error. 77 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom); 78 79 // We need to custom lower some of the intrinsics 80 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 81 82 // Library functions. These default to Expand, but we have instructions 83 // for them. 84 setOperationAction(ISD::FCEIL, MVT::f32, Legal); 85 setOperationAction(ISD::FEXP2, MVT::f32, Legal); 86 setOperationAction(ISD::FPOW, MVT::f32, Legal); 87 setOperationAction(ISD::FLOG2, MVT::f32, Legal); 88 setOperationAction(ISD::FABS, MVT::f32, Legal); 89 setOperationAction(ISD::FFLOOR, MVT::f32, Legal); 90 setOperationAction(ISD::FRINT, MVT::f32, Legal); 91 setOperationAction(ISD::FTRUNC, MVT::f32, Legal); 92 setOperationAction(ISD::FMINNUM, MVT::f32, Legal); 93 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal); 94 95 setOperationAction(ISD::FROUND, MVT::f32, Custom); 96 setOperationAction(ISD::FROUND, MVT::f64, Custom); 97 98 setOperationAction(ISD::FREM, MVT::f32, Custom); 99 setOperationAction(ISD::FREM, MVT::f64, Custom); 100 101 // v_mad_f32 does not support denormals according to some sources. 102 if (!Subtarget->hasFP32Denormals()) 103 setOperationAction(ISD::FMAD, MVT::f32, Legal); 104 105 // Expand to fneg + fadd. 106 setOperationAction(ISD::FSUB, MVT::f64, Expand); 107 108 // Lower floating point store/load to integer store/load to reduce the number 109 // of patterns in tablegen. 110 setOperationAction(ISD::STORE, MVT::f32, Promote); 111 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32); 112 113 setOperationAction(ISD::STORE, MVT::v2f32, Promote); 114 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32); 115 116 setOperationAction(ISD::STORE, MVT::v4f32, Promote); 117 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32); 118 119 setOperationAction(ISD::STORE, MVT::v8f32, Promote); 120 AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32); 121 122 setOperationAction(ISD::STORE, MVT::v16f32, Promote); 123 AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32); 124 125 setOperationAction(ISD::STORE, MVT::f64, Promote); 126 AddPromotedToType(ISD::STORE, MVT::f64, MVT::i64); 127 128 setOperationAction(ISD::STORE, MVT::v2f64, Promote); 129 AddPromotedToType(ISD::STORE, MVT::v2f64, MVT::v2i64); 130 131 // Custom lowering of vector stores is required for local address space 132 // stores. 133 setOperationAction(ISD::STORE, MVT::v4i32, Custom); 134 135 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom); 136 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom); 137 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom); 138 139 // XXX: This can be change to Custom, once ExpandVectorStores can 140 // handle 64-bit stores. 141 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand); 142 143 setTruncStoreAction(MVT::i64, MVT::i16, Expand); 144 setTruncStoreAction(MVT::i64, MVT::i8, Expand); 145 setTruncStoreAction(MVT::i64, MVT::i1, Expand); 146 setTruncStoreAction(MVT::v2i64, MVT::v2i1, Expand); 147 setTruncStoreAction(MVT::v4i64, MVT::v4i1, Expand); 148 149 150 setOperationAction(ISD::LOAD, MVT::f32, Promote); 151 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32); 152 153 setOperationAction(ISD::LOAD, MVT::v2f32, Promote); 154 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32); 155 156 setOperationAction(ISD::LOAD, MVT::v4f32, Promote); 157 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32); 158 159 setOperationAction(ISD::LOAD, MVT::v8f32, Promote); 160 AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32); 161 162 setOperationAction(ISD::LOAD, MVT::v16f32, Promote); 163 AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32); 164 165 setOperationAction(ISD::LOAD, MVT::f64, Promote); 166 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::i64); 167 168 setOperationAction(ISD::LOAD, MVT::v2f64, Promote); 169 AddPromotedToType(ISD::LOAD, MVT::v2f64, MVT::v2i64); 170 171 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom); 172 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom); 173 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom); 174 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom); 175 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom); 176 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom); 177 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4f32, Custom); 178 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom); 179 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8f32, Custom); 180 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8i32, Custom); 181 182 // There are no 64-bit extloads. These should be done as a 32-bit extload and 183 // an extension to 64-bit. 184 for (MVT VT : MVT::integer_valuetypes()) { 185 setLoadExtAction(ISD::EXTLOAD, MVT::i64, VT, Expand); 186 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, VT, Expand); 187 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, VT, Expand); 188 } 189 190 for (MVT VT : MVT::integer_vector_valuetypes()) { 191 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i8, Expand); 192 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i8, Expand); 193 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i8, Expand); 194 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i8, Expand); 195 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i8, Expand); 196 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i8, Expand); 197 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i16, Expand); 198 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i16, Expand); 199 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i16, Expand); 200 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i16, Expand); 201 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i16, Expand); 202 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i16, Expand); 203 } 204 205 setOperationAction(ISD::BR_CC, MVT::i1, Expand); 206 207 if (Subtarget->getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) { 208 setOperationAction(ISD::FCEIL, MVT::f64, Custom); 209 setOperationAction(ISD::FTRUNC, MVT::f64, Custom); 210 setOperationAction(ISD::FRINT, MVT::f64, Custom); 211 setOperationAction(ISD::FFLOOR, MVT::f64, Custom); 212 } 213 214 if (!Subtarget->hasBFI()) { 215 // fcopysign can be done in a single instruction with BFI. 216 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); 217 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 218 } 219 220 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand); 221 222 setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand); 223 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, MVT::v2f16, Expand); 224 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, MVT::v4f16, Expand); 225 setLoadExtAction(ISD::EXTLOAD, MVT::v8f32, MVT::v8f16, Expand); 226 227 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand); 228 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand); 229 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f16, Expand); 230 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f16, Expand); 231 232 setTruncStoreAction(MVT::f32, MVT::f16, Expand); 233 setTruncStoreAction(MVT::v2f32, MVT::v2f16, Expand); 234 setTruncStoreAction(MVT::v4f32, MVT::v4f16, Expand); 235 setTruncStoreAction(MVT::v8f32, MVT::v8f16, Expand); 236 237 setTruncStoreAction(MVT::f64, MVT::f16, Expand); 238 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 239 240 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 }; 241 for (MVT VT : ScalarIntVTs) { 242 setOperationAction(ISD::SREM, VT, Expand); 243 setOperationAction(ISD::SDIV, VT, Expand); 244 245 // GPU does not have divrem function for signed or unsigned. 246 setOperationAction(ISD::SDIVREM, VT, Custom); 247 setOperationAction(ISD::UDIVREM, VT, Custom); 248 249 // GPU does not have [S|U]MUL_LOHI functions as a single instruction. 250 setOperationAction(ISD::SMUL_LOHI, VT, Expand); 251 setOperationAction(ISD::UMUL_LOHI, VT, Expand); 252 253 setOperationAction(ISD::BSWAP, VT, Expand); 254 setOperationAction(ISD::CTTZ, VT, Expand); 255 setOperationAction(ISD::CTLZ, VT, Expand); 256 } 257 258 if (!Subtarget->hasBCNT(32)) 259 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 260 261 if (!Subtarget->hasBCNT(64)) 262 setOperationAction(ISD::CTPOP, MVT::i64, Expand); 263 264 // The hardware supports 32-bit ROTR, but not ROTL. 265 setOperationAction(ISD::ROTL, MVT::i32, Expand); 266 setOperationAction(ISD::ROTL, MVT::i64, Expand); 267 setOperationAction(ISD::ROTR, MVT::i64, Expand); 268 269 setOperationAction(ISD::MUL, MVT::i64, Expand); 270 setOperationAction(ISD::MULHU, MVT::i64, Expand); 271 setOperationAction(ISD::MULHS, MVT::i64, Expand); 272 setOperationAction(ISD::UDIV, MVT::i32, Expand); 273 setOperationAction(ISD::UREM, MVT::i32, Expand); 274 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom); 275 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); 276 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); 277 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom); 278 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand); 279 280 setOperationAction(ISD::SMIN, MVT::i32, Legal); 281 setOperationAction(ISD::UMIN, MVT::i32, Legal); 282 setOperationAction(ISD::SMAX, MVT::i32, Legal); 283 setOperationAction(ISD::UMAX, MVT::i32, Legal); 284 285 if (Subtarget->hasFFBH()) 286 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Custom); 287 288 if (Subtarget->hasFFBL()) 289 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Legal); 290 291 setOperationAction(ISD::CTLZ, MVT::i64, Custom); 292 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom); 293 294 // We only really have 32-bit BFE instructions (and 16-bit on VI). 295 // 296 // On SI+ there are 64-bit BFEs, but they are scalar only and there isn't any 297 // effort to match them now. We want this to be false for i64 cases when the 298 // extraction isn't restricted to the upper or lower half. Ideally we would 299 // have some pass reduce 64-bit extracts to 32-bit if possible. Extracts that 300 // span the midpoint are probably relatively rare, so don't worry about them 301 // for now. 302 if (Subtarget->hasBFE()) 303 setHasExtractBitsInsn(true); 304 305 static const MVT::SimpleValueType VectorIntTypes[] = { 306 MVT::v2i32, MVT::v4i32 307 }; 308 309 for (MVT VT : VectorIntTypes) { 310 // Expand the following operations for the current type by default. 311 setOperationAction(ISD::ADD, VT, Expand); 312 setOperationAction(ISD::AND, VT, Expand); 313 setOperationAction(ISD::FP_TO_SINT, VT, Expand); 314 setOperationAction(ISD::FP_TO_UINT, VT, Expand); 315 setOperationAction(ISD::MUL, VT, Expand); 316 setOperationAction(ISD::OR, VT, Expand); 317 setOperationAction(ISD::SHL, VT, Expand); 318 setOperationAction(ISD::SRA, VT, Expand); 319 setOperationAction(ISD::SRL, VT, Expand); 320 setOperationAction(ISD::ROTL, VT, Expand); 321 setOperationAction(ISD::ROTR, VT, Expand); 322 setOperationAction(ISD::SUB, VT, Expand); 323 setOperationAction(ISD::SINT_TO_FP, VT, Expand); 324 setOperationAction(ISD::UINT_TO_FP, VT, Expand); 325 setOperationAction(ISD::SDIV, VT, Expand); 326 setOperationAction(ISD::UDIV, VT, Expand); 327 setOperationAction(ISD::SREM, VT, Expand); 328 setOperationAction(ISD::UREM, VT, Expand); 329 setOperationAction(ISD::SMUL_LOHI, VT, Expand); 330 setOperationAction(ISD::UMUL_LOHI, VT, Expand); 331 setOperationAction(ISD::SDIVREM, VT, Custom); 332 setOperationAction(ISD::UDIVREM, VT, Expand); 333 setOperationAction(ISD::ADDC, VT, Expand); 334 setOperationAction(ISD::SUBC, VT, Expand); 335 setOperationAction(ISD::ADDE, VT, Expand); 336 setOperationAction(ISD::SUBE, VT, Expand); 337 setOperationAction(ISD::SELECT, VT, Expand); 338 setOperationAction(ISD::VSELECT, VT, Expand); 339 setOperationAction(ISD::SELECT_CC, VT, Expand); 340 setOperationAction(ISD::XOR, VT, Expand); 341 setOperationAction(ISD::BSWAP, VT, Expand); 342 setOperationAction(ISD::CTPOP, VT, Expand); 343 setOperationAction(ISD::CTTZ, VT, Expand); 344 setOperationAction(ISD::CTLZ, VT, Expand); 345 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand); 346 } 347 348 static const MVT::SimpleValueType FloatVectorTypes[] = { 349 MVT::v2f32, MVT::v4f32 350 }; 351 352 for (MVT VT : FloatVectorTypes) { 353 setOperationAction(ISD::FABS, VT, Expand); 354 setOperationAction(ISD::FMINNUM, VT, Expand); 355 setOperationAction(ISD::FMAXNUM, VT, Expand); 356 setOperationAction(ISD::FADD, VT, Expand); 357 setOperationAction(ISD::FCEIL, VT, Expand); 358 setOperationAction(ISD::FCOS, VT, Expand); 359 setOperationAction(ISD::FDIV, VT, Expand); 360 setOperationAction(ISD::FEXP2, VT, Expand); 361 setOperationAction(ISD::FLOG2, VT, Expand); 362 setOperationAction(ISD::FREM, VT, Expand); 363 setOperationAction(ISD::FPOW, VT, Expand); 364 setOperationAction(ISD::FFLOOR, VT, Expand); 365 setOperationAction(ISD::FTRUNC, VT, Expand); 366 setOperationAction(ISD::FMUL, VT, Expand); 367 setOperationAction(ISD::FMA, VT, Expand); 368 setOperationAction(ISD::FRINT, VT, Expand); 369 setOperationAction(ISD::FNEARBYINT, VT, Expand); 370 setOperationAction(ISD::FSQRT, VT, Expand); 371 setOperationAction(ISD::FSIN, VT, Expand); 372 setOperationAction(ISD::FSUB, VT, Expand); 373 setOperationAction(ISD::FNEG, VT, Expand); 374 setOperationAction(ISD::SELECT, VT, Expand); 375 setOperationAction(ISD::VSELECT, VT, Expand); 376 setOperationAction(ISD::SELECT_CC, VT, Expand); 377 setOperationAction(ISD::FCOPYSIGN, VT, Expand); 378 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand); 379 } 380 381 setOperationAction(ISD::FNEARBYINT, MVT::f32, Custom); 382 setOperationAction(ISD::FNEARBYINT, MVT::f64, Custom); 383 384 setTargetDAGCombine(ISD::AND); 385 setTargetDAGCombine(ISD::SHL); 386 setTargetDAGCombine(ISD::SRA); 387 setTargetDAGCombine(ISD::SRL); 388 setTargetDAGCombine(ISD::MUL); 389 setTargetDAGCombine(ISD::SELECT); 390 setTargetDAGCombine(ISD::SELECT_CC); 391 setTargetDAGCombine(ISD::STORE); 392 393 setTargetDAGCombine(ISD::FADD); 394 setTargetDAGCombine(ISD::FSUB); 395 396 setTargetDAGCombine(ISD::BITCAST); 397 398 setBooleanContents(ZeroOrNegativeOneBooleanContent); 399 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 400 401 setSchedulingPreference(Sched::RegPressure); 402 setJumpIsExpensive(true); 403 404 // SI at least has hardware support for floating point exceptions, but no way 405 // of using or handling them is implemented. They are also optional in OpenCL 406 // (Section 7.3) 407 setHasFloatingPointExceptions(Subtarget->hasFPExceptions()); 408 409 setSelectIsExpensive(false); 410 PredictableSelectIsExpensive = false; 411 412 setFsqrtIsCheap(true); 413 414 // We want to find all load dependencies for long chains of stores to enable 415 // merging into very wide vectors. The problem is with vectors with > 4 416 // elements. MergeConsecutiveStores will attempt to merge these because x8/x16 417 // vectors are a legal type, even though we have to split the loads 418 // usually. When we can more precisely specify load legality per address 419 // space, we should be able to make FindBetterChain/MergeConsecutiveStores 420 // smarter so that they can figure out what to do in 2 iterations without all 421 // N > 4 stores on the same chain. 422 GatherAllAliasesMaxDepth = 16; 423 424 // FIXME: Need to really handle these. 425 MaxStoresPerMemcpy = 4096; 426 MaxStoresPerMemmove = 4096; 427 MaxStoresPerMemset = 4096; 428 } 429 430 //===----------------------------------------------------------------------===// 431 // Target Information 432 //===----------------------------------------------------------------------===// 433 434 MVT AMDGPUTargetLowering::getVectorIdxTy(const DataLayout &) const { 435 return MVT::i32; 436 } 437 438 bool AMDGPUTargetLowering::isSelectSupported(SelectSupportKind SelType) const { 439 return true; 440 } 441 442 // The backend supports 32 and 64 bit floating point immediates. 443 // FIXME: Why are we reporting vectors of FP immediates as legal? 444 bool AMDGPUTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { 445 EVT ScalarVT = VT.getScalarType(); 446 return (ScalarVT == MVT::f32 || ScalarVT == MVT::f64); 447 } 448 449 // We don't want to shrink f64 / f32 constants. 450 bool AMDGPUTargetLowering::ShouldShrinkFPConstant(EVT VT) const { 451 EVT ScalarVT = VT.getScalarType(); 452 return (ScalarVT != MVT::f32 && ScalarVT != MVT::f64); 453 } 454 455 bool AMDGPUTargetLowering::shouldReduceLoadWidth(SDNode *N, 456 ISD::LoadExtType, 457 EVT NewVT) const { 458 459 unsigned NewSize = NewVT.getStoreSizeInBits(); 460 461 // If we are reducing to a 32-bit load, this is always better. 462 if (NewSize == 32) 463 return true; 464 465 EVT OldVT = N->getValueType(0); 466 unsigned OldSize = OldVT.getStoreSizeInBits(); 467 468 // Don't produce extloads from sub 32-bit types. SI doesn't have scalar 469 // extloads, so doing one requires using a buffer_load. In cases where we 470 // still couldn't use a scalar load, using the wider load shouldn't really 471 // hurt anything. 472 473 // If the old size already had to be an extload, there's no harm in continuing 474 // to reduce the width. 475 return (OldSize < 32); 476 } 477 478 bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy, 479 EVT CastTy) const { 480 if (LoadTy.getSizeInBits() != CastTy.getSizeInBits()) 481 return true; 482 483 unsigned LScalarSize = LoadTy.getScalarType().getSizeInBits(); 484 unsigned CastScalarSize = CastTy.getScalarType().getSizeInBits(); 485 486 return ((LScalarSize <= CastScalarSize) || 487 (CastScalarSize >= 32) || 488 (LScalarSize < 32)); 489 } 490 491 // SI+ has instructions for cttz / ctlz for 32-bit values. This is probably also 492 // profitable with the expansion for 64-bit since it's generally good to 493 // speculate things. 494 // FIXME: These should really have the size as a parameter. 495 bool AMDGPUTargetLowering::isCheapToSpeculateCttz() const { 496 return true; 497 } 498 499 bool AMDGPUTargetLowering::isCheapToSpeculateCtlz() const { 500 return true; 501 } 502 503 //===---------------------------------------------------------------------===// 504 // Target Properties 505 //===---------------------------------------------------------------------===// 506 507 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const { 508 assert(VT.isFloatingPoint()); 509 return VT == MVT::f32 || VT == MVT::f64; 510 } 511 512 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const { 513 assert(VT.isFloatingPoint()); 514 return VT == MVT::f32 || VT == MVT::f64; 515 } 516 517 bool AMDGPUTargetLowering:: storeOfVectorConstantIsCheap(EVT MemVT, 518 unsigned NumElem, 519 unsigned AS) const { 520 return true; 521 } 522 523 bool AMDGPUTargetLowering::aggressivelyPreferBuildVectorSources(EVT VecVT) const { 524 // There are few operations which truly have vector input operands. Any vector 525 // operation is going to involve operations on each component, and a 526 // build_vector will be a copy per element, so it always makes sense to use a 527 // build_vector input in place of the extracted element to avoid a copy into a 528 // super register. 529 // 530 // We should probably only do this if all users are extracts only, but this 531 // should be the common case. 532 return true; 533 } 534 535 bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const { 536 // Truncate is just accessing a subregister. 537 return Dest.bitsLT(Source) && (Dest.getSizeInBits() % 32 == 0); 538 } 539 540 bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const { 541 // Truncate is just accessing a subregister. 542 return Dest->getPrimitiveSizeInBits() < Source->getPrimitiveSizeInBits() && 543 (Dest->getPrimitiveSizeInBits() % 32 == 0); 544 } 545 546 bool AMDGPUTargetLowering::isZExtFree(Type *Src, Type *Dest) const { 547 unsigned SrcSize = Src->getScalarSizeInBits(); 548 unsigned DestSize = Dest->getScalarSizeInBits(); 549 550 return SrcSize == 32 && DestSize == 64; 551 } 552 553 bool AMDGPUTargetLowering::isZExtFree(EVT Src, EVT Dest) const { 554 // Any register load of a 64-bit value really requires 2 32-bit moves. For all 555 // practical purposes, the extra mov 0 to load a 64-bit is free. As used, 556 // this will enable reducing 64-bit operations the 32-bit, which is always 557 // good. 558 return Src == MVT::i32 && Dest == MVT::i64; 559 } 560 561 bool AMDGPUTargetLowering::isZExtFree(SDValue Val, EVT VT2) const { 562 return isZExtFree(Val.getValueType(), VT2); 563 } 564 565 bool AMDGPUTargetLowering::isNarrowingProfitable(EVT SrcVT, EVT DestVT) const { 566 // There aren't really 64-bit registers, but pairs of 32-bit ones and only a 567 // limited number of native 64-bit operations. Shrinking an operation to fit 568 // in a single 32-bit register should always be helpful. As currently used, 569 // this is much less general than the name suggests, and is only used in 570 // places trying to reduce the sizes of loads. Shrinking loads to < 32-bits is 571 // not profitable, and may actually be harmful. 572 return SrcVT.getSizeInBits() > 32 && DestVT.getSizeInBits() == 32; 573 } 574 575 //===---------------------------------------------------------------------===// 576 // TargetLowering Callbacks 577 //===---------------------------------------------------------------------===// 578 579 void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State, 580 const SmallVectorImpl<ISD::InputArg> &Ins) const { 581 582 State.AnalyzeFormalArguments(Ins, CC_AMDGPU); 583 } 584 585 void AMDGPUTargetLowering::AnalyzeReturn(CCState &State, 586 const SmallVectorImpl<ISD::OutputArg> &Outs) const { 587 588 State.AnalyzeReturn(Outs, RetCC_SI); 589 } 590 591 SDValue AMDGPUTargetLowering::LowerReturn( 592 SDValue Chain, 593 CallingConv::ID CallConv, 594 bool isVarArg, 595 const SmallVectorImpl<ISD::OutputArg> &Outs, 596 const SmallVectorImpl<SDValue> &OutVals, 597 SDLoc DL, SelectionDAG &DAG) const { 598 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, Chain); 599 } 600 601 //===---------------------------------------------------------------------===// 602 // Target specific lowering 603 //===---------------------------------------------------------------------===// 604 605 SDValue AMDGPUTargetLowering::LowerCall(CallLoweringInfo &CLI, 606 SmallVectorImpl<SDValue> &InVals) const { 607 SDValue Callee = CLI.Callee; 608 SelectionDAG &DAG = CLI.DAG; 609 610 const Function &Fn = *DAG.getMachineFunction().getFunction(); 611 612 StringRef FuncName("<unknown>"); 613 614 if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee)) 615 FuncName = G->getSymbol(); 616 else if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 617 FuncName = G->getGlobal()->getName(); 618 619 DiagnosticInfoUnsupported NoCalls( 620 Fn, "unsupported call to function " + FuncName, CLI.DL.getDebugLoc()); 621 DAG.getContext()->diagnose(NoCalls); 622 return SDValue(); 623 } 624 625 SDValue AMDGPUTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, 626 SelectionDAG &DAG) const { 627 const Function &Fn = *DAG.getMachineFunction().getFunction(); 628 629 DiagnosticInfoUnsupported NoDynamicAlloca(Fn, "unsupported dynamic alloca", 630 SDLoc(Op).getDebugLoc()); 631 DAG.getContext()->diagnose(NoDynamicAlloca); 632 return SDValue(); 633 } 634 635 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op, 636 SelectionDAG &DAG) const { 637 switch (Op.getOpcode()) { 638 default: 639 Op->dump(&DAG); 640 llvm_unreachable("Custom lowering code for this" 641 "instruction is not implemented yet!"); 642 break; 643 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG); 644 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG); 645 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG); 646 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); 647 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG); 648 case ISD::SDIVREM: return LowerSDIVREM(Op, DAG); 649 case ISD::FREM: return LowerFREM(Op, DAG); 650 case ISD::FCEIL: return LowerFCEIL(Op, DAG); 651 case ISD::FTRUNC: return LowerFTRUNC(Op, DAG); 652 case ISD::FRINT: return LowerFRINT(Op, DAG); 653 case ISD::FNEARBYINT: return LowerFNEARBYINT(Op, DAG); 654 case ISD::FROUND: return LowerFROUND(Op, DAG); 655 case ISD::FFLOOR: return LowerFFLOOR(Op, DAG); 656 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG); 657 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG); 658 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG); 659 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG); 660 case ISD::CTLZ: 661 case ISD::CTLZ_ZERO_UNDEF: 662 return LowerCTLZ(Op, DAG); 663 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG); 664 } 665 return Op; 666 } 667 668 void AMDGPUTargetLowering::ReplaceNodeResults(SDNode *N, 669 SmallVectorImpl<SDValue> &Results, 670 SelectionDAG &DAG) const { 671 switch (N->getOpcode()) { 672 case ISD::SIGN_EXTEND_INREG: 673 // Different parts of legalization seem to interpret which type of 674 // sign_extend_inreg is the one to check for custom lowering. The extended 675 // from type is what really matters, but some places check for custom 676 // lowering of the result type. This results in trying to use 677 // ReplaceNodeResults to sext_in_reg to an illegal type, so we'll just do 678 // nothing here and let the illegal result integer be handled normally. 679 return; 680 default: 681 return; 682 } 683 } 684 685 // FIXME: This implements accesses to initialized globals in the constant 686 // address space by copying them to private and accessing that. It does not 687 // properly handle illegal types or vectors. The private vector loads are not 688 // scalarized, and the illegal scalars hit an assertion. This technique will not 689 // work well with large initializers, and this should eventually be 690 // removed. Initialized globals should be placed into a data section that the 691 // runtime will load into a buffer before the kernel is executed. Uses of the 692 // global need to be replaced with a pointer loaded from an implicit kernel 693 // argument into this buffer holding the copy of the data, which will remove the 694 // need for any of this. 695 SDValue AMDGPUTargetLowering::LowerConstantInitializer(const Constant* Init, 696 const GlobalValue *GV, 697 const SDValue &InitPtr, 698 SDValue Chain, 699 SelectionDAG &DAG) const { 700 const DataLayout &TD = DAG.getDataLayout(); 701 SDLoc DL(InitPtr); 702 Type *InitTy = Init->getType(); 703 704 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Init)) { 705 EVT VT = EVT::getEVT(InitTy); 706 PointerType *PtrTy = PointerType::get(InitTy, AMDGPUAS::PRIVATE_ADDRESS); 707 return DAG.getStore(Chain, DL, DAG.getConstant(*CI, DL, VT), InitPtr, 708 MachinePointerInfo(UndefValue::get(PtrTy)), false, 709 false, TD.getPrefTypeAlignment(InitTy)); 710 } 711 712 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Init)) { 713 EVT VT = EVT::getEVT(CFP->getType()); 714 PointerType *PtrTy = PointerType::get(CFP->getType(), 0); 715 return DAG.getStore(Chain, DL, DAG.getConstantFP(*CFP, DL, VT), InitPtr, 716 MachinePointerInfo(UndefValue::get(PtrTy)), false, 717 false, TD.getPrefTypeAlignment(CFP->getType())); 718 } 719 720 if (StructType *ST = dyn_cast<StructType>(InitTy)) { 721 const StructLayout *SL = TD.getStructLayout(ST); 722 723 EVT PtrVT = InitPtr.getValueType(); 724 SmallVector<SDValue, 8> Chains; 725 726 for (unsigned I = 0, N = ST->getNumElements(); I != N; ++I) { 727 SDValue Offset = DAG.getConstant(SL->getElementOffset(I), DL, PtrVT); 728 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, InitPtr, Offset); 729 730 Constant *Elt = Init->getAggregateElement(I); 731 Chains.push_back(LowerConstantInitializer(Elt, GV, Ptr, Chain, DAG)); 732 } 733 734 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 735 } 736 737 if (SequentialType *SeqTy = dyn_cast<SequentialType>(InitTy)) { 738 EVT PtrVT = InitPtr.getValueType(); 739 740 unsigned NumElements; 741 if (ArrayType *AT = dyn_cast<ArrayType>(SeqTy)) 742 NumElements = AT->getNumElements(); 743 else if (VectorType *VT = dyn_cast<VectorType>(SeqTy)) 744 NumElements = VT->getNumElements(); 745 else 746 llvm_unreachable("Unexpected type"); 747 748 unsigned EltSize = TD.getTypeAllocSize(SeqTy->getElementType()); 749 SmallVector<SDValue, 8> Chains; 750 for (unsigned i = 0; i < NumElements; ++i) { 751 SDValue Offset = DAG.getConstant(i * EltSize, DL, PtrVT); 752 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, InitPtr, Offset); 753 754 Constant *Elt = Init->getAggregateElement(i); 755 Chains.push_back(LowerConstantInitializer(Elt, GV, Ptr, Chain, DAG)); 756 } 757 758 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 759 } 760 761 if (isa<UndefValue>(Init)) { 762 EVT VT = EVT::getEVT(InitTy); 763 PointerType *PtrTy = PointerType::get(InitTy, AMDGPUAS::PRIVATE_ADDRESS); 764 return DAG.getStore(Chain, DL, DAG.getUNDEF(VT), InitPtr, 765 MachinePointerInfo(UndefValue::get(PtrTy)), false, 766 false, TD.getPrefTypeAlignment(InitTy)); 767 } 768 769 Init->dump(); 770 llvm_unreachable("Unhandled constant initializer"); 771 } 772 773 static bool hasDefinedInitializer(const GlobalValue *GV) { 774 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 775 if (!GVar || !GVar->hasInitializer()) 776 return false; 777 778 return !isa<UndefValue>(GVar->getInitializer()); 779 } 780 781 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI, 782 SDValue Op, 783 SelectionDAG &DAG) const { 784 785 const DataLayout &DL = DAG.getDataLayout(); 786 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op); 787 const GlobalValue *GV = G->getGlobal(); 788 789 switch (G->getAddressSpace()) { 790 case AMDGPUAS::LOCAL_ADDRESS: { 791 // XXX: What does the value of G->getOffset() mean? 792 assert(G->getOffset() == 0 && 793 "Do not know what to do with an non-zero offset"); 794 795 // TODO: We could emit code to handle the initialization somewhere. 796 if (hasDefinedInitializer(GV)) 797 break; 798 799 unsigned Offset; 800 if (MFI->LocalMemoryObjects.count(GV) == 0) { 801 unsigned Align = GV->getAlignment(); 802 if (Align == 0) 803 Align = DL.getABITypeAlignment(GV->getValueType()); 804 805 /// TODO: We should sort these to minimize wasted space due to alignment 806 /// padding. Currently the padding is decided by the first encountered use 807 /// during lowering. 808 Offset = MFI->LDSSize = alignTo(MFI->LDSSize, Align); 809 MFI->LocalMemoryObjects[GV] = Offset; 810 MFI->LDSSize += DL.getTypeAllocSize(GV->getValueType()); 811 } else { 812 Offset = MFI->LocalMemoryObjects[GV]; 813 } 814 815 return DAG.getConstant(Offset, SDLoc(Op), 816 getPointerTy(DL, AMDGPUAS::LOCAL_ADDRESS)); 817 } 818 } 819 820 const Function &Fn = *DAG.getMachineFunction().getFunction(); 821 DiagnosticInfoUnsupported BadInit( 822 Fn, "unsupported initializer for address space", SDLoc(Op).getDebugLoc()); 823 DAG.getContext()->diagnose(BadInit); 824 return SDValue(); 825 } 826 827 SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op, 828 SelectionDAG &DAG) const { 829 SmallVector<SDValue, 8> Args; 830 831 for (const SDUse &U : Op->ops()) 832 DAG.ExtractVectorElements(U.get(), Args); 833 834 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args); 835 } 836 837 SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, 838 SelectionDAG &DAG) const { 839 840 SmallVector<SDValue, 8> Args; 841 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 842 EVT VT = Op.getValueType(); 843 DAG.ExtractVectorElements(Op.getOperand(0), Args, Start, 844 VT.getVectorNumElements()); 845 846 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args); 847 } 848 849 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, 850 SelectionDAG &DAG) const { 851 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 852 SDLoc DL(Op); 853 EVT VT = Op.getValueType(); 854 855 switch (IntrinsicID) { 856 default: return Op; 857 case AMDGPUIntrinsic::AMDGPU_clamp: 858 case AMDGPUIntrinsic::AMDIL_clamp: // Legacy name. 859 return DAG.getNode(AMDGPUISD::CLAMP, DL, VT, 860 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 861 862 case Intrinsic::AMDGPU_ldexp: // Legacy name 863 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT, Op.getOperand(1), 864 Op.getOperand(2)); 865 866 case AMDGPUIntrinsic::AMDGPU_bfe_i32: 867 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT, 868 Op.getOperand(1), 869 Op.getOperand(2), 870 Op.getOperand(3)); 871 872 case AMDGPUIntrinsic::AMDGPU_bfe_u32: 873 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT, 874 Op.getOperand(1), 875 Op.getOperand(2), 876 Op.getOperand(3)); 877 878 case AMDGPUIntrinsic::AMDIL_exp: // Legacy name. 879 return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1)); 880 881 case AMDGPUIntrinsic::AMDGPU_brev: // Legacy name 882 return DAG.getNode(ISD::BITREVERSE, DL, VT, Op.getOperand(1)); 883 } 884 } 885 886 /// \brief Generate Min/Max node 887 SDValue AMDGPUTargetLowering::CombineFMinMaxLegacy(SDLoc DL, 888 EVT VT, 889 SDValue LHS, 890 SDValue RHS, 891 SDValue True, 892 SDValue False, 893 SDValue CC, 894 DAGCombinerInfo &DCI) const { 895 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) 896 return SDValue(); 897 898 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True)) 899 return SDValue(); 900 901 SelectionDAG &DAG = DCI.DAG; 902 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get(); 903 switch (CCOpcode) { 904 case ISD::SETOEQ: 905 case ISD::SETONE: 906 case ISD::SETUNE: 907 case ISD::SETNE: 908 case ISD::SETUEQ: 909 case ISD::SETEQ: 910 case ISD::SETFALSE: 911 case ISD::SETFALSE2: 912 case ISD::SETTRUE: 913 case ISD::SETTRUE2: 914 case ISD::SETUO: 915 case ISD::SETO: 916 break; 917 case ISD::SETULE: 918 case ISD::SETULT: { 919 if (LHS == True) 920 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS); 921 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS); 922 } 923 case ISD::SETOLE: 924 case ISD::SETOLT: 925 case ISD::SETLE: 926 case ISD::SETLT: { 927 // Ordered. Assume ordered for undefined. 928 929 // Only do this after legalization to avoid interfering with other combines 930 // which might occur. 931 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG && 932 !DCI.isCalledByLegalizer()) 933 return SDValue(); 934 935 // We need to permute the operands to get the correct NaN behavior. The 936 // selected operand is the second one based on the failing compare with NaN, 937 // so permute it based on the compare type the hardware uses. 938 if (LHS == True) 939 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS); 940 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS); 941 } 942 case ISD::SETUGE: 943 case ISD::SETUGT: { 944 if (LHS == True) 945 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS); 946 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS); 947 } 948 case ISD::SETGT: 949 case ISD::SETGE: 950 case ISD::SETOGE: 951 case ISD::SETOGT: { 952 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG && 953 !DCI.isCalledByLegalizer()) 954 return SDValue(); 955 956 if (LHS == True) 957 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS); 958 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS); 959 } 960 case ISD::SETCC_INVALID: 961 llvm_unreachable("Invalid setcc condcode!"); 962 } 963 return SDValue(); 964 } 965 966 std::pair<SDValue, SDValue> 967 AMDGPUTargetLowering::split64BitValue(SDValue Op, SelectionDAG &DAG) const { 968 SDLoc SL(Op); 969 970 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op); 971 972 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 973 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 974 975 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero); 976 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One); 977 978 return std::make_pair(Lo, Hi); 979 } 980 981 SDValue AMDGPUTargetLowering::getLoHalf64(SDValue Op, SelectionDAG &DAG) const { 982 SDLoc SL(Op); 983 984 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op); 985 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 986 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero); 987 } 988 989 SDValue AMDGPUTargetLowering::getHiHalf64(SDValue Op, SelectionDAG &DAG) const { 990 SDLoc SL(Op); 991 992 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op); 993 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 994 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One); 995 } 996 997 SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue Op, 998 SelectionDAG &DAG) const { 999 LoadSDNode *Load = cast<LoadSDNode>(Op); 1000 EVT VT = Op.getValueType(); 1001 1002 1003 // If this is a 2 element vector, we really want to scalarize and not create 1004 // weird 1 element vectors. 1005 if (VT.getVectorNumElements() == 2) 1006 return scalarizeVectorLoad(Load, DAG); 1007 1008 SDValue BasePtr = Load->getBasePtr(); 1009 EVT PtrVT = BasePtr.getValueType(); 1010 EVT MemVT = Load->getMemoryVT(); 1011 SDLoc SL(Op); 1012 1013 const MachinePointerInfo &SrcValue = Load->getMemOperand()->getPointerInfo(); 1014 1015 EVT LoVT, HiVT; 1016 EVT LoMemVT, HiMemVT; 1017 SDValue Lo, Hi; 1018 1019 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT); 1020 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT); 1021 std::tie(Lo, Hi) = DAG.SplitVector(Op, SL, LoVT, HiVT); 1022 1023 unsigned Size = LoMemVT.getStoreSize(); 1024 unsigned BaseAlign = Load->getAlignment(); 1025 unsigned HiAlign = MinAlign(BaseAlign, Size); 1026 1027 SDValue LoLoad 1028 = DAG.getExtLoad(Load->getExtensionType(), SL, LoVT, 1029 Load->getChain(), BasePtr, 1030 SrcValue, 1031 LoMemVT, Load->isVolatile(), Load->isNonTemporal(), 1032 Load->isInvariant(), BaseAlign); 1033 1034 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr, 1035 DAG.getConstant(Size, SL, PtrVT)); 1036 1037 SDValue HiLoad 1038 = DAG.getExtLoad(Load->getExtensionType(), SL, HiVT, 1039 Load->getChain(), HiPtr, 1040 SrcValue.getWithOffset(LoMemVT.getStoreSize()), 1041 HiMemVT, Load->isVolatile(), Load->isNonTemporal(), 1042 Load->isInvariant(), HiAlign); 1043 1044 SDValue Ops[] = { 1045 DAG.getNode(ISD::CONCAT_VECTORS, SL, VT, LoLoad, HiLoad), 1046 DAG.getNode(ISD::TokenFactor, SL, MVT::Other, 1047 LoLoad.getValue(1), HiLoad.getValue(1)) 1048 }; 1049 1050 return DAG.getMergeValues(Ops, SL); 1051 } 1052 1053 // FIXME: This isn't doing anything for SI. This should be used in a target 1054 // combine during type legalization. 1055 SDValue AMDGPUTargetLowering::MergeVectorStore(const SDValue &Op, 1056 SelectionDAG &DAG) const { 1057 StoreSDNode *Store = cast<StoreSDNode>(Op); 1058 EVT MemVT = Store->getMemoryVT(); 1059 unsigned MemBits = MemVT.getSizeInBits(); 1060 1061 // Byte stores are really expensive, so if possible, try to pack 32-bit vector 1062 // truncating store into an i32 store. 1063 // XXX: We could also handle optimize other vector bitwidths. 1064 if (!MemVT.isVector() || MemBits > 32) { 1065 return SDValue(); 1066 } 1067 1068 SDLoc DL(Op); 1069 SDValue Value = Store->getValue(); 1070 EVT VT = Value.getValueType(); 1071 EVT ElemVT = VT.getVectorElementType(); 1072 SDValue Ptr = Store->getBasePtr(); 1073 EVT MemEltVT = MemVT.getVectorElementType(); 1074 unsigned MemEltBits = MemEltVT.getSizeInBits(); 1075 unsigned MemNumElements = MemVT.getVectorNumElements(); 1076 unsigned PackedSize = MemVT.getStoreSizeInBits(); 1077 SDValue Mask = DAG.getConstant((1 << MemEltBits) - 1, DL, MVT::i32); 1078 1079 assert(Value.getValueType().getScalarSizeInBits() >= 32); 1080 1081 SDValue PackedValue; 1082 for (unsigned i = 0; i < MemNumElements; ++i) { 1083 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT, Value, 1084 DAG.getConstant(i, DL, MVT::i32)); 1085 Elt = DAG.getZExtOrTrunc(Elt, DL, MVT::i32); 1086 Elt = DAG.getNode(ISD::AND, DL, MVT::i32, Elt, Mask); // getZeroExtendInReg 1087 1088 SDValue Shift = DAG.getConstant(MemEltBits * i, DL, MVT::i32); 1089 Elt = DAG.getNode(ISD::SHL, DL, MVT::i32, Elt, Shift); 1090 1091 if (i == 0) { 1092 PackedValue = Elt; 1093 } else { 1094 PackedValue = DAG.getNode(ISD::OR, DL, MVT::i32, PackedValue, Elt); 1095 } 1096 } 1097 1098 if (PackedSize < 32) { 1099 EVT PackedVT = EVT::getIntegerVT(*DAG.getContext(), PackedSize); 1100 return DAG.getTruncStore(Store->getChain(), DL, PackedValue, Ptr, 1101 Store->getMemOperand()->getPointerInfo(), 1102 PackedVT, 1103 Store->isNonTemporal(), Store->isVolatile(), 1104 Store->getAlignment()); 1105 } 1106 1107 return DAG.getStore(Store->getChain(), DL, PackedValue, Ptr, 1108 Store->getMemOperand()->getPointerInfo(), 1109 Store->isVolatile(), Store->isNonTemporal(), 1110 Store->getAlignment()); 1111 } 1112 1113 SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op, 1114 SelectionDAG &DAG) const { 1115 StoreSDNode *Store = cast<StoreSDNode>(Op); 1116 SDValue Val = Store->getValue(); 1117 EVT VT = Val.getValueType(); 1118 1119 // If this is a 2 element vector, we really want to scalarize and not create 1120 // weird 1 element vectors. 1121 if (VT.getVectorNumElements() == 2) 1122 return scalarizeVectorStore(Store, DAG); 1123 1124 EVT MemVT = Store->getMemoryVT(); 1125 SDValue Chain = Store->getChain(); 1126 SDValue BasePtr = Store->getBasePtr(); 1127 SDLoc SL(Op); 1128 1129 EVT LoVT, HiVT; 1130 EVT LoMemVT, HiMemVT; 1131 SDValue Lo, Hi; 1132 1133 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT); 1134 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT); 1135 std::tie(Lo, Hi) = DAG.SplitVector(Val, SL, LoVT, HiVT); 1136 1137 EVT PtrVT = BasePtr.getValueType(); 1138 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr, 1139 DAG.getConstant(LoMemVT.getStoreSize(), SL, 1140 PtrVT)); 1141 1142 const MachinePointerInfo &SrcValue = Store->getMemOperand()->getPointerInfo(); 1143 unsigned BaseAlign = Store->getAlignment(); 1144 unsigned Size = LoMemVT.getStoreSize(); 1145 unsigned HiAlign = MinAlign(BaseAlign, Size); 1146 1147 SDValue LoStore 1148 = DAG.getTruncStore(Chain, SL, Lo, 1149 BasePtr, 1150 SrcValue, 1151 LoMemVT, 1152 Store->isNonTemporal(), 1153 Store->isVolatile(), 1154 BaseAlign); 1155 SDValue HiStore 1156 = DAG.getTruncStore(Chain, SL, Hi, 1157 HiPtr, 1158 SrcValue.getWithOffset(Size), 1159 HiMemVT, 1160 Store->isNonTemporal(), 1161 Store->isVolatile(), 1162 HiAlign); 1163 1164 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoStore, HiStore); 1165 } 1166 1167 // This is a shortcut for integer division because we have fast i32<->f32 1168 // conversions, and fast f32 reciprocal instructions. The fractional part of a 1169 // float is enough to accurately represent up to a 24-bit integer. 1170 SDValue AMDGPUTargetLowering::LowerDIVREM24(SDValue Op, SelectionDAG &DAG, bool sign) const { 1171 SDLoc DL(Op); 1172 EVT VT = Op.getValueType(); 1173 SDValue LHS = Op.getOperand(0); 1174 SDValue RHS = Op.getOperand(1); 1175 MVT IntVT = MVT::i32; 1176 MVT FltVT = MVT::f32; 1177 1178 ISD::NodeType ToFp = sign ? ISD::SINT_TO_FP : ISD::UINT_TO_FP; 1179 ISD::NodeType ToInt = sign ? ISD::FP_TO_SINT : ISD::FP_TO_UINT; 1180 1181 if (VT.isVector()) { 1182 unsigned NElts = VT.getVectorNumElements(); 1183 IntVT = MVT::getVectorVT(MVT::i32, NElts); 1184 FltVT = MVT::getVectorVT(MVT::f32, NElts); 1185 } 1186 1187 unsigned BitSize = VT.getScalarType().getSizeInBits(); 1188 1189 SDValue jq = DAG.getConstant(1, DL, IntVT); 1190 1191 if (sign) { 1192 // char|short jq = ia ^ ib; 1193 jq = DAG.getNode(ISD::XOR, DL, VT, LHS, RHS); 1194 1195 // jq = jq >> (bitsize - 2) 1196 jq = DAG.getNode(ISD::SRA, DL, VT, jq, 1197 DAG.getConstant(BitSize - 2, DL, VT)); 1198 1199 // jq = jq | 0x1 1200 jq = DAG.getNode(ISD::OR, DL, VT, jq, DAG.getConstant(1, DL, VT)); 1201 1202 // jq = (int)jq 1203 jq = DAG.getSExtOrTrunc(jq, DL, IntVT); 1204 } 1205 1206 // int ia = (int)LHS; 1207 SDValue ia = sign ? 1208 DAG.getSExtOrTrunc(LHS, DL, IntVT) : DAG.getZExtOrTrunc(LHS, DL, IntVT); 1209 1210 // int ib, (int)RHS; 1211 SDValue ib = sign ? 1212 DAG.getSExtOrTrunc(RHS, DL, IntVT) : DAG.getZExtOrTrunc(RHS, DL, IntVT); 1213 1214 // float fa = (float)ia; 1215 SDValue fa = DAG.getNode(ToFp, DL, FltVT, ia); 1216 1217 // float fb = (float)ib; 1218 SDValue fb = DAG.getNode(ToFp, DL, FltVT, ib); 1219 1220 // TODO: Should this propagate fast-math-flags? 1221 // float fq = native_divide(fa, fb); 1222 SDValue fq = DAG.getNode(ISD::FMUL, DL, FltVT, 1223 fa, DAG.getNode(AMDGPUISD::RCP, DL, FltVT, fb)); 1224 1225 // fq = trunc(fq); 1226 fq = DAG.getNode(ISD::FTRUNC, DL, FltVT, fq); 1227 1228 // float fqneg = -fq; 1229 SDValue fqneg = DAG.getNode(ISD::FNEG, DL, FltVT, fq); 1230 1231 // float fr = mad(fqneg, fb, fa); 1232 SDValue fr = DAG.getNode(ISD::FADD, DL, FltVT, 1233 DAG.getNode(ISD::FMUL, DL, FltVT, fqneg, fb), fa); 1234 1235 // int iq = (int)fq; 1236 SDValue iq = DAG.getNode(ToInt, DL, IntVT, fq); 1237 1238 // fr = fabs(fr); 1239 fr = DAG.getNode(ISD::FABS, DL, FltVT, fr); 1240 1241 // fb = fabs(fb); 1242 fb = DAG.getNode(ISD::FABS, DL, FltVT, fb); 1243 1244 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 1245 1246 // int cv = fr >= fb; 1247 SDValue cv = DAG.getSetCC(DL, SetCCVT, fr, fb, ISD::SETOGE); 1248 1249 // jq = (cv ? jq : 0); 1250 jq = DAG.getNode(ISD::SELECT, DL, VT, cv, jq, DAG.getConstant(0, DL, VT)); 1251 1252 // dst = trunc/extend to legal type 1253 iq = sign ? DAG.getSExtOrTrunc(iq, DL, VT) : DAG.getZExtOrTrunc(iq, DL, VT); 1254 1255 // dst = iq + jq; 1256 SDValue Div = DAG.getNode(ISD::ADD, DL, VT, iq, jq); 1257 1258 // Rem needs compensation, it's easier to recompute it 1259 SDValue Rem = DAG.getNode(ISD::MUL, DL, VT, Div, RHS); 1260 Rem = DAG.getNode(ISD::SUB, DL, VT, LHS, Rem); 1261 1262 SDValue Res[2] = { 1263 Div, 1264 Rem 1265 }; 1266 return DAG.getMergeValues(Res, DL); 1267 } 1268 1269 void AMDGPUTargetLowering::LowerUDIVREM64(SDValue Op, 1270 SelectionDAG &DAG, 1271 SmallVectorImpl<SDValue> &Results) const { 1272 assert(Op.getValueType() == MVT::i64); 1273 1274 SDLoc DL(Op); 1275 EVT VT = Op.getValueType(); 1276 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext()); 1277 1278 SDValue one = DAG.getConstant(1, DL, HalfVT); 1279 SDValue zero = DAG.getConstant(0, DL, HalfVT); 1280 1281 //HiLo split 1282 SDValue LHS = Op.getOperand(0); 1283 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero); 1284 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one); 1285 1286 SDValue RHS = Op.getOperand(1); 1287 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero); 1288 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one); 1289 1290 if (VT == MVT::i64 && 1291 DAG.MaskedValueIsZero(RHS, APInt::getHighBitsSet(64, 32)) && 1292 DAG.MaskedValueIsZero(LHS, APInt::getHighBitsSet(64, 32))) { 1293 1294 SDValue Res = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(HalfVT, HalfVT), 1295 LHS_Lo, RHS_Lo); 1296 1297 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(0), zero}); 1298 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(1), zero}); 1299 1300 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV)); 1301 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM)); 1302 return; 1303 } 1304 1305 // Get Speculative values 1306 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo); 1307 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo); 1308 1309 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ); 1310 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {REM_Lo, zero}); 1311 REM = DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM); 1312 1313 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ); 1314 SDValue DIV_Lo = zero; 1315 1316 const unsigned halfBitWidth = HalfVT.getSizeInBits(); 1317 1318 for (unsigned i = 0; i < halfBitWidth; ++i) { 1319 const unsigned bitPos = halfBitWidth - i - 1; 1320 SDValue POS = DAG.getConstant(bitPos, DL, HalfVT); 1321 // Get value of high bit 1322 SDValue HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS); 1323 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one); 1324 HBit = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, HBit); 1325 1326 // Shift 1327 REM = DAG.getNode(ISD::SHL, DL, VT, REM, DAG.getConstant(1, DL, VT)); 1328 // Add LHS high bit 1329 REM = DAG.getNode(ISD::OR, DL, VT, REM, HBit); 1330 1331 SDValue BIT = DAG.getConstant(1ULL << bitPos, DL, HalfVT); 1332 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETUGE); 1333 1334 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT); 1335 1336 // Update REM 1337 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS); 1338 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETUGE); 1339 } 1340 1341 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {DIV_Lo, DIV_Hi}); 1342 DIV = DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV); 1343 Results.push_back(DIV); 1344 Results.push_back(REM); 1345 } 1346 1347 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op, 1348 SelectionDAG &DAG) const { 1349 SDLoc DL(Op); 1350 EVT VT = Op.getValueType(); 1351 1352 if (VT == MVT::i64) { 1353 SmallVector<SDValue, 2> Results; 1354 LowerUDIVREM64(Op, DAG, Results); 1355 return DAG.getMergeValues(Results, DL); 1356 } 1357 1358 SDValue Num = Op.getOperand(0); 1359 SDValue Den = Op.getOperand(1); 1360 1361 if (VT == MVT::i32) { 1362 if (DAG.MaskedValueIsZero(Num, APInt::getHighBitsSet(32, 8)) && 1363 DAG.MaskedValueIsZero(Den, APInt::getHighBitsSet(32, 8))) { 1364 // TODO: We technically could do this for i64, but shouldn't that just be 1365 // handled by something generally reducing 64-bit division on 32-bit 1366 // values to 32-bit? 1367 return LowerDIVREM24(Op, DAG, false); 1368 } 1369 } 1370 1371 // RCP = URECIP(Den) = 2^32 / Den + e 1372 // e is rounding error. 1373 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den); 1374 1375 // RCP_LO = mul(RCP, Den) */ 1376 SDValue RCP_LO = DAG.getNode(ISD::MUL, DL, VT, RCP, Den); 1377 1378 // RCP_HI = mulhu (RCP, Den) */ 1379 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den); 1380 1381 // NEG_RCP_LO = -RCP_LO 1382 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), 1383 RCP_LO); 1384 1385 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO) 1386 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT), 1387 NEG_RCP_LO, RCP_LO, 1388 ISD::SETEQ); 1389 // Calculate the rounding error from the URECIP instruction 1390 // E = mulhu(ABS_RCP_LO, RCP) 1391 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP); 1392 1393 // RCP_A_E = RCP + E 1394 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E); 1395 1396 // RCP_S_E = RCP - E 1397 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E); 1398 1399 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E) 1400 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT), 1401 RCP_A_E, RCP_S_E, 1402 ISD::SETEQ); 1403 // Quotient = mulhu(Tmp0, Num) 1404 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num); 1405 1406 // Num_S_Remainder = Quotient * Den 1407 SDValue Num_S_Remainder = DAG.getNode(ISD::MUL, DL, VT, Quotient, Den); 1408 1409 // Remainder = Num - Num_S_Remainder 1410 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder); 1411 1412 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0) 1413 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den, 1414 DAG.getConstant(-1, DL, VT), 1415 DAG.getConstant(0, DL, VT), 1416 ISD::SETUGE); 1417 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0) 1418 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num, 1419 Num_S_Remainder, 1420 DAG.getConstant(-1, DL, VT), 1421 DAG.getConstant(0, DL, VT), 1422 ISD::SETUGE); 1423 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero 1424 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den, 1425 Remainder_GE_Zero); 1426 1427 // Calculate Division result: 1428 1429 // Quotient_A_One = Quotient + 1 1430 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient, 1431 DAG.getConstant(1, DL, VT)); 1432 1433 // Quotient_S_One = Quotient - 1 1434 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient, 1435 DAG.getConstant(1, DL, VT)); 1436 1437 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One) 1438 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT), 1439 Quotient, Quotient_A_One, ISD::SETEQ); 1440 1441 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div) 1442 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT), 1443 Quotient_S_One, Div, ISD::SETEQ); 1444 1445 // Calculate Rem result: 1446 1447 // Remainder_S_Den = Remainder - Den 1448 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den); 1449 1450 // Remainder_A_Den = Remainder + Den 1451 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den); 1452 1453 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den) 1454 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT), 1455 Remainder, Remainder_S_Den, ISD::SETEQ); 1456 1457 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem) 1458 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT), 1459 Remainder_A_Den, Rem, ISD::SETEQ); 1460 SDValue Ops[2] = { 1461 Div, 1462 Rem 1463 }; 1464 return DAG.getMergeValues(Ops, DL); 1465 } 1466 1467 SDValue AMDGPUTargetLowering::LowerSDIVREM(SDValue Op, 1468 SelectionDAG &DAG) const { 1469 SDLoc DL(Op); 1470 EVT VT = Op.getValueType(); 1471 1472 SDValue LHS = Op.getOperand(0); 1473 SDValue RHS = Op.getOperand(1); 1474 1475 SDValue Zero = DAG.getConstant(0, DL, VT); 1476 SDValue NegOne = DAG.getConstant(-1, DL, VT); 1477 1478 if (VT == MVT::i32 && 1479 DAG.ComputeNumSignBits(LHS) > 8 && 1480 DAG.ComputeNumSignBits(RHS) > 8) { 1481 return LowerDIVREM24(Op, DAG, true); 1482 } 1483 if (VT == MVT::i64 && 1484 DAG.ComputeNumSignBits(LHS) > 32 && 1485 DAG.ComputeNumSignBits(RHS) > 32) { 1486 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext()); 1487 1488 //HiLo split 1489 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, Zero); 1490 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, Zero); 1491 SDValue DIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(HalfVT, HalfVT), 1492 LHS_Lo, RHS_Lo); 1493 SDValue Res[2] = { 1494 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(0)), 1495 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(1)) 1496 }; 1497 return DAG.getMergeValues(Res, DL); 1498 } 1499 1500 SDValue LHSign = DAG.getSelectCC(DL, LHS, Zero, NegOne, Zero, ISD::SETLT); 1501 SDValue RHSign = DAG.getSelectCC(DL, RHS, Zero, NegOne, Zero, ISD::SETLT); 1502 SDValue DSign = DAG.getNode(ISD::XOR, DL, VT, LHSign, RHSign); 1503 SDValue RSign = LHSign; // Remainder sign is the same as LHS 1504 1505 LHS = DAG.getNode(ISD::ADD, DL, VT, LHS, LHSign); 1506 RHS = DAG.getNode(ISD::ADD, DL, VT, RHS, RHSign); 1507 1508 LHS = DAG.getNode(ISD::XOR, DL, VT, LHS, LHSign); 1509 RHS = DAG.getNode(ISD::XOR, DL, VT, RHS, RHSign); 1510 1511 SDValue Div = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT), LHS, RHS); 1512 SDValue Rem = Div.getValue(1); 1513 1514 Div = DAG.getNode(ISD::XOR, DL, VT, Div, DSign); 1515 Rem = DAG.getNode(ISD::XOR, DL, VT, Rem, RSign); 1516 1517 Div = DAG.getNode(ISD::SUB, DL, VT, Div, DSign); 1518 Rem = DAG.getNode(ISD::SUB, DL, VT, Rem, RSign); 1519 1520 SDValue Res[2] = { 1521 Div, 1522 Rem 1523 }; 1524 return DAG.getMergeValues(Res, DL); 1525 } 1526 1527 // (frem x, y) -> (fsub x, (fmul (ftrunc (fdiv x, y)), y)) 1528 SDValue AMDGPUTargetLowering::LowerFREM(SDValue Op, SelectionDAG &DAG) const { 1529 SDLoc SL(Op); 1530 EVT VT = Op.getValueType(); 1531 SDValue X = Op.getOperand(0); 1532 SDValue Y = Op.getOperand(1); 1533 1534 // TODO: Should this propagate fast-math-flags? 1535 1536 SDValue Div = DAG.getNode(ISD::FDIV, SL, VT, X, Y); 1537 SDValue Floor = DAG.getNode(ISD::FTRUNC, SL, VT, Div); 1538 SDValue Mul = DAG.getNode(ISD::FMUL, SL, VT, Floor, Y); 1539 1540 return DAG.getNode(ISD::FSUB, SL, VT, X, Mul); 1541 } 1542 1543 SDValue AMDGPUTargetLowering::LowerFCEIL(SDValue Op, SelectionDAG &DAG) const { 1544 SDLoc SL(Op); 1545 SDValue Src = Op.getOperand(0); 1546 1547 // result = trunc(src) 1548 // if (src > 0.0 && src != result) 1549 // result += 1.0 1550 1551 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src); 1552 1553 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64); 1554 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64); 1555 1556 EVT SetCCVT = 1557 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64); 1558 1559 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOGT); 1560 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE); 1561 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc); 1562 1563 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, One, Zero); 1564 // TODO: Should this propagate fast-math-flags? 1565 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add); 1566 } 1567 1568 static SDValue extractF64Exponent(SDValue Hi, SDLoc SL, SelectionDAG &DAG) { 1569 const unsigned FractBits = 52; 1570 const unsigned ExpBits = 11; 1571 1572 SDValue ExpPart = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32, 1573 Hi, 1574 DAG.getConstant(FractBits - 32, SL, MVT::i32), 1575 DAG.getConstant(ExpBits, SL, MVT::i32)); 1576 SDValue Exp = DAG.getNode(ISD::SUB, SL, MVT::i32, ExpPart, 1577 DAG.getConstant(1023, SL, MVT::i32)); 1578 1579 return Exp; 1580 } 1581 1582 SDValue AMDGPUTargetLowering::LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const { 1583 SDLoc SL(Op); 1584 SDValue Src = Op.getOperand(0); 1585 1586 assert(Op.getValueType() == MVT::f64); 1587 1588 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 1589 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 1590 1591 SDValue VecSrc = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src); 1592 1593 // Extract the upper half, since this is where we will find the sign and 1594 // exponent. 1595 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecSrc, One); 1596 1597 SDValue Exp = extractF64Exponent(Hi, SL, DAG); 1598 1599 const unsigned FractBits = 52; 1600 1601 // Extract the sign bit. 1602 const SDValue SignBitMask = DAG.getConstant(UINT32_C(1) << 31, SL, MVT::i32); 1603 SDValue SignBit = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, SignBitMask); 1604 1605 // Extend back to to 64-bits. 1606 SDValue SignBit64 = DAG.getBuildVector(MVT::v2i32, SL, {Zero, SignBit}); 1607 SignBit64 = DAG.getNode(ISD::BITCAST, SL, MVT::i64, SignBit64); 1608 1609 SDValue BcInt = DAG.getNode(ISD::BITCAST, SL, MVT::i64, Src); 1610 const SDValue FractMask 1611 = DAG.getConstant((UINT64_C(1) << FractBits) - 1, SL, MVT::i64); 1612 1613 SDValue Shr = DAG.getNode(ISD::SRA, SL, MVT::i64, FractMask, Exp); 1614 SDValue Not = DAG.getNOT(SL, Shr, MVT::i64); 1615 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, BcInt, Not); 1616 1617 EVT SetCCVT = 1618 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32); 1619 1620 const SDValue FiftyOne = DAG.getConstant(FractBits - 1, SL, MVT::i32); 1621 1622 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT); 1623 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT); 1624 1625 SDValue Tmp1 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpLt0, SignBit64, Tmp0); 1626 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpGt51, BcInt, Tmp1); 1627 1628 return DAG.getNode(ISD::BITCAST, SL, MVT::f64, Tmp2); 1629 } 1630 1631 SDValue AMDGPUTargetLowering::LowerFRINT(SDValue Op, SelectionDAG &DAG) const { 1632 SDLoc SL(Op); 1633 SDValue Src = Op.getOperand(0); 1634 1635 assert(Op.getValueType() == MVT::f64); 1636 1637 APFloat C1Val(APFloat::IEEEdouble, "0x1.0p+52"); 1638 SDValue C1 = DAG.getConstantFP(C1Val, SL, MVT::f64); 1639 SDValue CopySign = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, C1, Src); 1640 1641 // TODO: Should this propagate fast-math-flags? 1642 1643 SDValue Tmp1 = DAG.getNode(ISD::FADD, SL, MVT::f64, Src, CopySign); 1644 SDValue Tmp2 = DAG.getNode(ISD::FSUB, SL, MVT::f64, Tmp1, CopySign); 1645 1646 SDValue Fabs = DAG.getNode(ISD::FABS, SL, MVT::f64, Src); 1647 1648 APFloat C2Val(APFloat::IEEEdouble, "0x1.fffffffffffffp+51"); 1649 SDValue C2 = DAG.getConstantFP(C2Val, SL, MVT::f64); 1650 1651 EVT SetCCVT = 1652 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64); 1653 SDValue Cond = DAG.getSetCC(SL, SetCCVT, Fabs, C2, ISD::SETOGT); 1654 1655 return DAG.getSelect(SL, MVT::f64, Cond, Src, Tmp2); 1656 } 1657 1658 SDValue AMDGPUTargetLowering::LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const { 1659 // FNEARBYINT and FRINT are the same, except in their handling of FP 1660 // exceptions. Those aren't really meaningful for us, and OpenCL only has 1661 // rint, so just treat them as equivalent. 1662 return DAG.getNode(ISD::FRINT, SDLoc(Op), Op.getValueType(), Op.getOperand(0)); 1663 } 1664 1665 // XXX - May require not supporting f32 denormals? 1666 SDValue AMDGPUTargetLowering::LowerFROUND32(SDValue Op, SelectionDAG &DAG) const { 1667 SDLoc SL(Op); 1668 SDValue X = Op.getOperand(0); 1669 1670 SDValue T = DAG.getNode(ISD::FTRUNC, SL, MVT::f32, X); 1671 1672 // TODO: Should this propagate fast-math-flags? 1673 1674 SDValue Diff = DAG.getNode(ISD::FSUB, SL, MVT::f32, X, T); 1675 1676 SDValue AbsDiff = DAG.getNode(ISD::FABS, SL, MVT::f32, Diff); 1677 1678 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f32); 1679 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); 1680 const SDValue Half = DAG.getConstantFP(0.5, SL, MVT::f32); 1681 1682 SDValue SignOne = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f32, One, X); 1683 1684 EVT SetCCVT = 1685 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32); 1686 1687 SDValue Cmp = DAG.getSetCC(SL, SetCCVT, AbsDiff, Half, ISD::SETOGE); 1688 1689 SDValue Sel = DAG.getNode(ISD::SELECT, SL, MVT::f32, Cmp, SignOne, Zero); 1690 1691 return DAG.getNode(ISD::FADD, SL, MVT::f32, T, Sel); 1692 } 1693 1694 SDValue AMDGPUTargetLowering::LowerFROUND64(SDValue Op, SelectionDAG &DAG) const { 1695 SDLoc SL(Op); 1696 SDValue X = Op.getOperand(0); 1697 1698 SDValue L = DAG.getNode(ISD::BITCAST, SL, MVT::i64, X); 1699 1700 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 1701 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 1702 const SDValue NegOne = DAG.getConstant(-1, SL, MVT::i32); 1703 const SDValue FiftyOne = DAG.getConstant(51, SL, MVT::i32); 1704 EVT SetCCVT = 1705 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32); 1706 1707 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X); 1708 1709 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, One); 1710 1711 SDValue Exp = extractF64Exponent(Hi, SL, DAG); 1712 1713 const SDValue Mask = DAG.getConstant(INT64_C(0x000fffffffffffff), SL, 1714 MVT::i64); 1715 1716 SDValue M = DAG.getNode(ISD::SRA, SL, MVT::i64, Mask, Exp); 1717 SDValue D = DAG.getNode(ISD::SRA, SL, MVT::i64, 1718 DAG.getConstant(INT64_C(0x0008000000000000), SL, 1719 MVT::i64), 1720 Exp); 1721 1722 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, L, M); 1723 SDValue Tmp1 = DAG.getSetCC(SL, SetCCVT, 1724 DAG.getConstant(0, SL, MVT::i64), Tmp0, 1725 ISD::SETNE); 1726 1727 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, Tmp1, 1728 D, DAG.getConstant(0, SL, MVT::i64)); 1729 SDValue K = DAG.getNode(ISD::ADD, SL, MVT::i64, L, Tmp2); 1730 1731 K = DAG.getNode(ISD::AND, SL, MVT::i64, K, DAG.getNOT(SL, M, MVT::i64)); 1732 K = DAG.getNode(ISD::BITCAST, SL, MVT::f64, K); 1733 1734 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT); 1735 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT); 1736 SDValue ExpEqNegOne = DAG.getSetCC(SL, SetCCVT, NegOne, Exp, ISD::SETEQ); 1737 1738 SDValue Mag = DAG.getNode(ISD::SELECT, SL, MVT::f64, 1739 ExpEqNegOne, 1740 DAG.getConstantFP(1.0, SL, MVT::f64), 1741 DAG.getConstantFP(0.0, SL, MVT::f64)); 1742 1743 SDValue S = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, Mag, X); 1744 1745 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpLt0, S, K); 1746 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpGt51, X, K); 1747 1748 return K; 1749 } 1750 1751 SDValue AMDGPUTargetLowering::LowerFROUND(SDValue Op, SelectionDAG &DAG) const { 1752 EVT VT = Op.getValueType(); 1753 1754 if (VT == MVT::f32) 1755 return LowerFROUND32(Op, DAG); 1756 1757 if (VT == MVT::f64) 1758 return LowerFROUND64(Op, DAG); 1759 1760 llvm_unreachable("unhandled type"); 1761 } 1762 1763 SDValue AMDGPUTargetLowering::LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const { 1764 SDLoc SL(Op); 1765 SDValue Src = Op.getOperand(0); 1766 1767 // result = trunc(src); 1768 // if (src < 0.0 && src != result) 1769 // result += -1.0. 1770 1771 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src); 1772 1773 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64); 1774 const SDValue NegOne = DAG.getConstantFP(-1.0, SL, MVT::f64); 1775 1776 EVT SetCCVT = 1777 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64); 1778 1779 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOLT); 1780 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE); 1781 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc); 1782 1783 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, NegOne, Zero); 1784 // TODO: Should this propagate fast-math-flags? 1785 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add); 1786 } 1787 1788 SDValue AMDGPUTargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const { 1789 SDLoc SL(Op); 1790 SDValue Src = Op.getOperand(0); 1791 bool ZeroUndef = Op.getOpcode() == ISD::CTLZ_ZERO_UNDEF; 1792 1793 if (ZeroUndef && Src.getValueType() == MVT::i32) 1794 return DAG.getNode(AMDGPUISD::FFBH_U32, SL, MVT::i32, Src); 1795 1796 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src); 1797 1798 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 1799 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 1800 1801 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero); 1802 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One); 1803 1804 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), 1805 *DAG.getContext(), MVT::i32); 1806 1807 SDValue Hi0 = DAG.getSetCC(SL, SetCCVT, Hi, Zero, ISD::SETEQ); 1808 1809 SDValue CtlzLo = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Lo); 1810 SDValue CtlzHi = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Hi); 1811 1812 const SDValue Bits32 = DAG.getConstant(32, SL, MVT::i32); 1813 SDValue Add = DAG.getNode(ISD::ADD, SL, MVT::i32, CtlzLo, Bits32); 1814 1815 // ctlz(x) = hi_32(x) == 0 ? ctlz(lo_32(x)) + 32 : ctlz(hi_32(x)) 1816 SDValue NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32, Hi0, Add, CtlzHi); 1817 1818 if (!ZeroUndef) { 1819 // Test if the full 64-bit input is zero. 1820 1821 // FIXME: DAG combines turn what should be an s_and_b64 into a v_or_b32, 1822 // which we probably don't want. 1823 SDValue Lo0 = DAG.getSetCC(SL, SetCCVT, Lo, Zero, ISD::SETEQ); 1824 SDValue SrcIsZero = DAG.getNode(ISD::AND, SL, SetCCVT, Lo0, Hi0); 1825 1826 // TODO: If i64 setcc is half rate, it can result in 1 fewer instruction 1827 // with the same cycles, otherwise it is slower. 1828 // SDValue SrcIsZero = DAG.getSetCC(SL, SetCCVT, Src, 1829 // DAG.getConstant(0, SL, MVT::i64), ISD::SETEQ); 1830 1831 const SDValue Bits32 = DAG.getConstant(64, SL, MVT::i32); 1832 1833 // The instruction returns -1 for 0 input, but the defined intrinsic 1834 // behavior is to return the number of bits. 1835 NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32, 1836 SrcIsZero, Bits32, NewCtlz); 1837 } 1838 1839 return DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i64, NewCtlz); 1840 } 1841 1842 SDValue AMDGPUTargetLowering::LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG, 1843 bool Signed) const { 1844 // Unsigned 1845 // cul2f(ulong u) 1846 //{ 1847 // uint lz = clz(u); 1848 // uint e = (u != 0) ? 127U + 63U - lz : 0; 1849 // u = (u << lz) & 0x7fffffffffffffffUL; 1850 // ulong t = u & 0xffffffffffUL; 1851 // uint v = (e << 23) | (uint)(u >> 40); 1852 // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U); 1853 // return as_float(v + r); 1854 //} 1855 // Signed 1856 // cl2f(long l) 1857 //{ 1858 // long s = l >> 63; 1859 // float r = cul2f((l + s) ^ s); 1860 // return s ? -r : r; 1861 //} 1862 1863 SDLoc SL(Op); 1864 SDValue Src = Op.getOperand(0); 1865 SDValue L = Src; 1866 1867 SDValue S; 1868 if (Signed) { 1869 const SDValue SignBit = DAG.getConstant(63, SL, MVT::i64); 1870 S = DAG.getNode(ISD::SRA, SL, MVT::i64, L, SignBit); 1871 1872 SDValue LPlusS = DAG.getNode(ISD::ADD, SL, MVT::i64, L, S); 1873 L = DAG.getNode(ISD::XOR, SL, MVT::i64, LPlusS, S); 1874 } 1875 1876 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), 1877 *DAG.getContext(), MVT::f32); 1878 1879 1880 SDValue ZeroI32 = DAG.getConstant(0, SL, MVT::i32); 1881 SDValue ZeroI64 = DAG.getConstant(0, SL, MVT::i64); 1882 SDValue LZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i64, L); 1883 LZ = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LZ); 1884 1885 SDValue K = DAG.getConstant(127U + 63U, SL, MVT::i32); 1886 SDValue E = DAG.getSelect(SL, MVT::i32, 1887 DAG.getSetCC(SL, SetCCVT, L, ZeroI64, ISD::SETNE), 1888 DAG.getNode(ISD::SUB, SL, MVT::i32, K, LZ), 1889 ZeroI32); 1890 1891 SDValue U = DAG.getNode(ISD::AND, SL, MVT::i64, 1892 DAG.getNode(ISD::SHL, SL, MVT::i64, L, LZ), 1893 DAG.getConstant((-1ULL) >> 1, SL, MVT::i64)); 1894 1895 SDValue T = DAG.getNode(ISD::AND, SL, MVT::i64, U, 1896 DAG.getConstant(0xffffffffffULL, SL, MVT::i64)); 1897 1898 SDValue UShl = DAG.getNode(ISD::SRL, SL, MVT::i64, 1899 U, DAG.getConstant(40, SL, MVT::i64)); 1900 1901 SDValue V = DAG.getNode(ISD::OR, SL, MVT::i32, 1902 DAG.getNode(ISD::SHL, SL, MVT::i32, E, DAG.getConstant(23, SL, MVT::i32)), 1903 DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, UShl)); 1904 1905 SDValue C = DAG.getConstant(0x8000000000ULL, SL, MVT::i64); 1906 SDValue RCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETUGT); 1907 SDValue TCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETEQ); 1908 1909 SDValue One = DAG.getConstant(1, SL, MVT::i32); 1910 1911 SDValue VTrunc1 = DAG.getNode(ISD::AND, SL, MVT::i32, V, One); 1912 1913 SDValue R = DAG.getSelect(SL, MVT::i32, 1914 RCmp, 1915 One, 1916 DAG.getSelect(SL, MVT::i32, TCmp, VTrunc1, ZeroI32)); 1917 R = DAG.getNode(ISD::ADD, SL, MVT::i32, V, R); 1918 R = DAG.getNode(ISD::BITCAST, SL, MVT::f32, R); 1919 1920 if (!Signed) 1921 return R; 1922 1923 SDValue RNeg = DAG.getNode(ISD::FNEG, SL, MVT::f32, R); 1924 return DAG.getSelect(SL, MVT::f32, DAG.getSExtOrTrunc(S, SL, SetCCVT), RNeg, R); 1925 } 1926 1927 SDValue AMDGPUTargetLowering::LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG, 1928 bool Signed) const { 1929 SDLoc SL(Op); 1930 SDValue Src = Op.getOperand(0); 1931 1932 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src); 1933 1934 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, 1935 DAG.getConstant(0, SL, MVT::i32)); 1936 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, 1937 DAG.getConstant(1, SL, MVT::i32)); 1938 1939 SDValue CvtHi = DAG.getNode(Signed ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, 1940 SL, MVT::f64, Hi); 1941 1942 SDValue CvtLo = DAG.getNode(ISD::UINT_TO_FP, SL, MVT::f64, Lo); 1943 1944 SDValue LdExp = DAG.getNode(AMDGPUISD::LDEXP, SL, MVT::f64, CvtHi, 1945 DAG.getConstant(32, SL, MVT::i32)); 1946 // TODO: Should this propagate fast-math-flags? 1947 return DAG.getNode(ISD::FADD, SL, MVT::f64, LdExp, CvtLo); 1948 } 1949 1950 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op, 1951 SelectionDAG &DAG) const { 1952 assert(Op.getOperand(0).getValueType() == MVT::i64 && 1953 "operation should be legal"); 1954 1955 EVT DestVT = Op.getValueType(); 1956 if (DestVT == MVT::f64) 1957 return LowerINT_TO_FP64(Op, DAG, false); 1958 1959 if (DestVT == MVT::f32) 1960 return LowerINT_TO_FP32(Op, DAG, false); 1961 1962 return SDValue(); 1963 } 1964 1965 SDValue AMDGPUTargetLowering::LowerSINT_TO_FP(SDValue Op, 1966 SelectionDAG &DAG) const { 1967 assert(Op.getOperand(0).getValueType() == MVT::i64 && 1968 "operation should be legal"); 1969 1970 EVT DestVT = Op.getValueType(); 1971 if (DestVT == MVT::f32) 1972 return LowerINT_TO_FP32(Op, DAG, true); 1973 1974 if (DestVT == MVT::f64) 1975 return LowerINT_TO_FP64(Op, DAG, true); 1976 1977 return SDValue(); 1978 } 1979 1980 SDValue AMDGPUTargetLowering::LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG, 1981 bool Signed) const { 1982 SDLoc SL(Op); 1983 1984 SDValue Src = Op.getOperand(0); 1985 1986 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src); 1987 1988 SDValue K0 = DAG.getConstantFP(BitsToDouble(UINT64_C(0x3df0000000000000)), SL, 1989 MVT::f64); 1990 SDValue K1 = DAG.getConstantFP(BitsToDouble(UINT64_C(0xc1f0000000000000)), SL, 1991 MVT::f64); 1992 // TODO: Should this propagate fast-math-flags? 1993 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, Trunc, K0); 1994 1995 SDValue FloorMul = DAG.getNode(ISD::FFLOOR, SL, MVT::f64, Mul); 1996 1997 1998 SDValue Fma = DAG.getNode(ISD::FMA, SL, MVT::f64, FloorMul, K1, Trunc); 1999 2000 SDValue Hi = DAG.getNode(Signed ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, SL, 2001 MVT::i32, FloorMul); 2002 SDValue Lo = DAG.getNode(ISD::FP_TO_UINT, SL, MVT::i32, Fma); 2003 2004 SDValue Result = DAG.getBuildVector(MVT::v2i32, SL, {Lo, Hi}); 2005 2006 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Result); 2007 } 2008 2009 SDValue AMDGPUTargetLowering::LowerFP_TO_SINT(SDValue Op, 2010 SelectionDAG &DAG) const { 2011 SDValue Src = Op.getOperand(0); 2012 2013 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64) 2014 return LowerFP64_TO_INT(Op, DAG, true); 2015 2016 return SDValue(); 2017 } 2018 2019 SDValue AMDGPUTargetLowering::LowerFP_TO_UINT(SDValue Op, 2020 SelectionDAG &DAG) const { 2021 SDValue Src = Op.getOperand(0); 2022 2023 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64) 2024 return LowerFP64_TO_INT(Op, DAG, false); 2025 2026 return SDValue(); 2027 } 2028 2029 SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, 2030 SelectionDAG &DAG) const { 2031 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 2032 MVT VT = Op.getSimpleValueType(); 2033 MVT ScalarVT = VT.getScalarType(); 2034 2035 if (!VT.isVector()) 2036 return SDValue(); 2037 2038 SDValue Src = Op.getOperand(0); 2039 SDLoc DL(Op); 2040 2041 // TODO: Don't scalarize on Evergreen? 2042 unsigned NElts = VT.getVectorNumElements(); 2043 SmallVector<SDValue, 8> Args; 2044 DAG.ExtractVectorElements(Src, Args, 0, NElts); 2045 2046 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType()); 2047 for (unsigned I = 0; I < NElts; ++I) 2048 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp); 2049 2050 return DAG.getBuildVector(VT, DL, Args); 2051 } 2052 2053 //===----------------------------------------------------------------------===// 2054 // Custom DAG optimizations 2055 //===----------------------------------------------------------------------===// 2056 2057 static bool isU24(SDValue Op, SelectionDAG &DAG) { 2058 APInt KnownZero, KnownOne; 2059 EVT VT = Op.getValueType(); 2060 DAG.computeKnownBits(Op, KnownZero, KnownOne); 2061 2062 return (VT.getSizeInBits() - KnownZero.countLeadingOnes()) <= 24; 2063 } 2064 2065 static bool isI24(SDValue Op, SelectionDAG &DAG) { 2066 EVT VT = Op.getValueType(); 2067 2068 // In order for this to be a signed 24-bit value, bit 23, must 2069 // be a sign bit. 2070 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated 2071 // as unsigned 24-bit values. 2072 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24; 2073 } 2074 2075 static void simplifyI24(SDValue Op, TargetLowering::DAGCombinerInfo &DCI) { 2076 2077 SelectionDAG &DAG = DCI.DAG; 2078 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 2079 EVT VT = Op.getValueType(); 2080 2081 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24); 2082 APInt KnownZero, KnownOne; 2083 TargetLowering::TargetLoweringOpt TLO(DAG, true, true); 2084 if (TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) 2085 DCI.CommitTargetLoweringOpt(TLO); 2086 } 2087 2088 template <typename IntTy> 2089 static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0, 2090 uint32_t Offset, uint32_t Width, SDLoc DL) { 2091 if (Width + Offset < 32) { 2092 uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width); 2093 IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width); 2094 return DAG.getConstant(Result, DL, MVT::i32); 2095 } 2096 2097 return DAG.getConstant(Src0 >> Offset, DL, MVT::i32); 2098 } 2099 2100 static bool usesAllNormalStores(SDNode *LoadVal) { 2101 for (SDNode::use_iterator I = LoadVal->use_begin(); !I.atEnd(); ++I) { 2102 if (!ISD::isNormalStore(*I)) 2103 return false; 2104 } 2105 2106 return true; 2107 } 2108 2109 // If we have a copy of an illegal type, replace it with a load / store of an 2110 // equivalently sized legal type. This avoids intermediate bit pack / unpack 2111 // instructions emitted when handling extloads and truncstores. Ideally we could 2112 // recognize the pack / unpack pattern to eliminate it. 2113 SDValue AMDGPUTargetLowering::performStoreCombine(SDNode *N, 2114 DAGCombinerInfo &DCI) const { 2115 if (!DCI.isBeforeLegalize()) 2116 return SDValue(); 2117 2118 StoreSDNode *SN = cast<StoreSDNode>(N); 2119 SDValue Value = SN->getValue(); 2120 EVT VT = Value.getValueType(); 2121 2122 if (isTypeLegal(VT) || SN->isVolatile() || 2123 !ISD::isNormalLoad(Value.getNode()) || VT.getSizeInBits() < 8) 2124 return SDValue(); 2125 2126 LoadSDNode *LoadVal = cast<LoadSDNode>(Value); 2127 if (LoadVal->isVolatile() || !usesAllNormalStores(LoadVal)) 2128 return SDValue(); 2129 2130 EVT MemVT = LoadVal->getMemoryVT(); 2131 2132 SDLoc SL(N); 2133 SelectionDAG &DAG = DCI.DAG; 2134 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), MemVT); 2135 2136 SDValue NewLoad = DAG.getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, 2137 LoadVT, SL, 2138 LoadVal->getChain(), 2139 LoadVal->getBasePtr(), 2140 LoadVal->getOffset(), 2141 LoadVT, 2142 LoadVal->getMemOperand()); 2143 2144 SDValue CastLoad = DAG.getNode(ISD::BITCAST, SL, VT, NewLoad.getValue(0)); 2145 DCI.CombineTo(LoadVal, CastLoad, NewLoad.getValue(1), false); 2146 2147 return DAG.getStore(SN->getChain(), SL, NewLoad, 2148 SN->getBasePtr(), SN->getMemOperand()); 2149 } 2150 2151 // TODO: Should repeat for other bit ops. 2152 SDValue AMDGPUTargetLowering::performAndCombine(SDNode *N, 2153 DAGCombinerInfo &DCI) const { 2154 if (N->getValueType(0) != MVT::i64) 2155 return SDValue(); 2156 2157 // Break up 64-bit and of a constant into two 32-bit ands. This will typically 2158 // happen anyway for a VALU 64-bit and. This exposes other 32-bit integer 2159 // combine opportunities since most 64-bit operations are decomposed this way. 2160 // TODO: We won't want this for SALU especially if it is an inline immediate. 2161 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2162 if (!RHS) 2163 return SDValue(); 2164 2165 uint64_t Val = RHS->getZExtValue(); 2166 if (Lo_32(Val) != 0 && Hi_32(Val) != 0 && !RHS->hasOneUse()) { 2167 // If either half of the constant is 0, this is really a 32-bit and, so 2168 // split it. If we can re-use the full materialized constant, keep it. 2169 return SDValue(); 2170 } 2171 2172 SDLoc SL(N); 2173 SelectionDAG &DAG = DCI.DAG; 2174 2175 SDValue Lo, Hi; 2176 std::tie(Lo, Hi) = split64BitValue(N->getOperand(0), DAG); 2177 2178 SDValue LoRHS = DAG.getConstant(Lo_32(Val), SL, MVT::i32); 2179 SDValue HiRHS = DAG.getConstant(Hi_32(Val), SL, MVT::i32); 2180 2181 SDValue LoAnd = DAG.getNode(ISD::AND, SL, MVT::i32, Lo, LoRHS); 2182 SDValue HiAnd = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, HiRHS); 2183 2184 // Re-visit the ands. It's possible we eliminated one of them and it could 2185 // simplify the vector. 2186 DCI.AddToWorklist(Lo.getNode()); 2187 DCI.AddToWorklist(Hi.getNode()); 2188 2189 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {LoAnd, HiAnd}); 2190 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 2191 } 2192 2193 SDValue AMDGPUTargetLowering::performShlCombine(SDNode *N, 2194 DAGCombinerInfo &DCI) const { 2195 if (N->getValueType(0) != MVT::i64) 2196 return SDValue(); 2197 2198 // i64 (shl x, C) -> (build_pair 0, (shl x, C -32)) 2199 2200 // On some subtargets, 64-bit shift is a quarter rate instruction. In the 2201 // common case, splitting this into a move and a 32-bit shift is faster and 2202 // the same code size. 2203 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2204 if (!RHS) 2205 return SDValue(); 2206 2207 unsigned RHSVal = RHS->getZExtValue(); 2208 if (RHSVal < 32) 2209 return SDValue(); 2210 2211 SDValue LHS = N->getOperand(0); 2212 2213 SDLoc SL(N); 2214 SelectionDAG &DAG = DCI.DAG; 2215 2216 SDValue ShiftAmt = DAG.getConstant(RHSVal - 32, SL, MVT::i32); 2217 2218 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS); 2219 SDValue NewShift = DAG.getNode(ISD::SHL, SL, MVT::i32, Lo, ShiftAmt); 2220 2221 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 2222 2223 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {Zero, NewShift}); 2224 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 2225 } 2226 2227 SDValue AMDGPUTargetLowering::performSraCombine(SDNode *N, 2228 DAGCombinerInfo &DCI) const { 2229 if (N->getValueType(0) != MVT::i64) 2230 return SDValue(); 2231 2232 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2233 if (!RHS) 2234 return SDValue(); 2235 2236 SelectionDAG &DAG = DCI.DAG; 2237 SDLoc SL(N); 2238 unsigned RHSVal = RHS->getZExtValue(); 2239 2240 // (sra i64:x, 32) -> build_pair x, (sra hi_32(x), 31) 2241 if (RHSVal == 32) { 2242 SDValue Hi = getHiHalf64(N->getOperand(0), DAG); 2243 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi, 2244 DAG.getConstant(31, SL, MVT::i32)); 2245 2246 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {Hi, NewShift}); 2247 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec); 2248 } 2249 2250 // (sra i64:x, 63) -> build_pair (sra hi_32(x), 31), (sra hi_32(x), 31) 2251 if (RHSVal == 63) { 2252 SDValue Hi = getHiHalf64(N->getOperand(0), DAG); 2253 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi, 2254 DAG.getConstant(31, SL, MVT::i32)); 2255 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, NewShift}); 2256 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec); 2257 } 2258 2259 return SDValue(); 2260 } 2261 2262 SDValue AMDGPUTargetLowering::performSrlCombine(SDNode *N, 2263 DAGCombinerInfo &DCI) const { 2264 if (N->getValueType(0) != MVT::i64) 2265 return SDValue(); 2266 2267 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2268 if (!RHS) 2269 return SDValue(); 2270 2271 unsigned ShiftAmt = RHS->getZExtValue(); 2272 if (ShiftAmt < 32) 2273 return SDValue(); 2274 2275 // srl i64:x, C for C >= 32 2276 // => 2277 // build_pair (srl hi_32(x), C - 32), 0 2278 2279 SelectionDAG &DAG = DCI.DAG; 2280 SDLoc SL(N); 2281 2282 SDValue One = DAG.getConstant(1, SL, MVT::i32); 2283 SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 2284 2285 SDValue VecOp = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, N->getOperand(0)); 2286 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, 2287 VecOp, One); 2288 2289 SDValue NewConst = DAG.getConstant(ShiftAmt - 32, SL, MVT::i32); 2290 SDValue NewShift = DAG.getNode(ISD::SRL, SL, MVT::i32, Hi, NewConst); 2291 2292 SDValue BuildPair = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, Zero}); 2293 2294 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildPair); 2295 } 2296 2297 SDValue AMDGPUTargetLowering::performMulCombine(SDNode *N, 2298 DAGCombinerInfo &DCI) const { 2299 EVT VT = N->getValueType(0); 2300 2301 if (VT.isVector() || VT.getSizeInBits() > 32) 2302 return SDValue(); 2303 2304 SelectionDAG &DAG = DCI.DAG; 2305 SDLoc DL(N); 2306 2307 SDValue N0 = N->getOperand(0); 2308 SDValue N1 = N->getOperand(1); 2309 SDValue Mul; 2310 2311 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) { 2312 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32); 2313 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32); 2314 Mul = DAG.getNode(AMDGPUISD::MUL_U24, DL, MVT::i32, N0, N1); 2315 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) { 2316 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32); 2317 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32); 2318 Mul = DAG.getNode(AMDGPUISD::MUL_I24, DL, MVT::i32, N0, N1); 2319 } else { 2320 return SDValue(); 2321 } 2322 2323 // We need to use sext even for MUL_U24, because MUL_U24 is used 2324 // for signed multiply of 8 and 16-bit types. 2325 return DAG.getSExtOrTrunc(Mul, DL, VT); 2326 } 2327 2328 static bool isNegativeOne(SDValue Val) { 2329 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) 2330 return C->isAllOnesValue(); 2331 return false; 2332 } 2333 2334 static bool isCtlzOpc(unsigned Opc) { 2335 return Opc == ISD::CTLZ || Opc == ISD::CTLZ_ZERO_UNDEF; 2336 } 2337 2338 // Get FFBH node if the incoming op may have been type legalized from a smaller 2339 // type VT. 2340 // Need to match pre-legalized type because the generic legalization inserts the 2341 // add/sub between the select and compare. 2342 static SDValue getFFBH_U32(const TargetLowering &TLI, 2343 SelectionDAG &DAG, SDLoc SL, SDValue Op) { 2344 EVT VT = Op.getValueType(); 2345 EVT LegalVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT); 2346 if (LegalVT != MVT::i32) 2347 return SDValue(); 2348 2349 if (VT != MVT::i32) 2350 Op = DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i32, Op); 2351 2352 SDValue FFBH = DAG.getNode(AMDGPUISD::FFBH_U32, SL, MVT::i32, Op); 2353 if (VT != MVT::i32) 2354 FFBH = DAG.getNode(ISD::TRUNCATE, SL, VT, FFBH); 2355 2356 return FFBH; 2357 } 2358 2359 // The native instructions return -1 on 0 input. Optimize out a select that 2360 // produces -1 on 0. 2361 // 2362 // TODO: If zero is not undef, we could also do this if the output is compared 2363 // against the bitwidth. 2364 // 2365 // TODO: Should probably combine against FFBH_U32 instead of ctlz directly. 2366 SDValue AMDGPUTargetLowering::performCtlzCombine(SDLoc SL, 2367 SDValue Cond, 2368 SDValue LHS, 2369 SDValue RHS, 2370 DAGCombinerInfo &DCI) const { 2371 ConstantSDNode *CmpRhs = dyn_cast<ConstantSDNode>(Cond.getOperand(1)); 2372 if (!CmpRhs || !CmpRhs->isNullValue()) 2373 return SDValue(); 2374 2375 SelectionDAG &DAG = DCI.DAG; 2376 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); 2377 SDValue CmpLHS = Cond.getOperand(0); 2378 2379 // select (setcc x, 0, eq), -1, (ctlz_zero_undef x) -> ffbh_u32 x 2380 if (CCOpcode == ISD::SETEQ && 2381 isCtlzOpc(RHS.getOpcode()) && 2382 RHS.getOperand(0) == CmpLHS && 2383 isNegativeOne(LHS)) { 2384 return getFFBH_U32(*this, DAG, SL, CmpLHS); 2385 } 2386 2387 // select (setcc x, 0, ne), (ctlz_zero_undef x), -1 -> ffbh_u32 x 2388 if (CCOpcode == ISD::SETNE && 2389 isCtlzOpc(LHS.getOpcode()) && 2390 LHS.getOperand(0) == CmpLHS && 2391 isNegativeOne(RHS)) { 2392 return getFFBH_U32(*this, DAG, SL, CmpLHS); 2393 } 2394 2395 return SDValue(); 2396 } 2397 2398 SDValue AMDGPUTargetLowering::performSelectCombine(SDNode *N, 2399 DAGCombinerInfo &DCI) const { 2400 SDValue Cond = N->getOperand(0); 2401 if (Cond.getOpcode() != ISD::SETCC) 2402 return SDValue(); 2403 2404 EVT VT = N->getValueType(0); 2405 SDValue LHS = Cond.getOperand(0); 2406 SDValue RHS = Cond.getOperand(1); 2407 SDValue CC = Cond.getOperand(2); 2408 2409 SDValue True = N->getOperand(1); 2410 SDValue False = N->getOperand(2); 2411 2412 if (VT == MVT::f32 && Cond.hasOneUse()) { 2413 SDValue MinMax 2414 = CombineFMinMaxLegacy(SDLoc(N), VT, LHS, RHS, True, False, CC, DCI); 2415 // Revisit this node so we can catch min3/max3/med3 patterns. 2416 //DCI.AddToWorklist(MinMax.getNode()); 2417 return MinMax; 2418 } 2419 2420 // There's no reason to not do this if the condition has other uses. 2421 return performCtlzCombine(SDLoc(N), Cond, True, False, DCI); 2422 } 2423 2424 SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N, 2425 DAGCombinerInfo &DCI) const { 2426 SelectionDAG &DAG = DCI.DAG; 2427 SDLoc DL(N); 2428 2429 switch(N->getOpcode()) { 2430 default: 2431 break; 2432 case ISD::BITCAST: { 2433 EVT DestVT = N->getValueType(0); 2434 if (DestVT.getSizeInBits() != 64 && !DestVT.isVector()) 2435 break; 2436 2437 // Fold bitcasts of constants. 2438 // 2439 // v2i32 (bitcast i64:k) -> build_vector lo_32(k), hi_32(k) 2440 // TODO: Generalize and move to DAGCombiner 2441 SDValue Src = N->getOperand(0); 2442 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src)) { 2443 assert(Src.getValueType() == MVT::i64); 2444 SDLoc SL(N); 2445 uint64_t CVal = C->getZExtValue(); 2446 return DAG.getNode(ISD::BUILD_VECTOR, SL, DestVT, 2447 DAG.getConstant(Lo_32(CVal), SL, MVT::i32), 2448 DAG.getConstant(Hi_32(CVal), SL, MVT::i32)); 2449 } 2450 2451 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Src)) { 2452 const APInt &Val = C->getValueAPF().bitcastToAPInt(); 2453 SDLoc SL(N); 2454 uint64_t CVal = Val.getZExtValue(); 2455 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, 2456 DAG.getConstant(Lo_32(CVal), SL, MVT::i32), 2457 DAG.getConstant(Hi_32(CVal), SL, MVT::i32)); 2458 2459 return DAG.getNode(ISD::BITCAST, SL, DestVT, Vec); 2460 } 2461 2462 break; 2463 } 2464 case ISD::SHL: { 2465 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 2466 break; 2467 2468 return performShlCombine(N, DCI); 2469 } 2470 case ISD::SRL: { 2471 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 2472 break; 2473 2474 return performSrlCombine(N, DCI); 2475 } 2476 case ISD::SRA: { 2477 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 2478 break; 2479 2480 return performSraCombine(N, DCI); 2481 } 2482 case ISD::AND: { 2483 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 2484 break; 2485 2486 return performAndCombine(N, DCI); 2487 } 2488 case ISD::MUL: 2489 return performMulCombine(N, DCI); 2490 case AMDGPUISD::MUL_I24: 2491 case AMDGPUISD::MUL_U24: { 2492 SDValue N0 = N->getOperand(0); 2493 SDValue N1 = N->getOperand(1); 2494 simplifyI24(N0, DCI); 2495 simplifyI24(N1, DCI); 2496 return SDValue(); 2497 } 2498 case ISD::SELECT: 2499 return performSelectCombine(N, DCI); 2500 case AMDGPUISD::BFE_I32: 2501 case AMDGPUISD::BFE_U32: { 2502 assert(!N->getValueType(0).isVector() && 2503 "Vector handling of BFE not implemented"); 2504 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2)); 2505 if (!Width) 2506 break; 2507 2508 uint32_t WidthVal = Width->getZExtValue() & 0x1f; 2509 if (WidthVal == 0) 2510 return DAG.getConstant(0, DL, MVT::i32); 2511 2512 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2513 if (!Offset) 2514 break; 2515 2516 SDValue BitsFrom = N->getOperand(0); 2517 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f; 2518 2519 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32; 2520 2521 if (OffsetVal == 0) { 2522 // This is already sign / zero extended, so try to fold away extra BFEs. 2523 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal); 2524 2525 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom); 2526 if (OpSignBits >= SignBits) 2527 return BitsFrom; 2528 2529 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal); 2530 if (Signed) { 2531 // This is a sign_extend_inreg. Replace it to take advantage of existing 2532 // DAG Combines. If not eliminated, we will match back to BFE during 2533 // selection. 2534 2535 // TODO: The sext_inreg of extended types ends, although we can could 2536 // handle them in a single BFE. 2537 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom, 2538 DAG.getValueType(SmallVT)); 2539 } 2540 2541 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT); 2542 } 2543 2544 if (ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(BitsFrom)) { 2545 if (Signed) { 2546 return constantFoldBFE<int32_t>(DAG, 2547 CVal->getSExtValue(), 2548 OffsetVal, 2549 WidthVal, 2550 DL); 2551 } 2552 2553 return constantFoldBFE<uint32_t>(DAG, 2554 CVal->getZExtValue(), 2555 OffsetVal, 2556 WidthVal, 2557 DL); 2558 } 2559 2560 if ((OffsetVal + WidthVal) >= 32) { 2561 SDValue ShiftVal = DAG.getConstant(OffsetVal, DL, MVT::i32); 2562 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32, 2563 BitsFrom, ShiftVal); 2564 } 2565 2566 if (BitsFrom.hasOneUse()) { 2567 APInt Demanded = APInt::getBitsSet(32, 2568 OffsetVal, 2569 OffsetVal + WidthVal); 2570 2571 APInt KnownZero, KnownOne; 2572 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), 2573 !DCI.isBeforeLegalizeOps()); 2574 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 2575 if (TLO.ShrinkDemandedConstant(BitsFrom, Demanded) || 2576 TLI.SimplifyDemandedBits(BitsFrom, Demanded, 2577 KnownZero, KnownOne, TLO)) { 2578 DCI.CommitTargetLoweringOpt(TLO); 2579 } 2580 } 2581 2582 break; 2583 } 2584 2585 case ISD::STORE: 2586 return performStoreCombine(N, DCI); 2587 } 2588 return SDValue(); 2589 } 2590 2591 //===----------------------------------------------------------------------===// 2592 // Helper functions 2593 //===----------------------------------------------------------------------===// 2594 2595 void AMDGPUTargetLowering::getOriginalFunctionArgs( 2596 SelectionDAG &DAG, 2597 const Function *F, 2598 const SmallVectorImpl<ISD::InputArg> &Ins, 2599 SmallVectorImpl<ISD::InputArg> &OrigIns) const { 2600 2601 for (unsigned i = 0, e = Ins.size(); i < e; ++i) { 2602 if (Ins[i].ArgVT == Ins[i].VT) { 2603 OrigIns.push_back(Ins[i]); 2604 continue; 2605 } 2606 2607 EVT VT; 2608 if (Ins[i].ArgVT.isVector() && !Ins[i].VT.isVector()) { 2609 // Vector has been split into scalars. 2610 VT = Ins[i].ArgVT.getVectorElementType(); 2611 } else if (Ins[i].VT.isVector() && Ins[i].ArgVT.isVector() && 2612 Ins[i].ArgVT.getVectorElementType() != 2613 Ins[i].VT.getVectorElementType()) { 2614 // Vector elements have been promoted 2615 VT = Ins[i].ArgVT; 2616 } else { 2617 // Vector has been spilt into smaller vectors. 2618 VT = Ins[i].VT; 2619 } 2620 2621 ISD::InputArg Arg(Ins[i].Flags, VT, VT, Ins[i].Used, 2622 Ins[i].OrigArgIndex, Ins[i].PartOffset); 2623 OrigIns.push_back(Arg); 2624 } 2625 } 2626 2627 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG, 2628 const TargetRegisterClass *RC, 2629 unsigned Reg, EVT VT) const { 2630 MachineFunction &MF = DAG.getMachineFunction(); 2631 MachineRegisterInfo &MRI = MF.getRegInfo(); 2632 unsigned VirtualRegister; 2633 if (!MRI.isLiveIn(Reg)) { 2634 VirtualRegister = MRI.createVirtualRegister(RC); 2635 MRI.addLiveIn(Reg, VirtualRegister); 2636 } else { 2637 VirtualRegister = MRI.getLiveInVirtReg(Reg); 2638 } 2639 return DAG.getRegister(VirtualRegister, VT); 2640 } 2641 2642 uint32_t AMDGPUTargetLowering::getImplicitParameterOffset( 2643 const AMDGPUMachineFunction *MFI, const ImplicitParameter Param) const { 2644 uint64_t ArgOffset = MFI->ABIArgOffset; 2645 switch (Param) { 2646 case GRID_DIM: 2647 return ArgOffset; 2648 case GRID_OFFSET: 2649 return ArgOffset + 4; 2650 } 2651 llvm_unreachable("unexpected implicit parameter type"); 2652 } 2653 2654 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node; 2655 2656 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const { 2657 switch ((AMDGPUISD::NodeType)Opcode) { 2658 case AMDGPUISD::FIRST_NUMBER: break; 2659 // AMDIL DAG nodes 2660 NODE_NAME_CASE(CALL); 2661 NODE_NAME_CASE(UMUL); 2662 NODE_NAME_CASE(RET_FLAG); 2663 NODE_NAME_CASE(BRANCH_COND); 2664 2665 // AMDGPU DAG nodes 2666 NODE_NAME_CASE(DWORDADDR) 2667 NODE_NAME_CASE(FRACT) 2668 NODE_NAME_CASE(CLAMP) 2669 NODE_NAME_CASE(COS_HW) 2670 NODE_NAME_CASE(SIN_HW) 2671 NODE_NAME_CASE(FMAX_LEGACY) 2672 NODE_NAME_CASE(FMIN_LEGACY) 2673 NODE_NAME_CASE(FMAX3) 2674 NODE_NAME_CASE(SMAX3) 2675 NODE_NAME_CASE(UMAX3) 2676 NODE_NAME_CASE(FMIN3) 2677 NODE_NAME_CASE(SMIN3) 2678 NODE_NAME_CASE(UMIN3) 2679 NODE_NAME_CASE(FMED3) 2680 NODE_NAME_CASE(SMED3) 2681 NODE_NAME_CASE(UMED3) 2682 NODE_NAME_CASE(URECIP) 2683 NODE_NAME_CASE(DIV_SCALE) 2684 NODE_NAME_CASE(DIV_FMAS) 2685 NODE_NAME_CASE(DIV_FIXUP) 2686 NODE_NAME_CASE(TRIG_PREOP) 2687 NODE_NAME_CASE(RCP) 2688 NODE_NAME_CASE(RSQ) 2689 NODE_NAME_CASE(RSQ_LEGACY) 2690 NODE_NAME_CASE(RSQ_CLAMP) 2691 NODE_NAME_CASE(LDEXP) 2692 NODE_NAME_CASE(FP_CLASS) 2693 NODE_NAME_CASE(DOT4) 2694 NODE_NAME_CASE(CARRY) 2695 NODE_NAME_CASE(BORROW) 2696 NODE_NAME_CASE(BFE_U32) 2697 NODE_NAME_CASE(BFE_I32) 2698 NODE_NAME_CASE(BFI) 2699 NODE_NAME_CASE(BFM) 2700 NODE_NAME_CASE(FFBH_U32) 2701 NODE_NAME_CASE(MUL_U24) 2702 NODE_NAME_CASE(MUL_I24) 2703 NODE_NAME_CASE(MAD_U24) 2704 NODE_NAME_CASE(MAD_I24) 2705 NODE_NAME_CASE(TEXTURE_FETCH) 2706 NODE_NAME_CASE(EXPORT) 2707 NODE_NAME_CASE(CONST_ADDRESS) 2708 NODE_NAME_CASE(REGISTER_LOAD) 2709 NODE_NAME_CASE(REGISTER_STORE) 2710 NODE_NAME_CASE(LOAD_INPUT) 2711 NODE_NAME_CASE(SAMPLE) 2712 NODE_NAME_CASE(SAMPLEB) 2713 NODE_NAME_CASE(SAMPLED) 2714 NODE_NAME_CASE(SAMPLEL) 2715 NODE_NAME_CASE(CVT_F32_UBYTE0) 2716 NODE_NAME_CASE(CVT_F32_UBYTE1) 2717 NODE_NAME_CASE(CVT_F32_UBYTE2) 2718 NODE_NAME_CASE(CVT_F32_UBYTE3) 2719 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR) 2720 NODE_NAME_CASE(CONST_DATA_PTR) 2721 case AMDGPUISD::FIRST_MEM_OPCODE_NUMBER: break; 2722 NODE_NAME_CASE(SENDMSG) 2723 NODE_NAME_CASE(INTERP_MOV) 2724 NODE_NAME_CASE(INTERP_P1) 2725 NODE_NAME_CASE(INTERP_P2) 2726 NODE_NAME_CASE(STORE_MSKOR) 2727 NODE_NAME_CASE(LOAD_CONSTANT) 2728 NODE_NAME_CASE(TBUFFER_STORE_FORMAT) 2729 NODE_NAME_CASE(ATOMIC_CMP_SWAP) 2730 NODE_NAME_CASE(ATOMIC_INC) 2731 NODE_NAME_CASE(ATOMIC_DEC) 2732 case AMDGPUISD::LAST_AMDGPU_ISD_NUMBER: break; 2733 } 2734 return nullptr; 2735 } 2736 2737 SDValue AMDGPUTargetLowering::getRsqrtEstimate(SDValue Operand, 2738 DAGCombinerInfo &DCI, 2739 unsigned &RefinementSteps, 2740 bool &UseOneConstNR) const { 2741 SelectionDAG &DAG = DCI.DAG; 2742 EVT VT = Operand.getValueType(); 2743 2744 if (VT == MVT::f32) { 2745 RefinementSteps = 0; 2746 return DAG.getNode(AMDGPUISD::RSQ, SDLoc(Operand), VT, Operand); 2747 } 2748 2749 // TODO: There is also f64 rsq instruction, but the documentation is less 2750 // clear on its precision. 2751 2752 return SDValue(); 2753 } 2754 2755 SDValue AMDGPUTargetLowering::getRecipEstimate(SDValue Operand, 2756 DAGCombinerInfo &DCI, 2757 unsigned &RefinementSteps) const { 2758 SelectionDAG &DAG = DCI.DAG; 2759 EVT VT = Operand.getValueType(); 2760 2761 if (VT == MVT::f32) { 2762 // Reciprocal, < 1 ulp error. 2763 // 2764 // This reciprocal approximation converges to < 0.5 ulp error with one 2765 // newton rhapson performed with two fused multiple adds (FMAs). 2766 2767 RefinementSteps = 0; 2768 return DAG.getNode(AMDGPUISD::RCP, SDLoc(Operand), VT, Operand); 2769 } 2770 2771 // TODO: There is also f64 rcp instruction, but the documentation is less 2772 // clear on its precision. 2773 2774 return SDValue(); 2775 } 2776 2777 void AMDGPUTargetLowering::computeKnownBitsForTargetNode( 2778 const SDValue Op, 2779 APInt &KnownZero, 2780 APInt &KnownOne, 2781 const SelectionDAG &DAG, 2782 unsigned Depth) const { 2783 2784 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); // Don't know anything. 2785 2786 APInt KnownZero2; 2787 APInt KnownOne2; 2788 unsigned Opc = Op.getOpcode(); 2789 2790 switch (Opc) { 2791 default: 2792 break; 2793 case AMDGPUISD::CARRY: 2794 case AMDGPUISD::BORROW: { 2795 KnownZero = APInt::getHighBitsSet(32, 31); 2796 break; 2797 } 2798 2799 case AMDGPUISD::BFE_I32: 2800 case AMDGPUISD::BFE_U32: { 2801 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 2802 if (!CWidth) 2803 return; 2804 2805 unsigned BitWidth = 32; 2806 uint32_t Width = CWidth->getZExtValue() & 0x1f; 2807 2808 if (Opc == AMDGPUISD::BFE_U32) 2809 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - Width); 2810 2811 break; 2812 } 2813 } 2814 } 2815 2816 unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode( 2817 SDValue Op, 2818 const SelectionDAG &DAG, 2819 unsigned Depth) const { 2820 switch (Op.getOpcode()) { 2821 case AMDGPUISD::BFE_I32: { 2822 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 2823 if (!Width) 2824 return 1; 2825 2826 unsigned SignBits = 32 - Width->getZExtValue() + 1; 2827 if (!isNullConstant(Op.getOperand(1))) 2828 return SignBits; 2829 2830 // TODO: Could probably figure something out with non-0 offsets. 2831 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1); 2832 return std::max(SignBits, Op0SignBits); 2833 } 2834 2835 case AMDGPUISD::BFE_U32: { 2836 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 2837 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1; 2838 } 2839 2840 case AMDGPUISD::CARRY: 2841 case AMDGPUISD::BORROW: 2842 return 31; 2843 2844 default: 2845 return 1; 2846 } 2847 } 2848