blob: e0b5cd51d1af7a1ef2452d54127031c2622ba74b [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"
13#include "llvm/Transforms/CleanupGCCOutput.h"
Chris Lattner068f4872001-11-01 02:41:09 +000014#include "llvm/Transforms/LevelChange.h"
Chris Lattnerdbe05142001-10-31 06:36:48 +000015#include "llvm/Transforms/ConstantMerge.h"
Chris Lattnerd7db8632002-01-22 01:04:08 +000016#include "llvm/Transforms/ChangeAllocations.h"
Chris Lattner65f1b892002-05-07 20:03:27 +000017#include "llvm/Transforms/Scalar.h"
Chris Lattner624c3e02002-06-25 15:57:43 +000018#include "llvm/Analysis/Verifier.h"
Chris Lattner3dc67dd2002-01-22 03:30:46 +000019#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerccbb3bd2002-07-23 18:09:58 +000020#include "llvm/Target/TargetData.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000021#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000022#include "Support/Signals.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000023#include <memory>
24#include <fstream>
Anand Shuklacf17bcc2002-06-25 21:57:48 +000025using std::cerr;
Chris Lattnerecbde332001-10-31 04:28:11 +000026
Chris Lattnerccbb3bd2002-07-23 18:09:58 +000027// FIXME: This should eventually be parameterized...
28static TargetData TD("opt target");
29
Chris Lattner5ff62e92002-07-22 02:10:13 +000030static cl::opt<string>
31InputFilename(cl::Positional, cl::desc("<input llvm assembly>"), cl::Required);
32
33static cl::opt<string>
34OutputFilename("o", cl::desc("Override output filename"),
35 cl::value_desc("filename"));
36
37static cl::opt<int>
38RunNPasses("stopAfterNPasses",
39 cl::desc("Only run the first N passes of gccas"), cl::Hidden,
40 cl::value_desc("# passes"));
41
42static cl::opt<bool>
43StopAtLevelRaise("stopraise", cl::desc("Stop optimization before level raise"),
44 cl::Hidden);
45
46static cl::opt<bool>
47Verify("verify", cl::desc("Verify each pass result"));
48
Chris Lattner624c3e02002-06-25 15:57:43 +000049
50static inline void addPass(PassManager &PM, Pass *P) {
51 static int NumPassesCreated = 0;
52
53 // If we haven't already created the number of passes that was requested...
54 if (RunNPasses == 0 || RunNPasses > NumPassesCreated) {
55 // Add the pass to the pass manager...
56 PM.add(P);
57
58 // If we are verifying all of the intermediate steps, add the verifier...
59 if (Verify) PM.add(createVerifierPass());
60
61 // Keep track of how many passes we made for -stopAfterNPasses
62 ++NumPassesCreated;
63 }
64}
65
66
67void AddConfiguredTransformationPasses(PassManager &PM) {
68 if (Verify) PM.add(createVerifierPass());
69
70 addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions
71 addPass(PM, createConstantMergePass()); // Merge dup global constants
72 addPass(PM, createDeadInstEliminationPass()); // Remove Dead code/vars
73 addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst
74 addPass(PM, createCleanupGCCOutputPass()); // Fix gccisms
75 addPass(PM, createIndVarSimplifyPass()); // Simplify indvars
76
77 // Level raise is eternally buggy/in need of enhancements. Allow
78 // transformation to stop right before it runs.
79 if (StopAtLevelRaise) return;
80
Chris Lattnerccbb3bd2002-07-23 18:09:58 +000081 addPass(PM, createRaisePointerReferencesPass(TD));// Eliminate casts
Chris Lattner624c3e02002-06-25 15:57:43 +000082 addPass(PM, createPromoteMemoryToRegister()); // Promote alloca's to regs
Vikram S. Advef8685eb2002-07-09 19:53:09 +000083 // Disabling until this is fixed -- Vikram, 7/7/02.
84 // addPass(PM, createReassociatePass()); // Reassociate expressions
Chris Lattner624c3e02002-06-25 15:57:43 +000085 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
86 addPass(PM, createDeadInstEliminationPass()); // Kill InstCombine remnants
87 addPass(PM, createLICMPass()); // Hoist loop invariants
88 addPass(PM, createGCSEPass()); // Remove common subexprs
89 addPass(PM, createSCCPPass()); // Constant prop with SCCP
90
91 // Run instcombine after redundancy elimination to exploit opportunities
92 // opened up by them.
93 addPass(PM, createInstructionCombiningPass());
94 addPass(PM, createAggressiveDCEPass()); // SSA based 'Agressive DCE'
95 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
96}
97
Chris Lattnerecbde332001-10-31 04:28:11 +000098
99int main(int argc, char **argv) {
Chris Lattner11c862c2001-10-31 04:33:33 +0000100 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
Chris Lattnerecbde332001-10-31 04:28:11 +0000101
Chris Lattnerecbde332001-10-31 04:28:11 +0000102 std::auto_ptr<Module> M;
103 try {
104 // Parse the file now...
105 M.reset(ParseAssemblyFile(InputFilename));
106 } catch (const ParseException &E) {
Chris Lattnerb4aef172002-06-30 16:19:14 +0000107 cerr << E.getMessage() << "\n";
Chris Lattnerecbde332001-10-31 04:28:11 +0000108 return 1;
109 }
110
111 if (M.get() == 0) {
112 cerr << "assembly didn't read correctly.\n";
113 return 1;
114 }
115
116 if (OutputFilename == "") { // Didn't specify an output filename?
Chris Lattner697954c2002-01-20 22:54:45 +0000117 std::string IFN = InputFilename;
Chris Lattnerecbde332001-10-31 04:28:11 +0000118 int Len = IFN.length();
119 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
Chris Lattner697954c2002-01-20 22:54:45 +0000120 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
Chris Lattnerecbde332001-10-31 04:28:11 +0000121 } else {
122 OutputFilename = IFN; // Append a .o to it
123 }
124 OutputFilename += ".o";
125 }
126
Anand Shuklacf17bcc2002-06-25 21:57:48 +0000127 std::ofstream Out(OutputFilename.c_str(), std::ios::out);
Chris Lattner3dc67dd2002-01-22 03:30:46 +0000128 if (!Out.good()) {
Chris Lattnerecbde332001-10-31 04:28:11 +0000129 cerr << "Error opening " << OutputFilename << "!\n";
130 return 1;
131 }
132
Chris Lattner76d12292002-04-18 19:55:25 +0000133 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
134 RemoveFileOnSignal(OutputFilename);
135
Chris Lattnerecbde332001-10-31 04:28:11 +0000136 // In addition to just parsing the input from GCC, we also want to spiff it up
137 // a little bit. Do this now.
138 //
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000139 PassManager Passes;
Chris Lattner4ad53222002-05-14 16:23:14 +0000140
Chris Lattner624c3e02002-06-25 15:57:43 +0000141 // Add all of the transformation passes to the pass manager to do the cleanup
142 // and optimization of the GCC output.
143 //
144 AddConfiguredTransformationPasses(Passes);
145
146 // Write bytecode to file...
147 Passes.add(new WriteBytecodePass(&Out));
Chris Lattnerecbde332001-10-31 04:28:11 +0000148
Chris Lattner3dc67dd2002-01-22 03:30:46 +0000149 // Run our queue of passes all at once now, efficiently.
Chris Lattner624c3e02002-06-25 15:57:43 +0000150 Passes.run(*M.get());
Chris Lattnerecbde332001-10-31 04:28:11 +0000151 return 0;
152}