[llvm] [InstCombine] Fold chained GEP with constant base into single GEP (PR #170439)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 4 00:02:29 PST 2025
================
@@ -2803,6 +2803,33 @@ Instruction *InstCombinerImpl::visitGEPOfGEP(GetElementPtrInst &GEP,
if (Src->getResultElementType() != GEP.getSourceElementType())
return nullptr;
+ // Fold chained GEP with constant base into single GEP:
+ // gep i8, (gep i8, %base, C1), (select Cond, C2, C3)
+ // -> gep i8, %base, (select Cond, C1+C2, C1+C3)
+ if (GEP.getNumIndices() == 1 && Src->getNumIndices() == 1) {
+ Value *SrcIdx = *Src->idx_begin();
+ Value *GEPIdx = *GEP.idx_begin();
+ const APInt *ConstOffset, *TrueVal, *FalseVal;
+ Value *Cond;
+ if (match(SrcIdx, m_APInt(ConstOffset)) &&
+ match(GEPIdx,
+ m_Select(m_Value(Cond), m_APInt(TrueVal), m_APInt(FalseVal)))) {
----------------
nikic wrote:
Please also test GEP with vector offsets (and the variant where one has scalar and one has vector). I think this is currently not handled correctly.
https://github.com/llvm/llvm-project/pull/170439
More information about the llvm-commits
mailing list