Expose funcresolve pass through opt
[oota-llvm.git] / tools / opt / opt.cpp
1 //===----------------------------------------------------------------------===//
2 // LLVM 'OPT' UTILITY 
3 //
4 // Optimizations may be specified an arbitrary number of times on the command
5 // line, they are run in the order specified.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Module.h"
10 #include "llvm/PassManager.h"
11 #include "llvm/Bytecode/Reader.h"
12 #include "llvm/Bytecode/WriteBytecodePass.h"
13 #include "llvm/Assembly/PrintModulePass.h"
14 #include "llvm/Analysis/Verifier.h"
15 #include "llvm/Transforms/UnifyMethodExitNodes.h"
16 #include "llvm/Transforms/ConstantMerge.h"
17 #include "llvm/Transforms/CleanupGCCOutput.h"
18 #include "llvm/Transforms/LevelChange.h"
19 #include "llvm/Transforms/MethodInlining.h"
20 #include "llvm/Transforms/SymbolStripping.h"
21 #include "llvm/Transforms/ChangeAllocations.h"
22 #include "llvm/Transforms/IPO/SimpleStructMutation.h"
23 #include "llvm/Transforms/IPO/GlobalDCE.h"
24 #include "llvm/Transforms/IPO/PoolAllocate.h"
25 #include "llvm/Transforms/Scalar/DCE.h"
26 #include "llvm/Transforms/Scalar/ConstantProp.h"
27 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
28 #include "llvm/Transforms/Scalar/InstructionCombining.h"
29 #include "llvm/Transforms/Scalar/PromoteMemoryToRegister.h"
30 #include "llvm/Transforms/Scalar/DecomposeMultiDimRefs.h"
31 #include "llvm/Transforms/Instrumentation/TraceValues.h"
32 #include "llvm/Transforms/Instrumentation/ProfilePaths.h"
33 #include "Support/CommandLine.h"
34 #include <fstream>
35 #include <memory>
36
37 // Opts enum - All of the transformations we can do...
38 enum Opts {
39   // Basic optimizations
40   dce, die, constprop, inlining, constmerge, strip, mstrip, mergereturn,
41
42   // Miscellaneous Transformations
43   raiseallocs, funcresolve, cleangcc, lowerrefs,
44
45   // Printing and verifying...
46   print, verify,
47
48   // More powerful optimizations
49   indvars, instcombine, sccp, adce, raise, mem2reg,
50
51   // Instrumentation
52   trace, tracem, paths,
53
54   // Interprocedural optimizations...
55   globaldce, swapstructs, sortstructs, poolalloc,
56 };
57
58 static Pass *createPrintMethodPass() {
59   return new PrintFunctionPass("Current Method: \n", &cerr);
60 }
61
62 // OptTable - Correlate enum Opts to Pass constructors...
63 //
64 struct {
65   enum Opts OptID;
66   Pass * (*PassCtor)();
67 } OptTable[] = {
68   { dce        , createDeadCodeEliminationPass },
69   { die        , createDeadInstEliminationPass },
70   { constprop  , createConstantPropogationPass }, 
71   { inlining   , createMethodInliningPass },
72   { constmerge , createConstantMergePass },
73   { strip      , createSymbolStrippingPass },
74   { mstrip     , createFullSymbolStrippingPass },
75   { mergereturn, createUnifyMethodExitNodesPass },
76
77   { indvars    , createIndVarSimplifyPass },
78   { instcombine, createInstructionCombiningPass },
79   { sccp       , createSCCPPass },
80   { adce       , createAgressiveDCEPass },
81   { raise      , createRaisePointerReferencesPass },
82   { mem2reg    , createPromoteMemoryToRegister },
83   { lowerrefs,   createDecomposeMultiDimRefsPass },
84
85   { trace      , createTraceValuesPassForBasicBlocks },
86   { tracem     , createTraceValuesPassForMethod },
87   { paths      , createProfilePathsPass },
88
89   { print      , createPrintMethodPass },
90   { verify     , createVerifierPass },
91
92   { raiseallocs, createRaiseAllocationsPass },
93   { cleangcc   , createCleanupGCCOutputPass },
94   { funcresolve, createFunctionResolvingPass },
95   { globaldce  , createGlobalDCEPass },
96   { swapstructs, createSwapElementsPass },
97   { sortstructs, createSortElementsPass },
98   { poolalloc  , createPoolAllocatePass },
99 };
100
101
102 // Command line option handling code...
103 //
104 cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
105 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
106 cl::Flag   Force         ("f", "Overwrite output files", cl::NoFlags, false);
107 cl::Flag   PrintEachXForm("p", "Print module after each transformation");
108 cl::Flag   Quiet         ("q", "Don't print modifying pass names", 0, false);
109 cl::Alias  QuietA        ("quiet", "Alias for -q", cl::NoFlags, Quiet);
110 cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
111   clEnumVal(dce        , "Dead Code Elimination"),
112   clEnumVal(die        , "Dead Instruction Elimination"),
113   clEnumVal(constprop  , "Simple constant propogation"),
114  clEnumValN(inlining   , "inline", "Method integration"),
115   clEnumVal(constmerge , "Merge identical global constants"),
116   clEnumVal(strip      , "Strip symbols"),
117   clEnumVal(mstrip     , "Strip module symbols"),
118   clEnumVal(mergereturn, "Unify method exit nodes"),
119
120   clEnumVal(indvars    , "Simplify Induction Variables"),
121   clEnumVal(instcombine, "Combine redundant instructions"),
122   clEnumVal(sccp       , "Sparse Conditional Constant Propogation"),
123   clEnumVal(adce       , "Agressive DCE"),
124   clEnumVal(mem2reg    , "Promote alloca locations to registers"),
125
126   clEnumVal(globaldce  , "Remove unreachable globals"),
127   clEnumVal(swapstructs, "Swap structure types around"),
128   clEnumVal(sortstructs, "Sort structure elements"),
129   clEnumVal(poolalloc  , "Pool allocate disjoint datastructures"),
130
131   clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
132   clEnumVal(cleangcc   , "Cleanup GCC Output"),
133   clEnumVal(funcresolve, "Resolve calls to foo(...) to foo(<concrete types>)"),
134   clEnumVal(raise      , "Raise to Higher Level"),
135   clEnumVal(trace      , "Insert BB & Method trace code"),
136   clEnumVal(tracem     , "Insert Method trace code only"),
137   clEnumVal(paths      , "Insert path profiling instrumentation"),
138   clEnumVal(print      , "Print working method to stderr"),
139   clEnumVal(verify     , "Verify module is well formed"),
140   clEnumVal(lowerrefs  , "Decompose multi-dimensional structure/array refs to use one index per instruction"),
141 0);
142
143
144
145 int main(int argc, char **argv) {
146   cl::ParseCommandLineOptions(argc, argv,
147                               " llvm .bc -> .bc modular optimizer\n");
148
149   // Load the input module...
150   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
151   if (M.get() == 0) {
152     cerr << "bytecode didn't read correctly.\n";
153     return 1;
154   }
155
156   // Figure out what stream we are supposed to write to...
157   std::ostream *Out = &std::cout;  // Default to printing to stdout...
158   if (OutputFilename != "") {
159     if (!Force && std::ifstream(OutputFilename.c_str())) {
160       // If force is not specified, make sure not to overwrite a file!
161       cerr << "Error opening '" << OutputFilename << "': File exists!\n"
162            << "Use -f command line argument to force output\n";
163       return 1;
164     }
165     Out = new std::ofstream(OutputFilename.c_str());
166
167     if (!Out->good()) {
168       cerr << "Error opening " << OutputFilename << "!\n";
169       return 1;
170     }
171   }
172
173   // Create a PassManager to hold and optimize the collection of passes we are
174   // about to build...
175   //
176   PassManager Passes;
177
178   // Create a new optimization pass for each one specified on the command line
179   for (unsigned i = 0; i < OptimizationList.size(); ++i) {
180     enum Opts Opt = OptimizationList[i];
181     for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
182       if (Opt == OptTable[j].OptID) {
183         Passes.add(OptTable[j].PassCtor());
184         break;
185       }
186
187     if (PrintEachXForm)
188       Passes.add(new PrintModulePass(&std::cerr));
189   }
190
191   // Check that the module is well formed on completion of optimization
192   Passes.add(createVerifierPass());
193
194   // Write bytecode out to disk or cout as the last step...
195   Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
196
197   // Now that we have all of the passes ready, run them.
198   if (Passes.run(M.get()) && !Quiet)
199     cerr << "Program modified.\n";
200
201   return 0;
202 }