blob: 2d08f36cc837614ad946c2d5ea1a45f5c382e543 [file] [log] [blame]
Chris Lattnerdbc23182003-10-15 21:49:57 +00001//===-- gccas.cpp - The "optimizing assembler" used by the GCC frontend ---===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// 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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
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"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/Support/CommandLine.h"
Bill Wendling68fe61d2006-11-29 00:19:40 +000026#include "llvm/Support/Streams.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000027#include "llvm/Support/ManagedStatic.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000028#include "llvm/System/Signals.h"
Bill Wendling68fe61d2006-11-29 00:19:40 +000029#include <iostream>
Chris Lattnerecbde332001-10-31 04:28:11 +000030#include <memory>
31#include <fstream>
Brian Gaeked0fde302003-11-11 22:41:34 +000032using namespace llvm;
33
Chris Lattnerf2956fc2003-04-16 17:34:29 +000034namespace {
Chris Lattnerf2956fc2003-04-16 17:34:29 +000035 cl::opt<std::string>
Chris Lattner8c7b0552003-04-16 17:49:18 +000036 InputFilename(cl::Positional,cl::desc("<input llvm assembly>"),cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000037
Misha Brukman3da94ae2005-04-22 00:00:37 +000038 cl::opt<std::string>
Chris Lattnerf2956fc2003-04-16 17:34:29 +000039 OutputFilename("o", cl::desc("Override output filename"),
40 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000041
Misha Brukman3da94ae2005-04-22 00:00:37 +000042 cl::opt<bool>
Chris Lattnerf2956fc2003-04-16 17:34:29 +000043 Verify("verify", cl::desc("Verify each pass result"));
Chris Lattner0cea3ec2003-10-10 18:18:53 +000044
45 cl::opt<bool>
46 DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
Chris Lattner74295c02003-12-30 03:24:27 +000047
48 cl::opt<bool>
49 DisableOptimizations("disable-opt",
50 cl::desc("Do not run any optimization passes"));
Chris Lattnerb11a4632004-07-22 08:34:33 +000051
52 cl::opt<bool>
Chris Lattner40add592004-12-03 05:45:58 +000053 StripDebug("strip-debug",
54 cl::desc("Strip debugger symbol info from translation unit"));
55
Misha Brukman3da94ae2005-04-22 00:00:37 +000056 cl::opt<bool>
Reid Spencercc0bd562004-11-08 17:37:04 +000057 NoCompress("disable-compression", cl::init(false),
Jeff Cohen7109ce82005-01-01 22:10:32 +000058 cl::desc("Don't compress the generated bytecode"));
Reid Spencer55126552004-12-22 02:58:43 +000059
60 cl::opt<bool> TF("traditional-format", cl::Hidden,
61 cl::desc("Compatibility option: ignored"));
Chris Lattnerf2956fc2003-04-16 17:34:29 +000062}
Chris Lattner5ff62e92002-07-22 02:10:13 +000063
Chris Lattner624c3e02002-06-25 15:57:43 +000064
65static inline void addPass(PassManager &PM, Pass *P) {
Chris Lattner36f18ae2003-08-31 21:47:24 +000066 // Add the pass to the pass manager...
67 PM.add(P);
Misha Brukman3da94ae2005-04-22 00:00:37 +000068
Chris Lattner36f18ae2003-08-31 21:47:24 +000069 // If we are verifying all of the intermediate steps, add the verifier...
70 if (Verify) PM.add(createVerifierPass());
Chris Lattner624c3e02002-06-25 15:57:43 +000071}
72
73
74void AddConfiguredTransformationPasses(PassManager &PM) {
Chris Lattnere643a6c2003-06-22 20:11:45 +000075 PM.add(createVerifierPass()); // Verify that input is correct
Chris Lattner40add592004-12-03 05:45:58 +000076
Chris Lattnerf7c7f5a2003-09-15 04:56:44 +000077 addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp
Chris Lattnerc5394832002-08-30 22:55:32 +000078 addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions
Chris Lattner74295c02003-12-30 03:24:27 +000079
Chris Lattner40add592004-12-03 05:45:58 +000080 // If the -strip-debug command line option was specified, do it.
81 if (StripDebug)
82 addPass(PM, createStripSymbolsPass(true));
83
Chris Lattner74295c02003-12-30 03:24:27 +000084 if (DisableOptimizations) return;
85
Chris Lattner14b170f2003-11-21 21:44:35 +000086 addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst
Chris Lattner05e4e8a2004-02-01 07:24:53 +000087 addPass(PM, createCFGSimplificationPass()); // Clean up disgusting code
Alkis Evlogimenosab7ada32005-03-28 02:01:12 +000088 addPass(PM, createPromoteMemoryToRegisterPass());// Kill useless allocas
Chris Lattner93a00e42004-10-07 04:12:02 +000089 addPass(PM, createGlobalOptimizerPass()); // Optimize out global vars
90 addPass(PM, createGlobalDCEPass()); // Remove unused fns and globs
Chris Lattnereaa35bb2003-10-23 18:25:57 +000091 addPass(PM, createIPConstantPropagationPass());// IP Constant Propagation
92 addPass(PM, createDeadArgEliminationPass()); // Dead argument elimination
Chris Lattner05e4e8a2004-02-01 07:24:53 +000093 addPass(PM, createInstructionCombiningPass()); // Clean up after IPCP & DAE
94 addPass(PM, createCFGSimplificationPass()); // Clean up after IPCP & DAE
Chris Lattnereaa35bb2003-10-23 18:25:57 +000095
Chris Lattnercf37c232003-08-31 21:45:55 +000096 addPass(PM, createPruneEHPass()); // Remove dead EH info
Chris Lattner0cea3ec2003-10-10 18:18:53 +000097
98 if (!DisableInline)
99 addPass(PM, createFunctionInliningPass()); // Inline small functions
Reid Spencerff1c9e22005-04-27 02:22:47 +0000100 addPass(PM, createSimplifyLibCallsPass()); // Library Call Optimizations
Chris Lattner2c7b4302004-03-13 21:38:35 +0000101 addPass(PM, createArgumentPromotionPass()); // Scalarize uninlined fn args
Chris Lattnercf37c232003-08-31 21:45:55 +0000102
Chris Lattnerf07c8332003-10-16 16:50:34 +0000103 addPass(PM, createRaisePointerReferencesPass());// Recover type information
Chris Lattnere643a6c2003-06-22 20:11:45 +0000104 addPass(PM, createTailDuplicationPass()); // Simplify cfg by copying code
105 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
Chris Lattner42ed21b2003-05-30 19:24:06 +0000106 addPass(PM, createScalarReplAggregatesPass()); // Break up aggregate allocas
Chris Lattnercf37c232003-08-31 21:45:55 +0000107 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
Chris Lattnercbe92e62005-05-07 22:45:35 +0000108 addPass(PM, createCondPropagationPass()); // Propagate conditionals
Chris Lattnercf37c232003-08-31 21:45:55 +0000109
Chris Lattnerca6cc6f2003-12-11 17:50:32 +0000110 addPass(PM, createTailCallEliminationPass()); // Eliminate tail calls
Chris Lattner4dd7d3e2002-09-06 18:41:33 +0000111 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
Chris Lattner35641ec2005-03-07 03:19:50 +0000112 addPass(PM, createReassociatePass()); // Reassociate expressions
Chris Lattnercbe92e62005-05-07 22:45:35 +0000113 addPass(PM, createLICMPass()); // Hoist loop invariants
Chris Lattnera2a51602006-02-22 07:33:49 +0000114 addPass(PM, createLoopUnswitchPass()); // Unswitch loops.
Chris Lattner35641ec2005-03-07 03:19:50 +0000115 addPass(PM, createInstructionCombiningPass()); // Clean up after LICM/reassoc
Chris Lattner93d82022004-04-18 05:21:01 +0000116 addPass(PM, createIndVarSimplifyPass()); // Canonicalize indvars
117 addPass(PM, createLoopUnrollPass()); // Unroll small loops
118 addPass(PM, createInstructionCombiningPass()); // Clean up after the unroller
Chris Lattnerc5394832002-08-30 22:55:32 +0000119 addPass(PM, createLoadValueNumberingPass()); // GVN for load instructions
120 addPass(PM, createGCSEPass()); // Remove common subexprs
121 addPass(PM, createSCCPPass()); // Constant prop with SCCP
Chris Lattner624c3e02002-06-25 15:57:43 +0000122
123 // Run instcombine after redundancy elimination to exploit opportunities
124 // opened up by them.
125 addPass(PM, createInstructionCombiningPass());
Chris Lattnercbe92e62005-05-07 22:45:35 +0000126 addPass(PM, createCondPropagationPass()); // Propagate conditionals
127
Chris Lattner40add592004-12-03 05:45:58 +0000128 addPass(PM, createDeadStoreEliminationPass()); // Delete dead stores
Chris Lattnere643a6c2003-06-22 20:11:45 +0000129 addPass(PM, createAggressiveDCEPass()); // SSA based 'Aggressive DCE'
Chris Lattner4dd7d3e2002-09-06 18:41:33 +0000130 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
Chris Lattnercf37c232003-08-31 21:45:55 +0000131 addPass(PM, createDeadTypeEliminationPass()); // Eliminate dead types
132 addPass(PM, createConstantMergePass()); // Merge dup global constants
Chris Lattner624c3e02002-06-25 15:57:43 +0000133}
134
Chris Lattnerecbde332001-10-31 04:28:11 +0000135
136int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +0000137 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Chris Lattnerecbde332001-10-31 04:28:11 +0000138 try {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000139 cl::ParseCommandLineOptions(argc, argv,
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000140 " llvm .s -> .o assembler for GCC\n");
141 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerecbde332001-10-31 04:28:11 +0000142
Reid Spencer61c83e02006-08-18 08:43:06 +0000143 ParseError Err;
144 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename,&Err));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000145 if (M.get() == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000146 cerr << argv[0] << ": " << Err.getMessage() << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000147 return 1;
148 }
149
150 std::ostream *Out = 0;
151 if (OutputFilename == "") { // Didn't specify an output filename?
152 if (InputFilename == "-") {
153 OutputFilename = "-";
154 } else {
155 std::string IFN = InputFilename;
156 int Len = IFN.length();
157 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
158 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
159 } else {
160 OutputFilename = IFN; // Append a .o to it
161 }
162 OutputFilename += ".o";
163 }
164 }
165
166 if (OutputFilename == "-")
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000167 // FIXME: cout is not binary!
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000168 Out = &std::cout;
169 else {
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000170 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
171 std::ios::binary;
172 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000173
174 // Make sure that the Out file gets unlinked from the disk if we get a
175 // signal
176 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
177 }
178
Misha Brukman3da94ae2005-04-22 00:00:37 +0000179
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000180 if (!Out->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000181 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000182 return 1;
183 }
184
Reid Spencer3d48e532006-07-03 16:46:03 +0000185 // In addition to just parsing the input from GCC, we also want to spiff
186 // it up a little bit. Do this now.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000187 PassManager Passes;
188
189 // Add an appropriate TargetData instance for this module...
Chris Lattner831b1212006-06-16 18:23:49 +0000190 Passes.add(new TargetData(M.get()));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000191
Reid Spencer3d48e532006-07-03 16:46:03 +0000192 // Add all of the transformation passes to the pass manager to do the
193 // cleanup and optimization of the GCC output.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000194 AddConfiguredTransformationPasses(Passes);
195
196 // Make sure everything is still good.
197 Passes.add(createVerifierPass());
198
199 // Write bytecode to file...
Bill Wendlinge8156192006-12-07 01:30:32 +0000200 OStream L(*Out);
Bill Wendling68fe61d2006-11-29 00:19:40 +0000201 Passes.add(new WriteBytecodePass(&L,false,!NoCompress));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000202
203 // Run our queue of passes all at once now, efficiently.
204 Passes.run(*M.get());
205
206 if (Out != &std::cout) delete Out;
207 return 0;
208 } catch (const std::string& msg) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000209 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000210 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000211 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattnerecbde332001-10-31 04:28:11 +0000212 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000213 return 1;
Chris Lattnerecbde332001-10-31 04:28:11 +0000214}