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