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