Chris Lattner | 424132a | 2003-04-18 04:34:29 +0000 | [diff] [blame] | 1 | //===- ConstantMerge.cpp - Merge duplicate global constants ---------------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines the interface to a pass that merges duplicate global |
| 11 | // constants together into a single constant that is shared. This is useful |
| 12 | // because some passes (ie TraceValues) insert a lot of string constants into |
Chris Lattner | 28d1035 | 2002-09-23 23:00:46 +0000 | [diff] [blame] | 13 | // the program, regardless of whether or not an existing string is available. |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 14 | // |
| 15 | // Algorithm: ConstantMerge is designed to build up a map of available constants |
Chris Lattner | 28d1035 | 2002-09-23 23:00:46 +0000 | [diff] [blame] | 16 | // and eliminate duplicates when it is initialized. |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Chris Lattner | b0cec70 | 2002-07-23 19:57:40 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 21 | #include "llvm/Module.h" |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 22 | #include "llvm/Pass.h" |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 23 | #include "Support/Statistic.h" |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 24 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | namespace llvm { |
| 26 | |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 27 | namespace { |
Chris Lattner | eac4dcd | 2002-10-09 23:16:04 +0000 | [diff] [blame] | 28 | Statistic<> NumMerged("constmerge", "Number of global constants merged"); |
| 29 | |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 30 | struct ConstantMerge : public Pass { |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 31 | // run - For this pass, process all of the globals in the module, |
| 32 | // eliminating duplicate constants. |
| 33 | // |
| 34 | bool run(Module &M); |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 35 | }; |
Chris Lattner | 6788f25 | 2002-07-23 18:06:30 +0000 | [diff] [blame] | 36 | |
Chris Lattner | a2c0985 | 2002-07-26 21:12:44 +0000 | [diff] [blame] | 37 | RegisterOpt<ConstantMerge> X("constmerge","Merge Duplicate Global Constants"); |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | Pass *createConstantMergePass() { return new ConstantMerge(); } |
| 41 | |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 42 | bool ConstantMerge::run(Module &M) { |
| 43 | std::map<Constant*, GlobalVariable*> CMap; |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 44 | bool MadeChanges = false; |
| 45 | |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 46 | for (Module::giterator GV = M.gbegin(), E = M.gend(); GV != E; ++GV) |
Chris Lattner | 424132a | 2003-04-18 04:34:29 +0000 | [diff] [blame] | 47 | // Only process constants with initializers |
| 48 | if (GV->isConstant() && GV->hasInitializer()) { |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 49 | Constant *Init = GV->getInitializer(); |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 50 | |
| 51 | // Check to see if the initializer is already known... |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 52 | std::map<Constant*, GlobalVariable*>::iterator I = CMap.find(Init); |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 53 | |
| 54 | if (I == CMap.end()) { // Nope, add it to the map |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 55 | CMap.insert(I, std::make_pair(Init, GV)); |
Chris Lattner | e5f15cd | 2003-10-29 06:01:26 +0000 | [diff] [blame] | 56 | } else if (GV->hasInternalLinkage()) { // Yup, this is a duplicate! |
Chris Lattner | 216c7b8 | 2003-09-10 05:29:43 +0000 | [diff] [blame] | 57 | // Make all uses of the duplicate constant use the canonical version... |
Chris Lattner | eac4dcd | 2002-10-09 23:16:04 +0000 | [diff] [blame] | 58 | GV->replaceAllUsesWith(I->second); |
Chris Lattner | e5f15cd | 2003-10-29 06:01:26 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 60 | // Delete the global value from the module... and back up iterator to |
| 61 | // not skip the next global... |
| 62 | GV = --M.getGlobalList().erase(GV); |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 64 | ++NumMerged; |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 65 | MadeChanges = true; |
Chris Lattner | e5f15cd | 2003-10-29 06:01:26 +0000 | [diff] [blame] | 66 | } else if (I->second->hasInternalLinkage()) { |
| 67 | // Make all uses of the duplicate constant use the canonical version... |
| 68 | I->second->replaceAllUsesWith(GV); |
| 69 | |
| 70 | // Delete the global value from the module... and back up iterator to |
| 71 | // not skip the next global... |
| 72 | M.getGlobalList().erase(I->second); |
| 73 | I->second = GV; |
| 74 | |
| 75 | ++NumMerged; |
| 76 | MadeChanges = true; |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 80 | return MadeChanges; |
| 81 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 82 | |
| 83 | } // End llvm namespace |