Remove support for "target data" pass ctors
[oota-llvm.git] / tools / opt / opt.cpp
1 //===----------------------------------------------------------------------===//
2 // LLVM Modular Optimizer Utility: opt
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/Target/TargetMachine.h"
16 #include "llvm/Target/TargetMachineImpls.h"
17 #include "llvm/Support/PassNameParser.h"
18 #include "Support/Signals.h"
19 #include <fstream>
20 #include <memory>
21 #include <algorithm>
22
23 using std::cerr;
24 using std::string;
25
26
27 // The OptimizationList is automatically populated with registered Passes by the
28 // PassNameParser.
29 //
30 static cl::list<const PassInfo*, bool,
31                 FilteredPassNameParser<PassInfo::Optimization> >
32 OptimizationList(cl::desc("Optimizations available:"));
33
34
35 // Other command line options...
36 //
37 static cl::opt<string>
38 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
39
40 static cl::opt<string>
41 OutputFilename("o", cl::desc("Override output filename"),
42                cl::value_desc("filename"));
43
44 static cl::opt<bool>
45 Force("f", cl::desc("Overwrite output files"));
46
47 static cl::opt<bool>
48 PrintEachXForm("p", cl::desc("Print module after each transformation"));
49
50 static cl::opt<bool>
51 NoOutput("disable-output",
52          cl::desc("Do not write result bytecode file"), cl::Hidden);
53
54 static cl::opt<bool>
55 NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
56
57 static cl::opt<bool>
58 Quiet("q", cl::desc("Don't print 'program modified' message"));
59
60 static cl::alias
61 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
62
63
64 //===----------------------------------------------------------------------===//
65 // main for opt
66 //
67 int main(int argc, char **argv) {
68   cl::ParseCommandLineOptions(argc, argv,
69                               " llvm .bc -> .bc modular optimizer\n");
70
71   // Allocate a full target machine description only if necessary...
72   // FIXME: The choice of target should be controllable on the command line.
73   std::auto_ptr<TargetMachine> target;
74
75   TargetMachine* TM = NULL;
76   std::string ErrorMessage;
77
78   // Load the input module...
79   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
80   if (M.get() == 0) {
81     cerr << argv[0] << ": ";
82     if (ErrorMessage.size())
83       cerr << ErrorMessage << "\n";
84     else
85       cerr << "bytecode didn't read correctly.\n";
86     return 1;
87   }
88
89   // Figure out what stream we are supposed to write to...
90   std::ostream *Out = &std::cout;  // Default to printing to stdout...
91   if (OutputFilename != "") {
92     if (!Force && std::ifstream(OutputFilename.c_str())) {
93       // If force is not specified, make sure not to overwrite a file!
94       cerr << argv[0] << ": error opening '" << OutputFilename
95            << "': file exists!\n"
96            << "Use -f command line argument to force output\n";
97       return 1;
98     }
99     Out = new std::ofstream(OutputFilename.c_str());
100
101     if (!Out->good()) {
102       cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
103       return 1;
104     }
105
106     // Make sure that the Output file gets unlink'd from the disk if we get a
107     // SIGINT
108     RemoveFileOnSignal(OutputFilename);
109   }
110
111   // Create a PassManager to hold and optimize the collection of passes we are
112   // about to build...
113   //
114   PassManager Passes;
115
116   // Create a new optimization pass for each one specified on the command line
117   for (unsigned i = 0; i < OptimizationList.size(); ++i) {
118     const PassInfo *Opt = OptimizationList[i];
119     
120     if (Opt->getNormalCtor())
121       Passes.add(Opt->getNormalCtor()());
122     else if (Opt->getTargetCtor()) {
123 #if 0
124       if (target.get() == NULL)
125         target.reset(allocateSparcTargetMachine()); // FIXME: target option
126 #endif
127       assert(target.get() && "Could not allocate target machine!");
128       Passes.add(Opt->getTargetCtor()(*target.get()));
129     } else
130       cerr << argv[0] << ": cannot create pass: " << Opt->getPassName() << "\n";
131
132     if (PrintEachXForm)
133       Passes.add(new PrintModulePass(&cerr));
134   }
135
136   // Check that the module is well formed on completion of optimization
137   if (!NoVerify)
138     Passes.add(createVerifierPass());
139
140   // Write bytecode out to disk or cout as the last step...
141   if (!NoOutput)
142     Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
143
144   // Now that we have all of the passes ready, run them.
145   if (Passes.run(*M.get()) && !Quiet)
146     cerr << "Program modified.\n";
147
148   return 0;
149 }