[llvm] [InstCombine] Fold pointer adding in integer to arithmetic add (PR #91596)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed May 15 20:38:55 PDT 2024


================
@@ -2073,6 +2073,20 @@ Instruction *InstCombinerImpl::visitPtrToInt(PtrToIntInst &CI) {
     return InsertElementInst::Create(Vec, NewCast, Index);
   }
 
+  // (ptrtoint (gep (inttoptr Base), Offset)) -> Base + Offset
+  Value *Base, *Offset, *GEP;
+  if (match(SrcOp, m_OneUse(m_Value(GEP))) &&
+      match(GEP,
+            m_PtrAdd(m_OneUse(m_IntToPtr(m_Value(Base))), m_Value(Offset))) &&
+      Base->getType() == Ty && Offset->getType() == Ty) {
+    auto *GEPInst = cast<GetElementPtrInst>(GEP);
----------------
nikic wrote:

```suggestion
    auto *GEPInst = cast<GEPOperator>(GEP);
```
is safer

https://github.com/llvm/llvm-project/pull/91596


More information about the llvm-commits mailing list