*** empty log message ***
[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/Verifier.h"
17 #include "llvm/Bytecode/WriteBytecodePass.h"
18 #include "llvm/Target/TargetData.h"
19 #include "Support/CommandLine.h"
20 #include "Support/Signals.h"
21 #include <memory>
22 #include <fstream>
23 using std::cerr;
24
25 // FIXME: This should eventually be parameterized...
26 static TargetData TD("opt target");
27
28 static cl::opt<std::string>
29 InputFilename(cl::Positional, cl::desc("<input llvm assembly>"), cl::Required);
30
31 static cl::opt<std::string> 
32 OutputFilename("o", cl::desc("Override output filename"),
33                cl::value_desc("filename"));
34
35 static cl::opt<int>
36 RunNPasses("stopAfterNPasses",
37            cl::desc("Only run the first N passes of gccas"), cl::Hidden,
38            cl::value_desc("# passes"));
39
40 static cl::opt<bool> 
41 StopAtLevelRaise("stopraise", cl::desc("Stop optimization before level raise"),
42                  cl::Hidden);
43
44 static cl::opt<bool>   
45 Verify("verify", cl::desc("Verify each pass result"));
46
47
48 static inline void addPass(PassManager &PM, Pass *P) {
49   static int NumPassesCreated = 0;
50   
51   // If we haven't already created the number of passes that was requested...
52   if (RunNPasses == 0 || RunNPasses > NumPassesCreated) {
53     // Add the pass to the pass manager...
54     PM.add(P);
55
56     // If we are verifying all of the intermediate steps, add the verifier...
57     if (Verify) PM.add(createVerifierPass());
58
59     // Keep track of how many passes we made for -stopAfterNPasses
60     ++NumPassesCreated;
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, createDeadTypeEliminationPass());   // Eliminate dead types
70   addPass(PM, createConstantMergePass());         // Merge dup global constants
71   addPass(PM, createDeadInstEliminationPass());   // Remove Dead code/vars
72   addPass(PM, createRaiseAllocationsPass());      // call %malloc -> malloc inst
73   addPass(PM, createIndVarSimplifyPass());        // Simplify indvars
74
75   // Level raise is eternally buggy/in need of enhancements.  Allow
76   // transformation to stop right before it runs.
77   if (StopAtLevelRaise) return;
78
79   addPass(PM, createRaisePointerReferencesPass(TD));// Eliminate casts
80   addPass(PM, createPromoteMemoryToRegister());   // Promote alloca's to regs
81   // Disabling until this is fixed -- Vikram, 7/7/02.
82   // addPass(PM, createReassociatePass());           // Reassociate expressions
83   addPass(PM, createInstructionCombiningPass());  // Combine silly seq's
84   addPass(PM, createDeadInstEliminationPass());   // Kill InstCombine remnants
85   addPass(PM, createLICMPass());                  // Hoist loop invariants
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     cerr << E.getMessage() << "\n";
106     return 1;
107   }
108
109   if (M.get() == 0) {
110     cerr << "assembly didn't read correctly.\n";
111     return 1;
112   }
113   
114   if (OutputFilename == "") {   // Didn't specify an output filename?
115     std::string IFN = InputFilename;
116     int Len = IFN.length();
117     if (IFN[Len-2] == '.' && IFN[Len-1] == 's') {   // Source ends in .s?
118       OutputFilename = std::string(IFN.begin(), IFN.end()-2);
119     } else {
120       OutputFilename = IFN;   // Append a .o to it
121     }
122     OutputFilename += ".o";
123   }
124
125   std::ofstream Out(OutputFilename.c_str(), std::ios::out);
126   if (!Out.good()) {
127     cerr << "Error opening " << OutputFilename << "!\n";
128     return 1;
129   }
130
131   // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
132   RemoveFileOnSignal(OutputFilename);
133
134   // In addition to just parsing the input from GCC, we also want to spiff it up
135   // a little bit.  Do this now.
136   //
137   PassManager Passes;
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   return 0;
150 }