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