[llvm] c45f939 - [InstCombine] Generalize ptrtoint(gep) fold (NFC)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 12 07:34:33 PDT 2024
Author: Nikita Popov
Date: 2024-07-12T16:34:26+02:00
New Revision: c45f939e34dafaf0f57fd1d93df7df5cc89f1dec
URL: https://github.com/llvm/llvm-project/commit/c45f939e34dafaf0f57fd1d93df7df5cc89f1dec
DIFF: https://github.com/llvm/llvm-project/commit/c45f939e34dafaf0f57fd1d93df7df5cc89f1dec.diff
LOG: [InstCombine] Generalize ptrtoint(gep) fold (NFC)
We're currently handling a special case of
ptrtoint gep -> add ptrtoint. Reframe the code to make it easier
to add more patterns for this transform.
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 8f83047020936..5f51c92f95e28 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -2025,6 +2025,25 @@ Instruction *InstCombinerImpl::visitIntToPtr(IntToPtrInst &CI) {
return nullptr;
}
+// Whether we should convert ptrtoint(gep P, X) to ptrtoint(P) + X
+static bool shouldPushPtrToIntThroughGEP(GEPOperator *GEP, Type *IntTy,
+ const DataLayout &DL) {
+ if (!GEP->hasOneUse())
+ return false;
+
+ // Skip cases whether there is a mismatch between the pointer integer type
+ // and the index type, or the GEP performs an implicit splat operation.
+ if (DL.getIndexType(GEP->getType()) != IntTy ||
+ GEP->getType() != GEP->getPointerOperand()->getType())
+ return false;
+
+ // (ptrtoint (gep (inttoptr Base), Offset)) -> Base + Offset
+ if (match(GEP->getPointerOperand(), m_OneUse(m_IntToPtr(m_Value()))))
+ return true;
+
+ return false;
+}
+
Instruction *InstCombinerImpl::visitPtrToInt(PtrToIntInst &CI) {
// If the destination integer type is not the intptr_t type for this target,
// do a ptrtoint to intptr_t then do a trunc or zext. This allows the cast
@@ -2064,10 +2083,8 @@ Instruction *InstCombinerImpl::visitPtrToInt(PtrToIntInst &CI) {
}
// (ptrtoint (gep (inttoptr Base), ...)) -> Base + Offset
- Value *Base;
- if (GEP->hasOneUse() &&
- match(GEP->getPointerOperand(), m_OneUse(m_IntToPtr(m_Value(Base)))) &&
- Base->getType() == Ty) {
+ if (shouldPushPtrToIntThroughGEP(GEP, Ty, DL)) {
+ Value *Base = Builder.CreatePtrToInt(GEP->getPointerOperand(), Ty);
Value *Offset = EmitGEPOffset(GEP);
auto *NewOp = BinaryOperator::CreateAdd(Base, Offset);
if (GEP->hasNoUnsignedWrap() ||
More information about the llvm-commits
mailing list