Run GCSE as part of gccas.
[oota-llvm.git] / tools / gccas / gccas.cpp
1 //===----------------------------------------------------------------------===//
2 // LLVM 'GCCAS' UTILITY 
3 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Module.h"
11 #include "llvm/PassManager.h"
12 #include "llvm/Assembly/Parser.h"
13 #include "llvm/Transforms/CleanupGCCOutput.h"
14 #include "llvm/Transforms/LevelChange.h"
15 #include "llvm/Transforms/ConstantMerge.h"
16 #include "llvm/Transforms/ChangeAllocations.h"
17 #include "llvm/Transforms/Scalar/DCE.h"
18 #include "llvm/Transforms/Scalar/GCSE.h"
19 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
20 #include "llvm/Transforms/Scalar/InstructionCombining.h"
21 #include "llvm/Transforms/Scalar/PromoteMemoryToRegister.h"
22 #include "llvm/Bytecode/WriteBytecodePass.h"
23 #include "Support/CommandLine.h"
24 #include "Support/Signals.h"
25 #include <memory>
26 #include <fstream>
27 #include <string>
28
29 cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
30                           cl::Required, "");
31 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
32 cl::Flag   StopAtLevelRaise("stopraise", "Stop optimization before level raise",
33                             cl::Hidden);
34
35 int main(int argc, char **argv) {
36   cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
37
38   std::auto_ptr<Module> M;
39   try {
40     // Parse the file now...
41     M.reset(ParseAssemblyFile(InputFilename));
42   } catch (const ParseException &E) {
43     cerr << E.getMessage() << endl;
44     return 1;
45   }
46
47   if (M.get() == 0) {
48     cerr << "assembly didn't read correctly.\n";
49     return 1;
50   }
51   
52   if (OutputFilename == "") {   // Didn't specify an output filename?
53     std::string IFN = InputFilename;
54     int Len = IFN.length();
55     if (IFN[Len-2] == '.' && IFN[Len-1] == 's') {   // Source ends in .s?
56       OutputFilename = std::string(IFN.begin(), IFN.end()-2);
57     } else {
58       OutputFilename = IFN;   // Append a .o to it
59     }
60     OutputFilename += ".o";
61   }
62
63   std::ofstream Out(OutputFilename.c_str(), ios::out);
64   if (!Out.good()) {
65     cerr << "Error opening " << OutputFilename << "!\n";
66     return 1;
67   }
68
69   // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
70   RemoveFileOnSignal(OutputFilename);
71
72   // In addition to just parsing the input from GCC, we also want to spiff it up
73   // a little bit.  Do this now.
74   //
75   PassManager Passes;
76   Passes.add(createFunctionResolvingPass());      // Resolve (...) functions
77   Passes.add(createDeadInstEliminationPass());    // Remove Dead code/vars
78   Passes.add(createRaiseAllocationsPass());       // call %malloc -> malloc inst
79   Passes.add(createCleanupGCCOutputPass());       // Fix gccisms
80   Passes.add(createIndVarSimplifyPass());         // Simplify indvars
81   if (!StopAtLevelRaise) {
82     Passes.add(createRaisePointerReferencesPass()); // Eliminate casts
83     Passes.add(createPromoteMemoryToRegister());    // Promote alloca's to regs
84     Passes.add(createConstantMergePass());          // Merge dup global consts
85     Passes.add(createInstructionCombiningPass());   // Combine silly seq's
86     Passes.add(createDeadCodeEliminationPass());    // Remove Dead code/vars
87     Passes.add(createGCSEPass());                   // Remove common subexprs
88   }
89   Passes.add(new WriteBytecodePass(&Out));        // Write bytecode to file...
90
91   // Run our queue of passes all at once now, efficiently.
92   Passes.run(M.get());
93   return 0;
94 }
95