Rename LowerAllocations.h to ChangeAllocations.h since it now contains the
[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/Assembly/Parser.h"
12 #include "llvm/Transforms/CleanupGCCOutput.h"
13 #include "llvm/Transforms/LevelChange.h"
14 #include "llvm/Transforms/ConstantMerge.h"
15 #include "llvm/Transforms/ChangeAllocations.h"
16 #include "llvm/Transforms/Scalar/DCE.h"
17 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
18 #include "llvm/Transforms/Scalar/InstructionCombining.h"
19 #include "llvm/Bytecode/Writer.h"
20 #include "Support/CommandLine.h"
21 #include <memory>
22 #include <fstream>
23 #include <string>
24
25 cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
26                           cl::Required, "");
27 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
28
29 int main(int argc, char **argv) {
30   cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
31
32   ostream *Out = 0;
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   Out = new std::ofstream(OutputFilename.c_str(), ios::out);
59   if (!Out->good()) {
60     cerr << "Error opening " << OutputFilename << "!\n";
61     return 1;
62   }
63
64   // In addition to just parsing the input from GCC, we also want to spiff it up
65   // a little bit.  Do this now.
66   //
67   PassManager Passes;
68   Passes.add(new DeadCodeElimination());       // Remove Dead code/vars
69   Passes.add(new RaiseAllocations());          // call %malloc -> malloc inst
70   Passes.add(new CleanupGCCOutput());          // Fix gccisms
71   Passes.add(new InductionVariableSimplify()); // Simplify indvars
72   Passes.add(new RaisePointerReferences());    // Eliminate casts
73   Passes.add(new ConstantMerge());             // Merge dup global consts
74   Passes.add(new InstructionCombining());      // Combine silly seq's
75   Passes.add(new DeadCodeElimination());       // Remove Dead code/vars
76
77   // Run our queue of passes all at once now, efficiently.  This form of
78   // runAllPasses frees the Pass objects after runAllPasses completes.
79   //
80   Passes.run(M.get());
81
82   WriteBytecodeToFile(M.get(), *Out);
83   return 0;
84 }
85