Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Logan | c439523 | 2010-11-27 18:54:17 +0800 | [diff] [blame] | 17 | #include "Compiler.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 18 | |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 19 | #include "Config.h" |
| 20 | |
Logan Chien | d2a5f30 | 2011-07-19 20:32:25 +0800 | [diff] [blame] | 21 | #if USE_OLD_JIT |
| 22 | #include "OldJIT/ContextManager.h" |
| 23 | #endif |
| 24 | |
Logan Chien | 4885cf8 | 2011-07-20 10:18:05 +0800 | [diff] [blame] | 25 | #if USE_DISASSEMBLER |
| 26 | #include "Disassembler/Disassembler.h" |
| 27 | #endif |
| 28 | |
Logan | 4dcd679 | 2011-02-28 05:12:00 +0800 | [diff] [blame] | 29 | #include "DebugHelper.h" |
Shih-wei Liao | 6c0c7b0 | 2011-05-21 21:47:14 -0700 | [diff] [blame] | 30 | #include "FileHandle.h" |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 31 | #include "Runtime.h" |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 32 | #include "ScriptCompiled.h" |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 33 | #include "Sha1Helper.h" |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 34 | |
Shih-wei Liao | 320b549 | 2011-06-20 22:53:33 -0700 | [diff] [blame] | 35 | #if USE_MCJIT |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 36 | #include "librsloader.h" |
Shih-wei Liao | 320b549 | 2011-06-20 22:53:33 -0700 | [diff] [blame] | 37 | #endif |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 38 | |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 39 | #include "llvm/ADT/StringRef.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 40 | |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 41 | #include "llvm/Analysis/Passes.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 42 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 43 | #include "llvm/Bitcode/ReaderWriter.h" |
| 44 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 45 | #include "llvm/CodeGen/Passes.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 46 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 47 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 48 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 49 | #include "llvm/MC/MCContext.h" |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 50 | #include "llvm/MC/SubtargetFeature.h" |
| 51 | |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 52 | #include "llvm/Transforms/IPO.h" |
| 53 | #include "llvm/Transforms/Scalar.h" |
| 54 | |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 55 | #include "llvm/Target/TargetData.h" |
| 56 | #include "llvm/Target/TargetMachine.h" |
| 57 | #include "llvm/Target/TargetOptions.h" |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 58 | |
| 59 | #include "llvm/Support/ErrorHandling.h" |
Shih-wei Liao | 898c5a9 | 2011-05-18 07:02:39 -0700 | [diff] [blame] | 60 | #include "llvm/Support/FormattedStream.h" |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 61 | #include "llvm/Support/MemoryBuffer.h" |
Logan Chien | bc9eb8f | 2011-10-21 15:17:45 +0800 | [diff] [blame] | 62 | #include "llvm/Support/TargetRegistry.h" |
| 63 | #include "llvm/Support/TargetSelect.h" |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 64 | #include "llvm/Support/raw_ostream.h" |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 65 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 66 | #include "llvm/Constants.h" |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 67 | #include "llvm/GlobalValue.h" |
| 68 | #include "llvm/Linker.h" |
| 69 | #include "llvm/LLVMContext.h" |
| 70 | #include "llvm/Metadata.h" |
| 71 | #include "llvm/Module.h" |
| 72 | #include "llvm/PassManager.h" |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 73 | #include "llvm/Type.h" |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 74 | #include "llvm/Value.h" |
| 75 | |
| 76 | #include <errno.h> |
| 77 | #include <sys/file.h> |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 78 | #include <sys/stat.h> |
| 79 | #include <sys/types.h> |
| 80 | #include <unistd.h> |
| 81 | |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 82 | #include <string.h> |
Logan | 8b77a77 | 2010-12-21 09:11:01 +0800 | [diff] [blame] | 83 | |
Logan Chien | 7890d43 | 2011-08-03 14:55:17 +0800 | [diff] [blame] | 84 | #include <algorithm> |
| 85 | #include <iterator> |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 86 | #include <string> |
| 87 | #include <vector> |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 88 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 89 | extern char* gDebugDumpDirectory; |
| 90 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 91 | namespace bcc { |
| 92 | |
| 93 | ////////////////////////////////////////////////////////////////////////////// |
| 94 | // BCC Compiler Static Variables |
| 95 | ////////////////////////////////////////////////////////////////////////////// |
| 96 | |
| 97 | bool Compiler::GlobalInitialized = false; |
| 98 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 99 | // Code generation optimization level for the compiler |
| 100 | llvm::CodeGenOpt::Level Compiler::CodeGenOptLevel; |
| 101 | |
| 102 | std::string Compiler::Triple; |
| 103 | |
| 104 | std::string Compiler::CPU; |
| 105 | |
| 106 | std::vector<std::string> Compiler::Features; |
| 107 | |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 108 | // Name of metadata node where pragma info resides (should be synced with |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 109 | // slang.cpp) |
| 110 | const llvm::StringRef Compiler::PragmaMetadataName = "#pragma"; |
| 111 | |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 112 | // Name of metadata node where exported variable names reside (should be |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 113 | // synced with slang_rs_metadata.h) |
| 114 | const llvm::StringRef Compiler::ExportVarMetadataName = "#rs_export_var"; |
| 115 | |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 116 | // Name of metadata node where exported function names reside (should be |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 117 | // synced with slang_rs_metadata.h) |
| 118 | const llvm::StringRef Compiler::ExportFuncMetadataName = "#rs_export_func"; |
| 119 | |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 120 | // Name of metadata node where RS object slot info resides (should be |
| 121 | // synced with slang_rs_metadata.h) |
| 122 | const llvm::StringRef Compiler::ObjectSlotMetadataName = "#rs_object_slots"; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 123 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 124 | // Name of metadata node where RS optimization level resides (should be |
| 125 | // synced with slang_rs_metadata.h) |
| 126 | const llvm::StringRef OptimizationLevelMetadataName = "#optimization_level"; |
| 127 | |
| 128 | |
| 129 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 130 | ////////////////////////////////////////////////////////////////////////////// |
| 131 | // Compiler |
| 132 | ////////////////////////////////////////////////////////////////////////////// |
| 133 | |
| 134 | void Compiler::GlobalInitialization() { |
| 135 | if (GlobalInitialized) |
| 136 | return; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 137 | // if (!llvm::llvm_is_multithreaded()) |
| 138 | // llvm::llvm_start_multithreaded(); |
| 139 | |
| 140 | // Set Triple, CPU and Features here |
| 141 | Triple = TARGET_TRIPLE_STRING; |
| 142 | |
Logan Chien | 3bb7707 | 2011-09-17 16:53:53 +0800 | [diff] [blame] | 143 | #if defined(DEFAULT_ARM_CODEGEN) |
| 144 | |
Stephen Hines | a12d2f3 | 2011-09-07 15:30:06 -0700 | [diff] [blame] | 145 | #if defined(ARCH_ARM_HAVE_VFP) |
| 146 | Features.push_back("+vfp3"); |
| 147 | #if !defined(ARCH_ARM_HAVE_VFP_D32) |
| 148 | Features.push_back("+d16"); |
| 149 | #endif |
| 150 | #endif |
| 151 | |
Logan Chien | 3bb7707 | 2011-09-17 16:53:53 +0800 | [diff] [blame] | 152 | // NOTE: Currently, we have to turn off the support for NEON explicitly. |
| 153 | // Since the ARMCodeEmitter.cpp is not ready for JITing NEON |
| 154 | // instructions. |
| 155 | |
| 156 | // FIXME: Re-enable NEON when ARMCodeEmitter supports NEON. |
| 157 | #define USE_ARM_NEON 0 |
| 158 | #if USE_ARM_NEON |
Joseph Wen | 51001b8 | 2011-06-23 18:56:45 -0700 | [diff] [blame] | 159 | Features.push_back("+neon"); |
| 160 | Features.push_back("+neonfp"); |
| 161 | #else |
Joseph Wen | 51001b8 | 2011-06-23 18:56:45 -0700 | [diff] [blame] | 162 | Features.push_back("-neon"); |
Logan | 4fe966f | 2011-02-27 08:26:40 +0800 | [diff] [blame] | 163 | Features.push_back("-neonfp"); |
Logan Chien | 3bb7707 | 2011-09-17 16:53:53 +0800 | [diff] [blame] | 164 | #endif // USE_ARM_NEON |
| 165 | #endif // DEFAULT_ARM_CODEGEN |
Joseph Wen | 51001b8 | 2011-06-23 18:56:45 -0700 | [diff] [blame] | 166 | |
Logan Chien | 3bb7707 | 2011-09-17 16:53:53 +0800 | [diff] [blame] | 167 | #if defined(PROVIDE_ARM_CODEGEN) |
Logan Chien | b9ef9ab | 2011-07-20 11:11:06 +0800 | [diff] [blame] | 168 | LLVMInitializeARMAsmPrinter(); |
Logan Chien | bc9eb8f | 2011-10-21 15:17:45 +0800 | [diff] [blame] | 169 | LLVMInitializeARMTargetMC(); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 170 | LLVMInitializeARMTargetInfo(); |
| 171 | LLVMInitializeARMTarget(); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 172 | #endif |
| 173 | |
Logan Chien | 3bb7707 | 2011-09-17 16:53:53 +0800 | [diff] [blame] | 174 | #if defined(PROVIDE_X86_CODEGEN) |
Logan Chien | b9ef9ab | 2011-07-20 11:11:06 +0800 | [diff] [blame] | 175 | LLVMInitializeX86AsmPrinter(); |
Logan Chien | bc9eb8f | 2011-10-21 15:17:45 +0800 | [diff] [blame] | 176 | LLVMInitializeX86TargetMC(); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 177 | LLVMInitializeX86TargetInfo(); |
| 178 | LLVMInitializeX86Target(); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 179 | #endif |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 180 | |
| 181 | #if USE_DISASSEMBLER |
| 182 | InitializeDisassembler(); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 183 | #endif |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 184 | // Below are the global settings to LLVM |
| 185 | |
| 186 | // Disable frame pointer elimination optimization |
| 187 | llvm::NoFramePointerElim = false; |
| 188 | |
| 189 | // Use hardfloat ABI |
| 190 | // |
| 191 | // TODO(all): Need to detect the CPU capability and decide whether to use |
| 192 | // softfp. To use softfp, change following 2 lines to |
| 193 | // |
| 194 | // llvm::FloatABIType = llvm::FloatABI::Soft; |
| 195 | // llvm::UseSoftFloat = true; |
| 196 | // |
Shih-wei Liao | e728cb8 | 2010-12-15 15:20:47 -0800 | [diff] [blame] | 197 | llvm::FloatABIType = llvm::FloatABI::Soft; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 198 | llvm::UseSoftFloat = false; |
| 199 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 200 | // Register the scheduler |
| 201 | llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler); |
| 202 | |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 203 | #if USE_CACHE |
Ying Wang | 26fea10 | 2011-07-05 15:12:25 -0700 | [diff] [blame] | 204 | // Read in SHA1 checksum of libbcc and libRS. |
| 205 | readSHA1(sha1LibBCC_SHA1, sizeof(sha1LibBCC_SHA1), pathLibBCC_SHA1); |
Joseph Wen | 7691907 | 2011-07-07 23:06:15 -0700 | [diff] [blame] | 206 | |
| 207 | calcFileSHA1(sha1LibRS, pathLibRS); |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 208 | #endif |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 209 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 210 | GlobalInitialized = true; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | void Compiler::LLVMErrorHandler(void *UserData, const std::string &Message) { |
| 215 | std::string *Error = static_cast<std::string*>(UserData); |
| 216 | Error->assign(Message); |
| 217 | LOGE("%s", Message.c_str()); |
| 218 | exit(1); |
| 219 | } |
| 220 | |
| 221 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 222 | #if USE_OLD_JIT |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 223 | CodeMemoryManager *Compiler::createCodeMemoryManager() { |
| 224 | mCodeMemMgr.reset(new CodeMemoryManager()); |
| 225 | return mCodeMemMgr.get(); |
| 226 | } |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 227 | #endif |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 228 | |
| 229 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 230 | #if USE_OLD_JIT |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 231 | CodeEmitter *Compiler::createCodeEmitter() { |
Logan | 7dcaac9 | 2011-01-06 04:26:23 +0800 | [diff] [blame] | 232 | mCodeEmitter.reset(new CodeEmitter(mpResult, mCodeMemMgr.get())); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 233 | return mCodeEmitter.get(); |
| 234 | } |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 235 | #endif |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 236 | |
| 237 | |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 238 | Compiler::Compiler(ScriptCompiled *result) |
| 239 | : mpResult(result), |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 240 | #if USE_MCJIT |
| 241 | mRSExecutable(NULL), |
| 242 | #endif |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 243 | mpSymbolLookupFn(NULL), |
| 244 | mpSymbolLookupContext(NULL), |
| 245 | mContext(NULL), |
| 246 | mModule(NULL), |
| 247 | mHasLinked(false) /* Turn off linker */ { |
| 248 | llvm::remove_fatal_error_handler(); |
| 249 | llvm::install_fatal_error_handler(LLVMErrorHandler, &mError); |
| 250 | mContext = new llvm::LLVMContext(); |
| 251 | return; |
| 252 | } |
| 253 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 254 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 255 | llvm::Module *Compiler::parseBitcodeFile(llvm::MemoryBuffer *MEM) { |
| 256 | llvm::Module *result = llvm::ParseBitcodeFile(MEM, *mContext, &mError); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 257 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 258 | if (!result) { |
| 259 | LOGE("Unable to ParseBitcodeFile: %s\n", mError.c_str()); |
| 260 | return NULL; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 261 | } |
| 262 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 263 | return result; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 267 | int Compiler::linkModule(llvm::Module *moduleWith) { |
Logan Chien | bc9eb8f | 2011-10-21 15:17:45 +0800 | [diff] [blame] | 268 | if (llvm::Linker::LinkModules(mModule, moduleWith, |
| 269 | llvm::Linker::DestroySource, |
| 270 | &mError) != 0) { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 271 | return hasError(); |
| 272 | } |
| 273 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 274 | // Everything for linking should be settled down here with no error occurs |
| 275 | mHasLinked = true; |
| 276 | return hasError(); |
| 277 | } |
| 278 | |
| 279 | |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 280 | int Compiler::compile(bool compileOnly) { |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 281 | llvm::Target const *Target = NULL; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 282 | llvm::TargetData *TD = NULL; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 283 | llvm::TargetMachine *TM = NULL; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 284 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 285 | std::string FeaturesStr; |
| 286 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 287 | if (mModule == NULL) // No module was loaded |
| 288 | return 0; |
| 289 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 290 | llvm::NamedMDNode const *PragmaMetadata; |
| 291 | llvm::NamedMDNode const *ExportVarMetadata; |
| 292 | llvm::NamedMDNode const *ExportFuncMetadata; |
| 293 | llvm::NamedMDNode const *ObjectSlotMetadata; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 294 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 295 | llvm::NamedMDNode const *OptimizationLevelMetadata = |
| 296 | mModule->getNamedMetadata(OptimizationLevelMetadataName); |
| 297 | |
| 298 | // Default to maximum optimization in the absence of named metadata node |
| 299 | int OptimizationLevel = 3; |
| 300 | if (OptimizationLevelMetadata) { |
| 301 | llvm::ConstantInt* OL = llvm::dyn_cast<llvm::ConstantInt>( |
| 302 | OptimizationLevelMetadata->getOperand(0)->getOperand(0)); |
| 303 | OptimizationLevel = OL->getZExtValue(); |
| 304 | } |
| 305 | |
| 306 | if (OptimizationLevel == 0) { |
| 307 | CodeGenOptLevel = llvm::CodeGenOpt::None; |
| 308 | } else if (OptimizationLevel == 1) { |
| 309 | CodeGenOptLevel = llvm::CodeGenOpt::Less; |
| 310 | } else if (OptimizationLevel == 2) { |
| 311 | CodeGenOptLevel = llvm::CodeGenOpt::Default; |
| 312 | } else if (OptimizationLevel == 3) { |
| 313 | CodeGenOptLevel = llvm::CodeGenOpt::Aggressive; |
| 314 | } |
| 315 | |
| 316 | // not the best place for this, but we need to set the register allocation |
| 317 | // policy after we read the optimization_level metadata from the bitcode |
| 318 | |
| 319 | // Register allocation policy: |
| 320 | // createFastRegisterAllocator: fast but bad quality |
| 321 | // createLinearScanRegisterAllocator: not so fast but good quality |
| 322 | llvm::RegisterRegAlloc::setDefault |
| 323 | ((CodeGenOptLevel == llvm::CodeGenOpt::None) ? |
| 324 | llvm::createFastRegisterAllocator : |
| 325 | llvm::createLinearScanRegisterAllocator); |
| 326 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 327 | |
| 328 | // Create TargetMachine |
| 329 | Target = llvm::TargetRegistry::lookupTarget(Triple, mError); |
| 330 | if (hasError()) |
| 331 | goto on_bcc_compile_error; |
| 332 | |
| 333 | if (!CPU.empty() || !Features.empty()) { |
| 334 | llvm::SubtargetFeatures F; |
Logan | a4994f5 | 2010-11-27 14:06:02 +0800 | [diff] [blame] | 335 | |
| 336 | for (std::vector<std::string>::const_iterator |
| 337 | I = Features.begin(), E = Features.end(); I != E; I++) { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 338 | F.AddFeature(*I); |
Logan | a4994f5 | 2010-11-27 14:06:02 +0800 | [diff] [blame] | 339 | } |
| 340 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 341 | FeaturesStr = F.getString(); |
| 342 | } |
| 343 | |
Logan Chien | bc9eb8f | 2011-10-21 15:17:45 +0800 | [diff] [blame] | 344 | #if defined(DEFAULT_X86_64_CODEGEN) |
| 345 | // Data address in X86_64 architecture may reside in a far-away place |
Logan Chien | d3fe15c | 2011-07-20 22:08:39 +0800 | [diff] [blame] | 346 | TM = Target->createTargetMachine(Triple, CPU, FeaturesStr, |
Logan Chien | bc9eb8f | 2011-10-21 15:17:45 +0800 | [diff] [blame] | 347 | llvm::Reloc::Static, |
| 348 | llvm::CodeModel::Medium); |
| 349 | #else |
| 350 | // This is set for the linker (specify how large of the virtual addresses |
| 351 | // we can access for all unknown symbols.) |
| 352 | TM = Target->createTargetMachine(Triple, CPU, FeaturesStr, |
| 353 | llvm::Reloc::Static, |
| 354 | llvm::CodeModel::Small); |
| 355 | #endif |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 356 | if (TM == NULL) { |
| 357 | setError("Failed to create target machine implementation for the" |
| 358 | " specified triple '" + Triple + "'"); |
| 359 | goto on_bcc_compile_error; |
| 360 | } |
| 361 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 362 | // Get target data from Module |
| 363 | TD = new llvm::TargetData(mModule); |
| 364 | |
| 365 | // Load named metadata |
| 366 | ExportVarMetadata = mModule->getNamedMetadata(ExportVarMetadataName); |
| 367 | ExportFuncMetadata = mModule->getNamedMetadata(ExportFuncMetadataName); |
| 368 | PragmaMetadata = mModule->getNamedMetadata(PragmaMetadataName); |
| 369 | ObjectSlotMetadata = mModule->getNamedMetadata(ObjectSlotMetadataName); |
| 370 | |
| 371 | // Perform link-time optimization if we have multiple modules |
| 372 | if (mHasLinked) { |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 373 | runLTO(new llvm::TargetData(*TD), ExportVarMetadata, ExportFuncMetadata, |
| 374 | CodeGenOptLevel); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // Perform code generation |
| 378 | #if USE_OLD_JIT |
| 379 | if (runCodeGen(new llvm::TargetData(*TD), TM, |
| 380 | ExportVarMetadata, ExportFuncMetadata) != 0) { |
| 381 | goto on_bcc_compile_error; |
| 382 | } |
| 383 | #endif |
| 384 | |
| 385 | #if USE_MCJIT |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 386 | if (runMCCodeGen(new llvm::TargetData(*TD), TM) != 0) { |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 387 | goto on_bcc_compile_error; |
| 388 | } |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 389 | |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 390 | if (compileOnly) |
| 391 | return 0; |
| 392 | |
| 393 | // Load the ELF Object |
| 394 | mRSExecutable = |
| 395 | rsloaderCreateExec((unsigned char *)&*mEmittedELFExecutable.begin(), |
| 396 | mEmittedELFExecutable.size(), |
| 397 | &resolveSymbolAdapter, this); |
| 398 | |
| 399 | if (!mRSExecutable) { |
| 400 | setError("Fail to load emitted ELF relocatable file"); |
| 401 | goto on_bcc_compile_error; |
| 402 | } |
| 403 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 404 | rsloaderUpdateSectionHeaders(mRSExecutable, |
| 405 | (unsigned char*) mEmittedELFExecutable.begin()); |
| 406 | |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 407 | if (ExportVarMetadata) { |
| 408 | ScriptCompiled::ExportVarList &varList = mpResult->mExportVars; |
| 409 | std::vector<std::string> &varNameList = mpResult->mExportVarsName; |
| 410 | |
| 411 | for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) { |
| 412 | llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i); |
| 413 | if (ExportVar != NULL && ExportVar->getNumOperands() > 1) { |
| 414 | llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0); |
| 415 | if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) { |
| 416 | llvm::StringRef ExportVarName = |
| 417 | static_cast<llvm::MDString*>(ExportVarNameMDS)->getString(); |
| 418 | |
| 419 | varList.push_back( |
| 420 | rsloaderGetSymbolAddress(mRSExecutable, |
| 421 | ExportVarName.str().c_str())); |
| 422 | varNameList.push_back(ExportVarName.str()); |
| 423 | #if DEBUG_MCJIT_REFLECT |
| 424 | LOGD("runMCCodeGen(): Exported Var: %s @ %p\n", ExportVarName.str().c_str(), |
| 425 | varList.back()); |
| 426 | #endif |
| 427 | continue; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | varList.push_back(NULL); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | if (ExportFuncMetadata) { |
| 436 | ScriptCompiled::ExportFuncList &funcList = mpResult->mExportFuncs; |
| 437 | std::vector<std::string> &funcNameList = mpResult->mExportFuncsName; |
| 438 | |
| 439 | for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) { |
| 440 | llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i); |
| 441 | if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) { |
| 442 | llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0); |
| 443 | if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) { |
| 444 | llvm::StringRef ExportFuncName = |
| 445 | static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString(); |
| 446 | |
| 447 | funcList.push_back( |
| 448 | rsloaderGetSymbolAddress(mRSExecutable, |
| 449 | ExportFuncName.str().c_str())); |
| 450 | funcNameList.push_back(ExportFuncName.str()); |
| 451 | #if DEBUG_MCJIT_RELECT |
| 452 | LOGD("runMCCodeGen(): Exported Func: %s @ %p\n", ExportFuncName.str().c_str(), |
| 453 | funcList.back()); |
| 454 | #endif |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
Logan Chien | 4885cf8 | 2011-07-20 10:18:05 +0800 | [diff] [blame] | 460 | #if DEBUG_MCJIT_DISASSEMBLER |
Shih-wei Liao | 90cd3d1 | 2011-06-20 15:43:34 -0700 | [diff] [blame] | 461 | { |
Shih-wei Liao | d3c551f | 2011-07-01 04:28:27 -0700 | [diff] [blame] | 462 | // Get MC codegen emitted function name list |
| 463 | size_t func_list_size = rsloaderGetFuncCount(mRSExecutable); |
| 464 | std::vector<char const *> func_list(func_list_size, NULL); |
| 465 | rsloaderGetFuncNameList(mRSExecutable, func_list_size, &*func_list.begin()); |
Shih-wei Liao | 320b549 | 2011-06-20 22:53:33 -0700 | [diff] [blame] | 466 | |
Shih-wei Liao | d3c551f | 2011-07-01 04:28:27 -0700 | [diff] [blame] | 467 | // Disassemble each function |
Shih-wei Liao | 90cd3d1 | 2011-06-20 15:43:34 -0700 | [diff] [blame] | 468 | for (size_t i = 0; i < func_list_size; ++i) { |
| 469 | void *func = rsloaderGetSymbolAddress(mRSExecutable, func_list[i]); |
| 470 | if (func) { |
| 471 | size_t size = rsloaderGetSymbolSize(mRSExecutable, func_list[i]); |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 472 | Disassemble(DEBUG_MCJIT_DISASSEMBLER_FILE, |
| 473 | Target, TM, func_list[i], (unsigned char const *)func, size); |
Shih-wei Liao | 90cd3d1 | 2011-06-20 15:43:34 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | } |
| 477 | #endif |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 478 | #endif |
| 479 | |
| 480 | // Read pragma information from the metadata node of the module. |
| 481 | if (PragmaMetadata) { |
| 482 | ScriptCompiled::PragmaList &pragmaList = mpResult->mPragmas; |
| 483 | |
| 484 | for (int i = 0, e = PragmaMetadata->getNumOperands(); i != e; i++) { |
| 485 | llvm::MDNode *Pragma = PragmaMetadata->getOperand(i); |
| 486 | if (Pragma != NULL && |
| 487 | Pragma->getNumOperands() == 2 /* should have exactly 2 operands */) { |
| 488 | llvm::Value *PragmaNameMDS = Pragma->getOperand(0); |
| 489 | llvm::Value *PragmaValueMDS = Pragma->getOperand(1); |
| 490 | |
| 491 | if ((PragmaNameMDS->getValueID() == llvm::Value::MDStringVal) && |
| 492 | (PragmaValueMDS->getValueID() == llvm::Value::MDStringVal)) { |
| 493 | llvm::StringRef PragmaName = |
| 494 | static_cast<llvm::MDString*>(PragmaNameMDS)->getString(); |
| 495 | llvm::StringRef PragmaValue = |
| 496 | static_cast<llvm::MDString*>(PragmaValueMDS)->getString(); |
| 497 | |
| 498 | pragmaList.push_back( |
| 499 | std::make_pair(std::string(PragmaName.data(), |
| 500 | PragmaName.size()), |
| 501 | std::string(PragmaValue.data(), |
| 502 | PragmaValue.size()))); |
Shih-wei Liao | 9f73de0 | 2011-07-01 04:40:24 -0700 | [diff] [blame] | 503 | #if DEBUG_BCC_REFLECT |
| 504 | LOGD("compile(): Pragma: %s -> %s\n", |
| 505 | pragmaList.back().first.c_str(), |
| 506 | pragmaList.back().second.c_str()); |
Shih-wei Liao | 749a51c | 2011-06-17 16:02:18 -0700 | [diff] [blame] | 507 | #endif |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | if (ObjectSlotMetadata) { |
| 514 | ScriptCompiled::ObjectSlotList &objectSlotList = mpResult->mObjectSlots; |
| 515 | |
| 516 | for (int i = 0, e = ObjectSlotMetadata->getNumOperands(); i != e; i++) { |
| 517 | llvm::MDNode *ObjectSlot = ObjectSlotMetadata->getOperand(i); |
| 518 | if (ObjectSlot != NULL && |
| 519 | ObjectSlot->getNumOperands() == 1) { |
| 520 | llvm::Value *SlotMDS = ObjectSlot->getOperand(0); |
| 521 | if (SlotMDS->getValueID() == llvm::Value::MDStringVal) { |
| 522 | llvm::StringRef Slot = |
| 523 | static_cast<llvm::MDString*>(SlotMDS)->getString(); |
| 524 | uint32_t USlot = 0; |
| 525 | if (Slot.getAsInteger(10, USlot)) { |
| 526 | setError("Non-integer object slot value '" + Slot.str() + "'"); |
| 527 | goto on_bcc_compile_error; |
| 528 | } |
| 529 | objectSlotList.push_back(USlot); |
Shih-wei Liao | 9f73de0 | 2011-07-01 04:40:24 -0700 | [diff] [blame] | 530 | #if DEBUG_BCC_REFLECT |
Shih-wei Liao | 749a51c | 2011-06-17 16:02:18 -0700 | [diff] [blame] | 531 | LOGD("compile(): RefCount Slot: %s @ %u\n", Slot.str().c_str(), USlot); |
| 532 | #endif |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | } |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | on_bcc_compile_error: |
| 539 | // LOGE("on_bcc_compiler_error"); |
| 540 | if (TD) { |
| 541 | delete TD; |
| 542 | } |
| 543 | |
| 544 | if (TM) { |
| 545 | delete TM; |
| 546 | } |
| 547 | |
| 548 | if (mError.empty()) { |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | // LOGE(getErrorMessage()); |
| 553 | return 1; |
| 554 | } |
| 555 | |
| 556 | |
| 557 | #if USE_OLD_JIT |
| 558 | int Compiler::runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM, |
| 559 | llvm::NamedMDNode const *ExportVarMetadata, |
| 560 | llvm::NamedMDNode const *ExportFuncMetadata) { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 561 | // Create memory manager for creation of code emitter later. |
| 562 | if (!mCodeMemMgr.get() && !createCodeMemoryManager()) { |
| 563 | setError("Failed to startup memory management for further compilation"); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 564 | return 1; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 565 | } |
Logan | 02286cb | 2011-01-07 00:30:47 +0800 | [diff] [blame] | 566 | |
| 567 | mpResult->mContext = (char *) (mCodeMemMgr.get()->getCodeMemBase()); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 568 | |
| 569 | // Create code emitter |
| 570 | if (!mCodeEmitter.get()) { |
| 571 | if (!createCodeEmitter()) { |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 572 | setError("Failed to create machine code emitter for compilation"); |
| 573 | return 1; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 574 | } |
| 575 | } else { |
| 576 | // Reuse the code emitter |
| 577 | mCodeEmitter->reset(); |
| 578 | } |
| 579 | |
| 580 | mCodeEmitter->setTargetMachine(*TM); |
| 581 | mCodeEmitter->registerSymbolCallback(mpSymbolLookupFn, |
| 582 | mpSymbolLookupContext); |
| 583 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 584 | // Create code-gen pass to run the code emitter |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 585 | llvm::OwningPtr<llvm::FunctionPassManager> CodeGenPasses( |
| 586 | new llvm::FunctionPassManager(mModule)); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 587 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 588 | // Add TargetData to code generation pass manager |
| 589 | CodeGenPasses->add(TD); |
| 590 | |
| 591 | // Add code emit passes |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 592 | if (TM->addPassesToEmitMachineCode(*CodeGenPasses, |
| 593 | *mCodeEmitter, |
| 594 | CodeGenOptLevel)) { |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 595 | setError("The machine code emission is not supported on '" + Triple + "'"); |
| 596 | return 1; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 597 | } |
| 598 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 599 | // Run the code emitter on every non-declaration function in the module |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 600 | CodeGenPasses->doInitialization(); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 601 | for (llvm::Module::iterator |
| 602 | I = mModule->begin(), E = mModule->end(); I != E; I++) { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 603 | if (!I->isDeclaration()) { |
| 604 | CodeGenPasses->run(*I); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | CodeGenPasses->doFinalization(); |
Logan Chien | b0ceca2 | 2011-06-12 13:34:49 +0800 | [diff] [blame] | 609 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 610 | // Copy the global address mapping from code emitter and remapping |
| 611 | if (ExportVarMetadata) { |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 612 | ScriptCompiled::ExportVarList &varList = mpResult->mExportVars; |
| 613 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 614 | for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) { |
| 615 | llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i); |
| 616 | if (ExportVar != NULL && ExportVar->getNumOperands() > 1) { |
| 617 | llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0); |
| 618 | if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) { |
| 619 | llvm::StringRef ExportVarName = |
| 620 | static_cast<llvm::MDString*>(ExportVarNameMDS)->getString(); |
| 621 | |
| 622 | CodeEmitter::global_addresses_const_iterator I, E; |
| 623 | for (I = mCodeEmitter->global_address_begin(), |
| 624 | E = mCodeEmitter->global_address_end(); |
| 625 | I != E; I++) { |
| 626 | if (I->first->getValueID() != llvm::Value::GlobalVariableVal) |
| 627 | continue; |
| 628 | if (ExportVarName == I->first->getName()) { |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 629 | varList.push_back(I->second); |
Shih-wei Liao | 9f73de0 | 2011-07-01 04:40:24 -0700 | [diff] [blame] | 630 | #if DEBUG_BCC_REFLECT |
Shih-wei Liao | 749a51c | 2011-06-17 16:02:18 -0700 | [diff] [blame] | 631 | LOGD("runCodeGen(): Exported VAR: %s @ %p\n", ExportVarName.str().c_str(), I->second); |
| 632 | #endif |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 633 | break; |
| 634 | } |
| 635 | } |
| 636 | if (I != mCodeEmitter->global_address_end()) |
| 637 | continue; // found |
Logan Chien | 7d1bf58 | 2011-06-13 23:22:40 +0800 | [diff] [blame] | 638 | |
Shih-wei Liao | 9f73de0 | 2011-07-01 04:40:24 -0700 | [diff] [blame] | 639 | #if DEBUG_BCC_REFLECT |
Shih-wei Liao | 749a51c | 2011-06-17 16:02:18 -0700 | [diff] [blame] | 640 | LOGD("runCodeGen(): Exported VAR: %s @ %p\n", |
Logan Chien | 7d1bf58 | 2011-06-13 23:22:40 +0800 | [diff] [blame] | 641 | ExportVarName.str().c_str(), (void *)0); |
Shih-wei Liao | 749a51c | 2011-06-17 16:02:18 -0700 | [diff] [blame] | 642 | #endif |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | // if reaching here, we know the global variable record in metadata is |
| 646 | // not found. So we make an empty slot |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 647 | varList.push_back(NULL); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 648 | } |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 649 | |
Stephen Hines | bbcef8a | 2011-05-04 19:40:10 -0700 | [diff] [blame] | 650 | bccAssert((varList.size() == ExportVarMetadata->getNumOperands()) && |
| 651 | "Number of slots doesn't match the number of export variables!"); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | if (ExportFuncMetadata) { |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 655 | ScriptCompiled::ExportFuncList &funcList = mpResult->mExportFuncs; |
| 656 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 657 | for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) { |
| 658 | llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i); |
| 659 | if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) { |
| 660 | llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0); |
| 661 | if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) { |
| 662 | llvm::StringRef ExportFuncName = |
| 663 | static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString(); |
Logan | 7dcaac9 | 2011-01-06 04:26:23 +0800 | [diff] [blame] | 664 | funcList.push_back(mpResult->lookup(ExportFuncName.str().c_str())); |
Shih-wei Liao | 9f73de0 | 2011-07-01 04:40:24 -0700 | [diff] [blame] | 665 | #if DEBUG_BCC_REFLECT |
Shih-wei Liao | 749a51c | 2011-06-17 16:02:18 -0700 | [diff] [blame] | 666 | LOGD("runCodeGen(): Exported Func: %s @ %p\n", ExportFuncName.str().c_str(), |
Logan Chien | 7d1bf58 | 2011-06-13 23:22:40 +0800 | [diff] [blame] | 667 | funcList.back()); |
Shih-wei Liao | 749a51c | 2011-06-17 16:02:18 -0700 | [diff] [blame] | 668 | #endif |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | // Tell code emitter now can release the memory using during the JIT since |
| 675 | // we have done the code emission |
| 676 | mCodeEmitter->releaseUnnecessary(); |
| 677 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 678 | return 0; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 679 | } |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 680 | #endif // USE_OLD_JIT |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 681 | |
| 682 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 683 | #if USE_MCJIT |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 684 | int Compiler::runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM) { |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 685 | // Decorate mEmittedELFExecutable with formatted ostream |
| 686 | llvm::raw_svector_ostream OutSVOS(mEmittedELFExecutable); |
| 687 | |
| 688 | // Relax all machine instructions |
| 689 | TM->setMCRelaxAll(/* RelaxAll= */ true); |
| 690 | |
| 691 | // Create MC code generation pass manager |
| 692 | llvm::PassManager MCCodeGenPasses; |
| 693 | |
| 694 | // Add TargetData to MC code generation pass manager |
| 695 | MCCodeGenPasses.add(TD); |
| 696 | |
| 697 | // Add MC code generation passes to pass manager |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 698 | llvm::MCContext *Ctx = NULL; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 699 | if (TM->addPassesToEmitMC(MCCodeGenPasses, Ctx, OutSVOS, |
| 700 | CodeGenOptLevel, false)) { |
| 701 | setError("Fail to add passes to emit file"); |
| 702 | return 1; |
| 703 | } |
| 704 | |
| 705 | MCCodeGenPasses.run(*mModule); |
| 706 | OutSVOS.flush(); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 707 | return 0; |
| 708 | } |
| 709 | #endif // USE_MCJIT |
| 710 | |
| 711 | |
| 712 | int Compiler::runLTO(llvm::TargetData *TD, |
| 713 | llvm::NamedMDNode const *ExportVarMetadata, |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 714 | llvm::NamedMDNode const *ExportFuncMetadata, |
| 715 | llvm::CodeGenOpt::Level OptimizationLevel) { |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 716 | // Collect All Exported Symbols |
| 717 | std::vector<const char*> ExportSymbols; |
| 718 | |
| 719 | // Note: This is a workaround for getting export variable and function name. |
| 720 | // We should refine it soon. |
| 721 | if (ExportVarMetadata) { |
| 722 | for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) { |
| 723 | llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i); |
| 724 | if (ExportVar != NULL && ExportVar->getNumOperands() > 1) { |
| 725 | llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0); |
| 726 | if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) { |
| 727 | llvm::StringRef ExportVarName = |
| 728 | static_cast<llvm::MDString*>(ExportVarNameMDS)->getString(); |
| 729 | ExportSymbols.push_back(ExportVarName.data()); |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | if (ExportFuncMetadata) { |
| 736 | for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) { |
| 737 | llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i); |
| 738 | if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) { |
| 739 | llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0); |
| 740 | if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) { |
| 741 | llvm::StringRef ExportFuncName = |
| 742 | static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString(); |
| 743 | ExportSymbols.push_back(ExportFuncName.data()); |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
Logan Chien | 7890d43 | 2011-08-03 14:55:17 +0800 | [diff] [blame] | 749 | // TODO(logan): Remove this after we have finished the |
| 750 | // bccMarkExternalSymbol API. |
| 751 | |
Stephen Hines | 6416010 | 2011-09-01 17:30:26 -0700 | [diff] [blame] | 752 | // root(), init(), and .rs.dtor() are born to be exported |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 753 | ExportSymbols.push_back("root"); |
| 754 | ExportSymbols.push_back("init"); |
Stephen Hines | 6416010 | 2011-09-01 17:30:26 -0700 | [diff] [blame] | 755 | ExportSymbols.push_back(".rs.dtor"); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 756 | |
Logan Chien | 7890d43 | 2011-08-03 14:55:17 +0800 | [diff] [blame] | 757 | // User-defined exporting symbols |
| 758 | std::vector<char const *> const &UserDefinedExternalSymbols = |
| 759 | mpResult->getUserDefinedExternalSymbols(); |
| 760 | |
| 761 | std::copy(UserDefinedExternalSymbols.begin(), |
| 762 | UserDefinedExternalSymbols.end(), |
| 763 | std::back_inserter(ExportSymbols)); |
| 764 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 765 | llvm::PassManager LTOPasses; |
| 766 | |
| 767 | // Add TargetData to LTO passes |
| 768 | LTOPasses.add(TD); |
| 769 | |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 770 | // We now create passes list performing LTO. These are copied from |
| 771 | // (including comments) llvm::createStandardLTOPasses(). |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 772 | // Only a subset of these LTO passes are enabled in optimization level 0 |
| 773 | // as they interfere with interactive debugging. |
| 774 | // FIXME: figure out which passes (if any) makes sense for levels 1 and 2 |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 775 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 776 | if (OptimizationLevel != llvm::CodeGenOpt::None) { |
| 777 | // Internalize all other symbols not listed in ExportSymbols |
| 778 | LTOPasses.add(llvm::createInternalizePass(ExportSymbols)); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 779 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 780 | // Propagate constants at call sites into the functions they call. This |
| 781 | // opens opportunities for globalopt (and inlining) by substituting |
| 782 | // function pointers passed as arguments to direct uses of functions. |
| 783 | LTOPasses.add(llvm::createIPSCCPPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 784 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 785 | // Now that we internalized some globals, see if we can hack on them! |
| 786 | LTOPasses.add(llvm::createGlobalOptimizerPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 787 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 788 | // Linking modules together can lead to duplicated global constants, only |
| 789 | // keep one copy of each constant... |
| 790 | LTOPasses.add(llvm::createConstantMergePass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 791 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 792 | // Remove unused arguments from functions... |
| 793 | LTOPasses.add(llvm::createDeadArgEliminationPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 794 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 795 | // Reduce the code after globalopt and ipsccp. Both can open up |
| 796 | // significant simplification opportunities, and both can propagate |
| 797 | // functions through function pointers. When this happens, we often have |
| 798 | // to resolve varargs calls, etc, so let instcombine do this. |
| 799 | LTOPasses.add(llvm::createInstructionCombiningPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 800 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 801 | // Inline small functions |
| 802 | LTOPasses.add(llvm::createFunctionInliningPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 803 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 804 | // Remove dead EH info. |
| 805 | LTOPasses.add(llvm::createPruneEHPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 806 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 807 | // Internalize the globals again after inlining |
| 808 | LTOPasses.add(llvm::createGlobalOptimizerPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 809 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 810 | // Remove dead functions. |
| 811 | LTOPasses.add(llvm::createGlobalDCEPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 812 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 813 | // If we didn't decide to inline a function, check to see if we can |
| 814 | // transform it to pass arguments by value instead of by reference. |
| 815 | LTOPasses.add(llvm::createArgumentPromotionPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 816 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 817 | // The IPO passes may leave cruft around. Clean up after them. |
| 818 | LTOPasses.add(llvm::createInstructionCombiningPass()); |
| 819 | LTOPasses.add(llvm::createJumpThreadingPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 820 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 821 | // Break up allocas |
| 822 | LTOPasses.add(llvm::createScalarReplAggregatesPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 823 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 824 | // Run a few AA driven optimizations here and now, to cleanup the code. |
| 825 | LTOPasses.add(llvm::createFunctionAttrsPass()); // Add nocapture. |
| 826 | LTOPasses.add(llvm::createGlobalsModRefPass()); // IP alias analysis. |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 827 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 828 | // Hoist loop invariants. |
| 829 | LTOPasses.add(llvm::createLICMPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 830 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 831 | // Remove redundancies. |
| 832 | LTOPasses.add(llvm::createGVNPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 833 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 834 | // Remove dead memcpys. |
| 835 | LTOPasses.add(llvm::createMemCpyOptPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 836 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 837 | // Nuke dead stores. |
| 838 | LTOPasses.add(llvm::createDeadStoreEliminationPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 839 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 840 | // Cleanup and simplify the code after the scalar optimizations. |
| 841 | LTOPasses.add(llvm::createInstructionCombiningPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 842 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 843 | LTOPasses.add(llvm::createJumpThreadingPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 844 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 845 | // Delete basic blocks, which optimization passes may have killed. |
| 846 | LTOPasses.add(llvm::createCFGSimplificationPass()); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 847 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 848 | // Now that we have optimized the program, discard unreachable functions. |
| 849 | LTOPasses.add(llvm::createGlobalDCEPass()); |
| 850 | |
| 851 | } else { |
| 852 | LTOPasses.add(llvm::createInternalizePass(ExportSymbols)); |
| 853 | LTOPasses.add(llvm::createGlobalOptimizerPass()); |
| 854 | LTOPasses.add(llvm::createConstantMergePass()); |
| 855 | } |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 856 | |
| 857 | LTOPasses.run(*mModule); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 858 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 859 | #if ANDROID_ENGINEERING_BUILD |
| 860 | if (0 != gDebugDumpDirectory) { |
| 861 | std::string errs; |
| 862 | std::string Filename(gDebugDumpDirectory); |
| 863 | Filename += "/post-lto-module.ll"; |
| 864 | llvm::raw_fd_ostream FS(Filename.c_str(), errs); |
| 865 | mModule->print(FS, 0); |
| 866 | FS.close(); |
| 867 | } |
| 868 | #endif |
| 869 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 870 | return 0; |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 874 | #if USE_MCJIT |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 875 | void *Compiler::getSymbolAddress(char const *name) { |
| 876 | return rsloaderGetSymbolAddress(mRSExecutable, name); |
| 877 | } |
| 878 | #endif |
| 879 | |
| 880 | |
| 881 | #if USE_MCJIT |
| 882 | void *Compiler::resolveSymbolAdapter(void *context, char const *name) { |
| 883 | Compiler *self = reinterpret_cast<Compiler *>(context); |
| 884 | |
| 885 | if (void *Addr = FindRuntimeFunction(name)) { |
| 886 | return Addr; |
| 887 | } |
| 888 | |
| 889 | if (self->mpSymbolLookupFn) { |
| 890 | if (void *Addr = self->mpSymbolLookupFn(self->mpSymbolLookupContext, name)) { |
| 891 | return Addr; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | LOGE("Unable to resolve symbol: %s\n", name); |
| 896 | return NULL; |
| 897 | } |
| 898 | #endif |
| 899 | |
| 900 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 901 | Compiler::~Compiler() { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 902 | delete mModule; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 903 | delete mContext; |
Logan | a4994f5 | 2010-11-27 14:06:02 +0800 | [diff] [blame] | 904 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 905 | #if USE_MCJIT |
| 906 | rsloaderDisposeExec(mRSExecutable); |
| 907 | #endif |
| 908 | |
Logan | a4994f5 | 2010-11-27 14:06:02 +0800 | [diff] [blame] | 909 | // llvm::llvm_shutdown(); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 910 | } |
| 911 | |
Shih-wei Liao | 90cd3d1 | 2011-06-20 15:43:34 -0700 | [diff] [blame] | 912 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 913 | } // namespace bcc |