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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 7 19:52:41 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:

We can avoid this check since `!(x < y) && !(x > y)` implies `x == y`.

```suggestion
  auto CmpGt = checkCondition(IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT, LHS,
                     RHS, II, Info);
  if (CmpGt.value_or(false)) {
    II->replaceAllUsesWith(ConstantInt::get(II->getType(), 1));
    ToRemove.push_back(II);
    return true;
  }
  auto CmpLt = checkCondition(IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, LHS,
                     RHS, II, Info);
  if (CmpLt.value_or(false)) {
    II->replaceAllUsesWith(ConstantInt::getSigned(II->getType(), -1));
    ToRemove.push_back(II);
    return true;
  }
  if (CmpLt == false && CmpGt == false) {
```


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


More information about the llvm-commits mailing list