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