2002-01-22 04:30:46 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-31 05:28:11 +01:00
|
|
|
// LLVM 'GCCAS' UTILITY
|
|
|
|
//
|
|
|
|
// This utility is designed to be used by the GCC frontend for creating
|
|
|
|
// bytecode files from it's intermediate llvm assembly. The requirements for
|
|
|
|
// this utility are thus slightly different than that of the standard as util.
|
|
|
|
//
|
2002-01-22 04:30:46 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-31 05:28:11 +01:00
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2002-01-31 01:46:22 +01:00
|
|
|
#include "llvm/PassManager.h"
|
2001-10-31 05:28:11 +01:00
|
|
|
#include "llvm/Assembly/Parser.h"
|
|
|
|
#include "llvm/Transforms/CleanupGCCOutput.h"
|
2001-11-01 03:41:09 +01:00
|
|
|
#include "llvm/Transforms/LevelChange.h"
|
2001-10-31 07:36:48 +01:00
|
|
|
#include "llvm/Transforms/ConstantMerge.h"
|
2002-01-22 02:04:08 +01:00
|
|
|
#include "llvm/Transforms/ChangeAllocations.h"
|
2002-05-07 22:03:27 +02:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2002-01-22 04:30:46 +01:00
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.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-10-31 05:28:11 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
|
|
|
|
cl::Required, "");
|
|
|
|
cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
|
2002-03-21 22:21:50 +01:00
|
|
|
cl::Flag StopAtLevelRaise("stopraise", "Stop optimization before level raise",
|
|
|
|
cl::Hidden);
|
2001-10-31 05:28:11 +01:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2001-10-31 05:33:33 +01:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
|
2001-10-31 05:28:11 +01:00
|
|
|
|
|
|
|
std::auto_ptr<Module> M;
|
|
|
|
try {
|
|
|
|
// Parse the file now...
|
|
|
|
M.reset(ParseAssemblyFile(InputFilename));
|
|
|
|
} catch (const ParseException &E) {
|
|
|
|
cerr << E.getMessage() << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (M.get() == 0) {
|
|
|
|
cerr << "assembly didn't read correctly.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OutputFilename == "") { // Didn't specify an output filename?
|
2002-01-20 23:54:45 +01:00
|
|
|
std::string IFN = InputFilename;
|
2001-10-31 05:28:11 +01:00
|
|
|
int Len = IFN.length();
|
|
|
|
if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
|
2002-01-20 23:54:45 +01:00
|
|
|
OutputFilename = std::string(IFN.begin(), IFN.end()-2);
|
2001-10-31 05:28:11 +01:00
|
|
|
} else {
|
|
|
|
OutputFilename = IFN; // Append a .o to it
|
|
|
|
}
|
|
|
|
OutputFilename += ".o";
|
|
|
|
}
|
|
|
|
|
2002-01-22 04:30:46 +01:00
|
|
|
std::ofstream Out(OutputFilename.c_str(), ios::out);
|
|
|
|
if (!Out.good()) {
|
2001-10-31 05:28:11 +01:00
|
|
|
cerr << "Error opening " << OutputFilename << "!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-04-18 21:55:25 +02:00
|
|
|
// Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
|
|
|
|
RemoveFileOnSignal(OutputFilename);
|
|
|
|
|
2001-10-31 05:28:11 +01:00
|
|
|
// In addition to just parsing the input from GCC, we also want to spiff it up
|
|
|
|
// a little bit. Do this now.
|
|
|
|
//
|
2002-01-21 08:31:50 +01:00
|
|
|
PassManager Passes;
|
2002-04-10 22:33:32 +02:00
|
|
|
Passes.add(createFunctionResolvingPass()); // Resolve (...) functions
|
2002-04-29 22:11:38 +02:00
|
|
|
Passes.add(createConstantMergePass()); // Merge dup global constants
|
2002-02-26 22:47:29 +01:00
|
|
|
Passes.add(createDeadInstEliminationPass()); // Remove Dead code/vars
|
|
|
|
Passes.add(createRaiseAllocationsPass()); // call %malloc -> malloc inst
|
|
|
|
Passes.add(createCleanupGCCOutputPass()); // Fix gccisms
|
|
|
|
Passes.add(createIndVarSimplifyPass()); // Simplify indvars
|
2002-03-21 22:21:50 +01:00
|
|
|
if (!StopAtLevelRaise) {
|
|
|
|
Passes.add(createRaisePointerReferencesPass()); // Eliminate casts
|
2002-04-01 21:45:11 +02:00
|
|
|
Passes.add(createPromoteMemoryToRegister()); // Promote alloca's to regs
|
2002-05-09 00:18:20 +02:00
|
|
|
Passes.add(createReassociatePass()); // Reassociate expressions
|
2002-03-21 22:21:50 +01:00
|
|
|
Passes.add(createInstructionCombiningPass()); // Combine silly seq's
|
2002-05-06 18:52:15 +02:00
|
|
|
Passes.add(createDeadInstEliminationPass()); // Kill InstCombine remnants
|
2002-05-11 00:44:31 +02:00
|
|
|
Passes.add(createLICMPass()); // Hoist loop invariants
|
2002-04-28 03:00:15 +02:00
|
|
|
Passes.add(createGCSEPass()); // Remove common subexprs
|
2002-05-06 20:54:12 +02:00
|
|
|
Passes.add(createSCCPPass()); // Constant prop with SCCP
|
2002-05-14 18:23:14 +02:00
|
|
|
|
|
|
|
// Run instcombine after redundancy elimination to exploit opportunities
|
|
|
|
// opened up by them.
|
|
|
|
Passes.add(createInstructionCombiningPass());
|
2002-05-22 23:30:24 +02:00
|
|
|
Passes.add(createAggressiveDCEPass()); // SSA based 'Agressive DCE'
|
2002-05-21 22:05:08 +02:00
|
|
|
Passes.add(createCFGSimplificationPass()); // Merge & remove BBs
|
2002-03-21 22:21:50 +01:00
|
|
|
}
|
2002-02-26 22:47:29 +01:00
|
|
|
Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
|
2001-10-31 05:28:11 +01:00
|
|
|
|
2002-01-22 04:30:46 +01:00
|
|
|
// Run our queue of passes all at once now, efficiently.
|
2002-01-21 08:31:50 +01:00
|
|
|
Passes.run(M.get());
|
2001-10-31 05:28:11 +01:00
|
|
|
return 0;
|
|
|
|
}
|