[llvm] c89b69a - [ADCE] Keep track of if we modified the CFG and preserve analyses accordingly

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 1 09:22:37 PST 2023


Author: Arthur Eubanks
Date: 2023-03-01T09:22:22-08:00
New Revision: c89b69a1274a5b822326a9169dcb39cd4403440b

URL: https://github.com/llvm/llvm-project/commit/c89b69a1274a5b822326a9169dcb39cd4403440b
DIFF: https://github.com/llvm/llvm-project/commit/c89b69a1274a5b822326a9169dcb39cd4403440b.diff

LOG: [ADCE] Keep track of if we modified the CFG and preserve analyses accordingly

No measurable compile time impact, but might as well resolve the TODO.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D145014

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/ADCE.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/ADCE.cpp b/llvm/lib/Transforms/Scalar/ADCE.cpp
index 253293582945..d03a5033f403 100644
--- a/llvm/lib/Transforms/Scalar/ADCE.cpp
+++ b/llvm/lib/Transforms/Scalar/ADCE.cpp
@@ -113,6 +113,11 @@ struct BlockInfoType {
   bool terminatorIsLive() const { return TerminatorLiveInfo->Live; }
 };
 
+struct ADCEChanged {
+  bool ChangedAnything = false;
+  bool ChangedControlFlow = false;
+};
+
 class AggressiveDeadCodeElimination {
   Function &F;
 
@@ -179,7 +184,7 @@ class AggressiveDeadCodeElimination {
 
   /// Remove instructions not marked live, return if any instruction was
   /// removed.
-  bool removeDeadInstructions();
+  ADCEChanged removeDeadInstructions();
 
   /// Identify connected sections of the control flow graph which have
   /// dead terminators and rewrite the control flow graph to remove them.
@@ -197,12 +202,12 @@ class AggressiveDeadCodeElimination {
                                 PostDominatorTree &PDT)
       : F(F), DT(DT), PDT(PDT) {}
 
-  bool performDeadCodeElimination();
+  ADCEChanged performDeadCodeElimination();
 };
 
 } // end anonymous namespace
 
-bool AggressiveDeadCodeElimination::performDeadCodeElimination() {
+ADCEChanged AggressiveDeadCodeElimination::performDeadCodeElimination() {
   initialize();
   markLiveInstructions();
   return removeDeadInstructions();
@@ -504,9 +509,10 @@ void AggressiveDeadCodeElimination::markLiveBranchesFromControlDependences() {
 //  Routines to update the CFG and SSA information before removing dead code.
 //
 //===----------------------------------------------------------------------===//
-bool AggressiveDeadCodeElimination::removeDeadInstructions() {
+ADCEChanged AggressiveDeadCodeElimination::removeDeadInstructions() {
+  ADCEChanged Changed;
   // Updates control and dataflow around dead blocks
-  bool RegionsUpdated = updateDeadRegions();
+  Changed.ChangedControlFlow = updateDeadRegions();
 
   LLVM_DEBUG({
     for (Instruction &I : instructions(F)) {
@@ -569,7 +575,9 @@ bool AggressiveDeadCodeElimination::removeDeadInstructions() {
     I->eraseFromParent();
   }
 
-  return !Worklist.empty() || RegionsUpdated;
+  Changed.ChangedAnything = Changed.ChangedControlFlow || !Worklist.empty();
+
+  return Changed;
 }
 
 // A dead region is the set of dead blocks with a common live post-dominator.
@@ -699,17 +707,17 @@ PreservedAnalyses ADCEPass::run(Function &F, FunctionAnalysisManager &FAM) {
   // to update analysis if it is already available.
   auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
   auto &PDT = FAM.getResult<PostDominatorTreeAnalysis>(F);
-  if (!AggressiveDeadCodeElimination(F, DT, PDT).performDeadCodeElimination())
+  ADCEChanged Changed =
+      AggressiveDeadCodeElimination(F, DT, PDT).performDeadCodeElimination();
+  if (!Changed.ChangedAnything)
     return PreservedAnalyses::all();
 
   PreservedAnalyses PA;
-  // TODO: We could track if we have actually done CFG changes.
-  if (!RemoveControlFlowFlag)
+  if (!Changed.ChangedControlFlow)
     PA.preserveSet<CFGAnalyses>();
-  else {
-    PA.preserve<DominatorTreeAnalysis>();
-    PA.preserve<PostDominatorTreeAnalysis>();
-  }
+  PA.preserve<DominatorTreeAnalysis>();
+  PA.preserve<PostDominatorTreeAnalysis>();
+
   return PA;
 }
 
@@ -731,8 +739,9 @@ struct ADCELegacyPass : public FunctionPass {
     auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
     auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
     auto &PDT = getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
-    return AggressiveDeadCodeElimination(F, DT, PDT)
-        .performDeadCodeElimination();
+    ADCEChanged Changed =
+        AggressiveDeadCodeElimination(F, DT, PDT).performDeadCodeElimination();
+    return Changed.ChangedAnything;
   }
 
   void getAnalysisUsage(AnalysisUsage &AU) const override {


        


More information about the llvm-commits mailing list