xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
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"
20e8d8bef9SDimitry Andric #include "llvm/IR/IntrinsicsAMDGPU.h"
21e8d8bef9SDimitry Andric #include "llvm/Transforms/InstCombine/InstCombiner.h"
22e8d8bef9SDimitry Andric 
23e8d8bef9SDimitry Andric using namespace llvm;
24e8d8bef9SDimitry Andric 
25e8d8bef9SDimitry Andric #define DEBUG_TYPE "AMDGPUtti"
26e8d8bef9SDimitry Andric 
27e8d8bef9SDimitry Andric namespace {
28e8d8bef9SDimitry Andric 
29e8d8bef9SDimitry Andric struct AMDGPUImageDMaskIntrinsic {
30e8d8bef9SDimitry Andric   unsigned Intr;
31e8d8bef9SDimitry Andric };
32e8d8bef9SDimitry Andric 
33e8d8bef9SDimitry Andric #define GET_AMDGPUImageDMaskIntrinsicTable_IMPL
34e8d8bef9SDimitry Andric #include "InstCombineTables.inc"
35e8d8bef9SDimitry Andric 
36e8d8bef9SDimitry Andric } // end anonymous namespace
37e8d8bef9SDimitry Andric 
38e8d8bef9SDimitry Andric // Constant fold llvm.amdgcn.fmed3 intrinsics for standard inputs.
39e8d8bef9SDimitry Andric //
40e8d8bef9SDimitry Andric // A single NaN input is folded to minnum, so we rely on that folding for
41e8d8bef9SDimitry Andric // handling NaNs.
42e8d8bef9SDimitry Andric static APFloat fmed3AMDGCN(const APFloat &Src0, const APFloat &Src1,
43e8d8bef9SDimitry Andric                            const APFloat &Src2) {
44e8d8bef9SDimitry Andric   APFloat Max3 = maxnum(maxnum(Src0, Src1), Src2);
45e8d8bef9SDimitry Andric 
46e8d8bef9SDimitry Andric   APFloat::cmpResult Cmp0 = Max3.compare(Src0);
47e8d8bef9SDimitry Andric   assert(Cmp0 != APFloat::cmpUnordered && "nans handled separately");
48e8d8bef9SDimitry Andric   if (Cmp0 == APFloat::cmpEqual)
49e8d8bef9SDimitry Andric     return maxnum(Src1, Src2);
50e8d8bef9SDimitry Andric 
51e8d8bef9SDimitry Andric   APFloat::cmpResult Cmp1 = Max3.compare(Src1);
52e8d8bef9SDimitry Andric   assert(Cmp1 != APFloat::cmpUnordered && "nans handled separately");
53e8d8bef9SDimitry Andric   if (Cmp1 == APFloat::cmpEqual)
54e8d8bef9SDimitry Andric     return maxnum(Src0, Src2);
55e8d8bef9SDimitry Andric 
56e8d8bef9SDimitry Andric   return maxnum(Src0, Src1);
57e8d8bef9SDimitry Andric }
58e8d8bef9SDimitry Andric 
59e8d8bef9SDimitry Andric // Check if a value can be converted to a 16-bit value without losing
60e8d8bef9SDimitry Andric // precision.
61e8d8bef9SDimitry Andric static bool canSafelyConvertTo16Bit(Value &V) {
62e8d8bef9SDimitry Andric   Type *VTy = V.getType();
63e8d8bef9SDimitry Andric   if (VTy->isHalfTy() || VTy->isIntegerTy(16)) {
64e8d8bef9SDimitry Andric     // The value is already 16-bit, so we don't want to convert to 16-bit again!
65e8d8bef9SDimitry Andric     return false;
66e8d8bef9SDimitry Andric   }
67e8d8bef9SDimitry Andric   if (ConstantFP *ConstFloat = dyn_cast<ConstantFP>(&V)) {
68e8d8bef9SDimitry Andric     // We need to check that if we cast the index down to a half, we do not lose
69e8d8bef9SDimitry Andric     // precision.
70e8d8bef9SDimitry Andric     APFloat FloatValue(ConstFloat->getValueAPF());
71e8d8bef9SDimitry Andric     bool LosesInfo = true;
72e8d8bef9SDimitry Andric     FloatValue.convert(APFloat::IEEEhalf(), APFloat::rmTowardZero, &LosesInfo);
73e8d8bef9SDimitry Andric     return !LosesInfo;
74e8d8bef9SDimitry Andric   }
75e8d8bef9SDimitry Andric   Value *CastSrc;
76e8d8bef9SDimitry Andric   if (match(&V, m_FPExt(PatternMatch::m_Value(CastSrc))) ||
77e8d8bef9SDimitry Andric       match(&V, m_SExt(PatternMatch::m_Value(CastSrc))) ||
78e8d8bef9SDimitry Andric       match(&V, m_ZExt(PatternMatch::m_Value(CastSrc)))) {
79e8d8bef9SDimitry Andric     Type *CastSrcTy = CastSrc->getType();
80e8d8bef9SDimitry Andric     if (CastSrcTy->isHalfTy() || CastSrcTy->isIntegerTy(16))
81e8d8bef9SDimitry Andric       return true;
82e8d8bef9SDimitry Andric   }
83e8d8bef9SDimitry Andric 
84e8d8bef9SDimitry Andric   return false;
85e8d8bef9SDimitry Andric }
86e8d8bef9SDimitry Andric 
87e8d8bef9SDimitry Andric // Convert a value to 16-bit.
88e8d8bef9SDimitry Andric static Value *convertTo16Bit(Value &V, InstCombiner::BuilderTy &Builder) {
89e8d8bef9SDimitry Andric   Type *VTy = V.getType();
90e8d8bef9SDimitry Andric   if (isa<FPExtInst>(&V) || isa<SExtInst>(&V) || isa<ZExtInst>(&V))
91e8d8bef9SDimitry Andric     return cast<Instruction>(&V)->getOperand(0);
92e8d8bef9SDimitry Andric   if (VTy->isIntegerTy())
93e8d8bef9SDimitry Andric     return Builder.CreateIntCast(&V, Type::getInt16Ty(V.getContext()), false);
94e8d8bef9SDimitry Andric   if (VTy->isFloatingPointTy())
95e8d8bef9SDimitry Andric     return Builder.CreateFPCast(&V, Type::getHalfTy(V.getContext()));
96e8d8bef9SDimitry Andric 
97e8d8bef9SDimitry Andric   llvm_unreachable("Should never be called!");
98e8d8bef9SDimitry Andric }
99e8d8bef9SDimitry Andric 
100e8d8bef9SDimitry Andric static Optional<Instruction *>
101e8d8bef9SDimitry Andric simplifyAMDGCNImageIntrinsic(const GCNSubtarget *ST,
102e8d8bef9SDimitry Andric                              const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr,
103e8d8bef9SDimitry Andric                              IntrinsicInst &II, InstCombiner &IC) {
104e8d8bef9SDimitry Andric   if (!ST->hasA16() && !ST->hasG16())
105e8d8bef9SDimitry Andric     return None;
106e8d8bef9SDimitry Andric 
107e8d8bef9SDimitry Andric   bool FloatCoord = false;
108e8d8bef9SDimitry Andric   // true means derivatives can be converted to 16 bit, coordinates not
109e8d8bef9SDimitry Andric   bool OnlyDerivatives = false;
110e8d8bef9SDimitry Andric 
111e8d8bef9SDimitry Andric   for (unsigned OperandIndex = ImageDimIntr->GradientStart;
112e8d8bef9SDimitry Andric        OperandIndex < ImageDimIntr->VAddrEnd; OperandIndex++) {
113e8d8bef9SDimitry Andric     Value *Coord = II.getOperand(OperandIndex);
114e8d8bef9SDimitry Andric     // If the values are not derived from 16-bit values, we cannot optimize.
115e8d8bef9SDimitry Andric     if (!canSafelyConvertTo16Bit(*Coord)) {
116e8d8bef9SDimitry Andric       if (OperandIndex < ImageDimIntr->CoordStart ||
117e8d8bef9SDimitry Andric           ImageDimIntr->GradientStart == ImageDimIntr->CoordStart) {
118e8d8bef9SDimitry Andric         return None;
119e8d8bef9SDimitry Andric       }
120e8d8bef9SDimitry Andric       // All gradients can be converted, so convert only them
121e8d8bef9SDimitry Andric       OnlyDerivatives = true;
122e8d8bef9SDimitry Andric       break;
123e8d8bef9SDimitry Andric     }
124e8d8bef9SDimitry Andric 
125e8d8bef9SDimitry Andric     assert(OperandIndex == ImageDimIntr->GradientStart ||
126e8d8bef9SDimitry Andric            FloatCoord == Coord->getType()->isFloatingPointTy());
127e8d8bef9SDimitry Andric     FloatCoord = Coord->getType()->isFloatingPointTy();
128e8d8bef9SDimitry Andric   }
129e8d8bef9SDimitry Andric 
130e8d8bef9SDimitry Andric   if (OnlyDerivatives) {
131e8d8bef9SDimitry Andric     if (!ST->hasG16())
132e8d8bef9SDimitry Andric       return None;
133e8d8bef9SDimitry Andric   } else {
134e8d8bef9SDimitry Andric     if (!ST->hasA16())
135e8d8bef9SDimitry Andric       OnlyDerivatives = true; // Only supports G16
136e8d8bef9SDimitry Andric   }
137e8d8bef9SDimitry Andric 
138e8d8bef9SDimitry Andric   Type *CoordType = FloatCoord ? Type::getHalfTy(II.getContext())
139e8d8bef9SDimitry Andric                                : Type::getInt16Ty(II.getContext());
140e8d8bef9SDimitry Andric 
141e8d8bef9SDimitry Andric   SmallVector<Type *, 4> ArgTys;
142e8d8bef9SDimitry Andric   if (!Intrinsic::getIntrinsicSignature(II.getCalledFunction(), ArgTys))
143e8d8bef9SDimitry Andric     return None;
144e8d8bef9SDimitry Andric 
145e8d8bef9SDimitry Andric   ArgTys[ImageDimIntr->GradientTyArg] = CoordType;
146e8d8bef9SDimitry Andric   if (!OnlyDerivatives)
147e8d8bef9SDimitry Andric     ArgTys[ImageDimIntr->CoordTyArg] = CoordType;
148e8d8bef9SDimitry Andric   Function *I =
149e8d8bef9SDimitry Andric       Intrinsic::getDeclaration(II.getModule(), II.getIntrinsicID(), ArgTys);
150e8d8bef9SDimitry Andric 
151349cc55cSDimitry Andric   SmallVector<Value *, 8> Args(II.args());
152e8d8bef9SDimitry Andric 
153e8d8bef9SDimitry Andric   unsigned EndIndex =
154e8d8bef9SDimitry Andric       OnlyDerivatives ? ImageDimIntr->CoordStart : ImageDimIntr->VAddrEnd;
155e8d8bef9SDimitry Andric   for (unsigned OperandIndex = ImageDimIntr->GradientStart;
156e8d8bef9SDimitry Andric        OperandIndex < EndIndex; OperandIndex++) {
157e8d8bef9SDimitry Andric     Args[OperandIndex] =
158e8d8bef9SDimitry Andric         convertTo16Bit(*II.getOperand(OperandIndex), IC.Builder);
159e8d8bef9SDimitry Andric   }
160e8d8bef9SDimitry Andric 
161e8d8bef9SDimitry Andric   CallInst *NewCall = IC.Builder.CreateCall(I, Args);
162e8d8bef9SDimitry Andric   NewCall->takeName(&II);
163e8d8bef9SDimitry Andric   NewCall->copyMetadata(II);
164e8d8bef9SDimitry Andric   if (isa<FPMathOperator>(NewCall))
165e8d8bef9SDimitry Andric     NewCall->copyFastMathFlags(&II);
166e8d8bef9SDimitry Andric   return IC.replaceInstUsesWith(II, NewCall);
167e8d8bef9SDimitry Andric }
168e8d8bef9SDimitry Andric 
169e8d8bef9SDimitry Andric bool GCNTTIImpl::canSimplifyLegacyMulToMul(const Value *Op0, const Value *Op1,
170e8d8bef9SDimitry Andric                                            InstCombiner &IC) const {
171e8d8bef9SDimitry Andric   // The legacy behaviour is that multiplying +/-0.0 by anything, even NaN or
172e8d8bef9SDimitry Andric   // infinity, gives +0.0. If we can prove we don't have one of the special
173e8d8bef9SDimitry Andric   // cases then we can use a normal multiply instead.
174e8d8bef9SDimitry Andric   // TODO: Create and use isKnownFiniteNonZero instead of just matching
175e8d8bef9SDimitry Andric   // constants here.
176e8d8bef9SDimitry Andric   if (match(Op0, PatternMatch::m_FiniteNonZero()) ||
177e8d8bef9SDimitry Andric       match(Op1, PatternMatch::m_FiniteNonZero())) {
178e8d8bef9SDimitry Andric     // One operand is not zero or infinity or NaN.
179e8d8bef9SDimitry Andric     return true;
180e8d8bef9SDimitry Andric   }
181e8d8bef9SDimitry Andric   auto *TLI = &IC.getTargetLibraryInfo();
182e8d8bef9SDimitry Andric   if (isKnownNeverInfinity(Op0, TLI) && isKnownNeverNaN(Op0, TLI) &&
183e8d8bef9SDimitry Andric       isKnownNeverInfinity(Op1, TLI) && isKnownNeverNaN(Op1, TLI)) {
184e8d8bef9SDimitry Andric     // Neither operand is infinity or NaN.
185e8d8bef9SDimitry Andric     return true;
186e8d8bef9SDimitry Andric   }
187e8d8bef9SDimitry Andric   return false;
188e8d8bef9SDimitry Andric }
189e8d8bef9SDimitry Andric 
190e8d8bef9SDimitry Andric Optional<Instruction *>
191e8d8bef9SDimitry Andric GCNTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
192e8d8bef9SDimitry Andric   Intrinsic::ID IID = II.getIntrinsicID();
193e8d8bef9SDimitry Andric   switch (IID) {
194e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_rcp: {
195e8d8bef9SDimitry Andric     Value *Src = II.getArgOperand(0);
196e8d8bef9SDimitry Andric 
197e8d8bef9SDimitry Andric     // TODO: Move to ConstantFolding/InstSimplify?
198e8d8bef9SDimitry Andric     if (isa<UndefValue>(Src)) {
199e8d8bef9SDimitry Andric       Type *Ty = II.getType();
200e8d8bef9SDimitry Andric       auto *QNaN = ConstantFP::get(Ty, APFloat::getQNaN(Ty->getFltSemantics()));
201e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, QNaN);
202e8d8bef9SDimitry Andric     }
203e8d8bef9SDimitry Andric 
204e8d8bef9SDimitry Andric     if (II.isStrictFP())
205e8d8bef9SDimitry Andric       break;
206e8d8bef9SDimitry Andric 
207e8d8bef9SDimitry Andric     if (const ConstantFP *C = dyn_cast<ConstantFP>(Src)) {
208e8d8bef9SDimitry Andric       const APFloat &ArgVal = C->getValueAPF();
209e8d8bef9SDimitry Andric       APFloat Val(ArgVal.getSemantics(), 1);
210e8d8bef9SDimitry Andric       Val.divide(ArgVal, APFloat::rmNearestTiesToEven);
211e8d8bef9SDimitry Andric 
212e8d8bef9SDimitry Andric       // This is more precise than the instruction may give.
213e8d8bef9SDimitry Andric       //
214e8d8bef9SDimitry Andric       // TODO: The instruction always flushes denormal results (except for f16),
215e8d8bef9SDimitry Andric       // should this also?
216e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, ConstantFP::get(II.getContext(), Val));
217e8d8bef9SDimitry Andric     }
218e8d8bef9SDimitry Andric 
219e8d8bef9SDimitry Andric     break;
220e8d8bef9SDimitry Andric   }
221e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_rsq: {
222e8d8bef9SDimitry Andric     Value *Src = II.getArgOperand(0);
223e8d8bef9SDimitry Andric 
224e8d8bef9SDimitry Andric     // TODO: Move to ConstantFolding/InstSimplify?
225e8d8bef9SDimitry Andric     if (isa<UndefValue>(Src)) {
226e8d8bef9SDimitry Andric       Type *Ty = II.getType();
227e8d8bef9SDimitry Andric       auto *QNaN = ConstantFP::get(Ty, APFloat::getQNaN(Ty->getFltSemantics()));
228e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, QNaN);
229e8d8bef9SDimitry Andric     }
230e8d8bef9SDimitry Andric 
231e8d8bef9SDimitry Andric     break;
232e8d8bef9SDimitry Andric   }
233e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_frexp_mant:
234e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_frexp_exp: {
235e8d8bef9SDimitry Andric     Value *Src = II.getArgOperand(0);
236e8d8bef9SDimitry Andric     if (const ConstantFP *C = dyn_cast<ConstantFP>(Src)) {
237e8d8bef9SDimitry Andric       int Exp;
238e8d8bef9SDimitry Andric       APFloat Significand =
239e8d8bef9SDimitry Andric           frexp(C->getValueAPF(), Exp, APFloat::rmNearestTiesToEven);
240e8d8bef9SDimitry Andric 
241e8d8bef9SDimitry Andric       if (IID == Intrinsic::amdgcn_frexp_mant) {
242e8d8bef9SDimitry Andric         return IC.replaceInstUsesWith(
243e8d8bef9SDimitry Andric             II, ConstantFP::get(II.getContext(), Significand));
244e8d8bef9SDimitry Andric       }
245e8d8bef9SDimitry Andric 
246e8d8bef9SDimitry Andric       // Match instruction special case behavior.
247e8d8bef9SDimitry Andric       if (Exp == APFloat::IEK_NaN || Exp == APFloat::IEK_Inf)
248e8d8bef9SDimitry Andric         Exp = 0;
249e8d8bef9SDimitry Andric 
250e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, ConstantInt::get(II.getType(), Exp));
251e8d8bef9SDimitry Andric     }
252e8d8bef9SDimitry Andric 
253e8d8bef9SDimitry Andric     if (isa<UndefValue>(Src)) {
254e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, UndefValue::get(II.getType()));
255e8d8bef9SDimitry Andric     }
256e8d8bef9SDimitry Andric 
257e8d8bef9SDimitry Andric     break;
258e8d8bef9SDimitry Andric   }
259e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_class: {
260e8d8bef9SDimitry Andric     enum {
261e8d8bef9SDimitry Andric       S_NAN = 1 << 0,       // Signaling NaN
262e8d8bef9SDimitry Andric       Q_NAN = 1 << 1,       // Quiet NaN
263e8d8bef9SDimitry Andric       N_INFINITY = 1 << 2,  // Negative infinity
264e8d8bef9SDimitry Andric       N_NORMAL = 1 << 3,    // Negative normal
265e8d8bef9SDimitry Andric       N_SUBNORMAL = 1 << 4, // Negative subnormal
266e8d8bef9SDimitry Andric       N_ZERO = 1 << 5,      // Negative zero
267e8d8bef9SDimitry Andric       P_ZERO = 1 << 6,      // Positive zero
268e8d8bef9SDimitry Andric       P_SUBNORMAL = 1 << 7, // Positive subnormal
269e8d8bef9SDimitry Andric       P_NORMAL = 1 << 8,    // Positive normal
270e8d8bef9SDimitry Andric       P_INFINITY = 1 << 9   // Positive infinity
271e8d8bef9SDimitry Andric     };
272e8d8bef9SDimitry Andric 
273e8d8bef9SDimitry Andric     const uint32_t FullMask = S_NAN | Q_NAN | N_INFINITY | N_NORMAL |
274e8d8bef9SDimitry Andric                               N_SUBNORMAL | N_ZERO | P_ZERO | P_SUBNORMAL |
275e8d8bef9SDimitry Andric                               P_NORMAL | P_INFINITY;
276e8d8bef9SDimitry Andric 
277e8d8bef9SDimitry Andric     Value *Src0 = II.getArgOperand(0);
278e8d8bef9SDimitry Andric     Value *Src1 = II.getArgOperand(1);
279e8d8bef9SDimitry Andric     const ConstantInt *CMask = dyn_cast<ConstantInt>(Src1);
280e8d8bef9SDimitry Andric     if (!CMask) {
281e8d8bef9SDimitry Andric       if (isa<UndefValue>(Src0)) {
282e8d8bef9SDimitry Andric         return IC.replaceInstUsesWith(II, UndefValue::get(II.getType()));
283e8d8bef9SDimitry Andric       }
284e8d8bef9SDimitry Andric 
285e8d8bef9SDimitry Andric       if (isa<UndefValue>(Src1)) {
286e8d8bef9SDimitry Andric         return IC.replaceInstUsesWith(II,
287e8d8bef9SDimitry Andric                                       ConstantInt::get(II.getType(), false));
288e8d8bef9SDimitry Andric       }
289e8d8bef9SDimitry Andric       break;
290e8d8bef9SDimitry Andric     }
291e8d8bef9SDimitry Andric 
292e8d8bef9SDimitry Andric     uint32_t Mask = CMask->getZExtValue();
293e8d8bef9SDimitry Andric 
294e8d8bef9SDimitry Andric     // If all tests are made, it doesn't matter what the value is.
295e8d8bef9SDimitry Andric     if ((Mask & FullMask) == FullMask) {
296e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, ConstantInt::get(II.getType(), true));
297e8d8bef9SDimitry Andric     }
298e8d8bef9SDimitry Andric 
299e8d8bef9SDimitry Andric     if ((Mask & FullMask) == 0) {
300e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, ConstantInt::get(II.getType(), false));
301e8d8bef9SDimitry Andric     }
302e8d8bef9SDimitry Andric 
303e8d8bef9SDimitry Andric     if (Mask == (S_NAN | Q_NAN)) {
304e8d8bef9SDimitry Andric       // Equivalent of isnan. Replace with standard fcmp.
305e8d8bef9SDimitry Andric       Value *FCmp = IC.Builder.CreateFCmpUNO(Src0, Src0);
306e8d8bef9SDimitry Andric       FCmp->takeName(&II);
307e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, FCmp);
308e8d8bef9SDimitry Andric     }
309e8d8bef9SDimitry Andric 
310e8d8bef9SDimitry Andric     if (Mask == (N_ZERO | P_ZERO)) {
311e8d8bef9SDimitry Andric       // Equivalent of == 0.
312e8d8bef9SDimitry Andric       Value *FCmp =
313e8d8bef9SDimitry Andric           IC.Builder.CreateFCmpOEQ(Src0, ConstantFP::get(Src0->getType(), 0.0));
314e8d8bef9SDimitry Andric 
315e8d8bef9SDimitry Andric       FCmp->takeName(&II);
316e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, FCmp);
317e8d8bef9SDimitry Andric     }
318e8d8bef9SDimitry Andric 
319e8d8bef9SDimitry Andric     // fp_class (nnan x), qnan|snan|other -> fp_class (nnan x), other
320e8d8bef9SDimitry Andric     if (((Mask & S_NAN) || (Mask & Q_NAN)) &&
321e8d8bef9SDimitry Andric         isKnownNeverNaN(Src0, &IC.getTargetLibraryInfo())) {
322e8d8bef9SDimitry Andric       return IC.replaceOperand(
323e8d8bef9SDimitry Andric           II, 1, ConstantInt::get(Src1->getType(), Mask & ~(S_NAN | Q_NAN)));
324e8d8bef9SDimitry Andric     }
325e8d8bef9SDimitry Andric 
326e8d8bef9SDimitry Andric     const ConstantFP *CVal = dyn_cast<ConstantFP>(Src0);
327e8d8bef9SDimitry Andric     if (!CVal) {
328e8d8bef9SDimitry Andric       if (isa<UndefValue>(Src0)) {
329e8d8bef9SDimitry Andric         return IC.replaceInstUsesWith(II, UndefValue::get(II.getType()));
330e8d8bef9SDimitry Andric       }
331e8d8bef9SDimitry Andric 
332e8d8bef9SDimitry Andric       // Clamp mask to used bits
333e8d8bef9SDimitry Andric       if ((Mask & FullMask) != Mask) {
334e8d8bef9SDimitry Andric         CallInst *NewCall = IC.Builder.CreateCall(
335e8d8bef9SDimitry Andric             II.getCalledFunction(),
336e8d8bef9SDimitry Andric             {Src0, ConstantInt::get(Src1->getType(), Mask & FullMask)});
337e8d8bef9SDimitry Andric 
338e8d8bef9SDimitry Andric         NewCall->takeName(&II);
339e8d8bef9SDimitry Andric         return IC.replaceInstUsesWith(II, NewCall);
340e8d8bef9SDimitry Andric       }
341e8d8bef9SDimitry Andric 
342e8d8bef9SDimitry Andric       break;
343e8d8bef9SDimitry Andric     }
344e8d8bef9SDimitry Andric 
345e8d8bef9SDimitry Andric     const APFloat &Val = CVal->getValueAPF();
346e8d8bef9SDimitry Andric 
347e8d8bef9SDimitry Andric     bool Result =
348e8d8bef9SDimitry Andric         ((Mask & S_NAN) && Val.isNaN() && Val.isSignaling()) ||
349e8d8bef9SDimitry Andric         ((Mask & Q_NAN) && Val.isNaN() && !Val.isSignaling()) ||
350e8d8bef9SDimitry Andric         ((Mask & N_INFINITY) && Val.isInfinity() && Val.isNegative()) ||
351e8d8bef9SDimitry Andric         ((Mask & N_NORMAL) && Val.isNormal() && Val.isNegative()) ||
352e8d8bef9SDimitry Andric         ((Mask & N_SUBNORMAL) && Val.isDenormal() && Val.isNegative()) ||
353e8d8bef9SDimitry Andric         ((Mask & N_ZERO) && Val.isZero() && Val.isNegative()) ||
354e8d8bef9SDimitry Andric         ((Mask & P_ZERO) && Val.isZero() && !Val.isNegative()) ||
355e8d8bef9SDimitry Andric         ((Mask & P_SUBNORMAL) && Val.isDenormal() && !Val.isNegative()) ||
356e8d8bef9SDimitry Andric         ((Mask & P_NORMAL) && Val.isNormal() && !Val.isNegative()) ||
357e8d8bef9SDimitry Andric         ((Mask & P_INFINITY) && Val.isInfinity() && !Val.isNegative());
358e8d8bef9SDimitry Andric 
359e8d8bef9SDimitry Andric     return IC.replaceInstUsesWith(II, ConstantInt::get(II.getType(), Result));
360e8d8bef9SDimitry Andric   }
361e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_cvt_pkrtz: {
362e8d8bef9SDimitry Andric     Value *Src0 = II.getArgOperand(0);
363e8d8bef9SDimitry Andric     Value *Src1 = II.getArgOperand(1);
364e8d8bef9SDimitry Andric     if (const ConstantFP *C0 = dyn_cast<ConstantFP>(Src0)) {
365e8d8bef9SDimitry Andric       if (const ConstantFP *C1 = dyn_cast<ConstantFP>(Src1)) {
366e8d8bef9SDimitry Andric         const fltSemantics &HalfSem =
367e8d8bef9SDimitry Andric             II.getType()->getScalarType()->getFltSemantics();
368e8d8bef9SDimitry Andric         bool LosesInfo;
369e8d8bef9SDimitry Andric         APFloat Val0 = C0->getValueAPF();
370e8d8bef9SDimitry Andric         APFloat Val1 = C1->getValueAPF();
371e8d8bef9SDimitry Andric         Val0.convert(HalfSem, APFloat::rmTowardZero, &LosesInfo);
372e8d8bef9SDimitry Andric         Val1.convert(HalfSem, APFloat::rmTowardZero, &LosesInfo);
373e8d8bef9SDimitry Andric 
374e8d8bef9SDimitry Andric         Constant *Folded =
375e8d8bef9SDimitry Andric             ConstantVector::get({ConstantFP::get(II.getContext(), Val0),
376e8d8bef9SDimitry Andric                                  ConstantFP::get(II.getContext(), Val1)});
377e8d8bef9SDimitry Andric         return IC.replaceInstUsesWith(II, Folded);
378e8d8bef9SDimitry Andric       }
379e8d8bef9SDimitry Andric     }
380e8d8bef9SDimitry Andric 
381e8d8bef9SDimitry Andric     if (isa<UndefValue>(Src0) && isa<UndefValue>(Src1)) {
382e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, UndefValue::get(II.getType()));
383e8d8bef9SDimitry Andric     }
384e8d8bef9SDimitry Andric 
385e8d8bef9SDimitry Andric     break;
386e8d8bef9SDimitry Andric   }
387e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_cvt_pknorm_i16:
388e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_cvt_pknorm_u16:
389e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_cvt_pk_i16:
390e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_cvt_pk_u16: {
391e8d8bef9SDimitry Andric     Value *Src0 = II.getArgOperand(0);
392e8d8bef9SDimitry Andric     Value *Src1 = II.getArgOperand(1);
393e8d8bef9SDimitry Andric 
394e8d8bef9SDimitry Andric     if (isa<UndefValue>(Src0) && isa<UndefValue>(Src1)) {
395e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, UndefValue::get(II.getType()));
396e8d8bef9SDimitry Andric     }
397e8d8bef9SDimitry Andric 
398e8d8bef9SDimitry Andric     break;
399e8d8bef9SDimitry Andric   }
400e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_ubfe:
401e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_sbfe: {
402e8d8bef9SDimitry Andric     // Decompose simple cases into standard shifts.
403e8d8bef9SDimitry Andric     Value *Src = II.getArgOperand(0);
404e8d8bef9SDimitry Andric     if (isa<UndefValue>(Src)) {
405e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, Src);
406e8d8bef9SDimitry Andric     }
407e8d8bef9SDimitry Andric 
408e8d8bef9SDimitry Andric     unsigned Width;
409e8d8bef9SDimitry Andric     Type *Ty = II.getType();
410e8d8bef9SDimitry Andric     unsigned IntSize = Ty->getIntegerBitWidth();
411e8d8bef9SDimitry Andric 
412e8d8bef9SDimitry Andric     ConstantInt *CWidth = dyn_cast<ConstantInt>(II.getArgOperand(2));
413e8d8bef9SDimitry Andric     if (CWidth) {
414e8d8bef9SDimitry Andric       Width = CWidth->getZExtValue();
415e8d8bef9SDimitry Andric       if ((Width & (IntSize - 1)) == 0) {
416e8d8bef9SDimitry Andric         return IC.replaceInstUsesWith(II, ConstantInt::getNullValue(Ty));
417e8d8bef9SDimitry Andric       }
418e8d8bef9SDimitry Andric 
419e8d8bef9SDimitry Andric       // Hardware ignores high bits, so remove those.
420e8d8bef9SDimitry Andric       if (Width >= IntSize) {
421e8d8bef9SDimitry Andric         return IC.replaceOperand(
422e8d8bef9SDimitry Andric             II, 2, ConstantInt::get(CWidth->getType(), Width & (IntSize - 1)));
423e8d8bef9SDimitry Andric       }
424e8d8bef9SDimitry Andric     }
425e8d8bef9SDimitry Andric 
426e8d8bef9SDimitry Andric     unsigned Offset;
427e8d8bef9SDimitry Andric     ConstantInt *COffset = dyn_cast<ConstantInt>(II.getArgOperand(1));
428e8d8bef9SDimitry Andric     if (COffset) {
429e8d8bef9SDimitry Andric       Offset = COffset->getZExtValue();
430e8d8bef9SDimitry Andric       if (Offset >= IntSize) {
431e8d8bef9SDimitry Andric         return IC.replaceOperand(
432e8d8bef9SDimitry Andric             II, 1,
433e8d8bef9SDimitry Andric             ConstantInt::get(COffset->getType(), Offset & (IntSize - 1)));
434e8d8bef9SDimitry Andric       }
435e8d8bef9SDimitry Andric     }
436e8d8bef9SDimitry Andric 
437e8d8bef9SDimitry Andric     bool Signed = IID == Intrinsic::amdgcn_sbfe;
438e8d8bef9SDimitry Andric 
439e8d8bef9SDimitry Andric     if (!CWidth || !COffset)
440e8d8bef9SDimitry Andric       break;
441e8d8bef9SDimitry Andric 
442349cc55cSDimitry Andric     // The case of Width == 0 is handled above, which makes this transformation
443e8d8bef9SDimitry Andric     // safe.  If Width == 0, then the ashr and lshr instructions become poison
444e8d8bef9SDimitry Andric     // value since the shift amount would be equal to the bit size.
445e8d8bef9SDimitry Andric     assert(Width != 0);
446e8d8bef9SDimitry Andric 
447e8d8bef9SDimitry Andric     // TODO: This allows folding to undef when the hardware has specific
448e8d8bef9SDimitry Andric     // behavior?
449e8d8bef9SDimitry Andric     if (Offset + Width < IntSize) {
450e8d8bef9SDimitry Andric       Value *Shl = IC.Builder.CreateShl(Src, IntSize - Offset - Width);
451e8d8bef9SDimitry Andric       Value *RightShift = Signed ? IC.Builder.CreateAShr(Shl, IntSize - Width)
452e8d8bef9SDimitry Andric                                  : IC.Builder.CreateLShr(Shl, IntSize - Width);
453e8d8bef9SDimitry Andric       RightShift->takeName(&II);
454e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, RightShift);
455e8d8bef9SDimitry Andric     }
456e8d8bef9SDimitry Andric 
457e8d8bef9SDimitry Andric     Value *RightShift = Signed ? IC.Builder.CreateAShr(Src, Offset)
458e8d8bef9SDimitry Andric                                : IC.Builder.CreateLShr(Src, Offset);
459e8d8bef9SDimitry Andric 
460e8d8bef9SDimitry Andric     RightShift->takeName(&II);
461e8d8bef9SDimitry Andric     return IC.replaceInstUsesWith(II, RightShift);
462e8d8bef9SDimitry Andric   }
463e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_exp:
464e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_exp_compr: {
465e8d8bef9SDimitry Andric     ConstantInt *En = cast<ConstantInt>(II.getArgOperand(1));
466e8d8bef9SDimitry Andric     unsigned EnBits = En->getZExtValue();
467e8d8bef9SDimitry Andric     if (EnBits == 0xf)
468e8d8bef9SDimitry Andric       break; // All inputs enabled.
469e8d8bef9SDimitry Andric 
470e8d8bef9SDimitry Andric     bool IsCompr = IID == Intrinsic::amdgcn_exp_compr;
471e8d8bef9SDimitry Andric     bool Changed = false;
472e8d8bef9SDimitry Andric     for (int I = 0; I < (IsCompr ? 2 : 4); ++I) {
473e8d8bef9SDimitry Andric       if ((!IsCompr && (EnBits & (1 << I)) == 0) ||
474e8d8bef9SDimitry Andric           (IsCompr && ((EnBits & (0x3 << (2 * I))) == 0))) {
475e8d8bef9SDimitry Andric         Value *Src = II.getArgOperand(I + 2);
476e8d8bef9SDimitry Andric         if (!isa<UndefValue>(Src)) {
477e8d8bef9SDimitry Andric           IC.replaceOperand(II, I + 2, UndefValue::get(Src->getType()));
478e8d8bef9SDimitry Andric           Changed = true;
479e8d8bef9SDimitry Andric         }
480e8d8bef9SDimitry Andric       }
481e8d8bef9SDimitry Andric     }
482e8d8bef9SDimitry Andric 
483e8d8bef9SDimitry Andric     if (Changed) {
484e8d8bef9SDimitry Andric       return &II;
485e8d8bef9SDimitry Andric     }
486e8d8bef9SDimitry Andric 
487e8d8bef9SDimitry Andric     break;
488e8d8bef9SDimitry Andric   }
489e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_fmed3: {
490e8d8bef9SDimitry Andric     // Note this does not preserve proper sNaN behavior if IEEE-mode is enabled
491e8d8bef9SDimitry Andric     // for the shader.
492e8d8bef9SDimitry Andric 
493e8d8bef9SDimitry Andric     Value *Src0 = II.getArgOperand(0);
494e8d8bef9SDimitry Andric     Value *Src1 = II.getArgOperand(1);
495e8d8bef9SDimitry Andric     Value *Src2 = II.getArgOperand(2);
496e8d8bef9SDimitry Andric 
497e8d8bef9SDimitry Andric     // Checking for NaN before canonicalization provides better fidelity when
498e8d8bef9SDimitry Andric     // mapping other operations onto fmed3 since the order of operands is
499e8d8bef9SDimitry Andric     // unchanged.
500e8d8bef9SDimitry Andric     CallInst *NewCall = nullptr;
501e8d8bef9SDimitry Andric     if (match(Src0, PatternMatch::m_NaN()) || isa<UndefValue>(Src0)) {
502e8d8bef9SDimitry Andric       NewCall = IC.Builder.CreateMinNum(Src1, Src2);
503e8d8bef9SDimitry Andric     } else if (match(Src1, PatternMatch::m_NaN()) || isa<UndefValue>(Src1)) {
504e8d8bef9SDimitry Andric       NewCall = IC.Builder.CreateMinNum(Src0, Src2);
505e8d8bef9SDimitry Andric     } else if (match(Src2, PatternMatch::m_NaN()) || isa<UndefValue>(Src2)) {
506e8d8bef9SDimitry Andric       NewCall = IC.Builder.CreateMaxNum(Src0, Src1);
507e8d8bef9SDimitry Andric     }
508e8d8bef9SDimitry Andric 
509e8d8bef9SDimitry Andric     if (NewCall) {
510e8d8bef9SDimitry Andric       NewCall->copyFastMathFlags(&II);
511e8d8bef9SDimitry Andric       NewCall->takeName(&II);
512e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, NewCall);
513e8d8bef9SDimitry Andric     }
514e8d8bef9SDimitry Andric 
515e8d8bef9SDimitry Andric     bool Swap = false;
516e8d8bef9SDimitry Andric     // Canonicalize constants to RHS operands.
517e8d8bef9SDimitry Andric     //
518e8d8bef9SDimitry Andric     // fmed3(c0, x, c1) -> fmed3(x, c0, c1)
519e8d8bef9SDimitry Andric     if (isa<Constant>(Src0) && !isa<Constant>(Src1)) {
520e8d8bef9SDimitry Andric       std::swap(Src0, Src1);
521e8d8bef9SDimitry Andric       Swap = true;
522e8d8bef9SDimitry Andric     }
523e8d8bef9SDimitry Andric 
524e8d8bef9SDimitry Andric     if (isa<Constant>(Src1) && !isa<Constant>(Src2)) {
525e8d8bef9SDimitry Andric       std::swap(Src1, Src2);
526e8d8bef9SDimitry Andric       Swap = true;
527e8d8bef9SDimitry Andric     }
528e8d8bef9SDimitry Andric 
529e8d8bef9SDimitry Andric     if (isa<Constant>(Src0) && !isa<Constant>(Src1)) {
530e8d8bef9SDimitry Andric       std::swap(Src0, Src1);
531e8d8bef9SDimitry Andric       Swap = true;
532e8d8bef9SDimitry Andric     }
533e8d8bef9SDimitry Andric 
534e8d8bef9SDimitry Andric     if (Swap) {
535e8d8bef9SDimitry Andric       II.setArgOperand(0, Src0);
536e8d8bef9SDimitry Andric       II.setArgOperand(1, Src1);
537e8d8bef9SDimitry Andric       II.setArgOperand(2, Src2);
538e8d8bef9SDimitry Andric       return &II;
539e8d8bef9SDimitry Andric     }
540e8d8bef9SDimitry Andric 
541e8d8bef9SDimitry Andric     if (const ConstantFP *C0 = dyn_cast<ConstantFP>(Src0)) {
542e8d8bef9SDimitry Andric       if (const ConstantFP *C1 = dyn_cast<ConstantFP>(Src1)) {
543e8d8bef9SDimitry Andric         if (const ConstantFP *C2 = dyn_cast<ConstantFP>(Src2)) {
544e8d8bef9SDimitry Andric           APFloat Result = fmed3AMDGCN(C0->getValueAPF(), C1->getValueAPF(),
545e8d8bef9SDimitry Andric                                        C2->getValueAPF());
546e8d8bef9SDimitry Andric           return IC.replaceInstUsesWith(
547e8d8bef9SDimitry Andric               II, ConstantFP::get(IC.Builder.getContext(), Result));
548e8d8bef9SDimitry Andric         }
549e8d8bef9SDimitry Andric       }
550e8d8bef9SDimitry Andric     }
551e8d8bef9SDimitry Andric 
552e8d8bef9SDimitry Andric     break;
553e8d8bef9SDimitry Andric   }
554e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_icmp:
555e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_fcmp: {
556e8d8bef9SDimitry Andric     const ConstantInt *CC = cast<ConstantInt>(II.getArgOperand(2));
557e8d8bef9SDimitry Andric     // Guard against invalid arguments.
558e8d8bef9SDimitry Andric     int64_t CCVal = CC->getZExtValue();
559e8d8bef9SDimitry Andric     bool IsInteger = IID == Intrinsic::amdgcn_icmp;
560e8d8bef9SDimitry Andric     if ((IsInteger && (CCVal < CmpInst::FIRST_ICMP_PREDICATE ||
561e8d8bef9SDimitry Andric                        CCVal > CmpInst::LAST_ICMP_PREDICATE)) ||
562e8d8bef9SDimitry Andric         (!IsInteger && (CCVal < CmpInst::FIRST_FCMP_PREDICATE ||
563e8d8bef9SDimitry Andric                         CCVal > CmpInst::LAST_FCMP_PREDICATE)))
564e8d8bef9SDimitry Andric       break;
565e8d8bef9SDimitry Andric 
566e8d8bef9SDimitry Andric     Value *Src0 = II.getArgOperand(0);
567e8d8bef9SDimitry Andric     Value *Src1 = II.getArgOperand(1);
568e8d8bef9SDimitry Andric 
569e8d8bef9SDimitry Andric     if (auto *CSrc0 = dyn_cast<Constant>(Src0)) {
570e8d8bef9SDimitry Andric       if (auto *CSrc1 = dyn_cast<Constant>(Src1)) {
571e8d8bef9SDimitry Andric         Constant *CCmp = ConstantExpr::getCompare(CCVal, CSrc0, CSrc1);
572e8d8bef9SDimitry Andric         if (CCmp->isNullValue()) {
573e8d8bef9SDimitry Andric           return IC.replaceInstUsesWith(
574e8d8bef9SDimitry Andric               II, ConstantExpr::getSExt(CCmp, II.getType()));
575e8d8bef9SDimitry Andric         }
576e8d8bef9SDimitry Andric 
577e8d8bef9SDimitry Andric         // The result of V_ICMP/V_FCMP assembly instructions (which this
578e8d8bef9SDimitry Andric         // intrinsic exposes) is one bit per thread, masked with the EXEC
579e8d8bef9SDimitry Andric         // register (which contains the bitmask of live threads). So a
580e8d8bef9SDimitry Andric         // comparison that always returns true is the same as a read of the
581e8d8bef9SDimitry Andric         // EXEC register.
582e8d8bef9SDimitry Andric         Function *NewF = Intrinsic::getDeclaration(
583e8d8bef9SDimitry Andric             II.getModule(), Intrinsic::read_register, II.getType());
584e8d8bef9SDimitry Andric         Metadata *MDArgs[] = {MDString::get(II.getContext(), "exec")};
585e8d8bef9SDimitry Andric         MDNode *MD = MDNode::get(II.getContext(), MDArgs);
586e8d8bef9SDimitry Andric         Value *Args[] = {MetadataAsValue::get(II.getContext(), MD)};
587e8d8bef9SDimitry Andric         CallInst *NewCall = IC.Builder.CreateCall(NewF, Args);
588349cc55cSDimitry Andric         NewCall->addFnAttr(Attribute::Convergent);
589e8d8bef9SDimitry Andric         NewCall->takeName(&II);
590e8d8bef9SDimitry Andric         return IC.replaceInstUsesWith(II, NewCall);
591e8d8bef9SDimitry Andric       }
592e8d8bef9SDimitry Andric 
593e8d8bef9SDimitry Andric       // Canonicalize constants to RHS.
594e8d8bef9SDimitry Andric       CmpInst::Predicate SwapPred =
595e8d8bef9SDimitry Andric           CmpInst::getSwappedPredicate(static_cast<CmpInst::Predicate>(CCVal));
596e8d8bef9SDimitry Andric       II.setArgOperand(0, Src1);
597e8d8bef9SDimitry Andric       II.setArgOperand(1, Src0);
598e8d8bef9SDimitry Andric       II.setArgOperand(
599e8d8bef9SDimitry Andric           2, ConstantInt::get(CC->getType(), static_cast<int>(SwapPred)));
600e8d8bef9SDimitry Andric       return &II;
601e8d8bef9SDimitry Andric     }
602e8d8bef9SDimitry Andric 
603e8d8bef9SDimitry Andric     if (CCVal != CmpInst::ICMP_EQ && CCVal != CmpInst::ICMP_NE)
604e8d8bef9SDimitry Andric       break;
605e8d8bef9SDimitry Andric 
606e8d8bef9SDimitry Andric     // Canonicalize compare eq with true value to compare != 0
607e8d8bef9SDimitry Andric     // llvm.amdgcn.icmp(zext (i1 x), 1, eq)
608e8d8bef9SDimitry Andric     //   -> llvm.amdgcn.icmp(zext (i1 x), 0, ne)
609e8d8bef9SDimitry Andric     // llvm.amdgcn.icmp(sext (i1 x), -1, eq)
610e8d8bef9SDimitry Andric     //   -> llvm.amdgcn.icmp(sext (i1 x), 0, ne)
611e8d8bef9SDimitry Andric     Value *ExtSrc;
612e8d8bef9SDimitry Andric     if (CCVal == CmpInst::ICMP_EQ &&
613e8d8bef9SDimitry Andric         ((match(Src1, PatternMatch::m_One()) &&
614e8d8bef9SDimitry Andric           match(Src0, m_ZExt(PatternMatch::m_Value(ExtSrc)))) ||
615e8d8bef9SDimitry Andric          (match(Src1, PatternMatch::m_AllOnes()) &&
616e8d8bef9SDimitry Andric           match(Src0, m_SExt(PatternMatch::m_Value(ExtSrc))))) &&
617e8d8bef9SDimitry Andric         ExtSrc->getType()->isIntegerTy(1)) {
618e8d8bef9SDimitry Andric       IC.replaceOperand(II, 1, ConstantInt::getNullValue(Src1->getType()));
619e8d8bef9SDimitry Andric       IC.replaceOperand(II, 2,
620e8d8bef9SDimitry Andric                         ConstantInt::get(CC->getType(), CmpInst::ICMP_NE));
621e8d8bef9SDimitry Andric       return &II;
622e8d8bef9SDimitry Andric     }
623e8d8bef9SDimitry Andric 
624e8d8bef9SDimitry Andric     CmpInst::Predicate SrcPred;
625e8d8bef9SDimitry Andric     Value *SrcLHS;
626e8d8bef9SDimitry Andric     Value *SrcRHS;
627e8d8bef9SDimitry Andric 
628e8d8bef9SDimitry Andric     // Fold compare eq/ne with 0 from a compare result as the predicate to the
629e8d8bef9SDimitry Andric     // intrinsic. The typical use is a wave vote function in the library, which
630e8d8bef9SDimitry Andric     // will be fed from a user code condition compared with 0. Fold in the
631e8d8bef9SDimitry Andric     // redundant compare.
632e8d8bef9SDimitry Andric 
633e8d8bef9SDimitry Andric     // llvm.amdgcn.icmp([sz]ext ([if]cmp pred a, b), 0, ne)
634e8d8bef9SDimitry Andric     //   -> llvm.amdgcn.[if]cmp(a, b, pred)
635e8d8bef9SDimitry Andric     //
636e8d8bef9SDimitry Andric     // llvm.amdgcn.icmp([sz]ext ([if]cmp pred a, b), 0, eq)
637e8d8bef9SDimitry Andric     //   -> llvm.amdgcn.[if]cmp(a, b, inv pred)
638e8d8bef9SDimitry Andric     if (match(Src1, PatternMatch::m_Zero()) &&
639e8d8bef9SDimitry Andric         match(Src0, PatternMatch::m_ZExtOrSExt(
640e8d8bef9SDimitry Andric                         m_Cmp(SrcPred, PatternMatch::m_Value(SrcLHS),
641e8d8bef9SDimitry Andric                               PatternMatch::m_Value(SrcRHS))))) {
642e8d8bef9SDimitry Andric       if (CCVal == CmpInst::ICMP_EQ)
643e8d8bef9SDimitry Andric         SrcPred = CmpInst::getInversePredicate(SrcPred);
644e8d8bef9SDimitry Andric 
645e8d8bef9SDimitry Andric       Intrinsic::ID NewIID = CmpInst::isFPPredicate(SrcPred)
646e8d8bef9SDimitry Andric                                  ? Intrinsic::amdgcn_fcmp
647e8d8bef9SDimitry Andric                                  : Intrinsic::amdgcn_icmp;
648e8d8bef9SDimitry Andric 
649e8d8bef9SDimitry Andric       Type *Ty = SrcLHS->getType();
650e8d8bef9SDimitry Andric       if (auto *CmpType = dyn_cast<IntegerType>(Ty)) {
651e8d8bef9SDimitry Andric         // Promote to next legal integer type.
652e8d8bef9SDimitry Andric         unsigned Width = CmpType->getBitWidth();
653e8d8bef9SDimitry Andric         unsigned NewWidth = Width;
654e8d8bef9SDimitry Andric 
655e8d8bef9SDimitry Andric         // Don't do anything for i1 comparisons.
656e8d8bef9SDimitry Andric         if (Width == 1)
657e8d8bef9SDimitry Andric           break;
658e8d8bef9SDimitry Andric 
659e8d8bef9SDimitry Andric         if (Width <= 16)
660e8d8bef9SDimitry Andric           NewWidth = 16;
661e8d8bef9SDimitry Andric         else if (Width <= 32)
662e8d8bef9SDimitry Andric           NewWidth = 32;
663e8d8bef9SDimitry Andric         else if (Width <= 64)
664e8d8bef9SDimitry Andric           NewWidth = 64;
665e8d8bef9SDimitry Andric         else if (Width > 64)
666e8d8bef9SDimitry Andric           break; // Can't handle this.
667e8d8bef9SDimitry Andric 
668e8d8bef9SDimitry Andric         if (Width != NewWidth) {
669e8d8bef9SDimitry Andric           IntegerType *CmpTy = IC.Builder.getIntNTy(NewWidth);
670e8d8bef9SDimitry Andric           if (CmpInst::isSigned(SrcPred)) {
671e8d8bef9SDimitry Andric             SrcLHS = IC.Builder.CreateSExt(SrcLHS, CmpTy);
672e8d8bef9SDimitry Andric             SrcRHS = IC.Builder.CreateSExt(SrcRHS, CmpTy);
673e8d8bef9SDimitry Andric           } else {
674e8d8bef9SDimitry Andric             SrcLHS = IC.Builder.CreateZExt(SrcLHS, CmpTy);
675e8d8bef9SDimitry Andric             SrcRHS = IC.Builder.CreateZExt(SrcRHS, CmpTy);
676e8d8bef9SDimitry Andric           }
677e8d8bef9SDimitry Andric         }
678e8d8bef9SDimitry Andric       } else if (!Ty->isFloatTy() && !Ty->isDoubleTy() && !Ty->isHalfTy())
679e8d8bef9SDimitry Andric         break;
680e8d8bef9SDimitry Andric 
681e8d8bef9SDimitry Andric       Function *NewF = Intrinsic::getDeclaration(
682e8d8bef9SDimitry Andric           II.getModule(), NewIID, {II.getType(), SrcLHS->getType()});
683e8d8bef9SDimitry Andric       Value *Args[] = {SrcLHS, SrcRHS,
684e8d8bef9SDimitry Andric                        ConstantInt::get(CC->getType(), SrcPred)};
685e8d8bef9SDimitry Andric       CallInst *NewCall = IC.Builder.CreateCall(NewF, Args);
686e8d8bef9SDimitry Andric       NewCall->takeName(&II);
687e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, NewCall);
688e8d8bef9SDimitry Andric     }
689e8d8bef9SDimitry Andric 
690e8d8bef9SDimitry Andric     break;
691e8d8bef9SDimitry Andric   }
692e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_ballot: {
693e8d8bef9SDimitry Andric     if (auto *Src = dyn_cast<ConstantInt>(II.getArgOperand(0))) {
694e8d8bef9SDimitry Andric       if (Src->isZero()) {
695e8d8bef9SDimitry Andric         // amdgcn.ballot(i1 0) is zero.
696e8d8bef9SDimitry Andric         return IC.replaceInstUsesWith(II, Constant::getNullValue(II.getType()));
697e8d8bef9SDimitry Andric       }
698e8d8bef9SDimitry Andric 
699e8d8bef9SDimitry Andric       if (Src->isOne()) {
700e8d8bef9SDimitry Andric         // amdgcn.ballot(i1 1) is exec.
701e8d8bef9SDimitry Andric         const char *RegName = "exec";
702e8d8bef9SDimitry Andric         if (II.getType()->isIntegerTy(32))
703e8d8bef9SDimitry Andric           RegName = "exec_lo";
704e8d8bef9SDimitry Andric         else if (!II.getType()->isIntegerTy(64))
705e8d8bef9SDimitry Andric           break;
706e8d8bef9SDimitry Andric 
707e8d8bef9SDimitry Andric         Function *NewF = Intrinsic::getDeclaration(
708e8d8bef9SDimitry Andric             II.getModule(), Intrinsic::read_register, II.getType());
709e8d8bef9SDimitry Andric         Metadata *MDArgs[] = {MDString::get(II.getContext(), RegName)};
710e8d8bef9SDimitry Andric         MDNode *MD = MDNode::get(II.getContext(), MDArgs);
711e8d8bef9SDimitry Andric         Value *Args[] = {MetadataAsValue::get(II.getContext(), MD)};
712e8d8bef9SDimitry Andric         CallInst *NewCall = IC.Builder.CreateCall(NewF, Args);
713349cc55cSDimitry Andric         NewCall->addFnAttr(Attribute::Convergent);
714e8d8bef9SDimitry Andric         NewCall->takeName(&II);
715e8d8bef9SDimitry Andric         return IC.replaceInstUsesWith(II, NewCall);
716e8d8bef9SDimitry Andric       }
717e8d8bef9SDimitry Andric     }
718e8d8bef9SDimitry Andric     break;
719e8d8bef9SDimitry Andric   }
720e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_wqm_vote: {
721e8d8bef9SDimitry Andric     // wqm_vote is identity when the argument is constant.
722e8d8bef9SDimitry Andric     if (!isa<Constant>(II.getArgOperand(0)))
723e8d8bef9SDimitry Andric       break;
724e8d8bef9SDimitry Andric 
725e8d8bef9SDimitry Andric     return IC.replaceInstUsesWith(II, II.getArgOperand(0));
726e8d8bef9SDimitry Andric   }
727e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_kill: {
728e8d8bef9SDimitry Andric     const ConstantInt *C = dyn_cast<ConstantInt>(II.getArgOperand(0));
729e8d8bef9SDimitry Andric     if (!C || !C->getZExtValue())
730e8d8bef9SDimitry Andric       break;
731e8d8bef9SDimitry Andric 
732e8d8bef9SDimitry Andric     // amdgcn.kill(i1 1) is a no-op
733e8d8bef9SDimitry Andric     return IC.eraseInstFromFunction(II);
734e8d8bef9SDimitry Andric   }
735e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_update_dpp: {
736e8d8bef9SDimitry Andric     Value *Old = II.getArgOperand(0);
737e8d8bef9SDimitry Andric 
738e8d8bef9SDimitry Andric     auto *BC = cast<ConstantInt>(II.getArgOperand(5));
739e8d8bef9SDimitry Andric     auto *RM = cast<ConstantInt>(II.getArgOperand(3));
740e8d8bef9SDimitry Andric     auto *BM = cast<ConstantInt>(II.getArgOperand(4));
741e8d8bef9SDimitry Andric     if (BC->isZeroValue() || RM->getZExtValue() != 0xF ||
742e8d8bef9SDimitry Andric         BM->getZExtValue() != 0xF || isa<UndefValue>(Old))
743e8d8bef9SDimitry Andric       break;
744e8d8bef9SDimitry Andric 
745e8d8bef9SDimitry Andric     // If bound_ctrl = 1, row mask = bank mask = 0xf we can omit old value.
746e8d8bef9SDimitry Andric     return IC.replaceOperand(II, 0, UndefValue::get(Old->getType()));
747e8d8bef9SDimitry Andric   }
748e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_permlane16:
749e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_permlanex16: {
750e8d8bef9SDimitry Andric     // Discard vdst_in if it's not going to be read.
751e8d8bef9SDimitry Andric     Value *VDstIn = II.getArgOperand(0);
752e8d8bef9SDimitry Andric     if (isa<UndefValue>(VDstIn))
753e8d8bef9SDimitry Andric       break;
754e8d8bef9SDimitry Andric 
755e8d8bef9SDimitry Andric     ConstantInt *FetchInvalid = cast<ConstantInt>(II.getArgOperand(4));
756e8d8bef9SDimitry Andric     ConstantInt *BoundCtrl = cast<ConstantInt>(II.getArgOperand(5));
757e8d8bef9SDimitry Andric     if (!FetchInvalid->getZExtValue() && !BoundCtrl->getZExtValue())
758e8d8bef9SDimitry Andric       break;
759e8d8bef9SDimitry Andric 
760e8d8bef9SDimitry Andric     return IC.replaceOperand(II, 0, UndefValue::get(VDstIn->getType()));
761e8d8bef9SDimitry Andric   }
762e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_readfirstlane:
763e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_readlane: {
764e8d8bef9SDimitry Andric     // A constant value is trivially uniform.
765e8d8bef9SDimitry Andric     if (Constant *C = dyn_cast<Constant>(II.getArgOperand(0))) {
766e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, C);
767e8d8bef9SDimitry Andric     }
768e8d8bef9SDimitry Andric 
769e8d8bef9SDimitry Andric     // The rest of these may not be safe if the exec may not be the same between
770e8d8bef9SDimitry Andric     // the def and use.
771e8d8bef9SDimitry Andric     Value *Src = II.getArgOperand(0);
772e8d8bef9SDimitry Andric     Instruction *SrcInst = dyn_cast<Instruction>(Src);
773e8d8bef9SDimitry Andric     if (SrcInst && SrcInst->getParent() != II.getParent())
774e8d8bef9SDimitry Andric       break;
775e8d8bef9SDimitry Andric 
776e8d8bef9SDimitry Andric     // readfirstlane (readfirstlane x) -> readfirstlane x
777e8d8bef9SDimitry Andric     // readlane (readfirstlane x), y -> readfirstlane x
778e8d8bef9SDimitry Andric     if (match(Src,
779e8d8bef9SDimitry Andric               PatternMatch::m_Intrinsic<Intrinsic::amdgcn_readfirstlane>())) {
780e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, Src);
781e8d8bef9SDimitry Andric     }
782e8d8bef9SDimitry Andric 
783e8d8bef9SDimitry Andric     if (IID == Intrinsic::amdgcn_readfirstlane) {
784e8d8bef9SDimitry Andric       // readfirstlane (readlane x, y) -> readlane x, y
785e8d8bef9SDimitry Andric       if (match(Src, PatternMatch::m_Intrinsic<Intrinsic::amdgcn_readlane>())) {
786e8d8bef9SDimitry Andric         return IC.replaceInstUsesWith(II, Src);
787e8d8bef9SDimitry Andric       }
788e8d8bef9SDimitry Andric     } else {
789e8d8bef9SDimitry Andric       // readlane (readlane x, y), y -> readlane x, y
790e8d8bef9SDimitry Andric       if (match(Src, PatternMatch::m_Intrinsic<Intrinsic::amdgcn_readlane>(
791e8d8bef9SDimitry Andric                          PatternMatch::m_Value(),
792e8d8bef9SDimitry Andric                          PatternMatch::m_Specific(II.getArgOperand(1))))) {
793e8d8bef9SDimitry Andric         return IC.replaceInstUsesWith(II, Src);
794e8d8bef9SDimitry Andric       }
795e8d8bef9SDimitry Andric     }
796e8d8bef9SDimitry Andric 
797e8d8bef9SDimitry Andric     break;
798e8d8bef9SDimitry Andric   }
799e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_ldexp: {
800e8d8bef9SDimitry Andric     // FIXME: This doesn't introduce new instructions and belongs in
801e8d8bef9SDimitry Andric     // InstructionSimplify.
802e8d8bef9SDimitry Andric     Type *Ty = II.getType();
803e8d8bef9SDimitry Andric     Value *Op0 = II.getArgOperand(0);
804e8d8bef9SDimitry Andric     Value *Op1 = II.getArgOperand(1);
805e8d8bef9SDimitry Andric 
806e8d8bef9SDimitry Andric     // Folding undef to qnan is safe regardless of the FP mode.
807e8d8bef9SDimitry Andric     if (isa<UndefValue>(Op0)) {
808e8d8bef9SDimitry Andric       auto *QNaN = ConstantFP::get(Ty, APFloat::getQNaN(Ty->getFltSemantics()));
809e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, QNaN);
810e8d8bef9SDimitry Andric     }
811e8d8bef9SDimitry Andric 
812e8d8bef9SDimitry Andric     const APFloat *C = nullptr;
813e8d8bef9SDimitry Andric     match(Op0, PatternMatch::m_APFloat(C));
814e8d8bef9SDimitry Andric 
815e8d8bef9SDimitry Andric     // FIXME: Should flush denorms depending on FP mode, but that's ignored
816e8d8bef9SDimitry Andric     // everywhere else.
817e8d8bef9SDimitry Andric     //
818e8d8bef9SDimitry Andric     // These cases should be safe, even with strictfp.
819e8d8bef9SDimitry Andric     // ldexp(0.0, x) -> 0.0
820e8d8bef9SDimitry Andric     // ldexp(-0.0, x) -> -0.0
821e8d8bef9SDimitry Andric     // ldexp(inf, x) -> inf
822e8d8bef9SDimitry Andric     // ldexp(-inf, x) -> -inf
823e8d8bef9SDimitry Andric     if (C && (C->isZero() || C->isInfinity())) {
824e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, Op0);
825e8d8bef9SDimitry Andric     }
826e8d8bef9SDimitry Andric 
827e8d8bef9SDimitry Andric     // With strictfp, be more careful about possibly needing to flush denormals
828e8d8bef9SDimitry Andric     // or not, and snan behavior depends on ieee_mode.
829e8d8bef9SDimitry Andric     if (II.isStrictFP())
830e8d8bef9SDimitry Andric       break;
831e8d8bef9SDimitry Andric 
832e8d8bef9SDimitry Andric     if (C && C->isNaN()) {
833e8d8bef9SDimitry Andric       // FIXME: We just need to make the nan quiet here, but that's unavailable
834e8d8bef9SDimitry Andric       // on APFloat, only IEEEfloat
835e8d8bef9SDimitry Andric       auto *Quieted =
836e8d8bef9SDimitry Andric           ConstantFP::get(Ty, scalbn(*C, 0, APFloat::rmNearestTiesToEven));
837e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, Quieted);
838e8d8bef9SDimitry Andric     }
839e8d8bef9SDimitry Andric 
840e8d8bef9SDimitry Andric     // ldexp(x, 0) -> x
841e8d8bef9SDimitry Andric     // ldexp(x, undef) -> x
842e8d8bef9SDimitry Andric     if (isa<UndefValue>(Op1) || match(Op1, PatternMatch::m_ZeroInt())) {
843e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, Op0);
844e8d8bef9SDimitry Andric     }
845e8d8bef9SDimitry Andric 
846e8d8bef9SDimitry Andric     break;
847e8d8bef9SDimitry Andric   }
848e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_fmul_legacy: {
849e8d8bef9SDimitry Andric     Value *Op0 = II.getArgOperand(0);
850e8d8bef9SDimitry Andric     Value *Op1 = II.getArgOperand(1);
851e8d8bef9SDimitry Andric 
852e8d8bef9SDimitry Andric     // The legacy behaviour is that multiplying +/-0.0 by anything, even NaN or
853e8d8bef9SDimitry Andric     // infinity, gives +0.0.
854e8d8bef9SDimitry Andric     // TODO: Move to InstSimplify?
855e8d8bef9SDimitry Andric     if (match(Op0, PatternMatch::m_AnyZeroFP()) ||
856e8d8bef9SDimitry Andric         match(Op1, PatternMatch::m_AnyZeroFP()))
857e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, ConstantFP::getNullValue(II.getType()));
858e8d8bef9SDimitry Andric 
859e8d8bef9SDimitry Andric     // If we can prove we don't have one of the special cases then we can use a
860e8d8bef9SDimitry Andric     // normal fmul instruction instead.
861e8d8bef9SDimitry Andric     if (canSimplifyLegacyMulToMul(Op0, Op1, IC)) {
862e8d8bef9SDimitry Andric       auto *FMul = IC.Builder.CreateFMulFMF(Op0, Op1, &II);
863e8d8bef9SDimitry Andric       FMul->takeName(&II);
864e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, FMul);
865e8d8bef9SDimitry Andric     }
866e8d8bef9SDimitry Andric     break;
867e8d8bef9SDimitry Andric   }
868e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_fma_legacy: {
869e8d8bef9SDimitry Andric     Value *Op0 = II.getArgOperand(0);
870e8d8bef9SDimitry Andric     Value *Op1 = II.getArgOperand(1);
871e8d8bef9SDimitry Andric     Value *Op2 = II.getArgOperand(2);
872e8d8bef9SDimitry Andric 
873e8d8bef9SDimitry Andric     // The legacy behaviour is that multiplying +/-0.0 by anything, even NaN or
874e8d8bef9SDimitry Andric     // infinity, gives +0.0.
875e8d8bef9SDimitry Andric     // TODO: Move to InstSimplify?
876e8d8bef9SDimitry Andric     if (match(Op0, PatternMatch::m_AnyZeroFP()) ||
877e8d8bef9SDimitry Andric         match(Op1, PatternMatch::m_AnyZeroFP())) {
878e8d8bef9SDimitry Andric       // It's tempting to just return Op2 here, but that would give the wrong
879e8d8bef9SDimitry Andric       // result if Op2 was -0.0.
880e8d8bef9SDimitry Andric       auto *Zero = ConstantFP::getNullValue(II.getType());
881e8d8bef9SDimitry Andric       auto *FAdd = IC.Builder.CreateFAddFMF(Zero, Op2, &II);
882e8d8bef9SDimitry Andric       FAdd->takeName(&II);
883e8d8bef9SDimitry Andric       return IC.replaceInstUsesWith(II, FAdd);
884e8d8bef9SDimitry Andric     }
885e8d8bef9SDimitry Andric 
886e8d8bef9SDimitry Andric     // If we can prove we don't have one of the special cases then we can use a
887e8d8bef9SDimitry Andric     // normal fma instead.
888e8d8bef9SDimitry Andric     if (canSimplifyLegacyMulToMul(Op0, Op1, IC)) {
889e8d8bef9SDimitry Andric       II.setCalledOperand(Intrinsic::getDeclaration(
890e8d8bef9SDimitry Andric           II.getModule(), Intrinsic::fma, II.getType()));
891e8d8bef9SDimitry Andric       return &II;
892e8d8bef9SDimitry Andric     }
893e8d8bef9SDimitry Andric     break;
894e8d8bef9SDimitry Andric   }
895*0eae32dcSDimitry Andric   case Intrinsic::amdgcn_is_shared:
896*0eae32dcSDimitry Andric   case Intrinsic::amdgcn_is_private: {
897*0eae32dcSDimitry Andric     if (isa<UndefValue>(II.getArgOperand(0)))
898*0eae32dcSDimitry Andric       return IC.replaceInstUsesWith(II, UndefValue::get(II.getType()));
899*0eae32dcSDimitry Andric 
900*0eae32dcSDimitry Andric     if (isa<ConstantPointerNull>(II.getArgOperand(0)))
901*0eae32dcSDimitry Andric       return IC.replaceInstUsesWith(II, ConstantInt::getFalse(II.getType()));
902*0eae32dcSDimitry Andric     break;
903*0eae32dcSDimitry Andric   }
904e8d8bef9SDimitry Andric   default: {
905e8d8bef9SDimitry Andric     if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr =
906e8d8bef9SDimitry Andric             AMDGPU::getImageDimIntrinsicInfo(II.getIntrinsicID())) {
907e8d8bef9SDimitry Andric       return simplifyAMDGCNImageIntrinsic(ST, ImageDimIntr, II, IC);
908e8d8bef9SDimitry Andric     }
909e8d8bef9SDimitry Andric   }
910e8d8bef9SDimitry Andric   }
911e8d8bef9SDimitry Andric   return None;
912e8d8bef9SDimitry Andric }
913e8d8bef9SDimitry Andric 
914e8d8bef9SDimitry Andric /// Implement SimplifyDemandedVectorElts for amdgcn buffer and image intrinsics.
915e8d8bef9SDimitry Andric ///
916e8d8bef9SDimitry Andric /// Note: This only supports non-TFE/LWE image intrinsic calls; those have
917e8d8bef9SDimitry Andric ///       struct returns.
918e8d8bef9SDimitry Andric static Value *simplifyAMDGCNMemoryIntrinsicDemanded(InstCombiner &IC,
919e8d8bef9SDimitry Andric                                                     IntrinsicInst &II,
920e8d8bef9SDimitry Andric                                                     APInt DemandedElts,
921e8d8bef9SDimitry Andric                                                     int DMaskIdx = -1) {
922e8d8bef9SDimitry Andric 
923e8d8bef9SDimitry Andric   auto *IIVTy = cast<FixedVectorType>(II.getType());
924e8d8bef9SDimitry Andric   unsigned VWidth = IIVTy->getNumElements();
925e8d8bef9SDimitry Andric   if (VWidth == 1)
926e8d8bef9SDimitry Andric     return nullptr;
927e8d8bef9SDimitry Andric 
928e8d8bef9SDimitry Andric   IRBuilderBase::InsertPointGuard Guard(IC.Builder);
929e8d8bef9SDimitry Andric   IC.Builder.SetInsertPoint(&II);
930e8d8bef9SDimitry Andric 
931e8d8bef9SDimitry Andric   // Assume the arguments are unchanged and later override them, if needed.
932e8d8bef9SDimitry Andric   SmallVector<Value *, 16> Args(II.args());
933e8d8bef9SDimitry Andric 
934e8d8bef9SDimitry Andric   if (DMaskIdx < 0) {
935e8d8bef9SDimitry Andric     // Buffer case.
936e8d8bef9SDimitry Andric 
937e8d8bef9SDimitry Andric     const unsigned ActiveBits = DemandedElts.getActiveBits();
938e8d8bef9SDimitry Andric     const unsigned UnusedComponentsAtFront = DemandedElts.countTrailingZeros();
939e8d8bef9SDimitry Andric 
940e8d8bef9SDimitry Andric     // Start assuming the prefix of elements is demanded, but possibly clear
941e8d8bef9SDimitry Andric     // some other bits if there are trailing zeros (unused components at front)
942e8d8bef9SDimitry Andric     // and update offset.
943e8d8bef9SDimitry Andric     DemandedElts = (1 << ActiveBits) - 1;
944e8d8bef9SDimitry Andric 
945e8d8bef9SDimitry Andric     if (UnusedComponentsAtFront > 0) {
946e8d8bef9SDimitry Andric       static const unsigned InvalidOffsetIdx = 0xf;
947e8d8bef9SDimitry Andric 
948e8d8bef9SDimitry Andric       unsigned OffsetIdx;
949e8d8bef9SDimitry Andric       switch (II.getIntrinsicID()) {
950e8d8bef9SDimitry Andric       case Intrinsic::amdgcn_raw_buffer_load:
951e8d8bef9SDimitry Andric         OffsetIdx = 1;
952e8d8bef9SDimitry Andric         break;
953e8d8bef9SDimitry Andric       case Intrinsic::amdgcn_s_buffer_load:
954e8d8bef9SDimitry Andric         // If resulting type is vec3, there is no point in trimming the
955e8d8bef9SDimitry Andric         // load with updated offset, as the vec3 would most likely be widened to
956e8d8bef9SDimitry Andric         // vec4 anyway during lowering.
957e8d8bef9SDimitry Andric         if (ActiveBits == 4 && UnusedComponentsAtFront == 1)
958e8d8bef9SDimitry Andric           OffsetIdx = InvalidOffsetIdx;
959e8d8bef9SDimitry Andric         else
960e8d8bef9SDimitry Andric           OffsetIdx = 1;
961e8d8bef9SDimitry Andric         break;
962e8d8bef9SDimitry Andric       case Intrinsic::amdgcn_struct_buffer_load:
963e8d8bef9SDimitry Andric         OffsetIdx = 2;
964e8d8bef9SDimitry Andric         break;
965e8d8bef9SDimitry Andric       default:
966e8d8bef9SDimitry Andric         // TODO: handle tbuffer* intrinsics.
967e8d8bef9SDimitry Andric         OffsetIdx = InvalidOffsetIdx;
968e8d8bef9SDimitry Andric         break;
969e8d8bef9SDimitry Andric       }
970e8d8bef9SDimitry Andric 
971e8d8bef9SDimitry Andric       if (OffsetIdx != InvalidOffsetIdx) {
972e8d8bef9SDimitry Andric         // Clear demanded bits and update the offset.
973e8d8bef9SDimitry Andric         DemandedElts &= ~((1 << UnusedComponentsAtFront) - 1);
974e8d8bef9SDimitry Andric         auto *Offset = II.getArgOperand(OffsetIdx);
975e8d8bef9SDimitry Andric         unsigned SingleComponentSizeInBits =
976e8d8bef9SDimitry Andric             IC.getDataLayout().getTypeSizeInBits(II.getType()->getScalarType());
977e8d8bef9SDimitry Andric         unsigned OffsetAdd =
978e8d8bef9SDimitry Andric             UnusedComponentsAtFront * SingleComponentSizeInBits / 8;
979e8d8bef9SDimitry Andric         auto *OffsetAddVal = ConstantInt::get(Offset->getType(), OffsetAdd);
980e8d8bef9SDimitry Andric         Args[OffsetIdx] = IC.Builder.CreateAdd(Offset, OffsetAddVal);
981e8d8bef9SDimitry Andric       }
982e8d8bef9SDimitry Andric     }
983e8d8bef9SDimitry Andric   } else {
984e8d8bef9SDimitry Andric     // Image case.
985e8d8bef9SDimitry Andric 
986e8d8bef9SDimitry Andric     ConstantInt *DMask = cast<ConstantInt>(II.getArgOperand(DMaskIdx));
987e8d8bef9SDimitry Andric     unsigned DMaskVal = DMask->getZExtValue() & 0xf;
988e8d8bef9SDimitry Andric 
989e8d8bef9SDimitry Andric     // Mask off values that are undefined because the dmask doesn't cover them
990e8d8bef9SDimitry Andric     DemandedElts &= (1 << countPopulation(DMaskVal)) - 1;
991e8d8bef9SDimitry Andric 
992e8d8bef9SDimitry Andric     unsigned NewDMaskVal = 0;
993e8d8bef9SDimitry Andric     unsigned OrigLoadIdx = 0;
994e8d8bef9SDimitry Andric     for (unsigned SrcIdx = 0; SrcIdx < 4; ++SrcIdx) {
995e8d8bef9SDimitry Andric       const unsigned Bit = 1 << SrcIdx;
996e8d8bef9SDimitry Andric       if (!!(DMaskVal & Bit)) {
997e8d8bef9SDimitry Andric         if (!!DemandedElts[OrigLoadIdx])
998e8d8bef9SDimitry Andric           NewDMaskVal |= Bit;
999e8d8bef9SDimitry Andric         OrigLoadIdx++;
1000e8d8bef9SDimitry Andric       }
1001e8d8bef9SDimitry Andric     }
1002e8d8bef9SDimitry Andric 
1003e8d8bef9SDimitry Andric     if (DMaskVal != NewDMaskVal)
1004e8d8bef9SDimitry Andric       Args[DMaskIdx] = ConstantInt::get(DMask->getType(), NewDMaskVal);
1005e8d8bef9SDimitry Andric   }
1006e8d8bef9SDimitry Andric 
1007e8d8bef9SDimitry Andric   unsigned NewNumElts = DemandedElts.countPopulation();
1008e8d8bef9SDimitry Andric   if (!NewNumElts)
1009e8d8bef9SDimitry Andric     return UndefValue::get(II.getType());
1010e8d8bef9SDimitry Andric 
1011e8d8bef9SDimitry Andric   if (NewNumElts >= VWidth && DemandedElts.isMask()) {
1012e8d8bef9SDimitry Andric     if (DMaskIdx >= 0)
1013e8d8bef9SDimitry Andric       II.setArgOperand(DMaskIdx, Args[DMaskIdx]);
1014e8d8bef9SDimitry Andric     return nullptr;
1015e8d8bef9SDimitry Andric   }
1016e8d8bef9SDimitry Andric 
1017e8d8bef9SDimitry Andric   // Validate function argument and return types, extracting overloaded types
1018e8d8bef9SDimitry Andric   // along the way.
1019e8d8bef9SDimitry Andric   SmallVector<Type *, 6> OverloadTys;
1020e8d8bef9SDimitry Andric   if (!Intrinsic::getIntrinsicSignature(II.getCalledFunction(), OverloadTys))
1021e8d8bef9SDimitry Andric     return nullptr;
1022e8d8bef9SDimitry Andric 
1023e8d8bef9SDimitry Andric   Module *M = II.getParent()->getParent()->getParent();
1024e8d8bef9SDimitry Andric   Type *EltTy = IIVTy->getElementType();
1025e8d8bef9SDimitry Andric   Type *NewTy =
1026e8d8bef9SDimitry Andric       (NewNumElts == 1) ? EltTy : FixedVectorType::get(EltTy, NewNumElts);
1027e8d8bef9SDimitry Andric 
1028e8d8bef9SDimitry Andric   OverloadTys[0] = NewTy;
1029e8d8bef9SDimitry Andric   Function *NewIntrin =
1030e8d8bef9SDimitry Andric       Intrinsic::getDeclaration(M, II.getIntrinsicID(), OverloadTys);
1031e8d8bef9SDimitry Andric 
1032e8d8bef9SDimitry Andric   CallInst *NewCall = IC.Builder.CreateCall(NewIntrin, Args);
1033e8d8bef9SDimitry Andric   NewCall->takeName(&II);
1034e8d8bef9SDimitry Andric   NewCall->copyMetadata(II);
1035e8d8bef9SDimitry Andric 
1036e8d8bef9SDimitry Andric   if (NewNumElts == 1) {
1037e8d8bef9SDimitry Andric     return IC.Builder.CreateInsertElement(UndefValue::get(II.getType()),
1038e8d8bef9SDimitry Andric                                           NewCall,
1039e8d8bef9SDimitry Andric                                           DemandedElts.countTrailingZeros());
1040e8d8bef9SDimitry Andric   }
1041e8d8bef9SDimitry Andric 
1042e8d8bef9SDimitry Andric   SmallVector<int, 8> EltMask;
1043e8d8bef9SDimitry Andric   unsigned NewLoadIdx = 0;
1044e8d8bef9SDimitry Andric   for (unsigned OrigLoadIdx = 0; OrigLoadIdx < VWidth; ++OrigLoadIdx) {
1045e8d8bef9SDimitry Andric     if (!!DemandedElts[OrigLoadIdx])
1046e8d8bef9SDimitry Andric       EltMask.push_back(NewLoadIdx++);
1047e8d8bef9SDimitry Andric     else
1048e8d8bef9SDimitry Andric       EltMask.push_back(NewNumElts);
1049e8d8bef9SDimitry Andric   }
1050e8d8bef9SDimitry Andric 
1051e8d8bef9SDimitry Andric   Value *Shuffle = IC.Builder.CreateShuffleVector(NewCall, EltMask);
1052e8d8bef9SDimitry Andric 
1053e8d8bef9SDimitry Andric   return Shuffle;
1054e8d8bef9SDimitry Andric }
1055e8d8bef9SDimitry Andric 
1056e8d8bef9SDimitry Andric Optional<Value *> GCNTTIImpl::simplifyDemandedVectorEltsIntrinsic(
1057e8d8bef9SDimitry Andric     InstCombiner &IC, IntrinsicInst &II, APInt DemandedElts, APInt &UndefElts,
1058e8d8bef9SDimitry Andric     APInt &UndefElts2, APInt &UndefElts3,
1059e8d8bef9SDimitry Andric     std::function<void(Instruction *, unsigned, APInt, APInt &)>
1060e8d8bef9SDimitry Andric         SimplifyAndSetOp) const {
1061e8d8bef9SDimitry Andric   switch (II.getIntrinsicID()) {
1062e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_buffer_load:
1063e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_buffer_load_format:
1064e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_raw_buffer_load:
1065e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_raw_buffer_load_format:
1066e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_raw_tbuffer_load:
1067e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_s_buffer_load:
1068e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_struct_buffer_load:
1069e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_struct_buffer_load_format:
1070e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_struct_tbuffer_load:
1071e8d8bef9SDimitry Andric   case Intrinsic::amdgcn_tbuffer_load:
1072e8d8bef9SDimitry Andric     return simplifyAMDGCNMemoryIntrinsicDemanded(IC, II, DemandedElts);
1073e8d8bef9SDimitry Andric   default: {
1074e8d8bef9SDimitry Andric     if (getAMDGPUImageDMaskIntrinsic(II.getIntrinsicID())) {
1075e8d8bef9SDimitry Andric       return simplifyAMDGCNMemoryIntrinsicDemanded(IC, II, DemandedElts, 0);
1076e8d8bef9SDimitry Andric     }
1077e8d8bef9SDimitry Andric     break;
1078e8d8bef9SDimitry Andric   }
1079e8d8bef9SDimitry Andric   }
1080e8d8bef9SDimitry Andric   return None;
1081e8d8bef9SDimitry Andric }
1082