efc91f400b309da2e8e0a8de3deef398fb75fb08
[oota-llvm.git] / include / llvm / Transforms / IPO / ConstantMerge.h
1 //===- llvm/Transforms/ConstantMerge.h - Merge duplicate consts --*- C++ -*--=//
2 //
3 // This file defines the interface to a pass that merges duplicate global
4 // constants together into a single constant that is shared.  This is useful
5 // because some passes (ie TraceValues) insert a lot of string constants into
6 // the program, regardless of whether or not they duplicate an existing string.
7 //
8 // Algorithm: ConstantMerge is designed to build up a map of available constants
9 // and elminate duplicates when it is initialized.
10 //
11 // The DynamicConstantMerge method is a superset of the ConstantMerge algorithm
12 // that checks for each method to see if constants have been added to the
13 // constant pool since it was last run... if so, it processes them.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_TRANSFORMS_CONSTANTMERGE_H
18 #define LLVM_TRANSFORMS_CONSTANTMERGE_H
19
20 #include "llvm/Pass.h"
21 class Constant;
22 class GlobalVariable;
23
24 // FIXME: ConstantMerge should not be a methodPass!!!
25 class ConstantMerge : public MethodPass {
26 protected:
27   std::map<Constant*, GlobalVariable*> Constants;
28   unsigned LastConstantSeen;
29 public:
30   inline ConstantMerge() : LastConstantSeen(0) {}
31
32   // mergeDuplicateConstants - Static accessor for clients that don't want to
33   // deal with passes.
34   //
35   static bool mergeDuplicateConstants(Module *M);
36
37   // doInitialization - For this pass, process all of the globals in the
38   // module, eliminating duplicate constants.
39   //
40   bool doInitialization(Module *M);
41
42   bool runOnMethod(Method*) { return false; }
43
44   // doFinalization - Clean up internal state for this module
45   //
46   bool doFinalization(Module *M) {
47     LastConstantSeen = 0;
48     Constants.clear();
49     return false;
50   }
51 };
52
53 struct DynamicConstantMerge : public ConstantMerge {
54   // doPerMethodWork - Check to see if any globals have been added to the 
55   // global list for the module.  If so, eliminate them.
56   //
57   bool runOnMethod(Method *M);
58 };
59
60 #endif