blob: bf32400c6348c4298abce134e1453f4e36ff155d [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"
John Criswelldc0de4f2003-09-18 16:22:26 +000020#include "llvm/Bytecode/WriteBytecodePass.h"
Misha Brukmanbb5a4d02003-09-30 17:33:12 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/Transforms/IPO.h"
23#include "llvm/Transforms/Scalar.h"
24#include "llvm/Transforms/Utils/Linker.h"
John Criswelldc0de4f2003-09-18 16:22:26 +000025#include "Support/SystemUtils.h"
Chris Lattner246ce3c2003-10-24 18:09:23 +000026#include "Support/CommandLine.h"
27
28namespace {
29 cl::opt<bool>
30 DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
31}
32
John Criswelldc0de4f2003-09-18 16:22:26 +000033
Misha Brukman1c534052003-09-30 17:42:57 +000034/// GenerateBytecode - generates a bytecode file from the specified module.
35///
36/// Inputs:
37/// M - The module for which bytecode should be generated.
38/// Strip - Flags whether symbols should be stripped from the output.
39/// Internalize - Flags whether all symbols should be marked internal.
40/// Out - Pointer to file stream to which to write the output.
41///
42/// Outputs:
43/// None.
44///
45/// Returns non-zero value on error.
46///
John Criswelldc0de4f2003-09-18 16:22:26 +000047int
Misha Brukmanbb5a4d02003-09-30 17:33:12 +000048GenerateBytecode (Module *M, bool Strip, bool Internalize, std::ostream *Out) {
John Criswelldc0de4f2003-09-18 16:22:26 +000049 // In addition to just linking the input from GCC, we also want to spiff it up
50 // a little bit. Do this now.
51 PassManager Passes;
52
53 // Add an appropriate TargetData instance for this module...
54 Passes.add(new TargetData("gccld", M));
55
56 // Linking modules together can lead to duplicated global constants, only keep
57 // one copy of each constant...
John Criswelldc0de4f2003-09-18 16:22:26 +000058 Passes.add(createConstantMergePass());
59
60 // If the -s command line option was specified, strip the symbols out of the
61 // resulting program to make it smaller. -s is a GCC option that we are
62 // supporting.
John Criswelldc0de4f2003-09-18 16:22:26 +000063 if (Strip)
64 Passes.add(createSymbolStrippingPass());
65
66 // Often if the programmer does not specify proper prototypes for the
67 // functions they are calling, they end up calling a vararg version of the
68 // function that does not get a body filled in (the real function has typed
69 // arguments). This pass merges the two functions.
John Criswelldc0de4f2003-09-18 16:22:26 +000070 Passes.add(createFunctionResolvingPass());
71
72 if (Internalize) {
73 // Now that composite has been compiled, scan through the module, looking
74 // for a main function. If main is defined, mark all other functions
75 // internal.
John Criswelldc0de4f2003-09-18 16:22:26 +000076 Passes.add(createInternalizePass());
77 }
78
Chris Lattnereaa35bb2003-10-23 18:25:57 +000079 // Propagate constants at call sites into the functions they call.
80 Passes.add(createIPConstantPropagationPass());
81
John Criswelldc0de4f2003-09-18 16:22:26 +000082 // Remove unused arguments from functions...
John Criswelldc0de4f2003-09-18 16:22:26 +000083 Passes.add(createDeadArgEliminationPass());
84
Chris Lattner246ce3c2003-10-24 18:09:23 +000085 if (!DisableInline)
86 Passes.add(createFunctionInliningPass()); // Inline small functions
87
Chris Lattnercc650b62003-11-09 19:55:09 +000088 // Run a few AA driven optimizations here and now, to cleanup the code.
89 // Eventually we should put an IP AA in place here.
90
91 Passes.add(createLICMPass()); // Hoist loop invariants
92 Passes.add(createLoadValueNumberingPass()); // GVN for load instructions
93 Passes.add(createGCSEPass()); // Remove common subexprs
94
John Criswelldc0de4f2003-09-18 16:22:26 +000095 // The FuncResolve pass may leave cruft around if functions were prototyped
96 // differently than they were defined. Remove this cruft.
John Criswelldc0de4f2003-09-18 16:22:26 +000097 Passes.add(createInstructionCombiningPass());
98
99 // Delete basic blocks, which optimization passes may have killed...
John Criswelldc0de4f2003-09-18 16:22:26 +0000100 Passes.add(createCFGSimplificationPass());
101
102 // Now that we have optimized the program, discard unreachable functions...
John Criswelldc0de4f2003-09-18 16:22:26 +0000103 Passes.add(createGlobalDCEPass());
104
105 // Add the pass that writes bytecode to the output file...
106 Passes.add(new WriteBytecodePass(Out));
107
108 // Run our queue of passes all at once now, efficiently.
109 Passes.run(*M);
110
111 return 0;
112}
113
Misha Brukman1c534052003-09-30 17:42:57 +0000114/// GenerateAssembly - generates a native assembly language source file from the
115/// specified bytecode file.
116///
117/// Inputs:
118/// InputFilename - The name of the output bytecode file.
119/// OutputFilename - The name of the file to generate.
120/// llc - The pathname to use for LLC.
121/// envp - The environment to use when running LLC.
122///
123/// Outputs:
124/// None.
125///
126/// Return non-zero value on error.
127///
John Criswelldc0de4f2003-09-18 16:22:26 +0000128int
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000129GenerateAssembly(const std::string &OutputFilename,
130 const std::string &InputFilename,
131 const std::string &llc,
132 char ** const envp)
John Criswelldc0de4f2003-09-18 16:22:26 +0000133{
John Criswelldc0de4f2003-09-18 16:22:26 +0000134 // Run LLC to convert the bytecode file into assembly code.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000135 const char *cmd[8];
John Criswelldc0de4f2003-09-18 16:22:26 +0000136
Misha Brukmanb6b28432003-09-30 17:40:12 +0000137 cmd[0] = llc.c_str();
138 cmd[1] = "-f";
139 cmd[2] = "-o";
140 cmd[3] = OutputFilename.c_str();
141 cmd[4] = InputFilename.c_str();
142 cmd[5] = NULL;
John Criswelldc0de4f2003-09-18 16:22:26 +0000143
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000144 return ExecWait(cmd, envp);
John Criswelldc0de4f2003-09-18 16:22:26 +0000145}
146
Misha Brukman1c534052003-09-30 17:42:57 +0000147/// GenerateNative - generates a native assembly language source file from the
148/// specified assembly source file.
149///
150/// Inputs:
151/// InputFilename - The name of the output bytecode file.
152/// OutputFilename - The name of the file to generate.
153/// Libraries - The list of libraries with which to link.
154/// LibPaths - The list of directories in which to find libraries.
155/// gcc - The pathname to use for GGC.
156/// envp - A copy of the process's current environment.
157///
158/// Outputs:
159/// None.
160///
161/// Returns non-zero value on error.
162///
John Criswelldc0de4f2003-09-18 16:22:26 +0000163int
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000164GenerateNative(const std::string &OutputFilename,
165 const std::string &InputFilename,
166 const std::vector<std::string> &Libraries,
167 const std::vector<std::string> &LibPaths,
168 const std::string &gcc,
169 char ** const envp) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000170 // Remove these environment variables from the environment of the
171 // programs that we will execute. It appears that GCC sets these
172 // environment variables so that the programs it uses can configure
173 // themselves identically.
174 //
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000175 // However, when we invoke GCC below, we want it to use its normal
176 // configuration. Hence, we must sanitize its environment.
177 char ** clean_env = CopyEnv(envp);
John Criswelldc0de4f2003-09-18 16:22:26 +0000178 if (clean_env == NULL)
John Criswelldc0de4f2003-09-18 16:22:26 +0000179 return 1;
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000180 RemoveEnv("LIBRARY_PATH", clean_env);
181 RemoveEnv("COLLECT_GCC_OPTIONS", clean_env);
182 RemoveEnv("GCC_EXEC_PREFIX", clean_env);
183 RemoveEnv("COMPILER_PATH", clean_env);
184 RemoveEnv("COLLECT_GCC", clean_env);
John Criswelldc0de4f2003-09-18 16:22:26 +0000185
John Criswell71478b72003-09-19 20:24:23 +0000186 std::vector<const char *> cmd;
John Criswelldc0de4f2003-09-18 16:22:26 +0000187
John Criswelldc0de4f2003-09-18 16:22:26 +0000188 // Run GCC to assemble and link the program into native code.
189 //
190 // Note:
191 // We can't just assemble and link the file with the system assembler
192 // and linker because we don't know where to put the _start symbol.
193 // GCC mysteriously knows how to do it.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000194 cmd.push_back(gcc.c_str());
195 cmd.push_back("-o");
196 cmd.push_back(OutputFilename.c_str());
197 cmd.push_back(InputFilename.c_str());
John Criswelldc0de4f2003-09-18 16:22:26 +0000198
Misha Brukmanb6b28432003-09-30 17:40:12 +0000199 // Adding the library paths creates a problem for native generation. If we
200 // include the search paths from llvmgcc, then we'll be telling normal gcc
201 // to look inside of llvmgcc's library directories for libraries. This is
202 // bad because those libraries hold only bytecode files (not native object
203 // files). In the end, we attempt to link the bytecode libgcc into a native
204 // program.
Chris Lattner238cf3c2003-09-30 17:36:51 +0000205#if 0
John Criswell71478b72003-09-19 20:24:23 +0000206 // Add in the library path options.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000207 for (unsigned index=0; index < LibPaths.size(); index++) {
208 cmd.push_back("-L");
209 cmd.push_back(LibPaths[index].c_str());
John Criswell71478b72003-09-19 20:24:23 +0000210 }
211#endif
212
John Criswell71478b72003-09-19 20:24:23 +0000213 // Add in the libraries to link.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000214 std::vector<std::string> Libs(Libraries);
215 for (unsigned index = 0; index < Libs.size(); index++) {
John Criswell71478b72003-09-19 20:24:23 +0000216 Libs[index] = "-l" + Libs[index];
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000217 cmd.push_back(Libs[index].c_str());
John Criswell71478b72003-09-19 20:24:23 +0000218 }
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000219 cmd.push_back(NULL);
John Criswell71478b72003-09-19 20:24:23 +0000220
John Criswell71478b72003-09-19 20:24:23 +0000221 // Run the compiler to assembly and link together the program.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000222 return ExecWait(&(cmd[0]), clean_env);
John Criswelldc0de4f2003-09-18 16:22:26 +0000223}