blob: e0b1037053f01437e9a31c3f0ec71450bdfd7afe [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
Chris Lattner1631bcb2006-12-19 22:09:18 +000043STATISTIC(NumMerged, "Number of 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
Davide Italiano164b9bc2016-05-05 00:51:09 +000094static bool mergeConstants(Module &M) {
Chris Lattner67e53452010-09-15 00:30:11 +000095 // Find all the globals that are marked "used". These cannot be merged.
96 SmallPtrSet<const GlobalValue*, 8> UsedGlobals;
97 FindUsedValues(M.getGlobalVariable("llvm.used"), UsedGlobals);
98 FindUsedValues(M.getGlobalVariable("llvm.compiler.used"), UsedGlobals);
Mehdi Aminia28d91d2015-03-10 02:37:25 +000099
100 // Map unique constants to globals.
101 DenseMap<Constant *, GlobalVariable *> CMap;
Chris Lattnerc2ee0542003-12-22 23:49:36 +0000102
103 // Replacements - This vector contains a list of replacements to perform.
Chris Lattner75879be2010-02-12 18:17:23 +0000104 SmallVector<std::pair<GlobalVariable*, GlobalVariable*>, 32> Replacements;
Chris Lattnerc2ee0542003-12-22 23:49:36 +0000105
Chris Lattner56db5e92003-12-28 07:19:08 +0000106 bool MadeChange = false;
Chris Lattner4816d632001-10-18 20:05:37 +0000107
Chris Lattner56db5e92003-12-28 07:19:08 +0000108 // Iterate constant merging while we are still making progress. Merging two
109 // constants together may allow us to merge other constants together if the
110 // second level constants have initializers which point to the globals that
111 // were just merged.
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +0000112 while (true) {
Rafael Espindola751677a2011-01-16 17:05:09 +0000113 // First: Find the canonical constants others will be merged with.
Chris Lattner6f588392007-04-14 18:06:52 +0000114 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
115 GVI != E; ) {
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000116 GlobalVariable *GV = &*GVI++;
Rafael Espindola751677a2011-01-16 17:05:09 +0000117
Chris Lattner02137ee2007-04-14 01:11:54 +0000118 // If this GV is dead, remove it.
119 GV->removeDeadConstantUsers();
Rafael Espindola6de96a12009-01-15 20:18:42 +0000120 if (GV->use_empty() && GV->hasLocalLinkage()) {
Jeff Cohen4bd0fd32007-04-14 17:18:29 +0000121 GV->eraseFromParent();
122 continue;
Chris Lattner02137ee2007-04-14 01:11:54 +0000123 }
Rafael Espindola751677a2011-01-16 17:05:09 +0000124
Nick Lewycky0296a482011-01-15 18:14:21 +0000125 // Only process constants with initializers in the default address space.
Nick Lewycky4a1ff162011-01-15 18:42:52 +0000126 if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
Nick Lewycky0296a482011-01-15 18:14:21 +0000127 GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
Chris Lattner67e53452010-09-15 00:30:11 +0000128 // Don't touch values marked with attribute(used).
129 UsedGlobals.count(GV))
Chris Lattner75879be2010-02-12 18:17:23 +0000130 continue;
Rafael Espindola751677a2011-01-16 17:05:09 +0000131
Eli Friedmanb31c6272012-01-11 22:06:46 +0000132 // This transformation is legal for weak ODR globals in the sense it
133 // doesn't change semantics, but we really don't want to perform it
134 // anyway; it's likely to pessimize code generation, and some tools
135 // (like the Darwin linker in cases involving CFString) don't expect it.
136 if (GV->isWeakForLinker())
Bill Wendlingc7915512012-01-11 00:13:08 +0000137 continue;
138
Evgeniy Stepanov8537d992017-03-09 00:03:37 +0000139 // Don't touch globals with metadata other then !dbg.
140 if (hasMetadataOtherThanDebugLoc(GV))
141 continue;
142
Chris Lattner75879be2010-02-12 18:17:23 +0000143 Constant *Init = GV->getInitializer();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000144
Chris Lattner75879be2010-02-12 18:17:23 +0000145 // Check to see if the initializer is already known.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000146 GlobalVariable *&Slot = CMap[Init];
Misha Brukmanb1c93172005-04-21 23:48:37 +0000147
Bill Wendlingc7915512012-01-11 00:13:08 +0000148 // If this is the first constant we find or if the old one is local,
149 // replace with the current one. If the current is externally visible
Rafael Espindola751677a2011-01-16 17:05:09 +0000150 // it cannot be replace, but can be the canonical constant we merge with.
Craig Topperf40110f2014-04-25 05:29:35 +0000151 if (!Slot || IsBetterCanonical(*GV, *Slot))
Chris Lattner75879be2010-02-12 18:17:23 +0000152 Slot = GV;
Nick Lewycky0296a482011-01-15 18:14:21 +0000153 }
154
Rafael Espindola751677a2011-01-16 17:05:09 +0000155 // Second: identify all globals that can be merged together, filling in
156 // the Replacements vector. We cannot do the replacement in this pass
157 // because doing so may cause initializers of other globals to be rewritten,
158 // invalidating the Constant* pointers in CMap.
Nick Lewycky0296a482011-01-15 18:14:21 +0000159 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
160 GVI != E; ) {
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000161 GlobalVariable *GV = &*GVI++;
Nick Lewycky0296a482011-01-15 18:14:21 +0000162
163 // Only process constants with initializers in the default address space.
Nick Lewycky4a1ff162011-01-15 18:42:52 +0000164 if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
Nick Lewycky0296a482011-01-15 18:14:21 +0000165 GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
166 // Don't touch values marked with attribute(used).
167 UsedGlobals.count(GV))
168 continue;
169
Eli Friedmanb31c6272012-01-11 22:06:46 +0000170 // We can only replace constant with local linkage.
171 if (!GV->hasLocalLinkage())
Nick Lewycky0296a482011-01-15 18:14:21 +0000172 continue;
173
174 Constant *Init = GV->getInitializer();
175
176 // Check to see if the initializer is already known.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000177 GlobalVariable *Slot = CMap[Init];
Nick Lewycky0296a482011-01-15 18:14:21 +0000178
Rafael Espindola751677a2011-01-16 17:05:09 +0000179 if (!Slot || Slot == GV)
180 continue;
181
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000182 if (!Slot->hasGlobalUnnamedAddr() && !GV->hasGlobalUnnamedAddr())
Rafael Espindola751677a2011-01-16 17:05:09 +0000183 continue;
184
Evgeniy Stepanov8537d992017-03-09 00:03:37 +0000185 if (hasMetadataOtherThanDebugLoc(GV))
186 continue;
187
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000188 if (!GV->hasGlobalUnnamedAddr())
189 Slot->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
Rafael Espindola751677a2011-01-16 17:05:09 +0000190
191 // Make all uses of the duplicate constant use the canonical version.
192 Replacements.push_back(std::make_pair(GV, Slot));
Chris Lattner02137ee2007-04-14 01:11:54 +0000193 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000194
Chris Lattner56db5e92003-12-28 07:19:08 +0000195 if (Replacements.empty())
196 return MadeChange;
197 CMap.clear();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000198
Chris Lattner56db5e92003-12-28 07:19:08 +0000199 // Now that we have figured out which replacements must be made, do them all
200 // now. This avoid invalidating the pointers in CMap, which are unneeded
201 // now.
202 for (unsigned i = 0, e = Replacements.size(); i != e; ++i) {
Nick Lewycky8ac9ece2011-07-27 19:47:34 +0000203 // Bump the alignment if necessary.
204 if (Replacements[i].first->getAlignment() ||
205 Replacements[i].second->getAlignment()) {
Rafael Espindoladd8757a2013-11-12 20:21:43 +0000206 Replacements[i].second->setAlignment(
207 std::max(getAlignment(Replacements[i].first),
208 getAlignment(Replacements[i].second)));
Nick Lewycky8ac9ece2011-07-27 19:47:34 +0000209 }
210
Evgeniy Stepanov8537d992017-03-09 00:03:37 +0000211 copyDebugLocMetadata(Replacements[i].first, Replacements[i].second);
212
Chris Lattner75879be2010-02-12 18:17:23 +0000213 // Eliminate any uses of the dead global.
Chris Lattner56db5e92003-12-28 07:19:08 +0000214 Replacements[i].first->replaceAllUsesWith(Replacements[i].second);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000215
Chris Lattner75879be2010-02-12 18:17:23 +0000216 // Delete the global value from the module.
Nick Lewycky0296a482011-01-15 18:14:21 +0000217 assert(Replacements[i].first->hasLocalLinkage() &&
218 "Refusing to delete an externally visible global variable.");
Chris Lattner75879be2010-02-12 18:17:23 +0000219 Replacements[i].first->eraseFromParent();
Chris Lattner56db5e92003-12-28 07:19:08 +0000220 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000221
Chris Lattner56db5e92003-12-28 07:19:08 +0000222 NumMerged += Replacements.size();
223 Replacements.clear();
Chris Lattnerc2ee0542003-12-22 23:49:36 +0000224 }
Chris Lattnerc2ee0542003-12-22 23:49:36 +0000225}
Davide Italiano164b9bc2016-05-05 00:51:09 +0000226
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000227PreservedAnalyses ConstantMergePass::run(Module &M, ModuleAnalysisManager &) {
Davide Italiano164b9bc2016-05-05 00:51:09 +0000228 if (!mergeConstants(M))
229 return PreservedAnalyses::all();
230 return PreservedAnalyses::none();
231}
232
233namespace {
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +0000234
Davide Italiano164b9bc2016-05-05 00:51:09 +0000235struct ConstantMergeLegacyPass : public ModulePass {
236 static char ID; // Pass identification, replacement for typeid
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +0000237
Davide Italiano164b9bc2016-05-05 00:51:09 +0000238 ConstantMergeLegacyPass() : ModulePass(ID) {
239 initializeConstantMergeLegacyPassPass(*PassRegistry::getPassRegistry());
240 }
241
242 // For this pass, process all of the globals in the module, eliminating
243 // duplicate constants.
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +0000244 bool runOnModule(Module &M) override {
Davide Italiano164b9bc2016-05-05 00:51:09 +0000245 if (skipModule(M))
246 return false;
247 return mergeConstants(M);
248 }
249};
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +0000250
251} // end anonymous namespace
Davide Italiano164b9bc2016-05-05 00:51:09 +0000252
253char ConstantMergeLegacyPass::ID = 0;
Eugene Zelenkoe9ea08a2017-10-10 22:49:55 +0000254
Davide Italiano164b9bc2016-05-05 00:51:09 +0000255INITIALIZE_PASS(ConstantMergeLegacyPass, "constmerge",
256 "Merge Duplicate Global Constants", false, false)
257
258ModulePass *llvm::createConstantMergePass() {
259 return new ConstantMergeLegacyPass();
260}