aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
diff options
context:
space:
mode:
authorNikita Popov <npopov@redhat.com>2024-10-02 11:45:00 +0200
committerGitHub <noreply@github.com>2024-10-02 11:45:00 +0200
commit9f3d1695eb97a4612b0cf2bbc5eb85ebdb5eaff8 (patch)
tree38e89e85b5691f7fff1a1797a924dd9f283f89fb /llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
parent383a67042a30c12ca492d306f2f6a2459764022f (diff)
downloadllvm-9f3d1695eb97a4612b0cf2bbc5eb85ebdb5eaff8.zip
llvm-9f3d1695eb97a4612b0cf2bbc5eb85ebdb5eaff8.tar.gz
llvm-9f3d1695eb97a4612b0cf2bbc5eb85ebdb5eaff8.tar.bz2
[SCEVExpander] Preserve gep nuw during expansion (#102133)
When expanding SCEV adds to geps, transfer the nuw flag to the resulting gep. (Note that this doesn't apply to IV increment GEPs, which go through a different code path.)
Diffstat (limited to 'llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp16
1 files changed, 10 insertions, 6 deletions
diff --git a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
index 1088547..87f523c 100644
--- a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
+++ b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
@@ -352,16 +352,19 @@ Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode,
/// loop-invariant portions of expressions, after considering what
/// can be folded using target addressing modes.
///
-Value *SCEVExpander::expandAddToGEP(const SCEV *Offset, Value *V) {
+Value *SCEVExpander::expandAddToGEP(const SCEV *Offset, Value *V,
+ SCEV::NoWrapFlags Flags) {
assert(!isa<Instruction>(V) ||
SE.DT.dominates(cast<Instruction>(V), &*Builder.GetInsertPoint()));
Value *Idx = expand(Offset);
+ GEPNoWrapFlags NW = (Flags & SCEV::FlagNUW) ? GEPNoWrapFlags::noUnsignedWrap()
+ : GEPNoWrapFlags::none();
// Fold a GEP with constant operands.
if (Constant *CLHS = dyn_cast<Constant>(V))
if (Constant *CRHS = dyn_cast<Constant>(Idx))
- return Builder.CreatePtrAdd(CLHS, CRHS);
+ return Builder.CreatePtrAdd(CLHS, CRHS, "", NW);
// Do a quick scan to see if we have this GEP nearby. If so, reuse it.
unsigned ScanLimit = 6;
@@ -380,7 +383,7 @@ Value *SCEVExpander::expandAddToGEP(const SCEV *Offset, Value *V) {
GEP->getSourceElementType() == Builder.getInt8Ty() &&
GEP->getOperand(1) == Idx) {
rememberFlags(GEP);
- GEP->setNoWrapFlags(GEPNoWrapFlags::none());
+ GEP->setNoWrapFlags(GEP->getNoWrapFlags() & NW);
return &*IP;
}
}
@@ -402,7 +405,7 @@ Value *SCEVExpander::expandAddToGEP(const SCEV *Offset, Value *V) {
}
// Emit a GEP.
- return Builder.CreatePtrAdd(V, Idx, "scevgep");
+ return Builder.CreatePtrAdd(V, Idx, "scevgep", NW);
}
/// PickMostRelevantLoop - Given two loops pick the one that's most relevant for
@@ -549,7 +552,7 @@ Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) {
X = SE.getSCEV(U->getValue());
NewOps.push_back(X);
}
- Sum = expandAddToGEP(SE.getAddExpr(NewOps), Sum);
+ Sum = expandAddToGEP(SE.getAddExpr(NewOps), Sum, S->getNoWrapFlags());
} else if (Op->isNonConstantNegative()) {
// Instead of doing a negate and add, just do a subtract.
Value *W = expand(SE.getNegativeSCEV(Op));
@@ -1251,7 +1254,8 @@ Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
if (!S->getStart()->isZero()) {
if (isa<PointerType>(S->getType())) {
Value *StartV = expand(SE.getPointerBase(S));
- return expandAddToGEP(SE.removePointerBase(S), StartV);
+ return expandAddToGEP(SE.removePointerBase(S), StartV,
+ S->getNoWrapFlags(SCEV::FlagNUW));
}
SmallVector<const SCEV *, 4> NewOps(S->operands());