Skip to content

Commit 8522d21

Browse files
committed
[InstCombine] Pull shuffles out of binops with splatted ops
1 parent 5ebf099 commit 8522d21

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,6 +2271,31 @@ Instruction *InstCombinerImpl::foldVectorBinop(BinaryOperator &Inst) {
22712271
}
22722272
}
22732273

2274+
// Similar to the combine above, but handles the case for scalable vectors
2275+
// where both V1 and C are splats.
2276+
//
2277+
// Op(shuffle(V1, 0), (splat C)) -> shuffle(Op(V1, (splat C)), 0)
2278+
if (isa<ScalableVectorType>(Inst.getType()) &&
2279+
match(&Inst, m_c_BinOp(m_OneUse(m_Shuffle(m_Value(V1), m_Poison(),
2280+
m_ZeroMask())),
2281+
m_ImmConstant(C)))) {
2282+
if (Constant *Splat = C->getSplatValue()) {
2283+
bool ConstOp1 = isa<Constant>(RHS);
2284+
VectorType *V1Ty = cast<VectorType>(V1->getType());
2285+
Constant *NewC = ConstantVector::getSplat(V1Ty->getElementCount(), Splat);
2286+
2287+
Value *NewLHS = ConstOp1 ? V1 : NewC;
2288+
Value *NewRHS = ConstOp1 ? NewC : V1;
2289+
Value *XY = Builder.CreateBinOp(Opcode, NewLHS, NewRHS);
2290+
if (auto *BO = dyn_cast<BinaryOperator>(XY))
2291+
BO->copyIRFlags(&Inst);
2292+
2293+
VectorType *VTy = cast<VectorType>(Inst.getType());
2294+
SmallVector<int> NewM(VTy->getElementCount().getKnownMinValue(), 0);
2295+
return new ShuffleVectorInst(XY, NewM);
2296+
}
2297+
}
2298+
22742299
// Try to reassociate to sink a splat shuffle after a binary operation.
22752300
if (Inst.isAssociative() && Inst.isCommutative()) {
22762301
// Canonicalize shuffle operand as LHS.

llvm/test/Transforms/InstCombine/getelementptr.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ define <2 x i1> @test13_fixed_scalable(i64 %X, ptr %P, <2 x i64> %y) nounwind {
282282
define <vscale x 2 x i1> @test13_scalable_scalable(i64 %X, ptr %P, <vscale x 2 x i64> %y) nounwind {
283283
; CHECK-LABEL: @test13_scalable_scalable(
284284
; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <vscale x 2 x i64> poison, i64 [[X:%.*]], i64 0
285-
; CHECK-NEXT: [[DOTSPLAT:%.*]] = shufflevector <vscale x 2 x i64> [[DOTSPLATINSERT]], <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
286-
; CHECK-NEXT: [[A_IDX:%.*]] = shl nsw <vscale x 2 x i64> [[DOTSPLAT]], splat (i64 3)
285+
; CHECK-NEXT: [[TMP3:%.*]] = shl nsw <vscale x 2 x i64> [[DOTSPLATINSERT]], splat (i64 3)
286+
; CHECK-NEXT: [[A_IDX:%.*]] = shufflevector <vscale x 2 x i64> [[TMP3]], <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
287287
; CHECK-NEXT: [[TMP1:%.*]] = call i64 @llvm.vscale.i64()
288288
; CHECK-NEXT: [[TMP2:%.*]] = shl i64 [[TMP1]], 4
289289
; CHECK-NEXT: [[DOTSPLATINSERT1:%.*]] = insertelement <vscale x 2 x i64> poison, i64 [[TMP2]], i64 0

llvm/test/Transforms/InstCombine/vec_shuffle-inseltpoison.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,9 +1791,9 @@ define <4 x i32> @PR46872(<4 x i32> %x) {
17911791

17921792
define <vscale x 4 x i32> @scalable_splat_binop_constant_rhs(<vscale x 4 x i32> %x) {
17931793
; CHECK-LABEL: @scalable_splat_binop_constant_rhs(
1794-
; CHECK-NEXT: [[R:%.*]] = shufflevector <vscale x 4 x i32> [[TMP1:%.*]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
1795-
; CHECK-NEXT: [[R1:%.*]] = add <vscale x 4 x i32> [[R]], splat (i32 42)
1796-
; CHECK-NEXT: ret <vscale x 4 x i32> [[R1]]
1794+
; CHECK-NEXT: [[R1:%.*]] = add <vscale x 4 x i32> [[R:%.*]], splat (i32 42)
1795+
; CHECK-NEXT: [[R2:%.*]] = shufflevector <vscale x 4 x i32> [[R1]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
1796+
; CHECK-NEXT: ret <vscale x 4 x i32> [[R2]]
17971797
;
17981798

17991799
%splatx = shufflevector <vscale x 4 x i32> %x, <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
@@ -1803,9 +1803,9 @@ define <vscale x 4 x i32> @scalable_splat_binop_constant_rhs(<vscale x 4 x i32>
18031803

18041804
define <vscale x 4 x float> @scalable_splat_binop_constant_lhs(<vscale x 4 x float> %x) {
18051805
; CHECK-LABEL: @scalable_splat_binop_constant_lhs(
1806-
; CHECK-NEXT: [[R:%.*]] = shufflevector <vscale x 4 x float> [[TMP1:%.*]], <vscale x 4 x float> poison, <vscale x 4 x i32> zeroinitializer
1807-
; CHECK-NEXT: [[R1:%.*]] = fadd <vscale x 4 x float> [[R]], splat (float 4.200000e+01)
1808-
; CHECK-NEXT: ret <vscale x 4 x float> [[R1]]
1806+
; CHECK-NEXT: [[R1:%.*]] = fadd <vscale x 4 x float> [[R:%.*]], splat (float 4.200000e+01)
1807+
; CHECK-NEXT: [[R2:%.*]] = shufflevector <vscale x 4 x float> [[R1]], <vscale x 4 x float> poison, <vscale x 4 x i32> zeroinitializer
1808+
; CHECK-NEXT: ret <vscale x 4 x float> [[R2]]
18091809
;
18101810

18111811
%splatx = shufflevector <vscale x 4 x float> %x, <vscale x 4 x float> poison, <vscale x 4 x i32> zeroinitializer

0 commit comments

Comments
 (0)