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