Chris Lattner | a58d2be | 2003-09-30 03:24:28 +0000 | [diff] [blame] | 1 | //===- GenerateCode.cpp - Functions for generating executable files ------===// |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 2 | // |
| 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 Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 9 | // |
| 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 Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 16 | #include "gccld.h" |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/PassManager.h" |
Chris Lattner | cc650b6 | 2003-11-09 19:55:09 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/LoadValueNumbering.h" |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/Verifier.h" |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 21 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 22 | #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 Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 26 | #include "Support/SystemUtils.h" |
Chris Lattner | 246ce3c | 2003-10-24 18:09:23 +0000 | [diff] [blame] | 27 | #include "Support/CommandLine.h" |
| 28 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Chris Lattner | 246ce3c | 2003-10-24 18:09:23 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | cl::opt<bool> |
| 33 | DisableInline("disable-inlining", cl::desc("Do not run the inliner pass")); |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 34 | |
| 35 | cl::opt<bool> |
| 36 | Verify("verify", cl::desc("Verify intermediate results of all passes")); |
| 37 | |
| 38 | cl::opt<bool> |
| 39 | DisableOptimizations("disable-opt", |
| 40 | cl::desc("Do not run any optimization passes")); |
Chris Lattner | 246ce3c | 2003-10-24 18:09:23 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 43 | namespace llvm { |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 44 | |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 45 | static inline void addPass(PassManager &PM, Pass *P) { |
| 46 | // Add the pass to the pass manager... |
| 47 | PM.add(P); |
| 48 | |
| 49 | // If we are verifying all of the intermediate steps, add the verifier... |
| 50 | if (Verify) PM.add(createVerifierPass()); |
| 51 | } |
| 52 | |
Misha Brukman | 1c53405 | 2003-09-30 17:42:57 +0000 | [diff] [blame] | 53 | /// GenerateBytecode - generates a bytecode file from the specified module. |
| 54 | /// |
| 55 | /// Inputs: |
| 56 | /// M - The module for which bytecode should be generated. |
| 57 | /// Strip - Flags whether symbols should be stripped from the output. |
| 58 | /// Internalize - Flags whether all symbols should be marked internal. |
| 59 | /// Out - Pointer to file stream to which to write the output. |
| 60 | /// |
| 61 | /// Outputs: |
| 62 | /// None. |
| 63 | /// |
| 64 | /// Returns non-zero value on error. |
| 65 | /// |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 66 | int |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 67 | GenerateBytecode (Module *M, bool Strip, bool Internalize, std::ostream *Out) { |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 68 | // In addition to just linking the input from GCC, we also want to spiff it up |
| 69 | // a little bit. Do this now. |
| 70 | PassManager Passes; |
| 71 | |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 72 | if (Verify) Passes.add(createVerifierPass()); |
| 73 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 74 | // Add an appropriate TargetData instance for this module... |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 75 | addPass(Passes, new TargetData("gccld", M)); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 548e813 | 2003-11-28 09:44:03 +0000 | [diff] [blame] | 77 | // Often if the programmer does not specify proper prototypes for the |
| 78 | // functions they are calling, they end up calling a vararg version of the |
| 79 | // function that does not get a body filled in (the real function has typed |
| 80 | // arguments). This pass merges the two functions. |
| 81 | addPass(Passes, createFunctionResolvingPass()); |
| 82 | |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 83 | if (!DisableOptimizations) { |
| 84 | // Linking modules together can lead to duplicated global constants, only |
| 85 | // keep one copy of each constant... |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 86 | addPass(Passes, createConstantMergePass()); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 87 | |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 88 | // If the -s command line option was specified, strip the symbols out of the |
| 89 | // resulting program to make it smaller. -s is a GCC option that we are |
| 90 | // supporting. |
| 91 | if (Strip) |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 92 | addPass(Passes, createSymbolStrippingPass()); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 93 | |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 94 | if (Internalize) { |
| 95 | // Now that composite has been compiled, scan through the module, looking |
| 96 | // for a main function. If main is defined, mark all other functions |
| 97 | // internal. |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 98 | addPass(Passes, createInternalizePass()); |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Propagate constants at call sites into the functions they call. |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 102 | addPass(Passes, createIPConstantPropagationPass()); |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 103 | |
| 104 | // Remove unused arguments from functions... |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 105 | addPass(Passes, createDeadArgEliminationPass()); |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 106 | |
| 107 | if (!DisableInline) |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 108 | addPass(Passes, createFunctionInliningPass()); // Inline small functions |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 109 | |
| 110 | // Run a few AA driven optimizations here and now, to cleanup the code. |
| 111 | // Eventually we should put an IP AA in place here. |
| 112 | |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 113 | addPass(Passes, createLICMPass()); // Hoist loop invariants |
| 114 | addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs |
| 115 | addPass(Passes, createGCSEPass()); // Remove common subexprs |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 116 | |
| 117 | // The FuncResolve pass may leave cruft around if functions were prototyped |
| 118 | // differently than they were defined. Remove this cruft. |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 119 | addPass(Passes, createInstructionCombiningPass()); |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 120 | |
| 121 | // Delete basic blocks, which optimization passes may have killed... |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 122 | addPass(Passes, createCFGSimplificationPass()); |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 123 | |
| 124 | // Now that we have optimized the program, discard unreachable functions... |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 125 | addPass(Passes, createGlobalDCEPass()); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Chris Lattner | 0cccb18 | 2004-01-14 03:39:46 +0000 | [diff] [blame] | 128 | // Make sure everything is still good. |
| 129 | Passes.add(createVerifierPass()); |
| 130 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 131 | // Add the pass that writes bytecode to the output file... |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 132 | addPass(Passes, new WriteBytecodePass(Out)); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 133 | |
| 134 | // Run our queue of passes all at once now, efficiently. |
| 135 | Passes.run(*M); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Misha Brukman | 1c53405 | 2003-09-30 17:42:57 +0000 | [diff] [blame] | 140 | /// GenerateAssembly - generates a native assembly language source file from the |
| 141 | /// specified bytecode file. |
| 142 | /// |
| 143 | /// Inputs: |
| 144 | /// InputFilename - The name of the output bytecode file. |
| 145 | /// OutputFilename - The name of the file to generate. |
| 146 | /// llc - The pathname to use for LLC. |
| 147 | /// envp - The environment to use when running LLC. |
| 148 | /// |
| 149 | /// Outputs: |
| 150 | /// None. |
| 151 | /// |
| 152 | /// Return non-zero value on error. |
| 153 | /// |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 154 | int |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 155 | GenerateAssembly(const std::string &OutputFilename, |
| 156 | const std::string &InputFilename, |
| 157 | const std::string &llc, |
| 158 | char ** const envp) |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 159 | { |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 160 | // Run LLC to convert the bytecode file into assembly code. |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 161 | const char *cmd[8]; |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 162 | |
Misha Brukman | b6b2843 | 2003-09-30 17:40:12 +0000 | [diff] [blame] | 163 | cmd[0] = llc.c_str(); |
| 164 | cmd[1] = "-f"; |
| 165 | cmd[2] = "-o"; |
| 166 | cmd[3] = OutputFilename.c_str(); |
| 167 | cmd[4] = InputFilename.c_str(); |
| 168 | cmd[5] = NULL; |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 169 | |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 170 | return ExecWait(cmd, envp); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Misha Brukman | 1c53405 | 2003-09-30 17:42:57 +0000 | [diff] [blame] | 173 | /// GenerateNative - generates a native assembly language source file from the |
| 174 | /// specified assembly source file. |
| 175 | /// |
| 176 | /// Inputs: |
| 177 | /// InputFilename - The name of the output bytecode file. |
| 178 | /// OutputFilename - The name of the file to generate. |
| 179 | /// Libraries - The list of libraries with which to link. |
| 180 | /// LibPaths - The list of directories in which to find libraries. |
| 181 | /// gcc - The pathname to use for GGC. |
| 182 | /// envp - A copy of the process's current environment. |
| 183 | /// |
| 184 | /// Outputs: |
| 185 | /// None. |
| 186 | /// |
| 187 | /// Returns non-zero value on error. |
| 188 | /// |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 189 | int |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 190 | GenerateNative(const std::string &OutputFilename, |
| 191 | const std::string &InputFilename, |
| 192 | const std::vector<std::string> &Libraries, |
| 193 | const std::vector<std::string> &LibPaths, |
| 194 | const std::string &gcc, |
| 195 | char ** const envp) { |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 196 | // Remove these environment variables from the environment of the |
| 197 | // programs that we will execute. It appears that GCC sets these |
| 198 | // environment variables so that the programs it uses can configure |
| 199 | // themselves identically. |
| 200 | // |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 201 | // However, when we invoke GCC below, we want it to use its normal |
| 202 | // configuration. Hence, we must sanitize its environment. |
| 203 | char ** clean_env = CopyEnv(envp); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 204 | if (clean_env == NULL) |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 205 | return 1; |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 206 | RemoveEnv("LIBRARY_PATH", clean_env); |
| 207 | RemoveEnv("COLLECT_GCC_OPTIONS", clean_env); |
| 208 | RemoveEnv("GCC_EXEC_PREFIX", clean_env); |
| 209 | RemoveEnv("COMPILER_PATH", clean_env); |
| 210 | RemoveEnv("COLLECT_GCC", clean_env); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 211 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 212 | std::vector<const char *> cmd; |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 213 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 214 | // Run GCC to assemble and link the program into native code. |
| 215 | // |
| 216 | // Note: |
| 217 | // We can't just assemble and link the file with the system assembler |
| 218 | // and linker because we don't know where to put the _start symbol. |
| 219 | // GCC mysteriously knows how to do it. |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 220 | cmd.push_back(gcc.c_str()); |
| 221 | cmd.push_back("-o"); |
| 222 | cmd.push_back(OutputFilename.c_str()); |
| 223 | cmd.push_back(InputFilename.c_str()); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 224 | |
Misha Brukman | b6b2843 | 2003-09-30 17:40:12 +0000 | [diff] [blame] | 225 | // Adding the library paths creates a problem for native generation. If we |
| 226 | // include the search paths from llvmgcc, then we'll be telling normal gcc |
| 227 | // to look inside of llvmgcc's library directories for libraries. This is |
| 228 | // bad because those libraries hold only bytecode files (not native object |
| 229 | // files). In the end, we attempt to link the bytecode libgcc into a native |
| 230 | // program. |
Chris Lattner | 238cf3c | 2003-09-30 17:36:51 +0000 | [diff] [blame] | 231 | #if 0 |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 232 | // Add in the library path options. |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 233 | for (unsigned index=0; index < LibPaths.size(); index++) { |
| 234 | cmd.push_back("-L"); |
| 235 | cmd.push_back(LibPaths[index].c_str()); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 236 | } |
| 237 | #endif |
| 238 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 239 | // Add in the libraries to link. |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 240 | std::vector<std::string> Libs(Libraries); |
| 241 | for (unsigned index = 0; index < Libs.size(); index++) { |
John Criswell | 6f5592a | 2004-01-26 23:51:10 +0000 | [diff] [blame] | 242 | if (Libs[index] != "crtend") { |
| 243 | Libs[index] = "-l" + Libs[index]; |
| 244 | cmd.push_back(Libs[index].c_str()); |
| 245 | } |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 246 | } |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 247 | cmd.push_back(NULL); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 248 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 249 | // Run the compiler to assembly and link together the program. |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 250 | return ExecWait(&(cmd[0]), clean_env); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 251 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 252 | |
| 253 | } // End llvm namespace |