[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 04:26:12 PDT 2023
junaire updated this revision to Diff 511046.
junaire added a comment.
Rework
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
@@ -4631,10 +4631,10 @@
define i1 @mul_add_constant_sgt(i8 %a, i8 %b) {
; CHECK-LABEL: @mul_add_constant_sgt(
; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i8 [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT: [[ADD:%.*]] = or i8 [[MUL1]], 24
-; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[ADD]], 0
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[MUL1]], -1
; CHECK-NEXT: ret i1 [[CMP]]
;
+
%mul1 = mul nsw i8 %a, %b
%add = or i8 %mul1, 24
%cmp = icmp sgt i8 %add, 0
@@ -4655,8 +4655,7 @@
define i1 @mul_add_constant_sle(i8 %a, i8 %b) {
; CHECK-LABEL: @mul_add_constant_sle(
; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i8 [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT: [[ADD:%.*]] = or i8 [[MUL1]], 24
-; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[ADD]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[MUL1]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%mul1 = mul nsw i8 %a, %b
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -1338,11 +1338,34 @@
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;
+ BinaryOperator *BO;
+ const APInt *LHS, *RHS;
+ if (match(Op0, m_c_Or(m_BinOp(BO), m_APInt(LHS))) &&
+ match(Op1, m_APInt(RHS))) {
+ if (LHS->isStrictlyPositive() && RHS->isNonNegative()) {
+ auto Relax = [&](ICmpInst::Predicate Pred, Value *X) {
+ Value *NewCmp = Builder.CreateICmp(
+ Pred, X, ConstantInt::getNullValue(X->getType()));
+ return replaceInstUsesWith(Cmp, NewCmp);
+ };
+ switch (Pred) {
+ case ICmpInst::ICMP_SLE:
+ case ICmpInst::ICMP_SLT:
+ return Relax(ICmpInst::ICMP_SLT, BO);
+ case ICmpInst::ICMP_SGE:
+ case ICmpInst::ICMP_SGT:
+ return Relax(ICmpInst::ICMP_SGE, BO);
+ 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.511046.patch
Type: text/x-patch
Size: 2552 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230405/525c2677/attachment.bin>
More information about the llvm-commits
mailing list