Chris Lattner | 424132a | 2003-04-18 04:34:29 +0000 | [diff] [blame] | 1 | //===- ConstantMerge.cpp - Merge duplicate global constants ---------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | 75879be | 2010-02-12 18:17:23 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.h" |
Nick Lewycky | 8ac9ece | 2011-07-27 19:47:34 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/PointerIntPair.h" |
Chris Lattner | 67e5345 | 2010-09-15 00:30:11 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Constants.h" |
| 26 | #include "llvm/IR/DataLayout.h" |
| 27 | #include "llvm/IR/DerivedTypes.h" |
| 28 | #include "llvm/IR/Module.h" |
Rafael Espindola | c229a4f | 2013-05-06 01:48:55 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Operator.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/Pass.h" |
Chris Lattner | f52e03c | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 33 | #define DEBUG_TYPE "constmerge" |
| 34 | |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 35 | STATISTIC(NumMerged, "Number of global constants merged"); |
Chris Lattner | eac4dcd | 2002-10-09 23:16:04 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 37 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 38 | struct ConstantMerge : public ModulePass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 39 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 40 | ConstantMerge() : ModulePass(ID) { |
| 41 | initializeConstantMergePass(*PassRegistry::getPassRegistry()); |
| 42 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 43 | |
Nick Lewycky | 8ac9ece | 2011-07-27 19:47:34 +0000 | [diff] [blame] | 44 | // For this pass, process all of the globals in the module, eliminating |
| 45 | // duplicate constants. |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 46 | bool runOnModule(Module &M) override; |
Nick Lewycky | 8ac9ece | 2011-07-27 19:47:34 +0000 | [diff] [blame] | 47 | |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 48 | // Return true iff we can determine the alignment of this global variable. |
Nick Lewycky | 8ac9ece | 2011-07-27 19:47:34 +0000 | [diff] [blame] | 49 | bool hasKnownAlignment(GlobalVariable *GV) const; |
| 50 | |
| 51 | // Return the alignment of the global, including converting the default |
| 52 | // alignment to a concrete value. |
| 53 | unsigned getAlignment(GlobalVariable *GV) const; |
| 54 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 55 | const DataLayout *DL; |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 56 | }; |
| 57 | } |
| 58 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 59 | char ConstantMerge::ID = 0; |
Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 60 | INITIALIZE_PASS(ConstantMerge, "constmerge", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 61 | "Merge Duplicate Global Constants", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 63 | ModulePass *llvm::createConstantMergePass() { return new ConstantMerge(); } |
Chris Lattner | 2ae9cda | 2002-06-25 15:55:29 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 67e5345 | 2010-09-15 00:30:11 +0000 | [diff] [blame] | 65 | |
| 66 | |
| 67 | /// Find values that are marked as llvm.used. |
| 68 | static void FindUsedValues(GlobalVariable *LLVMUsed, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 69 | SmallPtrSetImpl<const GlobalValue*> &UsedValues) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 70 | if (!LLVMUsed) return; |
Rafael Espindola | 74f2e46 | 2013-04-22 14:58:02 +0000 | [diff] [blame] | 71 | ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer()); |
| 72 | |
Rafael Espindola | c229a4f | 2013-05-06 01:48:55 +0000 | [diff] [blame] | 73 | for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) { |
| 74 | Value *Operand = Inits->getOperand(i)->stripPointerCastsNoFollowAliases(); |
| 75 | GlobalValue *GV = cast<GlobalValue>(Operand); |
| 76 | UsedValues.insert(GV); |
| 77 | } |
Chris Lattner | 67e5345 | 2010-09-15 00:30:11 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 80 | // True if A is better than B. |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 81 | static bool IsBetterCanonical(const GlobalVariable &A, |
| 82 | const GlobalVariable &B) { |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 83 | if (!A.hasLocalLinkage() && B.hasLocalLinkage()) |
| 84 | return true; |
| 85 | |
| 86 | if (A.hasLocalLinkage() && !B.hasLocalLinkage()) |
| 87 | return false; |
| 88 | |
| 89 | return A.hasUnnamedAddr(); |
| 90 | } |
| 91 | |
Nick Lewycky | 8ac9ece | 2011-07-27 19:47:34 +0000 | [diff] [blame] | 92 | bool ConstantMerge::hasKnownAlignment(GlobalVariable *GV) const { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 93 | return DL || GV->getAlignment() != 0; |
Nick Lewycky | 8ac9ece | 2011-07-27 19:47:34 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | unsigned ConstantMerge::getAlignment(GlobalVariable *GV) const { |
Rafael Espindola | dd8757a | 2013-11-12 20:21:43 +0000 | [diff] [blame] | 97 | unsigned Align = GV->getAlignment(); |
| 98 | if (Align) |
| 99 | return Align; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 100 | if (DL) |
| 101 | return DL->getPreferredAlignment(GV); |
Rafael Espindola | dd8757a | 2013-11-12 20:21:43 +0000 | [diff] [blame] | 102 | return 0; |
Nick Lewycky | 8ac9ece | 2011-07-27 19:47:34 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 105 | bool ConstantMerge::runOnModule(Module &M) { |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 106 | DL = &M.getDataLayout(); |
Nick Lewycky | 8ac9ece | 2011-07-27 19:47:34 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 67e5345 | 2010-09-15 00:30:11 +0000 | [diff] [blame] | 108 | // Find all the globals that are marked "used". These cannot be merged. |
| 109 | SmallPtrSet<const GlobalValue*, 8> UsedGlobals; |
| 110 | FindUsedValues(M.getGlobalVariable("llvm.used"), UsedGlobals); |
| 111 | FindUsedValues(M.getGlobalVariable("llvm.compiler.used"), UsedGlobals); |
| 112 | |
Nick Lewycky | 8ac9ece | 2011-07-27 19:47:34 +0000 | [diff] [blame] | 113 | // Map unique <constants, has-unknown-alignment> pairs to globals. We don't |
| 114 | // want to merge globals of unknown alignment with those of explicit |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 115 | // alignment. If we have DataLayout, we always know the alignment. |
Nick Lewycky | 8ac9ece | 2011-07-27 19:47:34 +0000 | [diff] [blame] | 116 | DenseMap<PointerIntPair<Constant*, 1, bool>, GlobalVariable*> CMap; |
Chris Lattner | c2ee054 | 2003-12-22 23:49:36 +0000 | [diff] [blame] | 117 | |
| 118 | // Replacements - This vector contains a list of replacements to perform. |
Chris Lattner | 75879be | 2010-02-12 18:17:23 +0000 | [diff] [blame] | 119 | SmallVector<std::pair<GlobalVariable*, GlobalVariable*>, 32> Replacements; |
Chris Lattner | c2ee054 | 2003-12-22 23:49:36 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 56db5e9 | 2003-12-28 07:19:08 +0000 | [diff] [blame] | 121 | bool MadeChange = false; |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 56db5e9 | 2003-12-28 07:19:08 +0000 | [diff] [blame] | 123 | // Iterate constant merging while we are still making progress. Merging two |
| 124 | // constants together may allow us to merge other constants together if the |
| 125 | // second level constants have initializers which point to the globals that |
| 126 | // were just merged. |
| 127 | while (1) { |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 128 | |
| 129 | // First: Find the canonical constants others will be merged with. |
Chris Lattner | 6f58839 | 2007-04-14 18:06:52 +0000 | [diff] [blame] | 130 | for (Module::global_iterator GVI = M.global_begin(), E = M.global_end(); |
| 131 | GVI != E; ) { |
| 132 | GlobalVariable *GV = GVI++; |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 02137ee | 2007-04-14 01:11:54 +0000 | [diff] [blame] | 134 | // If this GV is dead, remove it. |
| 135 | GV->removeDeadConstantUsers(); |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 136 | if (GV->use_empty() && GV->hasLocalLinkage()) { |
Jeff Cohen | 4bd0fd3 | 2007-04-14 17:18:29 +0000 | [diff] [blame] | 137 | GV->eraseFromParent(); |
| 138 | continue; |
Chris Lattner | 02137ee | 2007-04-14 01:11:54 +0000 | [diff] [blame] | 139 | } |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 140 | |
Nick Lewycky | 0296a48 | 2011-01-15 18:14:21 +0000 | [diff] [blame] | 141 | // Only process constants with initializers in the default address space. |
Nick Lewycky | 4a1ff16 | 2011-01-15 18:42:52 +0000 | [diff] [blame] | 142 | if (!GV->isConstant() || !GV->hasDefinitiveInitializer() || |
Nick Lewycky | 0296a48 | 2011-01-15 18:14:21 +0000 | [diff] [blame] | 143 | GV->getType()->getAddressSpace() != 0 || GV->hasSection() || |
Chris Lattner | 67e5345 | 2010-09-15 00:30:11 +0000 | [diff] [blame] | 144 | // Don't touch values marked with attribute(used). |
| 145 | UsedGlobals.count(GV)) |
Chris Lattner | 75879be | 2010-02-12 18:17:23 +0000 | [diff] [blame] | 146 | continue; |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 147 | |
Eli Friedman | b31c627 | 2012-01-11 22:06:46 +0000 | [diff] [blame] | 148 | // This transformation is legal for weak ODR globals in the sense it |
| 149 | // doesn't change semantics, but we really don't want to perform it |
| 150 | // anyway; it's likely to pessimize code generation, and some tools |
| 151 | // (like the Darwin linker in cases involving CFString) don't expect it. |
| 152 | if (GV->isWeakForLinker()) |
Bill Wendling | c791551 | 2012-01-11 00:13:08 +0000 | [diff] [blame] | 153 | continue; |
| 154 | |
Chris Lattner | 75879be | 2010-02-12 18:17:23 +0000 | [diff] [blame] | 155 | Constant *Init = GV->getInitializer(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 75879be | 2010-02-12 18:17:23 +0000 | [diff] [blame] | 157 | // Check to see if the initializer is already known. |
Nick Lewycky | 8ac9ece | 2011-07-27 19:47:34 +0000 | [diff] [blame] | 158 | PointerIntPair<Constant*, 1, bool> Pair(Init, hasKnownAlignment(GV)); |
| 159 | GlobalVariable *&Slot = CMap[Pair]; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 160 | |
Bill Wendling | c791551 | 2012-01-11 00:13:08 +0000 | [diff] [blame] | 161 | // If this is the first constant we find or if the old one is local, |
| 162 | // replace with the current one. If the current is externally visible |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 163 | // it cannot be replace, but can be the canonical constant we merge with. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 164 | if (!Slot || IsBetterCanonical(*GV, *Slot)) |
Chris Lattner | 75879be | 2010-02-12 18:17:23 +0000 | [diff] [blame] | 165 | Slot = GV; |
Nick Lewycky | 0296a48 | 2011-01-15 18:14:21 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 168 | // Second: identify all globals that can be merged together, filling in |
| 169 | // the Replacements vector. We cannot do the replacement in this pass |
| 170 | // because doing so may cause initializers of other globals to be rewritten, |
| 171 | // invalidating the Constant* pointers in CMap. |
Nick Lewycky | 0296a48 | 2011-01-15 18:14:21 +0000 | [diff] [blame] | 172 | for (Module::global_iterator GVI = M.global_begin(), E = M.global_end(); |
| 173 | GVI != E; ) { |
| 174 | GlobalVariable *GV = GVI++; |
| 175 | |
| 176 | // Only process constants with initializers in the default address space. |
Nick Lewycky | 4a1ff16 | 2011-01-15 18:42:52 +0000 | [diff] [blame] | 177 | if (!GV->isConstant() || !GV->hasDefinitiveInitializer() || |
Nick Lewycky | 0296a48 | 2011-01-15 18:14:21 +0000 | [diff] [blame] | 178 | GV->getType()->getAddressSpace() != 0 || GV->hasSection() || |
| 179 | // Don't touch values marked with attribute(used). |
| 180 | UsedGlobals.count(GV)) |
| 181 | continue; |
| 182 | |
Eli Friedman | b31c627 | 2012-01-11 22:06:46 +0000 | [diff] [blame] | 183 | // We can only replace constant with local linkage. |
| 184 | if (!GV->hasLocalLinkage()) |
Nick Lewycky | 0296a48 | 2011-01-15 18:14:21 +0000 | [diff] [blame] | 185 | continue; |
| 186 | |
| 187 | Constant *Init = GV->getInitializer(); |
| 188 | |
| 189 | // Check to see if the initializer is already known. |
Nick Lewycky | 8ac9ece | 2011-07-27 19:47:34 +0000 | [diff] [blame] | 190 | PointerIntPair<Constant*, 1, bool> Pair(Init, hasKnownAlignment(GV)); |
| 191 | GlobalVariable *Slot = CMap[Pair]; |
Nick Lewycky | 0296a48 | 2011-01-15 18:14:21 +0000 | [diff] [blame] | 192 | |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 193 | if (!Slot || Slot == GV) |
| 194 | continue; |
| 195 | |
| 196 | if (!Slot->hasUnnamedAddr() && !GV->hasUnnamedAddr()) |
| 197 | continue; |
| 198 | |
| 199 | if (!GV->hasUnnamedAddr()) |
| 200 | Slot->setUnnamedAddr(false); |
| 201 | |
| 202 | // Make all uses of the duplicate constant use the canonical version. |
| 203 | Replacements.push_back(std::make_pair(GV, Slot)); |
Chris Lattner | 02137ee | 2007-04-14 01:11:54 +0000 | [diff] [blame] | 204 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 56db5e9 | 2003-12-28 07:19:08 +0000 | [diff] [blame] | 206 | if (Replacements.empty()) |
| 207 | return MadeChange; |
| 208 | CMap.clear(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 56db5e9 | 2003-12-28 07:19:08 +0000 | [diff] [blame] | 210 | // Now that we have figured out which replacements must be made, do them all |
| 211 | // now. This avoid invalidating the pointers in CMap, which are unneeded |
| 212 | // now. |
| 213 | for (unsigned i = 0, e = Replacements.size(); i != e; ++i) { |
Nick Lewycky | 8ac9ece | 2011-07-27 19:47:34 +0000 | [diff] [blame] | 214 | // Bump the alignment if necessary. |
| 215 | if (Replacements[i].first->getAlignment() || |
| 216 | Replacements[i].second->getAlignment()) { |
Rafael Espindola | dd8757a | 2013-11-12 20:21:43 +0000 | [diff] [blame] | 217 | Replacements[i].second->setAlignment( |
| 218 | std::max(getAlignment(Replacements[i].first), |
| 219 | getAlignment(Replacements[i].second))); |
Nick Lewycky | 8ac9ece | 2011-07-27 19:47:34 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Chris Lattner | 75879be | 2010-02-12 18:17:23 +0000 | [diff] [blame] | 222 | // Eliminate any uses of the dead global. |
Chris Lattner | 56db5e9 | 2003-12-28 07:19:08 +0000 | [diff] [blame] | 223 | Replacements[i].first->replaceAllUsesWith(Replacements[i].second); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 75879be | 2010-02-12 18:17:23 +0000 | [diff] [blame] | 225 | // Delete the global value from the module. |
Nick Lewycky | 0296a48 | 2011-01-15 18:14:21 +0000 | [diff] [blame] | 226 | assert(Replacements[i].first->hasLocalLinkage() && |
| 227 | "Refusing to delete an externally visible global variable."); |
Chris Lattner | 75879be | 2010-02-12 18:17:23 +0000 | [diff] [blame] | 228 | Replacements[i].first->eraseFromParent(); |
Chris Lattner | 56db5e9 | 2003-12-28 07:19:08 +0000 | [diff] [blame] | 229 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 230 | |
Chris Lattner | 56db5e9 | 2003-12-28 07:19:08 +0000 | [diff] [blame] | 231 | NumMerged += Replacements.size(); |
| 232 | Replacements.clear(); |
Chris Lattner | c2ee054 | 2003-12-22 23:49:36 +0000 | [diff] [blame] | 233 | } |
Chris Lattner | c2ee054 | 2003-12-22 23:49:36 +0000 | [diff] [blame] | 234 | } |