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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 8 | // |
| 9 | // This file defines the interface to a pass that merges duplicate global |
| 10 | // constants together into a single constant that is shared. This is useful |
| 11 | // because some passes (ie TraceValues) insert a lot of string constants into |
Chris Lattner | 28d1035 | 2002-09-23 23:00:46 +0000 | [diff] [blame] | 12 | // the program, regardless of whether or not an existing string is available. |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 13 | // |
| 14 | // Algorithm: ConstantMerge is designed to build up a map of available constants |
Chris Lattner | 28d1035 | 2002-09-23 23:00:46 +0000 | [diff] [blame] | 15 | // and eliminate duplicates when it is initialized. |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Davide Italiano | 164b9bc | 2016-05-05 00:51:09 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/IPO/ConstantMerge.h" |
Chris Lattner | 75879be | 2010-02-12 18:17:23 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 67e5345 | 2010-09-15 00:30:11 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
| 25 | #include "llvm/IR/DataLayout.h" |
| 26 | #include "llvm/IR/DerivedTypes.h" |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 27 | #include "llvm/IR/GlobalValue.h" |
| 28 | #include "llvm/IR/GlobalVariable.h" |
| 29 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Module.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/Pass.h" |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Casting.h" |
Davide Italiano | 164b9bc | 2016-05-05 00:51:09 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/IPO.h" |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 34 | #include <algorithm> |
| 35 | #include <cassert> |
| 36 | #include <utility> |
| 37 | |
Chris Lattner | f52e03c | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 38 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 39 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 40 | #define DEBUG_TYPE "constmerge" |
| 41 | |
JF Bastien | fe258d9 | 2018-08-10 22:41:09 +0000 | [diff] [blame] | 42 | STATISTIC(NumIdenticalMerged, "Number of identical global constants merged"); |
Chris Lattner | eac4dcd | 2002-10-09 23:16:04 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 67e5345 | 2010-09-15 00:30:11 +0000 | [diff] [blame] | 44 | /// Find values that are marked as llvm.used. |
| 45 | static void FindUsedValues(GlobalVariable *LLVMUsed, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 46 | SmallPtrSetImpl<const GlobalValue*> &UsedValues) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 47 | if (!LLVMUsed) return; |
Rafael Espindola | 74f2e46 | 2013-04-22 14:58:02 +0000 | [diff] [blame] | 48 | ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer()); |
| 49 | |
Rafael Espindola | c229a4f | 2013-05-06 01:48:55 +0000 | [diff] [blame] | 50 | for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) { |
| 51 | Value *Operand = Inits->getOperand(i)->stripPointerCastsNoFollowAliases(); |
| 52 | GlobalValue *GV = cast<GlobalValue>(Operand); |
| 53 | UsedValues.insert(GV); |
| 54 | } |
Chris Lattner | 67e5345 | 2010-09-15 00:30:11 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 57 | // True if A is better than B. |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 58 | static bool IsBetterCanonical(const GlobalVariable &A, |
| 59 | const GlobalVariable &B) { |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 60 | if (!A.hasLocalLinkage() && B.hasLocalLinkage()) |
| 61 | return true; |
| 62 | |
| 63 | if (A.hasLocalLinkage() && !B.hasLocalLinkage()) |
| 64 | return false; |
| 65 | |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 66 | return A.hasGlobalUnnamedAddr(); |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Evgeniy Stepanov | 8537d99 | 2017-03-09 00:03:37 +0000 | [diff] [blame] | 69 | static bool hasMetadataOtherThanDebugLoc(const GlobalVariable *GV) { |
| 70 | SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; |
| 71 | GV->getAllMetadata(MDs); |
| 72 | for (const auto &V : MDs) |
| 73 | if (V.first != LLVMContext::MD_dbg) |
| 74 | return true; |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | static void copyDebugLocMetadata(const GlobalVariable *From, |
| 79 | GlobalVariable *To) { |
| 80 | SmallVector<DIGlobalVariableExpression *, 1> MDs; |
| 81 | From->getDebugInfo(MDs); |
| 82 | for (auto MD : MDs) |
| 83 | To->addDebugInfo(MD); |
| 84 | } |
| 85 | |
Davide Italiano | 17da174 | 2016-05-04 03:21:20 +0000 | [diff] [blame] | 86 | static unsigned getAlignment(GlobalVariable *GV) { |
Rafael Espindola | dd8757a | 2013-11-12 20:21:43 +0000 | [diff] [blame] | 87 | unsigned Align = GV->getAlignment(); |
| 88 | if (Align) |
| 89 | return Align; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 90 | return GV->getParent()->getDataLayout().getPreferredAlignment(GV); |
Nick Lewycky | 8ac9ece | 2011-07-27 19:47:34 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Vedant Kumar | 857cacd | 2019-01-20 02:44:43 +0000 | [diff] [blame] | 93 | static bool |
| 94 | isUnmergeableGlobal(GlobalVariable *GV, |
| 95 | const SmallPtrSetImpl<const GlobalValue *> &UsedGlobals) { |
| 96 | // Only process constants with initializers in the default address space. |
| 97 | return !GV->isConstant() || !GV->hasDefinitiveInitializer() || |
| 98 | GV->getType()->getAddressSpace() != 0 || GV->hasSection() || |
| 99 | // Don't touch values marked with attribute(used). |
| 100 | UsedGlobals.count(GV); |
| 101 | } |
| 102 | |
JF Bastien | 42ca9cc | 2018-08-09 21:56:09 +0000 | [diff] [blame] | 103 | enum class CanMerge { No, Yes }; |
| 104 | static CanMerge makeMergeable(GlobalVariable *Old, GlobalVariable *New) { |
| 105 | if (!Old->hasGlobalUnnamedAddr() && !New->hasGlobalUnnamedAddr()) |
| 106 | return CanMerge::No; |
| 107 | if (hasMetadataOtherThanDebugLoc(Old)) |
| 108 | return CanMerge::No; |
| 109 | assert(!hasMetadataOtherThanDebugLoc(New)); |
| 110 | if (!Old->hasGlobalUnnamedAddr()) |
| 111 | New->setUnnamedAddr(GlobalValue::UnnamedAddr::None); |
| 112 | return CanMerge::Yes; |
| 113 | } |
| 114 | |
| 115 | static void replace(Module &M, GlobalVariable *Old, GlobalVariable *New) { |
| 116 | Constant *NewConstant = New; |
| 117 | |
| 118 | LLVM_DEBUG(dbgs() << "Replacing global: @" << Old->getName() << " -> @" |
| 119 | << New->getName() << "\n"); |
| 120 | |
| 121 | // Bump the alignment if necessary. |
| 122 | if (Old->getAlignment() || New->getAlignment()) |
| 123 | New->setAlignment(std::max(getAlignment(Old), getAlignment(New))); |
| 124 | |
| 125 | copyDebugLocMetadata(Old, New); |
| 126 | Old->replaceAllUsesWith(NewConstant); |
| 127 | |
| 128 | // Delete the global value from the module. |
| 129 | assert(Old->hasLocalLinkage() && |
| 130 | "Refusing to delete an externally visible global variable."); |
| 131 | Old->eraseFromParent(); |
| 132 | } |
| 133 | |
Davide Italiano | 164b9bc | 2016-05-05 00:51:09 +0000 | [diff] [blame] | 134 | static bool mergeConstants(Module &M) { |
Chris Lattner | 67e5345 | 2010-09-15 00:30:11 +0000 | [diff] [blame] | 135 | // Find all the globals that are marked "used". These cannot be merged. |
| 136 | SmallPtrSet<const GlobalValue*, 8> UsedGlobals; |
| 137 | FindUsedValues(M.getGlobalVariable("llvm.used"), UsedGlobals); |
| 138 | FindUsedValues(M.getGlobalVariable("llvm.compiler.used"), UsedGlobals); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 139 | |
| 140 | // Map unique constants to globals. |
| 141 | DenseMap<Constant *, GlobalVariable *> CMap; |
Chris Lattner | c2ee054 | 2003-12-22 23:49:36 +0000 | [diff] [blame] | 142 | |
JF Bastien | fe258d9 | 2018-08-10 22:41:09 +0000 | [diff] [blame] | 143 | SmallVector<std::pair<GlobalVariable *, GlobalVariable *>, 32> |
| 144 | SameContentReplacements; |
Chris Lattner | c2ee054 | 2003-12-22 23:49:36 +0000 | [diff] [blame] | 145 | |
JF Bastien | fe258d9 | 2018-08-10 22:41:09 +0000 | [diff] [blame] | 146 | size_t ChangesMade = 0; |
| 147 | size_t OldChangesMade = 0; |
Chris Lattner | 4816d63 | 2001-10-18 20:05:37 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 56db5e9 | 2003-12-28 07:19:08 +0000 | [diff] [blame] | 149 | // Iterate constant merging while we are still making progress. Merging two |
| 150 | // constants together may allow us to merge other constants together if the |
| 151 | // second level constants have initializers which point to the globals that |
| 152 | // were just merged. |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 153 | while (true) { |
JF Bastien | fe258d9 | 2018-08-10 22:41:09 +0000 | [diff] [blame] | 154 | // Find the canonical constants others will be merged with. |
Chris Lattner | 6f58839 | 2007-04-14 18:06:52 +0000 | [diff] [blame] | 155 | for (Module::global_iterator GVI = M.global_begin(), E = M.global_end(); |
| 156 | GVI != E; ) { |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 157 | GlobalVariable *GV = &*GVI++; |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 02137ee | 2007-04-14 01:11:54 +0000 | [diff] [blame] | 159 | // If this GV is dead, remove it. |
| 160 | GV->removeDeadConstantUsers(); |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 161 | if (GV->use_empty() && GV->hasLocalLinkage()) { |
Jeff Cohen | 4bd0fd3 | 2007-04-14 17:18:29 +0000 | [diff] [blame] | 162 | GV->eraseFromParent(); |
JF Bastien | fe258d9 | 2018-08-10 22:41:09 +0000 | [diff] [blame] | 163 | ++ChangesMade; |
Jeff Cohen | 4bd0fd3 | 2007-04-14 17:18:29 +0000 | [diff] [blame] | 164 | continue; |
Chris Lattner | 02137ee | 2007-04-14 01:11:54 +0000 | [diff] [blame] | 165 | } |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 166 | |
Vedant Kumar | 857cacd | 2019-01-20 02:44:43 +0000 | [diff] [blame] | 167 | if (isUnmergeableGlobal(GV, UsedGlobals)) |
Chris Lattner | 75879be | 2010-02-12 18:17:23 +0000 | [diff] [blame] | 168 | continue; |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 169 | |
Eli Friedman | b31c627 | 2012-01-11 22:06:46 +0000 | [diff] [blame] | 170 | // This transformation is legal for weak ODR globals in the sense it |
| 171 | // doesn't change semantics, but we really don't want to perform it |
| 172 | // anyway; it's likely to pessimize code generation, and some tools |
| 173 | // (like the Darwin linker in cases involving CFString) don't expect it. |
| 174 | if (GV->isWeakForLinker()) |
Bill Wendling | c791551 | 2012-01-11 00:13:08 +0000 | [diff] [blame] | 175 | continue; |
| 176 | |
Evgeniy Stepanov | 8537d99 | 2017-03-09 00:03:37 +0000 | [diff] [blame] | 177 | // Don't touch globals with metadata other then !dbg. |
| 178 | if (hasMetadataOtherThanDebugLoc(GV)) |
| 179 | continue; |
| 180 | |
JF Bastien | b99f131 | 2018-08-10 22:10:20 +0000 | [diff] [blame] | 181 | Constant *Init = GV->getInitializer(); |
| 182 | |
Chris Lattner | 75879be | 2010-02-12 18:17:23 +0000 | [diff] [blame] | 183 | // Check to see if the initializer is already known. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 184 | GlobalVariable *&Slot = CMap[Init]; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 185 | |
Bill Wendling | c791551 | 2012-01-11 00:13:08 +0000 | [diff] [blame] | 186 | // If this is the first constant we find or if the old one is local, |
| 187 | // replace with the current one. If the current is externally visible |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 188 | // it cannot be replace, but can be the canonical constant we merge with. |
JF Bastien | 42ca9cc | 2018-08-09 21:56:09 +0000 | [diff] [blame] | 189 | bool FirstConstantFound = !Slot; |
| 190 | if (FirstConstantFound || IsBetterCanonical(*GV, *Slot)) { |
Chris Lattner | 75879be | 2010-02-12 18:17:23 +0000 | [diff] [blame] | 191 | Slot = GV; |
JF Bastien | 42ca9cc | 2018-08-09 21:56:09 +0000 | [diff] [blame] | 192 | LLVM_DEBUG(dbgs() << "Cmap[" << *Init << "] = " << GV->getName() |
| 193 | << (FirstConstantFound ? "\n" : " (updated)\n")); |
| 194 | } |
Nick Lewycky | 0296a48 | 2011-01-15 18:14:21 +0000 | [diff] [blame] | 195 | } |
| 196 | |
JF Bastien | fe258d9 | 2018-08-10 22:41:09 +0000 | [diff] [blame] | 197 | // Identify all globals that can be merged together, filling in the |
| 198 | // SameContentReplacements vector. We cannot do the replacement in this pass |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 199 | // because doing so may cause initializers of other globals to be rewritten, |
| 200 | // invalidating the Constant* pointers in CMap. |
Nick Lewycky | 0296a48 | 2011-01-15 18:14:21 +0000 | [diff] [blame] | 201 | for (Module::global_iterator GVI = M.global_begin(), E = M.global_end(); |
| 202 | GVI != E; ) { |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 203 | GlobalVariable *GV = &*GVI++; |
Nick Lewycky | 0296a48 | 2011-01-15 18:14:21 +0000 | [diff] [blame] | 204 | |
Vedant Kumar | 857cacd | 2019-01-20 02:44:43 +0000 | [diff] [blame] | 205 | if (isUnmergeableGlobal(GV, UsedGlobals)) |
Nick Lewycky | 0296a48 | 2011-01-15 18:14:21 +0000 | [diff] [blame] | 206 | continue; |
| 207 | |
Eli Friedman | b31c627 | 2012-01-11 22:06:46 +0000 | [diff] [blame] | 208 | // We can only replace constant with local linkage. |
| 209 | if (!GV->hasLocalLinkage()) |
Nick Lewycky | 0296a48 | 2011-01-15 18:14:21 +0000 | [diff] [blame] | 210 | continue; |
| 211 | |
JF Bastien | b99f131 | 2018-08-10 22:10:20 +0000 | [diff] [blame] | 212 | Constant *Init = GV->getInitializer(); |
| 213 | |
Nick Lewycky | 0296a48 | 2011-01-15 18:14:21 +0000 | [diff] [blame] | 214 | // Check to see if the initializer is already known. |
JF Bastien | 3f27033 | 2018-08-09 04:17:48 +0000 | [diff] [blame] | 215 | auto Found = CMap.find(Init); |
| 216 | if (Found == CMap.end()) |
| 217 | continue; |
Nick Lewycky | 0296a48 | 2011-01-15 18:14:21 +0000 | [diff] [blame] | 218 | |
JF Bastien | 3f27033 | 2018-08-09 04:17:48 +0000 | [diff] [blame] | 219 | GlobalVariable *Slot = Found->second; |
| 220 | if (Slot == GV) |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 221 | continue; |
| 222 | |
JF Bastien | 42ca9cc | 2018-08-09 21:56:09 +0000 | [diff] [blame] | 223 | if (makeMergeable(GV, Slot) == CanMerge::No) |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 224 | continue; |
| 225 | |
Rafael Espindola | 751677a | 2011-01-16 17:05:09 +0000 | [diff] [blame] | 226 | // Make all uses of the duplicate constant use the canonical version. |
JF Bastien | 42ca9cc | 2018-08-09 21:56:09 +0000 | [diff] [blame] | 227 | LLVM_DEBUG(dbgs() << "Will replace: @" << GV->getName() << " -> @" |
| 228 | << Slot->getName() << "\n"); |
JF Bastien | fe258d9 | 2018-08-10 22:41:09 +0000 | [diff] [blame] | 229 | SameContentReplacements.push_back(std::make_pair(GV, Slot)); |
Chris Lattner | 02137ee | 2007-04-14 01:11:54 +0000 | [diff] [blame] | 230 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 56db5e9 | 2003-12-28 07:19:08 +0000 | [diff] [blame] | 232 | // Now that we have figured out which replacements must be made, do them all |
| 233 | // now. This avoid invalidating the pointers in CMap, which are unneeded |
| 234 | // now. |
JF Bastien | fe258d9 | 2018-08-10 22:41:09 +0000 | [diff] [blame] | 235 | for (unsigned i = 0, e = SameContentReplacements.size(); i != e; ++i) { |
| 236 | GlobalVariable *Old = SameContentReplacements[i].first; |
| 237 | GlobalVariable *New = SameContentReplacements[i].second; |
JF Bastien | 42ca9cc | 2018-08-09 21:56:09 +0000 | [diff] [blame] | 238 | replace(M, Old, New); |
JF Bastien | fe258d9 | 2018-08-10 22:41:09 +0000 | [diff] [blame] | 239 | ++ChangesMade; |
| 240 | ++NumIdenticalMerged; |
Chris Lattner | 56db5e9 | 2003-12-28 07:19:08 +0000 | [diff] [blame] | 241 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 242 | |
JF Bastien | fe258d9 | 2018-08-10 22:41:09 +0000 | [diff] [blame] | 243 | if (ChangesMade == OldChangesMade) |
| 244 | break; |
| 245 | OldChangesMade = ChangesMade; |
| 246 | |
| 247 | SameContentReplacements.clear(); |
| 248 | CMap.clear(); |
Chris Lattner | c2ee054 | 2003-12-22 23:49:36 +0000 | [diff] [blame] | 249 | } |
JF Bastien | fe258d9 | 2018-08-10 22:41:09 +0000 | [diff] [blame] | 250 | |
| 251 | return ChangesMade; |
Chris Lattner | c2ee054 | 2003-12-22 23:49:36 +0000 | [diff] [blame] | 252 | } |
Davide Italiano | 164b9bc | 2016-05-05 00:51:09 +0000 | [diff] [blame] | 253 | |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 254 | PreservedAnalyses ConstantMergePass::run(Module &M, ModuleAnalysisManager &) { |
Davide Italiano | 164b9bc | 2016-05-05 00:51:09 +0000 | [diff] [blame] | 255 | if (!mergeConstants(M)) |
| 256 | return PreservedAnalyses::all(); |
| 257 | return PreservedAnalyses::none(); |
| 258 | } |
| 259 | |
| 260 | namespace { |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 261 | |
Davide Italiano | 164b9bc | 2016-05-05 00:51:09 +0000 | [diff] [blame] | 262 | struct ConstantMergeLegacyPass : public ModulePass { |
| 263 | static char ID; // Pass identification, replacement for typeid |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 264 | |
Davide Italiano | 164b9bc | 2016-05-05 00:51:09 +0000 | [diff] [blame] | 265 | ConstantMergeLegacyPass() : ModulePass(ID) { |
| 266 | initializeConstantMergeLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 267 | } |
| 268 | |
| 269 | // For this pass, process all of the globals in the module, eliminating |
| 270 | // duplicate constants. |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 271 | bool runOnModule(Module &M) override { |
Davide Italiano | 164b9bc | 2016-05-05 00:51:09 +0000 | [diff] [blame] | 272 | if (skipModule(M)) |
| 273 | return false; |
| 274 | return mergeConstants(M); |
| 275 | } |
| 276 | }; |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 277 | |
| 278 | } // end anonymous namespace |
Davide Italiano | 164b9bc | 2016-05-05 00:51:09 +0000 | [diff] [blame] | 279 | |
| 280 | char ConstantMergeLegacyPass::ID = 0; |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 281 | |
Davide Italiano | 164b9bc | 2016-05-05 00:51:09 +0000 | [diff] [blame] | 282 | INITIALIZE_PASS(ConstantMergeLegacyPass, "constmerge", |
| 283 | "Merge Duplicate Global Constants", false, false) |
| 284 | |
| 285 | ModulePass *llvm::createConstantMergePass() { |
| 286 | return new ConstantMergeLegacyPass(); |
| 287 | } |