[llvm] [ConstraintElimination] Add support for UCMP/SCMP intrinsics (PR #97974)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Jul 7 15:12:09 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: None (Poseydon42)
<details>
<summary>Changes</summary>
This adds checks to fold calls to `ucmp`/`scmp` where a comparative relationship between the arguments can be established.
---
Full diff: https://github.com/llvm/llvm-project/pull/97974.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Scalar/ConstraintElimination.cpp (+34)
- (added) llvm/test/Transforms/ConstraintElimination/uscmp.ll (+110)
``````````diff
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index bf0c67d9dbc4f..478aea7287875 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -1073,6 +1073,8 @@ void State::addInfoFor(BasicBlock &BB) {
}
// Enqueue ssub_with_overflow for simplification.
case Intrinsic::ssub_with_overflow:
+ case Intrinsic::ucmp:
+ case Intrinsic::scmp:
WorkList.push_back(
FactOrCheck::getCheck(DT.getNode(&BB), cast<CallInst>(&I)));
break;
@@ -1434,6 +1436,33 @@ static bool checkAndReplaceMinMax(MinMaxIntrinsic *MinMax, ConstraintInfo &Info,
return false;
}
+static bool checkAndReplaceCmp(IntrinsicInst *II, ConstraintInfo &Info,
+ SmallVectorImpl<Instruction *> &ToRemove) {
+ bool IsSigned = II->getIntrinsicID() == Intrinsic::scmp;
+ Value *LHS = II->getOperand(0);
+ Value *RHS = II->getOperand(1);
+ if (checkCondition(IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT, LHS,
+ RHS, II, Info)
+ .value_or(false)) {
+ II->replaceAllUsesWith(ConstantInt::get(II->getType(), 1));
+ ToRemove.push_back(II);
+ return true;
+ }
+ if (checkCondition(IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, LHS,
+ RHS, II, Info)
+ .value_or(false)) {
+ II->replaceAllUsesWith(ConstantInt::getSigned(II->getType(), -1));
+ ToRemove.push_back(II);
+ return true;
+ }
+ if (checkCondition(ICmpInst::ICMP_EQ, LHS, RHS, II, Info).value_or(false)) {
+ II->replaceAllUsesWith(ConstantInt::get(II->getType(), 0));
+ ToRemove.push_back(II);
+ return true;
+ }
+ return false;
+}
+
static void
removeEntryFromStack(const StackEntry &E, ConstraintInfo &Info,
Module *ReproducerModule,
@@ -1736,6 +1765,11 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
Changed |= Simplified;
} else if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Inst)) {
Changed |= checkAndReplaceMinMax(MinMax, Info, ToRemove);
+ } else if (auto *CmpIntrinsic = dyn_cast<IntrinsicInst>(Inst);
+ CmpIntrinsic &&
+ (CmpIntrinsic->getIntrinsicID() == Intrinsic::scmp ||
+ CmpIntrinsic->getIntrinsicID() == Intrinsic::ucmp)) {
+ Changed |= checkAndReplaceCmp(CmpIntrinsic, Info, ToRemove);
}
continue;
}
diff --git a/llvm/test/Transforms/ConstraintElimination/uscmp.ll b/llvm/test/Transforms/ConstraintElimination/uscmp.ll
new file mode 100644
index 0000000000000..16ca93f0427e7
--- /dev/null
+++ b/llvm/test/Transforms/ConstraintElimination/uscmp.ll
@@ -0,0 +1,110 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -passes=constraint-elimination -S %s | FileCheck %s
+
+define i8 @scmp_1(i32 %x, i32 %y) {
+; CHECK-LABEL: @scmp_1(
+; CHECK-NEXT: [[COND:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: br i1 [[COND]], label [[TRUE:%.*]], label [[FALSE:%.*]]
+; CHECK: true:
+; CHECK-NEXT: ret i8 1
+; CHECK: false:
+; CHECK-NEXT: ret i8 20
+;
+ %cond = icmp sgt i32 %x, %y
+ br i1 %cond, label %true, label %false
+true:
+ %r = call i8 @llvm.scmp(i32 %x, i32 %y)
+ ret i8 %r
+false:
+ ret i8 20
+}
+
+define i8 @ucmp_1(i32 %x, i32 %y) {
+; CHECK-LABEL: @ucmp_1(
+; CHECK-NEXT: [[COND:%.*]] = icmp ult i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: br i1 [[COND]], label [[TRUE:%.*]], label [[FALSE:%.*]]
+; CHECK: true:
+; CHECK-NEXT: ret i8 -1
+; CHECK: false:
+; CHECK-NEXT: ret i8 20
+;
+ %cond = icmp ult i32 %x, %y
+ br i1 %cond, label %true, label %false
+true:
+ %r = call i8 @llvm.ucmp(i32 %x, i32 %y)
+ ret i8 %r
+false:
+ ret i8 20
+}
+
+define i8 @scmp_2(i32 %x, i32 %y) {
+; CHECK-LABEL: @scmp_2(
+; CHECK-NEXT: [[COND:%.*]] = icmp sge i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: br i1 [[COND]], label [[TRUE:%.*]], label [[FALSE:%.*]]
+; CHECK: true:
+; CHECK-NEXT: ret i8 20
+; CHECK: false:
+; CHECK-NEXT: ret i8 -1
+;
+ %cond = icmp sge i32 %x, %y
+ br i1 %cond, label %true, label %false
+true:
+ ret i8 20
+false:
+ %r = call i8 @llvm.scmp(i32 %x, i32 %y)
+ ret i8 %r
+}
+
+define i8 @ucmp_2(i32 %x, i32 %y) {
+; CHECK-LABEL: @ucmp_2(
+; CHECK-NEXT: [[COND:%.*]] = icmp ule i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: br i1 [[COND]], label [[TRUE:%.*]], label [[FALSE:%.*]]
+; CHECK: true:
+; CHECK-NEXT: ret i8 20
+; CHECK: false:
+; CHECK-NEXT: ret i8 1
+;
+ %cond = icmp ule i32 %x, %y
+ br i1 %cond, label %true, label %false
+true:
+ ret i8 20
+false:
+ %r = call i8 @llvm.ucmp(i32 %x, i32 %y)
+ ret i8 %r
+}
+
+define i8 @scmp_3(i32 %x, i32 %y) {
+; CHECK-LABEL: @scmp_3(
+; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: br i1 [[COND]], label [[TRUE:%.*]], label [[FALSE:%.*]]
+; CHECK: true:
+; CHECK-NEXT: ret i8 0
+; CHECK: false:
+; CHECK-NEXT: ret i8 20
+;
+ %cond = icmp eq i32 %x, %y
+ br i1 %cond, label %true, label %false
+true:
+ %r = call i8 @llvm.scmp(i32 %x, i32 %y)
+ ret i8 %r
+false:
+ ret i8 20
+}
+
+define i8 @ucmp_3(i32 %x, i32 %y) {
+; CHECK-LABEL: @ucmp_3(
+; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: br i1 [[COND]], label [[TRUE:%.*]], label [[FALSE:%.*]]
+; CHECK: true:
+; CHECK-NEXT: ret i8 0
+; CHECK: false:
+; CHECK-NEXT: ret i8 20
+;
+ %cond = icmp eq i32 %x, %y
+ br i1 %cond, label %true, label %false
+true:
+ %r = call i8 @llvm.ucmp(i32 %x, i32 %y)
+ ret i8 %r
+false:
+ ret i8 20
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/97974
More information about the llvm-commits
mailing list