[llvm] [InstSimplify] Drop redundant icmp(ptrtoint, const) fold (PR #171988)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 12 03:01:36 PST 2025


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/171988

I believe this fold isn't going to do anything useful: If the constant is 0, then we'll already handle this via isKnownNonZero(). If the constant is non-zero, we'll create an inttoptr constant expression, and as far as I can tell we don't have any InstSimplify folds that would apply for that case.

>From 77167f813e200bf73316c7e561a67b6b38d7f30e Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Fri, 12 Dec 2025 11:59:15 +0100
Subject: [PATCH] [InstSimplify] Drop redundant icmp(ptrtoint, const) fold

I believe this fold isn't going to do anything useful: If the
constant is 0, then we'll already handle this via isKnownNonZero().
If the constant is non-zero, we'll create an inttoptr constant
expression, and as far as I can tell we don't have any InstSimplify
folds that would apply for that case.
---
 llvm/lib/Analysis/InstructionSimplify.cpp | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)

diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 63930617071f3..d826846135a77 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -3847,22 +3847,16 @@ static Value *simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS,
     Type *SrcTy = SrcOp->getType();
     Type *DstTy = LI->getType();
 
-    // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input
+    // Turn icmp (ptrtoint x), (ptrtoint y) into a compare of the input
     // if the integer type is the same size as the pointer type.
-    if (MaxRecurse && isa<PtrToIntInst>(LI) &&
+    if (MaxRecurse && isa<PtrToIntInst>(LI) && isa<PtrToIntInst>(RHS) &&
         Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) {
-      if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
-        // Transfer the cast to the constant.
-        if (Value *V = simplifyICmpInst(Pred, SrcOp,
-                                        ConstantExpr::getIntToPtr(RHSC, SrcTy),
-                                        Q, MaxRecurse - 1))
+      auto *RI = cast<PtrToIntInst>(RHS);
+      if (RI->getOperand(0)->getType() == SrcTy) {
+        // Compare without the cast.
+        if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
+                                        MaxRecurse - 1))
           return V;
-      } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
-        if (RI->getOperand(0)->getType() == SrcTy)
-          // Compare without the cast.
-          if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
-                                          MaxRecurse - 1))
-            return V;
       }
     }
 



More information about the llvm-commits mailing list