blob: 0a50d9fbb542c5df6b94503666e797be82ffc4df [file] [log] [blame]
Chris Lattner3bb02e42002-01-22 03:30:46 +00001//===----------------------------------------------------------------------===//
Chris Lattner1a3061762001-10-31 04:28:11 +00002// LLVM 'GCCAS' UTILITY
3//
Misha Brukman250bf362003-08-07 21:23:52 +00004// This utility is designed to be used by the GCC frontend for creating bytecode
5// files from its intermediate LLVM assembly. The requirements for this utility
6// are thus slightly different than that of the standard `as' util.
Chris Lattner1a3061762001-10-31 04:28:11 +00007//
Chris Lattner3bb02e42002-01-22 03:30:46 +00008//===----------------------------------------------------------------------===//
Chris Lattner1a3061762001-10-31 04:28:11 +00009
10#include "llvm/Module.h"
Chris Lattnerad50ec22002-01-31 00:46:22 +000011#include "llvm/PassManager.h"
Misha Brukman250bf362003-08-07 21:23:52 +000012#include "llvm/Analysis/LoadValueNumbering.h"
13#include "llvm/Analysis/Verifier.h"
Chris Lattner1a3061762001-10-31 04:28:11 +000014#include "llvm/Assembly/Parser.h"
Misha Brukman250bf362003-08-07 21:23:52 +000015#include "llvm/Bytecode/WriteBytecodePass.h"
16#include "llvm/Target/TargetData.h"
Chris Lattner35c45412002-07-23 22:04:43 +000017#include "llvm/Transforms/IPO.h"
Chris Lattner89a20ef2002-05-07 20:03:27 +000018#include "llvm/Transforms/Scalar.h"
Chris Lattner5de22042001-11-27 00:03:19 +000019#include "Support/CommandLine.h"
Chris Lattnerc065ad82002-04-18 19:55:25 +000020#include "Support/Signals.h"
Chris Lattner1a3061762001-10-31 04:28:11 +000021#include <memory>
22#include <fstream>
Chris Lattner1a3061762001-10-31 04:28:11 +000023
Chris Lattner1f412ae2003-04-16 17:34:29 +000024namespace {
Chris Lattner1f412ae2003-04-16 17:34:29 +000025 cl::opt<std::string>
Chris Lattner12fe9b42003-04-16 17:49:18 +000026 InputFilename(cl::Positional,cl::desc("<input llvm assembly>"),cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000027
Chris Lattner1f412ae2003-04-16 17:34:29 +000028 cl::opt<std::string>
29 OutputFilename("o", cl::desc("Override output filename"),
30 cl::value_desc("filename"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000031
Chris Lattner1f412ae2003-04-16 17:34:29 +000032 cl::opt<bool>
33 Verify("verify", cl::desc("Verify each pass result"));
Chris Lattner1f412ae2003-04-16 17:34:29 +000034}
Chris Lattnerf5cad152002-07-22 02:10:13 +000035
Chris Lattner5aa3a072002-06-25 15:57:43 +000036
37static inline void addPass(PassManager &PM, Pass *P) {
Chris Lattnerad7b5562003-08-31 21:47:24 +000038 // Add the pass to the pass manager...
39 PM.add(P);
Chris Lattner5aa3a072002-06-25 15:57:43 +000040
Chris Lattnerad7b5562003-08-31 21:47:24 +000041 // If we are verifying all of the intermediate steps, add the verifier...
42 if (Verify) PM.add(createVerifierPass());
Chris Lattner5aa3a072002-06-25 15:57:43 +000043}
44
45
46void AddConfiguredTransformationPasses(PassManager &PM) {
Chris Lattnere81e0d82003-06-22 20:11:45 +000047 PM.add(createVerifierPass()); // Verify that input is correct
Chris Lattnercf82b002003-09-15 04:56:44 +000048 addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp
Chris Lattner3f5b4262002-08-30 22:55:32 +000049 addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions
Chris Lattner3f5b4262002-08-30 22:55:32 +000050 addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst
Chris Lattner6f21fe92003-08-31 21:45:55 +000051 addPass(PM, createGlobalDCEPass()); // Remove unused globals
52 addPass(PM, createPruneEHPass()); // Remove dead EH info
53 addPass(PM, createFunctionInliningPass()); // Inline small functions
54
Chris Lattnere22031e2003-05-02 18:19:05 +000055 addPass(PM, createInstructionCombiningPass()); // Cleanup code for raise
Chris Lattner0e48f8c2003-04-24 18:26:03 +000056 addPass(PM, createRaisePointerReferencesPass());// Recover type information
Chris Lattnere81e0d82003-06-22 20:11:45 +000057 addPass(PM, createTailDuplicationPass()); // Simplify cfg by copying code
58 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
Chris Lattnerfdb2f4b2003-05-30 19:24:06 +000059 addPass(PM, createScalarReplAggregatesPass()); // Break up aggregate allocas
Chris Lattner25963c82003-09-20 05:26:22 +000060 addPass(PM, createTailCallEliminationPass()); // Eliminate tail calls
Chris Lattner6f21fe92003-08-31 21:45:55 +000061 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
62
Chris Lattnerf1257592002-10-31 17:13:11 +000063 addPass(PM, createReassociatePass()); // Reassociate expressions
Chris Lattnerc70222d2002-09-06 18:41:33 +000064 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
65 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
Chris Lattner3f5b4262002-08-30 22:55:32 +000066 addPass(PM, createLICMPass()); // Hoist loop invariants
67 addPass(PM, createLoadValueNumberingPass()); // GVN for load instructions
68 addPass(PM, createGCSEPass()); // Remove common subexprs
69 addPass(PM, createSCCPPass()); // Constant prop with SCCP
Chris Lattner5aa3a072002-06-25 15:57:43 +000070
71 // Run instcombine after redundancy elimination to exploit opportunities
72 // opened up by them.
73 addPass(PM, createInstructionCombiningPass());
Chris Lattner2f6de8b2003-09-11 16:34:07 +000074 addPass(PM, createIndVarSimplifyPass()); // Canonicalize indvars
Chris Lattnere81e0d82003-06-22 20:11:45 +000075 addPass(PM, createAggressiveDCEPass()); // SSA based 'Aggressive DCE'
Chris Lattnerc70222d2002-09-06 18:41:33 +000076 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
Chris Lattner6f21fe92003-08-31 21:45:55 +000077 addPass(PM, createDeadTypeEliminationPass()); // Eliminate dead types
78 addPass(PM, createConstantMergePass()); // Merge dup global constants
Chris Lattner5aa3a072002-06-25 15:57:43 +000079}
80
Chris Lattner1a3061762001-10-31 04:28:11 +000081
82int main(int argc, char **argv) {
Chris Lattnerae6de302001-10-31 04:33:33 +000083 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
Chris Lattner1a3061762001-10-31 04:28:11 +000084
Chris Lattner1a3061762001-10-31 04:28:11 +000085 std::auto_ptr<Module> M;
86 try {
87 // Parse the file now...
88 M.reset(ParseAssemblyFile(InputFilename));
89 } catch (const ParseException &E) {
Chris Lattner37088702003-04-16 17:41:08 +000090 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
Chris Lattner1a3061762001-10-31 04:28:11 +000091 return 1;
92 }
93
94 if (M.get() == 0) {
Chris Lattner37088702003-04-16 17:41:08 +000095 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
Chris Lattner1a3061762001-10-31 04:28:11 +000096 return 1;
97 }
Chris Lattner12fe9b42003-04-16 17:49:18 +000098
99 std::ostream *Out = 0;
Chris Lattner1a3061762001-10-31 04:28:11 +0000100 if (OutputFilename == "") { // Didn't specify an output filename?
Chris Lattner12fe9b42003-04-16 17:49:18 +0000101 if (InputFilename == "-") {
102 OutputFilename = "-";
Chris Lattner1a3061762001-10-31 04:28:11 +0000103 } else {
Chris Lattner12fe9b42003-04-16 17:49:18 +0000104 std::string IFN = InputFilename;
105 int Len = IFN.length();
106 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
107 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
108 } else {
109 OutputFilename = IFN; // Append a .o to it
110 }
111 OutputFilename += ".o";
Chris Lattner1a3061762001-10-31 04:28:11 +0000112 }
Chris Lattner1a3061762001-10-31 04:28:11 +0000113 }
114
Chris Lattner12fe9b42003-04-16 17:49:18 +0000115 if (OutputFilename == "-")
116 Out = &std::cout;
117 else {
118 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out);
119
Misha Brukmand6769742003-10-10 17:56:49 +0000120 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner12fe9b42003-04-16 17:49:18 +0000121 // signal
122 RemoveFileOnSignal(OutputFilename);
123 }
124
125
126 if (!Out->good()) {
Chris Lattner37088702003-04-16 17:41:08 +0000127 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner1a3061762001-10-31 04:28:11 +0000128 return 1;
129 }
130
131 // In addition to just parsing the input from GCC, we also want to spiff it up
132 // a little bit. Do this now.
133 //
Chris Lattner0686e432002-01-21 07:31:50 +0000134 PassManager Passes;
Chris Lattnerc9149442002-05-14 16:23:14 +0000135
Chris Lattnerd571e2a2003-04-24 19:13:02 +0000136 // Add an appropriate TargetData instance for this module...
137 Passes.add(new TargetData("gccas", M.get()));
138
Chris Lattner5aa3a072002-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...
Chris Lattner12fe9b42003-04-16 17:49:18 +0000145 Passes.add(new WriteBytecodePass(Out));
Chris Lattner1a3061762001-10-31 04:28:11 +0000146
Chris Lattner3bb02e42002-01-22 03:30:46 +0000147 // Run our queue of passes all at once now, efficiently.
Chris Lattner5aa3a072002-06-25 15:57:43 +0000148 Passes.run(*M.get());
Chris Lattner12fe9b42003-04-16 17:49:18 +0000149
150 if (Out != &std::cout) delete Out;
Chris Lattner1a3061762001-10-31 04:28:11 +0000151 return 0;
152}