Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 1 | //===- 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 Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 12 | // that checks for each function to see if constants have been added to the |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 13 | // constant pool since it was last run... if so, it processes them. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | b0cec70 | 2002-07-23 19:57:40 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 20 | #include "Support/StatisticReporter.h" |
| 21 | |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | struct ConstantMerge : public Pass { |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 24 | // run - For this pass, process all of the globals in the module, |
| 25 | // eliminating duplicate constants. |
| 26 | // |
| 27 | bool run(Module &M); |
| 28 | |
| 29 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 30 | AU.preservesCFG(); |
| 31 | } |
| 32 | }; |
Chris Lattner | 6788f25 | 2002-07-23 18:06:30 +0000 | [diff] [blame] | 33 | |
Chris Lattner | a2c0985 | 2002-07-26 21:12:44 +0000 | [diff] [blame] | 34 | Statistic<> NumMerged("constmerge\t\t- Number of global constants merged"); |
| 35 | RegisterOpt<ConstantMerge> X("constmerge","Merge Duplicate Global Constants"); |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | Pass *createConstantMergePass() { return new ConstantMerge(); } |
| 39 | |
| 40 | |
| 41 | // ConstantMerge::run - Workhorse for the pass. This eliminates duplicate |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 42 | // constants, starting at global ConstantNo, and adds vars to the map if they |
| 43 | // are new and unique. |
| 44 | // |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 45 | bool ConstantMerge::run(Module &M) { |
| 46 | std::map<Constant*, GlobalVariable*> CMap; |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 47 | bool MadeChanges = false; |
| 48 | |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 49 | for (Module::giterator GV = M.gbegin(), E = M.gend(); GV != E; ++GV) |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 50 | if (GV->isConstant()) { // Only process constants |
| 51 | assert(GV->hasInitializer() && "Globals constants must have inits!"); |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 52 | Constant *Init = GV->getInitializer(); |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 53 | |
| 54 | // Check to see if the initializer is already known... |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 55 | std::map<Constant*, GlobalVariable*>::iterator I = CMap.find(Init); |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 56 | |
| 57 | if (I == CMap.end()) { // Nope, add it to the map |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 58 | CMap.insert(I, std::make_pair(Init, GV)); |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 59 | } else { // Yup, this is a duplicate! |
| 60 | // Make all uses of the duplicate constant use the cannonical version... |
| 61 | GV->replaceAllUsesWith(I->second); |
| 62 | |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 63 | // Delete the global value from the module... and back up iterator to |
| 64 | // not skip the next global... |
| 65 | GV = --M.getGlobalList().erase(GV); |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 67 | ++NumMerged; |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 68 | MadeChanges = true; |
| 69 | } |
| 70 | } |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 72 | return MadeChanges; |
| 73 | } |