From 5e563c326490207ebd58d47935fb9efda7638aa2 Mon Sep 17 00:00:00 2001 From: Devang Patel Date: Tue, 15 Jan 2008 23:52:34 +0000 Subject: [PATCH] - Introduces versioning macro LLVM_LTO_VERSION - Communicate symbol visibility - Communicate code generation model git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@46033 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/LinkTimeOptimizer.h | 44 +++++++++++++++++++++------- tools/lto/lto.cpp | 49 ++++++++++++++++++++++++++------ 2 files changed, 75 insertions(+), 18 deletions(-) diff --git a/include/llvm/LinkTimeOptimizer.h b/include/llvm/LinkTimeOptimizer.h index 67c9b290f0e..de33871d680 100644 --- a/include/llvm/LinkTimeOptimizer.h +++ b/include/llvm/LinkTimeOptimizer.h @@ -20,6 +20,8 @@ #include #include +#define LLVM_LTO_VERSION 2 + namespace llvm { class Module; @@ -45,6 +47,19 @@ namespace llvm { LTOInternalLinkage // Rename collisions when linking (static functions) }; + enum LTOVisibilityTypes { + LTODefaultVisibility = 0, ///< The GV is visible + LTOHiddenVisibility, ///< The GV is hidden + LTOProtectedVisibility ///< The GV is protected + }; + + + enum LTOCodeGenModel { + LTO_CGM_Static, + LTO_CGM_Dynamic, + LTO_CGM_DynamicNoPIC + }; + /// This class represents LLVM symbol information without exposing details /// of LLVM global values. It encapsulates symbol linkage information. This /// is typically used in hash_map where associated name identifies the @@ -54,10 +69,13 @@ namespace llvm { public: LTOLinkageTypes getLinkage() const { return linkage; } + LTOVisibilityTypes getVisibility() const { return visibility; } void mayBeNotUsed(); - LLVMSymbol (enum LTOLinkageTypes lt, GlobalValue *g, const std::string &n, - const std::string &m, int a) : linkage(lt), gv(g), name(n), + LLVMSymbol (enum LTOLinkageTypes lt, enum LTOVisibilityTypes vis, + GlobalValue *g, const std::string &n, + const std::string &m, int a) : linkage(lt), visibility(vis), + gv(g), name(n), mangledName(m), alignment(a) {} const char *getName() { return name.c_str(); } @@ -66,6 +84,7 @@ namespace llvm { private: enum LTOLinkageTypes linkage; + enum LTOVisibilityTypes visibility; GlobalValue *gv; std::string name; std::string mangledName; @@ -91,11 +110,12 @@ namespace llvm { NameToSymbolMap &, std::set &) = 0; virtual enum LTOStatus optimizeModules(const std::string &, - std::vector &, - std::string &, bool, - const char *) = 0; + std::vector &exportList, + std::string &targetTriple, + bool saveTemps, const char *) = 0; virtual void getTargetTriple(const std::string &, std::string &) = 0; virtual void removeModule (const std::string &InputFilename) = 0; + virtual void setCodeGenModel(LTOCodeGenModel CGM) = 0; virtual void printVersion () = 0; virtual ~LinkTimeOptimizer() = 0; }; @@ -115,17 +135,20 @@ namespace llvm { std::set &references); enum LTOStatus optimizeModules(const std::string &OutputFilename, std::vector &exportList, - std::string &targetTriple, bool saveTemps, - const char *); + std::string &targetTriple, + bool saveTemps, const char *); void getTargetTriple(const std::string &InputFilename, std::string &targetTriple); void removeModule (const std::string &InputFilename); void printVersion(); + void setCodeGenModel(LTOCodeGenModel CGM) { + CGModel = CGM; + } + // Constructors and destructors - LTO() { + LTO() : Target(NULL), CGModel(LTO_CGM_Dynamic) { /// TODO: Use Target info, it is available at this time. - Target = NULL; } ~LTO(); @@ -140,6 +163,7 @@ namespace llvm { NameToSymbolMap allSymbols; NameToModuleMap allModules; TargetMachine *Target; + LTOCodeGenModel CGModel; }; } // End llvm namespace @@ -148,6 +172,6 @@ namespace llvm { /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer. /// extern "C" helps, because dlopen() interface uses name to find the symbol. extern "C" -llvm::LinkTimeOptimizer *createLLVMOptimizer(); +llvm::LinkTimeOptimizer *createLLVMOptimizer(unsigned VERSION = LLVM_LTO_VERSION); #endif diff --git a/tools/lto/lto.cpp b/tools/lto/lto.cpp index 74888a7be21..99024589f19 100644 --- a/tools/lto/lto.cpp +++ b/tools/lto/lto.cpp @@ -45,8 +45,15 @@ using namespace llvm; extern "C" -llvm::LinkTimeOptimizer *createLLVMOptimizer() +llvm::LinkTimeOptimizer *createLLVMOptimizer(unsigned VERSION) { + // Linker records LLVM_LTO_VERSION based on llvm optimizer available + // during linker build. Match linker's recorded LTO VERSION number + // with installed llvm optimizer version. If these numbers do not match + // then linker may not be able to use llvm optimizer dynamically. + if (VERSION != LLVM_LTO_VERSION) + return NULL; + llvm::LTO *l = new llvm::LTO(); return l; } @@ -74,6 +81,20 @@ getLTOLinkageType(GlobalValue *v) return lt; } +// MAP LLVM VisibilityType to LTO VisibilityType +static LTOVisibilityTypes +getLTOVisibilityType(GlobalValue *v) +{ + LTOVisibilityTypes vis; + if (v->hasHiddenVisibility()) + vis = LTOHiddenVisibility; + else if (v->hasProtectedVisibility()) + vis = LTOProtectedVisibility; + else + vis = LTODefaultVisibility; + return vis; +} + // Find exeternal symbols referenced by VALUE. This is a recursive function. static void findExternalRefs(Value *value, std::set &references, @@ -164,13 +185,12 @@ LTO::readLLVMObjectFile(const std::string &InputFilename, modules.push_back(m); for (Module::iterator f = m->begin(), e = m->end(); f != e; ++f) { - LTOLinkageTypes lt = getLTOLinkageType(f); - + LTOVisibilityTypes vis = getLTOVisibilityType(f); if (!f->isDeclaration() && lt != LTOInternalLinkage && strncmp (f->getName().c_str(), "llvm.", 5)) { int alignment = ( 16 > f->getAlignment() ? 16 : f->getAlignment()); - LLVMSymbol *newSymbol = new LLVMSymbol(lt, f, f->getName(), + LLVMSymbol *newSymbol = new LLVMSymbol(lt, vis, f, f->getName(), mangler.getValueName(f), Log2_32(alignment)); symbols[newSymbol->getMangledName()] = newSymbol; @@ -180,19 +200,21 @@ LTO::readLLVMObjectFile(const std::string &InputFilename, // Collect external symbols referenced by this function. for (Function::iterator b = f->begin(), fe = f->end(); b != fe; ++b) for (BasicBlock::iterator i = b->begin(), be = b->end(); - i != be; ++i) + i != be; ++i) { for (unsigned count = 0, total = i->getNumOperands(); count != total; ++count) findExternalRefs(i->getOperand(count), references, mangler); + } } for (Module::global_iterator v = m->global_begin(), e = m->global_end(); v != e; ++v) { LTOLinkageTypes lt = getLTOLinkageType(v); + LTOVisibilityTypes vis = getLTOVisibilityType(v); if (!v->isDeclaration() && lt != LTOInternalLinkage && strncmp (v->getName().c_str(), "llvm.", 5)) { const TargetData *TD = Target->getTargetData(); - LLVMSymbol *newSymbol = new LLVMSymbol(lt, v, v->getName(), + LLVMSymbol *newSymbol = new LLVMSymbol(lt, vis, v, v->getName(), mangler.getValueName(v), TD->getPreferredAlignmentLog(v)); symbols[newSymbol->getMangledName()] = newSymbol; @@ -354,8 +376,7 @@ enum LTOStatus LTO::optimizeModules(const std::string &OutputFilename, std::vector &exportList, std::string &targetTriple, - bool saveTemps, - const char *FinalOutputFilename) + bool saveTemps, const char *FinalOutputFilename) { if (modules.empty()) return LTO_NO_WORK; @@ -374,6 +395,18 @@ LTO::optimizeModules(const std::string &OutputFilename, sys::Path FinalOutputPath(FinalOutputFilename); FinalOutputPath.eraseSuffix(); + switch(CGModel) { + case LTO_CGM_Dynamic: + Target->setRelocationModel(Reloc::PIC_); + break; + case LTO_CGM_DynamicNoPIC: + Target->setRelocationModel(Reloc::DynamicNoPIC); + break; + case LTO_CGM_Static: + Target->setRelocationModel(Reloc::Static); + break; + } + if (saveTemps) { std::string tempFileName(FinalOutputPath.c_str()); tempFileName += "0.bc"; -- 2.34.1