Skip to content

Commit 25172e1

Browse files
committed
[InstCombine] Allow folds of shifts by constants for scalable vectors again
However this meant that FoldShiftByConstant no longer kicked in for scalable vectors because scalable splats are represented by ConstantExprs. This fixes it by explicitly allowing splats of ConstantInts, it's not the prettiest so open to any suggestions. But I'm also hoping that UseConstantIntForScalableSplat will eventually remove the need for this. I noticed this when trying to reverse a combine on RISC-V in #132245, and saw that the resulting vector and scalar forms were different.
1 parent 5cb140c commit 25172e1

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,10 @@ Instruction *InstCombinerImpl::commonShiftTransforms(BinaryOperator &I) {
428428
return R;
429429

430430
Constant *CUI;
431-
if (match(Op1, m_ImmConstant(CUI)))
431+
if (match(Op1, m_Constant(CUI)) &&
432+
(!isa<ConstantExpr>(CUI) ||
433+
(Ty->isVectorTy() &&
434+
isa_and_present<ConstantInt>(CUI->getSplatValue()))))
432435
if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
433436
return Res;
434437

llvm/test/Transforms/InstCombine/shl-bo.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,8 @@ define <16 x i8> @test_FoldShiftByConstant_CreateAnd(<16 x i8> %in0) {
659659

660660
define <vscale x 1 x i8> @test_FoldShiftByConstant_CreateAnd_scalable(<vscale x 1 x i8> %x) {
661661
; CHECK-LABEL: @test_FoldShiftByConstant_CreateAnd_scalable(
662-
; CHECK-NEXT: [[TMP1:%.*]] = and <vscale x 1 x i8> [[X:%.*]], splat (i8 2)
663-
; CHECK-NEXT: [[TMP2:%.*]] = shl nuw nsw <vscale x 1 x i8> [[TMP1]], splat (i8 2)
662+
; CHECK-NEXT: [[TMP1:%.*]] = shl <vscale x 1 x i8> [[X:%.*]], splat (i8 2)
663+
; CHECK-NEXT: [[TMP2:%.*]] = and <vscale x 1 x i8> [[TMP1]], splat (i8 8)
664664
; CHECK-NEXT: ret <vscale x 1 x i8> [[TMP2]]
665665
;
666666
%1 = and <vscale x 1 x i8> %x, splat (i8 2)

0 commit comments

Comments
 (0)