blob: dad1bc2442f2cb6153be694966afb3332e859d17 [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 Gaeked0fde302003-11-11 22:41:34 +000042namespace llvm {
John Criswelldc0de4f2003-09-18 16:22:26 +000043
Brian Gaeke1ab90d42003-11-16 23:07:28 +000044static inline void addPass(PassManager &PM, Pass *P) {
45 // Add the pass to the pass manager...
46 PM.add(P);
47
48 // If we are verifying all of the intermediate steps, add the verifier...
49 if (Verify) PM.add(createVerifierPass());
50}
51
Misha Brukman1c534052003-09-30 17:42:57 +000052/// GenerateBytecode - generates a bytecode file from the specified module.
53///
54/// Inputs:
55/// M - The module for which bytecode should be generated.
56/// Strip - Flags whether symbols should be stripped from the output.
57/// Internalize - Flags whether all symbols should be marked internal.
58/// Out - Pointer to file stream to which to write the output.
59///
60/// Outputs:
61/// None.
62///
63/// Returns non-zero value on error.
64///
John Criswelldc0de4f2003-09-18 16:22:26 +000065int
Misha Brukmanbb5a4d02003-09-30 17:33:12 +000066GenerateBytecode (Module *M, bool Strip, bool Internalize, std::ostream *Out) {
John Criswelldc0de4f2003-09-18 16:22:26 +000067 // In addition to just linking the input from GCC, we also want to spiff it up
68 // a little bit. Do this now.
69 PassManager Passes;
70
Brian Gaeke1ab90d42003-11-16 23:07:28 +000071 if (Verify) Passes.add(createVerifierPass());
72
John Criswelldc0de4f2003-09-18 16:22:26 +000073 // Add an appropriate TargetData instance for this module...
Misha Brukman438e3642003-11-20 06:26:15 +000074 addPass(Passes, new TargetData("gccld", M));
John Criswelldc0de4f2003-09-18 16:22:26 +000075
Chris Lattner548e8132003-11-28 09:44:03 +000076 // Often if the programmer does not specify proper prototypes for the
77 // functions they are calling, they end up calling a vararg version of the
78 // function that does not get a body filled in (the real function has typed
79 // arguments). This pass merges the two functions.
80 addPass(Passes, createFunctionResolvingPass());
81
Brian Gaeke1ab90d42003-11-16 23:07:28 +000082 if (!DisableOptimizations) {
Chris Lattnerdd429c62004-02-25 21:35:13 +000083 if (Internalize) {
84 // Now that composite has been compiled, scan through the module, looking
85 // for a main function. If main is defined, mark all other functions
86 // internal.
87 addPass(Passes, createInternalizePass());
88 }
89
90 // Now that we internalized some globals, see if we can mark any globals as
91 // being constant!
92 addPass(Passes, createGlobalConstifierPass());
93
Brian Gaeke1ab90d42003-11-16 23:07:28 +000094 // Linking modules together can lead to duplicated global constants, only
95 // keep one copy of each constant...
Misha Brukman438e3642003-11-20 06:26:15 +000096 addPass(Passes, createConstantMergePass());
John Criswelldc0de4f2003-09-18 16:22:26 +000097
Brian Gaeke1ab90d42003-11-16 23:07:28 +000098 // If the -s command line option was specified, strip the symbols out of the
99 // resulting program to make it smaller. -s is a GCC option that we are
100 // supporting.
101 if (Strip)
Misha Brukman438e3642003-11-20 06:26:15 +0000102 addPass(Passes, createSymbolStrippingPass());
John Criswelldc0de4f2003-09-18 16:22:26 +0000103
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000104 // Propagate constants at call sites into the functions they call.
Misha Brukman438e3642003-11-20 06:26:15 +0000105 addPass(Passes, createIPConstantPropagationPass());
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000106
107 // Remove unused arguments from functions...
Misha Brukman438e3642003-11-20 06:26:15 +0000108 addPass(Passes, createDeadArgEliminationPass());
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000109
110 if (!DisableInline)
Misha Brukman438e3642003-11-20 06:26:15 +0000111 addPass(Passes, createFunctionInliningPass()); // Inline small functions
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000112
Chris Lattnerf8338c42004-03-07 22:12:40 +0000113 // If we didn't decide to inline a function, check to see if we can
114 // transform it to pass arguments by value instead of by reference.
115 addPass(Passes, createArgumentPromotionPass());
116
Chris Lattnerf74a4012004-02-26 03:34:30 +0000117 // The IPO passes may leave cruft around. Clean up after them.
118 addPass(Passes, createInstructionCombiningPass());
119
120 addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
121
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000122 // Run a few AA driven optimizations here and now, to cleanup the code.
123 // Eventually we should put an IP AA in place here.
124
Misha Brukman438e3642003-11-20 06:26:15 +0000125 addPass(Passes, createLICMPass()); // Hoist loop invariants
126 addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs
127 addPass(Passes, createGCSEPass()); // Remove common subexprs
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000128
Chris Lattnerf74a4012004-02-26 03:34:30 +0000129 // Cleanup and simplify the code after the scalar optimizations.
Misha Brukman438e3642003-11-20 06:26:15 +0000130 addPass(Passes, createInstructionCombiningPass());
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000131
132 // Delete basic blocks, which optimization passes may have killed...
Misha Brukman438e3642003-11-20 06:26:15 +0000133 addPass(Passes, createCFGSimplificationPass());
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000134
135 // Now that we have optimized the program, discard unreachable functions...
Misha Brukman438e3642003-11-20 06:26:15 +0000136 addPass(Passes, createGlobalDCEPass());
John Criswelldc0de4f2003-09-18 16:22:26 +0000137 }
138
Chris Lattner0cccb182004-01-14 03:39:46 +0000139 // Make sure everything is still good.
140 Passes.add(createVerifierPass());
141
John Criswelldc0de4f2003-09-18 16:22:26 +0000142 // Add the pass that writes bytecode to the output file...
Misha Brukman438e3642003-11-20 06:26:15 +0000143 addPass(Passes, new WriteBytecodePass(Out));
John Criswelldc0de4f2003-09-18 16:22:26 +0000144
145 // Run our queue of passes all at once now, efficiently.
146 Passes.run(*M);
147
148 return 0;
149}
150
Misha Brukman1c534052003-09-30 17:42:57 +0000151/// GenerateAssembly - generates a native assembly language source file from the
152/// specified bytecode file.
153///
154/// Inputs:
155/// InputFilename - The name of the output bytecode file.
156/// OutputFilename - The name of the file to generate.
157/// llc - The pathname to use for LLC.
158/// envp - The environment to use when running LLC.
159///
160/// Outputs:
161/// None.
162///
163/// Return non-zero value on error.
164///
John Criswelldc0de4f2003-09-18 16:22:26 +0000165int
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000166GenerateAssembly(const std::string &OutputFilename,
167 const std::string &InputFilename,
168 const std::string &llc,
169 char ** const envp)
John Criswelldc0de4f2003-09-18 16:22:26 +0000170{
John Criswelldc0de4f2003-09-18 16:22:26 +0000171 // Run LLC to convert the bytecode file into assembly code.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000172 const char *cmd[8];
John Criswelldc0de4f2003-09-18 16:22:26 +0000173
Misha Brukmanb6b28432003-09-30 17:40:12 +0000174 cmd[0] = llc.c_str();
175 cmd[1] = "-f";
176 cmd[2] = "-o";
177 cmd[3] = OutputFilename.c_str();
178 cmd[4] = InputFilename.c_str();
179 cmd[5] = NULL;
John Criswelldc0de4f2003-09-18 16:22:26 +0000180
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000181 return ExecWait(cmd, envp);
John Criswelldc0de4f2003-09-18 16:22:26 +0000182}
183
Chris Lattner69e8d282004-04-06 16:43:13 +0000184/// GenerateAssembly - generates a native assembly language source file from the
185/// specified bytecode file.
186int GenerateCFile(const std::string &OutputFile, const std::string &InputFile,
187 const std::string &llc, char ** const envp) {
188 // Run LLC to convert the bytecode file into C.
189 const char *cmd[8];
190
191 cmd[0] = llc.c_str();
192 cmd[1] = "-march=c";
193 cmd[2] = "-f";
194 cmd[3] = "-o";
195 cmd[4] = OutputFile.c_str();
196 cmd[5] = InputFile.c_str();
197 cmd[6] = NULL;
198 return ExecWait(cmd, envp);
199}
200
Misha Brukman1c534052003-09-30 17:42:57 +0000201/// GenerateNative - generates a native assembly language source file from the
202/// specified assembly source file.
203///
204/// Inputs:
205/// InputFilename - The name of the output bytecode file.
206/// OutputFilename - The name of the file to generate.
207/// Libraries - The list of libraries with which to link.
208/// LibPaths - The list of directories in which to find libraries.
209/// gcc - The pathname to use for GGC.
210/// envp - A copy of the process's current environment.
211///
212/// Outputs:
213/// None.
214///
215/// Returns non-zero value on error.
216///
John Criswelldc0de4f2003-09-18 16:22:26 +0000217int
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000218GenerateNative(const std::string &OutputFilename,
219 const std::string &InputFilename,
220 const std::vector<std::string> &Libraries,
221 const std::vector<std::string> &LibPaths,
222 const std::string &gcc,
223 char ** const envp) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000224 // Remove these environment variables from the environment of the
225 // programs that we will execute. It appears that GCC sets these
226 // environment variables so that the programs it uses can configure
227 // themselves identically.
228 //
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000229 // However, when we invoke GCC below, we want it to use its normal
230 // configuration. Hence, we must sanitize its environment.
231 char ** clean_env = CopyEnv(envp);
John Criswelldc0de4f2003-09-18 16:22:26 +0000232 if (clean_env == NULL)
John Criswelldc0de4f2003-09-18 16:22:26 +0000233 return 1;
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000234 RemoveEnv("LIBRARY_PATH", clean_env);
235 RemoveEnv("COLLECT_GCC_OPTIONS", clean_env);
236 RemoveEnv("GCC_EXEC_PREFIX", clean_env);
237 RemoveEnv("COMPILER_PATH", clean_env);
238 RemoveEnv("COLLECT_GCC", clean_env);
John Criswelldc0de4f2003-09-18 16:22:26 +0000239
John Criswell71478b72003-09-19 20:24:23 +0000240 std::vector<const char *> cmd;
John Criswelldc0de4f2003-09-18 16:22:26 +0000241
John Criswelldc0de4f2003-09-18 16:22:26 +0000242 // Run GCC to assemble and link the program into native code.
243 //
244 // Note:
245 // We can't just assemble and link the file with the system assembler
246 // and linker because we don't know where to put the _start symbol.
247 // GCC mysteriously knows how to do it.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000248 cmd.push_back(gcc.c_str());
Chris Lattner69e8d282004-04-06 16:43:13 +0000249 cmd.push_back("-O3");
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000250 cmd.push_back("-o");
251 cmd.push_back(OutputFilename.c_str());
252 cmd.push_back(InputFilename.c_str());
John Criswelldc0de4f2003-09-18 16:22:26 +0000253
Misha Brukmanb6b28432003-09-30 17:40:12 +0000254 // Adding the library paths creates a problem for native generation. If we
255 // include the search paths from llvmgcc, then we'll be telling normal gcc
256 // to look inside of llvmgcc's library directories for libraries. This is
257 // bad because those libraries hold only bytecode files (not native object
258 // files). In the end, we attempt to link the bytecode libgcc into a native
259 // program.
Chris Lattner238cf3c2003-09-30 17:36:51 +0000260#if 0
John Criswell71478b72003-09-19 20:24:23 +0000261 // Add in the library path options.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000262 for (unsigned index=0; index < LibPaths.size(); index++) {
263 cmd.push_back("-L");
264 cmd.push_back(LibPaths[index].c_str());
John Criswell71478b72003-09-19 20:24:23 +0000265 }
266#endif
267
John Criswell71478b72003-09-19 20:24:23 +0000268 // Add in the libraries to link.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000269 std::vector<std::string> Libs(Libraries);
270 for (unsigned index = 0; index < Libs.size(); index++) {
John Criswell6f5592a2004-01-26 23:51:10 +0000271 if (Libs[index] != "crtend") {
272 Libs[index] = "-l" + Libs[index];
273 cmd.push_back(Libs[index].c_str());
274 }
John Criswell71478b72003-09-19 20:24:23 +0000275 }
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000276 cmd.push_back(NULL);
John Criswell71478b72003-09-19 20:24:23 +0000277
John Criswell71478b72003-09-19 20:24:23 +0000278 // Run the compiler to assembly and link together the program.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000279 return ExecWait(&(cmd[0]), clean_env);
John Criswelldc0de4f2003-09-18 16:22:26 +0000280}
Brian Gaeked0fde302003-11-11 22:41:34 +0000281
282} // End llvm namespace