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