Skip to content

Commit 4b4699a

Browse files
authored
[InstCombine] Don't cover up poison elements for shifts when folding shuffles thru binops (#141303)
As noted in the TODO, we don't need to cover up the poison elements placed in the unused lanes for shifts, since it's not UB unlike div/rem. New poison elements are only introduced in cases like ShMask = <1,1,2,2> and C = <5,5,6,6> --> NewC = <poison,5,6,poison> And the resulting shuffle won't use the poison lanes.
1 parent 294643e commit 4b4699a

File tree

4 files changed

+15
-17
lines changed

4 files changed

+15
-17
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,11 +2257,9 @@ Instruction *InstCombinerImpl::foldVectorBinop(BinaryOperator &Inst) {
22572257
}
22582258
if (MayChange) {
22592259
Constant *NewC = ConstantVector::get(NewVecC);
2260-
// It may not be safe to execute a binop on a vector with poison elements
2261-
// because the entire instruction can be folded to undef or create poison
2262-
// that did not exist in the original code.
2263-
// TODO: The shift case should not be necessary.
2264-
if (Inst.isIntDivRem() || (Inst.isShift() && ConstOp1))
2260+
// Lanes of NewC not used by the shuffle will be poison which will cause
2261+
// UB for div/rem. Mask them with a safe constant.
2262+
if (Inst.isIntDivRem())
22652263
NewC = getSafeVectorConstantForBinop(Opcode, NewC, ConstOp1);
22662264

22672265
// Op(shuffle(V1, Mask), C) -> shuffle(Op(V1, NewC), Mask)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ define <2 x i32> @shl_splat_constant0(<2 x i32> %x) {
880880

881881
define <2 x i32> @shl_splat_constant1(<2 x i32> %x) {
882882
; CHECK-LABEL: @shl_splat_constant1(
883-
; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i32> [[X:%.*]], <i32 5, i32 0>
883+
; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i32> [[X:%.*]], <i32 5, i32 poison>
884884
; CHECK-NEXT: [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
885885
; CHECK-NEXT: ret <2 x i32> [[R]]
886886
;
@@ -902,7 +902,7 @@ define <2 x i32> @ashr_splat_constant0(<2 x i32> %x) {
902902

903903
define <2 x i32> @ashr_splat_constant1(<2 x i32> %x) {
904904
; CHECK-LABEL: @ashr_splat_constant1(
905-
; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i32> [[X:%.*]], <i32 5, i32 0>
905+
; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i32> [[X:%.*]], <i32 5, i32 poison>
906906
; CHECK-NEXT: [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
907907
; CHECK-NEXT: ret <2 x i32> [[R]]
908908
;
@@ -924,7 +924,7 @@ define <2 x i32> @lshr_splat_constant0(<2 x i32> %x) {
924924

925925
define <2 x i32> @lshr_splat_constant1(<2 x i32> %x) {
926926
; CHECK-LABEL: @lshr_splat_constant1(
927-
; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i32> [[X:%.*]], <i32 5, i32 0>
927+
; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i32> [[X:%.*]], <i32 5, i32 poison>
928928
; CHECK-NEXT: [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
929929
; CHECK-NEXT: ret <2 x i32> [[R]]
930930
;
@@ -1161,7 +1161,7 @@ entry:
11611161
define <4 x i16> @shl_constant_mask_undef(<4 x i16> %in) {
11621162
; CHECK-LABEL: @shl_constant_mask_undef(
11631163
; CHECK-NEXT: entry:
1164-
; CHECK-NEXT: [[TMP0:%.*]] = shl <4 x i16> [[IN:%.*]], <i16 10, i16 0, i16 0, i16 0>
1164+
; CHECK-NEXT: [[TMP0:%.*]] = shl <4 x i16> [[IN:%.*]], <i16 10, i16 0, i16 poison, i16 poison>
11651165
; CHECK-NEXT: [[SHL:%.*]] = shufflevector <4 x i16> [[TMP0]], <4 x i16> poison, <4 x i32> <i32 0, i32 poison, i32 1, i32 1>
11661166
; CHECK-NEXT: ret <4 x i16> [[SHL]]
11671167
;

llvm/test/Transforms/InstCombine/vec_shuffle.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ define <2 x i32> @shl_splat_constant0(<2 x i32> %x) {
929929

930930
define <2 x i32> @shl_splat_constant1(<2 x i32> %x) {
931931
; CHECK-LABEL: @shl_splat_constant1(
932-
; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i32> [[X:%.*]], <i32 5, i32 0>
932+
; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i32> [[X:%.*]], <i32 5, i32 poison>
933933
; CHECK-NEXT: [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
934934
; CHECK-NEXT: ret <2 x i32> [[R]]
935935
;
@@ -951,7 +951,7 @@ define <2 x i32> @ashr_splat_constant0(<2 x i32> %x) {
951951

952952
define <2 x i32> @ashr_splat_constant1(<2 x i32> %x) {
953953
; CHECK-LABEL: @ashr_splat_constant1(
954-
; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i32> [[X:%.*]], <i32 5, i32 0>
954+
; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i32> [[X:%.*]], <i32 5, i32 poison>
955955
; CHECK-NEXT: [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
956956
; CHECK-NEXT: ret <2 x i32> [[R]]
957957
;
@@ -973,7 +973,7 @@ define <2 x i32> @lshr_splat_constant0(<2 x i32> %x) {
973973

974974
define <2 x i32> @lshr_splat_constant1(<2 x i32> %x) {
975975
; CHECK-LABEL: @lshr_splat_constant1(
976-
; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i32> [[X:%.*]], <i32 5, i32 0>
976+
; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i32> [[X:%.*]], <i32 5, i32 poison>
977977
; CHECK-NEXT: [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
978978
; CHECK-NEXT: ret <2 x i32> [[R]]
979979
;
@@ -1210,7 +1210,7 @@ entry:
12101210
define <4 x i16> @shl_constant_mask_undef(<4 x i16> %in) {
12111211
; CHECK-LABEL: @shl_constant_mask_undef(
12121212
; CHECK-NEXT: entry:
1213-
; CHECK-NEXT: [[TMP0:%.*]] = shl <4 x i16> [[IN:%.*]], <i16 10, i16 0, i16 0, i16 0>
1213+
; CHECK-NEXT: [[TMP0:%.*]] = shl <4 x i16> [[IN:%.*]], <i16 10, i16 0, i16 poison, i16 poison>
12141214
; CHECK-NEXT: [[SHL:%.*]] = shufflevector <4 x i16> [[TMP0]], <4 x i16> poison, <4 x i32> <i32 0, i32 poison, i32 1, i32 1>
12151215
; CHECK-NEXT: ret <4 x i16> [[SHL]]
12161216
;

llvm/test/Transforms/LoopVectorize/AArch64/deterministic-type-shrinkage.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ define void @test_shrink_zext_in_preheader(ptr noalias %src, ptr noalias %dst, i
164164
; CHECK-NEXT: [[TMP1:%.*]] = insertelement <16 x i16> poison, i16 [[TMP0]], i64 0
165165
; CHECK-NEXT: [[TMP2:%.*]] = insertelement <16 x i16> poison, i16 [[B]], i64 0
166166
; CHECK-NEXT: [[TMP3:%.*]] = mul <16 x i16> [[TMP1]], [[TMP2]]
167-
; CHECK-NEXT: [[TMP4:%.*]] = lshr <16 x i16> [[TMP3]], <i16 8, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0>
168-
; CHECK-NEXT: [[TMP5:%.*]] = trunc <16 x i16> [[TMP4]] to <16 x i8>
167+
; CHECK-NEXT: [[TMP4:%.*]] = lshr <16 x i16> [[TMP3]], <i16 8, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison>
168+
; CHECK-NEXT: [[TMP5:%.*]] = trunc nuw <16 x i16> [[TMP4]] to <16 x i8>
169169
; CHECK-NEXT: [[TMP6:%.*]] = shufflevector <16 x i8> [[TMP5]], <16 x i8> poison, <16 x i32> zeroinitializer
170170
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
171171
; CHECK: vector.body:
@@ -187,8 +187,8 @@ define void @test_shrink_zext_in_preheader(ptr noalias %src, ptr noalias %dst, i
187187
; CHECK-NEXT: [[TMP12:%.*]] = insertelement <8 x i16> poison, i16 [[TMP11]], i64 0
188188
; CHECK-NEXT: [[TMP13:%.*]] = insertelement <8 x i16> poison, i16 [[B]], i64 0
189189
; CHECK-NEXT: [[TMP14:%.*]] = mul <8 x i16> [[TMP12]], [[TMP13]]
190-
; CHECK-NEXT: [[TMP15:%.*]] = lshr <8 x i16> [[TMP14]], <i16 8, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0>
191-
; CHECK-NEXT: [[TMP16:%.*]] = trunc <8 x i16> [[TMP15]] to <8 x i8>
190+
; CHECK-NEXT: [[TMP15:%.*]] = lshr <8 x i16> [[TMP14]], <i16 8, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison>
191+
; CHECK-NEXT: [[TMP16:%.*]] = trunc nuw <8 x i16> [[TMP15]] to <8 x i8>
192192
; CHECK-NEXT: [[TMP17:%.*]] = shufflevector <8 x i8> [[TMP16]], <8 x i8> poison, <8 x i32> zeroinitializer
193193
; CHECK-NEXT: br label [[VEC_EPILOG_VECTOR_BODY:%.*]]
194194
; CHECK: vec.epilog.vector.body:

0 commit comments

Comments
 (0)