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/RaisePointerReferences.h"
18 #include "llvm/Transforms/IPO.h"
19 #include "llvm/Transforms/Scalar.h"
20 #include "Support/CommandLine.h"
21 #include "Support/Signals.h"
27 InputFilename(cl::Positional,cl::desc("<input llvm assembly>"),cl::init("-"));
30 OutputFilename("o", cl::desc("Override output filename"),
31 cl::value_desc("filename"));
34 Verify("verify", cl::desc("Verify each pass result"));
38 static inline void addPass(PassManager &PM, Pass *P) {
39 // Add the pass to the pass manager...
42 // If we are verifying all of the intermediate steps, add the verifier...
43 if (Verify) PM.add(createVerifierPass());
47 void AddConfiguredTransformationPasses(PassManager &PM) {
48 PM.add(createVerifierPass()); // Verify that input is correct
49 addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions
50 addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst
51 addPass(PM, createGlobalDCEPass()); // Remove unused globals
52 addPass(PM, createPruneEHPass()); // Remove dead EH info
53 addPass(PM, createFunctionInliningPass()); // Inline small functions
55 addPass(PM, createInstructionCombiningPass()); // Cleanup code for raise
56 addPass(PM, createRaisePointerReferencesPass());// Recover type information
57 addPass(PM, createTailDuplicationPass()); // Simplify cfg by copying code
58 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
59 addPass(PM, createScalarReplAggregatesPass()); // Break up aggregate allocas
60 addPass(PM, createPromoteMemoryToRegister()); // Promote alloca's to regs
61 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
64 addPass(PM, createIndVarSimplifyPass()); // Simplify indvars
65 addPass(PM, createReassociatePass()); // Reassociate expressions
66 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
67 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
68 addPass(PM, createLICMPass()); // Hoist loop invariants
69 addPass(PM, createLoadValueNumberingPass()); // GVN for load instructions
70 addPass(PM, createGCSEPass()); // Remove common subexprs
71 addPass(PM, createSCCPPass()); // Constant prop with SCCP
73 // Run instcombine after redundancy elimination to exploit opportunities
75 addPass(PM, createInstructionCombiningPass());
76 addPass(PM, createAggressiveDCEPass()); // SSA based 'Aggressive DCE'
77 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
78 addPass(PM, createDeadTypeEliminationPass()); // Eliminate dead types
79 addPass(PM, createConstantMergePass()); // Merge dup global constants
83 int main(int argc, char **argv) {
84 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
86 std::auto_ptr<Module> M;
88 // Parse the file now...
89 M.reset(ParseAssemblyFile(InputFilename));
90 } catch (const ParseException &E) {
91 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
96 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
100 std::ostream *Out = 0;
101 if (OutputFilename == "") { // Didn't specify an output filename?
102 if (InputFilename == "-") {
103 OutputFilename = "-";
105 std::string IFN = InputFilename;
106 int Len = IFN.length();
107 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
108 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
110 OutputFilename = IFN; // Append a .o to it
112 OutputFilename += ".o";
116 if (OutputFilename == "-")
119 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out);
121 // Make sure that the Out file gets unlink'd from the disk if we get a
123 RemoveFileOnSignal(OutputFilename);
128 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
132 // In addition to just parsing the input from GCC, we also want to spiff it up
133 // a little bit. Do this now.
137 // Add an appropriate TargetData instance for this module...
138 Passes.add(new TargetData("gccas", M.get()));
140 // Add all of the transformation passes to the pass manager to do the cleanup
141 // and optimization of the GCC output.
143 AddConfiguredTransformationPasses(Passes);
145 // Write bytecode to file...
146 Passes.add(new WriteBytecodePass(Out));
148 // Run our queue of passes all at once now, efficiently.
149 Passes.run(*M.get());
151 if (Out != &std::cout) delete Out;