Extract optimization pass selection code from llvm-gcc into a separate routine.
[oota-llvm.git] / utils / PassManagerUtils.cpp
1 //===-- PassManagerUtils.cpp - --------------------------------------------===//
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 pass manager utiliy routines.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/PassManagerUtils.h"
15 #include "llvm/PassManagers.h"
16 #include "llvm/Transforms/Scalar.h"
17 #include "llvm/Transforms/IPO.h"
18 #include "llvm/Analysis/LoopPass.h"
19
20 /// AddOptimizationPasses - This routine adds optimization passes 
21 /// based on selected optimization level, OptLevel. This routine is
22 /// used by llvm-gcc and other tools.
23 ///
24 /// OptLevel - Optimization Level
25 /// EnableIPO - Enables IPO passes. llvm-gcc enables this when
26 ///             flag_unit_at_a_time is set.
27 /// InlinerSelection - 1 : Add function inliner.
28 ///                  - 2 : Add AlwaysInliner.
29 /// OptLibCalls - Simplify lib calls, if set.
30 /// PruneEH - Add PruneEHPass, if set.
31 /// UnrollLoop - Unroll loops, if set.
32 void llvm::AddOptimizationPasses(FunctionPassManager &FPM, PassManager &MPM,
33                                  unsigned OptLevel, bool EnableIPO,
34                                  unsigned InlinerSelection, bool OptLibCalls,
35                                  bool PruneEH, bool UnrollLoop) {
36   if (OptLevel == 0) 
37     return;
38
39   FPM.add(createCFGSimplificationPass());
40   if (OptLevel == 1)
41     FPM.add(createPromoteMemoryToRegisterPass());
42   else
43     FPM.add(createScalarReplAggregatesPass());
44   FPM.add(createInstructionCombiningPass());
45
46   if (EnableIPO)
47     MPM.add(createRaiseAllocationsPass());      // call %malloc -> malloc inst
48   MPM.add(createCFGSimplificationPass());       // Clean up disgusting code
49   MPM.add(createPromoteMemoryToRegisterPass()); // Kill useless allocas
50   if (EnableIPO) {
51     MPM.add(createGlobalOptimizerPass());       // OptLevel out global vars
52     MPM.add(createGlobalDCEPass());             // Remove unused fns and globs
53     MPM.add(createIPConstantPropagationPass()); // IP Constant Propagation
54     MPM.add(createDeadArgEliminationPass());    // Dead argument elimination
55   }
56   MPM.add(createInstructionCombiningPass());    // Clean up after IPCP & DAE
57   MPM.add(createCFGSimplificationPass());       // Clean up after IPCP & DAE
58   if (EnableIPO && PruneEH)
59     MPM.add(createPruneEHPass());               // Remove dead EH info
60   if (InlinerSelection == 1)                    // respect -fno-inline-functions
61     MPM.add(createFunctionInliningPass());      // Inline small functions
62   else if (InlinerSelection == 2)
63     MPM.add(createAlwaysInlinerPass());         // Inline always_inline functions
64   if (OptLevel > 2)
65     MPM.add(createArgumentPromotionPass());   // Scalarize uninlined fn args
66   if (OptLibCalls)
67     MPM.add(createSimplifyLibCallsPass());    // Library Call Optimizations
68   MPM.add(createInstructionCombiningPass());  // Cleanup for scalarrepl.
69   MPM.add(createJumpThreadingPass());         // Thread jumps.
70   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
71   MPM.add(createScalarReplAggregatesPass());  // Break up aggregate allocas
72   MPM.add(createInstructionCombiningPass());  // Combine silly seq's
73   MPM.add(createCondPropagationPass());       // Propagate conditionals
74   MPM.add(createTailCallEliminationPass());   // Eliminate tail calls
75   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
76   MPM.add(createReassociatePass());           // Reassociate expressions
77   MPM.add(createLoopRotatePass());            // Rotate Loop
78   MPM.add(createLICMPass());                  // Hoist loop invariants
79   MPM.add(createLoopUnswitchPass());
80   MPM.add(createLoopIndexSplitPass());        // Split loop index
81   MPM.add(createInstructionCombiningPass());  
82   MPM.add(createIndVarSimplifyPass());        // Canonicalize indvars
83   MPM.add(createLoopDeletionPass());          // Delete dead loops
84   if (UnrollLoop)
85     MPM.add(createLoopUnrollPass());          // Unroll small loops
86   MPM.add(createInstructionCombiningPass());  // Clean up after the unroller
87   MPM.add(createGVNPass());                   // Remove redundancies
88   MPM.add(createMemCpyOptPass());             // Remove memcpy / form memset
89   MPM.add(createSCCPPass());                  // Constant prop with SCCP
90   
91   // Run instcombine after redundancy elimination to exploit opportunities
92   // opened up by them.
93   MPM.add(createInstructionCombiningPass());
94   MPM.add(createCondPropagationPass());       // Propagate conditionals
95   MPM.add(createDeadStoreEliminationPass());  // Delete dead stores
96   MPM.add(createAggressiveDCEPass());   // Delete dead instructions
97   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
98   
99   if (EnableIPO) {
100     MPM.add(createStripDeadPrototypesPass());   // Get rid of dead prototypes
101     MPM.add(createDeadTypeEliminationPass());   // Eliminate dead types
102   }
103   
104   if (OptLevel > 1 && EnableIPO)
105     MPM.add(createConstantMergePass());       // Merge dup global constants 
106   
107   return;
108 }