2002-01-22 11:30:46 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-31 12:28:11 +08: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 11:30:46 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-31 12:28:11 +08:00
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2002-01-31 08:46:22 +08:00
|
|
|
#include "llvm/PassManager.h"
|
2001-10-31 12:28:11 +08:00
|
|
|
#include "llvm/Assembly/Parser.h"
|
|
|
|
#include "llvm/Transforms/CleanupGCCOutput.h"
|
2001-11-01 10:41:09 +08:00
|
|
|
#include "llvm/Transforms/LevelChange.h"
|
2001-10-31 14:36:48 +08:00
|
|
|
#include "llvm/Transforms/ConstantMerge.h"
|
2002-01-22 09:04:08 +08:00
|
|
|
#include "llvm/Transforms/ChangeAllocations.h"
|
2002-05-08 04:03:27 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2002-01-22 11:30:46 +08:00
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
2001-11-27 08:03:19 +08:00
|
|
|
#include "Support/CommandLine.h"
|
2002-04-19 03:55:25 +08:00
|
|
|
#include "Support/Signals.h"
|
2001-10-31 12:28:11 +08: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-22 05:21:50 +08:00
|
|
|
cl::Flag StopAtLevelRaise("stopraise", "Stop optimization before level raise",
|
|
|
|
cl::Hidden);
|
2001-10-31 12:28:11 +08:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2001-10-31 12:33:33 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
|
2001-10-31 12:28:11 +08: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-21 06:54:45 +08:00
|
|
|
std::string IFN = InputFilename;
|
2001-10-31 12:28:11 +08:00
|
|
|
int Len = IFN.length();
|
|
|
|
if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
|
2002-01-21 06:54:45 +08:00
|
|
|
OutputFilename = std::string(IFN.begin(), IFN.end()-2);
|
2001-10-31 12:28:11 +08:00
|
|
|
} else {
|
|
|
|
OutputFilename = IFN; // Append a .o to it
|
|
|
|
}
|
|
|
|
OutputFilename += ".o";
|
|
|
|
}
|
|
|
|
|
2002-01-22 11:30:46 +08:00
|
|
|
std::ofstream Out(OutputFilename.c_str(), ios::out);
|
|
|
|
if (!Out.good()) {
|
2001-10-31 12:28:11 +08:00
|
|
|
cerr << "Error opening " << OutputFilename << "!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-04-19 03:55:25 +08:00
|
|
|
// Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
|
|
|
|
RemoveFileOnSignal(OutputFilename);
|
|
|
|
|
2001-10-31 12:28:11 +08: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 15:31:50 +08:00
|
|
|
PassManager Passes;
|
2002-04-11 04:33:32 +08:00
|
|
|
Passes.add(createFunctionResolvingPass()); // Resolve (...) functions
|
2002-04-30 04:11:38 +08:00
|
|
|
Passes.add(createConstantMergePass()); // Merge dup global constants
|
2002-02-27 05:47:29 +08: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-22 05:21:50 +08:00
|
|
|
if (!StopAtLevelRaise) {
|
|
|
|
Passes.add(createRaisePointerReferencesPass()); // Eliminate casts
|
2002-04-02 03:45:11 +08:00
|
|
|
Passes.add(createPromoteMemoryToRegister()); // Promote alloca's to regs
|
2002-05-09 06:18:20 +08:00
|
|
|
Passes.add(createReassociatePass()); // Reassociate expressions
|
2002-03-22 05:21:50 +08:00
|
|
|
Passes.add(createInstructionCombiningPass()); // Combine silly seq's
|
2002-05-07 00:52:15 +08:00
|
|
|
Passes.add(createDeadInstEliminationPass()); // Kill InstCombine remnants
|
2002-05-11 06:44:31 +08:00
|
|
|
Passes.add(createLICMPass()); // Hoist loop invariants
|
2002-04-28 09:00:15 +08:00
|
|
|
Passes.add(createGCSEPass()); // Remove common subexprs
|
2002-05-07 02:54:12 +08:00
|
|
|
Passes.add(createSCCPPass()); // Constant prop with SCCP
|
2002-05-15 00:23:14 +08:00
|
|
|
|
|
|
|
// Run instcombine after redundancy elimination to exploit opportunities
|
|
|
|
// opened up by them.
|
|
|
|
Passes.add(createInstructionCombiningPass());
|
2002-05-23 05:30:24 +08:00
|
|
|
Passes.add(createAggressiveDCEPass()); // SSA based 'Agressive DCE'
|
2002-05-22 04:05:08 +08:00
|
|
|
Passes.add(createCFGSimplificationPass()); // Merge & remove BBs
|
2002-03-22 05:21:50 +08:00
|
|
|
}
|
2002-02-27 05:47:29 +08:00
|
|
|
Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
|
2001-10-31 12:28:11 +08:00
|
|
|
|
2002-01-22 11:30:46 +08:00
|
|
|
// Run our queue of passes all at once now, efficiently.
|
2002-01-21 15:31:50 +08:00
|
|
|
Passes.run(M.get());
|
2001-10-31 12:28:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|