Skip to content

Conversation

@fhahn
Copy link
Contributor

@fhahn fhahn commented Oct 21, 2025

Add a new getGEPExpr variant which is independent of GEPOperator*.

To be used to construct SCEVs for VPlan recipes in #161276.

@fhahn fhahn requested review from artagnon, nikic and preames October 21, 2025 20:16
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Oct 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 21, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Florian Hahn (fhahn)

Changes

Add a new getGEPExpr variant which is independent of GEPOperator*.

To be used to construct SCEVs for VPlan recipes in #161276.


Full diff: https://github.com/llvm/llvm-project/pull/164487.diff

2 Files Affected:

  • (modified) llvm/include/llvm/Analysis/ScalarEvolution.h (+3)
  • (modified) llvm/lib/Analysis/ScalarEvolution.cpp (+14-5)
diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h index 3d3ec14796bc1..c419aed107464 100644 --- a/llvm/include/llvm/Analysis/ScalarEvolution.h +++ b/llvm/include/llvm/Analysis/ScalarEvolution.h @@ -640,6 +640,9 @@ class ScalarEvolution { /// \p IndexExprs The expressions for the indices. LLVM_ABI const SCEV * getGEPExpr(GEPOperator *GEP, const SmallVectorImpl<const SCEV *> &IndexExprs); + LLVM_ABI const SCEV *getGEPExpr( + const SCEV *BaseExpr, const SmallVectorImpl<const SCEV *> &IndexExprs, + Type *SrcElementTy, SCEV::NoWrapFlags OffsetWrap = SCEV::FlagAnyWrap); LLVM_ABI const SCEV *getAbsExpr(const SCEV *Op, bool IsNSW); LLVM_ABI const SCEV *getMinMaxExpr(SCEVTypes Kind, SmallVectorImpl<const SCEV *> &Operands); diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index 6f7dd79032cfe..4856c51ee2a4a 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -3774,7 +3774,6 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP, const SCEV *BaseExpr = getSCEV(GEP->getPointerOperand()); // getSCEV(Base)->getType() has the same address space as Base->getType() // because SCEV::getType() preserves the address space. - Type *IntIdxTy = getEffectiveSCEVType(BaseExpr->getType()); GEPNoWrapFlags NW = GEP->getNoWrapFlags(); if (NW != GEPNoWrapFlags::none()) { // We'd like to propagate flags from the IR to the corresponding SCEV nodes, @@ -3793,7 +3792,16 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP, if (NW.hasNoUnsignedWrap()) OffsetWrap = setFlags(OffsetWrap, SCEV::FlagNUW); - Type *CurTy = GEP->getType(); + return getGEPExpr(BaseExpr, IndexExprs, GEP->getSourceElementType(), + OffsetWrap); +} + +const SCEV * +ScalarEvolution::getGEPExpr(const SCEV *BaseExpr, + const SmallVectorImpl<const SCEV *> &IndexExprs, + Type *SrcElementTy, SCEV::NoWrapFlags OffsetWrap) { + Type *CurTy = BaseExpr->getType(); + Type *IntIdxTy = getEffectiveSCEVType(BaseExpr->getType()); bool FirstIter = true; SmallVector<const SCEV *, 4> Offsets; for (const SCEV *IndexExpr : IndexExprs) { @@ -3812,7 +3820,7 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP, if (FirstIter) { assert(isa<PointerType>(CurTy) && "The first index of a GEP indexes a pointer"); - CurTy = GEP->getSourceElementType(); + CurTy = SrcElementTy; FirstIter = false; } else { CurTy = GetElementPtrInst::getTypeAtIndex(CurTy, (uint64_t)0); @@ -3837,8 +3845,9 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP, // Add the base address and the offset. We cannot use the nsw flag, as the // base address is unsigned. However, if we know that the offset is // non-negative, we can use nuw. - bool NUW = NW.hasNoUnsignedWrap() || - (NW.hasNoUnsignedSignedWrap() && isKnownNonNegative(Offset)); + bool NUW = + hasFlags(OffsetWrap, SCEV::FlagNUW) || + (hasFlags(OffsetWrap, SCEV::FlagNSW) && isKnownNonNegative(Offset)); SCEV::NoWrapFlags BaseWrap = NUW ? SCEV::FlagNUW : SCEV::FlagAnyWrap; auto *GEPExpr = getAddExpr(BaseExpr, Offset, BaseWrap); assert(BaseExpr->getType() == GEPExpr->getType() && 
Copy link
Contributor

@artagnon artagnon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! ArrayRef improvement is optional.

getGEPExpr(GEPOperator *GEP, const SmallVectorImpl<const SCEV *> &IndexExprs);
LLVM_ABI const SCEV *getGEPExpr(
const SCEV *BaseExpr, const SmallVectorImpl<const SCEV *> &IndexExprs,
Type *SrcElementTy, SCEV::NoWrapFlags OffsetWrap = SCEV::FlagAnyWrap);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should accept GEPNoWrapFlags instead of OffsetWrap. We should not infer the base wrapping behavior from the offset wrapping behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, although currently I think the behavior currently stays the same, as OffsetWrap would be set to match the GEPNoWrapFlags (although with NSW instead of hasNoUnsignedSignedWrap)?

Add a new getGEPExpr variant which is independent of GEPOperator*. To be used to construct SCEVs for VPlan recipes in llvm#161276.
@fhahn fhahn force-pushed the scev-create-gep-refactor branch from 333e5a1 to fcc832e Compare October 22, 2025 08:51
@fhahn fhahn force-pushed the scev-create-gep-refactor branch from fcc832e to 790ea77 Compare October 22, 2025 08:53
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fhahn fhahn merged commit a321ce3 into llvm:main Oct 22, 2025
10 checks passed
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Oct 22, 2025
…or* (NFC) (#164487) Add a new getGEPExpr variant which is independent of GEPOperator*. To be used to construct SCEVs for VPlan recipes in llvm/llvm-project#161276. PR: llvm/llvm-project#164487
@fhahn fhahn deleted the scev-create-gep-refactor branch October 22, 2025 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding

4 participants