Index: llvm/lib/Transforms/Scalar/ADCE.cpp =================================================================== --- llvm/lib/Transforms/Scalar/ADCE.cpp +++ llvm/lib/Transforms/Scalar/ADCE.cpp @@ -113,6 +113,11 @@ bool terminatorIsLive() const { return TerminatorLiveInfo->Live; } }; +struct ADCEChanged { + bool ChangedAnything = false; + bool ChangedControlFlow = false; +}; + class AggressiveDeadCodeElimination { Function &F; @@ -179,7 +184,7 @@ /// 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 @@ 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 @@ // 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 @@ 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 @@ // to update analysis if it is already available. auto *DT = FAM.getCachedResult(F); auto &PDT = FAM.getResult(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(); - else { - PA.preserve(); - PA.preserve(); - } + PA.preserve(); + PA.preserve(); + return PA; } @@ -731,8 +739,9 @@ auto *DTWP = getAnalysisIfAvailable(); auto *DT = DTWP ? &DTWP->getDomTree() : nullptr; auto &PDT = getAnalysis().getPostDomTree(); - return AggressiveDeadCodeElimination(F, DT, PDT) - .performDeadCodeElimination(); + ADCEChanged Changed = + AggressiveDeadCodeElimination(F, DT, PDT).performDeadCodeElimination(); + return Changed.ChangedAnything; } void getAnalysisUsage(AnalysisUsage &AU) const override {