391fe279ce2d8d75bd79fd3d172ebcafb9c5db40
[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/Transforms/Utils/Linker.h"
14 #include "llvm/Assembly/PrintModulePass.h"
15 #include "llvm/Bytecode/WriteBytecodePass.h"
16 #include "llvm/Transforms/ConstantMerge.h"
17 #include "llvm/Module.h"
18 #include "llvm/Function.h"
19 #include "llvm/PassManager.h"
20 #include "Support/CommandLine.h"
21 #include "Support/Signals.h"
22 #include <memory>
23 #include <fstream>
24 using std::string;
25
26 static cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
27 static cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
28 static cl::Flag   Force         ("f", "Overwrite output files");
29 static cl::Flag   DumpAsm       ("d", "Print bytecode before native code generation", cl::Hidden);
30 static cl::String TraceLibPath  ("tracelibpath", "Path to libinstr for trace code", cl::Hidden, "");
31
32 enum TraceLevel {
33   TraceOff, TraceFunctions, TraceBasicBlocks
34 };
35
36 static cl::Enum<enum TraceLevel> TraceValues("trace", cl::NoFlags,
37   "Trace values through functions or basic blocks",
38   clEnumValN(TraceOff        , "off",        "Disable trace code"),
39   clEnumValN(TraceFunctions  , "function",   "Trace each function"),
40   clEnumValN(TraceBasicBlocks, "basicblock", "Trace each basic block"), 0);
41
42
43 // GetFileNameRoot - Helper function to get the basename of a filename...
44 static inline string GetFileNameRoot(const string &InputFilename) {
45   string IFN = InputFilename;
46   string outputFilename;
47   int Len = IFN.length();
48   if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
49     outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
50   } else {
51     outputFilename = IFN;
52   }
53   return outputFilename;
54 }
55
56 static void insertTraceCodeFor(Module *M) {
57   PassManager Passes;
58
59   // Insert trace code in all functions in the module
60   switch (TraceValues) {
61   case TraceBasicBlocks:
62     Passes.add(createTraceValuesPassForBasicBlocks());
63     break;
64   case TraceFunctions:
65     Passes.add(createTraceValuesPassForFunction());
66     break;
67   default:
68     assert(0 && "Bad value for TraceValues!");
69     abort();
70   }
71   
72   // Eliminate duplication in constant pool
73   Passes.add(createDynamicConstantMergePass());
74
75   // Run passes to insert and clean up trace code...
76   Passes.run(M);
77
78   std::string ErrorMessage;
79
80   // Load the module that contains the runtime helper routines neccesary for
81   // pointer hashing and stuff...  link this module into the program if possible
82   //
83   Module *TraceModule = ParseBytecodeFile(TraceLibPath+"libinstr.bc");
84
85   // Ok, the TraceLibPath didn't contain a valid module.  Try to load the module
86   // from the current LLVM-GCC install directory.  This is kindof a hack, but
87   // allows people to not HAVE to have built the library.
88   //
89   if (TraceModule == 0)
90     TraceModule = ParseBytecodeFile("/home/vadve/lattner/cvs/gcc_install/lib/"
91                                     "gcc-lib/llvm/3.1/libinstr.bc");
92
93   // If we still didn't get it, cancel trying to link it in...
94   if (TraceModule == 0) {
95     cerr << "Warning, could not load trace routines to link into program!\n";
96   } else {
97
98     // Link in the trace routines... if the link fails, don't panic, because the
99     // compile should still succeed, just the native linker will probably fail.
100     //
101     std::auto_ptr<Module> TraceRoutines(TraceModule);
102     if (LinkModules(M, TraceRoutines.get(), &ErrorMessage))
103       cerr << "Warning: Error linking in trace routines: "
104            << ErrorMessage << "\n";
105   }
106
107
108   // Write out the module with tracing code just before code generation
109   if (InputFilename != "-") {
110     string TraceFilename = GetFileNameRoot(InputFilename) + ".trace.bc";
111
112     std::ofstream Out(TraceFilename.c_str());
113     if (!Out.good()) {
114       cerr << "Error opening '" << TraceFilename
115            << "'!: Skipping output of trace code as bytecode\n";
116     } else {
117       cerr << "Emitting trace code to '" << TraceFilename
118            << "' for comparison...\n";
119       WriteBytecodeToFile(M, Out);
120     }
121   }
122
123 }
124   
125
126 //===---------------------------------------------------------------------===//
127 // Function main()
128 // 
129 // Entry point for the llc compiler.
130 //===---------------------------------------------------------------------===//
131
132 int main(int argc, char **argv) {
133   cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
134   
135   // Allocate a target... in the future this will be controllable on the
136   // command line.
137   std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
138   assert(target.get() && "Could not allocate target machine!");
139
140   TargetMachine &Target = *target.get();
141   
142   // Load the module to be compiled...
143   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
144   if (M.get() == 0) {
145     cerr << "bytecode didn't read correctly.\n";
146     return 1;
147   }
148
149   if (TraceValues != TraceOff)    // If tracing enabled...
150     insertTraceCodeFor(M.get());  // Hack up module before using passmanager...
151
152   // Build up all of the passes that we want to do to the module...
153   PassManager Passes;
154
155   // Decompose multi-dimensional refs into a sequence of 1D refs
156   Passes.add(createDecomposeMultiDimRefsPass());
157   
158   // Replace malloc and free instructions with library calls.
159   // Do this after tracing until lli implements these lib calls.
160   // For now, it will emulate malloc and free internally.
161   Passes.add(createLowerAllocationsPass(Target.DataLayout));
162   
163   // If LLVM dumping after transformations is requested, add it to the pipeline
164   if (DumpAsm)
165     Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
166
167   // Figure out where we are going to send the output...
168   std::ostream *Out = 0;
169   if (OutputFilename != "") {   // Specified an output filename?
170     if (!Force && std::ifstream(OutputFilename.c_str())) {
171       // If force is not specified, make sure not to overwrite a file!
172       cerr << "Error opening '" << OutputFilename << "': File exists!\n"
173            << "Use -f command line argument to force output\n";
174       return 1;
175     }
176     Out = new std::ofstream(OutputFilename.c_str());
177
178     // Make sure that the Out file gets unlink'd from the disk if we get a
179     // SIGINT
180     RemoveFileOnSignal(OutputFilename);
181   } else {
182     if (InputFilename == "-") {
183       OutputFilename = "-";
184       Out = &std::cout;
185     } else {
186       string OutputFilename = GetFileNameRoot(InputFilename); 
187       OutputFilename += ".s";
188
189       if (!Force && std::ifstream(OutputFilename.c_str())) {
190         // If force is not specified, make sure not to overwrite a file!
191         cerr << "Error opening '" << OutputFilename << "': File exists!\n"
192              << "Use -f command line argument to force output\n";
193         return 1;
194       }
195
196       Out = new std::ofstream(OutputFilename.c_str());
197       if (!Out->good()) {
198         cerr << "Error opening " << OutputFilename << "!\n";
199         delete Out;
200         return 1;
201       }
202       // Make sure that the Out file gets unlink'd from the disk if we get a
203       // SIGINT
204       RemoveFileOnSignal(OutputFilename);
205     }
206   }
207   
208   Target.addPassesToEmitAssembly(Passes, *Out);
209   
210   // Run our queue of passes all at once now, efficiently.
211   Passes.run(M.get());
212
213   if (Out != &std::cout) delete Out;
214
215   return 0;
216 }