From 12d93a27e7b78d58dd00817cb737f273d2dba8ae Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Wed, 12 Aug 2020 21:06:28 +0300 Subject: [PATCH] [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 --- llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp | 6 +++++- llvm/test/Transforms/InstCombine/getelementptr.ll | 4 ++-- llvm/test/Transforms/InstCombine/mul.ll | 6 +++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index 89641704a195..6162c546ff10 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -217,7 +217,11 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) { if (match(&I, m_Mul(m_Value(NewOp), m_Constant(C1)))) { // Replace X*(2^C) with X << C, where C is either a scalar or a vector. - if (Constant *NewCst = getLogBase2(NewOp->getType(), C1)) { + // Note that we need to sanitize undef multipliers to 1, + // to avoid introducing poison. + Constant *SafeC1 = Constant::replaceUndefsWith( + C1, ConstantInt::get(C1->getType()->getScalarType(), 1)); + if (Constant *NewCst = getLogBase2(NewOp->getType(), SafeC1)) { BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst); if (I.hasNoUnsignedWrap()) diff --git a/llvm/test/Transforms/InstCombine/getelementptr.ll b/llvm/test/Transforms/InstCombine/getelementptr.ll index 0257725d12de..17362b088241 100644 --- a/llvm/test/Transforms/InstCombine/getelementptr.ll +++ b/llvm/test/Transforms/InstCombine/getelementptr.ll @@ -216,7 +216,7 @@ define <2 x i1> @test13_vector(<2 x i64> %X, <2 x %S*> %P) nounwind { define <2 x i1> @test13_vector2(i64 %X, <2 x %S*> %P) nounwind { ; CHECK-LABEL: @test13_vector2( ; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <2 x i64> undef, i64 [[X:%.*]], i32 0 -; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i64> [[DOTSPLATINSERT]], +; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i64> [[DOTSPLATINSERT]], ; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <2 x i64> [[TMP1]], ; CHECK-NEXT: [[C:%.*]] = shufflevector <2 x i1> [[TMP2]], <2 x i1> undef, <2 x i32> zeroinitializer ; CHECK-NEXT: ret <2 x i1> [[C]] @@ -231,7 +231,7 @@ define <2 x i1> @test13_vector2(i64 %X, <2 x %S*> %P) nounwind { define <2 x i1> @test13_vector3(i64 %X, <2 x %S*> %P) nounwind { ; CHECK-LABEL: @test13_vector3( ; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <2 x i64> undef, i64 [[X:%.*]], i32 0 -; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i64> [[DOTSPLATINSERT]], +; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i64> [[DOTSPLATINSERT]], ; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <2 x i64> [[TMP1]], ; CHECK-NEXT: [[C:%.*]] = shufflevector <2 x i1> [[TMP2]], <2 x i1> undef, <2 x i32> zeroinitializer ; CHECK-NEXT: ret <2 x i1> [[C]] diff --git a/llvm/test/Transforms/InstCombine/mul.ll b/llvm/test/Transforms/InstCombine/mul.ll index 921c80440147..2e9250fe94c4 100644 --- a/llvm/test/Transforms/InstCombine/mul.ll +++ b/llvm/test/Transforms/InstCombine/mul.ll @@ -908,7 +908,7 @@ define <2 x i32> @mulsub1_vec_nonuniform(<2 x i32> %a0, <2 x i32> %a1) { define <2 x i32> @mulsub1_vec_nonuniform_undef(<2 x i32> %a0, <2 x i32> %a1) { ; CHECK-LABEL: @mulsub1_vec_nonuniform_undef( ; CHECK-NEXT: [[SUB_NEG:%.*]] = sub <2 x i32> [[A0:%.*]], [[A1:%.*]] -; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[SUB_NEG]], +; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[SUB_NEG]], ; CHECK-NEXT: ret <2 x i32> [[MUL]] ; %sub = sub <2 x i32> %a1, %a0 @@ -952,7 +952,7 @@ define <2 x i32> @mulsub2_vec_nonuniform(<2 x i32> %a0) { define <2 x i32> @mulsub2_vec_nonuniform_undef(<2 x i32> %a0) { ; CHECK-LABEL: @mulsub2_vec_nonuniform_undef( ; CHECK-NEXT: [[SUB_NEG:%.*]] = add <2 x i32> [[A0:%.*]], -; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[SUB_NEG]], +; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[SUB_NEG]], ; CHECK-NEXT: ret <2 x i32> [[MUL]] ; %sub = sub <2 x i32> , %a0 @@ -996,7 +996,7 @@ define <2 x i32> @muladd2_vec_nonuniform(<2 x i32> %a0) { define <2 x i32> @muladd2_vec_nonuniform_undef(<2 x i32> %a0) { ; CHECK-LABEL: @muladd2_vec_nonuniform_undef( ; CHECK-NEXT: [[ADD_NEG:%.*]] = sub <2 x i32> , [[A0:%.*]] -; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[ADD_NEG]], +; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[ADD_NEG]], ; CHECK-NEXT: ret <2 x i32> [[MUL]] ; %add = add <2 x i32> %a0,