[llvm] r277565 - [ADCE] Refactor anticipating new functionality (NFC)

David Callahan via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 2 21:28:39 PDT 2016


Author: david2050
Date: Tue Aug  2 23:28:39 2016
New Revision: 277565

URL: http://llvm.org/viewvc/llvm-project?rev=277565&view=rev
Log:
[ADCE] Refactor anticipating new functionality (NFC)

Summary:
This is the first refactoring before adding new functionality.
Add a class wrapper for the functions and container for
state associated with the transformation.

No functional change

Reviewers: majnemer, nadav, mehdi_amini

Subscribers: llvm-commits

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

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

Modified: llvm/trunk/lib/Transforms/Scalar/ADCE.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/ADCE.cpp?rev=277565&r1=277564&r2=277565&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/ADCE.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/ADCE.cpp Tue Aug  2 23:28:39 2016
@@ -15,6 +15,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Transforms/Scalar/ADCE.h"
+
 #include "llvm/ADT/DepthFirstIterator.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
@@ -35,8 +36,28 @@ using namespace llvm;
 
 STATISTIC(NumRemoved, "Number of instructions removed");
 
-static void collectLiveScopes(const DILocalScope &LS,
-                              SmallPtrSetImpl<const Metadata *> &AliveScopes) {
+namespace {
+class AgggressiveDeadCodeElimination {
+  Function &F;
+  // Instructions known to be live
+  SmallPtrSet<Instruction *, 32> Alive;
+  // Instructions known to be live where we need to mark
+  // reaching definitions as live
+  SmallVector<Instruction *, 128> Worklist;
+  // Debug info scopes around a live instruction
+  SmallPtrSet<const Metadata *, 32> AliveScopes;
+  
+  void collectLiveScopes(const DILocalScope &LS);
+  void collectLiveScopes(const DILocation &DL);
+  bool isInstrumentsConstant(Instruction &I);
+public:
+  AgggressiveDeadCodeElimination(Function &F) : F(F) {}
+  bool aggressiveDCE();
+};
+}
+
+void AgggressiveDeadCodeElimination::collectLiveScopes(
+    const DILocalScope &LS) {
   if (!AliveScopes.insert(&LS).second)
     return;
 
@@ -44,27 +65,26 @@ static void collectLiveScopes(const DILo
     return;
 
   // Tail-recurse through the scope chain.
-  collectLiveScopes(cast<DILocalScope>(*LS.getScope()), AliveScopes);
+  collectLiveScopes(cast<DILocalScope>(*LS.getScope()));
 }
 
-static void collectLiveScopes(const DILocation &DL,
-                              SmallPtrSetImpl<const Metadata *> &AliveScopes) {
+void AgggressiveDeadCodeElimination::collectLiveScopes(const DILocation &DL) {
   // Even though DILocations are not scopes, shove them into AliveScopes so we
   // don't revisit them.
   if (!AliveScopes.insert(&DL).second)
     return;
 
   // Collect live scopes from the scope chain.
-  collectLiveScopes(*DL.getScope(), AliveScopes);
+  collectLiveScopes(*DL.getScope());
 
   // Tail-recurse through the inlined-at chain.
   if (const DILocation *IA = DL.getInlinedAt())
-    collectLiveScopes(*IA, AliveScopes);
+    collectLiveScopes(*IA);
 }
 
 // Check if this instruction is a runtime call for value profiling and
 // if it's instrumenting a constant.
-static bool isInstrumentsConstant(Instruction &I) {
+bool AgggressiveDeadCodeElimination::isInstrumentsConstant(Instruction &I) {
   if (CallInst *CI = dyn_cast<CallInst>(&I))
     if (Function *Callee = CI->getCalledFunction())
       if (Callee->getName().equals(getInstrProfValueProfFuncName()))
@@ -73,9 +93,7 @@ static bool isInstrumentsConstant(Instru
   return false;
 }
 
-static bool aggressiveDCE(Function& F) {
-  SmallPtrSet<Instruction*, 32> Alive;
-  SmallVector<Instruction*, 128> Worklist;
+bool AgggressiveDeadCodeElimination::aggressiveDCE() {
 
   // Collect the set of "root" instructions that are known live.
   for (Instruction &I : instructions(F)) {
@@ -91,13 +109,12 @@ static bool aggressiveDCE(Function& F) {
 
   // Propagate liveness backwards to operands.  Keep track of live debug info
   // scopes.
-  SmallPtrSet<const Metadata *, 32> AliveScopes;
   while (!Worklist.empty()) {
     Instruction *Curr = Worklist.pop_back_val();
 
     // Collect the live debug info scopes attached to this instruction.
     if (const DILocation *DL = Curr->getDebugLoc())
-      collectLiveScopes(*DL, AliveScopes);
+      collectLiveScopes(*DL);
 
     for (Use &OI : Curr->operands()) {
       if (Instruction *Inst = dyn_cast<Instruction>(OI))
@@ -146,7 +163,7 @@ static bool aggressiveDCE(Function& F) {
 }
 
 PreservedAnalyses ADCEPass::run(Function &F, FunctionAnalysisManager &) {
-  if (!aggressiveDCE(F))
+  if (!AgggressiveDeadCodeElimination(F).aggressiveDCE())
     return PreservedAnalyses::all();
 
   // FIXME: This should also 'preserve the CFG'.
@@ -162,13 +179,13 @@ struct ADCELegacyPass : public FunctionP
     initializeADCELegacyPassPass(*PassRegistry::getPassRegistry());
   }
 
-  bool runOnFunction(Function& F) override {
+  bool runOnFunction(Function &F) override {
     if (skipFunction(F))
       return false;
-    return aggressiveDCE(F);
+    return AgggressiveDeadCodeElimination(F).aggressiveDCE();
   }
 
-  void getAnalysisUsage(AnalysisUsage& AU) const override {
+  void getAnalysisUsage(AnalysisUsage &AU) const override {
     AU.setPreservesCFG();
     AU.addPreserved<GlobalsAAWrapperPass>();
   }




More information about the llvm-commits mailing list