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