Remove the -stopAfterNPasses option, which has been long obsoleted by bugpoint
[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 bytecode
5 // files from its intermediate LLVM assembly.  The requirements for this utility
6 // 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/Analysis/LoadValueNumbering.h"
13 #include "llvm/Analysis/Verifier.h"
14 #include "llvm/Assembly/Parser.h"
15 #include "llvm/Bytecode/WriteBytecodePass.h"
16 #include "llvm/Target/TargetData.h"
17 #include "llvm/Transforms/RaisePointerReferences.h"
18 #include "llvm/Transforms/IPO.h"
19 #include "llvm/Transforms/Scalar.h"
20 #include "Support/CommandLine.h"
21 #include "Support/Signals.h"
22 #include <memory>
23 #include <fstream>
24
25 namespace {
26   cl::opt<std::string>
27   InputFilename(cl::Positional,cl::desc("<input llvm assembly>"),cl::init("-"));
28
29   cl::opt<std::string> 
30   OutputFilename("o", cl::desc("Override output filename"),
31                  cl::value_desc("filename"));
32
33   cl::opt<bool>   
34   Verify("verify", cl::desc("Verify each pass result"));
35 }
36
37
38 static inline void addPass(PassManager &PM, Pass *P) {
39   // Add the pass to the pass manager...
40   PM.add(P);
41   
42   // If we are verifying all of the intermediate steps, add the verifier...
43   if (Verify) PM.add(createVerifierPass());
44 }
45
46
47 void AddConfiguredTransformationPasses(PassManager &PM) {
48   PM.add(createVerifierPass());                  // Verify that input is correct
49   addPass(PM, createFunctionResolvingPass());    // Resolve (...) functions
50   addPass(PM, createRaiseAllocationsPass());     // call %malloc -> malloc inst
51   addPass(PM, createGlobalDCEPass());            // Remove unused globals
52   addPass(PM, createPruneEHPass());              // Remove dead EH info
53   addPass(PM, createFunctionInliningPass());     // Inline small functions
54
55   addPass(PM, createInstructionCombiningPass()); // Cleanup code for raise
56   addPass(PM, createRaisePointerReferencesPass());// Recover type information
57   addPass(PM, createTailDuplicationPass());      // Simplify cfg by copying code
58   addPass(PM, createCFGSimplificationPass());    // Merge & remove BBs
59   addPass(PM, createScalarReplAggregatesPass()); // Break up aggregate allocas
60   addPass(PM, createPromoteMemoryToRegister());  // Promote alloca's to regs
61   addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
62
63
64   addPass(PM, createIndVarSimplifyPass());       // Simplify indvars
65   addPass(PM, createReassociatePass());          // Reassociate expressions
66   addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
67   addPass(PM, createCFGSimplificationPass());    // Merge & remove BBs
68   addPass(PM, createLICMPass());                 // Hoist loop invariants
69   addPass(PM, createLoadValueNumberingPass());   // GVN for load instructions
70   addPass(PM, createGCSEPass());                 // Remove common subexprs
71   addPass(PM, createSCCPPass());                 // Constant prop with SCCP
72
73   // Run instcombine after redundancy elimination to exploit opportunities
74   // opened up by them.
75   addPass(PM, createInstructionCombiningPass());
76   addPass(PM, createAggressiveDCEPass());        // SSA based 'Aggressive DCE'
77   addPass(PM, createCFGSimplificationPass());    // Merge & remove BBs
78   addPass(PM, createDeadTypeEliminationPass());  // Eliminate dead types
79   addPass(PM, createConstantMergePass());        // Merge dup global constants
80 }
81
82
83 int main(int argc, char **argv) {
84   cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
85
86   std::auto_ptr<Module> M;
87   try {
88     // Parse the file now...
89     M.reset(ParseAssemblyFile(InputFilename));
90   } catch (const ParseException &E) {
91     std::cerr << argv[0] << ": " << E.getMessage() << "\n";
92     return 1;
93   }
94
95   if (M.get() == 0) {
96     std::cerr << argv[0] << ": assembly didn't read correctly.\n";
97     return 1;
98   }
99
100   std::ostream *Out = 0;
101   if (OutputFilename == "") {   // Didn't specify an output filename?
102     if (InputFilename == "-") {
103       OutputFilename = "-";
104     } else {
105       std::string IFN = InputFilename;
106       int Len = IFN.length();
107       if (IFN[Len-2] == '.' && IFN[Len-1] == 's') {   // Source ends in .s?
108         OutputFilename = std::string(IFN.begin(), IFN.end()-2);
109       } else {
110         OutputFilename = IFN;   // Append a .o to it
111       }
112       OutputFilename += ".o";
113     }
114   }
115
116   if (OutputFilename == "-")
117     Out = &std::cout;
118   else {
119     Out = new std::ofstream(OutputFilename.c_str(), std::ios::out);
120
121     // Make sure that the Out file gets unlink'd from the disk if we get a
122     // signal
123     RemoveFileOnSignal(OutputFilename);
124   }
125
126   
127   if (!Out->good()) {
128     std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
129     return 1;
130   }
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 an appropriate TargetData instance for this module...
138   Passes.add(new TargetData("gccas", M.get()));
139
140   // Add all of the transformation passes to the pass manager to do the cleanup
141   // and optimization of the GCC output.
142   //
143   AddConfiguredTransformationPasses(Passes);
144
145   // Write bytecode to file...
146   Passes.add(new WriteBytecodePass(Out));
147
148   // Run our queue of passes all at once now, efficiently.
149   Passes.run(*M.get());
150
151   if (Out != &std::cout) delete Out;
152   return 0;
153 }