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 HiOrLo = isCtlzOpc(Op.getOpcode()) ? Hi : Lo; 2212 SDValue Hi0orLo0 = DAG.getSetCC(SL, SetCCVT, HiOrLo, Zero, ISD::SETEQ); 2213 2214 SDValue OprLo = DAG.getNode(ISDOpc, SL, MVT::i32, Lo); 2215 SDValue OprHi = DAG.getNode(ISDOpc, SL, MVT::i32, Hi); 2216 2217 const SDValue Bits32 = DAG.getConstant(32, SL, MVT::i32); 2218 SDValue Add, NewOpr; 2219 if (isCtlzOpc(Op.getOpcode())) { 2220 Add = DAG.getNode(ISD::ADD, SL, MVT::i32, OprLo, Bits32); 2221 // ctlz(x) = hi_32(x) == 0 ? ctlz(lo_32(x)) + 32 : ctlz(hi_32(x)) 2222 NewOpr = DAG.getNode(ISD::SELECT, SL, MVT::i32, Hi0orLo0, Add, OprHi); 2223 } else { 2224 Add = DAG.getNode(ISD::ADD, SL, MVT::i32, OprHi, Bits32); 2225 // cttz(x) = lo_32(x) == 0 ? cttz(hi_32(x)) + 32 : cttz(lo_32(x)) 2226 NewOpr = DAG.getNode(ISD::SELECT, SL, MVT::i32, Hi0orLo0, Add, OprLo); 2227 } 2228 2229 if (!ZeroUndef) { 2230 // Test if the full 64-bit input is zero. 2231 2232 // FIXME: DAG combines turn what should be an s_and_b64 into a v_or_b32, 2233 // which we probably don't want. 2234 SDValue LoOrHi = isCtlzOpc(Op.getOpcode()) ? Lo : Hi; 2235 SDValue Lo0OrHi0 = DAG.getSetCC(SL, SetCCVT, LoOrHi, Zero, ISD::SETEQ); 2236 SDValue SrcIsZero = DAG.getNode(ISD::AND, SL, SetCCVT, Lo0OrHi0, Hi0orLo0); 2237 2238 // TODO: If i64 setcc is half rate, it can result in 1 fewer instruction 2239 // with the same cycles, otherwise it is slower. 2240 // SDValue SrcIsZero = DAG.getSetCC(SL, SetCCVT, Src, 2241 // DAG.getConstant(0, SL, MVT::i64), ISD::SETEQ); 2242 2243 const SDValue Bits32 = DAG.getConstant(64, SL, MVT::i32); 2244 2245 // The instruction returns -1 for 0 input, but the defined intrinsic 2246 // behavior is to return the number of bits. 2247 NewOpr = DAG.getNode(ISD::SELECT, SL, MVT::i32, 2248 SrcIsZero, Bits32, NewOpr); 2249 } 2250 2251 return DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i64, NewOpr); 2252 } 2253 2254 SDValue AMDGPUTargetLowering::LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG, 2255 bool Signed) const { 2256 // Unsigned 2257 // cul2f(ulong u) 2258 //{ 2259 // uint lz = clz(u); 2260 // uint e = (u != 0) ? 127U + 63U - lz : 0; 2261 // u = (u << lz) & 0x7fffffffffffffffUL; 2262 // ulong t = u & 0xffffffffffUL; 2263 // uint v = (e << 23) | (uint)(u >> 40); 2264 // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U); 2265 // return as_float(v + r); 2266 //} 2267 // Signed 2268 // cl2f(long l) 2269 //{ 2270 // long s = l >> 63; 2271 // float r = cul2f((l + s) ^ s); 2272 // return s ? -r : r; 2273 //} 2274 2275 SDLoc SL(Op); 2276 SDValue Src = Op.getOperand(0); 2277 SDValue L = Src; 2278 2279 SDValue S; 2280 if (Signed) { 2281 const SDValue SignBit = DAG.getConstant(63, SL, MVT::i64); 2282 S = DAG.getNode(ISD::SRA, SL, MVT::i64, L, SignBit); 2283 2284 SDValue LPlusS = DAG.getNode(ISD::ADD, SL, MVT::i64, L, S); 2285 L = DAG.getNode(ISD::XOR, SL, MVT::i64, LPlusS, S); 2286 } 2287 2288 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), 2289 *DAG.getContext(), MVT::f32); 2290 2291 2292 SDValue ZeroI32 = DAG.getConstant(0, SL, MVT::i32); 2293 SDValue ZeroI64 = DAG.getConstant(0, SL, MVT::i64); 2294 SDValue LZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i64, L); 2295 LZ = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LZ); 2296 2297 SDValue K = DAG.getConstant(127U + 63U, SL, MVT::i32); 2298 SDValue E = DAG.getSelect(SL, MVT::i32, 2299 DAG.getSetCC(SL, SetCCVT, L, ZeroI64, ISD::SETNE), 2300 DAG.getNode(ISD::SUB, SL, MVT::i32, K, LZ), 2301 ZeroI32); 2302 2303 SDValue U = DAG.getNode(ISD::AND, SL, MVT::i64, 2304 DAG.getNode(ISD::SHL, SL, MVT::i64, L, LZ), 2305 DAG.getConstant((-1ULL) >> 1, SL, MVT::i64)); 2306 2307 SDValue T = DAG.getNode(ISD::AND, SL, MVT::i64, U, 2308 DAG.getConstant(0xffffffffffULL, SL, MVT::i64)); 2309 2310 SDValue UShl = DAG.getNode(ISD::SRL, SL, MVT::i64, 2311 U, DAG.getConstant(40, SL, MVT::i64)); 2312 2313 SDValue V = DAG.getNode(ISD::OR, SL, MVT::i32, 2314 DAG.getNode(ISD::SHL, SL, MVT::i32, E, DAG.getConstant(23, SL, MVT::i32)), 2315 DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, UShl)); 2316 2317 SDValue C = DAG.getConstant(0x8000000000ULL, SL, MVT::i64); 2318 SDValue RCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETUGT); 2319 SDValue TCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETEQ); 2320 2321 SDValue One = DAG.getConstant(1, SL, MVT::i32); 2322 2323 SDValue VTrunc1 = DAG.getNode(ISD::AND, SL, MVT::i32, V, One); 2324 2325 SDValue R = DAG.getSelect(SL, MVT::i32, 2326 RCmp, 2327 One, 2328 DAG.getSelect(SL, MVT::i32, TCmp, VTrunc1, ZeroI32)); 2329 R = DAG.getNode(ISD::ADD, SL, MVT::i32, V, R); 2330 R = DAG.getNode(ISD::BITCAST, SL, MVT::f32, R); 2331 2332 if (!Signed) 2333 return R; 2334 2335 SDValue RNeg = DAG.getNode(ISD::FNEG, SL, MVT::f32, R); 2336 return DAG.getSelect(SL, MVT::f32, DAG.getSExtOrTrunc(S, SL, SetCCVT), RNeg, R); 2337 } 2338 2339 SDValue AMDGPUTargetLowering::LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG, 2340 bool Signed) const { 2341 SDLoc SL(Op); 2342 SDValue Src = Op.getOperand(0); 2343 2344 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src); 2345 2346 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, 2347 DAG.getConstant(0, SL, MVT::i32)); 2348 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, 2349 DAG.getConstant(1, SL, MVT::i32)); 2350 2351 SDValue CvtHi = DAG.getNode(Signed ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, 2352 SL, MVT::f64, Hi); 2353 2354 SDValue CvtLo = DAG.getNode(ISD::UINT_TO_FP, SL, MVT::f64, Lo); 2355 2356 SDValue LdExp = DAG.getNode(AMDGPUISD::LDEXP, SL, MVT::f64, CvtHi, 2357 DAG.getConstant(32, SL, MVT::i32)); 2358 // TODO: Should this propagate fast-math-flags? 2359 return DAG.getNode(ISD::FADD, SL, MVT::f64, LdExp, CvtLo); 2360 } 2361 2362 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op, 2363 SelectionDAG &DAG) const { 2364 assert(Op.getOperand(0).getValueType() == MVT::i64 && 2365 "operation should be legal"); 2366 2367 // TODO: Factor out code common with LowerSINT_TO_FP. 2368 2369 EVT DestVT = Op.getValueType(); 2370 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) { 2371 SDLoc DL(Op); 2372 SDValue Src = Op.getOperand(0); 2373 2374 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src); 2375 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op)); 2376 SDValue FPRound = 2377 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag); 2378 2379 return FPRound; 2380 } 2381 2382 if (DestVT == MVT::f32) 2383 return LowerINT_TO_FP32(Op, DAG, false); 2384 2385 assert(DestVT == MVT::f64); 2386 return LowerINT_TO_FP64(Op, DAG, false); 2387 } 2388 2389 SDValue AMDGPUTargetLowering::LowerSINT_TO_FP(SDValue Op, 2390 SelectionDAG &DAG) const { 2391 assert(Op.getOperand(0).getValueType() == MVT::i64 && 2392 "operation should be legal"); 2393 2394 // TODO: Factor out code common with LowerUINT_TO_FP. 2395 2396 EVT DestVT = Op.getValueType(); 2397 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) { 2398 SDLoc DL(Op); 2399 SDValue Src = Op.getOperand(0); 2400 2401 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src); 2402 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op)); 2403 SDValue FPRound = 2404 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag); 2405 2406 return FPRound; 2407 } 2408 2409 if (DestVT == MVT::f32) 2410 return LowerINT_TO_FP32(Op, DAG, true); 2411 2412 assert(DestVT == MVT::f64); 2413 return LowerINT_TO_FP64(Op, DAG, true); 2414 } 2415 2416 SDValue AMDGPUTargetLowering::LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG, 2417 bool Signed) const { 2418 SDLoc SL(Op); 2419 2420 SDValue Src = Op.getOperand(0); 2421 2422 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src); 2423 2424 SDValue K0 = DAG.getConstantFP(BitsToDouble(UINT64_C(0x3df0000000000000)), SL, 2425 MVT::f64); 2426 SDValue K1 = DAG.getConstantFP(BitsToDouble(UINT64_C(0xc1f0000000000000)), SL, 2427 MVT::f64); 2428 // TODO: Should this propagate fast-math-flags? 2429 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, Trunc, K0); 2430 2431 SDValue FloorMul = DAG.getNode(ISD::FFLOOR, SL, MVT::f64, Mul); 2432 2433 2434 SDValue Fma = DAG.getNode(ISD::FMA, SL, MVT::f64, FloorMul, K1, Trunc); 2435 2436 SDValue Hi = DAG.getNode(Signed ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, SL, 2437 MVT::i32, FloorMul); 2438 SDValue Lo = DAG.getNode(ISD::FP_TO_UINT, SL, MVT::i32, Fma); 2439 2440 SDValue Result = DAG.getBuildVector(MVT::v2i32, SL, {Lo, Hi}); 2441 2442 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Result); 2443 } 2444 2445 SDValue AMDGPUTargetLowering::LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const { 2446 SDLoc DL(Op); 2447 SDValue N0 = Op.getOperand(0); 2448 2449 // Convert to target node to get known bits 2450 if (N0.getValueType() == MVT::f32) 2451 return DAG.getNode(AMDGPUISD::FP_TO_FP16, DL, Op.getValueType(), N0); 2452 2453 if (getTargetMachine().Options.UnsafeFPMath) { 2454 // There is a generic expand for FP_TO_FP16 with unsafe fast math. 2455 return SDValue(); 2456 } 2457 2458 assert(N0.getSimpleValueType() == MVT::f64); 2459 2460 // f64 -> f16 conversion using round-to-nearest-even rounding mode. 2461 const unsigned ExpMask = 0x7ff; 2462 const unsigned ExpBiasf64 = 1023; 2463 const unsigned ExpBiasf16 = 15; 2464 SDValue Zero = DAG.getConstant(0, DL, MVT::i32); 2465 SDValue One = DAG.getConstant(1, DL, MVT::i32); 2466 SDValue U = DAG.getNode(ISD::BITCAST, DL, MVT::i64, N0); 2467 SDValue UH = DAG.getNode(ISD::SRL, DL, MVT::i64, U, 2468 DAG.getConstant(32, DL, MVT::i64)); 2469 UH = DAG.getZExtOrTrunc(UH, DL, MVT::i32); 2470 U = DAG.getZExtOrTrunc(U, DL, MVT::i32); 2471 SDValue E = DAG.getNode(ISD::SRL, DL, MVT::i32, UH, 2472 DAG.getConstant(20, DL, MVT::i64)); 2473 E = DAG.getNode(ISD::AND, DL, MVT::i32, E, 2474 DAG.getConstant(ExpMask, DL, MVT::i32)); 2475 // Subtract the fp64 exponent bias (1023) to get the real exponent and 2476 // add the f16 bias (15) to get the biased exponent for the f16 format. 2477 E = DAG.getNode(ISD::ADD, DL, MVT::i32, E, 2478 DAG.getConstant(-ExpBiasf64 + ExpBiasf16, DL, MVT::i32)); 2479 2480 SDValue M = DAG.getNode(ISD::SRL, DL, MVT::i32, UH, 2481 DAG.getConstant(8, DL, MVT::i32)); 2482 M = DAG.getNode(ISD::AND, DL, MVT::i32, M, 2483 DAG.getConstant(0xffe, DL, MVT::i32)); 2484 2485 SDValue MaskedSig = DAG.getNode(ISD::AND, DL, MVT::i32, UH, 2486 DAG.getConstant(0x1ff, DL, MVT::i32)); 2487 MaskedSig = DAG.getNode(ISD::OR, DL, MVT::i32, MaskedSig, U); 2488 2489 SDValue Lo40Set = DAG.getSelectCC(DL, MaskedSig, Zero, Zero, One, ISD::SETEQ); 2490 M = DAG.getNode(ISD::OR, DL, MVT::i32, M, Lo40Set); 2491 2492 // (M != 0 ? 0x0200 : 0) | 0x7c00; 2493 SDValue I = DAG.getNode(ISD::OR, DL, MVT::i32, 2494 DAG.getSelectCC(DL, M, Zero, DAG.getConstant(0x0200, DL, MVT::i32), 2495 Zero, ISD::SETNE), DAG.getConstant(0x7c00, DL, MVT::i32)); 2496 2497 // N = M | (E << 12); 2498 SDValue N = DAG.getNode(ISD::OR, DL, MVT::i32, M, 2499 DAG.getNode(ISD::SHL, DL, MVT::i32, E, 2500 DAG.getConstant(12, DL, MVT::i32))); 2501 2502 // B = clamp(1-E, 0, 13); 2503 SDValue OneSubExp = DAG.getNode(ISD::SUB, DL, MVT::i32, 2504 One, E); 2505 SDValue B = DAG.getNode(ISD::SMAX, DL, MVT::i32, OneSubExp, Zero); 2506 B = DAG.getNode(ISD::SMIN, DL, MVT::i32, B, 2507 DAG.getConstant(13, DL, MVT::i32)); 2508 2509 SDValue SigSetHigh = DAG.getNode(ISD::OR, DL, MVT::i32, M, 2510 DAG.getConstant(0x1000, DL, MVT::i32)); 2511 2512 SDValue D = DAG.getNode(ISD::SRL, DL, MVT::i32, SigSetHigh, B); 2513 SDValue D0 = DAG.getNode(ISD::SHL, DL, MVT::i32, D, B); 2514 SDValue D1 = DAG.getSelectCC(DL, D0, SigSetHigh, One, Zero, ISD::SETNE); 2515 D = DAG.getNode(ISD::OR, DL, MVT::i32, D, D1); 2516 2517 SDValue V = DAG.getSelectCC(DL, E, One, D, N, ISD::SETLT); 2518 SDValue VLow3 = DAG.getNode(ISD::AND, DL, MVT::i32, V, 2519 DAG.getConstant(0x7, DL, MVT::i32)); 2520 V = DAG.getNode(ISD::SRL, DL, MVT::i32, V, 2521 DAG.getConstant(2, DL, MVT::i32)); 2522 SDValue V0 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(3, DL, MVT::i32), 2523 One, Zero, ISD::SETEQ); 2524 SDValue V1 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(5, DL, MVT::i32), 2525 One, Zero, ISD::SETGT); 2526 V1 = DAG.getNode(ISD::OR, DL, MVT::i32, V0, V1); 2527 V = DAG.getNode(ISD::ADD, DL, MVT::i32, V, V1); 2528 2529 V = DAG.getSelectCC(DL, E, DAG.getConstant(30, DL, MVT::i32), 2530 DAG.getConstant(0x7c00, DL, MVT::i32), V, ISD::SETGT); 2531 V = DAG.getSelectCC(DL, E, DAG.getConstant(1039, DL, MVT::i32), 2532 I, V, ISD::SETEQ); 2533 2534 // Extract the sign bit. 2535 SDValue Sign = DAG.getNode(ISD::SRL, DL, MVT::i32, UH, 2536 DAG.getConstant(16, DL, MVT::i32)); 2537 Sign = DAG.getNode(ISD::AND, DL, MVT::i32, Sign, 2538 DAG.getConstant(0x8000, DL, MVT::i32)); 2539 2540 V = DAG.getNode(ISD::OR, DL, MVT::i32, Sign, V); 2541 return DAG.getZExtOrTrunc(V, DL, Op.getValueType()); 2542 } 2543 2544 SDValue AMDGPUTargetLowering::LowerFP_TO_SINT(SDValue Op, 2545 SelectionDAG &DAG) const { 2546 SDValue Src = Op.getOperand(0); 2547 2548 // TODO: Factor out code common with LowerFP_TO_UINT. 2549 2550 EVT SrcVT = Src.getValueType(); 2551 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) { 2552 SDLoc DL(Op); 2553 2554 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src); 2555 SDValue FpToInt32 = 2556 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend); 2557 2558 return FpToInt32; 2559 } 2560 2561 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64) 2562 return LowerFP64_TO_INT(Op, DAG, true); 2563 2564 return SDValue(); 2565 } 2566 2567 SDValue AMDGPUTargetLowering::LowerFP_TO_UINT(SDValue Op, 2568 SelectionDAG &DAG) const { 2569 SDValue Src = Op.getOperand(0); 2570 2571 // TODO: Factor out code common with LowerFP_TO_SINT. 2572 2573 EVT SrcVT = Src.getValueType(); 2574 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) { 2575 SDLoc DL(Op); 2576 2577 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src); 2578 SDValue FpToInt32 = 2579 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend); 2580 2581 return FpToInt32; 2582 } 2583 2584 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64) 2585 return LowerFP64_TO_INT(Op, DAG, false); 2586 2587 return SDValue(); 2588 } 2589 2590 SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, 2591 SelectionDAG &DAG) const { 2592 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 2593 MVT VT = Op.getSimpleValueType(); 2594 MVT ScalarVT = VT.getScalarType(); 2595 2596 assert(VT.isVector()); 2597 2598 SDValue Src = Op.getOperand(0); 2599 SDLoc DL(Op); 2600 2601 // TODO: Don't scalarize on Evergreen? 2602 unsigned NElts = VT.getVectorNumElements(); 2603 SmallVector<SDValue, 8> Args; 2604 DAG.ExtractVectorElements(Src, Args, 0, NElts); 2605 2606 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType()); 2607 for (unsigned I = 0; I < NElts; ++I) 2608 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp); 2609 2610 return DAG.getBuildVector(VT, DL, Args); 2611 } 2612 2613 //===----------------------------------------------------------------------===// 2614 // Custom DAG optimizations 2615 //===----------------------------------------------------------------------===// 2616 2617 static bool isU24(SDValue Op, SelectionDAG &DAG) { 2618 KnownBits Known; 2619 EVT VT = Op.getValueType(); 2620 DAG.computeKnownBits(Op, Known); 2621 2622 return (VT.getSizeInBits() - Known.countMinLeadingZeros()) <= 24; 2623 } 2624 2625 static bool isI24(SDValue Op, SelectionDAG &DAG) { 2626 EVT VT = Op.getValueType(); 2627 2628 // In order for this to be a signed 24-bit value, bit 23, must 2629 // be a sign bit. 2630 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated 2631 // as unsigned 24-bit values. 2632 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24; 2633 } 2634 2635 static bool simplifyI24(SDNode *Node24, unsigned OpIdx, 2636 TargetLowering::DAGCombinerInfo &DCI) { 2637 2638 SelectionDAG &DAG = DCI.DAG; 2639 SDValue Op = Node24->getOperand(OpIdx); 2640 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 2641 EVT VT = Op.getValueType(); 2642 2643 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24); 2644 APInt KnownZero, KnownOne; 2645 TargetLowering::TargetLoweringOpt TLO(DAG, true, true); 2646 if (TLI.SimplifyDemandedBits(Node24, OpIdx, Demanded, DCI, TLO)) 2647 return true; 2648 2649 return false; 2650 } 2651 2652 template <typename IntTy> 2653 static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0, uint32_t Offset, 2654 uint32_t Width, const SDLoc &DL) { 2655 if (Width + Offset < 32) { 2656 uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width); 2657 IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width); 2658 return DAG.getConstant(Result, DL, MVT::i32); 2659 } 2660 2661 return DAG.getConstant(Src0 >> Offset, DL, MVT::i32); 2662 } 2663 2664 static bool hasVolatileUser(SDNode *Val) { 2665 for (SDNode *U : Val->uses()) { 2666 if (MemSDNode *M = dyn_cast<MemSDNode>(U)) { 2667 if (M->isVolatile()) 2668 return true; 2669 } 2670 } 2671 2672 return false; 2673 } 2674 2675 bool AMDGPUTargetLowering::shouldCombineMemoryType(EVT VT) const { 2676 // i32 vectors are the canonical memory type. 2677 if (VT.getScalarType() == MVT::i32 || isTypeLegal(VT)) 2678 return false; 2679 2680 if (!VT.isByteSized()) 2681 return false; 2682 2683 unsigned Size = VT.getStoreSize(); 2684 2685 if ((Size == 1 || Size == 2 || Size == 4) && !VT.isVector()) 2686 return false; 2687 2688 if (Size == 3 || (Size > 4 && (Size % 4 != 0))) 2689 return false; 2690 2691 return true; 2692 } 2693 2694 // Replace load of an illegal type with a store of a bitcast to a friendlier 2695 // type. 2696 SDValue AMDGPUTargetLowering::performLoadCombine(SDNode *N, 2697 DAGCombinerInfo &DCI) const { 2698 if (!DCI.isBeforeLegalize()) 2699 return SDValue(); 2700 2701 LoadSDNode *LN = cast<LoadSDNode>(N); 2702 if (LN->isVolatile() || !ISD::isNormalLoad(LN) || hasVolatileUser(LN)) 2703 return SDValue(); 2704 2705 SDLoc SL(N); 2706 SelectionDAG &DAG = DCI.DAG; 2707 EVT VT = LN->getMemoryVT(); 2708 2709 unsigned Size = VT.getStoreSize(); 2710 unsigned Align = LN->getAlignment(); 2711 if (Align < Size && isTypeLegal(VT)) { 2712 bool IsFast; 2713 unsigned AS = LN->getAddressSpace(); 2714 2715 // Expand unaligned loads earlier than legalization. Due to visitation order 2716 // problems during legalization, the emitted instructions to pack and unpack 2717 // the bytes again are not eliminated in the case of an unaligned copy. 2718 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) { 2719 if (VT.isVector()) 2720 return scalarizeVectorLoad(LN, DAG); 2721 2722 SDValue Ops[2]; 2723 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(LN, DAG); 2724 return DAG.getMergeValues(Ops, SDLoc(N)); 2725 } 2726 2727 if (!IsFast) 2728 return SDValue(); 2729 } 2730 2731 if (!shouldCombineMemoryType(VT)) 2732 return SDValue(); 2733 2734 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT); 2735 2736 SDValue NewLoad 2737 = DAG.getLoad(NewVT, SL, LN->getChain(), 2738 LN->getBasePtr(), LN->getMemOperand()); 2739 2740 SDValue BC = DAG.getNode(ISD::BITCAST, SL, VT, NewLoad); 2741 DCI.CombineTo(N, BC, NewLoad.getValue(1)); 2742 return SDValue(N, 0); 2743 } 2744 2745 // Replace store of an illegal type with a store of a bitcast to a friendlier 2746 // type. 2747 SDValue AMDGPUTargetLowering::performStoreCombine(SDNode *N, 2748 DAGCombinerInfo &DCI) const { 2749 if (!DCI.isBeforeLegalize()) 2750 return SDValue(); 2751 2752 StoreSDNode *SN = cast<StoreSDNode>(N); 2753 if (SN->isVolatile() || !ISD::isNormalStore(SN)) 2754 return SDValue(); 2755 2756 EVT VT = SN->getMemoryVT(); 2757 unsigned Size = VT.getStoreSize(); 2758 2759 SDLoc SL(N); 2760 SelectionDAG &DAG = DCI.DAG; 2761 unsigned Align = SN->getAlignment(); 2762 if (Align < Size && isTypeLegal(VT)) { 2763 bool IsFast; 2764 unsigned AS = SN->getAddressSpace(); 2765 2766 // Expand unaligned stores earlier than legalization. Due to visitation 2767 // order problems during legalization, the emitted instructions to pack and 2768 // unpack the bytes again are not eliminated in the case of an unaligned 2769 // copy. 2770 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) { 2771 if (VT.isVector()) 2772 return scalarizeVectorStore(SN, DAG); 2773 2774 return expandUnalignedStore(SN, DAG); 2775 } 2776 2777 if (!IsFast) 2778 return SDValue(); 2779 } 2780 2781 if (!shouldCombineMemoryType(VT)) 2782 return SDValue(); 2783 2784 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT); 2785 SDValue Val = SN->getValue(); 2786 2787 //DCI.AddToWorklist(Val.getNode()); 2788 2789 bool OtherUses = !Val.hasOneUse(); 2790 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NewVT, Val); 2791 if (OtherUses) { 2792 SDValue CastBack = DAG.getNode(ISD::BITCAST, SL, VT, CastVal); 2793 DAG.ReplaceAllUsesOfValueWith(Val, CastBack); 2794 } 2795 2796 return DAG.getStore(SN->getChain(), SL, CastVal, 2797 SN->getBasePtr(), SN->getMemOperand()); 2798 } 2799 2800 SDValue AMDGPUTargetLowering::performClampCombine(SDNode *N, 2801 DAGCombinerInfo &DCI) const { 2802 ConstantFPSDNode *CSrc = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); 2803 if (!CSrc) 2804 return SDValue(); 2805 2806 const APFloat &F = CSrc->getValueAPF(); 2807 APFloat Zero = APFloat::getZero(F.getSemantics()); 2808 APFloat::cmpResult Cmp0 = F.compare(Zero); 2809 if (Cmp0 == APFloat::cmpLessThan || 2810 (Cmp0 == APFloat::cmpUnordered && Subtarget->enableDX10Clamp())) { 2811 return DCI.DAG.getConstantFP(Zero, SDLoc(N), N->getValueType(0)); 2812 } 2813 2814 APFloat One(F.getSemantics(), "1.0"); 2815 APFloat::cmpResult Cmp1 = F.compare(One); 2816 if (Cmp1 == APFloat::cmpGreaterThan) 2817 return DCI.DAG.getConstantFP(One, SDLoc(N), N->getValueType(0)); 2818 2819 return SDValue(CSrc, 0); 2820 } 2821 2822 // FIXME: This should go in generic DAG combiner with an isTruncateFree check, 2823 // but isTruncateFree is inaccurate for i16 now because of SALU vs. VALU 2824 // issues. 2825 SDValue AMDGPUTargetLowering::performAssertSZExtCombine(SDNode *N, 2826 DAGCombinerInfo &DCI) const { 2827 SelectionDAG &DAG = DCI.DAG; 2828 SDValue N0 = N->getOperand(0); 2829 2830 // (vt2 (assertzext (truncate vt0:x), vt1)) -> 2831 // (vt2 (truncate (assertzext vt0:x, vt1))) 2832 if (N0.getOpcode() == ISD::TRUNCATE) { 2833 SDValue N1 = N->getOperand(1); 2834 EVT ExtVT = cast<VTSDNode>(N1)->getVT(); 2835 SDLoc SL(N); 2836 2837 SDValue Src = N0.getOperand(0); 2838 EVT SrcVT = Src.getValueType(); 2839 if (SrcVT.bitsGE(ExtVT)) { 2840 SDValue NewInReg = DAG.getNode(N->getOpcode(), SL, SrcVT, Src, N1); 2841 return DAG.getNode(ISD::TRUNCATE, SL, N->getValueType(0), NewInReg); 2842 } 2843 } 2844 2845 return SDValue(); 2846 } 2847 /// Split the 64-bit value \p LHS into two 32-bit components, and perform the 2848 /// binary operation \p Opc to it with the corresponding constant operands. 2849 SDValue AMDGPUTargetLowering::splitBinaryBitConstantOpImpl( 2850 DAGCombinerInfo &DCI, const SDLoc &SL, 2851 unsigned Opc, SDValue LHS, 2852 uint32_t ValLo, uint32_t ValHi) const { 2853 SelectionDAG &DAG = DCI.DAG; 2854 SDValue Lo, Hi; 2855 std::tie(Lo, Hi) = split64BitValue(LHS, DAG); 2856 2857 SDValue LoRHS = DAG.getConstant(ValLo, SL, MVT::i32); 2858 SDValue HiRHS = DAG.getConstant(ValHi, SL, MVT::i32); 2859 2860 SDValue LoAnd = DAG.getNode(Opc, SL, MVT::i32, Lo, LoRHS); 2861 SDValue HiAnd = DAG.getNode(Opc, SL, MVT::i32, Hi, HiRHS); 2862 2863 // Re-visit the ands. It's possible we eliminated one of them and it could 2864 // simplify the vector. 2865 DCI.AddToWorklist(Lo.getNode()); 2866 DCI.AddToWorklist(Hi.getNode()); 2867 2868 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {LoAnd, HiAnd}); 2869 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 2870 } 2871 2872 SDValue AMDGPUTargetLowering::performShlCombine(SDNode *N, 2873 DAGCombinerInfo &DCI) const { 2874 EVT VT = N->getValueType(0); 2875 2876 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2877 if (!RHS) 2878 return SDValue(); 2879 2880 SDValue LHS = N->getOperand(0); 2881 unsigned RHSVal = RHS->getZExtValue(); 2882 if (!RHSVal) 2883 return LHS; 2884 2885 SDLoc SL(N); 2886 SelectionDAG &DAG = DCI.DAG; 2887 2888 switch (LHS->getOpcode()) { 2889 default: 2890 break; 2891 case ISD::ZERO_EXTEND: 2892 case ISD::SIGN_EXTEND: 2893 case ISD::ANY_EXTEND: { 2894 SDValue X = LHS->getOperand(0); 2895 2896 if (VT == MVT::i32 && RHSVal == 16 && X.getValueType() == MVT::i16 && 2897 isTypeLegal(MVT::v2i16)) { 2898 // Prefer build_vector as the canonical form if packed types are legal. 2899 // (shl ([asz]ext i16:x), 16 -> build_vector 0, x 2900 SDValue Vec = DAG.getBuildVector(MVT::v2i16, SL, 2901 { DAG.getConstant(0, SL, MVT::i16), LHS->getOperand(0) }); 2902 return DAG.getNode(ISD::BITCAST, SL, MVT::i32, Vec); 2903 } 2904 2905 // shl (ext x) => zext (shl x), if shift does not overflow int 2906 if (VT != MVT::i64) 2907 break; 2908 KnownBits Known; 2909 DAG.computeKnownBits(X, Known); 2910 unsigned LZ = Known.countMinLeadingZeros(); 2911 if (LZ < RHSVal) 2912 break; 2913 EVT XVT = X.getValueType(); 2914 SDValue Shl = DAG.getNode(ISD::SHL, SL, XVT, X, SDValue(RHS, 0)); 2915 return DAG.getZExtOrTrunc(Shl, SL, VT); 2916 } 2917 case ISD::OR: 2918 if (!isOrEquivalentToAdd(DAG, LHS)) 2919 break; 2920 LLVM_FALLTHROUGH; 2921 case ISD::ADD: { 2922 // shl (or|add x, c2), c1 => or|add (shl x, c1), (c2 << c1) 2923 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(LHS->getOperand(1))) { 2924 SDValue Shl = DAG.getNode(ISD::SHL, SL, VT, LHS->getOperand(0), 2925 SDValue(RHS, 0)); 2926 SDValue C2V = DAG.getConstant(C2->getAPIntValue() << RHSVal, 2927 SDLoc(C2), VT); 2928 return DAG.getNode(LHS->getOpcode(), SL, VT, Shl, C2V); 2929 } 2930 break; 2931 } 2932 } 2933 2934 if (VT != MVT::i64) 2935 return SDValue(); 2936 2937 // i64 (shl x, C) -> (build_pair 0, (shl x, C -32)) 2938 2939 // On some subtargets, 64-bit shift is a quarter rate instruction. In the 2940 // common case, splitting this into a move and a 32-bit shift is faster and 2941 // the same code size. 2942 if (RHSVal < 32) 2943 return SDValue(); 2944 2945 SDValue ShiftAmt = DAG.getConstant(RHSVal - 32, SL, MVT::i32); 2946 2947 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS); 2948 SDValue NewShift = DAG.getNode(ISD::SHL, SL, MVT::i32, Lo, ShiftAmt); 2949 2950 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 2951 2952 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {Zero, NewShift}); 2953 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 2954 } 2955 2956 SDValue AMDGPUTargetLowering::performSraCombine(SDNode *N, 2957 DAGCombinerInfo &DCI) const { 2958 if (N->getValueType(0) != MVT::i64) 2959 return SDValue(); 2960 2961 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2962 if (!RHS) 2963 return SDValue(); 2964 2965 SelectionDAG &DAG = DCI.DAG; 2966 SDLoc SL(N); 2967 unsigned RHSVal = RHS->getZExtValue(); 2968 2969 // (sra i64:x, 32) -> build_pair x, (sra hi_32(x), 31) 2970 if (RHSVal == 32) { 2971 SDValue Hi = getHiHalf64(N->getOperand(0), DAG); 2972 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi, 2973 DAG.getConstant(31, SL, MVT::i32)); 2974 2975 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {Hi, NewShift}); 2976 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec); 2977 } 2978 2979 // (sra i64:x, 63) -> build_pair (sra hi_32(x), 31), (sra hi_32(x), 31) 2980 if (RHSVal == 63) { 2981 SDValue Hi = getHiHalf64(N->getOperand(0), DAG); 2982 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi, 2983 DAG.getConstant(31, SL, MVT::i32)); 2984 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, NewShift}); 2985 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec); 2986 } 2987 2988 return SDValue(); 2989 } 2990 2991 SDValue AMDGPUTargetLowering::performSrlCombine(SDNode *N, 2992 DAGCombinerInfo &DCI) const { 2993 if (N->getValueType(0) != MVT::i64) 2994 return SDValue(); 2995 2996 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2997 if (!RHS) 2998 return SDValue(); 2999 3000 unsigned ShiftAmt = RHS->getZExtValue(); 3001 if (ShiftAmt < 32) 3002 return SDValue(); 3003 3004 // srl i64:x, C for C >= 32 3005 // => 3006 // build_pair (srl hi_32(x), C - 32), 0 3007 3008 SelectionDAG &DAG = DCI.DAG; 3009 SDLoc SL(N); 3010 3011 SDValue One = DAG.getConstant(1, SL, MVT::i32); 3012 SDValue Zero = DAG.getConstant(0, SL, MVT::i32); 3013 3014 SDValue VecOp = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, N->getOperand(0)); 3015 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, 3016 VecOp, One); 3017 3018 SDValue NewConst = DAG.getConstant(ShiftAmt - 32, SL, MVT::i32); 3019 SDValue NewShift = DAG.getNode(ISD::SRL, SL, MVT::i32, Hi, NewConst); 3020 3021 SDValue BuildPair = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, Zero}); 3022 3023 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildPair); 3024 } 3025 3026 // We need to specifically handle i64 mul here to avoid unnecessary conversion 3027 // instructions. If we only match on the legalized i64 mul expansion, 3028 // SimplifyDemandedBits will be unable to remove them because there will be 3029 // multiple uses due to the separate mul + mulh[su]. 3030 static SDValue getMul24(SelectionDAG &DAG, const SDLoc &SL, 3031 SDValue N0, SDValue N1, unsigned Size, bool Signed) { 3032 if (Size <= 32) { 3033 unsigned MulOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24; 3034 return DAG.getNode(MulOpc, SL, MVT::i32, N0, N1); 3035 } 3036 3037 // Because we want to eliminate extension instructions before the 3038 // operation, we need to create a single user here (i.e. not the separate 3039 // mul_lo + mul_hi) so that SimplifyDemandedBits will deal with it. 3040 3041 unsigned MulOpc = Signed ? AMDGPUISD::MUL_LOHI_I24 : AMDGPUISD::MUL_LOHI_U24; 3042 3043 SDValue Mul = DAG.getNode(MulOpc, SL, 3044 DAG.getVTList(MVT::i32, MVT::i32), N0, N1); 3045 3046 return DAG.getNode(ISD::BUILD_PAIR, SL, MVT::i64, 3047 Mul.getValue(0), Mul.getValue(1)); 3048 } 3049 3050 SDValue AMDGPUTargetLowering::performMulCombine(SDNode *N, 3051 DAGCombinerInfo &DCI) const { 3052 EVT VT = N->getValueType(0); 3053 3054 unsigned Size = VT.getSizeInBits(); 3055 if (VT.isVector() || Size > 64) 3056 return SDValue(); 3057 3058 // There are i16 integer mul/mad. 3059 if (Subtarget->has16BitInsts() && VT.getScalarType().bitsLE(MVT::i16)) 3060 return SDValue(); 3061 3062 SelectionDAG &DAG = DCI.DAG; 3063 SDLoc DL(N); 3064 3065 SDValue N0 = N->getOperand(0); 3066 SDValue N1 = N->getOperand(1); 3067 SDValue Mul; 3068 3069 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) { 3070 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32); 3071 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32); 3072 Mul = getMul24(DAG, DL, N0, N1, Size, false); 3073 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) { 3074 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32); 3075 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32); 3076 Mul = getMul24(DAG, DL, N0, N1, Size, true); 3077 } else { 3078 return SDValue(); 3079 } 3080 3081 // We need to use sext even for MUL_U24, because MUL_U24 is used 3082 // for signed multiply of 8 and 16-bit types. 3083 return DAG.getSExtOrTrunc(Mul, DL, VT); 3084 } 3085 3086 SDValue AMDGPUTargetLowering::performMulhsCombine(SDNode *N, 3087 DAGCombinerInfo &DCI) const { 3088 EVT VT = N->getValueType(0); 3089 3090 if (!Subtarget->hasMulI24() || VT.isVector()) 3091 return SDValue(); 3092 3093 SelectionDAG &DAG = DCI.DAG; 3094 SDLoc DL(N); 3095 3096 SDValue N0 = N->getOperand(0); 3097 SDValue N1 = N->getOperand(1); 3098 3099 if (!isI24(N0, DAG) || !isI24(N1, DAG)) 3100 return SDValue(); 3101 3102 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32); 3103 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32); 3104 3105 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_I24, DL, MVT::i32, N0, N1); 3106 DCI.AddToWorklist(Mulhi.getNode()); 3107 return DAG.getSExtOrTrunc(Mulhi, DL, VT); 3108 } 3109 3110 SDValue AMDGPUTargetLowering::performMulhuCombine(SDNode *N, 3111 DAGCombinerInfo &DCI) const { 3112 EVT VT = N->getValueType(0); 3113 3114 if (!Subtarget->hasMulU24() || VT.isVector() || VT.getSizeInBits() > 32) 3115 return SDValue(); 3116 3117 SelectionDAG &DAG = DCI.DAG; 3118 SDLoc DL(N); 3119 3120 SDValue N0 = N->getOperand(0); 3121 SDValue N1 = N->getOperand(1); 3122 3123 if (!isU24(N0, DAG) || !isU24(N1, DAG)) 3124 return SDValue(); 3125 3126 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32); 3127 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32); 3128 3129 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_U24, DL, MVT::i32, N0, N1); 3130 DCI.AddToWorklist(Mulhi.getNode()); 3131 return DAG.getZExtOrTrunc(Mulhi, DL, VT); 3132 } 3133 3134 SDValue AMDGPUTargetLowering::performMulLoHi24Combine( 3135 SDNode *N, DAGCombinerInfo &DCI) const { 3136 SelectionDAG &DAG = DCI.DAG; 3137 3138 // Simplify demanded bits before splitting into multiple users. 3139 if (simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI)) 3140 return SDValue(); 3141 3142 SDValue N0 = N->getOperand(0); 3143 SDValue N1 = N->getOperand(1); 3144 3145 bool Signed = (N->getOpcode() == AMDGPUISD::MUL_LOHI_I24); 3146 3147 unsigned MulLoOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24; 3148 unsigned MulHiOpc = Signed ? AMDGPUISD::MULHI_I24 : AMDGPUISD::MULHI_U24; 3149 3150 SDLoc SL(N); 3151 3152 SDValue MulLo = DAG.getNode(MulLoOpc, SL, MVT::i32, N0, N1); 3153 SDValue MulHi = DAG.getNode(MulHiOpc, SL, MVT::i32, N0, N1); 3154 return DAG.getMergeValues({ MulLo, MulHi }, SL); 3155 } 3156 3157 static bool isNegativeOne(SDValue Val) { 3158 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) 3159 return C->isAllOnesValue(); 3160 return false; 3161 } 3162 3163 SDValue AMDGPUTargetLowering::getFFBX_U32(SelectionDAG &DAG, 3164 SDValue Op, 3165 const SDLoc &DL, 3166 unsigned Opc) const { 3167 EVT VT = Op.getValueType(); 3168 EVT LegalVT = getTypeToTransformTo(*DAG.getContext(), VT); 3169 if (LegalVT != MVT::i32 && (Subtarget->has16BitInsts() && 3170 LegalVT != MVT::i16)) 3171 return SDValue(); 3172 3173 if (VT != MVT::i32) 3174 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Op); 3175 3176 SDValue FFBX = DAG.getNode(Opc, DL, MVT::i32, Op); 3177 if (VT != MVT::i32) 3178 FFBX = DAG.getNode(ISD::TRUNCATE, DL, VT, FFBX); 3179 3180 return FFBX; 3181 } 3182 3183 // The native instructions return -1 on 0 input. Optimize out a select that 3184 // produces -1 on 0. 3185 // 3186 // TODO: If zero is not undef, we could also do this if the output is compared 3187 // against the bitwidth. 3188 // 3189 // TODO: Should probably combine against FFBH_U32 instead of ctlz directly. 3190 SDValue AMDGPUTargetLowering::performCtlz_CttzCombine(const SDLoc &SL, SDValue Cond, 3191 SDValue LHS, SDValue RHS, 3192 DAGCombinerInfo &DCI) const { 3193 ConstantSDNode *CmpRhs = dyn_cast<ConstantSDNode>(Cond.getOperand(1)); 3194 if (!CmpRhs || !CmpRhs->isNullValue()) 3195 return SDValue(); 3196 3197 SelectionDAG &DAG = DCI.DAG; 3198 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); 3199 SDValue CmpLHS = Cond.getOperand(0); 3200 3201 unsigned Opc = isCttzOpc(RHS.getOpcode()) ? AMDGPUISD::FFBL_B32 : 3202 AMDGPUISD::FFBH_U32; 3203 3204 // select (setcc x, 0, eq), -1, (ctlz_zero_undef x) -> ffbh_u32 x 3205 // select (setcc x, 0, eq), -1, (cttz_zero_undef x) -> ffbl_u32 x 3206 if (CCOpcode == ISD::SETEQ && 3207 (isCtlzOpc(RHS.getOpcode()) || isCttzOpc(RHS.getOpcode())) && 3208 RHS.getOperand(0) == CmpLHS && 3209 isNegativeOne(LHS)) { 3210 return getFFBX_U32(DAG, CmpLHS, SL, Opc); 3211 } 3212 3213 // select (setcc x, 0, ne), (ctlz_zero_undef x), -1 -> ffbh_u32 x 3214 // select (setcc x, 0, ne), (cttz_zero_undef x), -1 -> ffbl_u32 x 3215 if (CCOpcode == ISD::SETNE && 3216 (isCtlzOpc(LHS.getOpcode()) || isCttzOpc(RHS.getOpcode())) && 3217 LHS.getOperand(0) == CmpLHS && 3218 isNegativeOne(RHS)) { 3219 return getFFBX_U32(DAG, CmpLHS, SL, Opc); 3220 } 3221 3222 return SDValue(); 3223 } 3224 3225 static SDValue distributeOpThroughSelect(TargetLowering::DAGCombinerInfo &DCI, 3226 unsigned Op, 3227 const SDLoc &SL, 3228 SDValue Cond, 3229 SDValue N1, 3230 SDValue N2) { 3231 SelectionDAG &DAG = DCI.DAG; 3232 EVT VT = N1.getValueType(); 3233 3234 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT, Cond, 3235 N1.getOperand(0), N2.getOperand(0)); 3236 DCI.AddToWorklist(NewSelect.getNode()); 3237 return DAG.getNode(Op, SL, VT, NewSelect); 3238 } 3239 3240 // Pull a free FP operation out of a select so it may fold into uses. 3241 // 3242 // select c, (fneg x), (fneg y) -> fneg (select c, x, y) 3243 // select c, (fneg x), k -> fneg (select c, x, (fneg k)) 3244 // 3245 // select c, (fabs x), (fabs y) -> fabs (select c, x, y) 3246 // select c, (fabs x), +k -> fabs (select c, x, k) 3247 static SDValue foldFreeOpFromSelect(TargetLowering::DAGCombinerInfo &DCI, 3248 SDValue N) { 3249 SelectionDAG &DAG = DCI.DAG; 3250 SDValue Cond = N.getOperand(0); 3251 SDValue LHS = N.getOperand(1); 3252 SDValue RHS = N.getOperand(2); 3253 3254 EVT VT = N.getValueType(); 3255 if ((LHS.getOpcode() == ISD::FABS && RHS.getOpcode() == ISD::FABS) || 3256 (LHS.getOpcode() == ISD::FNEG && RHS.getOpcode() == ISD::FNEG)) { 3257 return distributeOpThroughSelect(DCI, LHS.getOpcode(), 3258 SDLoc(N), Cond, LHS, RHS); 3259 } 3260 3261 bool Inv = false; 3262 if (RHS.getOpcode() == ISD::FABS || RHS.getOpcode() == ISD::FNEG) { 3263 std::swap(LHS, RHS); 3264 Inv = true; 3265 } 3266 3267 // TODO: Support vector constants. 3268 ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS); 3269 if ((LHS.getOpcode() == ISD::FNEG || LHS.getOpcode() == ISD::FABS) && CRHS) { 3270 SDLoc SL(N); 3271 // If one side is an fneg/fabs and the other is a constant, we can push the 3272 // fneg/fabs down. If it's an fabs, the constant needs to be non-negative. 3273 SDValue NewLHS = LHS.getOperand(0); 3274 SDValue NewRHS = RHS; 3275 3276 // Careful: if the neg can be folded up, don't try to pull it back down. 3277 bool ShouldFoldNeg = true; 3278 3279 if (NewLHS.hasOneUse()) { 3280 unsigned Opc = NewLHS.getOpcode(); 3281 if (LHS.getOpcode() == ISD::FNEG && fnegFoldsIntoOp(Opc)) 3282 ShouldFoldNeg = false; 3283 if (LHS.getOpcode() == ISD::FABS && Opc == ISD::FMUL) 3284 ShouldFoldNeg = false; 3285 } 3286 3287 if (ShouldFoldNeg) { 3288 if (LHS.getOpcode() == ISD::FNEG) 3289 NewRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3290 else if (CRHS->isNegative()) 3291 return SDValue(); 3292 3293 if (Inv) 3294 std::swap(NewLHS, NewRHS); 3295 3296 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT, 3297 Cond, NewLHS, NewRHS); 3298 DCI.AddToWorklist(NewSelect.getNode()); 3299 return DAG.getNode(LHS.getOpcode(), SL, VT, NewSelect); 3300 } 3301 } 3302 3303 return SDValue(); 3304 } 3305 3306 3307 SDValue AMDGPUTargetLowering::performSelectCombine(SDNode *N, 3308 DAGCombinerInfo &DCI) const { 3309 if (SDValue Folded = foldFreeOpFromSelect(DCI, SDValue(N, 0))) 3310 return Folded; 3311 3312 SDValue Cond = N->getOperand(0); 3313 if (Cond.getOpcode() != ISD::SETCC) 3314 return SDValue(); 3315 3316 EVT VT = N->getValueType(0); 3317 SDValue LHS = Cond.getOperand(0); 3318 SDValue RHS = Cond.getOperand(1); 3319 SDValue CC = Cond.getOperand(2); 3320 3321 SDValue True = N->getOperand(1); 3322 SDValue False = N->getOperand(2); 3323 3324 if (Cond.hasOneUse()) { // TODO: Look for multiple select uses. 3325 SelectionDAG &DAG = DCI.DAG; 3326 if ((DAG.isConstantValueOfAnyType(True) || 3327 DAG.isConstantValueOfAnyType(True)) && 3328 (!DAG.isConstantValueOfAnyType(False) && 3329 !DAG.isConstantValueOfAnyType(False))) { 3330 // Swap cmp + select pair to move constant to false input. 3331 // This will allow using VOPC cndmasks more often. 3332 // select (setcc x, y), k, x -> select (setcc y, x) x, x 3333 3334 SDLoc SL(N); 3335 ISD::CondCode NewCC = getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), 3336 LHS.getValueType().isInteger()); 3337 3338 SDValue NewCond = DAG.getSetCC(SL, Cond.getValueType(), LHS, RHS, NewCC); 3339 return DAG.getNode(ISD::SELECT, SL, VT, NewCond, False, True); 3340 } 3341 3342 if (VT == MVT::f32 && Subtarget->hasFminFmaxLegacy()) { 3343 SDValue MinMax 3344 = combineFMinMaxLegacy(SDLoc(N), VT, LHS, RHS, True, False, CC, DCI); 3345 // Revisit this node so we can catch min3/max3/med3 patterns. 3346 //DCI.AddToWorklist(MinMax.getNode()); 3347 return MinMax; 3348 } 3349 } 3350 3351 // There's no reason to not do this if the condition has other uses. 3352 return performCtlz_CttzCombine(SDLoc(N), Cond, True, False, DCI); 3353 } 3354 3355 static bool isConstantFPZero(SDValue N) { 3356 if (const ConstantFPSDNode *C = isConstOrConstSplatFP(N)) 3357 return C->isZero() && !C->isNegative(); 3358 return false; 3359 } 3360 3361 static unsigned inverseMinMax(unsigned Opc) { 3362 switch (Opc) { 3363 case ISD::FMAXNUM: 3364 return ISD::FMINNUM; 3365 case ISD::FMINNUM: 3366 return ISD::FMAXNUM; 3367 case AMDGPUISD::FMAX_LEGACY: 3368 return AMDGPUISD::FMIN_LEGACY; 3369 case AMDGPUISD::FMIN_LEGACY: 3370 return AMDGPUISD::FMAX_LEGACY; 3371 default: 3372 llvm_unreachable("invalid min/max opcode"); 3373 } 3374 } 3375 3376 SDValue AMDGPUTargetLowering::performFNegCombine(SDNode *N, 3377 DAGCombinerInfo &DCI) const { 3378 SelectionDAG &DAG = DCI.DAG; 3379 SDValue N0 = N->getOperand(0); 3380 EVT VT = N->getValueType(0); 3381 3382 unsigned Opc = N0.getOpcode(); 3383 3384 // If the input has multiple uses and we can either fold the negate down, or 3385 // the other uses cannot, give up. This both prevents unprofitable 3386 // transformations and infinite loops: we won't repeatedly try to fold around 3387 // a negate that has no 'good' form. 3388 if (N0.hasOneUse()) { 3389 // This may be able to fold into the source, but at a code size cost. Don't 3390 // fold if the fold into the user is free. 3391 if (allUsesHaveSourceMods(N, 0)) 3392 return SDValue(); 3393 } else { 3394 if (fnegFoldsIntoOp(Opc) && 3395 (allUsesHaveSourceMods(N) || !allUsesHaveSourceMods(N0.getNode()))) 3396 return SDValue(); 3397 } 3398 3399 SDLoc SL(N); 3400 switch (Opc) { 3401 case ISD::FADD: { 3402 if (!mayIgnoreSignedZero(N0)) 3403 return SDValue(); 3404 3405 // (fneg (fadd x, y)) -> (fadd (fneg x), (fneg y)) 3406 SDValue LHS = N0.getOperand(0); 3407 SDValue RHS = N0.getOperand(1); 3408 3409 if (LHS.getOpcode() != ISD::FNEG) 3410 LHS = DAG.getNode(ISD::FNEG, SL, VT, LHS); 3411 else 3412 LHS = LHS.getOperand(0); 3413 3414 if (RHS.getOpcode() != ISD::FNEG) 3415 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3416 else 3417 RHS = RHS.getOperand(0); 3418 3419 SDValue Res = DAG.getNode(ISD::FADD, SL, VT, LHS, RHS, N0->getFlags()); 3420 if (!N0.hasOneUse()) 3421 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 3422 return Res; 3423 } 3424 case ISD::FMUL: 3425 case AMDGPUISD::FMUL_LEGACY: { 3426 // (fneg (fmul x, y)) -> (fmul x, (fneg y)) 3427 // (fneg (fmul_legacy x, y)) -> (fmul_legacy x, (fneg y)) 3428 SDValue LHS = N0.getOperand(0); 3429 SDValue RHS = N0.getOperand(1); 3430 3431 if (LHS.getOpcode() == ISD::FNEG) 3432 LHS = LHS.getOperand(0); 3433 else if (RHS.getOpcode() == ISD::FNEG) 3434 RHS = RHS.getOperand(0); 3435 else 3436 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3437 3438 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, RHS, N0->getFlags()); 3439 if (!N0.hasOneUse()) 3440 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 3441 return Res; 3442 } 3443 case ISD::FMA: 3444 case ISD::FMAD: { 3445 if (!mayIgnoreSignedZero(N0)) 3446 return SDValue(); 3447 3448 // (fneg (fma x, y, z)) -> (fma x, (fneg y), (fneg z)) 3449 SDValue LHS = N0.getOperand(0); 3450 SDValue MHS = N0.getOperand(1); 3451 SDValue RHS = N0.getOperand(2); 3452 3453 if (LHS.getOpcode() == ISD::FNEG) 3454 LHS = LHS.getOperand(0); 3455 else if (MHS.getOpcode() == ISD::FNEG) 3456 MHS = MHS.getOperand(0); 3457 else 3458 MHS = DAG.getNode(ISD::FNEG, SL, VT, MHS); 3459 3460 if (RHS.getOpcode() != ISD::FNEG) 3461 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3462 else 3463 RHS = RHS.getOperand(0); 3464 3465 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, MHS, RHS); 3466 if (!N0.hasOneUse()) 3467 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 3468 return Res; 3469 } 3470 case ISD::FMAXNUM: 3471 case ISD::FMINNUM: 3472 case AMDGPUISD::FMAX_LEGACY: 3473 case AMDGPUISD::FMIN_LEGACY: { 3474 // fneg (fmaxnum x, y) -> fminnum (fneg x), (fneg y) 3475 // fneg (fminnum x, y) -> fmaxnum (fneg x), (fneg y) 3476 // fneg (fmax_legacy x, y) -> fmin_legacy (fneg x), (fneg y) 3477 // fneg (fmin_legacy x, y) -> fmax_legacy (fneg x), (fneg y) 3478 3479 SDValue LHS = N0.getOperand(0); 3480 SDValue RHS = N0.getOperand(1); 3481 3482 // 0 doesn't have a negated inline immediate. 3483 // TODO: Shouldn't fold 1/2pi either, and should be generalized to other 3484 // operations. 3485 if (isConstantFPZero(RHS)) 3486 return SDValue(); 3487 3488 SDValue NegLHS = DAG.getNode(ISD::FNEG, SL, VT, LHS); 3489 SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3490 unsigned Opposite = inverseMinMax(Opc); 3491 3492 SDValue Res = DAG.getNode(Opposite, SL, VT, NegLHS, NegRHS, N0->getFlags()); 3493 if (!N0.hasOneUse()) 3494 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res)); 3495 return Res; 3496 } 3497 case ISD::FP_EXTEND: 3498 case ISD::FTRUNC: 3499 case ISD::FRINT: 3500 case ISD::FNEARBYINT: // XXX - Should fround be handled? 3501 case ISD::FSIN: 3502 case AMDGPUISD::RCP: 3503 case AMDGPUISD::RCP_LEGACY: 3504 case AMDGPUISD::SIN_HW: { 3505 SDValue CvtSrc = N0.getOperand(0); 3506 if (CvtSrc.getOpcode() == ISD::FNEG) { 3507 // (fneg (fp_extend (fneg x))) -> (fp_extend x) 3508 // (fneg (rcp (fneg x))) -> (rcp x) 3509 return DAG.getNode(Opc, SL, VT, CvtSrc.getOperand(0)); 3510 } 3511 3512 if (!N0.hasOneUse()) 3513 return SDValue(); 3514 3515 // (fneg (fp_extend x)) -> (fp_extend (fneg x)) 3516 // (fneg (rcp x)) -> (rcp (fneg x)) 3517 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc); 3518 return DAG.getNode(Opc, SL, VT, Neg, N0->getFlags()); 3519 } 3520 case ISD::FP_ROUND: { 3521 SDValue CvtSrc = N0.getOperand(0); 3522 3523 if (CvtSrc.getOpcode() == ISD::FNEG) { 3524 // (fneg (fp_round (fneg x))) -> (fp_round x) 3525 return DAG.getNode(ISD::FP_ROUND, SL, VT, 3526 CvtSrc.getOperand(0), N0.getOperand(1)); 3527 } 3528 3529 if (!N0.hasOneUse()) 3530 return SDValue(); 3531 3532 // (fneg (fp_round x)) -> (fp_round (fneg x)) 3533 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc); 3534 return DAG.getNode(ISD::FP_ROUND, SL, VT, Neg, N0.getOperand(1)); 3535 } 3536 case ISD::FP16_TO_FP: { 3537 // v_cvt_f32_f16 supports source modifiers on pre-VI targets without legal 3538 // f16, but legalization of f16 fneg ends up pulling it out of the source. 3539 // Put the fneg back as a legal source operation that can be matched later. 3540 SDLoc SL(N); 3541 3542 SDValue Src = N0.getOperand(0); 3543 EVT SrcVT = Src.getValueType(); 3544 3545 // fneg (fp16_to_fp x) -> fp16_to_fp (xor x, 0x8000) 3546 SDValue IntFNeg = DAG.getNode(ISD::XOR, SL, SrcVT, Src, 3547 DAG.getConstant(0x8000, SL, SrcVT)); 3548 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFNeg); 3549 } 3550 default: 3551 return SDValue(); 3552 } 3553 } 3554 3555 SDValue AMDGPUTargetLowering::performFAbsCombine(SDNode *N, 3556 DAGCombinerInfo &DCI) const { 3557 SelectionDAG &DAG = DCI.DAG; 3558 SDValue N0 = N->getOperand(0); 3559 3560 if (!N0.hasOneUse()) 3561 return SDValue(); 3562 3563 switch (N0.getOpcode()) { 3564 case ISD::FP16_TO_FP: { 3565 assert(!Subtarget->has16BitInsts() && "should only see if f16 is illegal"); 3566 SDLoc SL(N); 3567 SDValue Src = N0.getOperand(0); 3568 EVT SrcVT = Src.getValueType(); 3569 3570 // fabs (fp16_to_fp x) -> fp16_to_fp (and x, 0x7fff) 3571 SDValue IntFAbs = DAG.getNode(ISD::AND, SL, SrcVT, Src, 3572 DAG.getConstant(0x7fff, SL, SrcVT)); 3573 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFAbs); 3574 } 3575 default: 3576 return SDValue(); 3577 } 3578 } 3579 3580 SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N, 3581 DAGCombinerInfo &DCI) const { 3582 SelectionDAG &DAG = DCI.DAG; 3583 SDLoc DL(N); 3584 3585 switch(N->getOpcode()) { 3586 default: 3587 break; 3588 case ISD::BITCAST: { 3589 EVT DestVT = N->getValueType(0); 3590 3591 // Push casts through vector builds. This helps avoid emitting a large 3592 // number of copies when materializing floating point vector constants. 3593 // 3594 // vNt1 bitcast (vNt0 (build_vector t0:x, t0:y)) => 3595 // vnt1 = build_vector (t1 (bitcast t0:x)), (t1 (bitcast t0:y)) 3596 if (DestVT.isVector()) { 3597 SDValue Src = N->getOperand(0); 3598 if (Src.getOpcode() == ISD::BUILD_VECTOR) { 3599 EVT SrcVT = Src.getValueType(); 3600 unsigned NElts = DestVT.getVectorNumElements(); 3601 3602 if (SrcVT.getVectorNumElements() == NElts) { 3603 EVT DestEltVT = DestVT.getVectorElementType(); 3604 3605 SmallVector<SDValue, 8> CastedElts; 3606 SDLoc SL(N); 3607 for (unsigned I = 0, E = SrcVT.getVectorNumElements(); I != E; ++I) { 3608 SDValue Elt = Src.getOperand(I); 3609 CastedElts.push_back(DAG.getNode(ISD::BITCAST, DL, DestEltVT, Elt)); 3610 } 3611 3612 return DAG.getBuildVector(DestVT, SL, CastedElts); 3613 } 3614 } 3615 } 3616 3617 if (DestVT.getSizeInBits() != 64 && !DestVT.isVector()) 3618 break; 3619 3620 // Fold bitcasts of constants. 3621 // 3622 // v2i32 (bitcast i64:k) -> build_vector lo_32(k), hi_32(k) 3623 // TODO: Generalize and move to DAGCombiner 3624 SDValue Src = N->getOperand(0); 3625 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src)) { 3626 assert(Src.getValueType() == MVT::i64); 3627 SDLoc SL(N); 3628 uint64_t CVal = C->getZExtValue(); 3629 return DAG.getNode(ISD::BUILD_VECTOR, SL, DestVT, 3630 DAG.getConstant(Lo_32(CVal), SL, MVT::i32), 3631 DAG.getConstant(Hi_32(CVal), SL, MVT::i32)); 3632 } 3633 3634 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Src)) { 3635 const APInt &Val = C->getValueAPF().bitcastToAPInt(); 3636 SDLoc SL(N); 3637 uint64_t CVal = Val.getZExtValue(); 3638 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, 3639 DAG.getConstant(Lo_32(CVal), SL, MVT::i32), 3640 DAG.getConstant(Hi_32(CVal), SL, MVT::i32)); 3641 3642 return DAG.getNode(ISD::BITCAST, SL, DestVT, Vec); 3643 } 3644 3645 break; 3646 } 3647 case ISD::SHL: { 3648 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 3649 break; 3650 3651 return performShlCombine(N, DCI); 3652 } 3653 case ISD::SRL: { 3654 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 3655 break; 3656 3657 return performSrlCombine(N, DCI); 3658 } 3659 case ISD::SRA: { 3660 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 3661 break; 3662 3663 return performSraCombine(N, DCI); 3664 } 3665 case ISD::MUL: 3666 return performMulCombine(N, DCI); 3667 case ISD::MULHS: 3668 return performMulhsCombine(N, DCI); 3669 case ISD::MULHU: 3670 return performMulhuCombine(N, DCI); 3671 case AMDGPUISD::MUL_I24: 3672 case AMDGPUISD::MUL_U24: 3673 case AMDGPUISD::MULHI_I24: 3674 case AMDGPUISD::MULHI_U24: { 3675 // If the first call to simplify is successfull, then N may end up being 3676 // deleted, so we shouldn't call simplifyI24 again. 3677 simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI); 3678 return SDValue(); 3679 } 3680 case AMDGPUISD::MUL_LOHI_I24: 3681 case AMDGPUISD::MUL_LOHI_U24: 3682 return performMulLoHi24Combine(N, DCI); 3683 case ISD::SELECT: 3684 return performSelectCombine(N, DCI); 3685 case ISD::FNEG: 3686 return performFNegCombine(N, DCI); 3687 case ISD::FABS: 3688 return performFAbsCombine(N, DCI); 3689 case AMDGPUISD::BFE_I32: 3690 case AMDGPUISD::BFE_U32: { 3691 assert(!N->getValueType(0).isVector() && 3692 "Vector handling of BFE not implemented"); 3693 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2)); 3694 if (!Width) 3695 break; 3696 3697 uint32_t WidthVal = Width->getZExtValue() & 0x1f; 3698 if (WidthVal == 0) 3699 return DAG.getConstant(0, DL, MVT::i32); 3700 3701 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); 3702 if (!Offset) 3703 break; 3704 3705 SDValue BitsFrom = N->getOperand(0); 3706 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f; 3707 3708 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32; 3709 3710 if (OffsetVal == 0) { 3711 // This is already sign / zero extended, so try to fold away extra BFEs. 3712 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal); 3713 3714 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom); 3715 if (OpSignBits >= SignBits) 3716 return BitsFrom; 3717 3718 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal); 3719 if (Signed) { 3720 // This is a sign_extend_inreg. Replace it to take advantage of existing 3721 // DAG Combines. If not eliminated, we will match back to BFE during 3722 // selection. 3723 3724 // TODO: The sext_inreg of extended types ends, although we can could 3725 // handle them in a single BFE. 3726 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom, 3727 DAG.getValueType(SmallVT)); 3728 } 3729 3730 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT); 3731 } 3732 3733 if (ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(BitsFrom)) { 3734 if (Signed) { 3735 return constantFoldBFE<int32_t>(DAG, 3736 CVal->getSExtValue(), 3737 OffsetVal, 3738 WidthVal, 3739 DL); 3740 } 3741 3742 return constantFoldBFE<uint32_t>(DAG, 3743 CVal->getZExtValue(), 3744 OffsetVal, 3745 WidthVal, 3746 DL); 3747 } 3748 3749 if ((OffsetVal + WidthVal) >= 32 && 3750 !(Subtarget->hasSDWA() && OffsetVal == 16 && WidthVal == 16)) { 3751 SDValue ShiftVal = DAG.getConstant(OffsetVal, DL, MVT::i32); 3752 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32, 3753 BitsFrom, ShiftVal); 3754 } 3755 3756 if (BitsFrom.hasOneUse()) { 3757 APInt Demanded = APInt::getBitsSet(32, 3758 OffsetVal, 3759 OffsetVal + WidthVal); 3760 3761 KnownBits Known; 3762 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), 3763 !DCI.isBeforeLegalizeOps()); 3764 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3765 if (TLI.ShrinkDemandedConstant(BitsFrom, Demanded, TLO) || 3766 TLI.SimplifyDemandedBits(BitsFrom, Demanded, Known, TLO)) { 3767 DCI.CommitTargetLoweringOpt(TLO); 3768 } 3769 } 3770 3771 break; 3772 } 3773 case ISD::LOAD: 3774 return performLoadCombine(N, DCI); 3775 case ISD::STORE: 3776 return performStoreCombine(N, DCI); 3777 case AMDGPUISD::CLAMP: 3778 return performClampCombine(N, DCI); 3779 case AMDGPUISD::RCP: { 3780 if (const auto *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) { 3781 // XXX - Should this flush denormals? 3782 const APFloat &Val = CFP->getValueAPF(); 3783 APFloat One(Val.getSemantics(), "1.0"); 3784 return DAG.getConstantFP(One / Val, SDLoc(N), N->getValueType(0)); 3785 } 3786 3787 break; 3788 } 3789 case ISD::AssertZext: 3790 case ISD::AssertSext: 3791 return performAssertSZExtCombine(N, DCI); 3792 } 3793 return SDValue(); 3794 } 3795 3796 //===----------------------------------------------------------------------===// 3797 // Helper functions 3798 //===----------------------------------------------------------------------===// 3799 3800 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG, 3801 const TargetRegisterClass *RC, 3802 unsigned Reg, EVT VT, 3803 const SDLoc &SL, 3804 bool RawReg) const { 3805 MachineFunction &MF = DAG.getMachineFunction(); 3806 MachineRegisterInfo &MRI = MF.getRegInfo(); 3807 unsigned VReg; 3808 3809 if (!MRI.isLiveIn(Reg)) { 3810 VReg = MRI.createVirtualRegister(RC); 3811 MRI.addLiveIn(Reg, VReg); 3812 } else { 3813 VReg = MRI.getLiveInVirtReg(Reg); 3814 } 3815 3816 if (RawReg) 3817 return DAG.getRegister(VReg, VT); 3818 3819 return DAG.getCopyFromReg(DAG.getEntryNode(), SL, VReg, VT); 3820 } 3821 3822 SDValue AMDGPUTargetLowering::loadStackInputValue(SelectionDAG &DAG, 3823 EVT VT, 3824 const SDLoc &SL, 3825 int64_t Offset) const { 3826 MachineFunction &MF = DAG.getMachineFunction(); 3827 MachineFrameInfo &MFI = MF.getFrameInfo(); 3828 3829 int FI = MFI.CreateFixedObject(VT.getStoreSize(), Offset, true); 3830 auto SrcPtrInfo = MachinePointerInfo::getStack(MF, Offset); 3831 SDValue Ptr = DAG.getFrameIndex(FI, MVT::i32); 3832 3833 return DAG.getLoad(VT, SL, DAG.getEntryNode(), Ptr, SrcPtrInfo, 4, 3834 MachineMemOperand::MODereferenceable | 3835 MachineMemOperand::MOInvariant); 3836 } 3837 3838 SDValue AMDGPUTargetLowering::storeStackInputValue(SelectionDAG &DAG, 3839 const SDLoc &SL, 3840 SDValue Chain, 3841 SDValue StackPtr, 3842 SDValue ArgVal, 3843 int64_t Offset) const { 3844 MachineFunction &MF = DAG.getMachineFunction(); 3845 MachinePointerInfo DstInfo = MachinePointerInfo::getStack(MF, Offset); 3846 SDValue PtrOffset = DAG.getConstant(Offset, SL, MVT::i32); 3847 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i32, StackPtr, PtrOffset); 3848 3849 SDValue Store = DAG.getStore(Chain, SL, ArgVal, Ptr, DstInfo, 4, 3850 MachineMemOperand::MODereferenceable); 3851 return Store; 3852 } 3853 3854 SDValue AMDGPUTargetLowering::loadInputValue(SelectionDAG &DAG, 3855 const TargetRegisterClass *RC, 3856 EVT VT, const SDLoc &SL, 3857 const ArgDescriptor &Arg) const { 3858 assert(Arg && "Attempting to load missing argument"); 3859 3860 if (Arg.isRegister()) 3861 return CreateLiveInRegister(DAG, RC, Arg.getRegister(), VT, SL); 3862 return loadStackInputValue(DAG, VT, SL, Arg.getStackOffset()); 3863 } 3864 3865 uint32_t AMDGPUTargetLowering::getImplicitParameterOffset( 3866 const AMDGPUMachineFunction *MFI, const ImplicitParameter Param) const { 3867 unsigned Alignment = Subtarget->getAlignmentForImplicitArgPtr(); 3868 uint64_t ArgOffset = alignTo(MFI->getABIArgOffset(), Alignment); 3869 switch (Param) { 3870 case GRID_DIM: 3871 return ArgOffset; 3872 case GRID_OFFSET: 3873 return ArgOffset + 4; 3874 } 3875 llvm_unreachable("unexpected implicit parameter type"); 3876 } 3877 3878 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node; 3879 3880 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const { 3881 switch ((AMDGPUISD::NodeType)Opcode) { 3882 case AMDGPUISD::FIRST_NUMBER: break; 3883 // AMDIL DAG nodes 3884 NODE_NAME_CASE(UMUL); 3885 NODE_NAME_CASE(BRANCH_COND); 3886 3887 // AMDGPU DAG nodes 3888 NODE_NAME_CASE(IF) 3889 NODE_NAME_CASE(ELSE) 3890 NODE_NAME_CASE(LOOP) 3891 NODE_NAME_CASE(CALL) 3892 NODE_NAME_CASE(TC_RETURN) 3893 NODE_NAME_CASE(TRAP) 3894 NODE_NAME_CASE(RET_FLAG) 3895 NODE_NAME_CASE(RETURN_TO_EPILOG) 3896 NODE_NAME_CASE(ENDPGM) 3897 NODE_NAME_CASE(DWORDADDR) 3898 NODE_NAME_CASE(FRACT) 3899 NODE_NAME_CASE(SETCC) 3900 NODE_NAME_CASE(SETREG) 3901 NODE_NAME_CASE(FMA_W_CHAIN) 3902 NODE_NAME_CASE(FMUL_W_CHAIN) 3903 NODE_NAME_CASE(CLAMP) 3904 NODE_NAME_CASE(COS_HW) 3905 NODE_NAME_CASE(SIN_HW) 3906 NODE_NAME_CASE(FMAX_LEGACY) 3907 NODE_NAME_CASE(FMIN_LEGACY) 3908 NODE_NAME_CASE(FMAX3) 3909 NODE_NAME_CASE(SMAX3) 3910 NODE_NAME_CASE(UMAX3) 3911 NODE_NAME_CASE(FMIN3) 3912 NODE_NAME_CASE(SMIN3) 3913 NODE_NAME_CASE(UMIN3) 3914 NODE_NAME_CASE(FMED3) 3915 NODE_NAME_CASE(SMED3) 3916 NODE_NAME_CASE(UMED3) 3917 NODE_NAME_CASE(URECIP) 3918 NODE_NAME_CASE(DIV_SCALE) 3919 NODE_NAME_CASE(DIV_FMAS) 3920 NODE_NAME_CASE(DIV_FIXUP) 3921 NODE_NAME_CASE(FMAD_FTZ) 3922 NODE_NAME_CASE(TRIG_PREOP) 3923 NODE_NAME_CASE(RCP) 3924 NODE_NAME_CASE(RSQ) 3925 NODE_NAME_CASE(RCP_LEGACY) 3926 NODE_NAME_CASE(RSQ_LEGACY) 3927 NODE_NAME_CASE(FMUL_LEGACY) 3928 NODE_NAME_CASE(RSQ_CLAMP) 3929 NODE_NAME_CASE(LDEXP) 3930 NODE_NAME_CASE(FP_CLASS) 3931 NODE_NAME_CASE(DOT4) 3932 NODE_NAME_CASE(CARRY) 3933 NODE_NAME_CASE(BORROW) 3934 NODE_NAME_CASE(BFE_U32) 3935 NODE_NAME_CASE(BFE_I32) 3936 NODE_NAME_CASE(BFI) 3937 NODE_NAME_CASE(BFM) 3938 NODE_NAME_CASE(FFBH_U32) 3939 NODE_NAME_CASE(FFBH_I32) 3940 NODE_NAME_CASE(FFBL_B32) 3941 NODE_NAME_CASE(MUL_U24) 3942 NODE_NAME_CASE(MUL_I24) 3943 NODE_NAME_CASE(MULHI_U24) 3944 NODE_NAME_CASE(MULHI_I24) 3945 NODE_NAME_CASE(MUL_LOHI_U24) 3946 NODE_NAME_CASE(MUL_LOHI_I24) 3947 NODE_NAME_CASE(MAD_U24) 3948 NODE_NAME_CASE(MAD_I24) 3949 NODE_NAME_CASE(TEXTURE_FETCH) 3950 NODE_NAME_CASE(EXPORT) 3951 NODE_NAME_CASE(EXPORT_DONE) 3952 NODE_NAME_CASE(R600_EXPORT) 3953 NODE_NAME_CASE(CONST_ADDRESS) 3954 NODE_NAME_CASE(REGISTER_LOAD) 3955 NODE_NAME_CASE(REGISTER_STORE) 3956 NODE_NAME_CASE(SAMPLE) 3957 NODE_NAME_CASE(SAMPLEB) 3958 NODE_NAME_CASE(SAMPLED) 3959 NODE_NAME_CASE(SAMPLEL) 3960 NODE_NAME_CASE(CVT_F32_UBYTE0) 3961 NODE_NAME_CASE(CVT_F32_UBYTE1) 3962 NODE_NAME_CASE(CVT_F32_UBYTE2) 3963 NODE_NAME_CASE(CVT_F32_UBYTE3) 3964 NODE_NAME_CASE(CVT_PKRTZ_F16_F32) 3965 NODE_NAME_CASE(FP_TO_FP16) 3966 NODE_NAME_CASE(FP16_ZEXT) 3967 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR) 3968 NODE_NAME_CASE(CONST_DATA_PTR) 3969 NODE_NAME_CASE(PC_ADD_REL_OFFSET) 3970 NODE_NAME_CASE(KILL) 3971 NODE_NAME_CASE(DUMMY_CHAIN) 3972 case AMDGPUISD::FIRST_MEM_OPCODE_NUMBER: break; 3973 NODE_NAME_CASE(INIT_EXEC) 3974 NODE_NAME_CASE(INIT_EXEC_FROM_INPUT) 3975 NODE_NAME_CASE(SENDMSG) 3976 NODE_NAME_CASE(SENDMSGHALT) 3977 NODE_NAME_CASE(INTERP_MOV) 3978 NODE_NAME_CASE(INTERP_P1) 3979 NODE_NAME_CASE(INTERP_P2) 3980 NODE_NAME_CASE(STORE_MSKOR) 3981 NODE_NAME_CASE(LOAD_CONSTANT) 3982 NODE_NAME_CASE(TBUFFER_STORE_FORMAT) 3983 NODE_NAME_CASE(TBUFFER_STORE_FORMAT_X3) 3984 NODE_NAME_CASE(TBUFFER_LOAD_FORMAT) 3985 NODE_NAME_CASE(ATOMIC_CMP_SWAP) 3986 NODE_NAME_CASE(ATOMIC_INC) 3987 NODE_NAME_CASE(ATOMIC_DEC) 3988 NODE_NAME_CASE(BUFFER_LOAD) 3989 NODE_NAME_CASE(BUFFER_LOAD_FORMAT) 3990 case AMDGPUISD::LAST_AMDGPU_ISD_NUMBER: break; 3991 } 3992 return nullptr; 3993 } 3994 3995 SDValue AMDGPUTargetLowering::getSqrtEstimate(SDValue Operand, 3996 SelectionDAG &DAG, int Enabled, 3997 int &RefinementSteps, 3998 bool &UseOneConstNR, 3999 bool Reciprocal) const { 4000 EVT VT = Operand.getValueType(); 4001 4002 if (VT == MVT::f32) { 4003 RefinementSteps = 0; 4004 return DAG.getNode(AMDGPUISD::RSQ, SDLoc(Operand), VT, Operand); 4005 } 4006 4007 // TODO: There is also f64 rsq instruction, but the documentation is less 4008 // clear on its precision. 4009 4010 return SDValue(); 4011 } 4012 4013 SDValue AMDGPUTargetLowering::getRecipEstimate(SDValue Operand, 4014 SelectionDAG &DAG, int Enabled, 4015 int &RefinementSteps) const { 4016 EVT VT = Operand.getValueType(); 4017 4018 if (VT == MVT::f32) { 4019 // Reciprocal, < 1 ulp error. 4020 // 4021 // This reciprocal approximation converges to < 0.5 ulp error with one 4022 // newton rhapson performed with two fused multiple adds (FMAs). 4023 4024 RefinementSteps = 0; 4025 return DAG.getNode(AMDGPUISD::RCP, SDLoc(Operand), VT, Operand); 4026 } 4027 4028 // TODO: There is also f64 rcp instruction, but the documentation is less 4029 // clear on its precision. 4030 4031 return SDValue(); 4032 } 4033 4034 void AMDGPUTargetLowering::computeKnownBitsForTargetNode( 4035 const SDValue Op, KnownBits &Known, 4036 const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth) const { 4037 4038 Known.resetAll(); // Don't know anything. 4039 4040 unsigned Opc = Op.getOpcode(); 4041 4042 switch (Opc) { 4043 default: 4044 break; 4045 case AMDGPUISD::CARRY: 4046 case AMDGPUISD::BORROW: { 4047 Known.Zero = APInt::getHighBitsSet(32, 31); 4048 break; 4049 } 4050 4051 case AMDGPUISD::BFE_I32: 4052 case AMDGPUISD::BFE_U32: { 4053 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 4054 if (!CWidth) 4055 return; 4056 4057 uint32_t Width = CWidth->getZExtValue() & 0x1f; 4058 4059 if (Opc == AMDGPUISD::BFE_U32) 4060 Known.Zero = APInt::getHighBitsSet(32, 32 - Width); 4061 4062 break; 4063 } 4064 case AMDGPUISD::FP_TO_FP16: 4065 case AMDGPUISD::FP16_ZEXT: { 4066 unsigned BitWidth = Known.getBitWidth(); 4067 4068 // High bits are zero. 4069 Known.Zero = APInt::getHighBitsSet(BitWidth, BitWidth - 16); 4070 break; 4071 } 4072 case AMDGPUISD::MUL_U24: 4073 case AMDGPUISD::MUL_I24: { 4074 KnownBits LHSKnown, RHSKnown; 4075 DAG.computeKnownBits(Op.getOperand(0), LHSKnown, Depth + 1); 4076 DAG.computeKnownBits(Op.getOperand(1), RHSKnown, Depth + 1); 4077 4078 unsigned TrailZ = LHSKnown.countMinTrailingZeros() + 4079 RHSKnown.countMinTrailingZeros(); 4080 Known.Zero.setLowBits(std::min(TrailZ, 32u)); 4081 4082 unsigned LHSValBits = 32 - std::max(LHSKnown.countMinSignBits(), 8u); 4083 unsigned RHSValBits = 32 - std::max(RHSKnown.countMinSignBits(), 8u); 4084 unsigned MaxValBits = std::min(LHSValBits + RHSValBits, 32u); 4085 if (MaxValBits >= 32) 4086 break; 4087 bool Negative = false; 4088 if (Opc == AMDGPUISD::MUL_I24) { 4089 bool LHSNegative = !!(LHSKnown.One & (1 << 23)); 4090 bool LHSPositive = !!(LHSKnown.Zero & (1 << 23)); 4091 bool RHSNegative = !!(RHSKnown.One & (1 << 23)); 4092 bool RHSPositive = !!(RHSKnown.Zero & (1 << 23)); 4093 if ((!LHSNegative && !LHSPositive) || (!RHSNegative && !RHSPositive)) 4094 break; 4095 Negative = (LHSNegative && RHSPositive) || (LHSPositive && RHSNegative); 4096 } 4097 if (Negative) 4098 Known.One.setHighBits(32 - MaxValBits); 4099 else 4100 Known.Zero.setHighBits(32 - MaxValBits); 4101 break; 4102 } 4103 } 4104 } 4105 4106 unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode( 4107 SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, 4108 unsigned Depth) const { 4109 switch (Op.getOpcode()) { 4110 case AMDGPUISD::BFE_I32: { 4111 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 4112 if (!Width) 4113 return 1; 4114 4115 unsigned SignBits = 32 - Width->getZExtValue() + 1; 4116 if (!isNullConstant(Op.getOperand(1))) 4117 return SignBits; 4118 4119 // TODO: Could probably figure something out with non-0 offsets. 4120 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1); 4121 return std::max(SignBits, Op0SignBits); 4122 } 4123 4124 case AMDGPUISD::BFE_U32: { 4125 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 4126 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1; 4127 } 4128 4129 case AMDGPUISD::CARRY: 4130 case AMDGPUISD::BORROW: 4131 return 31; 4132 case AMDGPUISD::FP_TO_FP16: 4133 case AMDGPUISD::FP16_ZEXT: 4134 return 16; 4135 default: 4136 return 1; 4137 } 4138 } 4139