LLVM  3.7.0
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"
21 #include "llvm/MC/MCContext.h"
25 #include <string>
26 #include <vector>
27 
28 // Forward references to llvm classes.
29 namespace llvm {
30  class Function;
31  class GlobalValue;
32  class MemoryBuffer;
33  class TargetOptions;
34  class Value;
35 
36 //===----------------------------------------------------------------------===//
37 /// C++ class which implements the opaque lto_module_t type.
38 ///
39 struct LTOModule {
40 private:
41  struct NameAndAttributes {
42  const char *name;
43  uint32_t attributes;
44  bool isFunction;
45  const GlobalValue *symbol;
46  };
47 
48  std::unique_ptr<LLVMContext> OwnedContext;
49 
50  std::string LinkerOpts;
51 
52  std::unique_ptr<object::IRObjectFile> IRFile;
53  std::unique_ptr<TargetMachine> _target;
54  std::vector<NameAndAttributes> _symbols;
55 
56  // _defines and _undefines only needed to disambiguate tentative definitions
57  StringSet<> _defines;
59  std::vector<const char*> _asm_undefines;
60 
61  LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM);
62  LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM,
63  std::unique_ptr<LLVMContext> Context);
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(const char *path);
71 
72  /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
73  /// triple.
74  static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
75  StringRef triplePrefix);
76 
77  /// Create a MemoryBuffer from a memory range with an optional name.
78  static std::unique_ptr<MemoryBuffer>
79  makeBuffer(const void *mem, size_t length, StringRef name = "");
80 
81  /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
82  /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
83  /// and the AsmParsers by calling:
84  ///
85  /// InitializeAllTargets();
86  /// InitializeAllTargetMCs();
87  /// InitializeAllAsmPrinters();
88  /// InitializeAllAsmParsers();
89  static LTOModule *createFromFile(const char *path, TargetOptions options,
90  std::string &errMsg);
91  static LTOModule *createFromOpenFile(int fd, const char *path, size_t size,
92  TargetOptions options,
93  std::string &errMsg);
94  static LTOModule *createFromOpenFileSlice(int fd, const char *path,
95  size_t map_size, off_t offset,
96  TargetOptions options,
97  std::string &errMsg);
98  static LTOModule *createFromBuffer(const void *mem, size_t length,
99  TargetOptions options, std::string &errMsg,
100  StringRef path = "");
101 
102  static LTOModule *createInLocalContext(const void *mem, size_t length,
103  TargetOptions options,
104  std::string &errMsg, StringRef path);
105  static LTOModule *createInContext(const void *mem, size_t length,
106  TargetOptions options, std::string &errMsg,
107  StringRef path, LLVMContext *Context);
108 
109  const Module &getModule() const {
110  return const_cast<LTOModule*>(this)->getModule();
111  }
113  return IRFile->getModule();
114  }
115 
116  /// Return the Module's target triple.
117  const std::string &getTargetTriple() {
118  return getModule().getTargetTriple();
119  }
120 
121  /// Set the Module's target triple.
123  getModule().setTargetTriple(Triple);
124  }
125 
126  /// Get the number of symbols
127  uint32_t getSymbolCount() {
128  return _symbols.size();
129  }
130 
131  /// Get the attributes for a symbol at the specified index.
133  if (index < _symbols.size())
134  return lto_symbol_attributes(_symbols[index].attributes);
135  return lto_symbol_attributes(0);
136  }
137 
138  /// Get the name of the symbol at the specified index.
139  const char *getSymbolName(uint32_t index) {
140  if (index < _symbols.size())
141  return _symbols[index].name;
142  return nullptr;
143  }
144 
145  const GlobalValue *getSymbolGV(uint32_t index) {
146  if (index < _symbols.size())
147  return _symbols[index].symbol;
148  return nullptr;
149  }
150 
151  const char *getLinkerOpts() {
152  return LinkerOpts.c_str();
153  }
154 
155  const std::vector<const char*> &getAsmUndefinedRefs() {
156  return _asm_undefines;
157  }
158 
159 private:
160  /// Parse metadata from the module
161  // FIXME: it only parses "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  bool parseSymbols(std::string &errMsg);
167 
168  /// Add a symbol which isn't defined just yet to a list to be resolved later.
169  void addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,
170  bool isFunc);
171 
172  /// Add a defined symbol to the list.
173  void addDefinedSymbol(const char *Name, const GlobalValue *def,
174  bool isFunction);
175 
176  /// Add a data symbol as defined to the list.
177  void addDefinedDataSymbol(const object::BasicSymbolRef &Sym);
178  void addDefinedDataSymbol(const char*Name, const GlobalValue *v);
179 
180  /// Add a function symbol as defined to the list.
181  void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym);
182  void addDefinedFunctionSymbol(const char *Name, const Function *F);
183 
184  /// Add a global symbol from module-level ASM to the defined list.
185  void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
186 
187  /// Add a global symbol from module-level ASM to the undefined list.
188  void addAsmGlobalSymbolUndef(const char *);
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).
203  static LTOModule *makeLTOModule(MemoryBufferRef Buffer, TargetOptions options,
204  std::string &errMsg, LLVMContext *Context);
205 };
206 }
207 #endif
uint32_t getSymbolCount()
Get the number of symbols.
Definition: LTOModule.h:127
static LTOModule * createInLocalContext(const void *mem, size_t length, TargetOptions options, std::string &errMsg, StringRef path)
Definition: LTOModule.cpp:135
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
const char * getLinkerOpts()
Definition: LTOModule.h:151
F(f)
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:261
lto_symbol_attributes getSymbolAttributes(uint32_t index)
Get the attributes for a symbol at the specified index.
Definition: LTOModule.h:132
const GlobalValue * getSymbolGV(uint32_t index)
Definition: LTOModule.h:145
const Module & getModule() const
Definition: LTOModule.h:109
const std::string & getTargetTriple()
Return the Module's target triple.
Definition: LTOModule.h:117
static bool isBitcodeForTarget(MemoryBuffer *memBuffer, StringRef triplePrefix)
Returns 'true' if the memory buffer is LLVM bitcode for the specified triple.
Definition: LTOModule.cpp:83
const char * getSymbolName(uint32_t index)
Get the name of the symbol at the specified index.
Definition: LTOModule.h:139
Module & getModule()
Definition: LTOModule.h:112
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
This is an important base class in LLVM.
Definition: Constant.h:41
void setTargetTriple(StringRef Triple)
Set the Module's target triple.
Definition: LTOModule.h:122
C++ class which implements the opaque lto_module_t type.
Definition: LTOModule.h:39
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
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:37
static LTOModule * createInContext(const void *mem, size_t length, TargetOptions options, std::string &errMsg, StringRef path, LLVMContext *Context)
Definition: LTOModule.cpp:142
lto_symbol_attributes
Definition: lto.h:48
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:258
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: SymbolicFile.h:78
Deduce function attributes
void size_t size
const std::vector< const char * > & getAsmUndefinedRefs()
Definition: LTOModule.h:155
void setTargetTriple(StringRef T)
Set the target triple.
Definition: Module.h:294
static LTOModule * createFromFile(const char *path, TargetOptions options, std::string &errMsg)
Create an LTOModule.
Definition: LTOModule.cpp:94
static LTOModule * createFromOpenFileSlice(int fd, const char *path, size_t map_size, off_t offset, TargetOptions options, std::string &errMsg)
Definition: LTOModule.cpp:113
static bool isBitcodeFile(const void *mem, size_t length)
Returns 'true' if the file or memory contents is LLVM bitcode.
Definition: LTOModule.cpp:66
static LTOModule * createFromBuffer(const void *mem, size_t length, TargetOptions options, std::string &errMsg, StringRef path="")
Definition: LTOModule.cpp:128
static const char * name
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:23
Primary interface to the complete machine description for the target machine.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
static LTOModule * createFromOpenFile(int fd, const char *path, size_t size, TargetOptions options, std::string &errMsg)
Definition: LTOModule.cpp:107