LLVM  10.0.0svn
LTOModule.h
Go to the documentation of this file.
1 //===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the LTOModule class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LTO_LTOMODULE_H
14 #define LLVM_LTO_LTOMODULE_H
15 
16 #include "llvm-c/lto.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringSet.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/LTO/LTO.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::string DependentLibraries;
52 
53  std::unique_ptr<Module> Mod;
54  MemoryBufferRef MBRef;
55  ModuleSymbolTable SymTab;
56  std::unique_ptr<TargetMachine> _target;
57  std::vector<NameAndAttributes> _symbols;
58 
59  // _defines and _undefines only needed to disambiguate tentative definitions
60  StringSet<> _defines;
62  std::vector<StringRef> _asm_undefines;
63 
64  LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,
65  TargetMachine *TM);
66 
67 public:
68  ~LTOModule();
69 
70  /// Returns 'true' if the file or memory contents is LLVM bitcode.
71  static bool isBitcodeFile(const void *mem, size_t length);
72  static bool isBitcodeFile(StringRef path);
73 
74  /// Returns 'true' if the Module is produced for ThinLTO.
75  bool isThinLTO();
76 
77  /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
78  /// triple.
79  static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
80  StringRef triplePrefix);
81 
82  /// Returns a string representing the producer identification stored in the
83  /// bitcode, or "" if the bitcode does not contains any.
84  ///
85  static std::string getProducerString(MemoryBuffer *Buffer);
86 
87  /// Create a MemoryBuffer from a memory range with an optional name.
88  static std::unique_ptr<MemoryBuffer>
89  makeBuffer(const void *mem, size_t length, StringRef name = "");
90 
91  /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
92  /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
93  /// and the AsmParsers by calling:
94  ///
95  /// InitializeAllTargets();
96  /// InitializeAllTargetMCs();
97  /// InitializeAllAsmPrinters();
98  /// InitializeAllAsmParsers();
101  const TargetOptions &options);
103  createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size,
104  const TargetOptions &options);
106  createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path,
107  size_t map_size, off_t offset,
108  const TargetOptions &options);
110  createFromBuffer(LLVMContext &Context, const void *mem, size_t length,
111  const TargetOptions &options, StringRef path = "");
113  createInLocalContext(std::unique_ptr<LLVMContext> Context, const void *mem,
114  size_t length, const TargetOptions &options,
115  StringRef path);
116 
117  const Module &getModule() const { return *Mod; }
118  Module &getModule() { return *Mod; }
119 
120  std::unique_ptr<Module> takeModule() { return std::move(Mod); }
121 
122  /// Return the Module's target triple.
123  const std::string &getTargetTriple() {
124  return getModule().getTargetTriple();
125  }
126 
127  /// Set the Module's target triple.
129  getModule().setTargetTriple(Triple);
130  }
131 
132  /// Get the number of symbols
134  return _symbols.size();
135  }
136 
137  /// Get the attributes for a symbol at the specified index.
139  if (index < _symbols.size())
140  return lto_symbol_attributes(_symbols[index].attributes);
141  return lto_symbol_attributes(0);
142  }
143 
144  /// Get the name of the symbol at the specified index.
146  if (index < _symbols.size())
147  return _symbols[index].name;
148  return StringRef();
149  }
150 
152  if (index < _symbols.size())
153  return _symbols[index].symbol;
154  return nullptr;
155  }
156 
157  StringRef getLinkerOpts() { return LinkerOpts; }
158 
159  const std::vector<StringRef> &getAsmUndefinedRefs() { return _asm_undefines; }
160 
161  static lto::InputFile *createInputFile(const void *buffer, size_t buffer_size,
162  const char *path, std::string &out_error);
163 
164  static size_t getDependentLibraryCount(lto::InputFile *input);
165 
166  static const char *getDependentLibrary(lto::InputFile *input, size_t index, size_t *size);
167 
168 private:
169  /// Parse metadata from the module
170  // FIXME: it only parses "llvm.linker.options" metadata at the moment
171  // FIXME: can't access metadata in lazily loaded modules
172  void parseMetadata();
173 
174  /// Parse the symbols from the module and model-level ASM and add them to
175  /// either the defined or undefined lists.
176  void parseSymbols();
177 
178  /// Add a symbol which isn't defined just yet to a list to be resolved later.
179  void addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,
180  bool isFunc);
181 
182  /// Add a defined symbol to the list.
183  void addDefinedSymbol(StringRef Name, const GlobalValue *def,
184  bool isFunction);
185 
186  /// Add a data symbol as defined to the list.
187  void addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym);
188  void addDefinedDataSymbol(StringRef Name, const GlobalValue *v);
189 
190  /// Add a function symbol as defined to the list.
191  void addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym);
192  void addDefinedFunctionSymbol(StringRef Name, const Function *F);
193 
194  /// Add a global symbol from module-level ASM to the defined list.
195  void addAsmGlobalSymbol(StringRef, lto_symbol_attributes scope);
196 
197  /// Add a global symbol from module-level ASM to the undefined list.
198  void addAsmGlobalSymbolUndef(StringRef);
199 
200  /// Parse i386/ppc ObjC class data structure.
201  void addObjCClass(const GlobalVariable *clgv);
202 
203  /// Parse i386/ppc ObjC category data structure.
204  void addObjCCategory(const GlobalVariable *clgv);
205 
206  /// Parse i386/ppc ObjC class list data structure.
207  void addObjCClassRef(const GlobalVariable *clgv);
208 
209  /// Get string that the data pointer points to.
210  bool objcClassNameFromExpression(const Constant *c, std::string &name);
211 
212  /// Create an LTOModule (private version).
214  makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
215  LLVMContext &Context, bool ShouldBeLazy);
216 };
217 }
218 #endif
static size_t getDependentLibraryCount(lto::InputFile *input)
Definition: LTOModule.cpp:669
uint32_t getSymbolCount()
Get the number of symbols.
Definition: LTOModule.h:133
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:241
Represents either an error or a value T.
Definition: ErrorOr.h:56
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static ErrorOr< std::unique_ptr< LTOModule > > createFromFile(LLVMContext &Context, StringRef path, const TargetOptions &options)
Create an LTOModule.
Definition: LTOModule.cpp:109
static lto::InputFile * createInputFile(const void *buffer, size_t buffer_size, const char *path, std::string &out_error)
Definition: LTOModule.cpp:652
bool isThinLTO()
Returns &#39;true&#39; if the Module is produced for ThinLTO.
Definition: LTOModule.cpp:72
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:129
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:95
std::unique_ptr< Module > takeModule()
Definition: LTOModule.h:120
lto_symbol_attributes getSymbolAttributes(uint32_t index)
Get the attributes for a symbol at the specified index.
Definition: LTOModule.h:138
const GlobalValue * getSymbolGV(uint32_t index)
Definition: LTOModule.h:151
static ErrorOr< std::unique_ptr< LTOModule > > createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size, const TargetOptions &options)
Definition: LTOModule.cpp:123
const std::string & getTargetTriple()
Return the Module&#39;s target triple.
Definition: LTOModule.h:123
An input file.
Definition: LTO.h:104
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:81
StringRef getSymbolName(uint32_t index)
Get the name of the symbol at the specified index.
Definition: LTOModule.h:145
static const char * getDependentLibrary(lto::InputFile *input, size_t index, size_t *size)
Definition: LTOModule.cpp:673
Module & getModule()
Definition: LTOModule.h:118
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
This is an important base class in LLVM.
Definition: Constant.h:41
void setTargetTriple(StringRef Triple)
Set the Module&#39;s target triple.
Definition: LTOModule.h:128
const Module & getModule() const
Definition: LTOModule.h:117
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:43
const std::vector< StringRef > & getAsmUndefinedRefs()
Definition: LTOModule.h:159
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1146
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:41
The access may modify the value stored in memory.
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:240
void setTargetTriple(StringRef T)
Set the target triple.
Definition: Module.h:284
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:55
StringRef getLinkerOpts()
Definition: LTOModule.h:157
Deduce and propagate attributes
static const char * name
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:27
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:65
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
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:154
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:156
static ErrorOr< std::unique_ptr< LTOModule > > createFromBuffer(LLVMContext &Context, const void *mem, size_t length, const TargetOptions &options, StringRef path="")
Definition: LTOModule.cpp:145