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