515c903fbe02c787725a8d2982b2984bd2cbc9bf
[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/ChangeAllocations.h"
12 #include "llvm/Transforms/Scalar.h"
13 #include "llvm/Assembly/PrintModulePass.h"
14 #include "llvm/Bytecode/WriteBytecodePass.h"
15 #include "llvm/Transforms/ConstantMerge.h"
16 #include "llvm/Module.h"
17 #include "llvm/Function.h"
18 #include "llvm/PassManager.h"
19 #include "Support/CommandLine.h"
20 #include "Support/Signals.h"
21 #include <memory>
22 #include <fstream>
23 using std::string;
24
25 static cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
26 static cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
27 static cl::Flag   Force         ("f", "Overwrite output files");
28 static cl::Flag   DumpAsm       ("d", "Print bytecode before native code generation", cl::Hidden);
29
30 enum TraceLevel {
31   TraceOff, TraceFunctions, TraceBasicBlocks
32 };
33
34 static cl::Enum<enum TraceLevel> TraceValues("trace", cl::NoFlags,
35   "Trace values through functions or basic blocks",
36   clEnumValN(TraceOff        , "off",        "Disable trace code"),
37   clEnumValN(TraceFunctions  , "function",   "Trace each function"),
38   clEnumValN(TraceBasicBlocks, "basicblock", "Trace each basic block"), 0);
39
40
41 // GetFileNameRoot - Helper function to get the basename of a filename...
42 static inline string GetFileNameRoot(const string &InputFilename) {
43   string IFN = InputFilename;
44   string outputFilename;
45   int Len = IFN.length();
46   if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
47     outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
48   } else {
49     outputFilename = IFN;
50   }
51   return outputFilename;
52 }
53
54
55 //===---------------------------------------------------------------------===//
56 // Function main()
57 // 
58 // Entry point for the llc compiler.
59 //===---------------------------------------------------------------------===//
60
61 int main(int argc, char **argv) {
62   cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
63   
64   // Allocate a target... in the future this will be controllable on the
65   // command line.
66   std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
67   assert(target.get() && "Could not allocate target machine!");
68
69   TargetMachine &Target = *target.get();
70   
71   // Load the module to be compiled...
72   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
73   if (M.get() == 0) {
74     cerr << "bytecode didn't read correctly.\n";
75     return 1;
76   }
77
78   // Build up all of the passes that we want to do to the module...
79   PassManager Passes;
80
81   if (TraceValues != TraceOff) {   // If tracing enabled...
82     // Insert trace code in all functions in the module
83     if (TraceValues == TraceBasicBlocks)
84       Passes.add(createTraceValuesPassForBasicBlocks());
85     else if (TraceValues == TraceFunctions)
86       Passes.add(createTraceValuesPassForFunction());
87     else
88       assert(0 && "Bad value for TraceValues!");
89
90     // Eliminate duplication in constant pool
91     Passes.add(createDynamicConstantMergePass());
92   }
93   
94   // Decompose multi-dimensional refs into a sequence of 1D refs
95   Passes.add(createDecomposeMultiDimRefsPass());
96   
97   // Write out the module with tracing code just before code generation
98   if (TraceValues != TraceOff) {   // If tracing enabled...
99     assert(InputFilename != "-" &&
100            "files on stdin not supported with tracing");
101     string traceFileName = GetFileNameRoot(InputFilename) + ".trace.bc";
102
103     if (!Force && std::ifstream(OutputFilename.c_str())) {
104       // If force is not specified, make sure not to overwrite a file!
105       cerr << "Error opening '" << OutputFilename << "': File exists!\n"
106            << "Use -f command line argument to force output\n";
107       return 1;
108     }
109     
110     std::ostream *os = new std::ofstream(traceFileName.c_str());
111     if (!os->good()) {
112       cerr << "Error opening " << traceFileName
113            << "! SKIPPING OUTPUT OF TRACE CODE\n";
114       delete os;
115       return 1;
116     }
117     
118     Passes.add(new WriteBytecodePass(os, true));
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   Passes.add(createLowerAllocationsPass(Target.DataLayout));
125   
126   // If LLVM dumping after transformations is requested, add it to the pipeline
127   if (DumpAsm)
128     Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
129
130   // Figure out where we are going to send the output...
131   std::ostream *Out = 0;
132   if (OutputFilename != "") {   // Specified an output filename?
133     if (!Force && std::ifstream(OutputFilename.c_str())) {
134       // If force is not specified, make sure not to overwrite a file!
135       cerr << "Error opening '" << OutputFilename << "': File exists!\n"
136            << "Use -f command line argument to force output\n";
137       return 1;
138     }
139     Out = new std::ofstream(OutputFilename.c_str());
140
141     // Make sure that the Out file gets unlink'd from the disk if we get a
142     // SIGINT
143     RemoveFileOnSignal(OutputFilename);
144   } else {
145     if (InputFilename == "-") {
146       OutputFilename = "-";
147       Out = &std::cout;
148     } else {
149       string OutputFilename = GetFileNameRoot(InputFilename); 
150       OutputFilename += ".s";
151
152       if (!Force && std::ifstream(OutputFilename.c_str())) {
153         // If force is not specified, make sure not to overwrite a file!
154         cerr << "Error opening '" << OutputFilename << "': File exists!\n"
155              << "Use -f command line argument to force output\n";
156         return 1;
157       }
158
159       Out = new std::ofstream(OutputFilename.c_str());
160       if (!Out->good()) {
161         cerr << "Error opening " << OutputFilename << "!\n";
162         delete Out;
163         return 1;
164       }
165       // Make sure that the Out file gets unlink'd from the disk if we get a
166       // SIGINT
167       RemoveFileOnSignal(OutputFilename);
168     }
169   }
170   
171   Target.addPassesToEmitAssembly(Passes, *Out);
172   
173   // Run our queue of passes all at once now, efficiently.
174   Passes.run(M.get());
175
176   if (Out != &std::cout) delete Out;
177
178   return 0;
179 }