Fixed some bugs.
[llvm/zpu.git] / lib / Target / ARM / ARMGlobalMerge.cpp
blobfdcb67062f9967da932d88617c87d07399e90ffc
1 //===-- 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.
15 // For example, consider the code which touches several global variables at
16 // once:
18 // static int foo[N], bar[N], baz[N];
20 // for (i = 0; i < N; ++i) {
21 // foo[i] = bar[i] * baz[i];
22 // }
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):
27 // ldr r1, [r5], #4
28 // ldr r2, [r6], #4
29 // mul r1, r2, r1
30 // str r1, [r0], #4
32 // Pass converts the code to something like:
34 // static struct {
35 // int foo[N];
36 // int bar[N];
37 // int baz[N];
38 // } merged;
40 // for (i = 0; i < N; ++i) {
41 // merged.foo[i] = merged.bar[i] * merged.baz[i];
42 // }
44 // and in ARM code this becomes:
46 // ldr r0, [r5, #40]
47 // ldr r1, [r5, #80]
48 // mul r0, r1, r0
49 // str r0, [r5], #4
51 // note that we saved 2 registers here almostly "for free".
52 // ===---------------------------------------------------------------------===//
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"
68 using namespace llvm;
70 namespace {
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;
79 public:
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 {
92 AU.setPreservesCFG();
93 FunctionPass::getAnalysisUsage(AU);
96 struct GlobalCmp {
97 const TargetData *TD;
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
119 // functions)
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; ) {
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) {
133 const Type *Ty = Globals[j]->getType()->getElementType();
134 Tys.push_back(Ty);
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) {
145 Constant *Idx[2] = {
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();
153 i = j;
156 return true;
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())
171 continue;
173 // Ignore fancy-aligned globals for now.
174 if (I->getAlignment() != 0)
175 continue;
177 // Ignore all 'special' globals.
178 if (I->getName().startswith("llvm.") ||
179 I->getName().startswith(".llvm."))
180 continue;
182 if (TD->getTypeAllocSize(I->getType()) < MaxOffset) {
183 if (I->isConstant())
184 ConstGlobals.push_back(I);
185 else
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);
199 return Changed;
202 bool ARMGlobalMerge::runOnFunction(Function &F) {
203 return false;
206 FunctionPass *llvm::createARMGlobalMergePass(const TargetLowering *tli) {
207 return new ARMGlobalMerge(tli);