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