[llvm] [InstCombine] Support ptrtoint of gep folds for chain of geps (PR #137323)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Sat Apr 26 07:47:23 PDT 2025
================
@@ -2067,6 +2067,42 @@ Instruction *InstCombinerImpl::visitIntToPtr(IntToPtrInst &CI) {
return nullptr;
}
+Value *InstCombinerImpl::foldPtrToIntOfGEP(Type *IntTy, Value *Ptr) {
+ // Look through chain of one-use GEPs.
+ Type *PtrTy = Ptr->getType();
+ SmallVector<GEPOperator *> GEPs;
+ while (true) {
+ auto *GEP = dyn_cast<GEPOperator>(Ptr);
+ if (!GEP || !GEP->hasOneUse())
+ break;
+ GEPs.push_back(GEP);
+ Ptr = GEP->getPointerOperand();
+ }
+
+ // Don't handle case where GEP converts from pointer to vector.
+ if (GEPs.empty() || PtrTy != Ptr->getType())
+ return nullptr;
+
+ // Check whether we know the integer value of the base pointer.
+ Value *Res;
+ Type *IdxTy = DL.getIndexType(PtrTy);
+ if (match(Ptr, m_OneUse(m_IntToPtr(m_Value(Res)))) &&
+ Res->getType() == IntTy && IntTy == IdxTy) {
----------------
nikic wrote:
I think this is correct, but it's worth noting that we can only run into this if the inttoptr hasn't been canonicalized yet (otherwise it would be guaranteed to use IntTy), so it's not really possible to test this case.
https://github.com/llvm/llvm-project/pull/137323
More information about the llvm-commits
mailing list