1 //===- ConstantMerge.cpp - Merge duplicate global constants ---------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
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
12 // the program, regardless of whether or not an existing string is available.
13 //
14 // Algorithm: ConstantMerge is designed to build up a map of available constants
15 // and eliminate duplicates when it is initialized.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "llvm/Transforms/IPO/ConstantMerge.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/GlobalValue.h"
28 #include "llvm/IR/GlobalVariable.h"
29 #include "llvm/IR/LLVMContext.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Transforms/IPO.h"
34 #include <algorithm>
35 #include <cassert>
36 #include <utility>
37 
38 using namespace llvm;
39 
40 #define DEBUG_TYPE "constmerge"
41 
42 STATISTIC(NumIdenticalMerged, "Number of identical global constants merged");
43 
44 /// Find values that are marked as llvm.used.
45 static void FindUsedValues(GlobalVariable *LLVMUsed,
46                            SmallPtrSetImpl<const GlobalValue*> &UsedValues) {
47   if (!LLVMUsed) return;
48   ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
49 
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   }
55 }
56 
57 // True if A is better than B.
58 static bool IsBetterCanonical(const GlobalVariable &A,
59                               const GlobalVariable &B) {
60   if (!A.hasLocalLinkage() && B.hasLocalLinkage())
61     return true;
62 
63   if (A.hasLocalLinkage() && !B.hasLocalLinkage())
64     return false;
65 
66   return A.hasGlobalUnnamedAddr();
67 }
68 
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 
86 static unsigned getAlignment(GlobalVariable *GV) {
87   unsigned Align = GV->getAlignment();
88   if (Align)
89     return Align;
90   return GV->getParent()->getDataLayout().getPreferredAlignment(GV);
91 }
92 
93 enum class CanMerge { No, Yes };
94 static CanMerge makeMergeable(GlobalVariable *Old, GlobalVariable *New) {
95   if (!Old->hasGlobalUnnamedAddr() && !New->hasGlobalUnnamedAddr())
96     return CanMerge::No;
97   if (hasMetadataOtherThanDebugLoc(Old))
98     return CanMerge::No;
99   assert(!hasMetadataOtherThanDebugLoc(New));
100   if (!Old->hasGlobalUnnamedAddr())
101     New->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
102   return CanMerge::Yes;
103 }
104 
105 static void replace(Module &M, GlobalVariable *Old, GlobalVariable *New) {
106   Constant *NewConstant = New;
107 
108   LLVM_DEBUG(dbgs() << "Replacing global: @" << Old->getName() << " -> @"
109                     << New->getName() << "\n");
110 
111   // Bump the alignment if necessary.
112   if (Old->getAlignment() || New->getAlignment())
113     New->setAlignment(std::max(getAlignment(Old), getAlignment(New)));
114 
115   copyDebugLocMetadata(Old, New);
116   Old->replaceAllUsesWith(NewConstant);
117 
118   // Delete the global value from the module.
119   assert(Old->hasLocalLinkage() &&
120          "Refusing to delete an externally visible global variable.");
121   Old->eraseFromParent();
122 }
123 
124 static bool mergeConstants(Module &M) {
125   // Find all the globals that are marked "used".  These cannot be merged.
126   SmallPtrSet<const GlobalValue*, 8> UsedGlobals;
127   FindUsedValues(M.getGlobalVariable("llvm.used"), UsedGlobals);
128   FindUsedValues(M.getGlobalVariable("llvm.compiler.used"), UsedGlobals);
129 
130   // Map unique constants to globals.
131   DenseMap<Constant *, GlobalVariable *> CMap;
132 
133   SmallVector<std::pair<GlobalVariable *, GlobalVariable *>, 32>
134       SameContentReplacements;
135 
136   size_t ChangesMade = 0;
137   size_t OldChangesMade = 0;
138 
139   // Iterate constant merging while we are still making progress.  Merging two
140   // constants together may allow us to merge other constants together if the
141   // second level constants have initializers which point to the globals that
142   // were just merged.
143   while (true) {
144     // Find the canonical constants others will be merged with.
145     for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
146          GVI != E; ) {
147       GlobalVariable *GV = &*GVI++;
148 
149       // If this GV is dead, remove it.
150       GV->removeDeadConstantUsers();
151       if (GV->use_empty() && GV->hasLocalLinkage()) {
152         GV->eraseFromParent();
153         ++ChangesMade;
154         continue;
155       }
156 
157       // Only process constants with initializers in the default address space.
158       if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
159           GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
160           // Don't touch values marked with attribute(used).
161           UsedGlobals.count(GV))
162         continue;
163 
164       // This transformation is legal for weak ODR globals in the sense it
165       // doesn't change semantics, but we really don't want to perform it
166       // anyway; it's likely to pessimize code generation, and some tools
167       // (like the Darwin linker in cases involving CFString) don't expect it.
168       if (GV->isWeakForLinker())
169         continue;
170 
171       // Don't touch globals with metadata other then !dbg.
172       if (hasMetadataOtherThanDebugLoc(GV))
173         continue;
174 
175       Constant *Init = GV->getInitializer();
176 
177       // Check to see if the initializer is already known.
178       GlobalVariable *&Slot = CMap[Init];
179 
180       // If this is the first constant we find or if the old one is local,
181       // replace with the current one. If the current is externally visible
182       // it cannot be replace, but can be the canonical constant we merge with.
183       bool FirstConstantFound = !Slot;
184       if (FirstConstantFound || IsBetterCanonical(*GV, *Slot)) {
185         Slot = GV;
186         LLVM_DEBUG(dbgs() << "Cmap[" << *Init << "] = " << GV->getName()
187                           << (FirstConstantFound ? "\n" : " (updated)\n"));
188       }
189     }
190 
191     // Identify all globals that can be merged together, filling in the
192     // SameContentReplacements vector. We cannot do the replacement in this pass
193     // because doing so may cause initializers of other globals to be rewritten,
194     // invalidating the Constant* pointers in CMap.
195     for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
196          GVI != E; ) {
197       GlobalVariable *GV = &*GVI++;
198 
199       // Only process constants with initializers in the default address space.
200       if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
201           GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
202           // Don't touch values marked with attribute(used).
203           UsedGlobals.count(GV))
204         continue;
205 
206       // We can only replace constant with local linkage.
207       if (!GV->hasLocalLinkage())
208         continue;
209 
210       Constant *Init = GV->getInitializer();
211 
212       // Check to see if the initializer is already known.
213       auto Found = CMap.find(Init);
214       if (Found == CMap.end())
215         continue;
216 
217       GlobalVariable *Slot = Found->second;
218       if (Slot == GV)
219         continue;
220 
221       if (makeMergeable(GV, Slot) == CanMerge::No)
222         continue;
223 
224       // Make all uses of the duplicate constant use the canonical version.
225       LLVM_DEBUG(dbgs() << "Will replace: @" << GV->getName() << " -> @"
226                         << Slot->getName() << "\n");
227       SameContentReplacements.push_back(std::make_pair(GV, Slot));
228     }
229 
230     // Now that we have figured out which replacements must be made, do them all
231     // now.  This avoid invalidating the pointers in CMap, which are unneeded
232     // now.
233     for (unsigned i = 0, e = SameContentReplacements.size(); i != e; ++i) {
234       GlobalVariable *Old = SameContentReplacements[i].first;
235       GlobalVariable *New = SameContentReplacements[i].second;
236       replace(M, Old, New);
237       ++ChangesMade;
238       ++NumIdenticalMerged;
239     }
240 
241     if (ChangesMade == OldChangesMade)
242       break;
243     OldChangesMade = ChangesMade;
244 
245     SameContentReplacements.clear();
246     CMap.clear();
247   }
248 
249   return ChangesMade;
250 }
251 
252 PreservedAnalyses ConstantMergePass::run(Module &M, ModuleAnalysisManager &) {
253   if (!mergeConstants(M))
254     return PreservedAnalyses::all();
255   return PreservedAnalyses::none();
256 }
257 
258 namespace {
259 
260 struct ConstantMergeLegacyPass : public ModulePass {
261   static char ID; // Pass identification, replacement for typeid
262 
263   ConstantMergeLegacyPass() : ModulePass(ID) {
264     initializeConstantMergeLegacyPassPass(*PassRegistry::getPassRegistry());
265   }
266 
267   // For this pass, process all of the globals in the module, eliminating
268   // duplicate constants.
269   bool runOnModule(Module &M) override {
270     if (skipModule(M))
271       return false;
272     return mergeConstants(M);
273   }
274 };
275 
276 } // end anonymous namespace
277 
278 char ConstantMergeLegacyPass::ID = 0;
279 
280 INITIALIZE_PASS(ConstantMergeLegacyPass, "constmerge",
281                 "Merge Duplicate Global Constants", false, false)
282 
283 ModulePass *llvm::createConstantMergePass() {
284   return new ConstantMergeLegacyPass();
285 }
286