[llvm] 4458973 - [InstCombine] fold mul of zext/sext bools to 'and'
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Sun Jul 12 12:58:18 PDT 2020
Author: Sanjay Patel
Date: 2020-07-12T15:56:26-04:00
New Revision: 445897334741c53e98f8044f5f33ab1e888b3818
URL: https://github.com/llvm/llvm-project/commit/445897334741c53e98f8044f5f33ab1e888b3818
DIFF: https://github.com/llvm/llvm-project/commit/445897334741c53e98f8044f5f33ab1e888b3818.diff
LOG: [InstCombine] fold mul of zext/sext bools to 'and'
Similar to rG40fcc42:
The base case only worked because we were relying on a
poison-unsafe select transform; if that is fixed, we
would regress on patterns like this.
The extra use tests show that the select transform can't
be applied consistently. So it may be a regression to have
an extra instruction on 1 test, but that result was not
created safely and does not happen reliably.
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/test/Transforms/InstCombine/mul.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 2965103d4029..c6233a68847d 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -376,6 +376,16 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) {
Value *And = Builder.CreateAnd(X, Y, "mulbool");
return CastInst::Create(Instruction::ZExt, And, I.getType());
}
+ // (sext bool X) * (zext bool Y) --> sext (and X, Y)
+ // (zext bool X) * (sext bool Y) --> sext (and X, Y)
+ // Note: -1 * 1 == 1 * -1 == -1
+ if (((match(Op0, m_SExt(m_Value(X))) && match(Op1, m_ZExt(m_Value(Y)))) ||
+ (match(Op0, m_ZExt(m_Value(X))) && match(Op1, m_SExt(m_Value(Y))))) &&
+ X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType() &&
+ (Op0->hasOneUse() || Op1->hasOneUse())) {
+ Value *And = Builder.CreateAnd(X, Y, "mulbool");
+ return CastInst::Create(Instruction::SExt, And, I.getType());
+ }
// (bool X) * Y --> X ? Y : 0
// Y * (bool X) --> X ? Y : 0
diff --git a/llvm/test/Transforms/InstCombine/mul.ll b/llvm/test/Transforms/InstCombine/mul.ll
index 9d1b8ad457e4..059b18d30b90 100644
--- a/llvm/test/Transforms/InstCombine/mul.ll
+++ b/llvm/test/Transforms/InstCombine/mul.ll
@@ -247,8 +247,8 @@ define i32 @mul_bools_sext_use3(i1 %x, i1 %y) {
define <3 x i32> @mul_bools_mixed_ext(<3 x i1> %x, <3 x i1> %y) {
; CHECK-LABEL: @mul_bools_mixed_ext(
-; CHECK-NEXT: [[NARROW:%.*]] = and <3 x i1> [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[R:%.*]] = sext <3 x i1> [[NARROW]] to <3 x i32>
+; CHECK-NEXT: [[MULBOOL:%.*]] = and <3 x i1> [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = sext <3 x i1> [[MULBOOL]] to <3 x i32>
; CHECK-NEXT: ret <3 x i32> [[R]]
;
%zx = zext <3 x i1> %x to <3 x i32>
@@ -261,8 +261,8 @@ define i32 @mul_bools_mixed_ext_use1(i1 %x, i1 %y) {
; CHECK-LABEL: @mul_bools_mixed_ext_use1(
; CHECK-NEXT: [[ZY:%.*]] = zext i1 [[Y:%.*]] to i32
; CHECK-NEXT: call void @use32(i32 [[ZY]])
-; CHECK-NEXT: [[NARROW:%.*]] = and i1 [[Y]], [[X:%.*]]
-; CHECK-NEXT: [[R:%.*]] = sext i1 [[NARROW]] to i32
+; CHECK-NEXT: [[MULBOOL:%.*]] = and i1 [[X:%.*]], [[Y]]
+; CHECK-NEXT: [[R:%.*]] = sext i1 [[MULBOOL]] to i32
; CHECK-NEXT: ret i32 [[R]]
;
%sx = sext i1 %x to i32
@@ -276,7 +276,8 @@ define i32 @mul_bools_mixed_ext_use2(i1 %x, i1 %y) {
; CHECK-LABEL: @mul_bools_mixed_ext_use2(
; CHECK-NEXT: [[SY:%.*]] = sext i1 [[Y:%.*]] to i32
; CHECK-NEXT: call void @use32(i32 [[SY]])
-; CHECK-NEXT: [[R:%.*]] = select i1 [[X:%.*]], i32 [[SY]], i32 0
+; CHECK-NEXT: [[MULBOOL:%.*]] = and i1 [[Y]], [[X:%.*]]
+; CHECK-NEXT: [[R:%.*]] = sext i1 [[MULBOOL]] to i32
; CHECK-NEXT: ret i32 [[R]]
;
%zx = zext i1 %x to i32
More information about the llvm-commits
mailing list