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

llvm-svn: 199759
diff --git a/llvm/include/llvm-c/lto.h b/llvm/include/llvm-c/lto.h
index 7078a74..fbad2b5 100644
--- a/llvm/include/llvm-c/lto.h
+++ b/llvm/include/llvm-c/lto.h
@@ -40,7 +40,7 @@
  * @{
  */
 
-#define LTO_API_VERSION 7
+#define LTO_API_VERSION 8
 
 /**
  * \since prior to LTO_API_VERSION=3
@@ -246,6 +246,43 @@
 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/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
index 5b22c9c..4e90e14 100644
--- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
+++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
@@ -80,6 +80,10 @@
 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 @@
   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/llvm/include/llvm/LTO/LTOModule.h b/llvm/include/llvm/LTO/LTOModule.h
index 78a72fe..c70afa4 100644
--- a/llvm/include/llvm/LTO/LTOModule.h
+++ b/llvm/include/llvm/LTO/LTOModule.h
@@ -51,6 +51,9 @@
   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 @@
     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 @@
   }
 
 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/llvm/include/llvm/Target/TargetLoweringObjectFile.h b/llvm/include/llvm/Target/TargetLoweringObjectFile.h
index 4aedd3a..5bf9a13 100644
--- a/llvm/include/llvm/Target/TargetLoweringObjectFile.h
+++ b/llvm/include/llvm/Target/TargetLoweringObjectFile.h
@@ -57,6 +57,12 @@
                                     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>,