Chris Lattner | 156bcf3 | 2003-04-18 04:34:29 +0000 | [diff] [blame] | 1 | //===- ConstantMerge.cpp - Merge duplicate global constants ---------------===// |
Chris Lattner | 475becb | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 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 |
Chris Lattner | e4314ed | 2002-09-23 23:00:46 +0000 | [diff] [blame] | 6 | // the program, regardless of whether or not an existing string is available. |
Chris Lattner | 475becb | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 7 | // |
| 8 | // Algorithm: ConstantMerge is designed to build up a map of available constants |
Chris Lattner | e4314ed | 2002-09-23 23:00:46 +0000 | [diff] [blame] | 9 | // and eliminate duplicates when it is initialized. |
Chris Lattner | 475becb | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Chris Lattner | 9cfea85 | 2002-07-23 19:57:40 +0000 | [diff] [blame] | 13 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 793c6b8 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 14 | #include "llvm/Module.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 15 | #include "llvm/Pass.h" |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 16 | #include "Support/Statistic.h" |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 9234d03 | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 18 | namespace { |
Chris Lattner | c67ebfa | 2002-10-09 23:16:04 +0000 | [diff] [blame] | 19 | Statistic<> NumMerged("constmerge", "Number of global constants merged"); |
| 20 | |
Chris Lattner | 9234d03 | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 21 | struct ConstantMerge : public Pass { |
Chris Lattner | 9234d03 | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 22 | // run - For this pass, process all of the globals in the module, |
| 23 | // eliminating duplicate constants. |
| 24 | // |
| 25 | bool run(Module &M); |
Chris Lattner | 9234d03 | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 26 | }; |
Chris Lattner | af41a12 | 2002-07-23 18:06:30 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 1e43516 | 2002-07-26 21:12:44 +0000 | [diff] [blame] | 28 | RegisterOpt<ConstantMerge> X("constmerge","Merge Duplicate Global Constants"); |
Chris Lattner | 9234d03 | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | Pass *createConstantMergePass() { return new ConstantMerge(); } |
| 32 | |
| 33 | |
Chris Lattner | 9234d03 | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 34 | bool ConstantMerge::run(Module &M) { |
| 35 | std::map<Constant*, GlobalVariable*> CMap; |
Chris Lattner | 475becb | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 36 | bool MadeChanges = false; |
| 37 | |
Chris Lattner | 9234d03 | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 38 | for (Module::giterator GV = M.gbegin(), E = M.gend(); GV != E; ++GV) |
Chris Lattner | 156bcf3 | 2003-04-18 04:34:29 +0000 | [diff] [blame] | 39 | // Only process constants with initializers |
| 40 | if (GV->isConstant() && GV->hasInitializer()) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 41 | Constant *Init = GV->getInitializer(); |
Chris Lattner | 475becb | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 42 | |
| 43 | // Check to see if the initializer is already known... |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 44 | std::map<Constant*, GlobalVariable*>::iterator I = CMap.find(Init); |
Chris Lattner | 475becb | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 45 | |
| 46 | if (I == CMap.end()) { // Nope, add it to the map |
Chris Lattner | 9234d03 | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 47 | CMap.insert(I, std::make_pair(Init, GV)); |
Chris Lattner | 475becb | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 48 | } else { // Yup, this is a duplicate! |
Chris Lattner | 065a616 | 2003-09-10 05:29:43 +0000 | [diff] [blame^] | 49 | // Make all uses of the duplicate constant use the canonical version... |
Chris Lattner | c67ebfa | 2002-10-09 23:16:04 +0000 | [diff] [blame] | 50 | GV->replaceAllUsesWith(I->second); |
Chris Lattner | 475becb | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 9234d03 | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 52 | // Delete the global value from the module... and back up iterator to |
| 53 | // not skip the next global... |
| 54 | GV = --M.getGlobalList().erase(GV); |
Chris Lattner | 475becb | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 56 | ++NumMerged; |
Chris Lattner | 475becb | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 57 | MadeChanges = true; |
| 58 | } |
| 59 | } |
Chris Lattner | 9234d03 | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 475becb | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 61 | return MadeChanges; |
| 62 | } |