Sink undesirable LTO functions into the old C API
[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 LTO_MODULE_H
15 #define LTO_MODULE_H
16
17 #include "llvm-c/lto.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/IR/Mangler.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCObjectFileInfo.h"
23 #include "llvm/Target/TargetMachine.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   typedef StringMap<uint8_t> StringSet;
41
42   struct NameAndAttributes {
43     const char        *name;
44     uint32_t           attributes;
45     bool               isFunction;
46     const GlobalValue *symbol;
47   };
48
49   std::unique_ptr<Module> _module;
50   std::unique_ptr<TargetMachine> _target;
51   MCObjectFileInfo ObjFileInfo;
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   MCContext _context;
62
63   // Use mangler to add GlobalPrefix to names to match linker names.
64   Mangler _mangler;
65
66   LTOModule(std::unique_ptr<Module> M, TargetMachine *TM);
67
68 public:
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 MemoryBuffer *makeBuffer(const void *mem, size_t length,
80                                   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   /// Return the Module's target triple.
104   const char *getTargetTriple() {
105     return _module->getTargetTriple().c_str();
106   }
107
108   /// Set the Module's target triple.
109   void setTargetTriple(const char *triple) {
110     _module->setTargetTriple(triple);
111   }
112
113   /// Get the number of symbols
114   uint32_t getSymbolCount() {
115     return _symbols.size();
116   }
117
118   /// Get the attributes for a symbol at the specified index.
119   lto_symbol_attributes getSymbolAttributes(uint32_t index) {
120     if (index < _symbols.size())
121       return lto_symbol_attributes(_symbols[index].attributes);
122     return lto_symbol_attributes(0);
123   }
124
125   /// Get the name of the symbol at the specified index.
126   const char *getSymbolName(uint32_t index) {
127     if (index < _symbols.size())
128       return _symbols[index].name;
129     return nullptr;
130   }
131
132   /// Get the number of dependent libraries
133   uint32_t getDependentLibraryCount() {
134     return _deplibs.size();
135   }
136
137   /// Get the dependent library at the specified index.
138   const char *getDependentLibrary(uint32_t index) {
139     if (index < _deplibs.size())
140       return _deplibs[index];
141     return nullptr;
142   }
143
144   /// Get the number of linker options
145   uint32_t getLinkerOptCount() {
146     return _linkeropts.size();
147   }
148
149   /// Get the linker option at the specified index.
150   const char *getLinkerOpt(uint32_t index) {
151     if (index < _linkeropts.size())
152       return _linkeropts[index];
153     return nullptr;
154   }
155
156   /// Return the Module.
157   Module *getLLVVMModule() { return _module.get(); }
158
159   const std::vector<const char*> &getAsmUndefinedRefs() {
160     return _asm_undefines;
161   }
162
163 private:
164   /// Parse metadata from the module
165   // FIXME: it only parses "Linker Options" metadata at the moment
166   void parseMetadata();
167
168   /// Parse the symbols from the module and model-level ASM and add them to
169   /// either the defined or undefined lists.
170   bool parseSymbols(std::string &errMsg);
171
172   /// Add a symbol which isn't defined just yet to a list to be resolved later.
173   void addPotentialUndefinedSymbol(const GlobalValue *dcl, bool isFunc);
174
175   /// Add a defined symbol to the list.
176   void addDefinedSymbol(const GlobalValue *def, bool isFunction);
177
178   /// Add a function symbol as defined to the list.
179   void addDefinedFunctionSymbol(const Function *f);
180
181   /// Add a data symbol as defined to the list.
182   void addDefinedDataSymbol(const GlobalValue *v);
183
184   /// Add global symbols from module-level ASM to the defined or undefined
185   /// lists.
186   bool addAsmGlobalSymbols(std::string &errMsg);
187
188   /// Add a global symbol from module-level ASM to the defined list.
189   void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
190
191   /// Add a global symbol from module-level ASM to the undefined list.
192   void addAsmGlobalSymbolUndef(const char *);
193
194   /// Parse i386/ppc ObjC class data structure.
195   void addObjCClass(const GlobalVariable *clgv);
196
197   /// Parse i386/ppc ObjC category data structure.
198   void addObjCCategory(const GlobalVariable *clgv);
199
200   /// Parse i386/ppc ObjC class list data structure.
201   void addObjCClassRef(const GlobalVariable *clgv);
202
203   /// Get string that the data pointer points to.
204   bool objcClassNameFromExpression(const Constant *c, std::string &name);
205
206   /// Create an LTOModule (private version). N.B. This method takes ownership of
207   /// the buffer.
208   static LTOModule *makeLTOModule(std::unique_ptr<MemoryBuffer> Buffer,
209                                   TargetOptions options, std::string &errMsg);
210 };
211 }
212 #endif // LTO_MODULE_H