summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYunzhong Gao <Yunzhong_Gao@playstation.sony.com>2014-01-21 18:31:27 +0000
committerYunzhong Gao <Yunzhong_Gao@playstation.sony.com>2014-01-21 18:31:27 +0000
commita747cf1c4d740dfe5fd1b3cd1b3d43251892457a (patch)
tree3640e0852df6d853ed2b84c2a1ccce517a3c336a
parent92e658b56616cf389de242df230682f0b1c9e4fd (diff)
downloadllvm-a747cf1c4d740dfe5fd1b3cd1b3d43251892457a.tar.gz
llvm-a747cf1c4d740dfe5fd1b3cd1b3d43251892457a.tar.bz2
llvm-a747cf1c4d740dfe5fd1b3cd1b3d43251892457a.tar.xz
Adding new LTO APIs to parse metadata nodes and extract linker options and
dependent libraries from a bitcode module. Differential Revision: http://llvm-reviews.chandlerc.com/D2343 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199759 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm-c/lto.h39
-rw-r--r--include/llvm/CodeGen/TargetLoweringObjectFileImpl.h8
-rw-r--r--include/llvm/LTO/LTOModule.h31
-rw-r--r--include/llvm/Target/TargetLoweringObjectFile.h6
-rw-r--r--lib/CodeGen/TargetLoweringObjectFileImpl.cpp18
-rw-r--r--lib/LTO/LTOModule.cpp29
-rw-r--r--tools/lto/lto.cpp22
-rw-r--r--tools/lto/lto.exports4
8 files changed, 156 insertions, 1 deletions
diff --git a/include/llvm-c/lto.h b/include/llvm-c/lto.h
index 7078a745ff..fbad2b5686 100644
--- a/include/llvm-c/lto.h
+++ b/include/llvm-c/lto.h
@@ -40,7 +40,7 @@ typedef bool lto_bool_t;
* @{
*/
-#define LTO_API_VERSION 7
+#define LTO_API_VERSION 8
/**
* \since prior to LTO_API_VERSION=3
@@ -246,6 +246,43 @@ lto_module_get_symbol_name(lto_module_t mod, unsigned int index);
extern lto_symbol_attributes
lto_module_get_symbol_attribute(lto_module_t mod, unsigned int index);
+
+/**
+ * Returns the number of dependent libraries in the object module.
+ *
+ * \since LTO_API_VERSION=8
+ */
+extern unsigned int
+lto_module_get_num_deplibs(lto_module_t mod);
+
+
+/**
+ * Returns the ith dependent library in the module.
+ *
+ * \since LTO_API_VERSION=8
+ */
+extern const char*
+lto_module_get_deplib(lto_module_t mod, unsigned int index);
+
+
+/**
+ * Returns the number of linker options in the object module.
+ *
+ * \since LTO_API_VERSION=8
+ */
+extern unsigned int
+lto_module_get_num_linkeropts(lto_module_t mod);
+
+
+/**
+ * Returns the ith linker option in the module.
+ *
+ * \since LTO_API_VERSION=8
+ */
+extern const char*
+lto_module_get_linkeropt(lto_module_t mod, unsigned int index);
+
+
/**
* Diagnostic severity.
*
diff --git a/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
index 5b22c9c685..4e90e141ce 100644
--- a/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
+++ b/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
@@ -80,6 +80,10 @@ class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
public:
virtual ~TargetLoweringObjectFileMachO() {}
+ /// getDepLibFromLinkerOpt - Extract the dependent library name from a linker
+ /// option string. Returns StringRef() if the option does not specify a library.
+ virtual StringRef getDepLibFromLinkerOpt(StringRef LinkerOption) const;
+
/// emitModuleFlags - Emit the module flags that specify the garbage
/// collection information.
virtual void emitModuleFlags(MCStreamer &Streamer,
@@ -129,6 +133,10 @@ public:
SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Mangler *Mang, const TargetMachine &TM) const;
+ /// getDepLibFromLinkerOpt - Extract the dependent library name from a linker
+ /// option string. Returns StringRef() if the option does not specify a library.
+ virtual StringRef getDepLibFromLinkerOpt(StringRef LinkerOption) const;
+
/// emitModuleFlags - Emit Obj-C garbage collection and linker options. Only
/// linker option emission is implemented for COFF.
virtual void emitModuleFlags(MCStreamer &Streamer,
diff --git a/include/llvm/LTO/LTOModule.h b/include/llvm/LTO/LTOModule.h
index 78a72fe168..c70afa4196 100644
--- a/include/llvm/LTO/LTOModule.h
+++ b/include/llvm/LTO/LTOModule.h
@@ -51,6 +51,9 @@ private:
llvm::OwningPtr<llvm::Module> _module;
llvm::OwningPtr<llvm::TargetMachine> _target;
llvm::MCObjectFileInfo ObjFileInfo;
+ StringSet _linkeropt_strings;
+ std::vector<const char *> _deplibs;
+ std::vector<const char *> _linkeropts;
std::vector<NameAndAttributes> _symbols;
// _defines and _undefines only needed to disambiguate tentative definitions
@@ -129,6 +132,30 @@ public:
return NULL;
}
+ /// getDependentLibraryCount - Get the number of dependent libraries
+ uint32_t getDependentLibraryCount() {
+ return _deplibs.size();
+ }
+
+ /// getDependentLibrary - Get the dependent library at the specified index.
+ const char *getDependentLibrary(uint32_t index) {
+ if (index < _deplibs.size())
+ return _deplibs[index];
+ return NULL;
+ }
+
+ /// getLinkerOptCount - Get the number of linker options
+ uint32_t getLinkerOptCount() {
+ return _linkeropts.size();
+ }
+
+ /// getLinkerOpt - Get the linker option at the specified index.
+ const char *getLinkerOpt(uint32_t index) {
+ if (index < _linkeropts.size())
+ return _linkeropts[index];
+ return NULL;
+ }
+
/// getLLVVMModule - Return the Module.
llvm::Module *getLLVVMModule() { return _module.get(); }
@@ -138,6 +165,10 @@ public:
}
private:
+ /// parseMetadata - Parse metadata from the module
+ // FIXME: it only parses "Linker Options" metadata at the moment
+ void parseMetadata();
+
/// parseSymbols - Parse the symbols from the module and model-level ASM and
/// add them to either the defined or undefined lists.
bool parseSymbols(std::string &errMsg);
diff --git a/include/llvm/Target/TargetLoweringObjectFile.h b/include/llvm/Target/TargetLoweringObjectFile.h
index 4aedd3aa31..5bf9a13563 100644
--- a/include/llvm/Target/TargetLoweringObjectFile.h
+++ b/include/llvm/Target/TargetLoweringObjectFile.h
@@ -57,6 +57,12 @@ public:
const TargetMachine &TM,
const MCSymbol *Sym) const;
+ /// getDepLibFromLinkerOpt - Extract the dependent library name from a linker
+ /// option string. Returns StringRef() if the option does not specify a library.
+ virtual StringRef getDepLibFromLinkerOpt(StringRef LinkerOption) const {
+ return StringRef();
+ }
+
/// emitModuleFlags - Emit the module flags that the platform cares about.
virtual void emitModuleFlags(MCStreamer &,
ArrayRef<Module::ModuleFlagEntry>,
diff --git a/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 60e74cf8ef..3621b58287 100644
--- a/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -402,6 +402,16 @@ TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
// MachO
//===----------------------------------------------------------------------===//
+/// getDepLibFromLinkerOpt - Extract the dependent library name from a linker
+/// option string. Returns StringRef() if the option does not specify a library.
+StringRef TargetLoweringObjectFileMachO::
+getDepLibFromLinkerOpt(StringRef LinkerOption) const {
+ const char *LibCmd = "-l";
+ if (LinkerOption.startswith(LibCmd))
+ return LinkerOption.substr(strlen(LibCmd));
+ return StringRef();
+}
+
/// emitModuleFlags - Perform code emission for module flags.
void TargetLoweringObjectFileMachO::
emitModuleFlags(MCStreamer &Streamer,
@@ -774,6 +784,14 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
return DataSection;
}
+StringRef TargetLoweringObjectFileCOFF::
+getDepLibFromLinkerOpt(StringRef LinkerOption) const {
+ const char *LibCmd = "/DEFAULTLIB:";
+ if (LinkerOption.startswith(LibCmd))
+ return LinkerOption.substr(strlen(LibCmd));
+ return StringRef();
+}
+
void TargetLoweringObjectFileCOFF::
emitModuleFlags(MCStreamer &Streamer,
ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
diff --git a/lib/LTO/LTOModule.cpp b/lib/LTO/LTOModule.cpp
index 79cff1f68f..f73a608e3c 100644
--- a/lib/LTO/LTOModule.cpp
+++ b/lib/LTO/LTOModule.cpp
@@ -18,6 +18,7 @@
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInst.h"
@@ -37,6 +38,8 @@
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/system_error.h"
+#include "llvm/Target/TargetLowering.h"
+#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetRegisterInfo.h"
#include "llvm/Transforms/Utils/GlobalStatus.h"
using namespace llvm;
@@ -177,6 +180,8 @@ LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
return NULL;
}
+ Ret->parseMetadata();
+
return Ret;
}
@@ -798,3 +803,27 @@ bool LTOModule::parseSymbols(std::string &errMsg) {
return false;
}
+
+/// parseMetadata - Parse metadata from the module
+void LTOModule::parseMetadata() {
+ // Linker Options
+ if (Value *Val = _module->getModuleFlag("Linker Options")) {
+ MDNode *LinkerOptions = cast<MDNode>(Val);
+ for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
+ MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
+ for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
+ MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
+ StringRef Op = _linkeropt_strings.
+ GetOrCreateValue(MDOption->getString()).getKey();
+ StringRef DepLibName = _target->getTargetLowering()->
+ getObjFileLowering().getDepLibFromLinkerOpt(Op);
+ if (!DepLibName.empty())
+ _deplibs.push_back(DepLibName.data());
+ else if (!Op.empty())
+ _linkeropts.push_back(Op.data());
+ }
+ }
+ }
+
+ // Add other interesting metadata here.
+}
diff --git a/tools/lto/lto.cpp b/tools/lto/lto.cpp
index 3d58a577dc..967250acd7 100644
--- a/tools/lto/lto.cpp
+++ b/tools/lto/lto.cpp
@@ -193,6 +193,28 @@ lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
return mod->getSymbolAttributes(index);
}
+/// lto_module_get_num_deplibs - Returns the number of dependent libraries in
+/// the object module.
+unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
+ return mod->getDependentLibraryCount();
+}
+
+/// lto_module_get_deplib - Returns the ith dependent library in the module.
+const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
+ return mod->getDependentLibrary(index);
+}
+
+/// lto_module_get_num_linkeropts - Returns the number of linker options in the
+/// object module.
+unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
+ return mod->getLinkerOptCount();
+}
+
+/// lto_module_get_linkeropt - Returns the ith linker option in the module.
+const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
+ return mod->getLinkerOpt(index);
+}
+
/// Set a diagnostic handler.
void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
lto_diagnostic_handler_t diag_handler,
diff --git a/tools/lto/lto.exports b/tools/lto/lto.exports
index c8f8ee873b..9b8dcb5f09 100644
--- a/tools/lto/lto.exports
+++ b/tools/lto/lto.exports
@@ -5,6 +5,10 @@ lto_module_create
lto_module_create_from_fd
lto_module_create_from_fd_at_offset
lto_module_create_from_memory
+lto_module_get_deplib
+lto_module_get_linkeropt
+lto_module_get_num_deplibs
+lto_module_get_num_linkeropts
lto_module_get_num_symbols
lto_module_get_symbol_attribute
lto_module_get_symbol_name