zcov: / lib/Module/Optimize.cpp


Files: 1 Branches Taken: 44.1% 15 / 34
Generated: 2009-05-17 22:47 Branches Executed: 88.2% 30 / 34
Line Coverage: 96.6% 112 / 116


Programs: 1 Runs 371


       1                 : // FIXME: This file is a bastard child of opt.cpp and llvm-ld's
       2                 : // Optimize.cpp. This stuff should live in common code.
       3                 : 
       4                 : 
       5                 : //===- Optimize.cpp - Optimize a complete program -------------------------===//
       6                 : //
       7                 : //                     The LLVM Compiler Infrastructure
       8                 : //
       9                 : // This file is distributed under the University of Illinois Open Source
      10                 : // License. See LICENSE.TXT for details.
      11                 : //
      12                 : //===----------------------------------------------------------------------===//
      13                 : //
      14                 : // This file implements all optimization of the linked module for llvm-ld.
      15                 : //
      16                 : //===----------------------------------------------------------------------===//
      17                 : 
      18                 : #include "llvm/Module.h"
      19                 : #include "llvm/PassManager.h"
      20                 : #include "llvm/Analysis/Passes.h"
      21                 : #include "llvm/Analysis/LoopPass.h"
      22                 : #include "llvm/Analysis/Verifier.h"
      23                 : #include "llvm/Support/CommandLine.h"
      24                 : #include "llvm/System/DynamicLibrary.h"
      25                 : #include "llvm/Target/TargetData.h"
      26                 : #include "llvm/Target/TargetMachine.h"
      27                 : #include "llvm/Transforms/IPO.h"
      28                 : #include "llvm/Transforms/Scalar.h"
      29                 : #include "llvm/Support/PassNameParser.h"
      30                 : #include "llvm/Support/PluginLoader.h"
      31                 : #include <iostream>
      32                 : using namespace llvm;
      33                 : 
      34                 : #if 0
      35                 : // Pass Name Options as generated by the PassNameParser
      36                 : static cl::list<const PassInfo*, bool, PassNameParser>
      37                 :   OptimizationList(cl::desc("Optimizations available:"));
      38                 : #endif
      39                 : 
      40                 : // Don't verify at the end
      41              206: static cl::opt<bool> DontVerify("disable-verify", cl::ReallyHidden);
      42                 : 
      43              103: static cl::opt<bool> DisableInline("disable-inlining",
      44                 :   cl::desc("Do not run the inliner pass"));
      45                 : 
      46                 : static cl::opt<bool>
      47              103: DisableOptimizations("disable-opt",
      48              103:   cl::desc("Do not run any optimization passes"));
      49                 : 
      50              103: static cl::opt<bool> DisableInternalize("disable-internalize",
      51                 :   cl::desc("Do not mark all symbols as internal"));
      52                 : 
      53              103: static cl::opt<bool> VerifyEach("verify-each",
      54              103:  cl::desc("Verify intermediate results of all passes"));
      55                 : 
      56              103: static cl::alias ExportDynamic("export-dynamic",
      57                 :   cl::aliasopt(DisableInternalize),
      58                 :   cl::desc("Alias for -disable-internalize"));
      59                 : 
      60              103: static cl::opt<bool> Strip("strip-all", 
      61                 :   cl::desc("Strip all symbol info from executable"));
      62                 : 
      63              103: static cl::alias A0("s", cl::desc("Alias for --strip-all"), 
      64              103:   cl::aliasopt(Strip));
      65                 : 
      66              103: static cl::opt<bool> StripDebug("strip-debug",
      67              103:   cl::desc("Strip debugger symbol info from executable"));
      68                 : 
      69              103: static cl::alias A1("S", cl::desc("Alias for --strip-debug"),
      70              103:   cl::aliasopt(StripDebug));
      71                 : 
      72                 : // A utility function that adds a pass to the pass manager but will also add
      73                 : // a verifier pass after if we're supposed to verify.
      74              222: static inline void addPass(PassManager &PM, Pass *P) {
      75                 :   // Add the pass to the pass manager...
      76              222:   PM.add(P);
      77                 : 
      78                 :   // If we are verifying all of the intermediate steps, add the verifier...
                        0: branch 0 not taken
                      222: branch 1 taken
      79              222:   if (VerifyEach)
      80                0:     PM.add(createVerifierPass());
      81              222: }
      82                 : 
      83                 : namespace llvm {
      84                 : 
      85                 : 
      86                3: static void AddStandardCompilePasses(PassManager &PM) {
      87                3:   PM.add(createVerifierPass());                  // Verify that input is correct
      88                 : 
      89                3:   addPass(PM, createLowerSetJmpPass());          // Lower llvm.setjmp/.longjmp
      90                 : 
      91                 :   // If the -strip-debug command line option was specified, do it.
                        0: branch 0 not taken
                        3: branch 1 taken
      92                3:   if (StripDebug)
      93                0:     addPass(PM, createStripSymbolsPass(true));
      94                 : 
                        3: branch 0 taken
                        0: branch 1 not taken
      95                3:   if (DisableOptimizations) return;
      96                 : 
      97                3:   addPass(PM, createRaiseAllocationsPass());     // call %malloc -> malloc inst
      98                3:   addPass(PM, createCFGSimplificationPass());    // Clean up disgusting code
      99                3:   addPass(PM, createPromoteMemoryToRegisterPass());// Kill useless allocas
     100                3:   addPass(PM, createGlobalOptimizerPass());      // Optimize out global vars
     101                3:   addPass(PM, createGlobalDCEPass());            // Remove unused fns and globs
     102                3:   addPass(PM, createIPConstantPropagationPass());// IP Constant Propagation
     103                3:   addPass(PM, createDeadArgEliminationPass());   // Dead argument elimination
     104                3:   addPass(PM, createInstructionCombiningPass()); // Clean up after IPCP & DAE
     105                3:   addPass(PM, createCFGSimplificationPass());    // Clean up after IPCP & DAE
     106                 : 
     107                3:   addPass(PM, createPruneEHPass());              // Remove dead EH info
     108                3:   addPass(PM, createFunctionAttrsPass());        // Deduce function attrs
     109                 : 
                        3: branch 0 taken
                        0: branch 1 not taken
     110                3:   if (!DisableInline)
     111                3:     addPass(PM, createFunctionInliningPass());   // Inline small functions
     112                3:   addPass(PM, createArgumentPromotionPass());    // Scalarize uninlined fn args
     113                 : 
     114                3:   addPass(PM, createSimplifyLibCallsPass());     // Library Call Optimizations
     115                3:   addPass(PM, createInstructionCombiningPass()); // Cleanup for scalarrepl.
     116                3:   addPass(PM, createJumpThreadingPass());        // Thread jumps.
     117                3:   addPass(PM, createCFGSimplificationPass());    // Merge & remove BBs
     118                3:   addPass(PM, createScalarReplAggregatesPass()); // Break up aggregate allocas
     119                3:   addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
     120                3:   addPass(PM, createCondPropagationPass());      // Propagate conditionals
     121                 : 
     122                3:   addPass(PM, createTailCallEliminationPass());  // Eliminate tail calls
     123                3:   addPass(PM, createCFGSimplificationPass());    // Merge & remove BBs
     124                3:   addPass(PM, createReassociatePass());          // Reassociate expressions
     125                3:   addPass(PM, createLoopRotatePass());
     126                3:   addPass(PM, createLICMPass());                 // Hoist loop invariants
     127                3:   addPass(PM, createLoopUnswitchPass());         // Unswitch loops.
     128                3:   addPass(PM, createLoopIndexSplitPass());       // Index split loops.
     129                 :   // FIXME : Removing instcombine causes nestedloop regression.
     130                3:   addPass(PM, createInstructionCombiningPass());
     131                3:   addPass(PM, createIndVarSimplifyPass());       // Canonicalize indvars
     132                3:   addPass(PM, createLoopDeletionPass());         // Delete dead loops
     133                3:   addPass(PM, createLoopUnrollPass());           // Unroll small loops
     134                3:   addPass(PM, createInstructionCombiningPass()); // Clean up after the unroller
     135                3:   addPass(PM, createGVNPass());                  // Remove redundancies
     136                3:   addPass(PM, createMemCpyOptPass());            // Remove memcpy / form memset
     137                3:   addPass(PM, createSCCPPass());                 // Constant prop with SCCP
     138                 : 
     139                 :   // Run instcombine after redundancy elimination to exploit opportunities
     140                 :   // opened up by them.
     141                3:   addPass(PM, createInstructionCombiningPass());
     142                3:   addPass(PM, createCondPropagationPass());      // Propagate conditionals
     143                 : 
     144                3:   addPass(PM, createDeadStoreEliminationPass()); // Delete dead stores
     145                3:   addPass(PM, createAggressiveDCEPass());        // Delete dead instructions
     146                3:   addPass(PM, createCFGSimplificationPass());    // Merge & remove BBs
     147                3:   addPass(PM, createStripDeadPrototypesPass());  // Get rid of dead prototypes
     148                3:   addPass(PM, createDeadTypeEliminationPass());  // Eliminate dead types
     149                3:   addPass(PM, createConstantMergePass());        // Merge dup global constants
     150                 : }
     151                 : 
     152                 : /// Optimize - Perform link time optimizations. This will run the scalar
     153                 : /// optimizations, any loaded plugin-optimization modules, and then the
     154                 : /// inter-procedural optimizations if applicable.
     155                3: void Optimize(Module* M) {
     156                 : 
     157                 :   // Instantiate the pass manager to organize the passes.
     158                3:   PassManager Passes;
     159                 : 
     160                 :   // If we're verifying, start off with a verification pass.
                        0: branch 0 not taken
                        3: branch 1 taken
     161                3:   if (VerifyEach)
     162                0:     Passes.add(createVerifierPass());
     163                 : 
     164                 :   // Add an appropriate TargetData instance for this module...
     165                3:   addPass(Passes, new TargetData(M));
     166                 : 
     167                 :   // DWD - Run the opt standard pass list as well.
     168                3:   AddStandardCompilePasses(Passes);
     169                 : 
                        3: branch 0 taken
                        0: branch 1 not taken
     170                3:   if (!DisableOptimizations) {
     171                 :     // Now that composite has been compiled, scan through the module, looking
     172                 :     // for a main function.  If main is defined, mark all other functions
     173                 :     // internal.
                        3: branch 0 taken
                        0: branch 1 not taken
     174                3:     if (!DisableInternalize)
     175                3:       addPass(Passes, createInternalizePass(true));
     176                 : 
     177                 :     // Propagate constants at call sites into the functions they call.  This
     178                 :     // opens opportunities for globalopt (and inlining) by substituting function
     179                 :     // pointers passed as arguments to direct uses of functions.  
     180                3:     addPass(Passes, createIPSCCPPass());
     181                 : 
     182                 :     // Now that we internalized some globals, see if we can hack on them!
     183                3:     addPass(Passes, createGlobalOptimizerPass());
     184                 : 
     185                 :     // Linking modules together can lead to duplicated global constants, only
     186                 :     // keep one copy of each constant...
     187                3:     addPass(Passes, createConstantMergePass());
     188                 : 
     189                 :     // Remove unused arguments from functions...
     190                3:     addPass(Passes, createDeadArgEliminationPass());
     191                 : 
     192                 :     // Reduce the code after globalopt and ipsccp.  Both can open up significant
     193                 :     // simplification opportunities, and both can propagate functions through
     194                 :     // function pointers.  When this happens, we often have to resolve varargs
     195                 :     // calls, etc, so let instcombine do this.
     196                3:     addPass(Passes, createInstructionCombiningPass());
     197                 : 
                        3: branch 0 taken
                        0: branch 1 not taken
     198                3:     if (!DisableInline)
     199                3:       addPass(Passes, createFunctionInliningPass()); // Inline small functions
     200                 : 
     201                3:     addPass(Passes, createPruneEHPass());            // Remove dead EH info
     202                3:     addPass(Passes, createGlobalOptimizerPass());    // Optimize globals again.
     203                3:     addPass(Passes, createGlobalDCEPass());          // Remove dead functions
     204                 : 
     205                 :     // If we didn't decide to inline a function, check to see if we can
     206                 :     // transform it to pass arguments by value instead of by reference.
     207                3:     addPass(Passes, createArgumentPromotionPass());
     208                 : 
     209                 :     // The IPO passes may leave cruft around.  Clean up after them.
     210                3:     addPass(Passes, createInstructionCombiningPass());
     211                3:     addPass(Passes, createJumpThreadingPass());        // Thread jumps.
     212                3:     addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
     213                 : 
     214                 :     // Run a few AA driven optimizations here and now, to cleanup the code.
     215                3:     addPass(Passes, createFunctionAttrsPass());      // Add nocapture
     216                3:     addPass(Passes, createGlobalsModRefPass());      // IP alias analysis
     217                 : 
     218                3:     addPass(Passes, createLICMPass());               // Hoist loop invariants
     219                3:     addPass(Passes, createGVNPass());                // Remove redundancies
     220                3:     addPass(Passes, createMemCpyOptPass());          // Remove dead memcpy's
     221                3:     addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores
     222                 : 
     223                 :     // Cleanup and simplify the code after the scalar optimizations.
     224                3:     addPass(Passes, createInstructionCombiningPass());
     225                 : 
     226                3:     addPass(Passes, createJumpThreadingPass());        // Thread jumps.
     227                3:     addPass(Passes, createPromoteMemoryToRegisterPass()); // Cleanup jumpthread.
     228                 :     
     229                 :     // Delete basic blocks, which optimization passes may have killed...
     230                3:     addPass(Passes, createCFGSimplificationPass());
     231                 : 
     232                 :     // Now that we have optimized the program, discard unreachable functions...
     233                3:     addPass(Passes, createGlobalDCEPass());
     234                 :   }
     235                 : 
     236                 :   // If the -s or -S command line options were specified, strip the symbols out
     237                 :   // of the resulting program to make it smaller.  -s and -S are GNU ld options
     238                 :   // that we are supporting; they alias -strip-all and -strip-debug.
                        3: branch 0 taken
                        0: branch 1 not taken
                        0: branch 2 not taken
                        3: branch 3 taken
                        0: branch 4 not taken
                        3: branch 5 taken
     239                6:   if (Strip || StripDebug)
                        0: branch 0 not taken
                        0: branch 1 not taken
                        0: branch 2 not taken
                        0: branch 3 not taken
     240                0:     addPass(Passes, createStripSymbolsPass(StripDebug && !Strip));
     241                 : 
     242                 : #if 0
     243                 :   // Create a new optimization pass for each one specified on the command line
     244                 :   std::auto_ptr<TargetMachine> target;
     245                 :   for (unsigned i = 0; i < OptimizationList.size(); ++i) {
     246                 :     const PassInfo *Opt = OptimizationList[i];
     247                 :     if (Opt->getNormalCtor())
     248                 :       addPass(Passes, Opt->getNormalCtor()());
     249                 :     else
     250                 :       std::cerr << "llvm-ld: cannot create pass: " << Opt->getPassName() 
     251                 :                 << "\n";
     252                 :   }
     253                 : #endif
     254                 : 
     255                 :   // The user's passes may leave cruft around. Clean up after them them but
     256                 :   // only if we haven't got DisableOptimizations set
                        3: branch 0 taken
                        0: branch 1 not taken
     257                3:   if (!DisableOptimizations) {
     258                3:     addPass(Passes, createInstructionCombiningPass());
     259                3:     addPass(Passes, createCFGSimplificationPass());
     260                3:     addPass(Passes, createAggressiveDCEPass());
     261                3:     addPass(Passes, createGlobalDCEPass());
     262                 :   }
     263                 : 
     264                 :   // Make sure everything is still good.
                        3: branch 0 taken
                        0: branch 1 not taken
     265                3:   if (!DontVerify)
     266                3:     Passes.add(createVerifierPass());
     267                 : 
     268                 :   // Run our queue of passes all at once now, efficiently.
     269                3:   Passes.run(*M);
     270                3: }
     271                 : 
                      103: branch 0 taken
                        0: branch 1 not taken
                      103: branch 2 taken
                        0: branch 3 not taken
     272              309: }

Generated: 2009-05-17 22:47 by zcov