Skip to content

Commit fb492be

Browse files
committed
Use getSplatValue and correctly construct APInt and add i64 test
1 parent fa77c2c commit fb492be

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#include "llvm/Support/KnownBits.h"
6565
#include "llvm/Support/KnownFPClass.h"
6666
#include "llvm/Support/MathExtras.h"
67+
#include "llvm/Support/TypeSize.h"
6768
#include "llvm/Support/raw_ostream.h"
6869
#include "llvm/Transforms/InstCombine/InstCombiner.h"
6970
#include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
@@ -3769,29 +3770,24 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
37693770
// %4 = tail call i32 @llvm.vector.reduce.add.v4i32(%3)
37703771
// =>
37713772
// %2 = shl i32 %0, 2
3772-
Value *InputValue;
3773-
ArrayRef<int> Mask;
3774-
ConstantInt *InsertionIdx;
37753773
assert(Arg->getType()->isVectorTy() &&
37763774
"The vector.reduce.add intrinsic's argument must be a vector!");
37773775

3778-
if (match(Arg, m_Shuffle(m_InsertElt(m_Poison(), m_Value(InputValue),
3779-
m_ConstantInt(InsertionIdx)),
3780-
m_Poison(), m_Mask(Mask)))) {
3776+
if (Value *Splat = getSplatValue(Arg)) {
37813777
// It is only a multiplication if we add the same element over and over.
3782-
bool AllElementsAreTheSameInMask =
3783-
std::all_of(Mask.begin(), Mask.end(),
3784-
[&Mask](int MaskElt) { return MaskElt == Mask[0]; });
3785-
unsigned ReducedVectorLength = Mask.size();
3786-
3787-
if (AllElementsAreTheSameInMask &&
3788-
InsertionIdx->getSExtValue() == Mask[0] &&
3789-
isPowerOf2_32(ReducedVectorLength)) {
3790-
unsigned Pow2 = Log2_32(ReducedVectorLength);
3791-
Value *Res = Builder.CreateShl(
3792-
InputValue, Constant::getIntegerValue(InputValue->getType(),
3793-
APInt(32, Pow2)));
3794-
return replaceInstUsesWith(CI, Res);
3778+
ElementCount ReducedVectorElementCount =
3779+
static_cast<VectorType *>(Arg->getType())->getElementCount();
3780+
if (ReducedVectorElementCount.isFixed()) {
3781+
unsigned VectorSize = ReducedVectorElementCount.getFixedValue();
3782+
if (isPowerOf2_32(VectorSize)) {
3783+
unsigned Pow2 = Log2_32(VectorSize);
3784+
Value *Res = Builder.CreateShl(
3785+
Splat,
3786+
Constant::getIntegerValue(
3787+
Splat->getType(),
3788+
APInt(Splat->getType()->getIntegerBitWidth(), Pow2)));
3789+
return replaceInstUsesWith(CI, Res);
3790+
}
37953791
}
37963792
}
37973793
}

llvm/test/Transforms/InstCombine/vector-reductions.ll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,17 @@ define i32 @constant_multiplied_at_0(i32 %0) {
320320
ret i32 %4
321321
}
322322

323+
define i64 @constant_multiplied_at_0_64bits(i64 %0) {
324+
; CHECK-LABEL: @constant_multiplied_at_0_64bits(
325+
; CHECK-NEXT: [[TMP2:%.*]] = shl i64 [[TMP0:%.*]], 2
326+
; CHECK-NEXT: ret i64 [[TMP2]]
327+
;
328+
%2 = insertelement <4 x i64> poison, i64 %0, i64 0
329+
%3 = shufflevector <4 x i64> %2, <4 x i64> poison, <4 x i32> zeroinitializer
330+
%4 = tail call i64 @llvm.vector.reduce.add.v4i64(<4 x i64> %3)
331+
ret i64 %4
332+
}
333+
323334
define i32 @constant_multiplied_at_0_two_pow8(i32 %0) {
324335
; CHECK-LABEL: @constant_multiplied_at_0_two_pow8(
325336
; CHECK-NEXT: [[TMP2:%.*]] = shl i32 [[TMP0:%.*]], 3

0 commit comments

Comments
 (0)