[llvm] [ConstraintElimination] Add support for UCMP/SCMP intrinsics (PR #97974)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 9 00:42:01 PDT 2024


================
@@ -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)) {
----------------
dtcxzyw wrote:

@fhahn Do you have a plan to support eq/ne with signed predicates?

```
define i1 @test1(i32 noundef %x, i32 noundef %y) {
  %cond = icmp eq i32 %x, %y
  call void @llvm.assume(i1 %cond)
  %r = icmp slt i32 %x, %y
  ret i1 %r
}

define i1 @test2(i32 noundef %x, i32 noundef %y) {
  %cond1 = icmp sle i32 %x, %y
  call void @llvm.assume(i1 %cond1)
  %cond2 = icmp sge i32 %x, %y
  call void @llvm.assume(i1 %cond2)
  %r = icmp eq i32 %x, %y
  ret i1 %r
}
```

https://github.com/llvm/llvm-project/pull/97974


More information about the llvm-commits mailing list