[llvm] 261580c - [InstSimplify] Support non-inbounds GEP in ptrdiff fold (#162676)
    via llvm-commits 
    llvm-commits at lists.llvm.org
       
    Fri Oct 10 01:03:29 PDT 2025
    
    
  
Author: Nikita Popov
Date: 2025-10-10T08:03:25Z
New Revision: 261580cacd0be842ab62466193a385384a95c61a
URL: https://github.com/llvm/llvm-project/commit/261580cacd0be842ab62466193a385384a95c61a
DIFF: https://github.com/llvm/llvm-project/commit/261580cacd0be842ab62466193a385384a95c61a.diff
LOG: [InstSimplify] Support non-inbounds GEP in ptrdiff fold (#162676)
We can fold ptrdiff(ptradd(p, x), p) to x regardless of whether the
ptradd is inbounds.
Proof: https://alive2.llvm.org/ce/z/Xuvc7N
Added: 
    
Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstSimplify/ptr_diff.ll
Removed: 
    
################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 71050a4117d5a..4e3862693294e 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -671,12 +671,12 @@ Value *llvm::simplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
 /// This is very similar to stripAndAccumulateConstantOffsets(), except it
 /// normalizes the offset bitwidth to the stripped pointer type, not the
 /// original pointer type.
-static APInt stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V,
-                                            bool AllowNonInbounds = false) {
+static APInt stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V) {
   assert(V->getType()->isPtrOrPtrVectorTy());
 
   APInt Offset = APInt::getZero(DL.getIndexTypeSizeInBits(V->getType()));
-  V = V->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds);
+  V = V->stripAndAccumulateConstantOffsets(DL, Offset,
+                                           /*AllowNonInbounds=*/true);
   // As that strip may trace through `addrspacecast`, need to sext or trunc
   // the offset calculated.
   return Offset.sextOrTrunc(DL.getIndexTypeSizeInBits(V->getType()));
diff  --git a/llvm/test/Transforms/InstSimplify/ptr_
diff .ll b/llvm/test/Transforms/InstSimplify/ptr_
diff .ll
index fdd9e8e149c1c..508dfbc710830 100644
--- a/llvm/test/Transforms/InstSimplify/ptr_
diff .ll
+++ b/llvm/test/Transforms/InstSimplify/ptr_
diff .ll
@@ -14,11 +14,7 @@ define i64 @ptr
diff (ptr %ptr) {
 
 define i64 @ptr
diff _no_inbounds(ptr %ptr) {
 ; CHECK-LABEL: @ptr
diff _no_inbounds(
-; CHECK-NEXT:    [[LAST:%.*]] = getelementptr i8, ptr [[PTR:%.*]], i32 42
-; CHECK-NEXT:    [[FIRST_INT:%.*]] = ptrtoint ptr [[PTR]] to i64
-; CHECK-NEXT:    [[LAST_INT:%.*]] = ptrtoint ptr [[LAST]] to i64
-; CHECK-NEXT:    [[DIFF:%.*]] = sub i64 [[LAST_INT]], [[FIRST_INT]]
-; CHECK-NEXT:    ret i64 [[DIFF]]
+; CHECK-NEXT:    ret i64 42
 ;
   %last = getelementptr i8, ptr %ptr, i32 42
   %first.int = ptrtoint ptr %ptr to i64
        
    
    
More information about the llvm-commits
mailing list