1 //===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 /// \file 10 /// Custom DAG lowering for SI 11 // 12 //===----------------------------------------------------------------------===// 13 14 #if defined(_MSC_VER) || defined(__MINGW32__) 15 // Provide M_PI. 16 #define _USE_MATH_DEFINES 17 #endif 18 19 #include "SIISelLowering.h" 20 #include "AMDGPU.h" 21 #include "AMDGPUSubtarget.h" 22 #include "AMDGPUTargetMachine.h" 23 #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 24 #include "SIDefines.h" 25 #include "SIInstrInfo.h" 26 #include "SIMachineFunctionInfo.h" 27 #include "SIRegisterInfo.h" 28 #include "Utils/AMDGPUBaseInfo.h" 29 #include "llvm/ADT/APFloat.h" 30 #include "llvm/ADT/APInt.h" 31 #include "llvm/ADT/ArrayRef.h" 32 #include "llvm/ADT/BitVector.h" 33 #include "llvm/ADT/SmallVector.h" 34 #include "llvm/ADT/Statistic.h" 35 #include "llvm/ADT/StringRef.h" 36 #include "llvm/ADT/StringSwitch.h" 37 #include "llvm/ADT/Twine.h" 38 #include "llvm/Analysis/LegacyDivergenceAnalysis.h" 39 #include "llvm/CodeGen/Analysis.h" 40 #include "llvm/CodeGen/CallingConvLower.h" 41 #include "llvm/CodeGen/DAGCombine.h" 42 #include "llvm/CodeGen/ISDOpcodes.h" 43 #include "llvm/CodeGen/MachineBasicBlock.h" 44 #include "llvm/CodeGen/MachineFrameInfo.h" 45 #include "llvm/CodeGen/MachineFunction.h" 46 #include "llvm/CodeGen/MachineInstr.h" 47 #include "llvm/CodeGen/MachineInstrBuilder.h" 48 #include "llvm/CodeGen/MachineLoopInfo.h" 49 #include "llvm/CodeGen/MachineMemOperand.h" 50 #include "llvm/CodeGen/MachineModuleInfo.h" 51 #include "llvm/CodeGen/MachineOperand.h" 52 #include "llvm/CodeGen/MachineRegisterInfo.h" 53 #include "llvm/CodeGen/SelectionDAG.h" 54 #include "llvm/CodeGen/SelectionDAGNodes.h" 55 #include "llvm/CodeGen/TargetCallingConv.h" 56 #include "llvm/CodeGen/TargetRegisterInfo.h" 57 #include "llvm/CodeGen/ValueTypes.h" 58 #include "llvm/IR/Constants.h" 59 #include "llvm/IR/DataLayout.h" 60 #include "llvm/IR/DebugLoc.h" 61 #include "llvm/IR/DerivedTypes.h" 62 #include "llvm/IR/DiagnosticInfo.h" 63 #include "llvm/IR/Function.h" 64 #include "llvm/IR/GlobalValue.h" 65 #include "llvm/IR/InstrTypes.h" 66 #include "llvm/IR/Instruction.h" 67 #include "llvm/IR/Instructions.h" 68 #include "llvm/IR/IntrinsicInst.h" 69 #include "llvm/IR/Type.h" 70 #include "llvm/Support/Casting.h" 71 #include "llvm/Support/CodeGen.h" 72 #include "llvm/Support/CommandLine.h" 73 #include "llvm/Support/Compiler.h" 74 #include "llvm/Support/ErrorHandling.h" 75 #include "llvm/Support/KnownBits.h" 76 #include "llvm/Support/MachineValueType.h" 77 #include "llvm/Support/MathExtras.h" 78 #include "llvm/Target/TargetOptions.h" 79 #include <cassert> 80 #include <cmath> 81 #include <cstdint> 82 #include <iterator> 83 #include <tuple> 84 #include <utility> 85 #include <vector> 86 87 using namespace llvm; 88 89 #define DEBUG_TYPE "si-lower" 90 91 STATISTIC(NumTailCalls, "Number of tail calls"); 92 93 static cl::opt<bool> DisableLoopAlignment( 94 "amdgpu-disable-loop-alignment", 95 cl::desc("Do not align and prefetch loops"), 96 cl::init(false)); 97 98 static bool hasFP32Denormals(const MachineFunction &MF) { 99 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 100 return Info->getMode().allFP32Denormals(); 101 } 102 103 static bool hasFP64FP16Denormals(const MachineFunction &MF) { 104 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 105 return Info->getMode().allFP64FP16Denormals(); 106 } 107 108 static unsigned findFirstFreeSGPR(CCState &CCInfo) { 109 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs(); 110 for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) { 111 if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) { 112 return AMDGPU::SGPR0 + Reg; 113 } 114 } 115 llvm_unreachable("Cannot allocate sgpr"); 116 } 117 118 SITargetLowering::SITargetLowering(const TargetMachine &TM, 119 const GCNSubtarget &STI) 120 : AMDGPUTargetLowering(TM, STI), 121 Subtarget(&STI) { 122 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass); 123 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass); 124 125 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass); 126 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass); 127 128 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass); 129 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass); 130 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass); 131 132 addRegisterClass(MVT::v3i32, &AMDGPU::SGPR_96RegClass); 133 addRegisterClass(MVT::v3f32, &AMDGPU::VReg_96RegClass); 134 135 addRegisterClass(MVT::v2i64, &AMDGPU::SGPR_128RegClass); 136 addRegisterClass(MVT::v2f64, &AMDGPU::SGPR_128RegClass); 137 138 addRegisterClass(MVT::v4i32, &AMDGPU::SGPR_128RegClass); 139 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass); 140 141 addRegisterClass(MVT::v5i32, &AMDGPU::SGPR_160RegClass); 142 addRegisterClass(MVT::v5f32, &AMDGPU::VReg_160RegClass); 143 144 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass); 145 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass); 146 147 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass); 148 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass); 149 150 if (Subtarget->has16BitInsts()) { 151 addRegisterClass(MVT::i16, &AMDGPU::SReg_32RegClass); 152 addRegisterClass(MVT::f16, &AMDGPU::SReg_32RegClass); 153 154 // Unless there are also VOP3P operations, not operations are really legal. 155 addRegisterClass(MVT::v2i16, &AMDGPU::SReg_32RegClass); 156 addRegisterClass(MVT::v2f16, &AMDGPU::SReg_32RegClass); 157 addRegisterClass(MVT::v4i16, &AMDGPU::SReg_64RegClass); 158 addRegisterClass(MVT::v4f16, &AMDGPU::SReg_64RegClass); 159 } 160 161 if (Subtarget->hasMAIInsts()) { 162 addRegisterClass(MVT::v32i32, &AMDGPU::VReg_1024RegClass); 163 addRegisterClass(MVT::v32f32, &AMDGPU::VReg_1024RegClass); 164 } 165 166 computeRegisterProperties(Subtarget->getRegisterInfo()); 167 168 // The boolean content concept here is too inflexible. Compares only ever 169 // really produce a 1-bit result. Any copy/extend from these will turn into a 170 // select, and zext/1 or sext/-1 are equally cheap. Arbitrarily choose 0/1, as 171 // it's what most targets use. 172 setBooleanContents(ZeroOrOneBooleanContent); 173 setBooleanVectorContents(ZeroOrOneBooleanContent); 174 175 // We need to custom lower vector stores from local memory 176 setOperationAction(ISD::LOAD, MVT::v2i32, Custom); 177 setOperationAction(ISD::LOAD, MVT::v3i32, Custom); 178 setOperationAction(ISD::LOAD, MVT::v4i32, Custom); 179 setOperationAction(ISD::LOAD, MVT::v5i32, Custom); 180 setOperationAction(ISD::LOAD, MVT::v8i32, Custom); 181 setOperationAction(ISD::LOAD, MVT::v16i32, Custom); 182 setOperationAction(ISD::LOAD, MVT::i1, Custom); 183 setOperationAction(ISD::LOAD, MVT::v32i32, Custom); 184 185 setOperationAction(ISD::STORE, MVT::v2i32, Custom); 186 setOperationAction(ISD::STORE, MVT::v3i32, Custom); 187 setOperationAction(ISD::STORE, MVT::v4i32, Custom); 188 setOperationAction(ISD::STORE, MVT::v5i32, Custom); 189 setOperationAction(ISD::STORE, MVT::v8i32, Custom); 190 setOperationAction(ISD::STORE, MVT::v16i32, Custom); 191 setOperationAction(ISD::STORE, MVT::i1, Custom); 192 setOperationAction(ISD::STORE, MVT::v32i32, Custom); 193 194 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Expand); 195 setTruncStoreAction(MVT::v3i32, MVT::v3i16, Expand); 196 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand); 197 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand); 198 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand); 199 setTruncStoreAction(MVT::v32i32, MVT::v32i16, Expand); 200 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Expand); 201 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Expand); 202 setTruncStoreAction(MVT::v8i32, MVT::v8i8, Expand); 203 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand); 204 setTruncStoreAction(MVT::v32i32, MVT::v32i8, Expand); 205 206 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 207 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); 208 209 setOperationAction(ISD::SELECT, MVT::i1, Promote); 210 setOperationAction(ISD::SELECT, MVT::i64, Custom); 211 setOperationAction(ISD::SELECT, MVT::f64, Promote); 212 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64); 213 214 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand); 215 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand); 216 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand); 217 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand); 218 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand); 219 220 setOperationAction(ISD::SETCC, MVT::i1, Promote); 221 setOperationAction(ISD::SETCC, MVT::v2i1, Expand); 222 setOperationAction(ISD::SETCC, MVT::v4i1, Expand); 223 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32); 224 225 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand); 226 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand); 227 228 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom); 229 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom); 230 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom); 231 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom); 232 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom); 233 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v3i16, Custom); 234 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom); 235 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom); 236 237 setOperationAction(ISD::BRCOND, MVT::Other, Custom); 238 setOperationAction(ISD::BR_CC, MVT::i1, Expand); 239 setOperationAction(ISD::BR_CC, MVT::i32, Expand); 240 setOperationAction(ISD::BR_CC, MVT::i64, Expand); 241 setOperationAction(ISD::BR_CC, MVT::f32, Expand); 242 setOperationAction(ISD::BR_CC, MVT::f64, Expand); 243 244 setOperationAction(ISD::UADDO, MVT::i32, Legal); 245 setOperationAction(ISD::USUBO, MVT::i32, Legal); 246 247 setOperationAction(ISD::ADDCARRY, MVT::i32, Legal); 248 setOperationAction(ISD::SUBCARRY, MVT::i32, Legal); 249 250 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand); 251 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand); 252 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand); 253 254 #if 0 255 setOperationAction(ISD::ADDCARRY, MVT::i64, Legal); 256 setOperationAction(ISD::SUBCARRY, MVT::i64, Legal); 257 #endif 258 259 // We only support LOAD/STORE and vector manipulation ops for vectors 260 // with > 4 elements. 261 for (MVT VT : { MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, 262 MVT::v2i64, MVT::v2f64, MVT::v4i16, MVT::v4f16, 263 MVT::v32i32, MVT::v32f32 }) { 264 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) { 265 switch (Op) { 266 case ISD::LOAD: 267 case ISD::STORE: 268 case ISD::BUILD_VECTOR: 269 case ISD::BITCAST: 270 case ISD::EXTRACT_VECTOR_ELT: 271 case ISD::INSERT_VECTOR_ELT: 272 case ISD::INSERT_SUBVECTOR: 273 case ISD::EXTRACT_SUBVECTOR: 274 case ISD::SCALAR_TO_VECTOR: 275 break; 276 case ISD::CONCAT_VECTORS: 277 setOperationAction(Op, VT, Custom); 278 break; 279 default: 280 setOperationAction(Op, VT, Expand); 281 break; 282 } 283 } 284 } 285 286 setOperationAction(ISD::FP_EXTEND, MVT::v4f32, Expand); 287 288 // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that 289 // is expanded to avoid having two separate loops in case the index is a VGPR. 290 291 // Most operations are naturally 32-bit vector operations. We only support 292 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32. 293 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) { 294 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote); 295 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32); 296 297 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote); 298 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32); 299 300 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote); 301 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32); 302 303 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote); 304 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32); 305 } 306 307 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand); 308 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand); 309 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand); 310 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand); 311 312 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f16, Custom); 313 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom); 314 315 // Avoid stack access for these. 316 // TODO: Generalize to more vector types. 317 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i16, Custom); 318 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f16, Custom); 319 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom); 320 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f16, Custom); 321 322 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i16, Custom); 323 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f16, Custom); 324 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i8, Custom); 325 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i8, Custom); 326 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i8, Custom); 327 328 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i8, Custom); 329 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i8, Custom); 330 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i8, Custom); 331 332 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i16, Custom); 333 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f16, Custom); 334 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom); 335 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f16, Custom); 336 337 // Deal with vec3 vector operations when widened to vec4. 338 setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v3i32, Custom); 339 setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v3f32, Custom); 340 setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v4i32, Custom); 341 setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v4f32, Custom); 342 343 // Deal with vec5 vector operations when widened to vec8. 344 setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v5i32, Custom); 345 setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v5f32, Custom); 346 setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v8i32, Custom); 347 setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v8f32, Custom); 348 349 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling, 350 // and output demarshalling 351 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom); 352 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom); 353 354 // We can't return success/failure, only the old value, 355 // let LLVM add the comparison 356 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand); 357 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand); 358 359 if (Subtarget->hasFlatAddressSpace()) { 360 setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom); 361 setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom); 362 } 363 364 setOperationAction(ISD::BSWAP, MVT::i32, Legal); 365 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal); 366 367 // On SI this is s_memtime and s_memrealtime on VI. 368 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal); 369 setOperationAction(ISD::TRAP, MVT::Other, Custom); 370 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Custom); 371 372 if (Subtarget->has16BitInsts()) { 373 setOperationAction(ISD::FPOW, MVT::f16, Promote); 374 setOperationAction(ISD::FLOG, MVT::f16, Custom); 375 setOperationAction(ISD::FEXP, MVT::f16, Custom); 376 setOperationAction(ISD::FLOG10, MVT::f16, Custom); 377 } 378 379 // v_mad_f32 does not support denormals. We report it as unconditionally 380 // legal, and the context where it is formed will disallow it when fp32 381 // denormals are enabled. 382 setOperationAction(ISD::FMAD, MVT::f32, Legal); 383 384 if (!Subtarget->hasBFI()) { 385 // fcopysign can be done in a single instruction with BFI. 386 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); 387 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 388 } 389 390 if (!Subtarget->hasBCNT(32)) 391 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 392 393 if (!Subtarget->hasBCNT(64)) 394 setOperationAction(ISD::CTPOP, MVT::i64, Expand); 395 396 if (Subtarget->hasFFBH()) 397 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Custom); 398 399 if (Subtarget->hasFFBL()) 400 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Custom); 401 402 // We only really have 32-bit BFE instructions (and 16-bit on VI). 403 // 404 // On SI+ there are 64-bit BFEs, but they are scalar only and there isn't any 405 // effort to match them now. We want this to be false for i64 cases when the 406 // extraction isn't restricted to the upper or lower half. Ideally we would 407 // have some pass reduce 64-bit extracts to 32-bit if possible. Extracts that 408 // span the midpoint are probably relatively rare, so don't worry about them 409 // for now. 410 if (Subtarget->hasBFE()) 411 setHasExtractBitsInsn(true); 412 413 setOperationAction(ISD::FMINNUM, MVT::f32, Custom); 414 setOperationAction(ISD::FMAXNUM, MVT::f32, Custom); 415 setOperationAction(ISD::FMINNUM, MVT::f64, Custom); 416 setOperationAction(ISD::FMAXNUM, MVT::f64, Custom); 417 418 419 // These are really only legal for ieee_mode functions. We should be avoiding 420 // them for functions that don't have ieee_mode enabled, so just say they are 421 // legal. 422 setOperationAction(ISD::FMINNUM_IEEE, MVT::f32, Legal); 423 setOperationAction(ISD::FMAXNUM_IEEE, MVT::f32, Legal); 424 setOperationAction(ISD::FMINNUM_IEEE, MVT::f64, Legal); 425 setOperationAction(ISD::FMAXNUM_IEEE, MVT::f64, Legal); 426 427 428 if (Subtarget->haveRoundOpsF64()) { 429 setOperationAction(ISD::FTRUNC, MVT::f64, Legal); 430 setOperationAction(ISD::FCEIL, MVT::f64, Legal); 431 setOperationAction(ISD::FRINT, MVT::f64, Legal); 432 } else { 433 setOperationAction(ISD::FCEIL, MVT::f64, Custom); 434 setOperationAction(ISD::FTRUNC, MVT::f64, Custom); 435 setOperationAction(ISD::FRINT, MVT::f64, Custom); 436 setOperationAction(ISD::FFLOOR, MVT::f64, Custom); 437 } 438 439 setOperationAction(ISD::FFLOOR, MVT::f64, Legal); 440 441 setOperationAction(ISD::FSIN, MVT::f32, Custom); 442 setOperationAction(ISD::FCOS, MVT::f32, Custom); 443 setOperationAction(ISD::FDIV, MVT::f32, Custom); 444 setOperationAction(ISD::FDIV, MVT::f64, Custom); 445 446 if (Subtarget->has16BitInsts()) { 447 setOperationAction(ISD::Constant, MVT::i16, Legal); 448 449 setOperationAction(ISD::SMIN, MVT::i16, Legal); 450 setOperationAction(ISD::SMAX, MVT::i16, Legal); 451 452 setOperationAction(ISD::UMIN, MVT::i16, Legal); 453 setOperationAction(ISD::UMAX, MVT::i16, Legal); 454 455 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Promote); 456 AddPromotedToType(ISD::SIGN_EXTEND, MVT::i16, MVT::i32); 457 458 setOperationAction(ISD::ROTR, MVT::i16, Promote); 459 setOperationAction(ISD::ROTL, MVT::i16, Promote); 460 461 setOperationAction(ISD::SDIV, MVT::i16, Promote); 462 setOperationAction(ISD::UDIV, MVT::i16, Promote); 463 setOperationAction(ISD::SREM, MVT::i16, Promote); 464 setOperationAction(ISD::UREM, MVT::i16, Promote); 465 466 setOperationAction(ISD::BSWAP, MVT::i16, Promote); 467 setOperationAction(ISD::BITREVERSE, MVT::i16, Promote); 468 469 setOperationAction(ISD::CTTZ, MVT::i16, Promote); 470 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Promote); 471 setOperationAction(ISD::CTLZ, MVT::i16, Promote); 472 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Promote); 473 setOperationAction(ISD::CTPOP, MVT::i16, Promote); 474 475 setOperationAction(ISD::SELECT_CC, MVT::i16, Expand); 476 477 setOperationAction(ISD::BR_CC, MVT::i16, Expand); 478 479 setOperationAction(ISD::LOAD, MVT::i16, Custom); 480 481 setTruncStoreAction(MVT::i64, MVT::i16, Expand); 482 483 setOperationAction(ISD::FP16_TO_FP, MVT::i16, Promote); 484 AddPromotedToType(ISD::FP16_TO_FP, MVT::i16, MVT::i32); 485 setOperationAction(ISD::FP_TO_FP16, MVT::i16, Promote); 486 AddPromotedToType(ISD::FP_TO_FP16, MVT::i16, MVT::i32); 487 488 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote); 489 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote); 490 491 // F16 - Constant Actions. 492 setOperationAction(ISD::ConstantFP, MVT::f16, Legal); 493 494 // F16 - Load/Store Actions. 495 setOperationAction(ISD::LOAD, MVT::f16, Promote); 496 AddPromotedToType(ISD::LOAD, MVT::f16, MVT::i16); 497 setOperationAction(ISD::STORE, MVT::f16, Promote); 498 AddPromotedToType(ISD::STORE, MVT::f16, MVT::i16); 499 500 // F16 - VOP1 Actions. 501 setOperationAction(ISD::FP_ROUND, MVT::f16, Custom); 502 setOperationAction(ISD::FCOS, MVT::f16, Custom); 503 setOperationAction(ISD::FSIN, MVT::f16, Custom); 504 505 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Custom); 506 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Custom); 507 508 setOperationAction(ISD::FP_TO_SINT, MVT::f16, Promote); 509 setOperationAction(ISD::FP_TO_UINT, MVT::f16, Promote); 510 setOperationAction(ISD::SINT_TO_FP, MVT::f16, Promote); 511 setOperationAction(ISD::UINT_TO_FP, MVT::f16, Promote); 512 setOperationAction(ISD::FROUND, MVT::f16, Custom); 513 514 // F16 - VOP2 Actions. 515 setOperationAction(ISD::BR_CC, MVT::f16, Expand); 516 setOperationAction(ISD::SELECT_CC, MVT::f16, Expand); 517 518 setOperationAction(ISD::FDIV, MVT::f16, Custom); 519 520 // F16 - VOP3 Actions. 521 setOperationAction(ISD::FMA, MVT::f16, Legal); 522 if (STI.hasMadF16()) 523 setOperationAction(ISD::FMAD, MVT::f16, Legal); 524 525 for (MVT VT : {MVT::v2i16, MVT::v2f16, MVT::v4i16, MVT::v4f16}) { 526 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) { 527 switch (Op) { 528 case ISD::LOAD: 529 case ISD::STORE: 530 case ISD::BUILD_VECTOR: 531 case ISD::BITCAST: 532 case ISD::EXTRACT_VECTOR_ELT: 533 case ISD::INSERT_VECTOR_ELT: 534 case ISD::INSERT_SUBVECTOR: 535 case ISD::EXTRACT_SUBVECTOR: 536 case ISD::SCALAR_TO_VECTOR: 537 break; 538 case ISD::CONCAT_VECTORS: 539 setOperationAction(Op, VT, Custom); 540 break; 541 default: 542 setOperationAction(Op, VT, Expand); 543 break; 544 } 545 } 546 } 547 548 // XXX - Do these do anything? Vector constants turn into build_vector. 549 setOperationAction(ISD::Constant, MVT::v2i16, Legal); 550 setOperationAction(ISD::ConstantFP, MVT::v2f16, Legal); 551 552 setOperationAction(ISD::UNDEF, MVT::v2i16, Legal); 553 setOperationAction(ISD::UNDEF, MVT::v2f16, Legal); 554 555 setOperationAction(ISD::STORE, MVT::v2i16, Promote); 556 AddPromotedToType(ISD::STORE, MVT::v2i16, MVT::i32); 557 setOperationAction(ISD::STORE, MVT::v2f16, Promote); 558 AddPromotedToType(ISD::STORE, MVT::v2f16, MVT::i32); 559 560 setOperationAction(ISD::LOAD, MVT::v2i16, Promote); 561 AddPromotedToType(ISD::LOAD, MVT::v2i16, MVT::i32); 562 setOperationAction(ISD::LOAD, MVT::v2f16, Promote); 563 AddPromotedToType(ISD::LOAD, MVT::v2f16, MVT::i32); 564 565 setOperationAction(ISD::AND, MVT::v2i16, Promote); 566 AddPromotedToType(ISD::AND, MVT::v2i16, MVT::i32); 567 setOperationAction(ISD::OR, MVT::v2i16, Promote); 568 AddPromotedToType(ISD::OR, MVT::v2i16, MVT::i32); 569 setOperationAction(ISD::XOR, MVT::v2i16, Promote); 570 AddPromotedToType(ISD::XOR, MVT::v2i16, MVT::i32); 571 572 setOperationAction(ISD::LOAD, MVT::v4i16, Promote); 573 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::v2i32); 574 setOperationAction(ISD::LOAD, MVT::v4f16, Promote); 575 AddPromotedToType(ISD::LOAD, MVT::v4f16, MVT::v2i32); 576 577 setOperationAction(ISD::STORE, MVT::v4i16, Promote); 578 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::v2i32); 579 setOperationAction(ISD::STORE, MVT::v4f16, Promote); 580 AddPromotedToType(ISD::STORE, MVT::v4f16, MVT::v2i32); 581 582 setOperationAction(ISD::ANY_EXTEND, MVT::v2i32, Expand); 583 setOperationAction(ISD::ZERO_EXTEND, MVT::v2i32, Expand); 584 setOperationAction(ISD::SIGN_EXTEND, MVT::v2i32, Expand); 585 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Expand); 586 587 setOperationAction(ISD::ANY_EXTEND, MVT::v4i32, Expand); 588 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i32, Expand); 589 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i32, Expand); 590 591 if (!Subtarget->hasVOP3PInsts()) { 592 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i16, Custom); 593 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f16, Custom); 594 } 595 596 setOperationAction(ISD::FNEG, MVT::v2f16, Legal); 597 // This isn't really legal, but this avoids the legalizer unrolling it (and 598 // allows matching fneg (fabs x) patterns) 599 setOperationAction(ISD::FABS, MVT::v2f16, Legal); 600 601 setOperationAction(ISD::FMAXNUM, MVT::f16, Custom); 602 setOperationAction(ISD::FMINNUM, MVT::f16, Custom); 603 setOperationAction(ISD::FMAXNUM_IEEE, MVT::f16, Legal); 604 setOperationAction(ISD::FMINNUM_IEEE, MVT::f16, Legal); 605 606 setOperationAction(ISD::FMINNUM_IEEE, MVT::v4f16, Custom); 607 setOperationAction(ISD::FMAXNUM_IEEE, MVT::v4f16, Custom); 608 609 setOperationAction(ISD::FMINNUM, MVT::v4f16, Expand); 610 setOperationAction(ISD::FMAXNUM, MVT::v4f16, Expand); 611 } 612 613 if (Subtarget->hasVOP3PInsts()) { 614 setOperationAction(ISD::ADD, MVT::v2i16, Legal); 615 setOperationAction(ISD::SUB, MVT::v2i16, Legal); 616 setOperationAction(ISD::MUL, MVT::v2i16, Legal); 617 setOperationAction(ISD::SHL, MVT::v2i16, Legal); 618 setOperationAction(ISD::SRL, MVT::v2i16, Legal); 619 setOperationAction(ISD::SRA, MVT::v2i16, Legal); 620 setOperationAction(ISD::SMIN, MVT::v2i16, Legal); 621 setOperationAction(ISD::UMIN, MVT::v2i16, Legal); 622 setOperationAction(ISD::SMAX, MVT::v2i16, Legal); 623 setOperationAction(ISD::UMAX, MVT::v2i16, Legal); 624 625 setOperationAction(ISD::FADD, MVT::v2f16, Legal); 626 setOperationAction(ISD::FMUL, MVT::v2f16, Legal); 627 setOperationAction(ISD::FMA, MVT::v2f16, Legal); 628 629 setOperationAction(ISD::FMINNUM_IEEE, MVT::v2f16, Legal); 630 setOperationAction(ISD::FMAXNUM_IEEE, MVT::v2f16, Legal); 631 632 setOperationAction(ISD::FCANONICALIZE, MVT::v2f16, Legal); 633 634 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i16, Custom); 635 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f16, Custom); 636 637 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f16, Custom); 638 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom); 639 640 setOperationAction(ISD::SHL, MVT::v4i16, Custom); 641 setOperationAction(ISD::SRA, MVT::v4i16, Custom); 642 setOperationAction(ISD::SRL, MVT::v4i16, Custom); 643 setOperationAction(ISD::ADD, MVT::v4i16, Custom); 644 setOperationAction(ISD::SUB, MVT::v4i16, Custom); 645 setOperationAction(ISD::MUL, MVT::v4i16, Custom); 646 647 setOperationAction(ISD::SMIN, MVT::v4i16, Custom); 648 setOperationAction(ISD::SMAX, MVT::v4i16, Custom); 649 setOperationAction(ISD::UMIN, MVT::v4i16, Custom); 650 setOperationAction(ISD::UMAX, MVT::v4i16, Custom); 651 652 setOperationAction(ISD::FADD, MVT::v4f16, Custom); 653 setOperationAction(ISD::FMUL, MVT::v4f16, Custom); 654 setOperationAction(ISD::FMA, MVT::v4f16, Custom); 655 656 setOperationAction(ISD::FMAXNUM, MVT::v2f16, Custom); 657 setOperationAction(ISD::FMINNUM, MVT::v2f16, Custom); 658 659 setOperationAction(ISD::FMINNUM, MVT::v4f16, Custom); 660 setOperationAction(ISD::FMAXNUM, MVT::v4f16, Custom); 661 setOperationAction(ISD::FCANONICALIZE, MVT::v4f16, Custom); 662 663 setOperationAction(ISD::FEXP, MVT::v2f16, Custom); 664 setOperationAction(ISD::SELECT, MVT::v4i16, Custom); 665 setOperationAction(ISD::SELECT, MVT::v4f16, Custom); 666 } 667 668 setOperationAction(ISD::FNEG, MVT::v4f16, Custom); 669 setOperationAction(ISD::FABS, MVT::v4f16, Custom); 670 671 if (Subtarget->has16BitInsts()) { 672 setOperationAction(ISD::SELECT, MVT::v2i16, Promote); 673 AddPromotedToType(ISD::SELECT, MVT::v2i16, MVT::i32); 674 setOperationAction(ISD::SELECT, MVT::v2f16, Promote); 675 AddPromotedToType(ISD::SELECT, MVT::v2f16, MVT::i32); 676 } else { 677 // Legalization hack. 678 setOperationAction(ISD::SELECT, MVT::v2i16, Custom); 679 setOperationAction(ISD::SELECT, MVT::v2f16, Custom); 680 681 setOperationAction(ISD::FNEG, MVT::v2f16, Custom); 682 setOperationAction(ISD::FABS, MVT::v2f16, Custom); 683 } 684 685 for (MVT VT : { MVT::v4i16, MVT::v4f16, MVT::v2i8, MVT::v4i8, MVT::v8i8 }) { 686 setOperationAction(ISD::SELECT, VT, Custom); 687 } 688 689 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 690 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom); 691 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom); 692 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i16, Custom); 693 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f16, Custom); 694 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v2i16, Custom); 695 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v2f16, Custom); 696 697 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::v2f16, Custom); 698 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::v2i16, Custom); 699 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::v4f16, Custom); 700 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::v4i16, Custom); 701 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::v8f16, Custom); 702 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom); 703 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::f16, Custom); 704 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i16, Custom); 705 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i8, Custom); 706 707 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom); 708 setOperationAction(ISD::INTRINSIC_VOID, MVT::v2i16, Custom); 709 setOperationAction(ISD::INTRINSIC_VOID, MVT::v2f16, Custom); 710 setOperationAction(ISD::INTRINSIC_VOID, MVT::v4f16, Custom); 711 setOperationAction(ISD::INTRINSIC_VOID, MVT::v4i16, Custom); 712 setOperationAction(ISD::INTRINSIC_VOID, MVT::f16, Custom); 713 setOperationAction(ISD::INTRINSIC_VOID, MVT::i16, Custom); 714 setOperationAction(ISD::INTRINSIC_VOID, MVT::i8, Custom); 715 716 setTargetDAGCombine(ISD::ADD); 717 setTargetDAGCombine(ISD::ADDCARRY); 718 setTargetDAGCombine(ISD::SUB); 719 setTargetDAGCombine(ISD::SUBCARRY); 720 setTargetDAGCombine(ISD::FADD); 721 setTargetDAGCombine(ISD::FSUB); 722 setTargetDAGCombine(ISD::FMINNUM); 723 setTargetDAGCombine(ISD::FMAXNUM); 724 setTargetDAGCombine(ISD::FMINNUM_IEEE); 725 setTargetDAGCombine(ISD::FMAXNUM_IEEE); 726 setTargetDAGCombine(ISD::FMA); 727 setTargetDAGCombine(ISD::SMIN); 728 setTargetDAGCombine(ISD::SMAX); 729 setTargetDAGCombine(ISD::UMIN); 730 setTargetDAGCombine(ISD::UMAX); 731 setTargetDAGCombine(ISD::SETCC); 732 setTargetDAGCombine(ISD::AND); 733 setTargetDAGCombine(ISD::OR); 734 setTargetDAGCombine(ISD::XOR); 735 setTargetDAGCombine(ISD::SINT_TO_FP); 736 setTargetDAGCombine(ISD::UINT_TO_FP); 737 setTargetDAGCombine(ISD::FCANONICALIZE); 738 setTargetDAGCombine(ISD::SCALAR_TO_VECTOR); 739 setTargetDAGCombine(ISD::ZERO_EXTEND); 740 setTargetDAGCombine(ISD::SIGN_EXTEND_INREG); 741 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT); 742 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT); 743 744 // All memory operations. Some folding on the pointer operand is done to help 745 // matching the constant offsets in the addressing modes. 746 setTargetDAGCombine(ISD::LOAD); 747 setTargetDAGCombine(ISD::STORE); 748 setTargetDAGCombine(ISD::ATOMIC_LOAD); 749 setTargetDAGCombine(ISD::ATOMIC_STORE); 750 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP); 751 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS); 752 setTargetDAGCombine(ISD::ATOMIC_SWAP); 753 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD); 754 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB); 755 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND); 756 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR); 757 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR); 758 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND); 759 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN); 760 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX); 761 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN); 762 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX); 763 setTargetDAGCombine(ISD::ATOMIC_LOAD_FADD); 764 765 setSchedulingPreference(Sched::RegPressure); 766 } 767 768 const GCNSubtarget *SITargetLowering::getSubtarget() const { 769 return Subtarget; 770 } 771 772 //===----------------------------------------------------------------------===// 773 // TargetLowering queries 774 //===----------------------------------------------------------------------===// 775 776 // v_mad_mix* support a conversion from f16 to f32. 777 // 778 // There is only one special case when denormals are enabled we don't currently, 779 // where this is OK to use. 780 bool SITargetLowering::isFPExtFoldable(const SelectionDAG &DAG, unsigned Opcode, 781 EVT DestVT, EVT SrcVT) const { 782 return ((Opcode == ISD::FMAD && Subtarget->hasMadMixInsts()) || 783 (Opcode == ISD::FMA && Subtarget->hasFmaMixInsts())) && 784 DestVT.getScalarType() == MVT::f32 && 785 SrcVT.getScalarType() == MVT::f16 && 786 // TODO: This probably only requires no input flushing? 787 !hasFP32Denormals(DAG.getMachineFunction()); 788 } 789 790 bool SITargetLowering::isShuffleMaskLegal(ArrayRef<int>, EVT) const { 791 // SI has some legal vector types, but no legal vector operations. Say no 792 // shuffles are legal in order to prefer scalarizing some vector operations. 793 return false; 794 } 795 796 MVT SITargetLowering::getRegisterTypeForCallingConv(LLVMContext &Context, 797 CallingConv::ID CC, 798 EVT VT) const { 799 if (CC == CallingConv::AMDGPU_KERNEL) 800 return TargetLowering::getRegisterTypeForCallingConv(Context, CC, VT); 801 802 if (VT.isVector()) { 803 EVT ScalarVT = VT.getScalarType(); 804 unsigned Size = ScalarVT.getSizeInBits(); 805 if (Size == 32) 806 return ScalarVT.getSimpleVT(); 807 808 if (Size > 32) 809 return MVT::i32; 810 811 if (Size == 16 && Subtarget->has16BitInsts()) 812 return VT.isInteger() ? MVT::v2i16 : MVT::v2f16; 813 } else if (VT.getSizeInBits() > 32) 814 return MVT::i32; 815 816 return TargetLowering::getRegisterTypeForCallingConv(Context, CC, VT); 817 } 818 819 unsigned SITargetLowering::getNumRegistersForCallingConv(LLVMContext &Context, 820 CallingConv::ID CC, 821 EVT VT) const { 822 if (CC == CallingConv::AMDGPU_KERNEL) 823 return TargetLowering::getNumRegistersForCallingConv(Context, CC, VT); 824 825 if (VT.isVector()) { 826 unsigned NumElts = VT.getVectorNumElements(); 827 EVT ScalarVT = VT.getScalarType(); 828 unsigned Size = ScalarVT.getSizeInBits(); 829 830 if (Size == 32) 831 return NumElts; 832 833 if (Size > 32) 834 return NumElts * ((Size + 31) / 32); 835 836 if (Size == 16 && Subtarget->has16BitInsts()) 837 return (NumElts + 1) / 2; 838 } else if (VT.getSizeInBits() > 32) 839 return (VT.getSizeInBits() + 31) / 32; 840 841 return TargetLowering::getNumRegistersForCallingConv(Context, CC, VT); 842 } 843 844 unsigned SITargetLowering::getVectorTypeBreakdownForCallingConv( 845 LLVMContext &Context, CallingConv::ID CC, 846 EVT VT, EVT &IntermediateVT, 847 unsigned &NumIntermediates, MVT &RegisterVT) const { 848 if (CC != CallingConv::AMDGPU_KERNEL && VT.isVector()) { 849 unsigned NumElts = VT.getVectorNumElements(); 850 EVT ScalarVT = VT.getScalarType(); 851 unsigned Size = ScalarVT.getSizeInBits(); 852 if (Size == 32) { 853 RegisterVT = ScalarVT.getSimpleVT(); 854 IntermediateVT = RegisterVT; 855 NumIntermediates = NumElts; 856 return NumIntermediates; 857 } 858 859 if (Size > 32) { 860 RegisterVT = MVT::i32; 861 IntermediateVT = RegisterVT; 862 NumIntermediates = NumElts * ((Size + 31) / 32); 863 return NumIntermediates; 864 } 865 866 // FIXME: We should fix the ABI to be the same on targets without 16-bit 867 // support, but unless we can properly handle 3-vectors, it will be still be 868 // inconsistent. 869 if (Size == 16 && Subtarget->has16BitInsts()) { 870 RegisterVT = VT.isInteger() ? MVT::v2i16 : MVT::v2f16; 871 IntermediateVT = RegisterVT; 872 NumIntermediates = (NumElts + 1) / 2; 873 return NumIntermediates; 874 } 875 } 876 877 return TargetLowering::getVectorTypeBreakdownForCallingConv( 878 Context, CC, VT, IntermediateVT, NumIntermediates, RegisterVT); 879 } 880 881 // Peek through TFE struct returns to only use the data size. 882 static EVT memVTFromImageReturn(Type *Ty) { 883 auto *ST = dyn_cast<StructType>(Ty); 884 if (!ST) 885 return EVT::getEVT(Ty, true); 886 887 // Some intrinsics return an aggregate type - special case to work out the 888 // correct memVT. 889 // 890 // Only limited forms of aggregate type currently expected. 891 if (ST->getNumContainedTypes() != 2 || 892 !ST->getContainedType(1)->isIntegerTy(32)) 893 return EVT(); 894 return EVT::getEVT(ST->getContainedType(0)); 895 } 896 897 bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 898 const CallInst &CI, 899 MachineFunction &MF, 900 unsigned IntrID) const { 901 if (const AMDGPU::RsrcIntrinsic *RsrcIntr = 902 AMDGPU::lookupRsrcIntrinsic(IntrID)) { 903 AttributeList Attr = Intrinsic::getAttributes(CI.getContext(), 904 (Intrinsic::ID)IntrID); 905 if (Attr.hasFnAttribute(Attribute::ReadNone)) 906 return false; 907 908 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 909 910 if (RsrcIntr->IsImage) { 911 Info.ptrVal = MFI->getImagePSV( 912 *MF.getSubtarget<GCNSubtarget>().getInstrInfo(), 913 CI.getArgOperand(RsrcIntr->RsrcArg)); 914 Info.align.reset(); 915 } else { 916 Info.ptrVal = MFI->getBufferPSV( 917 *MF.getSubtarget<GCNSubtarget>().getInstrInfo(), 918 CI.getArgOperand(RsrcIntr->RsrcArg)); 919 } 920 921 Info.flags = MachineMemOperand::MODereferenceable; 922 if (Attr.hasFnAttribute(Attribute::ReadOnly)) { 923 Info.opc = ISD::INTRINSIC_W_CHAIN; 924 // TODO: Account for dmask reducing loaded size. 925 Info.memVT = memVTFromImageReturn(CI.getType()); 926 Info.flags |= MachineMemOperand::MOLoad; 927 } else if (Attr.hasFnAttribute(Attribute::WriteOnly)) { 928 Info.opc = ISD::INTRINSIC_VOID; 929 Info.memVT = MVT::getVT(CI.getArgOperand(0)->getType()); 930 Info.flags |= MachineMemOperand::MOStore; 931 } else { 932 // Atomic 933 Info.opc = ISD::INTRINSIC_W_CHAIN; 934 Info.memVT = MVT::getVT(CI.getType()); 935 Info.flags = MachineMemOperand::MOLoad | 936 MachineMemOperand::MOStore | 937 MachineMemOperand::MODereferenceable; 938 939 // XXX - Should this be volatile without known ordering? 940 Info.flags |= MachineMemOperand::MOVolatile; 941 } 942 return true; 943 } 944 945 switch (IntrID) { 946 case Intrinsic::amdgcn_atomic_inc: 947 case Intrinsic::amdgcn_atomic_dec: 948 case Intrinsic::amdgcn_ds_ordered_add: 949 case Intrinsic::amdgcn_ds_ordered_swap: 950 case Intrinsic::amdgcn_ds_fadd: 951 case Intrinsic::amdgcn_ds_fmin: 952 case Intrinsic::amdgcn_ds_fmax: { 953 Info.opc = ISD::INTRINSIC_W_CHAIN; 954 Info.memVT = MVT::getVT(CI.getType()); 955 Info.ptrVal = CI.getOperand(0); 956 Info.align.reset(); 957 Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore; 958 959 const ConstantInt *Vol = cast<ConstantInt>(CI.getOperand(4)); 960 if (!Vol->isZero()) 961 Info.flags |= MachineMemOperand::MOVolatile; 962 963 return true; 964 } 965 case Intrinsic::amdgcn_buffer_atomic_fadd: { 966 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 967 968 Info.opc = ISD::INTRINSIC_VOID; 969 Info.memVT = MVT::getVT(CI.getOperand(0)->getType()); 970 Info.ptrVal = MFI->getBufferPSV( 971 *MF.getSubtarget<GCNSubtarget>().getInstrInfo(), 972 CI.getArgOperand(1)); 973 Info.align.reset(); 974 Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore; 975 976 const ConstantInt *Vol = dyn_cast<ConstantInt>(CI.getOperand(4)); 977 if (!Vol || !Vol->isZero()) 978 Info.flags |= MachineMemOperand::MOVolatile; 979 980 return true; 981 } 982 case Intrinsic::amdgcn_global_atomic_fadd: { 983 Info.opc = ISD::INTRINSIC_VOID; 984 Info.memVT = MVT::getVT(CI.getOperand(0)->getType() 985 ->getPointerElementType()); 986 Info.ptrVal = CI.getOperand(0); 987 Info.align.reset(); 988 Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore; 989 990 return true; 991 } 992 case Intrinsic::amdgcn_ds_append: 993 case Intrinsic::amdgcn_ds_consume: { 994 Info.opc = ISD::INTRINSIC_W_CHAIN; 995 Info.memVT = MVT::getVT(CI.getType()); 996 Info.ptrVal = CI.getOperand(0); 997 Info.align.reset(); 998 Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore; 999 1000 const ConstantInt *Vol = cast<ConstantInt>(CI.getOperand(1)); 1001 if (!Vol->isZero()) 1002 Info.flags |= MachineMemOperand::MOVolatile; 1003 1004 return true; 1005 } 1006 case Intrinsic::amdgcn_ds_gws_init: 1007 case Intrinsic::amdgcn_ds_gws_barrier: 1008 case Intrinsic::amdgcn_ds_gws_sema_v: 1009 case Intrinsic::amdgcn_ds_gws_sema_br: 1010 case Intrinsic::amdgcn_ds_gws_sema_p: 1011 case Intrinsic::amdgcn_ds_gws_sema_release_all: { 1012 Info.opc = ISD::INTRINSIC_VOID; 1013 1014 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 1015 Info.ptrVal = 1016 MFI->getGWSPSV(*MF.getSubtarget<GCNSubtarget>().getInstrInfo()); 1017 1018 // This is an abstract access, but we need to specify a type and size. 1019 Info.memVT = MVT::i32; 1020 Info.size = 4; 1021 Info.align = Align(4); 1022 1023 Info.flags = MachineMemOperand::MOStore; 1024 if (IntrID == Intrinsic::amdgcn_ds_gws_barrier) 1025 Info.flags = MachineMemOperand::MOLoad; 1026 return true; 1027 } 1028 default: 1029 return false; 1030 } 1031 } 1032 1033 bool SITargetLowering::getAddrModeArguments(IntrinsicInst *II, 1034 SmallVectorImpl<Value*> &Ops, 1035 Type *&AccessTy) const { 1036 switch (II->getIntrinsicID()) { 1037 case Intrinsic::amdgcn_atomic_inc: 1038 case Intrinsic::amdgcn_atomic_dec: 1039 case Intrinsic::amdgcn_ds_ordered_add: 1040 case Intrinsic::amdgcn_ds_ordered_swap: 1041 case Intrinsic::amdgcn_ds_fadd: 1042 case Intrinsic::amdgcn_ds_fmin: 1043 case Intrinsic::amdgcn_ds_fmax: { 1044 Value *Ptr = II->getArgOperand(0); 1045 AccessTy = II->getType(); 1046 Ops.push_back(Ptr); 1047 return true; 1048 } 1049 default: 1050 return false; 1051 } 1052 } 1053 1054 bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const { 1055 if (!Subtarget->hasFlatInstOffsets()) { 1056 // Flat instructions do not have offsets, and only have the register 1057 // address. 1058 return AM.BaseOffs == 0 && AM.Scale == 0; 1059 } 1060 1061 return AM.Scale == 0 && 1062 (AM.BaseOffs == 0 || Subtarget->getInstrInfo()->isLegalFLATOffset( 1063 AM.BaseOffs, AMDGPUAS::FLAT_ADDRESS, 1064 /*Signed=*/false)); 1065 } 1066 1067 bool SITargetLowering::isLegalGlobalAddressingMode(const AddrMode &AM) const { 1068 if (Subtarget->hasFlatGlobalInsts()) 1069 return AM.Scale == 0 && 1070 (AM.BaseOffs == 0 || Subtarget->getInstrInfo()->isLegalFLATOffset( 1071 AM.BaseOffs, AMDGPUAS::GLOBAL_ADDRESS, 1072 /*Signed=*/true)); 1073 1074 if (!Subtarget->hasAddr64() || Subtarget->useFlatForGlobal()) { 1075 // Assume the we will use FLAT for all global memory accesses 1076 // on VI. 1077 // FIXME: This assumption is currently wrong. On VI we still use 1078 // MUBUF instructions for the r + i addressing mode. As currently 1079 // implemented, the MUBUF instructions only work on buffer < 4GB. 1080 // It may be possible to support > 4GB buffers with MUBUF instructions, 1081 // by setting the stride value in the resource descriptor which would 1082 // increase the size limit to (stride * 4GB). However, this is risky, 1083 // because it has never been validated. 1084 return isLegalFlatAddressingMode(AM); 1085 } 1086 1087 return isLegalMUBUFAddressingMode(AM); 1088 } 1089 1090 bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const { 1091 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and 1092 // additionally can do r + r + i with addr64. 32-bit has more addressing 1093 // mode options. Depending on the resource constant, it can also do 1094 // (i64 r0) + (i32 r1) * (i14 i). 1095 // 1096 // Private arrays end up using a scratch buffer most of the time, so also 1097 // assume those use MUBUF instructions. Scratch loads / stores are currently 1098 // implemented as mubuf instructions with offen bit set, so slightly 1099 // different than the normal addr64. 1100 if (!isUInt<12>(AM.BaseOffs)) 1101 return false; 1102 1103 // FIXME: Since we can split immediate into soffset and immediate offset, 1104 // would it make sense to allow any immediate? 1105 1106 switch (AM.Scale) { 1107 case 0: // r + i or just i, depending on HasBaseReg. 1108 return true; 1109 case 1: 1110 return true; // We have r + r or r + i. 1111 case 2: 1112 if (AM.HasBaseReg) { 1113 // Reject 2 * r + r. 1114 return false; 1115 } 1116 1117 // Allow 2 * r as r + r 1118 // Or 2 * r + i is allowed as r + r + i. 1119 return true; 1120 default: // Don't allow n * r 1121 return false; 1122 } 1123 } 1124 1125 bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL, 1126 const AddrMode &AM, Type *Ty, 1127 unsigned AS, Instruction *I) const { 1128 // No global is ever allowed as a base. 1129 if (AM.BaseGV) 1130 return false; 1131 1132 if (AS == AMDGPUAS::GLOBAL_ADDRESS) 1133 return isLegalGlobalAddressingMode(AM); 1134 1135 if (AS == AMDGPUAS::CONSTANT_ADDRESS || 1136 AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT || 1137 AS == AMDGPUAS::BUFFER_FAT_POINTER) { 1138 // If the offset isn't a multiple of 4, it probably isn't going to be 1139 // correctly aligned. 1140 // FIXME: Can we get the real alignment here? 1141 if (AM.BaseOffs % 4 != 0) 1142 return isLegalMUBUFAddressingMode(AM); 1143 1144 // There are no SMRD extloads, so if we have to do a small type access we 1145 // will use a MUBUF load. 1146 // FIXME?: We also need to do this if unaligned, but we don't know the 1147 // alignment here. 1148 if (Ty->isSized() && DL.getTypeStoreSize(Ty) < 4) 1149 return isLegalGlobalAddressingMode(AM); 1150 1151 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) { 1152 // SMRD instructions have an 8-bit, dword offset on SI. 1153 if (!isUInt<8>(AM.BaseOffs / 4)) 1154 return false; 1155 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) { 1156 // On CI+, this can also be a 32-bit literal constant offset. If it fits 1157 // in 8-bits, it can use a smaller encoding. 1158 if (!isUInt<32>(AM.BaseOffs / 4)) 1159 return false; 1160 } else if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 1161 // On VI, these use the SMEM format and the offset is 20-bit in bytes. 1162 if (!isUInt<20>(AM.BaseOffs)) 1163 return false; 1164 } else 1165 llvm_unreachable("unhandled generation"); 1166 1167 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg. 1168 return true; 1169 1170 if (AM.Scale == 1 && AM.HasBaseReg) 1171 return true; 1172 1173 return false; 1174 1175 } else if (AS == AMDGPUAS::PRIVATE_ADDRESS) { 1176 return isLegalMUBUFAddressingMode(AM); 1177 } else if (AS == AMDGPUAS::LOCAL_ADDRESS || 1178 AS == AMDGPUAS::REGION_ADDRESS) { 1179 // Basic, single offset DS instructions allow a 16-bit unsigned immediate 1180 // field. 1181 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have 1182 // an 8-bit dword offset but we don't know the alignment here. 1183 if (!isUInt<16>(AM.BaseOffs)) 1184 return false; 1185 1186 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg. 1187 return true; 1188 1189 if (AM.Scale == 1 && AM.HasBaseReg) 1190 return true; 1191 1192 return false; 1193 } else if (AS == AMDGPUAS::FLAT_ADDRESS || 1194 AS == AMDGPUAS::UNKNOWN_ADDRESS_SPACE) { 1195 // For an unknown address space, this usually means that this is for some 1196 // reason being used for pure arithmetic, and not based on some addressing 1197 // computation. We don't have instructions that compute pointers with any 1198 // addressing modes, so treat them as having no offset like flat 1199 // instructions. 1200 return isLegalFlatAddressingMode(AM); 1201 } else { 1202 llvm_unreachable("unhandled address space"); 1203 } 1204 } 1205 1206 bool SITargetLowering::canMergeStoresTo(unsigned AS, EVT MemVT, 1207 const SelectionDAG &DAG) const { 1208 if (AS == AMDGPUAS::GLOBAL_ADDRESS || AS == AMDGPUAS::FLAT_ADDRESS) { 1209 return (MemVT.getSizeInBits() <= 4 * 32); 1210 } else if (AS == AMDGPUAS::PRIVATE_ADDRESS) { 1211 unsigned MaxPrivateBits = 8 * getSubtarget()->getMaxPrivateElementSize(); 1212 return (MemVT.getSizeInBits() <= MaxPrivateBits); 1213 } else if (AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::REGION_ADDRESS) { 1214 return (MemVT.getSizeInBits() <= 2 * 32); 1215 } 1216 return true; 1217 } 1218 1219 bool SITargetLowering::allowsMisalignedMemoryAccessesImpl( 1220 unsigned Size, unsigned AddrSpace, unsigned Align, 1221 MachineMemOperand::Flags Flags, bool *IsFast) const { 1222 if (IsFast) 1223 *IsFast = false; 1224 1225 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS || 1226 AddrSpace == AMDGPUAS::REGION_ADDRESS) { 1227 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte 1228 // aligned, 8 byte access in a single operation using ds_read2/write2_b32 1229 // with adjacent offsets. 1230 bool AlignedBy4 = (Align % 4 == 0); 1231 if (IsFast) 1232 *IsFast = AlignedBy4; 1233 1234 return AlignedBy4; 1235 } 1236 1237 // FIXME: We have to be conservative here and assume that flat operations 1238 // will access scratch. If we had access to the IR function, then we 1239 // could determine if any private memory was used in the function. 1240 if (!Subtarget->hasUnalignedScratchAccess() && 1241 (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS || 1242 AddrSpace == AMDGPUAS::FLAT_ADDRESS)) { 1243 bool AlignedBy4 = Align >= 4; 1244 if (IsFast) 1245 *IsFast = AlignedBy4; 1246 1247 return AlignedBy4; 1248 } 1249 1250 if (Subtarget->hasUnalignedBufferAccess()) { 1251 // If we have an uniform constant load, it still requires using a slow 1252 // buffer instruction if unaligned. 1253 if (IsFast) { 1254 *IsFast = (AddrSpace == AMDGPUAS::CONSTANT_ADDRESS || 1255 AddrSpace == AMDGPUAS::CONSTANT_ADDRESS_32BIT) ? 1256 (Align % 4 == 0) : true; 1257 } 1258 1259 return true; 1260 } 1261 1262 // Smaller than dword value must be aligned. 1263 if (Size < 32) 1264 return false; 1265 1266 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the 1267 // byte-address are ignored, thus forcing Dword alignment. 1268 // This applies to private, global, and constant memory. 1269 if (IsFast) 1270 *IsFast = true; 1271 1272 return Size >= 32 && Align >= 4; 1273 } 1274 1275 bool SITargetLowering::allowsMisalignedMemoryAccesses( 1276 EVT VT, unsigned AddrSpace, unsigned Align, MachineMemOperand::Flags Flags, 1277 bool *IsFast) const { 1278 if (IsFast) 1279 *IsFast = false; 1280 1281 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96, 1282 // which isn't a simple VT. 1283 // Until MVT is extended to handle this, simply check for the size and 1284 // rely on the condition below: allow accesses if the size is a multiple of 4. 1285 if (VT == MVT::Other || (VT != MVT::Other && VT.getSizeInBits() > 1024 && 1286 VT.getStoreSize() > 16)) { 1287 return false; 1288 } 1289 1290 return allowsMisalignedMemoryAccessesImpl(VT.getSizeInBits(), AddrSpace, 1291 Align, Flags, IsFast); 1292 } 1293 1294 EVT SITargetLowering::getOptimalMemOpType( 1295 const MemOp &Op, const AttributeList &FuncAttributes) const { 1296 // FIXME: Should account for address space here. 1297 1298 // The default fallback uses the private pointer size as a guess for a type to 1299 // use. Make sure we switch these to 64-bit accesses. 1300 1301 if (Op.size() >= 16 && 1302 Op.isDstAligned(Align(4))) // XXX: Should only do for global 1303 return MVT::v4i32; 1304 1305 if (Op.size() >= 8 && Op.isDstAligned(Align(4))) 1306 return MVT::v2i32; 1307 1308 // Use the default. 1309 return MVT::Other; 1310 } 1311 1312 bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS, 1313 unsigned DestAS) const { 1314 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS); 1315 } 1316 1317 bool SITargetLowering::isMemOpHasNoClobberedMemOperand(const SDNode *N) const { 1318 const MemSDNode *MemNode = cast<MemSDNode>(N); 1319 const Value *Ptr = MemNode->getMemOperand()->getValue(); 1320 const Instruction *I = dyn_cast_or_null<Instruction>(Ptr); 1321 return I && I->getMetadata("amdgpu.noclobber"); 1322 } 1323 1324 bool SITargetLowering::isFreeAddrSpaceCast(unsigned SrcAS, 1325 unsigned DestAS) const { 1326 // Flat -> private/local is a simple truncate. 1327 // Flat -> global is no-op 1328 if (SrcAS == AMDGPUAS::FLAT_ADDRESS) 1329 return true; 1330 1331 return isNoopAddrSpaceCast(SrcAS, DestAS); 1332 } 1333 1334 bool SITargetLowering::isMemOpUniform(const SDNode *N) const { 1335 const MemSDNode *MemNode = cast<MemSDNode>(N); 1336 1337 return AMDGPUInstrInfo::isUniformMMO(MemNode->getMemOperand()); 1338 } 1339 1340 TargetLoweringBase::LegalizeTypeAction 1341 SITargetLowering::getPreferredVectorAction(MVT VT) const { 1342 int NumElts = VT.getVectorNumElements(); 1343 if (NumElts != 1 && VT.getScalarType().bitsLE(MVT::i16)) 1344 return VT.isPow2VectorType() ? TypeSplitVector : TypeWidenVector; 1345 return TargetLoweringBase::getPreferredVectorAction(VT); 1346 } 1347 1348 bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, 1349 Type *Ty) const { 1350 // FIXME: Could be smarter if called for vector constants. 1351 return true; 1352 } 1353 1354 bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const { 1355 if (Subtarget->has16BitInsts() && VT == MVT::i16) { 1356 switch (Op) { 1357 case ISD::LOAD: 1358 case ISD::STORE: 1359 1360 // These operations are done with 32-bit instructions anyway. 1361 case ISD::AND: 1362 case ISD::OR: 1363 case ISD::XOR: 1364 case ISD::SELECT: 1365 // TODO: Extensions? 1366 return true; 1367 default: 1368 return false; 1369 } 1370 } 1371 1372 // SimplifySetCC uses this function to determine whether or not it should 1373 // create setcc with i1 operands. We don't have instructions for i1 setcc. 1374 if (VT == MVT::i1 && Op == ISD::SETCC) 1375 return false; 1376 1377 return TargetLowering::isTypeDesirableForOp(Op, VT); 1378 } 1379 1380 SDValue SITargetLowering::lowerKernArgParameterPtr(SelectionDAG &DAG, 1381 const SDLoc &SL, 1382 SDValue Chain, 1383 uint64_t Offset) const { 1384 const DataLayout &DL = DAG.getDataLayout(); 1385 MachineFunction &MF = DAG.getMachineFunction(); 1386 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1387 1388 const ArgDescriptor *InputPtrReg; 1389 const TargetRegisterClass *RC; 1390 1391 std::tie(InputPtrReg, RC) 1392 = Info->getPreloadedValue(AMDGPUFunctionArgInfo::KERNARG_SEGMENT_PTR); 1393 1394 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); 1395 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS); 1396 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL, 1397 MRI.getLiveInVirtReg(InputPtrReg->getRegister()), PtrVT); 1398 1399 return DAG.getObjectPtrOffset(SL, BasePtr, Offset); 1400 } 1401 1402 SDValue SITargetLowering::getImplicitArgPtr(SelectionDAG &DAG, 1403 const SDLoc &SL) const { 1404 uint64_t Offset = getImplicitParameterOffset(DAG.getMachineFunction(), 1405 FIRST_IMPLICIT); 1406 return lowerKernArgParameterPtr(DAG, SL, DAG.getEntryNode(), Offset); 1407 } 1408 1409 SDValue SITargetLowering::convertArgType(SelectionDAG &DAG, EVT VT, EVT MemVT, 1410 const SDLoc &SL, SDValue Val, 1411 bool Signed, 1412 const ISD::InputArg *Arg) const { 1413 // First, if it is a widened vector, narrow it. 1414 if (VT.isVector() && 1415 VT.getVectorNumElements() != MemVT.getVectorNumElements()) { 1416 EVT NarrowedVT = 1417 EVT::getVectorVT(*DAG.getContext(), MemVT.getVectorElementType(), 1418 VT.getVectorNumElements()); 1419 Val = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, NarrowedVT, Val, 1420 DAG.getConstant(0, SL, MVT::i32)); 1421 } 1422 1423 // Then convert the vector elements or scalar value. 1424 if (Arg && (Arg->Flags.isSExt() || Arg->Flags.isZExt()) && 1425 VT.bitsLT(MemVT)) { 1426 unsigned Opc = Arg->Flags.isZExt() ? ISD::AssertZext : ISD::AssertSext; 1427 Val = DAG.getNode(Opc, SL, MemVT, Val, DAG.getValueType(VT)); 1428 } 1429 1430 if (MemVT.isFloatingPoint()) 1431 Val = getFPExtOrFPTrunc(DAG, Val, SL, VT); 1432 else if (Signed) 1433 Val = DAG.getSExtOrTrunc(Val, SL, VT); 1434 else 1435 Val = DAG.getZExtOrTrunc(Val, SL, VT); 1436 1437 return Val; 1438 } 1439 1440 SDValue SITargetLowering::lowerKernargMemParameter( 1441 SelectionDAG &DAG, EVT VT, EVT MemVT, 1442 const SDLoc &SL, SDValue Chain, 1443 uint64_t Offset, unsigned Align, bool Signed, 1444 const ISD::InputArg *Arg) const { 1445 MachinePointerInfo PtrInfo(AMDGPUAS::CONSTANT_ADDRESS); 1446 1447 // Try to avoid using an extload by loading earlier than the argument address, 1448 // and extracting the relevant bits. The load should hopefully be merged with 1449 // the previous argument. 1450 if (MemVT.getStoreSize() < 4 && Align < 4) { 1451 // TODO: Handle align < 4 and size >= 4 (can happen with packed structs). 1452 int64_t AlignDownOffset = alignDown(Offset, 4); 1453 int64_t OffsetDiff = Offset - AlignDownOffset; 1454 1455 EVT IntVT = MemVT.changeTypeToInteger(); 1456 1457 // TODO: If we passed in the base kernel offset we could have a better 1458 // alignment than 4, but we don't really need it. 1459 SDValue Ptr = lowerKernArgParameterPtr(DAG, SL, Chain, AlignDownOffset); 1460 SDValue Load = DAG.getLoad(MVT::i32, SL, Chain, Ptr, PtrInfo, 4, 1461 MachineMemOperand::MODereferenceable | 1462 MachineMemOperand::MOInvariant); 1463 1464 SDValue ShiftAmt = DAG.getConstant(OffsetDiff * 8, SL, MVT::i32); 1465 SDValue Extract = DAG.getNode(ISD::SRL, SL, MVT::i32, Load, ShiftAmt); 1466 1467 SDValue ArgVal = DAG.getNode(ISD::TRUNCATE, SL, IntVT, Extract); 1468 ArgVal = DAG.getNode(ISD::BITCAST, SL, MemVT, ArgVal); 1469 ArgVal = convertArgType(DAG, VT, MemVT, SL, ArgVal, Signed, Arg); 1470 1471 1472 return DAG.getMergeValues({ ArgVal, Load.getValue(1) }, SL); 1473 } 1474 1475 SDValue Ptr = lowerKernArgParameterPtr(DAG, SL, Chain, Offset); 1476 SDValue Load = DAG.getLoad(MemVT, SL, Chain, Ptr, PtrInfo, Align, 1477 MachineMemOperand::MODereferenceable | 1478 MachineMemOperand::MOInvariant); 1479 1480 SDValue Val = convertArgType(DAG, VT, MemVT, SL, Load, Signed, Arg); 1481 return DAG.getMergeValues({ Val, Load.getValue(1) }, SL); 1482 } 1483 1484 SDValue SITargetLowering::lowerStackParameter(SelectionDAG &DAG, CCValAssign &VA, 1485 const SDLoc &SL, SDValue Chain, 1486 const ISD::InputArg &Arg) const { 1487 MachineFunction &MF = DAG.getMachineFunction(); 1488 MachineFrameInfo &MFI = MF.getFrameInfo(); 1489 1490 if (Arg.Flags.isByVal()) { 1491 unsigned Size = Arg.Flags.getByValSize(); 1492 int FrameIdx = MFI.CreateFixedObject(Size, VA.getLocMemOffset(), false); 1493 return DAG.getFrameIndex(FrameIdx, MVT::i32); 1494 } 1495 1496 unsigned ArgOffset = VA.getLocMemOffset(); 1497 unsigned ArgSize = VA.getValVT().getStoreSize(); 1498 1499 int FI = MFI.CreateFixedObject(ArgSize, ArgOffset, true); 1500 1501 // Create load nodes to retrieve arguments from the stack. 1502 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32); 1503 SDValue ArgValue; 1504 1505 // For NON_EXTLOAD, generic code in getLoad assert(ValVT == MemVT) 1506 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD; 1507 MVT MemVT = VA.getValVT(); 1508 1509 switch (VA.getLocInfo()) { 1510 default: 1511 break; 1512 case CCValAssign::BCvt: 1513 MemVT = VA.getLocVT(); 1514 break; 1515 case CCValAssign::SExt: 1516 ExtType = ISD::SEXTLOAD; 1517 break; 1518 case CCValAssign::ZExt: 1519 ExtType = ISD::ZEXTLOAD; 1520 break; 1521 case CCValAssign::AExt: 1522 ExtType = ISD::EXTLOAD; 1523 break; 1524 } 1525 1526 ArgValue = DAG.getExtLoad( 1527 ExtType, SL, VA.getLocVT(), Chain, FIN, 1528 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), 1529 MemVT); 1530 return ArgValue; 1531 } 1532 1533 SDValue SITargetLowering::getPreloadedValue(SelectionDAG &DAG, 1534 const SIMachineFunctionInfo &MFI, 1535 EVT VT, 1536 AMDGPUFunctionArgInfo::PreloadedValue PVID) const { 1537 const ArgDescriptor *Reg; 1538 const TargetRegisterClass *RC; 1539 1540 std::tie(Reg, RC) = MFI.getPreloadedValue(PVID); 1541 return CreateLiveInRegister(DAG, RC, Reg->getRegister(), VT); 1542 } 1543 1544 static void processShaderInputArgs(SmallVectorImpl<ISD::InputArg> &Splits, 1545 CallingConv::ID CallConv, 1546 ArrayRef<ISD::InputArg> Ins, 1547 BitVector &Skipped, 1548 FunctionType *FType, 1549 SIMachineFunctionInfo *Info) { 1550 for (unsigned I = 0, E = Ins.size(), PSInputNum = 0; I != E; ++I) { 1551 const ISD::InputArg *Arg = &Ins[I]; 1552 1553 assert((!Arg->VT.isVector() || Arg->VT.getScalarSizeInBits() == 16) && 1554 "vector type argument should have been split"); 1555 1556 // First check if it's a PS input addr. 1557 if (CallConv == CallingConv::AMDGPU_PS && 1558 !Arg->Flags.isInReg() && PSInputNum <= 15) { 1559 bool SkipArg = !Arg->Used && !Info->isPSInputAllocated(PSInputNum); 1560 1561 // Inconveniently only the first part of the split is marked as isSplit, 1562 // so skip to the end. We only want to increment PSInputNum once for the 1563 // entire split argument. 1564 if (Arg->Flags.isSplit()) { 1565 while (!Arg->Flags.isSplitEnd()) { 1566 assert((!Arg->VT.isVector() || 1567 Arg->VT.getScalarSizeInBits() == 16) && 1568 "unexpected vector split in ps argument type"); 1569 if (!SkipArg) 1570 Splits.push_back(*Arg); 1571 Arg = &Ins[++I]; 1572 } 1573 } 1574 1575 if (SkipArg) { 1576 // We can safely skip PS inputs. 1577 Skipped.set(Arg->getOrigArgIndex()); 1578 ++PSInputNum; 1579 continue; 1580 } 1581 1582 Info->markPSInputAllocated(PSInputNum); 1583 if (Arg->Used) 1584 Info->markPSInputEnabled(PSInputNum); 1585 1586 ++PSInputNum; 1587 } 1588 1589 Splits.push_back(*Arg); 1590 } 1591 } 1592 1593 // Allocate special inputs passed in VGPRs. 1594 void SITargetLowering::allocateSpecialEntryInputVGPRs(CCState &CCInfo, 1595 MachineFunction &MF, 1596 const SIRegisterInfo &TRI, 1597 SIMachineFunctionInfo &Info) const { 1598 const LLT S32 = LLT::scalar(32); 1599 MachineRegisterInfo &MRI = MF.getRegInfo(); 1600 1601 if (Info.hasWorkItemIDX()) { 1602 Register Reg = AMDGPU::VGPR0; 1603 MRI.setType(MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass), S32); 1604 1605 CCInfo.AllocateReg(Reg); 1606 Info.setWorkItemIDX(ArgDescriptor::createRegister(Reg)); 1607 } 1608 1609 if (Info.hasWorkItemIDY()) { 1610 Register Reg = AMDGPU::VGPR1; 1611 MRI.setType(MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass), S32); 1612 1613 CCInfo.AllocateReg(Reg); 1614 Info.setWorkItemIDY(ArgDescriptor::createRegister(Reg)); 1615 } 1616 1617 if (Info.hasWorkItemIDZ()) { 1618 Register Reg = AMDGPU::VGPR2; 1619 MRI.setType(MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass), S32); 1620 1621 CCInfo.AllocateReg(Reg); 1622 Info.setWorkItemIDZ(ArgDescriptor::createRegister(Reg)); 1623 } 1624 } 1625 1626 // Try to allocate a VGPR at the end of the argument list, or if no argument 1627 // VGPRs are left allocating a stack slot. 1628 // If \p Mask is is given it indicates bitfield position in the register. 1629 // If \p Arg is given use it with new ]p Mask instead of allocating new. 1630 static ArgDescriptor allocateVGPR32Input(CCState &CCInfo, unsigned Mask = ~0u, 1631 ArgDescriptor Arg = ArgDescriptor()) { 1632 if (Arg.isSet()) 1633 return ArgDescriptor::createArg(Arg, Mask); 1634 1635 ArrayRef<MCPhysReg> ArgVGPRs 1636 = makeArrayRef(AMDGPU::VGPR_32RegClass.begin(), 32); 1637 unsigned RegIdx = CCInfo.getFirstUnallocated(ArgVGPRs); 1638 if (RegIdx == ArgVGPRs.size()) { 1639 // Spill to stack required. 1640 int64_t Offset = CCInfo.AllocateStack(4, 4); 1641 1642 return ArgDescriptor::createStack(Offset, Mask); 1643 } 1644 1645 unsigned Reg = ArgVGPRs[RegIdx]; 1646 Reg = CCInfo.AllocateReg(Reg); 1647 assert(Reg != AMDGPU::NoRegister); 1648 1649 MachineFunction &MF = CCInfo.getMachineFunction(); 1650 Register LiveInVReg = MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 1651 MF.getRegInfo().setType(LiveInVReg, LLT::scalar(32)); 1652 return ArgDescriptor::createRegister(Reg, Mask); 1653 } 1654 1655 static ArgDescriptor allocateSGPR32InputImpl(CCState &CCInfo, 1656 const TargetRegisterClass *RC, 1657 unsigned NumArgRegs) { 1658 ArrayRef<MCPhysReg> ArgSGPRs = makeArrayRef(RC->begin(), 32); 1659 unsigned RegIdx = CCInfo.getFirstUnallocated(ArgSGPRs); 1660 if (RegIdx == ArgSGPRs.size()) 1661 report_fatal_error("ran out of SGPRs for arguments"); 1662 1663 unsigned Reg = ArgSGPRs[RegIdx]; 1664 Reg = CCInfo.AllocateReg(Reg); 1665 assert(Reg != AMDGPU::NoRegister); 1666 1667 MachineFunction &MF = CCInfo.getMachineFunction(); 1668 MF.addLiveIn(Reg, RC); 1669 return ArgDescriptor::createRegister(Reg); 1670 } 1671 1672 static ArgDescriptor allocateSGPR32Input(CCState &CCInfo) { 1673 return allocateSGPR32InputImpl(CCInfo, &AMDGPU::SGPR_32RegClass, 32); 1674 } 1675 1676 static ArgDescriptor allocateSGPR64Input(CCState &CCInfo) { 1677 return allocateSGPR32InputImpl(CCInfo, &AMDGPU::SGPR_64RegClass, 16); 1678 } 1679 1680 void SITargetLowering::allocateSpecialInputVGPRs(CCState &CCInfo, 1681 MachineFunction &MF, 1682 const SIRegisterInfo &TRI, 1683 SIMachineFunctionInfo &Info) const { 1684 const unsigned Mask = 0x3ff; 1685 ArgDescriptor Arg; 1686 1687 if (Info.hasWorkItemIDX()) { 1688 Arg = allocateVGPR32Input(CCInfo, Mask); 1689 Info.setWorkItemIDX(Arg); 1690 } 1691 1692 if (Info.hasWorkItemIDY()) { 1693 Arg = allocateVGPR32Input(CCInfo, Mask << 10, Arg); 1694 Info.setWorkItemIDY(Arg); 1695 } 1696 1697 if (Info.hasWorkItemIDZ()) 1698 Info.setWorkItemIDZ(allocateVGPR32Input(CCInfo, Mask << 20, Arg)); 1699 } 1700 1701 void SITargetLowering::allocateSpecialInputSGPRs( 1702 CCState &CCInfo, 1703 MachineFunction &MF, 1704 const SIRegisterInfo &TRI, 1705 SIMachineFunctionInfo &Info) const { 1706 auto &ArgInfo = Info.getArgInfo(); 1707 1708 // TODO: Unify handling with private memory pointers. 1709 1710 if (Info.hasDispatchPtr()) 1711 ArgInfo.DispatchPtr = allocateSGPR64Input(CCInfo); 1712 1713 if (Info.hasQueuePtr()) 1714 ArgInfo.QueuePtr = allocateSGPR64Input(CCInfo); 1715 1716 if (Info.hasKernargSegmentPtr()) 1717 ArgInfo.KernargSegmentPtr = allocateSGPR64Input(CCInfo); 1718 1719 if (Info.hasDispatchID()) 1720 ArgInfo.DispatchID = allocateSGPR64Input(CCInfo); 1721 1722 // flat_scratch_init is not applicable for non-kernel functions. 1723 1724 if (Info.hasWorkGroupIDX()) 1725 ArgInfo.WorkGroupIDX = allocateSGPR32Input(CCInfo); 1726 1727 if (Info.hasWorkGroupIDY()) 1728 ArgInfo.WorkGroupIDY = allocateSGPR32Input(CCInfo); 1729 1730 if (Info.hasWorkGroupIDZ()) 1731 ArgInfo.WorkGroupIDZ = allocateSGPR32Input(CCInfo); 1732 1733 if (Info.hasImplicitArgPtr()) 1734 ArgInfo.ImplicitArgPtr = allocateSGPR64Input(CCInfo); 1735 } 1736 1737 // Allocate special inputs passed in user SGPRs. 1738 void SITargetLowering::allocateHSAUserSGPRs(CCState &CCInfo, 1739 MachineFunction &MF, 1740 const SIRegisterInfo &TRI, 1741 SIMachineFunctionInfo &Info) const { 1742 if (Info.hasImplicitBufferPtr()) { 1743 unsigned ImplicitBufferPtrReg = Info.addImplicitBufferPtr(TRI); 1744 MF.addLiveIn(ImplicitBufferPtrReg, &AMDGPU::SGPR_64RegClass); 1745 CCInfo.AllocateReg(ImplicitBufferPtrReg); 1746 } 1747 1748 // FIXME: How should these inputs interact with inreg / custom SGPR inputs? 1749 if (Info.hasPrivateSegmentBuffer()) { 1750 unsigned PrivateSegmentBufferReg = Info.addPrivateSegmentBuffer(TRI); 1751 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SGPR_128RegClass); 1752 CCInfo.AllocateReg(PrivateSegmentBufferReg); 1753 } 1754 1755 if (Info.hasDispatchPtr()) { 1756 unsigned DispatchPtrReg = Info.addDispatchPtr(TRI); 1757 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SGPR_64RegClass); 1758 CCInfo.AllocateReg(DispatchPtrReg); 1759 } 1760 1761 if (Info.hasQueuePtr()) { 1762 unsigned QueuePtrReg = Info.addQueuePtr(TRI); 1763 MF.addLiveIn(QueuePtrReg, &AMDGPU::SGPR_64RegClass); 1764 CCInfo.AllocateReg(QueuePtrReg); 1765 } 1766 1767 if (Info.hasKernargSegmentPtr()) { 1768 MachineRegisterInfo &MRI = MF.getRegInfo(); 1769 Register InputPtrReg = Info.addKernargSegmentPtr(TRI); 1770 CCInfo.AllocateReg(InputPtrReg); 1771 1772 Register VReg = MF.addLiveIn(InputPtrReg, &AMDGPU::SGPR_64RegClass); 1773 MRI.setType(VReg, LLT::pointer(AMDGPUAS::CONSTANT_ADDRESS, 64)); 1774 } 1775 1776 if (Info.hasDispatchID()) { 1777 unsigned DispatchIDReg = Info.addDispatchID(TRI); 1778 MF.addLiveIn(DispatchIDReg, &AMDGPU::SGPR_64RegClass); 1779 CCInfo.AllocateReg(DispatchIDReg); 1780 } 1781 1782 if (Info.hasFlatScratchInit()) { 1783 unsigned FlatScratchInitReg = Info.addFlatScratchInit(TRI); 1784 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SGPR_64RegClass); 1785 CCInfo.AllocateReg(FlatScratchInitReg); 1786 } 1787 1788 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read 1789 // these from the dispatch pointer. 1790 } 1791 1792 // Allocate special input registers that are initialized per-wave. 1793 void SITargetLowering::allocateSystemSGPRs(CCState &CCInfo, 1794 MachineFunction &MF, 1795 SIMachineFunctionInfo &Info, 1796 CallingConv::ID CallConv, 1797 bool IsShader) const { 1798 if (Info.hasWorkGroupIDX()) { 1799 unsigned Reg = Info.addWorkGroupIDX(); 1800 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass); 1801 CCInfo.AllocateReg(Reg); 1802 } 1803 1804 if (Info.hasWorkGroupIDY()) { 1805 unsigned Reg = Info.addWorkGroupIDY(); 1806 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass); 1807 CCInfo.AllocateReg(Reg); 1808 } 1809 1810 if (Info.hasWorkGroupIDZ()) { 1811 unsigned Reg = Info.addWorkGroupIDZ(); 1812 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass); 1813 CCInfo.AllocateReg(Reg); 1814 } 1815 1816 if (Info.hasWorkGroupInfo()) { 1817 unsigned Reg = Info.addWorkGroupInfo(); 1818 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass); 1819 CCInfo.AllocateReg(Reg); 1820 } 1821 1822 if (Info.hasPrivateSegmentWaveByteOffset()) { 1823 // Scratch wave offset passed in system SGPR. 1824 unsigned PrivateSegmentWaveByteOffsetReg; 1825 1826 if (IsShader) { 1827 PrivateSegmentWaveByteOffsetReg = 1828 Info.getPrivateSegmentWaveByteOffsetSystemSGPR(); 1829 1830 // This is true if the scratch wave byte offset doesn't have a fixed 1831 // location. 1832 if (PrivateSegmentWaveByteOffsetReg == AMDGPU::NoRegister) { 1833 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo); 1834 Info.setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg); 1835 } 1836 } else 1837 PrivateSegmentWaveByteOffsetReg = Info.addPrivateSegmentWaveByteOffset(); 1838 1839 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass); 1840 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg); 1841 } 1842 } 1843 1844 static void reservePrivateMemoryRegs(const TargetMachine &TM, 1845 MachineFunction &MF, 1846 const SIRegisterInfo &TRI, 1847 SIMachineFunctionInfo &Info) { 1848 // Now that we've figured out where the scratch register inputs are, see if 1849 // should reserve the arguments and use them directly. 1850 MachineFrameInfo &MFI = MF.getFrameInfo(); 1851 bool HasStackObjects = MFI.hasStackObjects(); 1852 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 1853 1854 // Record that we know we have non-spill stack objects so we don't need to 1855 // check all stack objects later. 1856 if (HasStackObjects) 1857 Info.setHasNonSpillStackObjects(true); 1858 1859 // Everything live out of a block is spilled with fast regalloc, so it's 1860 // almost certain that spilling will be required. 1861 if (TM.getOptLevel() == CodeGenOpt::None) 1862 HasStackObjects = true; 1863 1864 // For now assume stack access is needed in any callee functions, so we need 1865 // the scratch registers to pass in. 1866 bool RequiresStackAccess = HasStackObjects || MFI.hasCalls(); 1867 1868 if (RequiresStackAccess && ST.isAmdHsaOrMesa(MF.getFunction())) { 1869 // If we have stack objects, we unquestionably need the private buffer 1870 // resource. For the Code Object V2 ABI, this will be the first 4 user 1871 // SGPR inputs. We can reserve those and use them directly. 1872 1873 Register PrivateSegmentBufferReg = 1874 Info.getPreloadedReg(AMDGPUFunctionArgInfo::PRIVATE_SEGMENT_BUFFER); 1875 Info.setScratchRSrcReg(PrivateSegmentBufferReg); 1876 } else { 1877 unsigned ReservedBufferReg = TRI.reservedPrivateSegmentBufferReg(MF); 1878 // We tentatively reserve the last registers (skipping the last registers 1879 // which may contain VCC, FLAT_SCR, and XNACK). After register allocation, 1880 // we'll replace these with the ones immediately after those which were 1881 // really allocated. In the prologue copies will be inserted from the 1882 // argument to these reserved registers. 1883 1884 // Without HSA, relocations are used for the scratch pointer and the 1885 // buffer resource setup is always inserted in the prologue. Scratch wave 1886 // offset is still in an input SGPR. 1887 Info.setScratchRSrcReg(ReservedBufferReg); 1888 } 1889 1890 // hasFP should be accurate for kernels even before the frame is finalized. 1891 if (ST.getFrameLowering()->hasFP(MF)) { 1892 MachineRegisterInfo &MRI = MF.getRegInfo(); 1893 1894 // Try to use s32 as the SP, but move it if it would interfere with input 1895 // arguments. This won't work with calls though. 1896 // 1897 // FIXME: Move SP to avoid any possible inputs, or find a way to spill input 1898 // registers. 1899 if (!MRI.isLiveIn(AMDGPU::SGPR32)) { 1900 Info.setStackPtrOffsetReg(AMDGPU::SGPR32); 1901 } else { 1902 assert(AMDGPU::isShader(MF.getFunction().getCallingConv())); 1903 1904 if (MFI.hasCalls()) 1905 report_fatal_error("call in graphics shader with too many input SGPRs"); 1906 1907 for (unsigned Reg : AMDGPU::SGPR_32RegClass) { 1908 if (!MRI.isLiveIn(Reg)) { 1909 Info.setStackPtrOffsetReg(Reg); 1910 break; 1911 } 1912 } 1913 1914 if (Info.getStackPtrOffsetReg() == AMDGPU::SP_REG) 1915 report_fatal_error("failed to find register for SP"); 1916 } 1917 1918 if (MFI.hasCalls()) { 1919 Info.setScratchWaveOffsetReg(AMDGPU::SGPR33); 1920 Info.setFrameOffsetReg(AMDGPU::SGPR33); 1921 } else { 1922 unsigned ReservedOffsetReg = 1923 TRI.reservedPrivateSegmentWaveByteOffsetReg(MF); 1924 Info.setScratchWaveOffsetReg(ReservedOffsetReg); 1925 Info.setFrameOffsetReg(ReservedOffsetReg); 1926 } 1927 } else if (RequiresStackAccess) { 1928 assert(!MFI.hasCalls()); 1929 // We know there are accesses and they will be done relative to SP, so just 1930 // pin it to the input. 1931 // 1932 // FIXME: Should not do this if inline asm is reading/writing these 1933 // registers. 1934 Register PreloadedSP = Info.getPreloadedReg( 1935 AMDGPUFunctionArgInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); 1936 1937 Info.setStackPtrOffsetReg(PreloadedSP); 1938 Info.setScratchWaveOffsetReg(PreloadedSP); 1939 Info.setFrameOffsetReg(PreloadedSP); 1940 } else { 1941 assert(!MFI.hasCalls()); 1942 1943 // There may not be stack access at all. There may still be spills, or 1944 // access of a constant pointer (in which cases an extra copy will be 1945 // emitted in the prolog). 1946 unsigned ReservedOffsetReg 1947 = TRI.reservedPrivateSegmentWaveByteOffsetReg(MF); 1948 Info.setStackPtrOffsetReg(ReservedOffsetReg); 1949 Info.setScratchWaveOffsetReg(ReservedOffsetReg); 1950 Info.setFrameOffsetReg(ReservedOffsetReg); 1951 } 1952 } 1953 1954 bool SITargetLowering::supportSplitCSR(MachineFunction *MF) const { 1955 const SIMachineFunctionInfo *Info = MF->getInfo<SIMachineFunctionInfo>(); 1956 return !Info->isEntryFunction(); 1957 } 1958 1959 void SITargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const { 1960 1961 } 1962 1963 void SITargetLowering::insertCopiesSplitCSR( 1964 MachineBasicBlock *Entry, 1965 const SmallVectorImpl<MachineBasicBlock *> &Exits) const { 1966 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 1967 1968 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent()); 1969 if (!IStart) 1970 return; 1971 1972 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 1973 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo(); 1974 MachineBasicBlock::iterator MBBI = Entry->begin(); 1975 for (const MCPhysReg *I = IStart; *I; ++I) { 1976 const TargetRegisterClass *RC = nullptr; 1977 if (AMDGPU::SReg_64RegClass.contains(*I)) 1978 RC = &AMDGPU::SGPR_64RegClass; 1979 else if (AMDGPU::SReg_32RegClass.contains(*I)) 1980 RC = &AMDGPU::SGPR_32RegClass; 1981 else 1982 llvm_unreachable("Unexpected register class in CSRsViaCopy!"); 1983 1984 Register NewVR = MRI->createVirtualRegister(RC); 1985 // Create copy from CSR to a virtual register. 1986 Entry->addLiveIn(*I); 1987 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR) 1988 .addReg(*I); 1989 1990 // Insert the copy-back instructions right before the terminator. 1991 for (auto *Exit : Exits) 1992 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(), 1993 TII->get(TargetOpcode::COPY), *I) 1994 .addReg(NewVR); 1995 } 1996 } 1997 1998 SDValue SITargetLowering::LowerFormalArguments( 1999 SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 2000 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, 2001 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 2002 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 2003 2004 MachineFunction &MF = DAG.getMachineFunction(); 2005 const Function &Fn = MF.getFunction(); 2006 FunctionType *FType = MF.getFunction().getFunctionType(); 2007 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 2008 2009 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) { 2010 DiagnosticInfoUnsupported NoGraphicsHSA( 2011 Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc()); 2012 DAG.getContext()->diagnose(NoGraphicsHSA); 2013 return DAG.getEntryNode(); 2014 } 2015 2016 SmallVector<ISD::InputArg, 16> Splits; 2017 SmallVector<CCValAssign, 16> ArgLocs; 2018 BitVector Skipped(Ins.size()); 2019 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 2020 *DAG.getContext()); 2021 2022 bool IsShader = AMDGPU::isShader(CallConv); 2023 bool IsKernel = AMDGPU::isKernel(CallConv); 2024 bool IsEntryFunc = AMDGPU::isEntryFunctionCC(CallConv); 2025 2026 if (IsShader) { 2027 processShaderInputArgs(Splits, CallConv, Ins, Skipped, FType, Info); 2028 2029 // At least one interpolation mode must be enabled or else the GPU will 2030 // hang. 2031 // 2032 // Check PSInputAddr instead of PSInputEnable. The idea is that if the user 2033 // set PSInputAddr, the user wants to enable some bits after the compilation 2034 // based on run-time states. Since we can't know what the final PSInputEna 2035 // will look like, so we shouldn't do anything here and the user should take 2036 // responsibility for the correct programming. 2037 // 2038 // Otherwise, the following restrictions apply: 2039 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled. 2040 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be 2041 // enabled too. 2042 if (CallConv == CallingConv::AMDGPU_PS) { 2043 if ((Info->getPSInputAddr() & 0x7F) == 0 || 2044 ((Info->getPSInputAddr() & 0xF) == 0 && 2045 Info->isPSInputAllocated(11))) { 2046 CCInfo.AllocateReg(AMDGPU::VGPR0); 2047 CCInfo.AllocateReg(AMDGPU::VGPR1); 2048 Info->markPSInputAllocated(0); 2049 Info->markPSInputEnabled(0); 2050 } 2051 if (Subtarget->isAmdPalOS()) { 2052 // For isAmdPalOS, the user does not enable some bits after compilation 2053 // based on run-time states; the register values being generated here are 2054 // the final ones set in hardware. Therefore we need to apply the 2055 // workaround to PSInputAddr and PSInputEnable together. (The case where 2056 // a bit is set in PSInputAddr but not PSInputEnable is where the 2057 // frontend set up an input arg for a particular interpolation mode, but 2058 // nothing uses that input arg. Really we should have an earlier pass 2059 // that removes such an arg.) 2060 unsigned PsInputBits = Info->getPSInputAddr() & Info->getPSInputEnable(); 2061 if ((PsInputBits & 0x7F) == 0 || 2062 ((PsInputBits & 0xF) == 0 && 2063 (PsInputBits >> 11 & 1))) 2064 Info->markPSInputEnabled( 2065 countTrailingZeros(Info->getPSInputAddr(), ZB_Undefined)); 2066 } 2067 } 2068 2069 assert(!Info->hasDispatchPtr() && 2070 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() && 2071 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() && 2072 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() && 2073 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() && 2074 !Info->hasWorkItemIDZ()); 2075 } else if (IsKernel) { 2076 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX()); 2077 } else { 2078 Splits.append(Ins.begin(), Ins.end()); 2079 } 2080 2081 if (IsEntryFunc) { 2082 allocateSpecialEntryInputVGPRs(CCInfo, MF, *TRI, *Info); 2083 allocateHSAUserSGPRs(CCInfo, MF, *TRI, *Info); 2084 } 2085 2086 if (IsKernel) { 2087 analyzeFormalArgumentsCompute(CCInfo, Ins); 2088 } else { 2089 CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, isVarArg); 2090 CCInfo.AnalyzeFormalArguments(Splits, AssignFn); 2091 } 2092 2093 SmallVector<SDValue, 16> Chains; 2094 2095 // FIXME: This is the minimum kernel argument alignment. We should improve 2096 // this to the maximum alignment of the arguments. 2097 // 2098 // FIXME: Alignment of explicit arguments totally broken with non-0 explicit 2099 // kern arg offset. 2100 const unsigned KernelArgBaseAlign = 16; 2101 2102 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) { 2103 const ISD::InputArg &Arg = Ins[i]; 2104 if (Arg.isOrigArg() && Skipped[Arg.getOrigArgIndex()]) { 2105 InVals.push_back(DAG.getUNDEF(Arg.VT)); 2106 continue; 2107 } 2108 2109 CCValAssign &VA = ArgLocs[ArgIdx++]; 2110 MVT VT = VA.getLocVT(); 2111 2112 if (IsEntryFunc && VA.isMemLoc()) { 2113 VT = Ins[i].VT; 2114 EVT MemVT = VA.getLocVT(); 2115 2116 const uint64_t Offset = VA.getLocMemOffset(); 2117 unsigned Align = MinAlign(KernelArgBaseAlign, Offset); 2118 2119 SDValue Arg = lowerKernargMemParameter( 2120 DAG, VT, MemVT, DL, Chain, Offset, Align, Ins[i].Flags.isSExt(), &Ins[i]); 2121 Chains.push_back(Arg.getValue(1)); 2122 2123 auto *ParamTy = 2124 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex())); 2125 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS && 2126 ParamTy && (ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS || 2127 ParamTy->getAddressSpace() == AMDGPUAS::REGION_ADDRESS)) { 2128 // On SI local pointers are just offsets into LDS, so they are always 2129 // less than 16-bits. On CI and newer they could potentially be 2130 // real pointers, so we can't guarantee their size. 2131 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg, 2132 DAG.getValueType(MVT::i16)); 2133 } 2134 2135 InVals.push_back(Arg); 2136 continue; 2137 } else if (!IsEntryFunc && VA.isMemLoc()) { 2138 SDValue Val = lowerStackParameter(DAG, VA, DL, Chain, Arg); 2139 InVals.push_back(Val); 2140 if (!Arg.Flags.isByVal()) 2141 Chains.push_back(Val.getValue(1)); 2142 continue; 2143 } 2144 2145 assert(VA.isRegLoc() && "Parameter must be in a register!"); 2146 2147 Register Reg = VA.getLocReg(); 2148 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT); 2149 EVT ValVT = VA.getValVT(); 2150 2151 Reg = MF.addLiveIn(Reg, RC); 2152 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT); 2153 2154 if (Arg.Flags.isSRet()) { 2155 // The return object should be reasonably addressable. 2156 2157 // FIXME: This helps when the return is a real sret. If it is a 2158 // automatically inserted sret (i.e. CanLowerReturn returns false), an 2159 // extra copy is inserted in SelectionDAGBuilder which obscures this. 2160 unsigned NumBits 2161 = 32 - getSubtarget()->getKnownHighZeroBitsForFrameIndex(); 2162 Val = DAG.getNode(ISD::AssertZext, DL, VT, Val, 2163 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), NumBits))); 2164 } 2165 2166 // If this is an 8 or 16-bit value, it is really passed promoted 2167 // to 32 bits. Insert an assert[sz]ext to capture this, then 2168 // truncate to the right size. 2169 switch (VA.getLocInfo()) { 2170 case CCValAssign::Full: 2171 break; 2172 case CCValAssign::BCvt: 2173 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val); 2174 break; 2175 case CCValAssign::SExt: 2176 Val = DAG.getNode(ISD::AssertSext, DL, VT, Val, 2177 DAG.getValueType(ValVT)); 2178 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val); 2179 break; 2180 case CCValAssign::ZExt: 2181 Val = DAG.getNode(ISD::AssertZext, DL, VT, Val, 2182 DAG.getValueType(ValVT)); 2183 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val); 2184 break; 2185 case CCValAssign::AExt: 2186 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val); 2187 break; 2188 default: 2189 llvm_unreachable("Unknown loc info!"); 2190 } 2191 2192 InVals.push_back(Val); 2193 } 2194 2195 if (!IsEntryFunc) { 2196 // Special inputs come after user arguments. 2197 allocateSpecialInputVGPRs(CCInfo, MF, *TRI, *Info); 2198 } 2199 2200 // Start adding system SGPRs. 2201 if (IsEntryFunc) { 2202 allocateSystemSGPRs(CCInfo, MF, *Info, CallConv, IsShader); 2203 } else { 2204 CCInfo.AllocateReg(Info->getScratchRSrcReg()); 2205 CCInfo.AllocateReg(Info->getScratchWaveOffsetReg()); 2206 CCInfo.AllocateReg(Info->getFrameOffsetReg()); 2207 allocateSpecialInputSGPRs(CCInfo, MF, *TRI, *Info); 2208 } 2209 2210 auto &ArgUsageInfo = 2211 DAG.getPass()->getAnalysis<AMDGPUArgumentUsageInfo>(); 2212 ArgUsageInfo.setFuncArgInfo(Fn, Info->getArgInfo()); 2213 2214 unsigned StackArgSize = CCInfo.getNextStackOffset(); 2215 Info->setBytesInStackArgArea(StackArgSize); 2216 2217 return Chains.empty() ? Chain : 2218 DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 2219 } 2220 2221 // TODO: If return values can't fit in registers, we should return as many as 2222 // possible in registers before passing on stack. 2223 bool SITargetLowering::CanLowerReturn( 2224 CallingConv::ID CallConv, 2225 MachineFunction &MF, bool IsVarArg, 2226 const SmallVectorImpl<ISD::OutputArg> &Outs, 2227 LLVMContext &Context) const { 2228 // Replacing returns with sret/stack usage doesn't make sense for shaders. 2229 // FIXME: Also sort of a workaround for custom vector splitting in LowerReturn 2230 // for shaders. Vector types should be explicitly handled by CC. 2231 if (AMDGPU::isEntryFunctionCC(CallConv)) 2232 return true; 2233 2234 SmallVector<CCValAssign, 16> RVLocs; 2235 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context); 2236 return CCInfo.CheckReturn(Outs, CCAssignFnForReturn(CallConv, IsVarArg)); 2237 } 2238 2239 SDValue 2240 SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 2241 bool isVarArg, 2242 const SmallVectorImpl<ISD::OutputArg> &Outs, 2243 const SmallVectorImpl<SDValue> &OutVals, 2244 const SDLoc &DL, SelectionDAG &DAG) const { 2245 MachineFunction &MF = DAG.getMachineFunction(); 2246 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 2247 2248 if (AMDGPU::isKernel(CallConv)) { 2249 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs, 2250 OutVals, DL, DAG); 2251 } 2252 2253 bool IsShader = AMDGPU::isShader(CallConv); 2254 2255 Info->setIfReturnsVoid(Outs.empty()); 2256 bool IsWaveEnd = Info->returnsVoid() && IsShader; 2257 2258 // CCValAssign - represent the assignment of the return value to a location. 2259 SmallVector<CCValAssign, 48> RVLocs; 2260 SmallVector<ISD::OutputArg, 48> Splits; 2261 2262 // CCState - Info about the registers and stack slots. 2263 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 2264 *DAG.getContext()); 2265 2266 // Analyze outgoing return values. 2267 CCInfo.AnalyzeReturn(Outs, CCAssignFnForReturn(CallConv, isVarArg)); 2268 2269 SDValue Flag; 2270 SmallVector<SDValue, 48> RetOps; 2271 RetOps.push_back(Chain); // Operand #0 = Chain (updated below) 2272 2273 // Add return address for callable functions. 2274 if (!Info->isEntryFunction()) { 2275 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 2276 SDValue ReturnAddrReg = CreateLiveInRegister( 2277 DAG, &AMDGPU::SReg_64RegClass, TRI->getReturnAddressReg(MF), MVT::i64); 2278 2279 SDValue ReturnAddrVirtualReg = DAG.getRegister( 2280 MF.getRegInfo().createVirtualRegister(&AMDGPU::CCR_SGPR_64RegClass), 2281 MVT::i64); 2282 Chain = 2283 DAG.getCopyToReg(Chain, DL, ReturnAddrVirtualReg, ReturnAddrReg, Flag); 2284 Flag = Chain.getValue(1); 2285 RetOps.push_back(ReturnAddrVirtualReg); 2286 } 2287 2288 // Copy the result values into the output registers. 2289 for (unsigned I = 0, RealRVLocIdx = 0, E = RVLocs.size(); I != E; 2290 ++I, ++RealRVLocIdx) { 2291 CCValAssign &VA = RVLocs[I]; 2292 assert(VA.isRegLoc() && "Can only return in registers!"); 2293 // TODO: Partially return in registers if return values don't fit. 2294 SDValue Arg = OutVals[RealRVLocIdx]; 2295 2296 // Copied from other backends. 2297 switch (VA.getLocInfo()) { 2298 case CCValAssign::Full: 2299 break; 2300 case CCValAssign::BCvt: 2301 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg); 2302 break; 2303 case CCValAssign::SExt: 2304 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg); 2305 break; 2306 case CCValAssign::ZExt: 2307 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg); 2308 break; 2309 case CCValAssign::AExt: 2310 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg); 2311 break; 2312 default: 2313 llvm_unreachable("Unknown loc info!"); 2314 } 2315 2316 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag); 2317 Flag = Chain.getValue(1); 2318 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2319 } 2320 2321 // FIXME: Does sret work properly? 2322 if (!Info->isEntryFunction()) { 2323 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo(); 2324 const MCPhysReg *I = 2325 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction()); 2326 if (I) { 2327 for (; *I; ++I) { 2328 if (AMDGPU::SReg_64RegClass.contains(*I)) 2329 RetOps.push_back(DAG.getRegister(*I, MVT::i64)); 2330 else if (AMDGPU::SReg_32RegClass.contains(*I)) 2331 RetOps.push_back(DAG.getRegister(*I, MVT::i32)); 2332 else 2333 llvm_unreachable("Unexpected register class in CSRsViaCopy!"); 2334 } 2335 } 2336 } 2337 2338 // Update chain and glue. 2339 RetOps[0] = Chain; 2340 if (Flag.getNode()) 2341 RetOps.push_back(Flag); 2342 2343 unsigned Opc = AMDGPUISD::ENDPGM; 2344 if (!IsWaveEnd) 2345 Opc = IsShader ? AMDGPUISD::RETURN_TO_EPILOG : AMDGPUISD::RET_FLAG; 2346 return DAG.getNode(Opc, DL, MVT::Other, RetOps); 2347 } 2348 2349 SDValue SITargetLowering::LowerCallResult( 2350 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg, 2351 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, 2352 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, bool IsThisReturn, 2353 SDValue ThisVal) const { 2354 CCAssignFn *RetCC = CCAssignFnForReturn(CallConv, IsVarArg); 2355 2356 // Assign locations to each value returned by this call. 2357 SmallVector<CCValAssign, 16> RVLocs; 2358 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs, 2359 *DAG.getContext()); 2360 CCInfo.AnalyzeCallResult(Ins, RetCC); 2361 2362 // Copy all of the result registers out of their specified physreg. 2363 for (unsigned i = 0; i != RVLocs.size(); ++i) { 2364 CCValAssign VA = RVLocs[i]; 2365 SDValue Val; 2366 2367 if (VA.isRegLoc()) { 2368 Val = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), InFlag); 2369 Chain = Val.getValue(1); 2370 InFlag = Val.getValue(2); 2371 } else if (VA.isMemLoc()) { 2372 report_fatal_error("TODO: return values in memory"); 2373 } else 2374 llvm_unreachable("unknown argument location type"); 2375 2376 switch (VA.getLocInfo()) { 2377 case CCValAssign::Full: 2378 break; 2379 case CCValAssign::BCvt: 2380 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val); 2381 break; 2382 case CCValAssign::ZExt: 2383 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val, 2384 DAG.getValueType(VA.getValVT())); 2385 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val); 2386 break; 2387 case CCValAssign::SExt: 2388 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val, 2389 DAG.getValueType(VA.getValVT())); 2390 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val); 2391 break; 2392 case CCValAssign::AExt: 2393 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val); 2394 break; 2395 default: 2396 llvm_unreachable("Unknown loc info!"); 2397 } 2398 2399 InVals.push_back(Val); 2400 } 2401 2402 return Chain; 2403 } 2404 2405 // Add code to pass special inputs required depending on used features separate 2406 // from the explicit user arguments present in the IR. 2407 void SITargetLowering::passSpecialInputs( 2408 CallLoweringInfo &CLI, 2409 CCState &CCInfo, 2410 const SIMachineFunctionInfo &Info, 2411 SmallVectorImpl<std::pair<unsigned, SDValue>> &RegsToPass, 2412 SmallVectorImpl<SDValue> &MemOpChains, 2413 SDValue Chain) const { 2414 // If we don't have a call site, this was a call inserted by 2415 // legalization. These can never use special inputs. 2416 if (!CLI.CS) 2417 return; 2418 2419 const Function *CalleeFunc = CLI.CS.getCalledFunction(); 2420 assert(CalleeFunc); 2421 2422 SelectionDAG &DAG = CLI.DAG; 2423 const SDLoc &DL = CLI.DL; 2424 2425 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo(); 2426 2427 auto &ArgUsageInfo = 2428 DAG.getPass()->getAnalysis<AMDGPUArgumentUsageInfo>(); 2429 const AMDGPUFunctionArgInfo &CalleeArgInfo 2430 = ArgUsageInfo.lookupFuncArgInfo(*CalleeFunc); 2431 2432 const AMDGPUFunctionArgInfo &CallerArgInfo = Info.getArgInfo(); 2433 2434 // TODO: Unify with private memory register handling. This is complicated by 2435 // the fact that at least in kernels, the input argument is not necessarily 2436 // in the same location as the input. 2437 AMDGPUFunctionArgInfo::PreloadedValue InputRegs[] = { 2438 AMDGPUFunctionArgInfo::DISPATCH_PTR, 2439 AMDGPUFunctionArgInfo::QUEUE_PTR, 2440 AMDGPUFunctionArgInfo::KERNARG_SEGMENT_PTR, 2441 AMDGPUFunctionArgInfo::DISPATCH_ID, 2442 AMDGPUFunctionArgInfo::WORKGROUP_ID_X, 2443 AMDGPUFunctionArgInfo::WORKGROUP_ID_Y, 2444 AMDGPUFunctionArgInfo::WORKGROUP_ID_Z, 2445 AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR 2446 }; 2447 2448 for (auto InputID : InputRegs) { 2449 const ArgDescriptor *OutgoingArg; 2450 const TargetRegisterClass *ArgRC; 2451 2452 std::tie(OutgoingArg, ArgRC) = CalleeArgInfo.getPreloadedValue(InputID); 2453 if (!OutgoingArg) 2454 continue; 2455 2456 const ArgDescriptor *IncomingArg; 2457 const TargetRegisterClass *IncomingArgRC; 2458 std::tie(IncomingArg, IncomingArgRC) 2459 = CallerArgInfo.getPreloadedValue(InputID); 2460 assert(IncomingArgRC == ArgRC); 2461 2462 // All special arguments are ints for now. 2463 EVT ArgVT = TRI->getSpillSize(*ArgRC) == 8 ? MVT::i64 : MVT::i32; 2464 SDValue InputReg; 2465 2466 if (IncomingArg) { 2467 InputReg = loadInputValue(DAG, ArgRC, ArgVT, DL, *IncomingArg); 2468 } else { 2469 // The implicit arg ptr is special because it doesn't have a corresponding 2470 // input for kernels, and is computed from the kernarg segment pointer. 2471 assert(InputID == AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR); 2472 InputReg = getImplicitArgPtr(DAG, DL); 2473 } 2474 2475 if (OutgoingArg->isRegister()) { 2476 RegsToPass.emplace_back(OutgoingArg->getRegister(), InputReg); 2477 } else { 2478 unsigned SpecialArgOffset = CCInfo.AllocateStack(ArgVT.getStoreSize(), 4); 2479 SDValue ArgStore = storeStackInputValue(DAG, DL, Chain, InputReg, 2480 SpecialArgOffset); 2481 MemOpChains.push_back(ArgStore); 2482 } 2483 } 2484 2485 // Pack workitem IDs into a single register or pass it as is if already 2486 // packed. 2487 const ArgDescriptor *OutgoingArg; 2488 const TargetRegisterClass *ArgRC; 2489 2490 std::tie(OutgoingArg, ArgRC) = 2491 CalleeArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_X); 2492 if (!OutgoingArg) 2493 std::tie(OutgoingArg, ArgRC) = 2494 CalleeArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Y); 2495 if (!OutgoingArg) 2496 std::tie(OutgoingArg, ArgRC) = 2497 CalleeArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Z); 2498 if (!OutgoingArg) 2499 return; 2500 2501 const ArgDescriptor *IncomingArgX 2502 = CallerArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_X).first; 2503 const ArgDescriptor *IncomingArgY 2504 = CallerArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Y).first; 2505 const ArgDescriptor *IncomingArgZ 2506 = CallerArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Z).first; 2507 2508 SDValue InputReg; 2509 SDLoc SL; 2510 2511 // If incoming ids are not packed we need to pack them. 2512 if (IncomingArgX && !IncomingArgX->isMasked() && CalleeArgInfo.WorkItemIDX) 2513 InputReg = loadInputValue(DAG, ArgRC, MVT::i32, DL, *IncomingArgX); 2514 2515 if (IncomingArgY && !IncomingArgY->isMasked() && CalleeArgInfo.WorkItemIDY) { 2516 SDValue Y = loadInputValue(DAG, ArgRC, MVT::i32, DL, *IncomingArgY); 2517 Y = DAG.getNode(ISD::SHL, SL, MVT::i32, Y, 2518 DAG.getShiftAmountConstant(10, MVT::i32, SL)); 2519 InputReg = InputReg.getNode() ? 2520 DAG.getNode(ISD::OR, SL, MVT::i32, InputReg, Y) : Y; 2521 } 2522 2523 if (IncomingArgZ && !IncomingArgZ->isMasked() && CalleeArgInfo.WorkItemIDZ) { 2524 SDValue Z = loadInputValue(DAG, ArgRC, MVT::i32, DL, *IncomingArgZ); 2525 Z = DAG.getNode(ISD::SHL, SL, MVT::i32, Z, 2526 DAG.getShiftAmountConstant(20, MVT::i32, SL)); 2527 InputReg = InputReg.getNode() ? 2528 DAG.getNode(ISD::OR, SL, MVT::i32, InputReg, Z) : Z; 2529 } 2530 2531 if (!InputReg.getNode()) { 2532 // Workitem ids are already packed, any of present incoming arguments 2533 // will carry all required fields. 2534 ArgDescriptor IncomingArg = ArgDescriptor::createArg( 2535 IncomingArgX ? *IncomingArgX : 2536 IncomingArgY ? *IncomingArgY : 2537 *IncomingArgZ, ~0u); 2538 InputReg = loadInputValue(DAG, ArgRC, MVT::i32, DL, IncomingArg); 2539 } 2540 2541 if (OutgoingArg->isRegister()) { 2542 RegsToPass.emplace_back(OutgoingArg->getRegister(), InputReg); 2543 } else { 2544 unsigned SpecialArgOffset = CCInfo.AllocateStack(4, 4); 2545 SDValue ArgStore = storeStackInputValue(DAG, DL, Chain, InputReg, 2546 SpecialArgOffset); 2547 MemOpChains.push_back(ArgStore); 2548 } 2549 } 2550 2551 static bool canGuaranteeTCO(CallingConv::ID CC) { 2552 return CC == CallingConv::Fast; 2553 } 2554 2555 /// Return true if we might ever do TCO for calls with this calling convention. 2556 static bool mayTailCallThisCC(CallingConv::ID CC) { 2557 switch (CC) { 2558 case CallingConv::C: 2559 return true; 2560 default: 2561 return canGuaranteeTCO(CC); 2562 } 2563 } 2564 2565 bool SITargetLowering::isEligibleForTailCallOptimization( 2566 SDValue Callee, CallingConv::ID CalleeCC, bool IsVarArg, 2567 const SmallVectorImpl<ISD::OutputArg> &Outs, 2568 const SmallVectorImpl<SDValue> &OutVals, 2569 const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const { 2570 if (!mayTailCallThisCC(CalleeCC)) 2571 return false; 2572 2573 MachineFunction &MF = DAG.getMachineFunction(); 2574 const Function &CallerF = MF.getFunction(); 2575 CallingConv::ID CallerCC = CallerF.getCallingConv(); 2576 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 2577 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC); 2578 2579 // Kernels aren't callable, and don't have a live in return address so it 2580 // doesn't make sense to do a tail call with entry functions. 2581 if (!CallerPreserved) 2582 return false; 2583 2584 bool CCMatch = CallerCC == CalleeCC; 2585 2586 if (DAG.getTarget().Options.GuaranteedTailCallOpt) { 2587 if (canGuaranteeTCO(CalleeCC) && CCMatch) 2588 return true; 2589 return false; 2590 } 2591 2592 // TODO: Can we handle var args? 2593 if (IsVarArg) 2594 return false; 2595 2596 for (const Argument &Arg : CallerF.args()) { 2597 if (Arg.hasByValAttr()) 2598 return false; 2599 } 2600 2601 LLVMContext &Ctx = *DAG.getContext(); 2602 2603 // Check that the call results are passed in the same way. 2604 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, Ctx, Ins, 2605 CCAssignFnForCall(CalleeCC, IsVarArg), 2606 CCAssignFnForCall(CallerCC, IsVarArg))) 2607 return false; 2608 2609 // The callee has to preserve all registers the caller needs to preserve. 2610 if (!CCMatch) { 2611 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC); 2612 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved)) 2613 return false; 2614 } 2615 2616 // Nothing more to check if the callee is taking no arguments. 2617 if (Outs.empty()) 2618 return true; 2619 2620 SmallVector<CCValAssign, 16> ArgLocs; 2621 CCState CCInfo(CalleeCC, IsVarArg, MF, ArgLocs, Ctx); 2622 2623 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, IsVarArg)); 2624 2625 const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>(); 2626 // If the stack arguments for this call do not fit into our own save area then 2627 // the call cannot be made tail. 2628 // TODO: Is this really necessary? 2629 if (CCInfo.getNextStackOffset() > FuncInfo->getBytesInStackArgArea()) 2630 return false; 2631 2632 const MachineRegisterInfo &MRI = MF.getRegInfo(); 2633 return parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals); 2634 } 2635 2636 bool SITargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const { 2637 if (!CI->isTailCall()) 2638 return false; 2639 2640 const Function *ParentFn = CI->getParent()->getParent(); 2641 if (AMDGPU::isEntryFunctionCC(ParentFn->getCallingConv())) 2642 return false; 2643 return true; 2644 } 2645 2646 // The wave scratch offset register is used as the global base pointer. 2647 SDValue SITargetLowering::LowerCall(CallLoweringInfo &CLI, 2648 SmallVectorImpl<SDValue> &InVals) const { 2649 SelectionDAG &DAG = CLI.DAG; 2650 const SDLoc &DL = CLI.DL; 2651 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs; 2652 SmallVector<SDValue, 32> &OutVals = CLI.OutVals; 2653 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins; 2654 SDValue Chain = CLI.Chain; 2655 SDValue Callee = CLI.Callee; 2656 bool &IsTailCall = CLI.IsTailCall; 2657 CallingConv::ID CallConv = CLI.CallConv; 2658 bool IsVarArg = CLI.IsVarArg; 2659 bool IsSibCall = false; 2660 bool IsThisReturn = false; 2661 MachineFunction &MF = DAG.getMachineFunction(); 2662 2663 if (Callee.isUndef() || isNullConstant(Callee)) { 2664 if (!CLI.IsTailCall) { 2665 for (unsigned I = 0, E = CLI.Ins.size(); I != E; ++I) 2666 InVals.push_back(DAG.getUNDEF(CLI.Ins[I].VT)); 2667 } 2668 2669 return Chain; 2670 } 2671 2672 if (IsVarArg) { 2673 return lowerUnhandledCall(CLI, InVals, 2674 "unsupported call to variadic function "); 2675 } 2676 2677 if (!CLI.CS.getInstruction()) 2678 report_fatal_error("unsupported libcall legalization"); 2679 2680 if (!CLI.CS.getCalledFunction()) { 2681 return lowerUnhandledCall(CLI, InVals, 2682 "unsupported indirect call to function "); 2683 } 2684 2685 if (IsTailCall && MF.getTarget().Options.GuaranteedTailCallOpt) { 2686 return lowerUnhandledCall(CLI, InVals, 2687 "unsupported required tail call to function "); 2688 } 2689 2690 if (AMDGPU::isShader(MF.getFunction().getCallingConv())) { 2691 // Note the issue is with the CC of the calling function, not of the call 2692 // itself. 2693 return lowerUnhandledCall(CLI, InVals, 2694 "unsupported call from graphics shader of function "); 2695 } 2696 2697 if (IsTailCall) { 2698 IsTailCall = isEligibleForTailCallOptimization( 2699 Callee, CallConv, IsVarArg, Outs, OutVals, Ins, DAG); 2700 if (!IsTailCall && CLI.CS && CLI.CS.isMustTailCall()) { 2701 report_fatal_error("failed to perform tail call elimination on a call " 2702 "site marked musttail"); 2703 } 2704 2705 bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt; 2706 2707 // A sibling call is one where we're under the usual C ABI and not planning 2708 // to change that but can still do a tail call: 2709 if (!TailCallOpt && IsTailCall) 2710 IsSibCall = true; 2711 2712 if (IsTailCall) 2713 ++NumTailCalls; 2714 } 2715 2716 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 2717 2718 // Analyze operands of the call, assigning locations to each operand. 2719 SmallVector<CCValAssign, 16> ArgLocs; 2720 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); 2721 CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, IsVarArg); 2722 2723 CCInfo.AnalyzeCallOperands(Outs, AssignFn); 2724 2725 // Get a count of how many bytes are to be pushed on the stack. 2726 unsigned NumBytes = CCInfo.getNextStackOffset(); 2727 2728 if (IsSibCall) { 2729 // Since we're not changing the ABI to make this a tail call, the memory 2730 // operands are already available in the caller's incoming argument space. 2731 NumBytes = 0; 2732 } 2733 2734 // FPDiff is the byte offset of the call's argument area from the callee's. 2735 // Stores to callee stack arguments will be placed in FixedStackSlots offset 2736 // by this amount for a tail call. In a sibling call it must be 0 because the 2737 // caller will deallocate the entire stack and the callee still expects its 2738 // arguments to begin at SP+0. Completely unused for non-tail calls. 2739 int32_t FPDiff = 0; 2740 MachineFrameInfo &MFI = MF.getFrameInfo(); 2741 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; 2742 2743 // Adjust the stack pointer for the new arguments... 2744 // These operations are automatically eliminated by the prolog/epilog pass 2745 if (!IsSibCall) { 2746 Chain = DAG.getCALLSEQ_START(Chain, 0, 0, DL); 2747 2748 SmallVector<SDValue, 4> CopyFromChains; 2749 2750 // In the HSA case, this should be an identity copy. 2751 SDValue ScratchRSrcReg 2752 = DAG.getCopyFromReg(Chain, DL, Info->getScratchRSrcReg(), MVT::v4i32); 2753 RegsToPass.emplace_back(AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3, ScratchRSrcReg); 2754 CopyFromChains.push_back(ScratchRSrcReg.getValue(1)); 2755 Chain = DAG.getTokenFactor(DL, CopyFromChains); 2756 } 2757 2758 SmallVector<SDValue, 8> MemOpChains; 2759 MVT PtrVT = MVT::i32; 2760 2761 // Walk the register/memloc assignments, inserting copies/loads. 2762 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 2763 CCValAssign &VA = ArgLocs[i]; 2764 SDValue Arg = OutVals[i]; 2765 2766 // Promote the value if needed. 2767 switch (VA.getLocInfo()) { 2768 case CCValAssign::Full: 2769 break; 2770 case CCValAssign::BCvt: 2771 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg); 2772 break; 2773 case CCValAssign::ZExt: 2774 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg); 2775 break; 2776 case CCValAssign::SExt: 2777 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg); 2778 break; 2779 case CCValAssign::AExt: 2780 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg); 2781 break; 2782 case CCValAssign::FPExt: 2783 Arg = DAG.getNode(ISD::FP_EXTEND, DL, VA.getLocVT(), Arg); 2784 break; 2785 default: 2786 llvm_unreachable("Unknown loc info!"); 2787 } 2788 2789 if (VA.isRegLoc()) { 2790 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 2791 } else { 2792 assert(VA.isMemLoc()); 2793 2794 SDValue DstAddr; 2795 MachinePointerInfo DstInfo; 2796 2797 unsigned LocMemOffset = VA.getLocMemOffset(); 2798 int32_t Offset = LocMemOffset; 2799 2800 SDValue PtrOff = DAG.getConstant(Offset, DL, PtrVT); 2801 MaybeAlign Alignment; 2802 2803 if (IsTailCall) { 2804 ISD::ArgFlagsTy Flags = Outs[i].Flags; 2805 unsigned OpSize = Flags.isByVal() ? 2806 Flags.getByValSize() : VA.getValVT().getStoreSize(); 2807 2808 // FIXME: We can have better than the minimum byval required alignment. 2809 Alignment = 2810 Flags.isByVal() 2811 ? Flags.getNonZeroByValAlign() 2812 : commonAlignment(Subtarget->getStackAlignment(), Offset); 2813 2814 Offset = Offset + FPDiff; 2815 int FI = MFI.CreateFixedObject(OpSize, Offset, true); 2816 2817 DstAddr = DAG.getFrameIndex(FI, PtrVT); 2818 DstInfo = MachinePointerInfo::getFixedStack(MF, FI); 2819 2820 // Make sure any stack arguments overlapping with where we're storing 2821 // are loaded before this eventual operation. Otherwise they'll be 2822 // clobbered. 2823 2824 // FIXME: Why is this really necessary? This seems to just result in a 2825 // lot of code to copy the stack and write them back to the same 2826 // locations, which are supposed to be immutable? 2827 Chain = addTokenForArgument(Chain, DAG, MFI, FI); 2828 } else { 2829 DstAddr = PtrOff; 2830 DstInfo = MachinePointerInfo::getStack(MF, LocMemOffset); 2831 Alignment = 2832 commonAlignment(Subtarget->getStackAlignment(), LocMemOffset); 2833 } 2834 2835 if (Outs[i].Flags.isByVal()) { 2836 SDValue SizeNode = 2837 DAG.getConstant(Outs[i].Flags.getByValSize(), DL, MVT::i32); 2838 SDValue Cpy = 2839 DAG.getMemcpy(Chain, DL, DstAddr, Arg, SizeNode, 2840 Outs[i].Flags.getNonZeroByValAlign(), 2841 /*isVol = */ false, /*AlwaysInline = */ true, 2842 /*isTailCall = */ false, DstInfo, 2843 MachinePointerInfo(AMDGPUAS::PRIVATE_ADDRESS)); 2844 2845 MemOpChains.push_back(Cpy); 2846 } else { 2847 SDValue Store = DAG.getStore(Chain, DL, Arg, DstAddr, DstInfo, 2848 Alignment ? Alignment->value() : 0); 2849 MemOpChains.push_back(Store); 2850 } 2851 } 2852 } 2853 2854 // Copy special input registers after user input arguments. 2855 passSpecialInputs(CLI, CCInfo, *Info, RegsToPass, MemOpChains, Chain); 2856 2857 if (!MemOpChains.empty()) 2858 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains); 2859 2860 // Build a sequence of copy-to-reg nodes chained together with token chain 2861 // and flag operands which copy the outgoing args into the appropriate regs. 2862 SDValue InFlag; 2863 for (auto &RegToPass : RegsToPass) { 2864 Chain = DAG.getCopyToReg(Chain, DL, RegToPass.first, 2865 RegToPass.second, InFlag); 2866 InFlag = Chain.getValue(1); 2867 } 2868 2869 2870 SDValue PhysReturnAddrReg; 2871 if (IsTailCall) { 2872 // Since the return is being combined with the call, we need to pass on the 2873 // return address. 2874 2875 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 2876 SDValue ReturnAddrReg = CreateLiveInRegister( 2877 DAG, &AMDGPU::SReg_64RegClass, TRI->getReturnAddressReg(MF), MVT::i64); 2878 2879 PhysReturnAddrReg = DAG.getRegister(TRI->getReturnAddressReg(MF), 2880 MVT::i64); 2881 Chain = DAG.getCopyToReg(Chain, DL, PhysReturnAddrReg, ReturnAddrReg, InFlag); 2882 InFlag = Chain.getValue(1); 2883 } 2884 2885 // We don't usually want to end the call-sequence here because we would tidy 2886 // the frame up *after* the call, however in the ABI-changing tail-call case 2887 // we've carefully laid out the parameters so that when sp is reset they'll be 2888 // in the correct location. 2889 if (IsTailCall && !IsSibCall) { 2890 Chain = DAG.getCALLSEQ_END(Chain, 2891 DAG.getTargetConstant(NumBytes, DL, MVT::i32), 2892 DAG.getTargetConstant(0, DL, MVT::i32), 2893 InFlag, DL); 2894 InFlag = Chain.getValue(1); 2895 } 2896 2897 std::vector<SDValue> Ops; 2898 Ops.push_back(Chain); 2899 Ops.push_back(Callee); 2900 // Add a redundant copy of the callee global which will not be legalized, as 2901 // we need direct access to the callee later. 2902 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Callee); 2903 const GlobalValue *GV = GSD->getGlobal(); 2904 Ops.push_back(DAG.getTargetGlobalAddress(GV, DL, MVT::i64)); 2905 2906 if (IsTailCall) { 2907 // Each tail call may have to adjust the stack by a different amount, so 2908 // this information must travel along with the operation for eventual 2909 // consumption by emitEpilogue. 2910 Ops.push_back(DAG.getTargetConstant(FPDiff, DL, MVT::i32)); 2911 2912 Ops.push_back(PhysReturnAddrReg); 2913 } 2914 2915 // Add argument registers to the end of the list so that they are known live 2916 // into the call. 2917 for (auto &RegToPass : RegsToPass) { 2918 Ops.push_back(DAG.getRegister(RegToPass.first, 2919 RegToPass.second.getValueType())); 2920 } 2921 2922 // Add a register mask operand representing the call-preserved registers. 2923 2924 auto *TRI = static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo()); 2925 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv); 2926 assert(Mask && "Missing call preserved mask for calling convention"); 2927 Ops.push_back(DAG.getRegisterMask(Mask)); 2928 2929 if (InFlag.getNode()) 2930 Ops.push_back(InFlag); 2931 2932 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 2933 2934 // If we're doing a tall call, use a TC_RETURN here rather than an 2935 // actual call instruction. 2936 if (IsTailCall) { 2937 MFI.setHasTailCall(); 2938 return DAG.getNode(AMDGPUISD::TC_RETURN, DL, NodeTys, Ops); 2939 } 2940 2941 // Returns a chain and a flag for retval copy to use. 2942 SDValue Call = DAG.getNode(AMDGPUISD::CALL, DL, NodeTys, Ops); 2943 Chain = Call.getValue(0); 2944 InFlag = Call.getValue(1); 2945 2946 uint64_t CalleePopBytes = NumBytes; 2947 Chain = DAG.getCALLSEQ_END(Chain, DAG.getTargetConstant(0, DL, MVT::i32), 2948 DAG.getTargetConstant(CalleePopBytes, DL, MVT::i32), 2949 InFlag, DL); 2950 if (!Ins.empty()) 2951 InFlag = Chain.getValue(1); 2952 2953 // Handle result values, copying them out of physregs into vregs that we 2954 // return. 2955 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG, 2956 InVals, IsThisReturn, 2957 IsThisReturn ? OutVals[0] : SDValue()); 2958 } 2959 2960 Register SITargetLowering::getRegisterByName(const char* RegName, LLT VT, 2961 const MachineFunction &MF) const { 2962 Register Reg = StringSwitch<Register>(RegName) 2963 .Case("m0", AMDGPU::M0) 2964 .Case("exec", AMDGPU::EXEC) 2965 .Case("exec_lo", AMDGPU::EXEC_LO) 2966 .Case("exec_hi", AMDGPU::EXEC_HI) 2967 .Case("flat_scratch", AMDGPU::FLAT_SCR) 2968 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO) 2969 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI) 2970 .Default(Register()); 2971 2972 if (Reg == AMDGPU::NoRegister) { 2973 report_fatal_error(Twine("invalid register name \"" 2974 + StringRef(RegName) + "\".")); 2975 2976 } 2977 2978 if (!Subtarget->hasFlatScrRegister() && 2979 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) { 2980 report_fatal_error(Twine("invalid register \"" 2981 + StringRef(RegName) + "\" for subtarget.")); 2982 } 2983 2984 switch (Reg) { 2985 case AMDGPU::M0: 2986 case AMDGPU::EXEC_LO: 2987 case AMDGPU::EXEC_HI: 2988 case AMDGPU::FLAT_SCR_LO: 2989 case AMDGPU::FLAT_SCR_HI: 2990 if (VT.getSizeInBits() == 32) 2991 return Reg; 2992 break; 2993 case AMDGPU::EXEC: 2994 case AMDGPU::FLAT_SCR: 2995 if (VT.getSizeInBits() == 64) 2996 return Reg; 2997 break; 2998 default: 2999 llvm_unreachable("missing register type checking"); 3000 } 3001 3002 report_fatal_error(Twine("invalid type for register \"" 3003 + StringRef(RegName) + "\".")); 3004 } 3005 3006 // If kill is not the last instruction, split the block so kill is always a 3007 // proper terminator. 3008 MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI, 3009 MachineBasicBlock *BB) const { 3010 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3011 3012 MachineBasicBlock::iterator SplitPoint(&MI); 3013 ++SplitPoint; 3014 3015 if (SplitPoint == BB->end()) { 3016 // Don't bother with a new block. 3017 MI.setDesc(TII->getKillTerminatorFromPseudo(MI.getOpcode())); 3018 return BB; 3019 } 3020 3021 MachineFunction *MF = BB->getParent(); 3022 MachineBasicBlock *SplitBB 3023 = MF->CreateMachineBasicBlock(BB->getBasicBlock()); 3024 3025 MF->insert(++MachineFunction::iterator(BB), SplitBB); 3026 SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end()); 3027 3028 SplitBB->transferSuccessorsAndUpdatePHIs(BB); 3029 BB->addSuccessor(SplitBB); 3030 3031 MI.setDesc(TII->getKillTerminatorFromPseudo(MI.getOpcode())); 3032 return SplitBB; 3033 } 3034 3035 // Split block \p MBB at \p MI, as to insert a loop. If \p InstInLoop is true, 3036 // \p MI will be the only instruction in the loop body block. Otherwise, it will 3037 // be the first instruction in the remainder block. 3038 // 3039 /// \returns { LoopBody, Remainder } 3040 static std::pair<MachineBasicBlock *, MachineBasicBlock *> 3041 splitBlockForLoop(MachineInstr &MI, MachineBasicBlock &MBB, bool InstInLoop) { 3042 MachineFunction *MF = MBB.getParent(); 3043 MachineBasicBlock::iterator I(&MI); 3044 3045 // To insert the loop we need to split the block. Move everything after this 3046 // point to a new block, and insert a new empty block between the two. 3047 MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock(); 3048 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock(); 3049 MachineFunction::iterator MBBI(MBB); 3050 ++MBBI; 3051 3052 MF->insert(MBBI, LoopBB); 3053 MF->insert(MBBI, RemainderBB); 3054 3055 LoopBB->addSuccessor(LoopBB); 3056 LoopBB->addSuccessor(RemainderBB); 3057 3058 // Move the rest of the block into a new block. 3059 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB); 3060 3061 if (InstInLoop) { 3062 auto Next = std::next(I); 3063 3064 // Move instruction to loop body. 3065 LoopBB->splice(LoopBB->begin(), &MBB, I, Next); 3066 3067 // Move the rest of the block. 3068 RemainderBB->splice(RemainderBB->begin(), &MBB, Next, MBB.end()); 3069 } else { 3070 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end()); 3071 } 3072 3073 MBB.addSuccessor(LoopBB); 3074 3075 return std::make_pair(LoopBB, RemainderBB); 3076 } 3077 3078 /// Insert \p MI into a BUNDLE with an S_WAITCNT 0 immediately following it. 3079 void SITargetLowering::bundleInstWithWaitcnt(MachineInstr &MI) const { 3080 MachineBasicBlock *MBB = MI.getParent(); 3081 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3082 auto I = MI.getIterator(); 3083 auto E = std::next(I); 3084 3085 BuildMI(*MBB, E, MI.getDebugLoc(), TII->get(AMDGPU::S_WAITCNT)) 3086 .addImm(0); 3087 3088 MIBundleBuilder Bundler(*MBB, I, E); 3089 finalizeBundle(*MBB, Bundler.begin()); 3090 } 3091 3092 MachineBasicBlock * 3093 SITargetLowering::emitGWSMemViolTestLoop(MachineInstr &MI, 3094 MachineBasicBlock *BB) const { 3095 const DebugLoc &DL = MI.getDebugLoc(); 3096 3097 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 3098 3099 MachineBasicBlock *LoopBB; 3100 MachineBasicBlock *RemainderBB; 3101 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3102 3103 // Apparently kill flags are only valid if the def is in the same block? 3104 if (MachineOperand *Src = TII->getNamedOperand(MI, AMDGPU::OpName::data0)) 3105 Src->setIsKill(false); 3106 3107 std::tie(LoopBB, RemainderBB) = splitBlockForLoop(MI, *BB, true); 3108 3109 MachineBasicBlock::iterator I = LoopBB->end(); 3110 3111 const unsigned EncodedReg = AMDGPU::Hwreg::encodeHwreg( 3112 AMDGPU::Hwreg::ID_TRAPSTS, AMDGPU::Hwreg::OFFSET_MEM_VIOL, 1); 3113 3114 // Clear TRAP_STS.MEM_VIOL 3115 BuildMI(*LoopBB, LoopBB->begin(), DL, TII->get(AMDGPU::S_SETREG_IMM32_B32)) 3116 .addImm(0) 3117 .addImm(EncodedReg); 3118 3119 bundleInstWithWaitcnt(MI); 3120 3121 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 3122 3123 // Load and check TRAP_STS.MEM_VIOL 3124 BuildMI(*LoopBB, I, DL, TII->get(AMDGPU::S_GETREG_B32), Reg) 3125 .addImm(EncodedReg); 3126 3127 // FIXME: Do we need to use an isel pseudo that may clobber scc? 3128 BuildMI(*LoopBB, I, DL, TII->get(AMDGPU::S_CMP_LG_U32)) 3129 .addReg(Reg, RegState::Kill) 3130 .addImm(0); 3131 BuildMI(*LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_SCC1)) 3132 .addMBB(LoopBB); 3133 3134 return RemainderBB; 3135 } 3136 3137 // Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the 3138 // wavefront. If the value is uniform and just happens to be in a VGPR, this 3139 // will only do one iteration. In the worst case, this will loop 64 times. 3140 // 3141 // TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value. 3142 static MachineBasicBlock::iterator emitLoadM0FromVGPRLoop( 3143 const SIInstrInfo *TII, 3144 MachineRegisterInfo &MRI, 3145 MachineBasicBlock &OrigBB, 3146 MachineBasicBlock &LoopBB, 3147 const DebugLoc &DL, 3148 const MachineOperand &IdxReg, 3149 unsigned InitReg, 3150 unsigned ResultReg, 3151 unsigned PhiReg, 3152 unsigned InitSaveExecReg, 3153 int Offset, 3154 bool UseGPRIdxMode, 3155 bool IsIndirectSrc) { 3156 MachineFunction *MF = OrigBB.getParent(); 3157 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>(); 3158 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 3159 MachineBasicBlock::iterator I = LoopBB.begin(); 3160 3161 const TargetRegisterClass *BoolRC = TRI->getBoolRC(); 3162 Register PhiExec = MRI.createVirtualRegister(BoolRC); 3163 Register NewExec = MRI.createVirtualRegister(BoolRC); 3164 Register CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 3165 Register CondReg = MRI.createVirtualRegister(BoolRC); 3166 3167 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg) 3168 .addReg(InitReg) 3169 .addMBB(&OrigBB) 3170 .addReg(ResultReg) 3171 .addMBB(&LoopBB); 3172 3173 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec) 3174 .addReg(InitSaveExecReg) 3175 .addMBB(&OrigBB) 3176 .addReg(NewExec) 3177 .addMBB(&LoopBB); 3178 3179 // Read the next variant <- also loop target. 3180 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg) 3181 .addReg(IdxReg.getReg(), getUndefRegState(IdxReg.isUndef())); 3182 3183 // Compare the just read M0 value to all possible Idx values. 3184 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg) 3185 .addReg(CurrentIdxReg) 3186 .addReg(IdxReg.getReg(), 0, IdxReg.getSubReg()); 3187 3188 // Update EXEC, save the original EXEC value to VCC. 3189 BuildMI(LoopBB, I, DL, TII->get(ST.isWave32() ? AMDGPU::S_AND_SAVEEXEC_B32 3190 : AMDGPU::S_AND_SAVEEXEC_B64), 3191 NewExec) 3192 .addReg(CondReg, RegState::Kill); 3193 3194 MRI.setSimpleHint(NewExec, CondReg); 3195 3196 if (UseGPRIdxMode) { 3197 unsigned IdxReg; 3198 if (Offset == 0) { 3199 IdxReg = CurrentIdxReg; 3200 } else { 3201 IdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 3202 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), IdxReg) 3203 .addReg(CurrentIdxReg, RegState::Kill) 3204 .addImm(Offset); 3205 } 3206 unsigned IdxMode = IsIndirectSrc ? 3207 AMDGPU::VGPRIndexMode::SRC0_ENABLE : AMDGPU::VGPRIndexMode::DST_ENABLE; 3208 MachineInstr *SetOn = 3209 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 3210 .addReg(IdxReg, RegState::Kill) 3211 .addImm(IdxMode); 3212 SetOn->getOperand(3).setIsUndef(); 3213 } else { 3214 // Move index from VCC into M0 3215 if (Offset == 0) { 3216 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) 3217 .addReg(CurrentIdxReg, RegState::Kill); 3218 } else { 3219 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0) 3220 .addReg(CurrentIdxReg, RegState::Kill) 3221 .addImm(Offset); 3222 } 3223 } 3224 3225 // Update EXEC, switch all done bits to 0 and all todo bits to 1. 3226 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 3227 MachineInstr *InsertPt = 3228 BuildMI(LoopBB, I, DL, TII->get(ST.isWave32() ? AMDGPU::S_XOR_B32_term 3229 : AMDGPU::S_XOR_B64_term), Exec) 3230 .addReg(Exec) 3231 .addReg(NewExec); 3232 3233 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use 3234 // s_cbranch_scc0? 3235 3236 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover. 3237 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ)) 3238 .addMBB(&LoopBB); 3239 3240 return InsertPt->getIterator(); 3241 } 3242 3243 // This has slightly sub-optimal regalloc when the source vector is killed by 3244 // the read. The register allocator does not understand that the kill is 3245 // per-workitem, so is kept alive for the whole loop so we end up not re-using a 3246 // subregister from it, using 1 more VGPR than necessary. This was saved when 3247 // this was expanded after register allocation. 3248 static MachineBasicBlock::iterator loadM0FromVGPR(const SIInstrInfo *TII, 3249 MachineBasicBlock &MBB, 3250 MachineInstr &MI, 3251 unsigned InitResultReg, 3252 unsigned PhiReg, 3253 int Offset, 3254 bool UseGPRIdxMode, 3255 bool IsIndirectSrc) { 3256 MachineFunction *MF = MBB.getParent(); 3257 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>(); 3258 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 3259 MachineRegisterInfo &MRI = MF->getRegInfo(); 3260 const DebugLoc &DL = MI.getDebugLoc(); 3261 MachineBasicBlock::iterator I(&MI); 3262 3263 const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 3264 Register DstReg = MI.getOperand(0).getReg(); 3265 Register SaveExec = MRI.createVirtualRegister(BoolXExecRC); 3266 Register TmpExec = MRI.createVirtualRegister(BoolXExecRC); 3267 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 3268 unsigned MovExecOpc = ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64; 3269 3270 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec); 3271 3272 // Save the EXEC mask 3273 BuildMI(MBB, I, DL, TII->get(MovExecOpc), SaveExec) 3274 .addReg(Exec); 3275 3276 MachineBasicBlock *LoopBB; 3277 MachineBasicBlock *RemainderBB; 3278 std::tie(LoopBB, RemainderBB) = splitBlockForLoop(MI, MBB, false); 3279 3280 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); 3281 3282 auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx, 3283 InitResultReg, DstReg, PhiReg, TmpExec, 3284 Offset, UseGPRIdxMode, IsIndirectSrc); 3285 3286 MachineBasicBlock::iterator First = RemainderBB->begin(); 3287 BuildMI(*RemainderBB, First, DL, TII->get(MovExecOpc), Exec) 3288 .addReg(SaveExec); 3289 3290 return InsPt; 3291 } 3292 3293 // Returns subreg index, offset 3294 static std::pair<unsigned, int> 3295 computeIndirectRegAndOffset(const SIRegisterInfo &TRI, 3296 const TargetRegisterClass *SuperRC, 3297 unsigned VecReg, 3298 int Offset) { 3299 int NumElts = TRI.getRegSizeInBits(*SuperRC) / 32; 3300 3301 // Skip out of bounds offsets, or else we would end up using an undefined 3302 // register. 3303 if (Offset >= NumElts || Offset < 0) 3304 return std::make_pair(AMDGPU::sub0, Offset); 3305 3306 return std::make_pair(AMDGPURegisterInfo::getSubRegFromChannel(Offset), 0); 3307 } 3308 3309 // Return true if the index is an SGPR and was set. 3310 static bool setM0ToIndexFromSGPR(const SIInstrInfo *TII, 3311 MachineRegisterInfo &MRI, 3312 MachineInstr &MI, 3313 int Offset, 3314 bool UseGPRIdxMode, 3315 bool IsIndirectSrc) { 3316 MachineBasicBlock *MBB = MI.getParent(); 3317 const DebugLoc &DL = MI.getDebugLoc(); 3318 MachineBasicBlock::iterator I(&MI); 3319 3320 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); 3321 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg()); 3322 3323 assert(Idx->getReg() != AMDGPU::NoRegister); 3324 3325 if (!TII->getRegisterInfo().isSGPRClass(IdxRC)) 3326 return false; 3327 3328 if (UseGPRIdxMode) { 3329 unsigned IdxMode = IsIndirectSrc ? 3330 AMDGPU::VGPRIndexMode::SRC0_ENABLE : AMDGPU::VGPRIndexMode::DST_ENABLE; 3331 if (Offset == 0) { 3332 MachineInstr *SetOn = 3333 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 3334 .add(*Idx) 3335 .addImm(IdxMode); 3336 3337 SetOn->getOperand(3).setIsUndef(); 3338 } else { 3339 Register Tmp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 3340 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), Tmp) 3341 .add(*Idx) 3342 .addImm(Offset); 3343 MachineInstr *SetOn = 3344 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 3345 .addReg(Tmp, RegState::Kill) 3346 .addImm(IdxMode); 3347 3348 SetOn->getOperand(3).setIsUndef(); 3349 } 3350 3351 return true; 3352 } 3353 3354 if (Offset == 0) { 3355 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) 3356 .add(*Idx); 3357 } else { 3358 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0) 3359 .add(*Idx) 3360 .addImm(Offset); 3361 } 3362 3363 return true; 3364 } 3365 3366 // Control flow needs to be inserted if indexing with a VGPR. 3367 static MachineBasicBlock *emitIndirectSrc(MachineInstr &MI, 3368 MachineBasicBlock &MBB, 3369 const GCNSubtarget &ST) { 3370 const SIInstrInfo *TII = ST.getInstrInfo(); 3371 const SIRegisterInfo &TRI = TII->getRegisterInfo(); 3372 MachineFunction *MF = MBB.getParent(); 3373 MachineRegisterInfo &MRI = MF->getRegInfo(); 3374 3375 Register Dst = MI.getOperand(0).getReg(); 3376 Register SrcReg = TII->getNamedOperand(MI, AMDGPU::OpName::src)->getReg(); 3377 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm(); 3378 3379 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcReg); 3380 3381 unsigned SubReg; 3382 std::tie(SubReg, Offset) 3383 = computeIndirectRegAndOffset(TRI, VecRC, SrcReg, Offset); 3384 3385 const bool UseGPRIdxMode = ST.useVGPRIndexMode(); 3386 3387 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, true)) { 3388 MachineBasicBlock::iterator I(&MI); 3389 const DebugLoc &DL = MI.getDebugLoc(); 3390 3391 if (UseGPRIdxMode) { 3392 // TODO: Look at the uses to avoid the copy. This may require rescheduling 3393 // to avoid interfering with other uses, so probably requires a new 3394 // optimization pass. 3395 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst) 3396 .addReg(SrcReg, RegState::Undef, SubReg) 3397 .addReg(SrcReg, RegState::Implicit) 3398 .addReg(AMDGPU::M0, RegState::Implicit); 3399 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 3400 } else { 3401 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst) 3402 .addReg(SrcReg, RegState::Undef, SubReg) 3403 .addReg(SrcReg, RegState::Implicit); 3404 } 3405 3406 MI.eraseFromParent(); 3407 3408 return &MBB; 3409 } 3410 3411 const DebugLoc &DL = MI.getDebugLoc(); 3412 MachineBasicBlock::iterator I(&MI); 3413 3414 Register PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3415 Register InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3416 3417 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg); 3418 3419 auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, 3420 Offset, UseGPRIdxMode, true); 3421 MachineBasicBlock *LoopBB = InsPt->getParent(); 3422 3423 if (UseGPRIdxMode) { 3424 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst) 3425 .addReg(SrcReg, RegState::Undef, SubReg) 3426 .addReg(SrcReg, RegState::Implicit) 3427 .addReg(AMDGPU::M0, RegState::Implicit); 3428 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 3429 } else { 3430 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst) 3431 .addReg(SrcReg, RegState::Undef, SubReg) 3432 .addReg(SrcReg, RegState::Implicit); 3433 } 3434 3435 MI.eraseFromParent(); 3436 3437 return LoopBB; 3438 } 3439 3440 static MachineBasicBlock *emitIndirectDst(MachineInstr &MI, 3441 MachineBasicBlock &MBB, 3442 const GCNSubtarget &ST) { 3443 const SIInstrInfo *TII = ST.getInstrInfo(); 3444 const SIRegisterInfo &TRI = TII->getRegisterInfo(); 3445 MachineFunction *MF = MBB.getParent(); 3446 MachineRegisterInfo &MRI = MF->getRegInfo(); 3447 3448 Register Dst = MI.getOperand(0).getReg(); 3449 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src); 3450 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); 3451 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val); 3452 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm(); 3453 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg()); 3454 3455 // This can be an immediate, but will be folded later. 3456 assert(Val->getReg()); 3457 3458 unsigned SubReg; 3459 std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC, 3460 SrcVec->getReg(), 3461 Offset); 3462 const bool UseGPRIdxMode = ST.useVGPRIndexMode(); 3463 3464 if (Idx->getReg() == AMDGPU::NoRegister) { 3465 MachineBasicBlock::iterator I(&MI); 3466 const DebugLoc &DL = MI.getDebugLoc(); 3467 3468 assert(Offset == 0); 3469 3470 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst) 3471 .add(*SrcVec) 3472 .add(*Val) 3473 .addImm(SubReg); 3474 3475 MI.eraseFromParent(); 3476 return &MBB; 3477 } 3478 3479 const MCInstrDesc &MovRelDesc 3480 = TII->getIndirectRegWritePseudo(TRI.getRegSizeInBits(*VecRC), 32, false); 3481 3482 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, false)) { 3483 MachineBasicBlock::iterator I(&MI); 3484 const DebugLoc &DL = MI.getDebugLoc(); 3485 BuildMI(MBB, I, DL, MovRelDesc, Dst) 3486 .addReg(SrcVec->getReg()) 3487 .add(*Val) 3488 .addImm(SubReg); 3489 if (UseGPRIdxMode) 3490 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 3491 3492 MI.eraseFromParent(); 3493 return &MBB; 3494 } 3495 3496 if (Val->isReg()) 3497 MRI.clearKillFlags(Val->getReg()); 3498 3499 const DebugLoc &DL = MI.getDebugLoc(); 3500 3501 Register PhiReg = MRI.createVirtualRegister(VecRC); 3502 3503 auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg, 3504 Offset, UseGPRIdxMode, false); 3505 MachineBasicBlock *LoopBB = InsPt->getParent(); 3506 3507 BuildMI(*LoopBB, InsPt, DL, MovRelDesc, Dst) 3508 .addReg(PhiReg) 3509 .add(*Val) 3510 .addImm(AMDGPU::sub0); 3511 if (UseGPRIdxMode) 3512 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 3513 3514 MI.eraseFromParent(); 3515 return LoopBB; 3516 } 3517 3518 MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter( 3519 MachineInstr &MI, MachineBasicBlock *BB) const { 3520 3521 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3522 MachineFunction *MF = BB->getParent(); 3523 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 3524 3525 if (TII->isMIMG(MI)) { 3526 if (MI.memoperands_empty() && MI.mayLoadOrStore()) { 3527 report_fatal_error("missing mem operand from MIMG instruction"); 3528 } 3529 // Add a memoperand for mimg instructions so that they aren't assumed to 3530 // be ordered memory instuctions. 3531 3532 return BB; 3533 } 3534 3535 switch (MI.getOpcode()) { 3536 case AMDGPU::S_ADD_U64_PSEUDO: 3537 case AMDGPU::S_SUB_U64_PSEUDO: { 3538 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 3539 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>(); 3540 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 3541 const TargetRegisterClass *BoolRC = TRI->getBoolRC(); 3542 const DebugLoc &DL = MI.getDebugLoc(); 3543 3544 MachineOperand &Dest = MI.getOperand(0); 3545 MachineOperand &Src0 = MI.getOperand(1); 3546 MachineOperand &Src1 = MI.getOperand(2); 3547 3548 Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 3549 Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 3550 3551 MachineOperand Src0Sub0 = TII->buildExtractSubRegOrImm(MI, MRI, 3552 Src0, BoolRC, AMDGPU::sub0, 3553 &AMDGPU::SReg_32RegClass); 3554 MachineOperand Src0Sub1 = TII->buildExtractSubRegOrImm(MI, MRI, 3555 Src0, BoolRC, AMDGPU::sub1, 3556 &AMDGPU::SReg_32RegClass); 3557 3558 MachineOperand Src1Sub0 = TII->buildExtractSubRegOrImm(MI, MRI, 3559 Src1, BoolRC, AMDGPU::sub0, 3560 &AMDGPU::SReg_32RegClass); 3561 MachineOperand Src1Sub1 = TII->buildExtractSubRegOrImm(MI, MRI, 3562 Src1, BoolRC, AMDGPU::sub1, 3563 &AMDGPU::SReg_32RegClass); 3564 3565 bool IsAdd = (MI.getOpcode() == AMDGPU::S_ADD_U64_PSEUDO); 3566 3567 unsigned LoOpc = IsAdd ? AMDGPU::S_ADD_U32 : AMDGPU::S_SUB_U32; 3568 unsigned HiOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32; 3569 BuildMI(*BB, MI, DL, TII->get(LoOpc), DestSub0) 3570 .add(Src0Sub0) 3571 .add(Src1Sub0); 3572 BuildMI(*BB, MI, DL, TII->get(HiOpc), DestSub1) 3573 .add(Src0Sub1) 3574 .add(Src1Sub1); 3575 BuildMI(*BB, MI, DL, TII->get(TargetOpcode::REG_SEQUENCE), Dest.getReg()) 3576 .addReg(DestSub0) 3577 .addImm(AMDGPU::sub0) 3578 .addReg(DestSub1) 3579 .addImm(AMDGPU::sub1); 3580 MI.eraseFromParent(); 3581 return BB; 3582 } 3583 case AMDGPU::SI_INIT_M0: { 3584 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(), 3585 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) 3586 .add(MI.getOperand(0)); 3587 MI.eraseFromParent(); 3588 return BB; 3589 } 3590 case AMDGPU::SI_INIT_EXEC: 3591 // This should be before all vector instructions. 3592 BuildMI(*BB, &*BB->begin(), MI.getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), 3593 AMDGPU::EXEC) 3594 .addImm(MI.getOperand(0).getImm()); 3595 MI.eraseFromParent(); 3596 return BB; 3597 3598 case AMDGPU::SI_INIT_EXEC_LO: 3599 // This should be before all vector instructions. 3600 BuildMI(*BB, &*BB->begin(), MI.getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), 3601 AMDGPU::EXEC_LO) 3602 .addImm(MI.getOperand(0).getImm()); 3603 MI.eraseFromParent(); 3604 return BB; 3605 3606 case AMDGPU::SI_INIT_EXEC_FROM_INPUT: { 3607 // Extract the thread count from an SGPR input and set EXEC accordingly. 3608 // Since BFM can't shift by 64, handle that case with CMP + CMOV. 3609 // 3610 // S_BFE_U32 count, input, {shift, 7} 3611 // S_BFM_B64 exec, count, 0 3612 // S_CMP_EQ_U32 count, 64 3613 // S_CMOV_B64 exec, -1 3614 MachineInstr *FirstMI = &*BB->begin(); 3615 MachineRegisterInfo &MRI = MF->getRegInfo(); 3616 Register InputReg = MI.getOperand(0).getReg(); 3617 Register CountReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 3618 bool Found = false; 3619 3620 // Move the COPY of the input reg to the beginning, so that we can use it. 3621 for (auto I = BB->begin(); I != &MI; I++) { 3622 if (I->getOpcode() != TargetOpcode::COPY || 3623 I->getOperand(0).getReg() != InputReg) 3624 continue; 3625 3626 if (I == FirstMI) { 3627 FirstMI = &*++BB->begin(); 3628 } else { 3629 I->removeFromParent(); 3630 BB->insert(FirstMI, &*I); 3631 } 3632 Found = true; 3633 break; 3634 } 3635 assert(Found); 3636 (void)Found; 3637 3638 // This should be before all vector instructions. 3639 unsigned Mask = (getSubtarget()->getWavefrontSize() << 1) - 1; 3640 bool isWave32 = getSubtarget()->isWave32(); 3641 unsigned Exec = isWave32 ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 3642 BuildMI(*BB, FirstMI, DebugLoc(), TII->get(AMDGPU::S_BFE_U32), CountReg) 3643 .addReg(InputReg) 3644 .addImm((MI.getOperand(1).getImm() & Mask) | 0x70000); 3645 BuildMI(*BB, FirstMI, DebugLoc(), 3646 TII->get(isWave32 ? AMDGPU::S_BFM_B32 : AMDGPU::S_BFM_B64), 3647 Exec) 3648 .addReg(CountReg) 3649 .addImm(0); 3650 BuildMI(*BB, FirstMI, DebugLoc(), TII->get(AMDGPU::S_CMP_EQ_U32)) 3651 .addReg(CountReg, RegState::Kill) 3652 .addImm(getSubtarget()->getWavefrontSize()); 3653 BuildMI(*BB, FirstMI, DebugLoc(), 3654 TII->get(isWave32 ? AMDGPU::S_CMOV_B32 : AMDGPU::S_CMOV_B64), 3655 Exec) 3656 .addImm(-1); 3657 MI.eraseFromParent(); 3658 return BB; 3659 } 3660 3661 case AMDGPU::GET_GROUPSTATICSIZE: { 3662 assert(getTargetMachine().getTargetTriple().getOS() == Triple::AMDHSA || 3663 getTargetMachine().getTargetTriple().getOS() == Triple::AMDPAL); 3664 DebugLoc DL = MI.getDebugLoc(); 3665 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32)) 3666 .add(MI.getOperand(0)) 3667 .addImm(MFI->getLDSSize()); 3668 MI.eraseFromParent(); 3669 return BB; 3670 } 3671 case AMDGPU::SI_INDIRECT_SRC_V1: 3672 case AMDGPU::SI_INDIRECT_SRC_V2: 3673 case AMDGPU::SI_INDIRECT_SRC_V4: 3674 case AMDGPU::SI_INDIRECT_SRC_V8: 3675 case AMDGPU::SI_INDIRECT_SRC_V16: 3676 return emitIndirectSrc(MI, *BB, *getSubtarget()); 3677 case AMDGPU::SI_INDIRECT_DST_V1: 3678 case AMDGPU::SI_INDIRECT_DST_V2: 3679 case AMDGPU::SI_INDIRECT_DST_V4: 3680 case AMDGPU::SI_INDIRECT_DST_V8: 3681 case AMDGPU::SI_INDIRECT_DST_V16: 3682 return emitIndirectDst(MI, *BB, *getSubtarget()); 3683 case AMDGPU::SI_KILL_F32_COND_IMM_PSEUDO: 3684 case AMDGPU::SI_KILL_I1_PSEUDO: 3685 return splitKillBlock(MI, BB); 3686 case AMDGPU::V_CNDMASK_B64_PSEUDO: { 3687 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 3688 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>(); 3689 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 3690 3691 Register Dst = MI.getOperand(0).getReg(); 3692 Register Src0 = MI.getOperand(1).getReg(); 3693 Register Src1 = MI.getOperand(2).getReg(); 3694 const DebugLoc &DL = MI.getDebugLoc(); 3695 Register SrcCond = MI.getOperand(3).getReg(); 3696 3697 Register DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3698 Register DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3699 const auto *CondRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 3700 Register SrcCondCopy = MRI.createVirtualRegister(CondRC); 3701 3702 BuildMI(*BB, MI, DL, TII->get(AMDGPU::COPY), SrcCondCopy) 3703 .addReg(SrcCond); 3704 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo) 3705 .addImm(0) 3706 .addReg(Src0, 0, AMDGPU::sub0) 3707 .addImm(0) 3708 .addReg(Src1, 0, AMDGPU::sub0) 3709 .addReg(SrcCondCopy); 3710 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi) 3711 .addImm(0) 3712 .addReg(Src0, 0, AMDGPU::sub1) 3713 .addImm(0) 3714 .addReg(Src1, 0, AMDGPU::sub1) 3715 .addReg(SrcCondCopy); 3716 3717 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst) 3718 .addReg(DstLo) 3719 .addImm(AMDGPU::sub0) 3720 .addReg(DstHi) 3721 .addImm(AMDGPU::sub1); 3722 MI.eraseFromParent(); 3723 return BB; 3724 } 3725 case AMDGPU::SI_BR_UNDEF: { 3726 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3727 const DebugLoc &DL = MI.getDebugLoc(); 3728 MachineInstr *Br = BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_CBRANCH_SCC1)) 3729 .add(MI.getOperand(0)); 3730 Br->getOperand(1).setIsUndef(true); // read undef SCC 3731 MI.eraseFromParent(); 3732 return BB; 3733 } 3734 case AMDGPU::ADJCALLSTACKUP: 3735 case AMDGPU::ADJCALLSTACKDOWN: { 3736 const SIMachineFunctionInfo *Info = MF->getInfo<SIMachineFunctionInfo>(); 3737 MachineInstrBuilder MIB(*MF, &MI); 3738 3739 // Add an implicit use of the frame offset reg to prevent the restore copy 3740 // inserted after the call from being reorderd after stack operations in the 3741 // the caller's frame. 3742 MIB.addReg(Info->getStackPtrOffsetReg(), RegState::ImplicitDefine) 3743 .addReg(Info->getStackPtrOffsetReg(), RegState::Implicit) 3744 .addReg(Info->getFrameOffsetReg(), RegState::Implicit); 3745 return BB; 3746 } 3747 case AMDGPU::SI_CALL_ISEL: { 3748 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3749 const DebugLoc &DL = MI.getDebugLoc(); 3750 3751 unsigned ReturnAddrReg = TII->getRegisterInfo().getReturnAddressReg(*MF); 3752 3753 MachineInstrBuilder MIB; 3754 MIB = BuildMI(*BB, MI, DL, TII->get(AMDGPU::SI_CALL), ReturnAddrReg); 3755 3756 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) 3757 MIB.add(MI.getOperand(I)); 3758 3759 MIB.cloneMemRefs(MI); 3760 MI.eraseFromParent(); 3761 return BB; 3762 } 3763 case AMDGPU::V_ADD_I32_e32: 3764 case AMDGPU::V_SUB_I32_e32: 3765 case AMDGPU::V_SUBREV_I32_e32: { 3766 // TODO: Define distinct V_*_I32_Pseudo instructions instead. 3767 const DebugLoc &DL = MI.getDebugLoc(); 3768 unsigned Opc = MI.getOpcode(); 3769 3770 bool NeedClampOperand = false; 3771 if (TII->pseudoToMCOpcode(Opc) == -1) { 3772 Opc = AMDGPU::getVOPe64(Opc); 3773 NeedClampOperand = true; 3774 } 3775 3776 auto I = BuildMI(*BB, MI, DL, TII->get(Opc), MI.getOperand(0).getReg()); 3777 if (TII->isVOP3(*I)) { 3778 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>(); 3779 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 3780 I.addReg(TRI->getVCC(), RegState::Define); 3781 } 3782 I.add(MI.getOperand(1)) 3783 .add(MI.getOperand(2)); 3784 if (NeedClampOperand) 3785 I.addImm(0); // clamp bit for e64 encoding 3786 3787 TII->legalizeOperands(*I); 3788 3789 MI.eraseFromParent(); 3790 return BB; 3791 } 3792 case AMDGPU::DS_GWS_INIT: 3793 case AMDGPU::DS_GWS_SEMA_V: 3794 case AMDGPU::DS_GWS_SEMA_BR: 3795 case AMDGPU::DS_GWS_SEMA_P: 3796 case AMDGPU::DS_GWS_SEMA_RELEASE_ALL: 3797 case AMDGPU::DS_GWS_BARRIER: 3798 // A s_waitcnt 0 is required to be the instruction immediately following. 3799 if (getSubtarget()->hasGWSAutoReplay()) { 3800 bundleInstWithWaitcnt(MI); 3801 return BB; 3802 } 3803 3804 return emitGWSMemViolTestLoop(MI, BB); 3805 default: 3806 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB); 3807 } 3808 } 3809 3810 bool SITargetLowering::hasBitPreservingFPLogic(EVT VT) const { 3811 return isTypeLegal(VT.getScalarType()); 3812 } 3813 3814 bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const { 3815 // This currently forces unfolding various combinations of fsub into fma with 3816 // free fneg'd operands. As long as we have fast FMA (controlled by 3817 // isFMAFasterThanFMulAndFAdd), we should perform these. 3818 3819 // When fma is quarter rate, for f64 where add / sub are at best half rate, 3820 // most of these combines appear to be cycle neutral but save on instruction 3821 // count / code size. 3822 return true; 3823 } 3824 3825 EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx, 3826 EVT VT) const { 3827 if (!VT.isVector()) { 3828 return MVT::i1; 3829 } 3830 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements()); 3831 } 3832 3833 MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT VT) const { 3834 // TODO: Should i16 be used always if legal? For now it would force VALU 3835 // shifts. 3836 return (VT == MVT::i16) ? MVT::i16 : MVT::i32; 3837 } 3838 3839 // Answering this is somewhat tricky and depends on the specific device which 3840 // have different rates for fma or all f64 operations. 3841 // 3842 // v_fma_f64 and v_mul_f64 always take the same number of cycles as each other 3843 // regardless of which device (although the number of cycles differs between 3844 // devices), so it is always profitable for f64. 3845 // 3846 // v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable 3847 // only on full rate devices. Normally, we should prefer selecting v_mad_f32 3848 // which we can always do even without fused FP ops since it returns the same 3849 // result as the separate operations and since it is always full 3850 // rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32 3851 // however does not support denormals, so we do report fma as faster if we have 3852 // a fast fma device and require denormals. 3853 // 3854 bool SITargetLowering::isFMAFasterThanFMulAndFAdd(const MachineFunction &MF, 3855 EVT VT) const { 3856 VT = VT.getScalarType(); 3857 3858 switch (VT.getSimpleVT().SimpleTy) { 3859 case MVT::f32: { 3860 // This is as fast on some subtargets. However, we always have full rate f32 3861 // mad available which returns the same result as the separate operations 3862 // which we should prefer over fma. We can't use this if we want to support 3863 // denormals, so only report this in these cases. 3864 if (hasFP32Denormals(MF)) 3865 return Subtarget->hasFastFMAF32() || Subtarget->hasDLInsts(); 3866 3867 // If the subtarget has v_fmac_f32, that's just as good as v_mac_f32. 3868 return Subtarget->hasFastFMAF32() && Subtarget->hasDLInsts(); 3869 } 3870 case MVT::f64: 3871 return true; 3872 case MVT::f16: 3873 return Subtarget->has16BitInsts() && hasFP64FP16Denormals(MF); 3874 default: 3875 break; 3876 } 3877 3878 return false; 3879 } 3880 3881 bool SITargetLowering::isFMADLegalForFAddFSub(const SelectionDAG &DAG, 3882 const SDNode *N) const { 3883 // TODO: Check future ftz flag 3884 // v_mad_f32/v_mac_f32 do not support denormals. 3885 EVT VT = N->getValueType(0); 3886 if (VT == MVT::f32) 3887 return !hasFP32Denormals(DAG.getMachineFunction()); 3888 if (VT == MVT::f16) { 3889 return Subtarget->hasMadF16() && 3890 !hasFP64FP16Denormals(DAG.getMachineFunction()); 3891 } 3892 3893 return false; 3894 } 3895 3896 //===----------------------------------------------------------------------===// 3897 // Custom DAG Lowering Operations 3898 //===----------------------------------------------------------------------===// 3899 3900 // Work around LegalizeDAG doing the wrong thing and fully scalarizing if the 3901 // wider vector type is legal. 3902 SDValue SITargetLowering::splitUnaryVectorOp(SDValue Op, 3903 SelectionDAG &DAG) const { 3904 unsigned Opc = Op.getOpcode(); 3905 EVT VT = Op.getValueType(); 3906 assert(VT == MVT::v4f16); 3907 3908 SDValue Lo, Hi; 3909 std::tie(Lo, Hi) = DAG.SplitVectorOperand(Op.getNode(), 0); 3910 3911 SDLoc SL(Op); 3912 SDValue OpLo = DAG.getNode(Opc, SL, Lo.getValueType(), Lo, 3913 Op->getFlags()); 3914 SDValue OpHi = DAG.getNode(Opc, SL, Hi.getValueType(), Hi, 3915 Op->getFlags()); 3916 3917 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Op), VT, OpLo, OpHi); 3918 } 3919 3920 // Work around LegalizeDAG doing the wrong thing and fully scalarizing if the 3921 // wider vector type is legal. 3922 SDValue SITargetLowering::splitBinaryVectorOp(SDValue Op, 3923 SelectionDAG &DAG) const { 3924 unsigned Opc = Op.getOpcode(); 3925 EVT VT = Op.getValueType(); 3926 assert(VT == MVT::v4i16 || VT == MVT::v4f16); 3927 3928 SDValue Lo0, Hi0; 3929 std::tie(Lo0, Hi0) = DAG.SplitVectorOperand(Op.getNode(), 0); 3930 SDValue Lo1, Hi1; 3931 std::tie(Lo1, Hi1) = DAG.SplitVectorOperand(Op.getNode(), 1); 3932 3933 SDLoc SL(Op); 3934 3935 SDValue OpLo = DAG.getNode(Opc, SL, Lo0.getValueType(), Lo0, Lo1, 3936 Op->getFlags()); 3937 SDValue OpHi = DAG.getNode(Opc, SL, Hi0.getValueType(), Hi0, Hi1, 3938 Op->getFlags()); 3939 3940 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Op), VT, OpLo, OpHi); 3941 } 3942 3943 SDValue SITargetLowering::splitTernaryVectorOp(SDValue Op, 3944 SelectionDAG &DAG) const { 3945 unsigned Opc = Op.getOpcode(); 3946 EVT VT = Op.getValueType(); 3947 assert(VT == MVT::v4i16 || VT == MVT::v4f16); 3948 3949 SDValue Lo0, Hi0; 3950 std::tie(Lo0, Hi0) = DAG.SplitVectorOperand(Op.getNode(), 0); 3951 SDValue Lo1, Hi1; 3952 std::tie(Lo1, Hi1) = DAG.SplitVectorOperand(Op.getNode(), 1); 3953 SDValue Lo2, Hi2; 3954 std::tie(Lo2, Hi2) = DAG.SplitVectorOperand(Op.getNode(), 2); 3955 3956 SDLoc SL(Op); 3957 3958 SDValue OpLo = DAG.getNode(Opc, SL, Lo0.getValueType(), Lo0, Lo1, Lo2, 3959 Op->getFlags()); 3960 SDValue OpHi = DAG.getNode(Opc, SL, Hi0.getValueType(), Hi0, Hi1, Hi2, 3961 Op->getFlags()); 3962 3963 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Op), VT, OpLo, OpHi); 3964 } 3965 3966 3967 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 3968 switch (Op.getOpcode()) { 3969 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); 3970 case ISD::BRCOND: return LowerBRCOND(Op, DAG); 3971 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 3972 case ISD::LOAD: { 3973 SDValue Result = LowerLOAD(Op, DAG); 3974 assert((!Result.getNode() || 3975 Result.getNode()->getNumValues() == 2) && 3976 "Load should return a value and a chain"); 3977 return Result; 3978 } 3979 3980 case ISD::FSIN: 3981 case ISD::FCOS: 3982 return LowerTrig(Op, DAG); 3983 case ISD::SELECT: return LowerSELECT(Op, DAG); 3984 case ISD::FDIV: return LowerFDIV(Op, DAG); 3985 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG); 3986 case ISD::STORE: return LowerSTORE(Op, DAG); 3987 case ISD::GlobalAddress: { 3988 MachineFunction &MF = DAG.getMachineFunction(); 3989 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 3990 return LowerGlobalAddress(MFI, Op, DAG); 3991 } 3992 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); 3993 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG); 3994 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG); 3995 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG); 3996 case ISD::INSERT_SUBVECTOR: 3997 return lowerINSERT_SUBVECTOR(Op, DAG); 3998 case ISD::INSERT_VECTOR_ELT: 3999 return lowerINSERT_VECTOR_ELT(Op, DAG); 4000 case ISD::EXTRACT_VECTOR_ELT: 4001 return lowerEXTRACT_VECTOR_ELT(Op, DAG); 4002 case ISD::VECTOR_SHUFFLE: 4003 return lowerVECTOR_SHUFFLE(Op, DAG); 4004 case ISD::BUILD_VECTOR: 4005 return lowerBUILD_VECTOR(Op, DAG); 4006 case ISD::FP_ROUND: 4007 return lowerFP_ROUND(Op, DAG); 4008 case ISD::TRAP: 4009 return lowerTRAP(Op, DAG); 4010 case ISD::DEBUGTRAP: 4011 return lowerDEBUGTRAP(Op, DAG); 4012 case ISD::FABS: 4013 case ISD::FNEG: 4014 case ISD::FCANONICALIZE: 4015 return splitUnaryVectorOp(Op, DAG); 4016 case ISD::FMINNUM: 4017 case ISD::FMAXNUM: 4018 return lowerFMINNUM_FMAXNUM(Op, DAG); 4019 case ISD::FMA: 4020 return splitTernaryVectorOp(Op, DAG); 4021 case ISD::SHL: 4022 case ISD::SRA: 4023 case ISD::SRL: 4024 case ISD::ADD: 4025 case ISD::SUB: 4026 case ISD::MUL: 4027 case ISD::SMIN: 4028 case ISD::SMAX: 4029 case ISD::UMIN: 4030 case ISD::UMAX: 4031 case ISD::FADD: 4032 case ISD::FMUL: 4033 case ISD::FMINNUM_IEEE: 4034 case ISD::FMAXNUM_IEEE: 4035 return splitBinaryVectorOp(Op, DAG); 4036 } 4037 return SDValue(); 4038 } 4039 4040 static SDValue adjustLoadValueTypeImpl(SDValue Result, EVT LoadVT, 4041 const SDLoc &DL, 4042 SelectionDAG &DAG, bool Unpacked) { 4043 if (!LoadVT.isVector()) 4044 return Result; 4045 4046 if (Unpacked) { // From v2i32/v4i32 back to v2f16/v4f16. 4047 // Truncate to v2i16/v4i16. 4048 EVT IntLoadVT = LoadVT.changeTypeToInteger(); 4049 4050 // Workaround legalizer not scalarizing truncate after vector op 4051 // legalization byt not creating intermediate vector trunc. 4052 SmallVector<SDValue, 4> Elts; 4053 DAG.ExtractVectorElements(Result, Elts); 4054 for (SDValue &Elt : Elts) 4055 Elt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, Elt); 4056 4057 Result = DAG.getBuildVector(IntLoadVT, DL, Elts); 4058 4059 // Bitcast to original type (v2f16/v4f16). 4060 return DAG.getNode(ISD::BITCAST, DL, LoadVT, Result); 4061 } 4062 4063 // Cast back to the original packed type. 4064 return DAG.getNode(ISD::BITCAST, DL, LoadVT, Result); 4065 } 4066 4067 SDValue SITargetLowering::adjustLoadValueType(unsigned Opcode, 4068 MemSDNode *M, 4069 SelectionDAG &DAG, 4070 ArrayRef<SDValue> Ops, 4071 bool IsIntrinsic) const { 4072 SDLoc DL(M); 4073 4074 bool Unpacked = Subtarget->hasUnpackedD16VMem(); 4075 EVT LoadVT = M->getValueType(0); 4076 4077 EVT EquivLoadVT = LoadVT; 4078 if (Unpacked && LoadVT.isVector()) { 4079 EquivLoadVT = LoadVT.isVector() ? 4080 EVT::getVectorVT(*DAG.getContext(), MVT::i32, 4081 LoadVT.getVectorNumElements()) : LoadVT; 4082 } 4083 4084 // Change from v4f16/v2f16 to EquivLoadVT. 4085 SDVTList VTList = DAG.getVTList(EquivLoadVT, MVT::Other); 4086 4087 SDValue Load 4088 = DAG.getMemIntrinsicNode( 4089 IsIntrinsic ? (unsigned)ISD::INTRINSIC_W_CHAIN : Opcode, DL, 4090 VTList, Ops, M->getMemoryVT(), 4091 M->getMemOperand()); 4092 if (!Unpacked) // Just adjusted the opcode. 4093 return Load; 4094 4095 SDValue Adjusted = adjustLoadValueTypeImpl(Load, LoadVT, DL, DAG, Unpacked); 4096 4097 return DAG.getMergeValues({ Adjusted, Load.getValue(1) }, DL); 4098 } 4099 4100 SDValue SITargetLowering::lowerIntrinsicLoad(MemSDNode *M, bool IsFormat, 4101 SelectionDAG &DAG, 4102 ArrayRef<SDValue> Ops) const { 4103 SDLoc DL(M); 4104 EVT LoadVT = M->getValueType(0); 4105 EVT EltType = LoadVT.getScalarType(); 4106 EVT IntVT = LoadVT.changeTypeToInteger(); 4107 4108 bool IsD16 = IsFormat && (EltType.getSizeInBits() == 16); 4109 4110 unsigned Opc = 4111 IsFormat ? AMDGPUISD::BUFFER_LOAD_FORMAT : AMDGPUISD::BUFFER_LOAD; 4112 4113 if (IsD16) { 4114 return adjustLoadValueType(AMDGPUISD::BUFFER_LOAD_FORMAT_D16, M, DAG, Ops); 4115 } 4116 4117 // Handle BUFFER_LOAD_BYTE/UBYTE/SHORT/USHORT overloaded intrinsics 4118 if (!IsD16 && !LoadVT.isVector() && EltType.getSizeInBits() < 32) 4119 return handleByteShortBufferLoads(DAG, LoadVT, DL, Ops, M); 4120 4121 if (isTypeLegal(LoadVT)) { 4122 return getMemIntrinsicNode(Opc, DL, M->getVTList(), Ops, IntVT, 4123 M->getMemOperand(), DAG); 4124 } 4125 4126 EVT CastVT = getEquivalentMemType(*DAG.getContext(), LoadVT); 4127 SDVTList VTList = DAG.getVTList(CastVT, MVT::Other); 4128 SDValue MemNode = getMemIntrinsicNode(Opc, DL, VTList, Ops, CastVT, 4129 M->getMemOperand(), DAG); 4130 return DAG.getMergeValues( 4131 {DAG.getNode(ISD::BITCAST, DL, LoadVT, MemNode), MemNode.getValue(1)}, 4132 DL); 4133 } 4134 4135 static SDValue lowerICMPIntrinsic(const SITargetLowering &TLI, 4136 SDNode *N, SelectionDAG &DAG) { 4137 EVT VT = N->getValueType(0); 4138 const auto *CD = cast<ConstantSDNode>(N->getOperand(3)); 4139 int CondCode = CD->getSExtValue(); 4140 if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE || 4141 CondCode > ICmpInst::Predicate::LAST_ICMP_PREDICATE) 4142 return DAG.getUNDEF(VT); 4143 4144 ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode); 4145 4146 SDValue LHS = N->getOperand(1); 4147 SDValue RHS = N->getOperand(2); 4148 4149 SDLoc DL(N); 4150 4151 EVT CmpVT = LHS.getValueType(); 4152 if (CmpVT == MVT::i16 && !TLI.isTypeLegal(MVT::i16)) { 4153 unsigned PromoteOp = ICmpInst::isSigned(IcInput) ? 4154 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 4155 LHS = DAG.getNode(PromoteOp, DL, MVT::i32, LHS); 4156 RHS = DAG.getNode(PromoteOp, DL, MVT::i32, RHS); 4157 } 4158 4159 ISD::CondCode CCOpcode = getICmpCondCode(IcInput); 4160 4161 unsigned WavefrontSize = TLI.getSubtarget()->getWavefrontSize(); 4162 EVT CCVT = EVT::getIntegerVT(*DAG.getContext(), WavefrontSize); 4163 4164 SDValue SetCC = DAG.getNode(AMDGPUISD::SETCC, DL, CCVT, LHS, RHS, 4165 DAG.getCondCode(CCOpcode)); 4166 if (VT.bitsEq(CCVT)) 4167 return SetCC; 4168 return DAG.getZExtOrTrunc(SetCC, DL, VT); 4169 } 4170 4171 static SDValue lowerFCMPIntrinsic(const SITargetLowering &TLI, 4172 SDNode *N, SelectionDAG &DAG) { 4173 EVT VT = N->getValueType(0); 4174 const auto *CD = cast<ConstantSDNode>(N->getOperand(3)); 4175 4176 int CondCode = CD->getSExtValue(); 4177 if (CondCode < FCmpInst::Predicate::FIRST_FCMP_PREDICATE || 4178 CondCode > FCmpInst::Predicate::LAST_FCMP_PREDICATE) { 4179 return DAG.getUNDEF(VT); 4180 } 4181 4182 SDValue Src0 = N->getOperand(1); 4183 SDValue Src1 = N->getOperand(2); 4184 EVT CmpVT = Src0.getValueType(); 4185 SDLoc SL(N); 4186 4187 if (CmpVT == MVT::f16 && !TLI.isTypeLegal(CmpVT)) { 4188 Src0 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src0); 4189 Src1 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src1); 4190 } 4191 4192 FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode); 4193 ISD::CondCode CCOpcode = getFCmpCondCode(IcInput); 4194 unsigned WavefrontSize = TLI.getSubtarget()->getWavefrontSize(); 4195 EVT CCVT = EVT::getIntegerVT(*DAG.getContext(), WavefrontSize); 4196 SDValue SetCC = DAG.getNode(AMDGPUISD::SETCC, SL, CCVT, Src0, 4197 Src1, DAG.getCondCode(CCOpcode)); 4198 if (VT.bitsEq(CCVT)) 4199 return SetCC; 4200 return DAG.getZExtOrTrunc(SetCC, SL, VT); 4201 } 4202 4203 void SITargetLowering::ReplaceNodeResults(SDNode *N, 4204 SmallVectorImpl<SDValue> &Results, 4205 SelectionDAG &DAG) const { 4206 switch (N->getOpcode()) { 4207 case ISD::INSERT_VECTOR_ELT: { 4208 if (SDValue Res = lowerINSERT_VECTOR_ELT(SDValue(N, 0), DAG)) 4209 Results.push_back(Res); 4210 return; 4211 } 4212 case ISD::EXTRACT_VECTOR_ELT: { 4213 if (SDValue Res = lowerEXTRACT_VECTOR_ELT(SDValue(N, 0), DAG)) 4214 Results.push_back(Res); 4215 return; 4216 } 4217 case ISD::INTRINSIC_WO_CHAIN: { 4218 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 4219 switch (IID) { 4220 case Intrinsic::amdgcn_cvt_pkrtz: { 4221 SDValue Src0 = N->getOperand(1); 4222 SDValue Src1 = N->getOperand(2); 4223 SDLoc SL(N); 4224 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_PKRTZ_F16_F32, SL, MVT::i32, 4225 Src0, Src1); 4226 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Cvt)); 4227 return; 4228 } 4229 case Intrinsic::amdgcn_cvt_pknorm_i16: 4230 case Intrinsic::amdgcn_cvt_pknorm_u16: 4231 case Intrinsic::amdgcn_cvt_pk_i16: 4232 case Intrinsic::amdgcn_cvt_pk_u16: { 4233 SDValue Src0 = N->getOperand(1); 4234 SDValue Src1 = N->getOperand(2); 4235 SDLoc SL(N); 4236 unsigned Opcode; 4237 4238 if (IID == Intrinsic::amdgcn_cvt_pknorm_i16) 4239 Opcode = AMDGPUISD::CVT_PKNORM_I16_F32; 4240 else if (IID == Intrinsic::amdgcn_cvt_pknorm_u16) 4241 Opcode = AMDGPUISD::CVT_PKNORM_U16_F32; 4242 else if (IID == Intrinsic::amdgcn_cvt_pk_i16) 4243 Opcode = AMDGPUISD::CVT_PK_I16_I32; 4244 else 4245 Opcode = AMDGPUISD::CVT_PK_U16_U32; 4246 4247 EVT VT = N->getValueType(0); 4248 if (isTypeLegal(VT)) 4249 Results.push_back(DAG.getNode(Opcode, SL, VT, Src0, Src1)); 4250 else { 4251 SDValue Cvt = DAG.getNode(Opcode, SL, MVT::i32, Src0, Src1); 4252 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2i16, Cvt)); 4253 } 4254 return; 4255 } 4256 } 4257 break; 4258 } 4259 case ISD::INTRINSIC_W_CHAIN: { 4260 if (SDValue Res = LowerINTRINSIC_W_CHAIN(SDValue(N, 0), DAG)) { 4261 if (Res.getOpcode() == ISD::MERGE_VALUES) { 4262 // FIXME: Hacky 4263 Results.push_back(Res.getOperand(0)); 4264 Results.push_back(Res.getOperand(1)); 4265 } else { 4266 Results.push_back(Res); 4267 Results.push_back(Res.getValue(1)); 4268 } 4269 return; 4270 } 4271 4272 break; 4273 } 4274 case ISD::SELECT: { 4275 SDLoc SL(N); 4276 EVT VT = N->getValueType(0); 4277 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT); 4278 SDValue LHS = DAG.getNode(ISD::BITCAST, SL, NewVT, N->getOperand(1)); 4279 SDValue RHS = DAG.getNode(ISD::BITCAST, SL, NewVT, N->getOperand(2)); 4280 4281 EVT SelectVT = NewVT; 4282 if (NewVT.bitsLT(MVT::i32)) { 4283 LHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, LHS); 4284 RHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, RHS); 4285 SelectVT = MVT::i32; 4286 } 4287 4288 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, SelectVT, 4289 N->getOperand(0), LHS, RHS); 4290 4291 if (NewVT != SelectVT) 4292 NewSelect = DAG.getNode(ISD::TRUNCATE, SL, NewVT, NewSelect); 4293 Results.push_back(DAG.getNode(ISD::BITCAST, SL, VT, NewSelect)); 4294 return; 4295 } 4296 case ISD::FNEG: { 4297 if (N->getValueType(0) != MVT::v2f16) 4298 break; 4299 4300 SDLoc SL(N); 4301 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::i32, N->getOperand(0)); 4302 4303 SDValue Op = DAG.getNode(ISD::XOR, SL, MVT::i32, 4304 BC, 4305 DAG.getConstant(0x80008000, SL, MVT::i32)); 4306 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Op)); 4307 return; 4308 } 4309 case ISD::FABS: { 4310 if (N->getValueType(0) != MVT::v2f16) 4311 break; 4312 4313 SDLoc SL(N); 4314 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::i32, N->getOperand(0)); 4315 4316 SDValue Op = DAG.getNode(ISD::AND, SL, MVT::i32, 4317 BC, 4318 DAG.getConstant(0x7fff7fff, SL, MVT::i32)); 4319 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Op)); 4320 return; 4321 } 4322 default: 4323 break; 4324 } 4325 } 4326 4327 /// Helper function for LowerBRCOND 4328 static SDNode *findUser(SDValue Value, unsigned Opcode) { 4329 4330 SDNode *Parent = Value.getNode(); 4331 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end(); 4332 I != E; ++I) { 4333 4334 if (I.getUse().get() != Value) 4335 continue; 4336 4337 if (I->getOpcode() == Opcode) 4338 return *I; 4339 } 4340 return nullptr; 4341 } 4342 4343 unsigned SITargetLowering::isCFIntrinsic(const SDNode *Intr) const { 4344 if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) { 4345 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) { 4346 case Intrinsic::amdgcn_if: 4347 return AMDGPUISD::IF; 4348 case Intrinsic::amdgcn_else: 4349 return AMDGPUISD::ELSE; 4350 case Intrinsic::amdgcn_loop: 4351 return AMDGPUISD::LOOP; 4352 case Intrinsic::amdgcn_end_cf: 4353 llvm_unreachable("should not occur"); 4354 default: 4355 return 0; 4356 } 4357 } 4358 4359 // break, if_break, else_break are all only used as inputs to loop, not 4360 // directly as branch conditions. 4361 return 0; 4362 } 4363 4364 bool SITargetLowering::shouldEmitFixup(const GlobalValue *GV) const { 4365 const Triple &TT = getTargetMachine().getTargetTriple(); 4366 return (GV->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS || 4367 GV->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT) && 4368 AMDGPU::shouldEmitConstantsToTextSection(TT); 4369 } 4370 4371 bool SITargetLowering::shouldEmitGOTReloc(const GlobalValue *GV) const { 4372 // FIXME: Either avoid relying on address space here or change the default 4373 // address space for functions to avoid the explicit check. 4374 return (GV->getValueType()->isFunctionTy() || 4375 GV->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS || 4376 GV->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS || 4377 GV->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT) && 4378 !shouldEmitFixup(GV) && 4379 !getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV); 4380 } 4381 4382 bool SITargetLowering::shouldEmitPCReloc(const GlobalValue *GV) const { 4383 return !shouldEmitFixup(GV) && !shouldEmitGOTReloc(GV); 4384 } 4385 4386 bool SITargetLowering::shouldUseLDSConstAddress(const GlobalValue *GV) const { 4387 if (!GV->hasExternalLinkage()) 4388 return true; 4389 4390 const auto OS = getTargetMachine().getTargetTriple().getOS(); 4391 return OS == Triple::AMDHSA || OS == Triple::AMDPAL; 4392 } 4393 4394 /// This transforms the control flow intrinsics to get the branch destination as 4395 /// last parameter, also switches branch target with BR if the need arise 4396 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND, 4397 SelectionDAG &DAG) const { 4398 SDLoc DL(BRCOND); 4399 4400 SDNode *Intr = BRCOND.getOperand(1).getNode(); 4401 SDValue Target = BRCOND.getOperand(2); 4402 SDNode *BR = nullptr; 4403 SDNode *SetCC = nullptr; 4404 4405 if (Intr->getOpcode() == ISD::SETCC) { 4406 // As long as we negate the condition everything is fine 4407 SetCC = Intr; 4408 Intr = SetCC->getOperand(0).getNode(); 4409 4410 } else { 4411 // Get the target from BR if we don't negate the condition 4412 BR = findUser(BRCOND, ISD::BR); 4413 Target = BR->getOperand(1); 4414 } 4415 4416 // FIXME: This changes the types of the intrinsics instead of introducing new 4417 // nodes with the correct types. 4418 // e.g. llvm.amdgcn.loop 4419 4420 // eg: i1,ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3 4421 // => t9: ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3, BasicBlock:ch<bb1 0x7fee5286d088> 4422 4423 unsigned CFNode = isCFIntrinsic(Intr); 4424 if (CFNode == 0) { 4425 // This is a uniform branch so we don't need to legalize. 4426 return BRCOND; 4427 } 4428 4429 bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID || 4430 Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN; 4431 4432 assert(!SetCC || 4433 (SetCC->getConstantOperandVal(1) == 1 && 4434 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() == 4435 ISD::SETNE)); 4436 4437 // operands of the new intrinsic call 4438 SmallVector<SDValue, 4> Ops; 4439 if (HaveChain) 4440 Ops.push_back(BRCOND.getOperand(0)); 4441 4442 Ops.append(Intr->op_begin() + (HaveChain ? 2 : 1), Intr->op_end()); 4443 Ops.push_back(Target); 4444 4445 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end()); 4446 4447 // build the new intrinsic call 4448 SDNode *Result = DAG.getNode(CFNode, DL, DAG.getVTList(Res), Ops).getNode(); 4449 4450 if (!HaveChain) { 4451 SDValue Ops[] = { 4452 SDValue(Result, 0), 4453 BRCOND.getOperand(0) 4454 }; 4455 4456 Result = DAG.getMergeValues(Ops, DL).getNode(); 4457 } 4458 4459 if (BR) { 4460 // Give the branch instruction our target 4461 SDValue Ops[] = { 4462 BR->getOperand(0), 4463 BRCOND.getOperand(2) 4464 }; 4465 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops); 4466 DAG.ReplaceAllUsesWith(BR, NewBR.getNode()); 4467 BR = NewBR.getNode(); 4468 } 4469 4470 SDValue Chain = SDValue(Result, Result->getNumValues() - 1); 4471 4472 // Copy the intrinsic results to registers 4473 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) { 4474 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg); 4475 if (!CopyToReg) 4476 continue; 4477 4478 Chain = DAG.getCopyToReg( 4479 Chain, DL, 4480 CopyToReg->getOperand(1), 4481 SDValue(Result, i - 1), 4482 SDValue()); 4483 4484 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0)); 4485 } 4486 4487 // Remove the old intrinsic from the chain 4488 DAG.ReplaceAllUsesOfValueWith( 4489 SDValue(Intr, Intr->getNumValues() - 1), 4490 Intr->getOperand(0)); 4491 4492 return Chain; 4493 } 4494 4495 SDValue SITargetLowering::LowerRETURNADDR(SDValue Op, 4496 SelectionDAG &DAG) const { 4497 MVT VT = Op.getSimpleValueType(); 4498 SDLoc DL(Op); 4499 // Checking the depth 4500 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() != 0) 4501 return DAG.getConstant(0, DL, VT); 4502 4503 MachineFunction &MF = DAG.getMachineFunction(); 4504 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 4505 // Check for kernel and shader functions 4506 if (Info->isEntryFunction()) 4507 return DAG.getConstant(0, DL, VT); 4508 4509 MachineFrameInfo &MFI = MF.getFrameInfo(); 4510 // There is a call to @llvm.returnaddress in this function 4511 MFI.setReturnAddressIsTaken(true); 4512 4513 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 4514 // Get the return address reg and mark it as an implicit live-in 4515 unsigned Reg = MF.addLiveIn(TRI->getReturnAddressReg(MF), getRegClassFor(VT, Op.getNode()->isDivergent())); 4516 4517 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, VT); 4518 } 4519 4520 SDValue SITargetLowering::getFPExtOrFPTrunc(SelectionDAG &DAG, 4521 SDValue Op, 4522 const SDLoc &DL, 4523 EVT VT) const { 4524 return Op.getValueType().bitsLE(VT) ? 4525 DAG.getNode(ISD::FP_EXTEND, DL, VT, Op) : 4526 DAG.getNode(ISD::FTRUNC, DL, VT, Op); 4527 } 4528 4529 SDValue SITargetLowering::lowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const { 4530 assert(Op.getValueType() == MVT::f16 && 4531 "Do not know how to custom lower FP_ROUND for non-f16 type"); 4532 4533 SDValue Src = Op.getOperand(0); 4534 EVT SrcVT = Src.getValueType(); 4535 if (SrcVT != MVT::f64) 4536 return Op; 4537 4538 SDLoc DL(Op); 4539 4540 SDValue FpToFp16 = DAG.getNode(ISD::FP_TO_FP16, DL, MVT::i32, Src); 4541 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FpToFp16); 4542 return DAG.getNode(ISD::BITCAST, DL, MVT::f16, Trunc); 4543 } 4544 4545 SDValue SITargetLowering::lowerFMINNUM_FMAXNUM(SDValue Op, 4546 SelectionDAG &DAG) const { 4547 EVT VT = Op.getValueType(); 4548 const MachineFunction &MF = DAG.getMachineFunction(); 4549 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 4550 bool IsIEEEMode = Info->getMode().IEEE; 4551 4552 // FIXME: Assert during eslection that this is only selected for 4553 // ieee_mode. Currently a combine can produce the ieee version for non-ieee 4554 // mode functions, but this happens to be OK since it's only done in cases 4555 // where there is known no sNaN. 4556 if (IsIEEEMode) 4557 return expandFMINNUM_FMAXNUM(Op.getNode(), DAG); 4558 4559 if (VT == MVT::v4f16) 4560 return splitBinaryVectorOp(Op, DAG); 4561 return Op; 4562 } 4563 4564 SDValue SITargetLowering::lowerTRAP(SDValue Op, SelectionDAG &DAG) const { 4565 SDLoc SL(Op); 4566 SDValue Chain = Op.getOperand(0); 4567 4568 if (Subtarget->getTrapHandlerAbi() != GCNSubtarget::TrapHandlerAbiHsa || 4569 !Subtarget->isTrapHandlerEnabled()) 4570 return DAG.getNode(AMDGPUISD::ENDPGM, SL, MVT::Other, Chain); 4571 4572 MachineFunction &MF = DAG.getMachineFunction(); 4573 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 4574 unsigned UserSGPR = Info->getQueuePtrUserSGPR(); 4575 assert(UserSGPR != AMDGPU::NoRegister); 4576 SDValue QueuePtr = CreateLiveInRegister( 4577 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64); 4578 SDValue SGPR01 = DAG.getRegister(AMDGPU::SGPR0_SGPR1, MVT::i64); 4579 SDValue ToReg = DAG.getCopyToReg(Chain, SL, SGPR01, 4580 QueuePtr, SDValue()); 4581 SDValue Ops[] = { 4582 ToReg, 4583 DAG.getTargetConstant(GCNSubtarget::TrapIDLLVMTrap, SL, MVT::i16), 4584 SGPR01, 4585 ToReg.getValue(1) 4586 }; 4587 return DAG.getNode(AMDGPUISD::TRAP, SL, MVT::Other, Ops); 4588 } 4589 4590 SDValue SITargetLowering::lowerDEBUGTRAP(SDValue Op, SelectionDAG &DAG) const { 4591 SDLoc SL(Op); 4592 SDValue Chain = Op.getOperand(0); 4593 MachineFunction &MF = DAG.getMachineFunction(); 4594 4595 if (Subtarget->getTrapHandlerAbi() != GCNSubtarget::TrapHandlerAbiHsa || 4596 !Subtarget->isTrapHandlerEnabled()) { 4597 DiagnosticInfoUnsupported NoTrap(MF.getFunction(), 4598 "debugtrap handler not supported", 4599 Op.getDebugLoc(), 4600 DS_Warning); 4601 LLVMContext &Ctx = MF.getFunction().getContext(); 4602 Ctx.diagnose(NoTrap); 4603 return Chain; 4604 } 4605 4606 SDValue Ops[] = { 4607 Chain, 4608 DAG.getTargetConstant(GCNSubtarget::TrapIDLLVMDebugTrap, SL, MVT::i16) 4609 }; 4610 return DAG.getNode(AMDGPUISD::TRAP, SL, MVT::Other, Ops); 4611 } 4612 4613 SDValue SITargetLowering::getSegmentAperture(unsigned AS, const SDLoc &DL, 4614 SelectionDAG &DAG) const { 4615 // FIXME: Use inline constants (src_{shared, private}_base) instead. 4616 if (Subtarget->hasApertureRegs()) { 4617 unsigned Offset = AS == AMDGPUAS::LOCAL_ADDRESS ? 4618 AMDGPU::Hwreg::OFFSET_SRC_SHARED_BASE : 4619 AMDGPU::Hwreg::OFFSET_SRC_PRIVATE_BASE; 4620 unsigned WidthM1 = AS == AMDGPUAS::LOCAL_ADDRESS ? 4621 AMDGPU::Hwreg::WIDTH_M1_SRC_SHARED_BASE : 4622 AMDGPU::Hwreg::WIDTH_M1_SRC_PRIVATE_BASE; 4623 unsigned Encoding = 4624 AMDGPU::Hwreg::ID_MEM_BASES << AMDGPU::Hwreg::ID_SHIFT_ | 4625 Offset << AMDGPU::Hwreg::OFFSET_SHIFT_ | 4626 WidthM1 << AMDGPU::Hwreg::WIDTH_M1_SHIFT_; 4627 4628 SDValue EncodingImm = DAG.getTargetConstant(Encoding, DL, MVT::i16); 4629 SDValue ApertureReg = SDValue( 4630 DAG.getMachineNode(AMDGPU::S_GETREG_B32, DL, MVT::i32, EncodingImm), 0); 4631 SDValue ShiftAmount = DAG.getTargetConstant(WidthM1 + 1, DL, MVT::i32); 4632 return DAG.getNode(ISD::SHL, DL, MVT::i32, ApertureReg, ShiftAmount); 4633 } 4634 4635 MachineFunction &MF = DAG.getMachineFunction(); 4636 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 4637 unsigned UserSGPR = Info->getQueuePtrUserSGPR(); 4638 assert(UserSGPR != AMDGPU::NoRegister); 4639 4640 SDValue QueuePtr = CreateLiveInRegister( 4641 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64); 4642 4643 // Offset into amd_queue_t for group_segment_aperture_base_hi / 4644 // private_segment_aperture_base_hi. 4645 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44; 4646 4647 SDValue Ptr = DAG.getObjectPtrOffset(DL, QueuePtr, StructOffset); 4648 4649 // TODO: Use custom target PseudoSourceValue. 4650 // TODO: We should use the value from the IR intrinsic call, but it might not 4651 // be available and how do we get it? 4652 MachinePointerInfo PtrInfo(AMDGPUAS::CONSTANT_ADDRESS); 4653 return DAG.getLoad(MVT::i32, DL, QueuePtr.getValue(1), Ptr, PtrInfo, 4654 MinAlign(64, StructOffset), 4655 MachineMemOperand::MODereferenceable | 4656 MachineMemOperand::MOInvariant); 4657 } 4658 4659 SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op, 4660 SelectionDAG &DAG) const { 4661 SDLoc SL(Op); 4662 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op); 4663 4664 SDValue Src = ASC->getOperand(0); 4665 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64); 4666 4667 const AMDGPUTargetMachine &TM = 4668 static_cast<const AMDGPUTargetMachine &>(getTargetMachine()); 4669 4670 // flat -> local/private 4671 if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) { 4672 unsigned DestAS = ASC->getDestAddressSpace(); 4673 4674 if (DestAS == AMDGPUAS::LOCAL_ADDRESS || 4675 DestAS == AMDGPUAS::PRIVATE_ADDRESS) { 4676 unsigned NullVal = TM.getNullPointerValue(DestAS); 4677 SDValue SegmentNullPtr = DAG.getConstant(NullVal, SL, MVT::i32); 4678 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE); 4679 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src); 4680 4681 return DAG.getNode(ISD::SELECT, SL, MVT::i32, 4682 NonNull, Ptr, SegmentNullPtr); 4683 } 4684 } 4685 4686 // local/private -> flat 4687 if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) { 4688 unsigned SrcAS = ASC->getSrcAddressSpace(); 4689 4690 if (SrcAS == AMDGPUAS::LOCAL_ADDRESS || 4691 SrcAS == AMDGPUAS::PRIVATE_ADDRESS) { 4692 unsigned NullVal = TM.getNullPointerValue(SrcAS); 4693 SDValue SegmentNullPtr = DAG.getConstant(NullVal, SL, MVT::i32); 4694 4695 SDValue NonNull 4696 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE); 4697 4698 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), SL, DAG); 4699 SDValue CvtPtr 4700 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture); 4701 4702 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull, 4703 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr), 4704 FlatNullPtr); 4705 } 4706 } 4707 4708 // global <-> flat are no-ops and never emitted. 4709 4710 const MachineFunction &MF = DAG.getMachineFunction(); 4711 DiagnosticInfoUnsupported InvalidAddrSpaceCast( 4712 MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc()); 4713 DAG.getContext()->diagnose(InvalidAddrSpaceCast); 4714 4715 return DAG.getUNDEF(ASC->getValueType(0)); 4716 } 4717 4718 // This lowers an INSERT_SUBVECTOR by extracting the individual elements from 4719 // the small vector and inserting them into the big vector. That is better than 4720 // the default expansion of doing it via a stack slot. Even though the use of 4721 // the stack slot would be optimized away afterwards, the stack slot itself 4722 // remains. 4723 SDValue SITargetLowering::lowerINSERT_SUBVECTOR(SDValue Op, 4724 SelectionDAG &DAG) const { 4725 SDValue Vec = Op.getOperand(0); 4726 SDValue Ins = Op.getOperand(1); 4727 SDValue Idx = Op.getOperand(2); 4728 EVT VecVT = Vec.getValueType(); 4729 EVT InsVT = Ins.getValueType(); 4730 EVT EltVT = VecVT.getVectorElementType(); 4731 unsigned InsNumElts = InsVT.getVectorNumElements(); 4732 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue(); 4733 SDLoc SL(Op); 4734 4735 for (unsigned I = 0; I != InsNumElts; ++I) { 4736 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Ins, 4737 DAG.getConstant(I, SL, MVT::i32)); 4738 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, VecVT, Vec, Elt, 4739 DAG.getConstant(IdxVal + I, SL, MVT::i32)); 4740 } 4741 return Vec; 4742 } 4743 4744 SDValue SITargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op, 4745 SelectionDAG &DAG) const { 4746 SDValue Vec = Op.getOperand(0); 4747 SDValue InsVal = Op.getOperand(1); 4748 SDValue Idx = Op.getOperand(2); 4749 EVT VecVT = Vec.getValueType(); 4750 EVT EltVT = VecVT.getVectorElementType(); 4751 unsigned VecSize = VecVT.getSizeInBits(); 4752 unsigned EltSize = EltVT.getSizeInBits(); 4753 4754 4755 assert(VecSize <= 64); 4756 4757 unsigned NumElts = VecVT.getVectorNumElements(); 4758 SDLoc SL(Op); 4759 auto KIdx = dyn_cast<ConstantSDNode>(Idx); 4760 4761 if (NumElts == 4 && EltSize == 16 && KIdx) { 4762 SDValue BCVec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Vec); 4763 4764 SDValue LoHalf = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BCVec, 4765 DAG.getConstant(0, SL, MVT::i32)); 4766 SDValue HiHalf = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BCVec, 4767 DAG.getConstant(1, SL, MVT::i32)); 4768 4769 SDValue LoVec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i16, LoHalf); 4770 SDValue HiVec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i16, HiHalf); 4771 4772 unsigned Idx = KIdx->getZExtValue(); 4773 bool InsertLo = Idx < 2; 4774 SDValue InsHalf = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, MVT::v2i16, 4775 InsertLo ? LoVec : HiVec, 4776 DAG.getNode(ISD::BITCAST, SL, MVT::i16, InsVal), 4777 DAG.getConstant(InsertLo ? Idx : (Idx - 2), SL, MVT::i32)); 4778 4779 InsHalf = DAG.getNode(ISD::BITCAST, SL, MVT::i32, InsHalf); 4780 4781 SDValue Concat = InsertLo ? 4782 DAG.getBuildVector(MVT::v2i32, SL, { InsHalf, HiHalf }) : 4783 DAG.getBuildVector(MVT::v2i32, SL, { LoHalf, InsHalf }); 4784 4785 return DAG.getNode(ISD::BITCAST, SL, VecVT, Concat); 4786 } 4787 4788 if (isa<ConstantSDNode>(Idx)) 4789 return SDValue(); 4790 4791 MVT IntVT = MVT::getIntegerVT(VecSize); 4792 4793 // Avoid stack access for dynamic indexing. 4794 // v_bfi_b32 (v_bfm_b32 16, (shl idx, 16)), val, vec 4795 4796 // Create a congruent vector with the target value in each element so that 4797 // the required element can be masked and ORed into the target vector. 4798 SDValue ExtVal = DAG.getNode(ISD::BITCAST, SL, IntVT, 4799 DAG.getSplatBuildVector(VecVT, SL, InsVal)); 4800 4801 assert(isPowerOf2_32(EltSize)); 4802 SDValue ScaleFactor = DAG.getConstant(Log2_32(EltSize), SL, MVT::i32); 4803 4804 // Convert vector index to bit-index. 4805 SDValue ScaledIdx = DAG.getNode(ISD::SHL, SL, MVT::i32, Idx, ScaleFactor); 4806 4807 SDValue BCVec = DAG.getNode(ISD::BITCAST, SL, IntVT, Vec); 4808 SDValue BFM = DAG.getNode(ISD::SHL, SL, IntVT, 4809 DAG.getConstant(0xffff, SL, IntVT), 4810 ScaledIdx); 4811 4812 SDValue LHS = DAG.getNode(ISD::AND, SL, IntVT, BFM, ExtVal); 4813 SDValue RHS = DAG.getNode(ISD::AND, SL, IntVT, 4814 DAG.getNOT(SL, BFM, IntVT), BCVec); 4815 4816 SDValue BFI = DAG.getNode(ISD::OR, SL, IntVT, LHS, RHS); 4817 return DAG.getNode(ISD::BITCAST, SL, VecVT, BFI); 4818 } 4819 4820 SDValue SITargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op, 4821 SelectionDAG &DAG) const { 4822 SDLoc SL(Op); 4823 4824 EVT ResultVT = Op.getValueType(); 4825 SDValue Vec = Op.getOperand(0); 4826 SDValue Idx = Op.getOperand(1); 4827 EVT VecVT = Vec.getValueType(); 4828 unsigned VecSize = VecVT.getSizeInBits(); 4829 EVT EltVT = VecVT.getVectorElementType(); 4830 assert(VecSize <= 64); 4831 4832 DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr); 4833 4834 // Make sure we do any optimizations that will make it easier to fold 4835 // source modifiers before obscuring it with bit operations. 4836 4837 // XXX - Why doesn't this get called when vector_shuffle is expanded? 4838 if (SDValue Combined = performExtractVectorEltCombine(Op.getNode(), DCI)) 4839 return Combined; 4840 4841 unsigned EltSize = EltVT.getSizeInBits(); 4842 assert(isPowerOf2_32(EltSize)); 4843 4844 MVT IntVT = MVT::getIntegerVT(VecSize); 4845 SDValue ScaleFactor = DAG.getConstant(Log2_32(EltSize), SL, MVT::i32); 4846 4847 // Convert vector index to bit-index (* EltSize) 4848 SDValue ScaledIdx = DAG.getNode(ISD::SHL, SL, MVT::i32, Idx, ScaleFactor); 4849 4850 SDValue BC = DAG.getNode(ISD::BITCAST, SL, IntVT, Vec); 4851 SDValue Elt = DAG.getNode(ISD::SRL, SL, IntVT, BC, ScaledIdx); 4852 4853 if (ResultVT == MVT::f16) { 4854 SDValue Result = DAG.getNode(ISD::TRUNCATE, SL, MVT::i16, Elt); 4855 return DAG.getNode(ISD::BITCAST, SL, ResultVT, Result); 4856 } 4857 4858 return DAG.getAnyExtOrTrunc(Elt, SL, ResultVT); 4859 } 4860 4861 static bool elementPairIsContiguous(ArrayRef<int> Mask, int Elt) { 4862 assert(Elt % 2 == 0); 4863 return Mask[Elt + 1] == Mask[Elt] + 1 && (Mask[Elt] % 2 == 0); 4864 } 4865 4866 SDValue SITargetLowering::lowerVECTOR_SHUFFLE(SDValue Op, 4867 SelectionDAG &DAG) const { 4868 SDLoc SL(Op); 4869 EVT ResultVT = Op.getValueType(); 4870 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op); 4871 4872 EVT PackVT = ResultVT.isInteger() ? MVT::v2i16 : MVT::v2f16; 4873 EVT EltVT = PackVT.getVectorElementType(); 4874 int SrcNumElts = Op.getOperand(0).getValueType().getVectorNumElements(); 4875 4876 // vector_shuffle <0,1,6,7> lhs, rhs 4877 // -> concat_vectors (extract_subvector lhs, 0), (extract_subvector rhs, 2) 4878 // 4879 // vector_shuffle <6,7,2,3> lhs, rhs 4880 // -> concat_vectors (extract_subvector rhs, 2), (extract_subvector lhs, 2) 4881 // 4882 // vector_shuffle <6,7,0,1> lhs, rhs 4883 // -> concat_vectors (extract_subvector rhs, 2), (extract_subvector lhs, 0) 4884 4885 // Avoid scalarizing when both halves are reading from consecutive elements. 4886 SmallVector<SDValue, 4> Pieces; 4887 for (int I = 0, N = ResultVT.getVectorNumElements(); I != N; I += 2) { 4888 if (elementPairIsContiguous(SVN->getMask(), I)) { 4889 const int Idx = SVN->getMaskElt(I); 4890 int VecIdx = Idx < SrcNumElts ? 0 : 1; 4891 int EltIdx = Idx < SrcNumElts ? Idx : Idx - SrcNumElts; 4892 SDValue SubVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, 4893 PackVT, SVN->getOperand(VecIdx), 4894 DAG.getConstant(EltIdx, SL, MVT::i32)); 4895 Pieces.push_back(SubVec); 4896 } else { 4897 const int Idx0 = SVN->getMaskElt(I); 4898 const int Idx1 = SVN->getMaskElt(I + 1); 4899 int VecIdx0 = Idx0 < SrcNumElts ? 0 : 1; 4900 int VecIdx1 = Idx1 < SrcNumElts ? 0 : 1; 4901 int EltIdx0 = Idx0 < SrcNumElts ? Idx0 : Idx0 - SrcNumElts; 4902 int EltIdx1 = Idx1 < SrcNumElts ? Idx1 : Idx1 - SrcNumElts; 4903 4904 SDValue Vec0 = SVN->getOperand(VecIdx0); 4905 SDValue Elt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, 4906 Vec0, DAG.getConstant(EltIdx0, SL, MVT::i32)); 4907 4908 SDValue Vec1 = SVN->getOperand(VecIdx1); 4909 SDValue Elt1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, 4910 Vec1, DAG.getConstant(EltIdx1, SL, MVT::i32)); 4911 Pieces.push_back(DAG.getBuildVector(PackVT, SL, { Elt0, Elt1 })); 4912 } 4913 } 4914 4915 return DAG.getNode(ISD::CONCAT_VECTORS, SL, ResultVT, Pieces); 4916 } 4917 4918 SDValue SITargetLowering::lowerBUILD_VECTOR(SDValue Op, 4919 SelectionDAG &DAG) const { 4920 SDLoc SL(Op); 4921 EVT VT = Op.getValueType(); 4922 4923 if (VT == MVT::v4i16 || VT == MVT::v4f16) { 4924 EVT HalfVT = MVT::getVectorVT(VT.getVectorElementType().getSimpleVT(), 2); 4925 4926 // Turn into pair of packed build_vectors. 4927 // TODO: Special case for constants that can be materialized with s_mov_b64. 4928 SDValue Lo = DAG.getBuildVector(HalfVT, SL, 4929 { Op.getOperand(0), Op.getOperand(1) }); 4930 SDValue Hi = DAG.getBuildVector(HalfVT, SL, 4931 { Op.getOperand(2), Op.getOperand(3) }); 4932 4933 SDValue CastLo = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Lo); 4934 SDValue CastHi = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Hi); 4935 4936 SDValue Blend = DAG.getBuildVector(MVT::v2i32, SL, { CastLo, CastHi }); 4937 return DAG.getNode(ISD::BITCAST, SL, VT, Blend); 4938 } 4939 4940 assert(VT == MVT::v2f16 || VT == MVT::v2i16); 4941 assert(!Subtarget->hasVOP3PInsts() && "this should be legal"); 4942 4943 SDValue Lo = Op.getOperand(0); 4944 SDValue Hi = Op.getOperand(1); 4945 4946 // Avoid adding defined bits with the zero_extend. 4947 if (Hi.isUndef()) { 4948 Lo = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Lo); 4949 SDValue ExtLo = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, Lo); 4950 return DAG.getNode(ISD::BITCAST, SL, VT, ExtLo); 4951 } 4952 4953 Hi = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Hi); 4954 Hi = DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i32, Hi); 4955 4956 SDValue ShlHi = DAG.getNode(ISD::SHL, SL, MVT::i32, Hi, 4957 DAG.getConstant(16, SL, MVT::i32)); 4958 if (Lo.isUndef()) 4959 return DAG.getNode(ISD::BITCAST, SL, VT, ShlHi); 4960 4961 Lo = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Lo); 4962 Lo = DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i32, Lo); 4963 4964 SDValue Or = DAG.getNode(ISD::OR, SL, MVT::i32, Lo, ShlHi); 4965 return DAG.getNode(ISD::BITCAST, SL, VT, Or); 4966 } 4967 4968 bool 4969 SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 4970 // We can fold offsets for anything that doesn't require a GOT relocation. 4971 return (GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS || 4972 GA->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS || 4973 GA->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT) && 4974 !shouldEmitGOTReloc(GA->getGlobal()); 4975 } 4976 4977 static SDValue 4978 buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV, 4979 const SDLoc &DL, unsigned Offset, EVT PtrVT, 4980 unsigned GAFlags = SIInstrInfo::MO_NONE) { 4981 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is 4982 // lowered to the following code sequence: 4983 // 4984 // For constant address space: 4985 // s_getpc_b64 s[0:1] 4986 // s_add_u32 s0, s0, $symbol 4987 // s_addc_u32 s1, s1, 0 4988 // 4989 // s_getpc_b64 returns the address of the s_add_u32 instruction and then 4990 // a fixup or relocation is emitted to replace $symbol with a literal 4991 // constant, which is a pc-relative offset from the encoding of the $symbol 4992 // operand to the global variable. 4993 // 4994 // For global address space: 4995 // s_getpc_b64 s[0:1] 4996 // s_add_u32 s0, s0, $symbol@{gotpc}rel32@lo 4997 // s_addc_u32 s1, s1, $symbol@{gotpc}rel32@hi 4998 // 4999 // s_getpc_b64 returns the address of the s_add_u32 instruction and then 5000 // fixups or relocations are emitted to replace $symbol@*@lo and 5001 // $symbol@*@hi with lower 32 bits and higher 32 bits of a literal constant, 5002 // which is a 64-bit pc-relative offset from the encoding of the $symbol 5003 // operand to the global variable. 5004 // 5005 // What we want here is an offset from the value returned by s_getpc 5006 // (which is the address of the s_add_u32 instruction) to the global 5007 // variable, but since the encoding of $symbol starts 4 bytes after the start 5008 // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too 5009 // small. This requires us to add 4 to the global variable offset in order to 5010 // compute the correct address. 5011 SDValue PtrLo = 5012 DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4, GAFlags); 5013 SDValue PtrHi; 5014 if (GAFlags == SIInstrInfo::MO_NONE) { 5015 PtrHi = DAG.getTargetConstant(0, DL, MVT::i32); 5016 } else { 5017 PtrHi = 5018 DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4, GAFlags + 1); 5019 } 5020 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, PtrLo, PtrHi); 5021 } 5022 5023 SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI, 5024 SDValue Op, 5025 SelectionDAG &DAG) const { 5026 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op); 5027 const GlobalValue *GV = GSD->getGlobal(); 5028 if ((GSD->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && 5029 shouldUseLDSConstAddress(GV)) || 5030 GSD->getAddressSpace() == AMDGPUAS::REGION_ADDRESS || 5031 GSD->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) 5032 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG); 5033 5034 SDLoc DL(GSD); 5035 EVT PtrVT = Op.getValueType(); 5036 5037 if (GSD->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) { 5038 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, GSD->getOffset(), 5039 SIInstrInfo::MO_ABS32_LO); 5040 return DAG.getNode(AMDGPUISD::LDS, DL, MVT::i32, GA); 5041 } 5042 5043 if (shouldEmitFixup(GV)) 5044 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT); 5045 else if (shouldEmitPCReloc(GV)) 5046 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT, 5047 SIInstrInfo::MO_REL32); 5048 5049 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT, 5050 SIInstrInfo::MO_GOTPCREL32); 5051 5052 Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext()); 5053 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS); 5054 const DataLayout &DataLayout = DAG.getDataLayout(); 5055 unsigned Align = DataLayout.getABITypeAlignment(PtrTy); 5056 MachinePointerInfo PtrInfo 5057 = MachinePointerInfo::getGOT(DAG.getMachineFunction()); 5058 5059 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align, 5060 MachineMemOperand::MODereferenceable | 5061 MachineMemOperand::MOInvariant); 5062 } 5063 5064 SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, 5065 const SDLoc &DL, SDValue V) const { 5066 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as 5067 // the destination register. 5068 // 5069 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions, 5070 // so we will end up with redundant moves to m0. 5071 // 5072 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result. 5073 5074 // A Null SDValue creates a glue result. 5075 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue, 5076 V, Chain); 5077 return SDValue(M0, 0); 5078 } 5079 5080 SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG, 5081 SDValue Op, 5082 MVT VT, 5083 unsigned Offset) const { 5084 SDLoc SL(Op); 5085 SDValue Param = lowerKernargMemParameter(DAG, MVT::i32, MVT::i32, SL, 5086 DAG.getEntryNode(), Offset, 4, false); 5087 // The local size values will have the hi 16-bits as zero. 5088 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param, 5089 DAG.getValueType(VT)); 5090 } 5091 5092 static SDValue emitNonHSAIntrinsicError(SelectionDAG &DAG, const SDLoc &DL, 5093 EVT VT) { 5094 DiagnosticInfoUnsupported BadIntrin(DAG.getMachineFunction().getFunction(), 5095 "non-hsa intrinsic with hsa target", 5096 DL.getDebugLoc()); 5097 DAG.getContext()->diagnose(BadIntrin); 5098 return DAG.getUNDEF(VT); 5099 } 5100 5101 static SDValue emitRemovedIntrinsicError(SelectionDAG &DAG, const SDLoc &DL, 5102 EVT VT) { 5103 DiagnosticInfoUnsupported BadIntrin(DAG.getMachineFunction().getFunction(), 5104 "intrinsic not supported on subtarget", 5105 DL.getDebugLoc()); 5106 DAG.getContext()->diagnose(BadIntrin); 5107 return DAG.getUNDEF(VT); 5108 } 5109 5110 static SDValue getBuildDwordsVector(SelectionDAG &DAG, SDLoc DL, 5111 ArrayRef<SDValue> Elts) { 5112 assert(!Elts.empty()); 5113 MVT Type; 5114 unsigned NumElts; 5115 5116 if (Elts.size() == 1) { 5117 Type = MVT::f32; 5118 NumElts = 1; 5119 } else if (Elts.size() == 2) { 5120 Type = MVT::v2f32; 5121 NumElts = 2; 5122 } else if (Elts.size() == 3) { 5123 Type = MVT::v3f32; 5124 NumElts = 3; 5125 } else if (Elts.size() <= 4) { 5126 Type = MVT::v4f32; 5127 NumElts = 4; 5128 } else if (Elts.size() <= 8) { 5129 Type = MVT::v8f32; 5130 NumElts = 8; 5131 } else { 5132 assert(Elts.size() <= 16); 5133 Type = MVT::v16f32; 5134 NumElts = 16; 5135 } 5136 5137 SmallVector<SDValue, 16> VecElts(NumElts); 5138 for (unsigned i = 0; i < Elts.size(); ++i) { 5139 SDValue Elt = Elts[i]; 5140 if (Elt.getValueType() != MVT::f32) 5141 Elt = DAG.getBitcast(MVT::f32, Elt); 5142 VecElts[i] = Elt; 5143 } 5144 for (unsigned i = Elts.size(); i < NumElts; ++i) 5145 VecElts[i] = DAG.getUNDEF(MVT::f32); 5146 5147 if (NumElts == 1) 5148 return VecElts[0]; 5149 return DAG.getBuildVector(Type, DL, VecElts); 5150 } 5151 5152 static bool parseCachePolicy(SDValue CachePolicy, SelectionDAG &DAG, 5153 SDValue *GLC, SDValue *SLC, SDValue *DLC) { 5154 auto CachePolicyConst = cast<ConstantSDNode>(CachePolicy.getNode()); 5155 5156 uint64_t Value = CachePolicyConst->getZExtValue(); 5157 SDLoc DL(CachePolicy); 5158 if (GLC) { 5159 *GLC = DAG.getTargetConstant((Value & 0x1) ? 1 : 0, DL, MVT::i32); 5160 Value &= ~(uint64_t)0x1; 5161 } 5162 if (SLC) { 5163 *SLC = DAG.getTargetConstant((Value & 0x2) ? 1 : 0, DL, MVT::i32); 5164 Value &= ~(uint64_t)0x2; 5165 } 5166 if (DLC) { 5167 *DLC = DAG.getTargetConstant((Value & 0x4) ? 1 : 0, DL, MVT::i32); 5168 Value &= ~(uint64_t)0x4; 5169 } 5170 5171 return Value == 0; 5172 } 5173 5174 static SDValue padEltsToUndef(SelectionDAG &DAG, const SDLoc &DL, EVT CastVT, 5175 SDValue Src, int ExtraElts) { 5176 EVT SrcVT = Src.getValueType(); 5177 5178 SmallVector<SDValue, 8> Elts; 5179 5180 if (SrcVT.isVector()) 5181 DAG.ExtractVectorElements(Src, Elts); 5182 else 5183 Elts.push_back(Src); 5184 5185 SDValue Undef = DAG.getUNDEF(SrcVT.getScalarType()); 5186 while (ExtraElts--) 5187 Elts.push_back(Undef); 5188 5189 return DAG.getBuildVector(CastVT, DL, Elts); 5190 } 5191 5192 // Re-construct the required return value for a image load intrinsic. 5193 // This is more complicated due to the optional use TexFailCtrl which means the required 5194 // return type is an aggregate 5195 static SDValue constructRetValue(SelectionDAG &DAG, 5196 MachineSDNode *Result, 5197 ArrayRef<EVT> ResultTypes, 5198 bool IsTexFail, bool Unpacked, bool IsD16, 5199 int DMaskPop, int NumVDataDwords, 5200 const SDLoc &DL, LLVMContext &Context) { 5201 // Determine the required return type. This is the same regardless of IsTexFail flag 5202 EVT ReqRetVT = ResultTypes[0]; 5203 int ReqRetNumElts = ReqRetVT.isVector() ? ReqRetVT.getVectorNumElements() : 1; 5204 int NumDataDwords = (!IsD16 || (IsD16 && Unpacked)) ? 5205 ReqRetNumElts : (ReqRetNumElts + 1) / 2; 5206 5207 int MaskPopDwords = (!IsD16 || (IsD16 && Unpacked)) ? 5208 DMaskPop : (DMaskPop + 1) / 2; 5209 5210 MVT DataDwordVT = NumDataDwords == 1 ? 5211 MVT::i32 : MVT::getVectorVT(MVT::i32, NumDataDwords); 5212 5213 MVT MaskPopVT = MaskPopDwords == 1 ? 5214 MVT::i32 : MVT::getVectorVT(MVT::i32, MaskPopDwords); 5215 5216 SDValue Data(Result, 0); 5217 SDValue TexFail; 5218 5219 if (IsTexFail) { 5220 SDValue ZeroIdx = DAG.getConstant(0, DL, MVT::i32); 5221 if (MaskPopVT.isVector()) { 5222 Data = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MaskPopVT, 5223 SDValue(Result, 0), ZeroIdx); 5224 } else { 5225 Data = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MaskPopVT, 5226 SDValue(Result, 0), ZeroIdx); 5227 } 5228 5229 TexFail = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, 5230 SDValue(Result, 0), 5231 DAG.getConstant(MaskPopDwords, DL, MVT::i32)); 5232 } 5233 5234 if (DataDwordVT.isVector()) 5235 Data = padEltsToUndef(DAG, DL, DataDwordVT, Data, 5236 NumDataDwords - MaskPopDwords); 5237 5238 if (IsD16) 5239 Data = adjustLoadValueTypeImpl(Data, ReqRetVT, DL, DAG, Unpacked); 5240 5241 if (!ReqRetVT.isVector()) 5242 Data = DAG.getNode(ISD::TRUNCATE, DL, ReqRetVT.changeTypeToInteger(), Data); 5243 5244 Data = DAG.getNode(ISD::BITCAST, DL, ReqRetVT, Data); 5245 5246 if (TexFail) 5247 return DAG.getMergeValues({Data, TexFail, SDValue(Result, 1)}, DL); 5248 5249 if (Result->getNumValues() == 1) 5250 return Data; 5251 5252 return DAG.getMergeValues({Data, SDValue(Result, 1)}, DL); 5253 } 5254 5255 static bool parseTexFail(SDValue TexFailCtrl, SelectionDAG &DAG, SDValue *TFE, 5256 SDValue *LWE, bool &IsTexFail) { 5257 auto TexFailCtrlConst = cast<ConstantSDNode>(TexFailCtrl.getNode()); 5258 5259 uint64_t Value = TexFailCtrlConst->getZExtValue(); 5260 if (Value) { 5261 IsTexFail = true; 5262 } 5263 5264 SDLoc DL(TexFailCtrlConst); 5265 *TFE = DAG.getTargetConstant((Value & 0x1) ? 1 : 0, DL, MVT::i32); 5266 Value &= ~(uint64_t)0x1; 5267 *LWE = DAG.getTargetConstant((Value & 0x2) ? 1 : 0, DL, MVT::i32); 5268 Value &= ~(uint64_t)0x2; 5269 5270 return Value == 0; 5271 } 5272 5273 SDValue SITargetLowering::lowerImage(SDValue Op, 5274 const AMDGPU::ImageDimIntrinsicInfo *Intr, 5275 SelectionDAG &DAG) const { 5276 SDLoc DL(Op); 5277 MachineFunction &MF = DAG.getMachineFunction(); 5278 const GCNSubtarget* ST = &MF.getSubtarget<GCNSubtarget>(); 5279 const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode = 5280 AMDGPU::getMIMGBaseOpcodeInfo(Intr->BaseOpcode); 5281 const AMDGPU::MIMGDimInfo *DimInfo = AMDGPU::getMIMGDimInfo(Intr->Dim); 5282 const AMDGPU::MIMGLZMappingInfo *LZMappingInfo = 5283 AMDGPU::getMIMGLZMappingInfo(Intr->BaseOpcode); 5284 const AMDGPU::MIMGMIPMappingInfo *MIPMappingInfo = 5285 AMDGPU::getMIMGMIPMappingInfo(Intr->BaseOpcode); 5286 unsigned IntrOpcode = Intr->BaseOpcode; 5287 bool IsGFX10 = Subtarget->getGeneration() >= AMDGPUSubtarget::GFX10; 5288 5289 SmallVector<EVT, 3> ResultTypes(Op->value_begin(), Op->value_end()); 5290 SmallVector<EVT, 3> OrigResultTypes(Op->value_begin(), Op->value_end()); 5291 bool IsD16 = false; 5292 bool IsA16 = false; 5293 SDValue VData; 5294 int NumVDataDwords; 5295 bool AdjustRetType = false; 5296 5297 unsigned AddrIdx; // Index of first address argument 5298 unsigned DMask; 5299 unsigned DMaskLanes = 0; 5300 5301 if (BaseOpcode->Atomic) { 5302 VData = Op.getOperand(2); 5303 5304 bool Is64Bit = VData.getValueType() == MVT::i64; 5305 if (BaseOpcode->AtomicX2) { 5306 SDValue VData2 = Op.getOperand(3); 5307 VData = DAG.getBuildVector(Is64Bit ? MVT::v2i64 : MVT::v2i32, DL, 5308 {VData, VData2}); 5309 if (Is64Bit) 5310 VData = DAG.getBitcast(MVT::v4i32, VData); 5311 5312 ResultTypes[0] = Is64Bit ? MVT::v2i64 : MVT::v2i32; 5313 DMask = Is64Bit ? 0xf : 0x3; 5314 NumVDataDwords = Is64Bit ? 4 : 2; 5315 AddrIdx = 4; 5316 } else { 5317 DMask = Is64Bit ? 0x3 : 0x1; 5318 NumVDataDwords = Is64Bit ? 2 : 1; 5319 AddrIdx = 3; 5320 } 5321 } else { 5322 unsigned DMaskIdx = BaseOpcode->Store ? 3 : isa<MemSDNode>(Op) ? 2 : 1; 5323 auto DMaskConst = cast<ConstantSDNode>(Op.getOperand(DMaskIdx)); 5324 DMask = DMaskConst->getZExtValue(); 5325 DMaskLanes = BaseOpcode->Gather4 ? 4 : countPopulation(DMask); 5326 5327 if (BaseOpcode->Store) { 5328 VData = Op.getOperand(2); 5329 5330 MVT StoreVT = VData.getSimpleValueType(); 5331 if (StoreVT.getScalarType() == MVT::f16) { 5332 if (!Subtarget->hasD16Images() || !BaseOpcode->HasD16) 5333 return Op; // D16 is unsupported for this instruction 5334 5335 IsD16 = true; 5336 VData = handleD16VData(VData, DAG); 5337 } 5338 5339 NumVDataDwords = (VData.getValueType().getSizeInBits() + 31) / 32; 5340 } else { 5341 // Work out the num dwords based on the dmask popcount and underlying type 5342 // and whether packing is supported. 5343 MVT LoadVT = ResultTypes[0].getSimpleVT(); 5344 if (LoadVT.getScalarType() == MVT::f16) { 5345 if (!Subtarget->hasD16Images() || !BaseOpcode->HasD16) 5346 return Op; // D16 is unsupported for this instruction 5347 5348 IsD16 = true; 5349 } 5350 5351 // Confirm that the return type is large enough for the dmask specified 5352 if ((LoadVT.isVector() && LoadVT.getVectorNumElements() < DMaskLanes) || 5353 (!LoadVT.isVector() && DMaskLanes > 1)) 5354 return Op; 5355 5356 if (IsD16 && !Subtarget->hasUnpackedD16VMem()) 5357 NumVDataDwords = (DMaskLanes + 1) / 2; 5358 else 5359 NumVDataDwords = DMaskLanes; 5360 5361 AdjustRetType = true; 5362 } 5363 5364 AddrIdx = DMaskIdx + 1; 5365 } 5366 5367 unsigned NumGradients = BaseOpcode->Gradients ? DimInfo->NumGradients : 0; 5368 unsigned NumCoords = BaseOpcode->Coordinates ? DimInfo->NumCoords : 0; 5369 unsigned NumLCM = BaseOpcode->LodOrClampOrMip ? 1 : 0; 5370 unsigned NumVAddrs = BaseOpcode->NumExtraArgs + NumGradients + 5371 NumCoords + NumLCM; 5372 unsigned NumMIVAddrs = NumVAddrs; 5373 5374 SmallVector<SDValue, 4> VAddrs; 5375 5376 // Optimize _L to _LZ when _L is zero 5377 if (LZMappingInfo) { 5378 if (auto ConstantLod = 5379 dyn_cast<ConstantFPSDNode>(Op.getOperand(AddrIdx+NumVAddrs-1))) { 5380 if (ConstantLod->isZero() || ConstantLod->isNegative()) { 5381 IntrOpcode = LZMappingInfo->LZ; // set new opcode to _lz variant of _l 5382 NumMIVAddrs--; // remove 'lod' 5383 } 5384 } 5385 } 5386 5387 // Optimize _mip away, when 'lod' is zero 5388 if (MIPMappingInfo) { 5389 if (auto ConstantLod = 5390 dyn_cast<ConstantSDNode>(Op.getOperand(AddrIdx+NumVAddrs-1))) { 5391 if (ConstantLod->isNullValue()) { 5392 IntrOpcode = MIPMappingInfo->NONMIP; // set new opcode to variant without _mip 5393 NumMIVAddrs--; // remove 'lod' 5394 } 5395 } 5396 } 5397 5398 // Check for 16 bit addresses and pack if true. 5399 unsigned DimIdx = AddrIdx + BaseOpcode->NumExtraArgs; 5400 MVT VAddrVT = Op.getOperand(DimIdx).getSimpleValueType(); 5401 const MVT VAddrScalarVT = VAddrVT.getScalarType(); 5402 if (((VAddrScalarVT == MVT::f16) || (VAddrScalarVT == MVT::i16))) { 5403 // Illegal to use a16 images 5404 if (!ST->hasFeature(AMDGPU::FeatureR128A16) && !ST->hasFeature(AMDGPU::FeatureGFX10A16)) 5405 return Op; 5406 5407 IsA16 = true; 5408 const MVT VectorVT = VAddrScalarVT == MVT::f16 ? MVT::v2f16 : MVT::v2i16; 5409 for (unsigned i = AddrIdx; i < (AddrIdx + NumMIVAddrs); ++i) { 5410 SDValue AddrLo; 5411 // Push back extra arguments. 5412 if (i < DimIdx) { 5413 AddrLo = Op.getOperand(i); 5414 } else { 5415 // Dz/dh, dz/dv and the last odd coord are packed with undef. Also, 5416 // in 1D, derivatives dx/dh and dx/dv are packed with undef. 5417 if (((i + 1) >= (AddrIdx + NumMIVAddrs)) || 5418 ((NumGradients / 2) % 2 == 1 && 5419 (i == DimIdx + (NumGradients / 2) - 1 || 5420 i == DimIdx + NumGradients - 1))) { 5421 AddrLo = Op.getOperand(i); 5422 if (AddrLo.getValueType() != MVT::i16) 5423 AddrLo = DAG.getBitcast(MVT::i16, Op.getOperand(i)); 5424 AddrLo = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, AddrLo); 5425 } else { 5426 AddrLo = DAG.getBuildVector(VectorVT, DL, 5427 {Op.getOperand(i), Op.getOperand(i + 1)}); 5428 i++; 5429 } 5430 AddrLo = DAG.getBitcast(MVT::f32, AddrLo); 5431 } 5432 VAddrs.push_back(AddrLo); 5433 } 5434 } else { 5435 for (unsigned i = 0; i < NumMIVAddrs; ++i) 5436 VAddrs.push_back(Op.getOperand(AddrIdx + i)); 5437 } 5438 5439 // If the register allocator cannot place the address registers contiguously 5440 // without introducing moves, then using the non-sequential address encoding 5441 // is always preferable, since it saves VALU instructions and is usually a 5442 // wash in terms of code size or even better. 5443 // 5444 // However, we currently have no way of hinting to the register allocator that 5445 // MIMG addresses should be placed contiguously when it is possible to do so, 5446 // so force non-NSA for the common 2-address case as a heuristic. 5447 // 5448 // SIShrinkInstructions will convert NSA encodings to non-NSA after register 5449 // allocation when possible. 5450 bool UseNSA = 5451 ST->hasFeature(AMDGPU::FeatureNSAEncoding) && VAddrs.size() >= 3; 5452 SDValue VAddr; 5453 if (!UseNSA) 5454 VAddr = getBuildDwordsVector(DAG, DL, VAddrs); 5455 5456 SDValue True = DAG.getTargetConstant(1, DL, MVT::i1); 5457 SDValue False = DAG.getTargetConstant(0, DL, MVT::i1); 5458 unsigned CtrlIdx; // Index of texfailctrl argument 5459 SDValue Unorm; 5460 if (!BaseOpcode->Sampler) { 5461 Unorm = True; 5462 CtrlIdx = AddrIdx + NumVAddrs + 1; 5463 } else { 5464 auto UnormConst = 5465 cast<ConstantSDNode>(Op.getOperand(AddrIdx + NumVAddrs + 2)); 5466 5467 Unorm = UnormConst->getZExtValue() ? True : False; 5468 CtrlIdx = AddrIdx + NumVAddrs + 3; 5469 } 5470 5471 SDValue TFE; 5472 SDValue LWE; 5473 SDValue TexFail = Op.getOperand(CtrlIdx); 5474 bool IsTexFail = false; 5475 if (!parseTexFail(TexFail, DAG, &TFE, &LWE, IsTexFail)) 5476 return Op; 5477 5478 if (IsTexFail) { 5479 if (!DMaskLanes) { 5480 // Expecting to get an error flag since TFC is on - and dmask is 0 5481 // Force dmask to be at least 1 otherwise the instruction will fail 5482 DMask = 0x1; 5483 DMaskLanes = 1; 5484 NumVDataDwords = 1; 5485 } 5486 NumVDataDwords += 1; 5487 AdjustRetType = true; 5488 } 5489 5490 // Has something earlier tagged that the return type needs adjusting 5491 // This happens if the instruction is a load or has set TexFailCtrl flags 5492 if (AdjustRetType) { 5493 // NumVDataDwords reflects the true number of dwords required in the return type 5494 if (DMaskLanes == 0 && !BaseOpcode->Store) { 5495 // This is a no-op load. This can be eliminated 5496 SDValue Undef = DAG.getUNDEF(Op.getValueType()); 5497 if (isa<MemSDNode>(Op)) 5498 return DAG.getMergeValues({Undef, Op.getOperand(0)}, DL); 5499 return Undef; 5500 } 5501 5502 EVT NewVT = NumVDataDwords > 1 ? 5503 EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumVDataDwords) 5504 : MVT::i32; 5505 5506 ResultTypes[0] = NewVT; 5507 if (ResultTypes.size() == 3) { 5508 // Original result was aggregate type used for TexFailCtrl results 5509 // The actual instruction returns as a vector type which has now been 5510 // created. Remove the aggregate result. 5511 ResultTypes.erase(&ResultTypes[1]); 5512 } 5513 } 5514 5515 SDValue GLC; 5516 SDValue SLC; 5517 SDValue DLC; 5518 if (BaseOpcode->Atomic) { 5519 GLC = True; // TODO no-return optimization 5520 if (!parseCachePolicy(Op.getOperand(CtrlIdx + 1), DAG, nullptr, &SLC, 5521 IsGFX10 ? &DLC : nullptr)) 5522 return Op; 5523 } else { 5524 if (!parseCachePolicy(Op.getOperand(CtrlIdx + 1), DAG, &GLC, &SLC, 5525 IsGFX10 ? &DLC : nullptr)) 5526 return Op; 5527 } 5528 5529 SmallVector<SDValue, 26> Ops; 5530 if (BaseOpcode->Store || BaseOpcode->Atomic) 5531 Ops.push_back(VData); // vdata 5532 if (UseNSA) { 5533 for (const SDValue &Addr : VAddrs) 5534 Ops.push_back(Addr); 5535 } else { 5536 Ops.push_back(VAddr); 5537 } 5538 Ops.push_back(Op.getOperand(AddrIdx + NumVAddrs)); // rsrc 5539 if (BaseOpcode->Sampler) 5540 Ops.push_back(Op.getOperand(AddrIdx + NumVAddrs + 1)); // sampler 5541 Ops.push_back(DAG.getTargetConstant(DMask, DL, MVT::i32)); 5542 if (IsGFX10) 5543 Ops.push_back(DAG.getTargetConstant(DimInfo->Encoding, DL, MVT::i32)); 5544 Ops.push_back(Unorm); 5545 if (IsGFX10) 5546 Ops.push_back(DLC); 5547 Ops.push_back(GLC); 5548 Ops.push_back(SLC); 5549 Ops.push_back(IsA16 && // r128, a16 for gfx9 5550 ST->hasFeature(AMDGPU::FeatureR128A16) ? True : False); 5551 if (IsGFX10) 5552 Ops.push_back(IsA16 ? True : False); 5553 Ops.push_back(TFE); 5554 Ops.push_back(LWE); 5555 if (!IsGFX10) 5556 Ops.push_back(DimInfo->DA ? True : False); 5557 if (BaseOpcode->HasD16) 5558 Ops.push_back(IsD16 ? True : False); 5559 if (isa<MemSDNode>(Op)) 5560 Ops.push_back(Op.getOperand(0)); // chain 5561 5562 int NumVAddrDwords = 5563 UseNSA ? VAddrs.size() : VAddr.getValueType().getSizeInBits() / 32; 5564 int Opcode = -1; 5565 5566 if (IsGFX10) { 5567 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, 5568 UseNSA ? AMDGPU::MIMGEncGfx10NSA 5569 : AMDGPU::MIMGEncGfx10Default, 5570 NumVDataDwords, NumVAddrDwords); 5571 } else { 5572 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) 5573 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, AMDGPU::MIMGEncGfx8, 5574 NumVDataDwords, NumVAddrDwords); 5575 if (Opcode == -1) 5576 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, AMDGPU::MIMGEncGfx6, 5577 NumVDataDwords, NumVAddrDwords); 5578 } 5579 assert(Opcode != -1); 5580 5581 MachineSDNode *NewNode = DAG.getMachineNode(Opcode, DL, ResultTypes, Ops); 5582 if (auto MemOp = dyn_cast<MemSDNode>(Op)) { 5583 MachineMemOperand *MemRef = MemOp->getMemOperand(); 5584 DAG.setNodeMemRefs(NewNode, {MemRef}); 5585 } 5586 5587 if (BaseOpcode->AtomicX2) { 5588 SmallVector<SDValue, 1> Elt; 5589 DAG.ExtractVectorElements(SDValue(NewNode, 0), Elt, 0, 1); 5590 return DAG.getMergeValues({Elt[0], SDValue(NewNode, 1)}, DL); 5591 } else if (!BaseOpcode->Store) { 5592 return constructRetValue(DAG, NewNode, 5593 OrigResultTypes, IsTexFail, 5594 Subtarget->hasUnpackedD16VMem(), IsD16, 5595 DMaskLanes, NumVDataDwords, DL, 5596 *DAG.getContext()); 5597 } 5598 5599 return SDValue(NewNode, 0); 5600 } 5601 5602 SDValue SITargetLowering::lowerSBuffer(EVT VT, SDLoc DL, SDValue Rsrc, 5603 SDValue Offset, SDValue CachePolicy, 5604 SelectionDAG &DAG) const { 5605 MachineFunction &MF = DAG.getMachineFunction(); 5606 5607 const DataLayout &DataLayout = DAG.getDataLayout(); 5608 unsigned Align = 5609 DataLayout.getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext())); 5610 5611 MachineMemOperand *MMO = MF.getMachineMemOperand( 5612 MachinePointerInfo(), 5613 MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable | 5614 MachineMemOperand::MOInvariant, 5615 VT.getStoreSize(), Align); 5616 5617 if (!Offset->isDivergent()) { 5618 SDValue Ops[] = { 5619 Rsrc, 5620 Offset, // Offset 5621 CachePolicy 5622 }; 5623 5624 // Widen vec3 load to vec4. 5625 if (VT.isVector() && VT.getVectorNumElements() == 3) { 5626 EVT WidenedVT = 5627 EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(), 4); 5628 auto WidenedOp = DAG.getMemIntrinsicNode( 5629 AMDGPUISD::SBUFFER_LOAD, DL, DAG.getVTList(WidenedVT), Ops, WidenedVT, 5630 MF.getMachineMemOperand(MMO, 0, WidenedVT.getStoreSize())); 5631 auto Subvector = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, WidenedOp, 5632 DAG.getVectorIdxConstant(0, DL)); 5633 return Subvector; 5634 } 5635 5636 return DAG.getMemIntrinsicNode(AMDGPUISD::SBUFFER_LOAD, DL, 5637 DAG.getVTList(VT), Ops, VT, MMO); 5638 } 5639 5640 // We have a divergent offset. Emit a MUBUF buffer load instead. We can 5641 // assume that the buffer is unswizzled. 5642 SmallVector<SDValue, 4> Loads; 5643 unsigned NumLoads = 1; 5644 MVT LoadVT = VT.getSimpleVT(); 5645 unsigned NumElts = LoadVT.isVector() ? LoadVT.getVectorNumElements() : 1; 5646 assert((LoadVT.getScalarType() == MVT::i32 || 5647 LoadVT.getScalarType() == MVT::f32)); 5648 5649 if (NumElts == 8 || NumElts == 16) { 5650 NumLoads = NumElts / 4; 5651 LoadVT = MVT::getVectorVT(LoadVT.getScalarType(), 4); 5652 } 5653 5654 SDVTList VTList = DAG.getVTList({LoadVT, MVT::Glue}); 5655 SDValue Ops[] = { 5656 DAG.getEntryNode(), // Chain 5657 Rsrc, // rsrc 5658 DAG.getConstant(0, DL, MVT::i32), // vindex 5659 {}, // voffset 5660 {}, // soffset 5661 {}, // offset 5662 CachePolicy, // cachepolicy 5663 DAG.getTargetConstant(0, DL, MVT::i1), // idxen 5664 }; 5665 5666 // Use the alignment to ensure that the required offsets will fit into the 5667 // immediate offsets. 5668 setBufferOffsets(Offset, DAG, &Ops[3], NumLoads > 1 ? 16 * NumLoads : 4); 5669 5670 uint64_t InstOffset = cast<ConstantSDNode>(Ops[5])->getZExtValue(); 5671 for (unsigned i = 0; i < NumLoads; ++i) { 5672 Ops[5] = DAG.getTargetConstant(InstOffset + 16 * i, DL, MVT::i32); 5673 Loads.push_back(getMemIntrinsicNode(AMDGPUISD::BUFFER_LOAD, DL, VTList, Ops, 5674 LoadVT, MMO, DAG)); 5675 } 5676 5677 if (NumElts == 8 || NumElts == 16) 5678 return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Loads); 5679 5680 return Loads[0]; 5681 } 5682 5683 SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, 5684 SelectionDAG &DAG) const { 5685 MachineFunction &MF = DAG.getMachineFunction(); 5686 auto MFI = MF.getInfo<SIMachineFunctionInfo>(); 5687 5688 EVT VT = Op.getValueType(); 5689 SDLoc DL(Op); 5690 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 5691 5692 // TODO: Should this propagate fast-math-flags? 5693 5694 switch (IntrinsicID) { 5695 case Intrinsic::amdgcn_implicit_buffer_ptr: { 5696 if (getSubtarget()->isAmdHsaOrMesa(MF.getFunction())) 5697 return emitNonHSAIntrinsicError(DAG, DL, VT); 5698 return getPreloadedValue(DAG, *MFI, VT, 5699 AMDGPUFunctionArgInfo::IMPLICIT_BUFFER_PTR); 5700 } 5701 case Intrinsic::amdgcn_dispatch_ptr: 5702 case Intrinsic::amdgcn_queue_ptr: { 5703 if (!Subtarget->isAmdHsaOrMesa(MF.getFunction())) { 5704 DiagnosticInfoUnsupported BadIntrin( 5705 MF.getFunction(), "unsupported hsa intrinsic without hsa target", 5706 DL.getDebugLoc()); 5707 DAG.getContext()->diagnose(BadIntrin); 5708 return DAG.getUNDEF(VT); 5709 } 5710 5711 auto RegID = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ? 5712 AMDGPUFunctionArgInfo::DISPATCH_PTR : AMDGPUFunctionArgInfo::QUEUE_PTR; 5713 return getPreloadedValue(DAG, *MFI, VT, RegID); 5714 } 5715 case Intrinsic::amdgcn_implicitarg_ptr: { 5716 if (MFI->isEntryFunction()) 5717 return getImplicitArgPtr(DAG, DL); 5718 return getPreloadedValue(DAG, *MFI, VT, 5719 AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR); 5720 } 5721 case Intrinsic::amdgcn_kernarg_segment_ptr: { 5722 return getPreloadedValue(DAG, *MFI, VT, 5723 AMDGPUFunctionArgInfo::KERNARG_SEGMENT_PTR); 5724 } 5725 case Intrinsic::amdgcn_dispatch_id: { 5726 return getPreloadedValue(DAG, *MFI, VT, AMDGPUFunctionArgInfo::DISPATCH_ID); 5727 } 5728 case Intrinsic::amdgcn_rcp: 5729 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1)); 5730 case Intrinsic::amdgcn_rsq: 5731 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1)); 5732 case Intrinsic::amdgcn_rsq_legacy: 5733 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) 5734 return emitRemovedIntrinsicError(DAG, DL, VT); 5735 5736 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1)); 5737 case Intrinsic::amdgcn_rcp_legacy: 5738 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) 5739 return emitRemovedIntrinsicError(DAG, DL, VT); 5740 return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1)); 5741 case Intrinsic::amdgcn_rsq_clamp: { 5742 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) 5743 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1)); 5744 5745 Type *Type = VT.getTypeForEVT(*DAG.getContext()); 5746 APFloat Max = APFloat::getLargest(Type->getFltSemantics()); 5747 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true); 5748 5749 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1)); 5750 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq, 5751 DAG.getConstantFP(Max, DL, VT)); 5752 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp, 5753 DAG.getConstantFP(Min, DL, VT)); 5754 } 5755 case Intrinsic::r600_read_ngroups_x: 5756 if (Subtarget->isAmdHsaOS()) 5757 return emitNonHSAIntrinsicError(DAG, DL, VT); 5758 5759 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 5760 SI::KernelInputOffsets::NGROUPS_X, 4, false); 5761 case Intrinsic::r600_read_ngroups_y: 5762 if (Subtarget->isAmdHsaOS()) 5763 return emitNonHSAIntrinsicError(DAG, DL, VT); 5764 5765 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 5766 SI::KernelInputOffsets::NGROUPS_Y, 4, false); 5767 case Intrinsic::r600_read_ngroups_z: 5768 if (Subtarget->isAmdHsaOS()) 5769 return emitNonHSAIntrinsicError(DAG, DL, VT); 5770 5771 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 5772 SI::KernelInputOffsets::NGROUPS_Z, 4, false); 5773 case Intrinsic::r600_read_global_size_x: 5774 if (Subtarget->isAmdHsaOS()) 5775 return emitNonHSAIntrinsicError(DAG, DL, VT); 5776 5777 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 5778 SI::KernelInputOffsets::GLOBAL_SIZE_X, 4, false); 5779 case Intrinsic::r600_read_global_size_y: 5780 if (Subtarget->isAmdHsaOS()) 5781 return emitNonHSAIntrinsicError(DAG, DL, VT); 5782 5783 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 5784 SI::KernelInputOffsets::GLOBAL_SIZE_Y, 4, false); 5785 case Intrinsic::r600_read_global_size_z: 5786 if (Subtarget->isAmdHsaOS()) 5787 return emitNonHSAIntrinsicError(DAG, DL, VT); 5788 5789 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 5790 SI::KernelInputOffsets::GLOBAL_SIZE_Z, 4, false); 5791 case Intrinsic::r600_read_local_size_x: 5792 if (Subtarget->isAmdHsaOS()) 5793 return emitNonHSAIntrinsicError(DAG, DL, VT); 5794 5795 return lowerImplicitZextParam(DAG, Op, MVT::i16, 5796 SI::KernelInputOffsets::LOCAL_SIZE_X); 5797 case Intrinsic::r600_read_local_size_y: 5798 if (Subtarget->isAmdHsaOS()) 5799 return emitNonHSAIntrinsicError(DAG, DL, VT); 5800 5801 return lowerImplicitZextParam(DAG, Op, MVT::i16, 5802 SI::KernelInputOffsets::LOCAL_SIZE_Y); 5803 case Intrinsic::r600_read_local_size_z: 5804 if (Subtarget->isAmdHsaOS()) 5805 return emitNonHSAIntrinsicError(DAG, DL, VT); 5806 5807 return lowerImplicitZextParam(DAG, Op, MVT::i16, 5808 SI::KernelInputOffsets::LOCAL_SIZE_Z); 5809 case Intrinsic::amdgcn_workgroup_id_x: 5810 case Intrinsic::r600_read_tgid_x: 5811 return getPreloadedValue(DAG, *MFI, VT, 5812 AMDGPUFunctionArgInfo::WORKGROUP_ID_X); 5813 case Intrinsic::amdgcn_workgroup_id_y: 5814 case Intrinsic::r600_read_tgid_y: 5815 return getPreloadedValue(DAG, *MFI, VT, 5816 AMDGPUFunctionArgInfo::WORKGROUP_ID_Y); 5817 case Intrinsic::amdgcn_workgroup_id_z: 5818 case Intrinsic::r600_read_tgid_z: 5819 return getPreloadedValue(DAG, *MFI, VT, 5820 AMDGPUFunctionArgInfo::WORKGROUP_ID_Z); 5821 case Intrinsic::amdgcn_workitem_id_x: 5822 case Intrinsic::r600_read_tidig_x: 5823 return loadInputValue(DAG, &AMDGPU::VGPR_32RegClass, MVT::i32, 5824 SDLoc(DAG.getEntryNode()), 5825 MFI->getArgInfo().WorkItemIDX); 5826 case Intrinsic::amdgcn_workitem_id_y: 5827 case Intrinsic::r600_read_tidig_y: 5828 return loadInputValue(DAG, &AMDGPU::VGPR_32RegClass, MVT::i32, 5829 SDLoc(DAG.getEntryNode()), 5830 MFI->getArgInfo().WorkItemIDY); 5831 case Intrinsic::amdgcn_workitem_id_z: 5832 case Intrinsic::r600_read_tidig_z: 5833 return loadInputValue(DAG, &AMDGPU::VGPR_32RegClass, MVT::i32, 5834 SDLoc(DAG.getEntryNode()), 5835 MFI->getArgInfo().WorkItemIDZ); 5836 case Intrinsic::amdgcn_wavefrontsize: 5837 return DAG.getConstant(MF.getSubtarget<GCNSubtarget>().getWavefrontSize(), 5838 SDLoc(Op), MVT::i32); 5839 case Intrinsic::amdgcn_s_buffer_load: { 5840 bool IsGFX10 = Subtarget->getGeneration() >= AMDGPUSubtarget::GFX10; 5841 SDValue GLC; 5842 SDValue DLC = DAG.getTargetConstant(0, DL, MVT::i1); 5843 if (!parseCachePolicy(Op.getOperand(3), DAG, &GLC, nullptr, 5844 IsGFX10 ? &DLC : nullptr)) 5845 return Op; 5846 return lowerSBuffer(VT, DL, Op.getOperand(1), Op.getOperand(2), Op.getOperand(3), 5847 DAG); 5848 } 5849 case Intrinsic::amdgcn_fdiv_fast: 5850 return lowerFDIV_FAST(Op, DAG); 5851 case Intrinsic::amdgcn_sin: 5852 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1)); 5853 5854 case Intrinsic::amdgcn_cos: 5855 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1)); 5856 5857 case Intrinsic::amdgcn_mul_u24: 5858 return DAG.getNode(AMDGPUISD::MUL_U24, DL, VT, Op.getOperand(1), Op.getOperand(2)); 5859 case Intrinsic::amdgcn_mul_i24: 5860 return DAG.getNode(AMDGPUISD::MUL_I24, DL, VT, Op.getOperand(1), Op.getOperand(2)); 5861 5862 case Intrinsic::amdgcn_log_clamp: { 5863 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) 5864 return SDValue(); 5865 5866 DiagnosticInfoUnsupported BadIntrin( 5867 MF.getFunction(), "intrinsic not supported on subtarget", 5868 DL.getDebugLoc()); 5869 DAG.getContext()->diagnose(BadIntrin); 5870 return DAG.getUNDEF(VT); 5871 } 5872 case Intrinsic::amdgcn_ldexp: 5873 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT, 5874 Op.getOperand(1), Op.getOperand(2)); 5875 5876 case Intrinsic::amdgcn_fract: 5877 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1)); 5878 5879 case Intrinsic::amdgcn_class: 5880 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT, 5881 Op.getOperand(1), Op.getOperand(2)); 5882 case Intrinsic::amdgcn_div_fmas: 5883 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT, 5884 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3), 5885 Op.getOperand(4)); 5886 5887 case Intrinsic::amdgcn_div_fixup: 5888 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT, 5889 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 5890 5891 case Intrinsic::amdgcn_trig_preop: 5892 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT, 5893 Op.getOperand(1), Op.getOperand(2)); 5894 case Intrinsic::amdgcn_div_scale: { 5895 const ConstantSDNode *Param = cast<ConstantSDNode>(Op.getOperand(3)); 5896 5897 // Translate to the operands expected by the machine instruction. The 5898 // first parameter must be the same as the first instruction. 5899 SDValue Numerator = Op.getOperand(1); 5900 SDValue Denominator = Op.getOperand(2); 5901 5902 // Note this order is opposite of the machine instruction's operations, 5903 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The 5904 // intrinsic has the numerator as the first operand to match a normal 5905 // division operation. 5906 5907 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator; 5908 5909 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0, 5910 Denominator, Numerator); 5911 } 5912 case Intrinsic::amdgcn_icmp: { 5913 // There is a Pat that handles this variant, so return it as-is. 5914 if (Op.getOperand(1).getValueType() == MVT::i1 && 5915 Op.getConstantOperandVal(2) == 0 && 5916 Op.getConstantOperandVal(3) == ICmpInst::Predicate::ICMP_NE) 5917 return Op; 5918 return lowerICMPIntrinsic(*this, Op.getNode(), DAG); 5919 } 5920 case Intrinsic::amdgcn_fcmp: { 5921 return lowerFCMPIntrinsic(*this, Op.getNode(), DAG); 5922 } 5923 case Intrinsic::amdgcn_fmed3: 5924 return DAG.getNode(AMDGPUISD::FMED3, DL, VT, 5925 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 5926 case Intrinsic::amdgcn_fdot2: 5927 return DAG.getNode(AMDGPUISD::FDOT2, DL, VT, 5928 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3), 5929 Op.getOperand(4)); 5930 case Intrinsic::amdgcn_fmul_legacy: 5931 return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT, 5932 Op.getOperand(1), Op.getOperand(2)); 5933 case Intrinsic::amdgcn_sffbh: 5934 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1)); 5935 case Intrinsic::amdgcn_sbfe: 5936 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT, 5937 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 5938 case Intrinsic::amdgcn_ubfe: 5939 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT, 5940 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 5941 case Intrinsic::amdgcn_cvt_pkrtz: 5942 case Intrinsic::amdgcn_cvt_pknorm_i16: 5943 case Intrinsic::amdgcn_cvt_pknorm_u16: 5944 case Intrinsic::amdgcn_cvt_pk_i16: 5945 case Intrinsic::amdgcn_cvt_pk_u16: { 5946 // FIXME: Stop adding cast if v2f16/v2i16 are legal. 5947 EVT VT = Op.getValueType(); 5948 unsigned Opcode; 5949 5950 if (IntrinsicID == Intrinsic::amdgcn_cvt_pkrtz) 5951 Opcode = AMDGPUISD::CVT_PKRTZ_F16_F32; 5952 else if (IntrinsicID == Intrinsic::amdgcn_cvt_pknorm_i16) 5953 Opcode = AMDGPUISD::CVT_PKNORM_I16_F32; 5954 else if (IntrinsicID == Intrinsic::amdgcn_cvt_pknorm_u16) 5955 Opcode = AMDGPUISD::CVT_PKNORM_U16_F32; 5956 else if (IntrinsicID == Intrinsic::amdgcn_cvt_pk_i16) 5957 Opcode = AMDGPUISD::CVT_PK_I16_I32; 5958 else 5959 Opcode = AMDGPUISD::CVT_PK_U16_U32; 5960 5961 if (isTypeLegal(VT)) 5962 return DAG.getNode(Opcode, DL, VT, Op.getOperand(1), Op.getOperand(2)); 5963 5964 SDValue Node = DAG.getNode(Opcode, DL, MVT::i32, 5965 Op.getOperand(1), Op.getOperand(2)); 5966 return DAG.getNode(ISD::BITCAST, DL, VT, Node); 5967 } 5968 case Intrinsic::amdgcn_fmad_ftz: 5969 return DAG.getNode(AMDGPUISD::FMAD_FTZ, DL, VT, Op.getOperand(1), 5970 Op.getOperand(2), Op.getOperand(3)); 5971 5972 case Intrinsic::amdgcn_if_break: 5973 return SDValue(DAG.getMachineNode(AMDGPU::SI_IF_BREAK, DL, VT, 5974 Op->getOperand(1), Op->getOperand(2)), 0); 5975 5976 case Intrinsic::amdgcn_groupstaticsize: { 5977 Triple::OSType OS = getTargetMachine().getTargetTriple().getOS(); 5978 if (OS == Triple::AMDHSA || OS == Triple::AMDPAL) 5979 return Op; 5980 5981 const Module *M = MF.getFunction().getParent(); 5982 const GlobalValue *GV = 5983 M->getNamedValue(Intrinsic::getName(Intrinsic::amdgcn_groupstaticsize)); 5984 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0, 5985 SIInstrInfo::MO_ABS32_LO); 5986 return {DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, GA), 0}; 5987 } 5988 case Intrinsic::amdgcn_is_shared: 5989 case Intrinsic::amdgcn_is_private: { 5990 SDLoc SL(Op); 5991 unsigned AS = (IntrinsicID == Intrinsic::amdgcn_is_shared) ? 5992 AMDGPUAS::LOCAL_ADDRESS : AMDGPUAS::PRIVATE_ADDRESS; 5993 SDValue Aperture = getSegmentAperture(AS, SL, DAG); 5994 SDValue SrcVec = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, 5995 Op.getOperand(1)); 5996 5997 SDValue SrcHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, SrcVec, 5998 DAG.getConstant(1, SL, MVT::i32)); 5999 return DAG.getSetCC(SL, MVT::i1, SrcHi, Aperture, ISD::SETEQ); 6000 } 6001 default: 6002 if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr = 6003 AMDGPU::getImageDimIntrinsicInfo(IntrinsicID)) 6004 return lowerImage(Op, ImageDimIntr, DAG); 6005 6006 return Op; 6007 } 6008 } 6009 6010 // This function computes an appropriate offset to pass to 6011 // MachineMemOperand::setOffset() based on the offset inputs to 6012 // an intrinsic. If any of the offsets are non-contstant or 6013 // if VIndex is non-zero then this function returns 0. Otherwise, 6014 // it returns the sum of VOffset, SOffset, and Offset. 6015 static unsigned getBufferOffsetForMMO(SDValue VOffset, 6016 SDValue SOffset, 6017 SDValue Offset, 6018 SDValue VIndex = SDValue()) { 6019 6020 if (!isa<ConstantSDNode>(VOffset) || !isa<ConstantSDNode>(SOffset) || 6021 !isa<ConstantSDNode>(Offset)) 6022 return 0; 6023 6024 if (VIndex) { 6025 if (!isa<ConstantSDNode>(VIndex) || !cast<ConstantSDNode>(VIndex)->isNullValue()) 6026 return 0; 6027 } 6028 6029 return cast<ConstantSDNode>(VOffset)->getSExtValue() + 6030 cast<ConstantSDNode>(SOffset)->getSExtValue() + 6031 cast<ConstantSDNode>(Offset)->getSExtValue(); 6032 } 6033 6034 static unsigned getDSShaderTypeValue(const MachineFunction &MF) { 6035 switch (MF.getFunction().getCallingConv()) { 6036 case CallingConv::AMDGPU_PS: 6037 return 1; 6038 case CallingConv::AMDGPU_VS: 6039 return 2; 6040 case CallingConv::AMDGPU_GS: 6041 return 3; 6042 case CallingConv::AMDGPU_HS: 6043 case CallingConv::AMDGPU_LS: 6044 case CallingConv::AMDGPU_ES: 6045 report_fatal_error("ds_ordered_count unsupported for this calling conv"); 6046 case CallingConv::AMDGPU_CS: 6047 case CallingConv::AMDGPU_KERNEL: 6048 case CallingConv::C: 6049 case CallingConv::Fast: 6050 default: 6051 // Assume other calling conventions are various compute callable functions 6052 return 0; 6053 } 6054 } 6055 6056 SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, 6057 SelectionDAG &DAG) const { 6058 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 6059 SDLoc DL(Op); 6060 6061 switch (IntrID) { 6062 case Intrinsic::amdgcn_ds_ordered_add: 6063 case Intrinsic::amdgcn_ds_ordered_swap: { 6064 MemSDNode *M = cast<MemSDNode>(Op); 6065 SDValue Chain = M->getOperand(0); 6066 SDValue M0 = M->getOperand(2); 6067 SDValue Value = M->getOperand(3); 6068 unsigned IndexOperand = M->getConstantOperandVal(7); 6069 unsigned WaveRelease = M->getConstantOperandVal(8); 6070 unsigned WaveDone = M->getConstantOperandVal(9); 6071 6072 unsigned OrderedCountIndex = IndexOperand & 0x3f; 6073 IndexOperand &= ~0x3f; 6074 unsigned CountDw = 0; 6075 6076 if (Subtarget->getGeneration() >= AMDGPUSubtarget::GFX10) { 6077 CountDw = (IndexOperand >> 24) & 0xf; 6078 IndexOperand &= ~(0xf << 24); 6079 6080 if (CountDw < 1 || CountDw > 4) { 6081 report_fatal_error( 6082 "ds_ordered_count: dword count must be between 1 and 4"); 6083 } 6084 } 6085 6086 if (IndexOperand) 6087 report_fatal_error("ds_ordered_count: bad index operand"); 6088 6089 if (WaveDone && !WaveRelease) 6090 report_fatal_error("ds_ordered_count: wave_done requires wave_release"); 6091 6092 unsigned Instruction = IntrID == Intrinsic::amdgcn_ds_ordered_add ? 0 : 1; 6093 unsigned ShaderType = getDSShaderTypeValue(DAG.getMachineFunction()); 6094 unsigned Offset0 = OrderedCountIndex << 2; 6095 unsigned Offset1 = WaveRelease | (WaveDone << 1) | (ShaderType << 2) | 6096 (Instruction << 4); 6097 6098 if (Subtarget->getGeneration() >= AMDGPUSubtarget::GFX10) 6099 Offset1 |= (CountDw - 1) << 6; 6100 6101 unsigned Offset = Offset0 | (Offset1 << 8); 6102 6103 SDValue Ops[] = { 6104 Chain, 6105 Value, 6106 DAG.getTargetConstant(Offset, DL, MVT::i16), 6107 copyToM0(DAG, Chain, DL, M0).getValue(1), // Glue 6108 }; 6109 return DAG.getMemIntrinsicNode(AMDGPUISD::DS_ORDERED_COUNT, DL, 6110 M->getVTList(), Ops, M->getMemoryVT(), 6111 M->getMemOperand()); 6112 } 6113 case Intrinsic::amdgcn_ds_fadd: { 6114 MemSDNode *M = cast<MemSDNode>(Op); 6115 unsigned Opc; 6116 switch (IntrID) { 6117 case Intrinsic::amdgcn_ds_fadd: 6118 Opc = ISD::ATOMIC_LOAD_FADD; 6119 break; 6120 } 6121 6122 return DAG.getAtomic(Opc, SDLoc(Op), M->getMemoryVT(), 6123 M->getOperand(0), M->getOperand(2), M->getOperand(3), 6124 M->getMemOperand()); 6125 } 6126 case Intrinsic::amdgcn_atomic_inc: 6127 case Intrinsic::amdgcn_atomic_dec: 6128 case Intrinsic::amdgcn_ds_fmin: 6129 case Intrinsic::amdgcn_ds_fmax: { 6130 MemSDNode *M = cast<MemSDNode>(Op); 6131 unsigned Opc; 6132 switch (IntrID) { 6133 case Intrinsic::amdgcn_atomic_inc: 6134 Opc = AMDGPUISD::ATOMIC_INC; 6135 break; 6136 case Intrinsic::amdgcn_atomic_dec: 6137 Opc = AMDGPUISD::ATOMIC_DEC; 6138 break; 6139 case Intrinsic::amdgcn_ds_fmin: 6140 Opc = AMDGPUISD::ATOMIC_LOAD_FMIN; 6141 break; 6142 case Intrinsic::amdgcn_ds_fmax: 6143 Opc = AMDGPUISD::ATOMIC_LOAD_FMAX; 6144 break; 6145 default: 6146 llvm_unreachable("Unknown intrinsic!"); 6147 } 6148 SDValue Ops[] = { 6149 M->getOperand(0), // Chain 6150 M->getOperand(2), // Ptr 6151 M->getOperand(3) // Value 6152 }; 6153 6154 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops, 6155 M->getMemoryVT(), M->getMemOperand()); 6156 } 6157 case Intrinsic::amdgcn_buffer_load: 6158 case Intrinsic::amdgcn_buffer_load_format: { 6159 unsigned Glc = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue(); 6160 unsigned Slc = cast<ConstantSDNode>(Op.getOperand(6))->getZExtValue(); 6161 unsigned IdxEn = 1; 6162 if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(3))) 6163 IdxEn = Idx->getZExtValue() != 0; 6164 SDValue Ops[] = { 6165 Op.getOperand(0), // Chain 6166 Op.getOperand(2), // rsrc 6167 Op.getOperand(3), // vindex 6168 SDValue(), // voffset -- will be set by setBufferOffsets 6169 SDValue(), // soffset -- will be set by setBufferOffsets 6170 SDValue(), // offset -- will be set by setBufferOffsets 6171 DAG.getTargetConstant(Glc | (Slc << 1), DL, MVT::i32), // cachepolicy 6172 DAG.getTargetConstant(IdxEn, DL, MVT::i1), // idxen 6173 }; 6174 6175 unsigned Offset = setBufferOffsets(Op.getOperand(4), DAG, &Ops[3]); 6176 // We don't know the offset if vindex is non-zero, so clear it. 6177 if (IdxEn) 6178 Offset = 0; 6179 6180 unsigned Opc = (IntrID == Intrinsic::amdgcn_buffer_load) ? 6181 AMDGPUISD::BUFFER_LOAD : AMDGPUISD::BUFFER_LOAD_FORMAT; 6182 6183 EVT VT = Op.getValueType(); 6184 EVT IntVT = VT.changeTypeToInteger(); 6185 auto *M = cast<MemSDNode>(Op); 6186 M->getMemOperand()->setOffset(Offset); 6187 EVT LoadVT = Op.getValueType(); 6188 6189 if (LoadVT.getScalarType() == MVT::f16) 6190 return adjustLoadValueType(AMDGPUISD::BUFFER_LOAD_FORMAT_D16, 6191 M, DAG, Ops); 6192 6193 // Handle BUFFER_LOAD_BYTE/UBYTE/SHORT/USHORT overloaded intrinsics 6194 if (LoadVT.getScalarType() == MVT::i8 || 6195 LoadVT.getScalarType() == MVT::i16) 6196 return handleByteShortBufferLoads(DAG, LoadVT, DL, Ops, M); 6197 6198 return getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, IntVT, 6199 M->getMemOperand(), DAG); 6200 } 6201 case Intrinsic::amdgcn_raw_buffer_load: 6202 case Intrinsic::amdgcn_raw_buffer_load_format: { 6203 const bool IsFormat = IntrID == Intrinsic::amdgcn_raw_buffer_load_format; 6204 6205 auto Offsets = splitBufferOffsets(Op.getOperand(3), DAG); 6206 SDValue Ops[] = { 6207 Op.getOperand(0), // Chain 6208 Op.getOperand(2), // rsrc 6209 DAG.getConstant(0, DL, MVT::i32), // vindex 6210 Offsets.first, // voffset 6211 Op.getOperand(4), // soffset 6212 Offsets.second, // offset 6213 Op.getOperand(5), // cachepolicy, swizzled buffer 6214 DAG.getTargetConstant(0, DL, MVT::i1), // idxen 6215 }; 6216 6217 auto *M = cast<MemSDNode>(Op); 6218 M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[3], Ops[4], Ops[5])); 6219 return lowerIntrinsicLoad(M, IsFormat, DAG, Ops); 6220 } 6221 case Intrinsic::amdgcn_struct_buffer_load: 6222 case Intrinsic::amdgcn_struct_buffer_load_format: { 6223 const bool IsFormat = IntrID == Intrinsic::amdgcn_struct_buffer_load_format; 6224 6225 auto Offsets = splitBufferOffsets(Op.getOperand(4), DAG); 6226 SDValue Ops[] = { 6227 Op.getOperand(0), // Chain 6228 Op.getOperand(2), // rsrc 6229 Op.getOperand(3), // vindex 6230 Offsets.first, // voffset 6231 Op.getOperand(5), // soffset 6232 Offsets.second, // offset 6233 Op.getOperand(6), // cachepolicy, swizzled buffer 6234 DAG.getTargetConstant(1, DL, MVT::i1), // idxen 6235 }; 6236 6237 auto *M = cast<MemSDNode>(Op); 6238 M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[3], Ops[4], Ops[5], 6239 Ops[2])); 6240 return lowerIntrinsicLoad(cast<MemSDNode>(Op), IsFormat, DAG, Ops); 6241 } 6242 case Intrinsic::amdgcn_tbuffer_load: { 6243 MemSDNode *M = cast<MemSDNode>(Op); 6244 EVT LoadVT = Op.getValueType(); 6245 6246 unsigned Dfmt = cast<ConstantSDNode>(Op.getOperand(7))->getZExtValue(); 6247 unsigned Nfmt = cast<ConstantSDNode>(Op.getOperand(8))->getZExtValue(); 6248 unsigned Glc = cast<ConstantSDNode>(Op.getOperand(9))->getZExtValue(); 6249 unsigned Slc = cast<ConstantSDNode>(Op.getOperand(10))->getZExtValue(); 6250 unsigned IdxEn = 1; 6251 if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(3))) 6252 IdxEn = Idx->getZExtValue() != 0; 6253 SDValue Ops[] = { 6254 Op.getOperand(0), // Chain 6255 Op.getOperand(2), // rsrc 6256 Op.getOperand(3), // vindex 6257 Op.getOperand(4), // voffset 6258 Op.getOperand(5), // soffset 6259 Op.getOperand(6), // offset 6260 DAG.getTargetConstant(Dfmt | (Nfmt << 4), DL, MVT::i32), // format 6261 DAG.getTargetConstant(Glc | (Slc << 1), DL, MVT::i32), // cachepolicy 6262 DAG.getTargetConstant(IdxEn, DL, MVT::i1) // idxen 6263 }; 6264 6265 if (LoadVT.getScalarType() == MVT::f16) 6266 return adjustLoadValueType(AMDGPUISD::TBUFFER_LOAD_FORMAT_D16, 6267 M, DAG, Ops); 6268 return getMemIntrinsicNode(AMDGPUISD::TBUFFER_LOAD_FORMAT, DL, 6269 Op->getVTList(), Ops, LoadVT, M->getMemOperand(), 6270 DAG); 6271 } 6272 case Intrinsic::amdgcn_raw_tbuffer_load: { 6273 MemSDNode *M = cast<MemSDNode>(Op); 6274 EVT LoadVT = Op.getValueType(); 6275 auto Offsets = splitBufferOffsets(Op.getOperand(3), DAG); 6276 6277 SDValue Ops[] = { 6278 Op.getOperand(0), // Chain 6279 Op.getOperand(2), // rsrc 6280 DAG.getConstant(0, DL, MVT::i32), // vindex 6281 Offsets.first, // voffset 6282 Op.getOperand(4), // soffset 6283 Offsets.second, // offset 6284 Op.getOperand(5), // format 6285 Op.getOperand(6), // cachepolicy, swizzled buffer 6286 DAG.getTargetConstant(0, DL, MVT::i1), // idxen 6287 }; 6288 6289 if (LoadVT.getScalarType() == MVT::f16) 6290 return adjustLoadValueType(AMDGPUISD::TBUFFER_LOAD_FORMAT_D16, 6291 M, DAG, Ops); 6292 return getMemIntrinsicNode(AMDGPUISD::TBUFFER_LOAD_FORMAT, DL, 6293 Op->getVTList(), Ops, LoadVT, M->getMemOperand(), 6294 DAG); 6295 } 6296 case Intrinsic::amdgcn_struct_tbuffer_load: { 6297 MemSDNode *M = cast<MemSDNode>(Op); 6298 EVT LoadVT = Op.getValueType(); 6299 auto Offsets = splitBufferOffsets(Op.getOperand(4), DAG); 6300 6301 SDValue Ops[] = { 6302 Op.getOperand(0), // Chain 6303 Op.getOperand(2), // rsrc 6304 Op.getOperand(3), // vindex 6305 Offsets.first, // voffset 6306 Op.getOperand(5), // soffset 6307 Offsets.second, // offset 6308 Op.getOperand(6), // format 6309 Op.getOperand(7), // cachepolicy, swizzled buffer 6310 DAG.getTargetConstant(1, DL, MVT::i1), // idxen 6311 }; 6312 6313 if (LoadVT.getScalarType() == MVT::f16) 6314 return adjustLoadValueType(AMDGPUISD::TBUFFER_LOAD_FORMAT_D16, 6315 M, DAG, Ops); 6316 return getMemIntrinsicNode(AMDGPUISD::TBUFFER_LOAD_FORMAT, DL, 6317 Op->getVTList(), Ops, LoadVT, M->getMemOperand(), 6318 DAG); 6319 } 6320 case Intrinsic::amdgcn_buffer_atomic_swap: 6321 case Intrinsic::amdgcn_buffer_atomic_add: 6322 case Intrinsic::amdgcn_buffer_atomic_sub: 6323 case Intrinsic::amdgcn_buffer_atomic_smin: 6324 case Intrinsic::amdgcn_buffer_atomic_umin: 6325 case Intrinsic::amdgcn_buffer_atomic_smax: 6326 case Intrinsic::amdgcn_buffer_atomic_umax: 6327 case Intrinsic::amdgcn_buffer_atomic_and: 6328 case Intrinsic::amdgcn_buffer_atomic_or: 6329 case Intrinsic::amdgcn_buffer_atomic_xor: { 6330 unsigned Slc = cast<ConstantSDNode>(Op.getOperand(6))->getZExtValue(); 6331 unsigned IdxEn = 1; 6332 if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(4))) 6333 IdxEn = Idx->getZExtValue() != 0; 6334 SDValue Ops[] = { 6335 Op.getOperand(0), // Chain 6336 Op.getOperand(2), // vdata 6337 Op.getOperand(3), // rsrc 6338 Op.getOperand(4), // vindex 6339 SDValue(), // voffset -- will be set by setBufferOffsets 6340 SDValue(), // soffset -- will be set by setBufferOffsets 6341 SDValue(), // offset -- will be set by setBufferOffsets 6342 DAG.getTargetConstant(Slc << 1, DL, MVT::i32), // cachepolicy 6343 DAG.getTargetConstant(IdxEn, DL, MVT::i1), // idxen 6344 }; 6345 unsigned Offset = setBufferOffsets(Op.getOperand(5), DAG, &Ops[4]); 6346 // We don't know the offset if vindex is non-zero, so clear it. 6347 if (IdxEn) 6348 Offset = 0; 6349 EVT VT = Op.getValueType(); 6350 6351 auto *M = cast<MemSDNode>(Op); 6352 M->getMemOperand()->setOffset(Offset); 6353 unsigned Opcode = 0; 6354 6355 switch (IntrID) { 6356 case Intrinsic::amdgcn_buffer_atomic_swap: 6357 Opcode = AMDGPUISD::BUFFER_ATOMIC_SWAP; 6358 break; 6359 case Intrinsic::amdgcn_buffer_atomic_add: 6360 Opcode = AMDGPUISD::BUFFER_ATOMIC_ADD; 6361 break; 6362 case Intrinsic::amdgcn_buffer_atomic_sub: 6363 Opcode = AMDGPUISD::BUFFER_ATOMIC_SUB; 6364 break; 6365 case Intrinsic::amdgcn_buffer_atomic_smin: 6366 Opcode = AMDGPUISD::BUFFER_ATOMIC_SMIN; 6367 break; 6368 case Intrinsic::amdgcn_buffer_atomic_umin: 6369 Opcode = AMDGPUISD::BUFFER_ATOMIC_UMIN; 6370 break; 6371 case Intrinsic::amdgcn_buffer_atomic_smax: 6372 Opcode = AMDGPUISD::BUFFER_ATOMIC_SMAX; 6373 break; 6374 case Intrinsic::amdgcn_buffer_atomic_umax: 6375 Opcode = AMDGPUISD::BUFFER_ATOMIC_UMAX; 6376 break; 6377 case Intrinsic::amdgcn_buffer_atomic_and: 6378 Opcode = AMDGPUISD::BUFFER_ATOMIC_AND; 6379 break; 6380 case Intrinsic::amdgcn_buffer_atomic_or: 6381 Opcode = AMDGPUISD::BUFFER_ATOMIC_OR; 6382 break; 6383 case Intrinsic::amdgcn_buffer_atomic_xor: 6384 Opcode = AMDGPUISD::BUFFER_ATOMIC_XOR; 6385 break; 6386 default: 6387 llvm_unreachable("unhandled atomic opcode"); 6388 } 6389 6390 return DAG.getMemIntrinsicNode(Opcode, DL, Op->getVTList(), Ops, VT, 6391 M->getMemOperand()); 6392 } 6393 case Intrinsic::amdgcn_raw_buffer_atomic_swap: 6394 case Intrinsic::amdgcn_raw_buffer_atomic_add: 6395 case Intrinsic::amdgcn_raw_buffer_atomic_sub: 6396 case Intrinsic::amdgcn_raw_buffer_atomic_smin: 6397 case Intrinsic::amdgcn_raw_buffer_atomic_umin: 6398 case Intrinsic::amdgcn_raw_buffer_atomic_smax: 6399 case Intrinsic::amdgcn_raw_buffer_atomic_umax: 6400 case Intrinsic::amdgcn_raw_buffer_atomic_and: 6401 case Intrinsic::amdgcn_raw_buffer_atomic_or: 6402 case Intrinsic::amdgcn_raw_buffer_atomic_xor: 6403 case Intrinsic::amdgcn_raw_buffer_atomic_inc: 6404 case Intrinsic::amdgcn_raw_buffer_atomic_dec: { 6405 auto Offsets = splitBufferOffsets(Op.getOperand(4), DAG); 6406 SDValue Ops[] = { 6407 Op.getOperand(0), // Chain 6408 Op.getOperand(2), // vdata 6409 Op.getOperand(3), // rsrc 6410 DAG.getConstant(0, DL, MVT::i32), // vindex 6411 Offsets.first, // voffset 6412 Op.getOperand(5), // soffset 6413 Offsets.second, // offset 6414 Op.getOperand(6), // cachepolicy 6415 DAG.getTargetConstant(0, DL, MVT::i1), // idxen 6416 }; 6417 EVT VT = Op.getValueType(); 6418 6419 auto *M = cast<MemSDNode>(Op); 6420 M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[4], Ops[5], Ops[6])); 6421 unsigned Opcode = 0; 6422 6423 switch (IntrID) { 6424 case Intrinsic::amdgcn_raw_buffer_atomic_swap: 6425 Opcode = AMDGPUISD::BUFFER_ATOMIC_SWAP; 6426 break; 6427 case Intrinsic::amdgcn_raw_buffer_atomic_add: 6428 Opcode = AMDGPUISD::BUFFER_ATOMIC_ADD; 6429 break; 6430 case Intrinsic::amdgcn_raw_buffer_atomic_sub: 6431 Opcode = AMDGPUISD::BUFFER_ATOMIC_SUB; 6432 break; 6433 case Intrinsic::amdgcn_raw_buffer_atomic_smin: 6434 Opcode = AMDGPUISD::BUFFER_ATOMIC_SMIN; 6435 break; 6436 case Intrinsic::amdgcn_raw_buffer_atomic_umin: 6437 Opcode = AMDGPUISD::BUFFER_ATOMIC_UMIN; 6438 break; 6439 case Intrinsic::amdgcn_raw_buffer_atomic_smax: 6440 Opcode = AMDGPUISD::BUFFER_ATOMIC_SMAX; 6441 break; 6442 case Intrinsic::amdgcn_raw_buffer_atomic_umax: 6443 Opcode = AMDGPUISD::BUFFER_ATOMIC_UMAX; 6444 break; 6445 case Intrinsic::amdgcn_raw_buffer_atomic_and: 6446 Opcode = AMDGPUISD::BUFFER_ATOMIC_AND; 6447 break; 6448 case Intrinsic::amdgcn_raw_buffer_atomic_or: 6449 Opcode = AMDGPUISD::BUFFER_ATOMIC_OR; 6450 break; 6451 case Intrinsic::amdgcn_raw_buffer_atomic_xor: 6452 Opcode = AMDGPUISD::BUFFER_ATOMIC_XOR; 6453 break; 6454 case Intrinsic::amdgcn_raw_buffer_atomic_inc: 6455 Opcode = AMDGPUISD::BUFFER_ATOMIC_INC; 6456 break; 6457 case Intrinsic::amdgcn_raw_buffer_atomic_dec: 6458 Opcode = AMDGPUISD::BUFFER_ATOMIC_DEC; 6459 break; 6460 default: 6461 llvm_unreachable("unhandled atomic opcode"); 6462 } 6463 6464 return DAG.getMemIntrinsicNode(Opcode, DL, Op->getVTList(), Ops, VT, 6465 M->getMemOperand()); 6466 } 6467 case Intrinsic::amdgcn_struct_buffer_atomic_swap: 6468 case Intrinsic::amdgcn_struct_buffer_atomic_add: 6469 case Intrinsic::amdgcn_struct_buffer_atomic_sub: 6470 case Intrinsic::amdgcn_struct_buffer_atomic_smin: 6471 case Intrinsic::amdgcn_struct_buffer_atomic_umin: 6472 case Intrinsic::amdgcn_struct_buffer_atomic_smax: 6473 case Intrinsic::amdgcn_struct_buffer_atomic_umax: 6474 case Intrinsic::amdgcn_struct_buffer_atomic_and: 6475 case Intrinsic::amdgcn_struct_buffer_atomic_or: 6476 case Intrinsic::amdgcn_struct_buffer_atomic_xor: 6477 case Intrinsic::amdgcn_struct_buffer_atomic_inc: 6478 case Intrinsic::amdgcn_struct_buffer_atomic_dec: { 6479 auto Offsets = splitBufferOffsets(Op.getOperand(5), DAG); 6480 SDValue Ops[] = { 6481 Op.getOperand(0), // Chain 6482 Op.getOperand(2), // vdata 6483 Op.getOperand(3), // rsrc 6484 Op.getOperand(4), // vindex 6485 Offsets.first, // voffset 6486 Op.getOperand(6), // soffset 6487 Offsets.second, // offset 6488 Op.getOperand(7), // cachepolicy 6489 DAG.getTargetConstant(1, DL, MVT::i1), // idxen 6490 }; 6491 EVT VT = Op.getValueType(); 6492 6493 auto *M = cast<MemSDNode>(Op); 6494 M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[4], Ops[5], Ops[6], 6495 Ops[3])); 6496 unsigned Opcode = 0; 6497 6498 switch (IntrID) { 6499 case Intrinsic::amdgcn_struct_buffer_atomic_swap: 6500 Opcode = AMDGPUISD::BUFFER_ATOMIC_SWAP; 6501 break; 6502 case Intrinsic::amdgcn_struct_buffer_atomic_add: 6503 Opcode = AMDGPUISD::BUFFER_ATOMIC_ADD; 6504 break; 6505 case Intrinsic::amdgcn_struct_buffer_atomic_sub: 6506 Opcode = AMDGPUISD::BUFFER_ATOMIC_SUB; 6507 break; 6508 case Intrinsic::amdgcn_struct_buffer_atomic_smin: 6509 Opcode = AMDGPUISD::BUFFER_ATOMIC_SMIN; 6510 break; 6511 case Intrinsic::amdgcn_struct_buffer_atomic_umin: 6512 Opcode = AMDGPUISD::BUFFER_ATOMIC_UMIN; 6513 break; 6514 case Intrinsic::amdgcn_struct_buffer_atomic_smax: 6515 Opcode = AMDGPUISD::BUFFER_ATOMIC_SMAX; 6516 break; 6517 case Intrinsic::amdgcn_struct_buffer_atomic_umax: 6518 Opcode = AMDGPUISD::BUFFER_ATOMIC_UMAX; 6519 break; 6520 case Intrinsic::amdgcn_struct_buffer_atomic_and: 6521 Opcode = AMDGPUISD::BUFFER_ATOMIC_AND; 6522 break; 6523 case Intrinsic::amdgcn_struct_buffer_atomic_or: 6524 Opcode = AMDGPUISD::BUFFER_ATOMIC_OR; 6525 break; 6526 case Intrinsic::amdgcn_struct_buffer_atomic_xor: 6527 Opcode = AMDGPUISD::BUFFER_ATOMIC_XOR; 6528 break; 6529 case Intrinsic::amdgcn_struct_buffer_atomic_inc: 6530 Opcode = AMDGPUISD::BUFFER_ATOMIC_INC; 6531 break; 6532 case Intrinsic::amdgcn_struct_buffer_atomic_dec: 6533 Opcode = AMDGPUISD::BUFFER_ATOMIC_DEC; 6534 break; 6535 default: 6536 llvm_unreachable("unhandled atomic opcode"); 6537 } 6538 6539 return DAG.getMemIntrinsicNode(Opcode, DL, Op->getVTList(), Ops, VT, 6540 M->getMemOperand()); 6541 } 6542 case Intrinsic::amdgcn_buffer_atomic_cmpswap: { 6543 unsigned Slc = cast<ConstantSDNode>(Op.getOperand(7))->getZExtValue(); 6544 unsigned IdxEn = 1; 6545 if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(5))) 6546 IdxEn = Idx->getZExtValue() != 0; 6547 SDValue Ops[] = { 6548 Op.getOperand(0), // Chain 6549 Op.getOperand(2), // src 6550 Op.getOperand(3), // cmp 6551 Op.getOperand(4), // rsrc 6552 Op.getOperand(5), // vindex 6553 SDValue(), // voffset -- will be set by setBufferOffsets 6554 SDValue(), // soffset -- will be set by setBufferOffsets 6555 SDValue(), // offset -- will be set by setBufferOffsets 6556 DAG.getTargetConstant(Slc << 1, DL, MVT::i32), // cachepolicy 6557 DAG.getTargetConstant(IdxEn, DL, MVT::i1), // idxen 6558 }; 6559 unsigned Offset = setBufferOffsets(Op.getOperand(6), DAG, &Ops[5]); 6560 // We don't know the offset if vindex is non-zero, so clear it. 6561 if (IdxEn) 6562 Offset = 0; 6563 EVT VT = Op.getValueType(); 6564 auto *M = cast<MemSDNode>(Op); 6565 M->getMemOperand()->setOffset(Offset); 6566 6567 return DAG.getMemIntrinsicNode(AMDGPUISD::BUFFER_ATOMIC_CMPSWAP, DL, 6568 Op->getVTList(), Ops, VT, M->getMemOperand()); 6569 } 6570 case Intrinsic::amdgcn_raw_buffer_atomic_cmpswap: { 6571 auto Offsets = splitBufferOffsets(Op.getOperand(5), DAG); 6572 SDValue Ops[] = { 6573 Op.getOperand(0), // Chain 6574 Op.getOperand(2), // src 6575 Op.getOperand(3), // cmp 6576 Op.getOperand(4), // rsrc 6577 DAG.getConstant(0, DL, MVT::i32), // vindex 6578 Offsets.first, // voffset 6579 Op.getOperand(6), // soffset 6580 Offsets.second, // offset 6581 Op.getOperand(7), // cachepolicy 6582 DAG.getTargetConstant(0, DL, MVT::i1), // idxen 6583 }; 6584 EVT VT = Op.getValueType(); 6585 auto *M = cast<MemSDNode>(Op); 6586 M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[5], Ops[6], Ops[7])); 6587 6588 return DAG.getMemIntrinsicNode(AMDGPUISD::BUFFER_ATOMIC_CMPSWAP, DL, 6589 Op->getVTList(), Ops, VT, M->getMemOperand()); 6590 } 6591 case Intrinsic::amdgcn_struct_buffer_atomic_cmpswap: { 6592 auto Offsets = splitBufferOffsets(Op.getOperand(6), DAG); 6593 SDValue Ops[] = { 6594 Op.getOperand(0), // Chain 6595 Op.getOperand(2), // src 6596 Op.getOperand(3), // cmp 6597 Op.getOperand(4), // rsrc 6598 Op.getOperand(5), // vindex 6599 Offsets.first, // voffset 6600 Op.getOperand(7), // soffset 6601 Offsets.second, // offset 6602 Op.getOperand(8), // cachepolicy 6603 DAG.getTargetConstant(1, DL, MVT::i1), // idxen 6604 }; 6605 EVT VT = Op.getValueType(); 6606 auto *M = cast<MemSDNode>(Op); 6607 M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[5], Ops[6], Ops[7], 6608 Ops[4])); 6609 6610 return DAG.getMemIntrinsicNode(AMDGPUISD::BUFFER_ATOMIC_CMPSWAP, DL, 6611 Op->getVTList(), Ops, VT, M->getMemOperand()); 6612 } 6613 6614 default: 6615 if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr = 6616 AMDGPU::getImageDimIntrinsicInfo(IntrID)) 6617 return lowerImage(Op, ImageDimIntr, DAG); 6618 6619 return SDValue(); 6620 } 6621 } 6622 6623 // Call DAG.getMemIntrinsicNode for a load, but first widen a dwordx3 type to 6624 // dwordx4 if on SI. 6625 SDValue SITargetLowering::getMemIntrinsicNode(unsigned Opcode, const SDLoc &DL, 6626 SDVTList VTList, 6627 ArrayRef<SDValue> Ops, EVT MemVT, 6628 MachineMemOperand *MMO, 6629 SelectionDAG &DAG) const { 6630 EVT VT = VTList.VTs[0]; 6631 EVT WidenedVT = VT; 6632 EVT WidenedMemVT = MemVT; 6633 if (!Subtarget->hasDwordx3LoadStores() && 6634 (WidenedVT == MVT::v3i32 || WidenedVT == MVT::v3f32)) { 6635 WidenedVT = EVT::getVectorVT(*DAG.getContext(), 6636 WidenedVT.getVectorElementType(), 4); 6637 WidenedMemVT = EVT::getVectorVT(*DAG.getContext(), 6638 WidenedMemVT.getVectorElementType(), 4); 6639 MMO = DAG.getMachineFunction().getMachineMemOperand(MMO, 0, 16); 6640 } 6641 6642 assert(VTList.NumVTs == 2); 6643 SDVTList WidenedVTList = DAG.getVTList(WidenedVT, VTList.VTs[1]); 6644 6645 auto NewOp = DAG.getMemIntrinsicNode(Opcode, DL, WidenedVTList, Ops, 6646 WidenedMemVT, MMO); 6647 if (WidenedVT != VT) { 6648 auto Extract = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, NewOp, 6649 DAG.getVectorIdxConstant(0, DL)); 6650 NewOp = DAG.getMergeValues({ Extract, SDValue(NewOp.getNode(), 1) }, DL); 6651 } 6652 return NewOp; 6653 } 6654 6655 SDValue SITargetLowering::handleD16VData(SDValue VData, 6656 SelectionDAG &DAG) const { 6657 EVT StoreVT = VData.getValueType(); 6658 6659 // No change for f16 and legal vector D16 types. 6660 if (!StoreVT.isVector()) 6661 return VData; 6662 6663 SDLoc DL(VData); 6664 assert((StoreVT.getVectorNumElements() != 3) && "Handle v3f16"); 6665 6666 if (Subtarget->hasUnpackedD16VMem()) { 6667 // We need to unpack the packed data to store. 6668 EVT IntStoreVT = StoreVT.changeTypeToInteger(); 6669 SDValue IntVData = DAG.getNode(ISD::BITCAST, DL, IntStoreVT, VData); 6670 6671 EVT EquivStoreVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, 6672 StoreVT.getVectorNumElements()); 6673 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, EquivStoreVT, IntVData); 6674 return DAG.UnrollVectorOp(ZExt.getNode()); 6675 } 6676 6677 assert(isTypeLegal(StoreVT)); 6678 return VData; 6679 } 6680 6681 SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op, 6682 SelectionDAG &DAG) const { 6683 SDLoc DL(Op); 6684 SDValue Chain = Op.getOperand(0); 6685 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 6686 MachineFunction &MF = DAG.getMachineFunction(); 6687 6688 switch (IntrinsicID) { 6689 case Intrinsic::amdgcn_exp_compr: { 6690 SDValue Src0 = Op.getOperand(4); 6691 SDValue Src1 = Op.getOperand(5); 6692 // Hack around illegal type on SI by directly selecting it. 6693 if (isTypeLegal(Src0.getValueType())) 6694 return SDValue(); 6695 6696 const ConstantSDNode *Done = cast<ConstantSDNode>(Op.getOperand(6)); 6697 SDValue Undef = DAG.getUNDEF(MVT::f32); 6698 const SDValue Ops[] = { 6699 Op.getOperand(2), // tgt 6700 DAG.getNode(ISD::BITCAST, DL, MVT::f32, Src0), // src0 6701 DAG.getNode(ISD::BITCAST, DL, MVT::f32, Src1), // src1 6702 Undef, // src2 6703 Undef, // src3 6704 Op.getOperand(7), // vm 6705 DAG.getTargetConstant(1, DL, MVT::i1), // compr 6706 Op.getOperand(3), // en 6707 Op.getOperand(0) // Chain 6708 }; 6709 6710 unsigned Opc = Done->isNullValue() ? AMDGPU::EXP : AMDGPU::EXP_DONE; 6711 return SDValue(DAG.getMachineNode(Opc, DL, Op->getVTList(), Ops), 0); 6712 } 6713 case Intrinsic::amdgcn_s_barrier: { 6714 if (getTargetMachine().getOptLevel() > CodeGenOpt::None) { 6715 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 6716 unsigned WGSize = ST.getFlatWorkGroupSizes(MF.getFunction()).second; 6717 if (WGSize <= ST.getWavefrontSize()) 6718 return SDValue(DAG.getMachineNode(AMDGPU::WAVE_BARRIER, DL, MVT::Other, 6719 Op.getOperand(0)), 0); 6720 } 6721 return SDValue(); 6722 }; 6723 case Intrinsic::amdgcn_tbuffer_store: { 6724 SDValue VData = Op.getOperand(2); 6725 bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16); 6726 if (IsD16) 6727 VData = handleD16VData(VData, DAG); 6728 unsigned Dfmt = cast<ConstantSDNode>(Op.getOperand(8))->getZExtValue(); 6729 unsigned Nfmt = cast<ConstantSDNode>(Op.getOperand(9))->getZExtValue(); 6730 unsigned Glc = cast<ConstantSDNode>(Op.getOperand(10))->getZExtValue(); 6731 unsigned Slc = cast<ConstantSDNode>(Op.getOperand(11))->getZExtValue(); 6732 unsigned IdxEn = 1; 6733 if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(4))) 6734 IdxEn = Idx->getZExtValue() != 0; 6735 SDValue Ops[] = { 6736 Chain, 6737 VData, // vdata 6738 Op.getOperand(3), // rsrc 6739 Op.getOperand(4), // vindex 6740 Op.getOperand(5), // voffset 6741 Op.getOperand(6), // soffset 6742 Op.getOperand(7), // offset 6743 DAG.getTargetConstant(Dfmt | (Nfmt << 4), DL, MVT::i32), // format 6744 DAG.getTargetConstant(Glc | (Slc << 1), DL, MVT::i32), // cachepolicy 6745 DAG.getTargetConstant(IdxEn, DL, MVT::i1), // idexen 6746 }; 6747 unsigned Opc = IsD16 ? AMDGPUISD::TBUFFER_STORE_FORMAT_D16 : 6748 AMDGPUISD::TBUFFER_STORE_FORMAT; 6749 MemSDNode *M = cast<MemSDNode>(Op); 6750 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, 6751 M->getMemoryVT(), M->getMemOperand()); 6752 } 6753 6754 case Intrinsic::amdgcn_struct_tbuffer_store: { 6755 SDValue VData = Op.getOperand(2); 6756 bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16); 6757 if (IsD16) 6758 VData = handleD16VData(VData, DAG); 6759 auto Offsets = splitBufferOffsets(Op.getOperand(5), DAG); 6760 SDValue Ops[] = { 6761 Chain, 6762 VData, // vdata 6763 Op.getOperand(3), // rsrc 6764 Op.getOperand(4), // vindex 6765 Offsets.first, // voffset 6766 Op.getOperand(6), // soffset 6767 Offsets.second, // offset 6768 Op.getOperand(7), // format 6769 Op.getOperand(8), // cachepolicy, swizzled buffer 6770 DAG.getTargetConstant(1, DL, MVT::i1), // idexen 6771 }; 6772 unsigned Opc = IsD16 ? AMDGPUISD::TBUFFER_STORE_FORMAT_D16 : 6773 AMDGPUISD::TBUFFER_STORE_FORMAT; 6774 MemSDNode *M = cast<MemSDNode>(Op); 6775 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, 6776 M->getMemoryVT(), M->getMemOperand()); 6777 } 6778 6779 case Intrinsic::amdgcn_raw_tbuffer_store: { 6780 SDValue VData = Op.getOperand(2); 6781 bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16); 6782 if (IsD16) 6783 VData = handleD16VData(VData, DAG); 6784 auto Offsets = splitBufferOffsets(Op.getOperand(4), DAG); 6785 SDValue Ops[] = { 6786 Chain, 6787 VData, // vdata 6788 Op.getOperand(3), // rsrc 6789 DAG.getConstant(0, DL, MVT::i32), // vindex 6790 Offsets.first, // voffset 6791 Op.getOperand(5), // soffset 6792 Offsets.second, // offset 6793 Op.getOperand(6), // format 6794 Op.getOperand(7), // cachepolicy, swizzled buffer 6795 DAG.getTargetConstant(0, DL, MVT::i1), // idexen 6796 }; 6797 unsigned Opc = IsD16 ? AMDGPUISD::TBUFFER_STORE_FORMAT_D16 : 6798 AMDGPUISD::TBUFFER_STORE_FORMAT; 6799 MemSDNode *M = cast<MemSDNode>(Op); 6800 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, 6801 M->getMemoryVT(), M->getMemOperand()); 6802 } 6803 6804 case Intrinsic::amdgcn_buffer_store: 6805 case Intrinsic::amdgcn_buffer_store_format: { 6806 SDValue VData = Op.getOperand(2); 6807 bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16); 6808 if (IsD16) 6809 VData = handleD16VData(VData, DAG); 6810 unsigned Glc = cast<ConstantSDNode>(Op.getOperand(6))->getZExtValue(); 6811 unsigned Slc = cast<ConstantSDNode>(Op.getOperand(7))->getZExtValue(); 6812 unsigned IdxEn = 1; 6813 if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(4))) 6814 IdxEn = Idx->getZExtValue() != 0; 6815 SDValue Ops[] = { 6816 Chain, 6817 VData, 6818 Op.getOperand(3), // rsrc 6819 Op.getOperand(4), // vindex 6820 SDValue(), // voffset -- will be set by setBufferOffsets 6821 SDValue(), // soffset -- will be set by setBufferOffsets 6822 SDValue(), // offset -- will be set by setBufferOffsets 6823 DAG.getTargetConstant(Glc | (Slc << 1), DL, MVT::i32), // cachepolicy 6824 DAG.getTargetConstant(IdxEn, DL, MVT::i1), // idxen 6825 }; 6826 unsigned Offset = setBufferOffsets(Op.getOperand(5), DAG, &Ops[4]); 6827 // We don't know the offset if vindex is non-zero, so clear it. 6828 if (IdxEn) 6829 Offset = 0; 6830 unsigned Opc = IntrinsicID == Intrinsic::amdgcn_buffer_store ? 6831 AMDGPUISD::BUFFER_STORE : AMDGPUISD::BUFFER_STORE_FORMAT; 6832 Opc = IsD16 ? AMDGPUISD::BUFFER_STORE_FORMAT_D16 : Opc; 6833 MemSDNode *M = cast<MemSDNode>(Op); 6834 M->getMemOperand()->setOffset(Offset); 6835 6836 // Handle BUFFER_STORE_BYTE/SHORT overloaded intrinsics 6837 EVT VDataType = VData.getValueType().getScalarType(); 6838 if (VDataType == MVT::i8 || VDataType == MVT::i16) 6839 return handleByteShortBufferStores(DAG, VDataType, DL, Ops, M); 6840 6841 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, 6842 M->getMemoryVT(), M->getMemOperand()); 6843 } 6844 6845 case Intrinsic::amdgcn_raw_buffer_store: 6846 case Intrinsic::amdgcn_raw_buffer_store_format: { 6847 const bool IsFormat = 6848 IntrinsicID == Intrinsic::amdgcn_raw_buffer_store_format; 6849 6850 SDValue VData = Op.getOperand(2); 6851 EVT VDataVT = VData.getValueType(); 6852 EVT EltType = VDataVT.getScalarType(); 6853 bool IsD16 = IsFormat && (EltType.getSizeInBits() == 16); 6854 if (IsD16) 6855 VData = handleD16VData(VData, DAG); 6856 6857 if (!isTypeLegal(VDataVT)) { 6858 VData = 6859 DAG.getNode(ISD::BITCAST, DL, 6860 getEquivalentMemType(*DAG.getContext(), VDataVT), VData); 6861 } 6862 6863 auto Offsets = splitBufferOffsets(Op.getOperand(4), DAG); 6864 SDValue Ops[] = { 6865 Chain, 6866 VData, 6867 Op.getOperand(3), // rsrc 6868 DAG.getConstant(0, DL, MVT::i32), // vindex 6869 Offsets.first, // voffset 6870 Op.getOperand(5), // soffset 6871 Offsets.second, // offset 6872 Op.getOperand(6), // cachepolicy, swizzled buffer 6873 DAG.getTargetConstant(0, DL, MVT::i1), // idxen 6874 }; 6875 unsigned Opc = 6876 IsFormat ? AMDGPUISD::BUFFER_STORE_FORMAT : AMDGPUISD::BUFFER_STORE; 6877 Opc = IsD16 ? AMDGPUISD::BUFFER_STORE_FORMAT_D16 : Opc; 6878 MemSDNode *M = cast<MemSDNode>(Op); 6879 M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[4], Ops[5], Ops[6])); 6880 6881 // Handle BUFFER_STORE_BYTE/SHORT overloaded intrinsics 6882 if (!IsD16 && !VDataVT.isVector() && EltType.getSizeInBits() < 32) 6883 return handleByteShortBufferStores(DAG, VDataVT, DL, Ops, M); 6884 6885 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, 6886 M->getMemoryVT(), M->getMemOperand()); 6887 } 6888 6889 case Intrinsic::amdgcn_struct_buffer_store: 6890 case Intrinsic::amdgcn_struct_buffer_store_format: { 6891 const bool IsFormat = 6892 IntrinsicID == Intrinsic::amdgcn_struct_buffer_store_format; 6893 6894 SDValue VData = Op.getOperand(2); 6895 EVT VDataVT = VData.getValueType(); 6896 EVT EltType = VDataVT.getScalarType(); 6897 bool IsD16 = IsFormat && (EltType.getSizeInBits() == 16); 6898 6899 if (IsD16) 6900 VData = handleD16VData(VData, DAG); 6901 6902 if (!isTypeLegal(VDataVT)) { 6903 VData = 6904 DAG.getNode(ISD::BITCAST, DL, 6905 getEquivalentMemType(*DAG.getContext(), VDataVT), VData); 6906 } 6907 6908 auto Offsets = splitBufferOffsets(Op.getOperand(5), DAG); 6909 SDValue Ops[] = { 6910 Chain, 6911 VData, 6912 Op.getOperand(3), // rsrc 6913 Op.getOperand(4), // vindex 6914 Offsets.first, // voffset 6915 Op.getOperand(6), // soffset 6916 Offsets.second, // offset 6917 Op.getOperand(7), // cachepolicy, swizzled buffer 6918 DAG.getTargetConstant(1, DL, MVT::i1), // idxen 6919 }; 6920 unsigned Opc = IntrinsicID == Intrinsic::amdgcn_struct_buffer_store ? 6921 AMDGPUISD::BUFFER_STORE : AMDGPUISD::BUFFER_STORE_FORMAT; 6922 Opc = IsD16 ? AMDGPUISD::BUFFER_STORE_FORMAT_D16 : Opc; 6923 MemSDNode *M = cast<MemSDNode>(Op); 6924 M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[4], Ops[5], Ops[6], 6925 Ops[3])); 6926 6927 // Handle BUFFER_STORE_BYTE/SHORT overloaded intrinsics 6928 EVT VDataType = VData.getValueType().getScalarType(); 6929 if (!IsD16 && !VDataVT.isVector() && EltType.getSizeInBits() < 32) 6930 return handleByteShortBufferStores(DAG, VDataType, DL, Ops, M); 6931 6932 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, 6933 M->getMemoryVT(), M->getMemOperand()); 6934 } 6935 6936 case Intrinsic::amdgcn_buffer_atomic_fadd: { 6937 unsigned Slc = cast<ConstantSDNode>(Op.getOperand(6))->getZExtValue(); 6938 unsigned IdxEn = 1; 6939 if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(4))) 6940 IdxEn = Idx->getZExtValue() != 0; 6941 SDValue Ops[] = { 6942 Chain, 6943 Op.getOperand(2), // vdata 6944 Op.getOperand(3), // rsrc 6945 Op.getOperand(4), // vindex 6946 SDValue(), // voffset -- will be set by setBufferOffsets 6947 SDValue(), // soffset -- will be set by setBufferOffsets 6948 SDValue(), // offset -- will be set by setBufferOffsets 6949 DAG.getTargetConstant(Slc << 1, DL, MVT::i32), // cachepolicy 6950 DAG.getTargetConstant(IdxEn, DL, MVT::i1), // idxen 6951 }; 6952 unsigned Offset = setBufferOffsets(Op.getOperand(5), DAG, &Ops[4]); 6953 // We don't know the offset if vindex is non-zero, so clear it. 6954 if (IdxEn) 6955 Offset = 0; 6956 EVT VT = Op.getOperand(2).getValueType(); 6957 6958 auto *M = cast<MemSDNode>(Op); 6959 M->getMemOperand()->setOffset(Offset); 6960 unsigned Opcode = VT.isVector() ? AMDGPUISD::BUFFER_ATOMIC_PK_FADD 6961 : AMDGPUISD::BUFFER_ATOMIC_FADD; 6962 6963 return DAG.getMemIntrinsicNode(Opcode, DL, Op->getVTList(), Ops, VT, 6964 M->getMemOperand()); 6965 } 6966 6967 case Intrinsic::amdgcn_global_atomic_fadd: { 6968 SDValue Ops[] = { 6969 Chain, 6970 Op.getOperand(2), // ptr 6971 Op.getOperand(3) // vdata 6972 }; 6973 EVT VT = Op.getOperand(3).getValueType(); 6974 6975 auto *M = cast<MemSDNode>(Op); 6976 if (VT.isVector()) { 6977 return DAG.getMemIntrinsicNode( 6978 AMDGPUISD::ATOMIC_PK_FADD, DL, Op->getVTList(), Ops, VT, 6979 M->getMemOperand()); 6980 } 6981 6982 return DAG.getAtomic(ISD::ATOMIC_LOAD_FADD, DL, VT, 6983 DAG.getVTList(VT, MVT::Other), Ops, 6984 M->getMemOperand()).getValue(1); 6985 } 6986 case Intrinsic::amdgcn_end_cf: 6987 return SDValue(DAG.getMachineNode(AMDGPU::SI_END_CF, DL, MVT::Other, 6988 Op->getOperand(2), Chain), 0); 6989 6990 default: { 6991 if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr = 6992 AMDGPU::getImageDimIntrinsicInfo(IntrinsicID)) 6993 return lowerImage(Op, ImageDimIntr, DAG); 6994 6995 return Op; 6996 } 6997 } 6998 } 6999 7000 // The raw.(t)buffer and struct.(t)buffer intrinsics have two offset args: 7001 // offset (the offset that is included in bounds checking and swizzling, to be 7002 // split between the instruction's voffset and immoffset fields) and soffset 7003 // (the offset that is excluded from bounds checking and swizzling, to go in 7004 // the instruction's soffset field). This function takes the first kind of 7005 // offset and figures out how to split it between voffset and immoffset. 7006 std::pair<SDValue, SDValue> SITargetLowering::splitBufferOffsets( 7007 SDValue Offset, SelectionDAG &DAG) const { 7008 SDLoc DL(Offset); 7009 const unsigned MaxImm = 4095; 7010 SDValue N0 = Offset; 7011 ConstantSDNode *C1 = nullptr; 7012 7013 if ((C1 = dyn_cast<ConstantSDNode>(N0))) 7014 N0 = SDValue(); 7015 else if (DAG.isBaseWithConstantOffset(N0)) { 7016 C1 = cast<ConstantSDNode>(N0.getOperand(1)); 7017 N0 = N0.getOperand(0); 7018 } 7019 7020 if (C1) { 7021 unsigned ImmOffset = C1->getZExtValue(); 7022 // If the immediate value is too big for the immoffset field, put the value 7023 // and -4096 into the immoffset field so that the value that is copied/added 7024 // for the voffset field is a multiple of 4096, and it stands more chance 7025 // of being CSEd with the copy/add for another similar load/store. 7026 // However, do not do that rounding down to a multiple of 4096 if that is a 7027 // negative number, as it appears to be illegal to have a negative offset 7028 // in the vgpr, even if adding the immediate offset makes it positive. 7029 unsigned Overflow = ImmOffset & ~MaxImm; 7030 ImmOffset -= Overflow; 7031 if ((int32_t)Overflow < 0) { 7032 Overflow += ImmOffset; 7033 ImmOffset = 0; 7034 } 7035 C1 = cast<ConstantSDNode>(DAG.getTargetConstant(ImmOffset, DL, MVT::i32)); 7036 if (Overflow) { 7037 auto OverflowVal = DAG.getConstant(Overflow, DL, MVT::i32); 7038 if (!N0) 7039 N0 = OverflowVal; 7040 else { 7041 SDValue Ops[] = { N0, OverflowVal }; 7042 N0 = DAG.getNode(ISD::ADD, DL, MVT::i32, Ops); 7043 } 7044 } 7045 } 7046 if (!N0) 7047 N0 = DAG.getConstant(0, DL, MVT::i32); 7048 if (!C1) 7049 C1 = cast<ConstantSDNode>(DAG.getTargetConstant(0, DL, MVT::i32)); 7050 return {N0, SDValue(C1, 0)}; 7051 } 7052 7053 // Analyze a combined offset from an amdgcn_buffer_ intrinsic and store the 7054 // three offsets (voffset, soffset and instoffset) into the SDValue[3] array 7055 // pointed to by Offsets. 7056 unsigned SITargetLowering::setBufferOffsets(SDValue CombinedOffset, 7057 SelectionDAG &DAG, SDValue *Offsets, 7058 unsigned Align) const { 7059 SDLoc DL(CombinedOffset); 7060 if (auto C = dyn_cast<ConstantSDNode>(CombinedOffset)) { 7061 uint32_t Imm = C->getZExtValue(); 7062 uint32_t SOffset, ImmOffset; 7063 if (AMDGPU::splitMUBUFOffset(Imm, SOffset, ImmOffset, Subtarget, Align)) { 7064 Offsets[0] = DAG.getConstant(0, DL, MVT::i32); 7065 Offsets[1] = DAG.getConstant(SOffset, DL, MVT::i32); 7066 Offsets[2] = DAG.getTargetConstant(ImmOffset, DL, MVT::i32); 7067 return SOffset + ImmOffset; 7068 } 7069 } 7070 if (DAG.isBaseWithConstantOffset(CombinedOffset)) { 7071 SDValue N0 = CombinedOffset.getOperand(0); 7072 SDValue N1 = CombinedOffset.getOperand(1); 7073 uint32_t SOffset, ImmOffset; 7074 int Offset = cast<ConstantSDNode>(N1)->getSExtValue(); 7075 if (Offset >= 0 && AMDGPU::splitMUBUFOffset(Offset, SOffset, ImmOffset, 7076 Subtarget, Align)) { 7077 Offsets[0] = N0; 7078 Offsets[1] = DAG.getConstant(SOffset, DL, MVT::i32); 7079 Offsets[2] = DAG.getTargetConstant(ImmOffset, DL, MVT::i32); 7080 return 0; 7081 } 7082 } 7083 Offsets[0] = CombinedOffset; 7084 Offsets[1] = DAG.getConstant(0, DL, MVT::i32); 7085 Offsets[2] = DAG.getTargetConstant(0, DL, MVT::i32); 7086 return 0; 7087 } 7088 7089 // Handle 8 bit and 16 bit buffer loads 7090 SDValue SITargetLowering::handleByteShortBufferLoads(SelectionDAG &DAG, 7091 EVT LoadVT, SDLoc DL, 7092 ArrayRef<SDValue> Ops, 7093 MemSDNode *M) const { 7094 EVT IntVT = LoadVT.changeTypeToInteger(); 7095 unsigned Opc = (LoadVT.getScalarType() == MVT::i8) ? 7096 AMDGPUISD::BUFFER_LOAD_UBYTE : AMDGPUISD::BUFFER_LOAD_USHORT; 7097 7098 SDVTList ResList = DAG.getVTList(MVT::i32, MVT::Other); 7099 SDValue BufferLoad = DAG.getMemIntrinsicNode(Opc, DL, ResList, 7100 Ops, IntVT, 7101 M->getMemOperand()); 7102 SDValue LoadVal = DAG.getNode(ISD::TRUNCATE, DL, IntVT, BufferLoad); 7103 LoadVal = DAG.getNode(ISD::BITCAST, DL, LoadVT, LoadVal); 7104 7105 return DAG.getMergeValues({LoadVal, BufferLoad.getValue(1)}, DL); 7106 } 7107 7108 // Handle 8 bit and 16 bit buffer stores 7109 SDValue SITargetLowering::handleByteShortBufferStores(SelectionDAG &DAG, 7110 EVT VDataType, SDLoc DL, 7111 SDValue Ops[], 7112 MemSDNode *M) const { 7113 if (VDataType == MVT::f16) 7114 Ops[1] = DAG.getNode(ISD::BITCAST, DL, MVT::i16, Ops[1]); 7115 7116 SDValue BufferStoreExt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Ops[1]); 7117 Ops[1] = BufferStoreExt; 7118 unsigned Opc = (VDataType == MVT::i8) ? AMDGPUISD::BUFFER_STORE_BYTE : 7119 AMDGPUISD::BUFFER_STORE_SHORT; 7120 ArrayRef<SDValue> OpsRef = makeArrayRef(&Ops[0], 9); 7121 return DAG.getMemIntrinsicNode(Opc, DL, M->getVTList(), OpsRef, VDataType, 7122 M->getMemOperand()); 7123 } 7124 7125 static SDValue getLoadExtOrTrunc(SelectionDAG &DAG, 7126 ISD::LoadExtType ExtType, SDValue Op, 7127 const SDLoc &SL, EVT VT) { 7128 if (VT.bitsLT(Op.getValueType())) 7129 return DAG.getNode(ISD::TRUNCATE, SL, VT, Op); 7130 7131 switch (ExtType) { 7132 case ISD::SEXTLOAD: 7133 return DAG.getNode(ISD::SIGN_EXTEND, SL, VT, Op); 7134 case ISD::ZEXTLOAD: 7135 return DAG.getNode(ISD::ZERO_EXTEND, SL, VT, Op); 7136 case ISD::EXTLOAD: 7137 return DAG.getNode(ISD::ANY_EXTEND, SL, VT, Op); 7138 case ISD::NON_EXTLOAD: 7139 return Op; 7140 } 7141 7142 llvm_unreachable("invalid ext type"); 7143 } 7144 7145 SDValue SITargetLowering::widenLoad(LoadSDNode *Ld, DAGCombinerInfo &DCI) const { 7146 SelectionDAG &DAG = DCI.DAG; 7147 if (Ld->getAlignment() < 4 || Ld->isDivergent()) 7148 return SDValue(); 7149 7150 // FIXME: Constant loads should all be marked invariant. 7151 unsigned AS = Ld->getAddressSpace(); 7152 if (AS != AMDGPUAS::CONSTANT_ADDRESS && 7153 AS != AMDGPUAS::CONSTANT_ADDRESS_32BIT && 7154 (AS != AMDGPUAS::GLOBAL_ADDRESS || !Ld->isInvariant())) 7155 return SDValue(); 7156 7157 // Don't do this early, since it may interfere with adjacent load merging for 7158 // illegal types. We can avoid losing alignment information for exotic types 7159 // pre-legalize. 7160 EVT MemVT = Ld->getMemoryVT(); 7161 if ((MemVT.isSimple() && !DCI.isAfterLegalizeDAG()) || 7162 MemVT.getSizeInBits() >= 32) 7163 return SDValue(); 7164 7165 SDLoc SL(Ld); 7166 7167 assert((!MemVT.isVector() || Ld->getExtensionType() == ISD::NON_EXTLOAD) && 7168 "unexpected vector extload"); 7169 7170 // TODO: Drop only high part of range. 7171 SDValue Ptr = Ld->getBasePtr(); 7172 SDValue NewLoad = DAG.getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, 7173 MVT::i32, SL, Ld->getChain(), Ptr, 7174 Ld->getOffset(), 7175 Ld->getPointerInfo(), MVT::i32, 7176 Ld->getAlignment(), 7177 Ld->getMemOperand()->getFlags(), 7178 Ld->getAAInfo(), 7179 nullptr); // Drop ranges 7180 7181 EVT TruncVT = EVT::getIntegerVT(*DAG.getContext(), MemVT.getSizeInBits()); 7182 if (MemVT.isFloatingPoint()) { 7183 assert(Ld->getExtensionType() == ISD::NON_EXTLOAD && 7184 "unexpected fp extload"); 7185 TruncVT = MemVT.changeTypeToInteger(); 7186 } 7187 7188 SDValue Cvt = NewLoad; 7189 if (Ld->getExtensionType() == ISD::SEXTLOAD) { 7190 Cvt = DAG.getNode(ISD::SIGN_EXTEND_INREG, SL, MVT::i32, NewLoad, 7191 DAG.getValueType(TruncVT)); 7192 } else if (Ld->getExtensionType() == ISD::ZEXTLOAD || 7193 Ld->getExtensionType() == ISD::NON_EXTLOAD) { 7194 Cvt = DAG.getZeroExtendInReg(NewLoad, SL, TruncVT); 7195 } else { 7196 assert(Ld->getExtensionType() == ISD::EXTLOAD); 7197 } 7198 7199 EVT VT = Ld->getValueType(0); 7200 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits()); 7201 7202 DCI.AddToWorklist(Cvt.getNode()); 7203 7204 // We may need to handle exotic cases, such as i16->i64 extloads, so insert 7205 // the appropriate extension from the 32-bit load. 7206 Cvt = getLoadExtOrTrunc(DAG, Ld->getExtensionType(), Cvt, SL, IntVT); 7207 DCI.AddToWorklist(Cvt.getNode()); 7208 7209 // Handle conversion back to floating point if necessary. 7210 Cvt = DAG.getNode(ISD::BITCAST, SL, VT, Cvt); 7211 7212 return DAG.getMergeValues({ Cvt, NewLoad.getValue(1) }, SL); 7213 } 7214 7215 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const { 7216 SDLoc DL(Op); 7217 LoadSDNode *Load = cast<LoadSDNode>(Op); 7218 ISD::LoadExtType ExtType = Load->getExtensionType(); 7219 EVT MemVT = Load->getMemoryVT(); 7220 7221 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) { 7222 if (MemVT == MVT::i16 && isTypeLegal(MVT::i16)) 7223 return SDValue(); 7224 7225 // FIXME: Copied from PPC 7226 // First, load into 32 bits, then truncate to 1 bit. 7227 7228 SDValue Chain = Load->getChain(); 7229 SDValue BasePtr = Load->getBasePtr(); 7230 MachineMemOperand *MMO = Load->getMemOperand(); 7231 7232 EVT RealMemVT = (MemVT == MVT::i1) ? MVT::i8 : MVT::i16; 7233 7234 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain, 7235 BasePtr, RealMemVT, MMO); 7236 7237 if (!MemVT.isVector()) { 7238 SDValue Ops[] = { 7239 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD), 7240 NewLD.getValue(1) 7241 }; 7242 7243 return DAG.getMergeValues(Ops, DL); 7244 } 7245 7246 SmallVector<SDValue, 3> Elts; 7247 for (unsigned I = 0, N = MemVT.getVectorNumElements(); I != N; ++I) { 7248 SDValue Elt = DAG.getNode(ISD::SRL, DL, MVT::i32, NewLD, 7249 DAG.getConstant(I, DL, MVT::i32)); 7250 7251 Elts.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Elt)); 7252 } 7253 7254 SDValue Ops[] = { 7255 DAG.getBuildVector(MemVT, DL, Elts), 7256 NewLD.getValue(1) 7257 }; 7258 7259 return DAG.getMergeValues(Ops, DL); 7260 } 7261 7262 if (!MemVT.isVector()) 7263 return SDValue(); 7264 7265 assert(Op.getValueType().getVectorElementType() == MVT::i32 && 7266 "Custom lowering for non-i32 vectors hasn't been implemented."); 7267 7268 if (!allowsMemoryAccessForAlignment(*DAG.getContext(), DAG.getDataLayout(), 7269 MemVT, *Load->getMemOperand())) { 7270 SDValue Ops[2]; 7271 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG); 7272 return DAG.getMergeValues(Ops, DL); 7273 } 7274 7275 unsigned Alignment = Load->getAlignment(); 7276 unsigned AS = Load->getAddressSpace(); 7277 if (Subtarget->hasLDSMisalignedBug() && 7278 AS == AMDGPUAS::FLAT_ADDRESS && 7279 Alignment < MemVT.getStoreSize() && MemVT.getSizeInBits() > 32) { 7280 return SplitVectorLoad(Op, DAG); 7281 } 7282 7283 MachineFunction &MF = DAG.getMachineFunction(); 7284 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 7285 // If there is a possibilty that flat instruction access scratch memory 7286 // then we need to use the same legalization rules we use for private. 7287 if (AS == AMDGPUAS::FLAT_ADDRESS && 7288 !Subtarget->hasMultiDwordFlatScratchAddressing()) 7289 AS = MFI->hasFlatScratchInit() ? 7290 AMDGPUAS::PRIVATE_ADDRESS : AMDGPUAS::GLOBAL_ADDRESS; 7291 7292 unsigned NumElements = MemVT.getVectorNumElements(); 7293 7294 if (AS == AMDGPUAS::CONSTANT_ADDRESS || 7295 AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT) { 7296 if (!Op->isDivergent() && Alignment >= 4 && NumElements < 32) { 7297 if (MemVT.isPow2VectorType()) 7298 return SDValue(); 7299 if (NumElements == 3) 7300 return WidenVectorLoad(Op, DAG); 7301 return SplitVectorLoad(Op, DAG); 7302 } 7303 // Non-uniform loads will be selected to MUBUF instructions, so they 7304 // have the same legalization requirements as global and private 7305 // loads. 7306 // 7307 } 7308 7309 if (AS == AMDGPUAS::CONSTANT_ADDRESS || 7310 AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT || 7311 AS == AMDGPUAS::GLOBAL_ADDRESS) { 7312 if (Subtarget->getScalarizeGlobalBehavior() && !Op->isDivergent() && 7313 !Load->isVolatile() && isMemOpHasNoClobberedMemOperand(Load) && 7314 Alignment >= 4 && NumElements < 32) { 7315 if (MemVT.isPow2VectorType()) 7316 return SDValue(); 7317 if (NumElements == 3) 7318 return WidenVectorLoad(Op, DAG); 7319 return SplitVectorLoad(Op, DAG); 7320 } 7321 // Non-uniform loads will be selected to MUBUF instructions, so they 7322 // have the same legalization requirements as global and private 7323 // loads. 7324 // 7325 } 7326 if (AS == AMDGPUAS::CONSTANT_ADDRESS || 7327 AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT || 7328 AS == AMDGPUAS::GLOBAL_ADDRESS || 7329 AS == AMDGPUAS::FLAT_ADDRESS) { 7330 if (NumElements > 4) 7331 return SplitVectorLoad(Op, DAG); 7332 // v3 loads not supported on SI. 7333 if (NumElements == 3 && !Subtarget->hasDwordx3LoadStores()) 7334 return WidenVectorLoad(Op, DAG); 7335 // v3 and v4 loads are supported for private and global memory. 7336 return SDValue(); 7337 } 7338 if (AS == AMDGPUAS::PRIVATE_ADDRESS) { 7339 // Depending on the setting of the private_element_size field in the 7340 // resource descriptor, we can only make private accesses up to a certain 7341 // size. 7342 switch (Subtarget->getMaxPrivateElementSize()) { 7343 case 4: { 7344 SDValue Ops[2]; 7345 std::tie(Ops[0], Ops[1]) = scalarizeVectorLoad(Load, DAG); 7346 return DAG.getMergeValues(Ops, DL); 7347 } 7348 case 8: 7349 if (NumElements > 2) 7350 return SplitVectorLoad(Op, DAG); 7351 return SDValue(); 7352 case 16: 7353 // Same as global/flat 7354 if (NumElements > 4) 7355 return SplitVectorLoad(Op, DAG); 7356 // v3 loads not supported on SI. 7357 if (NumElements == 3 && !Subtarget->hasDwordx3LoadStores()) 7358 return WidenVectorLoad(Op, DAG); 7359 return SDValue(); 7360 default: 7361 llvm_unreachable("unsupported private_element_size"); 7362 } 7363 } else if (AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::REGION_ADDRESS) { 7364 // Use ds_read_b128 if possible. 7365 if (Subtarget->useDS128() && Load->getAlignment() >= 16 && 7366 MemVT.getStoreSize() == 16) 7367 return SDValue(); 7368 7369 if (NumElements > 2) 7370 return SplitVectorLoad(Op, DAG); 7371 7372 // SI has a hardware bug in the LDS / GDS boounds checking: if the base 7373 // address is negative, then the instruction is incorrectly treated as 7374 // out-of-bounds even if base + offsets is in bounds. Split vectorized 7375 // loads here to avoid emitting ds_read2_b32. We may re-combine the 7376 // load later in the SILoadStoreOptimizer. 7377 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS && 7378 NumElements == 2 && MemVT.getStoreSize() == 8 && 7379 Load->getAlignment() < 8) { 7380 return SplitVectorLoad(Op, DAG); 7381 } 7382 } 7383 return SDValue(); 7384 } 7385 7386 SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 7387 EVT VT = Op.getValueType(); 7388 assert(VT.getSizeInBits() == 64); 7389 7390 SDLoc DL(Op); 7391 SDValue Cond = Op.getOperand(0); 7392 7393 SDValue Zero = DAG.getConstant(0, DL, MVT::i32); 7394 SDValue One = DAG.getConstant(1, DL, MVT::i32); 7395 7396 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1)); 7397 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2)); 7398 7399 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero); 7400 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero); 7401 7402 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1); 7403 7404 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One); 7405 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One); 7406 7407 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1); 7408 7409 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi}); 7410 return DAG.getNode(ISD::BITCAST, DL, VT, Res); 7411 } 7412 7413 // Catch division cases where we can use shortcuts with rcp and rsq 7414 // instructions. 7415 SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op, 7416 SelectionDAG &DAG) const { 7417 SDLoc SL(Op); 7418 SDValue LHS = Op.getOperand(0); 7419 SDValue RHS = Op.getOperand(1); 7420 EVT VT = Op.getValueType(); 7421 const SDNodeFlags Flags = Op->getFlags(); 7422 7423 bool AllowInaccurateRcp = DAG.getTarget().Options.UnsafeFPMath || 7424 Flags.hasApproximateFuncs(); 7425 7426 // Without !fpmath accuracy information, we can't do more because we don't 7427 // know exactly whether rcp is accurate enough to meet !fpmath requirement. 7428 if (!AllowInaccurateRcp) 7429 return SDValue(); 7430 7431 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) { 7432 if (CLHS->isExactlyValue(1.0)) { 7433 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to 7434 // the CI documentation has a worst case error of 1 ulp. 7435 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to 7436 // use it as long as we aren't trying to use denormals. 7437 // 7438 // v_rcp_f16 and v_rsq_f16 DO support denormals. 7439 7440 // 1.0 / sqrt(x) -> rsq(x) 7441 7442 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP 7443 // error seems really high at 2^29 ULP. 7444 if (RHS.getOpcode() == ISD::FSQRT) 7445 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0)); 7446 7447 // 1.0 / x -> rcp(x) 7448 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); 7449 } 7450 7451 // Same as for 1.0, but expand the sign out of the constant. 7452 if (CLHS->isExactlyValue(-1.0)) { 7453 // -1.0 / x -> rcp (fneg x) 7454 SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 7455 return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS); 7456 } 7457 } 7458 7459 // Turn into multiply by the reciprocal. 7460 // x / y -> x * (1.0 / y) 7461 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); 7462 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, Flags); 7463 } 7464 7465 static SDValue getFPBinOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL, 7466 EVT VT, SDValue A, SDValue B, SDValue GlueChain) { 7467 if (GlueChain->getNumValues() <= 1) { 7468 return DAG.getNode(Opcode, SL, VT, A, B); 7469 } 7470 7471 assert(GlueChain->getNumValues() == 3); 7472 7473 SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue); 7474 switch (Opcode) { 7475 default: llvm_unreachable("no chain equivalent for opcode"); 7476 case ISD::FMUL: 7477 Opcode = AMDGPUISD::FMUL_W_CHAIN; 7478 break; 7479 } 7480 7481 return DAG.getNode(Opcode, SL, VTList, GlueChain.getValue(1), A, B, 7482 GlueChain.getValue(2)); 7483 } 7484 7485 static SDValue getFPTernOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL, 7486 EVT VT, SDValue A, SDValue B, SDValue C, 7487 SDValue GlueChain) { 7488 if (GlueChain->getNumValues() <= 1) { 7489 return DAG.getNode(Opcode, SL, VT, A, B, C); 7490 } 7491 7492 assert(GlueChain->getNumValues() == 3); 7493 7494 SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue); 7495 switch (Opcode) { 7496 default: llvm_unreachable("no chain equivalent for opcode"); 7497 case ISD::FMA: 7498 Opcode = AMDGPUISD::FMA_W_CHAIN; 7499 break; 7500 } 7501 7502 return DAG.getNode(Opcode, SL, VTList, GlueChain.getValue(1), A, B, C, 7503 GlueChain.getValue(2)); 7504 } 7505 7506 SDValue SITargetLowering::LowerFDIV16(SDValue Op, SelectionDAG &DAG) const { 7507 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG)) 7508 return FastLowered; 7509 7510 SDLoc SL(Op); 7511 SDValue Src0 = Op.getOperand(0); 7512 SDValue Src1 = Op.getOperand(1); 7513 7514 SDValue CvtSrc0 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src0); 7515 SDValue CvtSrc1 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src1); 7516 7517 SDValue RcpSrc1 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, CvtSrc1); 7518 SDValue Quot = DAG.getNode(ISD::FMUL, SL, MVT::f32, CvtSrc0, RcpSrc1); 7519 7520 SDValue FPRoundFlag = DAG.getTargetConstant(0, SL, MVT::i32); 7521 SDValue BestQuot = DAG.getNode(ISD::FP_ROUND, SL, MVT::f16, Quot, FPRoundFlag); 7522 7523 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f16, BestQuot, Src1, Src0); 7524 } 7525 7526 // Faster 2.5 ULP division that does not support denormals. 7527 SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const { 7528 SDLoc SL(Op); 7529 SDValue LHS = Op.getOperand(1); 7530 SDValue RHS = Op.getOperand(2); 7531 7532 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS); 7533 7534 const APFloat K0Val(BitsToFloat(0x6f800000)); 7535 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32); 7536 7537 const APFloat K1Val(BitsToFloat(0x2f800000)); 7538 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32); 7539 7540 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); 7541 7542 EVT SetCCVT = 7543 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32); 7544 7545 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT); 7546 7547 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One); 7548 7549 // TODO: Should this propagate fast-math-flags? 7550 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3); 7551 7552 // rcp does not support denormals. 7553 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1); 7554 7555 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0); 7556 7557 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul); 7558 } 7559 7560 // Returns immediate value for setting the F32 denorm mode when using the 7561 // S_DENORM_MODE instruction. 7562 static const SDValue getSPDenormModeValue(int SPDenormMode, SelectionDAG &DAG, 7563 const SDLoc &SL, const GCNSubtarget *ST) { 7564 assert(ST->hasDenormModeInst() && "Requires S_DENORM_MODE"); 7565 int DPDenormModeDefault = hasFP64FP16Denormals(DAG.getMachineFunction()) 7566 ? FP_DENORM_FLUSH_NONE 7567 : FP_DENORM_FLUSH_IN_FLUSH_OUT; 7568 7569 int Mode = SPDenormMode | (DPDenormModeDefault << 2); 7570 return DAG.getTargetConstant(Mode, SL, MVT::i32); 7571 } 7572 7573 SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const { 7574 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG)) 7575 return FastLowered; 7576 7577 SDLoc SL(Op); 7578 SDValue LHS = Op.getOperand(0); 7579 SDValue RHS = Op.getOperand(1); 7580 7581 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); 7582 7583 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1); 7584 7585 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, 7586 RHS, RHS, LHS); 7587 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, 7588 LHS, RHS, LHS); 7589 7590 // Denominator is scaled to not be denormal, so using rcp is ok. 7591 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, 7592 DenominatorScaled); 7593 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32, 7594 DenominatorScaled); 7595 7596 const unsigned Denorm32Reg = AMDGPU::Hwreg::ID_MODE | 7597 (4 << AMDGPU::Hwreg::OFFSET_SHIFT_) | 7598 (1 << AMDGPU::Hwreg::WIDTH_M1_SHIFT_); 7599 const SDValue BitField = DAG.getTargetConstant(Denorm32Reg, SL, MVT::i16); 7600 7601 const bool HasFP32Denormals = hasFP32Denormals(DAG.getMachineFunction()); 7602 7603 if (!HasFP32Denormals) { 7604 SDVTList BindParamVTs = DAG.getVTList(MVT::Other, MVT::Glue); 7605 7606 SDValue EnableDenorm; 7607 if (Subtarget->hasDenormModeInst()) { 7608 const SDValue EnableDenormValue = 7609 getSPDenormModeValue(FP_DENORM_FLUSH_NONE, DAG, SL, Subtarget); 7610 7611 EnableDenorm = DAG.getNode(AMDGPUISD::DENORM_MODE, SL, BindParamVTs, 7612 DAG.getEntryNode(), EnableDenormValue); 7613 } else { 7614 const SDValue EnableDenormValue = DAG.getConstant(FP_DENORM_FLUSH_NONE, 7615 SL, MVT::i32); 7616 EnableDenorm = DAG.getNode(AMDGPUISD::SETREG, SL, BindParamVTs, 7617 DAG.getEntryNode(), EnableDenormValue, 7618 BitField); 7619 } 7620 7621 SDValue Ops[3] = { 7622 NegDivScale0, 7623 EnableDenorm.getValue(0), 7624 EnableDenorm.getValue(1) 7625 }; 7626 7627 NegDivScale0 = DAG.getMergeValues(Ops, SL); 7628 } 7629 7630 SDValue Fma0 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, 7631 ApproxRcp, One, NegDivScale0); 7632 7633 SDValue Fma1 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp, 7634 ApproxRcp, Fma0); 7635 7636 SDValue Mul = getFPBinOp(DAG, ISD::FMUL, SL, MVT::f32, NumeratorScaled, 7637 Fma1, Fma1); 7638 7639 SDValue Fma2 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Mul, 7640 NumeratorScaled, Mul); 7641 7642 SDValue Fma3 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, Fma2, Fma1, Mul, Fma2); 7643 7644 SDValue Fma4 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3, 7645 NumeratorScaled, Fma3); 7646 7647 if (!HasFP32Denormals) { 7648 SDValue DisableDenorm; 7649 if (Subtarget->hasDenormModeInst()) { 7650 const SDValue DisableDenormValue = 7651 getSPDenormModeValue(FP_DENORM_FLUSH_IN_FLUSH_OUT, DAG, SL, Subtarget); 7652 7653 DisableDenorm = DAG.getNode(AMDGPUISD::DENORM_MODE, SL, MVT::Other, 7654 Fma4.getValue(1), DisableDenormValue, 7655 Fma4.getValue(2)); 7656 } else { 7657 const SDValue DisableDenormValue = 7658 DAG.getConstant(FP_DENORM_FLUSH_IN_FLUSH_OUT, SL, MVT::i32); 7659 7660 DisableDenorm = DAG.getNode(AMDGPUISD::SETREG, SL, MVT::Other, 7661 Fma4.getValue(1), DisableDenormValue, 7662 BitField, Fma4.getValue(2)); 7663 } 7664 7665 SDValue OutputChain = DAG.getNode(ISD::TokenFactor, SL, MVT::Other, 7666 DisableDenorm, DAG.getRoot()); 7667 DAG.setRoot(OutputChain); 7668 } 7669 7670 SDValue Scale = NumeratorScaled.getValue(1); 7671 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32, 7672 Fma4, Fma1, Fma3, Scale); 7673 7674 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS); 7675 } 7676 7677 SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const { 7678 if (DAG.getTarget().Options.UnsafeFPMath) 7679 return lowerFastUnsafeFDIV(Op, DAG); 7680 7681 SDLoc SL(Op); 7682 SDValue X = Op.getOperand(0); 7683 SDValue Y = Op.getOperand(1); 7684 7685 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64); 7686 7687 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1); 7688 7689 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X); 7690 7691 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0); 7692 7693 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0); 7694 7695 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One); 7696 7697 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp); 7698 7699 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One); 7700 7701 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X); 7702 7703 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1); 7704 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3); 7705 7706 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64, 7707 NegDivScale0, Mul, DivScale1); 7708 7709 SDValue Scale; 7710 7711 if (!Subtarget->hasUsableDivScaleConditionOutput()) { 7712 // Workaround a hardware bug on SI where the condition output from div_scale 7713 // is not usable. 7714 7715 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32); 7716 7717 // Figure out if the scale to use for div_fmas. 7718 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X); 7719 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y); 7720 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0); 7721 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1); 7722 7723 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi); 7724 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi); 7725 7726 SDValue Scale0Hi 7727 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi); 7728 SDValue Scale1Hi 7729 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi); 7730 7731 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ); 7732 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ); 7733 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen); 7734 } else { 7735 Scale = DivScale1.getValue(1); 7736 } 7737 7738 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64, 7739 Fma4, Fma3, Mul, Scale); 7740 7741 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X); 7742 } 7743 7744 SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const { 7745 EVT VT = Op.getValueType(); 7746 7747 if (VT == MVT::f32) 7748 return LowerFDIV32(Op, DAG); 7749 7750 if (VT == MVT::f64) 7751 return LowerFDIV64(Op, DAG); 7752 7753 if (VT == MVT::f16) 7754 return LowerFDIV16(Op, DAG); 7755 7756 llvm_unreachable("Unexpected type for fdiv"); 7757 } 7758 7759 SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const { 7760 SDLoc DL(Op); 7761 StoreSDNode *Store = cast<StoreSDNode>(Op); 7762 EVT VT = Store->getMemoryVT(); 7763 7764 if (VT == MVT::i1) { 7765 return DAG.getTruncStore(Store->getChain(), DL, 7766 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32), 7767 Store->getBasePtr(), MVT::i1, Store->getMemOperand()); 7768 } 7769 7770 assert(VT.isVector() && 7771 Store->getValue().getValueType().getScalarType() == MVT::i32); 7772 7773 if (!allowsMemoryAccessForAlignment(*DAG.getContext(), DAG.getDataLayout(), 7774 VT, *Store->getMemOperand())) { 7775 return expandUnalignedStore(Store, DAG); 7776 } 7777 7778 unsigned AS = Store->getAddressSpace(); 7779 if (Subtarget->hasLDSMisalignedBug() && 7780 AS == AMDGPUAS::FLAT_ADDRESS && 7781 Store->getAlignment() < VT.getStoreSize() && VT.getSizeInBits() > 32) { 7782 return SplitVectorStore(Op, DAG); 7783 } 7784 7785 MachineFunction &MF = DAG.getMachineFunction(); 7786 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 7787 // If there is a possibilty that flat instruction access scratch memory 7788 // then we need to use the same legalization rules we use for private. 7789 if (AS == AMDGPUAS::FLAT_ADDRESS && 7790 !Subtarget->hasMultiDwordFlatScratchAddressing()) 7791 AS = MFI->hasFlatScratchInit() ? 7792 AMDGPUAS::PRIVATE_ADDRESS : AMDGPUAS::GLOBAL_ADDRESS; 7793 7794 unsigned NumElements = VT.getVectorNumElements(); 7795 if (AS == AMDGPUAS::GLOBAL_ADDRESS || 7796 AS == AMDGPUAS::FLAT_ADDRESS) { 7797 if (NumElements > 4) 7798 return SplitVectorStore(Op, DAG); 7799 // v3 stores not supported on SI. 7800 if (NumElements == 3 && !Subtarget->hasDwordx3LoadStores()) 7801 return SplitVectorStore(Op, DAG); 7802 return SDValue(); 7803 } else if (AS == AMDGPUAS::PRIVATE_ADDRESS) { 7804 switch (Subtarget->getMaxPrivateElementSize()) { 7805 case 4: 7806 return scalarizeVectorStore(Store, DAG); 7807 case 8: 7808 if (NumElements > 2) 7809 return SplitVectorStore(Op, DAG); 7810 return SDValue(); 7811 case 16: 7812 if (NumElements > 4 || NumElements == 3) 7813 return SplitVectorStore(Op, DAG); 7814 return SDValue(); 7815 default: 7816 llvm_unreachable("unsupported private_element_size"); 7817 } 7818 } else if (AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::REGION_ADDRESS) { 7819 // Use ds_write_b128 if possible. 7820 if (Subtarget->useDS128() && Store->getAlignment() >= 16 && 7821 VT.getStoreSize() == 16 && NumElements != 3) 7822 return SDValue(); 7823 7824 if (NumElements > 2) 7825 return SplitVectorStore(Op, DAG); 7826 7827 // SI has a hardware bug in the LDS / GDS boounds checking: if the base 7828 // address is negative, then the instruction is incorrectly treated as 7829 // out-of-bounds even if base + offsets is in bounds. Split vectorized 7830 // stores here to avoid emitting ds_write2_b32. We may re-combine the 7831 // store later in the SILoadStoreOptimizer. 7832 if (!Subtarget->hasUsableDSOffset() && 7833 NumElements == 2 && VT.getStoreSize() == 8 && 7834 Store->getAlignment() < 8) { 7835 return SplitVectorStore(Op, DAG); 7836 } 7837 7838 return SDValue(); 7839 } else { 7840 llvm_unreachable("unhandled address space"); 7841 } 7842 } 7843 7844 SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const { 7845 SDLoc DL(Op); 7846 EVT VT = Op.getValueType(); 7847 SDValue Arg = Op.getOperand(0); 7848 SDValue TrigVal; 7849 7850 // TODO: Should this propagate fast-math-flags? 7851 7852 SDValue OneOver2Pi = DAG.getConstantFP(0.5 / M_PI, DL, VT); 7853 7854 if (Subtarget->hasTrigReducedRange()) { 7855 SDValue MulVal = DAG.getNode(ISD::FMUL, DL, VT, Arg, OneOver2Pi); 7856 TrigVal = DAG.getNode(AMDGPUISD::FRACT, DL, VT, MulVal); 7857 } else { 7858 TrigVal = DAG.getNode(ISD::FMUL, DL, VT, Arg, OneOver2Pi); 7859 } 7860 7861 switch (Op.getOpcode()) { 7862 case ISD::FCOS: 7863 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, TrigVal); 7864 case ISD::FSIN: 7865 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, TrigVal); 7866 default: 7867 llvm_unreachable("Wrong trig opcode"); 7868 } 7869 } 7870 7871 SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const { 7872 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op); 7873 assert(AtomicNode->isCompareAndSwap()); 7874 unsigned AS = AtomicNode->getAddressSpace(); 7875 7876 // No custom lowering required for local address space 7877 if (!isFlatGlobalAddrSpace(AS)) 7878 return Op; 7879 7880 // Non-local address space requires custom lowering for atomic compare 7881 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2 7882 SDLoc DL(Op); 7883 SDValue ChainIn = Op.getOperand(0); 7884 SDValue Addr = Op.getOperand(1); 7885 SDValue Old = Op.getOperand(2); 7886 SDValue New = Op.getOperand(3); 7887 EVT VT = Op.getValueType(); 7888 MVT SimpleVT = VT.getSimpleVT(); 7889 MVT VecType = MVT::getVectorVT(SimpleVT, 2); 7890 7891 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old}); 7892 SDValue Ops[] = { ChainIn, Addr, NewOld }; 7893 7894 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(), 7895 Ops, VT, AtomicNode->getMemOperand()); 7896 } 7897 7898 //===----------------------------------------------------------------------===// 7899 // Custom DAG optimizations 7900 //===----------------------------------------------------------------------===// 7901 7902 SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N, 7903 DAGCombinerInfo &DCI) const { 7904 EVT VT = N->getValueType(0); 7905 EVT ScalarVT = VT.getScalarType(); 7906 if (ScalarVT != MVT::f32) 7907 return SDValue(); 7908 7909 SelectionDAG &DAG = DCI.DAG; 7910 SDLoc DL(N); 7911 7912 SDValue Src = N->getOperand(0); 7913 EVT SrcVT = Src.getValueType(); 7914 7915 // TODO: We could try to match extracting the higher bytes, which would be 7916 // easier if i8 vectors weren't promoted to i32 vectors, particularly after 7917 // types are legalized. v4i8 -> v4f32 is probably the only case to worry 7918 // about in practice. 7919 if (DCI.isAfterLegalizeDAG() && SrcVT == MVT::i32) { 7920 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) { 7921 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src); 7922 DCI.AddToWorklist(Cvt.getNode()); 7923 return Cvt; 7924 } 7925 } 7926 7927 return SDValue(); 7928 } 7929 7930 // (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2) 7931 7932 // This is a variant of 7933 // (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2), 7934 // 7935 // The normal DAG combiner will do this, but only if the add has one use since 7936 // that would increase the number of instructions. 7937 // 7938 // This prevents us from seeing a constant offset that can be folded into a 7939 // memory instruction's addressing mode. If we know the resulting add offset of 7940 // a pointer can be folded into an addressing offset, we can replace the pointer 7941 // operand with the add of new constant offset. This eliminates one of the uses, 7942 // and may allow the remaining use to also be simplified. 7943 // 7944 SDValue SITargetLowering::performSHLPtrCombine(SDNode *N, 7945 unsigned AddrSpace, 7946 EVT MemVT, 7947 DAGCombinerInfo &DCI) const { 7948 SDValue N0 = N->getOperand(0); 7949 SDValue N1 = N->getOperand(1); 7950 7951 // We only do this to handle cases where it's profitable when there are 7952 // multiple uses of the add, so defer to the standard combine. 7953 if ((N0.getOpcode() != ISD::ADD && N0.getOpcode() != ISD::OR) || 7954 N0->hasOneUse()) 7955 return SDValue(); 7956 7957 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1); 7958 if (!CN1) 7959 return SDValue(); 7960 7961 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 7962 if (!CAdd) 7963 return SDValue(); 7964 7965 // If the resulting offset is too large, we can't fold it into the addressing 7966 // mode offset. 7967 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue(); 7968 Type *Ty = MemVT.getTypeForEVT(*DCI.DAG.getContext()); 7969 7970 AddrMode AM; 7971 AM.HasBaseReg = true; 7972 AM.BaseOffs = Offset.getSExtValue(); 7973 if (!isLegalAddressingMode(DCI.DAG.getDataLayout(), AM, Ty, AddrSpace)) 7974 return SDValue(); 7975 7976 SelectionDAG &DAG = DCI.DAG; 7977 SDLoc SL(N); 7978 EVT VT = N->getValueType(0); 7979 7980 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1); 7981 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32); 7982 7983 SDNodeFlags Flags; 7984 Flags.setNoUnsignedWrap(N->getFlags().hasNoUnsignedWrap() && 7985 (N0.getOpcode() == ISD::OR || 7986 N0->getFlags().hasNoUnsignedWrap())); 7987 7988 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset, Flags); 7989 } 7990 7991 SDValue SITargetLowering::performMemSDNodeCombine(MemSDNode *N, 7992 DAGCombinerInfo &DCI) const { 7993 SDValue Ptr = N->getBasePtr(); 7994 SelectionDAG &DAG = DCI.DAG; 7995 SDLoc SL(N); 7996 7997 // TODO: We could also do this for multiplies. 7998 if (Ptr.getOpcode() == ISD::SHL) { 7999 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), N->getAddressSpace(), 8000 N->getMemoryVT(), DCI); 8001 if (NewPtr) { 8002 SmallVector<SDValue, 8> NewOps(N->op_begin(), N->op_end()); 8003 8004 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr; 8005 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0); 8006 } 8007 } 8008 8009 return SDValue(); 8010 } 8011 8012 static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) { 8013 return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) || 8014 (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) || 8015 (Opc == ISD::XOR && Val == 0); 8016 } 8017 8018 // Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This 8019 // will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit 8020 // integer combine opportunities since most 64-bit operations are decomposed 8021 // this way. TODO: We won't want this for SALU especially if it is an inline 8022 // immediate. 8023 SDValue SITargetLowering::splitBinaryBitConstantOp( 8024 DAGCombinerInfo &DCI, 8025 const SDLoc &SL, 8026 unsigned Opc, SDValue LHS, 8027 const ConstantSDNode *CRHS) const { 8028 uint64_t Val = CRHS->getZExtValue(); 8029 uint32_t ValLo = Lo_32(Val); 8030 uint32_t ValHi = Hi_32(Val); 8031 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 8032 8033 if ((bitOpWithConstantIsReducible(Opc, ValLo) || 8034 bitOpWithConstantIsReducible(Opc, ValHi)) || 8035 (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) { 8036 // If we need to materialize a 64-bit immediate, it will be split up later 8037 // anyway. Avoid creating the harder to understand 64-bit immediate 8038 // materialization. 8039 return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi); 8040 } 8041 8042 return SDValue(); 8043 } 8044 8045 // Returns true if argument is a boolean value which is not serialized into 8046 // memory or argument and does not require v_cmdmask_b32 to be deserialized. 8047 static bool isBoolSGPR(SDValue V) { 8048 if (V.getValueType() != MVT::i1) 8049 return false; 8050 switch (V.getOpcode()) { 8051 default: break; 8052 case ISD::SETCC: 8053 case ISD::AND: 8054 case ISD::OR: 8055 case ISD::XOR: 8056 case AMDGPUISD::FP_CLASS: 8057 return true; 8058 } 8059 return false; 8060 } 8061 8062 // If a constant has all zeroes or all ones within each byte return it. 8063 // Otherwise return 0. 8064 static uint32_t getConstantPermuteMask(uint32_t C) { 8065 // 0xff for any zero byte in the mask 8066 uint32_t ZeroByteMask = 0; 8067 if (!(C & 0x000000ff)) ZeroByteMask |= 0x000000ff; 8068 if (!(C & 0x0000ff00)) ZeroByteMask |= 0x0000ff00; 8069 if (!(C & 0x00ff0000)) ZeroByteMask |= 0x00ff0000; 8070 if (!(C & 0xff000000)) ZeroByteMask |= 0xff000000; 8071 uint32_t NonZeroByteMask = ~ZeroByteMask; // 0xff for any non-zero byte 8072 if ((NonZeroByteMask & C) != NonZeroByteMask) 8073 return 0; // Partial bytes selected. 8074 return C; 8075 } 8076 8077 // Check if a node selects whole bytes from its operand 0 starting at a byte 8078 // boundary while masking the rest. Returns select mask as in the v_perm_b32 8079 // or -1 if not succeeded. 8080 // Note byte select encoding: 8081 // value 0-3 selects corresponding source byte; 8082 // value 0xc selects zero; 8083 // value 0xff selects 0xff. 8084 static uint32_t getPermuteMask(SelectionDAG &DAG, SDValue V) { 8085 assert(V.getValueSizeInBits() == 32); 8086 8087 if (V.getNumOperands() != 2) 8088 return ~0; 8089 8090 ConstantSDNode *N1 = dyn_cast<ConstantSDNode>(V.getOperand(1)); 8091 if (!N1) 8092 return ~0; 8093 8094 uint32_t C = N1->getZExtValue(); 8095 8096 switch (V.getOpcode()) { 8097 default: 8098 break; 8099 case ISD::AND: 8100 if (uint32_t ConstMask = getConstantPermuteMask(C)) { 8101 return (0x03020100 & ConstMask) | (0x0c0c0c0c & ~ConstMask); 8102 } 8103 break; 8104 8105 case ISD::OR: 8106 if (uint32_t ConstMask = getConstantPermuteMask(C)) { 8107 return (0x03020100 & ~ConstMask) | ConstMask; 8108 } 8109 break; 8110 8111 case ISD::SHL: 8112 if (C % 8) 8113 return ~0; 8114 8115 return uint32_t((0x030201000c0c0c0cull << C) >> 32); 8116 8117 case ISD::SRL: 8118 if (C % 8) 8119 return ~0; 8120 8121 return uint32_t(0x0c0c0c0c03020100ull >> C); 8122 } 8123 8124 return ~0; 8125 } 8126 8127 SDValue SITargetLowering::performAndCombine(SDNode *N, 8128 DAGCombinerInfo &DCI) const { 8129 if (DCI.isBeforeLegalize()) 8130 return SDValue(); 8131 8132 SelectionDAG &DAG = DCI.DAG; 8133 EVT VT = N->getValueType(0); 8134 SDValue LHS = N->getOperand(0); 8135 SDValue RHS = N->getOperand(1); 8136 8137 8138 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS); 8139 if (VT == MVT::i64 && CRHS) { 8140 if (SDValue Split 8141 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS)) 8142 return Split; 8143 } 8144 8145 if (CRHS && VT == MVT::i32) { 8146 // and (srl x, c), mask => shl (bfe x, nb + c, mask >> nb), nb 8147 // nb = number of trailing zeroes in mask 8148 // It can be optimized out using SDWA for GFX8+ in the SDWA peephole pass, 8149 // given that we are selecting 8 or 16 bit fields starting at byte boundary. 8150 uint64_t Mask = CRHS->getZExtValue(); 8151 unsigned Bits = countPopulation(Mask); 8152 if (getSubtarget()->hasSDWA() && LHS->getOpcode() == ISD::SRL && 8153 (Bits == 8 || Bits == 16) && isShiftedMask_64(Mask) && !(Mask & 1)) { 8154 if (auto *CShift = dyn_cast<ConstantSDNode>(LHS->getOperand(1))) { 8155 unsigned Shift = CShift->getZExtValue(); 8156 unsigned NB = CRHS->getAPIntValue().countTrailingZeros(); 8157 unsigned Offset = NB + Shift; 8158 if ((Offset & (Bits - 1)) == 0) { // Starts at a byte or word boundary. 8159 SDLoc SL(N); 8160 SDValue BFE = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32, 8161 LHS->getOperand(0), 8162 DAG.getConstant(Offset, SL, MVT::i32), 8163 DAG.getConstant(Bits, SL, MVT::i32)); 8164 EVT NarrowVT = EVT::getIntegerVT(*DAG.getContext(), Bits); 8165 SDValue Ext = DAG.getNode(ISD::AssertZext, SL, VT, BFE, 8166 DAG.getValueType(NarrowVT)); 8167 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(LHS), VT, Ext, 8168 DAG.getConstant(NB, SDLoc(CRHS), MVT::i32)); 8169 return Shl; 8170 } 8171 } 8172 } 8173 8174 // and (perm x, y, c1), c2 -> perm x, y, permute_mask(c1, c2) 8175 if (LHS.hasOneUse() && LHS.getOpcode() == AMDGPUISD::PERM && 8176 isa<ConstantSDNode>(LHS.getOperand(2))) { 8177 uint32_t Sel = getConstantPermuteMask(Mask); 8178 if (!Sel) 8179 return SDValue(); 8180 8181 // Select 0xc for all zero bytes 8182 Sel = (LHS.getConstantOperandVal(2) & Sel) | (~Sel & 0x0c0c0c0c); 8183 SDLoc DL(N); 8184 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, LHS.getOperand(0), 8185 LHS.getOperand(1), DAG.getConstant(Sel, DL, MVT::i32)); 8186 } 8187 } 8188 8189 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) -> 8190 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity) 8191 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) { 8192 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get(); 8193 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get(); 8194 8195 SDValue X = LHS.getOperand(0); 8196 SDValue Y = RHS.getOperand(0); 8197 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X) 8198 return SDValue(); 8199 8200 if (LCC == ISD::SETO) { 8201 if (X != LHS.getOperand(1)) 8202 return SDValue(); 8203 8204 if (RCC == ISD::SETUNE) { 8205 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1)); 8206 if (!C1 || !C1->isInfinity() || C1->isNegative()) 8207 return SDValue(); 8208 8209 const uint32_t Mask = SIInstrFlags::N_NORMAL | 8210 SIInstrFlags::N_SUBNORMAL | 8211 SIInstrFlags::N_ZERO | 8212 SIInstrFlags::P_ZERO | 8213 SIInstrFlags::P_SUBNORMAL | 8214 SIInstrFlags::P_NORMAL; 8215 8216 static_assert(((~(SIInstrFlags::S_NAN | 8217 SIInstrFlags::Q_NAN | 8218 SIInstrFlags::N_INFINITY | 8219 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask, 8220 "mask not equal"); 8221 8222 SDLoc DL(N); 8223 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, 8224 X, DAG.getConstant(Mask, DL, MVT::i32)); 8225 } 8226 } 8227 } 8228 8229 if (RHS.getOpcode() == ISD::SETCC && LHS.getOpcode() == AMDGPUISD::FP_CLASS) 8230 std::swap(LHS, RHS); 8231 8232 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == AMDGPUISD::FP_CLASS && 8233 RHS.hasOneUse()) { 8234 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get(); 8235 // and (fcmp seto), (fp_class x, mask) -> fp_class x, mask & ~(p_nan | n_nan) 8236 // and (fcmp setuo), (fp_class x, mask) -> fp_class x, mask & (p_nan | n_nan) 8237 const ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(RHS.getOperand(1)); 8238 if ((LCC == ISD::SETO || LCC == ISD::SETUO) && Mask && 8239 (RHS.getOperand(0) == LHS.getOperand(0) && 8240 LHS.getOperand(0) == LHS.getOperand(1))) { 8241 const unsigned OrdMask = SIInstrFlags::S_NAN | SIInstrFlags::Q_NAN; 8242 unsigned NewMask = LCC == ISD::SETO ? 8243 Mask->getZExtValue() & ~OrdMask : 8244 Mask->getZExtValue() & OrdMask; 8245 8246 SDLoc DL(N); 8247 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, RHS.getOperand(0), 8248 DAG.getConstant(NewMask, DL, MVT::i32)); 8249 } 8250 } 8251 8252 if (VT == MVT::i32 && 8253 (RHS.getOpcode() == ISD::SIGN_EXTEND || LHS.getOpcode() == ISD::SIGN_EXTEND)) { 8254 // and x, (sext cc from i1) => select cc, x, 0 8255 if (RHS.getOpcode() != ISD::SIGN_EXTEND) 8256 std::swap(LHS, RHS); 8257 if (isBoolSGPR(RHS.getOperand(0))) 8258 return DAG.getSelect(SDLoc(N), MVT::i32, RHS.getOperand(0), 8259 LHS, DAG.getConstant(0, SDLoc(N), MVT::i32)); 8260 } 8261 8262 // and (op x, c1), (op y, c2) -> perm x, y, permute_mask(c1, c2) 8263 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 8264 if (VT == MVT::i32 && LHS.hasOneUse() && RHS.hasOneUse() && 8265 N->isDivergent() && TII->pseudoToMCOpcode(AMDGPU::V_PERM_B32) != -1) { 8266 uint32_t LHSMask = getPermuteMask(DAG, LHS); 8267 uint32_t RHSMask = getPermuteMask(DAG, RHS); 8268 if (LHSMask != ~0u && RHSMask != ~0u) { 8269 // Canonicalize the expression in an attempt to have fewer unique masks 8270 // and therefore fewer registers used to hold the masks. 8271 if (LHSMask > RHSMask) { 8272 std::swap(LHSMask, RHSMask); 8273 std::swap(LHS, RHS); 8274 } 8275 8276 // Select 0xc for each lane used from source operand. Zero has 0xc mask 8277 // set, 0xff have 0xff in the mask, actual lanes are in the 0-3 range. 8278 uint32_t LHSUsedLanes = ~(LHSMask & 0x0c0c0c0c) & 0x0c0c0c0c; 8279 uint32_t RHSUsedLanes = ~(RHSMask & 0x0c0c0c0c) & 0x0c0c0c0c; 8280 8281 // Check of we need to combine values from two sources within a byte. 8282 if (!(LHSUsedLanes & RHSUsedLanes) && 8283 // If we select high and lower word keep it for SDWA. 8284 // TODO: teach SDWA to work with v_perm_b32 and remove the check. 8285 !(LHSUsedLanes == 0x0c0c0000 && RHSUsedLanes == 0x00000c0c)) { 8286 // Each byte in each mask is either selector mask 0-3, or has higher 8287 // bits set in either of masks, which can be 0xff for 0xff or 0x0c for 8288 // zero. If 0x0c is in either mask it shall always be 0x0c. Otherwise 8289 // mask which is not 0xff wins. By anding both masks we have a correct 8290 // result except that 0x0c shall be corrected to give 0x0c only. 8291 uint32_t Mask = LHSMask & RHSMask; 8292 for (unsigned I = 0; I < 32; I += 8) { 8293 uint32_t ByteSel = 0xff << I; 8294 if ((LHSMask & ByteSel) == 0x0c || (RHSMask & ByteSel) == 0x0c) 8295 Mask &= (0x0c << I) & 0xffffffff; 8296 } 8297 8298 // Add 4 to each active LHS lane. It will not affect any existing 0xff 8299 // or 0x0c. 8300 uint32_t Sel = Mask | (LHSUsedLanes & 0x04040404); 8301 SDLoc DL(N); 8302 8303 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, 8304 LHS.getOperand(0), RHS.getOperand(0), 8305 DAG.getConstant(Sel, DL, MVT::i32)); 8306 } 8307 } 8308 } 8309 8310 return SDValue(); 8311 } 8312 8313 SDValue SITargetLowering::performOrCombine(SDNode *N, 8314 DAGCombinerInfo &DCI) const { 8315 SelectionDAG &DAG = DCI.DAG; 8316 SDValue LHS = N->getOperand(0); 8317 SDValue RHS = N->getOperand(1); 8318 8319 EVT VT = N->getValueType(0); 8320 if (VT == MVT::i1) { 8321 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2) 8322 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS && 8323 RHS.getOpcode() == AMDGPUISD::FP_CLASS) { 8324 SDValue Src = LHS.getOperand(0); 8325 if (Src != RHS.getOperand(0)) 8326 return SDValue(); 8327 8328 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1)); 8329 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1)); 8330 if (!CLHS || !CRHS) 8331 return SDValue(); 8332 8333 // Only 10 bits are used. 8334 static const uint32_t MaxMask = 0x3ff; 8335 8336 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask; 8337 SDLoc DL(N); 8338 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, 8339 Src, DAG.getConstant(NewMask, DL, MVT::i32)); 8340 } 8341 8342 return SDValue(); 8343 } 8344 8345 // or (perm x, y, c1), c2 -> perm x, y, permute_mask(c1, c2) 8346 if (isa<ConstantSDNode>(RHS) && LHS.hasOneUse() && 8347 LHS.getOpcode() == AMDGPUISD::PERM && 8348 isa<ConstantSDNode>(LHS.getOperand(2))) { 8349 uint32_t Sel = getConstantPermuteMask(N->getConstantOperandVal(1)); 8350 if (!Sel) 8351 return SDValue(); 8352 8353 Sel |= LHS.getConstantOperandVal(2); 8354 SDLoc DL(N); 8355 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, LHS.getOperand(0), 8356 LHS.getOperand(1), DAG.getConstant(Sel, DL, MVT::i32)); 8357 } 8358 8359 // or (op x, c1), (op y, c2) -> perm x, y, permute_mask(c1, c2) 8360 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 8361 if (VT == MVT::i32 && LHS.hasOneUse() && RHS.hasOneUse() && 8362 N->isDivergent() && TII->pseudoToMCOpcode(AMDGPU::V_PERM_B32) != -1) { 8363 uint32_t LHSMask = getPermuteMask(DAG, LHS); 8364 uint32_t RHSMask = getPermuteMask(DAG, RHS); 8365 if (LHSMask != ~0u && RHSMask != ~0u) { 8366 // Canonicalize the expression in an attempt to have fewer unique masks 8367 // and therefore fewer registers used to hold the masks. 8368 if (LHSMask > RHSMask) { 8369 std::swap(LHSMask, RHSMask); 8370 std::swap(LHS, RHS); 8371 } 8372 8373 // Select 0xc for each lane used from source operand. Zero has 0xc mask 8374 // set, 0xff have 0xff in the mask, actual lanes are in the 0-3 range. 8375 uint32_t LHSUsedLanes = ~(LHSMask & 0x0c0c0c0c) & 0x0c0c0c0c; 8376 uint32_t RHSUsedLanes = ~(RHSMask & 0x0c0c0c0c) & 0x0c0c0c0c; 8377 8378 // Check of we need to combine values from two sources within a byte. 8379 if (!(LHSUsedLanes & RHSUsedLanes) && 8380 // If we select high and lower word keep it for SDWA. 8381 // TODO: teach SDWA to work with v_perm_b32 and remove the check. 8382 !(LHSUsedLanes == 0x0c0c0000 && RHSUsedLanes == 0x00000c0c)) { 8383 // Kill zero bytes selected by other mask. Zero value is 0xc. 8384 LHSMask &= ~RHSUsedLanes; 8385 RHSMask &= ~LHSUsedLanes; 8386 // Add 4 to each active LHS lane 8387 LHSMask |= LHSUsedLanes & 0x04040404; 8388 // Combine masks 8389 uint32_t Sel = LHSMask | RHSMask; 8390 SDLoc DL(N); 8391 8392 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, 8393 LHS.getOperand(0), RHS.getOperand(0), 8394 DAG.getConstant(Sel, DL, MVT::i32)); 8395 } 8396 } 8397 } 8398 8399 if (VT != MVT::i64) 8400 return SDValue(); 8401 8402 // TODO: This could be a generic combine with a predicate for extracting the 8403 // high half of an integer being free. 8404 8405 // (or i64:x, (zero_extend i32:y)) -> 8406 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x))) 8407 if (LHS.getOpcode() == ISD::ZERO_EXTEND && 8408 RHS.getOpcode() != ISD::ZERO_EXTEND) 8409 std::swap(LHS, RHS); 8410 8411 if (RHS.getOpcode() == ISD::ZERO_EXTEND) { 8412 SDValue ExtSrc = RHS.getOperand(0); 8413 EVT SrcVT = ExtSrc.getValueType(); 8414 if (SrcVT == MVT::i32) { 8415 SDLoc SL(N); 8416 SDValue LowLHS, HiBits; 8417 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG); 8418 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc); 8419 8420 DCI.AddToWorklist(LowOr.getNode()); 8421 DCI.AddToWorklist(HiBits.getNode()); 8422 8423 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, 8424 LowOr, HiBits); 8425 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 8426 } 8427 } 8428 8429 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 8430 if (CRHS) { 8431 if (SDValue Split 8432 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR, LHS, CRHS)) 8433 return Split; 8434 } 8435 8436 return SDValue(); 8437 } 8438 8439 SDValue SITargetLowering::performXorCombine(SDNode *N, 8440 DAGCombinerInfo &DCI) const { 8441 EVT VT = N->getValueType(0); 8442 if (VT != MVT::i64) 8443 return SDValue(); 8444 8445 SDValue LHS = N->getOperand(0); 8446 SDValue RHS = N->getOperand(1); 8447 8448 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS); 8449 if (CRHS) { 8450 if (SDValue Split 8451 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS)) 8452 return Split; 8453 } 8454 8455 return SDValue(); 8456 } 8457 8458 // Instructions that will be lowered with a final instruction that zeros the 8459 // high result bits. 8460 // XXX - probably only need to list legal operations. 8461 static bool fp16SrcZerosHighBits(unsigned Opc) { 8462 switch (Opc) { 8463 case ISD::FADD: 8464 case ISD::FSUB: 8465 case ISD::FMUL: 8466 case ISD::FDIV: 8467 case ISD::FREM: 8468 case ISD::FMA: 8469 case ISD::FMAD: 8470 case ISD::FCANONICALIZE: 8471 case ISD::FP_ROUND: 8472 case ISD::UINT_TO_FP: 8473 case ISD::SINT_TO_FP: 8474 case ISD::FABS: 8475 // Fabs is lowered to a bit operation, but it's an and which will clear the 8476 // high bits anyway. 8477 case ISD::FSQRT: 8478 case ISD::FSIN: 8479 case ISD::FCOS: 8480 case ISD::FPOWI: 8481 case ISD::FPOW: 8482 case ISD::FLOG: 8483 case ISD::FLOG2: 8484 case ISD::FLOG10: 8485 case ISD::FEXP: 8486 case ISD::FEXP2: 8487 case ISD::FCEIL: 8488 case ISD::FTRUNC: 8489 case ISD::FRINT: 8490 case ISD::FNEARBYINT: 8491 case ISD::FROUND: 8492 case ISD::FFLOOR: 8493 case ISD::FMINNUM: 8494 case ISD::FMAXNUM: 8495 case AMDGPUISD::FRACT: 8496 case AMDGPUISD::CLAMP: 8497 case AMDGPUISD::COS_HW: 8498 case AMDGPUISD::SIN_HW: 8499 case AMDGPUISD::FMIN3: 8500 case AMDGPUISD::FMAX3: 8501 case AMDGPUISD::FMED3: 8502 case AMDGPUISD::FMAD_FTZ: 8503 case AMDGPUISD::RCP: 8504 case AMDGPUISD::RSQ: 8505 case AMDGPUISD::RCP_IFLAG: 8506 case AMDGPUISD::LDEXP: 8507 return true; 8508 default: 8509 // fcopysign, select and others may be lowered to 32-bit bit operations 8510 // which don't zero the high bits. 8511 return false; 8512 } 8513 } 8514 8515 SDValue SITargetLowering::performZeroExtendCombine(SDNode *N, 8516 DAGCombinerInfo &DCI) const { 8517 if (!Subtarget->has16BitInsts() || 8518 DCI.getDAGCombineLevel() < AfterLegalizeDAG) 8519 return SDValue(); 8520 8521 EVT VT = N->getValueType(0); 8522 if (VT != MVT::i32) 8523 return SDValue(); 8524 8525 SDValue Src = N->getOperand(0); 8526 if (Src.getValueType() != MVT::i16) 8527 return SDValue(); 8528 8529 // (i32 zext (i16 (bitcast f16:$src))) -> fp16_zext $src 8530 // FIXME: It is not universally true that the high bits are zeroed on gfx9. 8531 if (Src.getOpcode() == ISD::BITCAST) { 8532 SDValue BCSrc = Src.getOperand(0); 8533 if (BCSrc.getValueType() == MVT::f16 && 8534 fp16SrcZerosHighBits(BCSrc.getOpcode())) 8535 return DCI.DAG.getNode(AMDGPUISD::FP16_ZEXT, SDLoc(N), VT, BCSrc); 8536 } 8537 8538 return SDValue(); 8539 } 8540 8541 SDValue SITargetLowering::performSignExtendInRegCombine(SDNode *N, 8542 DAGCombinerInfo &DCI) 8543 const { 8544 SDValue Src = N->getOperand(0); 8545 auto *VTSign = cast<VTSDNode>(N->getOperand(1)); 8546 8547 if (((Src.getOpcode() == AMDGPUISD::BUFFER_LOAD_UBYTE && 8548 VTSign->getVT() == MVT::i8) || 8549 (Src.getOpcode() == AMDGPUISD::BUFFER_LOAD_USHORT && 8550 VTSign->getVT() == MVT::i16)) && 8551 Src.hasOneUse()) { 8552 auto *M = cast<MemSDNode>(Src); 8553 SDValue Ops[] = { 8554 Src.getOperand(0), // Chain 8555 Src.getOperand(1), // rsrc 8556 Src.getOperand(2), // vindex 8557 Src.getOperand(3), // voffset 8558 Src.getOperand(4), // soffset 8559 Src.getOperand(5), // offset 8560 Src.getOperand(6), 8561 Src.getOperand(7) 8562 }; 8563 // replace with BUFFER_LOAD_BYTE/SHORT 8564 SDVTList ResList = DCI.DAG.getVTList(MVT::i32, 8565 Src.getOperand(0).getValueType()); 8566 unsigned Opc = (Src.getOpcode() == AMDGPUISD::BUFFER_LOAD_UBYTE) ? 8567 AMDGPUISD::BUFFER_LOAD_BYTE : AMDGPUISD::BUFFER_LOAD_SHORT; 8568 SDValue BufferLoadSignExt = DCI.DAG.getMemIntrinsicNode(Opc, SDLoc(N), 8569 ResList, 8570 Ops, M->getMemoryVT(), 8571 M->getMemOperand()); 8572 return DCI.DAG.getMergeValues({BufferLoadSignExt, 8573 BufferLoadSignExt.getValue(1)}, SDLoc(N)); 8574 } 8575 return SDValue(); 8576 } 8577 8578 SDValue SITargetLowering::performClassCombine(SDNode *N, 8579 DAGCombinerInfo &DCI) const { 8580 SelectionDAG &DAG = DCI.DAG; 8581 SDValue Mask = N->getOperand(1); 8582 8583 // fp_class x, 0 -> false 8584 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) { 8585 if (CMask->isNullValue()) 8586 return DAG.getConstant(0, SDLoc(N), MVT::i1); 8587 } 8588 8589 if (N->getOperand(0).isUndef()) 8590 return DAG.getUNDEF(MVT::i1); 8591 8592 return SDValue(); 8593 } 8594 8595 SDValue SITargetLowering::performRcpCombine(SDNode *N, 8596 DAGCombinerInfo &DCI) const { 8597 EVT VT = N->getValueType(0); 8598 SDValue N0 = N->getOperand(0); 8599 8600 if (N0.isUndef()) 8601 return N0; 8602 8603 if (VT == MVT::f32 && (N0.getOpcode() == ISD::UINT_TO_FP || 8604 N0.getOpcode() == ISD::SINT_TO_FP)) { 8605 return DCI.DAG.getNode(AMDGPUISD::RCP_IFLAG, SDLoc(N), VT, N0, 8606 N->getFlags()); 8607 } 8608 8609 if ((VT == MVT::f32 || VT == MVT::f16) && N0.getOpcode() == ISD::FSQRT) { 8610 return DCI.DAG.getNode(AMDGPUISD::RSQ, SDLoc(N), VT, 8611 N0.getOperand(0), N->getFlags()); 8612 } 8613 8614 return AMDGPUTargetLowering::performRcpCombine(N, DCI); 8615 } 8616 8617 bool SITargetLowering::isCanonicalized(SelectionDAG &DAG, SDValue Op, 8618 unsigned MaxDepth) const { 8619 unsigned Opcode = Op.getOpcode(); 8620 if (Opcode == ISD::FCANONICALIZE) 8621 return true; 8622 8623 if (auto *CFP = dyn_cast<ConstantFPSDNode>(Op)) { 8624 auto F = CFP->getValueAPF(); 8625 if (F.isNaN() && F.isSignaling()) 8626 return false; 8627 return !F.isDenormal() || denormalsEnabledForType(DAG, Op.getValueType()); 8628 } 8629 8630 // If source is a result of another standard FP operation it is already in 8631 // canonical form. 8632 if (MaxDepth == 0) 8633 return false; 8634 8635 switch (Opcode) { 8636 // These will flush denorms if required. 8637 case ISD::FADD: 8638 case ISD::FSUB: 8639 case ISD::FMUL: 8640 case ISD::FCEIL: 8641 case ISD::FFLOOR: 8642 case ISD::FMA: 8643 case ISD::FMAD: 8644 case ISD::FSQRT: 8645 case ISD::FDIV: 8646 case ISD::FREM: 8647 case ISD::FP_ROUND: 8648 case ISD::FP_EXTEND: 8649 case AMDGPUISD::FMUL_LEGACY: 8650 case AMDGPUISD::FMAD_FTZ: 8651 case AMDGPUISD::RCP: 8652 case AMDGPUISD::RSQ: 8653 case AMDGPUISD::RSQ_CLAMP: 8654 case AMDGPUISD::RCP_LEGACY: 8655 case AMDGPUISD::RSQ_LEGACY: 8656 case AMDGPUISD::RCP_IFLAG: 8657 case AMDGPUISD::TRIG_PREOP: 8658 case AMDGPUISD::DIV_SCALE: 8659 case AMDGPUISD::DIV_FMAS: 8660 case AMDGPUISD::DIV_FIXUP: 8661 case AMDGPUISD::FRACT: 8662 case AMDGPUISD::LDEXP: 8663 case AMDGPUISD::CVT_PKRTZ_F16_F32: 8664 case AMDGPUISD::CVT_F32_UBYTE0: 8665 case AMDGPUISD::CVT_F32_UBYTE1: 8666 case AMDGPUISD::CVT_F32_UBYTE2: 8667 case AMDGPUISD::CVT_F32_UBYTE3: 8668 return true; 8669 8670 // It can/will be lowered or combined as a bit operation. 8671 // Need to check their input recursively to handle. 8672 case ISD::FNEG: 8673 case ISD::FABS: 8674 case ISD::FCOPYSIGN: 8675 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1); 8676 8677 case ISD::FSIN: 8678 case ISD::FCOS: 8679 case ISD::FSINCOS: 8680 return Op.getValueType().getScalarType() != MVT::f16; 8681 8682 case ISD::FMINNUM: 8683 case ISD::FMAXNUM: 8684 case ISD::FMINNUM_IEEE: 8685 case ISD::FMAXNUM_IEEE: 8686 case AMDGPUISD::CLAMP: 8687 case AMDGPUISD::FMED3: 8688 case AMDGPUISD::FMAX3: 8689 case AMDGPUISD::FMIN3: { 8690 // FIXME: Shouldn't treat the generic operations different based these. 8691 // However, we aren't really required to flush the result from 8692 // minnum/maxnum.. 8693 8694 // snans will be quieted, so we only need to worry about denormals. 8695 if (Subtarget->supportsMinMaxDenormModes() || 8696 denormalsEnabledForType(DAG, Op.getValueType())) 8697 return true; 8698 8699 // Flushing may be required. 8700 // In pre-GFX9 targets V_MIN_F32 and others do not flush denorms. For such 8701 // targets need to check their input recursively. 8702 8703 // FIXME: Does this apply with clamp? It's implemented with max. 8704 for (unsigned I = 0, E = Op.getNumOperands(); I != E; ++I) { 8705 if (!isCanonicalized(DAG, Op.getOperand(I), MaxDepth - 1)) 8706 return false; 8707 } 8708 8709 return true; 8710 } 8711 case ISD::SELECT: { 8712 return isCanonicalized(DAG, Op.getOperand(1), MaxDepth - 1) && 8713 isCanonicalized(DAG, Op.getOperand(2), MaxDepth - 1); 8714 } 8715 case ISD::BUILD_VECTOR: { 8716 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) { 8717 SDValue SrcOp = Op.getOperand(i); 8718 if (!isCanonicalized(DAG, SrcOp, MaxDepth - 1)) 8719 return false; 8720 } 8721 8722 return true; 8723 } 8724 case ISD::EXTRACT_VECTOR_ELT: 8725 case ISD::EXTRACT_SUBVECTOR: { 8726 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1); 8727 } 8728 case ISD::INSERT_VECTOR_ELT: { 8729 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1) && 8730 isCanonicalized(DAG, Op.getOperand(1), MaxDepth - 1); 8731 } 8732 case ISD::UNDEF: 8733 // Could be anything. 8734 return false; 8735 8736 case ISD::BITCAST: { 8737 // Hack round the mess we make when legalizing extract_vector_elt 8738 SDValue Src = Op.getOperand(0); 8739 if (Src.getValueType() == MVT::i16 && 8740 Src.getOpcode() == ISD::TRUNCATE) { 8741 SDValue TruncSrc = Src.getOperand(0); 8742 if (TruncSrc.getValueType() == MVT::i32 && 8743 TruncSrc.getOpcode() == ISD::BITCAST && 8744 TruncSrc.getOperand(0).getValueType() == MVT::v2f16) { 8745 return isCanonicalized(DAG, TruncSrc.getOperand(0), MaxDepth - 1); 8746 } 8747 } 8748 8749 return false; 8750 } 8751 case ISD::INTRINSIC_WO_CHAIN: { 8752 unsigned IntrinsicID 8753 = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 8754 // TODO: Handle more intrinsics 8755 switch (IntrinsicID) { 8756 case Intrinsic::amdgcn_cvt_pkrtz: 8757 case Intrinsic::amdgcn_cubeid: 8758 case Intrinsic::amdgcn_frexp_mant: 8759 case Intrinsic::amdgcn_fdot2: 8760 return true; 8761 default: 8762 break; 8763 } 8764 8765 LLVM_FALLTHROUGH; 8766 } 8767 default: 8768 return denormalsEnabledForType(DAG, Op.getValueType()) && 8769 DAG.isKnownNeverSNaN(Op); 8770 } 8771 8772 llvm_unreachable("invalid operation"); 8773 } 8774 8775 // Constant fold canonicalize. 8776 SDValue SITargetLowering::getCanonicalConstantFP( 8777 SelectionDAG &DAG, const SDLoc &SL, EVT VT, const APFloat &C) const { 8778 // Flush denormals to 0 if not enabled. 8779 if (C.isDenormal() && !denormalsEnabledForType(DAG, VT)) 8780 return DAG.getConstantFP(0.0, SL, VT); 8781 8782 if (C.isNaN()) { 8783 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics()); 8784 if (C.isSignaling()) { 8785 // Quiet a signaling NaN. 8786 // FIXME: Is this supposed to preserve payload bits? 8787 return DAG.getConstantFP(CanonicalQNaN, SL, VT); 8788 } 8789 8790 // Make sure it is the canonical NaN bitpattern. 8791 // 8792 // TODO: Can we use -1 as the canonical NaN value since it's an inline 8793 // immediate? 8794 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt()) 8795 return DAG.getConstantFP(CanonicalQNaN, SL, VT); 8796 } 8797 8798 // Already canonical. 8799 return DAG.getConstantFP(C, SL, VT); 8800 } 8801 8802 static bool vectorEltWillFoldAway(SDValue Op) { 8803 return Op.isUndef() || isa<ConstantFPSDNode>(Op); 8804 } 8805 8806 SDValue SITargetLowering::performFCanonicalizeCombine( 8807 SDNode *N, 8808 DAGCombinerInfo &DCI) const { 8809 SelectionDAG &DAG = DCI.DAG; 8810 SDValue N0 = N->getOperand(0); 8811 EVT VT = N->getValueType(0); 8812 8813 // fcanonicalize undef -> qnan 8814 if (N0.isUndef()) { 8815 APFloat QNaN = APFloat::getQNaN(SelectionDAG::EVTToAPFloatSemantics(VT)); 8816 return DAG.getConstantFP(QNaN, SDLoc(N), VT); 8817 } 8818 8819 if (ConstantFPSDNode *CFP = isConstOrConstSplatFP(N0)) { 8820 EVT VT = N->getValueType(0); 8821 return getCanonicalConstantFP(DAG, SDLoc(N), VT, CFP->getValueAPF()); 8822 } 8823 8824 // fcanonicalize (build_vector x, k) -> build_vector (fcanonicalize x), 8825 // (fcanonicalize k) 8826 // 8827 // fcanonicalize (build_vector x, undef) -> build_vector (fcanonicalize x), 0 8828 8829 // TODO: This could be better with wider vectors that will be split to v2f16, 8830 // and to consider uses since there aren't that many packed operations. 8831 if (N0.getOpcode() == ISD::BUILD_VECTOR && VT == MVT::v2f16 && 8832 isTypeLegal(MVT::v2f16)) { 8833 SDLoc SL(N); 8834 SDValue NewElts[2]; 8835 SDValue Lo = N0.getOperand(0); 8836 SDValue Hi = N0.getOperand(1); 8837 EVT EltVT = Lo.getValueType(); 8838 8839 if (vectorEltWillFoldAway(Lo) || vectorEltWillFoldAway(Hi)) { 8840 for (unsigned I = 0; I != 2; ++I) { 8841 SDValue Op = N0.getOperand(I); 8842 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) { 8843 NewElts[I] = getCanonicalConstantFP(DAG, SL, EltVT, 8844 CFP->getValueAPF()); 8845 } else if (Op.isUndef()) { 8846 // Handled below based on what the other operand is. 8847 NewElts[I] = Op; 8848 } else { 8849 NewElts[I] = DAG.getNode(ISD::FCANONICALIZE, SL, EltVT, Op); 8850 } 8851 } 8852 8853 // If one half is undef, and one is constant, perfer a splat vector rather 8854 // than the normal qNaN. If it's a register, prefer 0.0 since that's 8855 // cheaper to use and may be free with a packed operation. 8856 if (NewElts[0].isUndef()) { 8857 if (isa<ConstantFPSDNode>(NewElts[1])) 8858 NewElts[0] = isa<ConstantFPSDNode>(NewElts[1]) ? 8859 NewElts[1]: DAG.getConstantFP(0.0f, SL, EltVT); 8860 } 8861 8862 if (NewElts[1].isUndef()) { 8863 NewElts[1] = isa<ConstantFPSDNode>(NewElts[0]) ? 8864 NewElts[0] : DAG.getConstantFP(0.0f, SL, EltVT); 8865 } 8866 8867 return DAG.getBuildVector(VT, SL, NewElts); 8868 } 8869 } 8870 8871 unsigned SrcOpc = N0.getOpcode(); 8872 8873 // If it's free to do so, push canonicalizes further up the source, which may 8874 // find a canonical source. 8875 // 8876 // TODO: More opcodes. Note this is unsafe for the the _ieee minnum/maxnum for 8877 // sNaNs. 8878 if (SrcOpc == ISD::FMINNUM || SrcOpc == ISD::FMAXNUM) { 8879 auto *CRHS = dyn_cast<ConstantFPSDNode>(N0.getOperand(1)); 8880 if (CRHS && N0.hasOneUse()) { 8881 SDLoc SL(N); 8882 SDValue Canon0 = DAG.getNode(ISD::FCANONICALIZE, SL, VT, 8883 N0.getOperand(0)); 8884 SDValue Canon1 = getCanonicalConstantFP(DAG, SL, VT, CRHS->getValueAPF()); 8885 DCI.AddToWorklist(Canon0.getNode()); 8886 8887 return DAG.getNode(N0.getOpcode(), SL, VT, Canon0, Canon1); 8888 } 8889 } 8890 8891 return isCanonicalized(DAG, N0) ? N0 : SDValue(); 8892 } 8893 8894 static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) { 8895 switch (Opc) { 8896 case ISD::FMAXNUM: 8897 case ISD::FMAXNUM_IEEE: 8898 return AMDGPUISD::FMAX3; 8899 case ISD::SMAX: 8900 return AMDGPUISD::SMAX3; 8901 case ISD::UMAX: 8902 return AMDGPUISD::UMAX3; 8903 case ISD::FMINNUM: 8904 case ISD::FMINNUM_IEEE: 8905 return AMDGPUISD::FMIN3; 8906 case ISD::SMIN: 8907 return AMDGPUISD::SMIN3; 8908 case ISD::UMIN: 8909 return AMDGPUISD::UMIN3; 8910 default: 8911 llvm_unreachable("Not a min/max opcode"); 8912 } 8913 } 8914 8915 SDValue SITargetLowering::performIntMed3ImmCombine( 8916 SelectionDAG &DAG, const SDLoc &SL, 8917 SDValue Op0, SDValue Op1, bool Signed) const { 8918 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1); 8919 if (!K1) 8920 return SDValue(); 8921 8922 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1)); 8923 if (!K0) 8924 return SDValue(); 8925 8926 if (Signed) { 8927 if (K0->getAPIntValue().sge(K1->getAPIntValue())) 8928 return SDValue(); 8929 } else { 8930 if (K0->getAPIntValue().uge(K1->getAPIntValue())) 8931 return SDValue(); 8932 } 8933 8934 EVT VT = K0->getValueType(0); 8935 unsigned Med3Opc = Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3; 8936 if (VT == MVT::i32 || (VT == MVT::i16 && Subtarget->hasMed3_16())) { 8937 return DAG.getNode(Med3Opc, SL, VT, 8938 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0)); 8939 } 8940 8941 // If there isn't a 16-bit med3 operation, convert to 32-bit. 8942 MVT NVT = MVT::i32; 8943 unsigned ExtOp = Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 8944 8945 SDValue Tmp1 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(0)); 8946 SDValue Tmp2 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(1)); 8947 SDValue Tmp3 = DAG.getNode(ExtOp, SL, NVT, Op1); 8948 8949 SDValue Med3 = DAG.getNode(Med3Opc, SL, NVT, Tmp1, Tmp2, Tmp3); 8950 return DAG.getNode(ISD::TRUNCATE, SL, VT, Med3); 8951 } 8952 8953 static ConstantFPSDNode *getSplatConstantFP(SDValue Op) { 8954 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) 8955 return C; 8956 8957 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Op)) { 8958 if (ConstantFPSDNode *C = BV->getConstantFPSplatNode()) 8959 return C; 8960 } 8961 8962 return nullptr; 8963 } 8964 8965 SDValue SITargetLowering::performFPMed3ImmCombine(SelectionDAG &DAG, 8966 const SDLoc &SL, 8967 SDValue Op0, 8968 SDValue Op1) const { 8969 ConstantFPSDNode *K1 = getSplatConstantFP(Op1); 8970 if (!K1) 8971 return SDValue(); 8972 8973 ConstantFPSDNode *K0 = getSplatConstantFP(Op0.getOperand(1)); 8974 if (!K0) 8975 return SDValue(); 8976 8977 // Ordered >= (although NaN inputs should have folded away by now). 8978 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF()); 8979 if (Cmp == APFloat::cmpGreaterThan) 8980 return SDValue(); 8981 8982 const MachineFunction &MF = DAG.getMachineFunction(); 8983 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 8984 8985 // TODO: Check IEEE bit enabled? 8986 EVT VT = Op0.getValueType(); 8987 if (Info->getMode().DX10Clamp) { 8988 // If dx10_clamp is enabled, NaNs clamp to 0.0. This is the same as the 8989 // hardware fmed3 behavior converting to a min. 8990 // FIXME: Should this be allowing -0.0? 8991 if (K1->isExactlyValue(1.0) && K0->isExactlyValue(0.0)) 8992 return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Op0.getOperand(0)); 8993 } 8994 8995 // med3 for f16 is only available on gfx9+, and not available for v2f16. 8996 if (VT == MVT::f32 || (VT == MVT::f16 && Subtarget->hasMed3_16())) { 8997 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a 8998 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would 8999 // then give the other result, which is different from med3 with a NaN 9000 // input. 9001 SDValue Var = Op0.getOperand(0); 9002 if (!DAG.isKnownNeverSNaN(Var)) 9003 return SDValue(); 9004 9005 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 9006 9007 if ((!K0->hasOneUse() || 9008 TII->isInlineConstant(K0->getValueAPF().bitcastToAPInt())) && 9009 (!K1->hasOneUse() || 9010 TII->isInlineConstant(K1->getValueAPF().bitcastToAPInt()))) { 9011 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0), 9012 Var, SDValue(K0, 0), SDValue(K1, 0)); 9013 } 9014 } 9015 9016 return SDValue(); 9017 } 9018 9019 SDValue SITargetLowering::performMinMaxCombine(SDNode *N, 9020 DAGCombinerInfo &DCI) const { 9021 SelectionDAG &DAG = DCI.DAG; 9022 9023 EVT VT = N->getValueType(0); 9024 unsigned Opc = N->getOpcode(); 9025 SDValue Op0 = N->getOperand(0); 9026 SDValue Op1 = N->getOperand(1); 9027 9028 // Only do this if the inner op has one use since this will just increases 9029 // register pressure for no benefit. 9030 9031 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY && 9032 !VT.isVector() && 9033 (VT == MVT::i32 || VT == MVT::f32 || 9034 ((VT == MVT::f16 || VT == MVT::i16) && Subtarget->hasMin3Max3_16()))) { 9035 // max(max(a, b), c) -> max3(a, b, c) 9036 // min(min(a, b), c) -> min3(a, b, c) 9037 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) { 9038 SDLoc DL(N); 9039 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), 9040 DL, 9041 N->getValueType(0), 9042 Op0.getOperand(0), 9043 Op0.getOperand(1), 9044 Op1); 9045 } 9046 9047 // Try commuted. 9048 // max(a, max(b, c)) -> max3(a, b, c) 9049 // min(a, min(b, c)) -> min3(a, b, c) 9050 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) { 9051 SDLoc DL(N); 9052 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), 9053 DL, 9054 N->getValueType(0), 9055 Op0, 9056 Op1.getOperand(0), 9057 Op1.getOperand(1)); 9058 } 9059 } 9060 9061 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1) 9062 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) { 9063 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true)) 9064 return Med3; 9065 } 9066 9067 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) { 9068 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false)) 9069 return Med3; 9070 } 9071 9072 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1) 9073 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) || 9074 (Opc == ISD::FMINNUM_IEEE && Op0.getOpcode() == ISD::FMAXNUM_IEEE) || 9075 (Opc == AMDGPUISD::FMIN_LEGACY && 9076 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) && 9077 (VT == MVT::f32 || VT == MVT::f64 || 9078 (VT == MVT::f16 && Subtarget->has16BitInsts()) || 9079 (VT == MVT::v2f16 && Subtarget->hasVOP3PInsts())) && 9080 Op0.hasOneUse()) { 9081 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1)) 9082 return Res; 9083 } 9084 9085 return SDValue(); 9086 } 9087 9088 static bool isClampZeroToOne(SDValue A, SDValue B) { 9089 if (ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A)) { 9090 if (ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B)) { 9091 // FIXME: Should this be allowing -0.0? 9092 return (CA->isExactlyValue(0.0) && CB->isExactlyValue(1.0)) || 9093 (CA->isExactlyValue(1.0) && CB->isExactlyValue(0.0)); 9094 } 9095 } 9096 9097 return false; 9098 } 9099 9100 // FIXME: Should only worry about snans for version with chain. 9101 SDValue SITargetLowering::performFMed3Combine(SDNode *N, 9102 DAGCombinerInfo &DCI) const { 9103 EVT VT = N->getValueType(0); 9104 // v_med3_f32 and v_max_f32 behave identically wrt denorms, exceptions and 9105 // NaNs. With a NaN input, the order of the operands may change the result. 9106 9107 SelectionDAG &DAG = DCI.DAG; 9108 SDLoc SL(N); 9109 9110 SDValue Src0 = N->getOperand(0); 9111 SDValue Src1 = N->getOperand(1); 9112 SDValue Src2 = N->getOperand(2); 9113 9114 if (isClampZeroToOne(Src0, Src1)) { 9115 // const_a, const_b, x -> clamp is safe in all cases including signaling 9116 // nans. 9117 // FIXME: Should this be allowing -0.0? 9118 return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Src2); 9119 } 9120 9121 const MachineFunction &MF = DAG.getMachineFunction(); 9122 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 9123 9124 // FIXME: dx10_clamp behavior assumed in instcombine. Should we really bother 9125 // handling no dx10-clamp? 9126 if (Info->getMode().DX10Clamp) { 9127 // If NaNs is clamped to 0, we are free to reorder the inputs. 9128 9129 if (isa<ConstantFPSDNode>(Src0) && !isa<ConstantFPSDNode>(Src1)) 9130 std::swap(Src0, Src1); 9131 9132 if (isa<ConstantFPSDNode>(Src1) && !isa<ConstantFPSDNode>(Src2)) 9133 std::swap(Src1, Src2); 9134 9135 if (isa<ConstantFPSDNode>(Src0) && !isa<ConstantFPSDNode>(Src1)) 9136 std::swap(Src0, Src1); 9137 9138 if (isClampZeroToOne(Src1, Src2)) 9139 return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Src0); 9140 } 9141 9142 return SDValue(); 9143 } 9144 9145 SDValue SITargetLowering::performCvtPkRTZCombine(SDNode *N, 9146 DAGCombinerInfo &DCI) const { 9147 SDValue Src0 = N->getOperand(0); 9148 SDValue Src1 = N->getOperand(1); 9149 if (Src0.isUndef() && Src1.isUndef()) 9150 return DCI.DAG.getUNDEF(N->getValueType(0)); 9151 return SDValue(); 9152 } 9153 9154 SDValue SITargetLowering::performExtractVectorEltCombine( 9155 SDNode *N, DAGCombinerInfo &DCI) const { 9156 SDValue Vec = N->getOperand(0); 9157 SelectionDAG &DAG = DCI.DAG; 9158 9159 EVT VecVT = Vec.getValueType(); 9160 EVT EltVT = VecVT.getVectorElementType(); 9161 9162 if ((Vec.getOpcode() == ISD::FNEG || 9163 Vec.getOpcode() == ISD::FABS) && allUsesHaveSourceMods(N)) { 9164 SDLoc SL(N); 9165 EVT EltVT = N->getValueType(0); 9166 SDValue Idx = N->getOperand(1); 9167 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, 9168 Vec.getOperand(0), Idx); 9169 return DAG.getNode(Vec.getOpcode(), SL, EltVT, Elt); 9170 } 9171 9172 // ScalarRes = EXTRACT_VECTOR_ELT ((vector-BINOP Vec1, Vec2), Idx) 9173 // => 9174 // Vec1Elt = EXTRACT_VECTOR_ELT(Vec1, Idx) 9175 // Vec2Elt = EXTRACT_VECTOR_ELT(Vec2, Idx) 9176 // ScalarRes = scalar-BINOP Vec1Elt, Vec2Elt 9177 if (Vec.hasOneUse() && DCI.isBeforeLegalize()) { 9178 SDLoc SL(N); 9179 EVT EltVT = N->getValueType(0); 9180 SDValue Idx = N->getOperand(1); 9181 unsigned Opc = Vec.getOpcode(); 9182 9183 switch(Opc) { 9184 default: 9185 break; 9186 // TODO: Support other binary operations. 9187 case ISD::FADD: 9188 case ISD::FSUB: 9189 case ISD::FMUL: 9190 case ISD::ADD: 9191 case ISD::UMIN: 9192 case ISD::UMAX: 9193 case ISD::SMIN: 9194 case ISD::SMAX: 9195 case ISD::FMAXNUM: 9196 case ISD::FMINNUM: 9197 case ISD::FMAXNUM_IEEE: 9198 case ISD::FMINNUM_IEEE: { 9199 SDValue Elt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, 9200 Vec.getOperand(0), Idx); 9201 SDValue Elt1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, 9202 Vec.getOperand(1), Idx); 9203 9204 DCI.AddToWorklist(Elt0.getNode()); 9205 DCI.AddToWorklist(Elt1.getNode()); 9206 return DAG.getNode(Opc, SL, EltVT, Elt0, Elt1, Vec->getFlags()); 9207 } 9208 } 9209 } 9210 9211 unsigned VecSize = VecVT.getSizeInBits(); 9212 unsigned EltSize = EltVT.getSizeInBits(); 9213 9214 // EXTRACT_VECTOR_ELT (<n x e>, var-idx) => n x select (e, const-idx) 9215 // This elminates non-constant index and subsequent movrel or scratch access. 9216 // Sub-dword vectors of size 2 dword or less have better implementation. 9217 // Vectors of size bigger than 8 dwords would yield too many v_cndmask_b32 9218 // instructions. 9219 if (VecSize <= 256 && (VecSize > 64 || EltSize >= 32) && 9220 !isa<ConstantSDNode>(N->getOperand(1))) { 9221 SDLoc SL(N); 9222 SDValue Idx = N->getOperand(1); 9223 SDValue V; 9224 for (unsigned I = 0, E = VecVT.getVectorNumElements(); I < E; ++I) { 9225 SDValue IC = DAG.getVectorIdxConstant(I, SL); 9226 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Vec, IC); 9227 if (I == 0) 9228 V = Elt; 9229 else 9230 V = DAG.getSelectCC(SL, Idx, IC, Elt, V, ISD::SETEQ); 9231 } 9232 return V; 9233 } 9234 9235 if (!DCI.isBeforeLegalize()) 9236 return SDValue(); 9237 9238 // Try to turn sub-dword accesses of vectors into accesses of the same 32-bit 9239 // elements. This exposes more load reduction opportunities by replacing 9240 // multiple small extract_vector_elements with a single 32-bit extract. 9241 auto *Idx = dyn_cast<ConstantSDNode>(N->getOperand(1)); 9242 if (isa<MemSDNode>(Vec) && 9243 EltSize <= 16 && 9244 EltVT.isByteSized() && 9245 VecSize > 32 && 9246 VecSize % 32 == 0 && 9247 Idx) { 9248 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VecVT); 9249 9250 unsigned BitIndex = Idx->getZExtValue() * EltSize; 9251 unsigned EltIdx = BitIndex / 32; 9252 unsigned LeftoverBitIdx = BitIndex % 32; 9253 SDLoc SL(N); 9254 9255 SDValue Cast = DAG.getNode(ISD::BITCAST, SL, NewVT, Vec); 9256 DCI.AddToWorklist(Cast.getNode()); 9257 9258 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Cast, 9259 DAG.getConstant(EltIdx, SL, MVT::i32)); 9260 DCI.AddToWorklist(Elt.getNode()); 9261 SDValue Srl = DAG.getNode(ISD::SRL, SL, MVT::i32, Elt, 9262 DAG.getConstant(LeftoverBitIdx, SL, MVT::i32)); 9263 DCI.AddToWorklist(Srl.getNode()); 9264 9265 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SL, EltVT.changeTypeToInteger(), Srl); 9266 DCI.AddToWorklist(Trunc.getNode()); 9267 return DAG.getNode(ISD::BITCAST, SL, EltVT, Trunc); 9268 } 9269 9270 return SDValue(); 9271 } 9272 9273 SDValue 9274 SITargetLowering::performInsertVectorEltCombine(SDNode *N, 9275 DAGCombinerInfo &DCI) const { 9276 SDValue Vec = N->getOperand(0); 9277 SDValue Idx = N->getOperand(2); 9278 EVT VecVT = Vec.getValueType(); 9279 EVT EltVT = VecVT.getVectorElementType(); 9280 unsigned VecSize = VecVT.getSizeInBits(); 9281 unsigned EltSize = EltVT.getSizeInBits(); 9282 9283 // INSERT_VECTOR_ELT (<n x e>, var-idx) 9284 // => BUILD_VECTOR n x select (e, const-idx) 9285 // This elminates non-constant index and subsequent movrel or scratch access. 9286 // Sub-dword vectors of size 2 dword or less have better implementation. 9287 // Vectors of size bigger than 8 dwords would yield too many v_cndmask_b32 9288 // instructions. 9289 if (isa<ConstantSDNode>(Idx) || 9290 VecSize > 256 || (VecSize <= 64 && EltSize < 32)) 9291 return SDValue(); 9292 9293 SelectionDAG &DAG = DCI.DAG; 9294 SDLoc SL(N); 9295 SDValue Ins = N->getOperand(1); 9296 EVT IdxVT = Idx.getValueType(); 9297 9298 SmallVector<SDValue, 16> Ops; 9299 for (unsigned I = 0, E = VecVT.getVectorNumElements(); I < E; ++I) { 9300 SDValue IC = DAG.getConstant(I, SL, IdxVT); 9301 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Vec, IC); 9302 SDValue V = DAG.getSelectCC(SL, Idx, IC, Ins, Elt, ISD::SETEQ); 9303 Ops.push_back(V); 9304 } 9305 9306 return DAG.getBuildVector(VecVT, SL, Ops); 9307 } 9308 9309 unsigned SITargetLowering::getFusedOpcode(const SelectionDAG &DAG, 9310 const SDNode *N0, 9311 const SDNode *N1) const { 9312 EVT VT = N0->getValueType(0); 9313 9314 // Only do this if we are not trying to support denormals. v_mad_f32 does not 9315 // support denormals ever. 9316 if (((VT == MVT::f32 && !hasFP32Denormals(DAG.getMachineFunction())) || 9317 (VT == MVT::f16 && !hasFP64FP16Denormals(DAG.getMachineFunction()) && 9318 getSubtarget()->hasMadF16())) && 9319 isOperationLegal(ISD::FMAD, VT)) 9320 return ISD::FMAD; 9321 9322 const TargetOptions &Options = DAG.getTarget().Options; 9323 if ((Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath || 9324 (N0->getFlags().hasAllowContract() && 9325 N1->getFlags().hasAllowContract())) && 9326 isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) { 9327 return ISD::FMA; 9328 } 9329 9330 return 0; 9331 } 9332 9333 // For a reassociatable opcode perform: 9334 // op x, (op y, z) -> op (op x, z), y, if x and z are uniform 9335 SDValue SITargetLowering::reassociateScalarOps(SDNode *N, 9336 SelectionDAG &DAG) const { 9337 EVT VT = N->getValueType(0); 9338 if (VT != MVT::i32 && VT != MVT::i64) 9339 return SDValue(); 9340 9341 unsigned Opc = N->getOpcode(); 9342 SDValue Op0 = N->getOperand(0); 9343 SDValue Op1 = N->getOperand(1); 9344 9345 if (!(Op0->isDivergent() ^ Op1->isDivergent())) 9346 return SDValue(); 9347 9348 if (Op0->isDivergent()) 9349 std::swap(Op0, Op1); 9350 9351 if (Op1.getOpcode() != Opc || !Op1.hasOneUse()) 9352 return SDValue(); 9353 9354 SDValue Op2 = Op1.getOperand(1); 9355 Op1 = Op1.getOperand(0); 9356 if (!(Op1->isDivergent() ^ Op2->isDivergent())) 9357 return SDValue(); 9358 9359 if (Op1->isDivergent()) 9360 std::swap(Op1, Op2); 9361 9362 // If either operand is constant this will conflict with 9363 // DAGCombiner::ReassociateOps(). 9364 if (DAG.isConstantIntBuildVectorOrConstantInt(Op0) || 9365 DAG.isConstantIntBuildVectorOrConstantInt(Op1)) 9366 return SDValue(); 9367 9368 SDLoc SL(N); 9369 SDValue Add1 = DAG.getNode(Opc, SL, VT, Op0, Op1); 9370 return DAG.getNode(Opc, SL, VT, Add1, Op2); 9371 } 9372 9373 static SDValue getMad64_32(SelectionDAG &DAG, const SDLoc &SL, 9374 EVT VT, 9375 SDValue N0, SDValue N1, SDValue N2, 9376 bool Signed) { 9377 unsigned MadOpc = Signed ? AMDGPUISD::MAD_I64_I32 : AMDGPUISD::MAD_U64_U32; 9378 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::i1); 9379 SDValue Mad = DAG.getNode(MadOpc, SL, VTs, N0, N1, N2); 9380 return DAG.getNode(ISD::TRUNCATE, SL, VT, Mad); 9381 } 9382 9383 SDValue SITargetLowering::performAddCombine(SDNode *N, 9384 DAGCombinerInfo &DCI) const { 9385 SelectionDAG &DAG = DCI.DAG; 9386 EVT VT = N->getValueType(0); 9387 SDLoc SL(N); 9388 SDValue LHS = N->getOperand(0); 9389 SDValue RHS = N->getOperand(1); 9390 9391 if ((LHS.getOpcode() == ISD::MUL || RHS.getOpcode() == ISD::MUL) 9392 && Subtarget->hasMad64_32() && 9393 !VT.isVector() && VT.getScalarSizeInBits() > 32 && 9394 VT.getScalarSizeInBits() <= 64) { 9395 if (LHS.getOpcode() != ISD::MUL) 9396 std::swap(LHS, RHS); 9397 9398 SDValue MulLHS = LHS.getOperand(0); 9399 SDValue MulRHS = LHS.getOperand(1); 9400 SDValue AddRHS = RHS; 9401 9402 // TODO: Maybe restrict if SGPR inputs. 9403 if (numBitsUnsigned(MulLHS, DAG) <= 32 && 9404 numBitsUnsigned(MulRHS, DAG) <= 32) { 9405 MulLHS = DAG.getZExtOrTrunc(MulLHS, SL, MVT::i32); 9406 MulRHS = DAG.getZExtOrTrunc(MulRHS, SL, MVT::i32); 9407 AddRHS = DAG.getZExtOrTrunc(AddRHS, SL, MVT::i64); 9408 return getMad64_32(DAG, SL, VT, MulLHS, MulRHS, AddRHS, false); 9409 } 9410 9411 if (numBitsSigned(MulLHS, DAG) < 32 && numBitsSigned(MulRHS, DAG) < 32) { 9412 MulLHS = DAG.getSExtOrTrunc(MulLHS, SL, MVT::i32); 9413 MulRHS = DAG.getSExtOrTrunc(MulRHS, SL, MVT::i32); 9414 AddRHS = DAG.getSExtOrTrunc(AddRHS, SL, MVT::i64); 9415 return getMad64_32(DAG, SL, VT, MulLHS, MulRHS, AddRHS, true); 9416 } 9417 9418 return SDValue(); 9419 } 9420 9421 if (SDValue V = reassociateScalarOps(N, DAG)) { 9422 return V; 9423 } 9424 9425 if (VT != MVT::i32 || !DCI.isAfterLegalizeDAG()) 9426 return SDValue(); 9427 9428 // add x, zext (setcc) => addcarry x, 0, setcc 9429 // add x, sext (setcc) => subcarry x, 0, setcc 9430 unsigned Opc = LHS.getOpcode(); 9431 if (Opc == ISD::ZERO_EXTEND || Opc == ISD::SIGN_EXTEND || 9432 Opc == ISD::ANY_EXTEND || Opc == ISD::ADDCARRY) 9433 std::swap(RHS, LHS); 9434 9435 Opc = RHS.getOpcode(); 9436 switch (Opc) { 9437 default: break; 9438 case ISD::ZERO_EXTEND: 9439 case ISD::SIGN_EXTEND: 9440 case ISD::ANY_EXTEND: { 9441 auto Cond = RHS.getOperand(0); 9442 // If this won't be a real VOPC output, we would still need to insert an 9443 // extra instruction anyway. 9444 if (!isBoolSGPR(Cond)) 9445 break; 9446 SDVTList VTList = DAG.getVTList(MVT::i32, MVT::i1); 9447 SDValue Args[] = { LHS, DAG.getConstant(0, SL, MVT::i32), Cond }; 9448 Opc = (Opc == ISD::SIGN_EXTEND) ? ISD::SUBCARRY : ISD::ADDCARRY; 9449 return DAG.getNode(Opc, SL, VTList, Args); 9450 } 9451 case ISD::ADDCARRY: { 9452 // add x, (addcarry y, 0, cc) => addcarry x, y, cc 9453 auto C = dyn_cast<ConstantSDNode>(RHS.getOperand(1)); 9454 if (!C || C->getZExtValue() != 0) break; 9455 SDValue Args[] = { LHS, RHS.getOperand(0), RHS.getOperand(2) }; 9456 return DAG.getNode(ISD::ADDCARRY, SDLoc(N), RHS->getVTList(), Args); 9457 } 9458 } 9459 return SDValue(); 9460 } 9461 9462 SDValue SITargetLowering::performSubCombine(SDNode *N, 9463 DAGCombinerInfo &DCI) const { 9464 SelectionDAG &DAG = DCI.DAG; 9465 EVT VT = N->getValueType(0); 9466 9467 if (VT != MVT::i32) 9468 return SDValue(); 9469 9470 SDLoc SL(N); 9471 SDValue LHS = N->getOperand(0); 9472 SDValue RHS = N->getOperand(1); 9473 9474 // sub x, zext (setcc) => subcarry x, 0, setcc 9475 // sub x, sext (setcc) => addcarry x, 0, setcc 9476 unsigned Opc = RHS.getOpcode(); 9477 switch (Opc) { 9478 default: break; 9479 case ISD::ZERO_EXTEND: 9480 case ISD::SIGN_EXTEND: 9481 case ISD::ANY_EXTEND: { 9482 auto Cond = RHS.getOperand(0); 9483 // If this won't be a real VOPC output, we would still need to insert an 9484 // extra instruction anyway. 9485 if (!isBoolSGPR(Cond)) 9486 break; 9487 SDVTList VTList = DAG.getVTList(MVT::i32, MVT::i1); 9488 SDValue Args[] = { LHS, DAG.getConstant(0, SL, MVT::i32), Cond }; 9489 Opc = (Opc == ISD::SIGN_EXTEND) ? ISD::ADDCARRY : ISD::SUBCARRY; 9490 return DAG.getNode(Opc, SL, VTList, Args); 9491 } 9492 } 9493 9494 if (LHS.getOpcode() == ISD::SUBCARRY) { 9495 // sub (subcarry x, 0, cc), y => subcarry x, y, cc 9496 auto C = dyn_cast<ConstantSDNode>(LHS.getOperand(1)); 9497 if (!C || !C->isNullValue()) 9498 return SDValue(); 9499 SDValue Args[] = { LHS.getOperand(0), RHS, LHS.getOperand(2) }; 9500 return DAG.getNode(ISD::SUBCARRY, SDLoc(N), LHS->getVTList(), Args); 9501 } 9502 return SDValue(); 9503 } 9504 9505 SDValue SITargetLowering::performAddCarrySubCarryCombine(SDNode *N, 9506 DAGCombinerInfo &DCI) const { 9507 9508 if (N->getValueType(0) != MVT::i32) 9509 return SDValue(); 9510 9511 auto C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 9512 if (!C || C->getZExtValue() != 0) 9513 return SDValue(); 9514 9515 SelectionDAG &DAG = DCI.DAG; 9516 SDValue LHS = N->getOperand(0); 9517 9518 // addcarry (add x, y), 0, cc => addcarry x, y, cc 9519 // subcarry (sub x, y), 0, cc => subcarry x, y, cc 9520 unsigned LHSOpc = LHS.getOpcode(); 9521 unsigned Opc = N->getOpcode(); 9522 if ((LHSOpc == ISD::ADD && Opc == ISD::ADDCARRY) || 9523 (LHSOpc == ISD::SUB && Opc == ISD::SUBCARRY)) { 9524 SDValue Args[] = { LHS.getOperand(0), LHS.getOperand(1), N->getOperand(2) }; 9525 return DAG.getNode(Opc, SDLoc(N), N->getVTList(), Args); 9526 } 9527 return SDValue(); 9528 } 9529 9530 SDValue SITargetLowering::performFAddCombine(SDNode *N, 9531 DAGCombinerInfo &DCI) const { 9532 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 9533 return SDValue(); 9534 9535 SelectionDAG &DAG = DCI.DAG; 9536 EVT VT = N->getValueType(0); 9537 9538 SDLoc SL(N); 9539 SDValue LHS = N->getOperand(0); 9540 SDValue RHS = N->getOperand(1); 9541 9542 // These should really be instruction patterns, but writing patterns with 9543 // source modiifiers is a pain. 9544 9545 // fadd (fadd (a, a), b) -> mad 2.0, a, b 9546 if (LHS.getOpcode() == ISD::FADD) { 9547 SDValue A = LHS.getOperand(0); 9548 if (A == LHS.getOperand(1)) { 9549 unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode()); 9550 if (FusedOp != 0) { 9551 const SDValue Two = DAG.getConstantFP(2.0, SL, VT); 9552 return DAG.getNode(FusedOp, SL, VT, A, Two, RHS); 9553 } 9554 } 9555 } 9556 9557 // fadd (b, fadd (a, a)) -> mad 2.0, a, b 9558 if (RHS.getOpcode() == ISD::FADD) { 9559 SDValue A = RHS.getOperand(0); 9560 if (A == RHS.getOperand(1)) { 9561 unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode()); 9562 if (FusedOp != 0) { 9563 const SDValue Two = DAG.getConstantFP(2.0, SL, VT); 9564 return DAG.getNode(FusedOp, SL, VT, A, Two, LHS); 9565 } 9566 } 9567 } 9568 9569 return SDValue(); 9570 } 9571 9572 SDValue SITargetLowering::performFSubCombine(SDNode *N, 9573 DAGCombinerInfo &DCI) const { 9574 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 9575 return SDValue(); 9576 9577 SelectionDAG &DAG = DCI.DAG; 9578 SDLoc SL(N); 9579 EVT VT = N->getValueType(0); 9580 assert(!VT.isVector()); 9581 9582 // Try to get the fneg to fold into the source modifier. This undoes generic 9583 // DAG combines and folds them into the mad. 9584 // 9585 // Only do this if we are not trying to support denormals. v_mad_f32 does 9586 // not support denormals ever. 9587 SDValue LHS = N->getOperand(0); 9588 SDValue RHS = N->getOperand(1); 9589 if (LHS.getOpcode() == ISD::FADD) { 9590 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c) 9591 SDValue A = LHS.getOperand(0); 9592 if (A == LHS.getOperand(1)) { 9593 unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode()); 9594 if (FusedOp != 0){ 9595 const SDValue Two = DAG.getConstantFP(2.0, SL, VT); 9596 SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 9597 9598 return DAG.getNode(FusedOp, SL, VT, A, Two, NegRHS); 9599 } 9600 } 9601 } 9602 9603 if (RHS.getOpcode() == ISD::FADD) { 9604 // (fsub c, (fadd a, a)) -> mad -2.0, a, c 9605 9606 SDValue A = RHS.getOperand(0); 9607 if (A == RHS.getOperand(1)) { 9608 unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode()); 9609 if (FusedOp != 0){ 9610 const SDValue NegTwo = DAG.getConstantFP(-2.0, SL, VT); 9611 return DAG.getNode(FusedOp, SL, VT, A, NegTwo, LHS); 9612 } 9613 } 9614 } 9615 9616 return SDValue(); 9617 } 9618 9619 SDValue SITargetLowering::performFMACombine(SDNode *N, 9620 DAGCombinerInfo &DCI) const { 9621 SelectionDAG &DAG = DCI.DAG; 9622 EVT VT = N->getValueType(0); 9623 SDLoc SL(N); 9624 9625 if (!Subtarget->hasDot2Insts() || VT != MVT::f32) 9626 return SDValue(); 9627 9628 // FMA((F32)S0.x, (F32)S1. x, FMA((F32)S0.y, (F32)S1.y, (F32)z)) -> 9629 // FDOT2((V2F16)S0, (V2F16)S1, (F32)z)) 9630 SDValue Op1 = N->getOperand(0); 9631 SDValue Op2 = N->getOperand(1); 9632 SDValue FMA = N->getOperand(2); 9633 9634 if (FMA.getOpcode() != ISD::FMA || 9635 Op1.getOpcode() != ISD::FP_EXTEND || 9636 Op2.getOpcode() != ISD::FP_EXTEND) 9637 return SDValue(); 9638 9639 // fdot2_f32_f16 always flushes fp32 denormal operand and output to zero, 9640 // regardless of the denorm mode setting. Therefore, unsafe-fp-math/fp-contract 9641 // is sufficient to allow generaing fdot2. 9642 const TargetOptions &Options = DAG.getTarget().Options; 9643 if (Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath || 9644 (N->getFlags().hasAllowContract() && 9645 FMA->getFlags().hasAllowContract())) { 9646 Op1 = Op1.getOperand(0); 9647 Op2 = Op2.getOperand(0); 9648 if (Op1.getOpcode() != ISD::EXTRACT_VECTOR_ELT || 9649 Op2.getOpcode() != ISD::EXTRACT_VECTOR_ELT) 9650 return SDValue(); 9651 9652 SDValue Vec1 = Op1.getOperand(0); 9653 SDValue Idx1 = Op1.getOperand(1); 9654 SDValue Vec2 = Op2.getOperand(0); 9655 9656 SDValue FMAOp1 = FMA.getOperand(0); 9657 SDValue FMAOp2 = FMA.getOperand(1); 9658 SDValue FMAAcc = FMA.getOperand(2); 9659 9660 if (FMAOp1.getOpcode() != ISD::FP_EXTEND || 9661 FMAOp2.getOpcode() != ISD::FP_EXTEND) 9662 return SDValue(); 9663 9664 FMAOp1 = FMAOp1.getOperand(0); 9665 FMAOp2 = FMAOp2.getOperand(0); 9666 if (FMAOp1.getOpcode() != ISD::EXTRACT_VECTOR_ELT || 9667 FMAOp2.getOpcode() != ISD::EXTRACT_VECTOR_ELT) 9668 return SDValue(); 9669 9670 SDValue Vec3 = FMAOp1.getOperand(0); 9671 SDValue Vec4 = FMAOp2.getOperand(0); 9672 SDValue Idx2 = FMAOp1.getOperand(1); 9673 9674 if (Idx1 != Op2.getOperand(1) || Idx2 != FMAOp2.getOperand(1) || 9675 // Idx1 and Idx2 cannot be the same. 9676 Idx1 == Idx2) 9677 return SDValue(); 9678 9679 if (Vec1 == Vec2 || Vec3 == Vec4) 9680 return SDValue(); 9681 9682 if (Vec1.getValueType() != MVT::v2f16 || Vec2.getValueType() != MVT::v2f16) 9683 return SDValue(); 9684 9685 if ((Vec1 == Vec3 && Vec2 == Vec4) || 9686 (Vec1 == Vec4 && Vec2 == Vec3)) { 9687 return DAG.getNode(AMDGPUISD::FDOT2, SL, MVT::f32, Vec1, Vec2, FMAAcc, 9688 DAG.getTargetConstant(0, SL, MVT::i1)); 9689 } 9690 } 9691 return SDValue(); 9692 } 9693 9694 SDValue SITargetLowering::performSetCCCombine(SDNode *N, 9695 DAGCombinerInfo &DCI) const { 9696 SelectionDAG &DAG = DCI.DAG; 9697 SDLoc SL(N); 9698 9699 SDValue LHS = N->getOperand(0); 9700 SDValue RHS = N->getOperand(1); 9701 EVT VT = LHS.getValueType(); 9702 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); 9703 9704 auto CRHS = dyn_cast<ConstantSDNode>(RHS); 9705 if (!CRHS) { 9706 CRHS = dyn_cast<ConstantSDNode>(LHS); 9707 if (CRHS) { 9708 std::swap(LHS, RHS); 9709 CC = getSetCCSwappedOperands(CC); 9710 } 9711 } 9712 9713 if (CRHS) { 9714 if (VT == MVT::i32 && LHS.getOpcode() == ISD::SIGN_EXTEND && 9715 isBoolSGPR(LHS.getOperand(0))) { 9716 // setcc (sext from i1 cc), -1, ne|sgt|ult) => not cc => xor cc, -1 9717 // setcc (sext from i1 cc), -1, eq|sle|uge) => cc 9718 // setcc (sext from i1 cc), 0, eq|sge|ule) => not cc => xor cc, -1 9719 // setcc (sext from i1 cc), 0, ne|ugt|slt) => cc 9720 if ((CRHS->isAllOnesValue() && 9721 (CC == ISD::SETNE || CC == ISD::SETGT || CC == ISD::SETULT)) || 9722 (CRHS->isNullValue() && 9723 (CC == ISD::SETEQ || CC == ISD::SETGE || CC == ISD::SETULE))) 9724 return DAG.getNode(ISD::XOR, SL, MVT::i1, LHS.getOperand(0), 9725 DAG.getConstant(-1, SL, MVT::i1)); 9726 if ((CRHS->isAllOnesValue() && 9727 (CC == ISD::SETEQ || CC == ISD::SETLE || CC == ISD::SETUGE)) || 9728 (CRHS->isNullValue() && 9729 (CC == ISD::SETNE || CC == ISD::SETUGT || CC == ISD::SETLT))) 9730 return LHS.getOperand(0); 9731 } 9732 9733 uint64_t CRHSVal = CRHS->getZExtValue(); 9734 if ((CC == ISD::SETEQ || CC == ISD::SETNE) && 9735 LHS.getOpcode() == ISD::SELECT && 9736 isa<ConstantSDNode>(LHS.getOperand(1)) && 9737 isa<ConstantSDNode>(LHS.getOperand(2)) && 9738 LHS.getConstantOperandVal(1) != LHS.getConstantOperandVal(2) && 9739 isBoolSGPR(LHS.getOperand(0))) { 9740 // Given CT != FT: 9741 // setcc (select cc, CT, CF), CF, eq => xor cc, -1 9742 // setcc (select cc, CT, CF), CF, ne => cc 9743 // setcc (select cc, CT, CF), CT, ne => xor cc, -1 9744 // setcc (select cc, CT, CF), CT, eq => cc 9745 uint64_t CT = LHS.getConstantOperandVal(1); 9746 uint64_t CF = LHS.getConstantOperandVal(2); 9747 9748 if ((CF == CRHSVal && CC == ISD::SETEQ) || 9749 (CT == CRHSVal && CC == ISD::SETNE)) 9750 return DAG.getNode(ISD::XOR, SL, MVT::i1, LHS.getOperand(0), 9751 DAG.getConstant(-1, SL, MVT::i1)); 9752 if ((CF == CRHSVal && CC == ISD::SETNE) || 9753 (CT == CRHSVal && CC == ISD::SETEQ)) 9754 return LHS.getOperand(0); 9755 } 9756 } 9757 9758 if (VT != MVT::f32 && VT != MVT::f64 && (Subtarget->has16BitInsts() && 9759 VT != MVT::f16)) 9760 return SDValue(); 9761 9762 // Match isinf/isfinite pattern 9763 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity)) 9764 // (fcmp one (fabs x), inf) -> (fp_class x, 9765 // (p_normal | n_normal | p_subnormal | n_subnormal | p_zero | n_zero) 9766 if ((CC == ISD::SETOEQ || CC == ISD::SETONE) && LHS.getOpcode() == ISD::FABS) { 9767 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS); 9768 if (!CRHS) 9769 return SDValue(); 9770 9771 const APFloat &APF = CRHS->getValueAPF(); 9772 if (APF.isInfinity() && !APF.isNegative()) { 9773 const unsigned IsInfMask = SIInstrFlags::P_INFINITY | 9774 SIInstrFlags::N_INFINITY; 9775 const unsigned IsFiniteMask = SIInstrFlags::N_ZERO | 9776 SIInstrFlags::P_ZERO | 9777 SIInstrFlags::N_NORMAL | 9778 SIInstrFlags::P_NORMAL | 9779 SIInstrFlags::N_SUBNORMAL | 9780 SIInstrFlags::P_SUBNORMAL; 9781 unsigned Mask = CC == ISD::SETOEQ ? IsInfMask : IsFiniteMask; 9782 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0), 9783 DAG.getConstant(Mask, SL, MVT::i32)); 9784 } 9785 } 9786 9787 return SDValue(); 9788 } 9789 9790 SDValue SITargetLowering::performCvtF32UByteNCombine(SDNode *N, 9791 DAGCombinerInfo &DCI) const { 9792 SelectionDAG &DAG = DCI.DAG; 9793 SDLoc SL(N); 9794 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0; 9795 9796 SDValue Src = N->getOperand(0); 9797 SDValue Srl = N->getOperand(0); 9798 if (Srl.getOpcode() == ISD::ZERO_EXTEND) 9799 Srl = Srl.getOperand(0); 9800 9801 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero. 9802 if (Srl.getOpcode() == ISD::SRL) { 9803 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x 9804 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x 9805 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x 9806 9807 if (const ConstantSDNode *C = 9808 dyn_cast<ConstantSDNode>(Srl.getOperand(1))) { 9809 Srl = DAG.getZExtOrTrunc(Srl.getOperand(0), SDLoc(Srl.getOperand(0)), 9810 EVT(MVT::i32)); 9811 9812 unsigned SrcOffset = C->getZExtValue() + 8 * Offset; 9813 if (SrcOffset < 32 && SrcOffset % 8 == 0) { 9814 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, SL, 9815 MVT::f32, Srl); 9816 } 9817 } 9818 } 9819 9820 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8); 9821 9822 KnownBits Known; 9823 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), 9824 !DCI.isBeforeLegalizeOps()); 9825 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9826 if (TLI.SimplifyDemandedBits(Src, Demanded, Known, TLO)) { 9827 DCI.CommitTargetLoweringOpt(TLO); 9828 } 9829 9830 return SDValue(); 9831 } 9832 9833 SDValue SITargetLowering::performClampCombine(SDNode *N, 9834 DAGCombinerInfo &DCI) const { 9835 ConstantFPSDNode *CSrc = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); 9836 if (!CSrc) 9837 return SDValue(); 9838 9839 const MachineFunction &MF = DCI.DAG.getMachineFunction(); 9840 const APFloat &F = CSrc->getValueAPF(); 9841 APFloat Zero = APFloat::getZero(F.getSemantics()); 9842 APFloat::cmpResult Cmp0 = F.compare(Zero); 9843 if (Cmp0 == APFloat::cmpLessThan || 9844 (Cmp0 == APFloat::cmpUnordered && 9845 MF.getInfo<SIMachineFunctionInfo>()->getMode().DX10Clamp)) { 9846 return DCI.DAG.getConstantFP(Zero, SDLoc(N), N->getValueType(0)); 9847 } 9848 9849 APFloat One(F.getSemantics(), "1.0"); 9850 APFloat::cmpResult Cmp1 = F.compare(One); 9851 if (Cmp1 == APFloat::cmpGreaterThan) 9852 return DCI.DAG.getConstantFP(One, SDLoc(N), N->getValueType(0)); 9853 9854 return SDValue(CSrc, 0); 9855 } 9856 9857 9858 SDValue SITargetLowering::PerformDAGCombine(SDNode *N, 9859 DAGCombinerInfo &DCI) const { 9860 if (getTargetMachine().getOptLevel() == CodeGenOpt::None) 9861 return SDValue(); 9862 switch (N->getOpcode()) { 9863 default: 9864 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); 9865 case ISD::ADD: 9866 return performAddCombine(N, DCI); 9867 case ISD::SUB: 9868 return performSubCombine(N, DCI); 9869 case ISD::ADDCARRY: 9870 case ISD::SUBCARRY: 9871 return performAddCarrySubCarryCombine(N, DCI); 9872 case ISD::FADD: 9873 return performFAddCombine(N, DCI); 9874 case ISD::FSUB: 9875 return performFSubCombine(N, DCI); 9876 case ISD::SETCC: 9877 return performSetCCCombine(N, DCI); 9878 case ISD::FMAXNUM: 9879 case ISD::FMINNUM: 9880 case ISD::FMAXNUM_IEEE: 9881 case ISD::FMINNUM_IEEE: 9882 case ISD::SMAX: 9883 case ISD::SMIN: 9884 case ISD::UMAX: 9885 case ISD::UMIN: 9886 case AMDGPUISD::FMIN_LEGACY: 9887 case AMDGPUISD::FMAX_LEGACY: 9888 return performMinMaxCombine(N, DCI); 9889 case ISD::FMA: 9890 return performFMACombine(N, DCI); 9891 case ISD::LOAD: { 9892 if (SDValue Widended = widenLoad(cast<LoadSDNode>(N), DCI)) 9893 return Widended; 9894 LLVM_FALLTHROUGH; 9895 } 9896 case ISD::STORE: 9897 case ISD::ATOMIC_LOAD: 9898 case ISD::ATOMIC_STORE: 9899 case ISD::ATOMIC_CMP_SWAP: 9900 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: 9901 case ISD::ATOMIC_SWAP: 9902 case ISD::ATOMIC_LOAD_ADD: 9903 case ISD::ATOMIC_LOAD_SUB: 9904 case ISD::ATOMIC_LOAD_AND: 9905 case ISD::ATOMIC_LOAD_OR: 9906 case ISD::ATOMIC_LOAD_XOR: 9907 case ISD::ATOMIC_LOAD_NAND: 9908 case ISD::ATOMIC_LOAD_MIN: 9909 case ISD::ATOMIC_LOAD_MAX: 9910 case ISD::ATOMIC_LOAD_UMIN: 9911 case ISD::ATOMIC_LOAD_UMAX: 9912 case ISD::ATOMIC_LOAD_FADD: 9913 case AMDGPUISD::ATOMIC_INC: 9914 case AMDGPUISD::ATOMIC_DEC: 9915 case AMDGPUISD::ATOMIC_LOAD_FMIN: 9916 case AMDGPUISD::ATOMIC_LOAD_FMAX: // TODO: Target mem intrinsics. 9917 if (DCI.isBeforeLegalize()) 9918 break; 9919 return performMemSDNodeCombine(cast<MemSDNode>(N), DCI); 9920 case ISD::AND: 9921 return performAndCombine(N, DCI); 9922 case ISD::OR: 9923 return performOrCombine(N, DCI); 9924 case ISD::XOR: 9925 return performXorCombine(N, DCI); 9926 case ISD::ZERO_EXTEND: 9927 return performZeroExtendCombine(N, DCI); 9928 case ISD::SIGN_EXTEND_INREG: 9929 return performSignExtendInRegCombine(N , DCI); 9930 case AMDGPUISD::FP_CLASS: 9931 return performClassCombine(N, DCI); 9932 case ISD::FCANONICALIZE: 9933 return performFCanonicalizeCombine(N, DCI); 9934 case AMDGPUISD::RCP: 9935 return performRcpCombine(N, DCI); 9936 case AMDGPUISD::FRACT: 9937 case AMDGPUISD::RSQ: 9938 case AMDGPUISD::RCP_LEGACY: 9939 case AMDGPUISD::RSQ_LEGACY: 9940 case AMDGPUISD::RCP_IFLAG: 9941 case AMDGPUISD::RSQ_CLAMP: 9942 case AMDGPUISD::LDEXP: { 9943 SDValue Src = N->getOperand(0); 9944 if (Src.isUndef()) 9945 return Src; 9946 break; 9947 } 9948 case ISD::SINT_TO_FP: 9949 case ISD::UINT_TO_FP: 9950 return performUCharToFloatCombine(N, DCI); 9951 case AMDGPUISD::CVT_F32_UBYTE0: 9952 case AMDGPUISD::CVT_F32_UBYTE1: 9953 case AMDGPUISD::CVT_F32_UBYTE2: 9954 case AMDGPUISD::CVT_F32_UBYTE3: 9955 return performCvtF32UByteNCombine(N, DCI); 9956 case AMDGPUISD::FMED3: 9957 return performFMed3Combine(N, DCI); 9958 case AMDGPUISD::CVT_PKRTZ_F16_F32: 9959 return performCvtPkRTZCombine(N, DCI); 9960 case AMDGPUISD::CLAMP: 9961 return performClampCombine(N, DCI); 9962 case ISD::SCALAR_TO_VECTOR: { 9963 SelectionDAG &DAG = DCI.DAG; 9964 EVT VT = N->getValueType(0); 9965 9966 // v2i16 (scalar_to_vector i16:x) -> v2i16 (bitcast (any_extend i16:x)) 9967 if (VT == MVT::v2i16 || VT == MVT::v2f16) { 9968 SDLoc SL(N); 9969 SDValue Src = N->getOperand(0); 9970 EVT EltVT = Src.getValueType(); 9971 if (EltVT == MVT::f16) 9972 Src = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Src); 9973 9974 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, Src); 9975 return DAG.getNode(ISD::BITCAST, SL, VT, Ext); 9976 } 9977 9978 break; 9979 } 9980 case ISD::EXTRACT_VECTOR_ELT: 9981 return performExtractVectorEltCombine(N, DCI); 9982 case ISD::INSERT_VECTOR_ELT: 9983 return performInsertVectorEltCombine(N, DCI); 9984 } 9985 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); 9986 } 9987 9988 /// Helper function for adjustWritemask 9989 static unsigned SubIdx2Lane(unsigned Idx) { 9990 switch (Idx) { 9991 default: return 0; 9992 case AMDGPU::sub0: return 0; 9993 case AMDGPU::sub1: return 1; 9994 case AMDGPU::sub2: return 2; 9995 case AMDGPU::sub3: return 3; 9996 case AMDGPU::sub4: return 4; // Possible with TFE/LWE 9997 } 9998 } 9999 10000 /// Adjust the writemask of MIMG instructions 10001 SDNode *SITargetLowering::adjustWritemask(MachineSDNode *&Node, 10002 SelectionDAG &DAG) const { 10003 unsigned Opcode = Node->getMachineOpcode(); 10004 10005 // Subtract 1 because the vdata output is not a MachineSDNode operand. 10006 int D16Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::d16) - 1; 10007 if (D16Idx >= 0 && Node->getConstantOperandVal(D16Idx)) 10008 return Node; // not implemented for D16 10009 10010 SDNode *Users[5] = { nullptr }; 10011 unsigned Lane = 0; 10012 unsigned DmaskIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::dmask) - 1; 10013 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx); 10014 unsigned NewDmask = 0; 10015 unsigned TFEIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::tfe) - 1; 10016 unsigned LWEIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::lwe) - 1; 10017 bool UsesTFC = (Node->getConstantOperandVal(TFEIdx) || 10018 Node->getConstantOperandVal(LWEIdx)) ? 1 : 0; 10019 unsigned TFCLane = 0; 10020 bool HasChain = Node->getNumValues() > 1; 10021 10022 if (OldDmask == 0) { 10023 // These are folded out, but on the chance it happens don't assert. 10024 return Node; 10025 } 10026 10027 unsigned OldBitsSet = countPopulation(OldDmask); 10028 // Work out which is the TFE/LWE lane if that is enabled. 10029 if (UsesTFC) { 10030 TFCLane = OldBitsSet; 10031 } 10032 10033 // Try to figure out the used register components 10034 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end(); 10035 I != E; ++I) { 10036 10037 // Don't look at users of the chain. 10038 if (I.getUse().getResNo() != 0) 10039 continue; 10040 10041 // Abort if we can't understand the usage 10042 if (!I->isMachineOpcode() || 10043 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG) 10044 return Node; 10045 10046 // Lane means which subreg of %vgpra_vgprb_vgprc_vgprd is used. 10047 // Note that subregs are packed, i.e. Lane==0 is the first bit set 10048 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit 10049 // set, etc. 10050 Lane = SubIdx2Lane(I->getConstantOperandVal(1)); 10051 10052 // Check if the use is for the TFE/LWE generated result at VGPRn+1. 10053 if (UsesTFC && Lane == TFCLane) { 10054 Users[Lane] = *I; 10055 } else { 10056 // Set which texture component corresponds to the lane. 10057 unsigned Comp; 10058 for (unsigned i = 0, Dmask = OldDmask; (i <= Lane) && (Dmask != 0); i++) { 10059 Comp = countTrailingZeros(Dmask); 10060 Dmask &= ~(1 << Comp); 10061 } 10062 10063 // Abort if we have more than one user per component. 10064 if (Users[Lane]) 10065 return Node; 10066 10067 Users[Lane] = *I; 10068 NewDmask |= 1 << Comp; 10069 } 10070 } 10071 10072 // Don't allow 0 dmask, as hardware assumes one channel enabled. 10073 bool NoChannels = !NewDmask; 10074 if (NoChannels) { 10075 if (!UsesTFC) { 10076 // No uses of the result and not using TFC. Then do nothing. 10077 return Node; 10078 } 10079 // If the original dmask has one channel - then nothing to do 10080 if (OldBitsSet == 1) 10081 return Node; 10082 // Use an arbitrary dmask - required for the instruction to work 10083 NewDmask = 1; 10084 } 10085 // Abort if there's no change 10086 if (NewDmask == OldDmask) 10087 return Node; 10088 10089 unsigned BitsSet = countPopulation(NewDmask); 10090 10091 // Check for TFE or LWE - increase the number of channels by one to account 10092 // for the extra return value 10093 // This will need adjustment for D16 if this is also included in 10094 // adjustWriteMask (this function) but at present D16 are excluded. 10095 unsigned NewChannels = BitsSet + UsesTFC; 10096 10097 int NewOpcode = 10098 AMDGPU::getMaskedMIMGOp(Node->getMachineOpcode(), NewChannels); 10099 assert(NewOpcode != -1 && 10100 NewOpcode != static_cast<int>(Node->getMachineOpcode()) && 10101 "failed to find equivalent MIMG op"); 10102 10103 // Adjust the writemask in the node 10104 SmallVector<SDValue, 12> Ops; 10105 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx); 10106 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32)); 10107 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end()); 10108 10109 MVT SVT = Node->getValueType(0).getVectorElementType().getSimpleVT(); 10110 10111 MVT ResultVT = NewChannels == 1 ? 10112 SVT : MVT::getVectorVT(SVT, NewChannels == 3 ? 4 : 10113 NewChannels == 5 ? 8 : NewChannels); 10114 SDVTList NewVTList = HasChain ? 10115 DAG.getVTList(ResultVT, MVT::Other) : DAG.getVTList(ResultVT); 10116 10117 10118 MachineSDNode *NewNode = DAG.getMachineNode(NewOpcode, SDLoc(Node), 10119 NewVTList, Ops); 10120 10121 if (HasChain) { 10122 // Update chain. 10123 DAG.setNodeMemRefs(NewNode, Node->memoperands()); 10124 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(NewNode, 1)); 10125 } 10126 10127 if (NewChannels == 1) { 10128 assert(Node->hasNUsesOfValue(1, 0)); 10129 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY, 10130 SDLoc(Node), Users[Lane]->getValueType(0), 10131 SDValue(NewNode, 0)); 10132 DAG.ReplaceAllUsesWith(Users[Lane], Copy); 10133 return nullptr; 10134 } 10135 10136 // Update the users of the node with the new indices 10137 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 5; ++i) { 10138 SDNode *User = Users[i]; 10139 if (!User) { 10140 // Handle the special case of NoChannels. We set NewDmask to 1 above, but 10141 // Users[0] is still nullptr because channel 0 doesn't really have a use. 10142 if (i || !NoChannels) 10143 continue; 10144 } else { 10145 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32); 10146 DAG.UpdateNodeOperands(User, SDValue(NewNode, 0), Op); 10147 } 10148 10149 switch (Idx) { 10150 default: break; 10151 case AMDGPU::sub0: Idx = AMDGPU::sub1; break; 10152 case AMDGPU::sub1: Idx = AMDGPU::sub2; break; 10153 case AMDGPU::sub2: Idx = AMDGPU::sub3; break; 10154 case AMDGPU::sub3: Idx = AMDGPU::sub4; break; 10155 } 10156 } 10157 10158 DAG.RemoveDeadNode(Node); 10159 return nullptr; 10160 } 10161 10162 static bool isFrameIndexOp(SDValue Op) { 10163 if (Op.getOpcode() == ISD::AssertZext) 10164 Op = Op.getOperand(0); 10165 10166 return isa<FrameIndexSDNode>(Op); 10167 } 10168 10169 /// Legalize target independent instructions (e.g. INSERT_SUBREG) 10170 /// with frame index operands. 10171 /// LLVM assumes that inputs are to these instructions are registers. 10172 SDNode *SITargetLowering::legalizeTargetIndependentNode(SDNode *Node, 10173 SelectionDAG &DAG) const { 10174 if (Node->getOpcode() == ISD::CopyToReg) { 10175 RegisterSDNode *DestReg = cast<RegisterSDNode>(Node->getOperand(1)); 10176 SDValue SrcVal = Node->getOperand(2); 10177 10178 // Insert a copy to a VReg_1 virtual register so LowerI1Copies doesn't have 10179 // to try understanding copies to physical registers. 10180 if (SrcVal.getValueType() == MVT::i1 && 10181 Register::isPhysicalRegister(DestReg->getReg())) { 10182 SDLoc SL(Node); 10183 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); 10184 SDValue VReg = DAG.getRegister( 10185 MRI.createVirtualRegister(&AMDGPU::VReg_1RegClass), MVT::i1); 10186 10187 SDNode *Glued = Node->getGluedNode(); 10188 SDValue ToVReg 10189 = DAG.getCopyToReg(Node->getOperand(0), SL, VReg, SrcVal, 10190 SDValue(Glued, Glued ? Glued->getNumValues() - 1 : 0)); 10191 SDValue ToResultReg 10192 = DAG.getCopyToReg(ToVReg, SL, SDValue(DestReg, 0), 10193 VReg, ToVReg.getValue(1)); 10194 DAG.ReplaceAllUsesWith(Node, ToResultReg.getNode()); 10195 DAG.RemoveDeadNode(Node); 10196 return ToResultReg.getNode(); 10197 } 10198 } 10199 10200 SmallVector<SDValue, 8> Ops; 10201 for (unsigned i = 0; i < Node->getNumOperands(); ++i) { 10202 if (!isFrameIndexOp(Node->getOperand(i))) { 10203 Ops.push_back(Node->getOperand(i)); 10204 continue; 10205 } 10206 10207 SDLoc DL(Node); 10208 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, 10209 Node->getOperand(i).getValueType(), 10210 Node->getOperand(i)), 0)); 10211 } 10212 10213 return DAG.UpdateNodeOperands(Node, Ops); 10214 } 10215 10216 /// Fold the instructions after selecting them. 10217 /// Returns null if users were already updated. 10218 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node, 10219 SelectionDAG &DAG) const { 10220 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 10221 unsigned Opcode = Node->getMachineOpcode(); 10222 10223 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() && 10224 !TII->isGather4(Opcode)) { 10225 return adjustWritemask(Node, DAG); 10226 } 10227 10228 if (Opcode == AMDGPU::INSERT_SUBREG || 10229 Opcode == AMDGPU::REG_SEQUENCE) { 10230 legalizeTargetIndependentNode(Node, DAG); 10231 return Node; 10232 } 10233 10234 switch (Opcode) { 10235 case AMDGPU::V_DIV_SCALE_F32: 10236 case AMDGPU::V_DIV_SCALE_F64: { 10237 // Satisfy the operand register constraint when one of the inputs is 10238 // undefined. Ordinarily each undef value will have its own implicit_def of 10239 // a vreg, so force these to use a single register. 10240 SDValue Src0 = Node->getOperand(0); 10241 SDValue Src1 = Node->getOperand(1); 10242 SDValue Src2 = Node->getOperand(2); 10243 10244 if ((Src0.isMachineOpcode() && 10245 Src0.getMachineOpcode() != AMDGPU::IMPLICIT_DEF) && 10246 (Src0 == Src1 || Src0 == Src2)) 10247 break; 10248 10249 MVT VT = Src0.getValueType().getSimpleVT(); 10250 const TargetRegisterClass *RC = 10251 getRegClassFor(VT, Src0.getNode()->isDivergent()); 10252 10253 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); 10254 SDValue UndefReg = DAG.getRegister(MRI.createVirtualRegister(RC), VT); 10255 10256 SDValue ImpDef = DAG.getCopyToReg(DAG.getEntryNode(), SDLoc(Node), 10257 UndefReg, Src0, SDValue()); 10258 10259 // src0 must be the same register as src1 or src2, even if the value is 10260 // undefined, so make sure we don't violate this constraint. 10261 if (Src0.isMachineOpcode() && 10262 Src0.getMachineOpcode() == AMDGPU::IMPLICIT_DEF) { 10263 if (Src1.isMachineOpcode() && 10264 Src1.getMachineOpcode() != AMDGPU::IMPLICIT_DEF) 10265 Src0 = Src1; 10266 else if (Src2.isMachineOpcode() && 10267 Src2.getMachineOpcode() != AMDGPU::IMPLICIT_DEF) 10268 Src0 = Src2; 10269 else { 10270 assert(Src1.getMachineOpcode() == AMDGPU::IMPLICIT_DEF); 10271 Src0 = UndefReg; 10272 Src1 = UndefReg; 10273 } 10274 } else 10275 break; 10276 10277 SmallVector<SDValue, 4> Ops = { Src0, Src1, Src2 }; 10278 for (unsigned I = 3, N = Node->getNumOperands(); I != N; ++I) 10279 Ops.push_back(Node->getOperand(I)); 10280 10281 Ops.push_back(ImpDef.getValue(1)); 10282 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops); 10283 } 10284 default: 10285 break; 10286 } 10287 10288 return Node; 10289 } 10290 10291 /// Assign the register class depending on the number of 10292 /// bits set in the writemask 10293 void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI, 10294 SDNode *Node) const { 10295 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 10296 10297 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 10298 10299 if (TII->isVOP3(MI.getOpcode())) { 10300 // Make sure constant bus requirements are respected. 10301 TII->legalizeOperandsVOP3(MRI, MI); 10302 10303 // Prefer VGPRs over AGPRs in mAI instructions where possible. 10304 // This saves a chain-copy of registers and better ballance register 10305 // use between vgpr and agpr as agpr tuples tend to be big. 10306 if (const MCOperandInfo *OpInfo = MI.getDesc().OpInfo) { 10307 unsigned Opc = MI.getOpcode(); 10308 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo(); 10309 for (auto I : { AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0), 10310 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1) }) { 10311 if (I == -1) 10312 break; 10313 MachineOperand &Op = MI.getOperand(I); 10314 if ((OpInfo[I].RegClass != llvm::AMDGPU::AV_64RegClassID && 10315 OpInfo[I].RegClass != llvm::AMDGPU::AV_32RegClassID) || 10316 !Register::isVirtualRegister(Op.getReg()) || 10317 !TRI->isAGPR(MRI, Op.getReg())) 10318 continue; 10319 auto *Src = MRI.getUniqueVRegDef(Op.getReg()); 10320 if (!Src || !Src->isCopy() || 10321 !TRI->isSGPRReg(MRI, Src->getOperand(1).getReg())) 10322 continue; 10323 auto *RC = TRI->getRegClassForReg(MRI, Op.getReg()); 10324 auto *NewRC = TRI->getEquivalentVGPRClass(RC); 10325 // All uses of agpr64 and agpr32 can also accept vgpr except for 10326 // v_accvgpr_read, but we do not produce agpr reads during selection, 10327 // so no use checks are needed. 10328 MRI.setRegClass(Op.getReg(), NewRC); 10329 } 10330 } 10331 10332 return; 10333 } 10334 10335 // Replace unused atomics with the no return version. 10336 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode()); 10337 if (NoRetAtomicOp != -1) { 10338 if (!Node->hasAnyUseOfValue(0)) { 10339 MI.setDesc(TII->get(NoRetAtomicOp)); 10340 MI.RemoveOperand(0); 10341 return; 10342 } 10343 10344 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg 10345 // instruction, because the return type of these instructions is a vec2 of 10346 // the memory type, so it can be tied to the input operand. 10347 // This means these instructions always have a use, so we need to add a 10348 // special case to check if the atomic has only one extract_subreg use, 10349 // which itself has no uses. 10350 if ((Node->hasNUsesOfValue(1, 0) && 10351 Node->use_begin()->isMachineOpcode() && 10352 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG && 10353 !Node->use_begin()->hasAnyUseOfValue(0))) { 10354 Register Def = MI.getOperand(0).getReg(); 10355 10356 // Change this into a noret atomic. 10357 MI.setDesc(TII->get(NoRetAtomicOp)); 10358 MI.RemoveOperand(0); 10359 10360 // If we only remove the def operand from the atomic instruction, the 10361 // extract_subreg will be left with a use of a vreg without a def. 10362 // So we need to insert an implicit_def to avoid machine verifier 10363 // errors. 10364 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), 10365 TII->get(AMDGPU::IMPLICIT_DEF), Def); 10366 } 10367 return; 10368 } 10369 } 10370 10371 static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL, 10372 uint64_t Val) { 10373 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32); 10374 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0); 10375 } 10376 10377 MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG, 10378 const SDLoc &DL, 10379 SDValue Ptr) const { 10380 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 10381 10382 // Build the half of the subregister with the constants before building the 10383 // full 128-bit register. If we are building multiple resource descriptors, 10384 // this will allow CSEing of the 2-component register. 10385 const SDValue Ops0[] = { 10386 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32), 10387 buildSMovImm32(DAG, DL, 0), 10388 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 10389 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32), 10390 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32) 10391 }; 10392 10393 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, 10394 MVT::v2i32, Ops0), 0); 10395 10396 // Combine the constants and the pointer. 10397 const SDValue Ops1[] = { 10398 DAG.getTargetConstant(AMDGPU::SGPR_128RegClassID, DL, MVT::i32), 10399 Ptr, 10400 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32), 10401 SubRegHi, 10402 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32) 10403 }; 10404 10405 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1); 10406 } 10407 10408 /// Return a resource descriptor with the 'Add TID' bit enabled 10409 /// The TID (Thread ID) is multiplied by the stride value (bits [61:48] 10410 /// of the resource descriptor) to create an offset, which is added to 10411 /// the resource pointer. 10412 MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL, 10413 SDValue Ptr, uint32_t RsrcDword1, 10414 uint64_t RsrcDword2And3) const { 10415 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr); 10416 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr); 10417 if (RsrcDword1) { 10418 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi, 10419 DAG.getConstant(RsrcDword1, DL, MVT::i32)), 10420 0); 10421 } 10422 10423 SDValue DataLo = buildSMovImm32(DAG, DL, 10424 RsrcDword2And3 & UINT64_C(0xFFFFFFFF)); 10425 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32); 10426 10427 const SDValue Ops[] = { 10428 DAG.getTargetConstant(AMDGPU::SGPR_128RegClassID, DL, MVT::i32), 10429 PtrLo, 10430 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 10431 PtrHi, 10432 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32), 10433 DataLo, 10434 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32), 10435 DataHi, 10436 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32) 10437 }; 10438 10439 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops); 10440 } 10441 10442 //===----------------------------------------------------------------------===// 10443 // SI Inline Assembly Support 10444 //===----------------------------------------------------------------------===// 10445 10446 std::pair<unsigned, const TargetRegisterClass *> 10447 SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 10448 StringRef Constraint, 10449 MVT VT) const { 10450 const TargetRegisterClass *RC = nullptr; 10451 if (Constraint.size() == 1) { 10452 switch (Constraint[0]) { 10453 default: 10454 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 10455 case 's': 10456 case 'r': 10457 switch (VT.getSizeInBits()) { 10458 default: 10459 return std::make_pair(0U, nullptr); 10460 case 32: 10461 case 16: 10462 RC = &AMDGPU::SReg_32RegClass; 10463 break; 10464 case 64: 10465 RC = &AMDGPU::SGPR_64RegClass; 10466 break; 10467 case 96: 10468 RC = &AMDGPU::SReg_96RegClass; 10469 break; 10470 case 128: 10471 RC = &AMDGPU::SGPR_128RegClass; 10472 break; 10473 case 160: 10474 RC = &AMDGPU::SReg_160RegClass; 10475 break; 10476 case 256: 10477 RC = &AMDGPU::SReg_256RegClass; 10478 break; 10479 case 512: 10480 RC = &AMDGPU::SReg_512RegClass; 10481 break; 10482 } 10483 break; 10484 case 'v': 10485 switch (VT.getSizeInBits()) { 10486 default: 10487 return std::make_pair(0U, nullptr); 10488 case 32: 10489 case 16: 10490 RC = &AMDGPU::VGPR_32RegClass; 10491 break; 10492 case 64: 10493 RC = &AMDGPU::VReg_64RegClass; 10494 break; 10495 case 96: 10496 RC = &AMDGPU::VReg_96RegClass; 10497 break; 10498 case 128: 10499 RC = &AMDGPU::VReg_128RegClass; 10500 break; 10501 case 160: 10502 RC = &AMDGPU::VReg_160RegClass; 10503 break; 10504 case 256: 10505 RC = &AMDGPU::VReg_256RegClass; 10506 break; 10507 case 512: 10508 RC = &AMDGPU::VReg_512RegClass; 10509 break; 10510 } 10511 break; 10512 case 'a': 10513 if (!Subtarget->hasMAIInsts()) 10514 break; 10515 switch (VT.getSizeInBits()) { 10516 default: 10517 return std::make_pair(0U, nullptr); 10518 case 32: 10519 case 16: 10520 RC = &AMDGPU::AGPR_32RegClass; 10521 break; 10522 case 64: 10523 RC = &AMDGPU::AReg_64RegClass; 10524 break; 10525 case 128: 10526 RC = &AMDGPU::AReg_128RegClass; 10527 break; 10528 case 512: 10529 RC = &AMDGPU::AReg_512RegClass; 10530 break; 10531 case 1024: 10532 RC = &AMDGPU::AReg_1024RegClass; 10533 // v32 types are not legal but we support them here. 10534 return std::make_pair(0U, RC); 10535 } 10536 break; 10537 } 10538 // We actually support i128, i16 and f16 as inline parameters 10539 // even if they are not reported as legal 10540 if (RC && (isTypeLegal(VT) || VT.SimpleTy == MVT::i128 || 10541 VT.SimpleTy == MVT::i16 || VT.SimpleTy == MVT::f16)) 10542 return std::make_pair(0U, RC); 10543 } 10544 10545 if (Constraint.size() > 1) { 10546 if (Constraint[1] == 'v') { 10547 RC = &AMDGPU::VGPR_32RegClass; 10548 } else if (Constraint[1] == 's') { 10549 RC = &AMDGPU::SGPR_32RegClass; 10550 } else if (Constraint[1] == 'a') { 10551 RC = &AMDGPU::AGPR_32RegClass; 10552 } 10553 10554 if (RC) { 10555 uint32_t Idx; 10556 bool Failed = Constraint.substr(2).getAsInteger(10, Idx); 10557 if (!Failed && Idx < RC->getNumRegs()) 10558 return std::make_pair(RC->getRegister(Idx), RC); 10559 } 10560 } 10561 10562 // FIXME: Returns VS_32 for physical SGPR constraints 10563 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 10564 } 10565 10566 SITargetLowering::ConstraintType 10567 SITargetLowering::getConstraintType(StringRef Constraint) const { 10568 if (Constraint.size() == 1) { 10569 switch (Constraint[0]) { 10570 default: break; 10571 case 's': 10572 case 'v': 10573 case 'a': 10574 return C_RegisterClass; 10575 } 10576 } 10577 return TargetLowering::getConstraintType(Constraint); 10578 } 10579 10580 // Figure out which registers should be reserved for stack access. Only after 10581 // the function is legalized do we know all of the non-spill stack objects or if 10582 // calls are present. 10583 void SITargetLowering::finalizeLowering(MachineFunction &MF) const { 10584 MachineRegisterInfo &MRI = MF.getRegInfo(); 10585 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 10586 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 10587 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo(); 10588 10589 if (Info->isEntryFunction()) { 10590 // Callable functions have fixed registers used for stack access. 10591 reservePrivateMemoryRegs(getTargetMachine(), MF, *TRI, *Info); 10592 } 10593 10594 assert(!TRI->isSubRegister(Info->getScratchRSrcReg(), 10595 Info->getStackPtrOffsetReg())); 10596 if (Info->getStackPtrOffsetReg() != AMDGPU::SP_REG) 10597 MRI.replaceRegWith(AMDGPU::SP_REG, Info->getStackPtrOffsetReg()); 10598 10599 // We need to worry about replacing the default register with itself in case 10600 // of MIR testcases missing the MFI. 10601 if (Info->getScratchRSrcReg() != AMDGPU::PRIVATE_RSRC_REG) 10602 MRI.replaceRegWith(AMDGPU::PRIVATE_RSRC_REG, Info->getScratchRSrcReg()); 10603 10604 if (Info->getFrameOffsetReg() != AMDGPU::FP_REG) 10605 MRI.replaceRegWith(AMDGPU::FP_REG, Info->getFrameOffsetReg()); 10606 10607 if (Info->getScratchWaveOffsetReg() != AMDGPU::SCRATCH_WAVE_OFFSET_REG) { 10608 MRI.replaceRegWith(AMDGPU::SCRATCH_WAVE_OFFSET_REG, 10609 Info->getScratchWaveOffsetReg()); 10610 } 10611 10612 Info->limitOccupancy(MF); 10613 10614 if (ST.isWave32() && !MF.empty()) { 10615 // Add VCC_HI def because many instructions marked as imp-use VCC where 10616 // we may only define VCC_LO. If nothing defines VCC_HI we may end up 10617 // having a use of undef. 10618 10619 const SIInstrInfo *TII = ST.getInstrInfo(); 10620 DebugLoc DL; 10621 10622 MachineBasicBlock &MBB = MF.front(); 10623 MachineBasicBlock::iterator I = MBB.getFirstNonDebugInstr(); 10624 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), AMDGPU::VCC_HI); 10625 10626 for (auto &MBB : MF) { 10627 for (auto &MI : MBB) { 10628 TII->fixImplicitOperands(MI); 10629 } 10630 } 10631 } 10632 10633 TargetLoweringBase::finalizeLowering(MF); 10634 } 10635 10636 void SITargetLowering::computeKnownBitsForFrameIndex(const SDValue Op, 10637 KnownBits &Known, 10638 const APInt &DemandedElts, 10639 const SelectionDAG &DAG, 10640 unsigned Depth) const { 10641 TargetLowering::computeKnownBitsForFrameIndex(Op, Known, DemandedElts, 10642 DAG, Depth); 10643 10644 // Set the high bits to zero based on the maximum allowed scratch size per 10645 // wave. We can't use vaddr in MUBUF instructions if we don't know the address 10646 // calculation won't overflow, so assume the sign bit is never set. 10647 Known.Zero.setHighBits(getSubtarget()->getKnownHighZeroBitsForFrameIndex()); 10648 } 10649 10650 Align SITargetLowering::getPrefLoopAlignment(MachineLoop *ML) const { 10651 const Align PrefAlign = TargetLowering::getPrefLoopAlignment(ML); 10652 const Align CacheLineAlign = Align(64); 10653 10654 // Pre-GFX10 target did not benefit from loop alignment 10655 if (!ML || DisableLoopAlignment || 10656 (getSubtarget()->getGeneration() < AMDGPUSubtarget::GFX10) || 10657 getSubtarget()->hasInstFwdPrefetchBug()) 10658 return PrefAlign; 10659 10660 // On GFX10 I$ is 4 x 64 bytes cache lines. 10661 // By default prefetcher keeps one cache line behind and reads two ahead. 10662 // We can modify it with S_INST_PREFETCH for larger loops to have two lines 10663 // behind and one ahead. 10664 // Therefor we can benefit from aligning loop headers if loop fits 192 bytes. 10665 // If loop fits 64 bytes it always spans no more than two cache lines and 10666 // does not need an alignment. 10667 // Else if loop is less or equal 128 bytes we do not need to modify prefetch, 10668 // Else if loop is less or equal 192 bytes we need two lines behind. 10669 10670 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 10671 const MachineBasicBlock *Header = ML->getHeader(); 10672 if (Header->getAlignment() != PrefAlign) 10673 return Header->getAlignment(); // Already processed. 10674 10675 unsigned LoopSize = 0; 10676 for (const MachineBasicBlock *MBB : ML->blocks()) { 10677 // If inner loop block is aligned assume in average half of the alignment 10678 // size to be added as nops. 10679 if (MBB != Header) 10680 LoopSize += MBB->getAlignment().value() / 2; 10681 10682 for (const MachineInstr &MI : *MBB) { 10683 LoopSize += TII->getInstSizeInBytes(MI); 10684 if (LoopSize > 192) 10685 return PrefAlign; 10686 } 10687 } 10688 10689 if (LoopSize <= 64) 10690 return PrefAlign; 10691 10692 if (LoopSize <= 128) 10693 return CacheLineAlign; 10694 10695 // If any of parent loops is surrounded by prefetch instructions do not 10696 // insert new for inner loop, which would reset parent's settings. 10697 for (MachineLoop *P = ML->getParentLoop(); P; P = P->getParentLoop()) { 10698 if (MachineBasicBlock *Exit = P->getExitBlock()) { 10699 auto I = Exit->getFirstNonDebugInstr(); 10700 if (I != Exit->end() && I->getOpcode() == AMDGPU::S_INST_PREFETCH) 10701 return CacheLineAlign; 10702 } 10703 } 10704 10705 MachineBasicBlock *Pre = ML->getLoopPreheader(); 10706 MachineBasicBlock *Exit = ML->getExitBlock(); 10707 10708 if (Pre && Exit) { 10709 BuildMI(*Pre, Pre->getFirstTerminator(), DebugLoc(), 10710 TII->get(AMDGPU::S_INST_PREFETCH)) 10711 .addImm(1); // prefetch 2 lines behind PC 10712 10713 BuildMI(*Exit, Exit->getFirstNonDebugInstr(), DebugLoc(), 10714 TII->get(AMDGPU::S_INST_PREFETCH)) 10715 .addImm(2); // prefetch 1 line behind PC 10716 } 10717 10718 return CacheLineAlign; 10719 } 10720 10721 LLVM_ATTRIBUTE_UNUSED 10722 static bool isCopyFromRegOfInlineAsm(const SDNode *N) { 10723 assert(N->getOpcode() == ISD::CopyFromReg); 10724 do { 10725 // Follow the chain until we find an INLINEASM node. 10726 N = N->getOperand(0).getNode(); 10727 if (N->getOpcode() == ISD::INLINEASM || 10728 N->getOpcode() == ISD::INLINEASM_BR) 10729 return true; 10730 } while (N->getOpcode() == ISD::CopyFromReg); 10731 return false; 10732 } 10733 10734 bool SITargetLowering::isSDNodeSourceOfDivergence(const SDNode * N, 10735 FunctionLoweringInfo * FLI, LegacyDivergenceAnalysis * KDA) const 10736 { 10737 switch (N->getOpcode()) { 10738 case ISD::CopyFromReg: 10739 { 10740 const RegisterSDNode *R = cast<RegisterSDNode>(N->getOperand(1)); 10741 const MachineFunction * MF = FLI->MF; 10742 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>(); 10743 const MachineRegisterInfo &MRI = MF->getRegInfo(); 10744 const SIRegisterInfo &TRI = ST.getInstrInfo()->getRegisterInfo(); 10745 unsigned Reg = R->getReg(); 10746 if (Register::isPhysicalRegister(Reg)) 10747 return !TRI.isSGPRReg(MRI, Reg); 10748 10749 if (MRI.isLiveIn(Reg)) { 10750 // workitem.id.x workitem.id.y workitem.id.z 10751 // Any VGPR formal argument is also considered divergent 10752 if (!TRI.isSGPRReg(MRI, Reg)) 10753 return true; 10754 // Formal arguments of non-entry functions 10755 // are conservatively considered divergent 10756 else if (!AMDGPU::isEntryFunctionCC(FLI->Fn->getCallingConv())) 10757 return true; 10758 return false; 10759 } 10760 const Value *V = FLI->getValueFromVirtualReg(Reg); 10761 if (V) 10762 return KDA->isDivergent(V); 10763 assert(Reg == FLI->DemoteRegister || isCopyFromRegOfInlineAsm(N)); 10764 return !TRI.isSGPRReg(MRI, Reg); 10765 } 10766 break; 10767 case ISD::LOAD: { 10768 const LoadSDNode *L = cast<LoadSDNode>(N); 10769 unsigned AS = L->getAddressSpace(); 10770 // A flat load may access private memory. 10771 return AS == AMDGPUAS::PRIVATE_ADDRESS || AS == AMDGPUAS::FLAT_ADDRESS; 10772 } break; 10773 case ISD::CALLSEQ_END: 10774 return true; 10775 break; 10776 case ISD::INTRINSIC_WO_CHAIN: 10777 { 10778 10779 } 10780 return AMDGPU::isIntrinsicSourceOfDivergence( 10781 cast<ConstantSDNode>(N->getOperand(0))->getZExtValue()); 10782 case ISD::INTRINSIC_W_CHAIN: 10783 return AMDGPU::isIntrinsicSourceOfDivergence( 10784 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()); 10785 } 10786 return false; 10787 } 10788 10789 bool SITargetLowering::denormalsEnabledForType(const SelectionDAG &DAG, 10790 EVT VT) const { 10791 switch (VT.getScalarType().getSimpleVT().SimpleTy) { 10792 case MVT::f32: 10793 return hasFP32Denormals(DAG.getMachineFunction()); 10794 case MVT::f64: 10795 case MVT::f16: 10796 return hasFP64FP16Denormals(DAG.getMachineFunction()); 10797 default: 10798 return false; 10799 } 10800 } 10801 10802 bool SITargetLowering::isKnownNeverNaNForTargetNode(SDValue Op, 10803 const SelectionDAG &DAG, 10804 bool SNaN, 10805 unsigned Depth) const { 10806 if (Op.getOpcode() == AMDGPUISD::CLAMP) { 10807 const MachineFunction &MF = DAG.getMachineFunction(); 10808 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 10809 10810 if (Info->getMode().DX10Clamp) 10811 return true; // Clamped to 0. 10812 return DAG.isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1); 10813 } 10814 10815 return AMDGPUTargetLowering::isKnownNeverNaNForTargetNode(Op, DAG, 10816 SNaN, Depth); 10817 } 10818 10819 TargetLowering::AtomicExpansionKind 10820 SITargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *RMW) const { 10821 switch (RMW->getOperation()) { 10822 case AtomicRMWInst::FAdd: { 10823 Type *Ty = RMW->getType(); 10824 10825 // We don't have a way to support 16-bit atomics now, so just leave them 10826 // as-is. 10827 if (Ty->isHalfTy()) 10828 return AtomicExpansionKind::None; 10829 10830 if (!Ty->isFloatTy()) 10831 return AtomicExpansionKind::CmpXChg; 10832 10833 // TODO: Do have these for flat. Older targets also had them for buffers. 10834 unsigned AS = RMW->getPointerAddressSpace(); 10835 10836 if (AS == AMDGPUAS::GLOBAL_ADDRESS && Subtarget->hasAtomicFaddInsts()) { 10837 return RMW->use_empty() ? AtomicExpansionKind::None : 10838 AtomicExpansionKind::CmpXChg; 10839 } 10840 10841 return (AS == AMDGPUAS::LOCAL_ADDRESS && Subtarget->hasLDSFPAtomics()) ? 10842 AtomicExpansionKind::None : AtomicExpansionKind::CmpXChg; 10843 } 10844 default: 10845 break; 10846 } 10847 10848 return AMDGPUTargetLowering::shouldExpandAtomicRMWInIR(RMW); 10849 } 10850 10851 const TargetRegisterClass * 10852 SITargetLowering::getRegClassFor(MVT VT, bool isDivergent) const { 10853 const TargetRegisterClass *RC = TargetLoweringBase::getRegClassFor(VT, false); 10854 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo(); 10855 if (RC == &AMDGPU::VReg_1RegClass && !isDivergent) 10856 return Subtarget->getWavefrontSize() == 64 ? &AMDGPU::SReg_64RegClass 10857 : &AMDGPU::SReg_32RegClass; 10858 if (!TRI->isSGPRClass(RC) && !isDivergent) 10859 return TRI->getEquivalentSGPRClass(RC); 10860 else if (TRI->isSGPRClass(RC) && isDivergent) 10861 return TRI->getEquivalentVGPRClass(RC); 10862 10863 return RC; 10864 } 10865 10866 static bool hasCFUser(const Value *V, SmallPtrSet<const Value *, 16> &Visited, 10867 unsigned WaveSize) { 10868 // FIXME: We asssume we never cast the mask results of a control flow 10869 // intrinsic. 10870 // Early exit if the type won't be consistent as a compile time hack. 10871 IntegerType *IT = dyn_cast<IntegerType>(V->getType()); 10872 if (!IT || IT->getBitWidth() != WaveSize) 10873 return false; 10874 10875 if (!isa<Instruction>(V)) 10876 return false; 10877 if (!Visited.insert(V).second) 10878 return false; 10879 bool Result = false; 10880 for (auto U : V->users()) { 10881 if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(U)) { 10882 if (V == U->getOperand(1)) { 10883 switch (Intrinsic->getIntrinsicID()) { 10884 default: 10885 Result = false; 10886 break; 10887 case Intrinsic::amdgcn_if_break: 10888 case Intrinsic::amdgcn_if: 10889 case Intrinsic::amdgcn_else: 10890 Result = true; 10891 break; 10892 } 10893 } 10894 if (V == U->getOperand(0)) { 10895 switch (Intrinsic->getIntrinsicID()) { 10896 default: 10897 Result = false; 10898 break; 10899 case Intrinsic::amdgcn_end_cf: 10900 case Intrinsic::amdgcn_loop: 10901 Result = true; 10902 break; 10903 } 10904 } 10905 } else { 10906 Result = hasCFUser(U, Visited, WaveSize); 10907 } 10908 if (Result) 10909 break; 10910 } 10911 return Result; 10912 } 10913 10914 bool SITargetLowering::requiresUniformRegister(MachineFunction &MF, 10915 const Value *V) const { 10916 if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) { 10917 switch (Intrinsic->getIntrinsicID()) { 10918 default: 10919 return false; 10920 case Intrinsic::amdgcn_if_break: 10921 return true; 10922 } 10923 } 10924 if (const ExtractValueInst *ExtValue = dyn_cast<ExtractValueInst>(V)) { 10925 if (const IntrinsicInst *Intrinsic = 10926 dyn_cast<IntrinsicInst>(ExtValue->getOperand(0))) { 10927 switch (Intrinsic->getIntrinsicID()) { 10928 default: 10929 return false; 10930 case Intrinsic::amdgcn_if: 10931 case Intrinsic::amdgcn_else: { 10932 ArrayRef<unsigned> Indices = ExtValue->getIndices(); 10933 if (Indices.size() == 1 && Indices[0] == 1) { 10934 return true; 10935 } 10936 } 10937 } 10938 } 10939 } 10940 if (const CallInst *CI = dyn_cast<CallInst>(V)) { 10941 if (isa<InlineAsm>(CI->getCalledValue())) { 10942 const SIRegisterInfo *SIRI = Subtarget->getRegisterInfo(); 10943 ImmutableCallSite CS(CI); 10944 TargetLowering::AsmOperandInfoVector TargetConstraints = ParseConstraints( 10945 MF.getDataLayout(), Subtarget->getRegisterInfo(), CS); 10946 for (auto &TC : TargetConstraints) { 10947 if (TC.Type == InlineAsm::isOutput) { 10948 ComputeConstraintToUse(TC, SDValue()); 10949 unsigned AssignedReg; 10950 const TargetRegisterClass *RC; 10951 std::tie(AssignedReg, RC) = getRegForInlineAsmConstraint( 10952 SIRI, TC.ConstraintCode, TC.ConstraintVT); 10953 if (RC) { 10954 MachineRegisterInfo &MRI = MF.getRegInfo(); 10955 if (AssignedReg != 0 && SIRI->isSGPRReg(MRI, AssignedReg)) 10956 return true; 10957 else if (SIRI->isSGPRClass(RC)) 10958 return true; 10959 } 10960 } 10961 } 10962 } 10963 } 10964 SmallPtrSet<const Value *, 16> Visited; 10965 return hasCFUser(V, Visited, Subtarget->getWavefrontSize()); 10966 } 10967