Skip to content

Commit 4774756

Browse files
committed
[SCEV] Expose getGEPExpr without needing to pass GEPOperator* (NFC)
Add a new getGEPExpr variant which is independent of GEPOperator*. To be used to construct SCEVs for VPlan recipes in #161276.
1 parent dda30e1 commit 4774756

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,9 @@ class ScalarEvolution {
640640
/// \p IndexExprs The expressions for the indices.
641641
LLVM_ABI const SCEV *
642642
getGEPExpr(GEPOperator *GEP, const SmallVectorImpl<const SCEV *> &IndexExprs);
643+
LLVM_ABI const SCEV *getGEPExpr(
644+
const SCEV *BaseExpr, const SmallVectorImpl<const SCEV *> &IndexExprs,
645+
Type *SrcElementTy, SCEV::NoWrapFlags OffsetWrap = SCEV::FlagAnyWrap);
643646
LLVM_ABI const SCEV *getAbsExpr(const SCEV *Op, bool IsNSW);
644647
LLVM_ABI const SCEV *getMinMaxExpr(SCEVTypes Kind,
645648
SmallVectorImpl<const SCEV *> &Operands);

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3774,7 +3774,6 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP,
37743774
const SCEV *BaseExpr = getSCEV(GEP->getPointerOperand());
37753775
// getSCEV(Base)->getType() has the same address space as Base->getType()
37763776
// because SCEV::getType() preserves the address space.
3777-
Type *IntIdxTy = getEffectiveSCEVType(BaseExpr->getType());
37783777
GEPNoWrapFlags NW = GEP->getNoWrapFlags();
37793778
if (NW != GEPNoWrapFlags::none()) {
37803779
// We'd like to propagate flags from the IR to the corresponding SCEV nodes,
@@ -3793,7 +3792,16 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP,
37933792
if (NW.hasNoUnsignedWrap())
37943793
OffsetWrap = setFlags(OffsetWrap, SCEV::FlagNUW);
37953794

3796-
Type *CurTy = GEP->getType();
3795+
return getGEPExpr(BaseExpr, IndexExprs, GEP->getSourceElementType(),
3796+
OffsetWrap);
3797+
}
3798+
3799+
const SCEV *
3800+
ScalarEvolution::getGEPExpr(const SCEV *BaseExpr,
3801+
const SmallVectorImpl<const SCEV *> &IndexExprs,
3802+
Type *SrcElementTy, SCEV::NoWrapFlags OffsetWrap) {
3803+
Type *CurTy = BaseExpr->getType();
3804+
Type *IntIdxTy = getEffectiveSCEVType(BaseExpr->getType());
37973805
bool FirstIter = true;
37983806
SmallVector<const SCEV *, 4> Offsets;
37993807
for (const SCEV *IndexExpr : IndexExprs) {
@@ -3812,7 +3820,7 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP,
38123820
if (FirstIter) {
38133821
assert(isa<PointerType>(CurTy) &&
38143822
"The first index of a GEP indexes a pointer");
3815-
CurTy = GEP->getSourceElementType();
3823+
CurTy = SrcElementTy;
38163824
FirstIter = false;
38173825
} else {
38183826
CurTy = GetElementPtrInst::getTypeAtIndex(CurTy, (uint64_t)0);
@@ -3837,8 +3845,9 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP,
38373845
// Add the base address and the offset. We cannot use the nsw flag, as the
38383846
// base address is unsigned. However, if we know that the offset is
38393847
// non-negative, we can use nuw.
3840-
bool NUW = NW.hasNoUnsignedWrap() ||
3841-
(NW.hasNoUnsignedSignedWrap() && isKnownNonNegative(Offset));
3848+
bool NUW =
3849+
hasFlags(OffsetWrap, SCEV::FlagNUW) ||
3850+
(hasFlags(OffsetWrap, SCEV::FlagNSW) && isKnownNonNegative(Offset));
38423851
SCEV::NoWrapFlags BaseWrap = NUW ? SCEV::FlagNUW : SCEV::FlagAnyWrap;
38433852
auto *GEPExpr = getAddExpr(BaseExpr, Offset, BaseWrap);
38443853
assert(BaseExpr->getType() == GEPExpr->getType() &&

0 commit comments

Comments
 (0)