[llvm] [Instcombine]: Folds`llvm.ucmp` and `llvm.scmp` (PR #168505)

Kevin Per via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 29 08:43:03 PST 2025


================
@@ -4461,6 +4461,47 @@ static Value *simplifyWithOpsReplaced(Value *V,
         return Absorber;
     }
 
+    if (auto *CI = dyn_cast<CallInst>(I)) {
+      Function *F = CI->getCalledFunction();
+
+      // `x == y ? 0 : ucmp(x, y)` where under the replacement y -> x, `ucmp(x,
+      // x)` becomes `0`.
+      if (F && F->isIntrinsic() &&
+          (F->getIntrinsicID() == Intrinsic::scmp ||
+           F->getIntrinsicID() == Intrinsic::ucmp)) {
+        // If the call contains (an invalid) range attribute then a replacement
+        // might produce an unexpected poison value.
+        if (CI->hasRetAttr(Attribute::AttrKind::Range)) {
+          const ConstantRange &CR =
+              CI->getRetAttr(Attribute::AttrKind::Range).getRange();
+
+          APInt Lo = CR.getLower();
+          APInt Hi = CR.getUpper();
+
+          if (!(Lo == llvm::APInt::getAllOnes(Lo.getBitWidth()) &&
+                Hi == llvm::APInt(Hi.getBitWidth(), 2)))
+            return nullptr;
+        }
+
+        // To apply this fold, we have to do the replacement and return `0` if
+        // the arguments are equal.
+        SmallVector<Value *, 2> ReplacedArgs;
----------------
kper wrote:

ah yes
didn't think of it :)

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


More information about the llvm-commits mailing list