Skip to content

Commit 522eeb6

Browse files
LebedevRIzmodem
authored andcommitted
[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 (cherry picked from commit 12d93a2)
1 parent 529b222 commit 522eeb6

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

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

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

222226
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]]

0 commit comments

Comments
 (0)