166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//===- Optimize.cpp - Optimize a complete program -------------------------===// 266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// 366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// The LLVM Compiler Infrastructure 466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// 566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// This file is distributed under the University of Illinois Open Source 666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// License. See LICENSE.TXT for details. 766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// 866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//===----------------------------------------------------------------------===// 966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// 1066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// This file implements all optimization of the linked module for llvm-ld. 1166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// 1266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//===----------------------------------------------------------------------===// 1366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 1466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Module.h" 1566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/PassManager.h" 1666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Analysis/Verifier.h" 1766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Support/CommandLine.h" 1866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Support/raw_ostream.h" 1966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Support/DynamicLibrary.h" 2066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Target/TargetData.h" 2166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Target/TargetMachine.h" 2266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Support/PassNameParser.h" 2366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Support/PluginLoader.h" 2466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Transforms/IPO.h" 2566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Transforms/IPO/PassManagerBuilder.h" 2666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Transforms/Scalar.h" 2766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanusing namespace llvm; 2866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 2966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// Pass Name Options as generated by the PassNameParser 3066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanstatic cl::list<const PassInfo*, bool, PassNameParser> 3166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman OptimizationList(cl::desc("Optimizations available:")); 3266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 3366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//Don't verify at the end 3466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanstatic cl::opt<bool> DontVerify("disable-verify", cl::ReallyHidden); 3566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 3666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanstatic cl::opt<bool> DisableInline("disable-inlining", 3766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman cl::desc("Do not run the inliner pass")); 3866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 3966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanstatic cl::opt<bool> 4066b8ab22586debccb1f787d4d52b7f042d4ddeb8John BaumanDisableOptimizations("disable-opt", 4166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman cl::desc("Do not run any optimization passes")); 4266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 4366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanstatic cl::opt<bool> DisableInternalize("disable-internalize", 4466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman cl::desc("Do not mark all symbols as internal")); 4566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 4666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanstatic cl::opt<bool> VerifyEach("verify-each", 4766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman cl::desc("Verify intermediate results of all passes")); 4866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 4966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanstatic cl::alias ExportDynamic("export-dynamic", 5066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman cl::aliasopt(DisableInternalize), 5166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman cl::desc("Alias for -disable-internalize")); 5266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 5366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanstatic cl::opt<bool> Strip("strip-all", 5466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman cl::desc("Strip all symbol info from executable")); 5566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 5666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanstatic cl::alias A0("s", cl::desc("Alias for --strip-all"), 5766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman cl::aliasopt(Strip)); 5866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 5966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanstatic cl::opt<bool> StripDebug("strip-debug", 6066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman cl::desc("Strip debugger symbol info from executable")); 6166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 6266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanstatic cl::alias A1("S", cl::desc("Alias for --strip-debug"), 6366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman cl::aliasopt(StripDebug)); 6466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 6566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// A utility function that adds a pass to the pass manager but will also add 6666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// a verifier pass after if we're supposed to verify. 6766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanstatic inline void addPass(PassManager &PM, Pass *P) { 6866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman // Add the pass to the pass manager... 6966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman PM.add(P); 7066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 7166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman // If we are verifying all of the intermediate steps, add the verifier... 7266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman if (VerifyEach) 7366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman PM.add(createVerifierPass()); 7466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman} 7566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 7666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumannamespace llvm { 7766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman/// Optimize - Perform link time optimizations. This will run the scalar 7866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman/// optimizations, any loaded plugin-optimization modules, and then the 7966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman/// inter-procedural optimizations if applicable. 8066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanvoid Optimize(Module *M) { 8166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 8266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman // Instantiate the pass manager to organize the passes. 8366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman PassManager Passes; 8466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 8566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman // If we're verifying, start off with a verification pass. 8666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman if (VerifyEach) 8766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman Passes.add(createVerifierPass()); 8866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 8966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman // Add an appropriate TargetData instance for this module... 9066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman addPass(Passes, new TargetData(M)); 9166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 9266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman if (!DisableOptimizations) 9366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman PassManagerBuilder().populateLTOPassManager(Passes, !DisableInternalize, 9466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman !DisableInline); 9566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 9666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman // If the -s or -S command line options were specified, strip the symbols out 9766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman // of the resulting program to make it smaller. -s and -S are GNU ld options 9866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman // that we are supporting; they alias -strip-all and -strip-debug. 9966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman if (Strip || StripDebug) 10066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman addPass(Passes, createStripSymbolsPass(StripDebug && !Strip)); 10166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 10266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman // Create a new optimization pass for each one specified on the command line 10366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman std::auto_ptr<TargetMachine> target; 10466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman for (unsigned i = 0; i < OptimizationList.size(); ++i) { 10566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman const PassInfo *Opt = OptimizationList[i]; 10666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman if (Opt->getNormalCtor()) 10766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman addPass(Passes, Opt->getNormalCtor()()); 10866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman else 10966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman errs() << "llvm-ld: cannot create pass: " << Opt->getPassName() 11066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman << "\n"; 11166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman } 11266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 11366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman // The user's passes may leave cruft around. Clean up after them them but 11466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman // only if we haven't got DisableOptimizations set 11566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman if (!DisableOptimizations) { 11666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman addPass(Passes, createInstructionCombiningPass()); 11766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman addPass(Passes, createCFGSimplificationPass()); 11866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman addPass(Passes, createAggressiveDCEPass()); 11966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman addPass(Passes, createGlobalDCEPass()); 12066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman } 12166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 12266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman // Make sure everything is still good. 12366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman if (!DontVerify) 12466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman Passes.add(createVerifierPass()); 12566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 12666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman // Run our queue of passes all at once now, efficiently. 12766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman Passes.run(*M); 12866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman} 12966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 13066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman} 131