blob: 4796eb2953a6f6e3bdba2a5ccc47afe540bfb8ac [file] [log] [blame]
Devang Patel827454e2011-10-17 17:17:43 +00001//===-- GlobalMerge.cpp - Internal globals merging -----------------------===//
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00002//
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//
Nadav Rotema94d6e82012-07-24 10:51:42 +000015// For example, consider the code which touches several global variables at
Eric Christophera99c3e92010-09-28 04:18:29 +000016// once:
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000017//
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".
Eric Christophera99c3e92010-09-28 04:18:29 +000052// ===---------------------------------------------------------------------===//
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000053
Devang Patel827454e2011-10-17 17:17:43 +000054#define DEBUG_TYPE "global-merge"
55#include "llvm/Transforms/Scalar.h"
Quentin Colombete5728092013-03-18 22:30:07 +000056#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000057#include "llvm/ADT/Statistic.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000058#include "llvm/IR/Attributes.h"
59#include "llvm/IR/Constants.h"
60#include "llvm/IR/DataLayout.h"
61#include "llvm/IR/DerivedTypes.h"
62#include "llvm/IR/Function.h"
63#include "llvm/IR/GlobalVariable.h"
64#include "llvm/IR/Instructions.h"
65#include "llvm/IR/Intrinsics.h"
66#include "llvm/IR/Module.h"
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000067#include "llvm/Pass.h"
Quentin Colombete5728092013-03-18 22:30:07 +000068#include "llvm/Support/CommandLine.h"
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000069#include "llvm/Target/TargetLowering.h"
Bob Wilson05646092010-11-17 21:25:39 +000070#include "llvm/Target/TargetLoweringObjectFile.h"
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000071using namespace llvm;
72
Quentin Colombete5728092013-03-18 22:30:07 +000073static cl::opt<bool>
74EnableGlobalMergeOnConst("global-merge-on-const", cl::Hidden,
75 cl::desc("Enable global merge pass on constants"),
76 cl::init(false));
77
Devang Patel827454e2011-10-17 17:17:43 +000078STATISTIC(NumMerged , "Number of globals merged");
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000079namespace {
Devang Patel827454e2011-10-17 17:17:43 +000080 class GlobalMerge : public FunctionPass {
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000081 /// TLI - Keep a pointer of a TargetLowering to consult for determining
82 /// target type sizes.
83 const TargetLowering *TLI;
84
85 bool doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
Silviu Barangae9716592013-01-07 12:31:25 +000086 Module &M, bool isConst, unsigned AddrSpace) const;
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000087
Quentin Colombete5728092013-03-18 22:30:07 +000088 /// \brief Check if the given variable has been identified as must keep
89 /// \pre setMustKeepGlobalVariables must have been called on the Module that
90 /// contains GV
91 bool isMustKeepGlobalVariable(const GlobalVariable *GV) const {
92 return MustKeepGlobalVariables.count(GV);
93 }
94
95 /// Collect every variables marked as "used" or used in a landing pad
96 /// instruction for this Module.
97 void setMustKeepGlobalVariables(Module &M);
98
99 /// Collect every variables marked as "used"
100 void collectUsedGlobalVariables(Module &M);
101
Quentin Colombet9deb9172013-03-19 21:46:49 +0000102 /// Keep track of the GlobalVariable that must not be merged away
Quentin Colombete5728092013-03-18 22:30:07 +0000103 SmallPtrSet<const GlobalVariable *, 16> MustKeepGlobalVariables;
104
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000105 public:
106 static char ID; // Pass identification, replacement for typeid.
Devang Patel827454e2011-10-17 17:17:43 +0000107 explicit GlobalMerge(const TargetLowering *tli = 0)
108 : FunctionPass(ID), TLI(tli) {
109 initializeGlobalMergePass(*PassRegistry::getPassRegistry());
110 }
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000111
112 virtual bool doInitialization(Module &M);
Chris Lattner252b4912010-09-05 21:18:45 +0000113 virtual bool runOnFunction(Function &F);
Quentin Colombet9deb9172013-03-19 21:46:49 +0000114 virtual bool doFinalization(Module &M);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000115
116 const char *getPassName() const {
117 return "Merge internal globals";
118 }
119
120 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
121 AU.setPreservesCFG();
122 FunctionPass::getAnalysisUsage(AU);
123 }
124
125 struct GlobalCmp {
Micah Villmow3574eca2012-10-08 16:38:25 +0000126 const DataLayout *TD;
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000127
Micah Villmow3574eca2012-10-08 16:38:25 +0000128 GlobalCmp(const DataLayout *td) : TD(td) { }
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000129
Chris Lattner252b4912010-09-05 21:18:45 +0000130 bool operator()(const GlobalVariable *GV1, const GlobalVariable *GV2) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000131 Type *Ty1 = cast<PointerType>(GV1->getType())->getElementType();
132 Type *Ty2 = cast<PointerType>(GV2->getType())->getElementType();
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000133
134 return (TD->getTypeAllocSize(Ty1) < TD->getTypeAllocSize(Ty2));
135 }
136 };
137 };
138} // end anonymous namespace
139
Devang Patel827454e2011-10-17 17:17:43 +0000140char GlobalMerge::ID = 0;
141INITIALIZE_PASS(GlobalMerge, "global-merge",
142 "Global Merge", false, false)
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000143
Devang Patel827454e2011-10-17 17:17:43 +0000144
145bool GlobalMerge::doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
Silviu Barangae9716592013-01-07 12:31:25 +0000146 Module &M, bool isConst, unsigned AddrSpace) const {
Micah Villmow3574eca2012-10-08 16:38:25 +0000147 const DataLayout *TD = TLI->getDataLayout();
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000148
149 // FIXME: Infer the maximum possible offset depending on the actual users
150 // (these max offsets are different for the users inside Thumb or ARM
151 // functions)
152 unsigned MaxOffset = TLI->getMaximalGlobalOffset();
153
154 // FIXME: Find better heuristics
155 std::stable_sort(Globals.begin(), Globals.end(), GlobalCmp(TD));
156
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000157 Type *Int32Ty = Type::getInt32Ty(M.getContext());
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000158
159 for (size_t i = 0, e = Globals.size(); i != e; ) {
160 size_t j = 0;
161 uint64_t MergedSize = 0;
Jay Foad5fdd6c82011-07-12 14:06:48 +0000162 std::vector<Type*> Tys;
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000163 std::vector<Constant*> Inits;
Bob Wilson619a3722010-11-17 21:25:36 +0000164 for (j = i; j != e; ++j) {
Jay Foad5fdd6c82011-07-12 14:06:48 +0000165 Type *Ty = Globals[j]->getType()->getElementType();
Bob Wilson619a3722010-11-17 21:25:36 +0000166 MergedSize += TD->getTypeAllocSize(Ty);
167 if (MergedSize > MaxOffset) {
168 break;
169 }
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000170 Tys.push_back(Ty);
171 Inits.push_back(Globals[j]->getInitializer());
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000172 }
173
Chris Lattner252b4912010-09-05 21:18:45 +0000174 StructType *MergedTy = StructType::get(M.getContext(), Tys);
175 Constant *MergedInit = ConstantStruct::get(MergedTy, Inits);
176 GlobalVariable *MergedGV = new GlobalVariable(M, MergedTy, isConst,
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000177 GlobalValue::InternalLinkage,
Silviu Barangae9716592013-01-07 12:31:25 +0000178 MergedInit, "_MergedGlobals",
179 0, GlobalVariable::NotThreadLocal,
180 AddrSpace);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000181 for (size_t k = i; k < j; ++k) {
Chris Lattner252b4912010-09-05 21:18:45 +0000182 Constant *Idx[2] = {
183 ConstantInt::get(Int32Ty, 0),
184 ConstantInt::get(Int32Ty, k-i)
185 };
Jay Foaddab3d292011-07-21 14:31:17 +0000186 Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(MergedGV, Idx);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000187 Globals[k]->replaceAllUsesWith(GEP);
188 Globals[k]->eraseFromParent();
Devang Patel827454e2011-10-17 17:17:43 +0000189 NumMerged++;
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000190 }
191 i = j;
192 }
193
194 return true;
195}
196
Quentin Colombete5728092013-03-18 22:30:07 +0000197void GlobalMerge::collectUsedGlobalVariables(Module &M) {
198 // Extract global variables from llvm.used array
199 const GlobalVariable *GV = M.getGlobalVariable("llvm.used");
200 if (!GV || !GV->hasInitializer()) return;
201
202 // Should be an array of 'i8*'.
Rafael Espindolacde25b42013-04-22 14:58:02 +0000203 const ConstantArray *InitList = cast<ConstantArray>(GV->getInitializer());
204
Quentin Colombete5728092013-03-18 22:30:07 +0000205 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
206 if (const GlobalVariable *G =
207 dyn_cast<GlobalVariable>(InitList->getOperand(i)->stripPointerCasts()))
208 MustKeepGlobalVariables.insert(G);
209}
210
211void GlobalMerge::setMustKeepGlobalVariables(Module &M) {
Quentin Colombete5728092013-03-18 22:30:07 +0000212 collectUsedGlobalVariables(M);
213
214 for (Module::iterator IFn = M.begin(), IEndFn = M.end(); IFn != IEndFn;
215 ++IFn) {
216 for (Function::iterator IBB = IFn->begin(), IEndBB = IFn->end();
217 IBB != IEndBB; ++IBB) {
218 // Follow the inwoke link to find the landing pad instruction
219 const InvokeInst *II = dyn_cast<InvokeInst>(IBB->getTerminator());
220 if (!II) continue;
221
222 const LandingPadInst *LPInst = II->getUnwindDest()->getLandingPadInst();
223 // Look for globals in the clauses of the landing pad instruction
224 for (unsigned Idx = 0, NumClauses = LPInst->getNumClauses();
225 Idx != NumClauses; ++Idx)
226 if (const GlobalVariable *GV =
227 dyn_cast<GlobalVariable>(LPInst->getClause(Idx)
228 ->stripPointerCasts()))
229 MustKeepGlobalVariables.insert(GV);
230 }
231 }
232}
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000233
Devang Patel827454e2011-10-17 17:17:43 +0000234bool GlobalMerge::doInitialization(Module &M) {
Silviu Barangae9716592013-01-07 12:31:25 +0000235 DenseMap<unsigned, SmallVector<GlobalVariable*, 16> > Globals, ConstGlobals,
236 BSSGlobals;
Micah Villmow3574eca2012-10-08 16:38:25 +0000237 const DataLayout *TD = TLI->getDataLayout();
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000238 unsigned MaxOffset = TLI->getMaximalGlobalOffset();
239 bool Changed = false;
Quentin Colombete5728092013-03-18 22:30:07 +0000240 setMustKeepGlobalVariables(M);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000241
242 // Grab all non-const globals.
243 for (Module::global_iterator I = M.global_begin(),
244 E = M.global_end(); I != E; ++I) {
245 // Merge is safe for "normal" internal globals only
246 if (!I->hasLocalLinkage() || I->isThreadLocal() || I->hasSection())
247 continue;
248
Silviu Barangae9716592013-01-07 12:31:25 +0000249 PointerType *PT = dyn_cast<PointerType>(I->getType());
250 assert(PT && "Global variable is not a pointer!");
251
252 unsigned AddressSpace = PT->getAddressSpace();
253
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000254 // Ignore fancy-aligned globals for now.
Eli Friedman3dad6102011-11-30 21:54:15 +0000255 unsigned Alignment = TD->getPreferredAlignment(I);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000256 Type *Ty = I->getType()->getElementType();
Cameron Zwarichf75ae4c2011-07-11 01:29:42 +0000257 if (Alignment > TD->getABITypeAlignment(Ty))
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000258 continue;
259
Anton Korobeynikovb5a0ef92010-07-26 18:45:39 +0000260 // Ignore all 'special' globals.
261 if (I->getName().startswith("llvm.") ||
262 I->getName().startswith(".llvm."))
263 continue;
264
Quentin Colombete5728092013-03-18 22:30:07 +0000265 // Ignore all "required" globals:
Quentin Colombete5728092013-03-18 22:30:07 +0000266 if (isMustKeepGlobalVariable(I))
267 continue;
268
Cameron Zwarichf75ae4c2011-07-11 01:29:42 +0000269 if (TD->getTypeAllocSize(Ty) < MaxOffset) {
Ahmed Charlesb83a67e2012-02-13 06:30:56 +0000270 if (TargetLoweringObjectFile::getKindForGlobal(I, TLI->getTargetMachine())
271 .isBSSLocal())
Silviu Barangae9716592013-01-07 12:31:25 +0000272 BSSGlobals[AddressSpace].push_back(I);
Bob Wilson05646092010-11-17 21:25:39 +0000273 else if (I->isConstant())
Silviu Barangae9716592013-01-07 12:31:25 +0000274 ConstGlobals[AddressSpace].push_back(I);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000275 else
Silviu Barangae9716592013-01-07 12:31:25 +0000276 Globals[AddressSpace].push_back(I);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000277 }
278 }
279
Silviu Barangae9716592013-01-07 12:31:25 +0000280 for (DenseMap<unsigned, SmallVector<GlobalVariable*, 16> >::iterator
281 I = Globals.begin(), E = Globals.end(); I != E; ++I)
282 if (I->second.size() > 1)
283 Changed |= doMerge(I->second, M, false, I->first);
284
285 for (DenseMap<unsigned, SmallVector<GlobalVariable*, 16> >::iterator
286 I = BSSGlobals.begin(), E = BSSGlobals.end(); I != E; ++I)
287 if (I->second.size() > 1)
288 Changed |= doMerge(I->second, M, false, I->first);
Bob Wilson05646092010-11-17 21:25:39 +0000289
Quentin Colombete5728092013-03-18 22:30:07 +0000290 if (EnableGlobalMergeOnConst)
291 for (DenseMap<unsigned, SmallVector<GlobalVariable*, 16> >::iterator
292 I = ConstGlobals.begin(), E = ConstGlobals.end(); I != E; ++I)
293 if (I->second.size() > 1)
294 Changed |= doMerge(I->second, M, true, I->first);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000295
296 return Changed;
297}
298
Devang Patel827454e2011-10-17 17:17:43 +0000299bool GlobalMerge::runOnFunction(Function &F) {
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000300 return false;
301}
302
Quentin Colombet9deb9172013-03-19 21:46:49 +0000303bool GlobalMerge::doFinalization(Module &M) {
304 MustKeepGlobalVariables.clear();
305 return false;
306}
307
Devang Patel827454e2011-10-17 17:17:43 +0000308Pass *llvm::createGlobalMergePass(const TargetLowering *tli) {
309 return new GlobalMerge(tli);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000310}