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