Skip to content

[SCCP] Simplify [us]cmp(X, Y) into X - Y #144717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented Jun 18, 2025

If the difference between [us]cmp's operands is not greater than 1, we can simplify it into X - Y.
Alive2: https://alive2.llvm.org/ce/z/JS55so
llvm-opt-benchmark diff: https://github.com/dtcxzyw/llvm-opt-benchmark/pull/2464/files

@llvmbot
Copy link
Member

llvmbot commented Jun 18, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Yingwei Zheng (dtcxzyw)

Changes

If the difference between [us]cmp's operands is not greater than 1, we can simplify it into X - Y.
Alive2: https://alive2.llvm.org/ce/z/JS55so
llvm-opt-benchmark diff: https://github.com/dtcxzyw/llvm-opt-benchmark/pull/2464/files


Full diff: https://github.com/llvm/llvm-project/pull/144717.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/SCCPSolver.cpp (+36-1)
  • (added) llvm/test/Transforms/SCCP/uscmp.ll (+78)
diff --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
index f4b378b82daec..0e3f0647d6284 100644
--- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp
+++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
@@ -245,11 +245,46 @@ static Value *simplifyInstruction(SCCPSolver &Solver,
   const APInt *RHSC;
   // Remove masking operations.
   if (match(&Inst, m_And(m_Value(X), m_LowBitMask(RHSC)))) {
-    ConstantRange LRange = GetRange(Inst.getOperand(0));
+    ConstantRange LRange = GetRange(X);
     if (LRange.getUnsignedMax().ule(*RHSC))
       return X;
   }
 
+  if (auto *II = dyn_cast<IntrinsicInst>(&Inst)) {
+    Intrinsic::ID IID = II->getIntrinsicID();
+    // Check if we can simplify [us]cmp(X, Y) to X - Y.
+    if (IID == Intrinsic::scmp || IID == Intrinsic::ucmp) {
+      Value *LHS = II->getOperand(0);
+      Value *RHS = II->getOperand(1);
+      unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
+      ConstantRange LRange = GetRange(LHS);
+      if (LRange.isSizeLargerThan(3))
+        return nullptr;
+      ConstantRange RRange = GetRange(RHS);
+      if (RRange.isSizeLargerThan(3))
+        return nullptr;
+      ConstantRange RHSLower = RRange.sub(APInt(BitWidth, 1));
+      ConstantRange RHSUpper = RRange.add(APInt(BitWidth, 1));
+      ICmpInst::Predicate Pred =
+          IID == Intrinsic::scmp ? CmpInst::ICMP_SLE : CmpInst::ICMP_ULE;
+      if (!RHSLower.icmp(Pred, LRange) || !LRange.icmp(Pred, RHSUpper))
+        return nullptr;
+      Instruction *Sub = BinaryOperator::CreateSub(LHS, RHS, Inst.getName(),
+                                                   Inst.getIterator());
+      if (IID == Intrinsic::scmp)
+        Sub->setHasNoSignedWrap(true);
+      Sub->setDebugLoc(Inst.getDebugLoc());
+      InsertedValues.insert(Sub);
+      if (Sub->getType() != Inst.getType()) {
+        Sub = CastInst::CreateIntegerCast(Sub, Inst.getType(), true, "",
+                                          Inst.getIterator());
+        Sub->setDebugLoc(Inst.getDebugLoc());
+        InsertedValues.insert(Sub);
+      }
+      return Sub;
+    }
+  }
+
   return nullptr;
 }
 
diff --git a/llvm/test/Transforms/SCCP/uscmp.ll b/llvm/test/Transforms/SCCP/uscmp.ll
new file mode 100644
index 0000000000000..ea5a65556ac55
--- /dev/null
+++ b/llvm/test/Transforms/SCCP/uscmp.ll
@@ -0,0 +1,78 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes=sccp -S < %s | FileCheck %s
+
+define i32 @scmp_to_sub(i32 range(i32 -1, 2) %a) {
+; CHECK-LABEL: define i32 @scmp_to_sub(
+; CHECK-SAME: i32 range(i32 -1, 2) [[A:%.*]]) {
+; CHECK-NEXT:    [[SCMP:%.*]] = sub nsw i32 [[A]], 0
+; CHECK-NEXT:    ret i32 [[SCMP]]
+;
+  %scmp = call i32 @llvm.scmp(i32 %a, i32 0)
+  ret i32 %scmp
+}
+
+define i32 @scmp_zext_to_sub(i1 %a, i1 %b) {
+; CHECK-LABEL: define i32 @scmp_zext_to_sub(
+; CHECK-SAME: i1 [[A:%.*]], i1 [[B:%.*]]) {
+; CHECK-NEXT:    [[ZEXT_A:%.*]] = zext i1 [[A]] to i32
+; CHECK-NEXT:    [[ZEXT_B:%.*]] = zext i1 [[B]] to i32
+; CHECK-NEXT:    [[SCMP:%.*]] = sub nsw i32 [[ZEXT_A]], [[ZEXT_B]]
+; CHECK-NEXT:    ret i32 [[SCMP]]
+;
+  %zext_a = zext i1 %a to i32
+  %zext_b = zext i1 %b to i32
+  %scmp = call i32 @llvm.scmp(i32 %zext_a, i32 %zext_b)
+  ret i32 %scmp
+}
+
+define i8 @scmp_to_sub_trunc(i32 range(i32 -1, 2) %a) {
+; CHECK-LABEL: define i8 @scmp_to_sub_trunc(
+; CHECK-SAME: i32 range(i32 -1, 2) [[A:%.*]]) {
+; CHECK-NEXT:    [[SCMP1:%.*]] = sub nsw i32 [[A]], 0
+; CHECK-NEXT:    [[SCMP:%.*]] = trunc i32 [[SCMP1]] to i8
+; CHECK-NEXT:    ret i8 [[SCMP]]
+;
+  %scmp = call i8 @llvm.scmp(i32 %a, i32 0)
+  ret i8 %scmp
+}
+
+define i64 @scmp_to_sub_sext(i32 range(i32 -1, 2) %a) {
+; CHECK-LABEL: define i64 @scmp_to_sub_sext(
+; CHECK-SAME: i32 range(i32 -1, 2) [[A:%.*]]) {
+; CHECK-NEXT:    [[SCMP1:%.*]] = sub nsw i32 [[A]], 0
+; CHECK-NEXT:    [[SCMP:%.*]] = sext i32 [[SCMP1]] to i64
+; CHECK-NEXT:    ret i64 [[SCMP]]
+;
+  %scmp = call i64 @llvm.scmp(i32 %a, i32 0)
+  ret i64 %scmp
+}
+
+define i32 @scmp_to_sub_small_range(i32 range(i32 -1, 1) %a) {
+; CHECK-LABEL: define i32 @scmp_to_sub_small_range(
+; CHECK-SAME: i32 range(i32 -1, 1) [[A:%.*]]) {
+; CHECK-NEXT:    [[SCMP:%.*]] = sub nsw i32 [[A]], 0
+; CHECK-NEXT:    ret i32 [[SCMP]]
+;
+  %scmp = call i32 @llvm.scmp(i32 %a, i32 0)
+  ret i32 %scmp
+}
+
+define i32 @ucmp_to_sub(i32 range(i32 0, 3) %a) {
+; CHECK-LABEL: define i32 @ucmp_to_sub(
+; CHECK-SAME: i32 range(i32 0, 3) [[A:%.*]]) {
+; CHECK-NEXT:    [[SCMP:%.*]] = sub nsw i32 [[A]], 1
+; CHECK-NEXT:    ret i32 [[SCMP]]
+;
+  %scmp = call i32 @llvm.scmp(i32 %a, i32 1)
+  ret i32 %scmp
+}
+
+define i32 @scmp_to_sub_large_range(i32 range(i32 -1, 3) %a) {
+; CHECK-LABEL: define i32 @scmp_to_sub_large_range(
+; CHECK-SAME: i32 range(i32 -1, 3) [[A:%.*]]) {
+; CHECK-NEXT:    [[SCMP:%.*]] = call i32 @llvm.scmp.i32.i32(i32 [[A]], i32 0)
+; CHECK-NEXT:    ret i32 [[SCMP]]
+;
+  %scmp = call i32 @llvm.scmp(i32 %a, i32 0)
+  ret i32 %scmp
+}

@llvmbot
Copy link
Member

llvmbot commented Jun 18, 2025

@llvm/pr-subscribers-function-specialization

Author: Yingwei Zheng (dtcxzyw)

Changes

If the difference between [us]cmp's operands is not greater than 1, we can simplify it into X - Y.
Alive2: https://alive2.llvm.org/ce/z/JS55so
llvm-opt-benchmark diff: https://github.com/dtcxzyw/llvm-opt-benchmark/pull/2464/files


Full diff: https://github.com/llvm/llvm-project/pull/144717.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/SCCPSolver.cpp (+36-1)
  • (added) llvm/test/Transforms/SCCP/uscmp.ll (+78)
diff --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
index f4b378b82daec..0e3f0647d6284 100644
--- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp
+++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
@@ -245,11 +245,46 @@ static Value *simplifyInstruction(SCCPSolver &Solver,
   const APInt *RHSC;
   // Remove masking operations.
   if (match(&Inst, m_And(m_Value(X), m_LowBitMask(RHSC)))) {
-    ConstantRange LRange = GetRange(Inst.getOperand(0));
+    ConstantRange LRange = GetRange(X);
     if (LRange.getUnsignedMax().ule(*RHSC))
       return X;
   }
 
+  if (auto *II = dyn_cast<IntrinsicInst>(&Inst)) {
+    Intrinsic::ID IID = II->getIntrinsicID();
+    // Check if we can simplify [us]cmp(X, Y) to X - Y.
+    if (IID == Intrinsic::scmp || IID == Intrinsic::ucmp) {
+      Value *LHS = II->getOperand(0);
+      Value *RHS = II->getOperand(1);
+      unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
+      ConstantRange LRange = GetRange(LHS);
+      if (LRange.isSizeLargerThan(3))
+        return nullptr;
+      ConstantRange RRange = GetRange(RHS);
+      if (RRange.isSizeLargerThan(3))
+        return nullptr;
+      ConstantRange RHSLower = RRange.sub(APInt(BitWidth, 1));
+      ConstantRange RHSUpper = RRange.add(APInt(BitWidth, 1));
+      ICmpInst::Predicate Pred =
+          IID == Intrinsic::scmp ? CmpInst::ICMP_SLE : CmpInst::ICMP_ULE;
+      if (!RHSLower.icmp(Pred, LRange) || !LRange.icmp(Pred, RHSUpper))
+        return nullptr;
+      Instruction *Sub = BinaryOperator::CreateSub(LHS, RHS, Inst.getName(),
+                                                   Inst.getIterator());
+      if (IID == Intrinsic::scmp)
+        Sub->setHasNoSignedWrap(true);
+      Sub->setDebugLoc(Inst.getDebugLoc());
+      InsertedValues.insert(Sub);
+      if (Sub->getType() != Inst.getType()) {
+        Sub = CastInst::CreateIntegerCast(Sub, Inst.getType(), true, "",
+                                          Inst.getIterator());
+        Sub->setDebugLoc(Inst.getDebugLoc());
+        InsertedValues.insert(Sub);
+      }
+      return Sub;
+    }
+  }
+
   return nullptr;
 }
 
diff --git a/llvm/test/Transforms/SCCP/uscmp.ll b/llvm/test/Transforms/SCCP/uscmp.ll
new file mode 100644
index 0000000000000..ea5a65556ac55
--- /dev/null
+++ b/llvm/test/Transforms/SCCP/uscmp.ll
@@ -0,0 +1,78 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes=sccp -S < %s | FileCheck %s
+
+define i32 @scmp_to_sub(i32 range(i32 -1, 2) %a) {
+; CHECK-LABEL: define i32 @scmp_to_sub(
+; CHECK-SAME: i32 range(i32 -1, 2) [[A:%.*]]) {
+; CHECK-NEXT:    [[SCMP:%.*]] = sub nsw i32 [[A]], 0
+; CHECK-NEXT:    ret i32 [[SCMP]]
+;
+  %scmp = call i32 @llvm.scmp(i32 %a, i32 0)
+  ret i32 %scmp
+}
+
+define i32 @scmp_zext_to_sub(i1 %a, i1 %b) {
+; CHECK-LABEL: define i32 @scmp_zext_to_sub(
+; CHECK-SAME: i1 [[A:%.*]], i1 [[B:%.*]]) {
+; CHECK-NEXT:    [[ZEXT_A:%.*]] = zext i1 [[A]] to i32
+; CHECK-NEXT:    [[ZEXT_B:%.*]] = zext i1 [[B]] to i32
+; CHECK-NEXT:    [[SCMP:%.*]] = sub nsw i32 [[ZEXT_A]], [[ZEXT_B]]
+; CHECK-NEXT:    ret i32 [[SCMP]]
+;
+  %zext_a = zext i1 %a to i32
+  %zext_b = zext i1 %b to i32
+  %scmp = call i32 @llvm.scmp(i32 %zext_a, i32 %zext_b)
+  ret i32 %scmp
+}
+
+define i8 @scmp_to_sub_trunc(i32 range(i32 -1, 2) %a) {
+; CHECK-LABEL: define i8 @scmp_to_sub_trunc(
+; CHECK-SAME: i32 range(i32 -1, 2) [[A:%.*]]) {
+; CHECK-NEXT:    [[SCMP1:%.*]] = sub nsw i32 [[A]], 0
+; CHECK-NEXT:    [[SCMP:%.*]] = trunc i32 [[SCMP1]] to i8
+; CHECK-NEXT:    ret i8 [[SCMP]]
+;
+  %scmp = call i8 @llvm.scmp(i32 %a, i32 0)
+  ret i8 %scmp
+}
+
+define i64 @scmp_to_sub_sext(i32 range(i32 -1, 2) %a) {
+; CHECK-LABEL: define i64 @scmp_to_sub_sext(
+; CHECK-SAME: i32 range(i32 -1, 2) [[A:%.*]]) {
+; CHECK-NEXT:    [[SCMP1:%.*]] = sub nsw i32 [[A]], 0
+; CHECK-NEXT:    [[SCMP:%.*]] = sext i32 [[SCMP1]] to i64
+; CHECK-NEXT:    ret i64 [[SCMP]]
+;
+  %scmp = call i64 @llvm.scmp(i32 %a, i32 0)
+  ret i64 %scmp
+}
+
+define i32 @scmp_to_sub_small_range(i32 range(i32 -1, 1) %a) {
+; CHECK-LABEL: define i32 @scmp_to_sub_small_range(
+; CHECK-SAME: i32 range(i32 -1, 1) [[A:%.*]]) {
+; CHECK-NEXT:    [[SCMP:%.*]] = sub nsw i32 [[A]], 0
+; CHECK-NEXT:    ret i32 [[SCMP]]
+;
+  %scmp = call i32 @llvm.scmp(i32 %a, i32 0)
+  ret i32 %scmp
+}
+
+define i32 @ucmp_to_sub(i32 range(i32 0, 3) %a) {
+; CHECK-LABEL: define i32 @ucmp_to_sub(
+; CHECK-SAME: i32 range(i32 0, 3) [[A:%.*]]) {
+; CHECK-NEXT:    [[SCMP:%.*]] = sub nsw i32 [[A]], 1
+; CHECK-NEXT:    ret i32 [[SCMP]]
+;
+  %scmp = call i32 @llvm.scmp(i32 %a, i32 1)
+  ret i32 %scmp
+}
+
+define i32 @scmp_to_sub_large_range(i32 range(i32 -1, 3) %a) {
+; CHECK-LABEL: define i32 @scmp_to_sub_large_range(
+; CHECK-SAME: i32 range(i32 -1, 3) [[A:%.*]]) {
+; CHECK-NEXT:    [[SCMP:%.*]] = call i32 @llvm.scmp.i32.i32(i32 [[A]], i32 0)
+; CHECK-NEXT:    ret i32 [[SCMP]]
+;
+  %scmp = call i32 @llvm.scmp(i32 %a, i32 0)
+  ret i32 %scmp
+}

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In practice, is this useful for anything other than the "extended bool" case? I'm wondering if it would be better to handle this as a KnownBits transform.

@dtcxzyw
Copy link
Member Author

dtcxzyw commented Jun 19, 2025

In practice, is this useful for anything other than the "extended bool" case? I'm wondering if it would be better to handle this as a KnownBits transform.

See the first case in the diff:

%45 = select i1 %44, i32 -1, i32 1
%47 = phi i32 [ %45, %bb1 ], [ 0, %bb2 ]
%.0.i.i.i.i.i.i.i.i = call noundef i32 @llvm.scmp.i32.i32(i32 %47, i32 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants