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