blob: c59b6ebe6a421f06d7cde86377a96bb637b859a2 [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 Lattner624c3e02002-06-25 15:57:43 +000016#include "llvm/Analysis/Verifier.h"
Chris Lattner3dc67dd2002-01-22 03:30:46 +000017#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerccbb3bd2002-07-23 18:09:58 +000018#include "llvm/Target/TargetData.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000020#include "Support/Signals.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000021#include <memory>
22#include <fstream>
Anand Shuklacf17bcc2002-06-25 21:57:48 +000023using std::cerr;
Chris Lattnerecbde332001-10-31 04:28:11 +000024
Chris Lattnerccbb3bd2002-07-23 18:09:58 +000025// FIXME: This should eventually be parameterized...
26static TargetData TD("opt target");
27
Chris Lattner5ff62e92002-07-22 02:10:13 +000028static cl::opt<string>
29InputFilename(cl::Positional, cl::desc("<input llvm assembly>"), cl::Required);
30
31static cl::opt<string>
32OutputFilename("o", cl::desc("Override output filename"),
33 cl::value_desc("filename"));
34
35static cl::opt<int>
36RunNPasses("stopAfterNPasses",
37 cl::desc("Only run the first N passes of gccas"), cl::Hidden,
38 cl::value_desc("# passes"));
39
40static cl::opt<bool>
41StopAtLevelRaise("stopraise", cl::desc("Stop optimization before level raise"),
42 cl::Hidden);
43
44static cl::opt<bool>
45Verify("verify", cl::desc("Verify each pass result"));
46
Chris Lattner624c3e02002-06-25 15:57:43 +000047
48static 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
65void AddConfiguredTransformationPasses(PassManager &PM) {
66 if (Verify) PM.add(createVerifierPass());
67
68 addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions
Chris Lattnerd9d8c072002-07-23 22:04:43 +000069 addPass(PM, createDeadTypeEliminationPass()); // Eliminate dead types
Chris Lattner624c3e02002-06-25 15:57:43 +000070 addPass(PM, createConstantMergePass()); // Merge dup global constants
71 addPass(PM, createDeadInstEliminationPass()); // Remove Dead code/vars
72 addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst
Chris Lattner624c3e02002-06-25 15:57:43 +000073 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
Chris Lattnerccbb3bd2002-07-23 18:09:58 +000079 addPass(PM, createRaisePointerReferencesPass(TD));// Eliminate casts
Chris Lattner624c3e02002-06-25 15:57:43 +000080 addPass(PM, createPromoteMemoryToRegister()); // Promote alloca's to regs
Vikram S. Advef8685eb2002-07-09 19:53:09 +000081 // Disabling until this is fixed -- Vikram, 7/7/02.
82 // addPass(PM, createReassociatePass()); // Reassociate expressions
Chris Lattner624c3e02002-06-25 15:57:43 +000083 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
Chris Lattnerecbde332001-10-31 04:28:11 +000096
97int main(int argc, char **argv) {
Chris Lattner11c862c2001-10-31 04:33:33 +000098 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
Chris Lattnerecbde332001-10-31 04:28:11 +000099
Chris Lattnerecbde332001-10-31 04:28:11 +0000100 std::auto_ptr<Module> M;
101 try {
102 // Parse the file now...
103 M.reset(ParseAssemblyFile(InputFilename));
104 } catch (const ParseException &E) {
Chris Lattnerb4aef172002-06-30 16:19:14 +0000105 cerr << E.getMessage() << "\n";
Chris Lattnerecbde332001-10-31 04:28:11 +0000106 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?
Chris Lattner697954c2002-01-20 22:54:45 +0000115 std::string IFN = InputFilename;
Chris Lattnerecbde332001-10-31 04:28:11 +0000116 int Len = IFN.length();
117 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
Chris Lattner697954c2002-01-20 22:54:45 +0000118 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
Chris Lattnerecbde332001-10-31 04:28:11 +0000119 } else {
120 OutputFilename = IFN; // Append a .o to it
121 }
122 OutputFilename += ".o";
123 }
124
Anand Shuklacf17bcc2002-06-25 21:57:48 +0000125 std::ofstream Out(OutputFilename.c_str(), std::ios::out);
Chris Lattner3dc67dd2002-01-22 03:30:46 +0000126 if (!Out.good()) {
Chris Lattnerecbde332001-10-31 04:28:11 +0000127 cerr << "Error opening " << OutputFilename << "!\n";
128 return 1;
129 }
130
Chris Lattner76d12292002-04-18 19:55:25 +0000131 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
132 RemoveFileOnSignal(OutputFilename);
133
Chris Lattnerecbde332001-10-31 04:28:11 +0000134 // In addition to just parsing the input from GCC, we also want to spiff it up
135 // a little bit. Do this now.
136 //
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000137 PassManager Passes;
Chris Lattner4ad53222002-05-14 16:23:14 +0000138
Chris Lattner624c3e02002-06-25 15:57:43 +0000139 // 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));
Chris Lattnerecbde332001-10-31 04:28:11 +0000146
Chris Lattner3dc67dd2002-01-22 03:30:46 +0000147 // Run our queue of passes all at once now, efficiently.
Chris Lattner624c3e02002-06-25 15:57:43 +0000148 Passes.run(*M.get());
Chris Lattnerecbde332001-10-31 04:28:11 +0000149 return 0;
150}