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