2001-10-04 03:40:53 +02:00
|
|
|
//===-- llc.cpp - Implement the LLVM Compiler -----------------------------===//
|
2001-09-08 00:20:50 +02:00
|
|
|
//
|
|
|
|
// This is the llc compiler driver.
|
|
|
|
//
|
2001-10-04 03:40:53 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-21 14:42:29 +02:00
|
|
|
|
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2002-10-29 21:45:04 +01:00
|
|
|
#include "llvm/Target/TargetMachineImpls.h"
|
2001-09-18 15:10:45 +02:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2001-10-15 01:29:28 +02:00
|
|
|
#include "llvm/Transforms/Instrumentation/TraceValues.h"
|
2002-05-07 22:03:27 +02:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2002-05-20 23:20:08 +02:00
|
|
|
#include "llvm/Transforms/Utils/Linker.h"
|
2001-10-18 22:32:07 +02:00
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
2002-07-24 00:04:43 +02:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2001-09-07 23:26:31 +02:00
|
|
|
#include "llvm/Module.h"
|
2002-01-31 01:46:45 +01:00
|
|
|
#include "llvm/PassManager.h"
|
2002-09-16 18:35:34 +02:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/PassNameParser.h"
|
2001-11-27 01:03:19 +01:00
|
|
|
#include "Support/CommandLine.h"
|
2002-04-18 21:55:25 +02:00
|
|
|
#include "Support/Signals.h"
|
2001-09-18 19:04:18 +02:00
|
|
|
#include <memory>
|
2001-09-19 18:52:09 +02:00
|
|
|
#include <fstream>
|
2002-01-20 23:54:45 +01:00
|
|
|
using std::string;
|
2002-06-25 23:57:48 +02:00
|
|
|
using std::cerr;
|
2001-07-23 04:35:57 +02:00
|
|
|
|
2002-09-16 18:35:34 +02:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Option declarations for LLC.
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Make all registered optimization passes available to llc. These passes
|
|
|
|
// will all be run before the simplification and lowering steps used by the
|
|
|
|
// back-end code generator, and will be run in the order specified on the
|
|
|
|
// command line. The OptimizationList is automatically populated with
|
|
|
|
// registered Passes by the PassNameParser.
|
|
|
|
//
|
|
|
|
static cl::list<const PassInfo*, bool,
|
|
|
|
FilteredPassNameParser<PassInfo::Optimization> >
|
|
|
|
OptimizationList(cl::desc("Optimizations available:"));
|
|
|
|
|
|
|
|
|
|
|
|
// General options for llc. Other pass-specific options are specified
|
|
|
|
// within the corresponding llc passes, and target-specific options
|
|
|
|
// and back-end code generation options are specified with the target machine.
|
|
|
|
//
|
2002-07-22 04:10:13 +02:00
|
|
|
static cl::opt<string>
|
|
|
|
InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<string>
|
|
|
|
OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
|
|
|
|
|
|
|
|
static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
DumpAsm("d", cl::desc("Print bytecode before native code generation"),
|
|
|
|
cl::Hidden);
|
|
|
|
|
|
|
|
static cl::opt<string>
|
|
|
|
TraceLibPath("tracelibpath", cl::desc("Path to libinstr for trace code"),
|
|
|
|
cl::value_desc("directory"), cl::Hidden);
|
2002-02-26 22:36:53 +01:00
|
|
|
|
|
|
|
|
2002-09-16 18:35:34 +02:00
|
|
|
// flags set from -tracem and -trace options to control tracing
|
|
|
|
static bool TraceFunctions = false;
|
|
|
|
static bool TraceBasicBlocks = false;
|
|
|
|
|
2001-10-04 03:40:53 +02:00
|
|
|
|
2001-10-15 19:30:47 +02:00
|
|
|
// GetFileNameRoot - Helper function to get the basename of a filename...
|
2002-09-16 18:35:34 +02:00
|
|
|
static inline string
|
|
|
|
GetFileNameRoot(const string &InputFilename)
|
|
|
|
{
|
2001-10-15 01:29:28 +02:00
|
|
|
string IFN = InputFilename;
|
|
|
|
string outputFilename;
|
|
|
|
int Len = IFN.length();
|
|
|
|
if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
|
|
|
|
outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
|
|
|
|
} else {
|
2001-10-15 19:30:47 +02:00
|
|
|
outputFilename = IFN;
|
2001-10-15 01:29:28 +02:00
|
|
|
}
|
|
|
|
return outputFilename;
|
|
|
|
}
|
|
|
|
|
2002-09-16 18:35:34 +02:00
|
|
|
static bool
|
|
|
|
insertTraceCodeFor(Module &M)
|
|
|
|
{
|
2002-05-20 23:20:08 +02:00
|
|
|
PassManager Passes;
|
|
|
|
|
|
|
|
// Insert trace code in all functions in the module
|
2002-09-16 18:35:34 +02:00
|
|
|
if (TraceBasicBlocks)
|
2002-05-20 23:20:08 +02:00
|
|
|
Passes.add(createTraceValuesPassForBasicBlocks());
|
2002-09-16 18:35:34 +02:00
|
|
|
else if (TraceFunctions)
|
2002-05-20 23:20:08 +02:00
|
|
|
Passes.add(createTraceValuesPassForFunction());
|
2002-09-16 18:35:34 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
2002-05-20 23:20:08 +02:00
|
|
|
// Eliminate duplication in constant pool
|
2002-06-25 18:13:24 +02:00
|
|
|
Passes.add(createConstantMergePass());
|
2002-05-20 23:20:08 +02:00
|
|
|
|
|
|
|
// Run passes to insert and clean up trace code...
|
|
|
|
Passes.run(M);
|
|
|
|
|
|
|
|
std::string ErrorMessage;
|
|
|
|
|
|
|
|
// Load the module that contains the runtime helper routines neccesary for
|
|
|
|
// pointer hashing and stuff... link this module into the program if possible
|
|
|
|
//
|
|
|
|
Module *TraceModule = ParseBytecodeFile(TraceLibPath+"libinstr.bc");
|
|
|
|
|
2002-09-16 18:35:34 +02:00
|
|
|
// Check if the TraceLibPath contains a valid module. If not, try to load
|
|
|
|
// the module from the current LLVM-GCC install directory. This is kindof
|
|
|
|
// a hack, but allows people to not HAVE to have built the library.
|
2002-05-20 23:20:08 +02:00
|
|
|
//
|
|
|
|
if (TraceModule == 0)
|
|
|
|
TraceModule = ParseBytecodeFile("/home/vadve/lattner/cvs/gcc_install/lib/"
|
|
|
|
"gcc-lib/llvm/3.1/libinstr.bc");
|
|
|
|
|
|
|
|
// If we still didn't get it, cancel trying to link it in...
|
2002-09-16 18:35:34 +02:00
|
|
|
if (TraceModule == 0)
|
2002-05-20 23:20:08 +02:00
|
|
|
cerr << "Warning, could not load trace routines to link into program!\n";
|
2002-09-16 18:35:34 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Link in the trace routines... if this fails, don't panic, because the
|
|
|
|
// compile should still succeed, but the native linker will probably fail.
|
|
|
|
//
|
|
|
|
std::auto_ptr<Module> TraceRoutines(TraceModule);
|
|
|
|
if (LinkModules(&M, TraceRoutines.get(), &ErrorMessage))
|
|
|
|
cerr << "Warning: Error linking in trace routines: "
|
|
|
|
<< ErrorMessage << "\n";
|
|
|
|
}
|
2002-05-20 23:20:08 +02:00
|
|
|
|
|
|
|
// Write out the module with tracing code just before code generation
|
2002-09-16 18:35:34 +02:00
|
|
|
assert (InputFilename != "-"
|
|
|
|
&& "Cannot write out traced bytecode when reading input from stdin");
|
|
|
|
string TraceFilename = GetFileNameRoot(InputFilename) + ".trace.bc";
|
|
|
|
|
|
|
|
std::ofstream Out(TraceFilename.c_str());
|
|
|
|
if (!Out.good())
|
|
|
|
cerr << "Error opening '" << TraceFilename
|
|
|
|
<< "'!: Skipping output of trace code as bytecode\n";
|
|
|
|
else
|
|
|
|
{
|
2002-05-20 23:20:08 +02:00
|
|
|
cerr << "Emitting trace code to '" << TraceFilename
|
|
|
|
<< "' for comparison...\n";
|
2002-06-25 18:13:24 +02:00
|
|
|
WriteBytecodeToFile(&M, Out);
|
2002-05-20 23:20:08 +02:00
|
|
|
}
|
|
|
|
|
2002-09-16 18:35:34 +02:00
|
|
|
return true;
|
2002-05-20 23:20:08 +02:00
|
|
|
}
|
2002-09-16 18:35:34 +02:00
|
|
|
|
|
|
|
// Making tracing a module pass so the entire module with tracing
|
|
|
|
// can be written out before continuing.
|
|
|
|
struct InsertTracingCodePass: public Pass {
|
|
|
|
virtual bool run(Module &M) {
|
|
|
|
return insertTraceCodeFor(M);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2001-09-18 19:04:18 +02:00
|
|
|
|
2001-10-15 01:29:28 +02:00
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// Function main()
|
|
|
|
//
|
|
|
|
// Entry point for the llc compiler.
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
2002-09-16 18:35:34 +02:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2001-10-15 01:29:28 +02:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
|
|
|
|
|
|
|
|
// Allocate a target... in the future this will be controllable on the
|
|
|
|
// command line.
|
2002-01-20 23:54:45 +01:00
|
|
|
std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
|
2001-10-15 19:30:47 +02:00
|
|
|
assert(target.get() && "Could not allocate target machine!");
|
|
|
|
|
|
|
|
TargetMachine &Target = *target.get();
|
2002-09-16 18:35:34 +02:00
|
|
|
|
2001-10-15 01:29:28 +02:00
|
|
|
// Load the module to be compiled...
|
2002-01-20 23:54:45 +01:00
|
|
|
std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
|
2002-09-16 18:35:34 +02:00
|
|
|
if (M.get() == 0)
|
|
|
|
{
|
|
|
|
cerr << argv[0] << ": bytecode didn't read correctly.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2002-05-20 23:20:08 +02:00
|
|
|
|
2001-10-15 19:30:47 +02:00
|
|
|
// Build up all of the passes that we want to do to the module...
|
2002-01-21 08:31:50 +01:00
|
|
|
PassManager Passes;
|
2001-10-15 19:30:47 +02:00
|
|
|
|
2002-09-16 18:35:34 +02:00
|
|
|
// Create a new optimization pass for each one specified on the command line
|
|
|
|
// Deal specially with tracing passes, which must be run differently than opt.
|
|
|
|
//
|
|
|
|
for (unsigned i = 0; i < OptimizationList.size(); ++i)
|
|
|
|
{
|
|
|
|
const PassInfo *Opt = OptimizationList[i];
|
|
|
|
|
2002-10-29 21:45:04 +01:00
|
|
|
if (std::string(Opt->getPassArgument()) == "trace")
|
2002-09-16 18:35:34 +02:00
|
|
|
TraceFunctions = !(TraceBasicBlocks = true);
|
2002-10-29 21:45:04 +01:00
|
|
|
else if (std::string(Opt->getPassArgument()) == "tracem")
|
2002-09-16 18:35:34 +02:00
|
|
|
TraceFunctions = !(TraceBasicBlocks = false);
|
|
|
|
else
|
|
|
|
{ // handle other passes as normal optimization passes
|
|
|
|
if (Opt->getNormalCtor())
|
|
|
|
Passes.add(Opt->getNormalCtor()());
|
|
|
|
else if (Opt->getDataCtor())
|
|
|
|
Passes.add(Opt->getDataCtor()(Target.DataLayout));
|
|
|
|
else if (Opt->getTargetCtor())
|
|
|
|
Passes.add(Opt->getTargetCtor()(Target));
|
|
|
|
else
|
|
|
|
cerr << argv[0] << ": cannot create pass: "
|
|
|
|
<< Opt->getPassName() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run tracing passes after other optimization passes and before llc passes.
|
|
|
|
if (TraceFunctions || TraceBasicBlocks)
|
|
|
|
Passes.add(new InsertTracingCodePass);
|
|
|
|
|
2002-03-24 04:19:54 +01:00
|
|
|
// Decompose multi-dimensional refs into a sequence of 1D refs
|
|
|
|
Passes.add(createDecomposeMultiDimRefsPass());
|
2002-09-16 18:35:34 +02:00
|
|
|
|
2001-10-18 20:20:20 +02:00
|
|
|
// Replace malloc and free instructions with library calls.
|
|
|
|
// Do this after tracing until lli implements these lib calls.
|
|
|
|
// For now, it will emulate malloc and free internally.
|
2002-09-26 01:47:49 +02:00
|
|
|
Passes.add(createLowerAllocationsPass());
|
2002-09-16 18:35:34 +02:00
|
|
|
|
2001-10-15 19:30:47 +02:00
|
|
|
// If LLVM dumping after transformations is requested, add it to the pipeline
|
|
|
|
if (DumpAsm)
|
2002-04-09 00:05:01 +02:00
|
|
|
Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
|
2001-10-15 19:30:47 +02:00
|
|
|
|
2002-06-30 18:25:07 +02:00
|
|
|
// Strip all of the symbols from the bytecode so that it will be smaller...
|
|
|
|
Passes.add(createSymbolStrippingPass());
|
|
|
|
|
2002-02-04 00:43:19 +01:00
|
|
|
// Figure out where we are going to send the output...
|
|
|
|
std::ostream *Out = 0;
|
2002-09-16 18:35:34 +02:00
|
|
|
if (OutputFilename != "")
|
|
|
|
{ // Specified an output filename?
|
2002-01-22 22:07:24 +01:00
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
2002-01-20 23:54:45 +01:00
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
2002-07-30 23:43:22 +02:00
|
|
|
cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists!\n"
|
2002-01-20 23:54:45 +01:00
|
|
|
<< "Use -f command line argument to force output\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
2002-09-16 18:35:34 +02:00
|
|
|
|
2002-09-19 18:06:28 +02:00
|
|
|
// Make sure that the Out file gets unlink'd from the disk if we get a
|
|
|
|
// SIGINT
|
2002-04-18 21:55:25 +02:00
|
|
|
RemoveFileOnSignal(OutputFilename);
|
2001-10-15 19:30:47 +02:00
|
|
|
}
|
2002-09-16 18:35:34 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (InputFilename == "-")
|
|
|
|
{
|
|
|
|
OutputFilename = "-";
|
|
|
|
Out = &std::cout;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string OutputFilename = GetFileNameRoot(InputFilename);
|
|
|
|
OutputFilename += ".s";
|
|
|
|
|
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str()))
|
|
|
|
{
|
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
|
|
|
cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
|
|
|
if (!Out->good())
|
|
|
|
{
|
|
|
|
cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
|
|
|
|
delete Out;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the Out file gets unlink'd from the disk if we get a
|
|
|
|
// SIGINT
|
|
|
|
RemoveFileOnSignal(OutputFilename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-29 22:12:46 +01:00
|
|
|
// Ask the target to add backend passes as neccesary
|
|
|
|
if (Target.addPassesToEmitAssembly(Passes, *Out)) {
|
2002-10-29 22:48:33 +01:00
|
|
|
cerr << argv[0] << ": target '" << Target.getName()
|
2002-10-29 22:12:46 +01:00
|
|
|
<< " does not support static compilation!\n";
|
|
|
|
} else {
|
|
|
|
// Run our queue of passes all at once now, efficiently.
|
|
|
|
Passes.run(*M.get());
|
|
|
|
}
|
2001-10-18 03:31:22 +02:00
|
|
|
|
2002-09-19 18:06:28 +02:00
|
|
|
// Delete the ostream if it's not a stdout stream
|
2002-02-04 00:43:19 +01:00
|
|
|
if (Out != &std::cout) delete Out;
|
|
|
|
|
2001-10-18 22:33:21 +02:00
|
|
|
return 0;
|
2001-10-15 01:29:28 +02:00
|
|
|
}
|