Removed the -q option and the default message written to stderr. The
[oota-llvm.git] / tools / opt / opt.cpp
1 //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // Optimizations may be specified an arbitrary number of times on the command
11 // line, they are run in the order specified.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Module.h"
16 #include "llvm/PassManager.h"
17 #include "llvm/Bytecode/Reader.h"
18 #include "llvm/Bytecode/WriteBytecodePass.h"
19 #include "llvm/Assembly/PrintModulePass.h"
20 #include "llvm/Analysis/Verifier.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Target/TargetMachineImpls.h"
23 #include "llvm/Support/PassNameParser.h"
24 #include "llvm/System/Signals.h"
25 #include "Support/SystemUtils.h"
26 #include <fstream>
27 #include <memory>
28 #include <algorithm>
29
30 using namespace llvm;
31
32 // The OptimizationList is automatically populated with registered Passes by the
33 // PassNameParser.
34 //
35 static cl::list<const PassInfo*, bool,
36                 FilteredPassNameParser<PassInfo::Optimization> >
37 OptimizationList(cl::desc("Optimizations available:"));
38
39
40 // Other command line options...
41 //
42 static cl::opt<std::string>
43 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
44
45 static cl::opt<std::string>
46 OutputFilename("o", cl::desc("Override output filename"),
47                cl::value_desc("filename"), cl::init("-"));
48
49 static cl::opt<bool>
50 Force("f", cl::desc("Overwrite output files"));
51
52 static cl::opt<bool>
53 PrintEachXForm("p", cl::desc("Print module after each transformation"));
54
55 static cl::opt<bool>
56 NoOutput("disable-output",
57          cl::desc("Do not write result bytecode file"), cl::Hidden);
58
59 static cl::opt<bool>
60 NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
61
62 static cl::opt<bool>
63 Quiet("q", cl::desc("Don't print 'program modified' message"));
64
65
66 //===----------------------------------------------------------------------===//
67 // main for opt
68 //
69 int main(int argc, char **argv) {
70   cl::ParseCommandLineOptions(argc, argv,
71                               " llvm .bc -> .bc modular optimizer\n");
72   PrintStackTraceOnErrorSignal();
73
74   // Allocate a full target machine description only if necessary...
75   // FIXME: The choice of target should be controllable on the command line.
76   std::auto_ptr<TargetMachine> target;
77
78   TargetMachine* TM = NULL;
79   std::string ErrorMessage;
80
81   // Load the input module...
82   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
83   if (M.get() == 0) {
84     std::cerr << argv[0] << ": ";
85     if (ErrorMessage.size())
86       std::cerr << ErrorMessage << "\n";
87     else
88       std::cerr << "bytecode didn't read correctly.\n";
89     return 1;
90   }
91
92   // Figure out what stream we are supposed to write to...
93   std::ostream *Out = &std::cout;  // Default to printing to stdout...
94   if (OutputFilename != "-") {
95     if (!Force && std::ifstream(OutputFilename.c_str())) {
96       // If force is not specified, make sure not to overwrite a file!
97       std::cerr << argv[0] << ": error opening '" << OutputFilename
98                 << "': file exists!\n"
99                 << "Use -f command line argument to force output\n";
100       return 1;
101     }
102     Out = new std::ofstream(OutputFilename.c_str());
103
104     if (!Out->good()) {
105       std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
106       return 1;
107     }
108
109     // Make sure that the Output file gets unlinked from the disk if we get a
110     // SIGINT
111     RemoveFileOnSignal(OutputFilename);
112   }
113
114   // If the output is set to be emitted to standard out, and standard out is a
115   // console, print out a warning message and refuse to do it.  We don't impress
116   // anyone by spewing tons of binary goo to a terminal.
117   if (Out == &std::cout && isStandardOutAConsole() && !Force && !NoOutput) {
118     std::cerr << "WARNING: It looks like you're attempting to print out a "
119               << "bytecode file.  I'm\ngoing to pretend you didn't ask me to do"
120               << " this (for your own good).  If you\nREALLY want to taste LLVM"
121               << " bytecode first hand, you can force output with the\n'-f'"
122               << " option.\n\n";
123     NoOutput = true;
124   }
125
126   // Create a PassManager to hold and optimize the collection of passes we are
127   // about to build...
128   //
129   PassManager Passes;
130
131   // Add an appropriate TargetData instance for this module...
132   Passes.add(new TargetData("opt", M.get()));
133
134   // Create a new optimization pass for each one specified on the command line
135   for (unsigned i = 0; i < OptimizationList.size(); ++i) {
136     const PassInfo *Opt = OptimizationList[i];
137     
138     if (Opt->getNormalCtor())
139       Passes.add(Opt->getNormalCtor()());
140     else if (Opt->getTargetCtor()) {
141 #if 0
142       if (target.get() == NULL)
143         target.reset(allocateSparcTargetMachine()); // FIXME: target option
144 #endif
145       assert(target.get() && "Could not allocate target machine!");
146       Passes.add(Opt->getTargetCtor()(*target.get()));
147     } else
148       std::cerr << argv[0] << ": cannot create pass: " << Opt->getPassName()
149                 << "\n";
150
151     if (PrintEachXForm)
152       Passes.add(new PrintModulePass(&std::cerr));
153   }
154
155   // Check that the module is well formed on completion of optimization
156   if (!NoVerify)
157     Passes.add(createVerifierPass());
158
159   // Write bytecode out to disk or cout as the last step...
160   if (!NoOutput)
161     Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
162
163   // Now that we have all of the passes ready, run them.
164   if (Passes.run(*M.get()))
165     return 0;
166
167   return 1;
168 }