Fixed/added namespace ending comments using clang-tidy. NFC
[oota-llvm.git] / include / llvm / LTO / LTOModule.h
1 //===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the LTOModule class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_LTO_LTOMODULE_H
15 #define LLVM_LTO_LTOMODULE_H
16
17 #include "llvm-c/lto.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringSet.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCObjectFileInfo.h"
23 #include "llvm/Object/IRObjectFile.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     const char        *name;
43     uint32_t           attributes;
44     bool               isFunction;
45     const GlobalValue *symbol;
46   };
47
48   std::unique_ptr<LLVMContext> OwnedContext;
49
50   std::unique_ptr<object::IRObjectFile> IRFile;
51   std::unique_ptr<TargetMachine> _target;
52   StringSet<>                             _linkeropt_strings;
53   std::vector<const char *>               _deplibs;
54   std::vector<const char *>               _linkeropts;
55   std::vector<NameAndAttributes>          _symbols;
56
57   // _defines and _undefines only needed to disambiguate tentative definitions
58   StringSet<>                             _defines;
59   StringMap<NameAndAttributes> _undefines;
60   std::vector<const char*>                _asm_undefines;
61
62   LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM);
63   LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM,
64             std::unique_ptr<LLVMContext> Context);
65
66 public:
67   ~LTOModule();
68
69   /// Returns 'true' if the file or memory contents is LLVM bitcode.
70   static bool isBitcodeFile(const void *mem, size_t length);
71   static bool isBitcodeFile(const char *path);
72
73   /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
74   /// triple.
75   static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
76                                  StringRef triplePrefix);
77
78   /// Create a MemoryBuffer from a memory range with an optional name.
79   static std::unique_ptr<MemoryBuffer>
80   makeBuffer(const void *mem, size_t length, StringRef name = "");
81
82   /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
83   /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
84   /// and the AsmParsers by calling:
85   ///
86   /// InitializeAllTargets();
87   /// InitializeAllTargetMCs();
88   /// InitializeAllAsmPrinters();
89   /// InitializeAllAsmParsers();
90   static LTOModule *createFromFile(const char *path, TargetOptions options,
91                                    std::string &errMsg);
92   static LTOModule *createFromOpenFile(int fd, const char *path, size_t size,
93                                        TargetOptions options,
94                                        std::string &errMsg);
95   static LTOModule *createFromOpenFileSlice(int fd, const char *path,
96                                             size_t map_size, off_t offset,
97                                             TargetOptions options,
98                                             std::string &errMsg);
99   static LTOModule *createFromBuffer(const void *mem, size_t length,
100                                      TargetOptions options, std::string &errMsg,
101                                      StringRef path = "");
102
103   static LTOModule *createInLocalContext(const void *mem, size_t length,
104                                          TargetOptions options,
105                                          std::string &errMsg, StringRef path);
106   static LTOModule *createInContext(const void *mem, size_t length,
107                                     TargetOptions options, std::string &errMsg,
108                                     StringRef path, LLVMContext *Context);
109
110   const Module &getModule() const {
111     return const_cast<LTOModule*>(this)->getModule();
112   }
113   Module &getModule() {
114     return IRFile->getModule();
115   }
116
117   /// Return the Module's target triple.
118   const std::string &getTargetTriple() {
119     return getModule().getTargetTriple();
120   }
121
122   /// Set the Module's target triple.
123   void setTargetTriple(StringRef Triple) {
124     getModule().setTargetTriple(Triple);
125   }
126
127   /// Get the number of symbols
128   uint32_t getSymbolCount() {
129     return _symbols.size();
130   }
131
132   /// Get the attributes for a symbol at the specified index.
133   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.
140   const char *getSymbolName(uint32_t index) {
141     if (index < _symbols.size())
142       return _symbols[index].name;
143     return nullptr;
144   }
145
146   const GlobalValue *getSymbolGV(uint32_t index) {
147     if (index < _symbols.size())
148       return _symbols[index].symbol;
149     return nullptr;
150   }
151
152   /// Get the number of dependent libraries
153   uint32_t getDependentLibraryCount() {
154     return _deplibs.size();
155   }
156
157   /// Get the dependent library at the specified index.
158   const char *getDependentLibrary(uint32_t index) {
159     if (index < _deplibs.size())
160       return _deplibs[index];
161     return nullptr;
162   }
163
164   /// Get the number of linker options
165   uint32_t getLinkerOptCount() {
166     return _linkeropts.size();
167   }
168
169   /// Get the linker option at the specified index.
170   const char *getLinkerOpt(uint32_t index) {
171     if (index < _linkeropts.size())
172       return _linkeropts[index];
173     return nullptr;
174   }
175
176   const std::vector<const char*> &getAsmUndefinedRefs() {
177     return _asm_undefines;
178   }
179
180 private:
181   /// Parse metadata from the module
182   // FIXME: it only parses "Linker Options" metadata at the moment
183   void parseMetadata();
184
185   /// Parse the symbols from the module and model-level ASM and add them to
186   /// either the defined or undefined lists.
187   bool parseSymbols(std::string &errMsg);
188
189   /// Add a symbol which isn't defined just yet to a list to be resolved later.
190   void addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,
191                                    bool isFunc);
192
193   /// Add a defined symbol to the list.
194   void addDefinedSymbol(const char *Name, const GlobalValue *def,
195                         bool isFunction);
196
197   /// Add a data symbol as defined to the list.
198   void addDefinedDataSymbol(const object::BasicSymbolRef &Sym);
199   void addDefinedDataSymbol(const char*Name, const GlobalValue *v);
200
201   /// Add a function symbol as defined to the list.
202   void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym);
203   void addDefinedFunctionSymbol(const char *Name, const Function *F);
204
205   /// Add a global symbol from module-level ASM to the defined list.
206   void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
207
208   /// Add a global symbol from module-level ASM to the undefined list.
209   void addAsmGlobalSymbolUndef(const char *);
210
211   /// Parse i386/ppc ObjC class data structure.
212   void addObjCClass(const GlobalVariable *clgv);
213
214   /// Parse i386/ppc ObjC category data structure.
215   void addObjCCategory(const GlobalVariable *clgv);
216
217   /// Parse i386/ppc ObjC class list data structure.
218   void addObjCClassRef(const GlobalVariable *clgv);
219
220   /// Get string that the data pointer points to.
221   bool objcClassNameFromExpression(const Constant *c, std::string &name);
222
223   /// Create an LTOModule (private version).
224   static LTOModule *makeLTOModule(MemoryBufferRef Buffer, TargetOptions options,
225                                   std::string &errMsg, LLVMContext *Context);
226 };
227 } // namespace llvm
228 #endif