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