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::CONSTANT_ADDRESS: { 791 MVT ConstPtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS); 792 SDValue GA = DAG.getTargetGlobalAddress(GV, SDLoc(G), ConstPtrVT); 793 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, SDLoc(G), ConstPtrVT, GA); 794 } 795 case AMDGPUAS::LOCAL_ADDRESS: { 796 // XXX: What does the value of G->getOffset() mean? 797 assert(G->getOffset() == 0 && 798 "Do not know what to do with an non-zero offset"); 799 800 // TODO: We could emit code to handle the initialization somewhere. 801 if (hasDefinedInitializer(GV)) 802 break; 803 804 unsigned Offset; 805 if (MFI->LocalMemoryObjects.count(GV) == 0) { 806 unsigned Align = GV->getAlignment(); 807 if (Align == 0) 808 Align = DL.getABITypeAlignment(GV->getValueType()); 809 810 /// TODO: We should sort these to minimize wasted space due to alignment 811 /// padding. Currently the padding is decided by the first encountered use 812 /// during lowering. 813 Offset = MFI->LDSSize = alignTo(MFI->LDSSize, Align); 814 MFI->LocalMemoryObjects[GV] = Offset; 815 MFI->LDSSize += DL.getTypeAllocSize(GV->getValueType()); 816 } else { 817 Offset = MFI->LocalMemoryObjects[GV]; 818 } 819 820 return DAG.getConstant(Offset, SDLoc(Op), 821 getPointerTy(DL, AMDGPUAS::LOCAL_ADDRESS)); 822 } 823 } 824 825 const Function &Fn = *DAG.getMachineFunction().getFunction(); 826 DiagnosticInfoUnsupported BadInit( 827 Fn, "unsupported initializer for address space", SDLoc(Op).getDebugLoc()); 828 DAG.getContext()->diagnose(BadInit); 829 return SDValue(); 830 } 831 832 SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op, 833 SelectionDAG &DAG) const { 834 SmallVector<SDValue, 8> Args; 835 836 for (const SDUse &U : Op->ops()) 837 DAG.ExtractVectorElements(U.get(), Args); 838 839 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args); 840 } 841 842 SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, 843 SelectionDAG &DAG) const { 844 845 SmallVector<SDValue, 8> Args; 846 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 847 EVT VT = Op.getValueType(); 848 DAG.ExtractVectorElements(Op.getOperand(0), Args, Start, 849 VT.getVectorNumElements()); 850 851 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args); 852 } 853 854 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, 855 SelectionDAG &DAG) const { 856 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 857 SDLoc DL(Op); 858 EVT VT = Op.getValueType(); 859 860 switch (IntrinsicID) { 861 default: return Op; 862 case AMDGPUIntrinsic::AMDGPU_clamp: 863 case AMDGPUIntrinsic::AMDIL_clamp: // Legacy name. 864 return DAG.getNode(AMDGPUISD::CLAMP, DL, VT, 865 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 866 867 case Intrinsic::AMDGPU_ldexp: // Legacy name 868 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT, Op.getOperand(1), 869 Op.getOperand(2)); 870 871 case AMDGPUIntrinsic::AMDGPU_bfe_i32: 872 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT, 873 Op.getOperand(1), 874 Op.getOperand(2), 875 Op.getOperand(3)); 876 877 case AMDGPUIntrinsic::AMDGPU_bfe_u32: 878 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT, 879 Op.getOperand(1), 880 Op.getOperand(2), 881 Op.getOperand(3)); 882 883 case AMDGPUIntrinsic::AMDIL_exp: // Legacy name. 884 return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1)); 885 886 case AMDGPUIntrinsic::AMDGPU_brev: // Legacy name 887 return DAG.getNode(ISD::BITREVERSE, DL, VT, Op.getOperand(1)); 888 } 889 } 890 891 /// \brief Generate Min/Max node 892 SDValue AMDGPUTargetLowering::CombineFMinMaxLegacy(SDLoc DL, 893 EVT VT, 894 SDValue LHS, 895 SDValue RHS, 896 SDValue True, 897 SDValue False, 898 SDValue CC, 899 DAGCombinerInfo &DCI) const { 900 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) 901 return SDValue(); 902 903 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True)) 904 return SDValue(); 905 906 SelectionDAG &DAG = DCI.DAG; 907 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get(); 908 switch (CCOpcode) { 909 case ISD::SETOEQ: 910 case ISD::SETONE: 911 case ISD::SETUNE: 912 case ISD::SETNE: 913 case ISD::SETUEQ: 914 case ISD::SETEQ: 915 case ISD::SETFALSE: 916 case ISD::SETFALSE2: 917 case ISD::SETTRUE: 918 case ISD::SETTRUE2: 919 case ISD::SETUO: 920 case ISD::SETO: 921 break; 922 case ISD::SETULE: 923 case ISD::SETULT: { 924 if (LHS == True) 925 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS); 926 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS); 927 } 928 case ISD::SETOLE: 929 case ISD::SETOLT: 930 case ISD::SETLE: 931 case ISD::SETLT: { 932 // Ordered. Assume ordered for undefined. 933 934 // Only do this after legalization to avoid interfering with other combines 935 // which might occur. 936 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG && 937 !DCI.isCalledByLegalizer()) 938 return SDValue(); 939 940 // We need to permute the operands to get the correct NaN behavior. The 941 // selected operand is the second one based on the failing compare with NaN, 942 // so permute it based on the compare type the hardware uses. 943 if (LHS == True) 944 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS); 945 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS); 946 } 947 case ISD::SETUGE: 948 case ISD::SETUGT: { 949 if (LHS == True) 950 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS); 951 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS); 952 } 953 case ISD::SETGT: 954 case ISD::SETGE: 955 case ISD::SETOGE: 956 case ISD::SETOGT: { 957 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG && 958 !DCI.isCalledByLegalizer()) 959 return SDValue(); 960 961 if (LHS == True) 962 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS); 963 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS); 964 } 965 case ISD::SETCC_INVALID: 966 llvm_unreachable("Invalid setcc condcode!"); 967 } 968 return SDValue(); 969 } 970 971 std::pair<SDValue, SDValue> 972 AMDGPUTargetLowering::split64BitValue(SDValue Op, SelectionDAG &DAG) const { 973 SDLoc SL(Op); 974 975 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op); 976 977 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 978 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 979 980 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero); 981 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One); 982 983 return std::make_pair(Lo, Hi); 984 } 985 986 SDValue AMDGPUTargetLowering::getLoHalf64(SDValue Op, SelectionDAG &DAG) const { 987 SDLoc SL(Op); 988 989 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op); 990 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 991 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero); 992 } 993 994 SDValue AMDGPUTargetLowering::getHiHalf64(SDValue Op, SelectionDAG &DAG) const { 995 SDLoc SL(Op); 996 997 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op); 998 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 999 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One); 1000 } 1001 1002 SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue Op, 1003 SelectionDAG &DAG) const { 1004 LoadSDNode *Load = cast<LoadSDNode>(Op); 1005 EVT VT = Op.getValueType(); 1006 1007 1008 // If this is a 2 element vector, we really want to scalarize and not create 1009 // weird 1 element vectors. 1010 if (VT.getVectorNumElements() == 2) 1011 return scalarizeVectorLoad(Load, DAG); 1012 1013 SDValue BasePtr = Load->getBasePtr(); 1014 EVT PtrVT = BasePtr.getValueType(); 1015 EVT MemVT = Load->getMemoryVT(); 1016 SDLoc SL(Op); 1017 1018 const MachinePointerInfo &SrcValue = Load->getMemOperand()->getPointerInfo(); 1019 1020 EVT LoVT, HiVT; 1021 EVT LoMemVT, HiMemVT; 1022 SDValue Lo, Hi; 1023 1024 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT); 1025 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT); 1026 std::tie(Lo, Hi) = DAG.SplitVector(Op, SL, LoVT, HiVT); 1027 1028 unsigned Size = LoMemVT.getStoreSize(); 1029 unsigned BaseAlign = Load->getAlignment(); 1030 unsigned HiAlign = MinAlign(BaseAlign, Size); 1031 1032 SDValue LoLoad 1033 = DAG.getExtLoad(Load->getExtensionType(), SL, LoVT, 1034 Load->getChain(), BasePtr, 1035 SrcValue, 1036 LoMemVT, Load->isVolatile(), Load->isNonTemporal(), 1037 Load->isInvariant(), BaseAlign); 1038 1039 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr, 1040 DAG.getConstant(Size, SL, PtrVT)); 1041 1042 SDValue HiLoad 1043 = DAG.getExtLoad(Load->getExtensionType(), SL, HiVT, 1044 Load->getChain(), HiPtr, 1045 SrcValue.getWithOffset(LoMemVT.getStoreSize()), 1046 HiMemVT, Load->isVolatile(), Load->isNonTemporal(), 1047 Load->isInvariant(), HiAlign); 1048 1049 SDValue Ops[] = { 1050 DAG.getNode(ISD::CONCAT_VECTORS, SL, VT, LoLoad, HiLoad), 1051 DAG.getNode(ISD::TokenFactor, SL, MVT::Other, 1052 LoLoad.getValue(1), HiLoad.getValue(1)) 1053 }; 1054 1055 return DAG.getMergeValues(Ops, SL); 1056 } 1057 1058 // FIXME: This isn't doing anything for SI. This should be used in a target 1059 // combine during type legalization. 1060 SDValue AMDGPUTargetLowering::MergeVectorStore(const SDValue &Op, 1061 SelectionDAG &DAG) const { 1062 StoreSDNode *Store = cast<StoreSDNode>(Op); 1063 EVT MemVT = Store->getMemoryVT(); 1064 unsigned MemBits = MemVT.getSizeInBits(); 1065 1066 // Byte stores are really expensive, so if possible, try to pack 32-bit vector 1067 // truncating store into an i32 store. 1068 // XXX: We could also handle optimize other vector bitwidths. 1069 if (!MemVT.isVector() || MemBits > 32) { 1070 return SDValue(); 1071 } 1072 1073 SDLoc DL(Op); 1074 SDValue Value = Store->getValue(); 1075 EVT VT = Value.getValueType(); 1076 EVT ElemVT = VT.getVectorElementType(); 1077 SDValue Ptr = Store->getBasePtr(); 1078 EVT MemEltVT = MemVT.getVectorElementType(); 1079 unsigned MemEltBits = MemEltVT.getSizeInBits(); 1080 unsigned MemNumElements = MemVT.getVectorNumElements(); 1081 unsigned PackedSize = MemVT.getStoreSizeInBits(); 1082 SDValue Mask = DAG.getConstant((1 << MemEltBits) - 1, DL, MVT::i32); 1083 1084 assert(Value.getValueType().getScalarSizeInBits() >= 32); 1085 1086 SDValue PackedValue; 1087 for (unsigned i = 0; i < MemNumElements; ++i) { 1088 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT, Value, 1089 DAG.getConstant(i, DL, MVT::i32)); 1090 Elt = DAG.getZExtOrTrunc(Elt, DL, MVT::i32); 1091 Elt = DAG.getNode(ISD::AND, DL, MVT::i32, Elt, Mask); // getZeroExtendInReg 1092 1093 SDValue Shift = DAG.getConstant(MemEltBits * i, DL, MVT::i32); 1094 Elt = DAG.getNode(ISD::SHL, DL, MVT::i32, Elt, Shift); 1095 1096 if (i == 0) { 1097 PackedValue = Elt; 1098 } else { 1099 PackedValue = DAG.getNode(ISD::OR, DL, MVT::i32, PackedValue, Elt); 1100 } 1101 } 1102 1103 if (PackedSize < 32) { 1104 EVT PackedVT = EVT::getIntegerVT(*DAG.getContext(), PackedSize); 1105 return DAG.getTruncStore(Store->getChain(), DL, PackedValue, Ptr, 1106 Store->getMemOperand()->getPointerInfo(), 1107 PackedVT, 1108 Store->isNonTemporal(), Store->isVolatile(), 1109 Store->getAlignment()); 1110 } 1111 1112 return DAG.getStore(Store->getChain(), DL, PackedValue, Ptr, 1113 Store->getMemOperand()->getPointerInfo(), 1114 Store->isVolatile(), Store->isNonTemporal(), 1115 Store->getAlignment()); 1116 } 1117 1118 SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op, 1119 SelectionDAG &DAG) const { 1120 StoreSDNode *Store = cast<StoreSDNode>(Op); 1121 SDValue Val = Store->getValue(); 1122 EVT VT = Val.getValueType(); 1123 1124 // If this is a 2 element vector, we really want to scalarize and not create 1125 // weird 1 element vectors. 1126 if (VT.getVectorNumElements() == 2) 1127 return scalarizeVectorStore(Store, DAG); 1128 1129 EVT MemVT = Store->getMemoryVT(); 1130 SDValue Chain = Store->getChain(); 1131 SDValue BasePtr = Store->getBasePtr(); 1132 SDLoc SL(Op); 1133 1134 EVT LoVT, HiVT; 1135 EVT LoMemVT, HiMemVT; 1136 SDValue Lo, Hi; 1137 1138 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT); 1139 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT); 1140 std::tie(Lo, Hi) = DAG.SplitVector(Val, SL, LoVT, HiVT); 1141 1142 EVT PtrVT = BasePtr.getValueType(); 1143 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr, 1144 DAG.getConstant(LoMemVT.getStoreSize(), SL, 1145 PtrVT)); 1146 1147 const MachinePointerInfo &SrcValue = Store->getMemOperand()->getPointerInfo(); 1148 unsigned BaseAlign = Store->getAlignment(); 1149 unsigned Size = LoMemVT.getStoreSize(); 1150 unsigned HiAlign = MinAlign(BaseAlign, Size); 1151 1152 SDValue LoStore 1153 = DAG.getTruncStore(Chain, SL, Lo, 1154 BasePtr, 1155 SrcValue, 1156 LoMemVT, 1157 Store->isNonTemporal(), 1158 Store->isVolatile(), 1159 BaseAlign); 1160 SDValue HiStore 1161 = DAG.getTruncStore(Chain, SL, Hi, 1162 HiPtr, 1163 SrcValue.getWithOffset(Size), 1164 HiMemVT, 1165 Store->isNonTemporal(), 1166 Store->isVolatile(), 1167 HiAlign); 1168 1169 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoStore, HiStore); 1170 } 1171 1172 // This is a shortcut for integer division because we have fast i32<->f32 1173 // conversions, and fast f32 reciprocal instructions. The fractional part of a 1174 // float is enough to accurately represent up to a 24-bit integer. 1175 SDValue AMDGPUTargetLowering::LowerDIVREM24(SDValue Op, SelectionDAG &DAG, bool sign) const { 1176 SDLoc DL(Op); 1177 EVT VT = Op.getValueType(); 1178 SDValue LHS = Op.getOperand(0); 1179 SDValue RHS = Op.getOperand(1); 1180 MVT IntVT = MVT::i32; 1181 MVT FltVT = MVT::f32; 1182 1183 ISD::NodeType ToFp = sign ? ISD::SINT_TO_FP : ISD::UINT_TO_FP; 1184 ISD::NodeType ToInt = sign ? ISD::FP_TO_SINT : ISD::FP_TO_UINT; 1185 1186 if (VT.isVector()) { 1187 unsigned NElts = VT.getVectorNumElements(); 1188 IntVT = MVT::getVectorVT(MVT::i32, NElts); 1189 FltVT = MVT::getVectorVT(MVT::f32, NElts); 1190 } 1191 1192 unsigned BitSize = VT.getScalarType().getSizeInBits(); 1193 1194 SDValue jq = DAG.getConstant(1, DL, IntVT); 1195 1196 if (sign) { 1197 // char|short jq = ia ^ ib; 1198 jq = DAG.getNode(ISD::XOR, DL, VT, LHS, RHS); 1199 1200 // jq = jq >> (bitsize - 2) 1201 jq = DAG.getNode(ISD::SRA, DL, VT, jq, 1202 DAG.getConstant(BitSize - 2, DL, VT)); 1203 1204 // jq = jq | 0x1 1205 jq = DAG.getNode(ISD::OR, DL, VT, jq, DAG.getConstant(1, DL, VT)); 1206 1207 // jq = (int)jq 1208 jq = DAG.getSExtOrTrunc(jq, DL, IntVT); 1209 } 1210 1211 // int ia = (int)LHS; 1212 SDValue ia = sign ? 1213 DAG.getSExtOrTrunc(LHS, DL, IntVT) : DAG.getZExtOrTrunc(LHS, DL, IntVT); 1214 1215 // int ib, (int)RHS; 1216 SDValue ib = sign ? 1217 DAG.getSExtOrTrunc(RHS, DL, IntVT) : DAG.getZExtOrTrunc(RHS, DL, IntVT); 1218 1219 // float fa = (float)ia; 1220 SDValue fa = DAG.getNode(ToFp, DL, FltVT, ia); 1221 1222 // float fb = (float)ib; 1223 SDValue fb = DAG.getNode(ToFp, DL, FltVT, ib); 1224 1225 // TODO: Should this propagate fast-math-flags? 1226 // float fq = native_divide(fa, fb); 1227 SDValue fq = DAG.getNode(ISD::FMUL, DL, FltVT, 1228 fa, DAG.getNode(AMDGPUISD::RCP, DL, FltVT, fb)); 1229 1230 // fq = trunc(fq); 1231 fq = DAG.getNode(ISD::FTRUNC, DL, FltVT, fq); 1232 1233 // float fqneg = -fq; 1234 SDValue fqneg = DAG.getNode(ISD::FNEG, DL, FltVT, fq); 1235 1236 // float fr = mad(fqneg, fb, fa); 1237 SDValue fr = DAG.getNode(ISD::FADD, DL, FltVT, 1238 DAG.getNode(ISD::FMUL, DL, FltVT, fqneg, fb), fa); 1239 1240 // int iq = (int)fq; 1241 SDValue iq = DAG.getNode(ToInt, DL, IntVT, fq); 1242 1243 // fr = fabs(fr); 1244 fr = DAG.getNode(ISD::FABS, DL, FltVT, fr); 1245 1246 // fb = fabs(fb); 1247 fb = DAG.getNode(ISD::FABS, DL, FltVT, fb); 1248 1249 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 1250 1251 // int cv = fr >= fb; 1252 SDValue cv = DAG.getSetCC(DL, SetCCVT, fr, fb, ISD::SETOGE); 1253 1254 // jq = (cv ? jq : 0); 1255 jq = DAG.getNode(ISD::SELECT, DL, VT, cv, jq, DAG.getConstant(0, DL, VT)); 1256 1257 // dst = trunc/extend to legal type 1258 iq = sign ? DAG.getSExtOrTrunc(iq, DL, VT) : DAG.getZExtOrTrunc(iq, DL, VT); 1259 1260 // dst = iq + jq; 1261 SDValue Div = DAG.getNode(ISD::ADD, DL, VT, iq, jq); 1262 1263 // Rem needs compensation, it's easier to recompute it 1264 SDValue Rem = DAG.getNode(ISD::MUL, DL, VT, Div, RHS); 1265 Rem = DAG.getNode(ISD::SUB, DL, VT, LHS, Rem); 1266 1267 SDValue Res[2] = { 1268 Div, 1269 Rem 1270 }; 1271 return DAG.getMergeValues(Res, DL); 1272 } 1273 1274 void AMDGPUTargetLowering::LowerUDIVREM64(SDValue Op, 1275 SelectionDAG &DAG, 1276 SmallVectorImpl<SDValue> &Results) const { 1277 assert(Op.getValueType() == MVT::i64); 1278 1279 SDLoc DL(Op); 1280 EVT VT = Op.getValueType(); 1281 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext()); 1282 1283 SDValue one = DAG.getConstant(1, DL, HalfVT); 1284 SDValue zero = DAG.getConstant(0, DL, HalfVT); 1285 1286 //HiLo split 1287 SDValue LHS = Op.getOperand(0); 1288 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero); 1289 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one); 1290 1291 SDValue RHS = Op.getOperand(1); 1292 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero); 1293 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one); 1294 1295 if (VT == MVT::i64 && 1296 DAG.MaskedValueIsZero(RHS, APInt::getHighBitsSet(64, 32)) && 1297 DAG.MaskedValueIsZero(LHS, APInt::getHighBitsSet(64, 32))) { 1298 1299 SDValue Res = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(HalfVT, HalfVT), 1300 LHS_Lo, RHS_Lo); 1301 1302 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(0), zero}); 1303 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(1), zero}); 1304 1305 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV)); 1306 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM)); 1307 return; 1308 } 1309 1310 // Get Speculative values 1311 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo); 1312 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo); 1313 1314 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ); 1315 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {REM_Lo, zero}); 1316 REM = DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM); 1317 1318 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ); 1319 SDValue DIV_Lo = zero; 1320 1321 const unsigned halfBitWidth = HalfVT.getSizeInBits(); 1322 1323 for (unsigned i = 0; i < halfBitWidth; ++i) { 1324 const unsigned bitPos = halfBitWidth - i - 1; 1325 SDValue POS = DAG.getConstant(bitPos, DL, HalfVT); 1326 // Get value of high bit 1327 SDValue HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS); 1328 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one); 1329 HBit = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, HBit); 1330 1331 // Shift 1332 REM = DAG.getNode(ISD::SHL, DL, VT, REM, DAG.getConstant(1, DL, VT)); 1333 // Add LHS high bit 1334 REM = DAG.getNode(ISD::OR, DL, VT, REM, HBit); 1335 1336 SDValue BIT = DAG.getConstant(1ULL << bitPos, DL, HalfVT); 1337 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETUGE); 1338 1339 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT); 1340 1341 // Update REM 1342 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS); 1343 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETUGE); 1344 } 1345 1346 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {DIV_Lo, DIV_Hi}); 1347 DIV = DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV); 1348 Results.push_back(DIV); 1349 Results.push_back(REM); 1350 } 1351 1352 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op, 1353 SelectionDAG &DAG) const { 1354 SDLoc DL(Op); 1355 EVT VT = Op.getValueType(); 1356 1357 if (VT == MVT::i64) { 1358 SmallVector<SDValue, 2> Results; 1359 LowerUDIVREM64(Op, DAG, Results); 1360 return DAG.getMergeValues(Results, DL); 1361 } 1362 1363 SDValue Num = Op.getOperand(0); 1364 SDValue Den = Op.getOperand(1); 1365 1366 if (VT == MVT::i32) { 1367 if (DAG.MaskedValueIsZero(Num, APInt::getHighBitsSet(32, 8)) && 1368 DAG.MaskedValueIsZero(Den, APInt::getHighBitsSet(32, 8))) { 1369 // TODO: We technically could do this for i64, but shouldn't that just be 1370 // handled by something generally reducing 64-bit division on 32-bit 1371 // values to 32-bit? 1372 return LowerDIVREM24(Op, DAG, false); 1373 } 1374 } 1375 1376 // RCP = URECIP(Den) = 2^32 / Den + e 1377 // e is rounding error. 1378 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den); 1379 1380 // RCP_LO = mul(RCP, Den) */ 1381 SDValue RCP_LO = DAG.getNode(ISD::MUL, DL, VT, RCP, Den); 1382 1383 // RCP_HI = mulhu (RCP, Den) */ 1384 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den); 1385 1386 // NEG_RCP_LO = -RCP_LO 1387 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), 1388 RCP_LO); 1389 1390 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO) 1391 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT), 1392 NEG_RCP_LO, RCP_LO, 1393 ISD::SETEQ); 1394 // Calculate the rounding error from the URECIP instruction 1395 // E = mulhu(ABS_RCP_LO, RCP) 1396 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP); 1397 1398 // RCP_A_E = RCP + E 1399 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E); 1400 1401 // RCP_S_E = RCP - E 1402 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E); 1403 1404 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E) 1405 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT), 1406 RCP_A_E, RCP_S_E, 1407 ISD::SETEQ); 1408 // Quotient = mulhu(Tmp0, Num) 1409 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num); 1410 1411 // Num_S_Remainder = Quotient * Den 1412 SDValue Num_S_Remainder = DAG.getNode(ISD::MUL, DL, VT, Quotient, Den); 1413 1414 // Remainder = Num - Num_S_Remainder 1415 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder); 1416 1417 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0) 1418 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den, 1419 DAG.getConstant(-1, DL, VT), 1420 DAG.getConstant(0, DL, VT), 1421 ISD::SETUGE); 1422 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0) 1423 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num, 1424 Num_S_Remainder, 1425 DAG.getConstant(-1, DL, VT), 1426 DAG.getConstant(0, DL, VT), 1427 ISD::SETUGE); 1428 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero 1429 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den, 1430 Remainder_GE_Zero); 1431 1432 // Calculate Division result: 1433 1434 // Quotient_A_One = Quotient + 1 1435 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient, 1436 DAG.getConstant(1, DL, VT)); 1437 1438 // Quotient_S_One = Quotient - 1 1439 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient, 1440 DAG.getConstant(1, DL, VT)); 1441 1442 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One) 1443 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT), 1444 Quotient, Quotient_A_One, ISD::SETEQ); 1445 1446 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div) 1447 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT), 1448 Quotient_S_One, Div, ISD::SETEQ); 1449 1450 // Calculate Rem result: 1451 1452 // Remainder_S_Den = Remainder - Den 1453 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den); 1454 1455 // Remainder_A_Den = Remainder + Den 1456 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den); 1457 1458 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den) 1459 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT), 1460 Remainder, Remainder_S_Den, ISD::SETEQ); 1461 1462 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem) 1463 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT), 1464 Remainder_A_Den, Rem, ISD::SETEQ); 1465 SDValue Ops[2] = { 1466 Div, 1467 Rem 1468 }; 1469 return DAG.getMergeValues(Ops, DL); 1470 } 1471 1472 SDValue AMDGPUTargetLowering::LowerSDIVREM(SDValue Op, 1473 SelectionDAG &DAG) const { 1474 SDLoc DL(Op); 1475 EVT VT = Op.getValueType(); 1476 1477 SDValue LHS = Op.getOperand(0); 1478 SDValue RHS = Op.getOperand(1); 1479 1480 SDValue Zero = DAG.getConstant(0, DL, VT); 1481 SDValue NegOne = DAG.getConstant(-1, DL, VT); 1482 1483 if (VT == MVT::i32 && 1484 DAG.ComputeNumSignBits(LHS) > 8 && 1485 DAG.ComputeNumSignBits(RHS) > 8) { 1486 return LowerDIVREM24(Op, DAG, true); 1487 } 1488 if (VT == MVT::i64 && 1489 DAG.ComputeNumSignBits(LHS) > 32 && 1490 DAG.ComputeNumSignBits(RHS) > 32) { 1491 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext()); 1492 1493 //HiLo split 1494 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, Zero); 1495 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, Zero); 1496 SDValue DIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(HalfVT, HalfVT), 1497 LHS_Lo, RHS_Lo); 1498 SDValue Res[2] = { 1499 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(0)), 1500 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(1)) 1501 }; 1502 return DAG.getMergeValues(Res, DL); 1503 } 1504 1505 SDValue LHSign = DAG.getSelectCC(DL, LHS, Zero, NegOne, Zero, ISD::SETLT); 1506 SDValue RHSign = DAG.getSelectCC(DL, RHS, Zero, NegOne, Zero, ISD::SETLT); 1507 SDValue DSign = DAG.getNode(ISD::XOR, DL, VT, LHSign, RHSign); 1508 SDValue RSign = LHSign; // Remainder sign is the same as LHS 1509 1510 LHS = DAG.getNode(ISD::ADD, DL, VT, LHS, LHSign); 1511 RHS = DAG.getNode(ISD::ADD, DL, VT, RHS, RHSign); 1512 1513 LHS = DAG.getNode(ISD::XOR, DL, VT, LHS, LHSign); 1514 RHS = DAG.getNode(ISD::XOR, DL, VT, RHS, RHSign); 1515 1516 SDValue Div = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT), LHS, RHS); 1517 SDValue Rem = Div.getValue(1); 1518 1519 Div = DAG.getNode(ISD::XOR, DL, VT, Div, DSign); 1520 Rem = DAG.getNode(ISD::XOR, DL, VT, Rem, RSign); 1521 1522 Div = DAG.getNode(ISD::SUB, DL, VT, Div, DSign); 1523 Rem = DAG.getNode(ISD::SUB, DL, VT, Rem, RSign); 1524 1525 SDValue Res[2] = { 1526 Div, 1527 Rem 1528 }; 1529 return DAG.getMergeValues(Res, DL); 1530 } 1531 1532 // (frem x, y) -> (fsub x, (fmul (ftrunc (fdiv x, y)), y)) 1533 SDValue AMDGPUTargetLowering::LowerFREM(SDValue Op, SelectionDAG &DAG) const { 1534 SDLoc SL(Op); 1535 EVT VT = Op.getValueType(); 1536 SDValue X = Op.getOperand(0); 1537 SDValue Y = Op.getOperand(1); 1538 1539 // TODO: Should this propagate fast-math-flags? 1540 1541 SDValue Div = DAG.getNode(ISD::FDIV, SL, VT, X, Y); 1542 SDValue Floor = DAG.getNode(ISD::FTRUNC, SL, VT, Div); 1543 SDValue Mul = DAG.getNode(ISD::FMUL, SL, VT, Floor, Y); 1544 1545 return DAG.getNode(ISD::FSUB, SL, VT, X, Mul); 1546 } 1547 1548 SDValue AMDGPUTargetLowering::LowerFCEIL(SDValue Op, SelectionDAG &DAG) const { 1549 SDLoc SL(Op); 1550 SDValue Src = Op.getOperand(0); 1551 1552 // result = trunc(src) 1553 // if (src > 0.0 && src != result) 1554 // result += 1.0 1555 1556 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src); 1557 1558 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64); 1559 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64); 1560 1561 EVT SetCCVT = 1562 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64); 1563 1564 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOGT); 1565 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE); 1566 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc); 1567 1568 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, One, Zero); 1569 // TODO: Should this propagate fast-math-flags? 1570 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add); 1571 } 1572 1573 static SDValue extractF64Exponent(SDValue Hi, SDLoc SL, SelectionDAG &DAG) { 1574 const unsigned FractBits = 52; 1575 const unsigned ExpBits = 11; 1576 1577 SDValue ExpPart = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32, 1578 Hi, 1579 DAG.getConstant(FractBits - 32, SL, MVT::i32), 1580 DAG.getConstant(ExpBits, SL, MVT::i32)); 1581 SDValue Exp = DAG.getNode(ISD::SUB, SL, MVT::i32, ExpPart, 1582 DAG.getConstant(1023, SL, MVT::i32)); 1583 1584 return Exp; 1585 } 1586 1587 SDValue AMDGPUTargetLowering::LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const { 1588 SDLoc SL(Op); 1589 SDValue Src = Op.getOperand(0); 1590 1591 assert(Op.getValueType() == MVT::f64); 1592 1593 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 1594 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 1595 1596 SDValue VecSrc = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src); 1597 1598 // Extract the upper half, since this is where we will find the sign and 1599 // exponent. 1600 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecSrc, One); 1601 1602 SDValue Exp = extractF64Exponent(Hi, SL, DAG); 1603 1604 const unsigned FractBits = 52; 1605 1606 // Extract the sign bit. 1607 const SDValue SignBitMask = DAG.getConstant(UINT32_C(1) << 31, SL, MVT::i32); 1608 SDValue SignBit = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, SignBitMask); 1609 1610 // Extend back to to 64-bits. 1611 SDValue SignBit64 = DAG.getBuildVector(MVT::v2i32, SL, {Zero, SignBit}); 1612 SignBit64 = DAG.getNode(ISD::BITCAST, SL, MVT::i64, SignBit64); 1613 1614 SDValue BcInt = DAG.getNode(ISD::BITCAST, SL, MVT::i64, Src); 1615 const SDValue FractMask 1616 = DAG.getConstant((UINT64_C(1) << FractBits) - 1, SL, MVT::i64); 1617 1618 SDValue Shr = DAG.getNode(ISD::SRA, SL, MVT::i64, FractMask, Exp); 1619 SDValue Not = DAG.getNOT(SL, Shr, MVT::i64); 1620 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, BcInt, Not); 1621 1622 EVT SetCCVT = 1623 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32); 1624 1625 const SDValue FiftyOne = DAG.getConstant(FractBits - 1, SL, MVT::i32); 1626 1627 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT); 1628 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT); 1629 1630 SDValue Tmp1 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpLt0, SignBit64, Tmp0); 1631 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpGt51, BcInt, Tmp1); 1632 1633 return DAG.getNode(ISD::BITCAST, SL, MVT::f64, Tmp2); 1634 } 1635 1636 SDValue AMDGPUTargetLowering::LowerFRINT(SDValue Op, SelectionDAG &DAG) const { 1637 SDLoc SL(Op); 1638 SDValue Src = Op.getOperand(0); 1639 1640 assert(Op.getValueType() == MVT::f64); 1641 1642 APFloat C1Val(APFloat::IEEEdouble, "0x1.0p+52"); 1643 SDValue C1 = DAG.getConstantFP(C1Val, SL, MVT::f64); 1644 SDValue CopySign = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, C1, Src); 1645 1646 // TODO: Should this propagate fast-math-flags? 1647 1648 SDValue Tmp1 = DAG.getNode(ISD::FADD, SL, MVT::f64, Src, CopySign); 1649 SDValue Tmp2 = DAG.getNode(ISD::FSUB, SL, MVT::f64, Tmp1, CopySign); 1650 1651 SDValue Fabs = DAG.getNode(ISD::FABS, SL, MVT::f64, Src); 1652 1653 APFloat C2Val(APFloat::IEEEdouble, "0x1.fffffffffffffp+51"); 1654 SDValue C2 = DAG.getConstantFP(C2Val, SL, MVT::f64); 1655 1656 EVT SetCCVT = 1657 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64); 1658 SDValue Cond = DAG.getSetCC(SL, SetCCVT, Fabs, C2, ISD::SETOGT); 1659 1660 return DAG.getSelect(SL, MVT::f64, Cond, Src, Tmp2); 1661 } 1662 1663 SDValue AMDGPUTargetLowering::LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const { 1664 // FNEARBYINT and FRINT are the same, except in their handling of FP 1665 // exceptions. Those aren't really meaningful for us, and OpenCL only has 1666 // rint, so just treat them as equivalent. 1667 return DAG.getNode(ISD::FRINT, SDLoc(Op), Op.getValueType(), Op.getOperand(0)); 1668 } 1669 1670 // XXX - May require not supporting f32 denormals? 1671 SDValue AMDGPUTargetLowering::LowerFROUND32(SDValue Op, SelectionDAG &DAG) const { 1672 SDLoc SL(Op); 1673 SDValue X = Op.getOperand(0); 1674 1675 SDValue T = DAG.getNode(ISD::FTRUNC, SL, MVT::f32, X); 1676 1677 // TODO: Should this propagate fast-math-flags? 1678 1679 SDValue Diff = DAG.getNode(ISD::FSUB, SL, MVT::f32, X, T); 1680 1681 SDValue AbsDiff = DAG.getNode(ISD::FABS, SL, MVT::f32, Diff); 1682 1683 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f32); 1684 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); 1685 const SDValue Half = DAG.getConstantFP(0.5, SL, MVT::f32); 1686 1687 SDValue SignOne = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f32, One, X); 1688 1689 EVT SetCCVT = 1690 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32); 1691 1692 SDValue Cmp = DAG.getSetCC(SL, SetCCVT, AbsDiff, Half, ISD::SETOGE); 1693 1694 SDValue Sel = DAG.getNode(ISD::SELECT, SL, MVT::f32, Cmp, SignOne, Zero); 1695 1696 return DAG.getNode(ISD::FADD, SL, MVT::f32, T, Sel); 1697 } 1698 1699 SDValue AMDGPUTargetLowering::LowerFROUND64(SDValue Op, SelectionDAG &DAG) const { 1700 SDLoc SL(Op); 1701 SDValue X = Op.getOperand(0); 1702 1703 SDValue L = DAG.getNode(ISD::BITCAST, SL, MVT::i64, X); 1704 1705 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 1706 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 1707 const SDValue NegOne = DAG.getConstant(-1, SL, MVT::i32); 1708 const SDValue FiftyOne = DAG.getConstant(51, SL, MVT::i32); 1709 EVT SetCCVT = 1710 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32); 1711 1712 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X); 1713 1714 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, One); 1715 1716 SDValue Exp = extractF64Exponent(Hi, SL, DAG); 1717 1718 const SDValue Mask = DAG.getConstant(INT64_C(0x000fffffffffffff), SL, 1719 MVT::i64); 1720 1721 SDValue M = DAG.getNode(ISD::SRA, SL, MVT::i64, Mask, Exp); 1722 SDValue D = DAG.getNode(ISD::SRA, SL, MVT::i64, 1723 DAG.getConstant(INT64_C(0x0008000000000000), SL, 1724 MVT::i64), 1725 Exp); 1726 1727 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, L, M); 1728 SDValue Tmp1 = DAG.getSetCC(SL, SetCCVT, 1729 DAG.getConstant(0, SL, MVT::i64), Tmp0, 1730 ISD::SETNE); 1731 1732 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, Tmp1, 1733 D, DAG.getConstant(0, SL, MVT::i64)); 1734 SDValue K = DAG.getNode(ISD::ADD, SL, MVT::i64, L, Tmp2); 1735 1736 K = DAG.getNode(ISD::AND, SL, MVT::i64, K, DAG.getNOT(SL, M, MVT::i64)); 1737 K = DAG.getNode(ISD::BITCAST, SL, MVT::f64, K); 1738 1739 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT); 1740 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT); 1741 SDValue ExpEqNegOne = DAG.getSetCC(SL, SetCCVT, NegOne, Exp, ISD::SETEQ); 1742 1743 SDValue Mag = DAG.getNode(ISD::SELECT, SL, MVT::f64, 1744 ExpEqNegOne, 1745 DAG.getConstantFP(1.0, SL, MVT::f64), 1746 DAG.getConstantFP(0.0, SL, MVT::f64)); 1747 1748 SDValue S = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, Mag, X); 1749 1750 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpLt0, S, K); 1751 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpGt51, X, K); 1752 1753 return K; 1754 } 1755 1756 SDValue AMDGPUTargetLowering::LowerFROUND(SDValue Op, SelectionDAG &DAG) const { 1757 EVT VT = Op.getValueType(); 1758 1759 if (VT == MVT::f32) 1760 return LowerFROUND32(Op, DAG); 1761 1762 if (VT == MVT::f64) 1763 return LowerFROUND64(Op, DAG); 1764 1765 llvm_unreachable("unhandled type"); 1766 } 1767 1768 SDValue AMDGPUTargetLowering::LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const { 1769 SDLoc SL(Op); 1770 SDValue Src = Op.getOperand(0); 1771 1772 // result = trunc(src); 1773 // if (src < 0.0 && src != result) 1774 // result += -1.0. 1775 1776 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src); 1777 1778 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64); 1779 const SDValue NegOne = DAG.getConstantFP(-1.0, SL, MVT::f64); 1780 1781 EVT SetCCVT = 1782 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64); 1783 1784 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOLT); 1785 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE); 1786 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc); 1787 1788 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, NegOne, Zero); 1789 // TODO: Should this propagate fast-math-flags? 1790 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add); 1791 } 1792 1793 SDValue AMDGPUTargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const { 1794 SDLoc SL(Op); 1795 SDValue Src = Op.getOperand(0); 1796 bool ZeroUndef = Op.getOpcode() == ISD::CTLZ_ZERO_UNDEF; 1797 1798 if (ZeroUndef && Src.getValueType() == MVT::i32) 1799 return DAG.getNode(AMDGPUISD::FFBH_U32, SL, MVT::i32, Src); 1800 1801 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src); 1802 1803 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 1804 const SDValue One = DAG.getConstant(1, SL, MVT::i32); 1805 1806 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero); 1807 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One); 1808 1809 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), 1810 *DAG.getContext(), MVT::i32); 1811 1812 SDValue Hi0 = DAG.getSetCC(SL, SetCCVT, Hi, Zero, ISD::SETEQ); 1813 1814 SDValue CtlzLo = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Lo); 1815 SDValue CtlzHi = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Hi); 1816 1817 const SDValue Bits32 = DAG.getConstant(32, SL, MVT::i32); 1818 SDValue Add = DAG.getNode(ISD::ADD, SL, MVT::i32, CtlzLo, Bits32); 1819 1820 // ctlz(x) = hi_32(x) == 0 ? ctlz(lo_32(x)) + 32 : ctlz(hi_32(x)) 1821 SDValue NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32, Hi0, Add, CtlzHi); 1822 1823 if (!ZeroUndef) { 1824 // Test if the full 64-bit input is zero. 1825 1826 // FIXME: DAG combines turn what should be an s_and_b64 into a v_or_b32, 1827 // which we probably don't want. 1828 SDValue Lo0 = DAG.getSetCC(SL, SetCCVT, Lo, Zero, ISD::SETEQ); 1829 SDValue SrcIsZero = DAG.getNode(ISD::AND, SL, SetCCVT, Lo0, Hi0); 1830 1831 // TODO: If i64 setcc is half rate, it can result in 1 fewer instruction 1832 // with the same cycles, otherwise it is slower. 1833 // SDValue SrcIsZero = DAG.getSetCC(SL, SetCCVT, Src, 1834 // DAG.getConstant(0, SL, MVT::i64), ISD::SETEQ); 1835 1836 const SDValue Bits32 = DAG.getConstant(64, SL, MVT::i32); 1837 1838 // The instruction returns -1 for 0 input, but the defined intrinsic 1839 // behavior is to return the number of bits. 1840 NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32, 1841 SrcIsZero, Bits32, NewCtlz); 1842 } 1843 1844 return DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i64, NewCtlz); 1845 } 1846 1847 SDValue AMDGPUTargetLowering::LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG, 1848 bool Signed) const { 1849 // Unsigned 1850 // cul2f(ulong u) 1851 //{ 1852 // uint lz = clz(u); 1853 // uint e = (u != 0) ? 127U + 63U - lz : 0; 1854 // u = (u << lz) & 0x7fffffffffffffffUL; 1855 // ulong t = u & 0xffffffffffUL; 1856 // uint v = (e << 23) | (uint)(u >> 40); 1857 // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U); 1858 // return as_float(v + r); 1859 //} 1860 // Signed 1861 // cl2f(long l) 1862 //{ 1863 // long s = l >> 63; 1864 // float r = cul2f((l + s) ^ s); 1865 // return s ? -r : r; 1866 //} 1867 1868 SDLoc SL(Op); 1869 SDValue Src = Op.getOperand(0); 1870 SDValue L = Src; 1871 1872 SDValue S; 1873 if (Signed) { 1874 const SDValue SignBit = DAG.getConstant(63, SL, MVT::i64); 1875 S = DAG.getNode(ISD::SRA, SL, MVT::i64, L, SignBit); 1876 1877 SDValue LPlusS = DAG.getNode(ISD::ADD, SL, MVT::i64, L, S); 1878 L = DAG.getNode(ISD::XOR, SL, MVT::i64, LPlusS, S); 1879 } 1880 1881 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), 1882 *DAG.getContext(), MVT::f32); 1883 1884 1885 SDValue ZeroI32 = DAG.getConstant(0, SL, MVT::i32); 1886 SDValue ZeroI64 = DAG.getConstant(0, SL, MVT::i64); 1887 SDValue LZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i64, L); 1888 LZ = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LZ); 1889 1890 SDValue K = DAG.getConstant(127U + 63U, SL, MVT::i32); 1891 SDValue E = DAG.getSelect(SL, MVT::i32, 1892 DAG.getSetCC(SL, SetCCVT, L, ZeroI64, ISD::SETNE), 1893 DAG.getNode(ISD::SUB, SL, MVT::i32, K, LZ), 1894 ZeroI32); 1895 1896 SDValue U = DAG.getNode(ISD::AND, SL, MVT::i64, 1897 DAG.getNode(ISD::SHL, SL, MVT::i64, L, LZ), 1898 DAG.getConstant((-1ULL) >> 1, SL, MVT::i64)); 1899 1900 SDValue T = DAG.getNode(ISD::AND, SL, MVT::i64, U, 1901 DAG.getConstant(0xffffffffffULL, SL, MVT::i64)); 1902 1903 SDValue UShl = DAG.getNode(ISD::SRL, SL, MVT::i64, 1904 U, DAG.getConstant(40, SL, MVT::i64)); 1905 1906 SDValue V = DAG.getNode(ISD::OR, SL, MVT::i32, 1907 DAG.getNode(ISD::SHL, SL, MVT::i32, E, DAG.getConstant(23, SL, MVT::i32)), 1908 DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, UShl)); 1909 1910 SDValue C = DAG.getConstant(0x8000000000ULL, SL, MVT::i64); 1911 SDValue RCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETUGT); 1912 SDValue TCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETEQ); 1913 1914 SDValue One = DAG.getConstant(1, SL, MVT::i32); 1915 1916 SDValue VTrunc1 = DAG.getNode(ISD::AND, SL, MVT::i32, V, One); 1917 1918 SDValue R = DAG.getSelect(SL, MVT::i32, 1919 RCmp, 1920 One, 1921 DAG.getSelect(SL, MVT::i32, TCmp, VTrunc1, ZeroI32)); 1922 R = DAG.getNode(ISD::ADD, SL, MVT::i32, V, R); 1923 R = DAG.getNode(ISD::BITCAST, SL, MVT::f32, R); 1924 1925 if (!Signed) 1926 return R; 1927 1928 SDValue RNeg = DAG.getNode(ISD::FNEG, SL, MVT::f32, R); 1929 return DAG.getSelect(SL, MVT::f32, DAG.getSExtOrTrunc(S, SL, SetCCVT), RNeg, R); 1930 } 1931 1932 SDValue AMDGPUTargetLowering::LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG, 1933 bool Signed) const { 1934 SDLoc SL(Op); 1935 SDValue Src = Op.getOperand(0); 1936 1937 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src); 1938 1939 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, 1940 DAG.getConstant(0, SL, MVT::i32)); 1941 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, 1942 DAG.getConstant(1, SL, MVT::i32)); 1943 1944 SDValue CvtHi = DAG.getNode(Signed ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, 1945 SL, MVT::f64, Hi); 1946 1947 SDValue CvtLo = DAG.getNode(ISD::UINT_TO_FP, SL, MVT::f64, Lo); 1948 1949 SDValue LdExp = DAG.getNode(AMDGPUISD::LDEXP, SL, MVT::f64, CvtHi, 1950 DAG.getConstant(32, SL, MVT::i32)); 1951 // TODO: Should this propagate fast-math-flags? 1952 return DAG.getNode(ISD::FADD, SL, MVT::f64, LdExp, CvtLo); 1953 } 1954 1955 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op, 1956 SelectionDAG &DAG) const { 1957 assert(Op.getOperand(0).getValueType() == MVT::i64 && 1958 "operation should be legal"); 1959 1960 EVT DestVT = Op.getValueType(); 1961 if (DestVT == MVT::f64) 1962 return LowerINT_TO_FP64(Op, DAG, false); 1963 1964 if (DestVT == MVT::f32) 1965 return LowerINT_TO_FP32(Op, DAG, false); 1966 1967 return SDValue(); 1968 } 1969 1970 SDValue AMDGPUTargetLowering::LowerSINT_TO_FP(SDValue Op, 1971 SelectionDAG &DAG) const { 1972 assert(Op.getOperand(0).getValueType() == MVT::i64 && 1973 "operation should be legal"); 1974 1975 EVT DestVT = Op.getValueType(); 1976 if (DestVT == MVT::f32) 1977 return LowerINT_TO_FP32(Op, DAG, true); 1978 1979 if (DestVT == MVT::f64) 1980 return LowerINT_TO_FP64(Op, DAG, true); 1981 1982 return SDValue(); 1983 } 1984 1985 SDValue AMDGPUTargetLowering::LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG, 1986 bool Signed) const { 1987 SDLoc SL(Op); 1988 1989 SDValue Src = Op.getOperand(0); 1990 1991 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src); 1992 1993 SDValue K0 = DAG.getConstantFP(BitsToDouble(UINT64_C(0x3df0000000000000)), SL, 1994 MVT::f64); 1995 SDValue K1 = DAG.getConstantFP(BitsToDouble(UINT64_C(0xc1f0000000000000)), SL, 1996 MVT::f64); 1997 // TODO: Should this propagate fast-math-flags? 1998 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, Trunc, K0); 1999 2000 SDValue FloorMul = DAG.getNode(ISD::FFLOOR, SL, MVT::f64, Mul); 2001 2002 2003 SDValue Fma = DAG.getNode(ISD::FMA, SL, MVT::f64, FloorMul, K1, Trunc); 2004 2005 SDValue Hi = DAG.getNode(Signed ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, SL, 2006 MVT::i32, FloorMul); 2007 SDValue Lo = DAG.getNode(ISD::FP_TO_UINT, SL, MVT::i32, Fma); 2008 2009 SDValue Result = DAG.getBuildVector(MVT::v2i32, SL, {Lo, Hi}); 2010 2011 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Result); 2012 } 2013 2014 SDValue AMDGPUTargetLowering::LowerFP_TO_SINT(SDValue Op, 2015 SelectionDAG &DAG) const { 2016 SDValue Src = Op.getOperand(0); 2017 2018 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64) 2019 return LowerFP64_TO_INT(Op, DAG, true); 2020 2021 return SDValue(); 2022 } 2023 2024 SDValue AMDGPUTargetLowering::LowerFP_TO_UINT(SDValue Op, 2025 SelectionDAG &DAG) const { 2026 SDValue Src = Op.getOperand(0); 2027 2028 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64) 2029 return LowerFP64_TO_INT(Op, DAG, false); 2030 2031 return SDValue(); 2032 } 2033 2034 SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, 2035 SelectionDAG &DAG) const { 2036 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 2037 MVT VT = Op.getSimpleValueType(); 2038 MVT ScalarVT = VT.getScalarType(); 2039 2040 if (!VT.isVector()) 2041 return SDValue(); 2042 2043 SDValue Src = Op.getOperand(0); 2044 SDLoc DL(Op); 2045 2046 // TODO: Don't scalarize on Evergreen? 2047 unsigned NElts = VT.getVectorNumElements(); 2048 SmallVector<SDValue, 8> Args; 2049 DAG.ExtractVectorElements(Src, Args, 0, NElts); 2050 2051 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType()); 2052 for (unsigned I = 0; I < NElts; ++I) 2053 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp); 2054 2055 return DAG.getBuildVector(VT, DL, Args); 2056 } 2057 2058 //===----------------------------------------------------------------------===// 2059 // Custom DAG optimizations 2060 //===----------------------------------------------------------------------===// 2061 2062 static bool isU24(SDValue Op, SelectionDAG &DAG) { 2063 APInt KnownZero, KnownOne; 2064 EVT VT = Op.getValueType(); 2065 DAG.computeKnownBits(Op, KnownZero, KnownOne); 2066 2067 return (VT.getSizeInBits() - KnownZero.countLeadingOnes()) <= 24; 2068 } 2069 2070 static bool isI24(SDValue Op, SelectionDAG &DAG) { 2071 EVT VT = Op.getValueType(); 2072 2073 // In order for this to be a signed 24-bit value, bit 23, must 2074 // be a sign bit. 2075 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated 2076 // as unsigned 24-bit values. 2077 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24; 2078 } 2079 2080 static void simplifyI24(SDValue Op, TargetLowering::DAGCombinerInfo &DCI) { 2081 2082 SelectionDAG &DAG = DCI.DAG; 2083 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 2084 EVT VT = Op.getValueType(); 2085 2086 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24); 2087 APInt KnownZero, KnownOne; 2088 TargetLowering::TargetLoweringOpt TLO(DAG, true, true); 2089 if (TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) 2090 DCI.CommitTargetLoweringOpt(TLO); 2091 } 2092 2093 template <typename IntTy> 2094 static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0, 2095 uint32_t Offset, uint32_t Width, SDLoc DL) { 2096 if (Width + Offset < 32) { 2097 uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width); 2098 IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width); 2099 return DAG.getConstant(Result, DL, MVT::i32); 2100 } 2101 2102 return DAG.getConstant(Src0 >> Offset, DL, MVT::i32); 2103 } 2104 2105 static bool usesAllNormalStores(SDNode *LoadVal) { 2106 for (SDNode::use_iterator I = LoadVal->use_begin(); !I.atEnd(); ++I) { 2107 if (!ISD::isNormalStore(*I)) 2108 return false; 2109 } 2110 2111 return true; 2112 } 2113 2114 // If we have a copy of an illegal type, replace it with a load / store of an 2115 // equivalently sized legal type. This avoids intermediate bit pack / unpack 2116 // instructions emitted when handling extloads and truncstores. Ideally we could 2117 // recognize the pack / unpack pattern to eliminate it. 2118 SDValue AMDGPUTargetLowering::performStoreCombine(SDNode *N, 2119 DAGCombinerInfo &DCI) const { 2120 if (!DCI.isBeforeLegalize()) 2121 return SDValue(); 2122 2123 StoreSDNode *SN = cast<StoreSDNode>(N); 2124 SDValue Value = SN->getValue(); 2125 EVT VT = Value.getValueType(); 2126 2127 if (isTypeLegal(VT) || SN->isVolatile() || 2128 !ISD::isNormalLoad(Value.getNode()) || VT.getSizeInBits() < 8) 2129 return SDValue(); 2130 2131 LoadSDNode *LoadVal = cast<LoadSDNode>(Value); 2132 if (LoadVal->isVolatile() || !usesAllNormalStores(LoadVal)) 2133 return SDValue(); 2134 2135 EVT MemVT = LoadVal->getMemoryVT(); 2136 2137 SDLoc SL(N); 2138 SelectionDAG &DAG = DCI.DAG; 2139 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), MemVT); 2140 2141 SDValue NewLoad = DAG.getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, 2142 LoadVT, SL, 2143 LoadVal->getChain(), 2144 LoadVal->getBasePtr(), 2145 LoadVal->getOffset(), 2146 LoadVT, 2147 LoadVal->getMemOperand()); 2148 2149 SDValue CastLoad = DAG.getNode(ISD::BITCAST, SL, VT, NewLoad.getValue(0)); 2150 DCI.CombineTo(LoadVal, CastLoad, NewLoad.getValue(1), false); 2151 2152 return DAG.getStore(SN->getChain(), SL, NewLoad, 2153 SN->getBasePtr(), SN->getMemOperand()); 2154 } 2155 2156 // TODO: Should repeat for other bit ops. 2157 SDValue AMDGPUTargetLowering::performAndCombine(SDNode *N, 2158 DAGCombinerInfo &DCI) const { 2159 if (N->getValueType(0) != MVT::i64) 2160 return SDValue(); 2161 2162 // Break up 64-bit and of a constant into two 32-bit ands. This will typically 2163 // happen anyway for a VALU 64-bit and. This exposes other 32-bit integer 2164 // combine opportunities since most 64-bit operations are decomposed this way. 2165 // TODO: We won't want this for SALU especially if it is an inline immediate. 2166 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2167 if (!RHS) 2168 return SDValue(); 2169 2170 uint64_t Val = RHS->getZExtValue(); 2171 if (Lo_32(Val) != 0 && Hi_32(Val) != 0 && !RHS->hasOneUse()) { 2172 // If either half of the constant is 0, this is really a 32-bit and, so 2173 // split it. If we can re-use the full materialized constant, keep it. 2174 return SDValue(); 2175 } 2176 2177 SDLoc SL(N); 2178 SelectionDAG &DAG = DCI.DAG; 2179 2180 SDValue Lo, Hi; 2181 std::tie(Lo, Hi) = split64BitValue(N->getOperand(0), DAG); 2182 2183 SDValue LoRHS = DAG.getConstant(Lo_32(Val), SL, MVT::i32); 2184 SDValue HiRHS = DAG.getConstant(Hi_32(Val), SL, MVT::i32); 2185 2186 SDValue LoAnd = DAG.getNode(ISD::AND, SL, MVT::i32, Lo, LoRHS); 2187 SDValue HiAnd = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, HiRHS); 2188 2189 // Re-visit the ands. It's possible we eliminated one of them and it could 2190 // simplify the vector. 2191 DCI.AddToWorklist(Lo.getNode()); 2192 DCI.AddToWorklist(Hi.getNode()); 2193 2194 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {LoAnd, HiAnd}); 2195 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 2196 } 2197 2198 SDValue AMDGPUTargetLowering::performShlCombine(SDNode *N, 2199 DAGCombinerInfo &DCI) const { 2200 if (N->getValueType(0) != MVT::i64) 2201 return SDValue(); 2202 2203 // i64 (shl x, C) -> (build_pair 0, (shl x, C -32)) 2204 2205 // On some subtargets, 64-bit shift is a quarter rate instruction. In the 2206 // common case, splitting this into a move and a 32-bit shift is faster and 2207 // the same code size. 2208 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2209 if (!RHS) 2210 return SDValue(); 2211 2212 unsigned RHSVal = RHS->getZExtValue(); 2213 if (RHSVal < 32) 2214 return SDValue(); 2215 2216 SDValue LHS = N->getOperand(0); 2217 2218 SDLoc SL(N); 2219 SelectionDAG &DAG = DCI.DAG; 2220 2221 SDValue ShiftAmt = DAG.getConstant(RHSVal - 32, SL, MVT::i32); 2222 2223 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS); 2224 SDValue NewShift = DAG.getNode(ISD::SHL, SL, MVT::i32, Lo, ShiftAmt); 2225 2226 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 2227 2228 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {Zero, NewShift}); 2229 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 2230 } 2231 2232 SDValue AMDGPUTargetLowering::performSraCombine(SDNode *N, 2233 DAGCombinerInfo &DCI) const { 2234 if (N->getValueType(0) != MVT::i64) 2235 return SDValue(); 2236 2237 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2238 if (!RHS) 2239 return SDValue(); 2240 2241 SelectionDAG &DAG = DCI.DAG; 2242 SDLoc SL(N); 2243 unsigned RHSVal = RHS->getZExtValue(); 2244 2245 // (sra i64:x, 32) -> build_pair x, (sra hi_32(x), 31) 2246 if (RHSVal == 32) { 2247 SDValue Hi = getHiHalf64(N->getOperand(0), DAG); 2248 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi, 2249 DAG.getConstant(31, SL, MVT::i32)); 2250 2251 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {Hi, NewShift}); 2252 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec); 2253 } 2254 2255 // (sra i64:x, 63) -> build_pair (sra hi_32(x), 31), (sra hi_32(x), 31) 2256 if (RHSVal == 63) { 2257 SDValue Hi = getHiHalf64(N->getOperand(0), DAG); 2258 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi, 2259 DAG.getConstant(31, SL, MVT::i32)); 2260 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, NewShift}); 2261 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec); 2262 } 2263 2264 return SDValue(); 2265 } 2266 2267 SDValue AMDGPUTargetLowering::performSrlCombine(SDNode *N, 2268 DAGCombinerInfo &DCI) const { 2269 if (N->getValueType(0) != MVT::i64) 2270 return SDValue(); 2271 2272 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2273 if (!RHS) 2274 return SDValue(); 2275 2276 unsigned ShiftAmt = RHS->getZExtValue(); 2277 if (ShiftAmt < 32) 2278 return SDValue(); 2279 2280 // srl i64:x, C for C >= 32 2281 // => 2282 // build_pair (srl hi_32(x), C - 32), 0 2283 2284 SelectionDAG &DAG = DCI.DAG; 2285 SDLoc SL(N); 2286 2287 SDValue One = DAG.getConstant(1, SL, MVT::i32); 2288 SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 2289 2290 SDValue VecOp = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, N->getOperand(0)); 2291 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, 2292 VecOp, One); 2293 2294 SDValue NewConst = DAG.getConstant(ShiftAmt - 32, SL, MVT::i32); 2295 SDValue NewShift = DAG.getNode(ISD::SRL, SL, MVT::i32, Hi, NewConst); 2296 2297 SDValue BuildPair = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, Zero}); 2298 2299 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildPair); 2300 } 2301 2302 SDValue AMDGPUTargetLowering::performMulCombine(SDNode *N, 2303 DAGCombinerInfo &DCI) const { 2304 EVT VT = N->getValueType(0); 2305 2306 if (VT.isVector() || VT.getSizeInBits() > 32) 2307 return SDValue(); 2308 2309 SelectionDAG &DAG = DCI.DAG; 2310 SDLoc DL(N); 2311 2312 SDValue N0 = N->getOperand(0); 2313 SDValue N1 = N->getOperand(1); 2314 SDValue Mul; 2315 2316 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) { 2317 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32); 2318 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32); 2319 Mul = DAG.getNode(AMDGPUISD::MUL_U24, DL, MVT::i32, N0, N1); 2320 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) { 2321 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32); 2322 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32); 2323 Mul = DAG.getNode(AMDGPUISD::MUL_I24, DL, MVT::i32, N0, N1); 2324 } else { 2325 return SDValue(); 2326 } 2327 2328 // We need to use sext even for MUL_U24, because MUL_U24 is used 2329 // for signed multiply of 8 and 16-bit types. 2330 return DAG.getSExtOrTrunc(Mul, DL, VT); 2331 } 2332 2333 static bool isNegativeOne(SDValue Val) { 2334 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) 2335 return C->isAllOnesValue(); 2336 return false; 2337 } 2338 2339 static bool isCtlzOpc(unsigned Opc) { 2340 return Opc == ISD::CTLZ || Opc == ISD::CTLZ_ZERO_UNDEF; 2341 } 2342 2343 // Get FFBH node if the incoming op may have been type legalized from a smaller 2344 // type VT. 2345 // Need to match pre-legalized type because the generic legalization inserts the 2346 // add/sub between the select and compare. 2347 static SDValue getFFBH_U32(const TargetLowering &TLI, 2348 SelectionDAG &DAG, SDLoc SL, SDValue Op) { 2349 EVT VT = Op.getValueType(); 2350 EVT LegalVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT); 2351 if (LegalVT != MVT::i32) 2352 return SDValue(); 2353 2354 if (VT != MVT::i32) 2355 Op = DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i32, Op); 2356 2357 SDValue FFBH = DAG.getNode(AMDGPUISD::FFBH_U32, SL, MVT::i32, Op); 2358 if (VT != MVT::i32) 2359 FFBH = DAG.getNode(ISD::TRUNCATE, SL, VT, FFBH); 2360 2361 return FFBH; 2362 } 2363 2364 // The native instructions return -1 on 0 input. Optimize out a select that 2365 // produces -1 on 0. 2366 // 2367 // TODO: If zero is not undef, we could also do this if the output is compared 2368 // against the bitwidth. 2369 // 2370 // TODO: Should probably combine against FFBH_U32 instead of ctlz directly. 2371 SDValue AMDGPUTargetLowering::performCtlzCombine(SDLoc SL, 2372 SDValue Cond, 2373 SDValue LHS, 2374 SDValue RHS, 2375 DAGCombinerInfo &DCI) const { 2376 ConstantSDNode *CmpRhs = dyn_cast<ConstantSDNode>(Cond.getOperand(1)); 2377 if (!CmpRhs || !CmpRhs->isNullValue()) 2378 return SDValue(); 2379 2380 SelectionDAG &DAG = DCI.DAG; 2381 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); 2382 SDValue CmpLHS = Cond.getOperand(0); 2383 2384 // select (setcc x, 0, eq), -1, (ctlz_zero_undef x) -> ffbh_u32 x 2385 if (CCOpcode == ISD::SETEQ && 2386 isCtlzOpc(RHS.getOpcode()) && 2387 RHS.getOperand(0) == CmpLHS && 2388 isNegativeOne(LHS)) { 2389 return getFFBH_U32(*this, DAG, SL, CmpLHS); 2390 } 2391 2392 // select (setcc x, 0, ne), (ctlz_zero_undef x), -1 -> ffbh_u32 x 2393 if (CCOpcode == ISD::SETNE && 2394 isCtlzOpc(LHS.getOpcode()) && 2395 LHS.getOperand(0) == CmpLHS && 2396 isNegativeOne(RHS)) { 2397 return getFFBH_U32(*this, DAG, SL, CmpLHS); 2398 } 2399 2400 return SDValue(); 2401 } 2402 2403 SDValue AMDGPUTargetLowering::performSelectCombine(SDNode *N, 2404 DAGCombinerInfo &DCI) const { 2405 SDValue Cond = N->getOperand(0); 2406 if (Cond.getOpcode() != ISD::SETCC) 2407 return SDValue(); 2408 2409 EVT VT = N->getValueType(0); 2410 SDValue LHS = Cond.getOperand(0); 2411 SDValue RHS = Cond.getOperand(1); 2412 SDValue CC = Cond.getOperand(2); 2413 2414 SDValue True = N->getOperand(1); 2415 SDValue False = N->getOperand(2); 2416 2417 if (VT == MVT::f32 && Cond.hasOneUse()) { 2418 SDValue MinMax 2419 = CombineFMinMaxLegacy(SDLoc(N), VT, LHS, RHS, True, False, CC, DCI); 2420 // Revisit this node so we can catch min3/max3/med3 patterns. 2421 //DCI.AddToWorklist(MinMax.getNode()); 2422 return MinMax; 2423 } 2424 2425 // There's no reason to not do this if the condition has other uses. 2426 return performCtlzCombine(SDLoc(N), Cond, True, False, DCI); 2427 } 2428 2429 SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N, 2430 DAGCombinerInfo &DCI) const { 2431 SelectionDAG &DAG = DCI.DAG; 2432 SDLoc DL(N); 2433 2434 switch(N->getOpcode()) { 2435 default: 2436 break; 2437 case ISD::BITCAST: { 2438 EVT DestVT = N->getValueType(0); 2439 if (DestVT.getSizeInBits() != 64 && !DestVT.isVector()) 2440 break; 2441 2442 // Fold bitcasts of constants. 2443 // 2444 // v2i32 (bitcast i64:k) -> build_vector lo_32(k), hi_32(k) 2445 // TODO: Generalize and move to DAGCombiner 2446 SDValue Src = N->getOperand(0); 2447 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src)) { 2448 assert(Src.getValueType() == MVT::i64); 2449 SDLoc SL(N); 2450 uint64_t CVal = C->getZExtValue(); 2451 return DAG.getNode(ISD::BUILD_VECTOR, SL, DestVT, 2452 DAG.getConstant(Lo_32(CVal), SL, MVT::i32), 2453 DAG.getConstant(Hi_32(CVal), SL, MVT::i32)); 2454 } 2455 2456 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Src)) { 2457 const APInt &Val = C->getValueAPF().bitcastToAPInt(); 2458 SDLoc SL(N); 2459 uint64_t CVal = Val.getZExtValue(); 2460 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, 2461 DAG.getConstant(Lo_32(CVal), SL, MVT::i32), 2462 DAG.getConstant(Hi_32(CVal), SL, MVT::i32)); 2463 2464 return DAG.getNode(ISD::BITCAST, SL, DestVT, Vec); 2465 } 2466 2467 break; 2468 } 2469 case ISD::SHL: { 2470 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 2471 break; 2472 2473 return performShlCombine(N, DCI); 2474 } 2475 case ISD::SRL: { 2476 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 2477 break; 2478 2479 return performSrlCombine(N, DCI); 2480 } 2481 case ISD::SRA: { 2482 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 2483 break; 2484 2485 return performSraCombine(N, DCI); 2486 } 2487 case ISD::AND: { 2488 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 2489 break; 2490 2491 return performAndCombine(N, DCI); 2492 } 2493 case ISD::MUL: 2494 return performMulCombine(N, DCI); 2495 case AMDGPUISD::MUL_I24: 2496 case AMDGPUISD::MUL_U24: { 2497 SDValue N0 = N->getOperand(0); 2498 SDValue N1 = N->getOperand(1); 2499 simplifyI24(N0, DCI); 2500 simplifyI24(N1, DCI); 2501 return SDValue(); 2502 } 2503 case ISD::SELECT: 2504 return performSelectCombine(N, DCI); 2505 case AMDGPUISD::BFE_I32: 2506 case AMDGPUISD::BFE_U32: { 2507 assert(!N->getValueType(0).isVector() && 2508 "Vector handling of BFE not implemented"); 2509 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2)); 2510 if (!Width) 2511 break; 2512 2513 uint32_t WidthVal = Width->getZExtValue() & 0x1f; 2514 if (WidthVal == 0) 2515 return DAG.getConstant(0, DL, MVT::i32); 2516 2517 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2518 if (!Offset) 2519 break; 2520 2521 SDValue BitsFrom = N->getOperand(0); 2522 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f; 2523 2524 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32; 2525 2526 if (OffsetVal == 0) { 2527 // This is already sign / zero extended, so try to fold away extra BFEs. 2528 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal); 2529 2530 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom); 2531 if (OpSignBits >= SignBits) 2532 return BitsFrom; 2533 2534 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal); 2535 if (Signed) { 2536 // This is a sign_extend_inreg. Replace it to take advantage of existing 2537 // DAG Combines. If not eliminated, we will match back to BFE during 2538 // selection. 2539 2540 // TODO: The sext_inreg of extended types ends, although we can could 2541 // handle them in a single BFE. 2542 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom, 2543 DAG.getValueType(SmallVT)); 2544 } 2545 2546 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT); 2547 } 2548 2549 if (ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(BitsFrom)) { 2550 if (Signed) { 2551 return constantFoldBFE<int32_t>(DAG, 2552 CVal->getSExtValue(), 2553 OffsetVal, 2554 WidthVal, 2555 DL); 2556 } 2557 2558 return constantFoldBFE<uint32_t>(DAG, 2559 CVal->getZExtValue(), 2560 OffsetVal, 2561 WidthVal, 2562 DL); 2563 } 2564 2565 if ((OffsetVal + WidthVal) >= 32) { 2566 SDValue ShiftVal = DAG.getConstant(OffsetVal, DL, MVT::i32); 2567 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32, 2568 BitsFrom, ShiftVal); 2569 } 2570 2571 if (BitsFrom.hasOneUse()) { 2572 APInt Demanded = APInt::getBitsSet(32, 2573 OffsetVal, 2574 OffsetVal + WidthVal); 2575 2576 APInt KnownZero, KnownOne; 2577 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), 2578 !DCI.isBeforeLegalizeOps()); 2579 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 2580 if (TLO.ShrinkDemandedConstant(BitsFrom, Demanded) || 2581 TLI.SimplifyDemandedBits(BitsFrom, Demanded, 2582 KnownZero, KnownOne, TLO)) { 2583 DCI.CommitTargetLoweringOpt(TLO); 2584 } 2585 } 2586 2587 break; 2588 } 2589 2590 case ISD::STORE: 2591 return performStoreCombine(N, DCI); 2592 } 2593 return SDValue(); 2594 } 2595 2596 //===----------------------------------------------------------------------===// 2597 // Helper functions 2598 //===----------------------------------------------------------------------===// 2599 2600 void AMDGPUTargetLowering::getOriginalFunctionArgs( 2601 SelectionDAG &DAG, 2602 const Function *F, 2603 const SmallVectorImpl<ISD::InputArg> &Ins, 2604 SmallVectorImpl<ISD::InputArg> &OrigIns) const { 2605 2606 for (unsigned i = 0, e = Ins.size(); i < e; ++i) { 2607 if (Ins[i].ArgVT == Ins[i].VT) { 2608 OrigIns.push_back(Ins[i]); 2609 continue; 2610 } 2611 2612 EVT VT; 2613 if (Ins[i].ArgVT.isVector() && !Ins[i].VT.isVector()) { 2614 // Vector has been split into scalars. 2615 VT = Ins[i].ArgVT.getVectorElementType(); 2616 } else if (Ins[i].VT.isVector() && Ins[i].ArgVT.isVector() && 2617 Ins[i].ArgVT.getVectorElementType() != 2618 Ins[i].VT.getVectorElementType()) { 2619 // Vector elements have been promoted 2620 VT = Ins[i].ArgVT; 2621 } else { 2622 // Vector has been spilt into smaller vectors. 2623 VT = Ins[i].VT; 2624 } 2625 2626 ISD::InputArg Arg(Ins[i].Flags, VT, VT, Ins[i].Used, 2627 Ins[i].OrigArgIndex, Ins[i].PartOffset); 2628 OrigIns.push_back(Arg); 2629 } 2630 } 2631 2632 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG, 2633 const TargetRegisterClass *RC, 2634 unsigned Reg, EVT VT) const { 2635 MachineFunction &MF = DAG.getMachineFunction(); 2636 MachineRegisterInfo &MRI = MF.getRegInfo(); 2637 unsigned VirtualRegister; 2638 if (!MRI.isLiveIn(Reg)) { 2639 VirtualRegister = MRI.createVirtualRegister(RC); 2640 MRI.addLiveIn(Reg, VirtualRegister); 2641 } else { 2642 VirtualRegister = MRI.getLiveInVirtReg(Reg); 2643 } 2644 return DAG.getRegister(VirtualRegister, VT); 2645 } 2646 2647 uint32_t AMDGPUTargetLowering::getImplicitParameterOffset( 2648 const AMDGPUMachineFunction *MFI, const ImplicitParameter Param) const { 2649 uint64_t ArgOffset = MFI->ABIArgOffset; 2650 switch (Param) { 2651 case GRID_DIM: 2652 return ArgOffset; 2653 case GRID_OFFSET: 2654 return ArgOffset + 4; 2655 } 2656 llvm_unreachable("unexpected implicit parameter type"); 2657 } 2658 2659 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node; 2660 2661 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const { 2662 switch ((AMDGPUISD::NodeType)Opcode) { 2663 case AMDGPUISD::FIRST_NUMBER: break; 2664 // AMDIL DAG nodes 2665 NODE_NAME_CASE(CALL); 2666 NODE_NAME_CASE(UMUL); 2667 NODE_NAME_CASE(RET_FLAG); 2668 NODE_NAME_CASE(BRANCH_COND); 2669 2670 // AMDGPU DAG nodes 2671 NODE_NAME_CASE(DWORDADDR) 2672 NODE_NAME_CASE(FRACT) 2673 NODE_NAME_CASE(CLAMP) 2674 NODE_NAME_CASE(COS_HW) 2675 NODE_NAME_CASE(SIN_HW) 2676 NODE_NAME_CASE(FMAX_LEGACY) 2677 NODE_NAME_CASE(FMIN_LEGACY) 2678 NODE_NAME_CASE(FMAX3) 2679 NODE_NAME_CASE(SMAX3) 2680 NODE_NAME_CASE(UMAX3) 2681 NODE_NAME_CASE(FMIN3) 2682 NODE_NAME_CASE(SMIN3) 2683 NODE_NAME_CASE(UMIN3) 2684 NODE_NAME_CASE(FMED3) 2685 NODE_NAME_CASE(SMED3) 2686 NODE_NAME_CASE(UMED3) 2687 NODE_NAME_CASE(URECIP) 2688 NODE_NAME_CASE(DIV_SCALE) 2689 NODE_NAME_CASE(DIV_FMAS) 2690 NODE_NAME_CASE(DIV_FIXUP) 2691 NODE_NAME_CASE(TRIG_PREOP) 2692 NODE_NAME_CASE(RCP) 2693 NODE_NAME_CASE(RSQ) 2694 NODE_NAME_CASE(RSQ_LEGACY) 2695 NODE_NAME_CASE(RSQ_CLAMP) 2696 NODE_NAME_CASE(LDEXP) 2697 NODE_NAME_CASE(FP_CLASS) 2698 NODE_NAME_CASE(DOT4) 2699 NODE_NAME_CASE(CARRY) 2700 NODE_NAME_CASE(BORROW) 2701 NODE_NAME_CASE(BFE_U32) 2702 NODE_NAME_CASE(BFE_I32) 2703 NODE_NAME_CASE(BFI) 2704 NODE_NAME_CASE(BFM) 2705 NODE_NAME_CASE(FFBH_U32) 2706 NODE_NAME_CASE(MUL_U24) 2707 NODE_NAME_CASE(MUL_I24) 2708 NODE_NAME_CASE(MAD_U24) 2709 NODE_NAME_CASE(MAD_I24) 2710 NODE_NAME_CASE(TEXTURE_FETCH) 2711 NODE_NAME_CASE(EXPORT) 2712 NODE_NAME_CASE(CONST_ADDRESS) 2713 NODE_NAME_CASE(REGISTER_LOAD) 2714 NODE_NAME_CASE(REGISTER_STORE) 2715 NODE_NAME_CASE(LOAD_INPUT) 2716 NODE_NAME_CASE(SAMPLE) 2717 NODE_NAME_CASE(SAMPLEB) 2718 NODE_NAME_CASE(SAMPLED) 2719 NODE_NAME_CASE(SAMPLEL) 2720 NODE_NAME_CASE(CVT_F32_UBYTE0) 2721 NODE_NAME_CASE(CVT_F32_UBYTE1) 2722 NODE_NAME_CASE(CVT_F32_UBYTE2) 2723 NODE_NAME_CASE(CVT_F32_UBYTE3) 2724 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR) 2725 NODE_NAME_CASE(CONST_DATA_PTR) 2726 case AMDGPUISD::FIRST_MEM_OPCODE_NUMBER: break; 2727 NODE_NAME_CASE(SENDMSG) 2728 NODE_NAME_CASE(INTERP_MOV) 2729 NODE_NAME_CASE(INTERP_P1) 2730 NODE_NAME_CASE(INTERP_P2) 2731 NODE_NAME_CASE(STORE_MSKOR) 2732 NODE_NAME_CASE(LOAD_CONSTANT) 2733 NODE_NAME_CASE(TBUFFER_STORE_FORMAT) 2734 NODE_NAME_CASE(ATOMIC_CMP_SWAP) 2735 NODE_NAME_CASE(ATOMIC_INC) 2736 NODE_NAME_CASE(ATOMIC_DEC) 2737 case AMDGPUISD::LAST_AMDGPU_ISD_NUMBER: break; 2738 } 2739 return nullptr; 2740 } 2741 2742 SDValue AMDGPUTargetLowering::getRsqrtEstimate(SDValue Operand, 2743 DAGCombinerInfo &DCI, 2744 unsigned &RefinementSteps, 2745 bool &UseOneConstNR) const { 2746 SelectionDAG &DAG = DCI.DAG; 2747 EVT VT = Operand.getValueType(); 2748 2749 if (VT == MVT::f32) { 2750 RefinementSteps = 0; 2751 return DAG.getNode(AMDGPUISD::RSQ, SDLoc(Operand), VT, Operand); 2752 } 2753 2754 // TODO: There is also f64 rsq instruction, but the documentation is less 2755 // clear on its precision. 2756 2757 return SDValue(); 2758 } 2759 2760 SDValue AMDGPUTargetLowering::getRecipEstimate(SDValue Operand, 2761 DAGCombinerInfo &DCI, 2762 unsigned &RefinementSteps) const { 2763 SelectionDAG &DAG = DCI.DAG; 2764 EVT VT = Operand.getValueType(); 2765 2766 if (VT == MVT::f32) { 2767 // Reciprocal, < 1 ulp error. 2768 // 2769 // This reciprocal approximation converges to < 0.5 ulp error with one 2770 // newton rhapson performed with two fused multiple adds (FMAs). 2771 2772 RefinementSteps = 0; 2773 return DAG.getNode(AMDGPUISD::RCP, SDLoc(Operand), VT, Operand); 2774 } 2775 2776 // TODO: There is also f64 rcp instruction, but the documentation is less 2777 // clear on its precision. 2778 2779 return SDValue(); 2780 } 2781 2782 void AMDGPUTargetLowering::computeKnownBitsForTargetNode( 2783 const SDValue Op, 2784 APInt &KnownZero, 2785 APInt &KnownOne, 2786 const SelectionDAG &DAG, 2787 unsigned Depth) const { 2788 2789 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); // Don't know anything. 2790 2791 APInt KnownZero2; 2792 APInt KnownOne2; 2793 unsigned Opc = Op.getOpcode(); 2794 2795 switch (Opc) { 2796 default: 2797 break; 2798 case AMDGPUISD::CARRY: 2799 case AMDGPUISD::BORROW: { 2800 KnownZero = APInt::getHighBitsSet(32, 31); 2801 break; 2802 } 2803 2804 case AMDGPUISD::BFE_I32: 2805 case AMDGPUISD::BFE_U32: { 2806 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 2807 if (!CWidth) 2808 return; 2809 2810 unsigned BitWidth = 32; 2811 uint32_t Width = CWidth->getZExtValue() & 0x1f; 2812 2813 if (Opc == AMDGPUISD::BFE_U32) 2814 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - Width); 2815 2816 break; 2817 } 2818 } 2819 } 2820 2821 unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode( 2822 SDValue Op, 2823 const SelectionDAG &DAG, 2824 unsigned Depth) const { 2825 switch (Op.getOpcode()) { 2826 case AMDGPUISD::BFE_I32: { 2827 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 2828 if (!Width) 2829 return 1; 2830 2831 unsigned SignBits = 32 - Width->getZExtValue() + 1; 2832 if (!isNullConstant(Op.getOperand(1))) 2833 return SignBits; 2834 2835 // TODO: Could probably figure something out with non-0 offsets. 2836 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1); 2837 return std::max(SignBits, Op0SignBits); 2838 } 2839 2840 case AMDGPUISD::BFE_U32: { 2841 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 2842 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1; 2843 } 2844 2845 case AMDGPUISD::CARRY: 2846 case AMDGPUISD::BORROW: 2847 return 31; 2848 2849 default: 2850 return 1; 2851 } 2852 } 2853