blob: 16a77c0efcb731142f0949d05d21a271225499e7 [file] [log] [blame]
Chris Lattner424132a2003-04-18 04:34:29 +00001//===- ConstantMerge.cpp - Merge duplicate global constants ---------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner4816d632001-10-18 20:05:37 +00009//
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 Lattner28d10352002-09-23 23:00:46 +000013// the program, regardless of whether or not an existing string is available.
Chris Lattner4816d632001-10-18 20:05:37 +000014//
15// Algorithm: ConstantMerge is designed to build up a map of available constants
Chris Lattner28d10352002-09-23 23:00:46 +000016// and eliminate duplicates when it is initialized.
Chris Lattner4816d632001-10-18 20:05:37 +000017//
18//===----------------------------------------------------------------------===//
19
Davide Italiano164b9bc2016-05-05 00:51:09 +000020#include "llvm/Transforms/IPO/ConstantMerge.h"
Chris Lattner75879be2010-02-12 18:17:23 +000021#include "llvm/ADT/DenseMap.h"
Chris Lattner67e53452010-09-15 00:30:11 +000022#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +000023#include "llvm/ADT/SmallVector.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/ADT/Statistic.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Constants.h"
26#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/DerivedTypes.h"
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +000028#include "llvm/IR/GlobalValue.h"
29#include "llvm/IR/GlobalVariable.h"
30#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/Module.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Pass.h"
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +000033#include "llvm/Support/Casting.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000034#include "llvm/Transforms/IPO.h"
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +000035#include <algorithm>
36#include <cassert>
37#include <utility>
38
Chris Lattnerf52e03c2003-11-21 21:54:22 +000039using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000040
Chandler Carruth964daaa2014-04-22 02:55:47 +000041#define DEBUG_TYPE "constmerge"
42
JF Bastien62fb8ea2018-08-10 21:58:00 +000043STATISTIC(NumIdenticalMerged, "Number of identical global constants merged");
Chris Lattnereac4dcd2002-10-09 23:16:04 +000044
Chris Lattner67e53452010-09-15 00:30:11 +000045/// Find values that are marked as llvm.used.
46static void FindUsedValues(GlobalVariable *LLVMUsed,
Craig Topper71b7b682014-08-21 05:55:13 +000047 SmallPtrSetImpl<const GlobalValue*> &UsedValues) {
Craig Topperf40110f2014-04-25 05:29:35 +000048 if (!LLVMUsed) return;
Rafael Espindola74f2e462013-04-22 14:58:02 +000049 ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
50
Rafael Espindolac229a4f2013-05-06 01:48:55 +000051 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
52 Value *Operand = Inits->getOperand(i)->stripPointerCastsNoFollowAliases();
53 GlobalValue *GV = cast<GlobalValue>(Operand);
54 UsedValues.insert(GV);
55 }
Chris Lattner67e53452010-09-15 00:30:11 +000056}
57
Rafael Espindola751677a2011-01-16 17:05:09 +000058// True if A is better than B.
Alp Tokercb402912014-01-24 17:20:08 +000059static bool IsBetterCanonical(const GlobalVariable &A,
60 const GlobalVariable &B) {
Rafael Espindola751677a2011-01-16 17:05:09 +000061 if (!A.hasLocalLinkage() && B.hasLocalLinkage())
62 return true;
63
64 if (A.hasLocalLinkage() && !B.hasLocalLinkage())
65 return false;
66
Peter Collingbourne96efdd62016-06-14 21:01:22 +000067 return A.hasGlobalUnnamedAddr();
Rafael Espindola751677a2011-01-16 17:05:09 +000068}
69
Evgeniy Stepanov8537d992017-03-09 00:03:37 +000070static bool hasMetadataOtherThanDebugLoc(const GlobalVariable *GV) {
71 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
72 GV->getAllMetadata(MDs);
73 for (const auto &V : MDs)
74 if (V.first != LLVMContext::MD_dbg)
75 return true;
76 return false;
77}
78
79static void copyDebugLocMetadata(const GlobalVariable *From,
80 GlobalVariable *To) {
81 SmallVector<DIGlobalVariableExpression *, 1> MDs;
82 From->getDebugInfo(MDs);
83 for (auto MD : MDs)
84 To->addDebugInfo(MD);
85}
86
Davide Italiano17da1742016-05-04 03:21:20 +000087static unsigned getAlignment(GlobalVariable *GV) {
Rafael Espindoladd8757a2013-11-12 20:21:43 +000088 unsigned Align = GV->getAlignment();
89 if (Align)
90 return Align;
Mehdi Aminia28d91d2015-03-10 02:37:25 +000091 return GV->getParent()->getDataLayout().getPreferredAlignment(GV);
Nick Lewycky8ac9ece2011-07-27 19:47:34 +000092}
93
JF Bastien42ca9cc2018-08-09 21:56:09 +000094enum class CanMerge { No, Yes };
95static CanMerge makeMergeable(GlobalVariable *Old, GlobalVariable *New) {
96 if (!Old->hasGlobalUnnamedAddr() && !New->hasGlobalUnnamedAddr())
97 return CanMerge::No;
98 if (hasMetadataOtherThanDebugLoc(Old))
99 return CanMerge::No;
100 assert(!hasMetadataOtherThanDebugLoc(New));
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->getAlignment() || New->getAlignment())
114 New->setAlignment(std::max(getAlignment(Old), getAlignment(New)));
115
116 copyDebugLocMetadata(Old, New);
117 Old->replaceAllUsesWith(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
Davide Italiano164b9bc2016-05-05 00:51:09 +0000125static bool mergeConstants(Module &M) {
Chris Lattner67e53452010-09-15 00:30:11 +0000126 // Find all the globals that are marked "used". These cannot be merged.
127 SmallPtrSet<const GlobalValue*, 8> UsedGlobals;
128 FindUsedValues(M.getGlobalVariable("llvm.used"), UsedGlobals);
129 FindUsedValues(M.getGlobalVariable("llvm.compiler.used"), UsedGlobals);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000130
131 // Map unique constants to globals.
132 DenseMap<Constant *, GlobalVariable *> CMap;
Chris Lattnerc2ee0542003-12-22 23:49:36 +0000133
JF Bastien62fb8ea2018-08-10 21:58:00 +0000134 SmallVector<std::pair<GlobalVariable *, GlobalVariable *>, 32>
135 SameContentReplacements;
Chris Lattnerc2ee0542003-12-22 23:49:36 +0000136
JF Bastien62fb8ea2018-08-10 21:58:00 +0000137 size_t ChangesMade = 0;
138 size_t OldChangesMade = 0;
Chris Lattner4816d632001-10-18 20:05:37 +0000139
Chris Lattner56db5e92003-12-28 07:19:08 +0000140 // 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.
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +0000144 while (true) {
JF Bastien62fb8ea2018-08-10 21:58:00 +0000145 // Find the canonical constants others will be merged with.
Chris Lattner6f588392007-04-14 18:06:52 +0000146 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
147 GVI != E; ) {
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000148 GlobalVariable *GV = &*GVI++;
JF Bastien62fb8ea2018-08-10 21:58:00 +0000149 Constant *Init = GV->getInitializer();
Rafael Espindola751677a2011-01-16 17:05:09 +0000150
Chris Lattner02137ee2007-04-14 01:11:54 +0000151 // If this GV is dead, remove it.
152 GV->removeDeadConstantUsers();
Rafael Espindola6de96a12009-01-15 20:18:42 +0000153 if (GV->use_empty() && GV->hasLocalLinkage()) {
Jeff Cohen4bd0fd32007-04-14 17:18:29 +0000154 GV->eraseFromParent();
JF Bastien62fb8ea2018-08-10 21:58:00 +0000155 ++ChangesMade;
Jeff Cohen4bd0fd32007-04-14 17:18:29 +0000156 continue;
Chris Lattner02137ee2007-04-14 01:11:54 +0000157 }
Rafael Espindola751677a2011-01-16 17:05:09 +0000158
Nick Lewycky0296a482011-01-15 18:14:21 +0000159 // Only process constants with initializers in the default address space.
Nick Lewycky4a1ff162011-01-15 18:42:52 +0000160 if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
Nick Lewycky0296a482011-01-15 18:14:21 +0000161 GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
Chris Lattner67e53452010-09-15 00:30:11 +0000162 // Don't touch values marked with attribute(used).
163 UsedGlobals.count(GV))
Chris Lattner75879be2010-02-12 18:17:23 +0000164 continue;
Rafael Espindola751677a2011-01-16 17:05:09 +0000165
Eli Friedmanb31c6272012-01-11 22:06:46 +0000166 // This transformation is legal for weak ODR globals in the sense it
167 // doesn't change semantics, but we really don't want to perform it
168 // anyway; it's likely to pessimize code generation, and some tools
169 // (like the Darwin linker in cases involving CFString) don't expect it.
170 if (GV->isWeakForLinker())
Bill Wendlingc7915512012-01-11 00:13:08 +0000171 continue;
172
Evgeniy Stepanov8537d992017-03-09 00:03:37 +0000173 // Don't touch globals with metadata other then !dbg.
174 if (hasMetadataOtherThanDebugLoc(GV))
175 continue;
176
Chris Lattner75879be2010-02-12 18:17:23 +0000177 // Check to see if the initializer is already known.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000178 GlobalVariable *&Slot = CMap[Init];
Misha Brukmanb1c93172005-04-21 23:48:37 +0000179
Bill Wendlingc7915512012-01-11 00:13:08 +0000180 // 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
Rafael Espindola751677a2011-01-16 17:05:09 +0000182 // it cannot be replace, but can be the canonical constant we merge with.
JF Bastien42ca9cc2018-08-09 21:56:09 +0000183 bool FirstConstantFound = !Slot;
184 if (FirstConstantFound || IsBetterCanonical(*GV, *Slot)) {
Chris Lattner75879be2010-02-12 18:17:23 +0000185 Slot = GV;
JF Bastien42ca9cc2018-08-09 21:56:09 +0000186 LLVM_DEBUG(dbgs() << "Cmap[" << *Init << "] = " << GV->getName()
187 << (FirstConstantFound ? "\n" : " (updated)\n"));
188 }
Nick Lewycky0296a482011-01-15 18:14:21 +0000189 }
190
JF Bastien62fb8ea2018-08-10 21:58:00 +0000191 // Identify all globals that can be merged together, filling in the
192 // SameContentReplacements vector. We cannot do the replacement in this pass
Rafael Espindola751677a2011-01-16 17:05:09 +0000193 // because doing so may cause initializers of other globals to be rewritten,
194 // invalidating the Constant* pointers in CMap.
Nick Lewycky0296a482011-01-15 18:14:21 +0000195 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
196 GVI != E; ) {
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000197 GlobalVariable *GV = &*GVI++;
JF Bastien62fb8ea2018-08-10 21:58:00 +0000198 Constant *Init = GV->getInitializer();
Nick Lewycky0296a482011-01-15 18:14:21 +0000199
200 // Only process constants with initializers in the default address space.
Nick Lewycky4a1ff162011-01-15 18:42:52 +0000201 if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
Nick Lewycky0296a482011-01-15 18:14:21 +0000202 GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
203 // Don't touch values marked with attribute(used).
204 UsedGlobals.count(GV))
205 continue;
206
Eli Friedmanb31c6272012-01-11 22:06:46 +0000207 // We can only replace constant with local linkage.
208 if (!GV->hasLocalLinkage())
Nick Lewycky0296a482011-01-15 18:14:21 +0000209 continue;
210
Nick Lewycky0296a482011-01-15 18:14:21 +0000211 // Check to see if the initializer is already known.
JF Bastien3f270332018-08-09 04:17:48 +0000212 auto Found = CMap.find(Init);
213 if (Found == CMap.end())
214 continue;
Nick Lewycky0296a482011-01-15 18:14:21 +0000215
JF Bastien3f270332018-08-09 04:17:48 +0000216 GlobalVariable *Slot = Found->second;
217 if (Slot == GV)
Rafael Espindola751677a2011-01-16 17:05:09 +0000218 continue;
219
JF Bastien42ca9cc2018-08-09 21:56:09 +0000220 if (makeMergeable(GV, Slot) == CanMerge::No)
Rafael Espindola751677a2011-01-16 17:05:09 +0000221 continue;
222
Rafael Espindola751677a2011-01-16 17:05:09 +0000223 // Make all uses of the duplicate constant use the canonical version.
JF Bastien42ca9cc2018-08-09 21:56:09 +0000224 LLVM_DEBUG(dbgs() << "Will replace: @" << GV->getName() << " -> @"
225 << Slot->getName() << "\n");
JF Bastien62fb8ea2018-08-10 21:58:00 +0000226 SameContentReplacements.push_back(std::make_pair(GV, Slot));
Chris Lattner02137ee2007-04-14 01:11:54 +0000227 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000228
Chris Lattner56db5e92003-12-28 07:19:08 +0000229 // Now that we have figured out which replacements must be made, do them all
230 // now. This avoid invalidating the pointers in CMap, which are unneeded
231 // now.
JF Bastien62fb8ea2018-08-10 21:58:00 +0000232 for (unsigned i = 0, e = SameContentReplacements.size(); i != e; ++i) {
233 GlobalVariable *Old = SameContentReplacements[i].first;
234 GlobalVariable *New = SameContentReplacements[i].second;
JF Bastien42ca9cc2018-08-09 21:56:09 +0000235 replace(M, Old, New);
JF Bastien62fb8ea2018-08-10 21:58:00 +0000236 ++ChangesMade;
237 ++NumIdenticalMerged;
Chris Lattner56db5e92003-12-28 07:19:08 +0000238 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000239
JF Bastien62fb8ea2018-08-10 21:58:00 +0000240 if (ChangesMade == OldChangesMade)
241 break;
242 OldChangesMade = ChangesMade;
243
244 SameContentReplacements.clear();
245 CMap.clear();
Chris Lattnerc2ee0542003-12-22 23:49:36 +0000246 }
JF Bastien62fb8ea2018-08-10 21:58:00 +0000247
248 return ChangesMade;
Chris Lattnerc2ee0542003-12-22 23:49:36 +0000249}
Davide Italiano164b9bc2016-05-05 00:51:09 +0000250
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000251PreservedAnalyses ConstantMergePass::run(Module &M, ModuleAnalysisManager &) {
Davide Italiano164b9bc2016-05-05 00:51:09 +0000252 if (!mergeConstants(M))
253 return PreservedAnalyses::all();
254 return PreservedAnalyses::none();
255}
256
257namespace {
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +0000258
Davide Italiano164b9bc2016-05-05 00:51:09 +0000259struct ConstantMergeLegacyPass : public ModulePass {
260 static char ID; // Pass identification, replacement for typeid
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +0000261
Davide Italiano164b9bc2016-05-05 00:51:09 +0000262 ConstantMergeLegacyPass() : ModulePass(ID) {
263 initializeConstantMergeLegacyPassPass(*PassRegistry::getPassRegistry());
264 }
265
266 // For this pass, process all of the globals in the module, eliminating
267 // duplicate constants.
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +0000268 bool runOnModule(Module &M) override {
Davide Italiano164b9bc2016-05-05 00:51:09 +0000269 if (skipModule(M))
270 return false;
271 return mergeConstants(M);
272 }
273};
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +0000274
275} // end anonymous namespace
Davide Italiano164b9bc2016-05-05 00:51:09 +0000276
277char ConstantMergeLegacyPass::ID = 0;
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +0000278
Davide Italiano164b9bc2016-05-05 00:51:09 +0000279INITIALIZE_PASS(ConstantMergeLegacyPass, "constmerge",
280 "Merge Duplicate Global Constants", false, false)
281
282ModulePass *llvm::createConstantMergePass() {
283 return new ConstantMergeLegacyPass();
284}