2003-10-15 23:49:57 +02:00
|
|
|
//===-- gccas.cpp - The "optimizing assembler" used by the GCC frontend ---===//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-31 05:28:11 +01:00
|
|
|
//
|
2003-08-07 23:23:52 +02:00
|
|
|
// This utility is designed to be used by the GCC frontend for creating bytecode
|
|
|
|
// files from its intermediate LLVM assembly. The requirements for this utility
|
|
|
|
// are thus slightly different than that of the standard `as' util.
|
2001-10-31 05:28:11 +01:00
|
|
|
//
|
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"
|
2002-08-30 22:25:25 +02:00
|
|
|
#include "llvm/Analysis/LoadValueNumbering.h"
|
2002-06-25 17:57:43 +02:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2003-08-07 23:23:52 +02:00
|
|
|
#include "llvm/Assembly/Parser.h"
|
2002-01-22 04:30:46 +01:00
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
2002-07-23 20:09:58 +02:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2003-08-07 23:23:52 +02:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2004-05-27 07:41:36 +02:00
|
|
|
#include "llvm/System/Signals.h"
|
2001-10-31 05:28:11 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <fstream>
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2003-04-16 19:34:29 +02:00
|
|
|
namespace {
|
|
|
|
cl::opt<std::string>
|
2003-04-16 19:49:18 +02:00
|
|
|
InputFilename(cl::Positional,cl::desc("<input llvm assembly>"),cl::init("-"));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2005-04-22 02:00:37 +02:00
|
|
|
cl::opt<std::string>
|
2003-04-16 19:34:29 +02:00
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
|
|
|
cl::value_desc("filename"));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2005-04-22 02:00:37 +02:00
|
|
|
cl::opt<bool>
|
2003-04-16 19:34:29 +02:00
|
|
|
Verify("verify", cl::desc("Verify each pass result"));
|
2003-10-10 20:18:53 +02:00
|
|
|
|
|
|
|
cl::opt<bool>
|
|
|
|
DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
|
2003-12-30 04:24:27 +01:00
|
|
|
|
|
|
|
cl::opt<bool>
|
|
|
|
DisableOptimizations("disable-opt",
|
|
|
|
cl::desc("Do not run any optimization passes"));
|
2004-07-22 10:34:33 +02:00
|
|
|
|
|
|
|
cl::opt<bool>
|
2004-12-03 06:45:58 +01:00
|
|
|
StripDebug("strip-debug",
|
|
|
|
cl::desc("Strip debugger symbol info from translation unit"));
|
|
|
|
|
2005-04-22 02:00:37 +02:00
|
|
|
cl::opt<bool>
|
2004-11-08 18:37:04 +01:00
|
|
|
NoCompress("disable-compression", cl::init(false),
|
2005-01-01 23:10:32 +01:00
|
|
|
cl::desc("Don't compress the generated bytecode"));
|
2004-12-22 03:58:43 +01:00
|
|
|
|
|
|
|
cl::opt<bool> TF("traditional-format", cl::Hidden,
|
|
|
|
cl::desc("Compatibility option: ignored"));
|
2003-04-16 19:34:29 +02:00
|
|
|
}
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2002-06-25 17:57:43 +02:00
|
|
|
|
|
|
|
static inline void addPass(PassManager &PM, Pass *P) {
|
2003-08-31 23:47:24 +02:00
|
|
|
// Add the pass to the pass manager...
|
|
|
|
PM.add(P);
|
2005-04-22 02:00:37 +02:00
|
|
|
|
2003-08-31 23:47:24 +02:00
|
|
|
// If we are verifying all of the intermediate steps, add the verifier...
|
|
|
|
if (Verify) PM.add(createVerifierPass());
|
2002-06-25 17:57:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AddConfiguredTransformationPasses(PassManager &PM) {
|
2003-06-22 22:11:45 +02:00
|
|
|
PM.add(createVerifierPass()); // Verify that input is correct
|
2004-12-03 06:45:58 +01:00
|
|
|
|
2003-09-15 06:56:44 +02:00
|
|
|
addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp
|
2002-08-31 00:55:32 +02:00
|
|
|
addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions
|
2003-12-30 04:24:27 +01:00
|
|
|
|
2004-12-03 06:45:58 +01:00
|
|
|
// If the -strip-debug command line option was specified, do it.
|
|
|
|
if (StripDebug)
|
|
|
|
addPass(PM, createStripSymbolsPass(true));
|
|
|
|
|
2003-12-30 04:24:27 +01:00
|
|
|
if (DisableOptimizations) return;
|
|
|
|
|
2003-11-21 22:44:35 +01:00
|
|
|
addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst
|
2004-02-01 08:24:53 +01:00
|
|
|
addPass(PM, createCFGSimplificationPass()); // Clean up disgusting code
|
2005-03-28 04:01:12 +02:00
|
|
|
addPass(PM, createPromoteMemoryToRegisterPass());// Kill useless allocas
|
2004-10-07 06:12:02 +02:00
|
|
|
addPass(PM, createGlobalOptimizerPass()); // Optimize out global vars
|
|
|
|
addPass(PM, createGlobalDCEPass()); // Remove unused fns and globs
|
2003-10-23 20:25:57 +02:00
|
|
|
addPass(PM, createIPConstantPropagationPass());// IP Constant Propagation
|
|
|
|
addPass(PM, createDeadArgEliminationPass()); // Dead argument elimination
|
2004-02-01 08:24:53 +01:00
|
|
|
addPass(PM, createInstructionCombiningPass()); // Clean up after IPCP & DAE
|
|
|
|
addPass(PM, createCFGSimplificationPass()); // Clean up after IPCP & DAE
|
2003-10-23 20:25:57 +02:00
|
|
|
|
2003-08-31 23:45:55 +02:00
|
|
|
addPass(PM, createPruneEHPass()); // Remove dead EH info
|
2003-10-10 20:18:53 +02:00
|
|
|
|
|
|
|
if (!DisableInline)
|
|
|
|
addPass(PM, createFunctionInliningPass()); // Inline small functions
|
2005-04-27 04:22:47 +02:00
|
|
|
addPass(PM, createSimplifyLibCallsPass()); // Library Call Optimizations
|
2004-03-13 22:38:35 +01:00
|
|
|
addPass(PM, createArgumentPromotionPass()); // Scalarize uninlined fn args
|
2003-08-31 23:45:55 +02:00
|
|
|
|
2003-10-16 18:50:34 +02:00
|
|
|
addPass(PM, createRaisePointerReferencesPass());// Recover type information
|
2003-06-22 22:11:45 +02:00
|
|
|
addPass(PM, createTailDuplicationPass()); // Simplify cfg by copying code
|
|
|
|
addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
|
2003-05-30 21:24:06 +02:00
|
|
|
addPass(PM, createScalarReplAggregatesPass()); // Break up aggregate allocas
|
2003-08-31 23:45:55 +02:00
|
|
|
addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
|
2005-05-08 00:45:35 +02:00
|
|
|
addPass(PM, createCondPropagationPass()); // Propagate conditionals
|
2003-08-31 23:45:55 +02:00
|
|
|
|
2003-12-11 18:50:32 +01:00
|
|
|
addPass(PM, createTailCallEliminationPass()); // Eliminate tail calls
|
2002-09-06 20:41:33 +02:00
|
|
|
addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
|
2005-03-07 04:19:50 +01:00
|
|
|
addPass(PM, createReassociatePass()); // Reassociate expressions
|
2005-05-08 00:45:35 +02:00
|
|
|
addPass(PM, createLICMPass()); // Hoist loop invariants
|
2005-03-07 04:19:50 +01:00
|
|
|
addPass(PM, createInstructionCombiningPass()); // Clean up after LICM/reassoc
|
2004-04-18 07:21:01 +02:00
|
|
|
addPass(PM, createIndVarSimplifyPass()); // Canonicalize indvars
|
|
|
|
addPass(PM, createLoopUnrollPass()); // Unroll small loops
|
|
|
|
addPass(PM, createInstructionCombiningPass()); // Clean up after the unroller
|
2002-08-31 00:55:32 +02:00
|
|
|
addPass(PM, createLoadValueNumberingPass()); // GVN for load instructions
|
|
|
|
addPass(PM, createGCSEPass()); // Remove common subexprs
|
|
|
|
addPass(PM, createSCCPPass()); // Constant prop with SCCP
|
2002-06-25 17:57:43 +02:00
|
|
|
|
|
|
|
// Run instcombine after redundancy elimination to exploit opportunities
|
|
|
|
// opened up by them.
|
|
|
|
addPass(PM, createInstructionCombiningPass());
|
2005-05-08 00:45:35 +02:00
|
|
|
addPass(PM, createCondPropagationPass()); // Propagate conditionals
|
|
|
|
|
2004-12-03 06:45:58 +01:00
|
|
|
addPass(PM, createDeadStoreEliminationPass()); // Delete dead stores
|
2003-06-22 22:11:45 +02:00
|
|
|
addPass(PM, createAggressiveDCEPass()); // SSA based 'Aggressive DCE'
|
2002-09-06 20:41:33 +02:00
|
|
|
addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
|
2003-08-31 23:45:55 +02:00
|
|
|
addPass(PM, createDeadTypeEliminationPass()); // Eliminate dead types
|
|
|
|
addPass(PM, createConstantMergePass()); // Merge dup global constants
|
2002-06-25 17:57:43 +02:00
|
|
|
}
|
|
|
|
|
2001-10-31 05:28:11 +01:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
try {
|
2005-04-22 02:00:37 +02:00
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
2004-12-30 06:36:08 +01:00
|
|
|
" llvm .s -> .o assembler for GCC\n");
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
|
|
|
|
std::auto_ptr<Module> M;
|
|
|
|
try {
|
|
|
|
// Parse the file now...
|
|
|
|
M.reset(ParseAssemblyFile(InputFilename));
|
|
|
|
} catch (const ParseException &E) {
|
|
|
|
std::cerr << argv[0] << ": " << E.getMessage() << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2001-10-31 05:28:11 +01:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
if (M.get() == 0) {
|
|
|
|
std::cerr << argv[0] << ": assembly didn't read correctly.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2003-04-16 19:49:18 +02:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
std::ostream *Out = 0;
|
|
|
|
if (OutputFilename == "") { // Didn't specify an output filename?
|
|
|
|
if (InputFilename == "-") {
|
|
|
|
OutputFilename = "-";
|
2003-04-16 19:49:18 +02:00
|
|
|
} else {
|
2004-12-30 06:36:08 +01:00
|
|
|
std::string IFN = InputFilename;
|
|
|
|
int Len = IFN.length();
|
|
|
|
if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
|
|
|
|
OutputFilename = std::string(IFN.begin(), IFN.end()-2);
|
|
|
|
} else {
|
|
|
|
OutputFilename = IFN; // Append a .o to it
|
|
|
|
}
|
|
|
|
OutputFilename += ".o";
|
2003-04-16 19:49:18 +02:00
|
|
|
}
|
2001-10-31 05:28:11 +01:00
|
|
|
}
|
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
if (OutputFilename == "-")
|
2005-01-22 18:36:17 +01:00
|
|
|
// FIXME: cout is not binary!
|
2004-12-30 06:36:08 +01:00
|
|
|
Out = &std::cout;
|
|
|
|
else {
|
2005-01-22 18:36:17 +01:00
|
|
|
std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
|
|
|
|
std::ios::binary;
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str(), io_mode);
|
2003-04-16 19:49:18 +02:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
// Make sure that the Out file gets unlinked from the disk if we get a
|
|
|
|
// signal
|
|
|
|
sys::RemoveFileOnSignal(sys::Path(OutputFilename));
|
|
|
|
}
|
2003-04-16 19:49:18 +02:00
|
|
|
|
2005-04-22 02:00:37 +02:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
if (!Out->good()) {
|
|
|
|
std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2001-10-31 05:28:11 +01:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
// In addition to just parsing the input from GCC, we also want to spiff it up
|
|
|
|
// a little bit. Do this now.
|
|
|
|
//
|
|
|
|
PassManager Passes;
|
2002-06-25 17:57:43 +02:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
// Add an appropriate TargetData instance for this module...
|
|
|
|
Passes.add(new TargetData("gccas", M.get()));
|
2003-04-24 21:13:02 +02:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
// Add all of the transformation passes to the pass manager to do the cleanup
|
|
|
|
// and optimization of the GCC output.
|
|
|
|
//
|
|
|
|
AddConfiguredTransformationPasses(Passes);
|
2002-06-25 17:57:43 +02:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
// Make sure everything is still good.
|
|
|
|
Passes.add(createVerifierPass());
|
2004-01-14 04:39:46 +01:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
// Write bytecode to file...
|
|
|
|
Passes.add(new WriteBytecodePass(Out,false,!NoCompress));
|
2001-10-31 05:28:11 +01:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
// Run our queue of passes all at once now, efficiently.
|
|
|
|
Passes.run(*M.get());
|
2003-04-16 19:49:18 +02:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
if (Out != &std::cout) delete Out;
|
|
|
|
return 0;
|
|
|
|
} catch (const std::string& msg) {
|
|
|
|
std::cerr << argv[0] << ": " << msg << "\n";
|
|
|
|
} catch (...) {
|
|
|
|
std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
|
|
|
|
}
|
|
|
|
return 1;
|
2001-10-31 05:28:11 +01:00
|
|
|
}
|