blob: fb5ed091b466b01511f3092771a8951fdc2dee13 [file] [log] [blame]
Chris Lattner3dc67dd2002-01-22 03:30:46 +00001//===----------------------------------------------------------------------===//
Chris Lattnerecbde332001-10-31 04:28:11 +00002// 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//
Chris Lattner3dc67dd2002-01-22 03:30:46 +00008//===----------------------------------------------------------------------===//
Chris Lattnerecbde332001-10-31 04:28:11 +00009
10#include "llvm/Module.h"
Chris Lattner0f3bfff2002-01-31 00:46:22 +000011#include "llvm/PassManager.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000012#include "llvm/Assembly/Parser.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000013#include "llvm/Transforms/RaisePointerReferences.h"
14#include "llvm/Transforms/IPO.h"
Chris Lattner65f1b892002-05-07 20:03:27 +000015#include "llvm/Transforms/Scalar.h"
Chris Lattner1b7fbef2002-08-30 20:25:25 +000016#include "llvm/Analysis/LoadValueNumbering.h"
Chris Lattner624c3e02002-06-25 15:57:43 +000017#include "llvm/Analysis/Verifier.h"
Chris Lattner3dc67dd2002-01-22 03:30:46 +000018#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerccbb3bd2002-07-23 18:09:58 +000019#include "llvm/Target/TargetData.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000020#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000021#include "Support/Signals.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000022#include <memory>
23#include <fstream>
Anand Shuklacf17bcc2002-06-25 21:57:48 +000024using std::cerr;
Chris Lattnerecbde332001-10-31 04:28:11 +000025
Chris Lattnerccbb3bd2002-07-23 18:09:58 +000026// FIXME: This should eventually be parameterized...
27static TargetData TD("opt target");
28
Chris Lattnerc7a09852002-07-25 16:31:09 +000029static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000030InputFilename(cl::Positional, cl::desc("<input llvm assembly>"), cl::Required);
31
Chris Lattnerc7a09852002-07-25 16:31:09 +000032static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000033OutputFilename("o", cl::desc("Override output filename"),
34 cl::value_desc("filename"));
35
36static cl::opt<int>
37RunNPasses("stopAfterNPasses",
38 cl::desc("Only run the first N passes of gccas"), cl::Hidden,
39 cl::value_desc("# passes"));
40
Chris Lattner5ff62e92002-07-22 02:10:13 +000041static cl::opt<bool>
42Verify("verify", cl::desc("Verify each pass result"));
43
Chris Lattner624c3e02002-06-25 15:57:43 +000044
45static inline void addPass(PassManager &PM, Pass *P) {
46 static int NumPassesCreated = 0;
47
48 // If we haven't already created the number of passes that was requested...
49 if (RunNPasses == 0 || RunNPasses > NumPassesCreated) {
50 // Add the pass to the pass manager...
51 PM.add(P);
52
53 // If we are verifying all of the intermediate steps, add the verifier...
54 if (Verify) PM.add(createVerifierPass());
55
56 // Keep track of how many passes we made for -stopAfterNPasses
57 ++NumPassesCreated;
Chris Lattner374a0952002-08-17 22:40:03 +000058 } else {
59 delete P; // We don't want this pass to run, just delete it now
Chris Lattner624c3e02002-06-25 15:57:43 +000060 }
61}
62
63
64void AddConfiguredTransformationPasses(PassManager &PM) {
65 if (Verify) PM.add(createVerifierPass());
66
Chris Lattnerc5394832002-08-30 22:55:32 +000067 addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions
68 addPass(PM, createGlobalDCEPass()); // Kill unused uinit g-vars
69 addPass(PM, createDeadTypeEliminationPass()); // Eliminate dead types
70 addPass(PM, createConstantMergePass()); // Merge dup global constants
71 addPass(PM, createVerifierPass()); // Verify that input is correct
72 addPass(PM, createDeadInstEliminationPass()); // Remove Dead code/vars
73 addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst
74 addPass(PM, createIndVarSimplifyPass()); // Simplify indvars
Chris Lattnerc5394832002-08-30 22:55:32 +000075 addPass(PM, createRaisePointerReferencesPass(TD));// Recover type information
Chris Lattnercc377df2002-09-22 18:50:22 +000076 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
Chris Lattnerc5394832002-08-30 22:55:32 +000077 addPass(PM, createPromoteMemoryToRegister()); // Promote alloca's to regs
Chris Lattner52af6302002-10-31 17:13:11 +000078 addPass(PM, createReassociatePass()); // Reassociate expressions
Vikram S. Adve02a74cc2002-11-03 12:41:50 +000079 //addPass(PM, createCorrelatedExpressionEliminationPass());// Kill corr branches
Chris Lattner4dd7d3e2002-09-06 18:41:33 +000080 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
81 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
Chris Lattnerc5394832002-08-30 22:55:32 +000082 addPass(PM, createLICMPass()); // Hoist loop invariants
83 addPass(PM, createLoadValueNumberingPass()); // GVN for load instructions
84 addPass(PM, createGCSEPass()); // Remove common subexprs
85 addPass(PM, createSCCPPass()); // Constant prop with SCCP
Chris Lattner624c3e02002-06-25 15:57:43 +000086
87 // Run instcombine after redundancy elimination to exploit opportunities
88 // opened up by them.
89 addPass(PM, createInstructionCombiningPass());
Chris Lattner4dd7d3e2002-09-06 18:41:33 +000090 addPass(PM, createAggressiveDCEPass()); // SSA based 'Agressive DCE'
91 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
Chris Lattner624c3e02002-06-25 15:57:43 +000092}
93
Chris Lattnerecbde332001-10-31 04:28:11 +000094
95int main(int argc, char **argv) {
Chris Lattner11c862c2001-10-31 04:33:33 +000096 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
Chris Lattnerecbde332001-10-31 04:28:11 +000097
Chris Lattnerecbde332001-10-31 04:28:11 +000098 std::auto_ptr<Module> M;
99 try {
100 // Parse the file now...
101 M.reset(ParseAssemblyFile(InputFilename));
102 } catch (const ParseException &E) {
Chris Lattnerd4c7f272002-07-30 21:43:25 +0000103 cerr << argv[0] << ": " << E.getMessage() << "\n";
Chris Lattnerecbde332001-10-31 04:28:11 +0000104 return 1;
105 }
106
107 if (M.get() == 0) {
Chris Lattnerd4c7f272002-07-30 21:43:25 +0000108 cerr << argv[0] << ": assembly didn't read correctly.\n";
Chris Lattnerecbde332001-10-31 04:28:11 +0000109 return 1;
110 }
111
112 if (OutputFilename == "") { // Didn't specify an output filename?
Chris Lattner697954c2002-01-20 22:54:45 +0000113 std::string IFN = InputFilename;
Chris Lattnerecbde332001-10-31 04:28:11 +0000114 int Len = IFN.length();
115 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
Chris Lattner697954c2002-01-20 22:54:45 +0000116 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
Chris Lattnerecbde332001-10-31 04:28:11 +0000117 } else {
118 OutputFilename = IFN; // Append a .o to it
119 }
120 OutputFilename += ".o";
121 }
122
Anand Shuklacf17bcc2002-06-25 21:57:48 +0000123 std::ofstream Out(OutputFilename.c_str(), std::ios::out);
Chris Lattner3dc67dd2002-01-22 03:30:46 +0000124 if (!Out.good()) {
Chris Lattnerd4c7f272002-07-30 21:43:25 +0000125 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattnerecbde332001-10-31 04:28:11 +0000126 return 1;
127 }
128
Chris Lattner76d12292002-04-18 19:55:25 +0000129 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
130 RemoveFileOnSignal(OutputFilename);
131
Chris Lattnerecbde332001-10-31 04:28:11 +0000132 // In addition to just parsing the input from GCC, we also want to spiff it up
133 // a little bit. Do this now.
134 //
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000135 PassManager Passes;
Chris Lattner4ad53222002-05-14 16:23:14 +0000136
Chris Lattner624c3e02002-06-25 15:57:43 +0000137 // Add all of the transformation passes to the pass manager to do the cleanup
138 // and optimization of the GCC output.
139 //
140 AddConfiguredTransformationPasses(Passes);
141
142 // Write bytecode to file...
143 Passes.add(new WriteBytecodePass(&Out));
Chris Lattnerecbde332001-10-31 04:28:11 +0000144
Chris Lattner3dc67dd2002-01-22 03:30:46 +0000145 // Run our queue of passes all at once now, efficiently.
Chris Lattner624c3e02002-06-25 15:57:43 +0000146 Passes.run(*M.get());
Chris Lattnerecbde332001-10-31 04:28:11 +0000147 return 0;
148}