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