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"
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, "");
33 TraceOff, TraceFunctions, TraceBasicBlocks
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);
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/
53 return outputFilename;
56 static void insertTraceCodeFor(Module *M) {
59 // Insert trace code in all functions in the module
60 switch (TraceValues) {
61 case TraceBasicBlocks:
62 Passes.add(createTraceValuesPassForBasicBlocks());
65 Passes.add(createTraceValuesPassForFunction());
68 assert(0 && "Bad value for TraceValues!");
72 // Eliminate duplication in constant pool
73 Passes.add(createDynamicConstantMergePass());
75 // Run passes to insert and clean up trace code...
78 std::string ErrorMessage;
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
83 Module *TraceModule = ParseBytecodeFile(TraceLibPath+"libinstr.bc");
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.
90 TraceModule = ParseBytecodeFile("/home/vadve/lattner/cvs/gcc_install/lib/"
91 "gcc-lib/llvm/3.1/libinstr.bc");
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";
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.
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";
108 // Write out the module with tracing code just before code generation
109 if (InputFilename != "-") {
110 string TraceFilename = GetFileNameRoot(InputFilename) + ".trace.bc";
112 std::ofstream Out(TraceFilename.c_str());
114 cerr << "Error opening '" << TraceFilename
115 << "'!: Skipping output of trace code as bytecode\n";
117 cerr << "Emitting trace code to '" << TraceFilename
118 << "' for comparison...\n";
119 WriteBytecodeToFile(M, Out);
126 //===---------------------------------------------------------------------===//
129 // Entry point for the llc compiler.
130 //===---------------------------------------------------------------------===//
132 int main(int argc, char **argv) {
133 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
135 // Allocate a target... in the future this will be controllable on the
137 std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
138 assert(target.get() && "Could not allocate target machine!");
140 TargetMachine &Target = *target.get();
142 // Load the module to be compiled...
143 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
145 cerr << "bytecode didn't read correctly.\n";
149 if (TraceValues != TraceOff) // If tracing enabled...
150 insertTraceCodeFor(M.get()); // Hack up module before using passmanager...
152 // Build up all of the passes that we want to do to the module...
155 // Decompose multi-dimensional refs into a sequence of 1D refs
156 Passes.add(createDecomposeMultiDimRefsPass());
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));
163 // If LLVM dumping after transformations is requested, add it to the pipeline
165 Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
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";
176 Out = new std::ofstream(OutputFilename.c_str());
178 // Make sure that the Out file gets unlink'd from the disk if we get a
180 RemoveFileOnSignal(OutputFilename);
182 if (InputFilename == "-") {
183 OutputFilename = "-";
186 string OutputFilename = GetFileNameRoot(InputFilename);
187 OutputFilename += ".s";
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";
196 Out = new std::ofstream(OutputFilename.c_str());
198 cerr << "Error opening " << OutputFilename << "!\n";
202 // Make sure that the Out file gets unlink'd from the disk if we get a
204 RemoveFileOnSignal(OutputFilename);
208 Target.addPassesToEmitAssembly(Passes, *Out);
210 // Run our queue of passes all at once now, efficiently.
213 if (Out != &std::cout) delete Out;