Move GlobalMerge from Transform to CodeGen.
[oota-llvm.git] / lib / CodeGen / GlobalMerge.cpp
1 //===-- GlobalMerge.cpp - Internal globals merging  -----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // This pass merges globals with internal linkage into one. This way all the
10 // globals which were merged into a biggest one can be addressed using offsets
11 // from the same base pointer (no need for separate base pointer for each of the
12 // global). Such a transformation can significantly reduce the register pressure
13 // when many globals are involved.
14 //
15 // For example, consider the code which touches several global variables at
16 // once:
17 //
18 // static int foo[N], bar[N], baz[N];
19 //
20 // for (i = 0; i < N; ++i) {
21 //    foo[i] = bar[i] * baz[i];
22 // }
23 //
24 //  On ARM the addresses of 3 arrays should be kept in the registers, thus
25 //  this code has quite large register pressure (loop body):
26 //
27 //  ldr     r1, [r5], #4
28 //  ldr     r2, [r6], #4
29 //  mul     r1, r2, r1
30 //  str     r1, [r0], #4
31 //
32 //  Pass converts the code to something like:
33 //
34 //  static struct {
35 //    int foo[N];
36 //    int bar[N];
37 //    int baz[N];
38 //  } merged;
39 //
40 //  for (i = 0; i < N; ++i) {
41 //    merged.foo[i] = merged.bar[i] * merged.baz[i];
42 //  }
43 //
44 //  and in ARM code this becomes:
45 //
46 //  ldr     r0, [r5, #40]
47 //  ldr     r1, [r5, #80]
48 //  mul     r0, r1, r0
49 //  str     r0, [r5], #4
50 //
51 //  note that we saved 2 registers here almostly "for free".
52 // ===---------------------------------------------------------------------===//
53
54 #include "llvm/Transforms/Scalar.h"
55 #include "llvm/ADT/SmallPtrSet.h"
56 #include "llvm/ADT/Statistic.h"
57 #include "llvm/IR/Attributes.h"
58 #include "llvm/IR/Constants.h"
59 #include "llvm/IR/DataLayout.h"
60 #include "llvm/IR/DerivedTypes.h"
61 #include "llvm/IR/Function.h"
62 #include "llvm/IR/GlobalVariable.h"
63 #include "llvm/IR/Instructions.h"
64 #include "llvm/IR/Intrinsics.h"
65 #include "llvm/IR/Module.h"
66 #include "llvm/Pass.h"
67 #include "llvm/CodeGen/Passes.h"
68 #include "llvm/Support/CommandLine.h"
69 #include "llvm/Target/TargetLowering.h"
70 #include "llvm/Target/TargetLoweringObjectFile.h"
71 using namespace llvm;
72
73 #define DEBUG_TYPE "global-merge"
74
75 static cl::opt<bool>
76 EnableGlobalMerge("enable-global-merge", cl::Hidden,
77                   cl::desc("Enable global merge pass"),
78                   cl::init(true));
79
80 static cl::opt<bool>
81 EnableGlobalMergeOnConst("global-merge-on-const", cl::Hidden,
82                          cl::desc("Enable global merge pass on constants"),
83                          cl::init(false));
84
85 // FIXME: this could be a transitional option, and we probably need to remove
86 // it if only we are sure this optimization could always benefit all targets.
87 static cl::opt<bool>
88 EnableGlobalMergeOnExternal("global-merge-on-external", cl::Hidden,
89      cl::desc("Enable global merge pass on external linkage"),
90      cl::init(false));
91
92 STATISTIC(NumMerged      , "Number of globals merged");
93 namespace {
94   class GlobalMerge : public FunctionPass {
95     const TargetMachine *TM;
96
97     bool doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
98                  Module &M, bool isConst, unsigned AddrSpace) const;
99
100     /// \brief Check if the given variable has been identified as must keep
101     /// \pre setMustKeepGlobalVariables must have been called on the Module that
102     ///      contains GV
103     bool isMustKeepGlobalVariable(const GlobalVariable *GV) const {
104       return MustKeepGlobalVariables.count(GV);
105     }
106
107     /// Collect every variables marked as "used" or used in a landing pad
108     /// instruction for this Module.
109     void setMustKeepGlobalVariables(Module &M);
110
111     /// Collect every variables marked as "used"
112     void collectUsedGlobalVariables(Module &M);
113
114     /// Keep track of the GlobalVariable that must not be merged away
115     SmallPtrSet<const GlobalVariable *, 16> MustKeepGlobalVariables;
116
117   public:
118     static char ID;             // Pass identification, replacement for typeid.
119     explicit GlobalMerge(const TargetMachine *TM = nullptr)
120       : FunctionPass(ID), TM(TM) {
121       initializeGlobalMergePass(*PassRegistry::getPassRegistry());
122     }
123
124     bool doInitialization(Module &M) override;
125     bool runOnFunction(Function &F) override;
126     bool doFinalization(Module &M) override;
127
128     const char *getPassName() const override {
129       return "Merge internal globals";
130     }
131
132     void getAnalysisUsage(AnalysisUsage &AU) const override {
133       AU.setPreservesCFG();
134       FunctionPass::getAnalysisUsage(AU);
135     }
136   };
137 } // end anonymous namespace
138
139 char GlobalMerge::ID = 0;
140 INITIALIZE_TM_PASS(GlobalMerge, "global-merge", "Merge global variables",
141                    false, false)
142
143 bool GlobalMerge::doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
144                           Module &M, bool isConst, unsigned AddrSpace) const {
145   const TargetLowering *TLI = TM->getTargetLowering();
146   const DataLayout *DL = TLI->getDataLayout();
147
148   // FIXME: Infer the maximum possible offset depending on the actual users
149   // (these max offsets are different for the users inside Thumb or ARM
150   // functions)
151   unsigned MaxOffset = TLI->getMaximalGlobalOffset();
152
153   // FIXME: Find better heuristics
154   std::stable_sort(Globals.begin(), Globals.end(),
155                    [DL](const GlobalVariable *GV1, const GlobalVariable *GV2) {
156     Type *Ty1 = cast<PointerType>(GV1->getType())->getElementType();
157     Type *Ty2 = cast<PointerType>(GV2->getType())->getElementType();
158
159     return (DL->getTypeAllocSize(Ty1) < DL->getTypeAllocSize(Ty2));
160   });
161
162   Type *Int32Ty = Type::getInt32Ty(M.getContext());
163
164   assert(Globals.size() > 1);
165
166   // FIXME: This simple solution merges globals all together as maximum as
167   // possible. However, with this solution it would be hard to remove dead
168   // global symbols at link-time. An alternative solution could be checking
169   // global symbols references function by function, and make the symbols
170   // being referred in the same function merged and we would probably need
171   // to introduce heuristic algorithm to solve the merge conflict from
172   // different functions.
173   for (size_t i = 0, e = Globals.size(); i != e; ) {
174     size_t j = 0;
175     uint64_t MergedSize = 0;
176     std::vector<Type*> Tys;
177     std::vector<Constant*> Inits;
178
179     bool HasExternal = false;
180     GlobalVariable *TheFirstExternal = 0;
181     for (j = i; j != e; ++j) {
182       Type *Ty = Globals[j]->getType()->getElementType();
183       MergedSize += DL->getTypeAllocSize(Ty);
184       if (MergedSize > MaxOffset) {
185         break;
186       }
187       Tys.push_back(Ty);
188       Inits.push_back(Globals[j]->getInitializer());
189
190       if (Globals[j]->hasExternalLinkage() && !HasExternal) {
191         HasExternal = true;
192         TheFirstExternal = Globals[j];
193       }
194     }
195
196     // If merged variables doesn't have external linkage, we needn't to expose
197     // the symbol after merging.
198     GlobalValue::LinkageTypes Linkage = HasExternal
199                                             ? GlobalValue::ExternalLinkage
200                                             : GlobalValue::InternalLinkage;
201
202     // If merged variables have external linkage, we use symbol name of the
203     // first variable merged as the suffix of global symbol name. This would
204     // be able to avoid the link-time naming conflict for globalm symbols.
205     Twine MergedGVName = HasExternal
206                              ? "_MergedGlobals_" + TheFirstExternal->getName()
207                              : "_MergedGlobals";
208
209     StructType *MergedTy = StructType::get(M.getContext(), Tys);
210     Constant *MergedInit = ConstantStruct::get(MergedTy, Inits);
211
212     GlobalVariable *MergedGV = new GlobalVariable(
213         M, MergedTy, isConst, Linkage, MergedInit, MergedGVName, nullptr,
214         GlobalVariable::NotThreadLocal, AddrSpace);
215
216     for (size_t k = i; k < j; ++k) {
217       GlobalValue::LinkageTypes Linkage = Globals[k]->getLinkage();
218       std::string Name = Globals[k]->getName();
219
220       Constant *Idx[2] = {
221         ConstantInt::get(Int32Ty, 0),
222         ConstantInt::get(Int32Ty, k-i)
223       };
224       Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(MergedGV, Idx);
225       Globals[k]->replaceAllUsesWith(GEP);
226       Globals[k]->eraseFromParent();
227
228       if (Linkage != GlobalValue::InternalLinkage) {
229         // Generate a new alias...
230         auto *PTy = cast<PointerType>(GEP->getType());
231         GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
232                             Linkage, Name, GEP, &M);
233       }
234
235       NumMerged++;
236     }
237     i = j;
238   }
239
240   return true;
241 }
242
243 void GlobalMerge::collectUsedGlobalVariables(Module &M) {
244   // Extract global variables from llvm.used array
245   const GlobalVariable *GV = M.getGlobalVariable("llvm.used");
246   if (!GV || !GV->hasInitializer()) return;
247
248   // Should be an array of 'i8*'.
249   const ConstantArray *InitList = cast<ConstantArray>(GV->getInitializer());
250
251   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
252     if (const GlobalVariable *G =
253         dyn_cast<GlobalVariable>(InitList->getOperand(i)->stripPointerCasts()))
254       MustKeepGlobalVariables.insert(G);
255 }
256
257 void GlobalMerge::setMustKeepGlobalVariables(Module &M) {
258   collectUsedGlobalVariables(M);
259
260   for (Module::iterator IFn = M.begin(), IEndFn = M.end(); IFn != IEndFn;
261        ++IFn) {
262     for (Function::iterator IBB = IFn->begin(), IEndBB = IFn->end();
263          IBB != IEndBB; ++IBB) {
264       // Follow the invoke link to find the landing pad instruction
265       const InvokeInst *II = dyn_cast<InvokeInst>(IBB->getTerminator());
266       if (!II) continue;
267
268       const LandingPadInst *LPInst = II->getUnwindDest()->getLandingPadInst();
269       // Look for globals in the clauses of the landing pad instruction
270       for (unsigned Idx = 0, NumClauses = LPInst->getNumClauses();
271            Idx != NumClauses; ++Idx)
272         if (const GlobalVariable *GV =
273             dyn_cast<GlobalVariable>(LPInst->getClause(Idx)
274                                      ->stripPointerCasts()))
275           MustKeepGlobalVariables.insert(GV);
276     }
277   }
278 }
279
280 bool GlobalMerge::doInitialization(Module &M) {
281   if (!EnableGlobalMerge)
282     return false;
283
284   DenseMap<unsigned, SmallVector<GlobalVariable*, 16> > Globals, ConstGlobals,
285                                                         BSSGlobals;
286   const TargetLowering *TLI = TM->getTargetLowering();
287   const DataLayout *DL = TLI->getDataLayout();
288   unsigned MaxOffset = TLI->getMaximalGlobalOffset();
289   bool Changed = false;
290   setMustKeepGlobalVariables(M);
291
292   // Grab all non-const globals.
293   for (Module::global_iterator I = M.global_begin(),
294          E = M.global_end(); I != E; ++I) {
295     // Merge is safe for "normal" internal or external globals only
296     if (I->isDeclaration() || I->isThreadLocal() || I->hasSection())
297       continue;
298
299     if (!(EnableGlobalMergeOnExternal && I->hasExternalLinkage()) &&
300         !I->hasInternalLinkage())
301       continue;
302
303     PointerType *PT = dyn_cast<PointerType>(I->getType());
304     assert(PT && "Global variable is not a pointer!");
305
306     unsigned AddressSpace = PT->getAddressSpace();
307
308     // Ignore fancy-aligned globals for now.
309     unsigned Alignment = DL->getPreferredAlignment(I);
310     Type *Ty = I->getType()->getElementType();
311     if (Alignment > DL->getABITypeAlignment(Ty))
312       continue;
313
314     // Ignore all 'special' globals.
315     if (I->getName().startswith("llvm.") ||
316         I->getName().startswith(".llvm."))
317       continue;
318
319     // Ignore all "required" globals:
320     if (isMustKeepGlobalVariable(I))
321       continue;
322
323     if (DL->getTypeAllocSize(Ty) < MaxOffset) {
324       if (TargetLoweringObjectFile::getKindForGlobal(I, *TM).isBSSLocal())
325         BSSGlobals[AddressSpace].push_back(I);
326       else if (I->isConstant())
327         ConstGlobals[AddressSpace].push_back(I);
328       else
329         Globals[AddressSpace].push_back(I);
330     }
331   }
332
333   for (DenseMap<unsigned, SmallVector<GlobalVariable*, 16> >::iterator
334        I = Globals.begin(), E = Globals.end(); I != E; ++I)
335     if (I->second.size() > 1)
336       Changed |= doMerge(I->second, M, false, I->first);
337
338   for (DenseMap<unsigned, SmallVector<GlobalVariable*, 16> >::iterator
339        I = BSSGlobals.begin(), E = BSSGlobals.end(); I != E; ++I)
340     if (I->second.size() > 1)
341       Changed |= doMerge(I->second, M, false, I->first);
342
343   if (EnableGlobalMergeOnConst)
344     for (DenseMap<unsigned, SmallVector<GlobalVariable*, 16> >::iterator
345          I = ConstGlobals.begin(), E = ConstGlobals.end(); I != E; ++I)
346       if (I->second.size() > 1)
347         Changed |= doMerge(I->second, M, true, I->first);
348
349   return Changed;
350 }
351
352 bool GlobalMerge::runOnFunction(Function &F) {
353   return false;
354 }
355
356 bool GlobalMerge::doFinalization(Module &M) {
357   MustKeepGlobalVariables.clear();
358   return false;
359 }
360
361 Pass *llvm::createGlobalMergePass(const TargetMachine *TM) {
362   return new GlobalMerge(TM);
363 }