Skip to content

Commit 4af7e77

Browse files
committed
[InstCombine] Optimize x * !x to 0 for vector #84608
1 parent 6e27dd4 commit 4af7e77

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,22 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
198198
if (SimplifyAssociativeOrCommutative(I))
199199
return &I;
200200

201+
// mul (sext (icmp eq x, 0)), x -> 0
202+
Value *SExtOp, *V0;
203+
if ((match(Op0, m_SExt(m_Value(SExtOp))) && match(Op1, m_Value(V0))) ||
204+
(match(Op1, m_SExt(m_Value(SExtOp))) && match(Op0, m_Value(V0)))) {
205+
Constant *CV;
206+
ICmpInst::Predicate Pred;
207+
Value *V1;
208+
const APInt *IV;
209+
if ((match(SExtOp, m_ICmp(Pred, m_Value(V1), m_Constant(CV))) ||
210+
match(SExtOp, m_ICmp(Pred, m_Constant(CV), m_Value(V1)))) &&
211+
Pred == ICmpInst::Predicate::ICMP_EQ && match(CV, m_APInt(IV)) &&
212+
*IV == 0 && V0 == V1) {
213+
return replaceInstUsesWith(I, CV);
214+
}
215+
}
216+
201217
if (Instruction *X = foldVectorBinop(I))
202218
return X;
203219

llvm/test/Transforms/InstCombine/mul.ll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,3 +2049,13 @@ define i32 @zext_negpow2_use(i8 %x) {
20492049
%r = mul i32 %zx, -16777216 ; -1 << 24
20502050
ret i32 %r
20512051
}
2052+
2053+
define <4 x i32> @mul_icmp_with_zero(<4 x i32> %x) {
2054+
; CHECK-LABEL: @mul_icmp_with_zero(
2055+
; CHECK-NEXT: ret <4 x i32> zeroinitializer
2056+
;
2057+
%cmp = icmp eq <4 x i32> %x, zeroinitializer
2058+
%sext = sext <4 x i1> %cmp to <4 x i32>
2059+
%mul = mul <4 x i32> %sext, %x
2060+
ret <4 x i32> %mul
2061+
}

0 commit comments

Comments
 (0)