2003-10-15 23:49:57 +02:00
|
|
|
//===-- gccas.cpp - The "optimizing assembler" used by the GCC frontend ---===//
|
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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
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"
|
2001-11-27 01:03:19 +01:00
|
|
|
#include "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
|
|
|
|
2003-04-16 19:34:29 +02:00
|
|
|
cl::opt<std::string>
|
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
|
|
|
cl::value_desc("filename"));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2003-04-16 19:34:29 +02:00
|
|
|
cl::opt<bool>
|
|
|
|
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>
|
|
|
|
DisableDSE("disable-dse", cl::desc("Do not run dead store elimination"));
|
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);
|
2002-06-25 17:57:43 +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
|
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
|
|
|
|
|
|
|
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
|
|
|
|
addPass(PM, createPromoteMemoryToRegister()); // Kill useless allocas
|
2004-02-25 22:35:02 +01:00
|
|
|
addPass(PM, createGlobalConstifierPass()); // Mark read-only globals const
|
2003-11-22 20:07:47 +01:00
|
|
|
addPass(PM, createGlobalDCEPass()); // Remove unused globals
|
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
|
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
|
|
|
|
|
2002-10-31 18:13:11 +01:00
|
|
|
addPass(PM, createReassociatePass()); // Reassociate expressions
|
2002-09-06 20:41:33 +02:00
|
|
|
addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
|
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
|
2002-08-31 00:55:32 +02:00
|
|
|
addPass(PM, createLICMPass()); // Hoist loop invariants
|
2004-04-18 07:21:01 +02:00
|
|
|
addPass(PM, createInstructionCombiningPass()); // Clean up after the unroller
|
|
|
|
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());
|
2004-07-22 10:34:33 +02:00
|
|
|
if (!DisableDSE)
|
|
|
|
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) {
|
2001-10-31 05:33:33 +01:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
|
2004-08-29 21:28:55 +02:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
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) {
|
2003-04-16 19:41:08 +02:00
|
|
|
std::cerr << argv[0] << ": " << E.getMessage() << "\n";
|
2001-10-31 05:28:11 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (M.get() == 0) {
|
2003-04-16 19:41:08 +02:00
|
|
|
std::cerr << argv[0] << ": assembly didn't read correctly.\n";
|
2001-10-31 05:28:11 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2003-04-16 19:49:18 +02:00
|
|
|
|
|
|
|
std::ostream *Out = 0;
|
2001-10-31 05:28:11 +01:00
|
|
|
if (OutputFilename == "") { // Didn't specify an output filename?
|
2003-04-16 19:49:18 +02:00
|
|
|
if (InputFilename == "-") {
|
|
|
|
OutputFilename = "-";
|
2001-10-31 05:28:11 +01:00
|
|
|
} else {
|
2003-04-16 19:49:18 +02: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 05:28:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-16 19:49:18 +02:00
|
|
|
if (OutputFilename == "-")
|
|
|
|
Out = &std::cout;
|
|
|
|
else {
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str(), std::ios::out);
|
|
|
|
|
2003-10-10 19:56:49 +02:00
|
|
|
// Make sure that the Out file gets unlinked from the disk if we get a
|
2003-04-16 19:49:18 +02:00
|
|
|
// signal
|
2004-08-29 21:28:55 +02:00
|
|
|
sys::RemoveFileOnSignal(OutputFilename);
|
2003-04-16 19:49:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!Out->good()) {
|
2003-04-16 19:41:08 +02:00
|
|
|
std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
|
2001-10-31 05:28:11 +01: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 08:31:50 +01:00
|
|
|
PassManager Passes;
|
2002-06-25 17:57:43 +02:00
|
|
|
|
2003-04-24 21:13:02 +02:00
|
|
|
// Add an appropriate TargetData instance for this module...
|
|
|
|
Passes.add(new TargetData("gccas", M.get()));
|
|
|
|
|
2002-06-25 17:57:43 +02: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 04:39:46 +01:00
|
|
|
// Make sure everything is still good.
|
|
|
|
Passes.add(createVerifierPass());
|
|
|
|
|
2002-06-25 17:57:43 +02:00
|
|
|
// Write bytecode to file...
|
2003-04-16 19:49:18 +02:00
|
|
|
Passes.add(new WriteBytecodePass(Out));
|
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-06-25 17:57:43 +02:00
|
|
|
Passes.run(*M.get());
|
2003-04-16 19:49:18 +02:00
|
|
|
|
|
|
|
if (Out != &std::cout) delete Out;
|
2001-10-31 05:28:11 +01:00
|
|
|
return 0;
|
|
|
|
}
|