2003-10-15 21:49:57 +00:00
|
|
|
//===-- gccas.cpp - The "optimizing assembler" used by the GCC frontend ---===//
|
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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
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"
|
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-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
|
|
|
|
2003-04-16 17:34:29 +00:00
|
|
|
cl::opt<std::string>
|
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
|
|
|
cl::value_desc("filename"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
2003-04-16 17:34:29 +00:00
|
|
|
cl::opt<bool>
|
|
|
|
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"));
|
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);
|
2002-06-25 15:57:43 +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
|
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
|
|
|
|
|
|
|
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
|
|
|
|
addPass(PM, createPromoteMemoryToRegister()); // Kill useless allocas
|
2003-11-22 19:07:47 +00:00
|
|
|
addPass(PM, createGlobalDCEPass()); // Remove unused globals
|
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
|
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
|
|
|
|
|
2002-10-31 17:13:11 +00:00
|
|
|
addPass(PM, createReassociatePass()); // Reassociate expressions
|
2002-09-06 18:41:33 +00:00
|
|
|
addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
|
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
|
2002-08-30 22:55:32 +00:00
|
|
|
addPass(PM, createLICMPass()); // Hoist loop invariants
|
|
|
|
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());
|
2003-09-11 16:34:07 +00:00
|
|
|
addPass(PM, createIndVarSimplifyPass()); // Canonicalize indvars
|
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) {
|
2001-10-31 04:33:33 +00:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
|
2004-02-19 20:32:39 +00:00
|
|
|
PrintStackTraceOnErrorSignal();
|
2001-10-31 04:28:11 +00:00
|
|
|
|
|
|
|
std::auto_ptr<Module> M;
|
|
|
|
try {
|
|
|
|
// Parse the file now...
|
|
|
|
M.reset(ParseAssemblyFile(InputFilename));
|
|
|
|
} catch (const ParseException &E) {
|
2003-04-16 17:41:08 +00:00
|
|
|
std::cerr << argv[0] << ": " << E.getMessage() << "\n";
|
2001-10-31 04:28:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (M.get() == 0) {
|
2003-04-16 17:41:08 +00:00
|
|
|
std::cerr << argv[0] << ": assembly didn't read correctly.\n";
|
2001-10-31 04:28:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2003-04-16 17:49:18 +00:00
|
|
|
|
|
|
|
std::ostream *Out = 0;
|
2001-10-31 04:28:11 +00:00
|
|
|
if (OutputFilename == "") { // Didn't specify an output filename?
|
2003-04-16 17:49:18 +00:00
|
|
|
if (InputFilename == "-") {
|
|
|
|
OutputFilename = "-";
|
2001-10-31 04:28:11 +00:00
|
|
|
} else {
|
2003-04-16 17:49:18 +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";
|
2001-10-31 04:28:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-16 17:49:18 +00:00
|
|
|
if (OutputFilename == "-")
|
|
|
|
Out = &std::cout;
|
|
|
|
else {
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str(), std::ios::out);
|
|
|
|
|
2003-10-10 17:56:49 +00:00
|
|
|
// Make sure that the Out file gets unlinked from the disk if we get a
|
2003-04-16 17:49:18 +00:00
|
|
|
// signal
|
|
|
|
RemoveFileOnSignal(OutputFilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!Out->good()) {
|
2003-04-16 17:41:08 +00:00
|
|
|
std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
|
2001-10-31 04:28:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 07:31:50 +00:00
|
|
|
PassManager Passes;
|
2002-06-25 15:57:43 +00:00
|
|
|
|
2003-04-24 19:13:02 +00:00
|
|
|
// Add an appropriate TargetData instance for this module...
|
|
|
|
Passes.add(new TargetData("gccas", M.get()));
|
|
|
|
|
2002-06-25 15:57:43 +00:00
|
|
|
// Add all of the transformation passes to the pass manager to do the cleanup
|
|
|
|
// and optimization of the GCC output.
|
|
|
|
//
|
|
|
|
AddConfiguredTransformationPasses(Passes);
|
|
|
|
|
2004-01-14 03:39:46 +00:00
|
|
|
// Make sure everything is still good.
|
|
|
|
Passes.add(createVerifierPass());
|
|
|
|
|
2002-06-25 15:57:43 +00:00
|
|
|
// Write bytecode to file...
|
2003-04-16 17:49:18 +00:00
|
|
|
Passes.add(new WriteBytecodePass(Out));
|
2001-10-31 04:28:11 +00:00
|
|
|
|
2002-01-22 03:30:46 +00:00
|
|
|
// Run our queue of passes all at once now, efficiently.
|
2002-06-25 15:57:43 +00:00
|
|
|
Passes.run(*M.get());
|
2003-04-16 17:49:18 +00:00
|
|
|
|
|
|
|
if (Out != &std::cout) delete Out;
|
2001-10-31 04:28:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|