blob: a844ee751a0c0aea4cbf787df451c2f4a2e4e50f [file] [log] [blame]
Chris Lattnera58d2be2003-09-30 03:24:28 +00001//===- GenerateCode.cpp - Functions for generating executable files ------===//
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//===----------------------------------------------------------------------===//
John Criswelldc0de4f2003-09-18 16:22:26 +00009//
10// This file contains functions for generating executable files once linking
11// has finished. This includes generating a shell script to run the JIT or
12// a native executable derived from the bytecode.
13//
14//===----------------------------------------------------------------------===//
15
Misha Brukmanbb5a4d02003-09-30 17:33:12 +000016#include "gccld.h"
John Criswelldc0de4f2003-09-18 16:22:26 +000017#include "llvm/Module.h"
18#include "llvm/PassManager.h"
Chris Lattnercc650b62003-11-09 19:55:09 +000019#include "llvm/Analysis/LoadValueNumbering.h"
Brian Gaeke1ab90d42003-11-16 23:07:28 +000020#include "llvm/Analysis/Verifier.h"
John Criswelldc0de4f2003-09-18 16:22:26 +000021#include "llvm/Bytecode/WriteBytecodePass.h"
Misha Brukmanbb5a4d02003-09-30 17:33:12 +000022#include "llvm/Target/TargetData.h"
23#include "llvm/Transforms/IPO.h"
24#include "llvm/Transforms/Scalar.h"
25#include "llvm/Transforms/Utils/Linker.h"
John Criswelldc0de4f2003-09-18 16:22:26 +000026#include "Support/SystemUtils.h"
Chris Lattner246ce3c2003-10-24 18:09:23 +000027#include "Support/CommandLine.h"
Brian Gaeked0fde302003-11-11 22:41:34 +000028using namespace llvm;
29
Chris Lattner246ce3c2003-10-24 18:09:23 +000030namespace {
31 cl::opt<bool>
32 DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
Brian Gaeke1ab90d42003-11-16 23:07:28 +000033
34 cl::opt<bool>
35 Verify("verify", cl::desc("Verify intermediate results of all passes"));
36
37 cl::opt<bool>
38 DisableOptimizations("disable-opt",
39 cl::desc("Do not run any optimization passes"));
Chris Lattner246ce3c2003-10-24 18:09:23 +000040}
41
Brian Gaeke1ab90d42003-11-16 23:07:28 +000042static inline void addPass(PassManager &PM, Pass *P) {
43 // Add the pass to the pass manager...
44 PM.add(P);
45
46 // If we are verifying all of the intermediate steps, add the verifier...
47 if (Verify) PM.add(createVerifierPass());
48}
49
Misha Brukman1c534052003-09-30 17:42:57 +000050/// GenerateBytecode - generates a bytecode file from the specified module.
51///
52/// Inputs:
53/// M - The module for which bytecode should be generated.
54/// Strip - Flags whether symbols should be stripped from the output.
55/// Internalize - Flags whether all symbols should be marked internal.
56/// Out - Pointer to file stream to which to write the output.
57///
Misha Brukman1c534052003-09-30 17:42:57 +000058/// Returns non-zero value on error.
59///
Chris Lattner27a9b272004-04-06 16:54:04 +000060int llvm::GenerateBytecode(Module *M, bool Strip, bool Internalize,
61 std::ostream *Out) {
John Criswelldc0de4f2003-09-18 16:22:26 +000062 // In addition to just linking the input from GCC, we also want to spiff it up
63 // a little bit. Do this now.
64 PassManager Passes;
65
Brian Gaeke1ab90d42003-11-16 23:07:28 +000066 if (Verify) Passes.add(createVerifierPass());
67
John Criswelldc0de4f2003-09-18 16:22:26 +000068 // Add an appropriate TargetData instance for this module...
Misha Brukman438e3642003-11-20 06:26:15 +000069 addPass(Passes, new TargetData("gccld", M));
John Criswelldc0de4f2003-09-18 16:22:26 +000070
Chris Lattner548e8132003-11-28 09:44:03 +000071 // Often if the programmer does not specify proper prototypes for the
72 // functions they are calling, they end up calling a vararg version of the
73 // function that does not get a body filled in (the real function has typed
74 // arguments). This pass merges the two functions.
75 addPass(Passes, createFunctionResolvingPass());
76
Brian Gaeke1ab90d42003-11-16 23:07:28 +000077 if (!DisableOptimizations) {
Chris Lattnerdd429c62004-02-25 21:35:13 +000078 if (Internalize) {
79 // Now that composite has been compiled, scan through the module, looking
80 // for a main function. If main is defined, mark all other functions
81 // internal.
82 addPass(Passes, createInternalizePass());
83 }
84
85 // Now that we internalized some globals, see if we can mark any globals as
86 // being constant!
87 addPass(Passes, createGlobalConstifierPass());
88
Brian Gaeke1ab90d42003-11-16 23:07:28 +000089 // Linking modules together can lead to duplicated global constants, only
90 // keep one copy of each constant...
Misha Brukman438e3642003-11-20 06:26:15 +000091 addPass(Passes, createConstantMergePass());
John Criswelldc0de4f2003-09-18 16:22:26 +000092
Brian Gaeke1ab90d42003-11-16 23:07:28 +000093 // If the -s command line option was specified, strip the symbols out of the
94 // resulting program to make it smaller. -s is a GCC option that we are
95 // supporting.
96 if (Strip)
Misha Brukman438e3642003-11-20 06:26:15 +000097 addPass(Passes, createSymbolStrippingPass());
John Criswelldc0de4f2003-09-18 16:22:26 +000098
Brian Gaeke1ab90d42003-11-16 23:07:28 +000099 // Propagate constants at call sites into the functions they call.
Misha Brukman438e3642003-11-20 06:26:15 +0000100 addPass(Passes, createIPConstantPropagationPass());
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000101
102 // Remove unused arguments from functions...
Misha Brukman438e3642003-11-20 06:26:15 +0000103 addPass(Passes, createDeadArgEliminationPass());
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000104
105 if (!DisableInline)
Misha Brukman438e3642003-11-20 06:26:15 +0000106 addPass(Passes, createFunctionInliningPass()); // Inline small functions
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000107
Chris Lattnerf9c455d2004-04-12 05:38:15 +0000108 addPass(Passes, createPruneEHPass()); // Remove dead EH info
109 addPass(Passes, createGlobalDCEPass()); // Remove dead functions
110
Chris Lattnerf8338c42004-03-07 22:12:40 +0000111 // If we didn't decide to inline a function, check to see if we can
112 // transform it to pass arguments by value instead of by reference.
113 addPass(Passes, createArgumentPromotionPass());
114
Chris Lattnerf74a4012004-02-26 03:34:30 +0000115 // The IPO passes may leave cruft around. Clean up after them.
116 addPass(Passes, createInstructionCombiningPass());
117
118 addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
119
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000120 // Run a few AA driven optimizations here and now, to cleanup the code.
121 // Eventually we should put an IP AA in place here.
122
Misha Brukman438e3642003-11-20 06:26:15 +0000123 addPass(Passes, createLICMPass()); // Hoist loop invariants
124 addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs
125 addPass(Passes, createGCSEPass()); // Remove common subexprs
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000126
Chris Lattnerf74a4012004-02-26 03:34:30 +0000127 // Cleanup and simplify the code after the scalar optimizations.
Misha Brukman438e3642003-11-20 06:26:15 +0000128 addPass(Passes, createInstructionCombiningPass());
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000129
130 // Delete basic blocks, which optimization passes may have killed...
Misha Brukman438e3642003-11-20 06:26:15 +0000131 addPass(Passes, createCFGSimplificationPass());
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000132
133 // Now that we have optimized the program, discard unreachable functions...
Misha Brukman438e3642003-11-20 06:26:15 +0000134 addPass(Passes, createGlobalDCEPass());
John Criswelldc0de4f2003-09-18 16:22:26 +0000135 }
136
Chris Lattner0cccb182004-01-14 03:39:46 +0000137 // Make sure everything is still good.
138 Passes.add(createVerifierPass());
139
John Criswelldc0de4f2003-09-18 16:22:26 +0000140 // Add the pass that writes bytecode to the output file...
Misha Brukman438e3642003-11-20 06:26:15 +0000141 addPass(Passes, new WriteBytecodePass(Out));
John Criswelldc0de4f2003-09-18 16:22:26 +0000142
143 // Run our queue of passes all at once now, efficiently.
144 Passes.run(*M);
145
146 return 0;
147}
148
Misha Brukman1c534052003-09-30 17:42:57 +0000149/// GenerateAssembly - generates a native assembly language source file from the
150/// specified bytecode file.
151///
152/// Inputs:
153/// InputFilename - The name of the output bytecode file.
154/// OutputFilename - The name of the file to generate.
155/// llc - The pathname to use for LLC.
156/// envp - The environment to use when running LLC.
157///
Misha Brukman1c534052003-09-30 17:42:57 +0000158/// Return non-zero value on error.
159///
Chris Lattner27a9b272004-04-06 16:54:04 +0000160int llvm::GenerateAssembly(const std::string &OutputFilename,
161 const std::string &InputFilename,
162 const std::string &llc,
163 char ** const envp) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000164 // Run LLC to convert the bytecode file into assembly code.
Chris Lattner27a9b272004-04-06 16:54:04 +0000165 const char *cmd[6];
Misha Brukmanb6b28432003-09-30 17:40:12 +0000166 cmd[0] = llc.c_str();
167 cmd[1] = "-f";
168 cmd[2] = "-o";
169 cmd[3] = OutputFilename.c_str();
170 cmd[4] = InputFilename.c_str();
Chris Lattner27a9b272004-04-06 16:54:04 +0000171 cmd[5] = 0;
John Criswelldc0de4f2003-09-18 16:22:26 +0000172
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000173 return ExecWait(cmd, envp);
John Criswelldc0de4f2003-09-18 16:22:26 +0000174}
175
Chris Lattner69e8d282004-04-06 16:43:13 +0000176/// GenerateAssembly - generates a native assembly language source file from the
177/// specified bytecode file.
Chris Lattner27a9b272004-04-06 16:54:04 +0000178int llvm::GenerateCFile(const std::string &OutputFile,
179 const std::string &InputFile,
180 const std::string &llc, char ** const envp) {
Chris Lattner69e8d282004-04-06 16:43:13 +0000181 // Run LLC to convert the bytecode file into C.
Chris Lattner67e0a342004-04-08 15:18:03 +0000182 const char *cmd[7];
Chris Lattner69e8d282004-04-06 16:43:13 +0000183
184 cmd[0] = llc.c_str();
185 cmd[1] = "-march=c";
186 cmd[2] = "-f";
Chris Lattner67e0a342004-04-08 15:18:03 +0000187 cmd[3] = "-o";
188 cmd[4] = OutputFile.c_str();
189 cmd[5] = InputFile.c_str();
190 cmd[6] = 0;
Chris Lattner69e8d282004-04-06 16:43:13 +0000191 return ExecWait(cmd, envp);
192}
193
Misha Brukman1c534052003-09-30 17:42:57 +0000194/// GenerateNative - generates a native assembly language source file from the
195/// specified assembly source file.
196///
197/// Inputs:
198/// InputFilename - The name of the output bytecode file.
199/// OutputFilename - The name of the file to generate.
200/// Libraries - The list of libraries with which to link.
201/// LibPaths - The list of directories in which to find libraries.
202/// gcc - The pathname to use for GGC.
203/// envp - A copy of the process's current environment.
204///
205/// Outputs:
206/// None.
207///
208/// Returns non-zero value on error.
209///
Chris Lattner27a9b272004-04-06 16:54:04 +0000210int llvm::GenerateNative(const std::string &OutputFilename,
211 const std::string &InputFilename,
212 const std::vector<std::string> &Libraries,
213 const std::vector<std::string> &LibPaths,
214 const std::string &gcc, char ** const envp) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000215 // Remove these environment variables from the environment of the
216 // programs that we will execute. It appears that GCC sets these
217 // environment variables so that the programs it uses can configure
218 // themselves identically.
219 //
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000220 // However, when we invoke GCC below, we want it to use its normal
221 // configuration. Hence, we must sanitize its environment.
222 char ** clean_env = CopyEnv(envp);
John Criswelldc0de4f2003-09-18 16:22:26 +0000223 if (clean_env == NULL)
John Criswelldc0de4f2003-09-18 16:22:26 +0000224 return 1;
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000225 RemoveEnv("LIBRARY_PATH", clean_env);
226 RemoveEnv("COLLECT_GCC_OPTIONS", clean_env);
227 RemoveEnv("GCC_EXEC_PREFIX", clean_env);
228 RemoveEnv("COMPILER_PATH", clean_env);
229 RemoveEnv("COLLECT_GCC", clean_env);
John Criswelldc0de4f2003-09-18 16:22:26 +0000230
John Criswell71478b72003-09-19 20:24:23 +0000231 std::vector<const char *> cmd;
John Criswelldc0de4f2003-09-18 16:22:26 +0000232
John Criswelldc0de4f2003-09-18 16:22:26 +0000233 // Run GCC to assemble and link the program into native code.
234 //
235 // Note:
236 // We can't just assemble and link the file with the system assembler
237 // and linker because we don't know where to put the _start symbol.
238 // GCC mysteriously knows how to do it.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000239 cmd.push_back(gcc.c_str());
Chris Lattnera1346a22004-04-08 15:18:59 +0000240 cmd.push_back("-fno-strict-aliasing");
Chris Lattner69e8d282004-04-06 16:43:13 +0000241 cmd.push_back("-O3");
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000242 cmd.push_back("-o");
243 cmd.push_back(OutputFilename.c_str());
244 cmd.push_back(InputFilename.c_str());
John Criswelldc0de4f2003-09-18 16:22:26 +0000245
Misha Brukmanb6b28432003-09-30 17:40:12 +0000246 // Adding the library paths creates a problem for native generation. If we
247 // include the search paths from llvmgcc, then we'll be telling normal gcc
248 // to look inside of llvmgcc's library directories for libraries. This is
249 // bad because those libraries hold only bytecode files (not native object
250 // files). In the end, we attempt to link the bytecode libgcc into a native
251 // program.
Chris Lattner238cf3c2003-09-30 17:36:51 +0000252#if 0
John Criswell71478b72003-09-19 20:24:23 +0000253 // Add in the library path options.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000254 for (unsigned index=0; index < LibPaths.size(); index++) {
255 cmd.push_back("-L");
256 cmd.push_back(LibPaths[index].c_str());
John Criswell71478b72003-09-19 20:24:23 +0000257 }
258#endif
259
John Criswell71478b72003-09-19 20:24:23 +0000260 // Add in the libraries to link.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000261 std::vector<std::string> Libs(Libraries);
262 for (unsigned index = 0; index < Libs.size(); index++) {
John Criswell6f5592a2004-01-26 23:51:10 +0000263 if (Libs[index] != "crtend") {
264 Libs[index] = "-l" + Libs[index];
265 cmd.push_back(Libs[index].c_str());
266 }
John Criswell71478b72003-09-19 20:24:23 +0000267 }
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000268 cmd.push_back(NULL);
John Criswell71478b72003-09-19 20:24:23 +0000269
John Criswell71478b72003-09-19 20:24:23 +0000270 // Run the compiler to assembly and link together the program.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000271 return ExecWait(&(cmd[0]), clean_env);
John Criswelldc0de4f2003-09-18 16:22:26 +0000272}