1 //===-- llc.cpp - Implement the LLVM Compiler -----------------------------===//
3 // This is the llc compiler driver.
5 //===----------------------------------------------------------------------===//
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"
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",
29 cl::Flag TraceMethodValues("tracem", "Trace values only at method exits",
31 cl::Flag DebugTrace ("dumptrace",
32 "output trace code to a <fn>.trace.ll file",
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/
46 return outputFilename;
50 //===---------------------------------------------------------------------===//
51 // GenerateCodeForTarget Pass
53 // Native code generation for a specified target.
54 //===---------------------------------------------------------------------===//
56 class GenerateCodeForTarget : public ConcretePass<GenerateCodeForTarget> {
57 TargetMachine &Target;
59 inline GenerateCodeForTarget(TargetMachine &T) : Target(T) {}
61 // doPerMethodWork - This method does the actual work of generating code for
62 // the specified method.
64 bool doPerMethodWorkVirt(Method *M) {
65 if (!M->isExternal() && Target.compileMethod(M)) {
66 cerr << "Error compiling " << InputFilename << "!\n";
75 //===---------------------------------------------------------------------===//
78 // Write assembly code to specified output stream
79 //===---------------------------------------------------------------------===//
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?
88 inline EmitAssembly(const TargetMachine &T, ostream *O, bool D)
89 : Target(T), Out(O), DeleteStream(D) {}
91 virtual bool doPassInitializationVirt(Module *M) {
97 Target.emitAssembly(TheMod, *Out);
99 if (DeleteStream) delete Out;
104 //===---------------------------------------------------------------------===//
107 // Entry point for the llc compiler.
108 //===---------------------------------------------------------------------===//
111 main(int argc, char **argv)
113 // Parse command line options...
114 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
116 // Allocate a target... in the future this will be controllable on the
118 auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
119 assert(target.get() && "Could not allocate target machine!");
121 TargetMachine &Target = *target.get();
123 // Load the module to be compiled...
124 auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
126 cerr << "bytecode didn't read correctly.\n";
130 // Build up all of the passes that we want to do to the module...
131 vector<Pass*> Passes;
133 // Replace malloc and free instructions with library calls
134 Passes.push_back(new LowerAllocations(Target.DataLayout));
136 // Hoist constants out of PHI nodes into predecessor BB's
137 Passes.push_back(new HoistPHIConstants());
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));
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";
150 ostream *os = new ofstream(traceFileName.c_str(),
151 (Force ? 0 : ios::noreplace)|ios::out);
153 cerr << "Error opening " << traceFileName << "!\n";
158 Passes.push_back(new PrintModulePass("", os, true));
161 // If LLVM dumping after transformations is requested, add it to the pipeline
163 Passes.push_back(new PrintModulePass("Method after xformations: \n",&cerr));
165 // Generate Target code...
166 Passes.push_back(new GenerateCodeForTarget(Target));
168 if (!DoNotEmitAssembly) { // If asm output is enabled...
169 // Figure out where we are going to send the output...
171 if (OutputFilename != "") { // Specified an output filename?
172 Out = new ofstream(OutputFilename.c_str(),
173 (Force ? 0 : ios::noreplace)|ios::out);
175 if (InputFilename == "-") {
176 OutputFilename = "-";
179 string OutputFilename = GetFileNameRoot(InputFilename);
180 OutputFilename += ".s";
181 Out = new ofstream(OutputFilename.c_str(),
182 (Force ? 0 : ios::noreplace)|ios::out);
184 cerr << "Error opening " << OutputFilename << "!\n";
191 // Output assembly language to the .s file
192 Passes.push_back(new EmitAssembly(Target, Out, Out != &cout));
195 // Run our queue of passes all at once now, efficiently.
196 return Pass::runAllPassesAndFree(M.get(), Passes);