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