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