[PATCH] D147597: [InstCombine] Fold icmp(Positive1 * X * Y + Positive2) --> icmp(X * Y)
Jun Zhang via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 5 02:18:22 PDT 2023
junaire updated this revision to Diff 511024.
junaire added a comment.
Simplify
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D147597/new/
https://reviews.llvm.org/D147597
Files:
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/icmp.ll
Index: llvm/test/Transforms/InstCombine/icmp.ll
===================================================================
--- llvm/test/Transforms/InstCombine/icmp.ll
+++ llvm/test/Transforms/InstCombine/icmp.ll
@@ -4630,9 +4630,8 @@
define i1 @mul_constant_add_constant_sge(i8 %a, i8 %b) {
; CHECK-LABEL: @mul_constant_add_constant_sge(
-; CHECK-NEXT: [[MUL:%.*]] = mul nsw i8 [[A:%.*]], 42
-; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i8 [[MUL]], [[B:%.*]]
-; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[MUL1]], -1
+; CHECK-NEXT: [[MUL2:%.*]] = mul i8 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[MUL2]], -1
; CHECK-NEXT: ret i1 [[CMP]]
;
%mul = mul nsw i8 %a, 42
@@ -4644,10 +4643,8 @@
define i1 @mul_constant_add_constant_sgt(i8 %a, i8 %b) {
; CHECK-LABEL: @mul_constant_add_constant_sgt(
-; CHECK-NEXT: [[MUL:%.*]] = mul nsw i8 [[A:%.*]], 42
-; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i8 [[MUL]], [[B:%.*]]
-; CHECK-NEXT: [[ADD:%.*]] = or i8 [[MUL1]], 24
-; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[ADD]], 0
+; CHECK-NEXT: [[MUL2:%.*]] = mul i8 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[MUL2]], -1
; CHECK-NEXT: ret i1 [[CMP]]
;
%mul = mul nsw i8 %a, 42
@@ -4659,10 +4656,8 @@
define i1 @mul_constant_add_constant_sle(i8 %a, i8 %b) {
; CHECK-LABEL: @mul_constant_add_constant_sle(
-; CHECK-NEXT: [[MUL:%.*]] = mul nsw i8 [[A:%.*]], 42
-; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i8 [[MUL]], [[B:%.*]]
-; CHECK-NEXT: [[ADD:%.*]] = or i8 [[MUL1]], 24
-; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[ADD]], 1
+; CHECK-NEXT: [[MUL2:%.*]] = mul i8 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[MUL2]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%mul = mul nsw i8 %a, 42
@@ -4674,9 +4669,8 @@
define i1 @mul_constant_add_constant_slt(i8 %a, i8 %b) {
; CHECK-LABEL: @mul_constant_add_constant_slt(
-; CHECK-NEXT: [[MUL:%.*]] = mul nsw i8 [[A:%.*]], 42
-; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i8 [[MUL]], [[B:%.*]]
-; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[MUL1]], 0
+; CHECK-NEXT: [[MUL2:%.*]] = mul i8 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[MUL2]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%mul = mul nsw i8 %a, 42
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -1338,11 +1338,37 @@
if (Instruction *Res = processUGT_ADDCST_ADD(Cmp, A, B, CI2, CI, *this))
return Res;
- // icmp(phi(C1, C2, ...), C) -> phi(icmp(C1, C), icmp(C2, C), ...).
Constant *C = dyn_cast<Constant>(Op1);
if (!C)
return nullptr;
+ // Fold icmp (Positive1 * a * b + Positive2) --> icmp (a * b)
+ if (match(Op0,
+ m_c_Or(m_c_Mul(m_c_Mul(m_Value(A), m_ConstantInt(CI)), m_Value(B)),
+ m_ConstantInt(CI2))) &&
+ match(Op1, m_ZeroInt())) {
+ if (CI->getValue().isStrictlyPositive() &&
+ CI2->getValue().isStrictlyPositive()) {
+ auto Relax = [&](CmpInst::Predicate Pred, Value *A, Value *B) {
+ Value *Mul = Builder.CreateMul(A, B, "mul");
+ Value *NewCmp = Builder.CreateICmp(
+ Pred, Mul, ConstantInt::getNullValue(A->getType()));
+ return replaceInstUsesWith(Cmp, NewCmp);
+ };
+ switch (Pred) {
+ case ICmpInst::ICMP_SGE:
+ case ICmpInst::ICMP_SGT:
+ return Relax(ICmpInst::ICMP_SGE, A, B);
+ case ICmpInst::ICMP_SLT:
+ case ICmpInst::ICMP_SLE:
+ return Relax(ICmpInst::ICMP_SLT, A, B);
+ default:
+ break;
+ }
+ }
+ }
+
+ // icmp(phi(C1, C2, ...), C) -> phi(icmp(C1, C), icmp(C2, C), ...).
if (auto *Phi = dyn_cast<PHINode>(Op0))
if (all_of(Phi->operands(), [](Value *V) { return isa<Constant>(V); })) {
Type *Ty = Cmp.getType();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147597.511024.patch
Type: text/x-patch
Size: 3945 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230405/26f90ec5/attachment.bin>
More information about the llvm-commits
mailing list