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