Add support to insert trace code as an "optimization"
[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 <iostream.h>
10 #include <fstream.h>
11 #include "llvm/Module.h"
12 #include "llvm/Bytecode/Reader.h"
13 #include "llvm/Bytecode/Writer.h"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Optimizations/AllOpts.h"
16 #include "llvm/Transforms/Instrumentation/TraceValues.h"
17 #include "llvm/Transforms/PrintModulePass.h"
18
19 using namespace opt;
20
21 enum Opts {
22   // Basic optimizations
23   dce, constprop, inlining, strip, mstrip,
24
25   // Miscellaneous Transformations
26   trace, tracem, print,
27
28   // More powerful optimizations
29   indvars, sccp, adce, raise,
30 };
31
32 struct {
33   enum Opts OptID;
34   Pass *ThePass;
35 } OptTable[] = {
36   { dce      , new opt::DeadCodeElimination() },
37   { constprop, new opt::ConstantPropogation() }, 
38   { inlining , new opt::MethodInlining() },
39   { strip    , new opt::SymbolStripping() },
40   { mstrip   , new opt::FullSymbolStripping() },
41   { indvars  , new opt::InductionVariableCannonicalize() },
42   { sccp     , new opt::SCCPPass() },
43   { adce     , new opt::AgressiveDCE() },
44   { raise    , new opt::RaiseRepresentation() },
45   { trace    , new InsertTraceCode(true, true) },
46   { tracem   , new InsertTraceCode(false, true) },
47   { print    , new PrintModulePass("Current Method: \n",&cerr) },
48 };
49
50 cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
51 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
52 cl::Flag   Force         ("f", "Overwrite output files", cl::NoFlags, false);
53 cl::Flag   Quiet         ("q", "Don't print modifying pass names", 0, false);
54 cl::Alias  QuietA        ("quiet", "Alias for -q", cl::NoFlags, Quiet);
55 cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
56   clEnumVal(dce      , "Dead Code Elimination"),
57   clEnumVal(constprop, "Simple Constant Propogation"),
58  clEnumValN(inlining , "inline", "Method Integration"),
59   clEnumVal(strip    , "Strip Symbols"),
60   clEnumVal(mstrip   , "Strip Module Symbols"),
61   clEnumVal(indvars  , "Simplify Induction Variables"),
62   clEnumVal(sccp     , "Sparse Conditional Constant Propogation"),
63   clEnumVal(adce     , "Agressive DCE"),
64   clEnumVal(raise    , "Raise to Higher Level"),
65   clEnumVal(trace    , "Insert BB & Method trace code"),
66   clEnumVal(tracem   , "Insert Method trace code only"),
67   clEnumVal(print    , "Print working method to stderr"),
68 0);
69
70
71 int main(int argc, char **argv) {
72   cl::ParseCommandLineOptions(argc, argv,
73                               " llvm .bc -> .bc modular optimizer\n");
74  
75   Module *C = ParseBytecodeFile(InputFilename);
76   if (C == 0) {
77     cerr << "bytecode didn't read correctly.\n";
78     return 1;
79   }
80
81   for (unsigned i = 0; i < OptimizationList.size(); ++i) {
82     enum Opts Opt = OptimizationList[i];
83
84     unsigned j;
85     for (j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j) {
86       if (Opt == OptTable[j].OptID) {
87         if (OptTable[j].ThePass->run(C) && !Quiet)
88           cerr << OptimizationList.getArgName(Opt)
89                << " pass made modifications!\n";
90         break;
91       }
92     }
93
94     if (j == sizeof(OptTable)/sizeof(OptTable[0])) 
95       cerr << "Optimization tables inconsistent!!\n";
96   }
97
98   ostream *Out = &cout;  // Default to printing to stdout...
99   if (OutputFilename != "") {
100     Out = new ofstream(OutputFilename.c_str(), 
101                        (Force ? 0 : ios::noreplace)|ios::out);
102     if (!Out->good()) {
103       cerr << "Error opening " << OutputFilename << "!\n";
104       delete C;
105       return 1;
106     }
107   }
108
109   // Okay, we're done now... write out result...
110   WriteBytecodeToFile(C, *Out);
111   delete C;
112
113   if (Out != &cout) delete Out;
114   return 0;
115 }