blob: d97c716da1e901406e79a61d98e10f7f43d76a46 [file] [log] [blame]
Chris Lattnerdbc23182003-10-15 21:49:57 +00001//===-- gccas.cpp - The "optimizing assembler" used by the GCC frontend ---===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerecbde332001-10-31 04:28:11 +00009//
Misha Brukman57d708b2003-08-07 21:23:52 +000010// This utility is designed to be used by the GCC frontend for creating bytecode
11// files from its intermediate LLVM assembly. The requirements for this utility
12// are thus slightly different than that of the standard `as' util.
Chris Lattnerecbde332001-10-31 04:28:11 +000013//
Chris Lattner3dc67dd2002-01-22 03:30:46 +000014//===----------------------------------------------------------------------===//
Chris Lattnerecbde332001-10-31 04:28:11 +000015
16#include "llvm/Module.h"
Chris Lattner0f3bfff2002-01-31 00:46:22 +000017#include "llvm/PassManager.h"
Misha Brukman57d708b2003-08-07 21:23:52 +000018#include "llvm/Analysis/LoadValueNumbering.h"
19#include "llvm/Analysis/Verifier.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000020#include "llvm/Assembly/Parser.h"
Misha Brukman57d708b2003-08-07 21:23:52 +000021#include "llvm/Bytecode/WriteBytecodePass.h"
22#include "llvm/Target/TargetData.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000023#include "llvm/Transforms/IPO.h"
Chris Lattner65f1b892002-05-07 20:03:27 +000024#include "llvm/Transforms/Scalar.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000025#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000026#include "Support/Signals.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000027#include <memory>
28#include <fstream>
Chris Lattnerecbde332001-10-31 04:28:11 +000029
Chris Lattnerf2956fc2003-04-16 17:34:29 +000030namespace {
Chris Lattnerf2956fc2003-04-16 17:34:29 +000031 cl::opt<std::string>
Chris Lattner8c7b0552003-04-16 17:49:18 +000032 InputFilename(cl::Positional,cl::desc("<input llvm assembly>"),cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000033
Chris Lattnerf2956fc2003-04-16 17:34:29 +000034 cl::opt<std::string>
35 OutputFilename("o", cl::desc("Override output filename"),
36 cl::value_desc("filename"));
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 Lattner0cea3ec2003-10-10 18:18:53 +000040
41 cl::opt<bool>
42 DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
Chris Lattnerf2956fc2003-04-16 17:34:29 +000043}
Chris Lattner5ff62e92002-07-22 02:10:13 +000044
Chris Lattner624c3e02002-06-25 15:57:43 +000045
46static inline void addPass(PassManager &PM, Pass *P) {
Chris Lattner36f18ae2003-08-31 21:47:24 +000047 // Add the pass to the pass manager...
48 PM.add(P);
Chris Lattner624c3e02002-06-25 15:57:43 +000049
Chris Lattner36f18ae2003-08-31 21:47:24 +000050 // If we are verifying all of the intermediate steps, add the verifier...
51 if (Verify) PM.add(createVerifierPass());
Chris Lattner624c3e02002-06-25 15:57:43 +000052}
53
54
55void AddConfiguredTransformationPasses(PassManager &PM) {
Chris Lattnere643a6c2003-06-22 20:11:45 +000056 PM.add(createVerifierPass()); // Verify that input is correct
Chris Lattnerf7c7f5a2003-09-15 04:56:44 +000057 addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp
Chris Lattnerc5394832002-08-30 22:55:32 +000058 addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions
Chris Lattnereaa35bb2003-10-23 18:25:57 +000059 addPass(PM, createCFGSimplificationPass()); // Clean up disgusting code
Chris Lattnerc5394832002-08-30 22:55:32 +000060 addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst
Chris Lattnercf37c232003-08-31 21:45:55 +000061 addPass(PM, createGlobalDCEPass()); // Remove unused globals
Chris Lattnereaa35bb2003-10-23 18:25:57 +000062 addPass(PM, createIPConstantPropagationPass());// IP Constant Propagation
63 addPass(PM, createDeadArgEliminationPass()); // Dead argument elimination
64
Chris Lattnercf37c232003-08-31 21:45:55 +000065 addPass(PM, createPruneEHPass()); // Remove dead EH info
Chris Lattner0cea3ec2003-10-10 18:18:53 +000066
67 if (!DisableInline)
68 addPass(PM, createFunctionInliningPass()); // Inline small functions
Chris Lattnercf37c232003-08-31 21:45:55 +000069
Chris Lattner590607b2003-05-02 18:19:05 +000070 addPass(PM, createInstructionCombiningPass()); // Cleanup code for raise
Chris Lattnerf07c8332003-10-16 16:50:34 +000071 addPass(PM, createRaisePointerReferencesPass());// Recover type information
Chris Lattnere643a6c2003-06-22 20:11:45 +000072 addPass(PM, createTailDuplicationPass()); // Simplify cfg by copying code
73 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
Chris Lattner42ed21b2003-05-30 19:24:06 +000074 addPass(PM, createScalarReplAggregatesPass()); // Break up aggregate allocas
Chris Lattner75ebab82003-09-20 05:26:22 +000075 addPass(PM, createTailCallEliminationPass()); // Eliminate tail calls
Chris Lattnercf37c232003-08-31 21:45:55 +000076 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
77
Chris Lattner52af6302002-10-31 17:13:11 +000078 addPass(PM, createReassociatePass()); // Reassociate expressions
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 Lattner47320522003-09-11 16:34:07 +000089 addPass(PM, createIndVarSimplifyPass()); // Canonicalize indvars
Chris Lattnere643a6c2003-06-22 20:11:45 +000090 addPass(PM, createAggressiveDCEPass()); // SSA based 'Aggressive DCE'
Chris Lattner4dd7d3e2002-09-06 18:41:33 +000091 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
Chris Lattnercf37c232003-08-31 21:45:55 +000092 addPass(PM, createDeadTypeEliminationPass()); // Eliminate dead types
93 addPass(PM, createConstantMergePass()); // Merge dup global constants
Chris Lattner624c3e02002-06-25 15:57:43 +000094}
95
Chris Lattnerecbde332001-10-31 04:28:11 +000096
97int main(int argc, char **argv) {
Chris Lattner11c862c2001-10-31 04:33:33 +000098 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
Chris Lattnerecbde332001-10-31 04:28:11 +000099
Chris Lattnerecbde332001-10-31 04:28:11 +0000100 std::auto_ptr<Module> M;
101 try {
102 // Parse the file now...
103 M.reset(ParseAssemblyFile(InputFilename));
104 } catch (const ParseException &E) {
Chris Lattner2c1d2f22003-04-16 17:41:08 +0000105 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
Chris Lattnerecbde332001-10-31 04:28:11 +0000106 return 1;
107 }
108
109 if (M.get() == 0) {
Chris Lattner2c1d2f22003-04-16 17:41:08 +0000110 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
Chris Lattnerecbde332001-10-31 04:28:11 +0000111 return 1;
112 }
Chris Lattner8c7b0552003-04-16 17:49:18 +0000113
114 std::ostream *Out = 0;
Chris Lattnerecbde332001-10-31 04:28:11 +0000115 if (OutputFilename == "") { // Didn't specify an output filename?
Chris Lattner8c7b0552003-04-16 17:49:18 +0000116 if (InputFilename == "-") {
117 OutputFilename = "-";
Chris Lattnerecbde332001-10-31 04:28:11 +0000118 } else {
Chris Lattner8c7b0552003-04-16 17:49:18 +0000119 std::string IFN = InputFilename;
120 int Len = IFN.length();
121 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
122 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
123 } else {
124 OutputFilename = IFN; // Append a .o to it
125 }
126 OutputFilename += ".o";
Chris Lattnerecbde332001-10-31 04:28:11 +0000127 }
Chris Lattnerecbde332001-10-31 04:28:11 +0000128 }
129
Chris Lattner8c7b0552003-04-16 17:49:18 +0000130 if (OutputFilename == "-")
131 Out = &std::cout;
132 else {
133 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out);
134
Misha Brukman452fea92003-10-10 17:56:49 +0000135 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner8c7b0552003-04-16 17:49:18 +0000136 // signal
137 RemoveFileOnSignal(OutputFilename);
138 }
139
140
141 if (!Out->good()) {
Chris Lattner2c1d2f22003-04-16 17:41:08 +0000142 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattnerecbde332001-10-31 04:28:11 +0000143 return 1;
144 }
145
146 // In addition to just parsing the input from GCC, we also want to spiff it up
147 // a little bit. Do this now.
148 //
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000149 PassManager Passes;
Chris Lattner4ad53222002-05-14 16:23:14 +0000150
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000151 // Add an appropriate TargetData instance for this module...
152 Passes.add(new TargetData("gccas", M.get()));
153
Chris Lattner624c3e02002-06-25 15:57:43 +0000154 // Add all of the transformation passes to the pass manager to do the cleanup
155 // and optimization of the GCC output.
156 //
157 AddConfiguredTransformationPasses(Passes);
158
159 // Write bytecode to file...
Chris Lattner8c7b0552003-04-16 17:49:18 +0000160 Passes.add(new WriteBytecodePass(Out));
Chris Lattnerecbde332001-10-31 04:28:11 +0000161
Chris Lattner3dc67dd2002-01-22 03:30:46 +0000162 // Run our queue of passes all at once now, efficiently.
Chris Lattner624c3e02002-06-25 15:57:43 +0000163 Passes.run(*M.get());
Chris Lattner8c7b0552003-04-16 17:49:18 +0000164
165 if (Out != &std::cout) delete Out;
Chris Lattnerecbde332001-10-31 04:28:11 +0000166 return 0;
167}