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"
26 // FIXME: This should eventually be parameterized...
27 TargetData TD("gccas target");
30 InputFilename(cl::Positional, cl::desc("<input llvm assembly>"),cl::Required);
33 OutputFilename("o", cl::desc("Override output filename"),
34 cl::value_desc("filename"));
37 RunNPasses("stopAfterNPasses",
38 cl::desc("Only run the first N passes of gccas"), cl::Hidden,
39 cl::value_desc("# passes"));
42 Verify("verify", cl::desc("Verify each pass result"));
45 cl::opt<std::string> // Be compatible with what GCC expects
46 QOption("Q", cl::desc("Compatibility option (ignored)"),
47 cl::Hidden, cl::Prefix);
49 PrintVersion("V", cl::desc("Print GCCAS version number"), cl::Hidden);
53 static inline void addPass(PassManager &PM, Pass *P) {
54 static int NumPassesCreated = 0;
56 // If we haven't already created the number of passes that was requested...
57 if (RunNPasses == 0 || RunNPasses > NumPassesCreated) {
58 // Add the pass to the pass manager...
61 // If we are verifying all of the intermediate steps, add the verifier...
62 if (Verify) PM.add(createVerifierPass());
64 // Keep track of how many passes we made for -stopAfterNPasses
67 delete P; // We don't want this pass to run, just delete it now
72 void AddConfiguredTransformationPasses(PassManager &PM) {
73 if (Verify) PM.add(createVerifierPass());
75 addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions
76 addPass(PM, createGlobalDCEPass()); // Kill unused uinit g-vars
77 addPass(PM, createDeadTypeEliminationPass()); // Eliminate dead types
78 addPass(PM, createConstantMergePass()); // Merge dup global constants
79 addPass(PM, createVerifierPass()); // Verify that input is correct
80 addPass(PM, createDeadInstEliminationPass()); // Remove Dead code/vars
81 addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst
82 addPass(PM, createIndVarSimplifyPass()); // Simplify indvars
83 addPass(PM, createRaisePointerReferencesPass(TD));// Recover type information
84 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
85 addPass(PM, createPromoteMemoryToRegister()); // Promote alloca's to regs
86 addPass(PM, createReassociatePass()); // Reassociate expressions
87 //addPass(PM, createCorrelatedExpressionEliminationPass());// Kill corr branches
88 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
89 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
90 addPass(PM, createLICMPass()); // Hoist loop invariants
91 addPass(PM, createLoadValueNumberingPass()); // GVN for load instructions
92 addPass(PM, createGCSEPass()); // Remove common subexprs
93 addPass(PM, createSCCPPass()); // Constant prop with SCCP
95 // Run instcombine after redundancy elimination to exploit opportunities
97 addPass(PM, createInstructionCombiningPass());
98 addPass(PM, createAggressiveDCEPass()); // SSA based 'Agressive DCE'
99 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
103 int main(int argc, char **argv) {
104 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
106 std::auto_ptr<Module> M;
108 // Parse the file now...
109 M.reset(ParseAssemblyFile(InputFilename));
110 } catch (const ParseException &E) {
111 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
116 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
120 if (OutputFilename == "") { // Didn't specify an output filename?
121 std::string IFN = InputFilename;
122 int Len = IFN.length();
123 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
124 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
126 OutputFilename = IFN; // Append a .o to it
128 OutputFilename += ".o";
131 std::ofstream Out(OutputFilename.c_str(), std::ios::out);
133 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
137 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
138 RemoveFileOnSignal(OutputFilename);
141 std::cerr << "LLVM GCCAS version xx\n"; /* For GNU compatibility */
143 // In addition to just parsing the input from GCC, we also want to spiff it up
144 // a little bit. Do this now.
148 // Add all of the transformation passes to the pass manager to do the cleanup
149 // and optimization of the GCC output.
151 AddConfiguredTransformationPasses(Passes);
153 // Write bytecode to file...
154 Passes.add(new WriteBytecodePass(&Out));
156 // Run our queue of passes all at once now, efficiently.
157 Passes.run(*M.get());