[llvm] [ValueTracking] Augment isImpliedByDomCondition by data-relation (PR #187224)

Kunqiu Chen via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 18 08:09:57 PDT 2026


https://github.com/Camsyn updated https://github.com/llvm/llvm-project/pull/187224

>From 2b3fbcd8abe4d85f25f4a26bbbaf23885361fffe Mon Sep 17 00:00:00 2001
From: Camsyn <camsyn at foxmail.com>
Date: Tue, 17 Mar 2026 22:26:40 +0800
Subject: [PATCH 1/9] Pre-commit tests

---
 llvm/test/Transforms/InstCombine/div.ll       |  32 +
 .../sub-after-sle-is-non-negative.ll          |  62 ++
 llvm/test/Transforms/InstSimplify/implies.ll  | 793 ++++++++++++++++++
 3 files changed, 887 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/div.ll b/llvm/test/Transforms/InstCombine/div.ll
index fa8e9c9723e4e..5cb4b86aa0380 100644
--- a/llvm/test/Transforms/InstCombine/div.ll
+++ b/llvm/test/Transforms/InstCombine/div.ll
@@ -3,6 +3,8 @@
 ; RUN: opt < %s -passes=instcombine -use-constant-int-for-fixed-length-splat -S | FileCheck %s
 
 declare void @use(i32)
+declare i64 @llvm.smin.i64(i64, i64)
+declare i64 @llvm.smax.i64(i64, i64)
 
 define i32 @test1(i32 %A) {
 ; CHECK-LABEL: @test1(
@@ -379,6 +381,36 @@ define i32 @test26(i32 %a) {
   ret i32 %div
 }
 
+define i64 @sdiv_shl_nonneg_from_sub_smin(i64 %a, i64 %b) {
+; CHECK-LABEL: @sdiv_shl_nonneg_from_sub_smin(
+; CHECK-NEXT:    [[M:%.*]] = call i64 @llvm.smin.i64(i64 [[B:%.*]], i64 [[A:%.*]])
+; CHECK-NEXT:    [[X:%.*]] = sub nsw i64 [[A]], [[M]]
+; CHECK-NEXT:    [[D:%.*]] = sdiv i64 [[X]], 4
+; CHECK-NEXT:    [[R:%.*]] = shl nsw i64 [[D]], 2
+; CHECK-NEXT:    ret i64 [[R]]
+;
+  %m = call i64 @llvm.smin.i64(i64 %b, i64 %a)
+  %x = sub nsw i64 %a, %m
+  %d = sdiv i64 %x, 4
+  %s = shl nsw i64 %d, 2
+  ret i64 %s
+}
+
+define i64 @sdiv_shl_nonneg_from_sub_smax(i64 %a, i64 %b) {
+; CHECK-LABEL: @sdiv_shl_nonneg_from_sub_smax(
+; CHECK-NEXT:    [[M:%.*]] = call i64 @llvm.smax.i64(i64 [[A:%.*]], i64 [[B:%.*]])
+; CHECK-NEXT:    [[X:%.*]] = sub nsw i64 [[M]], [[A]]
+; CHECK-NEXT:    [[D:%.*]] = sdiv i64 [[X]], 4
+; CHECK-NEXT:    [[R:%.*]] = shl nsw i64 [[D]], 2
+; CHECK-NEXT:    ret i64 [[R]]
+;
+  %m = call i64 @llvm.smax.i64(i64 %a, i64 %b)
+  %x = sub nsw i64 %m, %a
+  %d = sdiv i64 %x, 4
+  %s = shl nsw i64 %d, 2
+  ret i64 %s
+}
+
 define i32 @test27(i32 %a) {
 ; CHECK-LABEL: @test27(
 ; CHECK-NEXT:    [[DIV:%.*]] = shl nuw i32 [[A:%.*]], 1
diff --git a/llvm/test/Transforms/InstCombine/sub-after-sle-is-non-negative.ll b/llvm/test/Transforms/InstCombine/sub-after-sle-is-non-negative.ll
index 8395895b1af65..4c096aeb2bb71 100644
--- a/llvm/test/Transforms/InstCombine/sub-after-sle-is-non-negative.ll
+++ b/llvm/test/Transforms/InstCombine/sub-after-sle-is-non-negative.ll
@@ -1,6 +1,68 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
 declare void @subroutine(i16)
+declare i8 @llvm.smin.i8(i8, i8)
+declare i8 @llvm.smax.i8(i8, i8)
+
+define void @test_as_arg_smin(i8 %a, i8 %b) {
+; CHECK-LABEL: define void @test_as_arg_smin(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
+; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.smin.i8(i8 [[B]], i8 [[A]])
+; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[A]], [[M]]
+; CHECK-NEXT:    [[CONV:%.*]] = sext i8 [[SUB]] to i16
+; CHECK-NEXT:    call void @subroutine(i16 [[CONV]])
+; CHECK-NEXT:    ret void
+;
+  %m = call i8 @llvm.smin.i8(i8 %b, i8 %a)
+  %sub = sub nsw i8 %a, %m
+  %conv = sext i8 %sub to i16
+  call void @subroutine(i16 %conv)
+  ret void
+}
+
+define i16 @test_as_retval_smin(i8 %a, i8 %b) {
+; CHECK-LABEL: define i16 @test_as_retval_smin(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
+; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.smin.i8(i8 [[B]], i8 [[A]])
+; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[A]], [[M]]
+; CHECK-NEXT:    [[CONV:%.*]] = sext i8 [[SUB]] to i16
+; CHECK-NEXT:    ret i16 [[CONV]]
+;
+  %m = call i8 @llvm.smin.i8(i8 %b, i8 %a)
+  %sub = sub nsw i8 %a, %m
+  %conv = sext i8 %sub to i16
+  ret i16 %conv
+}
+
+define void @test_as_arg_smax(i8 %a, i8 %b) {
+; CHECK-LABEL: define void @test_as_arg_smax(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
+; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[A]], i8 [[B]])
+; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[M]], [[A]]
+; CHECK-NEXT:    [[CONV:%.*]] = sext i8 [[SUB]] to i16
+; CHECK-NEXT:    call void @subroutine(i16 [[CONV]])
+; CHECK-NEXT:    ret void
+;
+  %m = call i8 @llvm.smax.i8(i8 %a, i8 %b)
+  %sub = sub nsw i8 %m, %a
+  %conv = sext i8 %sub to i16
+  call void @subroutine(i16 %conv)
+  ret void
+}
+
+define i16 @test_as_retval_smax(i8 %a, i8 %b) {
+; CHECK-LABEL: define i16 @test_as_retval_smax(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
+; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[A]], i8 [[B]])
+; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[M]], [[A]]
+; CHECK-NEXT:    [[CONV:%.*]] = sext i8 [[SUB]] to i16
+; CHECK-NEXT:    ret i16 [[CONV]]
+;
+  %m = call i8 @llvm.smax.i8(i8 %a, i8 %b)
+  %sub = sub nsw i8 %m, %a
+  %conv = sext i8 %sub to i16
+  ret i16 %conv
+}
 
 define void @test_as_arg(i8 %a, i8 %b) {
 ; CHECK-LABEL: define void @test_as_arg(
diff --git a/llvm/test/Transforms/InstSimplify/implies.ll b/llvm/test/Transforms/InstSimplify/implies.ll
index 53d8d79add301..3414a844e0499 100644
--- a/llvm/test/Transforms/InstSimplify/implies.ll
+++ b/llvm/test/Transforms/InstSimplify/implies.ll
@@ -599,3 +599,796 @@ define i1 @same_ops_with_constant_wrong_sign(i8 %x) {
 }
 
 declare void @llvm.assume(i1)
+
+; ============================================================================
+; Tests for new isTrueIntPredicate patterns (use i8 to speedup alive2)
+; ============================================================================
+
+; Signed patterns - Category 1: x compared to op(x)
+
+define i1 @sle_nsw_add_pos(i8 %x) {
+; CHECK-LABEL: @sle_nsw_add_pos(
+; CHECK-NEXT:    ret i1 true
+;
+  %add = add nsw i8 %x, 5
+  %cmp = icmp sle i8 %x, %add
+  ret i1 %cmp
+}
+
+define i1 @slt_nsw_add_pos(i8 %x) {
+; CHECK-LABEL: @slt_nsw_add_pos(
+; CHECK-NEXT:    ret i1 true
+;
+  %add = add nsw i8 %x, 5
+  %cmp = icmp slt i8 %x, %add
+  ret i1 %cmp
+}
+
+define i1 @sle_nsw_sub_neg(i8 %x) {
+; CHECK-LABEL: @sle_nsw_sub_neg(
+; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[X:%.*]], 5
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[SUB]], [[X]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sub = sub nsw i8 %x, 5
+  %cmp = icmp sle i8 %sub, %x
+  ret i1 %cmp
+}
+
+define i1 @slt_nsw_sub_neg(i8 %x) {
+; CHECK-LABEL: @slt_nsw_sub_neg(
+; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[X:%.*]], 5
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[SUB]], [[X]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sub = sub nsw i8 %x, 5
+  %cmp = icmp slt i8 %sub, %x
+  ret i1 %cmp
+}
+
+define i1 @sle_nsw_nuw_shl(i8 %x, i8 %y) {
+; CHECK-LABEL: @sle_nsw_nuw_shl(
+; CHECK-NEXT:    [[SHL:%.*]] = shl nuw nsw i8 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[X]], [[SHL]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shl = shl nsw nuw i8 %x, %y
+  %cmp = icmp sle i8 %x, %shl
+  ret i1 %cmp
+}
+
+define i1 @sle_nsw_sub_nonpos_rhs(i8 %x) {
+; CHECK-LABEL: @sle_nsw_sub_nonpos_rhs(
+; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[X:%.*]], -5
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[X]], [[SUB]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sub = sub nsw i8 %x, -5
+  %cmp = icmp sle i8 %x, %sub
+  ret i1 %cmp
+}
+
+define i1 @slt_nsw_sub_neg_rhs(i8 %x) {
+; CHECK-LABEL: @slt_nsw_sub_neg_rhs(
+; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[X:%.*]], -5
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X]], [[SUB]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sub = sub nsw i8 %x, -5
+  %cmp = icmp slt i8 %x, %sub
+  ret i1 %cmp
+}
+
+define i1 @sle_or_pos(i8 %x) {
+; CHECK-LABEL: @sle_or_pos(
+; CHECK-NEXT:    ret i1 true
+;
+  %or = or i8 %x, 5
+  %cmp = icmp sle i8 %x, %or
+  ret i1 %cmp
+}
+
+define i1 @sle_smax(i8 %x, i8 %y) {
+; CHECK-LABEL: @sle_smax(
+; CHECK-NEXT:    ret i1 true
+;
+  %max = call i8 @llvm.smax.i8(i8 %x, i8 %y)
+  %cmp = icmp sle i8 %x, %max
+  ret i1 %cmp
+}
+
+define i1 @sgt_smax_const_operand(i8 %x) {
+; CHECK-LABEL: @sgt_smax_const_operand(
+; CHECK-NEXT:    ret i1 true
+;
+  %max = call i8 @llvm.smax.i8(i8 3, i8 %x)
+  %cmp = icmp sgt i8 %max, 2
+  ret i1 %cmp
+}
+
+define i1 @slt_smin_const_operand(i8 %x) {
+; CHECK-LABEL: @slt_smin_const_operand(
+; CHECK-NEXT:    ret i1 true
+;
+  %min = call i8 @llvm.smin.i8(i8 3, i8 %x)
+  %cmp = icmp slt i8 %min, 4
+  ret i1 %cmp
+}
+
+; Signed patterns - Category 2: op(x) compared to x
+
+define i1 @sle_nsw_add_neg_to_x(i8 %x) {
+; CHECK-LABEL: @sle_nsw_add_neg_to_x(
+; CHECK-NEXT:    ret i1 true
+;
+  %add = add nsw i8 %x, -5
+  %cmp = icmp sle i8 %add, %x
+  ret i1 %cmp
+}
+
+define i1 @sle_nsw_sub_pos_to_x(i8 %x) {
+; CHECK-LABEL: @sle_nsw_sub_pos_to_x(
+; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[X:%.*]], 5
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[SUB]], [[X]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sub = sub nsw i8 %x, 5
+  %cmp = icmp sle i8 %sub, %x
+  ret i1 %cmp
+}
+
+define i1 @sle_and_neg_to_x(i8 %x) {
+; CHECK-LABEL: @sle_and_neg_to_x(
+; CHECK-NEXT:    [[AND:%.*]] = and i8 [[X:%.*]], -5
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[AND]], [[X]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %and = and i8 %x, -5
+  %cmp = icmp sle i8 %and, %x
+  ret i1 %cmp
+}
+
+define i1 @sle_smin_to_x(i8 %x, i8 %y) {
+; CHECK-LABEL: @sle_smin_to_x(
+; CHECK-NEXT:    ret i1 true
+;
+  %min = call i8 @llvm.smin.i8(i8 %x, i8 %y)
+  %cmp = icmp sle i8 %min, %x
+  ret i1 %cmp
+}
+
+define i1 @slt_nsw_add_neg_to_x(i8 %x) {
+; CHECK-LABEL: @slt_nsw_add_neg_to_x(
+; CHECK-NEXT:    ret i1 true
+;
+  %add = add nsw i8 %x, -5
+  %cmp = icmp slt i8 %add, %x
+  ret i1 %cmp
+}
+
+; Signed patterns - Category 3: op(x, C1) compared to op(x, C2)
+
+define i1 @sle_nsw_add_add(i8 %x) {
+; CHECK-LABEL: @sle_nsw_add_add(
+; CHECK-NEXT:    ret i1 true
+;
+  %add1 = add nsw i8 %x, 3
+  %add2 = add nsw i8 %x, 7
+  %cmp = icmp sle i8 %add1, %add2
+  ret i1 %cmp
+}
+
+define i1 @slt_nsw_add_add(i8 %x) {
+; CHECK-LABEL: @slt_nsw_add_add(
+; CHECK-NEXT:    ret i1 true
+;
+  %add1 = add nsw i8 %x, 3
+  %add2 = add nsw i8 %x, 7
+  %cmp = icmp slt i8 %add1, %add2
+  ret i1 %cmp
+}
+
+define i1 @sle_nsw_sub_sub(i8 %x) {
+; CHECK-LABEL: @sle_nsw_sub_sub(
+; CHECK-NEXT:    [[SUB1:%.*]] = sub nsw i8 [[X:%.*]], 7
+; CHECK-NEXT:    [[SUB2:%.*]] = sub nsw i8 [[X]], 3
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[SUB1]], [[SUB2]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sub1 = sub nsw i8 %x, 7
+  %sub2 = sub nsw i8 %x, 3
+  %cmp = icmp sle i8 %sub1, %sub2
+  ret i1 %cmp
+}
+
+define i1 @sle_nsw_nuw_shl_shl(i8 %x) {
+; CHECK-LABEL: @sle_nsw_nuw_shl_shl(
+; CHECK-NEXT:    [[SHL1:%.*]] = shl nuw nsw i8 [[X:%.*]], 2
+; CHECK-NEXT:    [[SHL2:%.*]] = shl nuw nsw i8 [[X]], 5
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[SHL1]], [[SHL2]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shl1 = shl nsw nuw i8 %x, 2
+  %shl2 = shl nsw nuw i8 %x, 5
+  %cmp = icmp sle i8 %shl1, %shl2
+  ret i1 %cmp
+}
+
+define i1 @sle_lshr_lshr_signed(i8 %x) {
+; CHECK-LABEL: @sle_lshr_lshr_signed(
+; CHECK-NEXT:    [[SHR1:%.*]] = lshr i8 [[X:%.*]], 5
+; CHECK-NEXT:    [[SHR2:%.*]] = lshr i8 [[X]], 2
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[SHR1]], [[SHR2]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shr1 = lshr i8 %x, 5
+  %shr2 = lshr i8 %x, 2
+  %cmp = icmp sle i8 %shr1, %shr2
+  ret i1 %cmp
+}
+
+define i1 @sle_transitive_through_x(i8 %x) {
+; CHECK-LABEL: @sle_transitive_through_x(
+; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[X:%.*]], 1
+; CHECK-NEXT:    [[ADD:%.*]] = add nsw i8 [[X]], 1
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[SUB]], [[ADD]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sub = sub nsw i8 %x, 1
+  %add = add nsw i8 %x, 1
+  %cmp = icmp sle i8 %sub, %add
+  ret i1 %cmp
+}
+
+; Unsigned patterns - Category 1: x compared to op(x)
+
+define i1 @ule_nuw_add(i8 %x, i8 %y) {
+; CHECK-LABEL: @ule_nuw_add(
+; CHECK-NEXT:    ret i1 true
+;
+  %add = add nuw i8 %x, %y
+  %cmp = icmp ule i8 %x, %add
+  ret i1 %cmp
+}
+
+define i1 @ult_nuw_add(i8 %x) {
+; CHECK-LABEL: @ult_nuw_add(
+; CHECK-NEXT:    ret i1 true
+;
+  %add = add nuw i8 %x, 5
+  %cmp = icmp ult i8 %x, %add
+  ret i1 %cmp
+}
+
+define i1 @ule_nuw_mul(i8 %x) {
+; CHECK-LABEL: @ule_nuw_mul(
+; CHECK-NEXT:    ret i1 true
+;
+  %mul = mul nuw i8 %x, 3
+  %cmp = icmp ule i8 %x, %mul
+  ret i1 %cmp
+}
+
+define i1 @ule_or(i8 %x, i8 %y) {
+; CHECK-LABEL: @ule_or(
+; CHECK-NEXT:    ret i1 true
+;
+  %or = or i8 %x, %y
+  %cmp = icmp ule i8 %x, %or
+  ret i1 %cmp
+}
+
+define i1 @ule_nuw_shl(i8 %x, i8 %y) {
+; CHECK-LABEL: @ule_nuw_shl(
+; CHECK-NEXT:    [[SHL:%.*]] = shl nuw i8 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[X]], [[SHL]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shl = shl nuw i8 %x, %y
+  %cmp = icmp ule i8 %x, %shl
+  ret i1 %cmp
+}
+
+define i1 @ult_nuw_shl_const(i8 %x, i8 %y) {
+; CHECK-LABEL: @ult_nuw_shl_const(
+; CHECK-NEXT:    [[SHL:%.*]] = shl nuw i8 3, [[X:%.*]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i8 [[X]], [[SHL]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shl = shl nuw i8 3, %x
+  %cmp = icmp ult i8 %x, %shl
+  ret i1 %cmp
+}
+
+define i1 @ult_disjoint_or(i8 %x) {
+; CHECK-LABEL: @ult_disjoint_or(
+; CHECK-NEXT:    [[OR:%.*]] = or disjoint i8 [[X:%.*]], 5
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i8 [[X]], [[OR]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %or = or disjoint i8 %x, 5
+  %cmp = icmp ult i8 %x, %or
+  ret i1 %cmp
+}
+
+define i1 @ule_umax(i8 %x, i8 %y) {
+; CHECK-LABEL: @ule_umax(
+; CHECK-NEXT:    ret i1 true
+;
+  %max = call i8 @llvm.umax.i8(i8 %x, i8 %y)
+  %cmp = icmp ule i8 %x, %max
+  ret i1 %cmp
+}
+
+; Unsigned patterns - Category 2: op(x) compared to x
+
+define i1 @ule_nuw_sub_to_x(i8 %x, i8 %y) {
+; CHECK-LABEL: @ule_nuw_sub_to_x(
+; CHECK-NEXT:    [[SUB:%.*]] = sub nuw i8 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[SUB]], [[X]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sub = sub nuw i8 %x, %y
+  %cmp = icmp ule i8 %sub, %x
+  ret i1 %cmp
+}
+
+define i1 @ult_nuw_sub_to_x_const(i8 %x) {
+; CHECK-LABEL: @ult_nuw_sub_to_x_const(
+; CHECK-NEXT:    [[SUB:%.*]] = sub nuw i8 [[X:%.*]], 5
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i8 [[SUB]], [[X]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sub = sub nuw i8 %x, 5
+  %cmp = icmp ult i8 %sub, %x
+  ret i1 %cmp
+}
+
+
+define i1 @ule_lshr_to_x(i8 %x, i8 %y) {
+; CHECK-LABEL: @ule_lshr_to_x(
+; CHECK-NEXT:    ret i1 true
+;
+  %shr = lshr i8 %x, %y
+  %cmp = icmp ule i8 %shr, %x
+  ret i1 %cmp
+}
+
+define i1 @ule_udiv_to_x(i8 %x, i8 %y) {
+; CHECK-LABEL: @ule_udiv_to_x(
+; CHECK-NEXT:    ret i1 true
+;
+  %div = udiv i8 %x, %y
+  %cmp = icmp ule i8 %div, %x
+  ret i1 %cmp
+}
+
+define i1 @ule_urem_to_x(i8 %x, i8 %y) {
+; CHECK-LABEL: @ule_urem_to_x(
+; CHECK-NEXT:    ret i1 true
+;
+  %rem = urem i8 %x, %y
+  %cmp = icmp ule i8 %rem, %x
+  ret i1 %cmp
+}
+
+define i1 @ult_urem_of_x(i8 %x, i8 %y) {
+; CHECK-LABEL: @ult_urem_of_x(
+; CHECK-NEXT:    ret i1 true
+;
+  %rem = urem i8 %y, %x
+  %cmp = icmp ult i8 %rem, %x
+  ret i1 %cmp
+}
+
+define i1 @ule_and_to_x(i8 %x, i8 %y) {
+; CHECK-LABEL: @ule_and_to_x(
+; CHECK-NEXT:    ret i1 true
+;
+  %and = and i8 %x, %y
+  %cmp = icmp ule i8 %and, %x
+  ret i1 %cmp
+}
+
+define i1 @ule_umin_to_x(i8 %x, i8 %y) {
+; CHECK-LABEL: @ule_umin_to_x(
+; CHECK-NEXT:    ret i1 true
+;
+  %min = call i8 @llvm.umin.i8(i8 %x, i8 %y)
+  %cmp = icmp ule i8 %min, %x
+  ret i1 %cmp
+}
+
+; Unsigned patterns - Category 3: op(x, C1) compared to op(x, C2)
+
+define i1 @ule_nuw_add_add(i8 %x) {
+; CHECK-LABEL: @ule_nuw_add_add(
+; CHECK-NEXT:    ret i1 true
+;
+  %add1 = add nuw i8 %x, 3
+  %add2 = add nuw i8 %x, 7
+  %cmp = icmp ule i8 %add1, %add2
+  ret i1 %cmp
+}
+
+define i1 @ult_nuw_add_add(i8 %x) {
+; CHECK-LABEL: @ult_nuw_add_add(
+; CHECK-NEXT:    ret i1 true
+;
+  %add1 = add nuw i8 %x, 3
+  %add2 = add nuw i8 %x, 7
+  %cmp = icmp ult i8 %add1, %add2
+  ret i1 %cmp
+}
+
+define i1 @ule_nuw_mul_mul(i8 %x) {
+; CHECK-LABEL: @ule_nuw_mul_mul(
+; CHECK-NEXT:    [[MUL1:%.*]] = mul nuw i8 [[X:%.*]], 3
+; CHECK-NEXT:    [[MUL2:%.*]] = mul nuw i8 [[X]], 7
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[MUL1]], [[MUL2]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %mul1 = mul nuw i8 %x, 3
+  %mul2 = mul nuw i8 %x, 7
+  %cmp = icmp ule i8 %mul1, %mul2
+  ret i1 %cmp
+}
+
+define i1 @ule_nuw_shl_shl(i8 %x) {
+; CHECK-LABEL: @ule_nuw_shl_shl(
+; CHECK-NEXT:    [[SHL1:%.*]] = shl nuw i8 [[X:%.*]], 2
+; CHECK-NEXT:    [[SHL2:%.*]] = shl nuw i8 [[X]], 5
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[SHL1]], [[SHL2]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shl1 = shl nuw i8 %x, 2
+  %shl2 = shl nuw i8 %x, 5
+  %cmp = icmp ule i8 %shl1, %shl2
+  ret i1 %cmp
+}
+
+define i1 @ule_nuw_sub_sub(i8 %x) {
+; CHECK-LABEL: @ule_nuw_sub_sub(
+; CHECK-NEXT:    [[SUB1:%.*]] = sub nuw i8 [[X:%.*]], 7
+; CHECK-NEXT:    [[SUB2:%.*]] = sub nuw i8 [[X]], 3
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[SUB1]], [[SUB2]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sub1 = sub nuw i8 %x, 7
+  %sub2 = sub nuw i8 %x, 3
+  %cmp = icmp ule i8 %sub1, %sub2
+  ret i1 %cmp
+}
+
+define i1 @ule_udiv_udiv(i8 %x) {
+; CHECK-LABEL: @ule_udiv_udiv(
+; CHECK-NEXT:    [[DIV1:%.*]] = udiv i8 [[X:%.*]], 7
+; CHECK-NEXT:    [[DIV2:%.*]] = udiv i8 [[X]], 3
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[DIV1]], [[DIV2]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %div1 = udiv i8 %x, 7
+  %div2 = udiv i8 %x, 3
+  %cmp = icmp ule i8 %div1, %div2
+  ret i1 %cmp
+}
+
+define i1 @ule_lshr_lshr(i8 %x) {
+; CHECK-LABEL: @ule_lshr_lshr(
+; CHECK-NEXT:    [[SHR1:%.*]] = lshr i8 [[X:%.*]], 5
+; CHECK-NEXT:    [[SHR2:%.*]] = lshr i8 [[X]], 2
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[SHR1]], [[SHR2]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shr1 = lshr i8 %x, 5
+  %shr2 = lshr i8 %x, 2
+  %cmp = icmp ule i8 %shr1, %shr2
+  ret i1 %cmp
+}
+
+define i1 @ule_transitive_through_x(i8 %x) {
+; CHECK-LABEL: @ule_transitive_through_x(
+; CHECK-NEXT:    [[SUB:%.*]] = sub nuw i8 [[X:%.*]], 1
+; CHECK-NEXT:    [[ADD:%.*]] = add nuw i8 [[X]], 1
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[SUB]], [[ADD]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sub = sub nuw i8 %x, 1
+  %add = add nuw i8 %x, 1
+  %cmp = icmp ule i8 %sub, %add
+  ret i1 %cmp
+}
+
+
+; ============================================================================
+; Tests for isTrueFPPredicate patterns
+; ============================================================================
+
+define i1 @fp_ule_fadd_pos(float %x) {
+; CHECK-LABEL: @fp_ule_fadd_pos(
+; CHECK-NEXT:    [[ADD:%.*]] = fadd float [[X:%.*]], 5.000000e+00
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp ule float [[X]], [[ADD]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %add = fadd float %x, 5.000000e+00
+  %cmp = fcmp ule float %x, %add
+  ret i1 %cmp
+}
+
+define i1 @fp_uge_fsub_pos(float %x) {
+; CHECK-LABEL: @fp_uge_fsub_pos(
+; CHECK-NEXT:    [[SUB:%.*]] = fsub float [[X:%.*]], 5.000000e+00
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp uge float [[X]], [[SUB]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sub = fsub float %x, 5.000000e+00
+  %cmp = fcmp uge float %x, %sub
+  ret i1 %cmp
+}
+
+define i1 @fp_ule_maximumnum(float %x, float %y) {
+; CHECK-LABEL: @fp_ule_maximumnum(
+; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maximumnum.f32(float [[X:%.*]], float [[Y:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp ule float [[X]], [[MAX]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %max = call float @llvm.maximumnum.f32(float %x, float %y)
+  %cmp = fcmp ule float %x, %max
+  ret i1 %cmp
+}
+
+define i1 @fp_ule_maxnum(float %x, float %y) {
+; CHECK-LABEL: @fp_ule_maxnum(
+; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float [[Y:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp ule float [[X]], [[MAX]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %max = call float @llvm.maxnum.f32(float %x, float %y)
+  %cmp = fcmp ule float %x, %max
+  ret i1 %cmp
+}
+
+define i1 @fp_ule_maximum(float %x, float %y) {
+; CHECK-LABEL: @fp_ule_maximum(
+; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maximum.f32(float [[X:%.*]], float [[Y:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp ule float [[X]], [[MAX]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %max = call float @llvm.maximum.f32(float %x, float %y)
+  %cmp = fcmp ule float %x, %max
+  ret i1 %cmp
+}
+
+define i1 @fp_uge_minimumnum(float %x, float %y) {
+; CHECK-LABEL: @fp_uge_minimumnum(
+; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minimumnum.f32(float [[X:%.*]], float [[Y:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp uge float [[X]], [[MIN]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %min = call float @llvm.minimumnum.f32(float %x, float %y)
+  %cmp = fcmp uge float %x, %min
+  ret i1 %cmp
+}
+
+define i1 @fp_uge_minnum(float %x, float %y) {
+; CHECK-LABEL: @fp_uge_minnum(
+; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float [[Y:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp uge float [[X]], [[MIN]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %min = call float @llvm.minnum.f32(float %x, float %y)
+  %cmp = fcmp uge float %x, %min
+  ret i1 %cmp
+}
+
+define i1 @fp_uge_minimum(float %x, float %y) {
+; CHECK-LABEL: @fp_uge_minimum(
+; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minimum.f32(float [[X:%.*]], float [[Y:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp uge float [[X]], [[MIN]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %min = call float @llvm.minimum.f32(float %x, float %y)
+  %cmp = fcmp uge float %x, %min
+  ret i1 %cmp
+}
+
+define i1 @fp_ole_const_maximumnum(float %y) {
+; CHECK-LABEL: @fp_ole_const_maximumnum(
+; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maximumnum.f32(float 4.000000e+00, float [[Y:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp ole float 4.000000e+00, [[MAX]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %max = call float @llvm.maximumnum.f32(float 4.000000e+00, float %y)
+  %cmp = fcmp ole float 4.000000e+00, %max
+  ret i1 %cmp
+}
+
+define i1 @fp_ole_const_maxnum(float %y) {
+; CHECK-LABEL: @fp_ole_const_maxnum(
+; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float 4.000000e+00, float [[Y:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp ole float 4.000000e+00, [[MAX]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %max = call float @llvm.maxnum.f32(float 4.000000e+00, float %y)
+  %cmp = fcmp ole float 4.000000e+00, %max
+  ret i1 %cmp
+}
+
+define i1 @fp_ole_smaller_const_maxnum(float %y) {
+; CHECK-LABEL: @fp_ole_smaller_const_maxnum(
+; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float 3.000000e+00, float [[Y:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp ole float 2.000000e+00, [[MAX]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %max = call float @llvm.maxnum.f32(float 3.000000e+00, float %y)
+  %cmp = fcmp ole float 2.000000e+00, %max
+  ret i1 %cmp
+}
+
+; maximum propagates NaN
+define i1 @fp_ole_const_maximum_neg(float %y) {
+; CHECK-LABEL: @fp_ole_const_maximum_neg(
+; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maximum.f32(float 4.000000e+00, float [[Y:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp ole float 4.000000e+00, [[MAX]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %max = call float @llvm.maximum.f32(float 4.000000e+00, float %y)
+  %cmp = fcmp ole float 4.000000e+00, %max
+  ret i1 %cmp
+}
+
+define i1 @fp_oge_const_minnum(float %y) {
+; CHECK-LABEL: @fp_oge_const_minnum(
+; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float 4.000000e+00, float [[Y:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp oge float 4.000000e+00, [[MIN]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %min = call float @llvm.minnum.f32(float 4.000000e+00, float %y)
+  %cmp = fcmp oge float 4.000000e+00, %min
+  ret i1 %cmp
+}
+
+define i1 @fp_oge_larger_const_minnum(float %y) {
+; CHECK-LABEL: @fp_oge_larger_const_minnum(
+; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float 3.000000e+00, float [[Y:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp oge float 4.000000e+00, [[MIN]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %min = call float @llvm.minnum.f32(float 3.000000e+00, float %y)
+  %cmp = fcmp oge float 4.000000e+00, %min
+  ret i1 %cmp
+}
+
+define i1 @fp_oge_const_minimumnum(float %y) {
+; CHECK-LABEL: @fp_oge_const_minimumnum(
+; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minimumnum.f32(float 4.000000e+00, float [[Y:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp oge float 4.000000e+00, [[MIN]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %min = call float @llvm.minimumnum.f32(float 4.000000e+00, float %y)
+  %cmp = fcmp oge float 4.000000e+00, %min
+  ret i1 %cmp
+}
+
+define i1 @fp_oge_const_minimum_neg(float %y) {
+; CHECK-LABEL: @fp_oge_const_minimum_neg(
+; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minimum.f32(float 4.000000e+00, float [[Y:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp oge float 4.000000e+00, [[MIN]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %min = call float @llvm.minimum.f32(float 4.000000e+00, float %y)
+  %cmp = fcmp oge float 4.000000e+00, %min
+  ret i1 %cmp
+}
+
+define i1 @fp_uge_unrelated_maximumnum_neg(float %x, float %y, float %z) {
+; CHECK-LABEL: @fp_uge_unrelated_maximumnum_neg(
+; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maximumnum.f32(float [[Y:%.*]], float [[Z:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp uge float [[X:%.*]], [[MAX]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %max = call float @llvm.maximumnum.f32(float %y, float %z)
+  %cmp = fcmp uge float %x, %max
+  ret i1 %cmp
+}
+
+; ============================================================================
+; Negative tests - missing required flags
+; ============================================================================
+
+define i1 @sle_add_no_nsw_neg(i8 %x) {
+; CHECK-LABEL: @sle_add_no_nsw_neg(
+; CHECK-NEXT:    [[ADD:%.*]] = add i8 [[X:%.*]], 5
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[X]], [[ADD]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %add = add i8 %x, 5
+  %cmp = icmp sle i8 %x, %add
+  ret i1 %cmp
+}
+
+define i1 @ule_add_no_nuw_neg(i8 %x) {
+; CHECK-LABEL: @ule_add_no_nuw_neg(
+; CHECK-NEXT:    [[ADD:%.*]] = add i8 [[X:%.*]], 5
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[X]], [[ADD]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %add = add i8 %x, 5
+  %cmp = icmp ule i8 %x, %add
+  ret i1 %cmp
+}
+
+define i1 @ule_mul_no_nuw_neg(i8 %x) {
+; CHECK-LABEL: @ule_mul_no_nuw_neg(
+; CHECK-NEXT:    [[MUL:%.*]] = mul i8 [[X:%.*]], 3
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[X]], [[MUL]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %mul = mul i8 %x, 3
+  %cmp = icmp ule i8 %x, %mul
+  ret i1 %cmp
+}
+
+define i1 @sle_nsw_shl_shl(i8 %x) {
+; CHECK-LABEL: @sle_nsw_shl_shl(
+; CHECK-NEXT:    [[SHL1:%.*]] = shl nsw i8 [[X:%.*]], 2
+; CHECK-NEXT:    [[SHL2:%.*]] = shl nsw i8 [[X]], 5
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[SHL1]], [[SHL2]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shl1 = shl nsw i8 %x, 2
+  %shl2 = shl nsw i8 %x, 5
+  %cmp = icmp sle i8 %shl1, %shl2
+  ret i1 %cmp
+}
+
+define i1 @sle_nsw_shl(i8 %x, i8 %y) {
+; CHECK-LABEL: @sle_nsw_shl(
+; CHECK-NEXT:    [[SHL:%.*]] = shl nsw i8 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[X]], [[SHL]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shl = shl nsw i8 %x, %y
+  %cmp = icmp sle i8 %x, %shl
+  ret i1 %cmp
+}
+
+; ============================================================================
+; Negative tests - wrong constant sign
+; ============================================================================
+
+
+define i1 @sle_or_neg_wrong(i8 %x) {
+; CHECK-LABEL: @sle_or_neg_wrong(
+; CHECK-NEXT:    [[OR:%.*]] = or i8 [[X:%.*]], -5
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[X]], [[OR]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %or = or i8 %x, -5
+  %cmp = icmp sle i8 %x, %or
+  ret i1 %cmp
+}
+
+; X might be 0
+define i1 @ult_nuw_mul(i8 %x) {
+; CHECK-LABEL: @ult_nuw_mul(
+; CHECK-NEXT:    [[MUL:%.*]] = mul nuw i8 [[X:%.*]], 3
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i8 [[X]], [[MUL]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %mul = mul nuw i8 %x, 3
+  %cmp = icmp ult i8 %x, %mul
+  ret i1 %cmp
+}
+
+
+declare i8 @llvm.smax.i8(i8, i8)
+declare i8 @llvm.smin.i8(i8, i8)
+declare i8 @llvm.umax.i8(i8, i8)
+declare i8 @llvm.umin.i8(i8, i8)
+declare float @llvm.maxnum.f32(float, float)
+declare float @llvm.maximum.f32(float, float)
+declare float @llvm.maximumnum.f32(float, float)
+declare float @llvm.minnum.f32(float, float)
+declare float @llvm.minimum.f32(float, float)
+declare float @llvm.minimumnum.f32(float, float)

>From 644dec1cb41a2f282e32d5e6eab081fcd66a300b Mon Sep 17 00:00:00 2001
From: Camsyn <camsyn at foxmail.com>
Date: Tue, 17 Mar 2026 22:42:42 +0800
Subject: [PATCH 2/9] Generalize isTruePredicate and add it to
 isImpliedByDomCondition

---
 llvm/lib/Analysis/ValueTracking.cpp           | 557 ++++++++++++++++--
 llvm/test/Transforms/InstCombine/div.ll       |   6 +-
 .../sub-after-sle-is-non-negative.ll          |   8 +-
 llvm/test/Transforms/InstSimplify/implies.ll  | 154 ++---
 llvm/unittests/Analysis/ValueTrackingTest.cpp | 157 +++++
 5 files changed, 701 insertions(+), 181 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 0672dc889b640..29fffa02d4ff5 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9414,86 +9414,526 @@ bool llvm::matchSimpleTernaryIntrinsicRecurrence(const IntrinsicInst *I,
          II == I;
 }
 
+namespace {
+enum PatternKind {
+  MATCH_RHS,  // Match Pred LHS, (op LHS, ...)
+  MATCH_LHS,  // Match Pred (op RHS, ...), RHS
+  MATCH_BOTH, // Match Pred (op1 X, ...), (op2 X, ...)
+  MATCH_NONE  // Match none
+};
+
+/// Classify a comparison into one of the simple operand-sharing patterns used
+/// by isTrueIntPredicate()/isTrueFPPredicate(), and optionally replace a
+/// constant side with a stronger constant operand found on the opposite side.
+///
+/// \param Pred The original comparison predicate.
+/// \param X Output common operand anchor for MATCH_BOTH and direct-match cases.
+/// \param LHS In/out comparison LHS. May be rewritten to a constant operand
+///            from the other side when the original LHS is constant and that
+///            operand is already sufficient to imply the original predicate.
+/// \param RHS In/out comparison RHS. Symmetric to \p LHS.
+/// \param CLHS Parsed constant for the original LHS, or null if it is not a
+///             supported constant.
+/// \param CRHS Parsed constant for the original RHS, or null if it is not a
+///             supported constant.
+///
+/// Returns The recognized PatternKind, or MATCH_NONE if this helper cannot
+///          normalize the comparison into a supported shape.
+template <typename ConstantT>
+PatternKind classifyCmpPatternAndAnchorConstants(
+    CmpInst::Predicate Pred, const Value *&X, const Value *&LHS,
+    const Value *&RHS, const ConstantT *CLHS, const ConstantT *CRHS) {
+  static_assert(std::is_same_v<ConstantT, APInt> ||
+                    std::is_same_v<ConstantT, APFloat>,
+                "Only APInt and APFloat are supported");
+
+  // Candidate Operands: those ops with the same type
+  SmallSetVector<const Value *, 4> LHSOps, RHSOps;
+  auto CmpAndCollectOps = [&](SmallSetVector<const Value *, 4> &Ops,
+                              const Instruction *I,
+                              const Value *CmpTo) -> bool {
+    bool Contains = false;
+    for (const auto &Op : I->operands()) {
+      if (Op->getType() != LHS->getType())
+        continue;
+      if (Op.get() == CmpTo) {
+        X = CmpTo;
+        Contains = true;
+      }
+      Ops.insert(Op.get());
+    }
+    return Contains;
+  };
+  // Category 1: Match Pred LHS, (op LHS, ...)
+  if (auto *RHSInst = dyn_cast<Instruction>(RHS);
+      RHSInst && CmpAndCollectOps(RHSOps, RHSInst, LHS))
+    return MATCH_RHS;
+  // Category 2: Match Pred (op RHS, ...), RHS
+  if (auto *LHSInst = dyn_cast<Instruction>(LHS);
+      LHSInst && CmpAndCollectOps(LHSOps, LHSInst, RHS))
+    return MATCH_LHS;
+  // Category 3: Match Pred (op1 X, ...), (op2 X, ...)
+  if (const auto *It = find_if(
+          LHSOps, [&RHSOps](const Value *Op) { return RHSOps.contains(Op); });
+      It != LHSOps.end() && (X = *It))
+    return MATCH_BOTH;
+  // If one of LHS and RHS is constant, try to find a new LHS/RHS to continue.
+  // E.g., if pred is < :
+  //  CLHS < CLHS' < RHS  --> CLHS < RHS is true, hence we set LHS as CLHS'.
+  //  LHS  < CRHS' < CRHS --> RHS < CRHS is true, hence we set RHS as CRHS'.
+  if (CLHS || CRHS) {
+    const bool IsConstLHS = CLHS != nullptr;
+    const auto &Ops = IsConstLHS ? RHSOps : LHSOps;
+    const auto *It = find_if(Ops, [&](const Value *Op) {
+      const ConstantT *C;
+      if constexpr (std::is_same_v<ConstantT, APInt>) {
+        if (!match(Op, m_APInt(C)))
+          return false;
+        return IsConstLHS ? ICmpInst::compare(*CLHS, *C, Pred)
+                          : ICmpInst::compare(*C, *CRHS, Pred);
+      } else {
+        if (!match(Op, m_APFloat(C)))
+          return false;
+        return IsConstLHS ? FCmpInst::compare(*CLHS, *C, Pred)
+                          : FCmpInst::compare(*C, *CRHS, Pred);
+      }
+    });
+    if (It == Ops.end())
+      return MATCH_NONE;
+
+    return IsConstLHS ? (LHS = *It, MATCH_RHS) : (RHS = *It, MATCH_LHS);
+  }
+  // Fast path to quit: We do not handle other patterns for now.
+  return MATCH_NONE;
+}
+
 /// Return true if "icmp Pred LHS RHS" is always true.
-static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
-                            const Value *RHS) {
-  if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
+bool isTrueIntPredicate(CmpInst::Predicate Pred, const Value *LHS,
+                        const Value *RHS) {
+  switch (Pred) {
+  default:
+    break;
+  case CmpInst::ICMP_SGT:
+    return isTrueIntPredicate(CmpInst::ICMP_SLT, RHS, LHS);
+  case CmpInst::ICMP_SGE:
+    return isTrueIntPredicate(CmpInst::ICMP_SLE, RHS, LHS);
+  case CmpInst::ICMP_UGT:
+    return isTrueIntPredicate(CmpInst::ICMP_ULT, RHS, LHS);
+  case CmpInst::ICMP_UGE:
+    return isTrueIntPredicate(CmpInst::ICMP_ULE, RHS, LHS);
+  }
+
+  const APInt *CLHS = nullptr, *CRHS = nullptr;
+  match(LHS, m_APInt(CLHS));
+  match(RHS, m_APInt(CRHS));
+  // If both CLHS and CRHS are constant integers.
+  if (CLHS && CRHS)
+    return ICmpInst::compare(*CLHS, *CRHS, Pred);
+
+  // If the predicate is true when equal?
+  const bool CanEq = ICmpInst::isTrueWhenEqual(Pred);
+  if (CanEq && LHS == RHS)
     return true;
 
+  // Exclude NE/EQ
+  if (ICmpInst::isEquality(Pred))
+    return false;
+
+  // Represent the common operand between LHS and RHS
+  const Value *X;
+
+  // Derive possible match pattern
+  PatternKind PK =
+      classifyCmpPatternAndAnchorConstants(Pred, X, LHS, RHS, CLHS, CRHS);
+
+  // The pattern is too complex to analyze, quit early.
+  if (PK == MATCH_NONE)
+    return false;
+
+  const APInt *C;
+  const Value *V;
+  bool m;
+
   switch (Pred) {
   default:
     return false;
 
+  case CmpInst::ICMP_SLT:
+    // Delegate to CmpInst::ICMP_SLE to share common patterns.
   case CmpInst::ICMP_SLE: {
-    const APInt *C;
+    // TODO: handle select/phi.
+
+    // Category 1: Match Pred LHS, (op LHS, ...)
+    if (PK == MATCH_RHS) {
+      // LHS s<= LHS +_{nsw} C   if C >= 0
+      // LHS s<  LHS +_{nsw} C   if C > 0
+      if (match(RHS, m_c_NSWAdd(m_Specific(LHS), m_APInt(C))))
+        return CanEq ? C->isNonNegative() : C->isStrictlyPositive();
+      // LHS s<= LHS -_{nsw} C   if C <= 0
+      // LHS s<  LHS -_{nsw} C   if C < 0
+      if (match(RHS, m_NSWSub(m_Specific(LHS), m_APInt(C))))
+        return CanEq ? C->isNonPositive() : C->isNegative();
+      // LHS s<= LHS <<_{nsw,nuw} V  for any V (V < 0 is UB)
+      // slt: cannot exclude LHS == 0
+      if (CanEq && match(RHS, m_NSWShl(m_Specific(LHS), m_Value(V))) &&
+          cast<OverflowingBinaryOperator>(RHS)->hasNoUnsignedWrap())
+        return true;
+      // LHS s<= LHS | C         if C >= 0
+      // LHS s<  LHS |_{disjoint} C for any C (C = 0 should be folded before)
+      if (CanEq ? match(RHS, m_c_Or(m_Specific(LHS), m_APInt(C)))
+                : match(RHS, m_c_DisjointOr(m_Specific(LHS), m_APInt(C)))) {
+        return C->isNonNegative();
+      }
+      // LHS s<= smax(LHS, V) for any V
+      if (CanEq && match(RHS, m_SMax(m_Value(), m_Value())))
+        return true;
+    }
 
-    // LHS s<= LHS +_{nsw} C   if C >= 0
-    // LHS s<= LHS | C         if C >= 0
-    if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
-        match(RHS, m_Or(m_Specific(LHS), m_APInt(C))))
-      return !C->isNegative();
+    // Category 2: Match Pred (op RHS, ...), RHS
+    else if (PK == MATCH_LHS) {
+      // RHS +_{nsw} C s<= RHS if C <= 0
+      // RHS +_{nsw} C s<  RHS if C < 0
+      if (match(LHS, m_c_NSWAdd(m_Specific(RHS), m_APInt(C))))
+        return CanEq ? C->isNonPositive() : C->isNegative();
+      // RHS -_{nsw} C s<= RHS if C >= 0
+      // RHS -_{nsw} C s<  RHS if C > 0
+      if (match(LHS, m_NSWSub(m_Specific(RHS), m_APInt(C))))
+        return CanEq ? C->isNonNegative() : C->isStrictlyPositive();
+      // RHS & C s<= RHS if C <= 0 (keep the sign)
+      if (CanEq && match(LHS, m_c_And(m_Specific(RHS), m_APInt(C))))
+        return C->isNonPositive();
+      // smin(RHS, V) s<= RHS for any V
+      if (CanEq && match(LHS, m_SMin(m_Value(), m_Value())))
+        return true;
+    }
 
-    // LHS s<= smax(LHS, V) for any V
-    if (match(RHS, m_c_SMax(m_Specific(LHS), m_Value())))
-      return true;
+    // Category 3: Match Pred (X op V1), (X op V2)
+    else if (PK == MATCH_BOTH) {
+      assert(X && "X should be set if MATCH_BOTH");
+      // Canonicalization: constant should be RHS
+      const APInt *CLHS, *CRHS;
+      // (X +_{nsw} CA) s<= (X +_{nsw} CB) if CA s<= CB
+      // (X +_{nsw} CA) s<  (X +_{nsw} CB) if CA s<  CB
+      if (match(LHS, m_NSWAddLike(m_Specific(X), m_APInt(CLHS))) &&
+          match(RHS, m_NSWAddLike(m_Specific(X), m_APInt(CRHS))))
+        return CanEq ? CLHS->sle(*CRHS) : CLHS->slt(*CRHS);
+      // (X -_{nsw} CA) s<= (X -_{nsw} CB) if CA s>= CB
+      // (X -_{nsw} CA) s<  (X -_{nsw} CB) if CA s>  CB
+      if (match(LHS, m_NSWSub(m_Specific(X), m_APInt(CLHS))) &&
+          match(RHS, m_NSWSub(m_Specific(X), m_APInt(CRHS))))
+        return CanEq ? CLHS->sge(*CRHS) : CLHS->sgt(*CRHS);
+      // (X <<_nuw_nsw CA) s<= (X <<_nuw_nsw CB) if CA s<= CB
+      // slt: cannot exclude X == 0
+      if (CanEq && match(LHS, m_NSWShl(m_Specific(X), m_APInt(CLHS))) &&
+          match(RHS, m_NSWShl(m_Specific(X), m_APInt(CRHS))) &&
+          cast<OverflowingBinaryOperator>(RHS)->hasNoUnsignedWrap())
+        return CLHS->sle(*CRHS);
+      // (X >>u CA) s<= (X >>u CB) if CA s>= CB
+      // slt: cannot exclude X == 0
+      if (CanEq && match(LHS, m_LShr(m_Specific(X), m_APInt(CLHS))) &&
+          match(RHS, m_LShr(m_Specific(X), m_APInt(CRHS))))
+        return CLHS->sge(*CRHS);
+
+      // LHS s<= X s<= RHS --> LHS s<= RHS
+      if (CanEq && isTrueIntPredicate(CmpInst::ICMP_SLE, LHS, X) &&
+          isTrueIntPredicate(CmpInst::ICMP_SLE, X, RHS))
+        return true;
+      // LHS s<  X s<= RHS --> LHS s< RHS
+      // LHS s<= X s<  RHS --> LHS s< RHS
+      if (!CanEq && ((isTrueIntPredicate(CmpInst::ICMP_SLT, LHS, X) &&
+                      isTrueIntPredicate(CmpInst::ICMP_SLE, X, RHS)) ||
+                     (isTrueIntPredicate(CmpInst::ICMP_SLE, LHS, X) &&
+                      isTrueIntPredicate(CmpInst::ICMP_SLT, X, RHS))))
+        return true;
+    }
 
+    // Patterns that don't depend on PK classification
     // smin(RHS, V) s<= RHS for any V
-    if (match(LHS, m_c_SMin(m_Specific(RHS), m_Value())))
+    if (CanEq && match(LHS, m_c_SMin(m_Specific(RHS), m_Value())))
+      return true;
+    // LHS s<= smax(LHS, V) for any V
+    if (CanEq && match(RHS, m_c_SMax(m_Specific(LHS), m_Value())))
       return true;
-
-    // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
-    const Value *X;
-    const APInt *CLHS, *CRHS;
-    if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
-        match(RHS, m_NSWAddLike(m_Specific(X), m_APInt(CRHS))))
-      return CLHS->sle(*CRHS);
 
     return false;
   }
 
+  case CmpInst::ICMP_ULT:
+    // Delegate to CmpInst::ICMP_ULE to share common patterns.
   case CmpInst::ICMP_ULE: {
-    // LHS u<= LHS +_{nuw} V for any V
-    if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
-        cast<OverflowingBinaryOperator>(RHS)->hasNoUnsignedWrap())
-      return true;
+    // TODO: handle select/phi.
+
+    // Category 1: Match Pred LHS, (op LHS, ...)
+    if (PK == MATCH_RHS) {
+      // LHS u<= LHS +_{nuw} V for any V
+      // LHS u<  LHS +_{nuw} C for any C (C = 0 should be folded before)
+      if (match(RHS, m_c_NUWAdd(m_Specific(LHS), m_Value(V))))
+        return CanEq ? true
+                     : (m = match(V, m_APInt(C)),
+                        assert(!C->isZero() && "x + 0 should be folded before"),
+                        m);
+      // LHS u<= LHS *_{nuw} C for any C (C = 0,1 should be folded before)
+      // ult: cannot exclude LHS == 0
+      if (CanEq && (match(RHS, m_NUWMul(m_Specific(LHS), m_APInt(C))) ||
+                    match(RHS, m_NUWMul(m_APInt(C), m_Specific(LHS)))))
+        return true;
+      // LHS u<= LHS | V for any V
+      // LHS u<  LHS |_{disjoint} C for any C (C = 0 should be folded before)
+      if (CanEq ? match(RHS, m_c_Or(m_Specific(LHS), m_Value()))
+                : match(RHS, m_c_DisjointOr(m_Specific(LHS), m_APInt(C)))) {
+        assert(!C->isZero() && "x | 0 should be folded before");
+        return true;
+      }
+      // LHS u<= LHS <<_{nuw} V for any V (V < 0 is UB)
+      // ult: cannot exclude LHS == 0
+      if (CanEq && match(RHS, m_NUWShl(m_Specific(LHS), m_Value(V))))
+        return true;
+      // LHS u<= (V <<_{nuw} LHS) for any V
+      // LHS u<  (C <<_{nuw} LHS) for any C (C = 0 should be folded before)
+      if (match(RHS, m_NUWShl(m_Value(V), m_Specific(LHS))))
+        return CanEq
+                   ? true
+                   : (m = match(V, m_APInt(C)),
+                      assert(!C->isZero() && "0 << V should be folded before"),
+                      m);
+
+      // LHS u<= umax(LHS, V) for any V
+      if (CanEq && match(RHS, m_UMax(m_Value(), m_Value())))
+        return true;
+    }
 
-    // LHS u<= LHS | V for any V
-    if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
-      return true;
+    // Category 2: Match Pred (op RHS, ...), RHS
+    else if (PK == MATCH_LHS) {
+      // RHS -_{nuw} V u<= RHS for any V
+      if (match(LHS, m_NUWSub(m_Specific(RHS), m_Value(V))))
+        return CanEq ? true
+                     : (m = match(V, m_APInt(C)),
+                        assert(!C->isZero() && "x - 0 should be folded before"),
+                        m);
+      // (RHS >> V) u<= RHS for any V (V < 0 is UB)
+      // ult: cannot exclude RHS == 0
+      if (CanEq && match(LHS, m_LShr(m_Specific(RHS), m_Value(V))))
+        return true;
+      // RHS u/ V u<= RHS for any V (V = 0 means UB)
+      // ult: cannot exclude RHS == 0
+      if (CanEq && match(LHS, m_UDiv(m_Specific(RHS), m_Value(V))))
+        return true;
+      // RHS u% V u<= RHS for any V (V = 0 means UB)
+      if (CanEq && match(LHS, m_URem(m_Specific(RHS), m_Value())))
+        return true;
+      // V u% RHS u<  RHS for any V
+      if (match(LHS, m_URem(m_Value(), m_Specific(RHS))))
+        return true;
+      // RHS & V u<= RHS for any V
+      if (CanEq && match(LHS, m_And(m_Value(), m_Value())))
+        return true;
+      // umin(RHS, V) u<= RHS for any V
+      if (CanEq && match(LHS, m_UMin(m_Value(), m_Value())))
+        return true;
+    }
 
-    // LHS u<= umax(LHS, V) for any V
-    if (match(RHS, m_c_UMax(m_Specific(LHS), m_Value())))
-      return true;
+    // Category 3: Match Pred (X op V1), (X op V2)
+    else if (PK == MATCH_BOTH) {
+      assert(X && "X should be set if MATCH_BOTH");
+      // Canonicalization: constant should be RHS
+      const APInt *CLHS, *CRHS;
+      // (X +_{nuw} CA) u<= (X +_{nuw} CB) if CA u<= CB
+      // (X +_{nuw} CA) u<  (X +_{nuw} CB) if CA u<  CB
+      if (match(LHS, m_NUWAddLike(m_Specific(X), m_APInt(CLHS))) &&
+          match(RHS, m_NUWAddLike(m_Specific(X), m_APInt(CRHS))))
+        return CanEq ? CLHS->ule(*CRHS) : CLHS->ult(*CRHS);
+      // (X *_{nuw} CA) u<= (X *_{nuw} CB) if CA u<= CB
+      // ult: cannot exclude X == 0
+      if (CanEq &&
+          (match(LHS, m_NUWMul(m_Specific(X), m_APInt(CLHS))) ||
+           match(LHS, m_NUWMul(m_APInt(CLHS), m_Specific(X)))) &&
+          (match(RHS, m_NUWMul(m_Specific(X), m_APInt(CRHS))) ||
+           match(RHS, m_NUWMul(m_APInt(CRHS), m_Specific(X)))))
+        return CLHS->ule(*CRHS);
+      // (X <<_{nuw} CA) u<= (X <<_{nuw} CB) if CA u<= CB
+      // ult: cannot exclude X == 0
+      if (CanEq && match(LHS, m_NUWShl(m_Specific(X), m_APInt(CLHS))) &&
+          match(RHS, m_NUWShl(m_Specific(X), m_APInt(CRHS))))
+        return CLHS->ule(*CRHS);
+      // (X -_{nuw} CA) u<= (X -_{nuw} CB) if CA u>= CB
+      // (X -_{nuw} CA) u<  (X -_{nuw} CB) if CA u>  CB
+      if (match(LHS, m_NUWSub(m_Specific(X), m_APInt(CLHS))) &&
+          match(RHS, m_NUWSub(m_Specific(X), m_APInt(CRHS))))
+        return CanEq ? CLHS->uge(*CRHS) : CLHS->ugt(*CRHS);
+      // (X /_{nuw} CA) u<= (X /_{nuw} CB) if CA u>= CB
+      // ult: cannot exclude X == 0
+      if (CanEq && match(LHS, m_UDiv(m_Specific(X), m_APInt(CLHS))) &&
+          match(RHS, m_UDiv(m_Specific(X), m_APInt(CRHS))))
+        return CLHS->uge(*CRHS);
+      // (X >>_{nuw} CA) u<= (X >>_{nuw} CB) if CA u>= CB
+      // ult: cannot exclude X == 0
+      if (CanEq && match(LHS, m_LShr(m_Specific(X), m_APInt(CLHS))) &&
+          match(RHS, m_LShr(m_Specific(X), m_APInt(CRHS))))
+        return CLHS->uge(*CRHS);
+
+      // LHS u<= X u<= RHS --> LHS u<= RHS
+      if (CanEq && isTrueIntPredicate(CmpInst::ICMP_ULE, LHS, X) &&
+          isTrueIntPredicate(CmpInst::ICMP_ULE, X, RHS))
+        return true;
+      // LHS u<  X u<= RHS --> LHS u< RHS
+      // LHS u<= X u<  RHS --> LHS u< RHS
+      if (!CanEq && ((isTrueIntPredicate(CmpInst::ICMP_ULT, LHS, X) &&
+                      isTrueIntPredicate(CmpInst::ICMP_ULE, X, RHS)) ||
+                     (isTrueIntPredicate(CmpInst::ICMP_ULE, LHS, X) &&
+                      isTrueIntPredicate(CmpInst::ICMP_ULT, X, RHS))))
+        return true;
+    }
 
-    // RHS >> V u<= RHS for any V
-    if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
-      return true;
+    return false;
+  }
+  }
+}
 
-    // RHS u/ C_ugt_1 u<= RHS
-    const APInt *C;
-    if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
-      return true;
+/// Return true if "fcmp Pred LHS RHS" is always true.
+bool isTrueFPPredicate(CmpInst::Predicate Pred, const Value *LHS,
+                       const Value *RHS) {
+  if (Pred == CmpInst::FCMP_TRUE)
+    return true;
+
+  const bool CanEqual = CmpInst::isTrueWhenEqual(Pred);
+  if (CanEqual && LHS == RHS)
+    return true;
+
+  if (Pred == CmpInst::FCMP_UGE)
+    return isTrueFPPredicate(CmpInst::FCMP_ULE, RHS, LHS);
+
+  if (CmpInst::isUnordered(Pred) &&
+      (match(LHS, m_NaN()) || match(RHS, m_NaN())))
+    return true;
+
+  const APFloat *CLHS = nullptr, *CRHS = nullptr;
+  match(LHS, m_APFloat(CLHS));
+  match(RHS, m_APFloat(CRHS));
+  if (CLHS && CRHS)
+    return FCmpInst::compare(*CLHS, *CRHS, Pred);
+
+  const Value *X = nullptr;
+  PatternKind PK =
+      classifyCmpPatternAndAnchorConstants(Pred, X, LHS, RHS, CLHS, CRHS);
+  if (PK == MATCH_NONE)
+    return false;
+
+  // max(NaN, x) --> x
+  auto MatchOrderedFMax = [](const Value *V) -> bool {
+    return match(V, m_FMaxNum(m_Value(), m_Value())) ||
+           match(V, m_FMaximumNum(m_Value(), m_Value()));
+  };
+  // min(NaN, x) --> x
+  auto MatchOrderedFMin = [](const Value *V) -> bool {
+    return match(V, m_FMinNum(m_Value(), m_Value())) ||
+           match(V, m_FMinimumNum(m_Value(), m_Value()));
+  };
+  auto MatchFMax = [MatchOrderedFMax](const Value *V) -> bool {
+    return match(V, m_OrdOrUnordFMax(m_Value(), m_Value())) ||
+           match(V, m_FMaximum(m_Value(), m_Value())) || MatchOrderedFMax(V);
+  };
+  auto MatchFMin = [MatchOrderedFMin](const Value *V) -> bool {
+    return match(V, m_OrdOrUnordFMin(m_Value(), m_Value())) ||
+           match(V, m_FMinimum(m_Value(), m_Value())) || MatchOrderedFMin(V);
+  };
+
+  switch (Pred) {
+  default:
+    return false;
 
-    // RHS & V u<= RHS for any V
-    if (match(LHS, m_c_And(m_Specific(RHS), m_Value())))
+  case CmpInst::FCMP_OGT:
+  case CmpInst::FCMP_OLT:
+    // Hard to ensure NaN-free.
+    return false;
+  case CmpInst::FCMP_OGE:
+    return isTrueFPPredicate(CmpInst::FCMP_OLE, RHS, LHS);
+  case CmpInst::FCMP_ORD:
+  case CmpInst::FCMP_OLE: {
+    const APFloat *C;
+    // C o<= omax(V, C) for any V (C = NaN should be folded before)
+    if (PK == MATCH_RHS && match(LHS, m_APFloat(C)) && MatchOrderedFMax(RHS)) {
+      assert(!C->isNaN() && "max(NaN, LHS) should be folded before");
       return true;
+    }
+    // omin(V, C) o<= C for any V (C = NaN should be folded before)
+    if (PK == MATCH_LHS && match(RHS, m_APFloat(C)) && MatchOrderedFMin(LHS)) {
 
-    // umin(RHS, V) u<= RHS for any V
-    if (match(LHS, m_c_UMin(m_Specific(RHS), m_Value())))
+      assert(!C->isNaN() && "min(NaN, RHS) should be folded before");
       return true;
+    }
 
-    // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
-    const Value *X;
-    const APInt *CLHS, *CRHS;
-    if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
-        match(RHS, m_NUWAddLike(m_Specific(X), m_APInt(CRHS))))
-      return CLHS->ule(*CRHS);
+    return false;
+  }
+
+  case CmpInst::FCMP_UGT:
+  case CmpInst::FCMP_ULT:
+    // Hard to prove due to precision reasons.
+    return false;
+
+  case CmpInst::FCMP_ULE: {
+    const APFloat *C;
+
+    if (PK == MATCH_RHS) {
+      // Category 1: LHS u<= (op LHS, ...)
+
+      // LHS u<= LHS + C if C >= 0 or C is NaN
+      if (match(RHS, m_c_FAdd(m_Specific(LHS), m_APFloat(C))))
+        return C->isNaN() || !C->isNegative() || C->isZero();
+      // LHS u<= LHS - C if C <= 0 or C is NaN
+      if (match(RHS, m_FSub(m_Specific(LHS), m_APFloat(C))))
+        return C->isNaN() || C->isNegative() || C->isZero();
+
+      // LHS u<= fmax(LHS, V) for any V
+      if (MatchFMax(RHS))
+        return true;
+    } else if (PK == MATCH_LHS) {
+      // Category 2: (op RHS, ...) u<= RHS
+      // RHS + C u<= RHS if C <= 0 or C is NaN
+      if (match(LHS, m_c_FAdd(m_Specific(RHS), m_APFloat(C))))
+        return C->isNaN() || C->isNegative() || C->isZero();
+      // RHS - C u<= RHS if C >= 0 or C is NaN
+      if (match(LHS, m_FSub(m_Specific(RHS), m_APFloat(C))))
+        return C->isNaN() || !C->isNegative() || C->isZero();
+      // fmin(RHS, V) u<= RHS for any V
+      if (MatchFMin(LHS))
+        return true;
+    } else if (PK == MATCH_BOTH) {
+      // Category 3: Match Pred (X op C1), (X op C2)
+      const APFloat *CLHS, *CRHS;
+
+      // (X + CA) u<= (X + CB) if CA u<= CB
+      if (match(LHS, m_c_FAdd(m_Specific(X), m_APFloat(CLHS))) &&
+          match(RHS, m_c_FAdd(m_Specific(X), m_APFloat(CRHS))))
+        return FCmpInst::compare(*CLHS, *CRHS, FCmpInst::FCMP_ULE);
+      // (X - CA) u<= (X - CB) if CA u>= CB
+      if (match(LHS, m_FSub(m_Specific(X), m_APFloat(CLHS))) &&
+          match(RHS, m_FSub(m_Specific(X), m_APFloat(CRHS))))
+        return FCmpInst::compare(*CLHS, *CRHS, FCmpInst::FCMP_UGE);
+
+      // LHS u<= X u<= RHS --> LHS u<= RHS
+      if (isTrueFPPredicate(CmpInst::FCMP_ULE, LHS, X) &&
+          isTrueFPPredicate(CmpInst::FCMP_ULE, X, RHS))
+        return true;
+    }
 
     return false;
   }
   }
 }
 
+/// Return true if "cmp Pred LHS RHS" is always true.
+/// This function only matches simple patterns and does not use Known* APIs.
+bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
+                     const Value *RHS) {
+  if (CmpInst::isIntPredicate(Pred))
+    return isTrueIntPredicate(Pred, LHS, RHS);
+  if (CmpInst::isFPPredicate(Pred))
+    return isTrueFPPredicate(Pred, LHS, RHS);
+  return false;
+}
+
+} // namespace
+
 /// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
 /// ALHS ARHS" is true.  Otherwise, return std::nullopt.
 static std::optional<bool>
@@ -9505,29 +9945,29 @@ isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS,
 
   case CmpInst::ICMP_SLT:
   case CmpInst::ICMP_SLE:
-    if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
-        isTruePredicate(CmpInst::ICMP_SLE, ARHS, BRHS))
+    if (isTrueIntPredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
+        isTrueIntPredicate(CmpInst::ICMP_SLE, ARHS, BRHS))
       return true;
     return std::nullopt;
 
   case CmpInst::ICMP_SGT:
   case CmpInst::ICMP_SGE:
-    if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
-        isTruePredicate(CmpInst::ICMP_SLE, BRHS, ARHS))
+    if (isTrueIntPredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
+        isTrueIntPredicate(CmpInst::ICMP_SLE, BRHS, ARHS))
       return true;
     return std::nullopt;
 
   case CmpInst::ICMP_ULT:
   case CmpInst::ICMP_ULE:
-    if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
-        isTruePredicate(CmpInst::ICMP_ULE, ARHS, BRHS))
+    if (isTrueIntPredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
+        isTrueIntPredicate(CmpInst::ICMP_ULE, ARHS, BRHS))
       return true;
     return std::nullopt;
 
   case CmpInst::ICMP_UGT:
   case CmpInst::ICMP_UGE:
-    if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
-        isTruePredicate(CmpInst::ICMP_ULE, BRHS, ARHS))
+    if (isTrueIntPredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
+        isTrueIntPredicate(CmpInst::ICMP_ULE, BRHS, ARHS))
       return true;
     return std::nullopt;
   }
@@ -9941,6 +10381,13 @@ std::optional<bool> llvm::isImpliedByDomCondition(CmpPredicate Pred,
                                                   const Value *RHS,
                                                   const Instruction *ContextI,
                                                   const DataLayout &DL) {
+  // Check Pred, LHS, RHS just by data relation, e.g., x < max(x, y)
+  if (isTruePredicate(Pred, LHS, RHS))
+    return true;
+  if (isTruePredicate(CmpInst::getInversePredicate(Pred), LHS, RHS))
+    return false;
+
+  // Check Pred, LHS, RHS by dom condtion.
   auto PredCond = getDomPredecessorCondition(ContextI);
   if (PredCond.first)
     return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
diff --git a/llvm/test/Transforms/InstCombine/div.ll b/llvm/test/Transforms/InstCombine/div.ll
index 5cb4b86aa0380..f92ea1621ae73 100644
--- a/llvm/test/Transforms/InstCombine/div.ll
+++ b/llvm/test/Transforms/InstCombine/div.ll
@@ -385,8 +385,7 @@ define i64 @sdiv_shl_nonneg_from_sub_smin(i64 %a, i64 %b) {
 ; CHECK-LABEL: @sdiv_shl_nonneg_from_sub_smin(
 ; CHECK-NEXT:    [[M:%.*]] = call i64 @llvm.smin.i64(i64 [[B:%.*]], i64 [[A:%.*]])
 ; CHECK-NEXT:    [[X:%.*]] = sub nsw i64 [[A]], [[M]]
-; CHECK-NEXT:    [[D:%.*]] = sdiv i64 [[X]], 4
-; CHECK-NEXT:    [[R:%.*]] = shl nsw i64 [[D]], 2
+; CHECK-NEXT:    [[R:%.*]] = and i64 [[X]], -4
 ; CHECK-NEXT:    ret i64 [[R]]
 ;
   %m = call i64 @llvm.smin.i64(i64 %b, i64 %a)
@@ -400,8 +399,7 @@ define i64 @sdiv_shl_nonneg_from_sub_smax(i64 %a, i64 %b) {
 ; CHECK-LABEL: @sdiv_shl_nonneg_from_sub_smax(
 ; CHECK-NEXT:    [[M:%.*]] = call i64 @llvm.smax.i64(i64 [[A:%.*]], i64 [[B:%.*]])
 ; CHECK-NEXT:    [[X:%.*]] = sub nsw i64 [[M]], [[A]]
-; CHECK-NEXT:    [[D:%.*]] = sdiv i64 [[X]], 4
-; CHECK-NEXT:    [[R:%.*]] = shl nsw i64 [[D]], 2
+; CHECK-NEXT:    [[R:%.*]] = and i64 [[X]], -4
 ; CHECK-NEXT:    ret i64 [[R]]
 ;
   %m = call i64 @llvm.smax.i64(i64 %a, i64 %b)
diff --git a/llvm/test/Transforms/InstCombine/sub-after-sle-is-non-negative.ll b/llvm/test/Transforms/InstCombine/sub-after-sle-is-non-negative.ll
index 4c096aeb2bb71..c3ea736e6517c 100644
--- a/llvm/test/Transforms/InstCombine/sub-after-sle-is-non-negative.ll
+++ b/llvm/test/Transforms/InstCombine/sub-after-sle-is-non-negative.ll
@@ -9,7 +9,7 @@ define void @test_as_arg_smin(i8 %a, i8 %b) {
 ; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
 ; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.smin.i8(i8 [[B]], i8 [[A]])
 ; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[A]], [[M]]
-; CHECK-NEXT:    [[CONV:%.*]] = sext i8 [[SUB]] to i16
+; CHECK-NEXT:    [[CONV:%.*]] = zext nneg i8 [[SUB]] to i16
 ; CHECK-NEXT:    call void @subroutine(i16 [[CONV]])
 ; CHECK-NEXT:    ret void
 ;
@@ -25,7 +25,7 @@ define i16 @test_as_retval_smin(i8 %a, i8 %b) {
 ; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
 ; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.smin.i8(i8 [[B]], i8 [[A]])
 ; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[A]], [[M]]
-; CHECK-NEXT:    [[CONV:%.*]] = sext i8 [[SUB]] to i16
+; CHECK-NEXT:    [[CONV:%.*]] = zext nneg i8 [[SUB]] to i16
 ; CHECK-NEXT:    ret i16 [[CONV]]
 ;
   %m = call i8 @llvm.smin.i8(i8 %b, i8 %a)
@@ -39,7 +39,7 @@ define void @test_as_arg_smax(i8 %a, i8 %b) {
 ; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
 ; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[A]], i8 [[B]])
 ; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[M]], [[A]]
-; CHECK-NEXT:    [[CONV:%.*]] = sext i8 [[SUB]] to i16
+; CHECK-NEXT:    [[CONV:%.*]] = zext nneg i8 [[SUB]] to i16
 ; CHECK-NEXT:    call void @subroutine(i16 [[CONV]])
 ; CHECK-NEXT:    ret void
 ;
@@ -55,7 +55,7 @@ define i16 @test_as_retval_smax(i8 %a, i8 %b) {
 ; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
 ; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[A]], i8 [[B]])
 ; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[M]], [[A]]
-; CHECK-NEXT:    [[CONV:%.*]] = sext i8 [[SUB]] to i16
+; CHECK-NEXT:    [[CONV:%.*]] = zext nneg i8 [[SUB]] to i16
 ; CHECK-NEXT:    ret i16 [[CONV]]
 ;
   %m = call i8 @llvm.smax.i8(i8 %a, i8 %b)
diff --git a/llvm/test/Transforms/InstSimplify/implies.ll b/llvm/test/Transforms/InstSimplify/implies.ll
index 3414a844e0499..0522cbd7f1692 100644
--- a/llvm/test/Transforms/InstSimplify/implies.ll
+++ b/llvm/test/Transforms/InstSimplify/implies.ll
@@ -626,9 +626,7 @@ define i1 @slt_nsw_add_pos(i8 %x) {
 
 define i1 @sle_nsw_sub_neg(i8 %x) {
 ; CHECK-LABEL: @sle_nsw_sub_neg(
-; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[X:%.*]], 5
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[SUB]], [[X]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sub = sub nsw i8 %x, 5
   %cmp = icmp sle i8 %sub, %x
@@ -637,9 +635,7 @@ define i1 @sle_nsw_sub_neg(i8 %x) {
 
 define i1 @slt_nsw_sub_neg(i8 %x) {
 ; CHECK-LABEL: @slt_nsw_sub_neg(
-; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[X:%.*]], 5
-; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[SUB]], [[X]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sub = sub nsw i8 %x, 5
   %cmp = icmp slt i8 %sub, %x
@@ -648,9 +644,7 @@ define i1 @slt_nsw_sub_neg(i8 %x) {
 
 define i1 @sle_nsw_nuw_shl(i8 %x, i8 %y) {
 ; CHECK-LABEL: @sle_nsw_nuw_shl(
-; CHECK-NEXT:    [[SHL:%.*]] = shl nuw nsw i8 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[X]], [[SHL]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %shl = shl nsw nuw i8 %x, %y
   %cmp = icmp sle i8 %x, %shl
@@ -659,9 +653,7 @@ define i1 @sle_nsw_nuw_shl(i8 %x, i8 %y) {
 
 define i1 @sle_nsw_sub_nonpos_rhs(i8 %x) {
 ; CHECK-LABEL: @sle_nsw_sub_nonpos_rhs(
-; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[X:%.*]], -5
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[X]], [[SUB]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sub = sub nsw i8 %x, -5
   %cmp = icmp sle i8 %x, %sub
@@ -670,9 +662,7 @@ define i1 @sle_nsw_sub_nonpos_rhs(i8 %x) {
 
 define i1 @slt_nsw_sub_neg_rhs(i8 %x) {
 ; CHECK-LABEL: @slt_nsw_sub_neg_rhs(
-; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[X:%.*]], -5
-; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X]], [[SUB]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sub = sub nsw i8 %x, -5
   %cmp = icmp slt i8 %x, %sub
@@ -728,9 +718,7 @@ define i1 @sle_nsw_add_neg_to_x(i8 %x) {
 
 define i1 @sle_nsw_sub_pos_to_x(i8 %x) {
 ; CHECK-LABEL: @sle_nsw_sub_pos_to_x(
-; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[X:%.*]], 5
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[SUB]], [[X]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sub = sub nsw i8 %x, 5
   %cmp = icmp sle i8 %sub, %x
@@ -739,9 +727,7 @@ define i1 @sle_nsw_sub_pos_to_x(i8 %x) {
 
 define i1 @sle_and_neg_to_x(i8 %x) {
 ; CHECK-LABEL: @sle_and_neg_to_x(
-; CHECK-NEXT:    [[AND:%.*]] = and i8 [[X:%.*]], -5
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[AND]], [[X]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %and = and i8 %x, -5
   %cmp = icmp sle i8 %and, %x
@@ -790,10 +776,7 @@ define i1 @slt_nsw_add_add(i8 %x) {
 
 define i1 @sle_nsw_sub_sub(i8 %x) {
 ; CHECK-LABEL: @sle_nsw_sub_sub(
-; CHECK-NEXT:    [[SUB1:%.*]] = sub nsw i8 [[X:%.*]], 7
-; CHECK-NEXT:    [[SUB2:%.*]] = sub nsw i8 [[X]], 3
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[SUB1]], [[SUB2]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sub1 = sub nsw i8 %x, 7
   %sub2 = sub nsw i8 %x, 3
@@ -803,10 +786,7 @@ define i1 @sle_nsw_sub_sub(i8 %x) {
 
 define i1 @sle_nsw_nuw_shl_shl(i8 %x) {
 ; CHECK-LABEL: @sle_nsw_nuw_shl_shl(
-; CHECK-NEXT:    [[SHL1:%.*]] = shl nuw nsw i8 [[X:%.*]], 2
-; CHECK-NEXT:    [[SHL2:%.*]] = shl nuw nsw i8 [[X]], 5
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[SHL1]], [[SHL2]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %shl1 = shl nsw nuw i8 %x, 2
   %shl2 = shl nsw nuw i8 %x, 5
@@ -816,10 +796,7 @@ define i1 @sle_nsw_nuw_shl_shl(i8 %x) {
 
 define i1 @sle_lshr_lshr_signed(i8 %x) {
 ; CHECK-LABEL: @sle_lshr_lshr_signed(
-; CHECK-NEXT:    [[SHR1:%.*]] = lshr i8 [[X:%.*]], 5
-; CHECK-NEXT:    [[SHR2:%.*]] = lshr i8 [[X]], 2
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[SHR1]], [[SHR2]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %shr1 = lshr i8 %x, 5
   %shr2 = lshr i8 %x, 2
@@ -829,10 +806,7 @@ define i1 @sle_lshr_lshr_signed(i8 %x) {
 
 define i1 @sle_transitive_through_x(i8 %x) {
 ; CHECK-LABEL: @sle_transitive_through_x(
-; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i8 [[X:%.*]], 1
-; CHECK-NEXT:    [[ADD:%.*]] = add nsw i8 [[X]], 1
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[SUB]], [[ADD]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sub = sub nsw i8 %x, 1
   %add = add nsw i8 %x, 1
@@ -880,9 +854,7 @@ define i1 @ule_or(i8 %x, i8 %y) {
 
 define i1 @ule_nuw_shl(i8 %x, i8 %y) {
 ; CHECK-LABEL: @ule_nuw_shl(
-; CHECK-NEXT:    [[SHL:%.*]] = shl nuw i8 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[X]], [[SHL]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %shl = shl nuw i8 %x, %y
   %cmp = icmp ule i8 %x, %shl
@@ -891,9 +863,7 @@ define i1 @ule_nuw_shl(i8 %x, i8 %y) {
 
 define i1 @ult_nuw_shl_const(i8 %x, i8 %y) {
 ; CHECK-LABEL: @ult_nuw_shl_const(
-; CHECK-NEXT:    [[SHL:%.*]] = shl nuw i8 3, [[X:%.*]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i8 [[X]], [[SHL]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %shl = shl nuw i8 3, %x
   %cmp = icmp ult i8 %x, %shl
@@ -902,9 +872,7 @@ define i1 @ult_nuw_shl_const(i8 %x, i8 %y) {
 
 define i1 @ult_disjoint_or(i8 %x) {
 ; CHECK-LABEL: @ult_disjoint_or(
-; CHECK-NEXT:    [[OR:%.*]] = or disjoint i8 [[X:%.*]], 5
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i8 [[X]], [[OR]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %or = or disjoint i8 %x, 5
   %cmp = icmp ult i8 %x, %or
@@ -924,9 +892,7 @@ define i1 @ule_umax(i8 %x, i8 %y) {
 
 define i1 @ule_nuw_sub_to_x(i8 %x, i8 %y) {
 ; CHECK-LABEL: @ule_nuw_sub_to_x(
-; CHECK-NEXT:    [[SUB:%.*]] = sub nuw i8 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[SUB]], [[X]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sub = sub nuw i8 %x, %y
   %cmp = icmp ule i8 %sub, %x
@@ -935,9 +901,7 @@ define i1 @ule_nuw_sub_to_x(i8 %x, i8 %y) {
 
 define i1 @ult_nuw_sub_to_x_const(i8 %x) {
 ; CHECK-LABEL: @ult_nuw_sub_to_x_const(
-; CHECK-NEXT:    [[SUB:%.*]] = sub nuw i8 [[X:%.*]], 5
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i8 [[SUB]], [[X]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sub = sub nuw i8 %x, 5
   %cmp = icmp ult i8 %sub, %x
@@ -1023,10 +987,7 @@ define i1 @ult_nuw_add_add(i8 %x) {
 
 define i1 @ule_nuw_mul_mul(i8 %x) {
 ; CHECK-LABEL: @ule_nuw_mul_mul(
-; CHECK-NEXT:    [[MUL1:%.*]] = mul nuw i8 [[X:%.*]], 3
-; CHECK-NEXT:    [[MUL2:%.*]] = mul nuw i8 [[X]], 7
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[MUL1]], [[MUL2]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %mul1 = mul nuw i8 %x, 3
   %mul2 = mul nuw i8 %x, 7
@@ -1036,10 +997,7 @@ define i1 @ule_nuw_mul_mul(i8 %x) {
 
 define i1 @ule_nuw_shl_shl(i8 %x) {
 ; CHECK-LABEL: @ule_nuw_shl_shl(
-; CHECK-NEXT:    [[SHL1:%.*]] = shl nuw i8 [[X:%.*]], 2
-; CHECK-NEXT:    [[SHL2:%.*]] = shl nuw i8 [[X]], 5
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[SHL1]], [[SHL2]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %shl1 = shl nuw i8 %x, 2
   %shl2 = shl nuw i8 %x, 5
@@ -1049,10 +1007,7 @@ define i1 @ule_nuw_shl_shl(i8 %x) {
 
 define i1 @ule_nuw_sub_sub(i8 %x) {
 ; CHECK-LABEL: @ule_nuw_sub_sub(
-; CHECK-NEXT:    [[SUB1:%.*]] = sub nuw i8 [[X:%.*]], 7
-; CHECK-NEXT:    [[SUB2:%.*]] = sub nuw i8 [[X]], 3
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[SUB1]], [[SUB2]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sub1 = sub nuw i8 %x, 7
   %sub2 = sub nuw i8 %x, 3
@@ -1062,10 +1017,7 @@ define i1 @ule_nuw_sub_sub(i8 %x) {
 
 define i1 @ule_udiv_udiv(i8 %x) {
 ; CHECK-LABEL: @ule_udiv_udiv(
-; CHECK-NEXT:    [[DIV1:%.*]] = udiv i8 [[X:%.*]], 7
-; CHECK-NEXT:    [[DIV2:%.*]] = udiv i8 [[X]], 3
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[DIV1]], [[DIV2]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %div1 = udiv i8 %x, 7
   %div2 = udiv i8 %x, 3
@@ -1075,10 +1027,7 @@ define i1 @ule_udiv_udiv(i8 %x) {
 
 define i1 @ule_lshr_lshr(i8 %x) {
 ; CHECK-LABEL: @ule_lshr_lshr(
-; CHECK-NEXT:    [[SHR1:%.*]] = lshr i8 [[X:%.*]], 5
-; CHECK-NEXT:    [[SHR2:%.*]] = lshr i8 [[X]], 2
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[SHR1]], [[SHR2]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %shr1 = lshr i8 %x, 5
   %shr2 = lshr i8 %x, 2
@@ -1088,10 +1037,7 @@ define i1 @ule_lshr_lshr(i8 %x) {
 
 define i1 @ule_transitive_through_x(i8 %x) {
 ; CHECK-LABEL: @ule_transitive_through_x(
-; CHECK-NEXT:    [[SUB:%.*]] = sub nuw i8 [[X:%.*]], 1
-; CHECK-NEXT:    [[ADD:%.*]] = add nuw i8 [[X]], 1
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i8 [[SUB]], [[ADD]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sub = sub nuw i8 %x, 1
   %add = add nuw i8 %x, 1
@@ -1106,9 +1052,7 @@ define i1 @ule_transitive_through_x(i8 %x) {
 
 define i1 @fp_ule_fadd_pos(float %x) {
 ; CHECK-LABEL: @fp_ule_fadd_pos(
-; CHECK-NEXT:    [[ADD:%.*]] = fadd float [[X:%.*]], 5.000000e+00
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ule float [[X]], [[ADD]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %add = fadd float %x, 5.000000e+00
   %cmp = fcmp ule float %x, %add
@@ -1117,9 +1061,7 @@ define i1 @fp_ule_fadd_pos(float %x) {
 
 define i1 @fp_uge_fsub_pos(float %x) {
 ; CHECK-LABEL: @fp_uge_fsub_pos(
-; CHECK-NEXT:    [[SUB:%.*]] = fsub float [[X:%.*]], 5.000000e+00
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp uge float [[X]], [[SUB]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sub = fsub float %x, 5.000000e+00
   %cmp = fcmp uge float %x, %sub
@@ -1128,9 +1070,7 @@ define i1 @fp_uge_fsub_pos(float %x) {
 
 define i1 @fp_ule_maximumnum(float %x, float %y) {
 ; CHECK-LABEL: @fp_ule_maximumnum(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maximumnum.f32(float [[X:%.*]], float [[Y:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ule float [[X]], [[MAX]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %max = call float @llvm.maximumnum.f32(float %x, float %y)
   %cmp = fcmp ule float %x, %max
@@ -1139,9 +1079,7 @@ define i1 @fp_ule_maximumnum(float %x, float %y) {
 
 define i1 @fp_ule_maxnum(float %x, float %y) {
 ; CHECK-LABEL: @fp_ule_maxnum(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float [[Y:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ule float [[X]], [[MAX]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %max = call float @llvm.maxnum.f32(float %x, float %y)
   %cmp = fcmp ule float %x, %max
@@ -1150,9 +1088,7 @@ define i1 @fp_ule_maxnum(float %x, float %y) {
 
 define i1 @fp_ule_maximum(float %x, float %y) {
 ; CHECK-LABEL: @fp_ule_maximum(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maximum.f32(float [[X:%.*]], float [[Y:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ule float [[X]], [[MAX]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %max = call float @llvm.maximum.f32(float %x, float %y)
   %cmp = fcmp ule float %x, %max
@@ -1161,9 +1097,7 @@ define i1 @fp_ule_maximum(float %x, float %y) {
 
 define i1 @fp_uge_minimumnum(float %x, float %y) {
 ; CHECK-LABEL: @fp_uge_minimumnum(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minimumnum.f32(float [[X:%.*]], float [[Y:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp uge float [[X]], [[MIN]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %min = call float @llvm.minimumnum.f32(float %x, float %y)
   %cmp = fcmp uge float %x, %min
@@ -1172,9 +1106,7 @@ define i1 @fp_uge_minimumnum(float %x, float %y) {
 
 define i1 @fp_uge_minnum(float %x, float %y) {
 ; CHECK-LABEL: @fp_uge_minnum(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float [[Y:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp uge float [[X]], [[MIN]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %min = call float @llvm.minnum.f32(float %x, float %y)
   %cmp = fcmp uge float %x, %min
@@ -1183,9 +1115,7 @@ define i1 @fp_uge_minnum(float %x, float %y) {
 
 define i1 @fp_uge_minimum(float %x, float %y) {
 ; CHECK-LABEL: @fp_uge_minimum(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minimum.f32(float [[X:%.*]], float [[Y:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp uge float [[X]], [[MIN]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %min = call float @llvm.minimum.f32(float %x, float %y)
   %cmp = fcmp uge float %x, %min
@@ -1194,9 +1124,7 @@ define i1 @fp_uge_minimum(float %x, float %y) {
 
 define i1 @fp_ole_const_maximumnum(float %y) {
 ; CHECK-LABEL: @fp_ole_const_maximumnum(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maximumnum.f32(float 4.000000e+00, float [[Y:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ole float 4.000000e+00, [[MAX]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %max = call float @llvm.maximumnum.f32(float 4.000000e+00, float %y)
   %cmp = fcmp ole float 4.000000e+00, %max
@@ -1205,9 +1133,7 @@ define i1 @fp_ole_const_maximumnum(float %y) {
 
 define i1 @fp_ole_const_maxnum(float %y) {
 ; CHECK-LABEL: @fp_ole_const_maxnum(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float 4.000000e+00, float [[Y:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ole float 4.000000e+00, [[MAX]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %max = call float @llvm.maxnum.f32(float 4.000000e+00, float %y)
   %cmp = fcmp ole float 4.000000e+00, %max
@@ -1216,9 +1142,7 @@ define i1 @fp_ole_const_maxnum(float %y) {
 
 define i1 @fp_ole_smaller_const_maxnum(float %y) {
 ; CHECK-LABEL: @fp_ole_smaller_const_maxnum(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float 3.000000e+00, float [[Y:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ole float 2.000000e+00, [[MAX]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %max = call float @llvm.maxnum.f32(float 3.000000e+00, float %y)
   %cmp = fcmp ole float 2.000000e+00, %max
@@ -1239,9 +1163,7 @@ define i1 @fp_ole_const_maximum_neg(float %y) {
 
 define i1 @fp_oge_const_minnum(float %y) {
 ; CHECK-LABEL: @fp_oge_const_minnum(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float 4.000000e+00, float [[Y:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp oge float 4.000000e+00, [[MIN]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %min = call float @llvm.minnum.f32(float 4.000000e+00, float %y)
   %cmp = fcmp oge float 4.000000e+00, %min
@@ -1250,9 +1172,7 @@ define i1 @fp_oge_const_minnum(float %y) {
 
 define i1 @fp_oge_larger_const_minnum(float %y) {
 ; CHECK-LABEL: @fp_oge_larger_const_minnum(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float 3.000000e+00, float [[Y:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp oge float 4.000000e+00, [[MIN]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %min = call float @llvm.minnum.f32(float 3.000000e+00, float %y)
   %cmp = fcmp oge float 4.000000e+00, %min
@@ -1261,9 +1181,7 @@ define i1 @fp_oge_larger_const_minnum(float %y) {
 
 define i1 @fp_oge_const_minimumnum(float %y) {
 ; CHECK-LABEL: @fp_oge_const_minimumnum(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minimumnum.f32(float 4.000000e+00, float [[Y:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp oge float 4.000000e+00, [[MIN]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %min = call float @llvm.minimumnum.f32(float 4.000000e+00, float %y)
   %cmp = fcmp oge float 4.000000e+00, %min
diff --git a/llvm/unittests/Analysis/ValueTrackingTest.cpp b/llvm/unittests/Analysis/ValueTrackingTest.cpp
index de481e39307cb..7cc6eab804557 100644
--- a/llvm/unittests/Analysis/ValueTrackingTest.cpp
+++ b/llvm/unittests/Analysis/ValueTrackingTest.cpp
@@ -2580,6 +2580,163 @@ TEST_F(ValueTrackingTest, IsImpliedConditionOr2) {
   EXPECT_EQ(isImpliedCondition(A, A4, DL, false), std::nullopt);
 }
 
+TEST_F(ValueTrackingTest, IsImpliedConditionDoesNotInferFromUnrelatedPatterns) {
+  parseAssembly(R"(
+    declare i32 @llvm.smax.i32(i32, i32)
+    declare i32 @llvm.smin.i32(i32, i32)
+    declare i32 @llvm.umax.i32(i32, i32)
+    declare i32 @llvm.umin.i32(i32, i32)
+
+    define void @test(i32 %w, i32 %x, i32 %y, i32 %z) {
+      %A = icmp sle i32 %w, %x
+      %smax = call i32 @llvm.smax.i32(i32 %y, i32 %z)
+      %A2 = icmp sle i32 %x, %smax
+      %smin = call i32 @llvm.smin.i32(i32 %y, i32 %z)
+      %A3 = icmp sle i32 %smin, %x
+      %umax = call i32 @llvm.umax.i32(i32 %y, i32 %z)
+      %A4 = icmp ule i32 %x, %umax
+      %and = and i32 %y, %z
+      %A5 = icmp ule i32 %and, %x
+      %umin = call i32 @llvm.umin.i32(i32 %y, i32 %z)
+      %A6 = icmp ule i32 %umin, %x
+      %shl1 = shl i32 %x, 1
+      %shl2 = shl i32 %x, 2
+      %A7 = icmp ule i32 %shl1, %shl2
+      ret void
+    }
+  )");
+  const DataLayout &DL = M->getDataLayout();
+
+  for (Instruction *Cmp : {A2, A3, A4, A5, A6, A7})
+    EXPECT_EQ(isImpliedCondition(A, Cmp, DL), std::nullopt);
+}
+
+TEST_F(ValueTrackingTest, IsImpliedByDomConditionOrderedFPDistinguishesMaximum) {
+  parseAssembly(R"(
+    declare float @llvm.maxnum.f32(float, float)
+    declare float @llvm.maximum.f32(float, float)
+    declare float @llvm.maximumnum.f32(float, float)
+    declare float @llvm.minnum.f32(float, float)
+    declare float @llvm.minimum.f32(float, float)
+    declare float @llvm.minimumnum.f32(float, float)
+
+    define void @test(float %y) {
+    entry:
+      %maxnum = call float @llvm.maxnum.f32(float 4.000000e+00, float %y)
+      %A = fcmp ole float 4.000000e+00, %maxnum
+      %maximum = call float @llvm.maximum.f32(float 4.000000e+00, float %y)
+      %A2 = fcmp ole float 4.000000e+00, %maximum
+      %maximumnum = call float @llvm.maximumnum.f32(float 4.000000e+00, float %y)
+      %A3 = fcmp ole float 4.000000e+00, %maximumnum
+
+      %minnum = call float @llvm.minnum.f32(float 4.000000e+00, float %y)
+      %A4 = fcmp oge float 4.000000e+00, %minnum
+      %minimum = call float @llvm.minimum.f32(float 4.000000e+00, float %y)
+      %A5 = fcmp oge float 4.000000e+00, %minimum
+      %minimumnum = call float @llvm.minimumnum.f32(float 4.000000e+00, float %y)
+      %A6 = fcmp oge float 4.000000e+00, %minimumnum
+      ret void
+    }
+  )");
+  const DataLayout &DL = M->getDataLayout();
+
+  EXPECT_EQ(isImpliedCondition(A, A, DL), true);
+  EXPECT_EQ(isImpliedByDomCondition(cast<FCmpInst>(A)->getPredicate(),
+                                    cast<FCmpInst>(A)->getOperand(0),
+                                    cast<FCmpInst>(A)->getOperand(1),
+                                    cast<Instruction>(A), DL),
+            true);
+
+  EXPECT_EQ(isImpliedCondition(A2, A2, DL), true);
+  EXPECT_EQ(isImpliedByDomCondition(cast<FCmpInst>(A2)->getPredicate(),
+                                    cast<FCmpInst>(A2)->getOperand(0),
+                                    cast<FCmpInst>(A2)->getOperand(1),
+                                    cast<Instruction>(A2), DL),
+            std::nullopt);
+
+  EXPECT_EQ(isImpliedByDomCondition(cast<FCmpInst>(A3)->getPredicate(),
+                                    cast<FCmpInst>(A3)->getOperand(0),
+                                    cast<FCmpInst>(A3)->getOperand(1),
+                                    cast<Instruction>(A3), DL),
+            true);
+  EXPECT_EQ(isImpliedByDomCondition(cast<FCmpInst>(A4)->getPredicate(),
+                                    cast<FCmpInst>(A4)->getOperand(0),
+                                    cast<FCmpInst>(A4)->getOperand(1),
+                                    cast<Instruction>(A4), DL),
+            true);
+  EXPECT_EQ(isImpliedByDomCondition(cast<FCmpInst>(A5)->getPredicate(),
+                                    cast<FCmpInst>(A5)->getOperand(0),
+                                    cast<FCmpInst>(A5)->getOperand(1),
+                                    cast<Instruction>(A5), DL),
+            std::nullopt);
+  EXPECT_EQ(isImpliedByDomCondition(cast<FCmpInst>(A6)->getPredicate(),
+                                    cast<FCmpInst>(A6)->getOperand(0),
+                                    cast<FCmpInst>(A6)->getOperand(1),
+                                    cast<Instruction>(A6), DL),
+            true);
+}
+
+TEST_F(ValueTrackingTest, IsImpliedByDomConditionUnorderedFPAcceptsAllFamilies) {
+  parseAssembly(R"(
+    declare float @llvm.maxnum.f32(float, float)
+    declare float @llvm.maximum.f32(float, float)
+    declare float @llvm.maximumnum.f32(float, float)
+    declare float @llvm.minnum.f32(float, float)
+    declare float @llvm.minimum.f32(float, float)
+    declare float @llvm.minimumnum.f32(float, float)
+
+    define void @test(float %x, float %y) {
+      %maxnum = call float @llvm.maxnum.f32(float %x, float %y)
+      %A = fcmp ule float %x, %maxnum
+      %maximum = call float @llvm.maximum.f32(float %x, float %y)
+      %A2 = fcmp ule float %x, %maximum
+      %maximumnum = call float @llvm.maximumnum.f32(float %x, float %y)
+      %A3 = fcmp ule float %x, %maximumnum
+
+      %minnum = call float @llvm.minnum.f32(float %x, float %y)
+      %A4 = fcmp uge float %x, %minnum
+      %minimum = call float @llvm.minimum.f32(float %x, float %y)
+      %A5 = fcmp uge float %x, %minimum
+      %minimumnum = call float @llvm.minimumnum.f32(float %x, float %y)
+      %A6 = fcmp uge float %x, %minimumnum
+      ret void
+    }
+  )");
+  const DataLayout &DL = M->getDataLayout();
+
+  for (Instruction *Cmp : {A, A2, A3, A4, A5, A6}) {
+    auto *FCmp = cast<FCmpInst>(Cmp);
+    EXPECT_EQ(isImpliedByDomCondition(FCmp->getPredicate(), FCmp->getOperand(0),
+                                      FCmp->getOperand(1),
+                                      cast<Instruction>(Cmp), DL),
+              true);
+  }
+}
+
+TEST_F(ValueTrackingTest,
+       IsImpliedByDomConditionUnorderedFPSameOperandArithmetic) {
+  parseAssembly(R"(
+    define void @test(float %x) {
+      %add1 = fadd float %x, 1.000000e+00
+      %add2 = fadd float %x, 2.000000e+00
+      %A = fcmp ule float %add1, %add2
+      %sub1 = fsub float %x, 1.000000e+00
+      %sub2 = fsub float %x, 2.000000e+00
+      %A2 = fcmp uge float %sub1, %sub2
+      ret void
+    }
+  )");
+  const DataLayout &DL = M->getDataLayout();
+
+  for (Instruction *Cmp : {A, A2}) {
+    auto *FCmp = cast<FCmpInst>(Cmp);
+    EXPECT_EQ(isImpliedByDomCondition(FCmp->getPredicate(), FCmp->getOperand(0),
+                                      FCmp->getOperand(1),
+                                      cast<Instruction>(Cmp), DL),
+              true);
+  }
+}
+
 TEST_F(ComputeKnownBitsTest, KnownNonZeroShift) {
   // %q is known nonzero without known bits.
   // Because %q is nonzero, %A[0] is known to be zero.

>From d84a22b6efa52b3f5107668659d56cd9336a34c6 Mon Sep 17 00:00:00 2001
From: Camsyn <camsyn at foxmail.com>
Date: Tue, 17 Mar 2026 22:43:05 +0800
Subject: [PATCH 3/9] Update other tests

---
 llvm/test/Transforms/InstCombine/add4.ll      |  2 +-
 llvm/test/Transforms/InstCombine/icmp-sub.ll  |  2 +-
 .../Transforms/InstCombine/max-of-nots.ll     |  2 +-
 .../InstCombine/minmax-intrinsics.ll          |  4 ++--
 llvm/test/Transforms/InstCombine/pr63791.ll   |  2 +-
 .../InstCombine/saturating-add-sub.ll         |  4 +---
 .../test/Transforms/InstCombine/sub-minmax.ll | 24 +++++++++----------
 llvm/test/Transforms/InstCombine/sub.ll       | 11 ++++-----
 .../Transforms/InstSimplify/icmp-monotonic.ll |  5 +---
 .../LoopVectorize/AArch64/sve-inductions.ll   |  2 +-
 .../AArch64/sve-vector-reverse.ll             |  4 ++--
 .../AArch64/sve2-histcnt-epilogue.ll          |  4 ++--
 .../AArch64/sve2-histcnt-too-many-deps.ll     |  2 +-
 .../LoopVectorize/AArch64/sve2-histcnt.ll     |  2 +-
 .../RISCV/interleaved-masked-access.ll        |  4 ++--
 .../LoopVectorize/scalable-inductions.ll      |  8 +++----
 llvm/test/Transforms/NewGVN/pr33185.ll        |  3 +--
 17 files changed, 38 insertions(+), 47 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/add4.ll b/llvm/test/Transforms/InstCombine/add4.ll
index 15feb8a093e99..a5f9e3743cf8b 100644
--- a/llvm/test/Transforms/InstCombine/add4.ll
+++ b/llvm/test/Transforms/InstCombine/add4.ll
@@ -392,7 +392,7 @@ define i32 @fold_add_udiv_urem_pow2_both_mul(i32 noundef %arg) {
 define i32 @fold_add_udiv_urem_by_two_no_mul(i32 noundef %arg) {
 ; CHECK-LABEL: @fold_add_udiv_urem_by_two_no_mul(
 ; CHECK-NEXT:    [[LSHR:%.*]] = lshr i32 [[ARG:%.*]], 1
-; CHECK-NEXT:    [[ADD:%.*]] = sub i32 [[ARG]], [[LSHR]]
+; CHECK-NEXT:    [[ADD:%.*]] = sub nuw i32 [[ARG]], [[LSHR]]
 ; CHECK-NEXT:    ret i32 [[ADD]]
 ;
   %lshr = lshr i32 %arg, 1
diff --git a/llvm/test/Transforms/InstCombine/icmp-sub.ll b/llvm/test/Transforms/InstCombine/icmp-sub.ll
index 13ed7ba0c1703..afcf1caaf30fb 100644
--- a/llvm/test/Transforms/InstCombine/icmp-sub.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-sub.ll
@@ -512,7 +512,7 @@ define void @PR54558_reduced(i32 %arg) {
 ; CHECK-NEXT:    [[PHI_OUTER:%.*]] = phi i32 [ [[SUB:%.*]], [[BB_LOOP]] ], [ [[ARG:%.*]], [[BB_ENTRY:%.*]] ]
 ; CHECK-NEXT:    [[MIN:%.*]] = tail call i32 @llvm.umin.i32(i32 [[PHI_OUTER]], i32 43)
 ; CHECK-NEXT:    call void @use(i32 [[MIN]])
-; CHECK-NEXT:    [[SUB]] = sub i32 [[PHI_OUTER]], [[MIN]]
+; CHECK-NEXT:    [[SUB]] = sub nuw i32 [[PHI_OUTER]], [[MIN]]
 ; CHECK-NEXT:    [[COND_OUTER:%.*]] = icmp eq i32 [[SUB]], 0
 ; CHECK-NEXT:    br i1 [[COND_OUTER]], label [[BB_EXIT:%.*]], label [[BB_LOOP]]
 ; CHECK:       bb_exit:
diff --git a/llvm/test/Transforms/InstCombine/max-of-nots.ll b/llvm/test/Transforms/InstCombine/max-of-nots.ll
index b57f84b41033f..d6dd2b57edc82 100644
--- a/llvm/test/Transforms/InstCombine/max-of-nots.ll
+++ b/llvm/test/Transforms/InstCombine/max-of-nots.ll
@@ -478,7 +478,7 @@ define void @cmyk6(i8 %r, i8 %g, i8 %b) {
 ; CHECK-NEXT:    [[K:%.*]] = xor i8 [[TMP2]], -1
 ; CHECK-NEXT:    [[CK:%.*]] = sub i8 [[TMP2]], [[R]]
 ; CHECK-NEXT:    [[MK:%.*]] = sub i8 [[TMP2]], [[G]]
-; CHECK-NEXT:    [[YK:%.*]] = sub i8 [[TMP2]], [[B]]
+; CHECK-NEXT:    [[YK:%.*]] = sub nuw i8 [[TMP2]], [[B]]
 ; CHECK-NEXT:    tail call void @use(i8 [[CK]], i8 [[MK]], i8 [[YK]], i8 [[K]])
 ; CHECK-NEXT:    ret void
 ;
diff --git a/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll b/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll
index 52bc3636be359..d30a862ebf31b 100644
--- a/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll
+++ b/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll
@@ -1674,7 +1674,7 @@ define void @cmyk_commute4(i8 %r, i8 %g, i8 %b) {
 ; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.smax.i8(i8 [[G:%.*]], i8 [[R:%.*]])
 ; CHECK-NEXT:    [[TMP2:%.*]] = call i8 @llvm.umax.i8(i8 [[B:%.*]], i8 [[TMP1]])
 ; CHECK-NEXT:    [[K:%.*]] = xor i8 [[TMP2]], -1
-; CHECK-NEXT:    [[YK:%.*]] = sub i8 [[TMP2]], [[B]]
+; CHECK-NEXT:    [[YK:%.*]] = sub nuw i8 [[TMP2]], [[B]]
 ; CHECK-NEXT:    [[CK:%.*]] = sub i8 [[TMP2]], [[R]]
 ; CHECK-NEXT:    [[MK:%.*]] = sub i8 [[TMP2]], [[G]]
 ; CHECK-NEXT:    call void @use4(i8 [[CK]], i8 [[MK]], i8 [[YK]], i8 [[K]])
@@ -1824,7 +1824,7 @@ define void @cmyk_commute10(i8 %r, i8 %g, i8 %b) {
 ; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.smax.i8(i8 [[G:%.*]], i8 [[R:%.*]])
 ; CHECK-NEXT:    [[TMP2:%.*]] = call i8 @llvm.umax.i8(i8 [[B:%.*]], i8 [[TMP1]])
 ; CHECK-NEXT:    [[K:%.*]] = xor i8 [[TMP2]], -1
-; CHECK-NEXT:    [[YK:%.*]] = sub i8 [[TMP2]], [[B]]
+; CHECK-NEXT:    [[YK:%.*]] = sub nuw i8 [[TMP2]], [[B]]
 ; CHECK-NEXT:    [[CK:%.*]] = sub i8 [[R]], [[TMP2]]
 ; CHECK-NEXT:    [[MK:%.*]] = sub i8 [[TMP2]], [[G]]
 ; CHECK-NEXT:    call void @use4(i8 [[CK]], i8 [[MK]], i8 [[YK]], i8 [[K]])
diff --git a/llvm/test/Transforms/InstCombine/pr63791.ll b/llvm/test/Transforms/InstCombine/pr63791.ll
index 73a559f989261..6f1fb02fb12cb 100644
--- a/llvm/test/Transforms/InstCombine/pr63791.ll
+++ b/llvm/test/Transforms/InstCombine/pr63791.ll
@@ -17,7 +17,7 @@ define void @y() {
 ; CHECK:       for.cond5.preheader.i:
 ; CHECK-NEXT:    br i1 true, label [[FOR_COND1_LOOPEXIT_I:%.*]], label [[FOR_INC19_I:%.*]]
 ; CHECK:       for.inc19.i:
-; CHECK-NEXT:    br i1 poison, label [[FOR_INC19_I]], label [[FOR_COND1_LOOPEXIT_I]]
+; CHECK-NEXT:    br i1 false, label [[FOR_INC19_I]], label [[FOR_COND1_LOOPEXIT_I]]
 ;
 entry:
   br label %for.cond.i
diff --git a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
index efa89db4af61a..73f21b6de9026 100644
--- a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
+++ b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
@@ -1123,9 +1123,7 @@ define <2 x i8> @test_vector_usub_add_nuw_no_ov_nonsplat2(<2 x i8> %a) {
 ; Can be optimized if constant range is tracked per-element.
 define <2 x i8> @test_vector_usub_add_nuw_no_ov_nonsplat3(<2 x i8> %a) {
 ; CHECK-LABEL: @test_vector_usub_add_nuw_no_ov_nonsplat3(
-; CHECK-NEXT:    [[B:%.*]] = add nuw <2 x i8> [[A:%.*]], <i8 10, i8 9>
-; CHECK-NEXT:    [[R:%.*]] = call <2 x i8> @llvm.usub.sat.v2i8(<2 x i8> [[B]], <2 x i8> <i8 10, i8 9>)
-; CHECK-NEXT:    ret <2 x i8> [[R]]
+; CHECK-NEXT:    ret <2 x i8> [[R:%.*]]
 ;
   %b = add nuw <2 x i8> %a, <i8 10, i8 9>
   %r = call <2 x i8> @llvm.usub.sat.v2i8(<2 x i8> %b, <2 x i8> <i8 10, i8 9>)
diff --git a/llvm/test/Transforms/InstCombine/sub-minmax.ll b/llvm/test/Transforms/InstCombine/sub-minmax.ll
index c5af57449bf71..76d0fc57efee5 100644
--- a/llvm/test/Transforms/InstCombine/sub-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/sub-minmax.ll
@@ -68,7 +68,7 @@ define i5 @sub_umin_uses(i5 %a, i5 %b, ptr %p) {
 ; CHECK-SAME: (i5 [[A:%.*]], i5 [[B:%.*]], ptr [[P:%.*]]) {
 ; CHECK-NEXT:    [[UMIN:%.*]] = call i5 @llvm.umin.i5(i5 [[A]], i5 [[B]])
 ; CHECK-NEXT:    store i5 [[UMIN]], ptr [[P]], align 1
-; CHECK-NEXT:    [[R:%.*]] = sub i5 [[A]], [[UMIN]]
+; CHECK-NEXT:    [[R:%.*]] = sub nuw i5 [[A]], [[UMIN]]
 ; CHECK-NEXT:    ret i5 [[R]]
 ;
   %umin = call i5 @llvm.umin.i5(i5 %a, i5 %b)
@@ -200,7 +200,7 @@ define i32 @na_minus_max_na_bi_use(i32 %A, i32 %Bi) {
 ; CHECK-SAME: (i32 [[A:%.*]], i32 [[BI:%.*]]) {
 ; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.umax.i32(i32 [[A]], i32 -32)
 ; CHECK-NEXT:    [[L1:%.*]] = xor i32 [[TMP1]], -1
-; CHECK-NEXT:    [[X:%.*]] = sub i32 [[TMP1]], [[A]]
+; CHECK-NEXT:    [[X:%.*]] = sub nuw i32 [[TMP1]], [[A]]
 ; CHECK-NEXT:    call void @use32(i32 [[L1]])
 ; CHECK-NEXT:    ret i32 [[X]]
 ;
@@ -235,7 +235,7 @@ define i32 @na_minus_max_bi_na_use(i32 %A, i32 %Bi) {
 ; CHECK-SAME: (i32 [[A:%.*]], i32 [[BI:%.*]]) {
 ; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.umax.i32(i32 [[BI]], i32 [[A]])
 ; CHECK-NEXT:    [[L1:%.*]] = xor i32 [[TMP1]], -1
-; CHECK-NEXT:    [[X:%.*]] = sub i32 [[TMP1]], [[A]]
+; CHECK-NEXT:    [[X:%.*]] = sub nuw i32 [[TMP1]], [[A]]
 ; CHECK-NEXT:    call void @use32(i32 [[L1]])
 ; CHECK-NEXT:    ret i32 [[X]]
 ;
@@ -273,7 +273,7 @@ define i32 @na_minus_max_na_bi_use2(i32 %A, i32 %Bi) {
 ; CHECK-SAME: (i32 [[A:%.*]], i32 [[BI:%.*]]) {
 ; CHECK-NEXT:    [[NOT:%.*]] = xor i32 [[A]], -1
 ; CHECK-NEXT:    [[L1:%.*]] = call i32 @llvm.umin.i32(i32 [[NOT]], i32 31)
-; CHECK-NEXT:    [[X:%.*]] = sub i32 [[NOT]], [[L1]]
+; CHECK-NEXT:    [[X:%.*]] = sub nuw i32 [[NOT]], [[L1]]
 ; CHECK-NEXT:    call void @use32(i32 [[L1]])
 ; CHECK-NEXT:    call void @use32(i32 [[NOT]])
 ; CHECK-NEXT:    ret i32 [[X]]
@@ -314,7 +314,7 @@ define i32 @na_minus_max_bi_na_use2(i32 %A, i32 %Bi) {
 ; CHECK-NEXT:    [[NOT:%.*]] = xor i32 [[A]], -1
 ; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.umax.i32(i32 [[BI]], i32 [[A]])
 ; CHECK-NEXT:    [[L1:%.*]] = xor i32 [[TMP1]], -1
-; CHECK-NEXT:    [[X:%.*]] = sub i32 [[TMP1]], [[A]]
+; CHECK-NEXT:    [[X:%.*]] = sub nuw i32 [[TMP1]], [[A]]
 ; CHECK-NEXT:    call void @use32(i32 [[L1]])
 ; CHECK-NEXT:    call void @use32(i32 [[NOT]])
 ; CHECK-NEXT:    ret i32 [[X]]
@@ -334,8 +334,8 @@ define i8 @umin_not_sub(i8 %x, i8 %y) {
 ; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) {
 ; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.umax.i8(i8 [[X]], i8 [[Y]])
 ; CHECK-NEXT:    [[MINXY:%.*]] = xor i8 [[TMP1]], -1
-; CHECK-NEXT:    [[SUBX:%.*]] = sub i8 [[TMP1]], [[X]]
-; CHECK-NEXT:    [[SUBY:%.*]] = sub i8 [[TMP1]], [[Y]]
+; CHECK-NEXT:    [[SUBX:%.*]] = sub nuw i8 [[TMP1]], [[X]]
+; CHECK-NEXT:    [[SUBY:%.*]] = sub nuw i8 [[TMP1]], [[Y]]
 ; CHECK-NEXT:    call void @use8(i8 [[SUBX]])
 ; CHECK-NEXT:    call void @use8(i8 [[SUBY]])
 ; CHECK-NEXT:    ret i8 [[MINXY]]
@@ -380,7 +380,7 @@ define void @umin3_not_all_ops_extra_uses_invert_subs(i8 %x, i8 %y, i8 %z) {
 ; CHECK-NEXT:    [[TMP2:%.*]] = call i8 @llvm.umax.i8(i8 [[Y]], i8 [[TMP1]])
 ; CHECK-NEXT:    [[MINXYZ:%.*]] = xor i8 [[TMP2]], -1
 ; CHECK-NEXT:    [[XMIN:%.*]] = sub i8 [[TMP2]], [[X]]
-; CHECK-NEXT:    [[YMIN:%.*]] = sub i8 [[TMP2]], [[Y]]
+; CHECK-NEXT:    [[YMIN:%.*]] = sub nuw i8 [[TMP2]], [[Y]]
 ; CHECK-NEXT:    [[ZMIN:%.*]] = sub i8 [[TMP2]], [[Z]]
 ; CHECK-NEXT:    call void @use8(i8 [[MINXYZ]])
 ; CHECK-NEXT:    call void @use8(i8 [[XMIN]])
@@ -419,7 +419,7 @@ define i8 @umin_not_sub_intrinsic_commute0(i8 %x, i8 %y) {
 ; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.umax.i8(i8 [[X]], i8 [[Y]])
 ; CHECK-NEXT:    [[M:%.*]] = xor i8 [[TMP1]], -1
 ; CHECK-NEXT:    call void @use8(i8 [[M]])
-; CHECK-NEXT:    [[SUBX:%.*]] = sub i8 [[TMP1]], [[X]]
+; CHECK-NEXT:    [[SUBX:%.*]] = sub nuw i8 [[TMP1]], [[X]]
 ; CHECK-NEXT:    ret i8 [[SUBX]]
 ;
   %nx = xor i8 %x, -1
@@ -502,7 +502,7 @@ define i8 @umin_not_sub_intrinsic_uses(i8 %x, i8 %y) {
 ; CHECK-NEXT:    call void @use8(i8 [[NY]])
 ; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.umin.i8(i8 [[NX]], i8 [[NY]])
 ; CHECK-NEXT:    call void @use8(i8 [[M]])
-; CHECK-NEXT:    [[SUBX:%.*]] = sub i8 [[NX]], [[M]]
+; CHECK-NEXT:    [[SUBX:%.*]] = sub nuw i8 [[NX]], [[M]]
 ; CHECK-NEXT:    ret i8 [[SUBX]]
 ;
   %nx = xor i8 %x, -1
@@ -542,7 +542,7 @@ define i8 @umax_sub_op0_use(i8 %x, i8 %y) {
 ; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) {
 ; CHECK-NEXT:    [[U:%.*]] = call i8 @llvm.umax.i8(i8 [[X]], i8 [[Y]])
 ; CHECK-NEXT:    call void @use8(i8 [[U]])
-; CHECK-NEXT:    [[R:%.*]] = sub i8 [[U]], [[Y]]
+; CHECK-NEXT:    [[R:%.*]] = sub nuw i8 [[U]], [[Y]]
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %u = call i8 @llvm.umax.i8(i8 %x, i8 %y)
@@ -640,7 +640,7 @@ define i8 @umin_sub_op1_use(i8 %x, i8 %y) {
 ; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) {
 ; CHECK-NEXT:    [[U:%.*]] = call i8 @llvm.umin.i8(i8 [[Y]], i8 [[X]])
 ; CHECK-NEXT:    call void @use8(i8 [[U]])
-; CHECK-NEXT:    [[R:%.*]] = sub i8 [[Y]], [[U]]
+; CHECK-NEXT:    [[R:%.*]] = sub nuw i8 [[Y]], [[U]]
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %u = call i8 @llvm.umin.i8(i8 %y, i8 %x)
diff --git a/llvm/test/Transforms/InstCombine/sub.ll b/llvm/test/Transforms/InstCombine/sub.ll
index 439b59946fac1..1128b3499bb50 100644
--- a/llvm/test/Transforms/InstCombine/sub.ll
+++ b/llvm/test/Transforms/InstCombine/sub.ll
@@ -1352,7 +1352,7 @@ define i32 @test70(i32 %A) {
 define i32 @test71(i32 %A, i32 %B) {
 ; CHECK-LABEL: @test71(
 ; CHECK-NEXT:    [[C:%.*]] = or i32 [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT:    [[D:%.*]] = sub i32 [[C]], [[B]]
+; CHECK-NEXT:    [[D:%.*]] = sub nuw i32 [[C]], [[B]]
 ; CHECK-NEXT:    [[E:%.*]] = mul i32 [[C]], [[D]]
 ; CHECK-NEXT:    ret i32 [[E]]
 ;
@@ -1416,7 +1416,7 @@ define i8 @test74(i8 %x, i8 %y) {
 ; CHECK-LABEL: @test74(
 ; CHECK-NEXT:    [[T0:%.*]] = and i8 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    call void @use8(i8 [[T0]])
-; CHECK-NEXT:    [[T1:%.*]] = sub i8 [[X]], [[T0]]
+; CHECK-NEXT:    [[T1:%.*]] = sub nuw i8 [[X]], [[T0]]
 ; CHECK-NEXT:    ret i8 [[T1]]
 ;
   %t0 = and i8 %x, %y
@@ -1973,11 +1973,8 @@ define i16 @zext_nuw_noundef(i8 noundef %x, i8 %y) {
 
 define i16 @zext_nuw(i8 %x, i8 %y) {
 ; CHECK-LABEL: @zext_nuw(
-; CHECK-NEXT:    [[D:%.*]] = sub nuw i8 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[EX:%.*]] = zext i8 [[X]] to i16
-; CHECK-NEXT:    [[ED:%.*]] = zext i8 [[D]] to i16
-; CHECK-NEXT:    [[Z:%.*]] = sub nsw i16 [[EX]], [[ED]]
-; CHECK-NEXT:    ret i16 [[Z]]
+; CHECK-NEXT:    [[ED:%.*]] = zext i8 [[D:%.*]] to i16
+; CHECK-NEXT:    ret i16 [[ED]]
 ;
   %d = sub nuw i8 %x, %y
   %ex = zext i8 %x to i16
diff --git a/llvm/test/Transforms/InstSimplify/icmp-monotonic.ll b/llvm/test/Transforms/InstSimplify/icmp-monotonic.ll
index 685ff39566a1a..0c5fab6390abd 100644
--- a/llvm/test/Transforms/InstSimplify/icmp-monotonic.ll
+++ b/llvm/test/Transforms/InstSimplify/icmp-monotonic.ll
@@ -112,10 +112,7 @@ define i1 @urem_or_ule(i32 %x, i32 %y, i32 %z) {
 define i1 @urem_or_ule_invalid_swapped(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i1 @urem_or_ule_invalid_swapped(
 ; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
-; CHECK-NEXT:    [[OP1:%.*]] = urem i32 [[Y]], [[X]]
-; CHECK-NEXT:    [[OP2:%.*]] = or i32 [[X]], [[Z]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i32 [[OP1]], [[OP2]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %op1 = urem i32 %y, %x
   %op2 = or i32 %x, %z
diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/sve-inductions.ll b/llvm/test/Transforms/LoopVectorize/AArch64/sve-inductions.ll
index d46a1fb85beac..3d493df02cb5a 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/sve-inductions.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/sve-inductions.ll
@@ -22,7 +22,7 @@ define void @cond_ind64(ptr noalias nocapture %a, ptr noalias nocapture readonly
 ; CHECK-NEXT:    [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
 ; CHECK-NEXT:    [[TMP3:%.*]] = shl nuw i64 [[TMP2]], 2
 ; CHECK-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[N]], [[TMP3]]
-; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[N_MOD_VF]]
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub nuw i64 [[N]], [[N_MOD_VF]]
 ; CHECK-NEXT:    [[TMP6:%.*]] = call <vscale x 4 x i64> @llvm.stepvector.nxv4i64()
 ; CHECK-NEXT:    [[DOTSPLATINSERT:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[TMP3]], i64 0
 ; CHECK-NEXT:    [[DOTSPLAT:%.*]] = shufflevector <vscale x 4 x i64> [[DOTSPLATINSERT]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/sve-vector-reverse.ll b/llvm/test/Transforms/LoopVectorize/AArch64/sve-vector-reverse.ll
index 7194ff16df91f..3d0b405d6b507 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/sve-vector-reverse.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/sve-vector-reverse.ll
@@ -23,7 +23,7 @@ define void @vector_reverse_f64(i64 %N, ptr noalias %a, ptr noalias %b) #0{
 ; CHECK-NEXT:    [[TMP5:%.*]] = shl nuw i64 [[TMP4]], 3
 ; CHECK-NEXT:    [[TMP6:%.*]] = shl nuw i64 [[TMP4]], 4
 ; CHECK-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[N]], [[TMP6]]
-; CHECK-NEXT:    [[N_VEC:%.*]] = sub nsw i64 [[N]], [[N_MOD_VF]]
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub nuw nsw i64 [[N]], [[N_MOD_VF]]
 ; CHECK-NEXT:    br label [[VECTOR_BODY:%.*]]
 ; CHECK:       vector.body:
 ; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
@@ -94,7 +94,7 @@ define void @vector_reverse_i64(i64 %N, ptr %a, ptr %b) #0 {
 ; CHECK-NEXT:    [[TMP8:%.*]] = shl nuw i64 [[TMP7]], 3
 ; CHECK-NEXT:    [[TMP9:%.*]] = shl nuw i64 [[TMP7]], 4
 ; CHECK-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[N]], [[TMP9]]
-; CHECK-NEXT:    [[N_VEC:%.*]] = sub nsw i64 [[N]], [[N_MOD_VF]]
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub nuw nsw i64 [[N]], [[N_MOD_VF]]
 ; CHECK-NEXT:    br label [[VECTOR_BODY:%.*]]
 ; CHECK:       vector.body:
 ; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/sve2-histcnt-epilogue.ll b/llvm/test/Transforms/LoopVectorize/AArch64/sve2-histcnt-epilogue.ll
index 1aac78b88e9d2..ee1be566ae84f 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/sve2-histcnt-epilogue.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/sve2-histcnt-epilogue.ll
@@ -21,7 +21,7 @@ define void @simple_histogram(ptr noalias %buckets, ptr readonly %indices, i64 %
 ; CHECK-NEXT:    [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
 ; CHECK-NEXT:    [[TMP7:%.*]] = shl nuw i64 [[TMP2]], 2
 ; CHECK-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[N]], [[TMP7]]
-; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[N_MOD_VF]]
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub nuw i64 [[N]], [[N_MOD_VF]]
 ; CHECK-NEXT:    br label [[VECTOR_BODY:%.*]]
 ; CHECK:       vector.body:
 ; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH1]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
@@ -44,7 +44,7 @@ define void @simple_histogram(ptr noalias %buckets, ptr readonly %indices, i64 %
 ; CHECK-NEXT:    [[TMP24:%.*]] = call i64 @llvm.vscale.i64()
 ; CHECK-NEXT:    [[TMP25:%.*]] = shl nuw i64 [[TMP24]], 1
 ; CHECK-NEXT:    [[N_MOD_VF2:%.*]] = urem i64 [[N]], [[TMP25]]
-; CHECK-NEXT:    [[N_VEC3:%.*]] = sub i64 [[N]], [[N_MOD_VF2]]
+; CHECK-NEXT:    [[N_VEC3:%.*]] = sub nuw i64 [[N]], [[N_MOD_VF2]]
 ; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
 ; CHECK:       vec.epilog.vector.body:
 ; CHECK-NEXT:    [[INDEX4:%.*]] = phi i64 [ [[VEC_EPILOG_RESUME_VAL]], [[VEC_EPILOG_PH]] ], [ [[INDEX_NEXT6:%.*]], [[FOR_BODY]] ]
diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/sve2-histcnt-too-many-deps.ll b/llvm/test/Transforms/LoopVectorize/AArch64/sve2-histcnt-too-many-deps.ll
index 3d80b02ae585b..9a3407e87ec4b 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/sve2-histcnt-too-many-deps.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/sve2-histcnt-too-many-deps.ll
@@ -69,7 +69,7 @@ define void @many_deps(ptr noalias %buckets, ptr %array, ptr %indices, ptr %othe
 ; NORMAL_DEP_LIMIT-NEXT:    [[TMP4:%.*]] = call i64 @llvm.vscale.i64()
 ; NORMAL_DEP_LIMIT-NEXT:    [[TMP8:%.*]] = shl nuw i64 [[TMP4]], 2
 ; NORMAL_DEP_LIMIT-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[N]], [[TMP8]]
-; NORMAL_DEP_LIMIT-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[N_MOD_VF]]
+; NORMAL_DEP_LIMIT-NEXT:    [[N_VEC:%.*]] = sub nuw i64 [[N]], [[N_MOD_VF]]
 ; NORMAL_DEP_LIMIT-NEXT:    [[TMP7:%.*]] = call <vscale x 4 x i32> @llvm.stepvector.nxv4i32()
 ; NORMAL_DEP_LIMIT-NEXT:    [[TMP9:%.*]] = trunc i64 [[TMP8]] to i32
 ; NORMAL_DEP_LIMIT-NEXT:    [[DOTSPLATINSERT:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[TMP9]], i64 0
diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/sve2-histcnt.ll b/llvm/test/Transforms/LoopVectorize/AArch64/sve2-histcnt.ll
index 98b0f46cfc683..b2fd0895f0d19 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/sve2-histcnt.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/sve2-histcnt.ll
@@ -253,7 +253,7 @@ define void @histogram_8bit(ptr noalias %buckets, ptr readonly %indices, i64 %N)
 ; CHECK-NEXT:    [[TMP4:%.*]] = shl nuw nsw i64 [[TMP3]], 4
 ; CHECK-NEXT:    [[DOTNOT:%.*]] = add nsw i64 [[TMP4]], -1
 ; CHECK-NEXT:    [[N_VEC:%.*]] = and i64 [[N]], [[DOTNOT]]
-; CHECK-NEXT:    [[N_VEC1:%.*]] = sub i64 [[N]], [[N_VEC]]
+; CHECK-NEXT:    [[N_VEC1:%.*]] = sub nuw i64 [[N]], [[N_VEC]]
 ; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
 ; CHECK:       vector.body:
 ; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[IV_NEXT:%.*]], [[FOR_BODY]] ]
diff --git a/llvm/test/Transforms/LoopVectorize/RISCV/interleaved-masked-access.ll b/llvm/test/Transforms/LoopVectorize/RISCV/interleaved-masked-access.ll
index b62d6da2f2e2d..86047c235f7ab 100644
--- a/llvm/test/Transforms/LoopVectorize/RISCV/interleaved-masked-access.ll
+++ b/llvm/test/Transforms/LoopVectorize/RISCV/interleaved-masked-access.ll
@@ -62,7 +62,7 @@ define void @masked_strided_factor2(ptr noalias nocapture readonly %p, ptr noali
 ; PREDICATED_DATA-NEXT:    [[TMP1:%.*]] = shl nuw i32 [[TMP0]], 4
 ; PREDICATED_DATA-NEXT:    [[N_RND_UP:%.*]] = add i32 [[TMP1]], 1023
 ; PREDICATED_DATA-NEXT:    [[N_MOD_VF:%.*]] = urem i32 [[N_RND_UP]], [[TMP1]]
-; PREDICATED_DATA-NEXT:    [[N_VEC:%.*]] = sub i32 [[N_RND_UP]], [[N_MOD_VF]]
+; PREDICATED_DATA-NEXT:    [[N_VEC:%.*]] = sub nuw i32 [[N_RND_UP]], [[N_MOD_VF]]
 ; PREDICATED_DATA-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 16 x i32> poison, i32 [[CONV]], i64 0
 ; PREDICATED_DATA-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 16 x i32> [[BROADCAST_SPLATINSERT]], <vscale x 16 x i32> poison, <vscale x 16 x i32> zeroinitializer
 ; PREDICATED_DATA-NEXT:    [[TMP2:%.*]] = call <vscale x 16 x i32> @llvm.stepvector.nxv16i32()
@@ -240,7 +240,7 @@ define void @masked_strided_factor4(ptr noalias nocapture readonly %p, ptr noali
 ; PREDICATED_DATA-NEXT:    [[TMP1:%.*]] = shl nuw i32 [[TMP0]], 4
 ; PREDICATED_DATA-NEXT:    [[N_RND_UP:%.*]] = add i32 [[TMP1]], 1023
 ; PREDICATED_DATA-NEXT:    [[N_MOD_VF:%.*]] = urem i32 [[N_RND_UP]], [[TMP1]]
-; PREDICATED_DATA-NEXT:    [[N_VEC:%.*]] = sub i32 [[N_RND_UP]], [[N_MOD_VF]]
+; PREDICATED_DATA-NEXT:    [[N_VEC:%.*]] = sub nuw i32 [[N_RND_UP]], [[N_MOD_VF]]
 ; PREDICATED_DATA-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 16 x i32> poison, i32 [[CONV]], i64 0
 ; PREDICATED_DATA-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 16 x i32> [[BROADCAST_SPLATINSERT]], <vscale x 16 x i32> poison, <vscale x 16 x i32> zeroinitializer
 ; PREDICATED_DATA-NEXT:    [[TMP2:%.*]] = call <vscale x 16 x i32> @llvm.stepvector.nxv16i32()
diff --git a/llvm/test/Transforms/LoopVectorize/scalable-inductions.ll b/llvm/test/Transforms/LoopVectorize/scalable-inductions.ll
index d911b7a0cb0cf..369026879e6fe 100644
--- a/llvm/test/Transforms/LoopVectorize/scalable-inductions.ll
+++ b/llvm/test/Transforms/LoopVectorize/scalable-inductions.ll
@@ -21,7 +21,7 @@ define void @add_ind64_unrolled(ptr noalias nocapture %a, ptr noalias nocapture
 ; CHECK-NEXT:    [[DOTSPLAT:%.*]] = shufflevector <vscale x 2 x i64> [[BROADCAST_SPLATINSERT]], <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
 ; CHECK-NEXT:    [[TMP5:%.*]] = shl nuw i64 [[TMP4]], 2
 ; CHECK-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[N]], [[TMP5]]
-; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[N_MOD_VF]]
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub nuw i64 [[N]], [[N_MOD_VF]]
 ; CHECK-NEXT:    [[TMP6:%.*]] = call <vscale x 2 x i64> @llvm.stepvector.nxv2i64()
 ; CHECK-NEXT:    br label [[VECTOR_BODY:%.*]]
 ; CHECK:       vector.body:
@@ -95,7 +95,7 @@ define void @add_ind64_unrolled_nxv1i64(ptr noalias nocapture %a, ptr noalias no
 ; CHECK-NEXT:    [[DOTSPLAT:%.*]] = shufflevector <vscale x 1 x i64> [[BROADCAST_SPLATINSERT]], <vscale x 1 x i64> poison, <vscale x 1 x i32> zeroinitializer
 ; CHECK-NEXT:    [[TMP3:%.*]] = shl nuw i64 [[TMP2]], 1
 ; CHECK-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[N]], [[TMP3]]
-; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[N_MOD_VF]]
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub nuw i64 [[N]], [[N_MOD_VF]]
 ; CHECK-NEXT:    [[TMP6:%.*]] = call <vscale x 1 x i64> @llvm.stepvector.nxv1i64()
 ; CHECK-NEXT:    br label [[VECTOR_BODY:%.*]]
 ; CHECK:       vector.body:
@@ -174,7 +174,7 @@ define void @add_unique_ind32(ptr noalias nocapture %a, i64 %n) {
 ; CHECK-NEXT:    [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
 ; CHECK-NEXT:    [[TMP3:%.*]] = shl nuw i64 [[TMP2]], 2
 ; CHECK-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[N]], [[TMP3]]
-; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[N_MOD_VF]]
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub nuw i64 [[N]], [[N_MOD_VF]]
 ; CHECK-NEXT:    [[DOTCAST:%.*]] = trunc i64 [[N_VEC]] to i32
 ; CHECK-NEXT:    [[IND_END:%.*]] = shl i32 [[DOTCAST]], 1
 ; CHECK-NEXT:    [[TMP6:%.*]] = call <vscale x 4 x i32> @llvm.stepvector.nxv4i32()
@@ -249,7 +249,7 @@ define void @add_unique_indf32(ptr noalias nocapture %a, i64 %n) {
 ; CHECK-NEXT:    [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
 ; CHECK-NEXT:    [[TMP3:%.*]] = shl nuw i64 [[TMP2]], 2
 ; CHECK-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[N]], [[TMP3]]
-; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[N_MOD_VF]]
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub nuw i64 [[N]], [[N_MOD_VF]]
 ; CHECK-NEXT:    [[DOTCAST:%.*]] = sitofp i64 [[N_VEC]] to float
 ; CHECK-NEXT:    [[TMP4:%.*]] = fmul nnan float [[DOTCAST]], 2.000000e+00
 ; CHECK-NEXT:    [[IND_END:%.*]] = fadd float [[TMP4]], 0.000000e+00
diff --git a/llvm/test/Transforms/NewGVN/pr33185.ll b/llvm/test/Transforms/NewGVN/pr33185.ll
index edc18281afa59..98c80fad46b15 100644
--- a/llvm/test/Transforms/NewGVN/pr33185.ll
+++ b/llvm/test/Transforms/NewGVN/pr33185.ll
@@ -79,8 +79,7 @@ define i32 @test2() local_unnamed_addr {
 ; CHECK:       cond.end.i:
 ; CHECK-NEXT:    [[COND_I:%.*]] = phi i32 [ [[DIV_I]], [[COND_TRUE_I]] ], [ 0, [[FOR_BODY_I]] ]
 ; CHECK-NEXT:    [[INC_I]] = add nuw nsw i32 [[F_08_I]], 1
-; CHECK-NEXT:    [[TEST:%.*]] = udiv i32 [[MUL_I]], [[INC_I]]
-; CHECK-NEXT:    [[CALL5:%.*]] = tail call i32 (ptr, ...) @printf(ptr @.str4, i32 [[TEST]])
+; CHECK-NEXT:    [[CALL5:%.*]] = tail call i32 (ptr, ...) @printf(ptr @.str4, i32 0)
 ; CHECK-NEXT:    [[EXITCOND_I:%.*]] = icmp eq i32 [[INC_I]], 4
 ; CHECK-NEXT:    br i1 [[EXITCOND_I]], label [[FN1_EXIT:%.*]], label [[FOR_BODY_I]]
 ; CHECK:       fn1.exit:

>From 96c4337c6c28c6bcaa964e0d9aa9119e1a4aa2cb Mon Sep 17 00:00:00 2001
From: Camsyn <camsyn at foxmail.com>
Date: Wed, 18 Mar 2026 00:20:56 +0800
Subject: [PATCH 4/9] Fix float compting in denormal mode

---
 llvm/lib/Analysis/ValueTracking.cpp | 57 +++++++++++++++++++++++------
 1 file changed, 45 insertions(+), 12 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 29fffa02d4ff5..b83c3ed579aa5 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9422,6 +9422,36 @@ enum PatternKind {
   MATCH_NONE  // Match none
 };
 
+std::optional<APFloat> getCompareAPFloat(const APFloat &C,
+                                         const DenormalMode Mode) {
+  if (!C.isDenormal())
+    return C;
+  DenormalMode::DenormalModeKind InMode = Mode.Input;
+  if (InMode == DenormalMode::DenormalModeKind::IEEE)
+    return C;
+  if (InMode == DenormalMode::DenormalModeKind::Dynamic)
+    return std::nullopt;
+  assert(InMode != DenormalMode::DenormalModeKind::Invalid &&
+         InMode != DenormalMode::DenormalModeKind::Dynamic &&
+         "Expected a concrete denormal input mode");
+  // flush denormal input
+  return APFloat::getZero(C.getSemantics(),
+                          InMode == DenormalMode::DenormalModeKind::PreserveSign
+                              ? C.isNegative()
+                              : false);
+}
+
+// If LHS Pred RHS is alwasy true, return true.
+// This function = FCmpInst::compare + DenormalMode
+bool compareFloat(const FCmpInst::Predicate Pred, const APFloat &LHS,
+                  const APFloat &RHS, const Function *CxtF) {
+  DenormalMode Mode = CxtF ? CxtF->getDenormalMode(LHS.getSemantics())
+                           : DenormalMode::getDynamic();
+  auto L = getCompareAPFloat(LHS, Mode);
+  auto R = getCompareAPFloat(RHS, Mode);
+  return L && R && FCmpInst::compare(*L, *R, Pred);
+}
+
 /// Classify a comparison into one of the simple operand-sharing patterns used
 /// by isTrueIntPredicate()/isTrueFPPredicate(), and optionally replace a
 /// constant side with a stronger constant operand found on the opposite side.
@@ -9436,13 +9466,15 @@ enum PatternKind {
 ///             supported constant.
 /// \param CRHS Parsed constant for the original RHS, or null if it is not a
 ///             supported constant.
+/// \param CxtF Context function to extract DenormalMode for float computing.
 ///
 /// Returns The recognized PatternKind, or MATCH_NONE if this helper cannot
 ///          normalize the comparison into a supported shape.
 template <typename ConstantT>
 PatternKind classifyCmpPatternAndAnchorConstants(
     CmpInst::Predicate Pred, const Value *&X, const Value *&LHS,
-    const Value *&RHS, const ConstantT *CLHS, const ConstantT *CRHS) {
+    const Value *&RHS, const ConstantT *CLHS, const ConstantT *CRHS,
+    const Function *CxtF = nullptr) {
   static_assert(std::is_same_v<ConstantT, APInt> ||
                     std::is_same_v<ConstantT, APFloat>,
                 "Only APInt and APFloat are supported");
@@ -9494,8 +9526,8 @@ PatternKind classifyCmpPatternAndAnchorConstants(
       } else {
         if (!match(Op, m_APFloat(C)))
           return false;
-        return IsConstLHS ? FCmpInst::compare(*CLHS, *C, Pred)
-                          : FCmpInst::compare(*C, *CRHS, Pred);
+        return IsConstLHS ? compareFloat(Pred, *CLHS, *C, CxtF)
+                          : compareFloat(Pred, *C, *CRHS, CxtF);
       }
     });
     if (It == Ops.end())
@@ -9791,7 +9823,7 @@ bool isTrueIntPredicate(CmpInst::Predicate Pred, const Value *LHS,
 
 /// Return true if "fcmp Pred LHS RHS" is always true.
 bool isTrueFPPredicate(CmpInst::Predicate Pred, const Value *LHS,
-                       const Value *RHS) {
+                       const Value *RHS, const Instruction *ContextI = nullptr) {
   if (Pred == CmpInst::FCMP_TRUE)
     return true;
 
@@ -9809,12 +9841,13 @@ bool isTrueFPPredicate(CmpInst::Predicate Pred, const Value *LHS,
   const APFloat *CLHS = nullptr, *CRHS = nullptr;
   match(LHS, m_APFloat(CLHS));
   match(RHS, m_APFloat(CRHS));
+  const Function *CxtF = ContextI ? ContextI->getFunction() : nullptr;
   if (CLHS && CRHS)
-    return FCmpInst::compare(*CLHS, *CRHS, Pred);
+    return compareFloat(Pred, *CLHS, *CRHS, CxtF);
 
   const Value *X = nullptr;
   PatternKind PK =
-      classifyCmpPatternAndAnchorConstants(Pred, X, LHS, RHS, CLHS, CRHS);
+      classifyCmpPatternAndAnchorConstants(Pred, X, LHS, RHS, CLHS, CRHS, CxtF);
   if (PK == MATCH_NONE)
     return false;
 
@@ -9904,11 +9937,11 @@ bool isTrueFPPredicate(CmpInst::Predicate Pred, const Value *LHS,
       // (X + CA) u<= (X + CB) if CA u<= CB
       if (match(LHS, m_c_FAdd(m_Specific(X), m_APFloat(CLHS))) &&
           match(RHS, m_c_FAdd(m_Specific(X), m_APFloat(CRHS))))
-        return FCmpInst::compare(*CLHS, *CRHS, FCmpInst::FCMP_ULE);
+        return compareFloat(FCmpInst::FCMP_ULE, *CLHS, *CRHS, CxtF);
       // (X - CA) u<= (X - CB) if CA u>= CB
       if (match(LHS, m_FSub(m_Specific(X), m_APFloat(CLHS))) &&
           match(RHS, m_FSub(m_Specific(X), m_APFloat(CRHS))))
-        return FCmpInst::compare(*CLHS, *CRHS, FCmpInst::FCMP_UGE);
+        return compareFloat(FCmpInst::FCMP_UGE, *CLHS, *CRHS, CxtF);
 
       // LHS u<= X u<= RHS --> LHS u<= RHS
       if (isTrueFPPredicate(CmpInst::FCMP_ULE, LHS, X) &&
@@ -9924,11 +9957,11 @@ bool isTrueFPPredicate(CmpInst::Predicate Pred, const Value *LHS,
 /// Return true if "cmp Pred LHS RHS" is always true.
 /// This function only matches simple patterns and does not use Known* APIs.
 bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
-                     const Value *RHS) {
+                     const Value *RHS, const Instruction *ContextI = nullptr) {
   if (CmpInst::isIntPredicate(Pred))
     return isTrueIntPredicate(Pred, LHS, RHS);
   if (CmpInst::isFPPredicate(Pred))
-    return isTrueFPPredicate(Pred, LHS, RHS);
+    return isTrueFPPredicate(Pred, LHS, RHS, ContextI);
   return false;
 }
 
@@ -10382,9 +10415,9 @@ std::optional<bool> llvm::isImpliedByDomCondition(CmpPredicate Pred,
                                                   const Instruction *ContextI,
                                                   const DataLayout &DL) {
   // Check Pred, LHS, RHS just by data relation, e.g., x < max(x, y)
-  if (isTruePredicate(Pred, LHS, RHS))
+  if (isTruePredicate(Pred, LHS, RHS, ContextI))
     return true;
-  if (isTruePredicate(CmpInst::getInversePredicate(Pred), LHS, RHS))
+  if (isTruePredicate(CmpInst::getInversePredicate(Pred), LHS, RHS, ContextI))
     return false;
 
   // Check Pred, LHS, RHS by dom condtion.

>From 2275c83a603df155ed736e1a0916afbed97805e6 Mon Sep 17 00:00:00 2001
From: Camsyn <camsyn at foxmail.com>
Date: Wed, 18 Mar 2026 01:00:56 +0800
Subject: [PATCH 5/9] Fix mismatched types from  isImpliedCondICmps

Triggerred test: llvm-opt-bench/bench/quickjs/original/libbf.ll
---
 llvm/lib/Analysis/ValueTracking.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index b83c3ed579aa5..5f23c581f7aae 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9542,6 +9542,9 @@ PatternKind classifyCmpPatternAndAnchorConstants(
 /// Return true if "icmp Pred LHS RHS" is always true.
 bool isTrueIntPredicate(CmpInst::Predicate Pred, const Value *LHS,
                         const Value *RHS) {
+  if (LHS->getType() != RHS->getType())
+    return false;
+
   switch (Pred) {
   default:
     break;
@@ -9824,6 +9827,9 @@ bool isTrueIntPredicate(CmpInst::Predicate Pred, const Value *LHS,
 /// Return true if "fcmp Pred LHS RHS" is always true.
 bool isTrueFPPredicate(CmpInst::Predicate Pred, const Value *LHS,
                        const Value *RHS, const Instruction *ContextI = nullptr) {
+  if (LHS->getType() != RHS->getType())
+    return false;
+
   if (Pred == CmpInst::FCMP_TRUE)
     return true;
 

>From 4fae2d39655e0fe09ae00f6dd9f4a6eca6c848c6 Mon Sep 17 00:00:00 2001
From: Camsyn <camsyn at foxmail.com>
Date: Wed, 18 Mar 2026 17:37:46 +0800
Subject: [PATCH 6/9] Make formatter happy

---
 llvm/lib/Analysis/ValueTracking.cpp           | 3 ++-
 llvm/unittests/Analysis/ValueTrackingTest.cpp | 6 ++++--
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 5f23c581f7aae..6db8a0875845f 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9826,7 +9826,8 @@ bool isTrueIntPredicate(CmpInst::Predicate Pred, const Value *LHS,
 
 /// Return true if "fcmp Pred LHS RHS" is always true.
 bool isTrueFPPredicate(CmpInst::Predicate Pred, const Value *LHS,
-                       const Value *RHS, const Instruction *ContextI = nullptr) {
+                       const Value *RHS,
+                       const Instruction *ContextI = nullptr) {
   if (LHS->getType() != RHS->getType())
     return false;
 
diff --git a/llvm/unittests/Analysis/ValueTrackingTest.cpp b/llvm/unittests/Analysis/ValueTrackingTest.cpp
index 7cc6eab804557..dc8637894c543 100644
--- a/llvm/unittests/Analysis/ValueTrackingTest.cpp
+++ b/llvm/unittests/Analysis/ValueTrackingTest.cpp
@@ -2611,7 +2611,8 @@ TEST_F(ValueTrackingTest, IsImpliedConditionDoesNotInferFromUnrelatedPatterns) {
     EXPECT_EQ(isImpliedCondition(A, Cmp, DL), std::nullopt);
 }
 
-TEST_F(ValueTrackingTest, IsImpliedByDomConditionOrderedFPDistinguishesMaximum) {
+TEST_F(ValueTrackingTest,
+       IsImpliedByDomConditionOrderedFPDistinguishesMaximum) {
   parseAssembly(R"(
     declare float @llvm.maxnum.f32(float, float)
     declare float @llvm.maximum.f32(float, float)
@@ -2676,7 +2677,8 @@ TEST_F(ValueTrackingTest, IsImpliedByDomConditionOrderedFPDistinguishesMaximum)
             true);
 }
 
-TEST_F(ValueTrackingTest, IsImpliedByDomConditionUnorderedFPAcceptsAllFamilies) {
+TEST_F(ValueTrackingTest,
+       IsImpliedByDomConditionUnorderedFPAcceptsAllFamilies) {
   parseAssembly(R"(
     declare float @llvm.maxnum.f32(float, float)
     declare float @llvm.maximum.f32(float, float)

>From 1bee8158177d8bd83631b119468c03b6a835bae7 Mon Sep 17 00:00:00 2001
From: Camsyn <camsyn at foxmail.com>
Date: Wed, 18 Mar 2026 19:06:20 +0800
Subject: [PATCH 7/9] Fix incorrect assertions

---
 llvm/lib/Analysis/ValueTracking.cpp | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 6db8a0875845f..3eb681815bd96 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9705,8 +9705,9 @@ bool isTrueIntPredicate(CmpInst::Predicate Pred, const Value *LHS,
       if (match(RHS, m_c_NUWAdd(m_Specific(LHS), m_Value(V))))
         return CanEq ? true
                      : (m = match(V, m_APInt(C)),
-                        assert(!C->isZero() && "x + 0 should be folded before"),
-                        m);
+                        m && (assert(!C->isZero() &&
+                                     "x + 0 should be folded before"),
+                              true));
       // LHS u<= LHS *_{nuw} C for any C (C = 0,1 should be folded before)
       // ult: cannot exclude LHS == 0
       if (CanEq && (match(RHS, m_NUWMul(m_Specific(LHS), m_APInt(C))) ||
@@ -9716,7 +9717,7 @@ bool isTrueIntPredicate(CmpInst::Predicate Pred, const Value *LHS,
       // LHS u<  LHS |_{disjoint} C for any C (C = 0 should be folded before)
       if (CanEq ? match(RHS, m_c_Or(m_Specific(LHS), m_Value()))
                 : match(RHS, m_c_DisjointOr(m_Specific(LHS), m_APInt(C)))) {
-        assert(!C->isZero() && "x | 0 should be folded before");
+        assert((CanEq || !C->isZero()) && "x | 0 should be folded before");
         return true;
       }
       // LHS u<= LHS <<_{nuw} V for any V (V < 0 is UB)
@@ -9726,11 +9727,11 @@ bool isTrueIntPredicate(CmpInst::Predicate Pred, const Value *LHS,
       // LHS u<= (V <<_{nuw} LHS) for any V
       // LHS u<  (C <<_{nuw} LHS) for any C (C = 0 should be folded before)
       if (match(RHS, m_NUWShl(m_Value(V), m_Specific(LHS))))
-        return CanEq
-                   ? true
-                   : (m = match(V, m_APInt(C)),
-                      assert(!C->isZero() && "0 << V should be folded before"),
-                      m);
+        return CanEq ? true
+                     : (m = match(V, m_APInt(C)),
+                        m && (assert(!C->isZero() &&
+                                     "0 << V should be folded before"),
+                              true));
 
       // LHS u<= umax(LHS, V) for any V
       if (CanEq && match(RHS, m_UMax(m_Value(), m_Value())))
@@ -9743,8 +9744,9 @@ bool isTrueIntPredicate(CmpInst::Predicate Pred, const Value *LHS,
       if (match(LHS, m_NUWSub(m_Specific(RHS), m_Value(V))))
         return CanEq ? true
                      : (m = match(V, m_APInt(C)),
-                        assert(!C->isZero() && "x - 0 should be folded before"),
-                        m);
+                        m && (assert(!C->isZero() &&
+                                     "x - 0 should be folded before"),
+                              true));
       // (RHS >> V) u<= RHS for any V (V < 0 is UB)
       // ult: cannot exclude RHS == 0
       if (CanEq && match(LHS, m_LShr(m_Specific(RHS), m_Value(V))))

>From 12a1174a093a34c324108a47cb245d515d0aa1c8 Mon Sep 17 00:00:00 2001
From: Camsyn <camsyn at foxmail.com>
Date: Wed, 18 Mar 2026 21:40:45 +0800
Subject: [PATCH 8/9] Fix comments

---
 llvm/lib/Analysis/ValueTracking.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 3eb681815bd96..aeff4900ce1a2 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9614,7 +9614,7 @@ bool isTrueIntPredicate(CmpInst::Predicate Pred, const Value *LHS,
           cast<OverflowingBinaryOperator>(RHS)->hasNoUnsignedWrap())
         return true;
       // LHS s<= LHS | C         if C >= 0
-      // LHS s<  LHS |_{disjoint} C for any C (C = 0 should be folded before)
+      // LHS s<  LHS |_{disjoint} C if C > 0 (C = 0 should be folded before)
       if (CanEq ? match(RHS, m_c_Or(m_Specific(LHS), m_APInt(C)))
                 : match(RHS, m_c_DisjointOr(m_Specific(LHS), m_APInt(C)))) {
         return C->isNonNegative();

>From b79416446458febc88200b9a343cc1b8ac36798d Mon Sep 17 00:00:00 2001
From: Camsyn <camsyn at foxmail.com>
Date: Wed, 18 Mar 2026 23:09:35 +0800
Subject: [PATCH 9/9] Follow copilot's comments

---
 llvm/lib/Analysis/ValueTracking.cpp | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index aeff4900ce1a2..459b1ab39b4dc 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9441,7 +9441,7 @@ std::optional<APFloat> getCompareAPFloat(const APFloat &C,
                               : false);
 }
 
-// If LHS Pred RHS is alwasy true, return true.
+// If LHS Pred RHS is always true, return true.
 // This function = FCmpInst::compare + DenormalMode
 bool compareFloat(const FCmpInst::Predicate Pred, const APFloat &LHS,
                   const APFloat &RHS, const Function *CxtF) {
@@ -9840,8 +9840,14 @@ bool isTrueFPPredicate(CmpInst::Predicate Pred, const Value *LHS,
   if (CanEqual && LHS == RHS)
     return true;
 
-  if (Pred == CmpInst::FCMP_UGE)
-    return isTrueFPPredicate(CmpInst::FCMP_ULE, RHS, LHS);
+  switch (Pred) {
+  default:
+    break;
+  case CmpInst::FCMP_OGE:
+    return isTrueIntPredicate(FCmpInst::FCMP_OLE, RHS, LHS);
+  case CmpInst::FCMP_UGE:
+    return isTrueIntPredicate(FCmpInst::FCMP_ULE, RHS, LHS);
+  }
 
   if (CmpInst::isUnordered(Pred) &&
       (match(LHS, m_NaN()) || match(RHS, m_NaN())))
@@ -9887,8 +9893,6 @@ bool isTrueFPPredicate(CmpInst::Predicate Pred, const Value *LHS,
   case CmpInst::FCMP_OLT:
     // Hard to ensure NaN-free.
     return false;
-  case CmpInst::FCMP_OGE:
-    return isTrueFPPredicate(CmpInst::FCMP_OLE, RHS, LHS);
   case CmpInst::FCMP_ORD:
   case CmpInst::FCMP_OLE: {
     const APFloat *C;
@@ -9899,7 +9903,6 @@ bool isTrueFPPredicate(CmpInst::Predicate Pred, const Value *LHS,
     }
     // omin(V, C) o<= C for any V (C = NaN should be folded before)
     if (PK == MATCH_LHS && match(RHS, m_APFloat(C)) && MatchOrderedFMin(LHS)) {
-
       assert(!C->isNaN() && "min(NaN, RHS) should be folded before");
       return true;
     }
@@ -10429,7 +10432,7 @@ std::optional<bool> llvm::isImpliedByDomCondition(CmpPredicate Pred,
   if (isTruePredicate(CmpInst::getInversePredicate(Pred), LHS, RHS, ContextI))
     return false;
 
-  // Check Pred, LHS, RHS by dom condtion.
+  // Check Pred, LHS, RHS by dom condition.
   auto PredCond = getDomPredecessorCondition(ContextI);
   if (PredCond.first)
     return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,



More information about the llvm-commits mailing list