Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 1 | //===-LTOCodeGenerator.cpp - 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. |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 7 | // |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 10 | // This file implements the Link Time Optimization library. This library is |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 11 | // intended to be used by linker to optimize code at link time. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 15 | #include "LTOCodeGenerator.h" |
Bill Wendling | ab53bc7 | 2012-03-31 11:10:35 +0000 | [diff] [blame] | 16 | #include "LTOModule.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Nick Lewycky | 8189d40 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/Passes.h" |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/Verifier.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 20 | #include "llvm/Bitcode/ReaderWriter.h" |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 21 | #include "llvm/Config/config.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Constants.h" |
| 23 | #include "llvm/IR/DataLayout.h" |
| 24 | #include "llvm/IR/DerivedTypes.h" |
| 25 | #include "llvm/IR/LLVMContext.h" |
| 26 | #include "llvm/IR/Module.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 27 | #include "llvm/Linker.h" |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCAsmInfo.h" |
| 29 | #include "llvm/MC/MCContext.h" |
Evan Cheng | ab8be96 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 30 | #include "llvm/MC/SubtargetFeature.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 31 | #include "llvm/PassManager.h" |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
Rafael Espindola | 68c0efa | 2013-06-17 18:05:35 +0000 | [diff] [blame^] | 33 | #include "llvm/Support/FileSystem.h" |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 34 | #include "llvm/Support/FormattedStream.h" |
Michael J. Spencer | 3cc52ea | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Host.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 3cc52ea | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Signals.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 38 | #include "llvm/Support/TargetRegistry.h" |
| 39 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 40 | #include "llvm/Support/ToolOutputFile.h" |
Michael J. Spencer | f2f516f | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 41 | #include "llvm/Support/system_error.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 42 | #include "llvm/Target/Mangler.h" |
| 43 | #include "llvm/Target/TargetMachine.h" |
| 44 | #include "llvm/Target/TargetOptions.h" |
| 45 | #include "llvm/Target/TargetRegisterInfo.h" |
| 46 | #include "llvm/Transforms/IPO.h" |
| 47 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Bob Wilson | d696544 | 2013-03-29 23:28:55 +0000 | [diff] [blame] | 48 | #include "llvm/Transforms/ObjCARC.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 49 | using namespace llvm; |
| 50 | |
Bill Wendling | 9ac0aaa | 2012-08-06 21:34:54 +0000 | [diff] [blame] | 51 | static cl::opt<bool> |
Bill Wendling | 6303b66 | 2013-02-28 14:11:10 +0000 | [diff] [blame] | 52 | DisableOpt("disable-opt", cl::init(false), |
| 53 | cl::desc("Do not run any optimization passes")); |
| 54 | |
| 55 | static cl::opt<bool> |
Bill Wendling | 9ac0aaa | 2012-08-06 21:34:54 +0000 | [diff] [blame] | 56 | DisableInline("disable-inlining", cl::init(false), |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 57 | cl::desc("Do not run the inliner pass")); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 58 | |
Bill Wendling | 9ac0aaa | 2012-08-06 21:34:54 +0000 | [diff] [blame] | 59 | static cl::opt<bool> |
| 60 | DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false), |
Bill Wendling | 3197b44 | 2012-04-02 22:16:50 +0000 | [diff] [blame] | 61 | cl::desc("Do not run the GVN load PRE pass")); |
| 62 | |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 63 | const char* LTOCodeGenerator::getVersionString() { |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 64 | #ifdef LLVM_VERSION_INFO |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 65 | return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 66 | #else |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 67 | return PACKAGE_NAME " version " PACKAGE_VERSION; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 68 | #endif |
| 69 | } |
| 70 | |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 71 | LTOCodeGenerator::LTOCodeGenerator() |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 72 | : _context(getGlobalContext()), |
Rafael Espindola | 1051937 | 2013-05-04 02:43:00 +0000 | [diff] [blame] | 73 | _linker(new Module("ld-temp.o", _context)), _target(NULL), |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 74 | _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false), |
Bill Wendling | 50f3183 | 2012-12-10 21:33:45 +0000 | [diff] [blame] | 75 | _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC), |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 76 | _nativeObjectFile(NULL) { |
| 77 | InitializeAllTargets(); |
| 78 | InitializeAllTargetMCs(); |
| 79 | InitializeAllAsmPrinters(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 82 | LTOCodeGenerator::~LTOCodeGenerator() { |
| 83 | delete _target; |
| 84 | delete _nativeObjectFile; |
Rafael Espindola | 1051937 | 2013-05-04 02:43:00 +0000 | [diff] [blame] | 85 | delete _linker.getModule(); |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 86 | |
Bill Wendling | f2cc2ee | 2012-03-31 10:51:45 +0000 | [diff] [blame] | 87 | for (std::vector<char*>::iterator I = _codegenOptions.begin(), |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 88 | E = _codegenOptions.end(); I != E; ++I) |
| 89 | free(*I); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 92 | bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) { |
Rafael Espindola | fca8863 | 2013-05-04 03:06:50 +0000 | [diff] [blame] | 93 | bool ret = _linker.linkInModule(mod->getLLVVMModule(), &errMsg); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 94 | |
| 95 | const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs(); |
| 96 | for (int i = 0, e = undefs.size(); i != e; ++i) |
| 97 | _asmUndefinedRefs[undefs[i]] = 1; |
| 98 | |
| 99 | return ret; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 100 | } |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 101 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 102 | bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, |
| 103 | std::string& errMsg) { |
| 104 | switch (debug) { |
| 105 | case LTO_DEBUG_MODEL_NONE: |
| 106 | _emitDwarfDebugInfo = false; |
| 107 | return false; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 108 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 109 | case LTO_DEBUG_MODEL_DWARF: |
| 110 | _emitDwarfDebugInfo = true; |
| 111 | return false; |
| 112 | } |
| 113 | llvm_unreachable("Unknown debug format!"); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 116 | bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model, |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 117 | std::string& errMsg) { |
| 118 | switch (model) { |
| 119 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 120 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 121 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
| 122 | _codeModel = model; |
| 123 | return false; |
| 124 | } |
| 125 | llvm_unreachable("Unknown PIC model!"); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 128 | bool LTOCodeGenerator::writeMergedModules(const char *path, |
| 129 | std::string &errMsg) { |
| 130 | if (determineTarget(errMsg)) |
| 131 | return true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 132 | |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 133 | // mark which symbols can not be internalized |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 134 | applyScopeRestrictions(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 135 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 136 | // create output file |
| 137 | std::string ErrInfo; |
Dan Gohman | f291401 | 2010-08-20 16:59:15 +0000 | [diff] [blame] | 138 | tool_output_file Out(path, ErrInfo, |
| 139 | raw_fd_ostream::F_Binary); |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 140 | if (!ErrInfo.empty()) { |
| 141 | errMsg = "could not open bitcode file for writing: "; |
| 142 | errMsg += path; |
| 143 | return true; |
| 144 | } |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 145 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 146 | // write bitcode to it |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 147 | WriteBitcodeToFile(_linker.getModule(), Out.os()); |
| 148 | Out.os().close(); |
Dan Gohman | 4b7416b | 2010-05-27 20:19:47 +0000 | [diff] [blame] | 149 | |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 150 | if (Out.os().has_error()) { |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 151 | errMsg = "could not write bitcode file: "; |
| 152 | errMsg += path; |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 153 | Out.os().clear_error(); |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 154 | return true; |
| 155 | } |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 156 | |
Dan Gohman | f291401 | 2010-08-20 16:59:15 +0000 | [diff] [blame] | 157 | Out.keep(); |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 158 | return false; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 161 | bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) { |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 162 | // make unique temp .o file to put generated object file |
Rafael Espindola | 68c0efa | 2013-06-17 18:05:35 +0000 | [diff] [blame^] | 163 | SmallString<128> Filename; |
| 164 | int FD; |
| 165 | error_code EC = sys::fs::unique_file("lto-llvm-%%%%%%%.o", |
| 166 | FD, Filename); |
| 167 | if (EC) { |
| 168 | errMsg = EC.message(); |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 169 | return true; |
| 170 | } |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 171 | |
| 172 | // generate object file |
Rafael Espindola | 68c0efa | 2013-06-17 18:05:35 +0000 | [diff] [blame^] | 173 | tool_output_file objFile(Filename.c_str(), FD); |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 174 | |
Rafael Espindola | 68c0efa | 2013-06-17 18:05:35 +0000 | [diff] [blame^] | 175 | bool genResult = generateObjectFile(objFile.os(), errMsg); |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 176 | objFile.os().close(); |
| 177 | if (objFile.os().has_error()) { |
| 178 | objFile.os().clear_error(); |
Rafael Espindola | 68c0efa | 2013-06-17 18:05:35 +0000 | [diff] [blame^] | 179 | sys::fs::remove(Twine(Filename)); |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 180 | return true; |
| 181 | } |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 182 | |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 183 | objFile.keep(); |
Bill Wendling | 0ca36af | 2012-08-08 22:03:50 +0000 | [diff] [blame] | 184 | if (genResult) { |
Rafael Espindola | 68c0efa | 2013-06-17 18:05:35 +0000 | [diff] [blame^] | 185 | sys::fs::remove(Twine(Filename)); |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 186 | return true; |
| 187 | } |
| 188 | |
Rafael Espindola | 68c0efa | 2013-06-17 18:05:35 +0000 | [diff] [blame^] | 189 | _nativeObjectPath = Filename.c_str(); |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 190 | *name = _nativeObjectPath.c_str(); |
| 191 | return false; |
| 192 | } |
| 193 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 194 | const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) { |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 195 | const char *name; |
| 196 | if (compile_to_file(&name, errMsg)) |
| 197 | return NULL; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 198 | |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 199 | // remove old buffer if compile() called twice |
| 200 | delete _nativeObjectFile; |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 201 | |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 202 | // read .o file into memory buffer |
| 203 | OwningPtr<MemoryBuffer> BuffPtr; |
| 204 | if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) { |
| 205 | errMsg = ec.message(); |
Rafael Espindola | 68c0efa | 2013-06-17 18:05:35 +0000 | [diff] [blame^] | 206 | sys::fs::remove(_nativeObjectPath); |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 207 | return NULL; |
| 208 | } |
| 209 | _nativeObjectFile = BuffPtr.take(); |
Rafael Espindola | e9efea1 | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 210 | |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 211 | // remove temp files |
Rafael Espindola | 68c0efa | 2013-06-17 18:05:35 +0000 | [diff] [blame^] | 212 | sys::fs::remove(_nativeObjectPath); |
Rafael Espindola | e9efea1 | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 213 | |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 214 | // return buffer, unless error |
Bill Wendling | 0ca36af | 2012-08-08 22:03:50 +0000 | [diff] [blame] | 215 | if (_nativeObjectFile == NULL) |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 216 | return NULL; |
| 217 | *length = _nativeObjectFile->getBufferSize(); |
| 218 | return _nativeObjectFile->getBufferStart(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Bill Wendling | 8c18a6f | 2013-05-23 21:21:50 +0000 | [diff] [blame] | 221 | bool LTOCodeGenerator::determineTarget(std::string &errMsg) { |
Bill Wendling | 0ca36af | 2012-08-08 22:03:50 +0000 | [diff] [blame] | 222 | if (_target != NULL) |
| 223 | return false; |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 224 | |
Bill Wendling | 8c18a6f | 2013-05-23 21:21:50 +0000 | [diff] [blame] | 225 | // if options were requested, set them |
| 226 | if (!_codegenOptions.empty()) |
| 227 | cl::ParseCommandLineOptions(_codegenOptions.size(), |
| 228 | const_cast<char **>(&_codegenOptions[0])); |
| 229 | |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 230 | std::string TripleStr = _linker.getModule()->getTargetTriple(); |
| 231 | if (TripleStr.empty()) |
| 232 | TripleStr = sys::getDefaultTargetTriple(); |
| 233 | llvm::Triple Triple(TripleStr); |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 234 | |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 235 | // create target machine from info for merged modules |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 236 | const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg); |
Bill Wendling | 0ca36af | 2012-08-08 22:03:50 +0000 | [diff] [blame] | 237 | if (march == NULL) |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 238 | return true; |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 239 | |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 240 | // The relocation model is actually a static member of TargetMachine and |
| 241 | // needs to be set before the TargetMachine is instantiated. |
| 242 | Reloc::Model RelocModel = Reloc::Default; |
Bill Wendling | 0ca36af | 2012-08-08 22:03:50 +0000 | [diff] [blame] | 243 | switch (_codeModel) { |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 244 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 245 | RelocModel = Reloc::Static; |
| 246 | break; |
| 247 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 248 | RelocModel = Reloc::PIC_; |
| 249 | break; |
| 250 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
| 251 | RelocModel = Reloc::DynamicNoPIC; |
| 252 | break; |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 253 | } |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 254 | |
| 255 | // construct LTOModule, hand over ownership of module and target |
| 256 | SubtargetFeatures Features; |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 257 | Features.getDefaultSubtargetFeatures(Triple); |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 258 | std::string FeatureStr = Features.getString(); |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 259 | // Set a default CPU for Darwin triples. |
| 260 | if (_mCpu.empty() && Triple.isOSDarwin()) { |
| 261 | if (Triple.getArch() == llvm::Triple::x86_64) |
| 262 | _mCpu = "core2"; |
| 263 | else if (Triple.getArch() == llvm::Triple::x86) |
| 264 | _mCpu = "yonah"; |
| 265 | } |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 266 | TargetOptions Options; |
| 267 | LTOModule::getTargetOptions(Options); |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 268 | _target = march->createTargetMachine(TripleStr, _mCpu, FeatureStr, Options, |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 269 | RelocModel, CodeModel::Default, |
| 270 | CodeGenOpt::Aggressive); |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 271 | return false; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 274 | void LTOCodeGenerator:: |
| 275 | applyRestriction(GlobalValue &GV, |
| 276 | std::vector<const char*> &mustPreserveList, |
| 277 | SmallPtrSet<GlobalValue*, 8> &asmUsed, |
| 278 | Mangler &mangler) { |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 279 | SmallString<64> Buffer; |
| 280 | mangler.getNameWithPrefix(Buffer, &GV, false); |
| 281 | |
| 282 | if (GV.isDeclaration()) |
| 283 | return; |
| 284 | if (_mustPreserveSymbols.count(Buffer)) |
| 285 | mustPreserveList.push_back(GV.getName().data()); |
| 286 | if (_asmUndefinedRefs.count(Buffer)) |
| 287 | asmUsed.insert(&GV); |
| 288 | } |
| 289 | |
| 290 | static void findUsedValues(GlobalVariable *LLVMUsed, |
| 291 | SmallPtrSet<GlobalValue*, 8> &UsedValues) { |
| 292 | if (LLVMUsed == 0) return; |
| 293 | |
Rafael Espindola | d4ee392 | 2013-04-24 17:54:35 +0000 | [diff] [blame] | 294 | ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer()); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 295 | for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 296 | if (GlobalValue *GV = |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 297 | dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts())) |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 298 | UsedValues.insert(GV); |
| 299 | } |
| 300 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 301 | void LTOCodeGenerator::applyScopeRestrictions() { |
| 302 | if (_scopeRestrictionsDone) return; |
| 303 | Module *mergedModule = _linker.getModule(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 305 | // Start off with a verification pass. |
| 306 | PassManager passes; |
| 307 | passes.add(createVerifierPass()); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 308 | |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 309 | // mark which symbols can not be internalized |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 310 | MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL); |
Bill Wendling | cc5a882 | 2013-05-29 20:37:19 +0000 | [diff] [blame] | 311 | Mangler mangler(Context, _target); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 312 | std::vector<const char*> mustPreserveList; |
| 313 | SmallPtrSet<GlobalValue*, 8> asmUsed; |
| 314 | |
| 315 | for (Module::iterator f = mergedModule->begin(), |
| 316 | e = mergedModule->end(); f != e; ++f) |
| 317 | applyRestriction(*f, mustPreserveList, asmUsed, mangler); |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 318 | for (Module::global_iterator v = mergedModule->global_begin(), |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 319 | e = mergedModule->global_end(); v != e; ++v) |
| 320 | applyRestriction(*v, mustPreserveList, asmUsed, mangler); |
| 321 | for (Module::alias_iterator a = mergedModule->alias_begin(), |
| 322 | e = mergedModule->alias_end(); a != e; ++a) |
| 323 | applyRestriction(*a, mustPreserveList, asmUsed, mangler); |
| 324 | |
| 325 | GlobalVariable *LLVMCompilerUsed = |
| 326 | mergedModule->getGlobalVariable("llvm.compiler.used"); |
| 327 | findUsedValues(LLVMCompilerUsed, asmUsed); |
| 328 | if (LLVMCompilerUsed) |
| 329 | LLVMCompilerUsed->eraseFromParent(); |
| 330 | |
Rafael Espindola | d4ee392 | 2013-04-24 17:54:35 +0000 | [diff] [blame] | 331 | if (!asmUsed.empty()) { |
| 332 | llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context); |
| 333 | std::vector<Constant*> asmUsed2; |
| 334 | for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(), |
| 335 | e = asmUsed.end(); i !=e; ++i) { |
| 336 | GlobalValue *GV = *i; |
| 337 | Constant *c = ConstantExpr::getBitCast(GV, i8PTy); |
| 338 | asmUsed2.push_back(c); |
| 339 | } |
| 340 | |
| 341 | llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size()); |
| 342 | LLVMCompilerUsed = |
| 343 | new llvm::GlobalVariable(*mergedModule, ATy, false, |
| 344 | llvm::GlobalValue::AppendingLinkage, |
| 345 | llvm::ConstantArray::get(ATy, asmUsed2), |
| 346 | "llvm.compiler.used"); |
| 347 | |
| 348 | LLVMCompilerUsed->setSection("llvm.metadata"); |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 349 | } |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 350 | |
Bill Wendling | 50f3183 | 2012-12-10 21:33:45 +0000 | [diff] [blame] | 351 | passes.add(createInternalizePass(mustPreserveList)); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 352 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 353 | // apply scope restrictions |
| 354 | passes.run(*mergedModule); |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 355 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 356 | _scopeRestrictionsDone = true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 359 | /// Optimize merged modules using various IPO passes |
Chris Lattner | 817a01f | 2011-05-22 00:20:07 +0000 | [diff] [blame] | 360 | bool LTOCodeGenerator::generateObjectFile(raw_ostream &out, |
| 361 | std::string &errMsg) { |
Bill Wendling | 0ca36af | 2012-08-08 22:03:50 +0000 | [diff] [blame] | 362 | if (this->determineTarget(errMsg)) |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 363 | return true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 364 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 365 | Module* mergedModule = _linker.getModule(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 366 | |
Bill Wendling | 8c18a6f | 2013-05-23 21:21:50 +0000 | [diff] [blame] | 367 | // Mark which symbols can not be internalized |
Bill Wendling | 64d5b28 | 2012-04-09 22:18:01 +0000 | [diff] [blame] | 368 | this->applyScopeRestrictions(); |
| 369 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 370 | // Instantiate the pass manager to organize the passes. |
| 371 | PassManager passes; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 372 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 373 | // Start off with a verification pass. |
| 374 | passes.add(createVerifierPass()); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 375 | |
Micah Villmow | 791cfc2 | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 376 | // Add an appropriate DataLayout instance for this module... |
| 377 | passes.add(new DataLayout(*_target->getDataLayout())); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 378 | _target->addAnalysisPasses(passes); |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 379 | |
Rafael Espindola | 4d2e9d9 | 2012-04-16 10:58:38 +0000 | [diff] [blame] | 380 | // Enabling internalize here would use its AllButMain variant. It |
| 381 | // keeps only main if it exists and does nothing for libraries. Instead |
| 382 | // we create the pass ourselves with the symbol list provided by the linker. |
Bill Wendling | 8c18a6f | 2013-05-23 21:21:50 +0000 | [diff] [blame] | 383 | if (!DisableOpt) |
Bill Wendling | 6303b66 | 2013-02-28 14:11:10 +0000 | [diff] [blame] | 384 | PassManagerBuilder().populateLTOPassManager(passes, |
Bill Wendling | 50f3183 | 2012-12-10 21:33:45 +0000 | [diff] [blame] | 385 | /*Internalize=*/false, |
Bill Wendling | 3197b44 | 2012-04-02 22:16:50 +0000 | [diff] [blame] | 386 | !DisableInline, |
| 387 | DisableGVNLoadPRE); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 388 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 389 | // Make sure everything is still good. |
| 390 | passes.add(createVerifierPass()); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 391 | |
Lang Hames | a991b25 | 2013-03-13 21:18:46 +0000 | [diff] [blame] | 392 | PassManager codeGenPasses; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 393 | |
Lang Hames | a991b25 | 2013-03-13 21:18:46 +0000 | [diff] [blame] | 394 | codeGenPasses.add(new DataLayout(*_target->getDataLayout())); |
| 395 | _target->addAnalysisPasses(codeGenPasses); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 396 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 397 | formatted_raw_ostream Out(out); |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 398 | |
Bob Wilson | d696544 | 2013-03-29 23:28:55 +0000 | [diff] [blame] | 399 | // If the bitcode files contain ARC code and were compiled with optimization, |
| 400 | // the ObjCARCContractPass must be run, so do it unconditionally here. |
| 401 | codeGenPasses.add(createObjCARCContractPass()); |
| 402 | |
Lang Hames | a991b25 | 2013-03-13 21:18:46 +0000 | [diff] [blame] | 403 | if (_target->addPassesToEmitFile(codeGenPasses, Out, |
David Blaikie | 4f56a30 | 2012-05-30 18:42:51 +0000 | [diff] [blame] | 404 | TargetMachine::CGFT_ObjectFile)) { |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 405 | errMsg = "target file type not supported"; |
| 406 | return true; |
| 407 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 408 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 409 | // Run our queue of passes all at once now, efficiently. |
| 410 | passes.run(*mergedModule); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 411 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 412 | // Run the code generator, and write assembly file |
Lang Hames | a991b25 | 2013-03-13 21:18:46 +0000 | [diff] [blame] | 413 | codeGenPasses.run(*mergedModule); |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 414 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 415 | return false; // success |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 418 | /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging |
| 419 | /// LTO problems. |
| 420 | void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) { |
| 421 | for (std::pair<StringRef, StringRef> o = getToken(options); |
| 422 | !o.first.empty(); o = getToken(o.second)) { |
| 423 | // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add |
| 424 | // that. |
Bill Wendling | 0ca36af | 2012-08-08 22:03:50 +0000 | [diff] [blame] | 425 | if (_codegenOptions.empty()) |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 426 | _codegenOptions.push_back(strdup("libLTO")); |
| 427 | _codegenOptions.push_back(strdup(o.first.str().c_str())); |
| 428 | } |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 429 | } |