LLVM  9.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"
23 #include <string>
24 #include <vector>
25 
26 // Forward references to llvm classes.
27 namespace llvm {
28  class Function;
29  class GlobalValue;
30  class MemoryBuffer;
31  class TargetOptions;
32  class Value;
33 
34 //===----------------------------------------------------------------------===//
35 /// C++ class which implements the opaque lto_module_t type.
36 ///
37 struct LTOModule {
38 private:
39  struct NameAndAttributes {
41  uint32_t attributes = 0;
42  bool isFunction = 0;
43  const GlobalValue *symbol = 0;
44  };
45 
46  std::unique_ptr<LLVMContext> OwnedContext;
47 
48  std::string LinkerOpts;
49 
50  std::unique_ptr<Module> Mod;
51  MemoryBufferRef MBRef;
52  ModuleSymbolTable SymTab;
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<StringRef> _asm_undefines;
60 
61  LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,
62  TargetMachine *TM);
63 
64 public:
65  ~LTOModule();
66 
67  /// Returns 'true' if the file or memory contents is LLVM bitcode.
68  static bool isBitcodeFile(const void *mem, size_t length);
69  static bool isBitcodeFile(StringRef path);
70 
71  /// Returns 'true' if the Module is produced for ThinLTO.
72  bool isThinLTO();
73 
74  /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
75  /// triple.
76  static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
77  StringRef triplePrefix);
78 
79  /// Returns a string representing the producer identification stored in the
80  /// bitcode, or "" if the bitcode does not contains any.
81  ///
82  static std::string getProducerString(MemoryBuffer *Buffer);
83 
84  /// Create a MemoryBuffer from a memory range with an optional name.
85  static std::unique_ptr<MemoryBuffer>
86  makeBuffer(const void *mem, size_t length, StringRef name = "");
87 
88  /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
89  /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
90  /// and the AsmParsers by calling:
91  ///
92  /// InitializeAllTargets();
93  /// InitializeAllTargetMCs();
94  /// InitializeAllAsmPrinters();
95  /// InitializeAllAsmParsers();
98  const TargetOptions &options);
100  createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size,
101  const TargetOptions &options);
103  createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path,
104  size_t map_size, off_t offset,
105  const TargetOptions &options);
107  createFromBuffer(LLVMContext &Context, const void *mem, size_t length,
108  const TargetOptions &options, StringRef path = "");
110  createInLocalContext(std::unique_ptr<LLVMContext> Context, const void *mem,
111  size_t length, const TargetOptions &options,
112  StringRef path);
113 
114  const Module &getModule() const { return *Mod; }
115  Module &getModule() { return *Mod; }
116 
117  std::unique_ptr<Module> takeModule() { return std::move(Mod); }
118 
119  /// Return the Module's target triple.
120  const std::string &getTargetTriple() {
121  return getModule().getTargetTriple();
122  }
123 
124  /// Set the Module's target triple.
126  getModule().setTargetTriple(Triple);
127  }
128 
129  /// Get the number of symbols
131  return _symbols.size();
132  }
133 
134  /// Get the attributes for a symbol at the specified index.
136  if (index < _symbols.size())
137  return lto_symbol_attributes(_symbols[index].attributes);
138  return lto_symbol_attributes(0);
139  }
140 
141  /// Get the name of the symbol at the specified index.
143  if (index < _symbols.size())
144  return _symbols[index].name;
145  return StringRef();
146  }
147 
149  if (index < _symbols.size())
150  return _symbols[index].symbol;
151  return nullptr;
152  }
153 
154  StringRef getLinkerOpts() { return LinkerOpts; }
155 
156  const std::vector<StringRef> &getAsmUndefinedRefs() { return _asm_undefines; }
157 
158 private:
159  /// Parse metadata from the module
160  // FIXME: it only parses "llvm.linker.options" metadata at the moment
161  void parseMetadata();
162 
163  /// Parse the symbols from the module and model-level ASM and add them to
164  /// either the defined or undefined lists.
165  void parseSymbols();
166 
167  /// Add a symbol which isn't defined just yet to a list to be resolved later.
168  void addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,
169  bool isFunc);
170 
171  /// Add a defined symbol to the list.
172  void addDefinedSymbol(StringRef Name, const GlobalValue *def,
173  bool isFunction);
174 
175  /// Add a data symbol as defined to the list.
176  void addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym);
177  void addDefinedDataSymbol(StringRef Name, const GlobalValue *v);
178 
179  /// Add a function symbol as defined to the list.
180  void addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym);
181  void addDefinedFunctionSymbol(StringRef Name, const Function *F);
182 
183  /// Add a global symbol from module-level ASM to the defined list.
184  void addAsmGlobalSymbol(StringRef, lto_symbol_attributes scope);
185 
186  /// Add a global symbol from module-level ASM to the undefined list.
187  void addAsmGlobalSymbolUndef(StringRef);
188 
189  /// Parse i386/ppc ObjC class data structure.
190  void addObjCClass(const GlobalVariable *clgv);
191 
192  /// Parse i386/ppc ObjC category data structure.
193  void addObjCCategory(const GlobalVariable *clgv);
194 
195  /// Parse i386/ppc ObjC class list data structure.
196  void addObjCClassRef(const GlobalVariable *clgv);
197 
198  /// Get string that the data pointer points to.
199  bool objcClassNameFromExpression(const Constant *c, std::string &name);
200 
201  /// Create an LTOModule (private version).
203  makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
204  LLVMContext &Context, bool ShouldBeLazy);
205 };
206 }
207 #endif
uint32_t getSymbolCount()
Get the number of symbols.
Definition: LTOModule.h:130
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:239
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:64
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
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:117
lto_symbol_attributes getSymbolAttributes(uint32_t index)
Get the attributes for a symbol at the specified index.
Definition: LTOModule.h:135
const GlobalValue * getSymbolGV(uint32_t index)
Definition: LTOModule.h:148
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:120
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:142
Module & getModule()
Definition: LTOModule.h:115
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
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:125
const Module & getModule() const
Definition: LTOModule.h:114
C++ class which implements the opaque lto_module_t type.
Definition: LTOModule.h:37
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
const std::vector< StringRef > & getAsmUndefinedRefs()
Definition: LTOModule.h:156
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:1166
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:238
Deduce function attributes
void setTargetTriple(StringRef T)
Set the target triple.
Definition: Module.h:282
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:154
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:58
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:153
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
Definition: PointerUnion.h:86
static ErrorOr< std::unique_ptr< LTOModule > > createFromBuffer(LLVMContext &Context, const void *mem, size_t length, const TargetOptions &options, StringRef path="")
Definition: LTOModule.cpp:144