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