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