c3e86afe1d82b3ea8a42038fe8e119054f525126
[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::string LinkerOpts;
51
52   std::unique_ptr<object::IRObjectFile> IRFile;
53   std::unique_ptr<TargetMachine> _target;
54   std::vector<NameAndAttributes> _symbols;
55
56   // _defines and _undefines only needed to disambiguate tentative definitions
57   StringSet<>                             _defines;
58   StringMap<NameAndAttributes> _undefines;
59   std::vector<const char*>                _asm_undefines;
60
61   LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM);
62   LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM,
63             std::unique_ptr<LLVMContext> Context);
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(const char *path);
71
72   /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
73   /// triple.
74   static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
75                                  StringRef triplePrefix);
76
77   /// Returns a string representing the producer identification stored in the
78   /// bitcode, or "" if the bitcode does not contains any.
79   ///
80   static std::string getProducerString(MemoryBuffer *Buffer);
81
82   /// Create a MemoryBuffer from a memory range with an optional name.
83   static std::unique_ptr<MemoryBuffer>
84   makeBuffer(const void *mem, size_t length, StringRef name = "");
85
86   /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
87   /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
88   /// and the AsmParsers by calling:
89   ///
90   /// InitializeAllTargets();
91   /// InitializeAllTargetMCs();
92   /// InitializeAllAsmPrinters();
93   /// InitializeAllAsmParsers();
94   static LTOModule *createFromFile(const char *path, TargetOptions options,
95                                    std::string &errMsg);
96   static LTOModule *createFromOpenFile(int fd, const char *path, size_t size,
97                                        TargetOptions options,
98                                        std::string &errMsg);
99   static LTOModule *createFromOpenFileSlice(int fd, const char *path,
100                                             size_t map_size, off_t offset,
101                                             TargetOptions options,
102                                             std::string &errMsg);
103   static LTOModule *createFromBuffer(const void *mem, size_t length,
104                                      TargetOptions options, std::string &errMsg,
105                                      StringRef path = "");
106
107   static LTOModule *createInLocalContext(const void *mem, size_t length,
108                                          TargetOptions options,
109                                          std::string &errMsg, StringRef path);
110   static LTOModule *createInContext(const void *mem, size_t length,
111                                     TargetOptions options, std::string &errMsg,
112                                     StringRef path, LLVMContext *Context);
113
114   const Module &getModule() const {
115     return const_cast<LTOModule*>(this)->getModule();
116   }
117   Module &getModule() {
118     return IRFile->getModule();
119   }
120
121   std::unique_ptr<Module> takeModule() { return IRFile->takeModule(); }
122
123   /// Return the Module's target triple.
124   const std::string &getTargetTriple() {
125     return getModule().getTargetTriple();
126   }
127
128   /// Set the Module's target triple.
129   void setTargetTriple(StringRef Triple) {
130     getModule().setTargetTriple(Triple);
131   }
132
133   /// Get the number of symbols
134   uint32_t getSymbolCount() {
135     return _symbols.size();
136   }
137
138   /// Get the attributes for a symbol at the specified index.
139   lto_symbol_attributes getSymbolAttributes(uint32_t index) {
140     if (index < _symbols.size())
141       return lto_symbol_attributes(_symbols[index].attributes);
142     return lto_symbol_attributes(0);
143   }
144
145   /// Get the name of the symbol at the specified index.
146   const char *getSymbolName(uint32_t index) {
147     if (index < _symbols.size())
148       return _symbols[index].name;
149     return nullptr;
150   }
151
152   const GlobalValue *getSymbolGV(uint32_t index) {
153     if (index < _symbols.size())
154       return _symbols[index].symbol;
155     return nullptr;
156   }
157
158   const char *getLinkerOpts() {
159     return LinkerOpts.c_str();
160   }
161
162   const std::vector<const char*> &getAsmUndefinedRefs() {
163     return _asm_undefines;
164   }
165
166 private:
167   /// Parse metadata from the module
168   // FIXME: it only parses "Linker Options" metadata at the moment
169   void parseMetadata();
170
171   /// Parse the symbols from the module and model-level ASM and add them to
172   /// either the defined or undefined lists.
173   bool parseSymbols(std::string &errMsg);
174
175   /// Add a symbol which isn't defined just yet to a list to be resolved later.
176   void addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,
177                                    bool isFunc);
178
179   /// Add a defined symbol to the list.
180   void addDefinedSymbol(const char *Name, const GlobalValue *def,
181                         bool isFunction);
182
183   /// Add a data symbol as defined to the list.
184   void addDefinedDataSymbol(const object::BasicSymbolRef &Sym);
185   void addDefinedDataSymbol(const char*Name, const GlobalValue *v);
186
187   /// Add a function symbol as defined to the list.
188   void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym);
189   void addDefinedFunctionSymbol(const char *Name, const Function *F);
190
191   /// Add a global symbol from module-level ASM to the defined list.
192   void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
193
194   /// Add a global symbol from module-level ASM to the undefined list.
195   void addAsmGlobalSymbolUndef(const char *);
196
197   /// Parse i386/ppc ObjC class data structure.
198   void addObjCClass(const GlobalVariable *clgv);
199
200   /// Parse i386/ppc ObjC category data structure.
201   void addObjCCategory(const GlobalVariable *clgv);
202
203   /// Parse i386/ppc ObjC class list data structure.
204   void addObjCClassRef(const GlobalVariable *clgv);
205
206   /// Get string that the data pointer points to.
207   bool objcClassNameFromExpression(const Constant *c, std::string &name);
208
209   /// Create an LTOModule (private version).
210   static LTOModule *makeLTOModule(MemoryBufferRef Buffer, TargetOptions options,
211                                   std::string &errMsg, LLVMContext *Context);
212 };
213 }
214 #endif