Remove duplicate pass
[oota-llvm.git] / tools / llc / llc.cpp
1 //===-- llc.cpp - Implement the LLVM Compiler -----------------------------===//
2 //
3 // This is the llc compiler driver.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Bytecode/Reader.h"
8 #include "llvm/Target/TargetMachineImpls.h"
9 #include "llvm/Target/TargetMachine.h"
10 #include "llvm/Transforms/Scalar.h"
11 #include "llvm/Assembly/PrintModulePass.h"
12 #include "llvm/Module.h"
13 #include "llvm/PassManager.h"
14 #include "llvm/Pass.h"
15 #include "llvm/Support/PassNameParser.h"
16 #include "Support/CommandLine.h"
17 #include "Support/Signals.h"
18 #include <memory>
19 #include <fstream>
20
21 //------------------------------------------------------------------------------
22 // Option declarations for LLC.
23 //------------------------------------------------------------------------------
24
25 // Make all registered optimization passes available to llc.  These passes
26 // will all be run before the simplification and lowering steps used by the
27 // back-end code generator, and will be run in the order specified on the
28 // command line. The OptimizationList is automatically populated with
29 // registered Passes by the PassNameParser.
30 //
31 static cl::list<const PassInfo*, bool,
32                 FilteredPassNameParser<PassInfo::Optimization> >
33 OptimizationList(cl::desc("Optimizations available:"));
34
35
36 // General options for llc.  Other pass-specific options are specified
37 // within the corresponding llc passes, and target-specific options
38 // and back-end code generation options are specified with the target machine.
39 // 
40 static cl::opt<std::string>
41 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
42
43 static cl::opt<std::string>
44 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
45
46 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
47
48 static cl::opt<bool>
49 DisableStrip("disable-strip",
50           cl::desc("Do not strip the LLVM bytecode included in the executable"));
51
52 static cl::opt<bool>
53 DumpAsm("d", cl::desc("Print bytecode before native code generation"),
54         cl::Hidden);
55
56 // GetFileNameRoot - Helper function to get the basename of a filename...
57 static inline std::string
58 GetFileNameRoot(const std::string &InputFilename)
59 {
60   std::string IFN = InputFilename;
61   std::string outputFilename;
62   int Len = IFN.length();
63   if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
64     outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
65   } else {
66     outputFilename = IFN;
67   }
68   return outputFilename;
69 }
70
71
72 //===---------------------------------------------------------------------===//
73 // Function main()
74 // 
75 // Entry point for the llc compiler.
76 //===---------------------------------------------------------------------===//
77
78 int
79 main(int argc, char **argv)
80 {
81   cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
82   
83   // Allocate a target... in the future this will be controllable on the
84   // command line.
85   std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
86   assert(target.get() && "Could not allocate target machine!");
87
88   TargetMachine &Target = *target.get();
89   const TargetData &TD = Target.getTargetData();
90
91   // Load the module to be compiled...
92   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
93   if (M.get() == 0)
94     {
95       std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
96       return 1;
97     }
98
99   // Build up all of the passes that we want to do to the module...
100   PassManager Passes;
101
102   Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
103                             TD.getPointerAlignment(), TD.getDoubleAlignment()));
104
105   // Create a new optimization pass for each one specified on the command line
106   // Deal specially with tracing passes, which must be run differently than opt.
107   // 
108   for (unsigned i = 0; i < OptimizationList.size(); ++i) {
109     const PassInfo *Opt = OptimizationList[i];
110     
111     // handle other passes as normal optimization passes
112     if (Opt->getNormalCtor())
113       Passes.add(Opt->getNormalCtor()());
114     else if (Opt->getTargetCtor())
115       Passes.add(Opt->getTargetCtor()(Target));
116     else
117       std::cerr << argv[0] << ": cannot create pass: "
118                 << Opt->getPassName() << "\n";
119   }
120
121   // Replace malloc and free instructions with library calls.
122   // Do this after tracing until lli implements these lib calls.
123   // For now, it will emulate malloc and free internally.
124   // FIXME: This is sparc specific!
125   Passes.add(createLowerAllocationsPass());
126
127   // If LLVM dumping after transformations is requested, add it to the pipeline
128   if (DumpAsm)
129     Passes.add(new PrintFunctionPass("Code after xformations: \n", &std::cerr));
130
131   // Strip all of the symbols from the bytecode so that it will be smaller...
132   if (!DisableStrip)
133     Passes.add(createSymbolStrippingPass());
134
135   // Figure out where we are going to send the output...
136   std::ostream *Out = 0;
137   if (OutputFilename != "")
138     {   // Specified an output filename?
139       if (!Force && std::ifstream(OutputFilename.c_str())) {
140         // If force is not specified, make sure not to overwrite a file!
141         std::cerr << argv[0] << ": error opening '" << OutputFilename
142                   << "': file exists!\n"
143                   << "Use -f command line argument to force output\n";
144         return 1;
145       }
146       Out = new std::ofstream(OutputFilename.c_str());
147
148       // Make sure that the Out file gets unlink'd from the disk if we get a
149       // SIGINT
150       RemoveFileOnSignal(OutputFilename);
151     }
152   else
153     {
154       if (InputFilename == "-")
155         {
156           OutputFilename = "-";
157           Out = &std::cout;
158         }
159       else
160         {
161           std::string OutputFilename = GetFileNameRoot(InputFilename); 
162           OutputFilename += ".s";
163
164           if (!Force && std::ifstream(OutputFilename.c_str()))
165             {
166               // If force is not specified, make sure not to overwrite a file!
167               std::cerr << argv[0] << ": error opening '" << OutputFilename
168                         << "': file exists!\n"
169                         << "Use -f command line argument to force output\n";
170               return 1;
171             }
172
173           Out = new std::ofstream(OutputFilename.c_str());
174           if (!Out->good())
175             {
176               std::cerr << argv[0] << ": error opening " << OutputFilename
177                         << "!\n";
178               delete Out;
179               return 1;
180             }
181
182           // Make sure that the Out file gets unlink'd from the disk if we get a
183           // SIGINT
184           RemoveFileOnSignal(OutputFilename);
185         }
186     }
187
188   // Ask the target to add backend passes as neccesary
189   if (Target.addPassesToEmitAssembly(Passes, *Out)) {
190     std::cerr << argv[0] << ": target '" << Target.getName()
191               << " does not support static compilation!\n";
192   } else {
193     // Run our queue of passes all at once now, efficiently.
194     Passes.run(*M.get());
195   }
196
197   // Delete the ostream if it's not a stdout stream
198   if (Out != &std::cout) delete Out;
199
200   return 0;
201 }