blob: 273e071ad2a4d86102e60da6bf8b473c3d764cb3 [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>
Chris Lattnerecbde332001-10-31 04:28:11 +000024
Chris Lattnerf2956fc2003-04-16 17:34:29 +000025namespace {
Chris Lattnerf2956fc2003-04-16 17:34:29 +000026 cl::opt<std::string>
Chris Lattner8c7b0552003-04-16 17:49:18 +000027 InputFilename(cl::Positional,cl::desc("<input llvm assembly>"),cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000028
Chris Lattnerf2956fc2003-04-16 17:34:29 +000029 cl::opt<std::string>
30 OutputFilename("o", cl::desc("Override output filename"),
31 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000032
Chris Lattnerf2956fc2003-04-16 17:34:29 +000033 cl::opt<int>
34 RunNPasses("stopAfterNPasses",
35 cl::desc("Only run the first N passes of gccas"), cl::Hidden,
36 cl::value_desc("# passes"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000037
Chris Lattnerf2956fc2003-04-16 17:34:29 +000038 cl::opt<bool>
39 Verify("verify", cl::desc("Verify each pass result"));
Chris Lattnerf2956fc2003-04-16 17:34:29 +000040}
Chris Lattner5ff62e92002-07-22 02:10:13 +000041
Chris Lattner624c3e02002-06-25 15:57:43 +000042
43static inline void addPass(PassManager &PM, Pass *P) {
44 static int NumPassesCreated = 0;
45
46 // If we haven't already created the number of passes that was requested...
47 if (RunNPasses == 0 || RunNPasses > NumPassesCreated) {
48 // Add the pass to the pass manager...
49 PM.add(P);
50
51 // If we are verifying all of the intermediate steps, add the verifier...
52 if (Verify) PM.add(createVerifierPass());
53
54 // Keep track of how many passes we made for -stopAfterNPasses
55 ++NumPassesCreated;
Chris Lattner374a0952002-08-17 22:40:03 +000056 } else {
57 delete P; // We don't want this pass to run, just delete it now
Chris Lattner624c3e02002-06-25 15:57:43 +000058 }
59}
60
61
62void AddConfiguredTransformationPasses(PassManager &PM) {
63 if (Verify) PM.add(createVerifierPass());
64
Chris Lattnerc5394832002-08-30 22:55:32 +000065 addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions
66 addPass(PM, createGlobalDCEPass()); // Kill unused uinit g-vars
67 addPass(PM, createDeadTypeEliminationPass()); // Eliminate dead types
68 addPass(PM, createConstantMergePass()); // Merge dup global constants
Chris Lattnera4dd4e22003-04-23 20:40:42 +000069 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
Chris Lattner9c3b55e2003-04-24 19:13:02 +000070 addPass(PM, createVerifierPass()); // Verify that input is correct
Chris Lattnerc5394832002-08-30 22:55:32 +000071 addPass(PM, createDeadInstEliminationPass()); // Remove Dead code/vars
72 addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst
73 addPass(PM, createIndVarSimplifyPass()); // Simplify indvars
Chris Lattner961f7b42003-04-24 18:26:03 +000074 addPass(PM, createRaisePointerReferencesPass());// Recover type information
Chris Lattnercc377df2002-09-22 18:50:22 +000075 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
Chris Lattnerc5394832002-08-30 22:55:32 +000076 addPass(PM, createPromoteMemoryToRegister()); // Promote alloca's to regs
Chris Lattner52af6302002-10-31 17:13:11 +000077 addPass(PM, createReassociatePass()); // Reassociate expressions
Vikram S. Adve02a74cc2002-11-03 12:41:50 +000078 //addPass(PM, createCorrelatedExpressionEliminationPass());// Kill corr branches
Chris Lattner4dd7d3e2002-09-06 18:41:33 +000079 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
80 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
Chris Lattnerc5394832002-08-30 22:55:32 +000081 addPass(PM, createLICMPass()); // Hoist loop invariants
82 addPass(PM, createLoadValueNumberingPass()); // GVN for load instructions
83 addPass(PM, createGCSEPass()); // Remove common subexprs
84 addPass(PM, createSCCPPass()); // Constant prop with SCCP
Chris Lattner624c3e02002-06-25 15:57:43 +000085
86 // Run instcombine after redundancy elimination to exploit opportunities
87 // opened up by them.
88 addPass(PM, createInstructionCombiningPass());
Chris Lattner4dd7d3e2002-09-06 18:41:33 +000089 addPass(PM, createAggressiveDCEPass()); // SSA based 'Agressive DCE'
90 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
Chris Lattner624c3e02002-06-25 15:57:43 +000091}
92
Chris Lattnerecbde332001-10-31 04:28:11 +000093
94int main(int argc, char **argv) {
Chris Lattner11c862c2001-10-31 04:33:33 +000095 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
Chris Lattnerecbde332001-10-31 04:28:11 +000096
Chris Lattnerecbde332001-10-31 04:28:11 +000097 std::auto_ptr<Module> M;
98 try {
99 // Parse the file now...
100 M.reset(ParseAssemblyFile(InputFilename));
101 } catch (const ParseException &E) {
Chris Lattner2c1d2f22003-04-16 17:41:08 +0000102 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
Chris Lattnerecbde332001-10-31 04:28:11 +0000103 return 1;
104 }
105
106 if (M.get() == 0) {
Chris Lattner2c1d2f22003-04-16 17:41:08 +0000107 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
Chris Lattnerecbde332001-10-31 04:28:11 +0000108 return 1;
109 }
Chris Lattner8c7b0552003-04-16 17:49:18 +0000110
111 std::ostream *Out = 0;
Chris Lattnerecbde332001-10-31 04:28:11 +0000112 if (OutputFilename == "") { // Didn't specify an output filename?
Chris Lattner8c7b0552003-04-16 17:49:18 +0000113 if (InputFilename == "-") {
114 OutputFilename = "-";
Chris Lattnerecbde332001-10-31 04:28:11 +0000115 } else {
Chris Lattner8c7b0552003-04-16 17:49:18 +0000116 std::string IFN = InputFilename;
117 int Len = IFN.length();
118 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
119 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
120 } else {
121 OutputFilename = IFN; // Append a .o to it
122 }
123 OutputFilename += ".o";
Chris Lattnerecbde332001-10-31 04:28:11 +0000124 }
Chris Lattnerecbde332001-10-31 04:28:11 +0000125 }
126
Chris Lattner8c7b0552003-04-16 17:49:18 +0000127 if (OutputFilename == "-")
128 Out = &std::cout;
129 else {
130 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out);
131
132 // Make sure that the Out file gets unlink'd from the disk if we get a
133 // signal
134 RemoveFileOnSignal(OutputFilename);
135 }
136
137
138 if (!Out->good()) {
Chris Lattner2c1d2f22003-04-16 17:41:08 +0000139 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattnerecbde332001-10-31 04:28:11 +0000140 return 1;
141 }
142
143 // In addition to just parsing the input from GCC, we also want to spiff it up
144 // a little bit. Do this now.
145 //
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000146 PassManager Passes;
Chris Lattner4ad53222002-05-14 16:23:14 +0000147
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000148 // Add an appropriate TargetData instance for this module...
149 Passes.add(new TargetData("gccas", M.get()));
150
Chris Lattner624c3e02002-06-25 15:57:43 +0000151 // Add all of the transformation passes to the pass manager to do the cleanup
152 // and optimization of the GCC output.
153 //
154 AddConfiguredTransformationPasses(Passes);
155
156 // Write bytecode to file...
Chris Lattner8c7b0552003-04-16 17:49:18 +0000157 Passes.add(new WriteBytecodePass(Out));
Chris Lattnerecbde332001-10-31 04:28:11 +0000158
Chris Lattner3dc67dd2002-01-22 03:30:46 +0000159 // Run our queue of passes all at once now, efficiently.
Chris Lattner624c3e02002-06-25 15:57:43 +0000160 Passes.run(*M.get());
Chris Lattner8c7b0552003-04-16 17:49:18 +0000161
162 if (Out != &std::cout) delete Out;
Chris Lattnerecbde332001-10-31 04:28:11 +0000163 return 0;
164}