1e8d8bef9SDimitry Andric //===- AMDGPInstCombineIntrinsic.cpp - AMDGPU specific InstCombine pass ---===// 2e8d8bef9SDimitry Andric // 3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e8d8bef9SDimitry Andric // 7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 8e8d8bef9SDimitry Andric // 9e8d8bef9SDimitry Andric // \file 10e8d8bef9SDimitry Andric // This file implements a TargetTransformInfo analysis pass specific to the 11e8d8bef9SDimitry Andric // AMDGPU target machine. It uses the target's detailed information to provide 12e8d8bef9SDimitry Andric // more precise answers to certain TTI queries, while letting the target 13e8d8bef9SDimitry Andric // independent and default TTI implementations handle the rest. 14e8d8bef9SDimitry Andric // 15e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 16e8d8bef9SDimitry Andric 17e8d8bef9SDimitry Andric #include "AMDGPUInstrInfo.h" 18e8d8bef9SDimitry Andric #include "AMDGPUTargetTransformInfo.h" 19e8d8bef9SDimitry Andric #include "GCNSubtarget.h" 20*bdd1243dSDimitry Andric #include "llvm/ADT/FloatingPointMode.h" 21e8d8bef9SDimitry Andric #include "llvm/IR/IntrinsicsAMDGPU.h" 22e8d8bef9SDimitry Andric #include "llvm/Transforms/InstCombine/InstCombiner.h" 23*bdd1243dSDimitry Andric #include <optional> 24e8d8bef9SDimitry Andric 25e8d8bef9SDimitry Andric using namespace llvm; 26e8d8bef9SDimitry Andric 27e8d8bef9SDimitry Andric #define DEBUG_TYPE "AMDGPUtti" 28e8d8bef9SDimitry Andric 29e8d8bef9SDimitry Andric namespace { 30e8d8bef9SDimitry Andric 31e8d8bef9SDimitry Andric struct AMDGPUImageDMaskIntrinsic { 32e8d8bef9SDimitry Andric unsigned Intr; 33e8d8bef9SDimitry Andric }; 34e8d8bef9SDimitry Andric 35e8d8bef9SDimitry Andric #define GET_AMDGPUImageDMaskIntrinsicTable_IMPL 36e8d8bef9SDimitry Andric #include "InstCombineTables.inc" 37e8d8bef9SDimitry Andric 38e8d8bef9SDimitry Andric } // end anonymous namespace 39e8d8bef9SDimitry Andric 40e8d8bef9SDimitry Andric // Constant fold llvm.amdgcn.fmed3 intrinsics for standard inputs. 41e8d8bef9SDimitry Andric // 42e8d8bef9SDimitry Andric // A single NaN input is folded to minnum, so we rely on that folding for 43e8d8bef9SDimitry Andric // handling NaNs. 44e8d8bef9SDimitry Andric static APFloat fmed3AMDGCN(const APFloat &Src0, const APFloat &Src1, 45e8d8bef9SDimitry Andric const APFloat &Src2) { 46e8d8bef9SDimitry Andric APFloat Max3 = maxnum(maxnum(Src0, Src1), Src2); 47e8d8bef9SDimitry Andric 48e8d8bef9SDimitry Andric APFloat::cmpResult Cmp0 = Max3.compare(Src0); 49e8d8bef9SDimitry Andric assert(Cmp0 != APFloat::cmpUnordered && "nans handled separately"); 50e8d8bef9SDimitry Andric if (Cmp0 == APFloat::cmpEqual) 51e8d8bef9SDimitry Andric return maxnum(Src1, Src2); 52e8d8bef9SDimitry Andric 53e8d8bef9SDimitry Andric APFloat::cmpResult Cmp1 = Max3.compare(Src1); 54e8d8bef9SDimitry Andric assert(Cmp1 != APFloat::cmpUnordered && "nans handled separately"); 55e8d8bef9SDimitry Andric if (Cmp1 == APFloat::cmpEqual) 56e8d8bef9SDimitry Andric return maxnum(Src0, Src2); 57e8d8bef9SDimitry Andric 58e8d8bef9SDimitry Andric return maxnum(Src0, Src1); 59e8d8bef9SDimitry Andric } 60e8d8bef9SDimitry Andric 61e8d8bef9SDimitry Andric // Check if a value can be converted to a 16-bit value without losing 62e8d8bef9SDimitry Andric // precision. 6304eeddc0SDimitry Andric // The value is expected to be either a float (IsFloat = true) or an unsigned 6404eeddc0SDimitry Andric // integer (IsFloat = false). 6504eeddc0SDimitry Andric static bool canSafelyConvertTo16Bit(Value &V, bool IsFloat) { 66e8d8bef9SDimitry Andric Type *VTy = V.getType(); 67e8d8bef9SDimitry Andric if (VTy->isHalfTy() || VTy->isIntegerTy(16)) { 68e8d8bef9SDimitry Andric // The value is already 16-bit, so we don't want to convert to 16-bit again! 69e8d8bef9SDimitry Andric return false; 70e8d8bef9SDimitry Andric } 7104eeddc0SDimitry Andric if (IsFloat) { 72e8d8bef9SDimitry Andric if (ConstantFP *ConstFloat = dyn_cast<ConstantFP>(&V)) { 7304eeddc0SDimitry Andric // We need to check that if we cast the index down to a half, we do not 7404eeddc0SDimitry Andric // lose precision. 75e8d8bef9SDimitry Andric APFloat FloatValue(ConstFloat->getValueAPF()); 76e8d8bef9SDimitry Andric bool LosesInfo = true; 7704eeddc0SDimitry Andric FloatValue.convert(APFloat::IEEEhalf(), APFloat::rmTowardZero, 7804eeddc0SDimitry Andric &LosesInfo); 79e8d8bef9SDimitry Andric return !LosesInfo; 80e8d8bef9SDimitry Andric } 8104eeddc0SDimitry Andric } else { 8204eeddc0SDimitry Andric if (ConstantInt *ConstInt = dyn_cast<ConstantInt>(&V)) { 8304eeddc0SDimitry Andric // We need to check that if we cast the index down to an i16, we do not 8404eeddc0SDimitry Andric // lose precision. 8504eeddc0SDimitry Andric APInt IntValue(ConstInt->getValue()); 8604eeddc0SDimitry Andric return IntValue.getActiveBits() <= 16; 8704eeddc0SDimitry Andric } 8804eeddc0SDimitry Andric } 8904eeddc0SDimitry Andric 90e8d8bef9SDimitry Andric Value *CastSrc; 9104eeddc0SDimitry Andric bool IsExt = IsFloat ? match(&V, m_FPExt(PatternMatch::m_Value(CastSrc))) 9204eeddc0SDimitry Andric : match(&V, m_ZExt(PatternMatch::m_Value(CastSrc))); 9304eeddc0SDimitry Andric if (IsExt) { 94e8d8bef9SDimitry Andric Type *CastSrcTy = CastSrc->getType(); 95e8d8bef9SDimitry Andric if (CastSrcTy->isHalfTy() || CastSrcTy->isIntegerTy(16)) 96e8d8bef9SDimitry Andric return true; 97e8d8bef9SDimitry Andric } 98e8d8bef9SDimitry Andric 99e8d8bef9SDimitry Andric return false; 100e8d8bef9SDimitry Andric } 101e8d8bef9SDimitry Andric 102e8d8bef9SDimitry Andric // Convert a value to 16-bit. 103e8d8bef9SDimitry Andric static Value *convertTo16Bit(Value &V, InstCombiner::BuilderTy &Builder) { 104e8d8bef9SDimitry Andric Type *VTy = V.getType(); 105e8d8bef9SDimitry Andric if (isa<FPExtInst>(&V) || isa<SExtInst>(&V) || isa<ZExtInst>(&V)) 106e8d8bef9SDimitry Andric return cast<Instruction>(&V)->getOperand(0); 107e8d8bef9SDimitry Andric if (VTy->isIntegerTy()) 108e8d8bef9SDimitry Andric return Builder.CreateIntCast(&V, Type::getInt16Ty(V.getContext()), false); 109e8d8bef9SDimitry Andric if (VTy->isFloatingPointTy()) 110e8d8bef9SDimitry Andric return Builder.CreateFPCast(&V, Type::getHalfTy(V.getContext())); 111e8d8bef9SDimitry Andric 112e8d8bef9SDimitry Andric llvm_unreachable("Should never be called!"); 113e8d8bef9SDimitry Andric } 114e8d8bef9SDimitry Andric 11581ad6265SDimitry Andric /// Applies Func(OldIntr.Args, OldIntr.ArgTys), creates intrinsic call with 11681ad6265SDimitry Andric /// modified arguments (based on OldIntr) and replaces InstToReplace with 11781ad6265SDimitry Andric /// this newly created intrinsic call. 118*bdd1243dSDimitry Andric static std::optional<Instruction *> modifyIntrinsicCall( 11981ad6265SDimitry Andric IntrinsicInst &OldIntr, Instruction &InstToReplace, unsigned NewIntr, 12081ad6265SDimitry Andric InstCombiner &IC, 12104eeddc0SDimitry Andric std::function<void(SmallVectorImpl<Value *> &, SmallVectorImpl<Type *> &)> 12204eeddc0SDimitry Andric Func) { 12304eeddc0SDimitry Andric SmallVector<Type *, 4> ArgTys; 12481ad6265SDimitry Andric if (!Intrinsic::getIntrinsicSignature(OldIntr.getCalledFunction(), ArgTys)) 125*bdd1243dSDimitry Andric return std::nullopt; 12604eeddc0SDimitry Andric 12781ad6265SDimitry Andric SmallVector<Value *, 8> Args(OldIntr.args()); 12804eeddc0SDimitry Andric 12904eeddc0SDimitry Andric // Modify arguments and types 13004eeddc0SDimitry Andric Func(Args, ArgTys); 13104eeddc0SDimitry Andric 13281ad6265SDimitry Andric Function *I = Intrinsic::getDeclaration(OldIntr.getModule(), NewIntr, ArgTys); 13304eeddc0SDimitry Andric 13404eeddc0SDimitry Andric CallInst *NewCall = IC.Builder.CreateCall(I, Args); 13581ad6265SDimitry Andric NewCall->takeName(&OldIntr); 13681ad6265SDimitry Andric NewCall->copyMetadata(OldIntr); 13704eeddc0SDimitry Andric if (isa<FPMathOperator>(NewCall)) 13881ad6265SDimitry Andric NewCall->copyFastMathFlags(&OldIntr); 13904eeddc0SDimitry Andric 14004eeddc0SDimitry Andric // Erase and replace uses 14181ad6265SDimitry Andric if (!InstToReplace.getType()->isVoidTy()) 14281ad6265SDimitry Andric IC.replaceInstUsesWith(InstToReplace, NewCall); 14381ad6265SDimitry Andric 14481ad6265SDimitry Andric bool RemoveOldIntr = &OldIntr != &InstToReplace; 14581ad6265SDimitry Andric 14681ad6265SDimitry Andric auto RetValue = IC.eraseInstFromFunction(InstToReplace); 14781ad6265SDimitry Andric if (RemoveOldIntr) 14881ad6265SDimitry Andric IC.eraseInstFromFunction(OldIntr); 14981ad6265SDimitry Andric 15081ad6265SDimitry Andric return RetValue; 15104eeddc0SDimitry Andric } 15204eeddc0SDimitry Andric 153*bdd1243dSDimitry Andric static std::optional<Instruction *> 154e8d8bef9SDimitry Andric simplifyAMDGCNImageIntrinsic(const GCNSubtarget *ST, 155e8d8bef9SDimitry Andric const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr, 156e8d8bef9SDimitry Andric IntrinsicInst &II, InstCombiner &IC) { 15704eeddc0SDimitry Andric // Optimize _L to _LZ when _L is zero 15804eeddc0SDimitry Andric if (const auto *LZMappingInfo = 15904eeddc0SDimitry Andric AMDGPU::getMIMGLZMappingInfo(ImageDimIntr->BaseOpcode)) { 16004eeddc0SDimitry Andric if (auto *ConstantLod = 16104eeddc0SDimitry Andric dyn_cast<ConstantFP>(II.getOperand(ImageDimIntr->LodIndex))) { 16204eeddc0SDimitry Andric if (ConstantLod->isZero() || ConstantLod->isNegative()) { 16304eeddc0SDimitry Andric const AMDGPU::ImageDimIntrinsicInfo *NewImageDimIntr = 16404eeddc0SDimitry Andric AMDGPU::getImageDimIntrinsicByBaseOpcode(LZMappingInfo->LZ, 16504eeddc0SDimitry Andric ImageDimIntr->Dim); 16604eeddc0SDimitry Andric return modifyIntrinsicCall( 16781ad6265SDimitry Andric II, II, NewImageDimIntr->Intr, IC, [&](auto &Args, auto &ArgTys) { 16804eeddc0SDimitry Andric Args.erase(Args.begin() + ImageDimIntr->LodIndex); 16904eeddc0SDimitry Andric }); 17004eeddc0SDimitry Andric } 17104eeddc0SDimitry Andric } 17204eeddc0SDimitry Andric } 17304eeddc0SDimitry Andric 17404eeddc0SDimitry Andric // Optimize _mip away, when 'lod' is zero 17504eeddc0SDimitry Andric if (const auto *MIPMappingInfo = 17604eeddc0SDimitry Andric AMDGPU::getMIMGMIPMappingInfo(ImageDimIntr->BaseOpcode)) { 17704eeddc0SDimitry Andric if (auto *ConstantMip = 17804eeddc0SDimitry Andric dyn_cast<ConstantInt>(II.getOperand(ImageDimIntr->MipIndex))) { 17904eeddc0SDimitry Andric if (ConstantMip->isZero()) { 18004eeddc0SDimitry Andric const AMDGPU::ImageDimIntrinsicInfo *NewImageDimIntr = 18104eeddc0SDimitry Andric AMDGPU::getImageDimIntrinsicByBaseOpcode(MIPMappingInfo->NONMIP, 18204eeddc0SDimitry Andric ImageDimIntr->Dim); 18304eeddc0SDimitry Andric return modifyIntrinsicCall( 18481ad6265SDimitry Andric II, II, NewImageDimIntr->Intr, IC, [&](auto &Args, auto &ArgTys) { 18504eeddc0SDimitry Andric Args.erase(Args.begin() + ImageDimIntr->MipIndex); 18604eeddc0SDimitry Andric }); 18704eeddc0SDimitry Andric } 18804eeddc0SDimitry Andric } 18904eeddc0SDimitry Andric } 19004eeddc0SDimitry Andric 19104eeddc0SDimitry Andric // Optimize _bias away when 'bias' is zero 19204eeddc0SDimitry Andric if (const auto *BiasMappingInfo = 19304eeddc0SDimitry Andric AMDGPU::getMIMGBiasMappingInfo(ImageDimIntr->BaseOpcode)) { 19404eeddc0SDimitry Andric if (auto *ConstantBias = 19504eeddc0SDimitry Andric dyn_cast<ConstantFP>(II.getOperand(ImageDimIntr->BiasIndex))) { 19604eeddc0SDimitry Andric if (ConstantBias->isZero()) { 19704eeddc0SDimitry Andric const AMDGPU::ImageDimIntrinsicInfo *NewImageDimIntr = 19804eeddc0SDimitry Andric AMDGPU::getImageDimIntrinsicByBaseOpcode(BiasMappingInfo->NoBias, 19904eeddc0SDimitry Andric ImageDimIntr->Dim); 20004eeddc0SDimitry Andric return modifyIntrinsicCall( 20181ad6265SDimitry Andric II, II, NewImageDimIntr->Intr, IC, [&](auto &Args, auto &ArgTys) { 20204eeddc0SDimitry Andric Args.erase(Args.begin() + ImageDimIntr->BiasIndex); 20304eeddc0SDimitry Andric ArgTys.erase(ArgTys.begin() + ImageDimIntr->BiasTyArg); 20404eeddc0SDimitry Andric }); 20504eeddc0SDimitry Andric } 20604eeddc0SDimitry Andric } 20704eeddc0SDimitry Andric } 20804eeddc0SDimitry Andric 20904eeddc0SDimitry Andric // Optimize _offset away when 'offset' is zero 21004eeddc0SDimitry Andric if (const auto *OffsetMappingInfo = 21104eeddc0SDimitry Andric AMDGPU::getMIMGOffsetMappingInfo(ImageDimIntr->BaseOpcode)) { 21204eeddc0SDimitry Andric if (auto *ConstantOffset = 21304eeddc0SDimitry Andric dyn_cast<ConstantInt>(II.getOperand(ImageDimIntr->OffsetIndex))) { 21404eeddc0SDimitry Andric if (ConstantOffset->isZero()) { 21504eeddc0SDimitry Andric const AMDGPU::ImageDimIntrinsicInfo *NewImageDimIntr = 21604eeddc0SDimitry Andric AMDGPU::getImageDimIntrinsicByBaseOpcode( 21704eeddc0SDimitry Andric OffsetMappingInfo->NoOffset, ImageDimIntr->Dim); 21804eeddc0SDimitry Andric return modifyIntrinsicCall( 21981ad6265SDimitry Andric II, II, NewImageDimIntr->Intr, IC, [&](auto &Args, auto &ArgTys) { 22004eeddc0SDimitry Andric Args.erase(Args.begin() + ImageDimIntr->OffsetIndex); 22104eeddc0SDimitry Andric }); 22204eeddc0SDimitry Andric } 22304eeddc0SDimitry Andric } 22404eeddc0SDimitry Andric } 22504eeddc0SDimitry Andric 22681ad6265SDimitry Andric // Try to use D16 22781ad6265SDimitry Andric if (ST->hasD16Images()) { 22881ad6265SDimitry Andric 22981ad6265SDimitry Andric const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode = 23081ad6265SDimitry Andric AMDGPU::getMIMGBaseOpcodeInfo(ImageDimIntr->BaseOpcode); 23181ad6265SDimitry Andric 23281ad6265SDimitry Andric if (BaseOpcode->HasD16) { 23381ad6265SDimitry Andric 23481ad6265SDimitry Andric // If the only use of image intrinsic is a fptrunc (with conversion to 23581ad6265SDimitry Andric // half) then both fptrunc and image intrinsic will be replaced with image 23681ad6265SDimitry Andric // intrinsic with D16 flag. 23781ad6265SDimitry Andric if (II.hasOneUse()) { 23881ad6265SDimitry Andric Instruction *User = II.user_back(); 23981ad6265SDimitry Andric 24081ad6265SDimitry Andric if (User->getOpcode() == Instruction::FPTrunc && 24181ad6265SDimitry Andric User->getType()->getScalarType()->isHalfTy()) { 24281ad6265SDimitry Andric 24381ad6265SDimitry Andric return modifyIntrinsicCall(II, *User, ImageDimIntr->Intr, IC, 24481ad6265SDimitry Andric [&](auto &Args, auto &ArgTys) { 24581ad6265SDimitry Andric // Change return type of image intrinsic. 24681ad6265SDimitry Andric // Set it to return type of fptrunc. 24781ad6265SDimitry Andric ArgTys[0] = User->getType(); 24881ad6265SDimitry Andric }); 24981ad6265SDimitry Andric } 25081ad6265SDimitry Andric } 25181ad6265SDimitry Andric } 25281ad6265SDimitry Andric } 25381ad6265SDimitry Andric 25404eeddc0SDimitry Andric // Try to use A16 or G16 255e8d8bef9SDimitry Andric if (!ST->hasA16() && !ST->hasG16()) 256*bdd1243dSDimitry Andric return std::nullopt; 257e8d8bef9SDimitry Andric 25804eeddc0SDimitry Andric // Address is interpreted as float if the instruction has a sampler or as 25904eeddc0SDimitry Andric // unsigned int if there is no sampler. 26004eeddc0SDimitry Andric bool HasSampler = 26104eeddc0SDimitry Andric AMDGPU::getMIMGBaseOpcodeInfo(ImageDimIntr->BaseOpcode)->Sampler; 262e8d8bef9SDimitry Andric bool FloatCoord = false; 263e8d8bef9SDimitry Andric // true means derivatives can be converted to 16 bit, coordinates not 264e8d8bef9SDimitry Andric bool OnlyDerivatives = false; 265e8d8bef9SDimitry Andric 266e8d8bef9SDimitry Andric for (unsigned OperandIndex = ImageDimIntr->GradientStart; 267e8d8bef9SDimitry Andric OperandIndex < ImageDimIntr->VAddrEnd; OperandIndex++) { 268e8d8bef9SDimitry Andric Value *Coord = II.getOperand(OperandIndex); 269e8d8bef9SDimitry Andric // If the values are not derived from 16-bit values, we cannot optimize. 27004eeddc0SDimitry Andric if (!canSafelyConvertTo16Bit(*Coord, HasSampler)) { 271e8d8bef9SDimitry Andric if (OperandIndex < ImageDimIntr->CoordStart || 272e8d8bef9SDimitry Andric ImageDimIntr->GradientStart == ImageDimIntr->CoordStart) { 273*bdd1243dSDimitry Andric return std::nullopt; 274e8d8bef9SDimitry Andric } 275e8d8bef9SDimitry Andric // All gradients can be converted, so convert only them 276e8d8bef9SDimitry Andric OnlyDerivatives = true; 277e8d8bef9SDimitry Andric break; 278e8d8bef9SDimitry Andric } 279e8d8bef9SDimitry Andric 280e8d8bef9SDimitry Andric assert(OperandIndex == ImageDimIntr->GradientStart || 281e8d8bef9SDimitry Andric FloatCoord == Coord->getType()->isFloatingPointTy()); 282e8d8bef9SDimitry Andric FloatCoord = Coord->getType()->isFloatingPointTy(); 283e8d8bef9SDimitry Andric } 284e8d8bef9SDimitry Andric 28504eeddc0SDimitry Andric if (!OnlyDerivatives && !ST->hasA16()) 286e8d8bef9SDimitry Andric OnlyDerivatives = true; // Only supports G16 28704eeddc0SDimitry Andric 28804eeddc0SDimitry Andric // Check if there is a bias parameter and if it can be converted to f16 28904eeddc0SDimitry Andric if (!OnlyDerivatives && ImageDimIntr->NumBiasArgs != 0) { 29004eeddc0SDimitry Andric Value *Bias = II.getOperand(ImageDimIntr->BiasIndex); 29104eeddc0SDimitry Andric assert(HasSampler && 29204eeddc0SDimitry Andric "Only image instructions with a sampler can have a bias"); 29304eeddc0SDimitry Andric if (!canSafelyConvertTo16Bit(*Bias, HasSampler)) 29404eeddc0SDimitry Andric OnlyDerivatives = true; 295e8d8bef9SDimitry Andric } 296e8d8bef9SDimitry Andric 29704eeddc0SDimitry Andric if (OnlyDerivatives && (!ST->hasG16() || ImageDimIntr->GradientStart == 29804eeddc0SDimitry Andric ImageDimIntr->CoordStart)) 299*bdd1243dSDimitry Andric return std::nullopt; 30004eeddc0SDimitry Andric 301e8d8bef9SDimitry Andric Type *CoordType = FloatCoord ? Type::getHalfTy(II.getContext()) 302e8d8bef9SDimitry Andric : Type::getInt16Ty(II.getContext()); 303e8d8bef9SDimitry Andric 30404eeddc0SDimitry Andric return modifyIntrinsicCall( 30581ad6265SDimitry Andric II, II, II.getIntrinsicID(), IC, [&](auto &Args, auto &ArgTys) { 306e8d8bef9SDimitry Andric ArgTys[ImageDimIntr->GradientTyArg] = CoordType; 30704eeddc0SDimitry Andric if (!OnlyDerivatives) { 308e8d8bef9SDimitry Andric ArgTys[ImageDimIntr->CoordTyArg] = CoordType; 309e8d8bef9SDimitry Andric 31004eeddc0SDimitry Andric // Change the bias type 31104eeddc0SDimitry Andric if (ImageDimIntr->NumBiasArgs != 0) 31204eeddc0SDimitry Andric ArgTys[ImageDimIntr->BiasTyArg] = Type::getHalfTy(II.getContext()); 31304eeddc0SDimitry Andric } 314e8d8bef9SDimitry Andric 315e8d8bef9SDimitry Andric unsigned EndIndex = 316e8d8bef9SDimitry Andric OnlyDerivatives ? ImageDimIntr->CoordStart : ImageDimIntr->VAddrEnd; 317e8d8bef9SDimitry Andric for (unsigned OperandIndex = ImageDimIntr->GradientStart; 318e8d8bef9SDimitry Andric OperandIndex < EndIndex; OperandIndex++) { 319e8d8bef9SDimitry Andric Args[OperandIndex] = 320e8d8bef9SDimitry Andric convertTo16Bit(*II.getOperand(OperandIndex), IC.Builder); 321e8d8bef9SDimitry Andric } 322e8d8bef9SDimitry Andric 32304eeddc0SDimitry Andric // Convert the bias 32404eeddc0SDimitry Andric if (!OnlyDerivatives && ImageDimIntr->NumBiasArgs != 0) { 32504eeddc0SDimitry Andric Value *Bias = II.getOperand(ImageDimIntr->BiasIndex); 32604eeddc0SDimitry Andric Args[ImageDimIntr->BiasIndex] = convertTo16Bit(*Bias, IC.Builder); 32704eeddc0SDimitry Andric } 32804eeddc0SDimitry Andric }); 329e8d8bef9SDimitry Andric } 330e8d8bef9SDimitry Andric 331e8d8bef9SDimitry Andric bool GCNTTIImpl::canSimplifyLegacyMulToMul(const Value *Op0, const Value *Op1, 332e8d8bef9SDimitry Andric InstCombiner &IC) const { 333e8d8bef9SDimitry Andric // The legacy behaviour is that multiplying +/-0.0 by anything, even NaN or 334e8d8bef9SDimitry Andric // infinity, gives +0.0. If we can prove we don't have one of the special 335e8d8bef9SDimitry Andric // cases then we can use a normal multiply instead. 336e8d8bef9SDimitry Andric // TODO: Create and use isKnownFiniteNonZero instead of just matching 337e8d8bef9SDimitry Andric // constants here. 338e8d8bef9SDimitry Andric if (match(Op0, PatternMatch::m_FiniteNonZero()) || 339e8d8bef9SDimitry Andric match(Op1, PatternMatch::m_FiniteNonZero())) { 340e8d8bef9SDimitry Andric // One operand is not zero or infinity or NaN. 341e8d8bef9SDimitry Andric return true; 342e8d8bef9SDimitry Andric } 343e8d8bef9SDimitry Andric auto *TLI = &IC.getTargetLibraryInfo(); 344e8d8bef9SDimitry Andric if (isKnownNeverInfinity(Op0, TLI) && isKnownNeverNaN(Op0, TLI) && 345e8d8bef9SDimitry Andric isKnownNeverInfinity(Op1, TLI) && isKnownNeverNaN(Op1, TLI)) { 346e8d8bef9SDimitry Andric // Neither operand is infinity or NaN. 347e8d8bef9SDimitry Andric return true; 348e8d8bef9SDimitry Andric } 349e8d8bef9SDimitry Andric return false; 350e8d8bef9SDimitry Andric } 351e8d8bef9SDimitry Andric 352*bdd1243dSDimitry Andric std::optional<Instruction *> 353e8d8bef9SDimitry Andric GCNTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const { 354e8d8bef9SDimitry Andric Intrinsic::ID IID = II.getIntrinsicID(); 355e8d8bef9SDimitry Andric switch (IID) { 356e8d8bef9SDimitry Andric case Intrinsic::amdgcn_rcp: { 357e8d8bef9SDimitry Andric Value *Src = II.getArgOperand(0); 358e8d8bef9SDimitry Andric 359e8d8bef9SDimitry Andric // TODO: Move to ConstantFolding/InstSimplify? 360e8d8bef9SDimitry Andric if (isa<UndefValue>(Src)) { 361e8d8bef9SDimitry Andric Type *Ty = II.getType(); 362e8d8bef9SDimitry Andric auto *QNaN = ConstantFP::get(Ty, APFloat::getQNaN(Ty->getFltSemantics())); 363e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, QNaN); 364e8d8bef9SDimitry Andric } 365e8d8bef9SDimitry Andric 366e8d8bef9SDimitry Andric if (II.isStrictFP()) 367e8d8bef9SDimitry Andric break; 368e8d8bef9SDimitry Andric 369e8d8bef9SDimitry Andric if (const ConstantFP *C = dyn_cast<ConstantFP>(Src)) { 370e8d8bef9SDimitry Andric const APFloat &ArgVal = C->getValueAPF(); 371e8d8bef9SDimitry Andric APFloat Val(ArgVal.getSemantics(), 1); 372e8d8bef9SDimitry Andric Val.divide(ArgVal, APFloat::rmNearestTiesToEven); 373e8d8bef9SDimitry Andric 374e8d8bef9SDimitry Andric // This is more precise than the instruction may give. 375e8d8bef9SDimitry Andric // 376e8d8bef9SDimitry Andric // TODO: The instruction always flushes denormal results (except for f16), 377e8d8bef9SDimitry Andric // should this also? 378e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, ConstantFP::get(II.getContext(), Val)); 379e8d8bef9SDimitry Andric } 380e8d8bef9SDimitry Andric 381e8d8bef9SDimitry Andric break; 382e8d8bef9SDimitry Andric } 383*bdd1243dSDimitry Andric case Intrinsic::amdgcn_sqrt: 384e8d8bef9SDimitry Andric case Intrinsic::amdgcn_rsq: { 385e8d8bef9SDimitry Andric Value *Src = II.getArgOperand(0); 386e8d8bef9SDimitry Andric 387e8d8bef9SDimitry Andric // TODO: Move to ConstantFolding/InstSimplify? 388e8d8bef9SDimitry Andric if (isa<UndefValue>(Src)) { 389e8d8bef9SDimitry Andric Type *Ty = II.getType(); 390e8d8bef9SDimitry Andric auto *QNaN = ConstantFP::get(Ty, APFloat::getQNaN(Ty->getFltSemantics())); 391e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, QNaN); 392e8d8bef9SDimitry Andric } 393e8d8bef9SDimitry Andric 394e8d8bef9SDimitry Andric break; 395e8d8bef9SDimitry Andric } 396e8d8bef9SDimitry Andric case Intrinsic::amdgcn_frexp_mant: 397e8d8bef9SDimitry Andric case Intrinsic::amdgcn_frexp_exp: { 398e8d8bef9SDimitry Andric Value *Src = II.getArgOperand(0); 399e8d8bef9SDimitry Andric if (const ConstantFP *C = dyn_cast<ConstantFP>(Src)) { 400e8d8bef9SDimitry Andric int Exp; 401e8d8bef9SDimitry Andric APFloat Significand = 402e8d8bef9SDimitry Andric frexp(C->getValueAPF(), Exp, APFloat::rmNearestTiesToEven); 403e8d8bef9SDimitry Andric 404e8d8bef9SDimitry Andric if (IID == Intrinsic::amdgcn_frexp_mant) { 405e8d8bef9SDimitry Andric return IC.replaceInstUsesWith( 406e8d8bef9SDimitry Andric II, ConstantFP::get(II.getContext(), Significand)); 407e8d8bef9SDimitry Andric } 408e8d8bef9SDimitry Andric 409e8d8bef9SDimitry Andric // Match instruction special case behavior. 410e8d8bef9SDimitry Andric if (Exp == APFloat::IEK_NaN || Exp == APFloat::IEK_Inf) 411e8d8bef9SDimitry Andric Exp = 0; 412e8d8bef9SDimitry Andric 413e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, ConstantInt::get(II.getType(), Exp)); 414e8d8bef9SDimitry Andric } 415e8d8bef9SDimitry Andric 416e8d8bef9SDimitry Andric if (isa<UndefValue>(Src)) { 417e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, UndefValue::get(II.getType())); 418e8d8bef9SDimitry Andric } 419e8d8bef9SDimitry Andric 420e8d8bef9SDimitry Andric break; 421e8d8bef9SDimitry Andric } 422e8d8bef9SDimitry Andric case Intrinsic::amdgcn_class: { 423e8d8bef9SDimitry Andric Value *Src0 = II.getArgOperand(0); 424e8d8bef9SDimitry Andric Value *Src1 = II.getArgOperand(1); 425e8d8bef9SDimitry Andric const ConstantInt *CMask = dyn_cast<ConstantInt>(Src1); 426e8d8bef9SDimitry Andric if (!CMask) { 427e8d8bef9SDimitry Andric if (isa<UndefValue>(Src0)) { 428e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, UndefValue::get(II.getType())); 429e8d8bef9SDimitry Andric } 430e8d8bef9SDimitry Andric 431e8d8bef9SDimitry Andric if (isa<UndefValue>(Src1)) { 432e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, 433e8d8bef9SDimitry Andric ConstantInt::get(II.getType(), false)); 434e8d8bef9SDimitry Andric } 435e8d8bef9SDimitry Andric break; 436e8d8bef9SDimitry Andric } 437e8d8bef9SDimitry Andric 438e8d8bef9SDimitry Andric uint32_t Mask = CMask->getZExtValue(); 439e8d8bef9SDimitry Andric 440e8d8bef9SDimitry Andric // If all tests are made, it doesn't matter what the value is. 441*bdd1243dSDimitry Andric if ((Mask & fcAllFlags) == fcAllFlags) { 442e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, ConstantInt::get(II.getType(), true)); 443e8d8bef9SDimitry Andric } 444e8d8bef9SDimitry Andric 445*bdd1243dSDimitry Andric if ((Mask & fcAllFlags) == 0) { 446e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, ConstantInt::get(II.getType(), false)); 447e8d8bef9SDimitry Andric } 448e8d8bef9SDimitry Andric 449*bdd1243dSDimitry Andric if (Mask == fcNan && !II.isStrictFP()) { 450e8d8bef9SDimitry Andric // Equivalent of isnan. Replace with standard fcmp. 451e8d8bef9SDimitry Andric Value *FCmp = IC.Builder.CreateFCmpUNO(Src0, Src0); 452e8d8bef9SDimitry Andric FCmp->takeName(&II); 453e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, FCmp); 454e8d8bef9SDimitry Andric } 455e8d8bef9SDimitry Andric 456*bdd1243dSDimitry Andric if (Mask == fcZero && !II.isStrictFP()) { 457e8d8bef9SDimitry Andric // Equivalent of == 0. 458e8d8bef9SDimitry Andric Value *FCmp = 459e8d8bef9SDimitry Andric IC.Builder.CreateFCmpOEQ(Src0, ConstantFP::get(Src0->getType(), 0.0)); 460e8d8bef9SDimitry Andric 461e8d8bef9SDimitry Andric FCmp->takeName(&II); 462e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, FCmp); 463e8d8bef9SDimitry Andric } 464e8d8bef9SDimitry Andric 465e8d8bef9SDimitry Andric // fp_class (nnan x), qnan|snan|other -> fp_class (nnan x), other 466*bdd1243dSDimitry Andric if ((Mask & fcNan) && isKnownNeverNaN(Src0, &IC.getTargetLibraryInfo())) { 467e8d8bef9SDimitry Andric return IC.replaceOperand( 468*bdd1243dSDimitry Andric II, 1, ConstantInt::get(Src1->getType(), Mask & ~fcNan)); 469e8d8bef9SDimitry Andric } 470e8d8bef9SDimitry Andric 471e8d8bef9SDimitry Andric const ConstantFP *CVal = dyn_cast<ConstantFP>(Src0); 472e8d8bef9SDimitry Andric if (!CVal) { 473e8d8bef9SDimitry Andric if (isa<UndefValue>(Src0)) { 474e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, UndefValue::get(II.getType())); 475e8d8bef9SDimitry Andric } 476e8d8bef9SDimitry Andric 477e8d8bef9SDimitry Andric // Clamp mask to used bits 478*bdd1243dSDimitry Andric if ((Mask & fcAllFlags) != Mask) { 479e8d8bef9SDimitry Andric CallInst *NewCall = IC.Builder.CreateCall( 480e8d8bef9SDimitry Andric II.getCalledFunction(), 481*bdd1243dSDimitry Andric {Src0, ConstantInt::get(Src1->getType(), Mask & fcAllFlags)}); 482e8d8bef9SDimitry Andric 483e8d8bef9SDimitry Andric NewCall->takeName(&II); 484e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, NewCall); 485e8d8bef9SDimitry Andric } 486e8d8bef9SDimitry Andric 487e8d8bef9SDimitry Andric break; 488e8d8bef9SDimitry Andric } 489e8d8bef9SDimitry Andric 490e8d8bef9SDimitry Andric const APFloat &Val = CVal->getValueAPF(); 491e8d8bef9SDimitry Andric 492e8d8bef9SDimitry Andric bool Result = 493*bdd1243dSDimitry Andric ((Mask & fcSNan) && Val.isNaN() && Val.isSignaling()) || 494*bdd1243dSDimitry Andric ((Mask & fcQNan) && Val.isNaN() && !Val.isSignaling()) || 495*bdd1243dSDimitry Andric ((Mask & fcNegInf) && Val.isInfinity() && Val.isNegative()) || 496*bdd1243dSDimitry Andric ((Mask & fcNegNormal) && Val.isNormal() && Val.isNegative()) || 497*bdd1243dSDimitry Andric ((Mask & fcNegSubnormal) && Val.isDenormal() && Val.isNegative()) || 498*bdd1243dSDimitry Andric ((Mask & fcNegZero) && Val.isZero() && Val.isNegative()) || 499*bdd1243dSDimitry Andric ((Mask & fcPosZero) && Val.isZero() && !Val.isNegative()) || 500*bdd1243dSDimitry Andric ((Mask & fcPosSubnormal) && Val.isDenormal() && !Val.isNegative()) || 501*bdd1243dSDimitry Andric ((Mask & fcPosNormal) && Val.isNormal() && !Val.isNegative()) || 502*bdd1243dSDimitry Andric ((Mask & fcPosInf) && Val.isInfinity() && !Val.isNegative()); 503e8d8bef9SDimitry Andric 504e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, ConstantInt::get(II.getType(), Result)); 505e8d8bef9SDimitry Andric } 506e8d8bef9SDimitry Andric case Intrinsic::amdgcn_cvt_pkrtz: { 507e8d8bef9SDimitry Andric Value *Src0 = II.getArgOperand(0); 508e8d8bef9SDimitry Andric Value *Src1 = II.getArgOperand(1); 509e8d8bef9SDimitry Andric if (const ConstantFP *C0 = dyn_cast<ConstantFP>(Src0)) { 510e8d8bef9SDimitry Andric if (const ConstantFP *C1 = dyn_cast<ConstantFP>(Src1)) { 511e8d8bef9SDimitry Andric const fltSemantics &HalfSem = 512e8d8bef9SDimitry Andric II.getType()->getScalarType()->getFltSemantics(); 513e8d8bef9SDimitry Andric bool LosesInfo; 514e8d8bef9SDimitry Andric APFloat Val0 = C0->getValueAPF(); 515e8d8bef9SDimitry Andric APFloat Val1 = C1->getValueAPF(); 516e8d8bef9SDimitry Andric Val0.convert(HalfSem, APFloat::rmTowardZero, &LosesInfo); 517e8d8bef9SDimitry Andric Val1.convert(HalfSem, APFloat::rmTowardZero, &LosesInfo); 518e8d8bef9SDimitry Andric 519e8d8bef9SDimitry Andric Constant *Folded = 520e8d8bef9SDimitry Andric ConstantVector::get({ConstantFP::get(II.getContext(), Val0), 521e8d8bef9SDimitry Andric ConstantFP::get(II.getContext(), Val1)}); 522e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, Folded); 523e8d8bef9SDimitry Andric } 524e8d8bef9SDimitry Andric } 525e8d8bef9SDimitry Andric 526e8d8bef9SDimitry Andric if (isa<UndefValue>(Src0) && isa<UndefValue>(Src1)) { 527e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, UndefValue::get(II.getType())); 528e8d8bef9SDimitry Andric } 529e8d8bef9SDimitry Andric 530e8d8bef9SDimitry Andric break; 531e8d8bef9SDimitry Andric } 532e8d8bef9SDimitry Andric case Intrinsic::amdgcn_cvt_pknorm_i16: 533e8d8bef9SDimitry Andric case Intrinsic::amdgcn_cvt_pknorm_u16: 534e8d8bef9SDimitry Andric case Intrinsic::amdgcn_cvt_pk_i16: 535e8d8bef9SDimitry Andric case Intrinsic::amdgcn_cvt_pk_u16: { 536e8d8bef9SDimitry Andric Value *Src0 = II.getArgOperand(0); 537e8d8bef9SDimitry Andric Value *Src1 = II.getArgOperand(1); 538e8d8bef9SDimitry Andric 539e8d8bef9SDimitry Andric if (isa<UndefValue>(Src0) && isa<UndefValue>(Src1)) { 540e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, UndefValue::get(II.getType())); 541e8d8bef9SDimitry Andric } 542e8d8bef9SDimitry Andric 543e8d8bef9SDimitry Andric break; 544e8d8bef9SDimitry Andric } 545e8d8bef9SDimitry Andric case Intrinsic::amdgcn_ubfe: 546e8d8bef9SDimitry Andric case Intrinsic::amdgcn_sbfe: { 547e8d8bef9SDimitry Andric // Decompose simple cases into standard shifts. 548e8d8bef9SDimitry Andric Value *Src = II.getArgOperand(0); 549e8d8bef9SDimitry Andric if (isa<UndefValue>(Src)) { 550e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, Src); 551e8d8bef9SDimitry Andric } 552e8d8bef9SDimitry Andric 553e8d8bef9SDimitry Andric unsigned Width; 554e8d8bef9SDimitry Andric Type *Ty = II.getType(); 555e8d8bef9SDimitry Andric unsigned IntSize = Ty->getIntegerBitWidth(); 556e8d8bef9SDimitry Andric 557e8d8bef9SDimitry Andric ConstantInt *CWidth = dyn_cast<ConstantInt>(II.getArgOperand(2)); 558e8d8bef9SDimitry Andric if (CWidth) { 559e8d8bef9SDimitry Andric Width = CWidth->getZExtValue(); 560e8d8bef9SDimitry Andric if ((Width & (IntSize - 1)) == 0) { 561e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, ConstantInt::getNullValue(Ty)); 562e8d8bef9SDimitry Andric } 563e8d8bef9SDimitry Andric 564e8d8bef9SDimitry Andric // Hardware ignores high bits, so remove those. 565e8d8bef9SDimitry Andric if (Width >= IntSize) { 566e8d8bef9SDimitry Andric return IC.replaceOperand( 567e8d8bef9SDimitry Andric II, 2, ConstantInt::get(CWidth->getType(), Width & (IntSize - 1))); 568e8d8bef9SDimitry Andric } 569e8d8bef9SDimitry Andric } 570e8d8bef9SDimitry Andric 571e8d8bef9SDimitry Andric unsigned Offset; 572e8d8bef9SDimitry Andric ConstantInt *COffset = dyn_cast<ConstantInt>(II.getArgOperand(1)); 573e8d8bef9SDimitry Andric if (COffset) { 574e8d8bef9SDimitry Andric Offset = COffset->getZExtValue(); 575e8d8bef9SDimitry Andric if (Offset >= IntSize) { 576e8d8bef9SDimitry Andric return IC.replaceOperand( 577e8d8bef9SDimitry Andric II, 1, 578e8d8bef9SDimitry Andric ConstantInt::get(COffset->getType(), Offset & (IntSize - 1))); 579e8d8bef9SDimitry Andric } 580e8d8bef9SDimitry Andric } 581e8d8bef9SDimitry Andric 582e8d8bef9SDimitry Andric bool Signed = IID == Intrinsic::amdgcn_sbfe; 583e8d8bef9SDimitry Andric 584e8d8bef9SDimitry Andric if (!CWidth || !COffset) 585e8d8bef9SDimitry Andric break; 586e8d8bef9SDimitry Andric 587349cc55cSDimitry Andric // The case of Width == 0 is handled above, which makes this transformation 588e8d8bef9SDimitry Andric // safe. If Width == 0, then the ashr and lshr instructions become poison 589e8d8bef9SDimitry Andric // value since the shift amount would be equal to the bit size. 590e8d8bef9SDimitry Andric assert(Width != 0); 591e8d8bef9SDimitry Andric 592e8d8bef9SDimitry Andric // TODO: This allows folding to undef when the hardware has specific 593e8d8bef9SDimitry Andric // behavior? 594e8d8bef9SDimitry Andric if (Offset + Width < IntSize) { 595e8d8bef9SDimitry Andric Value *Shl = IC.Builder.CreateShl(Src, IntSize - Offset - Width); 596e8d8bef9SDimitry Andric Value *RightShift = Signed ? IC.Builder.CreateAShr(Shl, IntSize - Width) 597e8d8bef9SDimitry Andric : IC.Builder.CreateLShr(Shl, IntSize - Width); 598e8d8bef9SDimitry Andric RightShift->takeName(&II); 599e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, RightShift); 600e8d8bef9SDimitry Andric } 601e8d8bef9SDimitry Andric 602e8d8bef9SDimitry Andric Value *RightShift = Signed ? IC.Builder.CreateAShr(Src, Offset) 603e8d8bef9SDimitry Andric : IC.Builder.CreateLShr(Src, Offset); 604e8d8bef9SDimitry Andric 605e8d8bef9SDimitry Andric RightShift->takeName(&II); 606e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, RightShift); 607e8d8bef9SDimitry Andric } 608e8d8bef9SDimitry Andric case Intrinsic::amdgcn_exp: 60981ad6265SDimitry Andric case Intrinsic::amdgcn_exp_row: 610e8d8bef9SDimitry Andric case Intrinsic::amdgcn_exp_compr: { 611e8d8bef9SDimitry Andric ConstantInt *En = cast<ConstantInt>(II.getArgOperand(1)); 612e8d8bef9SDimitry Andric unsigned EnBits = En->getZExtValue(); 613e8d8bef9SDimitry Andric if (EnBits == 0xf) 614e8d8bef9SDimitry Andric break; // All inputs enabled. 615e8d8bef9SDimitry Andric 616e8d8bef9SDimitry Andric bool IsCompr = IID == Intrinsic::amdgcn_exp_compr; 617e8d8bef9SDimitry Andric bool Changed = false; 618e8d8bef9SDimitry Andric for (int I = 0; I < (IsCompr ? 2 : 4); ++I) { 619e8d8bef9SDimitry Andric if ((!IsCompr && (EnBits & (1 << I)) == 0) || 620e8d8bef9SDimitry Andric (IsCompr && ((EnBits & (0x3 << (2 * I))) == 0))) { 621e8d8bef9SDimitry Andric Value *Src = II.getArgOperand(I + 2); 622e8d8bef9SDimitry Andric if (!isa<UndefValue>(Src)) { 623e8d8bef9SDimitry Andric IC.replaceOperand(II, I + 2, UndefValue::get(Src->getType())); 624e8d8bef9SDimitry Andric Changed = true; 625e8d8bef9SDimitry Andric } 626e8d8bef9SDimitry Andric } 627e8d8bef9SDimitry Andric } 628e8d8bef9SDimitry Andric 629e8d8bef9SDimitry Andric if (Changed) { 630e8d8bef9SDimitry Andric return &II; 631e8d8bef9SDimitry Andric } 632e8d8bef9SDimitry Andric 633e8d8bef9SDimitry Andric break; 634e8d8bef9SDimitry Andric } 635e8d8bef9SDimitry Andric case Intrinsic::amdgcn_fmed3: { 636e8d8bef9SDimitry Andric // Note this does not preserve proper sNaN behavior if IEEE-mode is enabled 637e8d8bef9SDimitry Andric // for the shader. 638e8d8bef9SDimitry Andric 639e8d8bef9SDimitry Andric Value *Src0 = II.getArgOperand(0); 640e8d8bef9SDimitry Andric Value *Src1 = II.getArgOperand(1); 641e8d8bef9SDimitry Andric Value *Src2 = II.getArgOperand(2); 642e8d8bef9SDimitry Andric 643e8d8bef9SDimitry Andric // Checking for NaN before canonicalization provides better fidelity when 644e8d8bef9SDimitry Andric // mapping other operations onto fmed3 since the order of operands is 645e8d8bef9SDimitry Andric // unchanged. 646e8d8bef9SDimitry Andric CallInst *NewCall = nullptr; 647e8d8bef9SDimitry Andric if (match(Src0, PatternMatch::m_NaN()) || isa<UndefValue>(Src0)) { 648e8d8bef9SDimitry Andric NewCall = IC.Builder.CreateMinNum(Src1, Src2); 649e8d8bef9SDimitry Andric } else if (match(Src1, PatternMatch::m_NaN()) || isa<UndefValue>(Src1)) { 650e8d8bef9SDimitry Andric NewCall = IC.Builder.CreateMinNum(Src0, Src2); 651e8d8bef9SDimitry Andric } else if (match(Src2, PatternMatch::m_NaN()) || isa<UndefValue>(Src2)) { 652e8d8bef9SDimitry Andric NewCall = IC.Builder.CreateMaxNum(Src0, Src1); 653e8d8bef9SDimitry Andric } 654e8d8bef9SDimitry Andric 655e8d8bef9SDimitry Andric if (NewCall) { 656e8d8bef9SDimitry Andric NewCall->copyFastMathFlags(&II); 657e8d8bef9SDimitry Andric NewCall->takeName(&II); 658e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, NewCall); 659e8d8bef9SDimitry Andric } 660e8d8bef9SDimitry Andric 661e8d8bef9SDimitry Andric bool Swap = false; 662e8d8bef9SDimitry Andric // Canonicalize constants to RHS operands. 663e8d8bef9SDimitry Andric // 664e8d8bef9SDimitry Andric // fmed3(c0, x, c1) -> fmed3(x, c0, c1) 665e8d8bef9SDimitry Andric if (isa<Constant>(Src0) && !isa<Constant>(Src1)) { 666e8d8bef9SDimitry Andric std::swap(Src0, Src1); 667e8d8bef9SDimitry Andric Swap = true; 668e8d8bef9SDimitry Andric } 669e8d8bef9SDimitry Andric 670e8d8bef9SDimitry Andric if (isa<Constant>(Src1) && !isa<Constant>(Src2)) { 671e8d8bef9SDimitry Andric std::swap(Src1, Src2); 672e8d8bef9SDimitry Andric Swap = true; 673e8d8bef9SDimitry Andric } 674e8d8bef9SDimitry Andric 675e8d8bef9SDimitry Andric if (isa<Constant>(Src0) && !isa<Constant>(Src1)) { 676e8d8bef9SDimitry Andric std::swap(Src0, Src1); 677e8d8bef9SDimitry Andric Swap = true; 678e8d8bef9SDimitry Andric } 679e8d8bef9SDimitry Andric 680e8d8bef9SDimitry Andric if (Swap) { 681e8d8bef9SDimitry Andric II.setArgOperand(0, Src0); 682e8d8bef9SDimitry Andric II.setArgOperand(1, Src1); 683e8d8bef9SDimitry Andric II.setArgOperand(2, Src2); 684e8d8bef9SDimitry Andric return &II; 685e8d8bef9SDimitry Andric } 686e8d8bef9SDimitry Andric 687e8d8bef9SDimitry Andric if (const ConstantFP *C0 = dyn_cast<ConstantFP>(Src0)) { 688e8d8bef9SDimitry Andric if (const ConstantFP *C1 = dyn_cast<ConstantFP>(Src1)) { 689e8d8bef9SDimitry Andric if (const ConstantFP *C2 = dyn_cast<ConstantFP>(Src2)) { 690e8d8bef9SDimitry Andric APFloat Result = fmed3AMDGCN(C0->getValueAPF(), C1->getValueAPF(), 691e8d8bef9SDimitry Andric C2->getValueAPF()); 692e8d8bef9SDimitry Andric return IC.replaceInstUsesWith( 693e8d8bef9SDimitry Andric II, ConstantFP::get(IC.Builder.getContext(), Result)); 694e8d8bef9SDimitry Andric } 695e8d8bef9SDimitry Andric } 696e8d8bef9SDimitry Andric } 697e8d8bef9SDimitry Andric 698e8d8bef9SDimitry Andric break; 699e8d8bef9SDimitry Andric } 700e8d8bef9SDimitry Andric case Intrinsic::amdgcn_icmp: 701e8d8bef9SDimitry Andric case Intrinsic::amdgcn_fcmp: { 702e8d8bef9SDimitry Andric const ConstantInt *CC = cast<ConstantInt>(II.getArgOperand(2)); 703e8d8bef9SDimitry Andric // Guard against invalid arguments. 704e8d8bef9SDimitry Andric int64_t CCVal = CC->getZExtValue(); 705e8d8bef9SDimitry Andric bool IsInteger = IID == Intrinsic::amdgcn_icmp; 706e8d8bef9SDimitry Andric if ((IsInteger && (CCVal < CmpInst::FIRST_ICMP_PREDICATE || 707e8d8bef9SDimitry Andric CCVal > CmpInst::LAST_ICMP_PREDICATE)) || 708e8d8bef9SDimitry Andric (!IsInteger && (CCVal < CmpInst::FIRST_FCMP_PREDICATE || 709e8d8bef9SDimitry Andric CCVal > CmpInst::LAST_FCMP_PREDICATE))) 710e8d8bef9SDimitry Andric break; 711e8d8bef9SDimitry Andric 712e8d8bef9SDimitry Andric Value *Src0 = II.getArgOperand(0); 713e8d8bef9SDimitry Andric Value *Src1 = II.getArgOperand(1); 714e8d8bef9SDimitry Andric 715e8d8bef9SDimitry Andric if (auto *CSrc0 = dyn_cast<Constant>(Src0)) { 716e8d8bef9SDimitry Andric if (auto *CSrc1 = dyn_cast<Constant>(Src1)) { 717e8d8bef9SDimitry Andric Constant *CCmp = ConstantExpr::getCompare(CCVal, CSrc0, CSrc1); 718e8d8bef9SDimitry Andric if (CCmp->isNullValue()) { 719e8d8bef9SDimitry Andric return IC.replaceInstUsesWith( 720e8d8bef9SDimitry Andric II, ConstantExpr::getSExt(CCmp, II.getType())); 721e8d8bef9SDimitry Andric } 722e8d8bef9SDimitry Andric 723e8d8bef9SDimitry Andric // The result of V_ICMP/V_FCMP assembly instructions (which this 724e8d8bef9SDimitry Andric // intrinsic exposes) is one bit per thread, masked with the EXEC 725e8d8bef9SDimitry Andric // register (which contains the bitmask of live threads). So a 726e8d8bef9SDimitry Andric // comparison that always returns true is the same as a read of the 727e8d8bef9SDimitry Andric // EXEC register. 728e8d8bef9SDimitry Andric Function *NewF = Intrinsic::getDeclaration( 729e8d8bef9SDimitry Andric II.getModule(), Intrinsic::read_register, II.getType()); 730e8d8bef9SDimitry Andric Metadata *MDArgs[] = {MDString::get(II.getContext(), "exec")}; 731e8d8bef9SDimitry Andric MDNode *MD = MDNode::get(II.getContext(), MDArgs); 732e8d8bef9SDimitry Andric Value *Args[] = {MetadataAsValue::get(II.getContext(), MD)}; 733e8d8bef9SDimitry Andric CallInst *NewCall = IC.Builder.CreateCall(NewF, Args); 734349cc55cSDimitry Andric NewCall->addFnAttr(Attribute::Convergent); 735e8d8bef9SDimitry Andric NewCall->takeName(&II); 736e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, NewCall); 737e8d8bef9SDimitry Andric } 738e8d8bef9SDimitry Andric 739e8d8bef9SDimitry Andric // Canonicalize constants to RHS. 740e8d8bef9SDimitry Andric CmpInst::Predicate SwapPred = 741e8d8bef9SDimitry Andric CmpInst::getSwappedPredicate(static_cast<CmpInst::Predicate>(CCVal)); 742e8d8bef9SDimitry Andric II.setArgOperand(0, Src1); 743e8d8bef9SDimitry Andric II.setArgOperand(1, Src0); 744e8d8bef9SDimitry Andric II.setArgOperand( 745e8d8bef9SDimitry Andric 2, ConstantInt::get(CC->getType(), static_cast<int>(SwapPred))); 746e8d8bef9SDimitry Andric return &II; 747e8d8bef9SDimitry Andric } 748e8d8bef9SDimitry Andric 749e8d8bef9SDimitry Andric if (CCVal != CmpInst::ICMP_EQ && CCVal != CmpInst::ICMP_NE) 750e8d8bef9SDimitry Andric break; 751e8d8bef9SDimitry Andric 752e8d8bef9SDimitry Andric // Canonicalize compare eq with true value to compare != 0 753e8d8bef9SDimitry Andric // llvm.amdgcn.icmp(zext (i1 x), 1, eq) 754e8d8bef9SDimitry Andric // -> llvm.amdgcn.icmp(zext (i1 x), 0, ne) 755e8d8bef9SDimitry Andric // llvm.amdgcn.icmp(sext (i1 x), -1, eq) 756e8d8bef9SDimitry Andric // -> llvm.amdgcn.icmp(sext (i1 x), 0, ne) 757e8d8bef9SDimitry Andric Value *ExtSrc; 758e8d8bef9SDimitry Andric if (CCVal == CmpInst::ICMP_EQ && 759e8d8bef9SDimitry Andric ((match(Src1, PatternMatch::m_One()) && 760e8d8bef9SDimitry Andric match(Src0, m_ZExt(PatternMatch::m_Value(ExtSrc)))) || 761e8d8bef9SDimitry Andric (match(Src1, PatternMatch::m_AllOnes()) && 762e8d8bef9SDimitry Andric match(Src0, m_SExt(PatternMatch::m_Value(ExtSrc))))) && 763e8d8bef9SDimitry Andric ExtSrc->getType()->isIntegerTy(1)) { 764e8d8bef9SDimitry Andric IC.replaceOperand(II, 1, ConstantInt::getNullValue(Src1->getType())); 765e8d8bef9SDimitry Andric IC.replaceOperand(II, 2, 766e8d8bef9SDimitry Andric ConstantInt::get(CC->getType(), CmpInst::ICMP_NE)); 767e8d8bef9SDimitry Andric return &II; 768e8d8bef9SDimitry Andric } 769e8d8bef9SDimitry Andric 770e8d8bef9SDimitry Andric CmpInst::Predicate SrcPred; 771e8d8bef9SDimitry Andric Value *SrcLHS; 772e8d8bef9SDimitry Andric Value *SrcRHS; 773e8d8bef9SDimitry Andric 774e8d8bef9SDimitry Andric // Fold compare eq/ne with 0 from a compare result as the predicate to the 775e8d8bef9SDimitry Andric // intrinsic. The typical use is a wave vote function in the library, which 776e8d8bef9SDimitry Andric // will be fed from a user code condition compared with 0. Fold in the 777e8d8bef9SDimitry Andric // redundant compare. 778e8d8bef9SDimitry Andric 779e8d8bef9SDimitry Andric // llvm.amdgcn.icmp([sz]ext ([if]cmp pred a, b), 0, ne) 780e8d8bef9SDimitry Andric // -> llvm.amdgcn.[if]cmp(a, b, pred) 781e8d8bef9SDimitry Andric // 782e8d8bef9SDimitry Andric // llvm.amdgcn.icmp([sz]ext ([if]cmp pred a, b), 0, eq) 783e8d8bef9SDimitry Andric // -> llvm.amdgcn.[if]cmp(a, b, inv pred) 784e8d8bef9SDimitry Andric if (match(Src1, PatternMatch::m_Zero()) && 785e8d8bef9SDimitry Andric match(Src0, PatternMatch::m_ZExtOrSExt( 786e8d8bef9SDimitry Andric m_Cmp(SrcPred, PatternMatch::m_Value(SrcLHS), 787e8d8bef9SDimitry Andric PatternMatch::m_Value(SrcRHS))))) { 788e8d8bef9SDimitry Andric if (CCVal == CmpInst::ICMP_EQ) 789e8d8bef9SDimitry Andric SrcPred = CmpInst::getInversePredicate(SrcPred); 790e8d8bef9SDimitry Andric 791e8d8bef9SDimitry Andric Intrinsic::ID NewIID = CmpInst::isFPPredicate(SrcPred) 792e8d8bef9SDimitry Andric ? Intrinsic::amdgcn_fcmp 793e8d8bef9SDimitry Andric : Intrinsic::amdgcn_icmp; 794e8d8bef9SDimitry Andric 795e8d8bef9SDimitry Andric Type *Ty = SrcLHS->getType(); 796e8d8bef9SDimitry Andric if (auto *CmpType = dyn_cast<IntegerType>(Ty)) { 797e8d8bef9SDimitry Andric // Promote to next legal integer type. 798e8d8bef9SDimitry Andric unsigned Width = CmpType->getBitWidth(); 799e8d8bef9SDimitry Andric unsigned NewWidth = Width; 800e8d8bef9SDimitry Andric 801e8d8bef9SDimitry Andric // Don't do anything for i1 comparisons. 802e8d8bef9SDimitry Andric if (Width == 1) 803e8d8bef9SDimitry Andric break; 804e8d8bef9SDimitry Andric 805e8d8bef9SDimitry Andric if (Width <= 16) 806e8d8bef9SDimitry Andric NewWidth = 16; 807e8d8bef9SDimitry Andric else if (Width <= 32) 808e8d8bef9SDimitry Andric NewWidth = 32; 809e8d8bef9SDimitry Andric else if (Width <= 64) 810e8d8bef9SDimitry Andric NewWidth = 64; 811e8d8bef9SDimitry Andric else if (Width > 64) 812e8d8bef9SDimitry Andric break; // Can't handle this. 813e8d8bef9SDimitry Andric 814e8d8bef9SDimitry Andric if (Width != NewWidth) { 815e8d8bef9SDimitry Andric IntegerType *CmpTy = IC.Builder.getIntNTy(NewWidth); 816e8d8bef9SDimitry Andric if (CmpInst::isSigned(SrcPred)) { 817e8d8bef9SDimitry Andric SrcLHS = IC.Builder.CreateSExt(SrcLHS, CmpTy); 818e8d8bef9SDimitry Andric SrcRHS = IC.Builder.CreateSExt(SrcRHS, CmpTy); 819e8d8bef9SDimitry Andric } else { 820e8d8bef9SDimitry Andric SrcLHS = IC.Builder.CreateZExt(SrcLHS, CmpTy); 821e8d8bef9SDimitry Andric SrcRHS = IC.Builder.CreateZExt(SrcRHS, CmpTy); 822e8d8bef9SDimitry Andric } 823e8d8bef9SDimitry Andric } 824e8d8bef9SDimitry Andric } else if (!Ty->isFloatTy() && !Ty->isDoubleTy() && !Ty->isHalfTy()) 825e8d8bef9SDimitry Andric break; 826e8d8bef9SDimitry Andric 827e8d8bef9SDimitry Andric Function *NewF = Intrinsic::getDeclaration( 828e8d8bef9SDimitry Andric II.getModule(), NewIID, {II.getType(), SrcLHS->getType()}); 829e8d8bef9SDimitry Andric Value *Args[] = {SrcLHS, SrcRHS, 830e8d8bef9SDimitry Andric ConstantInt::get(CC->getType(), SrcPred)}; 831e8d8bef9SDimitry Andric CallInst *NewCall = IC.Builder.CreateCall(NewF, Args); 832e8d8bef9SDimitry Andric NewCall->takeName(&II); 833e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, NewCall); 834e8d8bef9SDimitry Andric } 835e8d8bef9SDimitry Andric 836e8d8bef9SDimitry Andric break; 837e8d8bef9SDimitry Andric } 838e8d8bef9SDimitry Andric case Intrinsic::amdgcn_ballot: { 839e8d8bef9SDimitry Andric if (auto *Src = dyn_cast<ConstantInt>(II.getArgOperand(0))) { 840e8d8bef9SDimitry Andric if (Src->isZero()) { 841e8d8bef9SDimitry Andric // amdgcn.ballot(i1 0) is zero. 842e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, Constant::getNullValue(II.getType())); 843e8d8bef9SDimitry Andric } 844e8d8bef9SDimitry Andric 845e8d8bef9SDimitry Andric if (Src->isOne()) { 846e8d8bef9SDimitry Andric // amdgcn.ballot(i1 1) is exec. 847e8d8bef9SDimitry Andric const char *RegName = "exec"; 848e8d8bef9SDimitry Andric if (II.getType()->isIntegerTy(32)) 849e8d8bef9SDimitry Andric RegName = "exec_lo"; 850e8d8bef9SDimitry Andric else if (!II.getType()->isIntegerTy(64)) 851e8d8bef9SDimitry Andric break; 852e8d8bef9SDimitry Andric 853e8d8bef9SDimitry Andric Function *NewF = Intrinsic::getDeclaration( 854e8d8bef9SDimitry Andric II.getModule(), Intrinsic::read_register, II.getType()); 855e8d8bef9SDimitry Andric Metadata *MDArgs[] = {MDString::get(II.getContext(), RegName)}; 856e8d8bef9SDimitry Andric MDNode *MD = MDNode::get(II.getContext(), MDArgs); 857e8d8bef9SDimitry Andric Value *Args[] = {MetadataAsValue::get(II.getContext(), MD)}; 858e8d8bef9SDimitry Andric CallInst *NewCall = IC.Builder.CreateCall(NewF, Args); 859349cc55cSDimitry Andric NewCall->addFnAttr(Attribute::Convergent); 860e8d8bef9SDimitry Andric NewCall->takeName(&II); 861e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, NewCall); 862e8d8bef9SDimitry Andric } 863e8d8bef9SDimitry Andric } 864e8d8bef9SDimitry Andric break; 865e8d8bef9SDimitry Andric } 866e8d8bef9SDimitry Andric case Intrinsic::amdgcn_wqm_vote: { 867e8d8bef9SDimitry Andric // wqm_vote is identity when the argument is constant. 868e8d8bef9SDimitry Andric if (!isa<Constant>(II.getArgOperand(0))) 869e8d8bef9SDimitry Andric break; 870e8d8bef9SDimitry Andric 871e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, II.getArgOperand(0)); 872e8d8bef9SDimitry Andric } 873e8d8bef9SDimitry Andric case Intrinsic::amdgcn_kill: { 874e8d8bef9SDimitry Andric const ConstantInt *C = dyn_cast<ConstantInt>(II.getArgOperand(0)); 875e8d8bef9SDimitry Andric if (!C || !C->getZExtValue()) 876e8d8bef9SDimitry Andric break; 877e8d8bef9SDimitry Andric 878e8d8bef9SDimitry Andric // amdgcn.kill(i1 1) is a no-op 879e8d8bef9SDimitry Andric return IC.eraseInstFromFunction(II); 880e8d8bef9SDimitry Andric } 881e8d8bef9SDimitry Andric case Intrinsic::amdgcn_update_dpp: { 882e8d8bef9SDimitry Andric Value *Old = II.getArgOperand(0); 883e8d8bef9SDimitry Andric 884e8d8bef9SDimitry Andric auto *BC = cast<ConstantInt>(II.getArgOperand(5)); 885e8d8bef9SDimitry Andric auto *RM = cast<ConstantInt>(II.getArgOperand(3)); 886e8d8bef9SDimitry Andric auto *BM = cast<ConstantInt>(II.getArgOperand(4)); 887e8d8bef9SDimitry Andric if (BC->isZeroValue() || RM->getZExtValue() != 0xF || 888e8d8bef9SDimitry Andric BM->getZExtValue() != 0xF || isa<UndefValue>(Old)) 889e8d8bef9SDimitry Andric break; 890e8d8bef9SDimitry Andric 891e8d8bef9SDimitry Andric // If bound_ctrl = 1, row mask = bank mask = 0xf we can omit old value. 892e8d8bef9SDimitry Andric return IC.replaceOperand(II, 0, UndefValue::get(Old->getType())); 893e8d8bef9SDimitry Andric } 894e8d8bef9SDimitry Andric case Intrinsic::amdgcn_permlane16: 895e8d8bef9SDimitry Andric case Intrinsic::amdgcn_permlanex16: { 896e8d8bef9SDimitry Andric // Discard vdst_in if it's not going to be read. 897e8d8bef9SDimitry Andric Value *VDstIn = II.getArgOperand(0); 898e8d8bef9SDimitry Andric if (isa<UndefValue>(VDstIn)) 899e8d8bef9SDimitry Andric break; 900e8d8bef9SDimitry Andric 901e8d8bef9SDimitry Andric ConstantInt *FetchInvalid = cast<ConstantInt>(II.getArgOperand(4)); 902e8d8bef9SDimitry Andric ConstantInt *BoundCtrl = cast<ConstantInt>(II.getArgOperand(5)); 903e8d8bef9SDimitry Andric if (!FetchInvalid->getZExtValue() && !BoundCtrl->getZExtValue()) 904e8d8bef9SDimitry Andric break; 905e8d8bef9SDimitry Andric 906e8d8bef9SDimitry Andric return IC.replaceOperand(II, 0, UndefValue::get(VDstIn->getType())); 907e8d8bef9SDimitry Andric } 90881ad6265SDimitry Andric case Intrinsic::amdgcn_permlane64: 90981ad6265SDimitry Andric // A constant value is trivially uniform. 91081ad6265SDimitry Andric if (Constant *C = dyn_cast<Constant>(II.getArgOperand(0))) { 91181ad6265SDimitry Andric return IC.replaceInstUsesWith(II, C); 91281ad6265SDimitry Andric } 91381ad6265SDimitry Andric break; 914e8d8bef9SDimitry Andric case Intrinsic::amdgcn_readfirstlane: 915e8d8bef9SDimitry Andric case Intrinsic::amdgcn_readlane: { 916e8d8bef9SDimitry Andric // A constant value is trivially uniform. 917e8d8bef9SDimitry Andric if (Constant *C = dyn_cast<Constant>(II.getArgOperand(0))) { 918e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, C); 919e8d8bef9SDimitry Andric } 920e8d8bef9SDimitry Andric 921e8d8bef9SDimitry Andric // The rest of these may not be safe if the exec may not be the same between 922e8d8bef9SDimitry Andric // the def and use. 923e8d8bef9SDimitry Andric Value *Src = II.getArgOperand(0); 924e8d8bef9SDimitry Andric Instruction *SrcInst = dyn_cast<Instruction>(Src); 925e8d8bef9SDimitry Andric if (SrcInst && SrcInst->getParent() != II.getParent()) 926e8d8bef9SDimitry Andric break; 927e8d8bef9SDimitry Andric 928e8d8bef9SDimitry Andric // readfirstlane (readfirstlane x) -> readfirstlane x 929e8d8bef9SDimitry Andric // readlane (readfirstlane x), y -> readfirstlane x 930e8d8bef9SDimitry Andric if (match(Src, 931e8d8bef9SDimitry Andric PatternMatch::m_Intrinsic<Intrinsic::amdgcn_readfirstlane>())) { 932e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, Src); 933e8d8bef9SDimitry Andric } 934e8d8bef9SDimitry Andric 935e8d8bef9SDimitry Andric if (IID == Intrinsic::amdgcn_readfirstlane) { 936e8d8bef9SDimitry Andric // readfirstlane (readlane x, y) -> readlane x, y 937e8d8bef9SDimitry Andric if (match(Src, PatternMatch::m_Intrinsic<Intrinsic::amdgcn_readlane>())) { 938e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, Src); 939e8d8bef9SDimitry Andric } 940e8d8bef9SDimitry Andric } else { 941e8d8bef9SDimitry Andric // readlane (readlane x, y), y -> readlane x, y 942e8d8bef9SDimitry Andric if (match(Src, PatternMatch::m_Intrinsic<Intrinsic::amdgcn_readlane>( 943e8d8bef9SDimitry Andric PatternMatch::m_Value(), 944e8d8bef9SDimitry Andric PatternMatch::m_Specific(II.getArgOperand(1))))) { 945e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, Src); 946e8d8bef9SDimitry Andric } 947e8d8bef9SDimitry Andric } 948e8d8bef9SDimitry Andric 949e8d8bef9SDimitry Andric break; 950e8d8bef9SDimitry Andric } 951e8d8bef9SDimitry Andric case Intrinsic::amdgcn_ldexp: { 952e8d8bef9SDimitry Andric // FIXME: This doesn't introduce new instructions and belongs in 953e8d8bef9SDimitry Andric // InstructionSimplify. 954e8d8bef9SDimitry Andric Type *Ty = II.getType(); 955e8d8bef9SDimitry Andric Value *Op0 = II.getArgOperand(0); 956e8d8bef9SDimitry Andric Value *Op1 = II.getArgOperand(1); 957e8d8bef9SDimitry Andric 958e8d8bef9SDimitry Andric // Folding undef to qnan is safe regardless of the FP mode. 959e8d8bef9SDimitry Andric if (isa<UndefValue>(Op0)) { 960e8d8bef9SDimitry Andric auto *QNaN = ConstantFP::get(Ty, APFloat::getQNaN(Ty->getFltSemantics())); 961e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, QNaN); 962e8d8bef9SDimitry Andric } 963e8d8bef9SDimitry Andric 964e8d8bef9SDimitry Andric const APFloat *C = nullptr; 965e8d8bef9SDimitry Andric match(Op0, PatternMatch::m_APFloat(C)); 966e8d8bef9SDimitry Andric 967e8d8bef9SDimitry Andric // FIXME: Should flush denorms depending on FP mode, but that's ignored 968e8d8bef9SDimitry Andric // everywhere else. 969e8d8bef9SDimitry Andric // 970e8d8bef9SDimitry Andric // These cases should be safe, even with strictfp. 971e8d8bef9SDimitry Andric // ldexp(0.0, x) -> 0.0 972e8d8bef9SDimitry Andric // ldexp(-0.0, x) -> -0.0 973e8d8bef9SDimitry Andric // ldexp(inf, x) -> inf 974e8d8bef9SDimitry Andric // ldexp(-inf, x) -> -inf 975e8d8bef9SDimitry Andric if (C && (C->isZero() || C->isInfinity())) { 976e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, Op0); 977e8d8bef9SDimitry Andric } 978e8d8bef9SDimitry Andric 979e8d8bef9SDimitry Andric // With strictfp, be more careful about possibly needing to flush denormals 980e8d8bef9SDimitry Andric // or not, and snan behavior depends on ieee_mode. 981e8d8bef9SDimitry Andric if (II.isStrictFP()) 982e8d8bef9SDimitry Andric break; 983e8d8bef9SDimitry Andric 984e8d8bef9SDimitry Andric if (C && C->isNaN()) { 985e8d8bef9SDimitry Andric // FIXME: We just need to make the nan quiet here, but that's unavailable 986e8d8bef9SDimitry Andric // on APFloat, only IEEEfloat 987e8d8bef9SDimitry Andric auto *Quieted = 988e8d8bef9SDimitry Andric ConstantFP::get(Ty, scalbn(*C, 0, APFloat::rmNearestTiesToEven)); 989e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, Quieted); 990e8d8bef9SDimitry Andric } 991e8d8bef9SDimitry Andric 992e8d8bef9SDimitry Andric // ldexp(x, 0) -> x 993e8d8bef9SDimitry Andric // ldexp(x, undef) -> x 994e8d8bef9SDimitry Andric if (isa<UndefValue>(Op1) || match(Op1, PatternMatch::m_ZeroInt())) { 995e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, Op0); 996e8d8bef9SDimitry Andric } 997e8d8bef9SDimitry Andric 998e8d8bef9SDimitry Andric break; 999e8d8bef9SDimitry Andric } 1000e8d8bef9SDimitry Andric case Intrinsic::amdgcn_fmul_legacy: { 1001e8d8bef9SDimitry Andric Value *Op0 = II.getArgOperand(0); 1002e8d8bef9SDimitry Andric Value *Op1 = II.getArgOperand(1); 1003e8d8bef9SDimitry Andric 1004e8d8bef9SDimitry Andric // The legacy behaviour is that multiplying +/-0.0 by anything, even NaN or 1005e8d8bef9SDimitry Andric // infinity, gives +0.0. 1006e8d8bef9SDimitry Andric // TODO: Move to InstSimplify? 1007e8d8bef9SDimitry Andric if (match(Op0, PatternMatch::m_AnyZeroFP()) || 1008e8d8bef9SDimitry Andric match(Op1, PatternMatch::m_AnyZeroFP())) 1009e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, ConstantFP::getNullValue(II.getType())); 1010e8d8bef9SDimitry Andric 1011e8d8bef9SDimitry Andric // If we can prove we don't have one of the special cases then we can use a 1012e8d8bef9SDimitry Andric // normal fmul instruction instead. 1013e8d8bef9SDimitry Andric if (canSimplifyLegacyMulToMul(Op0, Op1, IC)) { 1014e8d8bef9SDimitry Andric auto *FMul = IC.Builder.CreateFMulFMF(Op0, Op1, &II); 1015e8d8bef9SDimitry Andric FMul->takeName(&II); 1016e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, FMul); 1017e8d8bef9SDimitry Andric } 1018e8d8bef9SDimitry Andric break; 1019e8d8bef9SDimitry Andric } 1020e8d8bef9SDimitry Andric case Intrinsic::amdgcn_fma_legacy: { 1021e8d8bef9SDimitry Andric Value *Op0 = II.getArgOperand(0); 1022e8d8bef9SDimitry Andric Value *Op1 = II.getArgOperand(1); 1023e8d8bef9SDimitry Andric Value *Op2 = II.getArgOperand(2); 1024e8d8bef9SDimitry Andric 1025e8d8bef9SDimitry Andric // The legacy behaviour is that multiplying +/-0.0 by anything, even NaN or 1026e8d8bef9SDimitry Andric // infinity, gives +0.0. 1027e8d8bef9SDimitry Andric // TODO: Move to InstSimplify? 1028e8d8bef9SDimitry Andric if (match(Op0, PatternMatch::m_AnyZeroFP()) || 1029e8d8bef9SDimitry Andric match(Op1, PatternMatch::m_AnyZeroFP())) { 1030e8d8bef9SDimitry Andric // It's tempting to just return Op2 here, but that would give the wrong 1031e8d8bef9SDimitry Andric // result if Op2 was -0.0. 1032e8d8bef9SDimitry Andric auto *Zero = ConstantFP::getNullValue(II.getType()); 1033e8d8bef9SDimitry Andric auto *FAdd = IC.Builder.CreateFAddFMF(Zero, Op2, &II); 1034e8d8bef9SDimitry Andric FAdd->takeName(&II); 1035e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, FAdd); 1036e8d8bef9SDimitry Andric } 1037e8d8bef9SDimitry Andric 1038e8d8bef9SDimitry Andric // If we can prove we don't have one of the special cases then we can use a 1039e8d8bef9SDimitry Andric // normal fma instead. 1040e8d8bef9SDimitry Andric if (canSimplifyLegacyMulToMul(Op0, Op1, IC)) { 1041e8d8bef9SDimitry Andric II.setCalledOperand(Intrinsic::getDeclaration( 1042e8d8bef9SDimitry Andric II.getModule(), Intrinsic::fma, II.getType())); 1043e8d8bef9SDimitry Andric return &II; 1044e8d8bef9SDimitry Andric } 1045e8d8bef9SDimitry Andric break; 1046e8d8bef9SDimitry Andric } 10470eae32dcSDimitry Andric case Intrinsic::amdgcn_is_shared: 10480eae32dcSDimitry Andric case Intrinsic::amdgcn_is_private: { 10490eae32dcSDimitry Andric if (isa<UndefValue>(II.getArgOperand(0))) 10500eae32dcSDimitry Andric return IC.replaceInstUsesWith(II, UndefValue::get(II.getType())); 10510eae32dcSDimitry Andric 10520eae32dcSDimitry Andric if (isa<ConstantPointerNull>(II.getArgOperand(0))) 10530eae32dcSDimitry Andric return IC.replaceInstUsesWith(II, ConstantInt::getFalse(II.getType())); 10540eae32dcSDimitry Andric break; 10550eae32dcSDimitry Andric } 1056e8d8bef9SDimitry Andric default: { 1057e8d8bef9SDimitry Andric if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr = 1058e8d8bef9SDimitry Andric AMDGPU::getImageDimIntrinsicInfo(II.getIntrinsicID())) { 1059e8d8bef9SDimitry Andric return simplifyAMDGCNImageIntrinsic(ST, ImageDimIntr, II, IC); 1060e8d8bef9SDimitry Andric } 1061e8d8bef9SDimitry Andric } 1062e8d8bef9SDimitry Andric } 1063*bdd1243dSDimitry Andric return std::nullopt; 1064e8d8bef9SDimitry Andric } 1065e8d8bef9SDimitry Andric 1066e8d8bef9SDimitry Andric /// Implement SimplifyDemandedVectorElts for amdgcn buffer and image intrinsics. 1067e8d8bef9SDimitry Andric /// 1068e8d8bef9SDimitry Andric /// Note: This only supports non-TFE/LWE image intrinsic calls; those have 1069e8d8bef9SDimitry Andric /// struct returns. 1070e8d8bef9SDimitry Andric static Value *simplifyAMDGCNMemoryIntrinsicDemanded(InstCombiner &IC, 1071e8d8bef9SDimitry Andric IntrinsicInst &II, 1072e8d8bef9SDimitry Andric APInt DemandedElts, 1073e8d8bef9SDimitry Andric int DMaskIdx = -1) { 1074e8d8bef9SDimitry Andric 1075e8d8bef9SDimitry Andric auto *IIVTy = cast<FixedVectorType>(II.getType()); 1076e8d8bef9SDimitry Andric unsigned VWidth = IIVTy->getNumElements(); 1077e8d8bef9SDimitry Andric if (VWidth == 1) 1078e8d8bef9SDimitry Andric return nullptr; 1079*bdd1243dSDimitry Andric Type *EltTy = IIVTy->getElementType(); 1080e8d8bef9SDimitry Andric 1081e8d8bef9SDimitry Andric IRBuilderBase::InsertPointGuard Guard(IC.Builder); 1082e8d8bef9SDimitry Andric IC.Builder.SetInsertPoint(&II); 1083e8d8bef9SDimitry Andric 1084e8d8bef9SDimitry Andric // Assume the arguments are unchanged and later override them, if needed. 1085e8d8bef9SDimitry Andric SmallVector<Value *, 16> Args(II.args()); 1086e8d8bef9SDimitry Andric 1087e8d8bef9SDimitry Andric if (DMaskIdx < 0) { 1088e8d8bef9SDimitry Andric // Buffer case. 1089e8d8bef9SDimitry Andric 1090e8d8bef9SDimitry Andric const unsigned ActiveBits = DemandedElts.getActiveBits(); 1091e8d8bef9SDimitry Andric const unsigned UnusedComponentsAtFront = DemandedElts.countTrailingZeros(); 1092e8d8bef9SDimitry Andric 1093e8d8bef9SDimitry Andric // Start assuming the prefix of elements is demanded, but possibly clear 1094e8d8bef9SDimitry Andric // some other bits if there are trailing zeros (unused components at front) 1095e8d8bef9SDimitry Andric // and update offset. 1096e8d8bef9SDimitry Andric DemandedElts = (1 << ActiveBits) - 1; 1097e8d8bef9SDimitry Andric 1098e8d8bef9SDimitry Andric if (UnusedComponentsAtFront > 0) { 1099e8d8bef9SDimitry Andric static const unsigned InvalidOffsetIdx = 0xf; 1100e8d8bef9SDimitry Andric 1101e8d8bef9SDimitry Andric unsigned OffsetIdx; 1102e8d8bef9SDimitry Andric switch (II.getIntrinsicID()) { 1103e8d8bef9SDimitry Andric case Intrinsic::amdgcn_raw_buffer_load: 1104e8d8bef9SDimitry Andric OffsetIdx = 1; 1105e8d8bef9SDimitry Andric break; 1106e8d8bef9SDimitry Andric case Intrinsic::amdgcn_s_buffer_load: 1107e8d8bef9SDimitry Andric // If resulting type is vec3, there is no point in trimming the 1108e8d8bef9SDimitry Andric // load with updated offset, as the vec3 would most likely be widened to 1109e8d8bef9SDimitry Andric // vec4 anyway during lowering. 1110e8d8bef9SDimitry Andric if (ActiveBits == 4 && UnusedComponentsAtFront == 1) 1111e8d8bef9SDimitry Andric OffsetIdx = InvalidOffsetIdx; 1112e8d8bef9SDimitry Andric else 1113e8d8bef9SDimitry Andric OffsetIdx = 1; 1114e8d8bef9SDimitry Andric break; 1115e8d8bef9SDimitry Andric case Intrinsic::amdgcn_struct_buffer_load: 1116e8d8bef9SDimitry Andric OffsetIdx = 2; 1117e8d8bef9SDimitry Andric break; 1118e8d8bef9SDimitry Andric default: 1119e8d8bef9SDimitry Andric // TODO: handle tbuffer* intrinsics. 1120e8d8bef9SDimitry Andric OffsetIdx = InvalidOffsetIdx; 1121e8d8bef9SDimitry Andric break; 1122e8d8bef9SDimitry Andric } 1123e8d8bef9SDimitry Andric 1124e8d8bef9SDimitry Andric if (OffsetIdx != InvalidOffsetIdx) { 1125e8d8bef9SDimitry Andric // Clear demanded bits and update the offset. 1126e8d8bef9SDimitry Andric DemandedElts &= ~((1 << UnusedComponentsAtFront) - 1); 1127*bdd1243dSDimitry Andric auto *Offset = Args[OffsetIdx]; 1128e8d8bef9SDimitry Andric unsigned SingleComponentSizeInBits = 1129*bdd1243dSDimitry Andric IC.getDataLayout().getTypeSizeInBits(EltTy); 1130e8d8bef9SDimitry Andric unsigned OffsetAdd = 1131e8d8bef9SDimitry Andric UnusedComponentsAtFront * SingleComponentSizeInBits / 8; 1132e8d8bef9SDimitry Andric auto *OffsetAddVal = ConstantInt::get(Offset->getType(), OffsetAdd); 1133e8d8bef9SDimitry Andric Args[OffsetIdx] = IC.Builder.CreateAdd(Offset, OffsetAddVal); 1134e8d8bef9SDimitry Andric } 1135e8d8bef9SDimitry Andric } 1136e8d8bef9SDimitry Andric } else { 1137e8d8bef9SDimitry Andric // Image case. 1138e8d8bef9SDimitry Andric 1139*bdd1243dSDimitry Andric ConstantInt *DMask = cast<ConstantInt>(Args[DMaskIdx]); 1140e8d8bef9SDimitry Andric unsigned DMaskVal = DMask->getZExtValue() & 0xf; 1141e8d8bef9SDimitry Andric 1142e8d8bef9SDimitry Andric // Mask off values that are undefined because the dmask doesn't cover them 1143*bdd1243dSDimitry Andric DemandedElts &= (1 << llvm::popcount(DMaskVal)) - 1; 1144e8d8bef9SDimitry Andric 1145e8d8bef9SDimitry Andric unsigned NewDMaskVal = 0; 1146e8d8bef9SDimitry Andric unsigned OrigLoadIdx = 0; 1147e8d8bef9SDimitry Andric for (unsigned SrcIdx = 0; SrcIdx < 4; ++SrcIdx) { 1148e8d8bef9SDimitry Andric const unsigned Bit = 1 << SrcIdx; 1149e8d8bef9SDimitry Andric if (!!(DMaskVal & Bit)) { 1150e8d8bef9SDimitry Andric if (!!DemandedElts[OrigLoadIdx]) 1151e8d8bef9SDimitry Andric NewDMaskVal |= Bit; 1152e8d8bef9SDimitry Andric OrigLoadIdx++; 1153e8d8bef9SDimitry Andric } 1154e8d8bef9SDimitry Andric } 1155e8d8bef9SDimitry Andric 1156e8d8bef9SDimitry Andric if (DMaskVal != NewDMaskVal) 1157e8d8bef9SDimitry Andric Args[DMaskIdx] = ConstantInt::get(DMask->getType(), NewDMaskVal); 1158e8d8bef9SDimitry Andric } 1159e8d8bef9SDimitry Andric 1160e8d8bef9SDimitry Andric unsigned NewNumElts = DemandedElts.countPopulation(); 1161e8d8bef9SDimitry Andric if (!NewNumElts) 1162*bdd1243dSDimitry Andric return UndefValue::get(IIVTy); 1163e8d8bef9SDimitry Andric 1164e8d8bef9SDimitry Andric if (NewNumElts >= VWidth && DemandedElts.isMask()) { 1165e8d8bef9SDimitry Andric if (DMaskIdx >= 0) 1166e8d8bef9SDimitry Andric II.setArgOperand(DMaskIdx, Args[DMaskIdx]); 1167e8d8bef9SDimitry Andric return nullptr; 1168e8d8bef9SDimitry Andric } 1169e8d8bef9SDimitry Andric 1170e8d8bef9SDimitry Andric // Validate function argument and return types, extracting overloaded types 1171e8d8bef9SDimitry Andric // along the way. 1172e8d8bef9SDimitry Andric SmallVector<Type *, 6> OverloadTys; 1173e8d8bef9SDimitry Andric if (!Intrinsic::getIntrinsicSignature(II.getCalledFunction(), OverloadTys)) 1174e8d8bef9SDimitry Andric return nullptr; 1175e8d8bef9SDimitry Andric 1176e8d8bef9SDimitry Andric Type *NewTy = 1177e8d8bef9SDimitry Andric (NewNumElts == 1) ? EltTy : FixedVectorType::get(EltTy, NewNumElts); 1178e8d8bef9SDimitry Andric OverloadTys[0] = NewTy; 1179e8d8bef9SDimitry Andric 1180*bdd1243dSDimitry Andric Function *NewIntrin = Intrinsic::getDeclaration( 1181*bdd1243dSDimitry Andric II.getModule(), II.getIntrinsicID(), OverloadTys); 1182e8d8bef9SDimitry Andric CallInst *NewCall = IC.Builder.CreateCall(NewIntrin, Args); 1183e8d8bef9SDimitry Andric NewCall->takeName(&II); 1184e8d8bef9SDimitry Andric NewCall->copyMetadata(II); 1185e8d8bef9SDimitry Andric 1186e8d8bef9SDimitry Andric if (NewNumElts == 1) { 1187*bdd1243dSDimitry Andric return IC.Builder.CreateInsertElement(UndefValue::get(IIVTy), NewCall, 1188e8d8bef9SDimitry Andric DemandedElts.countTrailingZeros()); 1189e8d8bef9SDimitry Andric } 1190e8d8bef9SDimitry Andric 1191e8d8bef9SDimitry Andric SmallVector<int, 8> EltMask; 1192e8d8bef9SDimitry Andric unsigned NewLoadIdx = 0; 1193e8d8bef9SDimitry Andric for (unsigned OrigLoadIdx = 0; OrigLoadIdx < VWidth; ++OrigLoadIdx) { 1194e8d8bef9SDimitry Andric if (!!DemandedElts[OrigLoadIdx]) 1195e8d8bef9SDimitry Andric EltMask.push_back(NewLoadIdx++); 1196e8d8bef9SDimitry Andric else 1197e8d8bef9SDimitry Andric EltMask.push_back(NewNumElts); 1198e8d8bef9SDimitry Andric } 1199e8d8bef9SDimitry Andric 1200e8d8bef9SDimitry Andric Value *Shuffle = IC.Builder.CreateShuffleVector(NewCall, EltMask); 1201e8d8bef9SDimitry Andric 1202e8d8bef9SDimitry Andric return Shuffle; 1203e8d8bef9SDimitry Andric } 1204e8d8bef9SDimitry Andric 1205*bdd1243dSDimitry Andric std::optional<Value *> GCNTTIImpl::simplifyDemandedVectorEltsIntrinsic( 1206e8d8bef9SDimitry Andric InstCombiner &IC, IntrinsicInst &II, APInt DemandedElts, APInt &UndefElts, 1207e8d8bef9SDimitry Andric APInt &UndefElts2, APInt &UndefElts3, 1208e8d8bef9SDimitry Andric std::function<void(Instruction *, unsigned, APInt, APInt &)> 1209e8d8bef9SDimitry Andric SimplifyAndSetOp) const { 1210e8d8bef9SDimitry Andric switch (II.getIntrinsicID()) { 1211e8d8bef9SDimitry Andric case Intrinsic::amdgcn_buffer_load: 1212e8d8bef9SDimitry Andric case Intrinsic::amdgcn_buffer_load_format: 1213e8d8bef9SDimitry Andric case Intrinsic::amdgcn_raw_buffer_load: 1214e8d8bef9SDimitry Andric case Intrinsic::amdgcn_raw_buffer_load_format: 1215e8d8bef9SDimitry Andric case Intrinsic::amdgcn_raw_tbuffer_load: 1216e8d8bef9SDimitry Andric case Intrinsic::amdgcn_s_buffer_load: 1217e8d8bef9SDimitry Andric case Intrinsic::amdgcn_struct_buffer_load: 1218e8d8bef9SDimitry Andric case Intrinsic::amdgcn_struct_buffer_load_format: 1219e8d8bef9SDimitry Andric case Intrinsic::amdgcn_struct_tbuffer_load: 1220e8d8bef9SDimitry Andric case Intrinsic::amdgcn_tbuffer_load: 1221e8d8bef9SDimitry Andric return simplifyAMDGCNMemoryIntrinsicDemanded(IC, II, DemandedElts); 1222e8d8bef9SDimitry Andric default: { 1223e8d8bef9SDimitry Andric if (getAMDGPUImageDMaskIntrinsic(II.getIntrinsicID())) { 1224e8d8bef9SDimitry Andric return simplifyAMDGCNMemoryIntrinsicDemanded(IC, II, DemandedElts, 0); 1225e8d8bef9SDimitry Andric } 1226e8d8bef9SDimitry Andric break; 1227e8d8bef9SDimitry Andric } 1228e8d8bef9SDimitry Andric } 1229*bdd1243dSDimitry Andric return std::nullopt; 1230e8d8bef9SDimitry Andric } 1231