ARMGlobalMerge.cpp [plain text]
#define DEBUG_TYPE "arm-global-merge"
#include "ARM.h"
#include "llvm/CodeGen/Passes.h"
#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"
#include "llvm/Target/TargetLoweringObjectFile.h"
using namespace llvm;
namespace {
class ARMGlobalMerge : public FunctionPass {
const TargetLowering *TLI;
bool doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
Module &M, bool isConst) const;
public:
static char ID; explicit ARMGlobalMerge(const TargetLowering *tli)
: FunctionPass(ID), TLI(tli) {}
virtual bool doInitialization(Module &M);
virtual bool runOnFunction(Function &F);
const char *getPassName() const {
return "Merge internal globals";
}
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesCFG();
FunctionPass::getAnalysisUsage(AU);
}
struct GlobalCmp {
const TargetData *TD;
GlobalCmp(const TargetData *td) : TD(td) { }
bool operator()(const GlobalVariable *GV1, const GlobalVariable *GV2) {
const Type *Ty1 = cast<PointerType>(GV1->getType())->getElementType();
const Type *Ty2 = cast<PointerType>(GV2->getType())->getElementType();
return (TD->getTypeAllocSize(Ty1) < TD->getTypeAllocSize(Ty2));
}
};
};
}
char ARMGlobalMerge::ID = 0;
bool ARMGlobalMerge::doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
Module &M, bool isConst) const {
const TargetData *TD = TLI->getTargetData();
unsigned MaxOffset = TLI->getMaximalGlobalOffset();
std::stable_sort(Globals.begin(), Globals.end(), GlobalCmp(TD));
const Type *Int32Ty = Type::getInt32Ty(M.getContext());
for (size_t i = 0, e = Globals.size(); i != e; ) {
size_t j = 0;
uint64_t MergedSize = 0;
std::vector<const Type*> Tys;
std::vector<Constant*> Inits;
for (j = i; j != e; ++j) {
const Type *Ty = Globals[j]->getType()->getElementType();
MergedSize += TD->getTypeAllocSize(Ty);
if (MergedSize > MaxOffset) {
break;
}
Tys.push_back(Ty);
Inits.push_back(Globals[j]->getInitializer());
}
StructType *MergedTy = StructType::get(M.getContext(), Tys);
Constant *MergedInit = ConstantStruct::get(MergedTy, Inits);
GlobalVariable *MergedGV = new GlobalVariable(M, MergedTy, isConst,
GlobalValue::InternalLinkage,
MergedInit, "_MergedGlobals");
for (size_t k = i; k < j; ++k) {
Constant *Idx[2] = {
ConstantInt::get(Int32Ty, 0),
ConstantInt::get(Int32Ty, k-i)
};
Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(MergedGV, Idx, 2);
Globals[k]->replaceAllUsesWith(GEP);
Globals[k]->eraseFromParent();
}
i = j;
}
return true;
}
bool ARMGlobalMerge::doInitialization(Module &M) {
SmallVector<GlobalVariable*, 16> Globals, ConstGlobals, BSSGlobals;
const TargetData *TD = TLI->getTargetData();
unsigned MaxOffset = TLI->getMaximalGlobalOffset();
bool Changed = false;
for (Module::global_iterator I = M.global_begin(),
E = M.global_end(); I != E; ++I) {
if (!I->hasLocalLinkage() || I->isThreadLocal() || I->hasSection())
continue;
if (I->getAlignment() != 0)
continue;
if (I->getName().startswith("llvm.") ||
I->getName().startswith(".llvm."))
continue;
if (TD->getTypeAllocSize(I->getType()->getElementType()) < MaxOffset) {
const TargetLoweringObjectFile &TLOF = TLI->getObjFileLowering();
if (TLOF.getKindForGlobal(I, TLI->getTargetMachine()).isBSSLocal())
BSSGlobals.push_back(I);
else if (I->isConstant())
ConstGlobals.push_back(I);
else
Globals.push_back(I);
}
}
if (Globals.size() > 1)
Changed |= doMerge(Globals, M, false);
if (BSSGlobals.size() > 1)
Changed |= doMerge(BSSGlobals, M, false);
return Changed;
}
bool ARMGlobalMerge::runOnFunction(Function &F) {
return false;
}
FunctionPass *llvm::createARMGlobalMergePass(const TargetLowering *tli) {
return new ARMGlobalMerge(tli);
}