Change over to use new style pass mechanism, now passes only expose small
[oota-llvm.git] / lib / Transforms / IPO / ConstantMerge.cpp
index 0951c731e1ce007974a4cd61b7f6d99f4486ba31..d78d185e8afdf23b45b26cf1ba56ad1f9940c853 100644 (file)
@@ -18,6 +18,7 @@
 #include "llvm/GlobalVariable.h"
 #include "llvm/Module.h"
 #include "llvm/Method.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,43 @@ bool mergeDuplicateConstants(Module *M, unsigned &ConstantNo,
   return MadeChanges;
 }
 
-
-// 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);
+namespace {
+  // FIXME: ConstantMerge should not be a methodPass!!!
+  class ConstantMerge : public MethodPass {
+  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 runOnMethod(Method*) { return false; }
+    
+    // doFinalization - Clean up internal state for this module
+    //
+    bool doFinalization(Module *M) {
+      LastConstantSeen = 0;
+      Constants.clear();
+      return false;
+    }
+  };
+  
+  struct DynamicConstantMerge : public ConstantMerge {
+    // doPerMethodWork - Check to see if any globals have been added to the 
+    // global list for the module.  If so, eliminate them.
+    //
+    bool runOnMethod(Method *M) {
+      return ::mergeDuplicateConstants(M->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(); }