blob: ee51edfee01f599c6bc7b656c618449e32cf8670 [file] [log] [blame]
Peter Collingbourned5395fb2012-01-08 22:09:58 +00001#include "llvm/Bitcode/ReaderWriter.h"
Tom Stellard976577e2013-06-26 18:20:32 +00002#include "llvm/IR/Function.h"
3#include "llvm/IR/GlobalVariable.h"
4#include "llvm/IR/LLVMContext.h"
5#include "llvm/IR/Module.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +00006#include "llvm/Support/CommandLine.h"
7#include "llvm/Support/ManagedStatic.h"
8#include "llvm/Support/MemoryBuffer.h"
Tom Stellard98dccb12014-04-30 18:35:20 +00009#include "llvm/Support/FileSystem.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +000010#include "llvm/Support/raw_ostream.h"
Tom Stellard8a63b152014-01-20 20:28:48 +000011#include "llvm/Support/ErrorOr.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +000012#include "llvm/Support/ToolOutputFile.h"
Niels Ole Salscheider109ce692014-08-22 22:24:28 +000013#include "llvm/Config/llvm-config.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +000014
Michel Danzera10b4922014-08-28 06:19:33 +000015#define LLVM_360_AND_NEWER \
16 (LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6))
17
Tom Stellard0ab10342014-06-13 01:30:14 +000018#define LLVM_350_AND_NEWER \
19 (LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5))
20
21#if LLVM_350_AND_NEWER
Jeroen Ketemad253e662014-06-21 09:20:31 +000022#include <system_error>
23
Tom Stellard0ab10342014-06-13 01:30:14 +000024#define ERROR_CODE std::error_code
Jeroen Ketemad253e662014-06-21 09:20:31 +000025#define UNIQUE_PTR std::unique_ptr
Tom Stellard0ab10342014-06-13 01:30:14 +000026#else
Jeroen Ketemad253e662014-06-21 09:20:31 +000027#include "llvm/ADT/OwningPtr.h"
28#include "llvm/Support/system_error.h"
29
Tom Stellard0ab10342014-06-13 01:30:14 +000030#define ERROR_CODE error_code
Jeroen Ketemad253e662014-06-21 09:20:31 +000031#define UNIQUE_PTR OwningPtr
Tom Stellard0ab10342014-06-13 01:30:14 +000032#endif
33
Jeroen Ketemad253e662014-06-21 09:20:31 +000034using namespace llvm;
35
Peter Collingbourned5395fb2012-01-08 22:09:58 +000036static cl::opt<std::string>
37InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
38
39static cl::opt<std::string>
40OutputFilename("o", cl::desc("Output filename"),
41 cl::value_desc("filename"));
42
43int main(int argc, char **argv) {
44 LLVMContext &Context = getGlobalContext();
45 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
46
47 cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n");
48
49 std::string ErrorMessage;
50 std::auto_ptr<Module> M;
51
52 {
Tom Stellard8d92c392014-07-07 17:46:45 +000053#if LLVM_350_AND_NEWER
54 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
55 MemoryBuffer::getFile(InputFilename);
56 std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();
57 if (std::error_code ec = BufferOrErr.getError())
58#else
Jeroen Ketemad253e662014-06-21 09:20:31 +000059 UNIQUE_PTR<MemoryBuffer> BufferPtr;
Tom Stellard0ab10342014-06-13 01:30:14 +000060 if (ERROR_CODE ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr))
Tom Stellard8d92c392014-07-07 17:46:45 +000061#endif
Peter Collingbourned5395fb2012-01-08 22:09:58 +000062 ErrorMessage = ec.message();
Tom Stellard8a63b152014-01-20 20:28:48 +000063 else {
64#if LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR > 4)
Michel Danzer7b77ab72014-08-28 06:19:37 +000065# if LLVM_360_AND_NEWER
66 ErrorOr<Module *> ModuleOrErr =
67 parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context);
68# else
Tom Stellard8a63b152014-01-20 20:28:48 +000069 ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(BufferPtr.get(), Context);
Michel Danzer7b77ab72014-08-28 06:19:37 +000070# endif
Tom Stellard0ab10342014-06-13 01:30:14 +000071 if (ERROR_CODE ec = ModuleOrErr.getError())
Tom Stellard8a63b152014-01-20 20:28:48 +000072 ErrorMessage = ec.message();
73 M.reset(ModuleOrErr.get());
74#else
Peter Collingbourned5395fb2012-01-08 22:09:58 +000075 M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage));
Tom Stellard8a63b152014-01-20 20:28:48 +000076#endif
77 }
Peter Collingbourned5395fb2012-01-08 22:09:58 +000078 }
79
80 if (M.get() == 0) {
81 errs() << argv[0] << ": ";
82 if (ErrorMessage.size())
83 errs() << ErrorMessage << "\n";
84 else
85 errs() << "bitcode didn't read correctly.\n";
86 return 1;
87 }
88
89 // Set linkage of every external definition to linkonce_odr.
90 for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
91 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
92 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
93 }
94
95 for (Module::global_iterator i = M->global_begin(), e = M->global_end();
96 i != e; ++i) {
97 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
98 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
99 }
100
101 if (OutputFilename.empty()) {
102 errs() << "no output file\n";
103 return 1;
104 }
105
Michel Danzera10b4922014-08-28 06:19:33 +0000106#if LLVM_360_AND_NEWER
107 std::error_code EC;
108 UNIQUE_PTR<tool_output_file> Out
109 (new tool_output_file(OutputFilename, EC, sys::fs::F_None));
110 if (EC) {
111 errs() << EC.message() << '\n';
112 exit(1);
113 }
114#else
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000115 std::string ErrorInfo;
Jeroen Ketemad253e662014-06-21 09:20:31 +0000116 UNIQUE_PTR<tool_output_file> Out
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000117 (new tool_output_file(OutputFilename.c_str(), ErrorInfo,
Tom Stellard7aee1cf2014-02-24 21:31:56 +0000118#if (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR == 4)
Aaron Watry0da3d3b52013-07-18 21:24:35 +0000119 sys::fs::F_Binary));
Tom Stellard7aee1cf2014-02-24 21:31:56 +0000120#elif LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5)
121 sys::fs::F_None));
Aaron Watry0da3d3b52013-07-18 21:24:35 +0000122#else
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000123 raw_fd_ostream::F_Binary));
Aaron Watry0da3d3b52013-07-18 21:24:35 +0000124#endif
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000125 if (!ErrorInfo.empty()) {
126 errs() << ErrorInfo << '\n';
127 exit(1);
128 }
Michel Danzera10b4922014-08-28 06:19:33 +0000129#endif // LLVM_360_AND_NEWER
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000130
131 WriteBitcodeToFile(M.get(), Out->os());
132
133 // Declare success.
134 Out->keep();
135 return 0;
136}
137