[InstCombine] Fold (sext bool X) * (sext bool X) to zext (and X, X)

InstCombine didn't perform (sext bool X) * (sext bool X) --> zext (and X, X) which can result in just (zext X). The patch adds regression tests to check this transformation and adds a check for equality of mul's operands for that case.

Differential Revision: https://reviews.llvm.org/D104193
This commit is contained in:
Daniil Seredkin 2021-06-02 11:55:38 +07:00
parent 07bbfd9c13
commit 6643e51d79
2 changed files with 3 additions and 4 deletions

View File

@ -328,7 +328,7 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
if (((match(Op0, m_ZExt(m_Value(X))) && match(Op1, m_ZExt(m_Value(Y)))) ||
(match(Op0, m_SExt(m_Value(X))) && match(Op1, m_SExt(m_Value(Y))))) &&
X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType() &&
(Op0->hasOneUse() || Op1->hasOneUse())) {
(Op0->hasOneUse() || Op1->hasOneUse() || X == Y)) {
Value *And = Builder.CreateAnd(X, Y, "mulbool");
return CastInst::Create(Instruction::ZExt, And, I.getType());
}

View File

@ -261,8 +261,7 @@ define i32 @mul_bools_sext_one_use_per_op(i1 %x, i1 %y) {
define i32 @mul_bool_sext_one_user(i1 %x) {
; CHECK-LABEL: @mul_bool_sext_one_user(
; CHECK-NEXT: [[SX:%.*]] = sext i1 [[X:%.*]] to i32
; CHECK-NEXT: [[R:%.*]] = mul nsw i32 [[SX]], [[SX]]
; CHECK-NEXT: [[R:%.*]] = zext i1 [[X:%.*]] to i32
; CHECK-NEXT: ret i32 [[R]]
;
%sx = sext i1 %x to i32
@ -296,7 +295,7 @@ define i32 @mul_bool_sext_one_extra_user(i1 %x) {
; CHECK-LABEL: @mul_bool_sext_one_extra_user(
; CHECK-NEXT: [[SX:%.*]] = sext i1 [[X:%.*]] to i32
; CHECK-NEXT: call void @use32(i32 [[SX]])
; CHECK-NEXT: [[R:%.*]] = mul nsw i32 [[SX]], [[SX]]
; CHECK-NEXT: [[R:%.*]] = zext i1 [[X]] to i32
; CHECK-NEXT: ret i32 [[R]]
;
%sx = sext i1 %x to i32