[InstSimplify] Fold ptrtoint(ptradd(P,X-ptrtoint(P))) to X#98649
Merged
[InstSimplify] Fold ptrtoint(ptradd(P,X-ptrtoint(P))) to X#98649
Conversation
This is a special case of the general ptrtoint(gep) to add(ptrtoint) transform that is particularly profitable, as everything folds away. Proof: https://alive2.llvm.org/ce/z/fwv8_L Fixes llvm#86417.
Member
|
@llvm/pr-subscribers-llvm-analysis Author: Nikita Popov (nikic) ChangesThis is a special case of the general ptrtoint(gep) to add(ptrtoint) transform that is particularly profitable, as everything folds away. Proof: https://alive2.llvm.org/ce/z/fwv8_L Fixes #86417. 2 Files Affected:
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 0917a362eccf5..242c200f7ef15 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5333,6 +5333,14 @@ static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
if (Op->getType() == Ty)
return Op;
+ // ptrtoint (ptradd (Ptr, X - ptrtoint(Ptr))) -> X
+ Value *Ptr, *X;
+ if (CastOpc == Instruction::PtrToInt &&
+ match(Op, m_PtrAdd(m_Value(Ptr),
+ m_Sub(m_Value(X), m_PtrToInt(m_Deferred(Ptr))))) &&
+ X->getType() == Ty && Ty == Q.DL.getIndexType(Ptr->getType()))
+ return X;
+
return nullptr;
}
diff --git a/llvm/test/Transforms/InstSimplify/ptrtoint.ll b/llvm/test/Transforms/InstSimplify/ptrtoint.ll
index 55a5a0d452f10..734618713c342 100644
--- a/llvm/test/Transforms/InstSimplify/ptrtoint.ll
+++ b/llvm/test/Transforms/InstSimplify/ptrtoint.ll
@@ -4,11 +4,7 @@
define i64 @ptrtoint_gep_sub(ptr %ptr, i64 %end.addr) {
; CHECK-LABEL: define i64 @ptrtoint_gep_sub(
; CHECK-SAME: ptr [[PTR:%.*]], i64 [[END_ADDR:%.*]]) {
-; CHECK-NEXT: [[PTR_ADDR:%.*]] = ptrtoint ptr [[PTR]] to i64
-; CHECK-NEXT: [[SIZE:%.*]] = sub i64 [[END_ADDR]], [[PTR_ADDR]]
-; CHECK-NEXT: [[END:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[SIZE]]
-; CHECK-NEXT: [[END_ADDR2:%.*]] = ptrtoint ptr [[END]] to i64
-; CHECK-NEXT: ret i64 [[END_ADDR2]]
+; CHECK-NEXT: ret i64 [[END_ADDR]]
;
%ptr.addr = ptrtoint ptr %ptr to i64
%size = sub i64 %end.addr, %ptr.addr
@@ -20,11 +16,7 @@ define i64 @ptrtoint_gep_sub(ptr %ptr, i64 %end.addr) {
define <2 x i64> @ptrtoint_gep_sub_vector(<2 x ptr> %ptr, <2 x i64> %end.addr) {
; CHECK-LABEL: define <2 x i64> @ptrtoint_gep_sub_vector(
; CHECK-SAME: <2 x ptr> [[PTR:%.*]], <2 x i64> [[END_ADDR:%.*]]) {
-; CHECK-NEXT: [[PTR_ADDR:%.*]] = ptrtoint <2 x ptr> [[PTR]] to <2 x i64>
-; CHECK-NEXT: [[SIZE:%.*]] = sub <2 x i64> [[END_ADDR]], [[PTR_ADDR]]
-; CHECK-NEXT: [[END:%.*]] = getelementptr i8, <2 x ptr> [[PTR]], <2 x i64> [[SIZE]]
-; CHECK-NEXT: [[END_ADDR2:%.*]] = ptrtoint <2 x ptr> [[END]] to <2 x i64>
-; CHECK-NEXT: ret <2 x i64> [[END_ADDR2]]
+; CHECK-NEXT: ret <2 x i64> [[END_ADDR]]
;
%ptr.addr = ptrtoint <2 x ptr> %ptr to <2 x i64>
%size = sub <2 x i64> %end.addr, %ptr.addr
|
Member
|
@llvm/pr-subscribers-llvm-transforms Author: Nikita Popov (nikic) ChangesThis is a special case of the general ptrtoint(gep) to add(ptrtoint) transform that is particularly profitable, as everything folds away. Proof: https://alive2.llvm.org/ce/z/fwv8_L Fixes #86417. 2 Files Affected:
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 0917a362eccf5..242c200f7ef15 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5333,6 +5333,14 @@ static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
if (Op->getType() == Ty)
return Op;
+ // ptrtoint (ptradd (Ptr, X - ptrtoint(Ptr))) -> X
+ Value *Ptr, *X;
+ if (CastOpc == Instruction::PtrToInt &&
+ match(Op, m_PtrAdd(m_Value(Ptr),
+ m_Sub(m_Value(X), m_PtrToInt(m_Deferred(Ptr))))) &&
+ X->getType() == Ty && Ty == Q.DL.getIndexType(Ptr->getType()))
+ return X;
+
return nullptr;
}
diff --git a/llvm/test/Transforms/InstSimplify/ptrtoint.ll b/llvm/test/Transforms/InstSimplify/ptrtoint.ll
index 55a5a0d452f10..734618713c342 100644
--- a/llvm/test/Transforms/InstSimplify/ptrtoint.ll
+++ b/llvm/test/Transforms/InstSimplify/ptrtoint.ll
@@ -4,11 +4,7 @@
define i64 @ptrtoint_gep_sub(ptr %ptr, i64 %end.addr) {
; CHECK-LABEL: define i64 @ptrtoint_gep_sub(
; CHECK-SAME: ptr [[PTR:%.*]], i64 [[END_ADDR:%.*]]) {
-; CHECK-NEXT: [[PTR_ADDR:%.*]] = ptrtoint ptr [[PTR]] to i64
-; CHECK-NEXT: [[SIZE:%.*]] = sub i64 [[END_ADDR]], [[PTR_ADDR]]
-; CHECK-NEXT: [[END:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[SIZE]]
-; CHECK-NEXT: [[END_ADDR2:%.*]] = ptrtoint ptr [[END]] to i64
-; CHECK-NEXT: ret i64 [[END_ADDR2]]
+; CHECK-NEXT: ret i64 [[END_ADDR]]
;
%ptr.addr = ptrtoint ptr %ptr to i64
%size = sub i64 %end.addr, %ptr.addr
@@ -20,11 +16,7 @@ define i64 @ptrtoint_gep_sub(ptr %ptr, i64 %end.addr) {
define <2 x i64> @ptrtoint_gep_sub_vector(<2 x ptr> %ptr, <2 x i64> %end.addr) {
; CHECK-LABEL: define <2 x i64> @ptrtoint_gep_sub_vector(
; CHECK-SAME: <2 x ptr> [[PTR:%.*]], <2 x i64> [[END_ADDR:%.*]]) {
-; CHECK-NEXT: [[PTR_ADDR:%.*]] = ptrtoint <2 x ptr> [[PTR]] to <2 x i64>
-; CHECK-NEXT: [[SIZE:%.*]] = sub <2 x i64> [[END_ADDR]], [[PTR_ADDR]]
-; CHECK-NEXT: [[END:%.*]] = getelementptr i8, <2 x ptr> [[PTR]], <2 x i64> [[SIZE]]
-; CHECK-NEXT: [[END_ADDR2:%.*]] = ptrtoint <2 x ptr> [[END]] to <2 x i64>
-; CHECK-NEXT: ret <2 x i64> [[END_ADDR2]]
+; CHECK-NEXT: ret <2 x i64> [[END_ADDR]]
;
%ptr.addr = ptrtoint <2 x ptr> %ptr to <2 x i64>
%size = sub <2 x i64> %end.addr, %ptr.addr
|
dtcxzyw
added a commit
to dtcxzyw/llvm-opt-benchmark
that referenced
this pull request
Jul 12, 2024
nikic
added a commit
that referenced
this pull request
Oct 16, 2025
InstCombine currently fails to call into InstSimplify for cast instructions. I noticed this because the transform from #98649 can be triggered via `-passes=instsimplify` but not `-passes=instcombine`, which is not supposed to happen.
llvm-sync Bot
pushed a commit
to arm/arm-toolchain
that referenced
this pull request
Oct 16, 2025
…2849) InstCombine currently fails to call into InstSimplify for cast instructions. I noticed this because the transform from llvm/llvm-project#98649 can be triggered via `-passes=instsimplify` but not `-passes=instcombine`, which is not supposed to happen.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a special case of the general ptrtoint(gep) to add(ptrtoint) transform that is particularly profitable, as everything folds away.
Proof: https://alive2.llvm.org/ce/z/fwv8_L
Fixes #86417.