- GCCAS now uses load value #ing for GCSE
[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 StopAtLevelRaise("stopraise", cl::desc("Stop optimization before level raise"),
43                  cl::Hidden);
44
45 static cl::opt<bool>   
46 Verify("verify", cl::desc("Verify each pass result"));
47
48
49 static inline void addPass(PassManager &PM, Pass *P) {
50   static int NumPassesCreated = 0;
51   
52   // If we haven't already created the number of passes that was requested...
53   if (RunNPasses == 0 || RunNPasses > NumPassesCreated) {
54     // Add the pass to the pass manager...
55     PM.add(P);
56
57     // If we are verifying all of the intermediate steps, add the verifier...
58     if (Verify) PM.add(createVerifierPass());
59
60     // Keep track of how many passes we made for -stopAfterNPasses
61     ++NumPassesCreated;
62   } else {
63     delete P;             // We don't want this pass to run, just delete it now
64   }
65 }
66
67
68 void AddConfiguredTransformationPasses(PassManager &PM) {
69   if (Verify) PM.add(createVerifierPass());
70
71   addPass(PM, createFunctionResolvingPass());     // Resolve (...) functions
72   addPass(PM, createGlobalDCEPass());             // Kill unused uinit g-vars
73   addPass(PM, createDeadTypeEliminationPass());   // Eliminate dead types
74   addPass(PM, createConstantMergePass());         // Merge dup global constants
75   addPass(PM, createDeadInstEliminationPass());   // Remove Dead code/vars
76   addPass(PM, createRaiseAllocationsPass());      // call %malloc -> malloc inst
77   addPass(PM, createIndVarSimplifyPass());        // Simplify indvars
78
79   // Level raise is eternally buggy/in need of enhancements.  Allow
80   // transformation to stop right before it runs.
81   if (StopAtLevelRaise) return;
82
83   addPass(PM, createRaisePointerReferencesPass(TD));// Eliminate casts
84   addPass(PM, createPromoteMemoryToRegister());   // Promote alloca's to regs
85   // Disabling until this is fixed -- Vikram, 7/7/02.
86   // addPass(PM, createReassociatePass());           // Reassociate expressions
87   addPass(PM, createInstructionCombiningPass());  // Combine silly seq's
88   addPass(PM, createDeadInstEliminationPass());   // Kill InstCombine remnants
89   addPass(PM, createLICMPass());                  // Hoist loop invariants
90   addPass(PM, createLoadValueNumberingPass());    // GVN for load instructions
91   addPass(PM, createGCSEPass());                  // Remove common subexprs
92   addPass(PM, createSCCPPass());                  // Constant prop with SCCP
93
94   // Run instcombine after redundancy elimination to exploit opportunities
95   // opened up by them.
96   addPass(PM, createInstructionCombiningPass());
97   addPass(PM, createAggressiveDCEPass());          // SSA based 'Agressive DCE'
98   addPass(PM, createCFGSimplificationPass());      // Merge & remove BBs
99 }
100
101
102 int main(int argc, char **argv) {
103   cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
104
105   std::auto_ptr<Module> M;
106   try {
107     // Parse the file now...
108     M.reset(ParseAssemblyFile(InputFilename));
109   } catch (const ParseException &E) {
110     cerr << argv[0] << ": " << E.getMessage() << "\n";
111     return 1;
112   }
113
114   if (M.get() == 0) {
115     cerr << argv[0] << ": assembly didn't read correctly.\n";
116     return 1;
117   }
118   
119   if (OutputFilename == "") {   // Didn't specify an output filename?
120     std::string IFN = InputFilename;
121     int Len = IFN.length();
122     if (IFN[Len-2] == '.' && IFN[Len-1] == 's') {   // Source ends in .s?
123       OutputFilename = std::string(IFN.begin(), IFN.end()-2);
124     } else {
125       OutputFilename = IFN;   // Append a .o to it
126     }
127     OutputFilename += ".o";
128   }
129
130   std::ofstream Out(OutputFilename.c_str(), std::ios::out);
131   if (!Out.good()) {
132     cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
133     return 1;
134   }
135
136   // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
137   RemoveFileOnSignal(OutputFilename);
138
139   // In addition to just parsing the input from GCC, we also want to spiff it up
140   // a little bit.  Do this now.
141   //
142   PassManager Passes;
143
144   // Add all of the transformation passes to the pass manager to do the cleanup
145   // and optimization of the GCC output.
146   //
147   AddConfiguredTransformationPasses(Passes);
148
149   // Write bytecode to file...
150   Passes.add(new WriteBytecodePass(&Out));
151
152   // Run our queue of passes all at once now, efficiently.
153   Passes.run(*M.get());
154   return 0;
155 }