[llvm] [InstCombine] Handle ptrtoaddr in gep of pointer sub fold (PR #164818)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 23 06:21:17 PDT 2025
================
@@ -3315,21 +3315,21 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
if (TyAllocSize == 1) {
// Canonicalize (gep i8* X, (ptrtoint Y)-(ptrtoint X)) to (bitcast Y),
- // but only if the result pointer is only used as if it were an integer,
- // or both point to the same underlying object (otherwise provenance is
- // not necessarily retained).
+ // but only if the result pointer is only used as if it were an integer.
+ // (The case where the underlying object is the same is handled by
+ // InstSimplify.)
Value *X = GEP.getPointerOperand();
Value *Y;
- if (match(GEP.getOperand(1),
- m_Sub(m_PtrToInt(m_Value(Y)), m_PtrToInt(m_Specific(X)))) &&
+ if (match(GEP.getOperand(1), m_Sub(m_PtrToIntOrAddr(m_Value(Y)),
+ m_PtrToIntOrAddr(m_Specific(X)))) &&
GEPType == Y->getType()) {
- bool HasSameUnderlyingObject =
- getUnderlyingObject(X) == getUnderlyingObject(Y);
----------------
nikic wrote:
See https://github.com/llvm/llvm-project/blob/53b9441f5355fcdca07dc5b6ddc4fa5bb7ce4920/llvm/lib/Analysis/InstructionSimplify.cpp#L5117 for the InstSimplify transform that handles the same underlying object case, which is why I dropped the handling here.
https://github.com/llvm/llvm-project/pull/164818
More information about the llvm-commits
mailing list