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