1 //===----------------------------------------------------------------------===//
2 // LLVM 'GCCAS' UTILITY
4 // This utility is designed to be used by the GCC frontend for creating bytecode
5 // files from its intermediate LLVM assembly. The requirements for this utility
6 // are thus slightly different than that of the standard `as' util.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Module.h"
11 #include "llvm/PassManager.h"
12 #include "llvm/Analysis/LoadValueNumbering.h"
13 #include "llvm/Analysis/Verifier.h"
14 #include "llvm/Assembly/Parser.h"
15 #include "llvm/Bytecode/WriteBytecodePass.h"
16 #include "llvm/Target/TargetData.h"
17 #include "llvm/Transforms/IPO.h"
18 #include "llvm/Transforms/Scalar.h"
19 #include "Support/CommandLine.h"
20 #include "Support/Signals.h"
26 InputFilename(cl::Positional,cl::desc("<input llvm assembly>"),cl::init("-"));
29 OutputFilename("o", cl::desc("Override output filename"),
30 cl::value_desc("filename"));
33 Verify("verify", cl::desc("Verify each pass result"));
37 static inline void addPass(PassManager &PM, Pass *P) {
38 // Add the pass to the pass manager...
41 // If we are verifying all of the intermediate steps, add the verifier...
42 if (Verify) PM.add(createVerifierPass());
46 void AddConfiguredTransformationPasses(PassManager &PM) {
47 PM.add(createVerifierPass()); // Verify that input is correct
48 addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions
49 addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst
50 addPass(PM, createGlobalDCEPass()); // Remove unused globals
51 addPass(PM, createPruneEHPass()); // Remove dead EH info
52 addPass(PM, createFunctionInliningPass()); // Inline small functions
54 addPass(PM, createInstructionCombiningPass()); // Cleanup code for raise
55 addPass(PM, createRaisePointerReferencesPass());// Recover type information
56 addPass(PM, createTailDuplicationPass()); // Simplify cfg by copying code
57 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
58 addPass(PM, createScalarReplAggregatesPass()); // Break up aggregate allocas
59 addPass(PM, createPromoteMemoryToRegister()); // Promote alloca's to regs
60 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
63 addPass(PM, createIndVarSimplifyPass()); // Simplify indvars
64 addPass(PM, createReassociatePass()); // Reassociate expressions
65 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
66 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
67 addPass(PM, createLICMPass()); // Hoist loop invariants
68 addPass(PM, createLoadValueNumberingPass()); // GVN for load instructions
69 addPass(PM, createGCSEPass()); // Remove common subexprs
70 addPass(PM, createSCCPPass()); // Constant prop with SCCP
72 // Run instcombine after redundancy elimination to exploit opportunities
74 addPass(PM, createInstructionCombiningPass());
75 addPass(PM, createAggressiveDCEPass()); // SSA based 'Aggressive DCE'
76 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
77 addPass(PM, createDeadTypeEliminationPass()); // Eliminate dead types
78 addPass(PM, createConstantMergePass()); // Merge dup global constants
82 int main(int argc, char **argv) {
83 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
85 std::auto_ptr<Module> M;
87 // Parse the file now...
88 M.reset(ParseAssemblyFile(InputFilename));
89 } catch (const ParseException &E) {
90 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
95 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
99 std::ostream *Out = 0;
100 if (OutputFilename == "") { // Didn't specify an output filename?
101 if (InputFilename == "-") {
102 OutputFilename = "-";
104 std::string IFN = InputFilename;
105 int Len = IFN.length();
106 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
107 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
109 OutputFilename = IFN; // Append a .o to it
111 OutputFilename += ".o";
115 if (OutputFilename == "-")
118 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out);
120 // Make sure that the Out file gets unlink'd from the disk if we get a
122 RemoveFileOnSignal(OutputFilename);
127 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
131 // In addition to just parsing the input from GCC, we also want to spiff it up
132 // a little bit. Do this now.
136 // Add an appropriate TargetData instance for this module...
137 Passes.add(new TargetData("gccas", M.get()));
139 // Add all of the transformation passes to the pass manager to do the cleanup
140 // and optimization of the GCC output.
142 AddConfiguredTransformationPasses(Passes);
144 // Write bytecode to file...
145 Passes.add(new WriteBytecodePass(Out));
147 // Run our queue of passes all at once now, efficiently.
148 Passes.run(*M.get());
150 if (Out != &std::cout) delete Out;