diff options
author | Nikita Popov <npopov@redhat.com> | 2023-06-16 17:16:52 +0200 |
---|---|---|
committer | Nikita Popov <npopov@redhat.com> | 2023-06-26 14:43:31 +0200 |
commit | 0e34b6a504a61d845209e58be70eaf829e59a922 (patch) | |
tree | c89ec133471e9f46e1160ab5866b34b0d6c038e9 /llvm/lib/Transforms/Utils/LCSSA.cpp | |
parent | 12bddc81b076a8acb62e940020e5b66597c86aca (diff) | |
download | llvm-0e34b6a504a61d845209e58be70eaf829e59a922.zip llvm-0e34b6a504a61d845209e58be70eaf829e59a922.tar.gz llvm-0e34b6a504a61d845209e58be70eaf829e59a922.tar.bz2 |
[LCSSA] Compute SCEV of LCSSA phi if original instruction had SCEV
The backstory is that the LCSSA invalidation we perform here is not
really necessary from a SCEV perspective. However, other code may
rely on the fact that invalidating only LCSSA phi nodes is sufficient
for transforms like loop peeling
(see https://reviews.llvm.org/D149331#4398582 for more details).
However, performing invalidation during LCSSA construction also
means that SCEV expansion (which may need to construct LCSSA) can
invalidate SCEV, which is somewhat unexpected and code may not be
prepared to deal with it (see the added test case, reported at
https://reviews.llvm.org/D149435#4428219).
Instead of invalidating SCEV, ensure that the LCSSA phi node also
has cached SCEV if the original instruction did. This means that
later invalidation of LCSSA phi nodes will work as expected. This
should avoid both the above issues and be more efficient.
Differential Revision: https://reviews.llvm.org/D153145
Diffstat (limited to 'llvm/lib/Transforms/Utils/LCSSA.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/LCSSA.cpp | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/llvm/lib/Transforms/Utils/LCSSA.cpp b/llvm/lib/Transforms/Utils/LCSSA.cpp index 183e0a4..c36b053 100644 --- a/llvm/lib/Transforms/Utils/LCSSA.cpp +++ b/llvm/lib/Transforms/Utils/LCSSA.cpp @@ -148,13 +148,10 @@ bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist, SSAUpdater SSAUpdate(&LocalInsertedPHIs); SSAUpdate.Initialize(I->getType(), I->getName()); - // Force re-computation of I, as some users now need to use the new PHI - // node. - if (SE) - SE->forgetValue(I); - // Insert the LCSSA phi's into all of the exit blocks dominated by the // value, and add them to the Phi's map. + bool HasSCEV = SE && SE->isSCEVable(I->getType()) && + SE->getExistingSCEV(I) != nullptr; for (BasicBlock *ExitBB : ExitBlocks) { if (!DT.dominates(DomNode, DT.getNode(ExitBB))) continue; @@ -202,6 +199,13 @@ bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist, if (auto *OtherLoop = LI.getLoopFor(ExitBB)) if (!L->contains(OtherLoop)) PostProcessPHIs.push_back(PN); + + // If we have a cached SCEV for the original instruction, make sure the + // new LCSSA phi node is also cached. This makes sures that BECounts + // based on it will be invalidated when the LCSSA phi node is invalidated, + // which some passes rely on. + if (HasSCEV) + SE->getSCEV(PN); } // Rewrite all uses outside the loop in terms of the new PHIs we just |