1 //===-- GlobalMerge.cpp - Internal globals merging -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
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.
15 // For example, consider the code which touches several global variables at
18 // static int foo[N], bar[N], baz[N];
20 // for (i = 0; i < N; ++i) {
21 // foo[i] = bar[i] * baz[i];
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):
32 // Pass converts the code to something like:
40 // for (i = 0; i < N; ++i) {
41 // merged.foo[i] = merged.bar[i] * merged.baz[i];
44 // and in ARM code this becomes:
51 // note that we saved 2 registers here almostly "for free".
52 // ===---------------------------------------------------------------------===//
54 #define DEBUG_TYPE "global-merge"
55 #include "llvm/Transforms/Scalar.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/Target/TargetLowering.h"
68 #include "llvm/Target/TargetLoweringObjectFile.h"
71 STATISTIC(NumMerged , "Number of globals merged");
73 class GlobalMerge : public FunctionPass {
74 /// TLI - Keep a pointer of a TargetLowering to consult for determining
75 /// target type sizes.
76 const TargetLowering *TLI;
78 bool doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
79 Module &M, bool isConst, unsigned AddrSpace) const;
82 static char ID; // Pass identification, replacement for typeid.
83 explicit GlobalMerge(const TargetLowering *tli = 0)
84 : FunctionPass(ID), TLI(tli) {
85 initializeGlobalMergePass(*PassRegistry::getPassRegistry());
88 virtual bool doInitialization(Module &M);
89 virtual bool runOnFunction(Function &F);
91 const char *getPassName() const {
92 return "Merge internal globals";
95 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
97 FunctionPass::getAnalysisUsage(AU);
101 const DataLayout *TD;
103 GlobalCmp(const DataLayout *td) : TD(td) { }
105 bool operator()(const GlobalVariable *GV1, const GlobalVariable *GV2) {
106 Type *Ty1 = cast<PointerType>(GV1->getType())->getElementType();
107 Type *Ty2 = cast<PointerType>(GV2->getType())->getElementType();
109 return (TD->getTypeAllocSize(Ty1) < TD->getTypeAllocSize(Ty2));
113 } // end anonymous namespace
115 char GlobalMerge::ID = 0;
116 INITIALIZE_PASS(GlobalMerge, "global-merge",
117 "Global Merge", false, false)
120 bool GlobalMerge::doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
121 Module &M, bool isConst, unsigned AddrSpace) const {
122 const DataLayout *TD = TLI->getDataLayout();
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
127 unsigned MaxOffset = TLI->getMaximalGlobalOffset();
129 // FIXME: Find better heuristics
130 std::stable_sort(Globals.begin(), Globals.end(), GlobalCmp(TD));
132 Type *Int32Ty = Type::getInt32Ty(M.getContext());
134 for (size_t i = 0, e = Globals.size(); i != e; ) {
136 uint64_t MergedSize = 0;
137 std::vector<Type*> Tys;
138 std::vector<Constant*> Inits;
139 for (j = i; j != e; ++j) {
140 Type *Ty = Globals[j]->getType()->getElementType();
141 MergedSize += TD->getTypeAllocSize(Ty);
142 if (MergedSize > MaxOffset) {
146 Inits.push_back(Globals[j]->getInitializer());
149 StructType *MergedTy = StructType::get(M.getContext(), Tys);
150 Constant *MergedInit = ConstantStruct::get(MergedTy, Inits);
151 GlobalVariable *MergedGV = new GlobalVariable(M, MergedTy, isConst,
152 GlobalValue::InternalLinkage,
153 MergedInit, "_MergedGlobals",
154 0, GlobalVariable::NotThreadLocal,
156 for (size_t k = i; k < j; ++k) {
158 ConstantInt::get(Int32Ty, 0),
159 ConstantInt::get(Int32Ty, k-i)
161 Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(MergedGV, Idx);
162 Globals[k]->replaceAllUsesWith(GEP);
163 Globals[k]->eraseFromParent();
173 bool GlobalMerge::doInitialization(Module &M) {
174 DenseMap<unsigned, SmallVector<GlobalVariable*, 16> > Globals, ConstGlobals,
176 const DataLayout *TD = TLI->getDataLayout();
177 unsigned MaxOffset = TLI->getMaximalGlobalOffset();
178 bool Changed = false;
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())
187 PointerType *PT = dyn_cast<PointerType>(I->getType());
188 assert(PT && "Global variable is not a pointer!");
190 unsigned AddressSpace = PT->getAddressSpace();
192 // Ignore fancy-aligned globals for now.
193 unsigned Alignment = TD->getPreferredAlignment(I);
194 Type *Ty = I->getType()->getElementType();
195 if (Alignment > TD->getABITypeAlignment(Ty))
198 // Ignore all 'special' globals.
199 if (I->getName().startswith("llvm.") ||
200 I->getName().startswith(".llvm."))
203 if (TD->getTypeAllocSize(Ty) < MaxOffset) {
204 if (TargetLoweringObjectFile::getKindForGlobal(I, TLI->getTargetMachine())
206 BSSGlobals[AddressSpace].push_back(I);
207 else if (I->isConstant())
208 ConstGlobals[AddressSpace].push_back(I);
210 Globals[AddressSpace].push_back(I);
214 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);
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);
224 // 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
226 // them in the future.
227 // if (ConstGlobals.size() > 1)
228 // Changed |= doMerge(ConstGlobals, M, true);
233 bool GlobalMerge::runOnFunction(Function &F) {
237 Pass *llvm::createGlobalMergePass(const TargetLowering *tli) {
238 return new GlobalMerge(tli);