blob: f66432899acfa22d480d0e5846c79336d16470f9 [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
41static cl::opt<bool>
42StopAtLevelRaise("stopraise", cl::desc("Stop optimization before level raise"),
43 cl::Hidden);
44
45static cl::opt<bool>
46Verify("verify", cl::desc("Verify each pass result"));
47
Chris Lattner624c3e02002-06-25 15:57:43 +000048
49static inline void addPass(PassManager &PM, Pass *P) {
50 static int NumPassesCreated = 0;
51
52 // If we haven't already created the number of passes that was requested...
53 if (RunNPasses == 0 || RunNPasses > NumPassesCreated) {
54 // Add the pass to the pass manager...
55 PM.add(P);
56
57 // If we are verifying all of the intermediate steps, add the verifier...
58 if (Verify) PM.add(createVerifierPass());
59
60 // Keep track of how many passes we made for -stopAfterNPasses
61 ++NumPassesCreated;
Chris Lattner374a0952002-08-17 22:40:03 +000062 } else {
63 delete P; // We don't want this pass to run, just delete it now
Chris Lattner624c3e02002-06-25 15:57:43 +000064 }
65}
66
67
68void AddConfiguredTransformationPasses(PassManager &PM) {
69 if (Verify) PM.add(createVerifierPass());
70
Chris Lattnerc5394832002-08-30 22:55:32 +000071 addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions
72 addPass(PM, createGlobalDCEPass()); // Kill unused uinit g-vars
73 addPass(PM, createDeadTypeEliminationPass()); // Eliminate dead types
74 addPass(PM, createConstantMergePass()); // Merge dup global constants
75 addPass(PM, createVerifierPass()); // Verify that input is correct
76 addPass(PM, createDeadInstEliminationPass()); // Remove Dead code/vars
77 addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst
78 addPass(PM, createIndVarSimplifyPass()); // Simplify indvars
Chris Lattner624c3e02002-06-25 15:57:43 +000079
80 // Level raise is eternally buggy/in need of enhancements. Allow
81 // transformation to stop right before it runs.
82 if (StopAtLevelRaise) return;
83
Chris Lattnerc5394832002-08-30 22:55:32 +000084 addPass(PM, createRaisePointerReferencesPass(TD));// Recover type information
85 addPass(PM, createPromoteMemoryToRegister()); // Promote alloca's to regs
Vikram S. Advef8685eb2002-07-09 19:53:09 +000086 // Disabling until this is fixed -- Vikram, 7/7/02.
Chris Lattnerc5394832002-08-30 22:55:32 +000087 // addPass(PM, createReassociatePass()); // Reassociate expressions
88 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
Chris Lattner4dd7d3e2002-09-06 18:41:33 +000089 addPass(PM, createCorrelatedExpressionEliminationPass());
90 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
91 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
Chris Lattnerc5394832002-08-30 22:55:32 +000092 addPass(PM, createLICMPass()); // Hoist loop invariants
93 addPass(PM, createLoadValueNumberingPass()); // GVN for load instructions
94 addPass(PM, createGCSEPass()); // Remove common subexprs
95 addPass(PM, createSCCPPass()); // Constant prop with SCCP
Chris Lattner624c3e02002-06-25 15:57:43 +000096
97 // Run instcombine after redundancy elimination to exploit opportunities
98 // opened up by them.
99 addPass(PM, createInstructionCombiningPass());
Chris Lattner4dd7d3e2002-09-06 18:41:33 +0000100 addPass(PM, createAggressiveDCEPass()); // SSA based 'Agressive DCE'
101 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
Chris Lattner624c3e02002-06-25 15:57:43 +0000102}
103
Chris Lattnerecbde332001-10-31 04:28:11 +0000104
105int main(int argc, char **argv) {
Chris Lattner11c862c2001-10-31 04:33:33 +0000106 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
Chris Lattnerecbde332001-10-31 04:28:11 +0000107
Chris Lattnerecbde332001-10-31 04:28:11 +0000108 std::auto_ptr<Module> M;
109 try {
110 // Parse the file now...
111 M.reset(ParseAssemblyFile(InputFilename));
112 } catch (const ParseException &E) {
Chris Lattnerd4c7f272002-07-30 21:43:25 +0000113 cerr << argv[0] << ": " << E.getMessage() << "\n";
Chris Lattnerecbde332001-10-31 04:28:11 +0000114 return 1;
115 }
116
117 if (M.get() == 0) {
Chris Lattnerd4c7f272002-07-30 21:43:25 +0000118 cerr << argv[0] << ": assembly didn't read correctly.\n";
Chris Lattnerecbde332001-10-31 04:28:11 +0000119 return 1;
120 }
121
122 if (OutputFilename == "") { // Didn't specify an output filename?
Chris Lattner697954c2002-01-20 22:54:45 +0000123 std::string IFN = InputFilename;
Chris Lattnerecbde332001-10-31 04:28:11 +0000124 int Len = IFN.length();
125 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
Chris Lattner697954c2002-01-20 22:54:45 +0000126 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
Chris Lattnerecbde332001-10-31 04:28:11 +0000127 } else {
128 OutputFilename = IFN; // Append a .o to it
129 }
130 OutputFilename += ".o";
131 }
132
Anand Shuklacf17bcc2002-06-25 21:57:48 +0000133 std::ofstream Out(OutputFilename.c_str(), std::ios::out);
Chris Lattner3dc67dd2002-01-22 03:30:46 +0000134 if (!Out.good()) {
Chris Lattnerd4c7f272002-07-30 21:43:25 +0000135 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattnerecbde332001-10-31 04:28:11 +0000136 return 1;
137 }
138
Chris Lattner76d12292002-04-18 19:55:25 +0000139 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
140 RemoveFileOnSignal(OutputFilename);
141
Chris Lattnerecbde332001-10-31 04:28:11 +0000142 // In addition to just parsing the input from GCC, we also want to spiff it up
143 // a little bit. Do this now.
144 //
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000145 PassManager Passes;
Chris Lattner4ad53222002-05-14 16:23:14 +0000146
Chris Lattner624c3e02002-06-25 15:57:43 +0000147 // Add all of the transformation passes to the pass manager to do the cleanup
148 // and optimization of the GCC output.
149 //
150 AddConfiguredTransformationPasses(Passes);
151
152 // Write bytecode to file...
153 Passes.add(new WriteBytecodePass(&Out));
Chris Lattnerecbde332001-10-31 04:28:11 +0000154
Chris Lattner3dc67dd2002-01-22 03:30:46 +0000155 // Run our queue of passes all at once now, efficiently.
Chris Lattner624c3e02002-06-25 15:57:43 +0000156 Passes.run(*M.get());
Chris Lattnerecbde332001-10-31 04:28:11 +0000157 return 0;
158}