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