Optimize.cpp

Go to the documentation of this file.
00001 // FIXME: This file is a bastard child of opt.cpp and llvm-ld's
00002 // Optimize.cpp. This stuff should live in common code.
00003 
00004 
00005 //===- Optimize.cpp - Optimize a complete program -------------------------===//
00006 //
00007 //                     The LLVM Compiler Infrastructure
00008 //
00009 // This file is distributed under the University of Illinois Open Source
00010 // License. See LICENSE.TXT for details.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 //
00014 // This file implements all optimization of the linked module for llvm-ld.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #include "llvm/Module.h"
00019 #include "llvm/PassManager.h"
00020 #include "llvm/Analysis/Passes.h"
00021 #include "llvm/Analysis/LoopPass.h"
00022 #include "llvm/Analysis/Verifier.h"
00023 #include "llvm/Support/CommandLine.h"
00024 #include "llvm/System/DynamicLibrary.h"
00025 #include "llvm/Target/TargetData.h"
00026 #include "llvm/Target/TargetMachine.h"
00027 #include "llvm/Transforms/IPO.h"
00028 #include "llvm/Transforms/Scalar.h"
00029 #include "llvm/Support/PassNameParser.h"
00030 #include "llvm/Support/PluginLoader.h"
00031 #include <iostream>
00032 using namespace llvm;
00033 
00034 #if 0
00035 // Pass Name Options as generated by the PassNameParser
00036 static cl::list<const PassInfo*, bool, PassNameParser>
00037   OptimizationList(cl::desc("Optimizations available:"));
00038 #endif
00039 
00040 // Don't verify at the end
00041 static cl::opt<bool> DontVerify("disable-verify", cl::ReallyHidden);
00042 
00043 static cl::opt<bool> DisableInline("disable-inlining",
00044   cl::desc("Do not run the inliner pass"));
00045 
00046 static cl::opt<bool>
00047 DisableOptimizations("disable-opt",
00048   cl::desc("Do not run any optimization passes"));
00049 
00050 static cl::opt<bool> DisableInternalize("disable-internalize",
00051   cl::desc("Do not mark all symbols as internal"));
00052 
00053 static cl::opt<bool> VerifyEach("verify-each",
00054  cl::desc("Verify intermediate results of all passes"));
00055 
00056 static cl::alias ExportDynamic("export-dynamic",
00057   cl::aliasopt(DisableInternalize),
00058   cl::desc("Alias for -disable-internalize"));
00059 
00060 static cl::opt<bool> Strip("strip-all", 
00061   cl::desc("Strip all symbol info from executable"));
00062 
00063 static cl::alias A0("s", cl::desc("Alias for --strip-all"), 
00064   cl::aliasopt(Strip));
00065 
00066 static cl::opt<bool> StripDebug("strip-debug",
00067   cl::desc("Strip debugger symbol info from executable"));
00068 
00069 static cl::alias A1("S", cl::desc("Alias for --strip-debug"),
00070   cl::aliasopt(StripDebug));
00071 
00072 // A utility function that adds a pass to the pass manager but will also add
00073 // a verifier pass after if we're supposed to verify.
00074 static inline void addPass(PassManager &PM, Pass *P) {
00075   // Add the pass to the pass manager...
00076   PM.add(P);
00077 
00078   // If we are verifying all of the intermediate steps, add the verifier...
00079   if (VerifyEach)
00080     PM.add(createVerifierPass());
00081 }
00082 
00083 namespace llvm {
00084 
00085 
00086 static void AddStandardCompilePasses(PassManager &PM) {
00087   PM.add(createVerifierPass());                  // Verify that input is correct
00088 
00089   addPass(PM, createLowerSetJmpPass());          // Lower llvm.setjmp/.longjmp
00090 
00091   // If the -strip-debug command line option was specified, do it.
00092   if (StripDebug)
00093     addPass(PM, createStripSymbolsPass(true));
00094 
00095   if (DisableOptimizations) return;
00096 
00097   addPass(PM, createRaiseAllocationsPass());     // call %malloc -> malloc inst
00098   addPass(PM, createCFGSimplificationPass());    // Clean up disgusting code
00099   addPass(PM, createPromoteMemoryToRegisterPass());// Kill useless allocas
00100   addPass(PM, createGlobalOptimizerPass());      // Optimize out global vars
00101   addPass(PM, createGlobalDCEPass());            // Remove unused fns and globs
00102   addPass(PM, createIPConstantPropagationPass());// IP Constant Propagation
00103   addPass(PM, createDeadArgEliminationPass());   // Dead argument elimination
00104   addPass(PM, createInstructionCombiningPass()); // Clean up after IPCP & DAE
00105   addPass(PM, createCFGSimplificationPass());    // Clean up after IPCP & DAE
00106 
00107   addPass(PM, createPruneEHPass());              // Remove dead EH info
00108   addPass(PM, createFunctionAttrsPass());        // Deduce function attrs
00109 
00110   if (!DisableInline)
00111     addPass(PM, createFunctionInliningPass());   // Inline small functions
00112   addPass(PM, createArgumentPromotionPass());    // Scalarize uninlined fn args
00113 
00114   addPass(PM, createSimplifyLibCallsPass());     // Library Call Optimizations
00115   addPass(PM, createInstructionCombiningPass()); // Cleanup for scalarrepl.
00116   addPass(PM, createJumpThreadingPass());        // Thread jumps.
00117   addPass(PM, createCFGSimplificationPass());    // Merge & remove BBs
00118   addPass(PM, createScalarReplAggregatesPass()); // Break up aggregate allocas
00119   addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
00120   addPass(PM, createCondPropagationPass());      // Propagate conditionals
00121 
00122   addPass(PM, createTailCallEliminationPass());  // Eliminate tail calls
00123   addPass(PM, createCFGSimplificationPass());    // Merge & remove BBs
00124   addPass(PM, createReassociatePass());          // Reassociate expressions
00125   addPass(PM, createLoopRotatePass());
00126   addPass(PM, createLICMPass());                 // Hoist loop invariants
00127   addPass(PM, createLoopUnswitchPass());         // Unswitch loops.
00128   addPass(PM, createLoopIndexSplitPass());       // Index split loops.
00129   // FIXME : Removing instcombine causes nestedloop regression.
00130   addPass(PM, createInstructionCombiningPass());
00131   addPass(PM, createIndVarSimplifyPass());       // Canonicalize indvars
00132   addPass(PM, createLoopDeletionPass());         // Delete dead loops
00133   addPass(PM, createLoopUnrollPass());           // Unroll small loops
00134   addPass(PM, createInstructionCombiningPass()); // Clean up after the unroller
00135   addPass(PM, createGVNPass());                  // Remove redundancies
00136   addPass(PM, createMemCpyOptPass());            // Remove memcpy / form memset
00137   addPass(PM, createSCCPPass());                 // Constant prop with SCCP
00138 
00139   // Run instcombine after redundancy elimination to exploit opportunities
00140   // opened up by them.
00141   addPass(PM, createInstructionCombiningPass());
00142   addPass(PM, createCondPropagationPass());      // Propagate conditionals
00143 
00144   addPass(PM, createDeadStoreEliminationPass()); // Delete dead stores
00145   addPass(PM, createAggressiveDCEPass());        // Delete dead instructions
00146   addPass(PM, createCFGSimplificationPass());    // Merge & remove BBs
00147   addPass(PM, createStripDeadPrototypesPass());  // Get rid of dead prototypes
00148   addPass(PM, createDeadTypeEliminationPass());  // Eliminate dead types
00149   addPass(PM, createConstantMergePass());        // Merge dup global constants
00150 }
00151 
00155 void Optimize(Module* M) {
00156 
00157   // Instantiate the pass manager to organize the passes.
00158   PassManager Passes;
00159 
00160   // If we're verifying, start off with a verification pass.
00161   if (VerifyEach)
00162     Passes.add(createVerifierPass());
00163 
00164   // Add an appropriate TargetData instance for this module...
00165   addPass(Passes, new TargetData(M));
00166 
00167   // DWD - Run the opt standard pass list as well.
00168   AddStandardCompilePasses(Passes);
00169 
00170   if (!DisableOptimizations) {
00171     // Now that composite has been compiled, scan through the module, looking
00172     // for a main function.  If main is defined, mark all other functions
00173     // internal.
00174     if (!DisableInternalize)
00175       addPass(Passes, createInternalizePass(true));
00176 
00177     // Propagate constants at call sites into the functions they call.  This
00178     // opens opportunities for globalopt (and inlining) by substituting function
00179     // pointers passed as arguments to direct uses of functions.  
00180     addPass(Passes, createIPSCCPPass());
00181 
00182     // Now that we internalized some globals, see if we can hack on them!
00183     addPass(Passes, createGlobalOptimizerPass());
00184 
00185     // Linking modules together can lead to duplicated global constants, only
00186     // keep one copy of each constant...
00187     addPass(Passes, createConstantMergePass());
00188 
00189     // Remove unused arguments from functions...
00190     addPass(Passes, createDeadArgEliminationPass());
00191 
00192     // Reduce the code after globalopt and ipsccp.  Both can open up significant
00193     // simplification opportunities, and both can propagate functions through
00194     // function pointers.  When this happens, we often have to resolve varargs
00195     // calls, etc, so let instcombine do this.
00196     addPass(Passes, createInstructionCombiningPass());
00197 
00198     if (!DisableInline)
00199       addPass(Passes, createFunctionInliningPass()); // Inline small functions
00200 
00201     addPass(Passes, createPruneEHPass());            // Remove dead EH info
00202     addPass(Passes, createGlobalOptimizerPass());    // Optimize globals again.
00203     addPass(Passes, createGlobalDCEPass());          // Remove dead functions
00204 
00205     // If we didn't decide to inline a function, check to see if we can
00206     // transform it to pass arguments by value instead of by reference.
00207     addPass(Passes, createArgumentPromotionPass());
00208 
00209     // The IPO passes may leave cruft around.  Clean up after them.
00210     addPass(Passes, createInstructionCombiningPass());
00211     addPass(Passes, createJumpThreadingPass());        // Thread jumps.
00212     addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
00213 
00214     // Run a few AA driven optimizations here and now, to cleanup the code.
00215     addPass(Passes, createFunctionAttrsPass());      // Add nocapture
00216     addPass(Passes, createGlobalsModRefPass());      // IP alias analysis
00217 
00218     addPass(Passes, createLICMPass());               // Hoist loop invariants
00219     addPass(Passes, createGVNPass());                // Remove redundancies
00220     addPass(Passes, createMemCpyOptPass());          // Remove dead memcpy's
00221     addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores
00222 
00223     // Cleanup and simplify the code after the scalar optimizations.
00224     addPass(Passes, createInstructionCombiningPass());
00225 
00226     addPass(Passes, createJumpThreadingPass());        // Thread jumps.
00227     addPass(Passes, createPromoteMemoryToRegisterPass()); // Cleanup jumpthread.
00228     
00229     // Delete basic blocks, which optimization passes may have killed...
00230     addPass(Passes, createCFGSimplificationPass());
00231 
00232     // Now that we have optimized the program, discard unreachable functions...
00233     addPass(Passes, createGlobalDCEPass());
00234   }
00235 
00236   // If the -s or -S command line options were specified, strip the symbols out
00237   // of the resulting program to make it smaller.  -s and -S are GNU ld options
00238   // that we are supporting; they alias -strip-all and -strip-debug.
00239   if (Strip || StripDebug)
00240     addPass(Passes, createStripSymbolsPass(StripDebug && !Strip));
00241 
00242 #if 0
00243   // Create a new optimization pass for each one specified on the command line
00244   std::auto_ptr<TargetMachine> target;
00245   for (unsigned i = 0; i < OptimizationList.size(); ++i) {
00246     const PassInfo *Opt = OptimizationList[i];
00247     if (Opt->getNormalCtor())
00248       addPass(Passes, Opt->getNormalCtor()());
00249     else
00250       std::cerr << "llvm-ld: cannot create pass: " << Opt->getPassName() 
00251                 << "\n";
00252   }
00253 #endif
00254 
00255   // The user's passes may leave cruft around. Clean up after them them but
00256   // only if we haven't got DisableOptimizations set
00257   if (!DisableOptimizations) {
00258     addPass(Passes, createInstructionCombiningPass());
00259     addPass(Passes, createCFGSimplificationPass());
00260     addPass(Passes, createAggressiveDCEPass());
00261     addPass(Passes, createGlobalDCEPass());
00262   }
00263 
00264   // Make sure everything is still good.
00265   if (!DontVerify)
00266     Passes.add(createVerifierPass());
00267 
00268   // Run our queue of passes all at once now, efficiently.
00269   Passes.run(*M);
00270 }
00271 
00272 }

Generated on Fri Jun 5 03:31:32 2009 for klee by  doxygen 1.5.8