[PATCH] D105503: [ScalarEvolution] Make sure getMinusSCEV doesn't negate pointers.
Eli Friedman via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 7 10:27:53 PDT 2021
This revision was automatically updated to reflect the committed changes.
Closed by commit rGf5603aa050ce: [ScalarEvolution] Make sure getMinusSCEV doesn't negate pointers. (authored by efriedma).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D105503/new/
https://reviews.llvm.org/D105503
Files:
llvm/lib/Analysis/ScalarEvolution.cpp
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -4131,6 +4131,41 @@
return getMinusSCEV(getMinusOne(Ty), V);
}
+/// Compute an expression equivalent to S - getPointerBase(S).
+static const SCEV *removePointerBase(ScalarEvolution *SE, const SCEV *P) {
+ assert(P->getType()->isPointerTy());
+
+ if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(P)) {
+ // The base of an AddRec is the first operand.
+ SmallVector<const SCEV *> Ops{AddRec->operands()};
+ Ops[0] = removePointerBase(SE, Ops[0]);
+ // Don't try to transfer nowrap flags for now. We could in some cases
+ // (for example, if pointer operand of the AddRec is a SCEVUnknown).
+ return SE->getAddRecExpr(Ops, AddRec->getLoop(), SCEV::FlagAnyWrap);
+ }
+ if (auto *Add = dyn_cast<SCEVAddExpr>(P)) {
+ // The base of an Add is the pointer operand.
+ SmallVector<const SCEV *> Ops{Add->operands()};
+ const SCEV **PtrOp = nullptr;
+ for (const SCEV *&AddOp : Ops) {
+ if (AddOp->getType()->isPointerTy()) {
+ // If we find an Add with multiple pointer operands, treat it as a
+ // pointer base to be consistent with getPointerBase. Eventually
+ // we should be able to assert this is impossible.
+ if (PtrOp)
+ return SE->getZero(P->getType());
+ PtrOp = &AddOp;
+ }
+ }
+ *PtrOp = removePointerBase(SE, *PtrOp);
+ // Don't try to transfer nowrap flags for now. We could in some cases
+ // (for example, if the pointer operand of the Add is a SCEVUnknown).
+ return SE->getAddExpr(Ops);
+ }
+ // Any other expression must be a pointer base.
+ return SE->getZero(P->getType());
+}
+
const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, const SCEV *RHS,
SCEV::NoWrapFlags Flags,
unsigned Depth) {
@@ -4145,6 +4180,8 @@
if (!LHS->getType()->isPointerTy() ||
getPointerBase(LHS) != getPointerBase(RHS))
return getCouldNotCompute();
+ LHS = removePointerBase(this, LHS);
+ RHS = removePointerBase(this, RHS);
}
// We represent LHS - RHS as LHS + (-1)*RHS. This transformation
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105503.356993.patch
Type: text/x-patch
Size: 2315 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210707/5cbed639/attachment.bin>
More information about the llvm-commits
mailing list