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