// 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.
//
//===----------------------------------------------------------------------===//
#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
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(); }