Skip to content

Conversation

nikic
Copy link
Contributor

@nikic nikic commented Oct 9, 2025

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

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
@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:analysis Includes value tracking, cost tables and constant folding llvm:transforms labels Oct 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 9, 2025

@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-llvm-transforms

Author: Nikita Popov (nikic)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/162676.diff

2 Files Affected:

  • (modified) llvm/lib/Analysis/InstructionSimplify.cpp (+3-3)
  • (modified) llvm/test/Transforms/InstSimplify/ptr_diff.ll (+1-5)
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index d1977f0b0daf9..333e3522613d4 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 @ptrdiff(ptr %ptr) {
 
 define i64 @ptrdiff_no_inbounds(ptr %ptr) {
 ; CHECK-LABEL: @ptrdiff_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

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

Copy link
Member

@XChy XChy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nikic nikic enabled auto-merge (squash) October 10, 2025 07:32
@nikic nikic merged commit 261580c into llvm:main Oct 10, 2025
9 of 10 checks passed
@nikic nikic deleted the instsimplify-ptrdiff-no-inbounds branch October 10, 2025 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants