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/Scalar.h"
12 #include "llvm/Transforms/Utils/Linker.h"
13 #include "llvm/Assembly/PrintModulePass.h"
14 #include "llvm/Bytecode/WriteBytecodePass.h"
15 #include "llvm/Transforms/IPO.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"
26 static cl::opt<string>
27 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
29 static cl::opt<string>
30 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
32 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
35 DumpAsm("d", cl::desc("Print bytecode before native code generation"),
38 static cl::opt<string>
39 TraceLibPath("tracelibpath", cl::desc("Path to libinstr for trace code"),
40 cl::value_desc("directory"), cl::Hidden);
43 TraceOff, TraceFunctions, TraceBasicBlocks
46 static cl::opt<TraceLevel>
47 TraceValues("trace", cl::desc("Trace values through functions or basic blocks"),
49 clEnumValN(TraceOff , "off", "Disable trace code"),
50 clEnumValN(TraceFunctions , "function", "Trace each function"),
51 clEnumValN(TraceBasicBlocks, "basicblock", "Trace each basic block"),
54 // GetFileNameRoot - Helper function to get the basename of a filename...
55 static inline string GetFileNameRoot(const string &InputFilename) {
56 string IFN = InputFilename;
57 string outputFilename;
58 int Len = IFN.length();
59 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
60 outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
64 return outputFilename;
67 static void insertTraceCodeFor(Module &M) {
70 // Insert trace code in all functions in the module
71 switch (TraceValues) {
72 case TraceBasicBlocks:
73 Passes.add(createTraceValuesPassForBasicBlocks());
76 Passes.add(createTraceValuesPassForFunction());
79 assert(0 && "Bad value for TraceValues!");
83 // Eliminate duplication in constant pool
84 Passes.add(createConstantMergePass());
86 // Run passes to insert and clean up trace code...
89 std::string ErrorMessage;
91 // Load the module that contains the runtime helper routines neccesary for
92 // pointer hashing and stuff... link this module into the program if possible
94 Module *TraceModule = ParseBytecodeFile(TraceLibPath+"libinstr.bc");
96 // Ok, the TraceLibPath didn't contain a valid module. Try to load the module
97 // from the current LLVM-GCC install directory. This is kindof a hack, but
98 // allows people to not HAVE to have built the library.
100 if (TraceModule == 0)
101 TraceModule = ParseBytecodeFile("/home/vadve/lattner/cvs/gcc_install/lib/"
102 "gcc-lib/llvm/3.1/libinstr.bc");
104 // If we still didn't get it, cancel trying to link it in...
105 if (TraceModule == 0) {
106 cerr << "Warning, could not load trace routines to link into program!\n";
109 // Link in the trace routines... if the link fails, don't panic, because the
110 // compile should still succeed, just the native linker will probably fail.
112 std::auto_ptr<Module> TraceRoutines(TraceModule);
113 if (LinkModules(&M, TraceRoutines.get(), &ErrorMessage))
114 cerr << "Warning: Error linking in trace routines: "
115 << ErrorMessage << "\n";
119 // Write out the module with tracing code just before code generation
120 if (InputFilename != "-") {
121 string TraceFilename = GetFileNameRoot(InputFilename) + ".trace.bc";
123 std::ofstream Out(TraceFilename.c_str());
125 cerr << "Error opening '" << TraceFilename
126 << "'!: Skipping output of trace code as bytecode\n";
128 cerr << "Emitting trace code to '" << TraceFilename
129 << "' for comparison...\n";
130 WriteBytecodeToFile(&M, Out);
137 //===---------------------------------------------------------------------===//
140 // Entry point for the llc compiler.
141 //===---------------------------------------------------------------------===//
143 int main(int argc, char **argv) {
144 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
146 // Allocate a target... in the future this will be controllable on the
148 std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
149 assert(target.get() && "Could not allocate target machine!");
151 TargetMachine &Target = *target.get();
153 // Load the module to be compiled...
154 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
156 cerr << "bytecode didn't read correctly.\n";
160 if (TraceValues != TraceOff) // If tracing enabled...
161 insertTraceCodeFor(*M.get()); // Hack up module before using passmanager...
163 // Build up all of the passes that we want to do to the module...
166 // Decompose multi-dimensional refs into a sequence of 1D refs
167 Passes.add(createDecomposeMultiDimRefsPass());
169 // Replace malloc and free instructions with library calls.
170 // Do this after tracing until lli implements these lib calls.
171 // For now, it will emulate malloc and free internally.
172 Passes.add(createLowerAllocationsPass(Target.DataLayout));
174 // If LLVM dumping after transformations is requested, add it to the pipeline
176 Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
178 // Strip all of the symbols from the bytecode so that it will be smaller...
179 Passes.add(createSymbolStrippingPass());
181 // Figure out where we are going to send the output...
182 std::ostream *Out = 0;
183 if (OutputFilename != "") { // Specified an output filename?
184 if (!Force && std::ifstream(OutputFilename.c_str())) {
185 // If force is not specified, make sure not to overwrite a file!
186 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
187 << "Use -f command line argument to force output\n";
190 Out = new std::ofstream(OutputFilename.c_str());
192 // Make sure that the Out file gets unlink'd from the disk if we get a
194 RemoveFileOnSignal(OutputFilename);
196 if (InputFilename == "-") {
197 OutputFilename = "-";
200 string OutputFilename = GetFileNameRoot(InputFilename);
201 OutputFilename += ".s";
203 if (!Force && std::ifstream(OutputFilename.c_str())) {
204 // If force is not specified, make sure not to overwrite a file!
205 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
206 << "Use -f command line argument to force output\n";
210 Out = new std::ofstream(OutputFilename.c_str());
212 cerr << "Error opening " << OutputFilename << "!\n";
216 // Make sure that the Out file gets unlink'd from the disk if we get a
218 RemoveFileOnSignal(OutputFilename);
222 Target.addPassesToEmitAssembly(Passes, *Out);
224 // Run our queue of passes all at once now, efficiently.
225 Passes.run(*M.get());
227 if (Out != &std::cout) delete Out;