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