From 63aeaf754a78c67ca3f8343d525dfb7a378dfa9e Mon Sep 17 00:00:00 2001 From: Alina Sbirlea Date: Mon, 14 Dec 2020 11:53:35 -0800 Subject: [PATCH] [DominatorTree] Add support for mixed pre/post CFG views. Add support for mixed pre/post CFG views. Update usages of the MemorySSAUpdater to use the new DT API by requesting the DT updates to be done by the MSSAUpdater. Differential Revision: https://reviews.llvm.org/D93371 --- llvm/include/llvm/Analysis/MemorySSAUpdater.h | 7 +++++-- llvm/include/llvm/Support/GenericDomTree.h | 18 +++++++++++------- llvm/lib/Analysis/MemorySSAUpdater.cpp | 17 ++++++++++++----- llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp | 8 ++++---- llvm/lib/Transforms/Scalar/LoopUnswitch.cpp | 5 +++-- .../Transforms/Scalar/SimpleLoopUnswitch.cpp | 5 +++-- .../lib/Transforms/Utils/LoopRotationUtils.cpp | 5 +++-- 7 files changed, 41 insertions(+), 24 deletions(-) diff --git a/llvm/include/llvm/Analysis/MemorySSAUpdater.h b/llvm/include/llvm/Analysis/MemorySSAUpdater.h index d41b93209979..b0bf2e5ead62 100644 --- a/llvm/include/llvm/Analysis/MemorySSAUpdater.h +++ b/llvm/include/llvm/Analysis/MemorySSAUpdater.h @@ -119,8 +119,11 @@ public: ArrayRef ExitBlocks, ArrayRef> VMaps, DominatorTree &DT); - /// Apply CFG updates, analogous with the DT edge updates. - void applyUpdates(ArrayRef Updates, DominatorTree &DT); + /// Apply CFG updates, analogous with the DT edge updates. By default, the + /// DT is assumed to be already up to date. If UpdateDTFirst is true, first + /// update the DT with the same updates. + void applyUpdates(ArrayRef Updates, DominatorTree &DT, + bool UpdateDTFirst = false); /// Apply CFG insert updates, analogous with the DT edge updates. void applyInsertUpdates(ArrayRef Updates, DominatorTree &DT); diff --git a/llvm/include/llvm/Support/GenericDomTree.h b/llvm/include/llvm/Support/GenericDomTree.h index d2d7c8c4481d..28b2537bc481 100644 --- a/llvm/include/llvm/Support/GenericDomTree.h +++ b/llvm/include/llvm/Support/GenericDomTree.h @@ -550,7 +550,7 @@ protected: /// \param Updates An unordered sequence of updates to perform. The current /// CFG and the reverse of these updates provides the pre-view of the CFG. /// \param PostViewUpdates An unordered sequence of update to perform in order - /// to obtain a post-view of the CFG. The DT will be updates assuming the + /// to obtain a post-view of the CFG. The DT will be updated assuming the /// obtained PostViewCFG is the desired end state. void applyUpdates(ArrayRef Updates, ArrayRef PostViewUpdates) { @@ -558,14 +558,18 @@ protected: GraphDiff PostViewCFG(PostViewUpdates); DomTreeBuilder::ApplyUpdates(*this, PostViewCFG, &PostViewCFG); } else { - // TODO: // PreViewCFG needs to merge Updates and PostViewCFG. The updates in // Updates need to be reversed, and match the direction in PostViewCFG. - // Normally, a PostViewCFG is created without reversing updates, so one - // of the internal vectors needs reversing in order to do the - // legalization of the merged vector of updates. - llvm_unreachable("Currently unsupported to update given a set of " - "updates towards a PostView"); + // The PostViewCFG is created with updates reversed (equivalent to changes + // made to the CFG), so the PreViewCFG needs all the updates reverse + // applied. + SmallVector AllUpdates(Updates.begin(), Updates.end()); + for (auto &Update : PostViewUpdates) + AllUpdates.push_back(Update); + GraphDiff PreViewCFG(AllUpdates, + /*ReverseApplyUpdates=*/true); + GraphDiff PostViewCFG(PostViewUpdates); + DomTreeBuilder::ApplyUpdates(*this, PreViewCFG, &PostViewCFG); } } diff --git a/llvm/lib/Analysis/MemorySSAUpdater.cpp b/llvm/lib/Analysis/MemorySSAUpdater.cpp index 4ff61d4324f8..99fa58b8872a 100644 --- a/llvm/lib/Analysis/MemorySSAUpdater.cpp +++ b/llvm/lib/Analysis/MemorySSAUpdater.cpp @@ -811,7 +811,7 @@ void MemorySSAUpdater::updateExitBlocksForClonedLoop( } void MemorySSAUpdater::applyUpdates(ArrayRef Updates, - DominatorTree &DT) { + DominatorTree &DT, bool UpdateDT) { SmallVector DeleteUpdates; SmallVector RevDeleteUpdates; SmallVector InsertUpdates; @@ -825,10 +825,15 @@ void MemorySSAUpdater::applyUpdates(ArrayRef Updates, } if (!DeleteUpdates.empty()) { - SmallVector Empty; - // Deletes are reversed applied, because this CFGView is pretending the - // deletes did not happen yet, hence the edges still exist. - DT.applyUpdates(Empty, RevDeleteUpdates); + if (!UpdateDT) { + SmallVector Empty; + // Deletes are reversed applied, because this CFGView is pretending the + // deletes did not happen yet, hence the edges still exist. + DT.applyUpdates(Empty, RevDeleteUpdates); + } else { + // Apply all updates, with the RevDeleteUpdates as PostCFGView. + DT.applyUpdates(Updates, RevDeleteUpdates); + } // Note: the MSSA update below doesn't distinguish between a GD with // (RevDelete,false) and (Delete, true), but this matters for the DT @@ -840,6 +845,8 @@ void MemorySSAUpdater::applyUpdates(ArrayRef Updates, // the standard update without a postview of the CFG. DT.applyUpdates(DeleteUpdates); } else { + if (UpdateDT) + DT.applyUpdates(Updates); GraphDiff GD; applyInsertUpdates(InsertUpdates, DT, &GD); } diff --git a/llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp b/llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp index c207ef9c4094..cc6d11220807 100644 --- a/llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp +++ b/llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp @@ -414,9 +414,10 @@ private: FixLCSSALoop = FixLCSSALoop->getParentLoop(); assert(FixLCSSALoop && "Should be a loop!"); // We need all DT updates to be done before forming LCSSA. - DTU.applyUpdates(DTUpdates); if (MSSAU) - MSSAU->applyUpdates(DTUpdates, DT); + MSSAU->applyUpdates(DTUpdates, DT, /*UpdateDT=*/true); + else + DTU.applyUpdates(DTUpdates); DTUpdates.clear(); formLCSSARecursively(*FixLCSSALoop, DT, &LI, &SE); } @@ -424,8 +425,7 @@ private: if (MSSAU) { // Clear all updates now. Facilitates deletes that follow. - DTU.applyUpdates(DTUpdates); - MSSAU->applyUpdates(DTUpdates, DT); + MSSAU->applyUpdates(DTUpdates, DT, /*UpdateDT=*/true); DTUpdates.clear(); if (VerifyMemorySSA) MSSAU->getMemorySSA()->verifyMemorySSA(); diff --git a/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp index 590b845082dc..3c1a6d118c80 100644 --- a/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp +++ b/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp @@ -960,10 +960,11 @@ void LoopUnswitch::emitPreheaderBranchOnCondition(Value *LIC, Constant *Val, if (OldBranchSucc != TrueDest && OldBranchSucc != FalseDest) { Updates.push_back({DominatorTree::Delete, OldBranchParent, OldBranchSucc}); } - DT->applyUpdates(Updates); if (MSSAU) - MSSAU->applyUpdates(Updates, *DT); + MSSAU->applyUpdates(Updates, *DT, /*UpdateDT=*/true); + else + DT->applyUpdates(Updates); } // If either edge is critical, split it. This helps preserve LoopSimplify diff --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp index 05e09a40f184..945476a2bdaf 100644 --- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp +++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp @@ -855,12 +855,13 @@ static bool unswitchTrivialSwitch(Loop &L, SwitchInst &SI, DominatorTree &DT, DTUpdates.push_back({DT.Delete, ParentBB, SplitUnswitchedPair.first}); DTUpdates.push_back({DT.Insert, OldPH, SplitUnswitchedPair.second}); } - DT.applyUpdates(DTUpdates); if (MSSAU) { - MSSAU->applyUpdates(DTUpdates, DT); + MSSAU->applyUpdates(DTUpdates, DT, /*UpdateDT=*/true); if (VerifyMemorySSA) MSSAU->getMemorySSA()->verifyMemorySSA(); + } else { + DT.applyUpdates(DTUpdates); } assert(DT.verify(DominatorTree::VerificationLevel::Fast)); diff --git a/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp b/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp index 68435c2f6c79..eb7018ce9aa3 100644 --- a/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp +++ b/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp @@ -498,12 +498,13 @@ bool LoopRotate::rotateLoop(Loop *L, bool SimplifiedLatch) { Updates.push_back({DominatorTree::Insert, OrigPreheader, Exit}); Updates.push_back({DominatorTree::Insert, OrigPreheader, NewHeader}); Updates.push_back({DominatorTree::Delete, OrigPreheader, OrigHeader}); - DT->applyUpdates(Updates); if (MSSAU) { - MSSAU->applyUpdates(Updates, *DT); + MSSAU->applyUpdates(Updates, *DT, /*UpdateDT=*/true); if (VerifyMemorySSA) MSSAU->getMemorySSA()->verifyMemorySSA(); + } else { + DT->applyUpdates(Updates); } }