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