1 //===----------------------------------------------------------------------===//
2 // LLVM 'GCCAS' UTILITY
4 // This utility is designed to be used by the GCC frontend for creating
5 // bytecode files from it's intermediate llvm assembly. The requirements for
6 // this utility 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/Assembly/Parser.h"
13 #include "llvm/Transforms/RaisePointerReferences.h"
14 #include "llvm/Transforms/IPO.h"
15 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/Analysis/LoadValueNumbering.h"
17 #include "llvm/Analysis/Verifier.h"
18 #include "llvm/Bytecode/WriteBytecodePass.h"
19 #include "llvm/Target/TargetData.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 RunNPasses("stopAfterNPasses",
35 cl::desc("Only run the first N passes of gccas"), cl::Hidden,
36 cl::value_desc("# passes"));
39 Verify("verify", cl::desc("Verify each pass result"));
43 static inline void addPass(PassManager &PM, Pass *P) {
44 static int NumPassesCreated = 0;
46 // If we haven't already created the number of passes that was requested...
47 if (RunNPasses == 0 || RunNPasses > NumPassesCreated) {
48 // Add the pass to the pass manager...
51 // If we are verifying all of the intermediate steps, add the verifier...
52 if (Verify) PM.add(createVerifierPass());
54 // Keep track of how many passes we made for -stopAfterNPasses
57 delete P; // We don't want this pass to run, just delete it now
62 void AddConfiguredTransformationPasses(PassManager &PM) {
63 if (Verify) PM.add(createVerifierPass());
65 addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions
66 addPass(PM, createGlobalDCEPass()); // Kill unused uinit g-vars
67 addPass(PM, createDeadTypeEliminationPass()); // Eliminate dead types
68 addPass(PM, createConstantMergePass()); // Merge dup global constants
69 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
70 addPass(PM, createVerifierPass()); // Verify that input is correct
71 addPass(PM, createDeadInstEliminationPass()); // Remove Dead code/vars
72 addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst
73 addPass(PM, createInstructionCombiningPass()); // Cleanup code for raise
74 addPass(PM, createRaisePointerReferencesPass());// Recover type information
75 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
76 addPass(PM, createScalarReplAggregatesPass()); // Break up aggregate allocas
77 addPass(PM, createPromoteMemoryToRegister()); // Promote alloca's to regs
78 addPass(PM, createIndVarSimplifyPass()); // Simplify indvars
79 addPass(PM, createReassociatePass()); // Reassociate expressions
80 //addPass(PM, createCorrelatedExpressionEliminationPass());// Kill corr branches
81 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
82 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
83 addPass(PM, createLICMPass()); // Hoist loop invariants
84 addPass(PM, createLoadValueNumberingPass()); // GVN for load instructions
85 addPass(PM, createGCSEPass()); // Remove common subexprs
86 addPass(PM, createSCCPPass()); // Constant prop with SCCP
88 // Run instcombine after redundancy elimination to exploit opportunities
90 addPass(PM, createInstructionCombiningPass());
91 addPass(PM, createAggressiveDCEPass()); // SSA based 'Agressive DCE'
92 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
96 int main(int argc, char **argv) {
97 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
99 std::auto_ptr<Module> M;
101 // Parse the file now...
102 M.reset(ParseAssemblyFile(InputFilename));
103 } catch (const ParseException &E) {
104 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
109 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
113 std::ostream *Out = 0;
114 if (OutputFilename == "") { // Didn't specify an output filename?
115 if (InputFilename == "-") {
116 OutputFilename = "-";
118 std::string IFN = InputFilename;
119 int Len = IFN.length();
120 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
121 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
123 OutputFilename = IFN; // Append a .o to it
125 OutputFilename += ".o";
129 if (OutputFilename == "-")
132 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out);
134 // Make sure that the Out file gets unlink'd from the disk if we get a
136 RemoveFileOnSignal(OutputFilename);
141 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
145 // In addition to just parsing the input from GCC, we also want to spiff it up
146 // a little bit. Do this now.
150 // Add an appropriate TargetData instance for this module...
151 Passes.add(new TargetData("gccas", M.get()));
153 // Add all of the transformation passes to the pass manager to do the cleanup
154 // and optimization of the GCC output.
156 AddConfiguredTransformationPasses(Passes);
158 // Write bytecode to file...
159 Passes.add(new WriteBytecodePass(Out));
161 // Run our queue of passes all at once now, efficiently.
162 Passes.run(*M.get());
164 if (Out != &std::cout) delete Out;