LLVM  14.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"
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::unique_ptr<Module> Mod;
52  MemoryBufferRef MBRef;
53  ModuleSymbolTable SymTab;
54  std::unique_ptr<TargetMachine> _target;
55  std::vector<NameAndAttributes> _symbols;
56 
57  // _defines and _undefines only needed to disambiguate tentative definitions
58  StringSet<> _defines;
60  std::vector<StringRef> _asm_undefines;
61 
62  LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,
63  TargetMachine *TM);
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(StringRef path);
71 
72  /// Returns 'true' if the Module is produced for ThinLTO.
73  bool isThinLTO();
74 
75  /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
76  /// triple.
77  static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
78  StringRef triplePrefix);
79 
80  /// Returns a string representing the producer identification stored in the
81  /// bitcode, or "" if the bitcode does not contains any.
82  ///
83  static std::string getProducerString(MemoryBuffer *Buffer);
84 
85  /// Create a MemoryBuffer from a memory range with an optional name.
86  static std::unique_ptr<MemoryBuffer>
87  makeBuffer(const void *mem, size_t length, StringRef name = "");
88 
89  /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
90  /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
91  /// and the AsmParsers by calling:
92  ///
93  /// InitializeAllTargets();
94  /// InitializeAllTargetMCs();
95  /// InitializeAllAsmPrinters();
96  /// InitializeAllAsmParsers();
99  const TargetOptions &options);
101  createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size,
102  const TargetOptions &options);
105  size_t map_size, off_t offset,
106  const TargetOptions &options);
108  createFromBuffer(LLVMContext &Context, const void *mem, size_t length,
109  const TargetOptions &options, StringRef path = "");
111  createInLocalContext(std::unique_ptr<LLVMContext> Context, const void *mem,
112  size_t length, const TargetOptions &options,
113  StringRef path);
114 
115  const Module &getModule() const { return *Mod; }
116  Module &getModule() { return *Mod; }
117 
118  std::unique_ptr<Module> takeModule() { return std::move(Mod); }
119 
120  /// Return the Module's target triple.
121  const std::string &getTargetTriple() {
122  return getModule().getTargetTriple();
123  }
124 
125  /// Set the Module's target triple.
128  }
129 
130  /// Get the number of symbols
132  return _symbols.size();
133  }
134 
135  /// Get the attributes for a symbol at the specified index.
137  if (index < _symbols.size())
138  return lto_symbol_attributes(_symbols[index].attributes);
139  return lto_symbol_attributes(0);
140  }
141 
142  /// Get the name of the symbol at the specified index.
144  if (index < _symbols.size())
145  return _symbols[index].name;
146  return StringRef();
147  }
148 
150  if (index < _symbols.size())
151  return _symbols[index].symbol;
152  return nullptr;
153  }
154 
155  StringRef getLinkerOpts() { return LinkerOpts; }
156 
157  const std::vector<StringRef> &getAsmUndefinedRefs() { return _asm_undefines; }
158 
159  static lto::InputFile *createInputFile(const void *buffer, size_t buffer_size,
160  const char *path, std::string &out_error);
161 
163 
164  static const char *getDependentLibrary(lto::InputFile *input, size_t index, size_t *size);
165 
167 
169 
170  /// Returns true if the module has either the @llvm.global_ctors or the
171  /// @llvm.global_dtors symbol. Otherwise returns false.
172  bool hasCtorDtor() const;
173 
174 private:
175  /// Parse metadata from the module
176  // FIXME: it only parses "llvm.linker.options" metadata at the moment
177  // FIXME: can't access metadata in lazily loaded modules
178  void parseMetadata();
179 
180  /// Parse the symbols from the module and model-level ASM and add them to
181  /// either the defined or undefined lists.
182  void parseSymbols();
183 
184  /// Add a symbol which isn't defined just yet to a list to be resolved later.
185  void addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,
186  bool isFunc);
187 
188  /// Add a defined symbol to the list.
189  void addDefinedSymbol(StringRef Name, const GlobalValue *def,
190  bool isFunction);
191 
192  /// Add a data symbol as defined to the list.
193  void addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym);
194  void addDefinedDataSymbol(StringRef Name, const GlobalValue *v);
195 
196  /// Add a function symbol as defined to the list.
197  void addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym);
198  void addDefinedFunctionSymbol(StringRef Name, const Function *F);
199 
200  /// Add a global symbol from module-level ASM to the defined list.
201  void addAsmGlobalSymbol(StringRef, lto_symbol_attributes scope);
202 
203  /// Add a global symbol from module-level ASM to the undefined list.
204  void addAsmGlobalSymbolUndef(StringRef);
205 
206  /// Parse i386/ppc ObjC class data structure.
207  void addObjCClass(const GlobalVariable *clgv);
208 
209  /// Parse i386/ppc ObjC category data structure.
210  void addObjCCategory(const GlobalVariable *clgv);
211 
212  /// Parse i386/ppc ObjC class list data structure.
213  void addObjCClassRef(const GlobalVariable *clgv);
214 
215  /// Get string that the data pointer points to.
216  bool objcClassNameFromExpression(const Constant *c, std::string &name);
217 
218  /// Create an LTOModule (private version).
220  makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
221  LLVMContext &Context, bool ShouldBeLazy);
222 };
223 }
224 #endif
llvm::LTOModule::createInLocalContext
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:156
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::TargetOptions
Definition: TargetOptions.h:124
llvm::Function
Definition: Function.h:61
llvm::LTOModule::takeModule
std::unique_ptr< Module > takeModule()
Definition: LTOModule.h:118
llvm::LTOModule::getTargetTriple
const std::string & getTargetTriple()
Return the Module's target triple.
Definition: LTOModule.h:121
llvm::LTOModule::getAsmUndefinedRefs
const std::vector< StringRef > & getAsmUndefinedRefs()
Definition: LTOModule.h:157
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::LTOModule::createFromFile
static ErrorOr< std::unique_ptr< LTOModule > > createFromFile(LLVMContext &Context, StringRef path, const TargetOptions &options)
Create an LTOModule.
Definition: LTOModule.cpp:111
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::LTOModule::getSymbolAttributes
lto_symbol_attributes getSymbolAttributes(uint32_t index)
Get the attributes for a symbol at the specified index.
Definition: LTOModule.h:136
Module.h
attributes
Deduce and propagate attributes
Definition: Attributor.cpp:3074
llvm::LTOModule::createFromOpenFileSlice
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:131
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::LTOModule::isThinLTO
bool isThinLTO()
Returns 'true' if the Module is produced for ThinLTO.
Definition: LTOModule.cpp:74
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:50
F
#define F(x, y, z)
Definition: MD5.cpp:56
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
TargetMachine.h
llvm::LTOModule::getDependentLibraryCount
static size_t getDependentLibraryCount(lto::InputFile *input)
Definition: LTOModule.cpp:673
llvm::LTOModule::isBitcodeFile
static bool isBitcodeFile(const void *mem, size_t length)
Returns 'true' if the file or memory contents is LLVM bitcode.
Definition: LTOModule.cpp:57
llvm::LTOModule::makeBuffer
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:244
llvm::LTOModule::hasCtorDtor
bool hasCtorDtor() const
Returns true if the module has either the @llvm.global_ctors or the @llvm.global_dtors symbol.
Definition: LTOModule.cpp:692
llvm::LTOModule::getLinkerOpts
StringRef getLinkerOpts()
Definition: LTOModule.h:155
llvm::LTOModule::createInputFile
static lto::InputFile * createInputFile(const void *buffer, size_t buffer_size, const char *path, std::string &out_error)
Definition: LTOModule.cpp:656
StringMap.h
input
The initial backend is deliberately restricted to z10 We should add support for later architectures at some point If an asm ties an i32 r result to an i64 input
Definition: README.txt:10
c
the resulting code requires compare and branches when and if the revised code is with conditional branches instead of More there is a byte word extend before each where there should be only and the condition codes are not remembered when the same two values are compared twice More LSR enhancements i8 and i32 load store addressing modes are identical int int c
Definition: README.txt:418
lto.h
llvm::Module::setTargetTriple
void setTargetTriple(StringRef T)
Set the target triple.
Definition: Module.h:300
llvm::LTOModule::setTargetTriple
void setTargetTriple(StringRef Triple)
Set the Module's target triple.
Definition: LTOModule.h:126
llvm::StringMap< NameAndAttributes >
IRObjectFile.h
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::LTOModule::isBitcodeForTarget
static bool isBitcodeForTarget(MemoryBuffer *memBuffer, StringRef triplePrefix)
Returns 'true' if the memory buffer is LLVM bitcode for the specified triple.
Definition: LTOModule.cpp:83
index
splat index
Definition: README_ALTIVEC.txt:181
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::LTOModule::getDependentLibrary
static const char * getDependentLibrary(lto::InputFile *input, size_t index, size_t *size)
Definition: LTOModule.cpp:677
llvm::LTOModule::getMachOCPUSubType
Expected< uint32_t > getMachOCPUSubType() const
Definition: LTOModule.cpp:688
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:79
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::LTOModule::getProducerString
static std::string getProducerString(MemoryBuffer *Buffer)
Returns a string representing the producer identification stored in the bitcode, or "" if the bitcode...
Definition: LTOModule.cpp:97
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1532
ModuleSymbolTable.h
llvm::StringSet
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:22
llvm::LTOModule::getModule
const Module & getModule() const
Definition: LTOModule.h:115
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
uint32_t
llvm::LTOModule::createFromBuffer
static ErrorOr< std::unique_ptr< LTOModule > > createFromBuffer(LLVMContext &Context, const void *mem, size_t length, const TargetOptions &options, StringRef path="")
Definition: LTOModule.cpp:147
llvm::PointerUnion
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:119
StringSet.h
name
static const char * name
Definition: SVEIntrinsicOpts.cpp:78
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::LTOModule::getSymbolCount
uint32_t getSymbolCount()
Get the number of symbols.
Definition: LTOModule.h:131
llvm::LTOModule::~LTOModule
~LTOModule()
Definition: LTOModule.cpp:53
options
The object format emitted by the WebAssembly backed is documented see the home and packaging for producing WebAssembly applications that can run in browsers and other environments wasi sdk provides a more minimal C C SDK based on llvm and a libc based on for producing WebAssemmbly applictions that use the WASI ABI Rust provides WebAssembly support integrated into Cargo There are two main options
Definition: README.txt:24
llvm::LTOModule::getSymbolGV
const GlobalValue * getSymbolGV(uint32_t index)
Definition: LTOModule.h:149
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::LTOModule::createFromOpenFile
static ErrorOr< std::unique_ptr< LTOModule > > createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size, const TargetOptions &options)
Definition: LTOModule.cpp:125
lto_symbol_attributes
lto_symbol_attributes
Definition: lto.h:54
llvm::ModuleSymbolTable
Definition: ModuleSymbolTable.h:33
LTO.h
llvm::Module::getTargetTriple
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:257
llvm::LTOModule::getSymbolName
StringRef getSymbolName(uint32_t index)
Get the name of the symbol at the specified index.
Definition: LTOModule.h:143
llvm::ErrorOr
Represents either an error or a value T.
Definition: ErrorOr.h:56
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::LTOModule::getMachOCPUType
Expected< uint32_t > getMachOCPUType() const
Definition: LTOModule.cpp:684
llvm::LTOModule::getModule
Module & getModule()
Definition: LTOModule.h:116
Mod
Module * Mod
Definition: PassBuilderBindings.cpp:54
llvm::LTOModule
C++ class which implements the opaque lto_module_t type.
Definition: LTOModule.h:38
llvm::codeview::PublicSymFlags::Function
@ Function
llvm::lto::InputFile
An input file.
Definition: LTO.h:105