[llvm] [InstSimplify] Implement simple folds for `ucmp`/`scmp` intrinsics (PR #95601)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 15 13:21:55 PDT 2024


================
@@ -6505,6 +6505,33 @@ Value *llvm::simplifyBinaryIntrinsic(Intrinsic::ID IID, Type *ReturnType,
 
     break;
   }
+  case Intrinsic::scmp:
+  case Intrinsic::ucmp: {
+    // Fold cmp x, x -> 0
+    if (Op0 == Op1)
+      return Constant::getNullValue(ReturnType);
+
+    // Fold to a constant if the relationship between operands can be
+    // established with certainty
+    if (isICmpTrue(CmpInst::ICMP_EQ, Op0, Op1, Q, RecursionLimit))
----------------
nikic wrote:

The situation with min/max is different. If you have something like `umin(undef, 5)` then `icmp ule undef, 5` could fold to true and return `undef` overall, which would be incorrect. In this case we return a constant, which is fine (in the same way that folding an icmp with undef is fine -- if this is not valid for the using context, the caller would already disable undef refinement).

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


More information about the llvm-commits mailing list