2001-10-04 01:40:53 +00:00
|
|
|
//===-- llc.cpp - Implement the LLVM Compiler -----------------------------===//
|
2001-09-07 22:20:50 +00:00
|
|
|
//
|
|
|
|
// This is the llc compiler driver.
|
|
|
|
//
|
2001-10-04 01:40:53 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-21 12:42:29 +00:00
|
|
|
|
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2001-09-14 05:34:53 +00:00
|
|
|
#include "llvm/Target/Sparc.h"
|
2001-09-18 13:10:45 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2001-10-14 23:29:28 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation/TraceValues.h"
|
2002-01-22 01:04:08 +00:00
|
|
|
#include "llvm/Transforms/ChangeAllocations.h"
|
2002-05-07 20:03:27 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2002-05-20 21:20:08 +00:00
|
|
|
#include "llvm/Transforms/Utils/Linker.h"
|
2001-10-18 20:32:07 +00:00
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
2001-10-18 20:06:31 +00:00
|
|
|
#include "llvm/Transforms/ConstantMerge.h"
|
2001-09-07 21:26:31 +00:00
|
|
|
#include "llvm/Module.h"
|
2002-04-07 20:49:59 +00:00
|
|
|
#include "llvm/Function.h"
|
2002-01-31 00:46:45 +00:00
|
|
|
#include "llvm/PassManager.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2002-04-18 19:55:25 +00:00
|
|
|
#include "Support/Signals.h"
|
2001-09-18 17:04:18 +00:00
|
|
|
#include <memory>
|
2001-09-19 16:52:09 +00:00
|
|
|
#include <fstream>
|
2002-01-20 22:54:45 +00:00
|
|
|
using std::string;
|
2002-06-25 21:57:48 +00:00
|
|
|
using std::cerr;
|
2001-07-23 02:35:57 +00:00
|
|
|
|
2002-02-26 21:36:53 +00:00
|
|
|
static cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
|
|
|
|
static cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
|
|
|
|
static cl::Flag Force ("f", "Overwrite output files");
|
|
|
|
static cl::Flag DumpAsm ("d", "Print bytecode before native code generation", cl::Hidden);
|
2002-05-20 21:20:08 +00:00
|
|
|
static cl::String TraceLibPath ("tracelibpath", "Path to libinstr for trace code", cl::Hidden, "");
|
2002-02-26 21:36:53 +00:00
|
|
|
|
|
|
|
enum TraceLevel {
|
2002-04-07 20:49:59 +00:00
|
|
|
TraceOff, TraceFunctions, TraceBasicBlocks
|
2002-02-26 21:36:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static cl::Enum<enum TraceLevel> TraceValues("trace", cl::NoFlags,
|
2002-04-07 20:49:59 +00:00
|
|
|
"Trace values through functions or basic blocks",
|
2002-02-26 21:36:53 +00:00
|
|
|
clEnumValN(TraceOff , "off", "Disable trace code"),
|
2002-04-07 20:49:59 +00:00
|
|
|
clEnumValN(TraceFunctions , "function", "Trace each function"),
|
2002-02-26 21:36:53 +00:00
|
|
|
clEnumValN(TraceBasicBlocks, "basicblock", "Trace each basic block"), 0);
|
2001-10-04 01:40:53 +00:00
|
|
|
|
|
|
|
|
2001-10-15 17:30:47 +00:00
|
|
|
// GetFileNameRoot - Helper function to get the basename of a filename...
|
|
|
|
static inline string GetFileNameRoot(const string &InputFilename) {
|
2001-10-14 23:29:28 +00: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 17:30:47 +00:00
|
|
|
outputFilename = IFN;
|
2001-10-14 23:29:28 +00:00
|
|
|
}
|
|
|
|
return outputFilename;
|
|
|
|
}
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
static void insertTraceCodeFor(Module &M) {
|
2002-05-20 21:20:08 +00:00
|
|
|
PassManager Passes;
|
|
|
|
|
|
|
|
// Insert trace code in all functions in the module
|
|
|
|
switch (TraceValues) {
|
|
|
|
case TraceBasicBlocks:
|
|
|
|
Passes.add(createTraceValuesPassForBasicBlocks());
|
|
|
|
break;
|
|
|
|
case TraceFunctions:
|
|
|
|
Passes.add(createTraceValuesPassForFunction());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0 && "Bad value for TraceValues!");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Eliminate duplication in constant pool
|
2002-06-25 16:13:24 +00:00
|
|
|
Passes.add(createConstantMergePass());
|
2002-05-20 21:20:08 +00: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");
|
|
|
|
|
|
|
|
// Ok, the TraceLibPath didn't contain a valid module. 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.
|
|
|
|
//
|
|
|
|
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...
|
|
|
|
if (TraceModule == 0) {
|
|
|
|
cerr << "Warning, could not load trace routines to link into program!\n";
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Link in the trace routines... if the link fails, don't panic, because the
|
|
|
|
// compile should still succeed, just the native linker will probably fail.
|
|
|
|
//
|
|
|
|
std::auto_ptr<Module> TraceRoutines(TraceModule);
|
2002-06-25 16:13:24 +00:00
|
|
|
if (LinkModules(&M, TraceRoutines.get(), &ErrorMessage))
|
2002-05-20 21:20:08 +00:00
|
|
|
cerr << "Warning: Error linking in trace routines: "
|
|
|
|
<< ErrorMessage << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Write out the module with tracing code just before code generation
|
|
|
|
if (InputFilename != "-") {
|
|
|
|
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 {
|
|
|
|
cerr << "Emitting trace code to '" << TraceFilename
|
|
|
|
<< "' for comparison...\n";
|
2002-06-25 16:13:24 +00:00
|
|
|
WriteBytecodeToFile(&M, Out);
|
2002-05-20 21:20:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-09-18 17:04:18 +00:00
|
|
|
|
2001-10-14 23:29:28 +00:00
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// Function main()
|
|
|
|
//
|
|
|
|
// Entry point for the llc compiler.
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
2001-10-15 17:41:24 +00:00
|
|
|
int main(int argc, char **argv) {
|
2001-10-14 23:29:28 +00: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 22:54:45 +00:00
|
|
|
std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
|
2001-10-15 17:30:47 +00:00
|
|
|
assert(target.get() && "Could not allocate target machine!");
|
|
|
|
|
|
|
|
TargetMachine &Target = *target.get();
|
2001-10-14 23:29:28 +00:00
|
|
|
|
|
|
|
// Load the module to be compiled...
|
2002-01-20 22:54:45 +00:00
|
|
|
std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
|
2001-10-14 23:29:28 +00:00
|
|
|
if (M.get() == 0) {
|
|
|
|
cerr << "bytecode didn't read correctly.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2001-10-15 17:30:47 +00:00
|
|
|
|
2002-05-20 21:20:08 +00:00
|
|
|
if (TraceValues != TraceOff) // If tracing enabled...
|
2002-06-25 16:13:24 +00:00
|
|
|
insertTraceCodeFor(*M.get()); // Hack up module before using passmanager...
|
2002-05-20 21:20:08 +00:00
|
|
|
|
2001-10-15 17:30:47 +00:00
|
|
|
// Build up all of the passes that we want to do to the module...
|
2002-01-21 07:31:50 +00:00
|
|
|
PassManager Passes;
|
2001-10-15 17:30:47 +00:00
|
|
|
|
2002-03-24 03:19:54 +00:00
|
|
|
// Decompose multi-dimensional refs into a sequence of 1D refs
|
|
|
|
Passes.add(createDecomposeMultiDimRefsPass());
|
|
|
|
|
2001-10-18 18:20:20 +00: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-02-26 21:36:53 +00:00
|
|
|
Passes.add(createLowerAllocationsPass(Target.DataLayout));
|
2001-10-18 18:20:20 +00:00
|
|
|
|
2001-10-15 17:30:47 +00:00
|
|
|
// If LLVM dumping after transformations is requested, add it to the pipeline
|
|
|
|
if (DumpAsm)
|
2002-04-08 22:05:01 +00:00
|
|
|
Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
|
2001-10-15 17:30:47 +00:00
|
|
|
|
2002-06-30 16:25:07 +00:00
|
|
|
// Strip all of the symbols from the bytecode so that it will be smaller...
|
|
|
|
Passes.add(createSymbolStrippingPass());
|
|
|
|
|
2002-02-03 23:43:19 +00:00
|
|
|
// Figure out where we are going to send the output...
|
|
|
|
std::ostream *Out = 0;
|
|
|
|
if (OutputFilename != "") { // Specified an output filename?
|
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
|
|
|
cerr << "Error opening '" << OutputFilename << "': File exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
2002-04-18 19:55:25 +00:00
|
|
|
|
|
|
|
// Make sure that the Out file gets unlink'd from the disk if we get a
|
|
|
|
// SIGINT
|
|
|
|
RemoveFileOnSignal(OutputFilename);
|
2002-02-03 23:43:19 +00:00
|
|
|
} else {
|
|
|
|
if (InputFilename == "-") {
|
|
|
|
OutputFilename = "-";
|
|
|
|
Out = &std::cout;
|
|
|
|
} else {
|
|
|
|
string OutputFilename = GetFileNameRoot(InputFilename);
|
|
|
|
OutputFilename += ".s";
|
2001-10-15 17:30:47 +00:00
|
|
|
|
2002-01-22 21:07:24 +00:00
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
2002-01-20 22:54:45 +00:00
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
|
|
|
cerr << "Error opening '" << OutputFilename << "': File exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2002-02-03 23:43:19 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
2002-02-03 23:43:19 +00:00
|
|
|
if (!Out->good()) {
|
|
|
|
cerr << "Error opening " << OutputFilename << "!\n";
|
|
|
|
delete Out;
|
|
|
|
return 1;
|
2001-10-15 17:30:47 +00:00
|
|
|
}
|
2002-04-18 19:55:25 +00:00
|
|
|
// Make sure that the Out file gets unlink'd from the disk if we get a
|
|
|
|
// SIGINT
|
|
|
|
RemoveFileOnSignal(OutputFilename);
|
2001-10-15 17:30:47 +00:00
|
|
|
}
|
|
|
|
}
|
2001-10-14 23:29:28 +00:00
|
|
|
|
2002-02-03 23:43:19 +00:00
|
|
|
Target.addPassesToEmitAssembly(Passes, *Out);
|
|
|
|
|
2002-01-21 07:31:50 +00:00
|
|
|
// Run our queue of passes all at once now, efficiently.
|
2002-06-25 16:13:24 +00:00
|
|
|
Passes.run(*M.get());
|
2001-10-18 01:31:22 +00:00
|
|
|
|
2002-02-03 23:43:19 +00:00
|
|
|
if (Out != &std::cout) delete Out;
|
|
|
|
|
2001-10-18 20:33:21 +00:00
|
|
|
return 0;
|
2001-10-14 23:29:28 +00:00
|
|
|
}
|