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

Merged
merged 5 commits into from
Jul 20, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion llvm/lib/Transforms/Utils/SCCPSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
78 changes: 78 additions & 0 deletions llvm/test/Transforms/SCCP/uscmp.ll
Original file line number Diff line number Diff line change
@@ -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

Choose a reason for hiding this comment

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

Missing tests for ucmp?

I'd also like a negative test for the case where abs diff <= 1 but the transform is invalid because it wraps. (That's why the precondition is expressed in such a roundabout way, right?)

Copy link
Member Author

Choose a reason for hiding this comment

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

That's why the precondition is expressed in such a roundabout way, right?

Yeah. I don't have a better idea to weaken the precondition (see the test ucmp_to_sub_small_range).

Loading