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