[llvm] [InstCombine] Recognize X - 1 as nonnegative when X is positive (PR #223168)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 20 10:21:01 PDT 2026
https://github.com/addmisol updated https://github.com/llvm/llvm-project/pull/223168
>From dec1143f913a3080923b8e6fdfec86cf9ade8d98 Mon Sep 17 00:00:00 2001
From: addmisol <addmisol9 at gmail.com>
Date: Sun, 13 Sep 2026 00:45:41 +0530
Subject: [PATCH 1/5] [InstCombine] Recognize X - 1 as nonnegative when X is
positive
Signed-off-by: addmisol <addmisol9 at gmail.com>
---
llvm/lib/Analysis/ValueTracking.cpp | 24 ++++++++++++++++-----
llvm/test/Transforms/InstCombine/bitcast.ll | 22 +++++++++++++++++++
llvm/test/Transforms/InstCombine/divceil.ll | 2 +-
3 files changed, 42 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 1580571a01451..74c078867ee14 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -531,11 +531,25 @@ static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
- if (!Add && NSW && !KnownOut.isNonNegative() &&
- (isImpliedByDomCondition(ICmpInst::ICMP_SLE, Op1, Op0, Q.CxtI, Q.DL)
- .value_or(false) ||
- match(Op1, m_c_SMin(m_Specific(Op0), m_Value()))))
- KnownOut.makeNonNegative();
+ if (NSW && !KnownOut.isNonNegative()) {
+ if (!Add) {
+ // X - Y is nonnegative if Y <= X (signed).
+ if (isImpliedByDomCondition(ICmpInst::ICMP_SLE, Op1, Op0, Q.CxtI, Q.DL)
+ .value_or(false) ||
+ match(Op1, m_c_SMin(m_Specific(Op0), m_Value())))
+ KnownOut.makeNonNegative();
+ // X - 1 is nonnegative if X is known positive (X > 0 implies X >= 1).
+ else if (match(Op1, m_One()) && Known2.isNonNegative() &&
+ (Known2.isNonZero() || isKnownNonZero(Op0, Q, Depth + 1)))
+ KnownOut.makeNonNegative();
+ } else {
+ // X + (-1) is nonnegative if X is known positive (X > 0 implies X >= 1).
+ // This handles "add nsw X, -1" which is semantically "X - 1".
+ if (match(Op1, m_AllOnes()) && Known2.isNonNegative() &&
+ (Known2.isNonZero() || isKnownNonZero(Op0, Q, Depth + 1)))
+ KnownOut.makeNonNegative();
+ }
+ }
if (Add)
// Try to match lerp pattern and combine results
diff --git a/llvm/test/Transforms/InstCombine/bitcast.ll b/llvm/test/Transforms/InstCombine/bitcast.ll
index a81eb5000f63c..34e75a7e90951 100644
--- a/llvm/test/Transforms/InstCombine/bitcast.ll
+++ b/llvm/test/Transforms/InstCombine/bitcast.ll
@@ -903,3 +903,25 @@ end: ; preds = %unreachable, %entry
%3 = extractelement <2 x i16> %2, i64 0
ret i16 %3
}
+
+; Issue #221943: X - 1 should be nonnegative when X > 0.
+; The assume that %magx > 0 should allow InstCombine to recognize that
+; %mag = %magx - 1 is nonnegative, enabling the copysign idiom fold.
+define float @copysign_idiom_sub1_positive(float %x, i32 %magx) {
+; CHECK-LABEL: @copysign_idiom_sub1_positive(
+; CHECK-NEXT: [[MAG:%.*]] = add nsw i32 [[MAGX:%.*]], -1
+; CHECK-NEXT: [[POSITIVE:%.*]] = icmp sgt i32 [[MAGX]], 0
+; CHECK-NEXT: call void @llvm.assume(i1 [[POSITIVE]])
+; CHECK-NEXT: [[TMP1:%.*]] = bitcast i32 [[MAG]] to float
+; CHECK-NEXT: [[Y:%.*]] = call float @llvm.copysign.f32(float [[TMP1]], float [[X:%.*]])
+; CHECK-NEXT: ret float [[Y]]
+;
+ %mag = add nsw i32 %magx, -1
+ %positive = icmp sgt i32 %magx, 0
+ call void @llvm.assume(i1 %positive)
+ %bits = bitcast float %x to i32
+ %sign = and i32 %bits, -2147483648
+ %res = or i32 %mag, %sign
+ %y = bitcast i32 %res to float
+ ret float %y
+}
diff --git a/llvm/test/Transforms/InstCombine/divceil.ll b/llvm/test/Transforms/InstCombine/divceil.ll
index c9f3ba750d7ec..e43d6c28fcd27 100644
--- a/llvm/test/Transforms/InstCombine/divceil.ll
+++ b/llvm/test/Transforms/InstCombine/divceil.ll
@@ -138,7 +138,7 @@ define i32 @divceil_i16_var_divisor_zext(i16 range(i16 0, 101) %x, i16 range(i16
; CHECK-NEXT: [[TMP1:%.*]] = add nsw i16 [[Y:%.*]], -1
; CHECK-NEXT: [[TMP2:%.*]] = add nuw nsw i16 [[X:%.*]], [[TMP1]]
; CHECK-NEXT: [[TMP3:%.*]] = udiv i16 [[TMP2]], [[Y]]
-; CHECK-NEXT: [[RESULT:%.*]] = zext i16 [[TMP3]] to i32
+; CHECK-NEXT: [[RESULT:%.*]] = zext nneg i16 [[TMP3]] to i32
; CHECK-NEXT: ret i32 [[RESULT]]
;
%q = udiv i16 %x, %y
>From 793e43272a58b53de1eeff7dd700ffeae650b7c0 Mon Sep 17 00:00:00 2001
From: Addmisol <addmisol9 at gmail.com>
Date: Sun, 13 Sep 2026 01:31:51 +0530
Subject: [PATCH 2/5] fix: add tests | fix issues
---
llvm/test/Transforms/InstCombine/bitcast.ll | 83 ++++++++++++++++++++-
1 file changed, 82 insertions(+), 1 deletion(-)
diff --git a/llvm/test/Transforms/InstCombine/bitcast.ll b/llvm/test/Transforms/InstCombine/bitcast.ll
index 34e75a7e90951..ee61761221a93 100644
--- a/llvm/test/Transforms/InstCombine/bitcast.ll
+++ b/llvm/test/Transforms/InstCombine/bitcast.ll
@@ -916,7 +916,7 @@ define float @copysign_idiom_sub1_positive(float %x, i32 %magx) {
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.copysign.f32(float [[TMP1]], float [[X:%.*]])
; CHECK-NEXT: ret float [[Y]]
;
- %mag = add nsw i32 %magx, -1
+ %mag = add i32 %magx, -1
%positive = icmp sgt i32 %magx, 0
call void @llvm.assume(i1 %positive)
%bits = bitcast float %x to i32
@@ -925,3 +925,84 @@ define float @copysign_idiom_sub1_positive(float %x, i32 %magx) {
%y = bitcast i32 %res to float
ret float %y
}
+
+; Test with sub instruction instead of add with -1.
+define float @copysign_idiom_sub1_positive_sub(float %x, i32 %magx) {
+; CHECK-LABEL: @copysign_idiom_sub1_positive_sub(
+; CHECK-NEXT: [[MAG:%.*]] = add nsw i32 [[MAGX:%.*]], -1
+; CHECK-NEXT: [[POSITIVE:%.*]] = icmp sgt i32 [[MAGX]], 0
+; CHECK-NEXT: call void @llvm.assume(i1 [[POSITIVE]])
+; CHECK-NEXT: [[TMP1:%.*]] = bitcast i32 [[MAG]] to float
+; CHECK-NEXT: [[Y:%.*]] = call float @llvm.copysign.f32(float [[TMP1]], float [[X:%.*]])
+; CHECK-NEXT: ret float [[Y]]
+;
+ %mag = sub i32 %magx, 1
+ %positive = icmp sgt i32 %magx, 0
+ call void @llvm.assume(i1 %positive)
+ %bits = bitcast float %x to i32
+ %sign = and i32 %bits, -2147483648
+ %res = or i32 %mag, %sign
+ %y = bitcast i32 %res to float
+ ret float %y
+}
+
+; Test with i64 for double precision.
+define double @copysign_idiom_sub1_positive_f64(double %x, i64 %magx) {
+; CHECK-LABEL: @copysign_idiom_sub1_positive_f64(
+; CHECK-NEXT: [[MAG:%.*]] = add nsw i64 [[MAGX:%.*]], -1
+; CHECK-NEXT: [[POSITIVE:%.*]] = icmp sgt i64 [[MAGX]], 0
+; CHECK-NEXT: call void @llvm.assume(i1 [[POSITIVE]])
+; CHECK-NEXT: [[TMP1:%.*]] = bitcast i64 [[MAG]] to double
+; CHECK-NEXT: [[Y:%.*]] = call double @llvm.copysign.f64(double [[TMP1]], double [[X:%.*]])
+; CHECK-NEXT: ret double [[Y]]
+;
+ %mag = add i64 %magx, -1
+ %positive = icmp sgt i64 %magx, 0
+ call void @llvm.assume(i1 %positive)
+ %bits = bitcast double %x to i64
+ %sign = and i64 %bits, -9223372036854775808
+ %res = or i64 %mag, %sign
+ %y = bitcast i64 %res to double
+ ret double %y
+}
+
+; Negative test: %magx >= 0 (nonnegative) is not enough, need %magx > 0 (positive).
+; %magx could be 0, so %mag = %magx - 1 could be -1.
+define float @copysign_idiom_sub1_nonnegative_not_positive(float %x, i32 %magx) {
+; CHECK-LABEL: @copysign_idiom_sub1_nonnegative_not_positive(
+; CHECK-NEXT: [[MAG:%.*]] = add nsw i32 [[MAGX:%.*]], -1
+; CHECK-NEXT: [[NONNEG:%.*]] = icmp sgt i32 [[MAGX]], -1
+; CHECK-NEXT: call void @llvm.assume(i1 [[NONNEG]])
+; CHECK-NEXT: [[BITS:%.*]] = bitcast float [[X:%.*]] to i32
+; CHECK-NEXT: [[SIGN:%.*]] = and i32 [[BITS]], -2147483648
+; CHECK-NEXT: [[RES:%.*]] = or i32 [[MAG]], [[SIGN]]
+; CHECK-NEXT: [[Y:%.*]] = bitcast i32 [[RES]] to float
+; CHECK-NEXT: ret float [[Y]]
+;
+ %mag = add i32 %magx, -1
+ %nonneg = icmp sgt i32 %magx, -1
+ call void @llvm.assume(i1 %nonneg)
+ %bits = bitcast float %x to i32
+ %sign = and i32 %bits, -2147483648
+ %res = or i32 %mag, %sign
+ %y = bitcast i32 %res to float
+ ret float %y
+}
+
+; Negative test: no assume, so we don't know if %magx is positive.
+define float @copysign_idiom_sub1_no_assume(float %x, i32 %magx) {
+; CHECK-LABEL: @copysign_idiom_sub1_no_assume(
+; CHECK-NEXT: [[MAG:%.*]] = add i32 [[MAGX:%.*]], -1
+; CHECK-NEXT: [[BITS:%.*]] = bitcast float [[X:%.*]] to i32
+; CHECK-NEXT: [[SIGN:%.*]] = and i32 [[BITS]], -2147483648
+; CHECK-NEXT: [[RES:%.*]] = or i32 [[MAG]], [[SIGN]]
+; CHECK-NEXT: [[Y:%.*]] = bitcast i32 [[RES]] to float
+; CHECK-NEXT: ret float [[Y]]
+;
+ %mag = add i32 %magx, -1
+ %bits = bitcast float %x to i32
+ %sign = and i32 %bits, -2147483648
+ %res = or i32 %mag, %sign
+ %y = bitcast i32 %res to float
+ ret float %y
+}
>From abe317f811690354392e3c2080447cc2f8079699 Mon Sep 17 00:00:00 2001
From: Addmisol <addmisol9 at gmail.com>
Date: Sun, 13 Sep 2026 02:13:11 +0530
Subject: [PATCH 3/5] fix: update tests
---
llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll b/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
index afabf6ce0fdf2..7a0255f18002a 100644
--- a/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
+++ b/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
@@ -261,7 +261,7 @@ define i64 @known_power_of_two_urem_loop_ashr(i64 %size, i64 %a) {
; CHECK-NEXT: [[SUM:%.*]] = phi i64 [ 0, [[ENTRY]] ], [ [[ADD:%.*]], [[FOR_BODY]] ]
; CHECK-NEXT: [[TMP0:%.*]] = add nsw i64 [[PHI]], -1
; CHECK-NEXT: [[UREM:%.*]] = and i64 [[SIZE:%.*]], [[TMP0]]
-; CHECK-NEXT: [[ADD]] = add nsw i64 [[SUM]], [[UREM]]
+; CHECK-NEXT: [[ADD]] = add nuw nsw i64 [[SUM]], [[UREM]]
; CHECK-NEXT: [[I]] = lshr i64 [[PHI]], [[A:%.*]]
; CHECK-NEXT: [[ICMP_NOT:%.*]] = icmp eq i64 [[I]], 0
; CHECK-NEXT: br i1 [[ICMP_NOT]], label [[FOR_END:%.*]], label [[FOR_BODY]]
>From ea5c63471e371fe427621247ba3662819c677cf0 Mon Sep 17 00:00:00 2001
From: addmisol <addmisol9 at gmail.com>
Date: Sun, 20 Sep 2026 22:42:39 +0530
Subject: [PATCH 4/5] fix(test): fix isses | address comments
Signed-off-by: addmisol <addmisol9 at gmail.com>
---
llvm/lib/Analysis/ValueTracking.cpp | 18 +--
llvm/test/Transforms/InstCombine/bitcast.ll | 119 ++++++++++++++++++--
2 files changed, 119 insertions(+), 18 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 183642445f5fe..42e655b6203f1 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -535,23 +535,25 @@ static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
+ // Determine if the result is nonnegative based on NSW flag and operand info.
if (NSW && !KnownOut.isNonNegative()) {
if (!Add) {
// X - Y is nonnegative if Y <= X (signed).
if (isImpliedByDomCondition(ICmpInst::ICMP_SLE, Op1, Op0, Q.CxtI, Q.DL)
.value_or(false) ||
- match(Op1, m_c_SMin(m_Specific(Op0), m_Value())))
+ match(Op1, m_c_SMin(m_Specific(Op0), m_Value()))) {
KnownOut.makeNonNegative();
- // X - 1 is nonnegative if X is known positive (X > 0 implies X >= 1).
- else if (match(Op1, m_One()) && Known2.isNonNegative() &&
- (Known2.isNonZero() || isKnownNonZero(Op0, Q, Depth + 1)))
+ } else if (match(Op1, m_One()) && Known2.isNonNegative() &&
+ (Known2.isNonZero() ||
+ isKnownNonZero(Op0, Q, Depth + 1))) {
+ // X - 1 is nonnegative if X is known positive (X > 0 implies X >= 1).
KnownOut.makeNonNegative();
- } else {
+ }
+ } else if (match(Op1, m_AllOnes()) && Known2.isNonNegative() &&
+ (Known2.isNonZero() || isKnownNonZero(Op0, Q, Depth + 1))) {
// X + (-1) is nonnegative if X is known positive (X > 0 implies X >= 1).
// This handles "add nsw X, -1" which is semantically "X - 1".
- if (match(Op1, m_AllOnes()) && Known2.isNonNegative() &&
- (Known2.isNonZero() || isKnownNonZero(Op0, Q, Depth + 1)))
- KnownOut.makeNonNegative();
+ KnownOut.makeNonNegative();
}
}
diff --git a/llvm/test/Transforms/InstCombine/bitcast.ll b/llvm/test/Transforms/InstCombine/bitcast.ll
index ee61761221a93..cfb347f8a07bf 100644
--- a/llvm/test/Transforms/InstCombine/bitcast.ll
+++ b/llvm/test/Transforms/InstCombine/bitcast.ll
@@ -905,10 +905,9 @@ end: ; preds = %unreachable, %entry
}
; Issue #221943: X - 1 should be nonnegative when X > 0.
-; The assume that %magx > 0 should allow InstCombine to recognize that
-; %mag = %magx - 1 is nonnegative, enabling the copysign idiom fold.
-define float @copysign_idiom_sub1_positive(float %x, i32 %magx) {
-; CHECK-LABEL: @copysign_idiom_sub1_positive(
+; Test with llvm.assume to indicate %magx > 0.
+define float @copysign_idiom_sub1_positive_assume(float %x, i32 %magx) {
+; CHECK-LABEL: @copysign_idiom_sub1_positive_assume(
; CHECK-NEXT: [[MAG:%.*]] = add nsw i32 [[MAGX:%.*]], -1
; CHECK-NEXT: [[POSITIVE:%.*]] = icmp sgt i32 [[MAGX]], 0
; CHECK-NEXT: call void @llvm.assume(i1 [[POSITIVE]])
@@ -926,9 +925,9 @@ define float @copysign_idiom_sub1_positive(float %x, i32 %magx) {
ret float %y
}
-; Test with sub instruction instead of add with -1.
-define float @copysign_idiom_sub1_positive_sub(float %x, i32 %magx) {
-; CHECK-LABEL: @copysign_idiom_sub1_positive_sub(
+; Test with sub instruction and assume.
+define float @copysign_idiom_sub1_positive_sub_assume(float %x, i32 %magx) {
+; CHECK-LABEL: @copysign_idiom_sub1_positive_sub_assume(
; CHECK-NEXT: [[MAG:%.*]] = add nsw i32 [[MAGX:%.*]], -1
; CHECK-NEXT: [[POSITIVE:%.*]] = icmp sgt i32 [[MAGX]], 0
; CHECK-NEXT: call void @llvm.assume(i1 [[POSITIVE]])
@@ -946,9 +945,9 @@ define float @copysign_idiom_sub1_positive_sub(float %x, i32 %magx) {
ret float %y
}
-; Test with i64 for double precision.
-define double @copysign_idiom_sub1_positive_f64(double %x, i64 %magx) {
-; CHECK-LABEL: @copysign_idiom_sub1_positive_f64(
+; Test with i64 for double precision using assume.
+define double @copysign_idiom_sub1_positive_f64_assume(double %x, i64 %magx) {
+; CHECK-LABEL: @copysign_idiom_sub1_positive_f64_assume(
; CHECK-NEXT: [[MAG:%.*]] = add nsw i64 [[MAGX:%.*]], -1
; CHECK-NEXT: [[POSITIVE:%.*]] = icmp sgt i64 [[MAGX]], 0
; CHECK-NEXT: call void @llvm.assume(i1 [[POSITIVE]])
@@ -1006,3 +1005,103 @@ define float @copysign_idiom_sub1_no_assume(float %x, i32 %magx) {
%y = bitcast i32 %res to float
ret float %y
}
+
+; Vector test: X - 1 with known positive X via range metadata.
+define <4 x float> @copysign_idiom_sub1_positive_vec(<4 x float> %x, <4 x i32> range(i32 1, 2147483647) %magx) {
+; CHECK-LABEL: @copysign_idiom_sub1_positive_vec(
+; CHECK-NEXT: [[MAG:%.*]] = add nsw <4 x i32> [[MAGX:%.*]], splat (i32 -1)
+; CHECK-NEXT: [[TMP1:%.*]] = bitcast <4 x i32> [[MAG]] to <4 x float>
+; CHECK-NEXT: [[Y:%.*]] = call <4 x float> @llvm.copysign.v4f32(<4 x float> [[TMP1]], <4 x float> [[X:%.*]])
+; CHECK-NEXT: ret <4 x float> [[Y]]
+;
+ %mag = add <4 x i32> %magx, splat (i32 -1)
+ %bits = bitcast <4 x float> %x to <4 x i32>
+ %sign = and <4 x i32> %bits, splat (i32 -2147483648)
+ %res = or <4 x i32> %mag, %sign
+ %y = bitcast <4 x i32> %res to <4 x float>
+ ret <4 x float> %y
+}
+
+; Vector test with sub instruction.
+define <2 x float> @copysign_idiom_sub1_positive_vec_sub(<2 x float> %x, <2 x i32> range(i32 1, 2147483647) %magx) {
+; CHECK-LABEL: @copysign_idiom_sub1_positive_vec_sub(
+; CHECK-NEXT: [[MAG:%.*]] = add nsw <2 x i32> [[MAGX:%.*]], splat (i32 -1)
+; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i32> [[MAG]] to <2 x float>
+; CHECK-NEXT: [[Y:%.*]] = call <2 x float> @llvm.copysign.v2f32(<2 x float> [[TMP1]], <2 x float> [[X:%.*]])
+; CHECK-NEXT: ret <2 x float> [[Y]]
+;
+ %mag = sub <2 x i32> %magx, splat (i32 1)
+ %bits = bitcast <2 x float> %x to <2 x i32>
+ %sign = and <2 x i32> %bits, splat (i32 -2147483648)
+ %res = or <2 x i32> %mag, %sign
+ %y = bitcast <2 x i32> %res to <2 x float>
+ ret <2 x float> %y
+}
+
+; Vector test with double precision (2 x double).
+define <2 x double> @copysign_idiom_sub1_positive_vec_f64(<2 x double> %x, <2 x i64> range(i64 1, 9223372036854775807) %magx) {
+; CHECK-LABEL: @copysign_idiom_sub1_positive_vec_f64(
+; CHECK-NEXT: [[MAG:%.*]] = add nsw <2 x i64> [[MAGX:%.*]], splat (i64 -1)
+; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> [[MAG]] to <2 x double>
+; CHECK-NEXT: [[Y:%.*]] = call <2 x double> @llvm.copysign.v2f64(<2 x double> [[TMP1]], <2 x double> [[X:%.*]])
+; CHECK-NEXT: ret <2 x double> [[Y]]
+;
+ %mag = add <2 x i64> %magx, splat (i64 -1)
+ %bits = bitcast <2 x double> %x to <2 x i64>
+ %sign = and <2 x i64> %bits, splat (i64 -9223372036854775808)
+ %res = or <2 x i64> %mag, %sign
+ %y = bitcast <2 x i64> %res to <2 x double>
+ ret <2 x double> %y
+}
+
+; Vector test with 8 elements.
+define <8 x float> @copysign_idiom_sub1_positive_vec8(<8 x float> %x, <8 x i32> range(i32 1, 2147483647) %magx) {
+; CHECK-LABEL: @copysign_idiom_sub1_positive_vec8(
+; CHECK-NEXT: [[MAG:%.*]] = add nsw <8 x i32> [[MAGX:%.*]], splat (i32 -1)
+; CHECK-NEXT: [[TMP1:%.*]] = bitcast <8 x i32> [[MAG]] to <8 x float>
+; CHECK-NEXT: [[Y:%.*]] = call <8 x float> @llvm.copysign.v8f32(<8 x float> [[TMP1]], <8 x float> [[X:%.*]])
+; CHECK-NEXT: ret <8 x float> [[Y]]
+;
+ %mag = add <8 x i32> %magx, splat (i32 -1)
+ %bits = bitcast <8 x float> %x to <8 x i32>
+ %sign = and <8 x i32> %bits, splat (i32 -2147483648)
+ %res = or <8 x i32> %mag, %sign
+ %y = bitcast <8 x i32> %res to <8 x float>
+ ret <8 x float> %y
+}
+
+; Negative vector test: nonnegative but not positive (range starts at 0).
+define <4 x float> @copysign_idiom_sub1_nonneg_not_pos_vec(<4 x float> %x, <4 x i32> range(i32 0, 2147483647) %magx) {
+; CHECK-LABEL: @copysign_idiom_sub1_nonneg_not_pos_vec(
+; CHECK-NEXT: [[MAG:%.*]] = add nsw <4 x i32> [[MAGX:%.*]], splat (i32 -1)
+; CHECK-NEXT: [[BITS:%.*]] = bitcast <4 x float> [[X:%.*]] to <4 x i32>
+; CHECK-NEXT: [[SIGN:%.*]] = and <4 x i32> [[BITS]], splat (i32 -2147483648)
+; CHECK-NEXT: [[RES:%.*]] = or <4 x i32> [[MAG]], [[SIGN]]
+; CHECK-NEXT: [[Y:%.*]] = bitcast <4 x i32> [[RES]] to <4 x float>
+; CHECK-NEXT: ret <4 x float> [[Y]]
+;
+ %mag = add <4 x i32> %magx, splat (i32 -1)
+ %bits = bitcast <4 x float> %x to <4 x i32>
+ %sign = and <4 x i32> %bits, splat (i32 -2147483648)
+ %res = or <4 x i32> %mag, %sign
+ %y = bitcast <4 x i32> %res to <4 x float>
+ ret <4 x float> %y
+}
+
+; Negative vector test: no range metadata.
+define <4 x float> @copysign_idiom_sub1_no_range_vec(<4 x float> %x, <4 x i32> %magx) {
+; CHECK-LABEL: @copysign_idiom_sub1_no_range_vec(
+; CHECK-NEXT: [[MAG:%.*]] = add <4 x i32> [[MAGX:%.*]], splat (i32 -1)
+; CHECK-NEXT: [[BITS:%.*]] = bitcast <4 x float> [[X:%.*]] to <4 x i32>
+; CHECK-NEXT: [[SIGN:%.*]] = and <4 x i32> [[BITS]], splat (i32 -2147483648)
+; CHECK-NEXT: [[RES:%.*]] = or <4 x i32> [[MAG]], [[SIGN]]
+; CHECK-NEXT: [[Y:%.*]] = bitcast <4 x i32> [[RES]] to <4 x float>
+; CHECK-NEXT: ret <4 x float> [[Y]]
+;
+ %mag = add <4 x i32> %magx, splat (i32 -1)
+ %bits = bitcast <4 x float> %x to <4 x i32>
+ %sign = and <4 x i32> %bits, splat (i32 -2147483648)
+ %res = or <4 x i32> %mag, %sign
+ %y = bitcast <4 x i32> %res to <4 x float>
+ ret <4 x float> %y
+}
>From 83235c8a6ae2aebad92c2a532518dc7d296bdfd2 Mon Sep 17 00:00:00 2001
From: Addmisol <addmisol9 at gmail.com>
Date: Sun, 20 Sep 2026 22:50:49 +0530
Subject: [PATCH 5/5] fix: code formatter issues
---
llvm/lib/Analysis/ValueTracking.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 42e655b6203f1..3445aae55400e 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -544,8 +544,7 @@ static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
match(Op1, m_c_SMin(m_Specific(Op0), m_Value()))) {
KnownOut.makeNonNegative();
} else if (match(Op1, m_One()) && Known2.isNonNegative() &&
- (Known2.isNonZero() ||
- isKnownNonZero(Op0, Q, Depth + 1))) {
+ (Known2.isNonZero() || isKnownNonZero(Op0, Q, Depth + 1))) {
// X - 1 is nonnegative if X is known positive (X > 0 implies X >= 1).
KnownOut.makeNonNegative();
}
More information about the llvm-commits
mailing list