blob: 44d523569eb17c1581522ec14a81d9a6e1454ac1 [file] [log] [blame]
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001//===-- ARMGlobalMerge.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//
Eric Christophera99c3e92010-09-28 04:18:29 +000015// For example, consider the code which touches several global variables at
16// 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
54#define DEBUG_TYPE "arm-global-merge"
55#include "ARM.h"
56#include "llvm/CodeGen/Passes.h"
57#include "llvm/Attributes.h"
58#include "llvm/Constants.h"
59#include "llvm/DerivedTypes.h"
60#include "llvm/Function.h"
61#include "llvm/GlobalVariable.h"
62#include "llvm/Instructions.h"
63#include "llvm/Intrinsics.h"
64#include "llvm/Module.h"
65#include "llvm/Pass.h"
66#include "llvm/Target/TargetData.h"
67#include "llvm/Target/TargetLowering.h"
68using namespace llvm;
69
70namespace {
Chris Lattner252b4912010-09-05 21:18:45 +000071 class ARMGlobalMerge : public FunctionPass {
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000072 /// TLI - Keep a pointer of a TargetLowering to consult for determining
73 /// target type sizes.
74 const TargetLowering *TLI;
75
76 bool doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
77 Module &M, bool) const;
78
79 public:
80 static char ID; // Pass identification, replacement for typeid.
81 explicit ARMGlobalMerge(const TargetLowering *tli)
Owen Anderson90c579d2010-08-06 18:33:48 +000082 : FunctionPass(ID), TLI(tli) {}
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000083
84 virtual bool doInitialization(Module &M);
Chris Lattner252b4912010-09-05 21:18:45 +000085 virtual bool runOnFunction(Function &F);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +000086
87 const char *getPassName() const {
88 return "Merge internal globals";
89 }
90
91 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
92 AU.setPreservesCFG();
93 FunctionPass::getAnalysisUsage(AU);
94 }
95
96 struct GlobalCmp {
97 const TargetData *TD;
98
Chris Lattner252b4912010-09-05 21:18:45 +000099 GlobalCmp(const TargetData *td) : TD(td) { }
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000100
Chris Lattner252b4912010-09-05 21:18:45 +0000101 bool operator()(const GlobalVariable *GV1, const GlobalVariable *GV2) {
102 const Type *Ty1 = cast<PointerType>(GV1->getType())->getElementType();
103 const Type *Ty2 = cast<PointerType>(GV2->getType())->getElementType();
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000104
105 return (TD->getTypeAllocSize(Ty1) < TD->getTypeAllocSize(Ty2));
106 }
107 };
108 };
109} // end anonymous namespace
110
111char ARMGlobalMerge::ID = 0;
112
113bool ARMGlobalMerge::doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
114 Module &M, bool isConst) const {
115 const TargetData *TD = TLI->getTargetData();
116
117 // FIXME: Infer the maximum possible offset depending on the actual users
118 // (these max offsets are different for the users inside Thumb or ARM
119 // functions)
120 unsigned MaxOffset = TLI->getMaximalGlobalOffset();
121
122 // FIXME: Find better heuristics
123 std::stable_sort(Globals.begin(), Globals.end(), GlobalCmp(TD));
124
125 const Type *Int32Ty = Type::getInt32Ty(M.getContext());
126
127 for (size_t i = 0, e = Globals.size(); i != e; ) {
128 size_t j = 0;
129 uint64_t MergedSize = 0;
130 std::vector<const Type*> Tys;
131 std::vector<Constant*> Inits;
132 for (j = i; MergedSize < MaxOffset && j != e; ++j) {
Chris Lattner252b4912010-09-05 21:18:45 +0000133 const Type *Ty = Globals[j]->getType()->getElementType();
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000134 Tys.push_back(Ty);
135 Inits.push_back(Globals[j]->getInitializer());
136 MergedSize += TD->getTypeAllocSize(Ty);
137 }
138
Chris Lattner252b4912010-09-05 21:18:45 +0000139 StructType *MergedTy = StructType::get(M.getContext(), Tys);
140 Constant *MergedInit = ConstantStruct::get(MergedTy, Inits);
141 GlobalVariable *MergedGV = new GlobalVariable(M, MergedTy, isConst,
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000142 GlobalValue::InternalLinkage,
Bob Wilson72831dc2010-11-17 21:25:33 +0000143 MergedInit, "_MergedGlobals");
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000144 for (size_t k = i; k < j; ++k) {
Chris Lattner252b4912010-09-05 21:18:45 +0000145 Constant *Idx[2] = {
146 ConstantInt::get(Int32Ty, 0),
147 ConstantInt::get(Int32Ty, k-i)
148 };
149 Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(MergedGV, Idx, 2);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000150 Globals[k]->replaceAllUsesWith(GEP);
151 Globals[k]->eraseFromParent();
152 }
153 i = j;
154 }
155
156 return true;
157}
158
159
Chris Lattner252b4912010-09-05 21:18:45 +0000160bool ARMGlobalMerge::doInitialization(Module &M) {
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000161 SmallVector<GlobalVariable*, 16> Globals, ConstGlobals;
162 const TargetData *TD = TLI->getTargetData();
163 unsigned MaxOffset = TLI->getMaximalGlobalOffset();
164 bool Changed = false;
165
166 // Grab all non-const globals.
167 for (Module::global_iterator I = M.global_begin(),
168 E = M.global_end(); I != E; ++I) {
169 // Merge is safe for "normal" internal globals only
170 if (!I->hasLocalLinkage() || I->isThreadLocal() || I->hasSection())
171 continue;
172
173 // Ignore fancy-aligned globals for now.
174 if (I->getAlignment() != 0)
175 continue;
176
Anton Korobeynikovb5a0ef92010-07-26 18:45:39 +0000177 // Ignore all 'special' globals.
178 if (I->getName().startswith("llvm.") ||
179 I->getName().startswith(".llvm."))
180 continue;
181
Bob Wilsonedf04672010-11-17 21:25:27 +0000182 if (TD->getTypeAllocSize(I->getType()->getElementType()) < MaxOffset) {
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000183 if (I->isConstant())
184 ConstGlobals.push_back(I);
185 else
186 Globals.push_back(I);
187 }
188 }
189
190 if (Globals.size() > 1)
191 Changed |= doMerge(Globals, M, false);
Anton Korobeynikovb5a0ef92010-07-26 18:45:39 +0000192 // FIXME: This currently breaks the EH processing due to way how the
193 // typeinfo detection works. We might want to detect the TIs and ignore
194 // them in the future.
195
196 // if (ConstGlobals.size() > 1)
197 // Changed |= doMerge(ConstGlobals, M, true);
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000198
199 return Changed;
200}
201
Chris Lattner252b4912010-09-05 21:18:45 +0000202bool ARMGlobalMerge::runOnFunction(Function &F) {
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000203 return false;
204}
205
206FunctionPass *llvm::createARMGlobalMergePass(const TargetLowering *tli) {
207 return new ARMGlobalMerge(tli);
208}