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/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"
27 static cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
28 static cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
29 static cl::Flag Force ("f", "Overwrite output files");
30 static cl::Flag DumpAsm ("d", "Print bytecode before native code generation", cl::Hidden);
31 static cl::String TraceLibPath ("tracelibpath", "Path to libinstr for trace code", cl::Hidden, "");
34 TraceOff, TraceFunctions, TraceBasicBlocks
37 static cl::Enum<enum TraceLevel> TraceValues("trace", cl::NoFlags,
38 "Trace values through functions or basic blocks",
39 clEnumValN(TraceOff , "off", "Disable trace code"),
40 clEnumValN(TraceFunctions , "function", "Trace each function"),
41 clEnumValN(TraceBasicBlocks, "basicblock", "Trace each basic block"), 0);
44 // GetFileNameRoot - Helper function to get the basename of a filename...
45 static inline string GetFileNameRoot(const string &InputFilename) {
46 string IFN = InputFilename;
47 string outputFilename;
48 int Len = IFN.length();
49 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
50 outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
54 return outputFilename;
57 static void insertTraceCodeFor(Module &M) {
60 // Insert trace code in all functions in the module
61 switch (TraceValues) {
62 case TraceBasicBlocks:
63 Passes.add(createTraceValuesPassForBasicBlocks());
66 Passes.add(createTraceValuesPassForFunction());
69 assert(0 && "Bad value for TraceValues!");
73 // Eliminate duplication in constant pool
74 Passes.add(createConstantMergePass());
76 // Run passes to insert and clean up trace code...
79 std::string ErrorMessage;
81 // Load the module that contains the runtime helper routines neccesary for
82 // pointer hashing and stuff... link this module into the program if possible
84 Module *TraceModule = ParseBytecodeFile(TraceLibPath+"libinstr.bc");
86 // Ok, the TraceLibPath didn't contain a valid module. Try to load the module
87 // from the current LLVM-GCC install directory. This is kindof a hack, but
88 // allows people to not HAVE to have built the library.
91 TraceModule = ParseBytecodeFile("/home/vadve/lattner/cvs/gcc_install/lib/"
92 "gcc-lib/llvm/3.1/libinstr.bc");
94 // If we still didn't get it, cancel trying to link it in...
95 if (TraceModule == 0) {
96 cerr << "Warning, could not load trace routines to link into program!\n";
99 // Link in the trace routines... if the link fails, don't panic, because the
100 // compile should still succeed, just the native linker will probably fail.
102 std::auto_ptr<Module> TraceRoutines(TraceModule);
103 if (LinkModules(&M, TraceRoutines.get(), &ErrorMessage))
104 cerr << "Warning: Error linking in trace routines: "
105 << ErrorMessage << "\n";
109 // Write out the module with tracing code just before code generation
110 if (InputFilename != "-") {
111 string TraceFilename = GetFileNameRoot(InputFilename) + ".trace.bc";
113 std::ofstream Out(TraceFilename.c_str());
115 cerr << "Error opening '" << TraceFilename
116 << "'!: Skipping output of trace code as bytecode\n";
118 cerr << "Emitting trace code to '" << TraceFilename
119 << "' for comparison...\n";
120 WriteBytecodeToFile(&M, Out);
127 //===---------------------------------------------------------------------===//
130 // Entry point for the llc compiler.
131 //===---------------------------------------------------------------------===//
133 int main(int argc, char **argv) {
134 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
136 // Allocate a target... in the future this will be controllable on the
138 std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
139 assert(target.get() && "Could not allocate target machine!");
141 TargetMachine &Target = *target.get();
143 // Load the module to be compiled...
144 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
146 cerr << "bytecode didn't read correctly.\n";
150 if (TraceValues != TraceOff) // If tracing enabled...
151 insertTraceCodeFor(*M.get()); // Hack up module before using passmanager...
153 // Build up all of the passes that we want to do to the module...
156 // Decompose multi-dimensional refs into a sequence of 1D refs
157 Passes.add(createDecomposeMultiDimRefsPass());
159 // Replace malloc and free instructions with library calls.
160 // Do this after tracing until lli implements these lib calls.
161 // For now, it will emulate malloc and free internally.
162 Passes.add(createLowerAllocationsPass(Target.DataLayout));
164 // If LLVM dumping after transformations is requested, add it to the pipeline
166 Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
168 // Figure out where we are going to send the output...
169 std::ostream *Out = 0;
170 if (OutputFilename != "") { // Specified an output filename?
171 if (!Force && std::ifstream(OutputFilename.c_str())) {
172 // If force is not specified, make sure not to overwrite a file!
173 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
174 << "Use -f command line argument to force output\n";
177 Out = new std::ofstream(OutputFilename.c_str());
179 // Make sure that the Out file gets unlink'd from the disk if we get a
181 RemoveFileOnSignal(OutputFilename);
183 if (InputFilename == "-") {
184 OutputFilename = "-";
187 string OutputFilename = GetFileNameRoot(InputFilename);
188 OutputFilename += ".s";
190 if (!Force && std::ifstream(OutputFilename.c_str())) {
191 // If force is not specified, make sure not to overwrite a file!
192 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
193 << "Use -f command line argument to force output\n";
197 Out = new std::ofstream(OutputFilename.c_str());
199 cerr << "Error opening " << OutputFilename << "!\n";
203 // Make sure that the Out file gets unlink'd from the disk if we get a
205 RemoveFileOnSignal(OutputFilename);
209 Target.addPassesToEmitAssembly(Passes, *Out);
211 // Run our queue of passes all at once now, efficiently.
212 Passes.run(*M.get());
214 if (Out != &std::cout) delete Out;