llvm.org GIT mirror llvm / ddc6f35
Add C wrappers for the LTO library (PR1081). Patch by Chandler Carruth! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@32966 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
2 changed file(s) with 124 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===-- llvm/LinkTimeOptimizer.h - LTO Public C Interface -------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Chandler Carruth and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This header provides a C API to use the LLVM link time optimization
10 // library. This is inteded to be used by linkers which are C-only in
11 // their implementation for performing LTO.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef __LTO_CAPI_H__
16 #define __LTO_CAPI_H__
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 /// This provides a dummy type for pointers to the LTO object.
23 typedef void* llvm_lto_t;
24
25 /// This provides a C-visible enumerator to manage status codes.
26 /// This should map exactly onto the C++ enumerator LTOStatus.
27 typedef enum llvm_lto_status {
28 LLVM_LTO_UNKNOWN,
29 LLVM_LTO_OPT_SUCCESS,
30 LLVM_LTO_READ_SUCCESS,
31 LLVM_LTO_READ_FAILURE,
32 LLVM_LTO_WRITE_FAILURE,
33 LLVM_LTO_NO_TARGET,
34 LLVM_LTO_NO_WORK,
35 LLVM_LTO_MODULE_MERGE_FAILURE,
36 LLVM_LTO_ASM_FAILURE,
37
38 // Added C-specific error codes
39 LLVM_LTO_NULL_OBJECT
40 } llvm_lto_status_t;
41
42 /// This provides C interface to initialize link time optimizer. This allows
43 /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
44 /// extern "C" helps, because dlopen() interface uses name to find the symbol.
45 extern llvm_lto_t llvm_create_optimizer(void);
46 extern void llvm_destroy_optimizer(llvm_lto_t lto);
47
48 extern llvm_lto_status_t llvm_read_object_file
49 (llvm_lto_t lto, const char* input_filename);
50 extern llvm_lto_status_t llvm_optimize_modules
51 (llvm_lto_t lto, const char* output_filename);
52
53 #ifdef __cplusplus
54 }
55 #endif
56
57 #endif
0 //===- lto-c.cpp - LLVM Link Time Optimizer C Wrappers --------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Chandler Carruth and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a C wrapper API for the Link Time Optimization
10 // library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm-c/LinkTimeOptimizer.h"
15 #include "llvm/LinkTimeOptimizer.h"
16 using namespace llvm;
17
18
19 /// Create an instance of the LLVM LTO object for performing the link
20 /// time optimizations.
21 extern "C"
22 llvm_lto_t llvm_create_optimizer() {
23 return new llvm::LTO();
24 }
25
26 /// Destroy an instance of the LLVM LTO object
27 extern "C"
28 void llvm_destroy_optimizer(llvm_lto_t lto) {
29 delete (llvm::LTO*)lto;
30 }
31
32 /// Read an LLVM bytecode file using LTO::readLLVMObjectFile.
33 extern "C"
34 llvm_lto_status
35 llvm_read_object_file(llvm_lto_t lto, const char *input_filename) {
36 llvm::LTO *l = (llvm::LTO*)lto;
37
38 if (input_filename == NULL)
39 return LLVM_LTO_READ_FAILURE;
40
41 std::string InputFilename(input_filename);
42 llvm::LTO::NameToSymbolMap symbols;
43 std::set references;
44 return (llvm_lto_status)((int)(l->readLLVMObjectFile(InputFilename, symbols,
45 references)));
46 }
47
48
49 /// Optimize and output object code using LTO::optimizeModules.
50 extern "C"
51 llvm_lto_status
52 llvm_optimize_modules(llvm_lto_t lto, const char *output_filename) {
53 llvm::LTO *l = (llvm::LTO*)lto;
54
55 std::string OutputFilename(output_filename);
56 std::vector exportList;
57 std::string targetTriple;
58
59 return (llvm_lto_status)((int)(
60 l->optimizeModules(OutputFilename, exportList,
61 targetTriple, false, "")));
62 }
63
64
65