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