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" |
| 17 | |
| 18 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
| 20 | #include "llvm/DerivedTypes.h" |
Nick Lewycky | 8189d40 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 21 | #include "llvm/Linker.h" |
Owen Anderson | 0e7a546 | 2009-07-02 00:31:14 +0000 | [diff] [blame] | 22 | #include "llvm/LLVMContext.h" |
Nick Lewycky | 8189d40 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 24 | #include "llvm/ModuleProvider.h" |
Nick Lewycky | 8189d40 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 25 | #include "llvm/PassManager.h" |
| 26 | #include "llvm/ADT/StringExtras.h" |
| 27 | #include "llvm/Analysis/Passes.h" |
| 28 | #include "llvm/Analysis/LoopPass.h" |
| 29 | #include "llvm/Analysis/Verifier.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 30 | #include "llvm/Bitcode/ReaderWriter.h" |
Nick Lewycky | 8189d40 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/FileWriters.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" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Mangler.h" |
| 35 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 006a034 | 2009-06-03 21:06:14 +0000 | [diff] [blame] | 36 | #include "llvm/Support/StandardPasses.h" |
| 37 | #include "llvm/Support/SystemUtils.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 38 | #include "llvm/System/Signals.h" |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 39 | #include "llvm/Target/SubtargetFeature.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 40 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 2deb58f | 2009-06-17 16:42:19 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetAsmInfo.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetData.h" |
| 43 | #include "llvm/Target/TargetMachine.h" |
| 44 | #include "llvm/Target/TargetMachineRegistry.h" |
Chris Lattner | 2deb58f | 2009-06-17 16:42:19 +0000 | [diff] [blame] | 45 | #include "llvm/Target/TargetSelect.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 46 | #include "llvm/Transforms/IPO.h" |
| 47 | #include "llvm/Transforms/Scalar.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 48 | #include "llvm/Config/config.h" |
| 49 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 50 | |
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 <fstream> |
| 53 | #include <unistd.h> |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 54 | #include <fcntl.h> |
| 55 | |
| 56 | |
| 57 | using namespace llvm; |
| 58 | |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 59 | static cl::opt<bool> DisableInline("disable-inlining", |
| 60 | cl::desc("Do not run the inliner pass")); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | const char* LTOCodeGenerator::getVersionString() |
| 64 | { |
| 65 | #ifdef LLVM_VERSION_INFO |
| 66 | return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO; |
| 67 | #else |
| 68 | return PACKAGE_NAME " version " PACKAGE_VERSION; |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | |
Owen Anderson | 0e7a546 | 2009-07-02 00:31:14 +0000 | [diff] [blame] | 73 | LTOCodeGenerator::LTOCodeGenerator() |
| 74 | : _context(getGlobalContext()), |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 75 | _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL), |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 76 | _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false), |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 77 | _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC), |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 78 | _nativeObjectFile(NULL), _gccPath(NULL), _assemblerPath(NULL) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 79 | { |
Chris Lattner | 2deb58f | 2009-06-17 16:42:19 +0000 | [diff] [blame] | 80 | InitializeAllTargets(); |
| 81 | InitializeAllAsmPrinters(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 82 | |
| 83 | } |
| 84 | |
| 85 | LTOCodeGenerator::~LTOCodeGenerator() |
| 86 | { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 87 | delete _target; |
| 88 | delete _nativeObjectFile; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) |
| 94 | { |
| 95 | return _linker.LinkInModule(mod->getLLVVMModule(), &errMsg); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg) |
| 100 | { |
| 101 | switch (debug) { |
| 102 | case LTO_DEBUG_MODEL_NONE: |
| 103 | _emitDwarfDebugInfo = false; |
| 104 | return false; |
| 105 | |
| 106 | case LTO_DEBUG_MODEL_DWARF: |
| 107 | _emitDwarfDebugInfo = true; |
| 108 | return false; |
| 109 | } |
| 110 | errMsg = "unknown debug format"; |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | |
| 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 | } |
| 125 | errMsg = "unknown pic model"; |
| 126 | return true; |
| 127 | } |
| 128 | |
Nick Lewycky | 195bea3 | 2009-04-30 15:24:09 +0000 | [diff] [blame] | 129 | void LTOCodeGenerator::setGccPath(const char* path) |
| 130 | { |
| 131 | if ( _gccPath ) |
| 132 | delete _gccPath; |
| 133 | _gccPath = new sys::Path(path); |
| 134 | } |
| 135 | |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 136 | void LTOCodeGenerator::setAssemblerPath(const char* path) |
| 137 | { |
| 138 | if ( _assemblerPath ) |
| 139 | delete _assemblerPath; |
| 140 | _assemblerPath = new sys::Path(path); |
| 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 | |
| 149 | bool LTOCodeGenerator::writeMergedModules(const char* path, std::string& errMsg) |
| 150 | { |
| 151 | if ( this->determineTarget(errMsg) ) |
| 152 | return true; |
| 153 | |
| 154 | // mark which symbols can not be internalized |
| 155 | this->applyScopeRestrictions(); |
| 156 | |
| 157 | // create output file |
| 158 | std::ofstream out(path, std::ios_base::out|std::ios::trunc|std::ios::binary); |
| 159 | if ( out.fail() ) { |
| 160 | errMsg = "could not open bitcode file for writing: "; |
| 161 | errMsg += path; |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | // write bitcode to it |
| 166 | WriteBitcodeToFile(_linker.getModule(), out); |
| 167 | if ( out.fail() ) { |
| 168 | errMsg = "could not write bitcode file: "; |
| 169 | errMsg += path; |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 177 | const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 178 | { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 179 | // make unique temp .s file to put generated assembly code |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 180 | sys::Path uniqueAsmPath("lto-llvm.s"); |
| 181 | if ( uniqueAsmPath.createTemporaryFileOnDisk(true, &errMsg) ) |
| 182 | return NULL; |
| 183 | sys::RemoveFileOnSignal(uniqueAsmPath); |
| 184 | |
| 185 | // generate assembly code |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 186 | bool genResult = false; |
| 187 | { |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 188 | raw_fd_ostream asmFD(raw_fd_ostream(uniqueAsmPath.c_str(), |
| 189 | false, errMsg)); |
| 190 | formatted_raw_ostream asmFile(asmFD); |
Dan Gohman | ed3e8b4 | 2008-08-21 15:33:45 +0000 | [diff] [blame] | 191 | if (!errMsg.empty()) |
| 192 | return NULL; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 193 | genResult = this->generateAssemblyCode(asmFile, errMsg); |
| 194 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 195 | if ( genResult ) { |
| 196 | if ( uniqueAsmPath.exists() ) |
| 197 | uniqueAsmPath.eraseFromDisk(); |
| 198 | return NULL; |
| 199 | } |
| 200 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 201 | // make unique temp .o file to put generated object file |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 202 | sys::PathWithStatus uniqueObjPath("lto-llvm.o"); |
| 203 | if ( uniqueObjPath.createTemporaryFileOnDisk(true, &errMsg) ) { |
| 204 | if ( uniqueAsmPath.exists() ) |
| 205 | uniqueAsmPath.eraseFromDisk(); |
| 206 | return NULL; |
| 207 | } |
| 208 | sys::RemoveFileOnSignal(uniqueObjPath); |
| 209 | |
| 210 | // assemble the assembly code |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 211 | const std::string& uniqueObjStr = uniqueObjPath.toString(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 212 | bool asmResult = this->assemble(uniqueAsmPath.toString(), |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 213 | uniqueObjStr, errMsg); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 214 | if ( !asmResult ) { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 215 | // remove old buffer if compile() called twice |
| 216 | delete _nativeObjectFile; |
| 217 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 218 | // read .o file into memory buffer |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 219 | _nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(),&errMsg); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 220 | } |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 221 | |
| 222 | // remove temp files |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 223 | uniqueAsmPath.eraseFromDisk(); |
| 224 | uniqueObjPath.eraseFromDisk(); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 225 | |
| 226 | // return buffer, unless error |
| 227 | if ( _nativeObjectFile == NULL ) |
| 228 | return NULL; |
| 229 | *length = _nativeObjectFile->getBufferSize(); |
| 230 | return _nativeObjectFile->getBufferStart(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
| 234 | bool LTOCodeGenerator::assemble(const std::string& asmPath, |
| 235 | const std::string& objPath, std::string& errMsg) |
| 236 | { |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 237 | sys::Path tool; |
| 238 | bool needsCompilerOptions = true; |
| 239 | if ( _assemblerPath ) { |
| 240 | tool = *_assemblerPath; |
| 241 | needsCompilerOptions = false; |
| 242 | } |
| 243 | else if ( _gccPath ) { |
| 244 | tool = *_gccPath; |
Nick Lewycky | 195bea3 | 2009-04-30 15:24:09 +0000 | [diff] [blame] | 245 | } else { |
| 246 | // find compiler driver |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 247 | tool = sys::Program::FindProgramByName("gcc"); |
| 248 | if ( tool.isEmpty() ) { |
Nick Lewycky | 195bea3 | 2009-04-30 15:24:09 +0000 | [diff] [blame] | 249 | errMsg = "can't locate gcc"; |
| 250 | return true; |
| 251 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // build argument list |
| 255 | std::vector<const char*> args; |
| 256 | std::string targetTriple = _linker.getModule()->getTargetTriple(); |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 257 | args.push_back(tool.c_str()); |
Rafael Espindola | f403cd7 | 2009-06-09 21:14:25 +0000 | [diff] [blame] | 258 | if ( targetTriple.find("darwin") != std::string::npos ) { |
Nick Kledzik | d8b4711 | 2009-06-04 19:14:08 +0000 | [diff] [blame] | 259 | // darwin specific command line options |
Devang Patel | f75e789 | 2008-11-04 23:13:50 +0000 | [diff] [blame] | 260 | if (strncmp(targetTriple.c_str(), "i386-apple-", 11) == 0) { |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 261 | args.push_back("-arch"); |
| 262 | args.push_back("i386"); |
| 263 | } |
| 264 | else if (strncmp(targetTriple.c_str(), "x86_64-apple-", 13) == 0) { |
| 265 | args.push_back("-arch"); |
| 266 | args.push_back("x86_64"); |
| 267 | } |
| 268 | else if (strncmp(targetTriple.c_str(), "powerpc-apple-", 14) == 0) { |
| 269 | args.push_back("-arch"); |
| 270 | args.push_back("ppc"); |
| 271 | } |
| 272 | else if (strncmp(targetTriple.c_str(), "powerpc64-apple-", 16) == 0) { |
| 273 | args.push_back("-arch"); |
| 274 | args.push_back("ppc64"); |
| 275 | } |
Evan Cheng | 9f777c6 | 2009-04-01 18:54:56 +0000 | [diff] [blame] | 276 | else if (strncmp(targetTriple.c_str(), "arm-apple-", 10) == 0) { |
| 277 | args.push_back("-arch"); |
| 278 | args.push_back("arm"); |
| 279 | } |
| 280 | else if ((strncmp(targetTriple.c_str(), "armv4t-apple-", 13) == 0) || |
| 281 | (strncmp(targetTriple.c_str(), "thumbv4t-apple-", 15) == 0)) { |
| 282 | args.push_back("-arch"); |
| 283 | args.push_back("armv4t"); |
| 284 | } |
| 285 | else if ((strncmp(targetTriple.c_str(), "armv5-apple-", 12) == 0) || |
| 286 | (strncmp(targetTriple.c_str(), "armv5e-apple-", 13) == 0) || |
| 287 | (strncmp(targetTriple.c_str(), "thumbv5-apple-", 14) == 0) || |
| 288 | (strncmp(targetTriple.c_str(), "thumbv5e-apple-", 15) == 0)) { |
| 289 | args.push_back("-arch"); |
| 290 | args.push_back("armv5"); |
| 291 | } |
| 292 | else if ((strncmp(targetTriple.c_str(), "armv6-apple-", 12) == 0) || |
| 293 | (strncmp(targetTriple.c_str(), "thumbv6-apple-", 14) == 0)) { |
| 294 | args.push_back("-arch"); |
| 295 | args.push_back("armv6"); |
| 296 | } |
Bob Wilson | 75d6ffd | 2009-06-22 18:01:28 +0000 | [diff] [blame] | 297 | else if ((strncmp(targetTriple.c_str(), "armv7-apple-", 12) == 0) || |
| 298 | (strncmp(targetTriple.c_str(), "thumbv7-apple-", 14) == 0)) { |
| 299 | args.push_back("-arch"); |
| 300 | args.push_back("armv7"); |
| 301 | } |
Nick Kledzik | d8b4711 | 2009-06-04 19:14:08 +0000 | [diff] [blame] | 302 | // add -static to assembler command line when code model requires |
| 303 | if ( (_assemblerPath != NULL) && (_codeModel == LTO_CODEGEN_PIC_MODEL_STATIC) ) |
| 304 | args.push_back("-static"); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 305 | } |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 306 | if ( needsCompilerOptions ) { |
| 307 | args.push_back("-c"); |
| 308 | args.push_back("-x"); |
| 309 | args.push_back("assembler"); |
| 310 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 311 | args.push_back("-o"); |
| 312 | args.push_back(objPath.c_str()); |
| 313 | args.push_back(asmPath.c_str()); |
| 314 | args.push_back(0); |
| 315 | |
| 316 | // invoke assembler |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 317 | if ( sys::Program::ExecuteAndWait(tool, &args[0], 0, 0, 0, 0, &errMsg) ) { |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 318 | errMsg = "error in assembly"; |
| 319 | return true; |
| 320 | } |
| 321 | return false; // success |
| 322 | } |
| 323 | |
| 324 | |
| 325 | |
| 326 | bool LTOCodeGenerator::determineTarget(std::string& errMsg) |
| 327 | { |
| 328 | if ( _target == NULL ) { |
| 329 | // create target machine from info for merged modules |
| 330 | Module* mergedModule = _linker.getModule(); |
Stuart Hastings | 2286f8d | 2009-07-15 17:27:11 +0000 | [diff] [blame^] | 331 | const TargetMachineRegistry::entry* march = |
| 332 | TargetMachineRegistry::getClosestStaticTargetForModule( |
| 333 | *mergedModule, errMsg); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 334 | if ( march == NULL ) |
| 335 | return true; |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 336 | |
Nick Kledzik | f5a1c35f1 | 2009-06-03 22:52:12 +0000 | [diff] [blame] | 337 | // The relocation model is actually a static member of TargetMachine |
| 338 | // and needs to be set before the TargetMachine is instantiated. |
| 339 | switch( _codeModel ) { |
| 340 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 341 | TargetMachine::setRelocationModel(Reloc::Static); |
| 342 | break; |
| 343 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 344 | TargetMachine::setRelocationModel(Reloc::PIC_); |
| 345 | break; |
| 346 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
| 347 | TargetMachine::setRelocationModel(Reloc::DynamicNoPIC); |
| 348 | break; |
| 349 | } |
| 350 | |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 351 | // construct LTModule, hand over ownership of module and target |
Bill Wendling | e424254 | 2008-06-18 21:39:02 +0000 | [diff] [blame] | 352 | std::string FeatureStr = |
| 353 | getFeatureString(_linker.getModule()->getTargetTriple().c_str()); |
Stuart Hastings | 2286f8d | 2009-07-15 17:27:11 +0000 | [diff] [blame^] | 354 | _target = march->CtorFn(*mergedModule, FeatureStr.c_str()); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 355 | } |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | void LTOCodeGenerator::applyScopeRestrictions() |
| 360 | { |
| 361 | if ( !_scopeRestrictionsDone ) { |
| 362 | Module* mergedModule = _linker.getModule(); |
| 363 | |
| 364 | // Start off with a verification pass. |
| 365 | PassManager passes; |
| 366 | passes.add(createVerifierPass()); |
| 367 | |
| 368 | // mark which symbols can not be internalized |
| 369 | if ( !_mustPreserveSymbols.empty() ) { |
| 370 | Mangler mangler(*mergedModule, |
| 371 | _target->getTargetAsmInfo()->getGlobalPrefix()); |
| 372 | std::vector<const char*> mustPreserveList; |
| 373 | for (Module::iterator f = mergedModule->begin(), |
| 374 | e = mergedModule->end(); f != e; ++f) { |
| 375 | if ( !f->isDeclaration() |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 376 | && _mustPreserveSymbols.count(mangler.getMangledName(f)) ) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 377 | mustPreserveList.push_back(::strdup(f->getName().c_str())); |
| 378 | } |
| 379 | for (Module::global_iterator v = mergedModule->global_begin(), |
| 380 | e = mergedModule->global_end(); v != e; ++v) { |
| 381 | if ( !v->isDeclaration() |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 382 | && _mustPreserveSymbols.count(mangler.getMangledName(v)) ) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 383 | mustPreserveList.push_back(::strdup(v->getName().c_str())); |
| 384 | } |
| 385 | passes.add(createInternalizePass(mustPreserveList)); |
| 386 | } |
| 387 | // apply scope restrictions |
| 388 | passes.run(*mergedModule); |
| 389 | |
| 390 | _scopeRestrictionsDone = true; |
| 391 | } |
| 392 | } |
| 393 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 394 | /// Optimize merged modules using various IPO passes |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 395 | bool LTOCodeGenerator::generateAssemblyCode(formatted_raw_ostream& out, |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 396 | std::string& errMsg) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 397 | { |
| 398 | if ( this->determineTarget(errMsg) ) |
| 399 | return true; |
| 400 | |
| 401 | // mark which symbols can not be internalized |
| 402 | this->applyScopeRestrictions(); |
| 403 | |
| 404 | Module* mergedModule = _linker.getModule(); |
| 405 | |
| 406 | // If target supports exception handling then enable it now. |
| 407 | if ( _target->getTargetAsmInfo()->doesSupportExceptionHandling() ) |
| 408 | llvm::ExceptionHandling = true; |
| 409 | |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 410 | // if options were requested, set them |
| 411 | if ( !_codegenOptions.empty() ) |
| 412 | cl::ParseCommandLineOptions(_codegenOptions.size(), |
| 413 | (char**)&_codegenOptions[0]); |
Devang Patel | a93ae71 | 2008-07-03 22:53:14 +0000 | [diff] [blame] | 414 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 415 | // Instantiate the pass manager to organize the passes. |
| 416 | PassManager passes; |
| 417 | |
| 418 | // Start off with a verification pass. |
| 419 | passes.add(createVerifierPass()); |
| 420 | |
| 421 | // Add an appropriate TargetData instance for this module... |
| 422 | passes.add(new TargetData(*_target->getTargetData())); |
| 423 | |
Daniel Dunbar | 006a034 | 2009-06-03 21:06:14 +0000 | [diff] [blame] | 424 | createStandardLTOPasses(&passes, /*Internalize=*/ false, !DisableInline, |
Daniel Dunbar | 006a034 | 2009-06-03 21:06:14 +0000 | [diff] [blame] | 425 | /*VerifyEach=*/ false); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 426 | |
| 427 | // Make sure everything is still good. |
| 428 | passes.add(createVerifierPass()); |
| 429 | |
| 430 | FunctionPassManager* codeGenPasses = |
| 431 | new FunctionPassManager(new ExistingModuleProvider(mergedModule)); |
| 432 | |
| 433 | codeGenPasses->add(new TargetData(*_target->getTargetData())); |
| 434 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 435 | ObjectCodeEmitter* oce = NULL; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 436 | |
| 437 | switch (_target->addPassesToEmitFile(*codeGenPasses, out, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 438 | TargetMachine::AssemblyFile, |
Bill Wendling | b8cb0bb | 2009-04-29 23:40:42 +0000 | [diff] [blame] | 439 | CodeGenOpt::Aggressive)) { |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 440 | case FileModel::MachOFile: |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 441 | oce = AddMachOWriter(*codeGenPasses, out, *_target); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 442 | break; |
| 443 | case FileModel::ElfFile: |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 444 | oce = AddELFWriter(*codeGenPasses, out, *_target); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 445 | break; |
| 446 | case FileModel::AsmFile: |
| 447 | break; |
| 448 | case FileModel::Error: |
| 449 | case FileModel::None: |
| 450 | errMsg = "target file type not supported"; |
| 451 | return true; |
| 452 | } |
| 453 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 454 | if (_target->addPassesToEmitFileFinish(*codeGenPasses, oce, |
Bill Wendling | b8cb0bb | 2009-04-29 23:40:42 +0000 | [diff] [blame] | 455 | CodeGenOpt::Aggressive)) { |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 456 | errMsg = "target does not support generation of this file type"; |
| 457 | return true; |
| 458 | } |
| 459 | |
| 460 | // Run our queue of passes all at once now, efficiently. |
| 461 | passes.run(*mergedModule); |
| 462 | |
| 463 | // Run the code generator, and write assembly file |
| 464 | codeGenPasses->doInitialization(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 465 | |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 466 | for (Module::iterator |
| 467 | it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it) |
| 468 | if (!it->isDeclaration()) |
| 469 | codeGenPasses->run(*it); |
| 470 | |
| 471 | codeGenPasses->doFinalization(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 472 | return false; // success |
| 473 | } |
| 474 | |
| 475 | |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 476 | /// Optimize merged modules using various IPO passes |
| 477 | void LTOCodeGenerator::setCodeGenDebugOptions(const char* options) |
| 478 | { |
| 479 | std::string ops(options); |
| 480 | for (std::string o = getToken(ops); !o.empty(); o = getToken(ops)) { |
| 481 | // ParseCommandLineOptions() expects argv[0] to be program name. |
| 482 | // Lazily add that. |
| 483 | if ( _codegenOptions.empty() ) |
| 484 | _codegenOptions.push_back("libLTO"); |
| 485 | _codegenOptions.push_back(strdup(o.c_str())); |
| 486 | } |
| 487 | } |