Skip to content

Commit 12d93a2

Browse files
committed
[InstCombine] Sanitize undef vector constant to 1 in X*(2^C) with X << C (PR47133)
While x*undef is undef, shift-by-undef is poison, which we must avoid introducing. Also log2(iN undef) is *NOT* iN undef, because log2(iN undef) u< N. See https://bugs.llvm.org/show_bug.cgi?id=47133
1 parent c561f4d commit 12d93a2

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,11 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
217217

218218
if (match(&I, m_Mul(m_Value(NewOp), m_Constant(C1)))) {
219219
// Replace X*(2^C) with X << C, where C is either a scalar or a vector.
220-
if (Constant *NewCst = getLogBase2(NewOp->getType(), C1)) {
220+
// Note that we need to sanitize undef multipliers to 1,
221+
// to avoid introducing poison.
222+
Constant *SafeC1 = Constant::replaceUndefsWith(
223+
C1, ConstantInt::get(C1->getType()->getScalarType(), 1));
224+
if (Constant *NewCst = getLogBase2(NewOp->getType(), SafeC1)) {
221225
BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst);
222226

223227
if (I.hasNoUnsignedWrap())

llvm/test/Transforms/InstCombine/getelementptr.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ define <2 x i1> @test13_vector(<2 x i64> %X, <2 x %S*> %P) nounwind {
216216
define <2 x i1> @test13_vector2(i64 %X, <2 x %S*> %P) nounwind {
217217
; CHECK-LABEL: @test13_vector2(
218218
; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <2 x i64> undef, i64 [[X:%.*]], i32 0
219-
; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i64> [[DOTSPLATINSERT]], <i64 2, i64 undef>
219+
; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i64> [[DOTSPLATINSERT]], <i64 2, i64 0>
220220
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <2 x i64> [[TMP1]], <i64 -4, i64 undef>
221221
; CHECK-NEXT: [[C:%.*]] = shufflevector <2 x i1> [[TMP2]], <2 x i1> undef, <2 x i32> zeroinitializer
222222
; CHECK-NEXT: ret <2 x i1> [[C]]
@@ -231,7 +231,7 @@ define <2 x i1> @test13_vector2(i64 %X, <2 x %S*> %P) nounwind {
231231
define <2 x i1> @test13_vector3(i64 %X, <2 x %S*> %P) nounwind {
232232
; CHECK-LABEL: @test13_vector3(
233233
; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <2 x i64> undef, i64 [[X:%.*]], i32 0
234-
; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i64> [[DOTSPLATINSERT]], <i64 2, i64 undef>
234+
; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i64> [[DOTSPLATINSERT]], <i64 2, i64 0>
235235
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <2 x i64> [[TMP1]], <i64 4, i64 undef>
236236
; CHECK-NEXT: [[C:%.*]] = shufflevector <2 x i1> [[TMP2]], <2 x i1> undef, <2 x i32> zeroinitializer
237237
; CHECK-NEXT: ret <2 x i1> [[C]]

llvm/test/Transforms/InstCombine/mul.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ define <2 x i32> @mulsub1_vec_nonuniform(<2 x i32> %a0, <2 x i32> %a1) {
908908
define <2 x i32> @mulsub1_vec_nonuniform_undef(<2 x i32> %a0, <2 x i32> %a1) {
909909
; CHECK-LABEL: @mulsub1_vec_nonuniform_undef(
910910
; CHECK-NEXT: [[SUB_NEG:%.*]] = sub <2 x i32> [[A0:%.*]], [[A1:%.*]]
911-
; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[SUB_NEG]], <i32 2, i32 undef>
911+
; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[SUB_NEG]], <i32 2, i32 0>
912912
; CHECK-NEXT: ret <2 x i32> [[MUL]]
913913
;
914914
%sub = sub <2 x i32> %a1, %a0
@@ -952,7 +952,7 @@ define <2 x i32> @mulsub2_vec_nonuniform(<2 x i32> %a0) {
952952
define <2 x i32> @mulsub2_vec_nonuniform_undef(<2 x i32> %a0) {
953953
; CHECK-LABEL: @mulsub2_vec_nonuniform_undef(
954954
; CHECK-NEXT: [[SUB_NEG:%.*]] = add <2 x i32> [[A0:%.*]], <i32 -16, i32 -32>
955-
; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[SUB_NEG]], <i32 2, i32 undef>
955+
; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[SUB_NEG]], <i32 2, i32 0>
956956
; CHECK-NEXT: ret <2 x i32> [[MUL]]
957957
;
958958
%sub = sub <2 x i32> <i32 16, i32 32>, %a0
@@ -996,7 +996,7 @@ define <2 x i32> @muladd2_vec_nonuniform(<2 x i32> %a0) {
996996
define <2 x i32> @muladd2_vec_nonuniform_undef(<2 x i32> %a0) {
997997
; CHECK-LABEL: @muladd2_vec_nonuniform_undef(
998998
; CHECK-NEXT: [[ADD_NEG:%.*]] = sub <2 x i32> <i32 -16, i32 -32>, [[A0:%.*]]
999-
; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[ADD_NEG]], <i32 2, i32 undef>
999+
; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[ADD_NEG]], <i32 2, i32 0>
10001000
; CHECK-NEXT: ret <2 x i32> [[MUL]]
10011001
;
10021002
%add = add <2 x i32> %a0, <i32 16, i32 32>

0 commit comments

Comments
 (0)