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