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