2011-10-17 17:17:43 +00:00
|
|
|
//===-- GlobalMerge.cpp - Internal globals merging -----------------------===//
|
2010-07-24 21:52:08 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// This pass merges globals with internal linkage into one. This way all the
|
|
|
|
// globals which were merged into a biggest one can be addressed using offsets
|
|
|
|
// from the same base pointer (no need for separate base pointer for each of the
|
|
|
|
// global). Such a transformation can significantly reduce the register pressure
|
|
|
|
// when many globals are involved.
|
|
|
|
//
|
2010-09-28 04:18:29 +00:00
|
|
|
// For example, consider the code which touches several global variables at
|
|
|
|
// once:
|
2010-07-24 21:52:08 +00:00
|
|
|
//
|
|
|
|
// static int foo[N], bar[N], baz[N];
|
|
|
|
//
|
|
|
|
// for (i = 0; i < N; ++i) {
|
|
|
|
// foo[i] = bar[i] * baz[i];
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// On ARM the addresses of 3 arrays should be kept in the registers, thus
|
|
|
|
// this code has quite large register pressure (loop body):
|
|
|
|
//
|
|
|
|
// ldr r1, [r5], #4
|
|
|
|
// ldr r2, [r6], #4
|
|
|
|
// mul r1, r2, r1
|
|
|
|
// str r1, [r0], #4
|
|
|
|
//
|
|
|
|
// Pass converts the code to something like:
|
|
|
|
//
|
|
|
|
// static struct {
|
|
|
|
// int foo[N];
|
|
|
|
// int bar[N];
|
|
|
|
// int baz[N];
|
|
|
|
// } merged;
|
|
|
|
//
|
|
|
|
// for (i = 0; i < N; ++i) {
|
|
|
|
// merged.foo[i] = merged.bar[i] * merged.baz[i];
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// and in ARM code this becomes:
|
|
|
|
//
|
|
|
|
// ldr r0, [r5, #40]
|
|
|
|
// ldr r1, [r5, #80]
|
|
|
|
// mul r0, r1, r0
|
|
|
|
// str r0, [r5], #4
|
|
|
|
//
|
|
|
|
// note that we saved 2 registers here almostly "for free".
|
2010-09-28 04:18:29 +00:00
|
|
|
// ===---------------------------------------------------------------------===//
|
2010-07-24 21:52:08 +00:00
|
|
|
|
2011-10-17 17:17:43 +00:00
|
|
|
#define DEBUG_TYPE "global-merge"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2010-07-24 21:52:08 +00:00
|
|
|
#include "llvm/Attributes.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/GlobalVariable.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Intrinsics.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2010-11-17 21:25:39 +00:00
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
2011-10-17 17:17:43 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2010-07-24 21:52:08 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2011-10-17 17:17:43 +00:00
|
|
|
STATISTIC(NumMerged , "Number of globals merged");
|
2010-07-24 21:52:08 +00:00
|
|
|
namespace {
|
2011-10-17 17:17:43 +00:00
|
|
|
class GlobalMerge : public FunctionPass {
|
2010-07-24 21:52:08 +00:00
|
|
|
/// TLI - Keep a pointer of a TargetLowering to consult for determining
|
|
|
|
/// target type sizes.
|
|
|
|
const TargetLowering *TLI;
|
|
|
|
|
|
|
|
bool doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
|
2010-11-17 21:25:39 +00:00
|
|
|
Module &M, bool isConst) const;
|
2010-07-24 21:52:08 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid.
|
2011-10-17 17:17:43 +00:00
|
|
|
explicit GlobalMerge(const TargetLowering *tli = 0)
|
|
|
|
: FunctionPass(ID), TLI(tli) {
|
|
|
|
initializeGlobalMergePass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-07-24 21:52:08 +00:00
|
|
|
|
|
|
|
virtual bool doInitialization(Module &M);
|
2010-09-05 21:18:45 +00:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2010-07-24 21:52:08 +00:00
|
|
|
|
|
|
|
const char *getPassName() const {
|
|
|
|
return "Merge internal globals";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
FunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GlobalCmp {
|
|
|
|
const TargetData *TD;
|
|
|
|
|
2010-09-05 21:18:45 +00:00
|
|
|
GlobalCmp(const TargetData *td) : TD(td) { }
|
2010-07-24 21:52:08 +00:00
|
|
|
|
2010-09-05 21:18:45 +00:00
|
|
|
bool operator()(const GlobalVariable *GV1, const GlobalVariable *GV2) {
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Ty1 = cast<PointerType>(GV1->getType())->getElementType();
|
|
|
|
Type *Ty2 = cast<PointerType>(GV2->getType())->getElementType();
|
2010-07-24 21:52:08 +00:00
|
|
|
|
|
|
|
return (TD->getTypeAllocSize(Ty1) < TD->getTypeAllocSize(Ty2));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-10-17 17:17:43 +00:00
|
|
|
char GlobalMerge::ID = 0;
|
|
|
|
INITIALIZE_PASS(GlobalMerge, "global-merge",
|
|
|
|
"Global Merge", false, false)
|
|
|
|
|
2010-07-24 21:52:08 +00:00
|
|
|
|
2011-10-17 17:17:43 +00:00
|
|
|
bool GlobalMerge::doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
|
2010-07-24 21:52:08 +00:00
|
|
|
Module &M, bool isConst) const {
|
|
|
|
const TargetData *TD = TLI->getTargetData();
|
|
|
|
|
|
|
|
// FIXME: Infer the maximum possible offset depending on the actual users
|
|
|
|
// (these max offsets are different for the users inside Thumb or ARM
|
|
|
|
// functions)
|
|
|
|
unsigned MaxOffset = TLI->getMaximalGlobalOffset();
|
|
|
|
|
|
|
|
// FIXME: Find better heuristics
|
|
|
|
std::stable_sort(Globals.begin(), Globals.end(), GlobalCmp(TD));
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Int32Ty = Type::getInt32Ty(M.getContext());
|
2010-07-24 21:52:08 +00:00
|
|
|
|
|
|
|
for (size_t i = 0, e = Globals.size(); i != e; ) {
|
|
|
|
size_t j = 0;
|
|
|
|
uint64_t MergedSize = 0;
|
2011-07-12 14:06:48 +00:00
|
|
|
std::vector<Type*> Tys;
|
2010-07-24 21:52:08 +00:00
|
|
|
std::vector<Constant*> Inits;
|
2010-11-17 21:25:36 +00:00
|
|
|
for (j = i; j != e; ++j) {
|
2011-07-12 14:06:48 +00:00
|
|
|
Type *Ty = Globals[j]->getType()->getElementType();
|
2010-11-17 21:25:36 +00:00
|
|
|
MergedSize += TD->getTypeAllocSize(Ty);
|
|
|
|
if (MergedSize > MaxOffset) {
|
|
|
|
break;
|
|
|
|
}
|
2010-07-24 21:52:08 +00:00
|
|
|
Tys.push_back(Ty);
|
|
|
|
Inits.push_back(Globals[j]->getInitializer());
|
|
|
|
}
|
|
|
|
|
2010-09-05 21:18:45 +00:00
|
|
|
StructType *MergedTy = StructType::get(M.getContext(), Tys);
|
|
|
|
Constant *MergedInit = ConstantStruct::get(MergedTy, Inits);
|
|
|
|
GlobalVariable *MergedGV = new GlobalVariable(M, MergedTy, isConst,
|
2010-07-24 21:52:08 +00:00
|
|
|
GlobalValue::InternalLinkage,
|
2010-11-17 21:25:33 +00:00
|
|
|
MergedInit, "_MergedGlobals");
|
2010-07-24 21:52:08 +00:00
|
|
|
for (size_t k = i; k < j; ++k) {
|
2010-09-05 21:18:45 +00:00
|
|
|
Constant *Idx[2] = {
|
|
|
|
ConstantInt::get(Int32Ty, 0),
|
|
|
|
ConstantInt::get(Int32Ty, k-i)
|
|
|
|
};
|
2011-07-21 14:31:17 +00:00
|
|
|
Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(MergedGV, Idx);
|
2010-07-24 21:52:08 +00:00
|
|
|
Globals[k]->replaceAllUsesWith(GEP);
|
|
|
|
Globals[k]->eraseFromParent();
|
2011-10-17 17:17:43 +00:00
|
|
|
NumMerged++;
|
2010-07-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
i = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-17 17:17:43 +00:00
|
|
|
bool GlobalMerge::doInitialization(Module &M) {
|
2010-11-17 21:25:39 +00:00
|
|
|
SmallVector<GlobalVariable*, 16> Globals, ConstGlobals, BSSGlobals;
|
2010-07-24 21:52:08 +00:00
|
|
|
const TargetData *TD = TLI->getTargetData();
|
|
|
|
unsigned MaxOffset = TLI->getMaximalGlobalOffset();
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// Grab all non-const globals.
|
|
|
|
for (Module::global_iterator I = M.global_begin(),
|
|
|
|
E = M.global_end(); I != E; ++I) {
|
|
|
|
// Merge is safe for "normal" internal globals only
|
|
|
|
if (!I->hasLocalLinkage() || I->isThreadLocal() || I->hasSection())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Ignore fancy-aligned globals for now.
|
2011-11-30 21:54:15 +00:00
|
|
|
unsigned Alignment = TD->getPreferredAlignment(I);
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Ty = I->getType()->getElementType();
|
2011-07-11 01:29:42 +00:00
|
|
|
if (Alignment > TD->getABITypeAlignment(Ty))
|
2010-07-24 21:52:08 +00:00
|
|
|
continue;
|
|
|
|
|
2010-07-26 18:45:39 +00:00
|
|
|
// Ignore all 'special' globals.
|
|
|
|
if (I->getName().startswith("llvm.") ||
|
|
|
|
I->getName().startswith(".llvm."))
|
|
|
|
continue;
|
|
|
|
|
2011-07-11 01:29:42 +00:00
|
|
|
if (TD->getTypeAllocSize(Ty) < MaxOffset) {
|
2012-02-13 06:30:56 +00:00
|
|
|
if (TargetLoweringObjectFile::getKindForGlobal(I, TLI->getTargetMachine())
|
|
|
|
.isBSSLocal())
|
2010-11-17 21:25:39 +00:00
|
|
|
BSSGlobals.push_back(I);
|
|
|
|
else if (I->isConstant())
|
2010-07-24 21:52:08 +00:00
|
|
|
ConstGlobals.push_back(I);
|
|
|
|
else
|
|
|
|
Globals.push_back(I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Globals.size() > 1)
|
|
|
|
Changed |= doMerge(Globals, M, false);
|
2010-11-17 21:25:39 +00:00
|
|
|
if (BSSGlobals.size() > 1)
|
|
|
|
Changed |= doMerge(BSSGlobals, M, false);
|
|
|
|
|
2010-07-26 18:45:39 +00:00
|
|
|
// FIXME: This currently breaks the EH processing due to way how the
|
|
|
|
// typeinfo detection works. We might want to detect the TIs and ignore
|
|
|
|
// them in the future.
|
|
|
|
// if (ConstGlobals.size() > 1)
|
|
|
|
// Changed |= doMerge(ConstGlobals, M, true);
|
2010-07-24 21:52:08 +00:00
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2011-10-17 17:17:43 +00:00
|
|
|
bool GlobalMerge::runOnFunction(Function &F) {
|
2010-07-24 21:52:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-17 17:17:43 +00:00
|
|
|
Pass *llvm::createGlobalMergePass(const TargetLowering *tli) {
|
|
|
|
return new GlobalMerge(tli);
|
2010-07-24 21:52:08 +00:00
|
|
|
}
|