diff options
author | Dmitry Makogon <d.makogon@g.nsu.ru> | 2023-06-19 16:33:46 +0700 |
---|---|---|
committer | Dmitry Makogon <d.makogon@g.nsu.ru> | 2023-06-22 15:49:15 +0700 |
commit | ce1ac1cf18040486e2d2ec0b962287afc1641fec (patch) | |
tree | 22d8eae51cca83d491ba3d3f501a7e3121cce450 /llvm/lib/Analysis/ScalarEvolution.cpp | |
parent | efc290ce9c2b85bbd66ebc9ea43986fe89cbff15 (diff) | |
download | llvm-ce1ac1cf18040486e2d2ec0b962287afc1641fec.zip llvm-ce1ac1cf18040486e2d2ec0b962287afc1641fec.tar.gz llvm-ce1ac1cf18040486e2d2ec0b962287afc1641fec.tar.bz2 |
[SCEV] Don't store AddRec loop when simplifying multiplication of AddRecs
When multiplying several AddRecs, we do the following simplification:
{A1,+,A2,+,...,+,An}<L> * {B1,+,B2,+,...,+,Bn}<L> = {x=1 in [ sum y=x..2x
[ sum z=max(y-x, y-n)..min(x,n) [
choose(x, 2x)*choose(2x-y, x-z)*A_{y-z}*B_z]]
],+,...up to x=2n}
This is done iteratively, pair by pair. So if we try to multiply three AddRecs
A1, A2, A3, then we'd try to simplify A1 * A2 to A1' and then try to
simplify A1' * A3 if A1' is also an AddRec.
The transform is only legal if the loops of the two AddRecs are the same.
It is checked in the code, but the loop of one of the AddRecs is stored
in a local variable and doesn't get updated when we simplify a pair to a new AddRec.
In the motivating test the new AddRec A1' was created for a different loop and,
as the loop variable didn't get updated, the check for different loops passed and
the transform worked for two AddRecs from different loops.
So it created a wrong SCEV. And it caused LSR to replace an instruction with another
one that had the same SCEV as the incorrectly computed one.
Differential Revision: https://reviews.llvm.org/D153254
Diffstat (limited to 'llvm/lib/Analysis/ScalarEvolution.cpp')
-rw-r--r-- | llvm/lib/Analysis/ScalarEvolution.cpp | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index 901ce76..7c09eb9 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -3261,9 +3261,8 @@ const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops, // if they are loop invariant w.r.t. the recurrence. SmallVector<const SCEV *, 8> LIOps; const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); - const Loop *AddRecLoop = AddRec->getLoop(); for (unsigned i = 0, e = Ops.size(); i != e; ++i) - if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) { + if (isAvailableAtLoopEntry(Ops[i], AddRec->getLoop())) { LIOps.push_back(Ops[i]); Ops.erase(Ops.begin()+i); --i; --e; @@ -3286,7 +3285,7 @@ const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops, // will be inferred if either NUW or NSW is true. SCEV::NoWrapFlags Flags = ComputeFlags({Scale, AddRec}); const SCEV *NewRec = getAddRecExpr( - NewOps, AddRecLoop, AddRec->getNoWrapFlags(Flags)); + NewOps, AddRec->getLoop(), AddRec->getNoWrapFlags(Flags)); // If all of the other operands were loop invariant, we are done. if (Ops.size() == 1) return NewRec; @@ -3320,7 +3319,7 @@ const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops, ++OtherIdx) { const SCEVAddRecExpr *OtherAddRec = dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx]); - if (!OtherAddRec || OtherAddRec->getLoop() != AddRecLoop) + if (!OtherAddRec || OtherAddRec->getLoop() != AddRec->getLoop()) continue; // Limit max number of arguments to avoid creation of unreasonably big @@ -3359,7 +3358,7 @@ const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops, AddRecOps.push_back(getAddExpr(SumOps, SCEV::FlagAnyWrap, Depth + 1)); } if (!Overflow) { - const SCEV *NewAddRec = getAddRecExpr(AddRecOps, AddRecLoop, + const SCEV *NewAddRec = getAddRecExpr(AddRecOps, AddRec->getLoop(), SCEV::FlagAnyWrap); if (Ops.size() == 2) return NewAddRec; Ops[Idx] = NewAddRec; |