blob: 146911e818ddb217892a23773b9b94a96b1e1df1 [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 Lattner475becb2001-10-18 20:05:37 +000022
23// mergeDuplicateConstants - Workhorse for the pass. This eliminates duplicate
24// constants, starting at global ConstantNo, and adds vars to the map if they
25// are new and unique.
26//
27static inline
28bool mergeDuplicateConstants(Module *M, unsigned &ConstantNo,
Chris Lattner697954c2002-01-20 22:54:45 +000029 std::map<Constant*, GlobalVariable*> &CMap) {
Chris Lattner475becb2001-10-18 20:05:37 +000030 Module::GlobalListType &GList = M->getGlobalList();
31 if (GList.size() <= ConstantNo) return false; // No new constants
32 bool MadeChanges = false;
33
34 for (; ConstantNo < GList.size(); ++ConstantNo) {
35 GlobalVariable *GV = GList[ConstantNo];
36 if (GV->isConstant()) { // Only process constants
37 assert(GV->hasInitializer() && "Globals constants must have inits!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +000038 Constant *Init = GV->getInitializer();
Chris Lattner475becb2001-10-18 20:05:37 +000039
40 // Check to see if the initializer is already known...
Chris Lattner697954c2002-01-20 22:54:45 +000041 std::map<Constant*, GlobalVariable*>::iterator I = CMap.find(Init);
Chris Lattner475becb2001-10-18 20:05:37 +000042
43 if (I == CMap.end()) { // Nope, add it to the map
Chris Lattner697954c2002-01-20 22:54:45 +000044 CMap.insert(std::make_pair(Init, GV));
Chris Lattner475becb2001-10-18 20:05:37 +000045 } else { // Yup, this is a duplicate!
46 // Make all uses of the duplicate constant use the cannonical version...
47 GV->replaceAllUsesWith(I->second);
48
49 // Remove and delete the global value from the module...
50 delete GList.remove(GList.begin()+ConstantNo);
51
52 --ConstantNo; // Don't skip the next constant.
53 MadeChanges = true;
54 }
55 }
56 }
57 return MadeChanges;
58}
59
Chris Lattnerbd0ef772002-02-26 21:46:54 +000060namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000061 // FIXME: ConstantMerge should not be a FunctionPass!!!
62 class ConstantMerge : public FunctionPass {
Chris Lattnerbd0ef772002-02-26 21:46:54 +000063 protected:
64 std::map<Constant*, GlobalVariable*> Constants;
65 unsigned LastConstantSeen;
66 public:
67 inline ConstantMerge() : LastConstantSeen(0) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000068
69 const char *getPassName() const {return "Merge Duplicate Global Constants";}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000070
71 // doInitialization - For this pass, process all of the globals in the
72 // module, eliminating duplicate constants.
73 //
74 bool doInitialization(Module *M) {
75 return ::mergeDuplicateConstants(M, LastConstantSeen, Constants);
76 }
77
Chris Lattnerf57b8452002-04-27 06:56:12 +000078 bool runOnFunction(Function *) { return false; }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000079
80 // doFinalization - Clean up internal state for this module
81 //
82 bool doFinalization(Module *M) {
83 LastConstantSeen = 0;
84 Constants.clear();
85 return false;
86 }
Chris Lattner97e52e42002-04-28 21:27:06 +000087
88 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
89 AU.setPreservesAll();
90 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000091 };
92
93 struct DynamicConstantMerge : public ConstantMerge {
Chris Lattner96c466b2002-04-29 14:57:45 +000094 const char *getPassName() const { return "Dynamic Constant Merge"; }
95
Chris Lattnerf57b8452002-04-27 06:56:12 +000096 // runOnFunction - Check to see if any globals have been added to the
Chris Lattnerbd0ef772002-02-26 21:46:54 +000097 // global list for the module. If so, eliminate them.
98 //
Chris Lattnerf57b8452002-04-27 06:56:12 +000099 bool runOnFunction(Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000100 return ::mergeDuplicateConstants(F->getParent(), LastConstantSeen,
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000101 Constants);
102 }
103 };
Chris Lattner475becb2001-10-18 20:05:37 +0000104}
105
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000106Pass *createConstantMergePass() { return new ConstantMerge(); }
107Pass *createDynamicConstantMergePass() { return new DynamicConstantMerge(); }