blob: 11191f535ab6e4e41bcfda86aeecdfd28043c332 [file] [log] [blame]
Chris Lattner475becb2001-10-18 20:05:37 +00001//===- ConstantMerge.cpp - Merge duplicate global constants -----------------=//
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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000012// that checks for each function to see if constants have been added to the
Chris Lattner475becb2001-10-18 20:05:37 +000013// constant pool since it was last run... if so, it processes them.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/Transforms/ConstantMerge.h"
18#include "llvm/GlobalVariable.h"
Chris Lattner793c6b82002-01-31 00:45:11 +000019#include "llvm/Module.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000020#include "llvm/Function.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000021#include "llvm/Pass.h"
Chris Lattner3dec1f22002-05-10 15:38:35 +000022#include "Support/StatisticReporter.h"
23
24static Statistic<> NumMerged("constmerge\t\t- Number of global constants merged");
Chris Lattner475becb2001-10-18 20:05:37 +000025
26// mergeDuplicateConstants - Workhorse for the pass. This eliminates duplicate
27// constants, starting at global ConstantNo, and adds vars to the map if they
28// are new and unique.
29//
30static inline
31bool mergeDuplicateConstants(Module *M, unsigned &ConstantNo,
Chris Lattner697954c2002-01-20 22:54:45 +000032 std::map<Constant*, GlobalVariable*> &CMap) {
Chris Lattner475becb2001-10-18 20:05:37 +000033 Module::GlobalListType &GList = M->getGlobalList();
34 if (GList.size() <= ConstantNo) return false; // No new constants
35 bool MadeChanges = false;
36
37 for (; ConstantNo < GList.size(); ++ConstantNo) {
38 GlobalVariable *GV = GList[ConstantNo];
39 if (GV->isConstant()) { // Only process constants
40 assert(GV->hasInitializer() && "Globals constants must have inits!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +000041 Constant *Init = GV->getInitializer();
Chris Lattner475becb2001-10-18 20:05:37 +000042
43 // Check to see if the initializer is already known...
Chris Lattner697954c2002-01-20 22:54:45 +000044 std::map<Constant*, GlobalVariable*>::iterator I = CMap.find(Init);
Chris Lattner475becb2001-10-18 20:05:37 +000045
46 if (I == CMap.end()) { // Nope, add it to the map
Chris Lattner697954c2002-01-20 22:54:45 +000047 CMap.insert(std::make_pair(Init, GV));
Chris Lattner475becb2001-10-18 20:05:37 +000048 } else { // Yup, this is a duplicate!
49 // Make all uses of the duplicate constant use the cannonical version...
50 GV->replaceAllUsesWith(I->second);
51
52 // Remove and delete the global value from the module...
53 delete GList.remove(GList.begin()+ConstantNo);
54
55 --ConstantNo; // Don't skip the next constant.
Chris Lattner3dec1f22002-05-10 15:38:35 +000056 ++NumMerged;
Chris Lattner475becb2001-10-18 20:05:37 +000057 MadeChanges = true;
58 }
59 }
60 }
61 return MadeChanges;
62}
63
Chris Lattnerbd0ef772002-02-26 21:46:54 +000064namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000065 // FIXME: ConstantMerge should not be a FunctionPass!!!
66 class ConstantMerge : public FunctionPass {
Chris Lattnerbd0ef772002-02-26 21:46:54 +000067 protected:
68 std::map<Constant*, GlobalVariable*> Constants;
69 unsigned LastConstantSeen;
70 public:
71 inline ConstantMerge() : LastConstantSeen(0) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000072
73 const char *getPassName() const {return "Merge Duplicate Global Constants";}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000074
75 // doInitialization - For this pass, process all of the globals in the
76 // module, eliminating duplicate constants.
77 //
78 bool doInitialization(Module *M) {
79 return ::mergeDuplicateConstants(M, LastConstantSeen, Constants);
80 }
81
Chris Lattnerf57b8452002-04-27 06:56:12 +000082 bool runOnFunction(Function *) { return false; }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000083
84 // doFinalization - Clean up internal state for this module
85 //
86 bool doFinalization(Module *M) {
87 LastConstantSeen = 0;
88 Constants.clear();
89 return false;
90 }
Chris Lattner97e52e42002-04-28 21:27:06 +000091
92 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
93 AU.setPreservesAll();
94 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000095 };
96
97 struct DynamicConstantMerge : public ConstantMerge {
Chris Lattner96c466b2002-04-29 14:57:45 +000098 const char *getPassName() const { return "Dynamic Constant Merge"; }
99
Chris Lattnerf57b8452002-04-27 06:56:12 +0000100 // runOnFunction - Check to see if any globals have been added to the
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000101 // global list for the module. If so, eliminate them.
102 //
Chris Lattnerf57b8452002-04-27 06:56:12 +0000103 bool runOnFunction(Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000104 return ::mergeDuplicateConstants(F->getParent(), LastConstantSeen,
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000105 Constants);
106 }
107 };
Chris Lattner475becb2001-10-18 20:05:37 +0000108}
109
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000110Pass *createConstantMergePass() { return new ConstantMerge(); }
111Pass *createDynamicConstantMergePass() { return new DynamicConstantMerge(); }