Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:11

0001 //===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file declares the LTOModule class.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_LTO_LEGACY_LTOMODULE_H
0014 #define LLVM_LTO_LEGACY_LTOMODULE_H
0015 
0016 #include "llvm-c/lto.h"
0017 #include "llvm/ADT/StringMap.h"
0018 #include "llvm/ADT/StringSet.h"
0019 #include "llvm/IR/Module.h"
0020 #include "llvm/LTO/LTO.h"
0021 #include "llvm/Object/IRObjectFile.h"
0022 #include "llvm/Object/ModuleSymbolTable.h"
0023 #include "llvm/Target/TargetMachine.h"
0024 #include <string>
0025 #include <vector>
0026 
0027 // Forward references to llvm classes.
0028 namespace llvm {
0029   class Function;
0030   class GlobalValue;
0031   class MemoryBuffer;
0032   class TargetOptions;
0033   class Value;
0034 
0035 //===----------------------------------------------------------------------===//
0036 /// C++ class which implements the opaque lto_module_t type.
0037 ///
0038 struct LTOModule {
0039 private:
0040   struct NameAndAttributes {
0041     StringRef name;
0042     uint32_t           attributes = 0;
0043     bool               isFunction = false;
0044     const GlobalValue *symbol = nullptr;
0045   };
0046 
0047   std::unique_ptr<LLVMContext> OwnedContext;
0048 
0049   std::string LinkerOpts;
0050 
0051   std::unique_ptr<Module> Mod;
0052   MemoryBufferRef MBRef;
0053   ModuleSymbolTable SymTab;
0054   std::unique_ptr<TargetMachine> _target;
0055   std::vector<NameAndAttributes> _symbols;
0056 
0057   // _defines and _undefines only needed to disambiguate tentative definitions
0058   StringSet<>                             _defines;
0059   StringMap<NameAndAttributes> _undefines;
0060   std::vector<StringRef> _asm_undefines;
0061 
0062   LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,
0063             TargetMachine *TM);
0064 
0065 public:
0066   ~LTOModule();
0067 
0068   /// Returns 'true' if the file or memory contents is LLVM bitcode.
0069   static bool isBitcodeFile(const void *mem, size_t length);
0070   static bool isBitcodeFile(StringRef path);
0071 
0072   /// Returns 'true' if the Module is produced for ThinLTO.
0073   bool isThinLTO();
0074 
0075   /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
0076   /// triple.
0077   static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
0078                                  StringRef triplePrefix);
0079 
0080   /// Returns a string representing the producer identification stored in the
0081   /// bitcode, or "" if the bitcode does not contains any.
0082   ///
0083   static std::string getProducerString(MemoryBuffer *Buffer);
0084 
0085   /// Create a MemoryBuffer from a memory range with an optional name.
0086   static std::unique_ptr<MemoryBuffer>
0087   makeBuffer(const void *mem, size_t length, StringRef name = "");
0088 
0089   /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
0090   /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
0091   /// and the AsmParsers by calling:
0092   ///
0093   /// InitializeAllTargets();
0094   /// InitializeAllTargetMCs();
0095   /// InitializeAllAsmPrinters();
0096   /// InitializeAllAsmParsers();
0097   static ErrorOr<std::unique_ptr<LTOModule>>
0098   createFromFile(LLVMContext &Context, StringRef path,
0099                  const TargetOptions &options);
0100   static ErrorOr<std::unique_ptr<LTOModule>>
0101   createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size,
0102                      const TargetOptions &options);
0103   static ErrorOr<std::unique_ptr<LTOModule>>
0104   createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path,
0105                           size_t map_size, off_t offset,
0106                           const TargetOptions &options);
0107   static ErrorOr<std::unique_ptr<LTOModule>>
0108   createFromBuffer(LLVMContext &Context, const void *mem, size_t length,
0109                    const TargetOptions &options, StringRef path = "");
0110   static ErrorOr<std::unique_ptr<LTOModule>>
0111   createInLocalContext(std::unique_ptr<LLVMContext> Context, const void *mem,
0112                        size_t length, const TargetOptions &options,
0113                        StringRef path);
0114 
0115   const Module &getModule() const { return *Mod; }
0116   Module &getModule() { return *Mod; }
0117 
0118   std::unique_ptr<Module> takeModule() { return std::move(Mod); }
0119 
0120   /// Return the Module's target triple.
0121   const std::string &getTargetTriple() {
0122     return getModule().getTargetTriple();
0123   }
0124 
0125   /// Set the Module's target triple.
0126   void setTargetTriple(StringRef Triple) {
0127     getModule().setTargetTriple(Triple);
0128   }
0129 
0130   /// Get the number of symbols
0131   uint32_t getSymbolCount() {
0132     return _symbols.size();
0133   }
0134 
0135   /// Get the attributes for a symbol at the specified index.
0136   lto_symbol_attributes getSymbolAttributes(uint32_t index) {
0137     if (index < _symbols.size())
0138       return lto_symbol_attributes(_symbols[index].attributes);
0139     return lto_symbol_attributes(0);
0140   }
0141 
0142   /// Get the name of the symbol at the specified index.
0143   StringRef getSymbolName(uint32_t index) {
0144     if (index < _symbols.size())
0145       return _symbols[index].name;
0146     return StringRef();
0147   }
0148 
0149   const GlobalValue *getSymbolGV(uint32_t index) {
0150     if (index < _symbols.size())
0151       return _symbols[index].symbol;
0152     return nullptr;
0153   }
0154 
0155   StringRef getLinkerOpts() { return LinkerOpts; }
0156 
0157   const std::vector<StringRef> &getAsmUndefinedRefs() { return _asm_undefines; }
0158 
0159   static lto::InputFile *createInputFile(const void *buffer, size_t buffer_size,
0160                                          const char *path, std::string &out_error);
0161 
0162   static size_t getDependentLibraryCount(lto::InputFile *input);
0163 
0164   static const char *getDependentLibrary(lto::InputFile *input, size_t index, size_t *size);
0165 
0166   Expected<uint32_t> getMachOCPUType() const;
0167 
0168   Expected<uint32_t> getMachOCPUSubType() const;
0169 
0170   /// Returns true if the module has either the @llvm.global_ctors or the
0171   /// @llvm.global_dtors symbol. Otherwise returns false.
0172   bool hasCtorDtor() const;
0173 
0174 private:
0175   /// Parse metadata from the module
0176   // FIXME: it only parses "llvm.linker.options" metadata at the moment
0177   // FIXME: can't access metadata in lazily loaded modules
0178   void parseMetadata();
0179 
0180   /// Parse the symbols from the module and model-level ASM and add them to
0181   /// either the defined or undefined lists.
0182   void parseSymbols();
0183 
0184   /// Add a symbol which isn't defined just yet to a list to be resolved later.
0185   void addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,
0186                                    bool isFunc);
0187 
0188   /// Add a defined symbol to the list.
0189   void addDefinedSymbol(StringRef Name, const GlobalValue *def,
0190                         bool isFunction);
0191 
0192   /// Add a data symbol as defined to the list.
0193   void addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym);
0194   void addDefinedDataSymbol(StringRef Name, const GlobalValue *v);
0195 
0196   /// Add a function symbol as defined to the list.
0197   void addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym);
0198   void addDefinedFunctionSymbol(StringRef Name, const GlobalValue *F);
0199 
0200   /// Add a global symbol from module-level ASM to the defined list.
0201   void addAsmGlobalSymbol(StringRef, lto_symbol_attributes scope);
0202 
0203   /// Add a global symbol from module-level ASM to the undefined list.
0204   void addAsmGlobalSymbolUndef(StringRef);
0205 
0206   /// Parse i386/ppc ObjC class data structure.
0207   void addObjCClass(const GlobalVariable *clgv);
0208 
0209   /// Parse i386/ppc ObjC category data structure.
0210   void addObjCCategory(const GlobalVariable *clgv);
0211 
0212   /// Parse i386/ppc ObjC class list data structure.
0213   void addObjCClassRef(const GlobalVariable *clgv);
0214 
0215   /// Get string that the data pointer points to.
0216   bool objcClassNameFromExpression(const Constant *c, std::string &name);
0217 
0218   /// Create an LTOModule (private version).
0219   static ErrorOr<std::unique_ptr<LTOModule>>
0220   makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
0221                 LLVMContext &Context, bool ShouldBeLazy);
0222 };
0223 }
0224 #endif