Tighten up the AnalysisUsage of lots of passes, primarily to correctly indicate wheth...
[oota-llvm.git] / lib / Transforms / IPO / ConstantMerge.cpp
index 0951c731e1ce007974a4cd61b7f6d99f4486ba31..a635b8d21bed9982cbbc69fca5fe179a743b62c8 100644 (file)
@@ -9,7 +9,7 @@
 // and elminate duplicates when it is initialized.
 //
 // The DynamicConstantMerge method is a superset of the ConstantMerge algorithm
-// that checks for each method to see if constants have been added to the
+// that checks for each function to see if constants have been added to the
 // constant pool since it was last run... if so, it processes them.
 //
 //===----------------------------------------------------------------------===//
@@ -17,7 +17,8 @@
 #include "llvm/Transforms/ConstantMerge.h"
 #include "llvm/GlobalVariable.h"
 #include "llvm/Module.h"
-#include "llvm/Method.h"
+#include "llvm/Function.h"
+#include "llvm/Pass.h"
 
 // mergeDuplicateConstants - Workhorse for the pass.  This eliminates duplicate
 // constants, starting at global ConstantNo, and adds vars to the map if they
@@ -56,27 +57,47 @@ bool mergeDuplicateConstants(Module *M, unsigned &ConstantNo,
   return MadeChanges;
 }
 
+namespace {
+  // FIXME: ConstantMerge should not be a FunctionPass!!!
+  class ConstantMerge : public FunctionPass {
+  protected:
+    std::map<Constant*, GlobalVariable*> Constants;
+    unsigned LastConstantSeen;
+  public:
+    inline ConstantMerge() : LastConstantSeen(0) {}
+    
+    // doInitialization - For this pass, process all of the globals in the
+    // module, eliminating duplicate constants.
+    //
+    bool doInitialization(Module *M) {
+      return ::mergeDuplicateConstants(M, LastConstantSeen, Constants);
+    }
+    
+    bool runOnFunction(Function *) { return false; }
+    
+    // doFinalization - Clean up internal state for this module
+    //
+    bool doFinalization(Module *M) {
+      LastConstantSeen = 0;
+      Constants.clear();
+      return false;
+    }
 
-// mergeDuplicateConstants - Static accessor for clients that don't want to
-// deal with passes.
-//
-bool ConstantMerge::mergeDuplicateConstants(Module *M) {
-  std::map<Constant*, GlobalVariable*> Constants;
-  unsigned LastConstantSeen = 0;
-  return ::mergeDuplicateConstants(M, LastConstantSeen, Constants);
-}
-
-
-// doInitialization - For this pass, process all of the globals in the
-// module, eliminating duplicate constants.
-//
-bool ConstantMerge::doInitialization(Module *M) {
-  return ::mergeDuplicateConstants(M, LastConstantSeen, Constants);
+    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+      AU.setPreservesAll();
+    }
+  };
+  
+  struct DynamicConstantMerge : public ConstantMerge {
+    // runOnFunction - Check to see if any globals have been added to the 
+    // global list for the module.  If so, eliminate them.
+    //
+    bool runOnFunction(Function *F) {
+      return ::mergeDuplicateConstants(F->getParent(), LastConstantSeen,
+                                       Constants);
+    }
+  };
 }
 
-// doPerMethodWork - Check to see if any globals have been added to the 
-// global list for the module.  If so, eliminate them.
-//
-bool DynamicConstantMerge::runOnMethod(Method *M) {
-  return ::mergeDuplicateConstants(M->getParent(), LastConstantSeen, Constants);
-}
+Pass *createConstantMergePass() { return new ConstantMerge(); }
+Pass *createDynamicConstantMergePass() { return new DynamicConstantMerge(); }