[PATCH] D106450: [InstCombine] Fold (gep (oneuse(gep Ptr, Idx0)), Idx1) -> (gep Ptr, (add Idx0, Idx1)) (PR51069)

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 22 09:25:30 PDT 2021


lebedev.ri added a comment.

So i think this should be something like

  if (Src->hasOneUse() &&
      GEP.getOperand(1)->getType() ==
          Src->getOperand(Src->getNumOperands() - 1)->getType()) {
    // Fold (gep(gep(Ptr,Idx0),Idx1) -> gep(Ptr,add(Idx0,Idx1))
    bool NewInBounds = isMergedGEPInBounds(*Src, *cast<GEPOperator>(&GEP));
    SmallVector<Value *, 8> Indices;
    Indices.reserve(Src->getNumIndices() + GEP.getNumIndices() - 1);
    append_range(Indices, Src->indices());
    Indices.back() = Builder.CreateAdd(
        *GEP.idx_begin(), Indices.back(), GEP.getName() + ".idx",
        /*HasNUW*/ false, /*HasNSW*/ NewInBounds);
    append_range(Indices, make_range(GEP.idx_begin() + 1, GEP.idx_end()));
    auto *NewGEP = GetElementPtrInst::Create(
        Src->getSourceElementType(), Src->getPointerOperand(), Indices);
    NewGEP->setIsInBounds(NewInBounds);
    return NewGEP;
  }

but i don't think i will pursue it further than this snippet.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106450/new/

https://reviews.llvm.org/D106450



More information about the llvm-commits mailing list