1 //===-- ARMGlobalMerge.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 "arm-global-merge"
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"
71 class ARMGlobalMerge
: public FunctionPass
{
72 /// TLI - Keep a pointer of a TargetLowering to consult for determining
73 /// target type sizes.
74 const TargetLowering
*TLI
;
76 bool doMerge(SmallVectorImpl
<GlobalVariable
*> &Globals
,
77 Module
&M
, bool) const;
80 static char ID
; // Pass identification, replacement for typeid.
81 explicit ARMGlobalMerge(const TargetLowering
*tli
)
82 : FunctionPass(ID
), TLI(tli
) {}
84 virtual bool doInitialization(Module
&M
);
85 virtual bool runOnFunction(Function
&F
);
87 const char *getPassName() const {
88 return "Merge internal globals";
91 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
93 FunctionPass::getAnalysisUsage(AU
);
99 GlobalCmp(const TargetData
*td
) : TD(td
) { }
101 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();
105 return (TD
->getTypeAllocSize(Ty1
) < TD
->getTypeAllocSize(Ty2
));
109 } // end anonymous namespace
111 char ARMGlobalMerge::ID
= 0;
113 bool ARMGlobalMerge::doMerge(SmallVectorImpl
<GlobalVariable
*> &Globals
,
114 Module
&M
, bool isConst
) const {
115 const TargetData
*TD
= TLI
->getTargetData();
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
120 unsigned MaxOffset
= TLI
->getMaximalGlobalOffset();
122 // FIXME: Find better heuristics
123 std::stable_sort(Globals
.begin(), Globals
.end(), GlobalCmp(TD
));
125 const Type
*Int32Ty
= Type::getInt32Ty(M
.getContext());
127 for (size_t i
= 0, e
= Globals
.size(); i
!= e
; ) {
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
) {
133 const Type
*Ty
= Globals
[j
]->getType()->getElementType();
135 Inits
.push_back(Globals
[j
]->getInitializer());
136 MergedSize
+= TD
->getTypeAllocSize(Ty
);
139 StructType
*MergedTy
= StructType::get(M
.getContext(), Tys
);
140 Constant
*MergedInit
= ConstantStruct::get(MergedTy
, Inits
);
141 GlobalVariable
*MergedGV
= new GlobalVariable(M
, MergedTy
, isConst
,
142 GlobalValue::InternalLinkage
,
143 MergedInit
, "merged");
144 for (size_t k
= i
; k
< j
; ++k
) {
146 ConstantInt::get(Int32Ty
, 0),
147 ConstantInt::get(Int32Ty
, k
-i
)
149 Constant
*GEP
= ConstantExpr::getInBoundsGetElementPtr(MergedGV
, Idx
, 2);
150 Globals
[k
]->replaceAllUsesWith(GEP
);
151 Globals
[k
]->eraseFromParent();
160 bool ARMGlobalMerge::doInitialization(Module
&M
) {
161 SmallVector
<GlobalVariable
*, 16> Globals
, ConstGlobals
;
162 const TargetData
*TD
= TLI
->getTargetData();
163 unsigned MaxOffset
= TLI
->getMaximalGlobalOffset();
164 bool Changed
= false;
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())
173 // Ignore fancy-aligned globals for now.
174 if (I
->getAlignment() != 0)
177 // Ignore all 'special' globals.
178 if (I
->getName().startswith("llvm.") ||
179 I
->getName().startswith(".llvm."))
182 if (TD
->getTypeAllocSize(I
->getType()) < MaxOffset
) {
184 ConstGlobals
.push_back(I
);
186 Globals
.push_back(I
);
190 if (Globals
.size() > 1)
191 Changed
|= doMerge(Globals
, M
, false);
192 // 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.
196 // if (ConstGlobals.size() > 1)
197 // Changed |= doMerge(ConstGlobals, M, true);
202 bool ARMGlobalMerge::runOnFunction(Function
&F
) {
206 FunctionPass
*llvm::createARMGlobalMergePass(const TargetLowering
*tli
) {
207 return new ARMGlobalMerge(tli
);