[llvm-branch-commits] [llvm] 12290e2 - Revert "[ConstraintElim] A signed sum of operands with opposite signs cannot …"
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sat Nov 14 17:07:13 PST 2026
Author: Jon Roelofs
Date: 2026-11-14T17:07:09-08:00
New Revision: 12290e217daeb9d7c39c0555aa94f96a6d0f6511
URL: https://github.com/llvm/llvm-project/commit/12290e217daeb9d7c39c0555aa94f96a6d0f6511
DIFF: https://github.com/llvm/llvm-project/commit/12290e217daeb9d7c39c0555aa94f96a6d0f6511.diff
LOG: Revert "[ConstraintElim] A signed sum of operands with opposite signs cannot …"
This reverts commit 4b5da127ee5c9926a7189b5c55ef073a1af17b85.
Added:
Modified:
llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
llvm/test/Transforms/ConstraintElimination/add-nsw.ll
llvm/test/Transforms/ConstraintElimination/sadd-with-overflow.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 89d4291bd28166..c79aa32a677770 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -419,12 +419,10 @@ class ConstraintInfo {
/// system implies it or because ValueTracking can prove it.
bool isKnownPositive(Value *V);
- /// Returns the sign of \p V, if known. The result is false if \p V is known
- /// to be non-negative. Otherwise it is true if \p V is known to be negative
- /// (if \p StrictlyNegative), or non-positive (if not \p StrictlyNegative).
- /// Returns std::nullopt if neither ValueTracking, nor the signed system can
- /// prove either fact.
- std::optional<bool> getKnownSign(Value *V, bool StrictlyNegative);
+ /// Returns true if \p V is known to be negative, false if \p V is known to be
+ /// nonnegative, or std::nullopt if neither the signed system nor
+ /// ValueTracking can prove it.
+ std::optional<bool> getKnownSign(Value *V);
void addFact(CmpInst::Predicate Pred, Value *A, Value *B, unsigned NumIn,
unsigned NumOut, SmallVectorImpl<StackEntry> &DFSInStack);
@@ -630,16 +628,6 @@ static bool isKnownNoWrap(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
return true;
}
- // A signed sum of operands with opposite signs cannot wrap.
- if (Signed && Opcode == Instruction::Add) {
- std::optional<bool> Op0Sign =
- Info.getKnownSign(Op0, /*StrictlyNegative=*/false);
- std::optional<bool> Op1Sign =
- Info.getKnownSign(Op1, /*StrictlyNegative=*/false);
- if (Op0Sign && Op1Sign && *Op0Sign != *Op1Sign)
- return true;
- }
-
if (!Signed && (NoWrapFlags & OBO::NoSignedWrap) &&
(Opcode == Instruction::Shl || Info.isKnownNonNegative(Op1)) &&
Info.isKnownNonNegative(Op0))
@@ -1177,18 +1165,16 @@ bool ConstraintInfo::isKnownPositive(Value *V) {
doesHold(CmpInst::ICMP_SGT, V, ConstantInt::get(V->getType(), 0));
}
-std::optional<bool> ConstraintInfo::getKnownSign(Value *V,
- bool StrictlyNegative) {
+std::optional<bool> ConstraintInfo::getKnownSign(Value *V) {
KnownBits Known = computeKnownBits(V, DL);
if (Known.isNonNegative())
return false;
- if (StrictlyNegative ? Known.isNegative() : Known.isNonPositive())
+ if (Known.isNegative())
return true;
Constant *Zero = ConstantInt::get(V->getType(), 0);
if (doesHold(CmpInst::ICMP_SGE, V, Zero))
return false;
- auto NegativePred = StrictlyNegative ? CmpInst::ICMP_SLT : CmpInst::ICMP_SLE;
- if (doesHold(NegativePred, V, Zero))
+ if (doesHold(CmpInst::ICMP_SLT, V, Zero))
return true;
return std::nullopt;
}
@@ -2674,10 +2660,8 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
// The operands also have the same sign: if one of them is known to
// be non-negative (or negative), so is the other.
if (A->getType()->isIntegerTy()) {
- std::optional<bool> ASign =
- Info.getKnownSign(A, /*StrictlyNegative=*/true);
- std::optional<bool> BSign =
- Info.getKnownSign(B, /*StrictlyNegative=*/true);
+ std::optional<bool> ASign = Info.getKnownSign(A);
+ std::optional<bool> BSign = Info.getKnownSign(B);
if (ASign.has_value() != BSign.has_value()) {
bool IsNegative = ASign ? *ASign : *BSign;
Value *OtherOp = ASign ? B : A;
diff --git a/llvm/test/Transforms/ConstraintElimination/add-nsw.ll b/llvm/test/Transforms/ConstraintElimination/add-nsw.ll
index 757ed12e167308..07ad9a51f1912d 100644
--- a/llvm/test/Transforms/ConstraintElimination/add-nsw.ll
+++ b/llvm/test/Transforms/ConstraintElimination/add-nsw.ll
@@ -978,7 +978,8 @@ define i1 @add_non_positive_slt(i64 %x, i64 %y, i64 %b) {
; CHECK-NEXT: [[C:%.*]] = icmp slt i64 [[X]], [[B:%.*]]
; CHECK-NEXT: call void @llvm.assume(i1 [[C]])
; CHECK-NEXT: [[SUB:%.*]] = add i64 [[X]], [[Y]]
-; CHECK-NEXT: ret i1 true
+; CHECK-NEXT: [[T:%.*]] = icmp slt i64 [[SUB]], [[B]]
+; CHECK-NEXT: ret i1 [[T]]
;
%x.non.neg = icmp sge i64 %x, 0
call void @llvm.assume(i1 %x.non.neg)
@@ -1000,7 +1001,8 @@ define i1 @add_non_positive_slt_commuted(i64 %x, i64 %y, i64 %b) {
; CHECK-NEXT: [[C:%.*]] = icmp slt i64 [[X]], [[B:%.*]]
; CHECK-NEXT: call void @llvm.assume(i1 [[C]])
; CHECK-NEXT: [[SUB:%.*]] = add i64 [[Y]], [[X]]
-; CHECK-NEXT: ret i1 true
+; CHECK-NEXT: [[T:%.*]] = icmp slt i64 [[SUB]], [[B]]
+; CHECK-NEXT: ret i1 [[T]]
;
%x.non.neg = icmp sge i64 %x, 0
call void @llvm.assume(i1 %x.non.neg)
@@ -1024,7 +1026,8 @@ define i1 @add_non_positive_slt_hoisted(i64 %x, i64 %y, i64 %b) {
; CHECK-NEXT: [[SUB:%.*]] = add i64 [[X]], [[Y]]
; CHECK-NEXT: br i1 [[AND_2]], label [[IF_END:%.*]], label [[EXIT:%.*]]
; CHECK: if.end:
-; CHECK-NEXT: ret i1 true
+; CHECK-NEXT: [[T:%.*]] = icmp slt i64 [[SUB]], [[B]]
+; CHECK-NEXT: ret i1 [[T]]
; CHECK: exit:
; CHECK-NEXT: ret i1 false
;
diff --git a/llvm/test/Transforms/ConstraintElimination/sadd-with-overflow.ll b/llvm/test/Transforms/ConstraintElimination/sadd-with-overflow.ll
index df7d867003d68c..4194797fa5ea05 100644
--- a/llvm/test/Transforms/ConstraintElimination/sadd-with-overflow.ll
+++ b/llvm/test/Transforms/ConstraintElimination/sadd-with-overflow.ll
@@ -318,65 +318,3 @@ then:
else:
ret i8 0
}
-
-define void @sadd_opposite_signs(i64 %v, i64 %s) {
-; CHECK-LABEL: define void @sadd_opposite_signs(
-; CHECK-SAME: i64 [[V:%.*]], i64 [[S:%.*]]) {
-; CHECK-NEXT: [[ENTRY:.*:]]
-; CHECK-NEXT: [[A:%.*]] = lshr i64 [[V]], 1
-; CHECK-NEXT: [[C:%.*]] = icmp sge i64 [[S]], 0
-; CHECK-NEXT: call void @llvm.assume(i1 [[C]])
-; CHECK-NEXT: [[N:%.*]] = xor i64 [[S]], -1
-; CHECK-NEXT: call void @use(i1 false)
-; CHECK-NEXT: ret void
-;
-entry:
- %a = lshr i64 %v, 1
- %c = icmp sge i64 %s, 0
- call void @llvm.assume(i1 %c)
- %n = xor i64 %s, -1
- %r = call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 %a, i64 %n)
- %ov = extractvalue { i64, i1 } %r, 1
- call void @use(i1 %ov)
- ret void
-}
-
-define void @sadd_same_signs(i64 %v, i64 %s) {
-; CHECK-LABEL: define void @sadd_same_signs(
-; CHECK-SAME: i64 [[V:%.*]], i64 [[S:%.*]]) {
-; CHECK-NEXT: [[ENTRY:.*:]]
-; CHECK-NEXT: [[A:%.*]] = lshr i64 [[V]], 1
-; CHECK-NEXT: [[C:%.*]] = icmp sge i64 [[S]], 0
-; CHECK-NEXT: call void @llvm.assume(i1 [[C]])
-; CHECK-NEXT: [[R:%.*]] = call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 [[A]], i64 [[S]])
-; CHECK-NEXT: [[OV:%.*]] = extractvalue { i64, i1 } [[R]], 1
-; CHECK-NEXT: call void @use(i1 [[OV]])
-; CHECK-NEXT: ret void
-;
-entry:
- %a = lshr i64 %v, 1
- %c = icmp sge i64 %s, 0
- call void @llvm.assume(i1 %c)
- %r = call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 %a, i64 %s)
- %ov = extractvalue { i64, i1 } %r, 1
- call void @use(i1 %ov)
- ret void
-}
-
-define void @sadd_opposite_signs_known_negative(i8 %v, i8 %w) {
-; CHECK-LABEL: define void @sadd_opposite_signs_known_negative(
-; CHECK-SAME: i8 [[V:%.*]], i8 [[W:%.*]]) {
-; CHECK-NEXT: [[ENTRY:.*:]]
-; CHECK-NEXT: [[A:%.*]] = lshr i8 [[V]], 1
-; CHECK-NEXT: [[B:%.*]] = or i8 [[W]], -128
-; CHECK-NEXT: call void @use(i1 false)
-; CHECK-NEXT: ret void
-;
-entry:
- %a = lshr i8 %v, 1
- %b = or i8 %w, -128
- %r = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 %a, i8 %b)
- %ov = extractvalue { i8, i1 } %r, 1
- call void @use(i1 %ov)
- ret void
-}
More information about the llvm-branch-commits
mailing list