llvm.org GIT mirror llvm / dd32608
Move LTOModule and LTOCodeGenerator to the llvm namespace. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@207911 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
4 changed file(s) with 96 addition(s) and 105 deletion(s). Raw diff Collapse all Expand all
5252 class TargetLibraryInfo;
5353 class TargetMachine;
5454 class raw_ostream;
55 }
5655
5756 //===----------------------------------------------------------------------===//
5857 /// C++ class which implements the opaque lto_code_gen_t type.
6665 // Merge given module, return true on success.
6766 bool addModule(struct LTOModule*, std::string &errMsg);
6867
69 void setTargetOptions(llvm::TargetOptions options);
68 void setTargetOptions(TargetOptions options);
7069 void setDebugInfo(lto_debug_model);
7170 void setCodePICModel(lto_codegen_model);
7271
119118 private:
120119 void initializeLTOPasses();
121120
122 bool generateObjectFile(llvm::raw_ostream &out,
123 bool disableOpt,
124 bool disableInline,
125 bool disableGVNLoadPRE,
126 std::string &errMsg);
121 bool generateObjectFile(raw_ostream &out, bool disableOpt, bool disableInline,
122 bool disableGVNLoadPRE, std::string &errMsg);
127123 void applyScopeRestrictions();
128 void applyRestriction(llvm::GlobalValue &GV,
129 const llvm::ArrayRef &Libcalls,
130 std::vector &MustPreserveList,
131 llvm::SmallPtrSet &AsmUsed,
132 llvm::Mangler &Mangler);
124 void applyRestriction(GlobalValue &GV, const ArrayRef &Libcalls,
125 std::vector &MustPreserveList,
126 SmallPtrSet &AsmUsed,
127 Mangler &Mangler);
133128 bool determineTarget(std::string &errMsg);
134129
135 static void DiagnosticHandler(const llvm::DiagnosticInfo &DI, void *Context);
130 static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context);
136131
137 void DiagnosticHandler2(const llvm::DiagnosticInfo &DI);
132 void DiagnosticHandler2(const DiagnosticInfo &DI);
138133
139 typedef llvm::StringMap StringSet;
134 typedef StringMap StringSet;
140135
141 llvm::LLVMContext &Context;
142 llvm::Linker Linker;
143 llvm::TargetMachine *TargetMach;
136 LLVMContext &Context;
137 Linker Linker;
138 TargetMachine *TargetMach;
144139 bool EmitDwarfDebugInfo;
145140 bool ScopeRestrictionsDone;
146141 lto_codegen_model CodeModel;
147142 StringSet MustPreserveSymbols;
148143 StringSet AsmUndefinedRefs;
149 llvm::MemoryBuffer *NativeObjectFile;
144 MemoryBuffer *NativeObjectFile;
150145 std::vector CodegenOptions;
151146 std::string MCpu;
152147 std::string MAttr;
153148 std::string NativeObjectPath;
154 llvm::TargetOptions Options;
149 TargetOptions Options;
155150 lto_diagnostic_handler_t DiagHandler;
156151 void *DiagContext;
157152 };
158
153 }
159154 #endif // LTO_CODE_GENERATOR_H
3030 class MemoryBuffer;
3131 class TargetOptions;
3232 class Value;
33 }
3433
3534 //===----------------------------------------------------------------------===//
3635 /// C++ class which implements the opaque lto_module_t type.
3736 ///
3837 struct LTOModule {
3938 private:
40 typedef llvm::StringMap StringSet;
39 typedef StringMap StringSet;
4140
4241 struct NameAndAttributes {
4342 const char *name;
4443 uint32_t attributes;
4544 bool isFunction;
46 const llvm::GlobalValue *symbol;
45 const GlobalValue *symbol;
4746 };
4847
49 std::unique_ptr _module;
50 std::unique_ptr _target;
51 llvm::MCObjectFileInfo ObjFileInfo;
48 std::unique_ptr _module;
49 std::unique_ptr _target;
50 MCObjectFileInfo ObjFileInfo;
5251 StringSet _linkeropt_strings;
5352 std::vector _deplibs;
5453 std::vector _linkeropts;
5655
5756 // _defines and _undefines only needed to disambiguate tentative definitions
5857 StringSet _defines;
59 llvm::StringMap _undefines;
58 StringMap _undefines;
6059 std::vector _asm_undefines;
61 llvm::MCContext _context;
60 MCContext _context;
6261
6362 // Use mangler to add GlobalPrefix to names to match linker names.
64 llvm::Mangler _mangler;
65
66 LTOModule(llvm::Module *m, llvm::TargetMachine *t);
63 Mangler _mangler;
64
65 LTOModule(Module *m, TargetMachine *t);
66
6767 public:
6868 /// Returns 'true' if the file or memory contents is LLVM bitcode.
6969 static bool isBitcodeFile(const void *mem, size_t length);
8585 /// InitializeAllTargetMCs();
8686 /// InitializeAllAsmPrinters();
8787 /// InitializeAllAsmParsers();
88 static LTOModule *makeLTOModule(const char* path,
89 llvm::TargetOptions options,
88 static LTOModule *makeLTOModule(const char *path, TargetOptions options,
9089 std::string &errMsg);
91 static LTOModule *makeLTOModule(int fd, const char *path,
92 size_t size, llvm::TargetOptions options,
90 static LTOModule *makeLTOModule(int fd, const char *path, size_t size,
91 TargetOptions options, std::string &errMsg);
92 static LTOModule *makeLTOModule(int fd, const char *path, size_t map_size,
93 off_t offset, TargetOptions options,
9394 std::string &errMsg);
94 static LTOModule *makeLTOModule(int fd, const char *path,
95 size_t map_size,
96 off_t offset, llvm::TargetOptions options,
97 std::string& errMsg);
9895 static LTOModule *makeLTOModule(const void *mem, size_t length,
99 llvm::TargetOptions options,
100 std::string &errMsg,
101 llvm::StringRef path = "");
96 TargetOptions options, std::string &errMsg,
97 StringRef path = "");
10298
10399 /// Return the Module's target triple.
104100 const char *getTargetTriple() {
154150 }
155151
156152 /// Return the Module.
157 llvm::Module *getLLVVMModule() { return _module.get(); }
153 Module *getLLVVMModule() { return _module.get(); }
158154
159155 const std::vector &getAsmUndefinedRefs() {
160156 return _asm_undefines;
170166 bool parseSymbols(std::string &errMsg);
171167
172168 /// Add a symbol which isn't defined just yet to a list to be resolved later.
173 void addPotentialUndefinedSymbol(const llvm::GlobalValue *dcl, bool isFunc);
169 void addPotentialUndefinedSymbol(const GlobalValue *dcl, bool isFunc);
174170
175171 /// Add a defined symbol to the list.
176 void addDefinedSymbol(const llvm::GlobalValue *def, bool isFunction);
172 void addDefinedSymbol(const GlobalValue *def, bool isFunction);
177173
178174 /// Add a function symbol as defined to the list.
179 void addDefinedFunctionSymbol(const llvm::Function *f);
175 void addDefinedFunctionSymbol(const Function *f);
180176
181177 /// Add a data symbol as defined to the list.
182 void addDefinedDataSymbol(const llvm::GlobalValue *v);
178 void addDefinedDataSymbol(const GlobalValue *v);
183179
184180 /// Add global symbols from module-level ASM to the defined or undefined
185181 /// lists.
192188 void addAsmGlobalSymbolUndef(const char *);
193189
194190 /// Parse i386/ppc ObjC class data structure.
195 void addObjCClass(const llvm::GlobalVariable *clgv);
191 void addObjCClass(const GlobalVariable *clgv);
196192
197193 /// Parse i386/ppc ObjC category data structure.
198 void addObjCCategory(const llvm::GlobalVariable *clgv);
194 void addObjCCategory(const GlobalVariable *clgv);
199195
200196 /// Parse i386/ppc ObjC class list data structure.
201 void addObjCClassRef(const llvm::GlobalVariable *clgv);
197 void addObjCClassRef(const GlobalVariable *clgv);
202198
203199 /// Get string that the data pointer points to.
204 bool objcClassNameFromExpression(const llvm::Constant* c, std::string &name);
200 bool objcClassNameFromExpression(const Constant *c, std::string &name);
205201
206202 /// Returns 'true' if the memory buffer is for the specified target triple.
207 static bool isTargetMatch(llvm::MemoryBuffer *memBuffer,
208 const char *triplePrefix);
203 static bool isTargetMatch(MemoryBuffer *memBuffer, const char *triplePrefix);
209204
210205 /// Create an LTOModule (private version). N.B. This method takes ownership of
211206 /// the buffer.
212 static LTOModule *makeLTOModule(llvm::MemoryBuffer *buffer,
213 llvm::TargetOptions options,
207 static LTOModule *makeLTOModule(MemoryBuffer *buffer, TargetOptions options,
214208 std::string &errMsg);
215209
216210 /// Create a MemoryBuffer from a memory range with an optional name.
217 static llvm::MemoryBuffer *makeBuffer(const void *mem, size_t length,
218 llvm::StringRef name = "");
211 static MemoryBuffer *makeBuffer(const void *mem, size_t length,
212 StringRef name = "");
219213 };
220
214 }
221215 #endif // LTO_MODULE_H
8383 } lto_codegen_model;
8484
8585 /** opaque reference to a loaded object module */
86 typedef struct LTOModule* lto_module_t;
86 typedef struct LLVMOpaqueLTOModule *lto_module_t;
8787
8888 /** opaque reference to a code generator */
89 typedef struct LTOCodeGenerator* lto_code_gen_t;
89 typedef struct LLVMOpaqueLTOCodeGenerator *lto_code_gen_t;
9090
9191 #ifdef __cplusplus
9292 extern "C" {
5555 }
5656 }
5757
58 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOCodeGenerator, lto_code_gen_t);
59 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t);
60
5861 // Convert the subtarget features into a string to pass to LTOCodeGenerator.
5962 static void lto_add_attrs(lto_code_gen_t cg) {
63 LTOCodeGenerator *CG = unwrap(cg);
6064 if (MAttrs.size()) {
6165 std::string attrs;
6266 for (unsigned i = 0; i < MAttrs.size(); ++i) {
6569 attrs.append(MAttrs[i]);
6670 }
6771
68 cg->setAttr(attrs.c_str());
72 CG->setAttr(attrs.c_str());
6973 }
7074 }
7175
100104 lto_module_t lto_module_create(const char* path) {
101105 lto_initialize();
102106 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
103 return LTOModule::makeLTOModule(path, Options, sLastErrorString);
107 return wrap(LTOModule::makeLTOModule(path, Options, sLastErrorString));
104108 }
105109
106110 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
107111 lto_initialize();
108112 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
109 return LTOModule::makeLTOModule(fd, path, size, Options, sLastErrorString);
113 return wrap(
114 LTOModule::makeLTOModule(fd, path, size, Options, sLastErrorString));
110115 }
111116
112117 lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
115120 off_t offset) {
116121 lto_initialize();
117122 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
118 return LTOModule::makeLTOModule(fd, path, map_size, offset, Options,
119 sLastErrorString);
123 return wrap(LTOModule::makeLTOModule(fd, path, map_size, offset, Options,
124 sLastErrorString));
120125 }
121126
122127 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
123128 lto_initialize();
124129 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
125 return LTOModule::makeLTOModule(mem, length, Options, sLastErrorString);
130 return wrap(LTOModule::makeLTOModule(mem, length, Options, sLastErrorString));
126131 }
127132
128133 lto_module_t lto_module_create_from_memory_with_path(const void* mem,
130135 const char *path) {
131136 lto_initialize();
132137 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
133 return LTOModule::makeLTOModule(mem, length, Options, sLastErrorString, path);
134 }
135
136 void lto_module_dispose(lto_module_t mod) {
137 delete mod;
138 }
138 return wrap(
139 LTOModule::makeLTOModule(mem, length, Options, sLastErrorString, path));
140 }
141
142 void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
139143
140144 const char* lto_module_get_target_triple(lto_module_t mod) {
141 return mod->getTargetTriple();
145 return unwrap(mod)->getTargetTriple();
142146 }
143147
144148 void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
145 return mod->setTargetTriple(triple);
149 return unwrap(mod)->setTargetTriple(triple);
146150 }
147151
148152 unsigned int lto_module_get_num_symbols(lto_module_t mod) {
149 return mod->getSymbolCount();
153 return unwrap(mod)->getSymbolCount();
150154 }
151155
152156 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
153 return mod->getSymbolName(index);
157 return unwrap(mod)->getSymbolName(index);
154158 }
155159
156160 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
157161 unsigned int index) {
158 return mod->getSymbolAttributes(index);
162 return unwrap(mod)->getSymbolAttributes(index);
159163 }
160164
161165 unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
162 return mod->getDependentLibraryCount();
166 return unwrap(mod)->getDependentLibraryCount();
163167 }
164168
165169 const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
166 return mod->getDependentLibrary(index);
170 return unwrap(mod)->getDependentLibrary(index);
167171 }
168172
169173 unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
170 return mod->getLinkerOptCount();
174 return unwrap(mod)->getLinkerOptCount();
171175 }
172176
173177 const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
174 return mod->getLinkerOpt(index);
178 return unwrap(mod)->getLinkerOpt(index);
175179 }
176180
177181 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
178182 lto_diagnostic_handler_t diag_handler,
179183 void *ctxt) {
180 cg->setDiagnosticHandler(diag_handler, ctxt);
184 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
181185 }
182186
183187 lto_code_gen_t lto_codegen_create(void) {
188192 LTOCodeGenerator *CodeGen = new LTOCodeGenerator();
189193 if (CodeGen)
190194 CodeGen->setTargetOptions(Options);
191 return CodeGen;
192 }
193
194 void lto_codegen_dispose(lto_code_gen_t cg) {
195 delete cg;
196 }
195 return wrap(CodeGen);
196 }
197
198 void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
197199
198200 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
199 return !cg->addModule(mod, sLastErrorString);
201 return !unwrap(cg)->addModule(unwrap(mod), sLastErrorString);
200202 }
201203
202204 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
203 cg->setDebugInfo(debug);
205 unwrap(cg)->setDebugInfo(debug);
204206 return false;
205207 }
206208
207209 bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
208 cg->setCodePICModel(model);
210 unwrap(cg)->setCodePICModel(model);
209211 return false;
210212 }
211213
212214 void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
213 return cg->setCpu(cpu);
215 return unwrap(cg)->setCpu(cpu);
214216 }
215217
216218 void lto_codegen_set_attr(lto_code_gen_t cg, const char *attr) {
217 return cg->setAttr(attr);
219 return unwrap(cg)->setAttr(attr);
218220 }
219221
220222 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
228230
229231 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
230232 const char *symbol) {
231 cg->addMustPreserveSymbol(symbol);
233 unwrap(cg)->addMustPreserveSymbol(symbol);
232234 }
233235
234236 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
235237 if (!parsedOptions) {
236 cg->parseCodeGenDebugOptions();
238 unwrap(cg)->parseCodeGenDebugOptions();
237239 lto_add_attrs(cg);
238240 parsedOptions = true;
239241 }
240 return !cg->writeMergedModules(path, sLastErrorString);
242 return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
241243 }
242244
243245 const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
244246 if (!parsedOptions) {
245 cg->parseCodeGenDebugOptions();
247 unwrap(cg)->parseCodeGenDebugOptions();
246248 lto_add_attrs(cg);
247249 parsedOptions = true;
248250 }
249 return cg->compile(length, DisableOpt, DisableInline, DisableGVNLoadPRE,
250 sLastErrorString);
251 return unwrap(cg)->compile(length, DisableOpt, DisableInline,
252 DisableGVNLoadPRE, sLastErrorString);
251253 }
252254
253255 bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
254256 if (!parsedOptions) {
255 cg->parseCodeGenDebugOptions();
257 unwrap(cg)->parseCodeGenDebugOptions();
256258 lto_add_attrs(cg);
257259 parsedOptions = true;
258260 }
259 return !cg->compile_to_file(name, DisableOpt, DisableInline, DisableGVNLoadPRE,
260 sLastErrorString);
261 return !unwrap(cg)->compile_to_file(name, DisableOpt, DisableInline,
262 DisableGVNLoadPRE, sLastErrorString);
261263 }
262264
263265 void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
264 cg->setCodeGenDebugOptions(opt);
265 }
266 unwrap(cg)->setCodeGenDebugOptions(opt);
267 }