1f1933329SEugene Zelenko //===- GlobalMerge.cpp - Internal globals merging -------------------------===//
296e92c1dSJiangning Liu //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
696e92c1dSJiangning Liu //
796e92c1dSJiangning Liu //===----------------------------------------------------------------------===//
8f1933329SEugene Zelenko //
996e92c1dSJiangning Liu // This pass merges globals with internal linkage into one. This way all the
1096e92c1dSJiangning Liu // globals which were merged into a biggest one can be addressed using offsets
1196e92c1dSJiangning Liu // from the same base pointer (no need for separate base pointer for each of the
1296e92c1dSJiangning Liu // global). Such a transformation can significantly reduce the register pressure
1396e92c1dSJiangning Liu // when many globals are involved.
1496e92c1dSJiangning Liu //
1596e92c1dSJiangning Liu // For example, consider the code which touches several global variables at
1696e92c1dSJiangning Liu // once:
1796e92c1dSJiangning Liu //
1896e92c1dSJiangning Liu // static int foo[N], bar[N], baz[N];
1996e92c1dSJiangning Liu //
2096e92c1dSJiangning Liu // for (i = 0; i < N; ++i) {
2196e92c1dSJiangning Liu //    foo[i] = bar[i] * baz[i];
2296e92c1dSJiangning Liu // }
2396e92c1dSJiangning Liu //
2496e92c1dSJiangning Liu //  On ARM the addresses of 3 arrays should be kept in the registers, thus
2596e92c1dSJiangning Liu //  this code has quite large register pressure (loop body):
2696e92c1dSJiangning Liu //
2796e92c1dSJiangning Liu //  ldr     r1, [r5], #4
2896e92c1dSJiangning Liu //  ldr     r2, [r6], #4
2996e92c1dSJiangning Liu //  mul     r1, r2, r1
3096e92c1dSJiangning Liu //  str     r1, [r0], #4
3196e92c1dSJiangning Liu //
3296e92c1dSJiangning Liu //  Pass converts the code to something like:
3396e92c1dSJiangning Liu //
3496e92c1dSJiangning Liu //  static struct {
3596e92c1dSJiangning Liu //    int foo[N];
3696e92c1dSJiangning Liu //    int bar[N];
3796e92c1dSJiangning Liu //    int baz[N];
3896e92c1dSJiangning Liu //  } merged;
3996e92c1dSJiangning Liu //
4096e92c1dSJiangning Liu //  for (i = 0; i < N; ++i) {
4196e92c1dSJiangning Liu //    merged.foo[i] = merged.bar[i] * merged.baz[i];
4296e92c1dSJiangning Liu //  }
4396e92c1dSJiangning Liu //
4496e92c1dSJiangning Liu //  and in ARM code this becomes:
4596e92c1dSJiangning Liu //
4696e92c1dSJiangning Liu //  ldr     r0, [r5, #40]
4796e92c1dSJiangning Liu //  ldr     r1, [r5, #80]
4896e92c1dSJiangning Liu //  mul     r0, r1, r0
4996e92c1dSJiangning Liu //  str     r0, [r5], #4
5096e92c1dSJiangning Liu //
5196e92c1dSJiangning Liu //  note that we saved 2 registers here almostly "for free".
52279e3ee9SAhmed Bougacha //
53279e3ee9SAhmed Bougacha // However, merging globals can have tradeoffs:
54279e3ee9SAhmed Bougacha // - it confuses debuggers, tools, and users
55279e3ee9SAhmed Bougacha // - it makes linker optimizations less useful (order files, LOHs, ...)
56279e3ee9SAhmed Bougacha // - it forces usage of indexed addressing (which isn't necessarily "free")
57279e3ee9SAhmed Bougacha // - it can increase register pressure when the uses are disparate enough.
58279e3ee9SAhmed Bougacha //
59279e3ee9SAhmed Bougacha // We use heuristics to discover the best global grouping we can (cf cl::opts).
60f1933329SEugene Zelenko //
6196e92c1dSJiangning Liu // ===---------------------------------------------------------------------===//
6296e92c1dSJiangning Liu 
63f1933329SEugene Zelenko #include "llvm/ADT/BitVector.h"
64279e3ee9SAhmed Bougacha #include "llvm/ADT/DenseMap.h"
6596e92c1dSJiangning Liu #include "llvm/ADT/SmallPtrSet.h"
66f1933329SEugene Zelenko #include "llvm/ADT/SmallVector.h"
6796e92c1dSJiangning Liu #include "llvm/ADT/Statistic.h"
68f1933329SEugene Zelenko #include "llvm/ADT/StringRef.h"
69f1933329SEugene Zelenko #include "llvm/ADT/Triple.h"
70f1933329SEugene Zelenko #include "llvm/ADT/Twine.h"
71d9903888SChandler Carruth #include "llvm/CodeGen/Passes.h"
72f1933329SEugene Zelenko #include "llvm/IR/BasicBlock.h"
7396e92c1dSJiangning Liu #include "llvm/IR/Constants.h"
7496e92c1dSJiangning Liu #include "llvm/IR/DataLayout.h"
7596e92c1dSJiangning Liu #include "llvm/IR/DerivedTypes.h"
7696e92c1dSJiangning Liu #include "llvm/IR/Function.h"
77f1933329SEugene Zelenko #include "llvm/IR/GlobalAlias.h"
78f1933329SEugene Zelenko #include "llvm/IR/GlobalValue.h"
7996e92c1dSJiangning Liu #include "llvm/IR/GlobalVariable.h"
80f1933329SEugene Zelenko #include "llvm/IR/Instruction.h"
8196e92c1dSJiangning Liu #include "llvm/IR/Module.h"
82f1933329SEugene Zelenko #include "llvm/IR/Type.h"
83f1933329SEugene Zelenko #include "llvm/IR/Use.h"
84f1933329SEugene Zelenko #include "llvm/IR/User.h"
8505da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
86cdafe59fSSimon Pilgrim #include "llvm/MC/SectionKind.h"
8796e92c1dSJiangning Liu #include "llvm/Pass.h"
88f1933329SEugene Zelenko #include "llvm/Support/Casting.h"
8996e92c1dSJiangning Liu #include "llvm/Support/CommandLine.h"
90279e3ee9SAhmed Bougacha #include "llvm/Support/Debug.h"
91279e3ee9SAhmed Bougacha #include "llvm/Support/raw_ostream.h"
926054e650SDavid Blaikie #include "llvm/Target/TargetLoweringObjectFile.h"
93f1933329SEugene Zelenko #include "llvm/Target/TargetMachine.h"
94279e3ee9SAhmed Bougacha #include <algorithm>
95f1933329SEugene Zelenko #include <cassert>
96f1933329SEugene Zelenko #include <cstddef>
97b3bde2eaSDavid Blaikie #include <cstdint>
98f1933329SEugene Zelenko #include <string>
99f1933329SEugene Zelenko #include <vector>
100f1933329SEugene Zelenko 
10196e92c1dSJiangning Liu using namespace llvm;
10296e92c1dSJiangning Liu 
10396e92c1dSJiangning Liu #define DEBUG_TYPE "global-merge"
10496e92c1dSJiangning Liu 
105b96444efSAhmed Bougacha // FIXME: This is only useful as a last-resort way to disable the pass.
10696e92c1dSJiangning Liu static cl::opt<bool>
10796e92c1dSJiangning Liu EnableGlobalMerge("enable-global-merge", cl::Hidden,
108b96444efSAhmed Bougacha                   cl::desc("Enable the global merge pass"),
10996e92c1dSJiangning Liu                   cl::init(true));
11096e92c1dSJiangning Liu 
111fe12d0e3SPeter Collingbourne static cl::opt<unsigned>
112fe12d0e3SPeter Collingbourne GlobalMergeMaxOffset("global-merge-max-offset", cl::Hidden,
113fe12d0e3SPeter Collingbourne                      cl::desc("Set maximum offset for global merge pass"),
114fe12d0e3SPeter Collingbourne                      cl::init(0));
115fe12d0e3SPeter Collingbourne 
116279e3ee9SAhmed Bougacha static cl::opt<bool> GlobalMergeGroupByUse(
117279e3ee9SAhmed Bougacha     "global-merge-group-by-use", cl::Hidden,
118279e3ee9SAhmed Bougacha     cl::desc("Improve global merge pass to look at uses"), cl::init(true));
119279e3ee9SAhmed Bougacha 
120279e3ee9SAhmed Bougacha static cl::opt<bool> GlobalMergeIgnoreSingleUse(
121279e3ee9SAhmed Bougacha     "global-merge-ignore-single-use", cl::Hidden,
122279e3ee9SAhmed Bougacha     cl::desc("Improve global merge pass to ignore globals only used alone"),
123279e3ee9SAhmed Bougacha     cl::init(true));
124279e3ee9SAhmed Bougacha 
12596e92c1dSJiangning Liu static cl::opt<bool>
12696e92c1dSJiangning Liu EnableGlobalMergeOnConst("global-merge-on-const", cl::Hidden,
12796e92c1dSJiangning Liu                          cl::desc("Enable global merge pass on constants"),
12896e92c1dSJiangning Liu                          cl::init(false));
12996e92c1dSJiangning Liu 
13096e92c1dSJiangning Liu // FIXME: this could be a transitional option, and we probably need to remove
13196e92c1dSJiangning Liu // it if only we are sure this optimization could always benefit all targets.
1328b954241SJohn Brawn static cl::opt<cl::boolOrDefault>
13396e92c1dSJiangning Liu EnableGlobalMergeOnExternal("global-merge-on-external", cl::Hidden,
1348b954241SJohn Brawn      cl::desc("Enable global merge pass on external linkage"));
13596e92c1dSJiangning Liu 
13696e92c1dSJiangning Liu STATISTIC(NumMerged, "Number of globals merged");
137f1933329SEugene Zelenko 
13896e92c1dSJiangning Liu namespace {
139f1933329SEugene Zelenko 
14096e92c1dSJiangning Liu   class GlobalMerge : public FunctionPass {
141f1933329SEugene Zelenko     const TargetMachine *TM = nullptr;
142f1933329SEugene Zelenko 
143ed47b229SEric Christopher     // FIXME: Infer the maximum possible offset depending on the actual users
144ed47b229SEric Christopher     // (these max offsets are different for the users inside Thumb or ARM
145ed47b229SEric Christopher     // functions), see the code that passes in the offset in the ARM backend
146ed47b229SEric Christopher     // for more information.
147ed47b229SEric Christopher     unsigned MaxOffset;
14896e92c1dSJiangning Liu 
14982076412SAhmed Bougacha     /// Whether we should try to optimize for size only.
15082076412SAhmed Bougacha     /// Currently, this applies a dead simple heuristic: only consider globals
15182076412SAhmed Bougacha     /// used in minsize functions for merging.
15282076412SAhmed Bougacha     /// FIXME: This could learn about optsize, and be used in the cost model.
153f1933329SEugene Zelenko     bool OnlyOptimizeForSize = false;
15482076412SAhmed Bougacha 
1558b954241SJohn Brawn     /// Whether we should merge global variables that have external linkage.
156f1933329SEugene Zelenko     bool MergeExternalGlobals = false;
1578b954241SJohn Brawn 
158fe12d0e3SPeter Collingbourne     bool IsMachO;
159fe12d0e3SPeter Collingbourne 
16096e92c1dSJiangning Liu     bool doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
16196e92c1dSJiangning Liu                  Module &M, bool isConst, unsigned AddrSpace) const;
162f1933329SEugene Zelenko 
1635f8f34e4SAdrian Prantl     /// Merge everything in \p Globals for which the corresponding bit
164279e3ee9SAhmed Bougacha     /// in \p GlobalSet is set.
16547bf5c01SDavid Blaikie     bool doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
166279e3ee9SAhmed Bougacha                  const BitVector &GlobalSet, Module &M, bool isConst,
167279e3ee9SAhmed Bougacha                  unsigned AddrSpace) const;
16896e92c1dSJiangning Liu 
1695f8f34e4SAdrian Prantl     /// Check if the given variable has been identified as must keep
17096e92c1dSJiangning Liu     /// \pre setMustKeepGlobalVariables must have been called on the Module that
17196e92c1dSJiangning Liu     ///      contains GV
isMustKeepGlobalVariable(const GlobalVariable * GV) const17296e92c1dSJiangning Liu     bool isMustKeepGlobalVariable(const GlobalVariable *GV) const {
17396e92c1dSJiangning Liu       return MustKeepGlobalVariables.count(GV);
17496e92c1dSJiangning Liu     }
17596e92c1dSJiangning Liu 
17696e92c1dSJiangning Liu     /// Collect every variables marked as "used" or used in a landing pad
17796e92c1dSJiangning Liu     /// instruction for this Module.
17896e92c1dSJiangning Liu     void setMustKeepGlobalVariables(Module &M);
17996e92c1dSJiangning Liu 
18096e92c1dSJiangning Liu     /// Collect every variables marked as "used"
181d6baff65SEli Friedman     void collectUsedGlobalVariables(Module &M, StringRef Name);
18296e92c1dSJiangning Liu 
18396e92c1dSJiangning Liu     /// Keep track of the GlobalVariable that must not be merged away
18496e92c1dSJiangning Liu     SmallPtrSet<const GlobalVariable *, 16> MustKeepGlobalVariables;
18596e92c1dSJiangning Liu 
18696e92c1dSJiangning Liu   public:
18796e92c1dSJiangning Liu     static char ID;             // Pass identification, replacement for typeid.
188f1933329SEugene Zelenko 
GlobalMerge()189fe12d0e3SPeter Collingbourne     explicit GlobalMerge()
190f1933329SEugene Zelenko         : FunctionPass(ID), MaxOffset(GlobalMergeMaxOffset) {
191fe12d0e3SPeter Collingbourne       initializeGlobalMergePass(*PassRegistry::getPassRegistry());
192fe12d0e3SPeter Collingbourne     }
193fe12d0e3SPeter Collingbourne 
GlobalMerge(const TargetMachine * TM,unsigned MaximalOffset,bool OnlyOptimizeForSize,bool MergeExternalGlobals)194fe12d0e3SPeter Collingbourne     explicit GlobalMerge(const TargetMachine *TM, unsigned MaximalOffset,
195fe12d0e3SPeter Collingbourne                          bool OnlyOptimizeForSize, bool MergeExternalGlobals)
196f6727b0dSMehdi Amini         : FunctionPass(ID), TM(TM), MaxOffset(MaximalOffset),
1978b954241SJohn Brawn           OnlyOptimizeForSize(OnlyOptimizeForSize),
1988b954241SJohn Brawn           MergeExternalGlobals(MergeExternalGlobals) {
19996e92c1dSJiangning Liu       initializeGlobalMergePass(*PassRegistry::getPassRegistry());
20096e92c1dSJiangning Liu     }
20196e92c1dSJiangning Liu 
20296e92c1dSJiangning Liu     bool doInitialization(Module &M) override;
20396e92c1dSJiangning Liu     bool runOnFunction(Function &F) override;
20496e92c1dSJiangning Liu     bool doFinalization(Module &M) override;
20596e92c1dSJiangning Liu 
getPassName() const206117296c0SMehdi Amini     StringRef getPassName() const override { return "Merge internal globals"; }
20796e92c1dSJiangning Liu 
getAnalysisUsage(AnalysisUsage & AU) const20896e92c1dSJiangning Liu     void getAnalysisUsage(AnalysisUsage &AU) const override {
20996e92c1dSJiangning Liu       AU.setPreservesCFG();
21096e92c1dSJiangning Liu       FunctionPass::getAnalysisUsage(AU);
21196e92c1dSJiangning Liu     }
21296e92c1dSJiangning Liu   };
213f1933329SEugene Zelenko 
21496e92c1dSJiangning Liu } // end anonymous namespace
21596e92c1dSJiangning Liu 
21696e92c1dSJiangning Liu char GlobalMerge::ID = 0;
217f1933329SEugene Zelenko 
2181527baabSMatthias Braun INITIALIZE_PASS(GlobalMerge, DEBUG_TYPE, "Merge global variables", false, false)
21996e92c1dSJiangning Liu 
doMerge(SmallVectorImpl<GlobalVariable * > & Globals,Module & M,bool isConst,unsigned AddrSpace) const22096e92c1dSJiangning Liu bool GlobalMerge::doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
22196e92c1dSJiangning Liu                           Module &M, bool isConst, unsigned AddrSpace) const {
222f6727b0dSMehdi Amini   auto &DL = M.getDataLayout();
22396e92c1dSJiangning Liu   // FIXME: Find better heuristics
224efd94c56SFangrui Song   llvm::stable_sort(
225efd94c56SFangrui Song       Globals, [&DL](const GlobalVariable *GV1, const GlobalVariable *GV2) {
22615474d76SDavid Sherwood         // We don't support scalable global variables.
22715474d76SDavid Sherwood         return DL.getTypeAllocSize(GV1->getValueType()).getFixedSize() <
22815474d76SDavid Sherwood                DL.getTypeAllocSize(GV2->getValueType()).getFixedSize();
22996e92c1dSJiangning Liu       });
23096e92c1dSJiangning Liu 
231279e3ee9SAhmed Bougacha   // If we want to just blindly group all globals together, do so.
232279e3ee9SAhmed Bougacha   if (!GlobalMergeGroupByUse) {
233279e3ee9SAhmed Bougacha     BitVector AllGlobals(Globals.size());
234279e3ee9SAhmed Bougacha     AllGlobals.set();
235279e3ee9SAhmed Bougacha     return doMerge(Globals, AllGlobals, M, isConst, AddrSpace);
236279e3ee9SAhmed Bougacha   }
237279e3ee9SAhmed Bougacha 
238279e3ee9SAhmed Bougacha   // If we want to be smarter, look at all uses of each global, to try to
239279e3ee9SAhmed Bougacha   // discover all sets of globals used together, and how many times each of
240df005cbeSBenjamin Kramer   // these sets occurred.
241279e3ee9SAhmed Bougacha   //
242279e3ee9SAhmed Bougacha   // Keep this reasonably efficient, by having an append-only list of all sets
243279e3ee9SAhmed Bougacha   // discovered so far (UsedGlobalSet), and mapping each "together-ness" unit of
244279e3ee9SAhmed Bougacha   // code (currently, a Function) to the set of globals seen so far that are
245279e3ee9SAhmed Bougacha   // used together in that unit (GlobalUsesByFunction).
246279e3ee9SAhmed Bougacha   //
247b09308d8SHaicheng Wu   // When we look at the Nth global, we know that any new set is either:
248279e3ee9SAhmed Bougacha   // - the singleton set {N}, containing this global only, or
249279e3ee9SAhmed Bougacha   // - the union of {N} and a previously-discovered set, containing some
250279e3ee9SAhmed Bougacha   //   combination of the previous N-1 globals.
251279e3ee9SAhmed Bougacha   // Using that knowledge, when looking at the Nth global, we can keep:
252279e3ee9SAhmed Bougacha   // - a reference to the singleton set {N} (CurGVOnlySetIdx)
253279e3ee9SAhmed Bougacha   // - a list mapping each previous set to its union with {N} (EncounteredUGS),
254279e3ee9SAhmed Bougacha   //   if it actually occurs.
255279e3ee9SAhmed Bougacha 
256279e3ee9SAhmed Bougacha   // We keep track of the sets of globals used together "close enough".
257279e3ee9SAhmed Bougacha   struct UsedGlobalSet {
258279e3ee9SAhmed Bougacha     BitVector Globals;
259f1933329SEugene Zelenko     unsigned UsageCount = 1;
260f1933329SEugene Zelenko 
261f1933329SEugene Zelenko     UsedGlobalSet(size_t Size) : Globals(Size) {}
262279e3ee9SAhmed Bougacha   };
263279e3ee9SAhmed Bougacha 
264279e3ee9SAhmed Bougacha   // Each set is unique in UsedGlobalSets.
265279e3ee9SAhmed Bougacha   std::vector<UsedGlobalSet> UsedGlobalSets;
266279e3ee9SAhmed Bougacha 
267279e3ee9SAhmed Bougacha   // Avoid repeating the create-global-set pattern.
268279e3ee9SAhmed Bougacha   auto CreateGlobalSet = [&]() -> UsedGlobalSet & {
269279e3ee9SAhmed Bougacha     UsedGlobalSets.emplace_back(Globals.size());
270279e3ee9SAhmed Bougacha     return UsedGlobalSets.back();
271279e3ee9SAhmed Bougacha   };
272279e3ee9SAhmed Bougacha 
273279e3ee9SAhmed Bougacha   // The first set is the empty set.
274279e3ee9SAhmed Bougacha   CreateGlobalSet().UsageCount = 0;
275279e3ee9SAhmed Bougacha 
276279e3ee9SAhmed Bougacha   // We define "close enough" to be "in the same function".
277279e3ee9SAhmed Bougacha   // FIXME: Grouping uses by function is way too aggressive, so we should have
278279e3ee9SAhmed Bougacha   // a better metric for distance between uses.
279279e3ee9SAhmed Bougacha   // The obvious alternative would be to group by BasicBlock, but that's in
280279e3ee9SAhmed Bougacha   // turn too conservative..
281279e3ee9SAhmed Bougacha   // Anything in between wouldn't be trivial to compute, so just stick with
282279e3ee9SAhmed Bougacha   // per-function grouping.
283279e3ee9SAhmed Bougacha 
284279e3ee9SAhmed Bougacha   // The value type is an index into UsedGlobalSets.
285279e3ee9SAhmed Bougacha   // The default (0) conveniently points to the empty set.
286279e3ee9SAhmed Bougacha   DenseMap<Function *, size_t /*UsedGlobalSetIdx*/> GlobalUsesByFunction;
287279e3ee9SAhmed Bougacha 
288279e3ee9SAhmed Bougacha   // Now, look at each merge-eligible global in turn.
289279e3ee9SAhmed Bougacha 
290279e3ee9SAhmed Bougacha   // Keep track of the sets we already encountered to which we added the
291279e3ee9SAhmed Bougacha   // current global.
292279e3ee9SAhmed Bougacha   // Each element matches the same-index element in UsedGlobalSets.
293279e3ee9SAhmed Bougacha   // This lets us efficiently tell whether a set has already been expanded to
294279e3ee9SAhmed Bougacha   // include the current global.
295279e3ee9SAhmed Bougacha   std::vector<size_t> EncounteredUGS;
296279e3ee9SAhmed Bougacha 
297279e3ee9SAhmed Bougacha   for (size_t GI = 0, GE = Globals.size(); GI != GE; ++GI) {
298279e3ee9SAhmed Bougacha     GlobalVariable *GV = Globals[GI];
299279e3ee9SAhmed Bougacha 
300279e3ee9SAhmed Bougacha     // Reset the encountered sets for this global...
301279e3ee9SAhmed Bougacha     std::fill(EncounteredUGS.begin(), EncounteredUGS.end(), 0);
302279e3ee9SAhmed Bougacha     // ...and grow it in case we created new sets for the previous global.
303279e3ee9SAhmed Bougacha     EncounteredUGS.resize(UsedGlobalSets.size());
304279e3ee9SAhmed Bougacha 
305279e3ee9SAhmed Bougacha     // We might need to create a set that only consists of the current global.
306279e3ee9SAhmed Bougacha     // Keep track of its index into UsedGlobalSets.
307279e3ee9SAhmed Bougacha     size_t CurGVOnlySetIdx = 0;
308279e3ee9SAhmed Bougacha 
309279e3ee9SAhmed Bougacha     // For each global, look at all its Uses.
310279e3ee9SAhmed Bougacha     for (auto &U : GV->uses()) {
311279e3ee9SAhmed Bougacha       // This Use might be a ConstantExpr.  We're interested in Instruction
312279e3ee9SAhmed Bougacha       // users, so look through ConstantExpr...
313279e3ee9SAhmed Bougacha       Use *UI, *UE;
314279e3ee9SAhmed Bougacha       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) {
3158379e298SOliver Stannard         if (CE->use_empty())
3168379e298SOliver Stannard           continue;
317279e3ee9SAhmed Bougacha         UI = &*CE->use_begin();
318279e3ee9SAhmed Bougacha         UE = nullptr;
319279e3ee9SAhmed Bougacha       } else if (isa<Instruction>(U.getUser())) {
320279e3ee9SAhmed Bougacha         UI = &U;
321279e3ee9SAhmed Bougacha         UE = UI->getNext();
322279e3ee9SAhmed Bougacha       } else {
323279e3ee9SAhmed Bougacha         continue;
324279e3ee9SAhmed Bougacha       }
325279e3ee9SAhmed Bougacha 
326279e3ee9SAhmed Bougacha       // ...to iterate on all the instruction users of the global.
327279e3ee9SAhmed Bougacha       // Note that we iterate on Uses and not on Users to be able to getNext().
328279e3ee9SAhmed Bougacha       for (; UI != UE; UI = UI->getNext()) {
329279e3ee9SAhmed Bougacha         Instruction *I = dyn_cast<Instruction>(UI->getUser());
330279e3ee9SAhmed Bougacha         if (!I)
331279e3ee9SAhmed Bougacha           continue;
332279e3ee9SAhmed Bougacha 
333279e3ee9SAhmed Bougacha         Function *ParentFn = I->getParent()->getParent();
33482076412SAhmed Bougacha 
33582076412SAhmed Bougacha         // If we're only optimizing for size, ignore non-minsize functions.
33685bd3978SEvandro Menezes         if (OnlyOptimizeForSize && !ParentFn->hasMinSize())
33782076412SAhmed Bougacha           continue;
33882076412SAhmed Bougacha 
339279e3ee9SAhmed Bougacha         size_t UGSIdx = GlobalUsesByFunction[ParentFn];
340279e3ee9SAhmed Bougacha 
341279e3ee9SAhmed Bougacha         // If this is the first global the basic block uses, map it to the set
342279e3ee9SAhmed Bougacha         // consisting of this global only.
343279e3ee9SAhmed Bougacha         if (!UGSIdx) {
344279e3ee9SAhmed Bougacha           // If that set doesn't exist yet, create it.
345279e3ee9SAhmed Bougacha           if (!CurGVOnlySetIdx) {
346279e3ee9SAhmed Bougacha             CurGVOnlySetIdx = UsedGlobalSets.size();
347279e3ee9SAhmed Bougacha             CreateGlobalSet().Globals.set(GI);
348279e3ee9SAhmed Bougacha           } else {
349279e3ee9SAhmed Bougacha             ++UsedGlobalSets[CurGVOnlySetIdx].UsageCount;
350279e3ee9SAhmed Bougacha           }
351279e3ee9SAhmed Bougacha 
352279e3ee9SAhmed Bougacha           GlobalUsesByFunction[ParentFn] = CurGVOnlySetIdx;
353279e3ee9SAhmed Bougacha           continue;
354279e3ee9SAhmed Bougacha         }
355279e3ee9SAhmed Bougacha 
356279e3ee9SAhmed Bougacha         // If we already encountered this BB, just increment the counter.
357279e3ee9SAhmed Bougacha         if (UsedGlobalSets[UGSIdx].Globals.test(GI)) {
358279e3ee9SAhmed Bougacha           ++UsedGlobalSets[UGSIdx].UsageCount;
359279e3ee9SAhmed Bougacha           continue;
360279e3ee9SAhmed Bougacha         }
361279e3ee9SAhmed Bougacha 
362279e3ee9SAhmed Bougacha         // If not, the previous set wasn't actually used in this function.
363279e3ee9SAhmed Bougacha         --UsedGlobalSets[UGSIdx].UsageCount;
364279e3ee9SAhmed Bougacha 
365279e3ee9SAhmed Bougacha         // If we already expanded the previous set to include this global, just
366279e3ee9SAhmed Bougacha         // reuse that expanded set.
367279e3ee9SAhmed Bougacha         if (size_t ExpandedIdx = EncounteredUGS[UGSIdx]) {
368279e3ee9SAhmed Bougacha           ++UsedGlobalSets[ExpandedIdx].UsageCount;
369279e3ee9SAhmed Bougacha           GlobalUsesByFunction[ParentFn] = ExpandedIdx;
370279e3ee9SAhmed Bougacha           continue;
371279e3ee9SAhmed Bougacha         }
372279e3ee9SAhmed Bougacha 
373279e3ee9SAhmed Bougacha         // If not, create a new set consisting of the union of the previous set
374279e3ee9SAhmed Bougacha         // and this global.  Mark it as encountered, so we can reuse it later.
375279e3ee9SAhmed Bougacha         GlobalUsesByFunction[ParentFn] = EncounteredUGS[UGSIdx] =
376279e3ee9SAhmed Bougacha             UsedGlobalSets.size();
377279e3ee9SAhmed Bougacha 
378279e3ee9SAhmed Bougacha         UsedGlobalSet &NewUGS = CreateGlobalSet();
379279e3ee9SAhmed Bougacha         NewUGS.Globals.set(GI);
380279e3ee9SAhmed Bougacha         NewUGS.Globals |= UsedGlobalSets[UGSIdx].Globals;
381279e3ee9SAhmed Bougacha       }
382279e3ee9SAhmed Bougacha     }
383279e3ee9SAhmed Bougacha   }
384279e3ee9SAhmed Bougacha 
385279e3ee9SAhmed Bougacha   // Now we found a bunch of sets of globals used together.  We accumulated
386279e3ee9SAhmed Bougacha   // the number of times we encountered the sets (i.e., the number of blocks
387279e3ee9SAhmed Bougacha   // that use that exact set of globals).
388279e3ee9SAhmed Bougacha   //
389279e3ee9SAhmed Bougacha   // Multiply that by the size of the set to give us a crude profitability
390279e3ee9SAhmed Bougacha   // metric.
391efd94c56SFangrui Song   llvm::stable_sort(UsedGlobalSets,
392279e3ee9SAhmed Bougacha                     [](const UsedGlobalSet &UGS1, const UsedGlobalSet &UGS2) {
393279e3ee9SAhmed Bougacha                       return UGS1.Globals.count() * UGS1.UsageCount <
394279e3ee9SAhmed Bougacha                              UGS2.Globals.count() * UGS2.UsageCount;
395279e3ee9SAhmed Bougacha                     });
396279e3ee9SAhmed Bougacha 
397279e3ee9SAhmed Bougacha   // We can choose to merge all globals together, but ignore globals never used
398279e3ee9SAhmed Bougacha   // with another global.  This catches the obviously non-profitable cases of
399279e3ee9SAhmed Bougacha   // having a single global, but is aggressive enough for any other case.
400279e3ee9SAhmed Bougacha   if (GlobalMergeIgnoreSingleUse) {
401279e3ee9SAhmed Bougacha     BitVector AllGlobals(Globals.size());
402fd7d4064SKazu Hirata     for (const UsedGlobalSet &UGS : llvm::reverse(UsedGlobalSets)) {
403279e3ee9SAhmed Bougacha       if (UGS.UsageCount == 0)
404279e3ee9SAhmed Bougacha         continue;
405279e3ee9SAhmed Bougacha       if (UGS.Globals.count() > 1)
406279e3ee9SAhmed Bougacha         AllGlobals |= UGS.Globals;
407279e3ee9SAhmed Bougacha     }
408279e3ee9SAhmed Bougacha     return doMerge(Globals, AllGlobals, M, isConst, AddrSpace);
409279e3ee9SAhmed Bougacha   }
410279e3ee9SAhmed Bougacha 
411279e3ee9SAhmed Bougacha   // Starting from the sets with the best (=biggest) profitability, find a
412279e3ee9SAhmed Bougacha   // good combination.
413279e3ee9SAhmed Bougacha   // The ideal (and expensive) solution can only be found by trying all
414279e3ee9SAhmed Bougacha   // combinations, looking for the one with the best profitability.
415279e3ee9SAhmed Bougacha   // Don't be smart about it, and just pick the first compatible combination,
416279e3ee9SAhmed Bougacha   // starting with the sets with the best profitability.
417279e3ee9SAhmed Bougacha   BitVector PickedGlobals(Globals.size());
418279e3ee9SAhmed Bougacha   bool Changed = false;
419279e3ee9SAhmed Bougacha 
420fd7d4064SKazu Hirata   for (const UsedGlobalSet &UGS : llvm::reverse(UsedGlobalSets)) {
421279e3ee9SAhmed Bougacha     if (UGS.UsageCount == 0)
422279e3ee9SAhmed Bougacha       continue;
423279e3ee9SAhmed Bougacha     if (PickedGlobals.anyCommon(UGS.Globals))
424279e3ee9SAhmed Bougacha       continue;
425279e3ee9SAhmed Bougacha     PickedGlobals |= UGS.Globals;
426279e3ee9SAhmed Bougacha     // If the set only contains one global, there's no point in merging.
427279e3ee9SAhmed Bougacha     // Ignore the global for inclusion in other sets though, so keep it in
428279e3ee9SAhmed Bougacha     // PickedGlobals.
429279e3ee9SAhmed Bougacha     if (UGS.Globals.count() < 2)
430279e3ee9SAhmed Bougacha       continue;
431279e3ee9SAhmed Bougacha     Changed |= doMerge(Globals, UGS.Globals, M, isConst, AddrSpace);
432279e3ee9SAhmed Bougacha   }
433279e3ee9SAhmed Bougacha 
434279e3ee9SAhmed Bougacha   return Changed;
435279e3ee9SAhmed Bougacha }
436279e3ee9SAhmed Bougacha 
doMerge(const SmallVectorImpl<GlobalVariable * > & Globals,const BitVector & GlobalSet,Module & M,bool isConst,unsigned AddrSpace) const43747bf5c01SDavid Blaikie bool GlobalMerge::doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
438279e3ee9SAhmed Bougacha                           const BitVector &GlobalSet, Module &M, bool isConst,
439279e3ee9SAhmed Bougacha                           unsigned AddrSpace) const {
44047bf5c01SDavid Blaikie   assert(Globals.size() > 1);
441279e3ee9SAhmed Bougacha 
44296e92c1dSJiangning Liu   Type *Int32Ty = Type::getInt32Ty(M.getContext());
4430887cf9cSEli Friedman   Type *Int8Ty = Type::getInt8Ty(M.getContext());
444f6727b0dSMehdi Amini   auto &DL = M.getDataLayout();
44596e92c1dSJiangning Liu 
446d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << " Trying to merge set, starts with #"
447279e3ee9SAhmed Bougacha                     << GlobalSet.find_first() << "\n");
448279e3ee9SAhmed Bougacha 
44969ba0613SHaicheng Wu   bool Changed = false;
450279e3ee9SAhmed Bougacha   ssize_t i = GlobalSet.find_first();
451279e3ee9SAhmed Bougacha   while (i != -1) {
452279e3ee9SAhmed Bougacha     ssize_t j = 0;
45396e92c1dSJiangning Liu     uint64_t MergedSize = 0;
45496e92c1dSJiangning Liu     std::vector<Type*> Tys;
45596e92c1dSJiangning Liu     std::vector<Constant*> Inits;
4560887cf9cSEli Friedman     std::vector<unsigned> StructIdxs;
45796e92c1dSJiangning Liu 
458554fd99dSAdrian Prantl     bool HasExternal = false;
459622bddb6SAdrian Prantl     StringRef FirstExternalName;
4600e62011dSGuillaume Chatelet     Align MaxAlign;
4610887cf9cSEli Friedman     unsigned CurIdx = 0;
462279e3ee9SAhmed Bougacha     for (j = i; j != -1; j = GlobalSet.find_next(j)) {
4639ed57a9eSDavid Blaikie       Type *Ty = Globals[j]->getValueType();
4641ba5e9acSEli Friedman 
46537696393SEli Friedman       // Make sure we use the same alignment AsmPrinter would use.
466368a5e3aSGuillaume Chatelet       Align Alignment = DL.getPreferredAlign(Globals[j]);
4670e62011dSGuillaume Chatelet       unsigned Padding = alignTo(MergedSize, Alignment) - MergedSize;
4680887cf9cSEli Friedman       MergedSize += Padding;
469f6727b0dSMehdi Amini       MergedSize += DL.getTypeAllocSize(Ty);
47096e92c1dSJiangning Liu       if (MergedSize > MaxOffset) {
47196e92c1dSJiangning Liu         break;
47296e92c1dSJiangning Liu       }
4730887cf9cSEli Friedman       if (Padding) {
4740887cf9cSEli Friedman         Tys.push_back(ArrayType::get(Int8Ty, Padding));
4750887cf9cSEli Friedman         Inits.push_back(ConstantAggregateZero::get(Tys.back()));
4760887cf9cSEli Friedman         ++CurIdx;
4770887cf9cSEli Friedman       }
47896e92c1dSJiangning Liu       Tys.push_back(Ty);
47996e92c1dSJiangning Liu       Inits.push_back(Globals[j]->getInitializer());
4800887cf9cSEli Friedman       StructIdxs.push_back(CurIdx++);
4810887cf9cSEli Friedman 
4820e62011dSGuillaume Chatelet       MaxAlign = std::max(MaxAlign, Alignment);
483554fd99dSAdrian Prantl 
484554fd99dSAdrian Prantl       if (Globals[j]->hasExternalLinkage() && !HasExternal) {
485554fd99dSAdrian Prantl         HasExternal = true;
486622bddb6SAdrian Prantl         FirstExternalName = Globals[j]->getName();
487554fd99dSAdrian Prantl       }
48896e92c1dSJiangning Liu     }
48996e92c1dSJiangning Liu 
49069ba0613SHaicheng Wu     // Exit early if there is only one global to merge.
49169ba0613SHaicheng Wu     if (Tys.size() < 2) {
49269ba0613SHaicheng Wu       i = j;
49369ba0613SHaicheng Wu       continue;
49469ba0613SHaicheng Wu     }
49569ba0613SHaicheng Wu 
496554fd99dSAdrian Prantl     // If merged variables doesn't have external linkage, we needn't to expose
497554fd99dSAdrian Prantl     // the symbol after merging.
498554fd99dSAdrian Prantl     GlobalValue::LinkageTypes Linkage = HasExternal
499554fd99dSAdrian Prantl                                             ? GlobalValue::ExternalLinkage
500554fd99dSAdrian Prantl                                             : GlobalValue::InternalLinkage;
5010887cf9cSEli Friedman     // Use a packed struct so we can control alignment.
5020887cf9cSEli Friedman     StructType *MergedTy = StructType::get(M.getContext(), Tys, true);
50396e92c1dSJiangning Liu     Constant *MergedInit = ConstantStruct::get(MergedTy, Inits);
50496e92c1dSJiangning Liu 
5056cb849e2SAdrian Prantl     // On Darwin external linkage needs to be preserved, otherwise
5066cb849e2SAdrian Prantl     // dsymutil cannot preserve the debug info for the merged
5076cb849e2SAdrian Prantl     // variables.  If they have external linkage, use the symbol name
5086cb849e2SAdrian Prantl     // of the first variable merged as the suffix of global symbol
5096cb849e2SAdrian Prantl     // name.  This avoids a link-time naming conflict for the
5106cb849e2SAdrian Prantl     // _MergedGlobals symbols.
511554fd99dSAdrian Prantl     Twine MergedName =
512554fd99dSAdrian Prantl         (IsMachO && HasExternal)
513622bddb6SAdrian Prantl             ? "_MergedGlobals_" + FirstExternalName
514554fd99dSAdrian Prantl             : "_MergedGlobals";
515554fd99dSAdrian Prantl     auto MergedLinkage = IsMachO ? Linkage : GlobalValue::PrivateLinkage;
516554fd99dSAdrian Prantl     auto *MergedGV = new GlobalVariable(
517554fd99dSAdrian Prantl         M, MergedTy, isConst, MergedLinkage, MergedInit, MergedName, nullptr,
518554fd99dSAdrian Prantl         GlobalVariable::NotThreadLocal, AddrSpace);
51996e92c1dSJiangning Liu 
5200887cf9cSEli Friedman     MergedGV->setAlignment(MaxAlign);
5211ba5e9acSEli Friedman     MergedGV->setSection(Globals[i]->getSection());
522d4135bbcSPeter Collingbourne 
5230887cf9cSEli Friedman     const StructLayout *MergedLayout = DL.getStructLayout(MergedTy);
5246614d8d2SDavid Blaikie     for (ssize_t k = i, idx = 0; k != j; k = GlobalSet.find_next(k), ++idx) {
52596e92c1dSJiangning Liu       GlobalValue::LinkageTypes Linkage = Globals[k]->getLinkage();
526adcd0268SBenjamin Kramer       std::string Name(Globals[k]->getName());
527a2fb2c0dSMichael Spang       GlobalValue::VisibilityTypes Visibility = Globals[k]->getVisibility();
5289ca8b571SMartin Storsjo       GlobalValue::DLLStorageClassTypes DLLStorage =
5299ca8b571SMartin Storsjo           Globals[k]->getDLLStorageClass();
53096e92c1dSJiangning Liu 
531d4135bbcSPeter Collingbourne       // Copy metadata while adjusting any debug info metadata by the original
532d4135bbcSPeter Collingbourne       // global's offset within the merged global.
5330887cf9cSEli Friedman       MergedGV->copyMetadata(Globals[k],
5340887cf9cSEli Friedman                              MergedLayout->getElementOffset(StructIdxs[idx]));
535d4135bbcSPeter Collingbourne 
53696e92c1dSJiangning Liu       Constant *Idx[2] = {
53796e92c1dSJiangning Liu           ConstantInt::get(Int32Ty, 0),
5380887cf9cSEli Friedman           ConstantInt::get(Int32Ty, StructIdxs[idx]),
53996e92c1dSJiangning Liu       };
5404a2e73b0SDavid Blaikie       Constant *GEP =
5414a2e73b0SDavid Blaikie           ConstantExpr::getInBoundsGetElementPtr(MergedTy, MergedGV, Idx);
54296e92c1dSJiangning Liu       Globals[k]->replaceAllUsesWith(GEP);
54396e92c1dSJiangning Liu       Globals[k]->eraseFromParent();
54496e92c1dSJiangning Liu 
5450bef27d8SJohn Brawn       // When the linkage is not internal we must emit an alias for the original
5460bef27d8SJohn Brawn       // variable name as it may be accessed from another object. On non-Mach-O
5470bef27d8SJohn Brawn       // we can also emit an alias for internal linkage as it's safe to do so.
5480bef27d8SJohn Brawn       // It's not safe on Mach-O as the alias (and thus the portion of the
5490bef27d8SJohn Brawn       // MergedGlobals variable) may be dead stripped at link time.
550fe12d0e3SPeter Collingbourne       if (Linkage != GlobalValue::InternalLinkage || !IsMachO) {
5510887cf9cSEli Friedman         GlobalAlias *GA = GlobalAlias::create(Tys[StructIdxs[idx]], AddrSpace,
5520887cf9cSEli Friedman                                               Linkage, Name, GEP, &M);
553a2fb2c0dSMichael Spang         GA->setVisibility(Visibility);
5549ca8b571SMartin Storsjo         GA->setDLLStorageClass(DLLStorage);
5550bef27d8SJohn Brawn       }
55696e92c1dSJiangning Liu 
55796e92c1dSJiangning Liu       NumMerged++;
55896e92c1dSJiangning Liu     }
55969ba0613SHaicheng Wu     Changed = true;
56096e92c1dSJiangning Liu     i = j;
56196e92c1dSJiangning Liu   }
56296e92c1dSJiangning Liu 
56369ba0613SHaicheng Wu   return Changed;
56496e92c1dSJiangning Liu }
56596e92c1dSJiangning Liu 
collectUsedGlobalVariables(Module & M,StringRef Name)566d6baff65SEli Friedman void GlobalMerge::collectUsedGlobalVariables(Module &M, StringRef Name) {
56796e92c1dSJiangning Liu   // Extract global variables from llvm.used array
568d6baff65SEli Friedman   const GlobalVariable *GV = M.getGlobalVariable(Name);
56996e92c1dSJiangning Liu   if (!GV || !GV->hasInitializer()) return;
57096e92c1dSJiangning Liu 
57196e92c1dSJiangning Liu   // Should be an array of 'i8*'.
57296e92c1dSJiangning Liu   const ConstantArray *InitList = cast<ConstantArray>(GV->getInitializer());
57396e92c1dSJiangning Liu 
57496e92c1dSJiangning Liu   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
57596e92c1dSJiangning Liu     if (const GlobalVariable *G =
57696e92c1dSJiangning Liu         dyn_cast<GlobalVariable>(InitList->getOperand(i)->stripPointerCasts()))
57796e92c1dSJiangning Liu       MustKeepGlobalVariables.insert(G);
57896e92c1dSJiangning Liu }
57996e92c1dSJiangning Liu 
setMustKeepGlobalVariables(Module & M)58096e92c1dSJiangning Liu void GlobalMerge::setMustKeepGlobalVariables(Module &M) {
581d6baff65SEli Friedman   collectUsedGlobalVariables(M, "llvm.used");
582d6baff65SEli Friedman   collectUsedGlobalVariables(M, "llvm.compiler.used");
58396e92c1dSJiangning Liu 
584f8d1d12fSReid Kleckner   for (Function &F : M) {
585f8d1d12fSReid Kleckner     for (BasicBlock &BB : F) {
586f8d1d12fSReid Kleckner       Instruction *Pad = BB.getFirstNonPHI();
587f8d1d12fSReid Kleckner       if (!Pad->isEHPad())
588f8d1d12fSReid Kleckner         continue;
58996e92c1dSJiangning Liu 
590f8d1d12fSReid Kleckner       // Keep globals used by landingpads and catchpads.
591f8d1d12fSReid Kleckner       for (const Use &U : Pad->operands()) {
59296e92c1dSJiangning Liu         if (const GlobalVariable *GV =
593f8d1d12fSReid Kleckner                 dyn_cast<GlobalVariable>(U->stripPointerCasts()))
59496e92c1dSJiangning Liu           MustKeepGlobalVariables.insert(GV);
595*e50a8c84SStefan Pintilie         else if (const ConstantArray *CA = dyn_cast<ConstantArray>(U->stripPointerCasts())) {
596*e50a8c84SStefan Pintilie           for (const Use &Elt : CA->operands()) {
597*e50a8c84SStefan Pintilie             if (const GlobalVariable *GV =
598*e50a8c84SStefan Pintilie                     dyn_cast<GlobalVariable>(Elt->stripPointerCasts()))
599*e50a8c84SStefan Pintilie               MustKeepGlobalVariables.insert(GV);
600*e50a8c84SStefan Pintilie           }
601*e50a8c84SStefan Pintilie         }
60296e92c1dSJiangning Liu       }
60396e92c1dSJiangning Liu     }
60496e92c1dSJiangning Liu   }
605f8d1d12fSReid Kleckner }
60696e92c1dSJiangning Liu 
doInitialization(Module & M)60796e92c1dSJiangning Liu bool GlobalMerge::doInitialization(Module &M) {
60896e92c1dSJiangning Liu   if (!EnableGlobalMerge)
60996e92c1dSJiangning Liu     return false;
61096e92c1dSJiangning Liu 
611fe12d0e3SPeter Collingbourne   IsMachO = Triple(M.getTargetTriple()).isOSBinFormatMachO();
612fe12d0e3SPeter Collingbourne 
613f6727b0dSMehdi Amini   auto &DL = M.getDataLayout();
6141ba5e9acSEli Friedman   DenseMap<std::pair<unsigned, StringRef>, SmallVector<GlobalVariable *, 16>>
6151ba5e9acSEli Friedman       Globals, ConstGlobals, BSSGlobals;
61696e92c1dSJiangning Liu   bool Changed = false;
61796e92c1dSJiangning Liu   setMustKeepGlobalVariables(M);
61896e92c1dSJiangning Liu 
619*e50a8c84SStefan Pintilie   LLVM_DEBUG({
620*e50a8c84SStefan Pintilie       dbgs() << "Number of GV that must be kept:  " <<
621*e50a8c84SStefan Pintilie                 MustKeepGlobalVariables.size() << "\n";
622*e50a8c84SStefan Pintilie       for (auto KeptGV = MustKeepGlobalVariables.begin();
623*e50a8c84SStefan Pintilie            KeptGV != MustKeepGlobalVariables.end(); KeptGV++)
624*e50a8c84SStefan Pintilie         dbgs() << "Kept: " << **KeptGV << "\n";
625*e50a8c84SStefan Pintilie   });
62696e92c1dSJiangning Liu   // Grab all non-const globals.
627530d040bSDuncan P. N. Exon Smith   for (auto &GV : M.globals()) {
62896e92c1dSJiangning Liu     // Merge is safe for "normal" internal or external globals only
6291ba5e9acSEli Friedman     if (GV.isDeclaration() || GV.isThreadLocal() || GV.hasImplicitSection())
63096e92c1dSJiangning Liu       continue;
63196e92c1dSJiangning Liu 
6326671616cSJohn Brawn     // It's not safe to merge globals that may be preempted
6336671616cSJohn Brawn     if (TM && !TM->shouldAssumeDSOLocal(M, &GV))
6346671616cSJohn Brawn       continue;
6356671616cSJohn Brawn 
636530d040bSDuncan P. N. Exon Smith     if (!(MergeExternalGlobals && GV.hasExternalLinkage()) &&
637530d040bSDuncan P. N. Exon Smith         !GV.hasInternalLinkage())
63896e92c1dSJiangning Liu       continue;
63996e92c1dSJiangning Liu 
640530d040bSDuncan P. N. Exon Smith     PointerType *PT = dyn_cast<PointerType>(GV.getType());
64196e92c1dSJiangning Liu     assert(PT && "Global variable is not a pointer!");
64296e92c1dSJiangning Liu 
64396e92c1dSJiangning Liu     unsigned AddressSpace = PT->getAddressSpace();
6441ba5e9acSEli Friedman     StringRef Section = GV.getSection();
64596e92c1dSJiangning Liu 
64696e92c1dSJiangning Liu     // Ignore all 'special' globals.
647530d040bSDuncan P. N. Exon Smith     if (GV.getName().startswith("llvm.") ||
648530d040bSDuncan P. N. Exon Smith         GV.getName().startswith(".llvm."))
64996e92c1dSJiangning Liu       continue;
65096e92c1dSJiangning Liu 
65196e92c1dSJiangning Liu     // Ignore all "required" globals:
652530d040bSDuncan P. N. Exon Smith     if (isMustKeepGlobalVariable(&GV))
65396e92c1dSJiangning Liu       continue;
65496e92c1dSJiangning Liu 
6550887cf9cSEli Friedman     Type *Ty = GV.getValueType();
656f6727b0dSMehdi Amini     if (DL.getTypeAllocSize(Ty) < MaxOffset) {
657fe12d0e3SPeter Collingbourne       if (TM &&
6582f410659SHuihui Zhang           TargetLoweringObjectFile::getKindForGlobal(&GV, *TM).isBSS())
6591ba5e9acSEli Friedman         BSSGlobals[{AddressSpace, Section}].push_back(&GV);
660530d040bSDuncan P. N. Exon Smith       else if (GV.isConstant())
6611ba5e9acSEli Friedman         ConstGlobals[{AddressSpace, Section}].push_back(&GV);
66296e92c1dSJiangning Liu       else
6631ba5e9acSEli Friedman         Globals[{AddressSpace, Section}].push_back(&GV);
66496e92c1dSJiangning Liu     }
66596e92c1dSJiangning Liu   }
66696e92c1dSJiangning Liu 
66747bf5c01SDavid Blaikie   for (auto &P : Globals)
66847bf5c01SDavid Blaikie     if (P.second.size() > 1)
6691ba5e9acSEli Friedman       Changed |= doMerge(P.second, M, false, P.first.first);
67096e92c1dSJiangning Liu 
67147bf5c01SDavid Blaikie   for (auto &P : BSSGlobals)
67247bf5c01SDavid Blaikie     if (P.second.size() > 1)
6731ba5e9acSEli Friedman       Changed |= doMerge(P.second, M, false, P.first.first);
67496e92c1dSJiangning Liu 
675d4860003SDavid Blaikie   if (EnableGlobalMergeOnConst)
67647bf5c01SDavid Blaikie     for (auto &P : ConstGlobals)
67747bf5c01SDavid Blaikie       if (P.second.size() > 1)
6781ba5e9acSEli Friedman         Changed |= doMerge(P.second, M, true, P.first.first);
67996e92c1dSJiangning Liu 
68096e92c1dSJiangning Liu   return Changed;
68196e92c1dSJiangning Liu }
68296e92c1dSJiangning Liu 
runOnFunction(Function & F)68396e92c1dSJiangning Liu bool GlobalMerge::runOnFunction(Function &F) {
68496e92c1dSJiangning Liu   return false;
68596e92c1dSJiangning Liu }
68696e92c1dSJiangning Liu 
doFinalization(Module & M)68796e92c1dSJiangning Liu bool GlobalMerge::doFinalization(Module &M) {
68896e92c1dSJiangning Liu   MustKeepGlobalVariables.clear();
68996e92c1dSJiangning Liu   return false;
69096e92c1dSJiangning Liu }
69196e92c1dSJiangning Liu 
createGlobalMergePass(const TargetMachine * TM,unsigned Offset,bool OnlyOptimizeForSize,bool MergeExternalByDefault)69282076412SAhmed Bougacha Pass *llvm::createGlobalMergePass(const TargetMachine *TM, unsigned Offset,
6938b954241SJohn Brawn                                   bool OnlyOptimizeForSize,
6948b954241SJohn Brawn                                   bool MergeExternalByDefault) {
6958b954241SJohn Brawn   bool MergeExternal = (EnableGlobalMergeOnExternal == cl::BOU_UNSET) ?
6968b954241SJohn Brawn     MergeExternalByDefault : (EnableGlobalMergeOnExternal == cl::BOU_TRUE);
6978b954241SJohn Brawn   return new GlobalMerge(TM, Offset, OnlyOptimizeForSize, MergeExternal);
69896e92c1dSJiangning Liu }
699