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