1 //===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file declares the LTOModule class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_LTO_LTOMODULE_H
14 #define LLVM_LTO_LTOMODULE_H
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"
21 #include "llvm/Object/IRObjectFile.h"
22 #include "llvm/Object/ModuleSymbolTable.h"
23 #include "llvm/Target/TargetMachine.h"
27 // Forward references to llvm classes.
35 //===----------------------------------------------------------------------===//
36 /// C++ class which implements the opaque lto_module_t type.
40 struct NameAndAttributes
{
42 uint32_t attributes
= 0;
44 const GlobalValue
*symbol
= 0;
47 std::unique_ptr
<LLVMContext
> OwnedContext
;
49 std::string LinkerOpts
;
51 std::string DependentLibraries
;
53 std::unique_ptr
<Module
> Mod
;
54 MemoryBufferRef MBRef
;
55 ModuleSymbolTable SymTab
;
56 std::unique_ptr
<TargetMachine
> _target
;
57 std::vector
<NameAndAttributes
> _symbols
;
59 // _defines and _undefines only needed to disambiguate tentative definitions
61 StringMap
<NameAndAttributes
> _undefines
;
62 std::vector
<StringRef
> _asm_undefines
;
64 LTOModule(std::unique_ptr
<Module
> M
, MemoryBufferRef MBRef
,
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
);
74 /// Returns 'true' if the Module is produced for ThinLTO.
77 /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
79 static bool isBitcodeForTarget(MemoryBuffer
*memBuffer
,
80 StringRef triplePrefix
);
82 /// Returns a string representing the producer identification stored in the
83 /// bitcode, or "" if the bitcode does not contains any.
85 static std::string
getProducerString(MemoryBuffer
*Buffer
);
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
= "");
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:
95 /// InitializeAllTargets();
96 /// InitializeAllTargetMCs();
97 /// InitializeAllAsmPrinters();
98 /// InitializeAllAsmParsers();
99 static ErrorOr
<std::unique_ptr
<LTOModule
>>
100 createFromFile(LLVMContext
&Context
, StringRef path
,
101 const TargetOptions
&options
);
102 static ErrorOr
<std::unique_ptr
<LTOModule
>>
103 createFromOpenFile(LLVMContext
&Context
, int fd
, StringRef path
, size_t size
,
104 const TargetOptions
&options
);
105 static ErrorOr
<std::unique_ptr
<LTOModule
>>
106 createFromOpenFileSlice(LLVMContext
&Context
, int fd
, StringRef path
,
107 size_t map_size
, off_t offset
,
108 const TargetOptions
&options
);
109 static ErrorOr
<std::unique_ptr
<LTOModule
>>
110 createFromBuffer(LLVMContext
&Context
, const void *mem
, size_t length
,
111 const TargetOptions
&options
, StringRef path
= "");
112 static ErrorOr
<std::unique_ptr
<LTOModule
>>
113 createInLocalContext(std::unique_ptr
<LLVMContext
> Context
, const void *mem
,
114 size_t length
, const TargetOptions
&options
,
117 const Module
&getModule() const { return *Mod
; }
118 Module
&getModule() { return *Mod
; }
120 std::unique_ptr
<Module
> takeModule() { return std::move(Mod
); }
122 /// Return the Module's target triple.
123 const std::string
&getTargetTriple() {
124 return getModule().getTargetTriple();
127 /// Set the Module's target triple.
128 void setTargetTriple(StringRef Triple
) {
129 getModule().setTargetTriple(Triple
);
132 /// Get the number of symbols
133 uint32_t getSymbolCount() {
134 return _symbols
.size();
137 /// Get the attributes for a symbol at the specified index.
138 lto_symbol_attributes
getSymbolAttributes(uint32_t index
) {
139 if (index
< _symbols
.size())
140 return lto_symbol_attributes(_symbols
[index
].attributes
);
141 return lto_symbol_attributes(0);
144 /// Get the name of the symbol at the specified index.
145 StringRef
getSymbolName(uint32_t index
) {
146 if (index
< _symbols
.size())
147 return _symbols
[index
].name
;
151 const GlobalValue
*getSymbolGV(uint32_t index
) {
152 if (index
< _symbols
.size())
153 return _symbols
[index
].symbol
;
157 StringRef
getLinkerOpts() { return LinkerOpts
; }
159 const std::vector
<StringRef
> &getAsmUndefinedRefs() { return _asm_undefines
; }
161 static lto::InputFile
*createInputFile(const void *buffer
, size_t buffer_size
,
162 const char *path
, std::string
&out_error
);
164 static size_t getDependentLibraryCount(lto::InputFile
*input
);
166 static const char *getDependentLibrary(lto::InputFile
*input
, size_t index
, size_t *size
);
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();
174 /// Parse the symbols from the module and model-level ASM and add them to
175 /// either the defined or undefined lists.
178 /// Add a symbol which isn't defined just yet to a list to be resolved later.
179 void addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym
,
182 /// Add a defined symbol to the list.
183 void addDefinedSymbol(StringRef Name
, const GlobalValue
*def
,
186 /// Add a data symbol as defined to the list.
187 void addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym
);
188 void addDefinedDataSymbol(StringRef Name
, const GlobalValue
*v
);
190 /// Add a function symbol as defined to the list.
191 void addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym
);
192 void addDefinedFunctionSymbol(StringRef Name
, const Function
*F
);
194 /// Add a global symbol from module-level ASM to the defined list.
195 void addAsmGlobalSymbol(StringRef
, lto_symbol_attributes scope
);
197 /// Add a global symbol from module-level ASM to the undefined list.
198 void addAsmGlobalSymbolUndef(StringRef
);
200 /// Parse i386/ppc ObjC class data structure.
201 void addObjCClass(const GlobalVariable
*clgv
);
203 /// Parse i386/ppc ObjC category data structure.
204 void addObjCCategory(const GlobalVariable
*clgv
);
206 /// Parse i386/ppc ObjC class list data structure.
207 void addObjCClassRef(const GlobalVariable
*clgv
);
209 /// Get string that the data pointer points to.
210 bool objcClassNameFromExpression(const Constant
*c
, std::string
&name
);
212 /// Create an LTOModule (private version).
213 static ErrorOr
<std::unique_ptr
<LTOModule
>>
214 makeLTOModule(MemoryBufferRef Buffer
, const TargetOptions
&options
,
215 LLVMContext
&Context
, bool ShouldBeLazy
);