LLVM  6.0.0svn
LTOModule.h
Go to the documentation of this file.
1 //===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the LTOModule class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LTO_LTOMODULE_H
15 #define LLVM_LTO_LTOMODULE_H
16 
17 #include "llvm-c/lto.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringSet.h"
20 #include "llvm/IR/Module.h"
24 #include <string>
25 #include <vector>
26 
27 // Forward references to llvm classes.
28 namespace llvm {
29  class Function;
30  class GlobalValue;
31  class MemoryBuffer;
32  class TargetOptions;
33  class Value;
34 
35 //===----------------------------------------------------------------------===//
36 /// C++ class which implements the opaque lto_module_t type.
37 ///
38 struct LTOModule {
39 private:
40  struct NameAndAttributes {
42  uint32_t attributes = 0;
43  bool isFunction = 0;
44  const GlobalValue *symbol = 0;
45  };
46 
47  std::unique_ptr<LLVMContext> OwnedContext;
48 
49  std::string LinkerOpts;
50 
51  std::unique_ptr<Module> Mod;
52  MemoryBufferRef MBRef;
53  ModuleSymbolTable SymTab;
54  std::unique_ptr<TargetMachine> _target;
55  std::vector<NameAndAttributes> _symbols;
56 
57  // _defines and _undefines only needed to disambiguate tentative definitions
58  StringSet<> _defines;
60  std::vector<StringRef> _asm_undefines;
61 
62  LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,
63  TargetMachine *TM);
64 
65 public:
66  ~LTOModule();
67 
68  /// Returns 'true' if the file or memory contents is LLVM bitcode.
69  static bool isBitcodeFile(const void *mem, size_t length);
70  static bool isBitcodeFile(StringRef path);
71 
72  /// Returns 'true' if the Module is produced for ThinLTO.
73  bool isThinLTO();
74 
75  /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
76  /// triple.
77  static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
78  StringRef triplePrefix);
79 
80  /// Returns a string representing the producer identification stored in the
81  /// bitcode, or "" if the bitcode does not contains any.
82  ///
83  static std::string getProducerString(MemoryBuffer *Buffer);
84 
85  /// Create a MemoryBuffer from a memory range with an optional name.
86  static std::unique_ptr<MemoryBuffer>
87  makeBuffer(const void *mem, size_t length, StringRef name = "");
88 
89  /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
90  /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
91  /// and the AsmParsers by calling:
92  ///
93  /// InitializeAllTargets();
94  /// InitializeAllTargetMCs();
95  /// InitializeAllAsmPrinters();
96  /// InitializeAllAsmParsers();
99  const TargetOptions &options);
101  createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size,
102  const TargetOptions &options);
104  createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path,
105  size_t map_size, off_t offset,
106  const TargetOptions &options);
108  createFromBuffer(LLVMContext &Context, const void *mem, size_t length,
109  const TargetOptions &options, StringRef path = "");
111  createInLocalContext(std::unique_ptr<LLVMContext> Context, const void *mem,
112  size_t length, const TargetOptions &options,
113  StringRef path);
114 
115  const Module &getModule() const { return *Mod; }
116  Module &getModule() { return *Mod; }
117 
118  std::unique_ptr<Module> takeModule() { return std::move(Mod); }
119 
120  /// Return the Module's target triple.
121  const std::string &getTargetTriple() {
122  return getModule().getTargetTriple();
123  }
124 
125  /// Set the Module's target triple.
127  getModule().setTargetTriple(Triple);
128  }
129 
130  /// Get the number of symbols
132  return _symbols.size();
133  }
134 
135  /// Get the attributes for a symbol at the specified index.
137  if (index < _symbols.size())
138  return lto_symbol_attributes(_symbols[index].attributes);
139  return lto_symbol_attributes(0);
140  }
141 
142  /// Get the name of the symbol at the specified index.
144  if (index < _symbols.size())
145  return _symbols[index].name;
146  return StringRef();
147  }
148 
150  if (index < _symbols.size())
151  return _symbols[index].symbol;
152  return nullptr;
153  }
154 
155  StringRef getLinkerOpts() { return LinkerOpts; }
156 
157  const std::vector<StringRef> &getAsmUndefinedRefs() { return _asm_undefines; }
158 
159 private:
160  /// Parse metadata from the module
161  // FIXME: it only parses "llvm.linker.options" metadata at the moment
162  void parseMetadata();
163 
164  /// Parse the symbols from the module and model-level ASM and add them to
165  /// either the defined or undefined lists.
166  void parseSymbols();
167 
168  /// Add a symbol which isn't defined just yet to a list to be resolved later.
169  void addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,
170  bool isFunc);
171 
172  /// Add a defined symbol to the list.
173  void addDefinedSymbol(StringRef Name, const GlobalValue *def,
174  bool isFunction);
175 
176  /// Add a data symbol as defined to the list.
177  void addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym);
178  void addDefinedDataSymbol(StringRef Name, const GlobalValue *v);
179 
180  /// Add a function symbol as defined to the list.
181  void addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym);
182  void addDefinedFunctionSymbol(StringRef Name, const Function *F);
183 
184  /// Add a global symbol from module-level ASM to the defined list.
185  void addAsmGlobalSymbol(StringRef, lto_symbol_attributes scope);
186 
187  /// Add a global symbol from module-level ASM to the undefined list.
188  void addAsmGlobalSymbolUndef(StringRef);
189 
190  /// Parse i386/ppc ObjC class data structure.
191  void addObjCClass(const GlobalVariable *clgv);
192 
193  /// Parse i386/ppc ObjC category data structure.
194  void addObjCCategory(const GlobalVariable *clgv);
195 
196  /// Parse i386/ppc ObjC class list data structure.
197  void addObjCClassRef(const GlobalVariable *clgv);
198 
199  /// Get string that the data pointer points to.
200  bool objcClassNameFromExpression(const Constant *c, std::string &name);
201 
202  /// Create an LTOModule (private version).
204  makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
205  LLVMContext &Context, bool ShouldBeLazy);
206 };
207 }
208 #endif
uint32_t getSymbolCount()
Get the number of symbols.
Definition: LTOModule.h:131
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:233
Represents either an error or a value T.
Definition: ErrorOr.h:69
LLVMContext & Context
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
static ErrorOr< std::unique_ptr< LTOModule > > createFromFile(LLVMContext &Context, StringRef path, const TargetOptions &options)
Create an LTOModule.
Definition: LTOModule.cpp:128
bool isThinLTO()
Returns &#39;true&#39; if the Module is produced for ThinLTO.
Definition: LTOModule.cpp:87
static ErrorOr< std::unique_ptr< LTOModule > > createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path, size_t map_size, off_t offset, const TargetOptions &options)
Definition: LTOModule.cpp:148
F(f)
static std::string getProducerString(MemoryBuffer *Buffer)
Returns a string representing the producer identification stored in the bitcode, or "" if the bitcode...
Definition: LTOModule.cpp:112
std::unique_ptr< Module > takeModule()
Definition: LTOModule.h:118
lto_symbol_attributes getSymbolAttributes(uint32_t index)
Get the attributes for a symbol at the specified index.
Definition: LTOModule.h:136
const GlobalValue * getSymbolGV(uint32_t index)
Definition: LTOModule.h:149
static ErrorOr< std::unique_ptr< LTOModule > > createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size, const TargetOptions &options)
Definition: LTOModule.cpp:142
const std::string & getTargetTriple()
Return the Module&#39;s target triple.
Definition: LTOModule.h:121
static bool isBitcodeForTarget(MemoryBuffer *memBuffer, StringRef triplePrefix)
Returns &#39;true&#39; if the memory buffer is LLVM bitcode for the specified triple.
Definition: LTOModule.cpp:96
StringRef getSymbolName(uint32_t index)
Get the name of the symbol at the specified index.
Definition: LTOModule.h:143
Module & getModule()
Definition: LTOModule.h:116
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
This is an important base class in LLVM.
Definition: Constant.h:42
void setTargetTriple(StringRef Triple)
Set the Module&#39;s target triple.
Definition: LTOModule.h:126
const Module & getModule() const
Definition: LTOModule.h:115
C++ class which implements the opaque lto_module_t type.
Definition: LTOModule.h:38
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
const std::vector< StringRef > & getAsmUndefinedRefs()
Definition: LTOModule.h:157
Module.h This file contains the declarations for the Module class.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:40
lto_symbol_attributes
Definition: lto.h:52
static std::unique_ptr< MemoryBuffer > makeBuffer(const void *mem, size_t length, StringRef name="")
Create a MemoryBuffer from a memory range with an optional name.
Definition: LTOModule.cpp:257
Deduce function attributes
void setTargetTriple(StringRef T)
Set the target triple.
Definition: Module.h:269
static bool isBitcodeFile(const void *mem, size_t length)
Returns &#39;true&#39; if the file or memory contents is LLVM bitcode.
Definition: LTOModule.cpp:62
StringRef getLinkerOpts()
Definition: LTOModule.h:155
static const char * name
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:28
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:57
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static ErrorOr< std::unique_ptr< LTOModule > > createInLocalContext(std::unique_ptr< LLVMContext > Context, const void *mem, size_t length, const TargetOptions &options, StringRef path)
Definition: LTOModule.cpp:172
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
Definition: PointerUnion.h:87
static ErrorOr< std::unique_ptr< LTOModule > > createFromBuffer(LLVMContext &Context, const void *mem, size_t length, const TargetOptions &options, StringRef path="")
Definition: LTOModule.cpp:163