blob: 8b06c3248b397a5560277f64332d273912c54eeb [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 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 Lattner624c3e02002-06-25 15:57:43 +000026static cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
27 cl::Required, "");
28static cl::String OutputFilename ("o", "Override output filename");
29static cl::Int RunNPasses ("stopAfterNPasses", "Only run the first N "
30 "passes of gccas", cl::Hidden);
31static cl::Flag StopAtLevelRaise("stopraise", "Stop optimization before "
32 "level raise", cl::Hidden);
33static cl::Flag Verify ("verify", "Verify each pass result");
34
35static inline void addPass(PassManager &PM, Pass *P) {
36 static int NumPassesCreated = 0;
37
38 // If we haven't already created the number of passes that was requested...
39 if (RunNPasses == 0 || RunNPasses > NumPassesCreated) {
40 // Add the pass to the pass manager...
41 PM.add(P);
42
43 // If we are verifying all of the intermediate steps, add the verifier...
44 if (Verify) PM.add(createVerifierPass());
45
46 // Keep track of how many passes we made for -stopAfterNPasses
47 ++NumPassesCreated;
48 }
49}
50
51
52void AddConfiguredTransformationPasses(PassManager &PM) {
53 if (Verify) PM.add(createVerifierPass());
54
55 addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions
56 addPass(PM, createConstantMergePass()); // Merge dup global constants
57 addPass(PM, createDeadInstEliminationPass()); // Remove Dead code/vars
58 addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst
59 addPass(PM, createCleanupGCCOutputPass()); // Fix gccisms
60 addPass(PM, createIndVarSimplifyPass()); // Simplify indvars
61
62 // Level raise is eternally buggy/in need of enhancements. Allow
63 // transformation to stop right before it runs.
64 if (StopAtLevelRaise) return;
65
66 addPass(PM, createRaisePointerReferencesPass());// Eliminate casts
67 addPass(PM, createPromoteMemoryToRegister()); // Promote alloca's to regs
Anand Shuklacf17bcc2002-06-25 21:57:48 +000068 /* addPass(PM, createReassociatePass());*/ // Reassociate expressions
Chris Lattner624c3e02002-06-25 15:57:43 +000069 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
70 addPass(PM, createDeadInstEliminationPass()); // Kill InstCombine remnants
71 addPass(PM, createLICMPass()); // Hoist loop invariants
72 addPass(PM, createGCSEPass()); // Remove common subexprs
73 addPass(PM, createSCCPPass()); // Constant prop with SCCP
74
75 // Run instcombine after redundancy elimination to exploit opportunities
76 // opened up by them.
77 addPass(PM, createInstructionCombiningPass());
78 addPass(PM, createAggressiveDCEPass()); // SSA based 'Agressive DCE'
79 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
80}
81
Chris Lattnerecbde332001-10-31 04:28:11 +000082
83int main(int argc, char **argv) {
Chris Lattner11c862c2001-10-31 04:33:33 +000084 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
Chris Lattnerecbde332001-10-31 04:28:11 +000085
Chris Lattnerecbde332001-10-31 04:28:11 +000086 std::auto_ptr<Module> M;
87 try {
88 // Parse the file now...
89 M.reset(ParseAssemblyFile(InputFilename));
90 } catch (const ParseException &E) {
Anand Shuklacf17bcc2002-06-25 21:57:48 +000091 cerr << E.getMessage() << std::endl;
Chris Lattnerecbde332001-10-31 04:28:11 +000092 return 1;
93 }
94
95 if (M.get() == 0) {
96 cerr << "assembly didn't read correctly.\n";
97 return 1;
98 }
99
100 if (OutputFilename == "") { // Didn't specify an output filename?
Chris Lattner697954c2002-01-20 22:54:45 +0000101 std::string IFN = InputFilename;
Chris Lattnerecbde332001-10-31 04:28:11 +0000102 int Len = IFN.length();
103 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
Chris Lattner697954c2002-01-20 22:54:45 +0000104 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
Chris Lattnerecbde332001-10-31 04:28:11 +0000105 } else {
106 OutputFilename = IFN; // Append a .o to it
107 }
108 OutputFilename += ".o";
109 }
110
Anand Shuklacf17bcc2002-06-25 21:57:48 +0000111 std::ofstream Out(OutputFilename.c_str(), std::ios::out);
Chris Lattner3dc67dd2002-01-22 03:30:46 +0000112 if (!Out.good()) {
Chris Lattnerecbde332001-10-31 04:28:11 +0000113 cerr << "Error opening " << OutputFilename << "!\n";
114 return 1;
115 }
116
Chris Lattner76d12292002-04-18 19:55:25 +0000117 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
118 RemoveFileOnSignal(OutputFilename);
119
Chris Lattnerecbde332001-10-31 04:28:11 +0000120 // In addition to just parsing the input from GCC, we also want to spiff it up
121 // a little bit. Do this now.
122 //
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000123 PassManager Passes;
Chris Lattner4ad53222002-05-14 16:23:14 +0000124
Chris Lattner624c3e02002-06-25 15:57:43 +0000125 // Add all of the transformation passes to the pass manager to do the cleanup
126 // and optimization of the GCC output.
127 //
128 AddConfiguredTransformationPasses(Passes);
129
130 // Write bytecode to file...
131 Passes.add(new WriteBytecodePass(&Out));
Chris Lattnerecbde332001-10-31 04:28:11 +0000132
Chris Lattner3dc67dd2002-01-22 03:30:46 +0000133 // Run our queue of passes all at once now, efficiently.
Chris Lattner624c3e02002-06-25 15:57:43 +0000134 Passes.run(*M.get());
Chris Lattnerecbde332001-10-31 04:28:11 +0000135 return 0;
136}