[llvm] c59ea32 - [InstCombine] Canonicalize `icmp pred (X +/- C1), C2` into `icmp pred X, C2 -/+ C1` with nowrap flag implied by with.overflow intrinsic (#75511)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Dec 16 01:59:01 PST 2023
Author: Yingwei Zheng
Date: 2023-12-16T17:58:57+08:00
New Revision: c59ea32f82128f550b471ed96b7ac093ff448c60
URL: https://github.com/llvm/llvm-project/commit/c59ea32f82128f550b471ed96b7ac093ff448c60
DIFF: https://github.com/llvm/llvm-project/commit/c59ea32f82128f550b471ed96b7ac093ff448c60.diff
LOG: [InstCombine] Canonicalize `icmp pred (X +/- C1), C2` into `icmp pred X, C2 -/+ C1` with nowrap flag implied by with.overflow intrinsic (#75511)
This patch tries to canonicalize the pattern `Overflow | icmp pred Res,
C2` into `Overflow | icmp pred X, C2 +/- C1`, where `Overflow` and `Res`
are return values of `xxx.with.overflow X, C1`.
Alive2: https://alive2.llvm.org/ce/z/PhR_3S
Fixes #75360.
Added:
llvm/test/Transforms/InstCombine/canonicalize-or-with-overflow-icmp.ll
Modified:
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 31db1d3164b772..5e362f4117d051 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3756,6 +3756,35 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
}
}
+ /// Res, Overflow = xxx_with_overflow X, C1
+ /// Try to canonicalize the pattern "Overflow | icmp pred Res, C2" into
+ /// "Overflow | icmp pred X, C2 +/- C1".
+ const WithOverflowInst *WO;
+ const Value *WOV;
+ const APInt *C1, *C2;
+ if (match(&I, m_c_Or(m_CombineAnd(m_ExtractValue<1>(m_CombineAnd(
+ m_WithOverflowInst(WO), m_Value(WOV))),
+ m_Value(Ov)),
+ m_OneUse(m_ICmp(Pred, m_ExtractValue<0>(m_Deferred(WOV)),
+ m_APInt(C2))))) &&
+ (WO->getBinaryOp() == Instruction::Add ||
+ WO->getBinaryOp() == Instruction::Sub) &&
+ (ICmpInst::isEquality(Pred) ||
+ WO->isSigned() == ICmpInst::isSigned(Pred)) &&
+ match(WO->getRHS(), m_APInt(C1))) {
+ bool Overflow;
+ APInt NewC = WO->getBinaryOp() == Instruction::Add
+ ? (ICmpInst::isSigned(Pred) ? C2->ssub_ov(*C1, Overflow)
+ : C2->usub_ov(*C1, Overflow))
+ : (ICmpInst::isSigned(Pred) ? C2->sadd_ov(*C1, Overflow)
+ : C2->uadd_ov(*C1, Overflow));
+ if (!Overflow || ICmpInst::isEquality(Pred)) {
+ Value *NewCmp = Builder.CreateICmp(
+ Pred, WO->getLHS(), ConstantInt::get(WO->getLHS()->getType(), NewC));
+ return BinaryOperator::CreateOr(Ov, NewCmp);
+ }
+ }
+
// (~x) | y --> ~(x & (~y)) iff that gets rid of inversions
if (sinkNotIntoOtherHandOfLogicalOp(I))
return &I;
diff --git a/llvm/test/Transforms/InstCombine/canonicalize-or-with-overflow-icmp.ll b/llvm/test/Transforms/InstCombine/canonicalize-or-with-overflow-icmp.ll
new file mode 100644
index 00000000000000..2e801489ef4f13
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/canonicalize-or-with-overflow-icmp.ll
@@ -0,0 +1,275 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+declare { i32, i1 } @llvm.sadd.with.overflow.i32(i32, i32)
+declare { i32, i1 } @llvm.ssub.with.overflow.i32(i32, i32)
+declare { i32, i1 } @llvm.smul.with.overflow.i32(i32, i32)
+declare { i32, i1 } @llvm.uadd.with.overflow.i32(i32, i32)
+
+declare void @use(i1)
+
+; Tests from PR75360
+define i1 @ckd_add_unsigned(i31 %num) {
+; CHECK-LABEL: define i1 @ckd_add_unsigned(
+; CHECK-SAME: i31 [[NUM:%.*]]) {
+; CHECK-NEXT: [[A2:%.*]] = icmp eq i31 [[NUM]], -1
+; CHECK-NEXT: ret i1 [[A2]]
+;
+ %a0 = zext i31 %num to i32
+ %a1 = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a0, i32 1)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp slt i32 %a3, 0
+ %a5 = or i1 %a2, %a4
+ ret i1 %a5
+}
+
+define i1 @ckd_add_unsigned_commuted(i31 %num) {
+; CHECK-LABEL: define i1 @ckd_add_unsigned_commuted(
+; CHECK-SAME: i31 [[NUM:%.*]]) {
+; CHECK-NEXT: [[A2:%.*]] = icmp eq i31 [[NUM]], -1
+; CHECK-NEXT: ret i1 [[A2]]
+;
+ %a0 = zext i31 %num to i32
+ %a1 = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a0, i32 1)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp slt i32 %a3, 0
+ %a5 = or i1 %a4, %a2
+ ret i1 %a5
+}
+
+define i1 @ckd_add_unsigned_imply_true(i31 %num) {
+; CHECK-LABEL: define i1 @ckd_add_unsigned_imply_true(
+; CHECK-SAME: i31 [[NUM:%.*]]) {
+; CHECK-NEXT: ret i1 true
+;
+ %a0 = zext i31 %num to i32
+ %a1 = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a0, i32 1)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp sgt i32 %a3, -1
+ %a5 = or i1 %a2, %a4
+ ret i1 %a5
+}
+
+define i1 @canonicalize_or_sadd_with_overflow_icmp(i32 %a0) {
+; CHECK-LABEL: define i1 @canonicalize_or_sadd_with_overflow_icmp(
+; CHECK-SAME: i32 [[A0:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[A0]], -2147483647
+; CHECK-NEXT: [[A5:%.*]] = icmp sgt i32 [[TMP1]], -1
+; CHECK-NEXT: ret i1 [[A5]]
+;
+ %a1 = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a0, i32 1)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp slt i32 %a3, 0
+ %a5 = or i1 %a2, %a4
+ ret i1 %a5
+}
+
+define i1 @canonicalize_or_ssub_with_overflow_icmp(i32 %a0) {
+; CHECK-LABEL: define i1 @canonicalize_or_ssub_with_overflow_icmp(
+; CHECK-SAME: i32 [[A0:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[A0]], 1
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %a1 = tail call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 %a0, i32 1)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp slt i32 %a3, 0
+ %a5 = or i1 %a2, %a4
+ ret i1 %a5
+}
+
+define i1 @canonicalize_or_uadd_with_overflow_icmp(i32 %a0) {
+; CHECK-LABEL: define i1 @canonicalize_or_uadd_with_overflow_icmp(
+; CHECK-SAME: i32 [[A0:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[A0]], 1
+; CHECK-NEXT: [[A5:%.*]] = icmp ult i32 [[TMP1]], 10
+; CHECK-NEXT: ret i1 [[A5]]
+;
+ %a1 = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %a0, i32 1)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp ult i32 %a3, 10
+ %a5 = or i1 %a2, %a4
+ ret i1 %a5
+}
+
+define i1 @canonicalize_or_sadd_with_overflow_icmp_eq(i32 %a0) {
+; CHECK-LABEL: define i1 @canonicalize_or_sadd_with_overflow_icmp_eq(
+; CHECK-SAME: i32 [[A0:%.*]]) {
+; CHECK-NEXT: [[A2:%.*]] = icmp eq i32 [[A0]], 2147483647
+; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[A0]], 9
+; CHECK-NEXT: [[A5:%.*]] = or i1 [[A2]], [[TMP1]]
+; CHECK-NEXT: ret i1 [[A5]]
+;
+ %a1 = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a0, i32 1)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp eq i32 %a3, 10
+ %a5 = or i1 %a2, %a4
+ ret i1 %a5
+}
+
+define i1 @canonicalize_or_uadd_with_overflow_icmp_ne(i32 %a0) {
+; CHECK-LABEL: define i1 @canonicalize_or_uadd_with_overflow_icmp_ne(
+; CHECK-SAME: i32 [[A0:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[A0]], 9
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %a1 = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %a0, i32 1)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp ne i32 %a3, 10
+ %a5 = or i1 %a2, %a4
+ ret i1 %a5
+}
+
+; Negative tests
+define i1 @canonicalize_or_sadd_with_overflow_icmp_mismatched_pred(i32 %a0) {
+; CHECK-LABEL: define i1 @canonicalize_or_sadd_with_overflow_icmp_mismatched_pred(
+; CHECK-SAME: i32 [[A0:%.*]]) {
+; CHECK-NEXT: [[A1:%.*]] = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[A0]], i32 1)
+; CHECK-NEXT: [[A2:%.*]] = extractvalue { i32, i1 } [[A1]], 1
+; CHECK-NEXT: [[A3:%.*]] = extractvalue { i32, i1 } [[A1]], 0
+; CHECK-NEXT: [[A4:%.*]] = icmp ult i32 [[A3]], 2
+; CHECK-NEXT: [[A5:%.*]] = or i1 [[A2]], [[A4]]
+; CHECK-NEXT: ret i1 [[A5]]
+;
+ %a1 = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a0, i32 1)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp ult i32 %a3, 2
+ %a5 = or i1 %a2, %a4
+ ret i1 %a5
+}
+
+define i1 @canonicalize_or_sadd_with_overflow_icmp_non_constant1(i32 %a0, i32 %c) {
+; CHECK-LABEL: define i1 @canonicalize_or_sadd_with_overflow_icmp_non_constant1(
+; CHECK-SAME: i32 [[A0:%.*]], i32 [[C:%.*]]) {
+; CHECK-NEXT: [[A1:%.*]] = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[A0]], i32 [[C]])
+; CHECK-NEXT: [[A2:%.*]] = extractvalue { i32, i1 } [[A1]], 1
+; CHECK-NEXT: [[A3:%.*]] = extractvalue { i32, i1 } [[A1]], 0
+; CHECK-NEXT: [[A4:%.*]] = icmp slt i32 [[A3]], 0
+; CHECK-NEXT: [[A5:%.*]] = or i1 [[A2]], [[A4]]
+; CHECK-NEXT: ret i1 [[A5]]
+;
+ %a1 = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a0, i32 %c)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp slt i32 %a3, 0
+ %a5 = or i1 %a2, %a4
+ ret i1 %a5
+}
+
+define i1 @canonicalize_or_sadd_with_overflow_icmp_non_constant2(i32 %a0, i32 %c) {
+; CHECK-LABEL: define i1 @canonicalize_or_sadd_with_overflow_icmp_non_constant2(
+; CHECK-SAME: i32 [[A0:%.*]], i32 [[C:%.*]]) {
+; CHECK-NEXT: [[A1:%.*]] = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[A0]], i32 1)
+; CHECK-NEXT: [[A2:%.*]] = extractvalue { i32, i1 } [[A1]], 1
+; CHECK-NEXT: [[A3:%.*]] = extractvalue { i32, i1 } [[A1]], 0
+; CHECK-NEXT: [[A4:%.*]] = icmp slt i32 [[A3]], [[C]]
+; CHECK-NEXT: [[A5:%.*]] = or i1 [[A2]], [[A4]]
+; CHECK-NEXT: ret i1 [[A5]]
+;
+ %a1 = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a0, i32 1)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp slt i32 %a3, %c
+ %a5 = or i1 %a2, %a4
+ ret i1 %a5
+}
+
+define i1 @canonicalize_or_sadd_with_overflow_icmp_multiuse(i32 %a0) {
+; CHECK-LABEL: define i1 @canonicalize_or_sadd_with_overflow_icmp_multiuse(
+; CHECK-SAME: i32 [[A0:%.*]]) {
+; CHECK-NEXT: [[A1:%.*]] = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[A0]], i32 1)
+; CHECK-NEXT: [[A2:%.*]] = extractvalue { i32, i1 } [[A1]], 1
+; CHECK-NEXT: [[A3:%.*]] = extractvalue { i32, i1 } [[A1]], 0
+; CHECK-NEXT: [[A4:%.*]] = icmp slt i32 [[A3]], 0
+; CHECK-NEXT: call void @use(i1 [[A4]])
+; CHECK-NEXT: [[A5:%.*]] = or i1 [[A2]], [[A4]]
+; CHECK-NEXT: ret i1 [[A5]]
+;
+ %a1 = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a0, i32 1)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp slt i32 %a3, 0
+ call void @use(i1 %a4)
+ %a5 = or i1 %a2, %a4
+ ret i1 %a5
+}
+
+define i1 @canonicalize_or_sadd_with_overflow_icmp_overflow(i32 %a0) {
+; CHECK-LABEL: define i1 @canonicalize_or_sadd_with_overflow_icmp_overflow(
+; CHECK-SAME: i32 [[A0:%.*]]) {
+; CHECK-NEXT: [[A1:%.*]] = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[A0]], i32 -2147483647)
+; CHECK-NEXT: [[A2:%.*]] = extractvalue { i32, i1 } [[A1]], 1
+; CHECK-NEXT: [[A3:%.*]] = extractvalue { i32, i1 } [[A1]], 0
+; CHECK-NEXT: [[A4:%.*]] = icmp slt i32 [[A3]], 2
+; CHECK-NEXT: [[A5:%.*]] = or i1 [[A2]], [[A4]]
+; CHECK-NEXT: ret i1 [[A5]]
+;
+ %a1 = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a0, i32 -2147483647)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp slt i32 %a3, 2
+ %a5 = or i1 %a2, %a4
+ ret i1 %a5
+}
+
+define i1 @canonicalize_or_uadd_with_overflow_icmp_overflow(i32 %a0) {
+; CHECK-LABEL: define i1 @canonicalize_or_uadd_with_overflow_icmp_overflow(
+; CHECK-SAME: i32 [[A0:%.*]]) {
+; CHECK-NEXT: [[A1:%.*]] = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[A0]], i32 3)
+; CHECK-NEXT: [[A2:%.*]] = extractvalue { i32, i1 } [[A1]], 1
+; CHECK-NEXT: [[A3:%.*]] = extractvalue { i32, i1 } [[A1]], 0
+; CHECK-NEXT: [[A4:%.*]] = icmp ult i32 [[A3]], 2
+; CHECK-NEXT: [[A5:%.*]] = or i1 [[A2]], [[A4]]
+; CHECK-NEXT: ret i1 [[A5]]
+;
+ %a1 = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %a0, i32 3)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp ult i32 %a3, 2
+ %a5 = or i1 %a2, %a4
+ ret i1 %a5
+}
+
+define i1 @canonicalize_or_ssub_with_overflow_icmp_overflow(i32 %a0) {
+; CHECK-LABEL: define i1 @canonicalize_or_ssub_with_overflow_icmp_overflow(
+; CHECK-SAME: i32 [[A0:%.*]]) {
+; CHECK-NEXT: [[A1:%.*]] = tail call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 [[A0]], i32 -2147483648)
+; CHECK-NEXT: [[A2:%.*]] = extractvalue { i32, i1 } [[A1]], 1
+; CHECK-NEXT: [[A3:%.*]] = extractvalue { i32, i1 } [[A1]], 0
+; CHECK-NEXT: [[A4:%.*]] = icmp slt i32 [[A3]], -1
+; CHECK-NEXT: [[A5:%.*]] = or i1 [[A2]], [[A4]]
+; CHECK-NEXT: ret i1 [[A5]]
+;
+ %a1 = tail call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 %a0, i32 -2147483648)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp slt i32 %a3, -1
+ %a5 = or i1 %a2, %a4
+ ret i1 %a5
+}
+
+define i1 @canonicalize_or_smul_with_overflow_icmp(i32 %a0) {
+; CHECK-LABEL: define i1 @canonicalize_or_smul_with_overflow_icmp(
+; CHECK-SAME: i32 [[A0:%.*]]) {
+; CHECK-NEXT: [[A1:%.*]] = tail call { i32, i1 } @llvm.smul.with.overflow.i32(i32 [[A0]], i32 3)
+; CHECK-NEXT: [[A2:%.*]] = extractvalue { i32, i1 } [[A1]], 1
+; CHECK-NEXT: [[A3:%.*]] = extractvalue { i32, i1 } [[A1]], 0
+; CHECK-NEXT: [[A4:%.*]] = icmp slt i32 [[A3]], 10
+; CHECK-NEXT: [[A5:%.*]] = or i1 [[A2]], [[A4]]
+; CHECK-NEXT: ret i1 [[A5]]
+;
+ %a1 = tail call { i32, i1 } @llvm.smul.with.overflow.i32(i32 %a0, i32 3)
+ %a2 = extractvalue { i32, i1 } %a1, 1
+ %a3 = extractvalue { i32, i1 } %a1, 0
+ %a4 = icmp slt i32 %a3, 10
+ %a5 = or i1 %a2, %a4
+ ret i1 %a5
+}
More information about the llvm-commits
mailing list