Disabling reassociate pass until it is fixed.
[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/Analysis/Verifier.h"
19 #include "llvm/Bytecode/WriteBytecodePass.h"
20 #include "Support/CommandLine.h"
21 #include "Support/Signals.h"
22 #include <memory>
23 #include <fstream>
24 using std::cerr;
25
26 static cl::String InputFilename   ("", "Parse <arg> file, compile to bytecode",
27                                    cl::Required, "");
28 static cl::String OutputFilename  ("o", "Override output filename");
29 static cl::Int    RunNPasses      ("stopAfterNPasses", "Only run the first N "
30                                    "passes of gccas", cl::Hidden);
31 static cl::Flag   StopAtLevelRaise("stopraise", "Stop optimization before "
32                                    "level raise", cl::Hidden);
33 static cl::Flag   Verify          ("verify", "Verify each pass result");
34
35 static inline void addPass(PassManager &PM, Pass *P) {
36   static int NumPassesCreated = 0;
37   
38   // If we haven't already created the number of passes that was requested...
39   if (RunNPasses == 0 || RunNPasses > NumPassesCreated) {
40     // Add the pass to the pass manager...
41     PM.add(P);
42
43     // If we are verifying all of the intermediate steps, add the verifier...
44     if (Verify) PM.add(createVerifierPass());
45
46     // Keep track of how many passes we made for -stopAfterNPasses
47     ++NumPassesCreated;
48   }
49 }
50
51
52 void AddConfiguredTransformationPasses(PassManager &PM) {
53   if (Verify) PM.add(createVerifierPass());
54
55   addPass(PM, createFunctionResolvingPass());     // Resolve (...) functions
56   addPass(PM, createConstantMergePass());         // Merge dup global constants
57   addPass(PM, createDeadInstEliminationPass());   // Remove Dead code/vars
58   addPass(PM, createRaiseAllocationsPass());      // call %malloc -> malloc inst
59   addPass(PM, createCleanupGCCOutputPass());      // Fix gccisms
60   addPass(PM, createIndVarSimplifyPass());        // Simplify indvars
61
62   // Level raise is eternally buggy/in need of enhancements.  Allow
63   // transformation to stop right before it runs.
64   if (StopAtLevelRaise) return;
65
66   addPass(PM, createRaisePointerReferencesPass());// Eliminate casts
67   addPass(PM, createPromoteMemoryToRegister());   // Promote alloca's to regs
68   // Disabling until this is fixed -- Vikram, 7/7/02.
69   // addPass(PM, createReassociatePass());           // Reassociate expressions
70   addPass(PM, createInstructionCombiningPass());  // Combine silly seq's
71   addPass(PM, createDeadInstEliminationPass());   // Kill InstCombine remnants
72   addPass(PM, createLICMPass());                  // Hoist loop invariants
73   addPass(PM, createGCSEPass());                  // Remove common subexprs
74   addPass(PM, createSCCPPass());                  // Constant prop with SCCP
75
76   // Run instcombine after redundancy elimination to exploit opportunities
77   // opened up by them.
78   addPass(PM, createInstructionCombiningPass());
79   addPass(PM, createAggressiveDCEPass());          // SSA based 'Agressive DCE'
80   addPass(PM, createCFGSimplificationPass());      // Merge & remove BBs
81 }
82
83
84 int main(int argc, char **argv) {
85   cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
86
87   std::auto_ptr<Module> M;
88   try {
89     // Parse the file now...
90     M.reset(ParseAssemblyFile(InputFilename));
91   } catch (const ParseException &E) {
92     cerr << E.getMessage() << "\n";
93     return 1;
94   }
95
96   if (M.get() == 0) {
97     cerr << "assembly didn't read correctly.\n";
98     return 1;
99   }
100   
101   if (OutputFilename == "") {   // Didn't specify an output filename?
102     std::string IFN = InputFilename;
103     int Len = IFN.length();
104     if (IFN[Len-2] == '.' && IFN[Len-1] == 's') {   // Source ends in .s?
105       OutputFilename = std::string(IFN.begin(), IFN.end()-2);
106     } else {
107       OutputFilename = IFN;   // Append a .o to it
108     }
109     OutputFilename += ".o";
110   }
111
112   std::ofstream Out(OutputFilename.c_str(), std::ios::out);
113   if (!Out.good()) {
114     cerr << "Error opening " << OutputFilename << "!\n";
115     return 1;
116   }
117
118   // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
119   RemoveFileOnSignal(OutputFilename);
120
121   // In addition to just parsing the input from GCC, we also want to spiff it up
122   // a little bit.  Do this now.
123   //
124   PassManager Passes;
125
126   // Add all of the transformation passes to the pass manager to do the cleanup
127   // and optimization of the GCC output.
128   //
129   AddConfiguredTransformationPasses(Passes);
130
131   // Write bytecode to file...
132   Passes.add(new WriteBytecodePass(&Out));
133
134   // Run our queue of passes all at once now, efficiently.
135   Passes.run(*M.get());
136   return 0;
137 }