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/HoistPHIConstants.h"
13 #include "llvm/Transforms/Scalar/DecomposeMultiDimRefs.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);
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;
57 //===---------------------------------------------------------------------===//
60 // Entry point for the llc compiler.
61 //===---------------------------------------------------------------------===//
63 int main(int argc, char **argv) {
64 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
66 // Allocate a target... in the future this will be controllable on the
68 std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
69 assert(target.get() && "Could not allocate target machine!");
71 TargetMachine &Target = *target.get();
73 // Load the module to be compiled...
74 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
76 cerr << "bytecode didn't read correctly.\n";
80 // Build up all of the passes that we want to do to the module...
83 // Hoist constants out of PHI nodes into predecessor BB's
84 Passes.add(createHoistPHIConstantsPass());
86 if (TraceValues != TraceOff) { // If tracing enabled...
87 // Insert trace code in all functions in the module
88 if (TraceValues == TraceBasicBlocks)
89 Passes.add(createTraceValuesPassForBasicBlocks());
90 else if (TraceValues == TraceFunctions)
91 Passes.add(createTraceValuesPassForFunction());
93 assert(0 && "Bad value for TraceValues!");
95 // Eliminate duplication in constant pool
96 Passes.add(createDynamicConstantMergePass());
99 // Decompose multi-dimensional refs into a sequence of 1D refs
100 Passes.add(createDecomposeMultiDimRefsPass());
102 // Write out the module with tracing code just before code generation
103 if (TraceValues != TraceOff) { // If tracing enabled...
104 assert(InputFilename != "-" &&
105 "files on stdin not supported with tracing");
106 string traceFileName = GetFileNameRoot(InputFilename) + ".trace.bc";
108 if (!Force && std::ifstream(OutputFilename.c_str())) {
109 // If force is not specified, make sure not to overwrite a file!
110 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
111 << "Use -f command line argument to force output\n";
115 std::ostream *os = new std::ofstream(traceFileName.c_str());
117 cerr << "Error opening " << traceFileName
118 << "! SKIPPING OUTPUT OF TRACE CODE\n";
123 Passes.add(new WriteBytecodePass(os, true));
126 // Replace malloc and free instructions with library calls.
127 // Do this after tracing until lli implements these lib calls.
128 // For now, it will emulate malloc and free internally.
129 Passes.add(createLowerAllocationsPass(Target.DataLayout));
131 // If LLVM dumping after transformations is requested, add it to the pipeline
133 Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
135 // Figure out where we are going to send the output...
136 std::ostream *Out = 0;
137 if (OutputFilename != "") { // Specified an output filename?
138 if (!Force && std::ifstream(OutputFilename.c_str())) {
139 // If force is not specified, make sure not to overwrite a file!
140 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
141 << "Use -f command line argument to force output\n";
144 Out = new std::ofstream(OutputFilename.c_str());
146 // Make sure that the Out file gets unlink'd from the disk if we get a
148 RemoveFileOnSignal(OutputFilename);
150 if (InputFilename == "-") {
151 OutputFilename = "-";
154 string OutputFilename = GetFileNameRoot(InputFilename);
155 OutputFilename += ".s";
157 if (!Force && std::ifstream(OutputFilename.c_str())) {
158 // If force is not specified, make sure not to overwrite a file!
159 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
160 << "Use -f command line argument to force output\n";
164 Out = new std::ofstream(OutputFilename.c_str());
166 cerr << "Error opening " << OutputFilename << "!\n";
170 // Make sure that the Out file gets unlink'd from the disk if we get a
172 RemoveFileOnSignal(OutputFilename);
176 Target.addPassesToEmitAssembly(Passes, *Out);
178 // Run our queue of passes all at once now, efficiently.
181 if (Out != &std::cout) delete Out;