LLVM 22.0.0git
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_LEGACY_LTOMODULE_H
14#define LLVM_LTO_LEGACY_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"
25#include <string>
26#include <vector>
27
28// Forward references to llvm classes.
29namespace 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///
39struct LTOModule {
40private:
41 struct NameAndAttributes {
42 StringRef name;
43 uint32_t attributes = 0;
44 bool isFunction = false;
45 const GlobalValue *symbol = nullptr;
46 };
47
48 std::unique_ptr<LLVMContext> OwnedContext;
49
50 std::string LinkerOpts;
51
52 std::unique_ptr<Module> Mod;
53 MemoryBufferRef MBRef;
54 ModuleSymbolTable SymTab;
55 std::unique_ptr<TargetMachine> _target;
56 std::vector<NameAndAttributes> _symbols;
57
58 // _defines and _undefines only needed to disambiguate tentative definitions
59 StringSet<> _defines;
61 std::vector<StringRef> _asm_undefines;
62
63 LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,
64 TargetMachine *TM);
65
66public:
68
69 /// Returns 'true' if the file or memory contents is LLVM bitcode.
70 LLVM_ABI static bool isBitcodeFile(const void *mem, size_t length);
71 LLVM_ABI static bool isBitcodeFile(StringRef path);
72
73 /// Returns 'true' if the Module is produced for ThinLTO.
74 LLVM_ABI bool isThinLTO();
75
76 /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
77 /// triple.
78 LLVM_ABI static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
79 StringRef triplePrefix);
80
81 /// Returns a string representing the producer identification stored in the
82 /// bitcode, or "" if the bitcode does not contains any.
83 ///
84 LLVM_ABI static std::string getProducerString(MemoryBuffer *Buffer);
85
86 /// Create a MemoryBuffer from a memory range with an optional name.
87 LLVM_ABI static std::unique_ptr<MemoryBuffer>
88 makeBuffer(const void *mem, size_t length, StringRef name = "");
89
90 /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
91 /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
92 /// and the AsmParsers by calling:
93 ///
94 /// InitializeAllTargets();
95 /// InitializeAllTargetMCs();
96 /// InitializeAllAsmPrinters();
97 /// InitializeAllAsmParsers();
100 const TargetOptions &options);
102 createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size,
103 const TargetOptions &options);
105 createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path,
106 size_t map_size, off_t offset,
107 const TargetOptions &options);
109 createFromBuffer(LLVMContext &Context, const void *mem, size_t length,
110 const TargetOptions &options, StringRef path = "");
112 createInLocalContext(std::unique_ptr<LLVMContext> Context, const void *mem,
113 size_t length, const TargetOptions &options,
114 StringRef path);
115
116 const Module &getModule() const { return *Mod; }
117 Module &getModule() { return *Mod; }
118
119 std::unique_ptr<Module> takeModule() { return std::move(Mod); }
120
121 /// Return the Module's target triple.
123
124 /// Set the Module's target triple.
126
127 /// Get the number of symbols
129 return _symbols.size();
130 }
131
132 /// Get the attributes for a symbol at the specified index.
134 if (index < _symbols.size())
135 return lto_symbol_attributes(_symbols[index].attributes);
136 return lto_symbol_attributes(0);
137 }
138
139 /// Get the name of the symbol at the specified index.
141 if (index < _symbols.size())
142 return _symbols[index].name;
143 return StringRef();
144 }
145
146 uint32_t getAsmUndefSymbolCount() { return _asm_undefines.size(); }
147
149 if (index < _asm_undefines.size())
150 return _asm_undefines[index];
151 return StringRef();
152 }
153
155 if (index < _symbols.size())
156 return _symbols[index].symbol;
157 return nullptr;
158 }
159
160 StringRef getLinkerOpts() { return LinkerOpts; }
161
162 const std::vector<StringRef> &getAsmUndefinedRefs() { return _asm_undefines; }
163
164 LLVM_ABI static lto::InputFile *createInputFile(const void *buffer,
165 size_t buffer_size,
166 const char *path,
167 std::string &out_error);
168
170
171 LLVM_ABI static const char *getDependentLibrary(lto::InputFile *input,
172 size_t index, size_t *size);
173
175
177
178 /// Returns true if the module has either the @llvm.global_ctors or the
179 /// @llvm.global_dtors symbol. Otherwise returns false.
180 LLVM_ABI bool hasCtorDtor() const;
181
182private:
183 /// Parse metadata from the module
184 // FIXME: it only parses "llvm.linker.options" metadata at the moment
185 // FIXME: can't access metadata in lazily loaded modules
186 void parseMetadata();
187
188 /// Parse the symbols from the module and model-level ASM and add them to
189 /// either the defined or undefined lists.
190 void parseSymbols();
191
192 /// Add a symbol which isn't defined just yet to a list to be resolved later.
193 void addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,
194 bool isFunc);
195
196 /// Add a defined symbol to the list.
197 void addDefinedSymbol(StringRef Name, const GlobalValue *def,
198 bool isFunction);
199
200 /// Add a data symbol as defined to the list.
201 void addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym);
202 void addDefinedDataSymbol(StringRef Name, const GlobalValue *v);
203
204 /// Add a function symbol as defined to the list.
205 void addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym);
206 void addDefinedFunctionSymbol(StringRef Name, const GlobalValue *F);
207
208 /// Add a global symbol from module-level ASM to the defined list.
209 void addAsmGlobalSymbol(StringRef, lto_symbol_attributes scope);
210
211 /// Add a global symbol from module-level ASM to the undefined list.
212 void addAsmGlobalSymbolUndef(StringRef);
213
214 /// Parse i386/ppc ObjC class data structure.
215 void addObjCClass(const GlobalVariable *clgv);
216
217 /// Parse i386/ppc ObjC category data structure.
218 void addObjCCategory(const GlobalVariable *clgv);
219
220 /// Parse i386/ppc ObjC class list data structure.
221 void addObjCClassRef(const GlobalVariable *clgv);
222
223 /// Get string that the data pointer points to.
224 bool objcClassNameFromExpression(const Constant *c, std::string &name);
225
226 /// Create an LTOModule (private version).
228 makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
229 LLVMContext &Context, bool ShouldBeLazy);
230};
231}
232#endif
This file defines the StringMap class.
#define LLVM_ABI
Definition: Compiler.h:213
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod
static const char * name
Definition: SMEABIPass.cpp:52
static bool isFunction(SDValue Op)
StringSet - A set-like wrapper for the StringMap.
This is an important base class in LLVM.
Definition: Constant.h:43
Represents either an error or a value T.
Definition: ErrorOr.h:56
Tagged union holding either a T or a Error.
Definition: Error.h:485
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:52
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
const Triple & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:281
void setTargetTriple(Triple T)
Set the target triple.
Definition: Module.h:324
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:118
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:25
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:83
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
An input file.
Definition: LTO.h:118
lto_symbol_attributes
Definition: lto.h:54
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
C++ class which implements the opaque lto_module_t type.
Definition: LTOModule.h:39
StringRef getSymbolName(uint32_t index)
Get the name of the symbol at the specified index.
Definition: LTOModule.h:140
const GlobalValue * getSymbolGV(uint32_t index)
Definition: LTOModule.h:154
static LLVM_ABI ErrorOr< std::unique_ptr< LTOModule > > createFromFile(LLVMContext &Context, StringRef path, const TargetOptions &options)
Create an LTOModule.
Definition: LTOModule.cpp:107
static LLVM_ABI bool isBitcodeFile(const void *mem, size_t length)
Returns 'true' if the file or memory contents is LLVM bitcode.
Definition: LTOModule.cpp:53
LLVM_ABI Expected< uint32_t > getMachOCPUType() const
Definition: LTOModule.cpp:689
static LLVM_ABI 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:241
uint32_t getSymbolCount()
Get the number of symbols.
Definition: LTOModule.h:128
static LLVM_ABI size_t getDependentLibraryCount(lto::InputFile *input)
Definition: LTOModule.cpp:678
uint32_t getAsmUndefSymbolCount()
Definition: LTOModule.h:146
Module & getModule()
Definition: LTOModule.h:117
LLVM_ABI bool hasCtorDtor() const
Returns true if the module has either the @llvm.global_ctors or the @llvm.global_dtors symbol.
Definition: LTOModule.cpp:697
static LLVM_ABI const char * getDependentLibrary(lto::InputFile *input, size_t index, size_t *size)
Definition: LTOModule.cpp:682
lto_symbol_attributes getSymbolAttributes(uint32_t index)
Get the attributes for a symbol at the specified index.
Definition: LTOModule.h:133
const Module & getModule() const
Definition: LTOModule.h:116
static LLVM_ABI 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:127
StringRef getLinkerOpts()
Definition: LTOModule.h:160
static LLVM_ABI std::string getProducerString(MemoryBuffer *Buffer)
Returns a string representing the producer identification stored in the bitcode, or "" if the bitcode...
Definition: LTOModule.cpp:93
const std::vector< StringRef > & getAsmUndefinedRefs()
Definition: LTOModule.h:162
static LLVM_ABI bool isBitcodeForTarget(MemoryBuffer *memBuffer, StringRef triplePrefix)
Returns 'true' if the memory buffer is LLVM bitcode for the specified triple.
Definition: LTOModule.cpp:79
LLVM_ABI bool isThinLTO()
Returns 'true' if the Module is produced for ThinLTO.
Definition: LTOModule.cpp:70
void setTargetTriple(Triple T)
Set the Module's target triple.
Definition: LTOModule.h:125
static LLVM_ABI ErrorOr< std::unique_ptr< LTOModule > > createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size, const TargetOptions &options)
Definition: LTOModule.cpp:121
static LLVM_ABI 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:152
static LLVM_ABI ErrorOr< std::unique_ptr< LTOModule > > createFromBuffer(LLVMContext &Context, const void *mem, size_t length, const TargetOptions &options, StringRef path="")
Definition: LTOModule.cpp:143
LLVM_ABI Expected< uint32_t > getMachOCPUSubType() const
Definition: LTOModule.cpp:693
LLVM_ABI ~LTOModule()
std::unique_ptr< Module > takeModule()
Definition: LTOModule.h:119
const Triple & getTargetTriple()
Return the Module's target triple.
Definition: LTOModule.h:122
static LLVM_ABI lto::InputFile * createInputFile(const void *buffer, size_t buffer_size, const char *path, std::string &out_error)
Definition: LTOModule.cpp:661
StringRef getAsmUndefSymbolName(uint32_t index)
Definition: LTOModule.h:148