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 | |
| 17 | #define LOG_TAG "bcc" |
| 18 | #include <cutils/log.h> |
| 19 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 20 | #if defined(__arm__) |
| 21 | # define DEFAULT_ARM_CODEGEN |
| 22 | # define PROVIDE_ARM_CODEGEN |
| 23 | #elif defined(__i386__) |
| 24 | # define DEFAULT_X86_CODEGEN |
| 25 | # define PROVIDE_X86_CODEGEN |
| 26 | #elif defined(__x86_64__) |
| 27 | # define DEFAULT_X64_CODEGEN |
| 28 | # define PROVIDE_X64_CODEGEN |
| 29 | #endif |
| 30 | |
| 31 | #if defined(FORCE_ARM_CODEGEN) |
| 32 | # define DEFAULT_ARM_CODEGEN |
| 33 | # undef DEFAULT_X86_CODEGEN |
| 34 | # undef DEFAULT_X64_CODEGEN |
| 35 | # define PROVIDE_ARM_CODEGEN |
| 36 | # undef PROVIDE_X86_CODEGEN |
| 37 | # undef PROVIDE_X64_CODEGEN |
| 38 | #elif defined(FORCE_X86_CODEGEN) |
| 39 | # undef DEFAULT_ARM_CODEGEN |
| 40 | # define DEFAULT_X86_CODEGEN |
| 41 | # undef DEFAULT_X64_CODEGEN |
| 42 | # undef PROVIDE_ARM_CODEGEN |
| 43 | # define PROVIDE_X86_CODEGEN |
| 44 | # undef PROVIDE_X64_CODEGEN |
| 45 | #elif defined(FORCE_X64_CODEGEN) |
| 46 | # undef DEFAULT_ARM_CODEGEN |
| 47 | # undef DEFAULT_X86_CODEGEN |
| 48 | # define DEFAULT_X64_CODEGEN |
| 49 | # undef PROVIDE_ARM_CODEGEN |
| 50 | # undef PROVIDE_X86_CODEGEN |
| 51 | # define PROVIDE_X64_CODEGEN |
| 52 | #endif |
| 53 | |
| 54 | #if defined(DEFAULT_ARM_CODEGEN) |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 55 | # define TARGET_TRIPLE_STRING "armv7-none-linux-gnueabi" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 56 | #elif defined(DEFAULT_X86_CODEGEN) |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 57 | # define TARGET_TRIPLE_STRING "i686-unknown-linux" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 58 | #elif defined(DEFAULT_X64_CODEGEN) |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 59 | # define TARGET_TRIPLE_STRING "x86_64-unknown-linux" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 60 | #endif |
| 61 | |
| 62 | #if (defined(__VFP_FP__) && !defined(__SOFTFP__)) |
| 63 | # define ARM_USE_VFP |
| 64 | #endif |
| 65 | |
Logan | c439523 | 2010-11-27 18:54:17 +0800 | [diff] [blame] | 66 | #include "Compiler.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 67 | |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 68 | #include "ContextManager.h" |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 69 | #include "ScriptCompiled.h" |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame^] | 70 | #include "Sha1Helper.h" |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 71 | |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 72 | #include "llvm/ADT/StringRef.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 73 | |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 74 | #include "llvm/Analysis/Passes.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 75 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 76 | #include "llvm/Bitcode/ReaderWriter.h" |
| 77 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 78 | #include "llvm/CodeGen/Passes.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 79 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 80 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 81 | |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 82 | #include "llvm/Transforms/IPO.h" |
| 83 | #include "llvm/Transforms/Scalar.h" |
| 84 | |
| 85 | #include "llvm/Target/SubtargetFeature.h" |
| 86 | #include "llvm/Target/TargetData.h" |
| 87 | #include "llvm/Target/TargetMachine.h" |
| 88 | #include "llvm/Target/TargetOptions.h" |
| 89 | #include "llvm/Target/TargetRegistry.h" |
| 90 | #include "llvm/Target/TargetSelect.h" |
| 91 | |
| 92 | #include "llvm/Support/ErrorHandling.h" |
| 93 | #include "llvm/Support/MemoryBuffer.h" |
| 94 | |
| 95 | #include "llvm/GlobalValue.h" |
| 96 | #include "llvm/Linker.h" |
| 97 | #include "llvm/LLVMContext.h" |
| 98 | #include "llvm/Metadata.h" |
| 99 | #include "llvm/Module.h" |
| 100 | #include "llvm/PassManager.h" |
| 101 | #include "llvm/Value.h" |
| 102 | |
| 103 | #include <errno.h> |
| 104 | #include <sys/file.h> |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 105 | #include <sys/stat.h> |
| 106 | #include <sys/types.h> |
| 107 | #include <unistd.h> |
| 108 | |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame^] | 109 | #include <string.h> |
Logan | 8b77a77 | 2010-12-21 09:11:01 +0800 | [diff] [blame] | 110 | |
Logan | df23afa | 2010-11-27 11:04:54 +0800 | [diff] [blame] | 111 | #include <string> |
| 112 | #include <vector> |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 113 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 114 | namespace bcc { |
| 115 | |
| 116 | ////////////////////////////////////////////////////////////////////////////// |
| 117 | // BCC Compiler Static Variables |
| 118 | ////////////////////////////////////////////////////////////////////////////// |
| 119 | |
| 120 | bool Compiler::GlobalInitialized = false; |
| 121 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 122 | // Code generation optimization level for the compiler |
| 123 | llvm::CodeGenOpt::Level Compiler::CodeGenOptLevel; |
| 124 | |
| 125 | std::string Compiler::Triple; |
| 126 | |
| 127 | std::string Compiler::CPU; |
| 128 | |
| 129 | std::vector<std::string> Compiler::Features; |
| 130 | |
| 131 | // The named of metadata node that pragma resides (should be synced with |
| 132 | // slang.cpp) |
| 133 | const llvm::StringRef Compiler::PragmaMetadataName = "#pragma"; |
| 134 | |
| 135 | // The named of metadata node that export variable name resides (should be |
| 136 | // synced with slang_rs_metadata.h) |
| 137 | const llvm::StringRef Compiler::ExportVarMetadataName = "#rs_export_var"; |
| 138 | |
| 139 | // The named of metadata node that export function name resides (should be |
| 140 | // synced with slang_rs_metadata.h) |
| 141 | const llvm::StringRef Compiler::ExportFuncMetadataName = "#rs_export_func"; |
| 142 | |
| 143 | |
| 144 | ////////////////////////////////////////////////////////////////////////////// |
| 145 | // Compiler |
| 146 | ////////////////////////////////////////////////////////////////////////////// |
| 147 | |
| 148 | void Compiler::GlobalInitialization() { |
| 149 | if (GlobalInitialized) |
| 150 | return; |
| 151 | |
Logan | 8706627 | 2010-12-29 00:34:32 +0800 | [diff] [blame] | 152 | LOGI("LIBBCC BUILD: %s %s\n", __DATE__, __TIME__); |
| 153 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 154 | // if (!llvm::llvm_is_multithreaded()) |
| 155 | // llvm::llvm_start_multithreaded(); |
| 156 | |
| 157 | // Set Triple, CPU and Features here |
| 158 | Triple = TARGET_TRIPLE_STRING; |
| 159 | |
| 160 | // TODO(sliao): NEON for JIT |
| 161 | // Features.push_back("+neon"); |
| 162 | // Features.push_back("+vmlx"); |
| 163 | // Features.push_back("+neonfp"); |
| 164 | Features.push_back("+vfp3"); |
| 165 | Features.push_back("+d16"); |
| 166 | |
| 167 | #if defined(DEFAULT_ARM_CODEGEN) || defined(PROVIDE_ARM_CODEGEN) |
| 168 | LLVMInitializeARMTargetInfo(); |
| 169 | LLVMInitializeARMTarget(); |
| 170 | #if defined(USE_DISASSEMBLER) |
| 171 | LLVMInitializeARMDisassembler(); |
| 172 | LLVMInitializeARMAsmPrinter(); |
| 173 | #endif |
| 174 | #endif |
| 175 | |
| 176 | #if defined(DEFAULT_X86_CODEGEN) || defined(PROVIDE_X86_CODEGEN) |
| 177 | LLVMInitializeX86TargetInfo(); |
| 178 | LLVMInitializeX86Target(); |
| 179 | #if defined(USE_DISASSEMBLER) |
| 180 | LLVMInitializeX86Disassembler(); |
| 181 | LLVMInitializeX86AsmPrinter(); |
| 182 | #endif |
| 183 | #endif |
| 184 | |
| 185 | #if defined(DEFAULT_X64_CODEGEN) || defined(PROVIDE_X64_CODEGEN) |
| 186 | LLVMInitializeX86TargetInfo(); |
| 187 | LLVMInitializeX86Target(); |
| 188 | #if defined(USE_DISASSEMBLER) |
| 189 | LLVMInitializeX86Disassembler(); |
| 190 | LLVMInitializeX86AsmPrinter(); |
| 191 | #endif |
| 192 | #endif |
| 193 | |
| 194 | // -O0: llvm::CodeGenOpt::None |
| 195 | // -O1: llvm::CodeGenOpt::Less |
| 196 | // -O2: llvm::CodeGenOpt::Default |
| 197 | // -O3: llvm::CodeGenOpt::Aggressive |
Shih-wei Liao | 72f67a6 | 2010-12-14 13:36:15 -0800 | [diff] [blame] | 198 | CodeGenOptLevel = llvm::CodeGenOpt::Aggressive; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 199 | |
| 200 | // Below are the global settings to LLVM |
| 201 | |
| 202 | // Disable frame pointer elimination optimization |
| 203 | llvm::NoFramePointerElim = false; |
| 204 | |
| 205 | // Use hardfloat ABI |
| 206 | // |
| 207 | // TODO(all): Need to detect the CPU capability and decide whether to use |
| 208 | // softfp. To use softfp, change following 2 lines to |
| 209 | // |
| 210 | // llvm::FloatABIType = llvm::FloatABI::Soft; |
| 211 | // llvm::UseSoftFloat = true; |
| 212 | // |
Shih-wei Liao | e728cb8 | 2010-12-15 15:20:47 -0800 | [diff] [blame] | 213 | llvm::FloatABIType = llvm::FloatABI::Soft; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 214 | llvm::UseSoftFloat = false; |
| 215 | |
| 216 | // BCC needs all unknown symbols resolved at JIT/compilation time. |
| 217 | // So we don't need any dynamic relocation model. |
| 218 | llvm::TargetMachine::setRelocationModel(llvm::Reloc::Static); |
| 219 | |
| 220 | #if defined(DEFAULT_X64_CODEGEN) |
| 221 | // Data address in X86_64 architecture may reside in a far-away place |
| 222 | llvm::TargetMachine::setCodeModel(llvm::CodeModel::Medium); |
| 223 | #else |
| 224 | // This is set for the linker (specify how large of the virtual addresses |
| 225 | // we can access for all unknown symbols.) |
| 226 | llvm::TargetMachine::setCodeModel(llvm::CodeModel::Small); |
| 227 | #endif |
| 228 | |
| 229 | // Register the scheduler |
| 230 | llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler); |
| 231 | |
| 232 | // Register allocation policy: |
| 233 | // createFastRegisterAllocator: fast but bad quality |
| 234 | // createLinearScanRegisterAllocator: not so fast but good quality |
| 235 | llvm::RegisterRegAlloc::setDefault |
| 236 | ((CodeGenOptLevel == llvm::CodeGenOpt::None) ? |
| 237 | llvm::createFastRegisterAllocator : |
| 238 | llvm::createLinearScanRegisterAllocator); |
| 239 | |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame^] | 240 | // Calculate the SHA1 checksum of libbcc and libRS. |
| 241 | calcFileSHA1(sha1LibBCC, pathLibBCC); |
| 242 | calcFileSHA1(sha1LibRS, pathLibRS); |
| 243 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 244 | GlobalInitialized = true; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | void Compiler::LLVMErrorHandler(void *UserData, const std::string &Message) { |
| 249 | std::string *Error = static_cast<std::string*>(UserData); |
| 250 | Error->assign(Message); |
| 251 | LOGE("%s", Message.c_str()); |
| 252 | exit(1); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | CodeMemoryManager *Compiler::createCodeMemoryManager() { |
| 257 | mCodeMemMgr.reset(new CodeMemoryManager()); |
| 258 | return mCodeMemMgr.get(); |
| 259 | } |
| 260 | |
| 261 | |
| 262 | CodeEmitter *Compiler::createCodeEmitter() { |
Logan | 7dcaac9 | 2011-01-06 04:26:23 +0800 | [diff] [blame] | 263 | mCodeEmitter.reset(new CodeEmitter(mpResult, mCodeMemMgr.get())); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 264 | return mCodeEmitter.get(); |
| 265 | } |
| 266 | |
| 267 | |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 268 | Compiler::Compiler(ScriptCompiled *result) |
| 269 | : mpResult(result), |
| 270 | mUseCache(false), |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 271 | mCacheNew(false), |
Logan | 8391349 | 2011-01-01 01:42:09 +0800 | [diff] [blame] | 272 | mCacheLoadFailed(false), |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 273 | mpSymbolLookupFn(NULL), |
| 274 | mpSymbolLookupContext(NULL), |
| 275 | mContext(NULL), |
| 276 | mModule(NULL), |
| 277 | mHasLinked(false) /* Turn off linker */ { |
| 278 | llvm::remove_fatal_error_handler(); |
| 279 | llvm::install_fatal_error_handler(LLVMErrorHandler, &mError); |
| 280 | mContext = new llvm::LLVMContext(); |
| 281 | return; |
| 282 | } |
| 283 | |
Shih-wei Liao | d161309 | 2010-12-23 22:59:15 +0800 | [diff] [blame] | 284 | // Compiler::readBC |
| 285 | // Parameters: |
| 286 | // resName: NULL means don't use cache. |
| 287 | // Note: If "cache-hit but bccLoadBinary fails for some reason", still |
| 288 | // pass in the resName. |
| 289 | // Rationale: So we may have future cache-hit. |
| 290 | // |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 291 | int Compiler::readBC(const char *bitcode, |
| 292 | size_t bitcodeSize, |
Shih-wei Liao | e6a1851 | 2010-12-09 12:38:10 -0800 | [diff] [blame] | 293 | const BCCchar *resName, |
| 294 | const BCCchar *cacheDir) { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 295 | llvm::OwningPtr<llvm::MemoryBuffer> MEM; |
| 296 | |
| 297 | if (bitcode == NULL || bitcodeSize <= 0) |
| 298 | return 0; |
| 299 | |
| 300 | // Package input to object MemoryBuffer |
| 301 | MEM.reset(llvm::MemoryBuffer::getMemBuffer( |
| 302 | llvm::StringRef(bitcode, bitcodeSize))); |
| 303 | |
| 304 | if (MEM.get() == NULL) { |
| 305 | setError("Error reading input program bitcode into memory"); |
| 306 | return hasError(); |
| 307 | } |
| 308 | |
| 309 | // Read the input Bitcode as a Module |
| 310 | mModule = llvm::ParseBitcodeFile(MEM.get(), *mContext, &mError); |
| 311 | MEM.reset(); |
| 312 | return hasError(); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | int Compiler::linkBC(const char *bitcode, size_t bitcodeSize) { |
| 317 | llvm::OwningPtr<llvm::MemoryBuffer> MEM; |
| 318 | |
| 319 | if (bitcode == NULL || bitcodeSize <= 0) |
| 320 | return 0; |
| 321 | |
| 322 | if (mModule == NULL) { |
| 323 | setError("No module presents for linking"); |
| 324 | return hasError(); |
| 325 | } |
| 326 | |
| 327 | MEM.reset(llvm::MemoryBuffer::getMemBuffer( |
| 328 | llvm::StringRef(bitcode, bitcodeSize))); |
| 329 | |
| 330 | if (MEM.get() == NULL) { |
| 331 | setError("Error reading input library bitcode into memory"); |
| 332 | return hasError(); |
| 333 | } |
| 334 | |
| 335 | llvm::OwningPtr<llvm::Module> Lib(llvm::ParseBitcodeFile(MEM.get(), |
| 336 | *mContext, |
| 337 | &mError)); |
| 338 | if (Lib.get() == NULL) |
| 339 | return hasError(); |
| 340 | |
| 341 | if (llvm::Linker::LinkModules(mModule, Lib.take(), &mError)) |
| 342 | return hasError(); |
| 343 | |
| 344 | // Everything for linking should be settled down here with no error occurs |
| 345 | mHasLinked = true; |
| 346 | return hasError(); |
| 347 | } |
| 348 | |
| 349 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 350 | // interace for bccCompileBC() |
| 351 | int Compiler::compile() { |
| 352 | llvm::TargetData *TD = NULL; |
| 353 | |
| 354 | llvm::TargetMachine *TM = NULL; |
| 355 | const llvm::Target *Target; |
| 356 | std::string FeaturesStr; |
| 357 | |
| 358 | llvm::FunctionPassManager *CodeGenPasses = NULL; |
| 359 | |
| 360 | const llvm::NamedMDNode *PragmaMetadata; |
| 361 | const llvm::NamedMDNode *ExportVarMetadata; |
| 362 | const llvm::NamedMDNode *ExportFuncMetadata; |
| 363 | |
| 364 | if (mModule == NULL) // No module was loaded |
| 365 | return 0; |
| 366 | |
| 367 | // Create TargetMachine |
| 368 | Target = llvm::TargetRegistry::lookupTarget(Triple, mError); |
| 369 | if (hasError()) |
| 370 | goto on_bcc_compile_error; |
| 371 | |
| 372 | if (!CPU.empty() || !Features.empty()) { |
| 373 | llvm::SubtargetFeatures F; |
| 374 | F.setCPU(CPU); |
Logan | a4994f5 | 2010-11-27 14:06:02 +0800 | [diff] [blame] | 375 | |
| 376 | for (std::vector<std::string>::const_iterator |
| 377 | I = Features.begin(), E = Features.end(); I != E; I++) { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 378 | F.AddFeature(*I); |
Logan | a4994f5 | 2010-11-27 14:06:02 +0800 | [diff] [blame] | 379 | } |
| 380 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 381 | FeaturesStr = F.getString(); |
| 382 | } |
| 383 | |
| 384 | TM = Target->createTargetMachine(Triple, FeaturesStr); |
| 385 | if (TM == NULL) { |
| 386 | setError("Failed to create target machine implementation for the" |
| 387 | " specified triple '" + Triple + "'"); |
| 388 | goto on_bcc_compile_error; |
| 389 | } |
| 390 | |
| 391 | // Create memory manager for creation of code emitter later. |
| 392 | if (!mCodeMemMgr.get() && !createCodeMemoryManager()) { |
| 393 | setError("Failed to startup memory management for further compilation"); |
| 394 | goto on_bcc_compile_error; |
| 395 | } |
Logan | 02286cb | 2011-01-07 00:30:47 +0800 | [diff] [blame] | 396 | |
| 397 | mpResult->mContext = (char *) (mCodeMemMgr.get()->getCodeMemBase()); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 398 | |
| 399 | // Create code emitter |
| 400 | if (!mCodeEmitter.get()) { |
| 401 | if (!createCodeEmitter()) { |
| 402 | setError("Failed to create machine code emitter to complete" |
| 403 | " the compilation"); |
| 404 | goto on_bcc_compile_error; |
| 405 | } |
| 406 | } else { |
| 407 | // Reuse the code emitter |
| 408 | mCodeEmitter->reset(); |
| 409 | } |
| 410 | |
| 411 | mCodeEmitter->setTargetMachine(*TM); |
| 412 | mCodeEmitter->registerSymbolCallback(mpSymbolLookupFn, |
| 413 | mpSymbolLookupContext); |
| 414 | |
| 415 | // Get target data from Module |
| 416 | TD = new llvm::TargetData(mModule); |
| 417 | |
| 418 | // Load named metadata |
| 419 | ExportVarMetadata = mModule->getNamedMetadata(ExportVarMetadataName); |
| 420 | ExportFuncMetadata = mModule->getNamedMetadata(ExportFuncMetadataName); |
| 421 | PragmaMetadata = mModule->getNamedMetadata(PragmaMetadataName); |
| 422 | |
| 423 | // Create LTO passes and run them on the mModule |
| 424 | if (mHasLinked) { |
| 425 | llvm::TimePassesIsEnabled = true; // TODO(all) |
| 426 | llvm::PassManager LTOPasses; |
| 427 | LTOPasses.add(new llvm::TargetData(*TD)); |
| 428 | |
| 429 | std::vector<const char*> ExportSymbols; |
| 430 | |
| 431 | // A workaround for getting export variable and function name. Will refine |
| 432 | // it soon. |
| 433 | if (ExportVarMetadata) { |
| 434 | for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) { |
| 435 | llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i); |
| 436 | if (ExportVar != NULL && ExportVar->getNumOperands() > 1) { |
| 437 | llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0); |
| 438 | if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) { |
| 439 | llvm::StringRef ExportVarName = |
| 440 | static_cast<llvm::MDString*>(ExportVarNameMDS)->getString(); |
| 441 | ExportSymbols.push_back(ExportVarName.data()); |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | if (ExportFuncMetadata) { |
| 448 | for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) { |
| 449 | llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i); |
| 450 | if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) { |
| 451 | llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0); |
| 452 | if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) { |
| 453 | llvm::StringRef ExportFuncName = |
| 454 | static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString(); |
| 455 | ExportSymbols.push_back(ExportFuncName.data()); |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | // root() and init() are born to be exported |
| 461 | ExportSymbols.push_back("root"); |
| 462 | ExportSymbols.push_back("init"); |
| 463 | |
| 464 | // We now create passes list performing LTO. These are copied from |
| 465 | // (including comments) llvm::createStandardLTOPasses(). |
| 466 | |
| 467 | // Internalize all other symbols not listed in ExportSymbols |
| 468 | LTOPasses.add(llvm::createInternalizePass(ExportSymbols)); |
| 469 | |
| 470 | // Propagate constants at call sites into the functions they call. This |
| 471 | // opens opportunities for globalopt (and inlining) by substituting |
| 472 | // function pointers passed as arguments to direct uses of functions. |
| 473 | LTOPasses.add(llvm::createIPSCCPPass()); |
| 474 | |
| 475 | // Now that we internalized some globals, see if we can hack on them! |
| 476 | LTOPasses.add(llvm::createGlobalOptimizerPass()); |
| 477 | |
| 478 | // Linking modules together can lead to duplicated global constants, only |
| 479 | // keep one copy of each constant... |
| 480 | LTOPasses.add(llvm::createConstantMergePass()); |
| 481 | |
| 482 | // Remove unused arguments from functions... |
| 483 | LTOPasses.add(llvm::createDeadArgEliminationPass()); |
| 484 | |
| 485 | // Reduce the code after globalopt and ipsccp. Both can open up |
| 486 | // significant simplification opportunities, and both can propagate |
| 487 | // functions through function pointers. When this happens, we often have |
| 488 | // to resolve varargs calls, etc, so let instcombine do this. |
| 489 | LTOPasses.add(llvm::createInstructionCombiningPass()); |
| 490 | |
| 491 | // Inline small functions |
| 492 | LTOPasses.add(llvm::createFunctionInliningPass()); |
| 493 | |
| 494 | // Remove dead EH info. |
| 495 | LTOPasses.add(llvm::createPruneEHPass()); |
| 496 | |
| 497 | // Internalize the globals again after inlining |
| 498 | LTOPasses.add(llvm::createGlobalOptimizerPass()); |
| 499 | |
| 500 | // Remove dead functions. |
| 501 | LTOPasses.add(llvm::createGlobalDCEPass()); |
| 502 | |
| 503 | // If we didn't decide to inline a function, check to see if we can |
| 504 | // transform it to pass arguments by value instead of by reference. |
| 505 | LTOPasses.add(llvm::createArgumentPromotionPass()); |
| 506 | |
| 507 | // The IPO passes may leave cruft around. Clean up after them. |
| 508 | LTOPasses.add(llvm::createInstructionCombiningPass()); |
| 509 | LTOPasses.add(llvm::createJumpThreadingPass()); |
| 510 | |
| 511 | // Break up allocas |
| 512 | LTOPasses.add(llvm::createScalarReplAggregatesPass()); |
| 513 | |
| 514 | // Run a few AA driven optimizations here and now, to cleanup the code. |
| 515 | LTOPasses.add(llvm::createFunctionAttrsPass()); // Add nocapture. |
| 516 | LTOPasses.add(llvm::createGlobalsModRefPass()); // IP alias analysis. |
| 517 | |
| 518 | // Hoist loop invariants. |
| 519 | LTOPasses.add(llvm::createLICMPass()); |
| 520 | |
| 521 | // Remove redundancies. |
| 522 | LTOPasses.add(llvm::createGVNPass()); |
| 523 | |
| 524 | // Remove dead memcpys. |
| 525 | LTOPasses.add(llvm::createMemCpyOptPass()); |
| 526 | |
| 527 | // Nuke dead stores. |
| 528 | LTOPasses.add(llvm::createDeadStoreEliminationPass()); |
| 529 | |
| 530 | // Cleanup and simplify the code after the scalar optimizations. |
| 531 | LTOPasses.add(llvm::createInstructionCombiningPass()); |
| 532 | |
| 533 | LTOPasses.add(llvm::createJumpThreadingPass()); |
| 534 | |
| 535 | // Delete basic blocks, which optimization passes may have killed. |
| 536 | LTOPasses.add(llvm::createCFGSimplificationPass()); |
| 537 | |
| 538 | // Now that we have optimized the program, discard unreachable functions. |
| 539 | LTOPasses.add(llvm::createGlobalDCEPass()); |
| 540 | |
| 541 | LTOPasses.run(*mModule); |
| 542 | } |
| 543 | |
| 544 | // Create code-gen pass to run the code emitter |
| 545 | CodeGenPasses = new llvm::FunctionPassManager(mModule); |
| 546 | CodeGenPasses->add(TD); // Will take the ownership of TD |
| 547 | |
| 548 | if (TM->addPassesToEmitMachineCode(*CodeGenPasses, |
| 549 | *mCodeEmitter, |
| 550 | CodeGenOptLevel)) { |
| 551 | setError("The machine code emission is not supported by BCC on target '" |
| 552 | + Triple + "'"); |
| 553 | goto on_bcc_compile_error; |
| 554 | } |
| 555 | |
| 556 | // Run the pass (the code emitter) on every non-declaration function in the |
| 557 | // module |
| 558 | CodeGenPasses->doInitialization(); |
| 559 | for (llvm::Module::iterator I = mModule->begin(), E = mModule->end(); |
| 560 | I != E; I++) { |
| 561 | if (!I->isDeclaration()) { |
| 562 | CodeGenPasses->run(*I); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | CodeGenPasses->doFinalization(); |
| 567 | |
| 568 | // Copy the global address mapping from code emitter and remapping |
| 569 | if (ExportVarMetadata) { |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 570 | ScriptCompiled::ExportVarList &varList = mpResult->mExportVars; |
| 571 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 572 | for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) { |
| 573 | llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i); |
| 574 | if (ExportVar != NULL && ExportVar->getNumOperands() > 1) { |
| 575 | llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0); |
| 576 | if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) { |
| 577 | llvm::StringRef ExportVarName = |
| 578 | static_cast<llvm::MDString*>(ExportVarNameMDS)->getString(); |
| 579 | |
| 580 | CodeEmitter::global_addresses_const_iterator I, E; |
| 581 | for (I = mCodeEmitter->global_address_begin(), |
| 582 | E = mCodeEmitter->global_address_end(); |
| 583 | I != E; I++) { |
| 584 | if (I->first->getValueID() != llvm::Value::GlobalVariableVal) |
| 585 | continue; |
| 586 | if (ExportVarName == I->first->getName()) { |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 587 | varList.push_back(I->second); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 588 | break; |
| 589 | } |
| 590 | } |
| 591 | if (I != mCodeEmitter->global_address_end()) |
| 592 | continue; // found |
| 593 | } |
| 594 | } |
| 595 | // if reaching here, we know the global variable record in metadata is |
| 596 | // not found. So we make an empty slot |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 597 | varList.push_back(NULL); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 598 | } |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 599 | |
| 600 | assert((varList.size() == ExportVarMetadata->getNumOperands()) && |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 601 | "Number of slots doesn't match the number of export variables!"); |
| 602 | } |
| 603 | |
| 604 | if (ExportFuncMetadata) { |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 605 | ScriptCompiled::ExportFuncList &funcList = mpResult->mExportFuncs; |
| 606 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 607 | for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) { |
| 608 | llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i); |
| 609 | if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) { |
| 610 | llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0); |
| 611 | if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) { |
| 612 | llvm::StringRef ExportFuncName = |
| 613 | static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString(); |
Logan | 7dcaac9 | 2011-01-06 04:26:23 +0800 | [diff] [blame] | 614 | funcList.push_back(mpResult->lookup(ExportFuncName.str().c_str())); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | // Tell code emitter now can release the memory using during the JIT since |
| 621 | // we have done the code emission |
| 622 | mCodeEmitter->releaseUnnecessary(); |
| 623 | |
| 624 | // Finally, read pragma information from the metadata node of the @Module if |
| 625 | // any. |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 626 | if (PragmaMetadata) { |
| 627 | ScriptCompiled::PragmaList &pragmaList = mpResult->mPragmas; |
| 628 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 629 | for (int i = 0, e = PragmaMetadata->getNumOperands(); i != e; i++) { |
| 630 | llvm::MDNode *Pragma = PragmaMetadata->getOperand(i); |
| 631 | if (Pragma != NULL && |
| 632 | Pragma->getNumOperands() == 2 /* should have exactly 2 operands */) { |
| 633 | llvm::Value *PragmaNameMDS = Pragma->getOperand(0); |
| 634 | llvm::Value *PragmaValueMDS = Pragma->getOperand(1); |
| 635 | |
| 636 | if ((PragmaNameMDS->getValueID() == llvm::Value::MDStringVal) && |
| 637 | (PragmaValueMDS->getValueID() == llvm::Value::MDStringVal)) { |
| 638 | llvm::StringRef PragmaName = |
| 639 | static_cast<llvm::MDString*>(PragmaNameMDS)->getString(); |
| 640 | llvm::StringRef PragmaValue = |
| 641 | static_cast<llvm::MDString*>(PragmaValueMDS)->getString(); |
| 642 | |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 643 | pragmaList.push_back( |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 644 | std::make_pair(std::string(PragmaName.data(), |
| 645 | PragmaName.size()), |
| 646 | std::string(PragmaValue.data(), |
| 647 | PragmaValue.size()))); |
| 648 | } |
| 649 | } |
| 650 | } |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 651 | } |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 652 | |
| 653 | on_bcc_compile_error: |
| 654 | // LOGE("on_bcc_compiler_error"); |
| 655 | if (CodeGenPasses) { |
| 656 | delete CodeGenPasses; |
| 657 | } else if (TD) { |
| 658 | delete TD; |
| 659 | } |
| 660 | if (TM) |
| 661 | delete TM; |
| 662 | |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 663 | #if 0 |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 664 | if (mError.empty()) { |
| 665 | if (mUseCache && mCacheFd >= 0 && mCacheNew) { |
| 666 | genCacheFile(); |
Shih-wei Liao | 8f77997 | 2010-12-23 12:07:59 +0800 | [diff] [blame] | 667 | //LOGI("DONE generating cache file"); //sliao |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 668 | flock(mCacheFd, LOCK_UN); |
| 669 | } |
| 670 | |
| 671 | return false; |
| 672 | } |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 673 | #endif |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 674 | |
| 675 | // LOGE(getErrorMessage()); |
| 676 | return true; |
| 677 | } |
| 678 | |
| 679 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 680 | Compiler::~Compiler() { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 681 | delete mModule; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 682 | delete mContext; |
Logan | a4994f5 | 2010-11-27 14:06:02 +0800 | [diff] [blame] | 683 | |
| 684 | // llvm::llvm_shutdown(); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 685 | } |
| 686 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 687 | } // namespace bcc |