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/TargetMachineImpls.h"
9 #include "llvm/Target/TargetMachine.h"
10 #include "llvm/Transforms/Instrumentation.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/PassManager.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/PassNameParser.h"
20 #include "Support/CommandLine.h"
21 #include "Support/Signals.h"
25 //------------------------------------------------------------------------------
26 // Option declarations for LLC.
27 //------------------------------------------------------------------------------
29 // Make all registered optimization passes available to llc. These passes
30 // will all be run before the simplification and lowering steps used by the
31 // back-end code generator, and will be run in the order specified on the
32 // command line. The OptimizationList is automatically populated with
33 // registered Passes by the PassNameParser.
35 static cl::list<const PassInfo*, bool,
36 FilteredPassNameParser<PassInfo::Optimization> >
37 OptimizationList(cl::desc("Optimizations available:"));
40 // General options for llc. Other pass-specific options are specified
41 // within the corresponding llc passes, and target-specific options
42 // and back-end code generation options are specified with the target machine.
44 static cl::opt<std::string>
45 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
47 static cl::opt<std::string>
48 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
50 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
53 DisableStrip("disable-strip",
54 cl::desc("Do not strip the LLVM bytecode included in the executable"));
57 DumpAsm("d", cl::desc("Print bytecode before native code generation"),
60 static cl::opt<std::string>
61 TraceLibPath("tracelibpath", cl::desc("Path to libinstr for trace code"),
62 cl::value_desc("directory"), cl::Hidden);
65 // flags set from -tracem and -trace options to control tracing
66 static bool TraceFunctions = false;
67 static bool TraceBasicBlocks = false;
70 // GetFileNameRoot - Helper function to get the basename of a filename...
71 static inline std::string
72 GetFileNameRoot(const std::string &InputFilename)
74 std::string IFN = InputFilename;
75 std::string outputFilename;
76 int Len = IFN.length();
77 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
78 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
82 return outputFilename;
86 insertTraceCodeFor(Module &M)
90 // Insert trace code in all functions in the module
92 Passes.add(createTraceValuesPassForBasicBlocks());
93 else if (TraceFunctions)
94 Passes.add(createTraceValuesPassForFunction());
98 // Eliminate duplication in constant pool
99 Passes.add(createConstantMergePass());
101 // Run passes to insert and clean up trace code...
104 std::string ErrorMessage;
106 // Load the module that contains the runtime helper routines neccesary for
107 // pointer hashing and stuff... link this module into the program if possible
109 Module *TraceModule = ParseBytecodeFile(TraceLibPath+"libinstr.bc");
111 // Check if the TraceLibPath contains a valid module. If not, try to load
112 // the module from the current LLVM-GCC install directory. This is kindof
113 // a hack, but allows people to not HAVE to have built the library.
115 if (TraceModule == 0)
116 TraceModule = ParseBytecodeFile("/home/vadve/lattner/cvs/gcc_install/lib/"
117 "gcc-lib/llvm/3.1/libinstr.bc");
119 // If we still didn't get it, cancel trying to link it in...
120 if (TraceModule == 0)
121 std::cerr <<"WARNING: couldn't load trace routines to link into program!\n";
124 // Link in the trace routines... if this fails, don't panic, because the
125 // compile should still succeed, but the native linker will probably fail.
127 std::auto_ptr<Module> TraceRoutines(TraceModule);
128 if (LinkModules(&M, TraceRoutines.get(), &ErrorMessage))
129 std::cerr << "WARNING: Error linking in trace routines: "
130 << ErrorMessage << "\n";
133 // Write out the module with tracing code just before code generation
134 assert (InputFilename != "-"
135 && "Cannot write out traced bytecode when reading input from stdin");
136 std::string TraceFilename = GetFileNameRoot(InputFilename) + ".trace.bc";
138 std::ofstream Out(TraceFilename.c_str());
140 std::cerr << "Error opening '" << TraceFilename
141 << "'!: Skipping output of trace code as bytecode\n";
144 std::cerr << "Emitting trace code to '" << TraceFilename
145 << "' for comparison...\n";
146 WriteBytecodeToFile(&M, Out);
152 // Making tracing a module pass so the entire module with tracing
153 // can be written out before continuing.
154 struct InsertTracingCodePass: public Pass {
155 virtual bool run(Module &M) {
156 return insertTraceCodeFor(M);
161 //===---------------------------------------------------------------------===//
164 // Entry point for the llc compiler.
165 //===---------------------------------------------------------------------===//
168 main(int argc, char **argv)
170 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
172 // Allocate a target... in the future this will be controllable on the
174 std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
175 assert(target.get() && "Could not allocate target machine!");
177 TargetMachine &Target = *target.get();
178 const TargetData &TD = Target.getTargetData();
180 // Load the module to be compiled...
181 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
184 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
188 // Build up all of the passes that we want to do to the module...
191 Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
192 TD.getPointerAlignment(), TD.getDoubleAlignment()));
194 // Create a new optimization pass for each one specified on the command line
195 // Deal specially with tracing passes, which must be run differently than opt.
197 for (unsigned i = 0; i < OptimizationList.size(); ++i)
199 const PassInfo *Opt = OptimizationList[i];
201 if (std::string(Opt->getPassArgument()) == "trace")
202 TraceFunctions = !(TraceBasicBlocks = true);
203 else if (std::string(Opt->getPassArgument()) == "tracem")
204 TraceFunctions = !(TraceBasicBlocks = false);
206 { // handle other passes as normal optimization passes
207 if (Opt->getNormalCtor())
208 Passes.add(Opt->getNormalCtor()());
209 else if (Opt->getTargetCtor())
210 Passes.add(Opt->getTargetCtor()(Target));
212 std::cerr << argv[0] << ": cannot create pass: "
213 << Opt->getPassName() << "\n";
217 // Run tracing passes after other optimization passes and before llc passes.
218 if (TraceFunctions || TraceBasicBlocks)
219 Passes.add(new InsertTracingCodePass);
221 // Decompose multi-dimensional refs into a sequence of 1D refs
222 Passes.add(createDecomposeMultiDimRefsPass());
224 // Replace malloc and free instructions with library calls.
225 // Do this after tracing until lli implements these lib calls.
226 // For now, it will emulate malloc and free internally.
227 Passes.add(createLowerAllocationsPass());
229 // If LLVM dumping after transformations is requested, add it to the pipeline
231 Passes.add(new PrintFunctionPass("Code after xformations: \n", &std::cerr));
233 // Strip all of the symbols from the bytecode so that it will be smaller...
235 Passes.add(createSymbolStrippingPass());
237 // Figure out where we are going to send the output...
238 std::ostream *Out = 0;
239 if (OutputFilename != "")
240 { // Specified an output filename?
241 if (!Force && std::ifstream(OutputFilename.c_str())) {
242 // If force is not specified, make sure not to overwrite a file!
243 std::cerr << argv[0] << ": error opening '" << OutputFilename
244 << "': file exists!\n"
245 << "Use -f command line argument to force output\n";
248 Out = new std::ofstream(OutputFilename.c_str());
250 // Make sure that the Out file gets unlink'd from the disk if we get a
252 RemoveFileOnSignal(OutputFilename);
256 if (InputFilename == "-")
258 OutputFilename = "-";
263 std::string OutputFilename = GetFileNameRoot(InputFilename);
264 OutputFilename += ".s";
266 if (!Force && std::ifstream(OutputFilename.c_str()))
268 // If force is not specified, make sure not to overwrite a file!
269 std::cerr << argv[0] << ": error opening '" << OutputFilename
270 << "': file exists!\n"
271 << "Use -f command line argument to force output\n";
275 Out = new std::ofstream(OutputFilename.c_str());
278 std::cerr << argv[0] << ": error opening " << OutputFilename
284 // Make sure that the Out file gets unlink'd from the disk if we get a
286 RemoveFileOnSignal(OutputFilename);
290 // Ask the target to add backend passes as neccesary
291 if (Target.addPassesToEmitAssembly(Passes, *Out)) {
292 std::cerr << argv[0] << ": target '" << Target.getName()
293 << " does not support static compilation!\n";
295 // Run our queue of passes all at once now, efficiently.
296 Passes.run(*M.get());
299 // Delete the ostream if it's not a stdout stream
300 if (Out != &std::cout) delete Out;