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