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