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/Support/Casting.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Transforms/IPO.h"
34#include <algorithm>
35#include <cassert>
36#include <utility>
37
38using namespace llvm;
39
40#define DEBUG_TYPE "constmerge"
41
42STATISTIC(NumIdenticalMerged, "Number of identical global constants merged");
43
44/// Find values that are marked as llvm.used.
45static void FindUsedValues(GlobalVariable *LLVMUsed,
46 SmallPtrSetImpl<const GlobalValue*> &UsedValues) {
47 if (!LLVMUsed) return;
48 ConstantArray *Inits = cast<ConstantArray>(Val: LLVMUsed->getInitializer());
49
50 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
51 Value *Operand = Inits->getOperand(i_nocapture: i)->stripPointerCasts();
52 GlobalValue *GV = cast<GlobalValue>(Val: Operand);
53 UsedValues.insert(Ptr: GV);
54 }
55}
56
57// True if A is better than B.
58static 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
69static void copyDebugLocMetadata(const GlobalVariable *From,
70 GlobalVariable *To) {
71 SmallVector<DIGlobalVariableExpression *, 1> MDs;
72 From->getDebugInfo(GVs&: MDs);
73 for (auto *MD : MDs)
74 To->addDebugInfo(GV: MD);
75}
76
77static Align getAlign(GlobalVariable *GV) {
78 return GV->getAlign().value_or(
79 u: GV->getDataLayout().getPreferredAlign(GV));
80}
81
82static bool
83isUnmergeableGlobal(GlobalVariable *GV,
84 const SmallPtrSetImpl<const GlobalValue *> &UsedGlobals) {
85 // Only process constants with initializers in the default address space.
86 return !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
87 GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
88 // Don't touch thread-local variables.
89 GV->isThreadLocal() ||
90 // Don't touch values marked with attribute(used).
91 UsedGlobals.count(Ptr: GV);
92}
93
94enum class CanMerge { No, Yes };
95static CanMerge makeMergeable(GlobalVariable *Old, GlobalVariable *New) {
96 if (!Old->hasGlobalUnnamedAddr() && !New->hasGlobalUnnamedAddr())
97 return CanMerge::No;
98 if (Old->hasMetadataOtherThanDebugLoc())
99 return CanMerge::No;
100 assert(!New->hasMetadataOtherThanDebugLoc());
101 if (!Old->hasGlobalUnnamedAddr())
102 New->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
103 return CanMerge::Yes;
104}
105
106static void replace(Module &M, GlobalVariable *Old, GlobalVariable *New) {
107 Constant *NewConstant = New;
108
109 LLVM_DEBUG(dbgs() << "Replacing global: @" << Old->getName() << " -> @"
110 << New->getName() << "\n");
111
112 // Bump the alignment if necessary.
113 if (Old->getAlign() || New->getAlign())
114 New->setAlignment(std::max(a: getAlign(GV: Old), b: getAlign(GV: New)));
115
116 copyDebugLocMetadata(From: Old, To: New);
117 Old->replaceAllUsesWith(V: NewConstant);
118
119 // Delete the global value from the module.
120 assert(Old->hasLocalLinkage() &&
121 "Refusing to delete an externally visible global variable.");
122 Old->eraseFromParent();
123}
124
125static bool mergeConstants(Module &M) {
126 // Find all the globals that are marked "used". These cannot be merged.
127 SmallPtrSet<const GlobalValue*, 8> UsedGlobals;
128 FindUsedValues(LLVMUsed: M.getGlobalVariable(Name: "llvm.used"), UsedValues&: UsedGlobals);
129 FindUsedValues(LLVMUsed: M.getGlobalVariable(Name: "llvm.compiler.used"), UsedValues&: UsedGlobals);
130
131 // Map unique constants to globals.
132 DenseMap<Constant *, GlobalVariable *> CMap;
133
134 SmallVector<std::pair<GlobalVariable *, GlobalVariable *>, 32>
135 SameContentReplacements;
136
137 size_t ChangesMade = 0;
138 size_t OldChangesMade = 0;
139
140 // Iterate constant merging while we are still making progress. Merging two
141 // constants together may allow us to merge other constants together if the
142 // second level constants have initializers which point to the globals that
143 // were just merged.
144 while (true) {
145 // Find the canonical constants others will be merged with.
146 for (GlobalVariable &GV : llvm::make_early_inc_range(Range: M.globals())) {
147 // If this GV is dead, remove it.
148 GV.removeDeadConstantUsers();
149 if (GV.use_empty() && GV.hasLocalLinkage()) {
150 GV.eraseFromParent();
151 ++ChangesMade;
152 continue;
153 }
154
155 if (isUnmergeableGlobal(GV: &GV, UsedGlobals))
156 continue;
157
158 // This transformation is legal for weak ODR globals in the sense it
159 // doesn't change semantics, but we really don't want to perform it
160 // anyway; it's likely to pessimize code generation, and some tools
161 // (like the Darwin linker in cases involving CFString) don't expect it.
162 if (GV.isWeakForLinker())
163 continue;
164
165 // Don't touch globals with metadata other then !dbg.
166 if (GV.hasMetadataOtherThanDebugLoc())
167 continue;
168
169 Constant *Init = GV.getInitializer();
170
171 // Check to see if the initializer is already known.
172 GlobalVariable *&Slot = CMap[Init];
173
174 // If this is the first constant we find or if the old one is local,
175 // replace with the current one. If the current is externally visible
176 // it cannot be replace, but can be the canonical constant we merge with.
177 bool FirstConstantFound = !Slot;
178 if (FirstConstantFound || IsBetterCanonical(A: GV, B: *Slot)) {
179 Slot = &GV;
180 LLVM_DEBUG(dbgs() << "Cmap[" << *Init << "] = " << GV.getName()
181 << (FirstConstantFound ? "\n" : " (updated)\n"));
182 }
183 }
184
185 // Identify all globals that can be merged together, filling in the
186 // SameContentReplacements vector. We cannot do the replacement in this pass
187 // because doing so may cause initializers of other globals to be rewritten,
188 // invalidating the Constant* pointers in CMap.
189 for (GlobalVariable &GV : llvm::make_early_inc_range(Range: M.globals())) {
190 if (isUnmergeableGlobal(GV: &GV, UsedGlobals))
191 continue;
192
193 // We can only replace constant with local linkage.
194 if (!GV.hasLocalLinkage())
195 continue;
196
197 Constant *Init = GV.getInitializer();
198
199 // Check to see if the initializer is already known.
200 auto Found = CMap.find(Val: Init);
201 if (Found == CMap.end())
202 continue;
203
204 GlobalVariable *Slot = Found->second;
205 if (Slot == &GV)
206 continue;
207
208 if (makeMergeable(Old: &GV, New: Slot) == CanMerge::No)
209 continue;
210
211 // Make all uses of the duplicate constant use the canonical version.
212 LLVM_DEBUG(dbgs() << "Will replace: @" << GV.getName() << " -> @"
213 << Slot->getName() << "\n");
214 SameContentReplacements.push_back(Elt: std::make_pair(x: &GV, y&: Slot));
215 }
216
217 // Now that we have figured out which replacements must be made, do them all
218 // now. This avoid invalidating the pointers in CMap, which are unneeded
219 // now.
220 for (const auto &[Old, New] : SameContentReplacements) {
221 replace(M, Old, New);
222 ++ChangesMade;
223 ++NumIdenticalMerged;
224 }
225
226 if (ChangesMade == OldChangesMade)
227 break;
228 OldChangesMade = ChangesMade;
229
230 SameContentReplacements.clear();
231 CMap.clear();
232 }
233
234 return ChangesMade;
235}
236
237PreservedAnalyses ConstantMergePass::run(Module &M, ModuleAnalysisManager &) {
238 if (!mergeConstants(M))
239 return PreservedAnalyses::all();
240 return PreservedAnalyses::none();
241}
242