s/lto_codegen_whole_program_optimization/lto_codegen_set_whole_program_optimization/
[oota-llvm.git] / tools / 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/Module.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/Target/Mangler.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/ADT/OwningPtr.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm-c/lto.h"
24 #include <vector>
25 #include <string>
26
27 // Forward references to llvm classes.
28 namespace llvm {
29   class Function;
30   class GlobalValue;
31   class MemoryBuffer;
32   class Value;
33 }
34
35 //===----------------------------------------------------------------------===//
36 /// LTOModule - C++ class which implements the opaque lto_module_t type.
37 ///
38 struct LTOModule {
39 private:
40   typedef llvm::StringMap<uint8_t> StringSet;
41
42   struct NameAndAttributes {
43     const char        *name;
44     uint32_t           attributes;
45     bool               isFunction;
46     llvm::GlobalValue *symbol;
47   };
48
49   llvm::OwningPtr<llvm::Module>           _module;
50   llvm::OwningPtr<llvm::TargetMachine>    _target;
51   std::vector<NameAndAttributes>          _symbols;
52
53   // _defines and _undefines only needed to disambiguate tentative definitions
54   StringSet                               _defines;
55   llvm::StringMap<NameAndAttributes>      _undefines;
56   std::vector<const char*>                _asm_undefines;
57   llvm::MCContext                         _context;
58
59   // Use mangler to add GlobalPrefix to names to match linker names.
60   llvm::Mangler                           _mangler;
61
62   LTOModule(llvm::Module *m, llvm::TargetMachine *t);
63 public:
64   /// isBitcodeFile - Returns 'true' if the file or memory contents is LLVM
65   /// bitcode.
66   static bool isBitcodeFile(const void *mem, size_t length);
67   static bool isBitcodeFile(const char *path);
68
69   /// isBitcodeFileForTarget - Returns 'true' if the file or memory contents
70   /// is LLVM bitcode for the specified triple.
71   static bool isBitcodeFileForTarget(const void *mem,
72                                      size_t length,
73                                      const char *triplePrefix);
74   static bool isBitcodeFileForTarget(const char *path,
75                                      const char *triplePrefix);
76
77   /// makeLTOModule - Create an LTOModule. N.B. These methods take ownership
78   /// of the buffer.
79   static LTOModule *makeLTOModule(const char* path,
80                                   std::string &errMsg);
81   static LTOModule *makeLTOModule(int fd, const char *path,
82                                   size_t size, std::string &errMsg);
83   static LTOModule *makeLTOModule(int fd, const char *path,
84                                   size_t file_size,
85                                   size_t map_size,
86                                   off_t offset,
87                                   std::string& errMsg);
88   static LTOModule *makeLTOModule(const void *mem, size_t length,
89                                   std::string &errMsg);
90
91   /// getTargetTriple - Return the Module's target triple.
92   const char *getTargetTriple() {
93     return _module->getTargetTriple().c_str();
94   }
95
96   /// setTargetTriple - Set the Module's target triple.
97   void setTargetTriple(const char *triple) {
98     _module->setTargetTriple(triple);
99   }
100
101   /// getSymbolCount - Get the number of symbols
102   uint32_t getSymbolCount() {
103     return _symbols.size();
104   }
105
106   /// getSymbolAttributes - Get the attributes for a symbol at the specified
107   /// index.
108   lto_symbol_attributes getSymbolAttributes(uint32_t index) {
109     if (index < _symbols.size())
110       return lto_symbol_attributes(_symbols[index].attributes);
111     return lto_symbol_attributes(0);
112   }
113
114   /// getSymbolName - Get the name of the symbol at the specified index.
115   const char *getSymbolName(uint32_t index) {
116     if (index < _symbols.size())
117       return _symbols[index].name;
118     return NULL;
119   }
120
121   /// getLLVVMModule - Return the Module.
122   llvm::Module *getLLVVMModule() { return _module.get(); }
123
124   /// getAsmUndefinedRefs -
125   const std::vector<const char*> &getAsmUndefinedRefs() {
126     return _asm_undefines;
127   }
128
129 private:
130   /// parseSymbols - Parse the symbols from the module and model-level ASM and
131   /// add them to either the defined or undefined lists.
132   bool parseSymbols(std::string &errMsg);
133
134   /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet
135   /// to a list to be resolved later.
136   void addPotentialUndefinedSymbol(llvm::GlobalValue *dcl, bool isFunc);
137
138   /// addDefinedSymbol - Add a defined symbol to the list.
139   void addDefinedSymbol(llvm::GlobalValue *def, bool isFunction);
140
141   /// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
142   void addDefinedFunctionSymbol(llvm::Function *f);
143
144   /// addDefinedDataSymbol - Add a data symbol as defined to the list.
145   void addDefinedDataSymbol(llvm::GlobalValue *v);
146
147   /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
148   /// defined or undefined lists.
149   bool addAsmGlobalSymbols(std::string &errMsg);
150
151   /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
152   /// defined list.
153   void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
154
155   /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to
156   /// the undefined list.
157   void addAsmGlobalSymbolUndef(const char *);
158
159   /// addObjCClass - Parse i386/ppc ObjC class data structure.
160   void addObjCClass(llvm::GlobalVariable *clgv);
161
162   /// addObjCCategory - Parse i386/ppc ObjC category data structure.
163   void addObjCCategory(llvm::GlobalVariable *clgv);
164
165   /// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
166   void addObjCClassRef(llvm::GlobalVariable *clgv);
167
168   /// objcClassNameFromExpression - Get string that the data pointer points
169   /// to.
170   bool objcClassNameFromExpression(llvm::Constant* c, std::string &name);
171
172   /// isTargetMatch - Returns 'true' if the memory buffer is for the specified
173   /// target triple.
174   static bool isTargetMatch(llvm::MemoryBuffer *memBuffer,
175                             const char *triplePrefix);
176
177   /// makeLTOModule - Create an LTOModule (private version). N.B. This
178   /// method takes ownership of the buffer.
179   static LTOModule *makeLTOModule(llvm::MemoryBuffer *buffer,
180                                   std::string &errMsg);
181
182   /// makeBuffer - Create a MemoryBuffer from a memory range.
183   static llvm::MemoryBuffer *makeBuffer(const void *mem, size_t length);
184 };
185
186 #endif // LTO_MODULE_H