blob: 1601a8d646655877c53e384f8be98dbe2366ab7f [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000056#include "llvm/ADT/Statistic.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000057#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"
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000066#include "llvm/Pass.h"
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000067#include "llvm/Target/TargetLowering.h"
Bob Wilson05646092010-11-17 21:25:39 +000068#include "llvm/Target/TargetLoweringObjectFile.h"
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000069using namespace llvm;
70
Devang Patel827454e2011-10-17 17:17:43 +000071STATISTIC(NumMerged , "Number of globals merged");
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000072namespace {
Devang Patel827454e2011-10-17 17:17:43 +000073 class GlobalMerge : public FunctionPass {
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000074 /// TLI - Keep a pointer of a TargetLowering to consult for determining
75 /// target type sizes.
76 const TargetLowering *TLI;
77
78 bool doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
Silviu Barangae9716592013-01-07 12:31:25 +000079 Module &M, bool isConst, unsigned AddrSpace) const;
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000080
81 public:
82 static char ID; // Pass identification, replacement for typeid.
Devang Patel827454e2011-10-17 17:17:43 +000083 explicit GlobalMerge(const TargetLowering *tli = 0)
84 : FunctionPass(ID), TLI(tli) {
85 initializeGlobalMergePass(*PassRegistry::getPassRegistry());
86 }
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000087
88 virtual bool doInitialization(Module &M);
Chris Lattner252b4912010-09-05 21:18:45 +000089 virtual bool runOnFunction(Function &F);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000090
91 const char *getPassName() const {
92 return "Merge internal globals";
93 }
94
95 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
96 AU.setPreservesCFG();
97 FunctionPass::getAnalysisUsage(AU);
98 }
99
100 struct GlobalCmp {
Micah Villmow3574eca2012-10-08 16:38:25 +0000101 const DataLayout *TD;
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000102
Micah Villmow3574eca2012-10-08 16:38:25 +0000103 GlobalCmp(const DataLayout *td) : TD(td) { }
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000104
Chris Lattner252b4912010-09-05 21:18:45 +0000105 bool operator()(const GlobalVariable *GV1, const GlobalVariable *GV2) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000106 Type *Ty1 = cast<PointerType>(GV1->getType())->getElementType();
107 Type *Ty2 = cast<PointerType>(GV2->getType())->getElementType();
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000108
109 return (TD->getTypeAllocSize(Ty1) < TD->getTypeAllocSize(Ty2));
110 }
111 };
112 };
113} // end anonymous namespace
114
Devang Patel827454e2011-10-17 17:17:43 +0000115char GlobalMerge::ID = 0;
116INITIALIZE_PASS(GlobalMerge, "global-merge",
117 "Global Merge", false, false)
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000118
Devang Patel827454e2011-10-17 17:17:43 +0000119
120bool GlobalMerge::doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
Silviu Barangae9716592013-01-07 12:31:25 +0000121 Module &M, bool isConst, unsigned AddrSpace) const {
Micah Villmow3574eca2012-10-08 16:38:25 +0000122 const DataLayout *TD = TLI->getDataLayout();
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000123
124 // FIXME: Infer the maximum possible offset depending on the actual users
125 // (these max offsets are different for the users inside Thumb or ARM
126 // functions)
127 unsigned MaxOffset = TLI->getMaximalGlobalOffset();
128
129 // FIXME: Find better heuristics
130 std::stable_sort(Globals.begin(), Globals.end(), GlobalCmp(TD));
131
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000132 Type *Int32Ty = Type::getInt32Ty(M.getContext());
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000133
134 for (size_t i = 0, e = Globals.size(); i != e; ) {
135 size_t j = 0;
136 uint64_t MergedSize = 0;
Jay Foad5fdd6c82011-07-12 14:06:48 +0000137 std::vector<Type*> Tys;
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000138 std::vector<Constant*> Inits;
Bob Wilson619a3722010-11-17 21:25:36 +0000139 for (j = i; j != e; ++j) {
Jay Foad5fdd6c82011-07-12 14:06:48 +0000140 Type *Ty = Globals[j]->getType()->getElementType();
Bob Wilson619a3722010-11-17 21:25:36 +0000141 MergedSize += TD->getTypeAllocSize(Ty);
142 if (MergedSize > MaxOffset) {
143 break;
144 }
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000145 Tys.push_back(Ty);
146 Inits.push_back(Globals[j]->getInitializer());
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000147 }
148
Chris Lattner252b4912010-09-05 21:18:45 +0000149 StructType *MergedTy = StructType::get(M.getContext(), Tys);
150 Constant *MergedInit = ConstantStruct::get(MergedTy, Inits);
151 GlobalVariable *MergedGV = new GlobalVariable(M, MergedTy, isConst,
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000152 GlobalValue::InternalLinkage,
Silviu Barangae9716592013-01-07 12:31:25 +0000153 MergedInit, "_MergedGlobals",
154 0, GlobalVariable::NotThreadLocal,
155 AddrSpace);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000156 for (size_t k = i; k < j; ++k) {
Chris Lattner252b4912010-09-05 21:18:45 +0000157 Constant *Idx[2] = {
158 ConstantInt::get(Int32Ty, 0),
159 ConstantInt::get(Int32Ty, k-i)
160 };
Jay Foaddab3d292011-07-21 14:31:17 +0000161 Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(MergedGV, Idx);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000162 Globals[k]->replaceAllUsesWith(GEP);
163 Globals[k]->eraseFromParent();
Devang Patel827454e2011-10-17 17:17:43 +0000164 NumMerged++;
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000165 }
166 i = j;
167 }
168
169 return true;
170}
171
172
Devang Patel827454e2011-10-17 17:17:43 +0000173bool GlobalMerge::doInitialization(Module &M) {
Silviu Barangae9716592013-01-07 12:31:25 +0000174 DenseMap<unsigned, SmallVector<GlobalVariable*, 16> > Globals, ConstGlobals,
175 BSSGlobals;
Micah Villmow3574eca2012-10-08 16:38:25 +0000176 const DataLayout *TD = TLI->getDataLayout();
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000177 unsigned MaxOffset = TLI->getMaximalGlobalOffset();
178 bool Changed = false;
179
180 // Grab all non-const globals.
181 for (Module::global_iterator I = M.global_begin(),
182 E = M.global_end(); I != E; ++I) {
183 // Merge is safe for "normal" internal globals only
184 if (!I->hasLocalLinkage() || I->isThreadLocal() || I->hasSection())
185 continue;
186
Silviu Barangae9716592013-01-07 12:31:25 +0000187 PointerType *PT = dyn_cast<PointerType>(I->getType());
188 assert(PT && "Global variable is not a pointer!");
189
190 unsigned AddressSpace = PT->getAddressSpace();
191
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000192 // Ignore fancy-aligned globals for now.
Eli Friedman3dad6102011-11-30 21:54:15 +0000193 unsigned Alignment = TD->getPreferredAlignment(I);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000194 Type *Ty = I->getType()->getElementType();
Cameron Zwarichf75ae4c2011-07-11 01:29:42 +0000195 if (Alignment > TD->getABITypeAlignment(Ty))
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000196 continue;
197
Anton Korobeynikovb5a0ef92010-07-26 18:45:39 +0000198 // Ignore all 'special' globals.
199 if (I->getName().startswith("llvm.") ||
200 I->getName().startswith(".llvm."))
201 continue;
202
Cameron Zwarichf75ae4c2011-07-11 01:29:42 +0000203 if (TD->getTypeAllocSize(Ty) < MaxOffset) {
Ahmed Charlesb83a67e2012-02-13 06:30:56 +0000204 if (TargetLoweringObjectFile::getKindForGlobal(I, TLI->getTargetMachine())
205 .isBSSLocal())
Silviu Barangae9716592013-01-07 12:31:25 +0000206 BSSGlobals[AddressSpace].push_back(I);
Bob Wilson05646092010-11-17 21:25:39 +0000207 else if (I->isConstant())
Silviu Barangae9716592013-01-07 12:31:25 +0000208 ConstGlobals[AddressSpace].push_back(I);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000209 else
Silviu Barangae9716592013-01-07 12:31:25 +0000210 Globals[AddressSpace].push_back(I);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000211 }
212 }
213
Silviu Barangae9716592013-01-07 12:31:25 +0000214 for (DenseMap<unsigned, SmallVector<GlobalVariable*, 16> >::iterator
215 I = Globals.begin(), E = Globals.end(); I != E; ++I)
216 if (I->second.size() > 1)
217 Changed |= doMerge(I->second, M, false, I->first);
218
219 for (DenseMap<unsigned, SmallVector<GlobalVariable*, 16> >::iterator
220 I = BSSGlobals.begin(), E = BSSGlobals.end(); I != E; ++I)
221 if (I->second.size() > 1)
222 Changed |= doMerge(I->second, M, false, I->first);
Bob Wilson05646092010-11-17 21:25:39 +0000223
Nadav Rotema94d6e82012-07-24 10:51:42 +0000224 // FIXME: This currently breaks the EH processing due to way how the
225 // typeinfo detection works. We might want to detect the TIs and ignore
Anton Korobeynikovb5a0ef92010-07-26 18:45:39 +0000226 // them in the future.
Anton Korobeynikovb5a0ef92010-07-26 18:45:39 +0000227 // if (ConstGlobals.size() > 1)
228 // Changed |= doMerge(ConstGlobals, M, true);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000229
230 return Changed;
231}
232
Devang Patel827454e2011-10-17 17:17:43 +0000233bool GlobalMerge::runOnFunction(Function &F) {
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000234 return false;
235}
236
Devang Patel827454e2011-10-17 17:17:43 +0000237Pass *llvm::createGlobalMergePass(const TargetLowering *tli) {
238 return new GlobalMerge(tli);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000239}