xref: /freebsd/contrib/llvm-project/llvm/include/llvm/LTO/legacy/LTOModule.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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"
21 #include "llvm/Object/IRObjectFile.h"
22 #include "llvm/Object/ModuleSymbolTable.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include <string>
26 #include <vector>
27 
28 // Forward references to llvm classes.
29 namespace 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 ///
39 struct LTOModule {
40 private:
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;
60   StringMap<NameAndAttributes> _undefines;
61   std::vector<StringRef> _asm_undefines;
62 
63   LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,
64             TargetMachine *TM);
65 
66 public:
67   LLVM_ABI ~LTOModule();
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();
98   LLVM_ABI static ErrorOr<std::unique_ptr<LTOModule>>
99   createFromFile(LLVMContext &Context, StringRef path,
100                  const TargetOptions &options);
101   LLVM_ABI static ErrorOr<std::unique_ptr<LTOModule>>
102   createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size,
103                      const TargetOptions &options);
104   LLVM_ABI static ErrorOr<std::unique_ptr<LTOModule>>
105   createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path,
106                           size_t map_size, off_t offset,
107                           const TargetOptions &options);
108   LLVM_ABI static ErrorOr<std::unique_ptr<LTOModule>>
109   createFromBuffer(LLVMContext &Context, const void *mem, size_t length,
110                    const TargetOptions &options, StringRef path = "");
111   LLVM_ABI static ErrorOr<std::unique_ptr<LTOModule>>
112   createInLocalContext(std::unique_ptr<LLVMContext> Context, const void *mem,
113                        size_t length, const TargetOptions &options,
114                        StringRef path);
115 
getModuleLTOModule116   const Module &getModule() const { return *Mod; }
getModuleLTOModule117   Module &getModule() { return *Mod; }
118 
takeModuleLTOModule119   std::unique_ptr<Module> takeModule() { return std::move(Mod); }
120 
121   /// Return the Module's target triple.
getTargetTripleLTOModule122   const Triple &getTargetTriple() { return getModule().getTargetTriple(); }
123 
124   /// Set the Module's target triple.
setTargetTripleLTOModule125   void setTargetTriple(Triple T) { getModule().setTargetTriple(T); }
126 
127   /// Get the number of symbols
getSymbolCountLTOModule128   uint32_t getSymbolCount() {
129     return _symbols.size();
130   }
131 
132   /// Get the attributes for a symbol at the specified index.
getSymbolAttributesLTOModule133   lto_symbol_attributes getSymbolAttributes(uint32_t 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.
getSymbolNameLTOModule140   StringRef getSymbolName(uint32_t index) {
141     if (index < _symbols.size())
142       return _symbols[index].name;
143     return StringRef();
144   }
145 
getAsmUndefSymbolCountLTOModule146   uint32_t getAsmUndefSymbolCount() { return _asm_undefines.size(); }
147 
getAsmUndefSymbolNameLTOModule148   StringRef getAsmUndefSymbolName(uint32_t index) {
149     if (index < _asm_undefines.size())
150       return _asm_undefines[index];
151     return StringRef();
152   }
153 
getSymbolGVLTOModule154   const GlobalValue *getSymbolGV(uint32_t index) {
155     if (index < _symbols.size())
156       return _symbols[index].symbol;
157     return nullptr;
158   }
159 
getLinkerOptsLTOModule160   StringRef getLinkerOpts() { return LinkerOpts; }
161 
getAsmUndefinedRefsLTOModule162   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 
169   LLVM_ABI static size_t getDependentLibraryCount(lto::InputFile *input);
170 
171   LLVM_ABI static const char *getDependentLibrary(lto::InputFile *input,
172                                                   size_t index, size_t *size);
173 
174   LLVM_ABI Expected<uint32_t> getMachOCPUType() const;
175 
176   LLVM_ABI Expected<uint32_t> getMachOCPUSubType() const;
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 
182 private:
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).
227   static ErrorOr<std::unique_ptr<LTOModule>>
228   makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
229                 LLVMContext &Context, bool ShouldBeLazy);
230 };
231 }
232 #endif
233