Minor cleanups
[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/Sparc.h"
9 #include "llvm/Target/TargetMachine.h"
10 #include "llvm/Transforms/Instrumentation/TraceValues.h"
11 #include "llvm/Transforms/LowerAllocations.h"
12 #include "llvm/Transforms/HoistPHIConstants.h"
13 #include "llvm/Transforms/PrintModulePass.h"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Module.h"
16 #include "llvm/Method.h"
17 #include <memory>
18 #include <string>
19 #include <fstream>
20
21 cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
22 cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
23 cl::Flag   Force         ("f", "Overwrite output files");
24 cl::Flag   DumpAsm       ("d", "Print bytecode before native code generation",
25                           cl::Hidden);
26 cl::Flag   DoNotEmitAssembly("noasm", "Do not emit assembly code", cl::Hidden);
27 cl::Flag   TraceBBValues ("trace",
28                           "Trace values at basic block and method exits");
29 cl::Flag   TraceMethodValues("tracem", "Trace values only at method exits");
30 cl::Flag   DebugTrace    ("dumptrace",
31                           "output trace code to a <fn>.trace.ll file",
32                           cl::Hidden);
33
34
35 // GetFileNameRoot - Helper function to get the basename of a filename...
36 static inline string GetFileNameRoot(const string &InputFilename) {
37   string IFN = InputFilename;
38   string outputFilename;
39   int Len = IFN.length();
40   if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
41     outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
42   } else {
43     outputFilename = IFN;
44   }
45   return outputFilename;
46 }
47
48
49 //===---------------------------------------------------------------------===//
50 // GenerateCodeForTarget Pass
51 // 
52 // Native code generation for a specified target.
53 //===---------------------------------------------------------------------===//
54
55 class GenerateCodeForTarget : public ConcretePass<GenerateCodeForTarget> {
56   TargetMachine &Target;
57 public:
58   inline GenerateCodeForTarget(TargetMachine &T) : Target(T) {}
59
60   // doPerMethodWork - This method does the actual work of generating code for
61   // the specified method.
62   //
63   bool doPerMethodWorkVirt(Method *M) {
64     if (!M->isExternal() && Target.compileMethod(M)) {
65       cerr << "Error compiling " << InputFilename << "!\n";
66       return true;
67     }
68     
69     return false;
70   }
71 };
72
73
74 //===---------------------------------------------------------------------===//
75 // EmitAssembly Pass
76 // 
77 // Write assembly code to specified output stream
78 //===---------------------------------------------------------------------===//
79
80 class EmitAssembly : public ConcretePass<EmitAssembly> {
81   const TargetMachine &Target;   // Target to compile for
82   ostream *Out;                  // Stream to print on
83   bool DeleteStream;             // Delete stream in dtor?
84
85   Module *TheMod;
86 public:
87   inline EmitAssembly(const TargetMachine &T, ostream *O, bool D)
88     : Target(T), Out(O), DeleteStream(D) {}
89
90   virtual bool doPassInitializationVirt(Module *M) {
91     TheMod = M;
92     return false;
93   }
94
95   ~EmitAssembly() {
96     // TODO: This should be performed as a moduleCleanup function, but we don't
97     // have one yet!
98     Target.emitAssembly(TheMod, *Out);
99
100     if (DeleteStream) delete Out;
101   }
102 };
103
104
105 //===---------------------------------------------------------------------===//
106 // Function main()
107 // 
108 // Entry point for the llc compiler.
109 //===---------------------------------------------------------------------===//
110
111 int main(int argc, char **argv) {
112   cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
113   
114   // Allocate a target... in the future this will be controllable on the
115   // command line.
116   auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
117   assert(target.get() && "Could not allocate target machine!");
118
119   TargetMachine &Target = *target.get();
120   
121   // Load the module to be compiled...
122   auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
123   if (M.get() == 0) {
124     cerr << "bytecode didn't read correctly.\n";
125     return 1;
126   }
127
128   // Build up all of the passes that we want to do to the module...
129   vector<Pass*> Passes;
130
131   // Replace malloc and free instructions with library calls
132   Passes.push_back(new LowerAllocations(Target.DataLayout));
133
134   // Hoist constants out of PHI nodes into predecessor BB's
135   Passes.push_back(new HoistPHIConstants());
136
137   if (TraceBBValues || TraceMethodValues)    // If tracing enabled...
138     // Insert trace code in all methods in the module
139     Passes.push_back(new InsertTraceCode(TraceBBValues, 
140                                          TraceBBValues || TraceMethodValues));
141
142
143   if (DebugTrace) {                          // If Trace Debugging is enabled...
144     // Then write the module with tracing code out in assembly form
145     assert(InputFilename != "-" && "files on stdin not supported with tracing");
146     string traceFileName = GetFileNameRoot(InputFilename) + ".trace.ll";
147
148     ostream *os = new ofstream(traceFileName.c_str(), 
149                                (Force ? 0 : ios::noreplace)|ios::out);
150     if (!os->good()) {
151       cerr << "Error opening " << traceFileName << "!\n";
152       delete os;
153       return 1;
154     }
155
156     Passes.push_back(new PrintModulePass("", os, true));
157   }
158
159   // If LLVM dumping after transformations is requested, add it to the pipeline
160   if (DumpAsm)
161     Passes.push_back(new PrintModulePass("Method after xformations: \n",&cerr));
162
163   // Generate Target code...
164   Passes.push_back(new GenerateCodeForTarget(Target));
165
166   if (!DoNotEmitAssembly) {                // If asm output is enabled...
167     // Figure out where we are going to send the output...
168     ostream *Out = 0;
169     if (OutputFilename != "") {   // Specified an output filename?
170       Out = new ofstream(OutputFilename.c_str(), 
171                          (Force ? 0 : ios::noreplace)|ios::out);
172     } else {
173       if (InputFilename == "-") {
174         OutputFilename = "-";
175         Out = &cout;
176       } else {
177         string OutputFilename = GetFileNameRoot(InputFilename); 
178         OutputFilename += ".s";
179         Out = new ofstream(OutputFilename.c_str(), 
180                            (Force ? 0 : ios::noreplace)|ios::out);
181         if (!Out->good()) {
182           cerr << "Error opening " << OutputFilename << "!\n";
183           delete Out;
184           return 1;
185         }
186       }
187     }
188     
189     // Output assembly language to the .s file
190     Passes.push_back(new EmitAssembly(Target, Out, Out != &cout));
191   }
192   
193   // Run our queue of passes all at once now, efficiently.
194   return Pass::runAllPassesAndFree(M.get(), Passes);
195 }
196
197