1*9880d681SAndroid Build Coastguard Worker //===- ConstantMerge.cpp - Merge duplicate global constants ---------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file defines the interface to a pass that merges duplicate global
11*9880d681SAndroid Build Coastguard Worker // constants together into a single constant that is shared. This is useful
12*9880d681SAndroid Build Coastguard Worker // because some passes (ie TraceValues) insert a lot of string constants into
13*9880d681SAndroid Build Coastguard Worker // the program, regardless of whether or not an existing string is available.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker // Algorithm: ConstantMerge is designed to build up a map of available constants
16*9880d681SAndroid Build Coastguard Worker // and eliminate duplicates when it is initialized.
17*9880d681SAndroid Build Coastguard Worker //
18*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/IPO/ConstantMerge.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/DenseMap.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/PointerIntPair.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallPtrSet.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DerivedTypes.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Operator.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/IPO.h"
32*9880d681SAndroid Build Coastguard Worker using namespace llvm;
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "constmerge"
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Worker STATISTIC(NumMerged, "Number of global constants merged");
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker /// Find values that are marked as llvm.used.
FindUsedValues(GlobalVariable * LLVMUsed,SmallPtrSetImpl<const GlobalValue * > & UsedValues)39*9880d681SAndroid Build Coastguard Worker static void FindUsedValues(GlobalVariable *LLVMUsed,
40*9880d681SAndroid Build Coastguard Worker SmallPtrSetImpl<const GlobalValue*> &UsedValues) {
41*9880d681SAndroid Build Coastguard Worker if (!LLVMUsed) return;
42*9880d681SAndroid Build Coastguard Worker ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
45*9880d681SAndroid Build Coastguard Worker Value *Operand = Inits->getOperand(i)->stripPointerCastsNoFollowAliases();
46*9880d681SAndroid Build Coastguard Worker GlobalValue *GV = cast<GlobalValue>(Operand);
47*9880d681SAndroid Build Coastguard Worker UsedValues.insert(GV);
48*9880d681SAndroid Build Coastguard Worker }
49*9880d681SAndroid Build Coastguard Worker }
50*9880d681SAndroid Build Coastguard Worker
51*9880d681SAndroid Build Coastguard Worker // True if A is better than B.
IsBetterCanonical(const GlobalVariable & A,const GlobalVariable & B)52*9880d681SAndroid Build Coastguard Worker static bool IsBetterCanonical(const GlobalVariable &A,
53*9880d681SAndroid Build Coastguard Worker const GlobalVariable &B) {
54*9880d681SAndroid Build Coastguard Worker if (!A.hasLocalLinkage() && B.hasLocalLinkage())
55*9880d681SAndroid Build Coastguard Worker return true;
56*9880d681SAndroid Build Coastguard Worker
57*9880d681SAndroid Build Coastguard Worker if (A.hasLocalLinkage() && !B.hasLocalLinkage())
58*9880d681SAndroid Build Coastguard Worker return false;
59*9880d681SAndroid Build Coastguard Worker
60*9880d681SAndroid Build Coastguard Worker return A.hasGlobalUnnamedAddr();
61*9880d681SAndroid Build Coastguard Worker }
62*9880d681SAndroid Build Coastguard Worker
getAlignment(GlobalVariable * GV)63*9880d681SAndroid Build Coastguard Worker static unsigned getAlignment(GlobalVariable *GV) {
64*9880d681SAndroid Build Coastguard Worker unsigned Align = GV->getAlignment();
65*9880d681SAndroid Build Coastguard Worker if (Align)
66*9880d681SAndroid Build Coastguard Worker return Align;
67*9880d681SAndroid Build Coastguard Worker return GV->getParent()->getDataLayout().getPreferredAlignment(GV);
68*9880d681SAndroid Build Coastguard Worker }
69*9880d681SAndroid Build Coastguard Worker
mergeConstants(Module & M)70*9880d681SAndroid Build Coastguard Worker static bool mergeConstants(Module &M) {
71*9880d681SAndroid Build Coastguard Worker // Find all the globals that are marked "used". These cannot be merged.
72*9880d681SAndroid Build Coastguard Worker SmallPtrSet<const GlobalValue*, 8> UsedGlobals;
73*9880d681SAndroid Build Coastguard Worker FindUsedValues(M.getGlobalVariable("llvm.used"), UsedGlobals);
74*9880d681SAndroid Build Coastguard Worker FindUsedValues(M.getGlobalVariable("llvm.compiler.used"), UsedGlobals);
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Worker // Map unique constants to globals.
77*9880d681SAndroid Build Coastguard Worker DenseMap<Constant *, GlobalVariable *> CMap;
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard Worker // Replacements - This vector contains a list of replacements to perform.
80*9880d681SAndroid Build Coastguard Worker SmallVector<std::pair<GlobalVariable*, GlobalVariable*>, 32> Replacements;
81*9880d681SAndroid Build Coastguard Worker
82*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
83*9880d681SAndroid Build Coastguard Worker
84*9880d681SAndroid Build Coastguard Worker // Iterate constant merging while we are still making progress. Merging two
85*9880d681SAndroid Build Coastguard Worker // constants together may allow us to merge other constants together if the
86*9880d681SAndroid Build Coastguard Worker // second level constants have initializers which point to the globals that
87*9880d681SAndroid Build Coastguard Worker // were just merged.
88*9880d681SAndroid Build Coastguard Worker while (1) {
89*9880d681SAndroid Build Coastguard Worker
90*9880d681SAndroid Build Coastguard Worker // First: Find the canonical constants others will be merged with.
91*9880d681SAndroid Build Coastguard Worker for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
92*9880d681SAndroid Build Coastguard Worker GVI != E; ) {
93*9880d681SAndroid Build Coastguard Worker GlobalVariable *GV = &*GVI++;
94*9880d681SAndroid Build Coastguard Worker
95*9880d681SAndroid Build Coastguard Worker // If this GV is dead, remove it.
96*9880d681SAndroid Build Coastguard Worker GV->removeDeadConstantUsers();
97*9880d681SAndroid Build Coastguard Worker if (GV->use_empty() && GV->hasLocalLinkage()) {
98*9880d681SAndroid Build Coastguard Worker GV->eraseFromParent();
99*9880d681SAndroid Build Coastguard Worker continue;
100*9880d681SAndroid Build Coastguard Worker }
101*9880d681SAndroid Build Coastguard Worker
102*9880d681SAndroid Build Coastguard Worker // Only process constants with initializers in the default address space.
103*9880d681SAndroid Build Coastguard Worker if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
104*9880d681SAndroid Build Coastguard Worker GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
105*9880d681SAndroid Build Coastguard Worker // Don't touch values marked with attribute(used).
106*9880d681SAndroid Build Coastguard Worker UsedGlobals.count(GV))
107*9880d681SAndroid Build Coastguard Worker continue;
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard Worker // This transformation is legal for weak ODR globals in the sense it
110*9880d681SAndroid Build Coastguard Worker // doesn't change semantics, but we really don't want to perform it
111*9880d681SAndroid Build Coastguard Worker // anyway; it's likely to pessimize code generation, and some tools
112*9880d681SAndroid Build Coastguard Worker // (like the Darwin linker in cases involving CFString) don't expect it.
113*9880d681SAndroid Build Coastguard Worker if (GV->isWeakForLinker())
114*9880d681SAndroid Build Coastguard Worker continue;
115*9880d681SAndroid Build Coastguard Worker
116*9880d681SAndroid Build Coastguard Worker Constant *Init = GV->getInitializer();
117*9880d681SAndroid Build Coastguard Worker
118*9880d681SAndroid Build Coastguard Worker // Check to see if the initializer is already known.
119*9880d681SAndroid Build Coastguard Worker GlobalVariable *&Slot = CMap[Init];
120*9880d681SAndroid Build Coastguard Worker
121*9880d681SAndroid Build Coastguard Worker // If this is the first constant we find or if the old one is local,
122*9880d681SAndroid Build Coastguard Worker // replace with the current one. If the current is externally visible
123*9880d681SAndroid Build Coastguard Worker // it cannot be replace, but can be the canonical constant we merge with.
124*9880d681SAndroid Build Coastguard Worker if (!Slot || IsBetterCanonical(*GV, *Slot))
125*9880d681SAndroid Build Coastguard Worker Slot = GV;
126*9880d681SAndroid Build Coastguard Worker }
127*9880d681SAndroid Build Coastguard Worker
128*9880d681SAndroid Build Coastguard Worker // Second: identify all globals that can be merged together, filling in
129*9880d681SAndroid Build Coastguard Worker // the Replacements vector. We cannot do the replacement in this pass
130*9880d681SAndroid Build Coastguard Worker // because doing so may cause initializers of other globals to be rewritten,
131*9880d681SAndroid Build Coastguard Worker // invalidating the Constant* pointers in CMap.
132*9880d681SAndroid Build Coastguard Worker for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
133*9880d681SAndroid Build Coastguard Worker GVI != E; ) {
134*9880d681SAndroid Build Coastguard Worker GlobalVariable *GV = &*GVI++;
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker // Only process constants with initializers in the default address space.
137*9880d681SAndroid Build Coastguard Worker if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
138*9880d681SAndroid Build Coastguard Worker GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
139*9880d681SAndroid Build Coastguard Worker // Don't touch values marked with attribute(used).
140*9880d681SAndroid Build Coastguard Worker UsedGlobals.count(GV))
141*9880d681SAndroid Build Coastguard Worker continue;
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard Worker // We can only replace constant with local linkage.
144*9880d681SAndroid Build Coastguard Worker if (!GV->hasLocalLinkage())
145*9880d681SAndroid Build Coastguard Worker continue;
146*9880d681SAndroid Build Coastguard Worker
147*9880d681SAndroid Build Coastguard Worker Constant *Init = GV->getInitializer();
148*9880d681SAndroid Build Coastguard Worker
149*9880d681SAndroid Build Coastguard Worker // Check to see if the initializer is already known.
150*9880d681SAndroid Build Coastguard Worker GlobalVariable *Slot = CMap[Init];
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker if (!Slot || Slot == GV)
153*9880d681SAndroid Build Coastguard Worker continue;
154*9880d681SAndroid Build Coastguard Worker
155*9880d681SAndroid Build Coastguard Worker if (!Slot->hasGlobalUnnamedAddr() && !GV->hasGlobalUnnamedAddr())
156*9880d681SAndroid Build Coastguard Worker continue;
157*9880d681SAndroid Build Coastguard Worker
158*9880d681SAndroid Build Coastguard Worker if (!GV->hasGlobalUnnamedAddr())
159*9880d681SAndroid Build Coastguard Worker Slot->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
160*9880d681SAndroid Build Coastguard Worker
161*9880d681SAndroid Build Coastguard Worker // Make all uses of the duplicate constant use the canonical version.
162*9880d681SAndroid Build Coastguard Worker Replacements.push_back(std::make_pair(GV, Slot));
163*9880d681SAndroid Build Coastguard Worker }
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker if (Replacements.empty())
166*9880d681SAndroid Build Coastguard Worker return MadeChange;
167*9880d681SAndroid Build Coastguard Worker CMap.clear();
168*9880d681SAndroid Build Coastguard Worker
169*9880d681SAndroid Build Coastguard Worker // Now that we have figured out which replacements must be made, do them all
170*9880d681SAndroid Build Coastguard Worker // now. This avoid invalidating the pointers in CMap, which are unneeded
171*9880d681SAndroid Build Coastguard Worker // now.
172*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Replacements.size(); i != e; ++i) {
173*9880d681SAndroid Build Coastguard Worker // Bump the alignment if necessary.
174*9880d681SAndroid Build Coastguard Worker if (Replacements[i].first->getAlignment() ||
175*9880d681SAndroid Build Coastguard Worker Replacements[i].second->getAlignment()) {
176*9880d681SAndroid Build Coastguard Worker Replacements[i].second->setAlignment(
177*9880d681SAndroid Build Coastguard Worker std::max(getAlignment(Replacements[i].first),
178*9880d681SAndroid Build Coastguard Worker getAlignment(Replacements[i].second)));
179*9880d681SAndroid Build Coastguard Worker }
180*9880d681SAndroid Build Coastguard Worker
181*9880d681SAndroid Build Coastguard Worker // Eliminate any uses of the dead global.
182*9880d681SAndroid Build Coastguard Worker Replacements[i].first->replaceAllUsesWith(Replacements[i].second);
183*9880d681SAndroid Build Coastguard Worker
184*9880d681SAndroid Build Coastguard Worker // Delete the global value from the module.
185*9880d681SAndroid Build Coastguard Worker assert(Replacements[i].first->hasLocalLinkage() &&
186*9880d681SAndroid Build Coastguard Worker "Refusing to delete an externally visible global variable.");
187*9880d681SAndroid Build Coastguard Worker Replacements[i].first->eraseFromParent();
188*9880d681SAndroid Build Coastguard Worker }
189*9880d681SAndroid Build Coastguard Worker
190*9880d681SAndroid Build Coastguard Worker NumMerged += Replacements.size();
191*9880d681SAndroid Build Coastguard Worker Replacements.clear();
192*9880d681SAndroid Build Coastguard Worker }
193*9880d681SAndroid Build Coastguard Worker }
194*9880d681SAndroid Build Coastguard Worker
run(Module & M,ModuleAnalysisManager &)195*9880d681SAndroid Build Coastguard Worker PreservedAnalyses ConstantMergePass::run(Module &M, ModuleAnalysisManager &) {
196*9880d681SAndroid Build Coastguard Worker if (!mergeConstants(M))
197*9880d681SAndroid Build Coastguard Worker return PreservedAnalyses::all();
198*9880d681SAndroid Build Coastguard Worker return PreservedAnalyses::none();
199*9880d681SAndroid Build Coastguard Worker }
200*9880d681SAndroid Build Coastguard Worker
201*9880d681SAndroid Build Coastguard Worker namespace {
202*9880d681SAndroid Build Coastguard Worker struct ConstantMergeLegacyPass : public ModulePass {
203*9880d681SAndroid Build Coastguard Worker static char ID; // Pass identification, replacement for typeid
ConstantMergeLegacyPass__anon038749970111::ConstantMergeLegacyPass204*9880d681SAndroid Build Coastguard Worker ConstantMergeLegacyPass() : ModulePass(ID) {
205*9880d681SAndroid Build Coastguard Worker initializeConstantMergeLegacyPassPass(*PassRegistry::getPassRegistry());
206*9880d681SAndroid Build Coastguard Worker }
207*9880d681SAndroid Build Coastguard Worker
208*9880d681SAndroid Build Coastguard Worker // For this pass, process all of the globals in the module, eliminating
209*9880d681SAndroid Build Coastguard Worker // duplicate constants.
runOnModule__anon038749970111::ConstantMergeLegacyPass210*9880d681SAndroid Build Coastguard Worker bool runOnModule(Module &M) {
211*9880d681SAndroid Build Coastguard Worker if (skipModule(M))
212*9880d681SAndroid Build Coastguard Worker return false;
213*9880d681SAndroid Build Coastguard Worker return mergeConstants(M);
214*9880d681SAndroid Build Coastguard Worker }
215*9880d681SAndroid Build Coastguard Worker };
216*9880d681SAndroid Build Coastguard Worker }
217*9880d681SAndroid Build Coastguard Worker
218*9880d681SAndroid Build Coastguard Worker char ConstantMergeLegacyPass::ID = 0;
219*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(ConstantMergeLegacyPass, "constmerge",
220*9880d681SAndroid Build Coastguard Worker "Merge Duplicate Global Constants", false, false)
221*9880d681SAndroid Build Coastguard Worker
createConstantMergePass()222*9880d681SAndroid Build Coastguard Worker ModulePass *llvm::createConstantMergePass() {
223*9880d681SAndroid Build Coastguard Worker return new ConstantMergeLegacyPass();
224*9880d681SAndroid Build Coastguard Worker }
225