Make sure that there is no case where a signal can occur leaving a partially
[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/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"
22 #include <memory>
23 #include <string>
24 #include <fstream>
25 using std::string;
26
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
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
57 //===---------------------------------------------------------------------===//
58 // Function main()
59 // 
60 // Entry point for the llc compiler.
61 //===---------------------------------------------------------------------===//
62
63 int main(int argc, char **argv) {
64   cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
65   
66   // Allocate a target... in the future this will be controllable on the
67   // command line.
68   std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
69   assert(target.get() && "Could not allocate target machine!");
70
71   TargetMachine &Target = *target.get();
72   
73   // Load the module to be compiled...
74   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
75   if (M.get() == 0) {
76     cerr << "bytecode didn't read correctly.\n";
77     return 1;
78   }
79
80   // Build up all of the passes that we want to do to the module...
81   PassManager Passes;
82
83   // Hoist constants out of PHI nodes into predecessor BB's
84   Passes.add(createHoistPHIConstantsPass());
85
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(createTraceValuesPassForMethod());
92     else
93       assert(0 && "Bad value for TraceValues!");
94
95     // Eliminate duplication in constant pool
96     Passes.add(createDynamicConstantMergePass());
97   }
98   
99   // Decompose multi-dimensional refs into a sequence of 1D refs
100   Passes.add(createDecomposeMultiDimRefsPass());
101   
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";
107
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";
112       return 1;
113     }
114     
115     std::ostream *os = new std::ofstream(traceFileName.c_str());
116     if (!os->good()) {
117       cerr << "Error opening " << traceFileName
118            << "! SKIPPING OUTPUT OF TRACE CODE\n";
119       delete os;
120       return 1;
121     }
122     
123     Passes.add(new WriteBytecodePass(os, true));
124   }
125   
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));
130   
131   // If LLVM dumping after transformations is requested, add it to the pipeline
132   if (DumpAsm)
133     Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
134
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";
142       return 1;
143     }
144     Out = new std::ofstream(OutputFilename.c_str());
145
146     // Make sure that the Out file gets unlink'd from the disk if we get a
147     // SIGINT
148     RemoveFileOnSignal(OutputFilename);
149   } else {
150     if (InputFilename == "-") {
151       OutputFilename = "-";
152       Out = &std::cout;
153     } else {
154       string OutputFilename = GetFileNameRoot(InputFilename); 
155       OutputFilename += ".s";
156
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";
161         return 1;
162       }
163
164       Out = new std::ofstream(OutputFilename.c_str());
165       if (!Out->good()) {
166         cerr << "Error opening " << OutputFilename << "!\n";
167         delete Out;
168         return 1;
169       }
170       // Make sure that the Out file gets unlink'd from the disk if we get a
171       // SIGINT
172       RemoveFileOnSignal(OutputFilename);
173     }
174   }
175   
176   Target.addPassesToEmitAssembly(Passes, *Out);
177   
178   // Run our queue of passes all at once now, efficiently.
179   Passes.run(M.get());
180
181   if (Out != &std::cout) delete Out;
182
183   return 0;
184 }