Compiler.cpp revision 587e1e8bc6521f6453ef46c5b5c0129f00db9ff5
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#include "Compiler.h" 18 19#include "Config.h" 20 21#include "ContextManager.h" 22#include "DebugHelper.h" 23#include "FileHandle.h" 24#include "Runtime.h" 25#include "ScriptCompiled.h" 26#include "Sha1Helper.h" 27 28#if USE_MCJIT 29#include "librsloader.h" 30#endif 31 32#include "llvm/ADT/StringRef.h" 33 34#include "llvm/Analysis/Passes.h" 35 36#include "llvm/Bitcode/ReaderWriter.h" 37 38#include "llvm/CodeGen/Passes.h" 39#include "llvm/CodeGen/RegAllocRegistry.h" 40#include "llvm/CodeGen/SchedulerRegistry.h" 41 42#include "llvm/Transforms/IPO.h" 43#include "llvm/Transforms/Scalar.h" 44 45#include "llvm/Target/SubtargetFeature.h" 46#include "llvm/Target/TargetData.h" 47#include "llvm/Target/TargetMachine.h" 48#include "llvm/Target/TargetOptions.h" 49#include "llvm/Target/TargetRegistry.h" 50#include "llvm/Target/TargetSelect.h" 51 52#if USE_DISASSEMBLER 53#include "llvm/MC/MCAsmInfo.h" 54#include "llvm/MC/MCDisassembler.h" 55#include "llvm/MC/MCInst.h" 56#include "llvm/MC/MCInstPrinter.h" 57#include "llvm/Support/MemoryObject.h" 58#include "llvm/LLVMContext.h" 59#endif 60 61#include "llvm/Support/ErrorHandling.h" 62#include "llvm/Support/FormattedStream.h" 63#include "llvm/Support/MemoryBuffer.h" 64 65#include "llvm/Type.h" 66#include "llvm/GlobalValue.h" 67#include "llvm/Linker.h" 68#include "llvm/LLVMContext.h" 69#include "llvm/Metadata.h" 70#include "llvm/Module.h" 71#include "llvm/PassManager.h" 72#include "llvm/Value.h" 73 74#include <errno.h> 75#include <sys/file.h> 76#include <sys/stat.h> 77#include <sys/types.h> 78#include <unistd.h> 79 80#include <string.h> 81 82#include <string> 83#include <vector> 84 85namespace { 86 87#if USE_DISASSEMBLER 88class BufferMemoryObject : public llvm::MemoryObject { 89private: 90 const uint8_t *mBytes; 91 uint64_t mLength; 92 93public: 94 BufferMemoryObject(const uint8_t *Bytes, uint64_t Length) 95 : mBytes(Bytes), mLength(Length) { 96 } 97 98 virtual uint64_t getBase() const { return 0; } 99 virtual uint64_t getExtent() const { return mLength; } 100 101 virtual int readByte(uint64_t Addr, uint8_t *Byte) const { 102 if (Addr > getExtent()) 103 return -1; 104 *Byte = mBytes[Addr]; 105 return 0; 106 } 107}; 108#endif 109 110}; // namespace anonymous 111 112#define DEBUG_MCJIT REFLECT 0 113#define DEBUG_MCJIT_DISASSEMBLE 0 114 115namespace bcc { 116 117////////////////////////////////////////////////////////////////////////////// 118// BCC Compiler Static Variables 119////////////////////////////////////////////////////////////////////////////// 120 121bool Compiler::GlobalInitialized = false; 122 123// Code generation optimization level for the compiler 124llvm::CodeGenOpt::Level Compiler::CodeGenOptLevel; 125 126std::string Compiler::Triple; 127 128std::string Compiler::CPU; 129 130std::vector<std::string> Compiler::Features; 131 132// Name of metadata node where pragma info resides (should be synced with 133// slang.cpp) 134const llvm::StringRef Compiler::PragmaMetadataName = "#pragma"; 135 136// Name of metadata node where exported variable names reside (should be 137// synced with slang_rs_metadata.h) 138const llvm::StringRef Compiler::ExportVarMetadataName = "#rs_export_var"; 139 140// Name of metadata node where exported function names reside (should be 141// synced with slang_rs_metadata.h) 142const llvm::StringRef Compiler::ExportFuncMetadataName = "#rs_export_func"; 143 144// Name of metadata node where RS object slot info resides (should be 145// synced with slang_rs_metadata.h) 146const llvm::StringRef Compiler::ObjectSlotMetadataName = "#rs_object_slots"; 147 148////////////////////////////////////////////////////////////////////////////// 149// Compiler 150////////////////////////////////////////////////////////////////////////////// 151 152void Compiler::GlobalInitialization() { 153 if (GlobalInitialized) 154 return; 155 // if (!llvm::llvm_is_multithreaded()) 156 // llvm::llvm_start_multithreaded(); 157 158 // Set Triple, CPU and Features here 159 Triple = TARGET_TRIPLE_STRING; 160 161 Features.push_back("+vfp3"); 162 163 // NOTE: Currently, we have to turn off the support for NEON explicitly. 164 // Since the ARMCodeEmitter.cpp is not ready for JITing NEON 165 // instructions. 166#if ARCH_ARM_HAVE_NEON 167 Features.push_back("+d32"); 168 Features.push_back("+neon"); 169 Features.push_back("+neonfp"); 170#else 171 Features.push_back("+d16"); 172 Features.push_back("-neon"); 173 Features.push_back("-neonfp"); 174#endif 175 176 Features.push_back("-vmlx"); 177 178#if defined(DEFAULT_ARM_CODEGEN) || defined(PROVIDE_ARM_CODEGEN) 179 LLVMInitializeARMTargetInfo(); 180 LLVMInitializeARMTarget(); 181#if USE_DISASSEMBLER 182 LLVMInitializeARMDisassembler(); 183 LLVMInitializeARMAsmPrinter(); 184#endif 185#endif 186 187#if defined(DEFAULT_X86_CODEGEN) || defined(PROVIDE_X86_CODEGEN) 188 LLVMInitializeX86TargetInfo(); 189 LLVMInitializeX86Target(); 190#if USE_DISASSEMBLER 191 LLVMInitializeX86Disassembler(); 192 LLVMInitializeX86AsmPrinter(); 193#endif 194#endif 195 196#if defined(DEFAULT_X64_CODEGEN) || defined(PROVIDE_X64_CODEGEN) 197 LLVMInitializeX86TargetInfo(); 198 LLVMInitializeX86Target(); 199#if USE_DISASSEMBLER 200 LLVMInitializeX86Disassembler(); 201 LLVMInitializeX86AsmPrinter(); 202#endif 203#endif 204 205 // -O0: llvm::CodeGenOpt::None 206 // -O1: llvm::CodeGenOpt::Less 207 // -O2: llvm::CodeGenOpt::Default 208 // -O3: llvm::CodeGenOpt::Aggressive 209 CodeGenOptLevel = llvm::CodeGenOpt::Aggressive; 210 211 // Below are the global settings to LLVM 212 213 // Disable frame pointer elimination optimization 214 llvm::NoFramePointerElim = false; 215 216 // Use hardfloat ABI 217 // 218 // TODO(all): Need to detect the CPU capability and decide whether to use 219 // softfp. To use softfp, change following 2 lines to 220 // 221 // llvm::FloatABIType = llvm::FloatABI::Soft; 222 // llvm::UseSoftFloat = true; 223 // 224 llvm::FloatABIType = llvm::FloatABI::Soft; 225 llvm::UseSoftFloat = false; 226 227 // BCC needs all unknown symbols resolved at JIT/compilation time. 228 // So we don't need any dynamic relocation model. 229 llvm::TargetMachine::setRelocationModel(llvm::Reloc::Static); 230 231#if defined(DEFAULT_X64_CODEGEN) 232 // Data address in X86_64 architecture may reside in a far-away place 233 llvm::TargetMachine::setCodeModel(llvm::CodeModel::Medium); 234#else 235 // This is set for the linker (specify how large of the virtual addresses 236 // we can access for all unknown symbols.) 237 llvm::TargetMachine::setCodeModel(llvm::CodeModel::Small); 238#endif 239 240 // Register the scheduler 241 llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler); 242 243 // Register allocation policy: 244 // createFastRegisterAllocator: fast but bad quality 245 // createLinearScanRegisterAllocator: not so fast but good quality 246 llvm::RegisterRegAlloc::setDefault 247 ((CodeGenOptLevel == llvm::CodeGenOpt::None) ? 248 llvm::createFastRegisterAllocator : 249 llvm::createLinearScanRegisterAllocator); 250 251#if USE_CACHE 252 // Calculate the SHA1 checksum of libbcc and libRS. 253 calcFileSHA1(sha1LibBCC_SHA1, pathLibBCC_SHA1); 254#endif 255 256 GlobalInitialized = true; 257} 258 259 260void Compiler::LLVMErrorHandler(void *UserData, const std::string &Message) { 261 std::string *Error = static_cast<std::string*>(UserData); 262 Error->assign(Message); 263 LOGE("%s", Message.c_str()); 264 exit(1); 265} 266 267 268#if USE_OLD_JIT 269CodeMemoryManager *Compiler::createCodeMemoryManager() { 270 mCodeMemMgr.reset(new CodeMemoryManager()); 271 return mCodeMemMgr.get(); 272} 273#endif 274 275 276#if USE_OLD_JIT 277CodeEmitter *Compiler::createCodeEmitter() { 278 mCodeEmitter.reset(new CodeEmitter(mpResult, mCodeMemMgr.get())); 279 return mCodeEmitter.get(); 280} 281#endif 282 283 284Compiler::Compiler(ScriptCompiled *result) 285 : mpResult(result), 286#if USE_MCJIT 287 mRSExecutable(NULL), 288#endif 289 mpSymbolLookupFn(NULL), 290 mpSymbolLookupContext(NULL), 291 mContext(NULL), 292 mModule(NULL), 293 mHasLinked(false) /* Turn off linker */ { 294 llvm::remove_fatal_error_handler(); 295 llvm::install_fatal_error_handler(LLVMErrorHandler, &mError); 296 mContext = new llvm::LLVMContext(); 297 return; 298} 299 300 301#if USE_MCJIT 302// input objPath: For example, 303// /data/user/0/com.example.android.rs.fountain/cache/ 304// @com.example.android.rs.fountain:raw@fountain.oBCC 305// output objPath: /data/user/0/com.example.android.rs.fountain/cache/ 306// fountain.o 307// 308bool Compiler::getObjPath(std::string &objPath) { 309 size_t found0 = objPath.find("@"); 310 size_t found1 = objPath.rfind("@"); 311 312 if (found0 == found1 || 313 found0 == std::string::npos || 314 found1 == std::string::npos) { 315 LOGE("Ill formatted resource name '%s'. The name should contain 2 @s", 316 objPath.c_str()); 317 return false; 318 } 319 320 objPath.replace(found0, found1 - found0 + 1, "", 0); 321 objPath.resize(objPath.length() - 3); 322 323 LOGV("objPath = %s", objPath.c_str()); 324 return true; 325} 326#endif 327 328 329llvm::Module *Compiler::parseBitcodeFile(llvm::MemoryBuffer *MEM) { 330 llvm::Module *result = llvm::ParseBitcodeFile(MEM, *mContext, &mError); 331 332 if (!result) { 333 LOGE("Unable to ParseBitcodeFile: %s\n", mError.c_str()); 334 return NULL; 335 } 336 337 return result; 338} 339 340 341int Compiler::linkModule(llvm::Module *moduleWith) { 342 if (llvm::Linker::LinkModules(mModule, moduleWith, &mError) != 0) { 343 return hasError(); 344 } 345 346 // Everything for linking should be settled down here with no error occurs 347 mHasLinked = true; 348 return hasError(); 349} 350 351 352int Compiler::compile() { 353 llvm::Target const *Target = NULL; 354 llvm::TargetData *TD = NULL; 355 llvm::TargetMachine *TM = NULL; 356 357 std::string FeaturesStr; 358 359 llvm::NamedMDNode const *PragmaMetadata; 360 llvm::NamedMDNode const *ExportVarMetadata; 361 llvm::NamedMDNode const *ExportFuncMetadata; 362 llvm::NamedMDNode const *ObjectSlotMetadata; 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); 375 376 for (std::vector<std::string>::const_iterator 377 I = Features.begin(), E = Features.end(); I != E; I++) { 378 F.AddFeature(*I); 379 } 380 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 // Get target data from Module 392 TD = new llvm::TargetData(mModule); 393 394 // Load named metadata 395 ExportVarMetadata = mModule->getNamedMetadata(ExportVarMetadataName); 396 ExportFuncMetadata = mModule->getNamedMetadata(ExportFuncMetadataName); 397 PragmaMetadata = mModule->getNamedMetadata(PragmaMetadataName); 398 ObjectSlotMetadata = mModule->getNamedMetadata(ObjectSlotMetadataName); 399 400 // Perform link-time optimization if we have multiple modules 401 if (mHasLinked) { 402 runLTO(new llvm::TargetData(*TD), ExportVarMetadata, ExportFuncMetadata); 403 } 404 405 // Perform code generation 406#if USE_OLD_JIT 407 if (runCodeGen(new llvm::TargetData(*TD), TM, 408 ExportVarMetadata, ExportFuncMetadata) != 0) { 409 goto on_bcc_compile_error; 410 } 411#endif 412 413#if USE_MCJIT 414 if (runMCCodeGen(new llvm::TargetData(*TD), TM, 415 ExportVarMetadata, ExportFuncMetadata) != 0) { 416 goto on_bcc_compile_error; 417 } 418#if USE_DISASSEMBLER && DEBUG_MCJIT_DISASSEMBLE 419 { 420 // Get MC codegen emitted function name list 421 size_t func_list_size = rsloaderGetFuncCount(mRSExecutable); 422 std::vector<char const *> func_list(func_list_size, NULL); 423 rsloaderGetFuncNameList(mRSExecutable, func_list_size, &*func_list.begin()); 424 425 // Disassemble each function 426 for (size_t i = 0; i < func_list_size; ++i) { 427 void *func = rsloaderGetSymbolAddress(mRSExecutable, func_list[i]); 428 if (func) { 429 size_t size = rsloaderGetSymbolSize(mRSExecutable, func_list[i]); 430 Disassemble(Target, TM, func_list[i], (unsigned char const *)func, size); 431 } 432 } 433 } 434#endif 435#endif 436 437 // Read pragma information from the metadata node of the module. 438 if (PragmaMetadata) { 439 ScriptCompiled::PragmaList &pragmaList = mpResult->mPragmas; 440 441 for (int i = 0, e = PragmaMetadata->getNumOperands(); i != e; i++) { 442 llvm::MDNode *Pragma = PragmaMetadata->getOperand(i); 443 if (Pragma != NULL && 444 Pragma->getNumOperands() == 2 /* should have exactly 2 operands */) { 445 llvm::Value *PragmaNameMDS = Pragma->getOperand(0); 446 llvm::Value *PragmaValueMDS = Pragma->getOperand(1); 447 448 if ((PragmaNameMDS->getValueID() == llvm::Value::MDStringVal) && 449 (PragmaValueMDS->getValueID() == llvm::Value::MDStringVal)) { 450 llvm::StringRef PragmaName = 451 static_cast<llvm::MDString*>(PragmaNameMDS)->getString(); 452 llvm::StringRef PragmaValue = 453 static_cast<llvm::MDString*>(PragmaValueMDS)->getString(); 454 455 pragmaList.push_back( 456 std::make_pair(std::string(PragmaName.data(), 457 PragmaName.size()), 458 std::string(PragmaValue.data(), 459 PragmaValue.size()))); 460#if DEBUG_MCJIT_REFLECT 461 LOGD("compile(): Pragma: %s -> %s\n", pragmaList.back().first.c_str(), 462 pragmaList.back().second.c_str()); 463#endif 464 } 465 } 466 } 467 } 468 469 if (ObjectSlotMetadata) { 470 ScriptCompiled::ObjectSlotList &objectSlotList = mpResult->mObjectSlots; 471 472 for (int i = 0, e = ObjectSlotMetadata->getNumOperands(); i != e; i++) { 473 llvm::MDNode *ObjectSlot = ObjectSlotMetadata->getOperand(i); 474 if (ObjectSlot != NULL && 475 ObjectSlot->getNumOperands() == 1) { 476 llvm::Value *SlotMDS = ObjectSlot->getOperand(0); 477 if (SlotMDS->getValueID() == llvm::Value::MDStringVal) { 478 llvm::StringRef Slot = 479 static_cast<llvm::MDString*>(SlotMDS)->getString(); 480 uint32_t USlot = 0; 481 if (Slot.getAsInteger(10, USlot)) { 482 setError("Non-integer object slot value '" + Slot.str() + "'"); 483 goto on_bcc_compile_error; 484 } 485 objectSlotList.push_back(USlot); 486#if DEBUG_MCJIT_REFLECT 487 LOGD("compile(): RefCount Slot: %s @ %u\n", Slot.str().c_str(), USlot); 488#endif 489 } 490 } 491 } 492 } 493 494on_bcc_compile_error: 495 // LOGE("on_bcc_compiler_error"); 496 if (TD) { 497 delete TD; 498 } 499 500 if (TM) { 501 delete TM; 502 } 503 504 if (mError.empty()) { 505 return 0; 506 } 507 508 // LOGE(getErrorMessage()); 509 return 1; 510} 511 512 513#if USE_OLD_JIT 514int Compiler::runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM, 515 llvm::NamedMDNode const *ExportVarMetadata, 516 llvm::NamedMDNode const *ExportFuncMetadata) { 517 // Create memory manager for creation of code emitter later. 518 if (!mCodeMemMgr.get() && !createCodeMemoryManager()) { 519 setError("Failed to startup memory management for further compilation"); 520 return 1; 521 } 522 523 mpResult->mContext = (char *) (mCodeMemMgr.get()->getCodeMemBase()); 524 525 // Create code emitter 526 if (!mCodeEmitter.get()) { 527 if (!createCodeEmitter()) { 528 setError("Failed to create machine code emitter for compilation"); 529 return 1; 530 } 531 } else { 532 // Reuse the code emitter 533 mCodeEmitter->reset(); 534 } 535 536 mCodeEmitter->setTargetMachine(*TM); 537 mCodeEmitter->registerSymbolCallback(mpSymbolLookupFn, 538 mpSymbolLookupContext); 539 540 // Create code-gen pass to run the code emitter 541 llvm::OwningPtr<llvm::FunctionPassManager> CodeGenPasses( 542 new llvm::FunctionPassManager(mModule)); 543 544 // Add TargetData to code generation pass manager 545 CodeGenPasses->add(TD); 546 547 // Add code emit passes 548 if (TM->addPassesToEmitMachineCode(*CodeGenPasses, 549 *mCodeEmitter, 550 CodeGenOptLevel)) { 551 setError("The machine code emission is not supported on '" + Triple + "'"); 552 return 1; 553 } 554 555 // Run the code emitter on every non-declaration function in the module 556 CodeGenPasses->doInitialization(); 557 for (llvm::Module::iterator 558 I = mModule->begin(), E = mModule->end(); I != E; I++) { 559 if (!I->isDeclaration()) { 560 CodeGenPasses->run(*I); 561 } 562 } 563 564 CodeGenPasses->doFinalization(); 565 566 // Copy the global address mapping from code emitter and remapping 567 if (ExportVarMetadata) { 568 ScriptCompiled::ExportVarList &varList = mpResult->mExportVars; 569 570 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) { 571 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i); 572 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) { 573 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0); 574 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) { 575 llvm::StringRef ExportVarName = 576 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString(); 577 578 CodeEmitter::global_addresses_const_iterator I, E; 579 for (I = mCodeEmitter->global_address_begin(), 580 E = mCodeEmitter->global_address_end(); 581 I != E; I++) { 582 if (I->first->getValueID() != llvm::Value::GlobalVariableVal) 583 continue; 584 if (ExportVarName == I->first->getName()) { 585 varList.push_back(I->second); 586#if DEBUG_MCJIT_REFLECT 587 LOGD("runCodeGen(): Exported VAR: %s @ %p\n", ExportVarName.str().c_str(), I->second); 588#endif 589 break; 590 } 591 } 592 if (I != mCodeEmitter->global_address_end()) 593 continue; // found 594 595#if DEBUG_MCJIT_REFLECT 596 LOGD("runCodeGen(): Exported VAR: %s @ %p\n", 597 ExportVarName.str().c_str(), (void *)0); 598#endif 599 } 600 } 601 // if reaching here, we know the global variable record in metadata is 602 // not found. So we make an empty slot 603 varList.push_back(NULL); 604 } 605 606 bccAssert((varList.size() == ExportVarMetadata->getNumOperands()) && 607 "Number of slots doesn't match the number of export variables!"); 608 } 609 610 if (ExportFuncMetadata) { 611 ScriptCompiled::ExportFuncList &funcList = mpResult->mExportFuncs; 612 613 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) { 614 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i); 615 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) { 616 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0); 617 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) { 618 llvm::StringRef ExportFuncName = 619 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString(); 620 funcList.push_back(mpResult->lookup(ExportFuncName.str().c_str())); 621#if DEBUG_MCJIT_REFLECT 622 LOGD("runCodeGen(): Exported Func: %s @ %p\n", ExportFuncName.str().c_str(), 623 funcList.back()); 624#endif 625 } 626 } 627 } 628 } 629 630 // Tell code emitter now can release the memory using during the JIT since 631 // we have done the code emission 632 mCodeEmitter->releaseUnnecessary(); 633 634 return 0; 635} 636#endif // USE_OLD_JIT 637 638 639#if USE_MCJIT 640int Compiler::runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM, 641 llvm::NamedMDNode const *ExportVarMetadata, 642 llvm::NamedMDNode const *ExportFuncMetadata) { 643 // Decorate mEmittedELFExecutable with formatted ostream 644 llvm::raw_svector_ostream OutSVOS(mEmittedELFExecutable); 645 646 // Relax all machine instructions 647 TM->setMCRelaxAll(/* RelaxAll= */ true); 648 649 // Create MC code generation pass manager 650 llvm::PassManager MCCodeGenPasses; 651 652 // Add TargetData to MC code generation pass manager 653 MCCodeGenPasses.add(TD); 654 655 // Add MC code generation passes to pass manager 656 llvm::MCContext *Ctx; 657 if (TM->addPassesToEmitMC(MCCodeGenPasses, Ctx, OutSVOS, 658 CodeGenOptLevel, false)) { 659 setError("Fail to add passes to emit file"); 660 return 1; 661 } 662 663 MCCodeGenPasses.run(*mModule); 664 OutSVOS.flush(); 665 666 // Load the ELF Object 667 mRSExecutable = 668 rsloaderCreateExec((unsigned char *)&*mEmittedELFExecutable.begin(), 669 mEmittedELFExecutable.size(), 670 &resolveSymbolAdapter, this); 671 672 if (!mRSExecutable) { 673 setError("Fail to load emitted ELF relocatable file"); 674 return 1; 675 } 676 677#if !USE_OLD_JIT 678 // Note: If old JIT is compiled then we prefer the old version instead of the 679 // new version. 680 681 if (ExportVarMetadata) { 682 ScriptCompiled::ExportVarList &varList = mpResult->mExportVars; 683 684 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) { 685 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i); 686 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) { 687 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0); 688 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) { 689 llvm::StringRef ExportVarName = 690 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString(); 691 692 varList.push_back( 693 rsloaderGetSymbolAddress(mRSExecutable, 694 ExportVarName.str().c_str())); 695#if DEBUG_MCJIT_REFLECT 696 LOGD("runMCCodeGen(): Exported Var: %s @ %p\n", ExportVarName.str().c_str(), 697 varList.back()); 698#endif 699 continue; 700 } 701 } 702 703 varList.push_back(NULL); 704 } 705 } 706 707 if (ExportFuncMetadata) { 708 ScriptCompiled::ExportFuncList &funcList = mpResult->mExportFuncs; 709 710 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) { 711 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i); 712 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) { 713 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0); 714 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) { 715 llvm::StringRef ExportFuncName = 716 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString(); 717 718 funcList.push_back( 719 rsloaderGetSymbolAddress(mRSExecutable, 720 ExportFuncName.str().c_str())); 721#if DEBUG_MCJIT_RELECT 722 LOGD("runMCCodeGen(): Exported Func: %s @ %p\n", ExportFuncName.str().c_str(), 723 funcList.back()); 724#endif 725 } 726 } 727 } 728 } 729#endif // !USE_OLD_JIT 730 return 0; 731} 732#endif // USE_MCJIT 733 734 735int Compiler::runLTO(llvm::TargetData *TD, 736 llvm::NamedMDNode const *ExportVarMetadata, 737 llvm::NamedMDNode const *ExportFuncMetadata) { 738 llvm::PassManager LTOPasses; 739 740 // Add TargetData to LTO passes 741 LTOPasses.add(TD); 742 743 // Collect All Exported Symbols 744 std::vector<const char*> ExportSymbols; 745 746 // Note: This is a workaround for getting export variable and function name. 747 // We should refine it soon. 748 if (ExportVarMetadata) { 749 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) { 750 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i); 751 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) { 752 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0); 753 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) { 754 llvm::StringRef ExportVarName = 755 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString(); 756 ExportSymbols.push_back(ExportVarName.data()); 757 } 758 } 759 } 760 } 761 762 if (ExportFuncMetadata) { 763 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) { 764 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i); 765 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) { 766 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0); 767 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) { 768 llvm::StringRef ExportFuncName = 769 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString(); 770 ExportSymbols.push_back(ExportFuncName.data()); 771 } 772 } 773 } 774 } 775 776 // root() and init() are born to be exported 777 ExportSymbols.push_back("root"); 778 ExportSymbols.push_back("init"); 779 780 // We now create passes list performing LTO. These are copied from 781 // (including comments) llvm::createStandardLTOPasses(). 782 783 // Internalize all other symbols not listed in ExportSymbols 784 LTOPasses.add(llvm::createInternalizePass(ExportSymbols)); 785 786 // Propagate constants at call sites into the functions they call. This 787 // opens opportunities for globalopt (and inlining) by substituting 788 // function pointers passed as arguments to direct uses of functions. 789 LTOPasses.add(llvm::createIPSCCPPass()); 790 791 // Now that we internalized some globals, see if we can hack on them! 792 LTOPasses.add(llvm::createGlobalOptimizerPass()); 793 794 // Linking modules together can lead to duplicated global constants, only 795 // keep one copy of each constant... 796 LTOPasses.add(llvm::createConstantMergePass()); 797 798 // Remove unused arguments from functions... 799 LTOPasses.add(llvm::createDeadArgEliminationPass()); 800 801 // Reduce the code after globalopt and ipsccp. Both can open up 802 // significant simplification opportunities, and both can propagate 803 // functions through function pointers. When this happens, we often have 804 // to resolve varargs calls, etc, so let instcombine do this. 805 LTOPasses.add(llvm::createInstructionCombiningPass()); 806 807 // Inline small functions 808 LTOPasses.add(llvm::createFunctionInliningPass()); 809 810 // Remove dead EH info. 811 LTOPasses.add(llvm::createPruneEHPass()); 812 813 // Internalize the globals again after inlining 814 LTOPasses.add(llvm::createGlobalOptimizerPass()); 815 816 // Remove dead functions. 817 LTOPasses.add(llvm::createGlobalDCEPass()); 818 819 // If we didn't decide to inline a function, check to see if we can 820 // transform it to pass arguments by value instead of by reference. 821 LTOPasses.add(llvm::createArgumentPromotionPass()); 822 823 // The IPO passes may leave cruft around. Clean up after them. 824 LTOPasses.add(llvm::createInstructionCombiningPass()); 825 LTOPasses.add(llvm::createJumpThreadingPass()); 826 827 // Break up allocas 828 LTOPasses.add(llvm::createScalarReplAggregatesPass()); 829 830 // Run a few AA driven optimizations here and now, to cleanup the code. 831 LTOPasses.add(llvm::createFunctionAttrsPass()); // Add nocapture. 832 LTOPasses.add(llvm::createGlobalsModRefPass()); // IP alias analysis. 833 834 // Hoist loop invariants. 835 LTOPasses.add(llvm::createLICMPass()); 836 837 // Remove redundancies. 838 LTOPasses.add(llvm::createGVNPass()); 839 840 // Remove dead memcpys. 841 LTOPasses.add(llvm::createMemCpyOptPass()); 842 843 // Nuke dead stores. 844 LTOPasses.add(llvm::createDeadStoreEliminationPass()); 845 846 // Cleanup and simplify the code after the scalar optimizations. 847 LTOPasses.add(llvm::createInstructionCombiningPass()); 848 849 LTOPasses.add(llvm::createJumpThreadingPass()); 850 851 // Delete basic blocks, which optimization passes may have killed. 852 LTOPasses.add(llvm::createCFGSimplificationPass()); 853 854 // Now that we have optimized the program, discard unreachable functions. 855 LTOPasses.add(llvm::createGlobalDCEPass()); 856 857 LTOPasses.run(*mModule); 858 859 return 0; 860} 861 862 863#if USE_MCJIT 864void *Compiler::getSymbolAddress(char const *name) { 865 return rsloaderGetSymbolAddress(mRSExecutable, name); 866} 867#endif 868 869 870#if USE_MCJIT 871void *Compiler::resolveSymbolAdapter(void *context, char const *name) { 872 Compiler *self = reinterpret_cast<Compiler *>(context); 873 874 if (void *Addr = FindRuntimeFunction(name)) { 875 return Addr; 876 } 877 878 if (self->mpSymbolLookupFn) { 879 if (void *Addr = self->mpSymbolLookupFn(self->mpSymbolLookupContext, name)) { 880 return Addr; 881 } 882 } 883 884 LOGE("Unable to resolve symbol: %s\n", name); 885 return NULL; 886} 887#endif 888 889 890Compiler::~Compiler() { 891 delete mModule; 892 delete mContext; 893 894#if USE_MCJIT 895 rsloaderDisposeExec(mRSExecutable); 896#endif 897 898 // llvm::llvm_shutdown(); 899} 900 901#if USE_MCJIT && USE_DISASSEMBLER 902void Compiler::Disassemble(llvm::Target const *Target, 903 llvm::TargetMachine *TM, 904 std::string const &Name, 905 unsigned char const *Func, 906 size_t FuncSize) { 907 llvm::raw_ostream *OS; 908 909#if USE_DISASSEMBLER_FILE 910 std::string ErrorInfo; 911 OS = new llvm::raw_fd_ostream("/data/local/tmp/mcjit-dis.s", ErrorInfo, 912 llvm::raw_fd_ostream::F_Append); 913 914 if (!ErrorInfo.empty()) { // some errors occurred 915 // LOGE("Error in creating disassembly file"); 916 delete OS; 917 return; 918 } 919#else 920 OS = &llvm::outs(); 921#endif 922 923 *OS << "MC/ Disassembled code: " << Name << "\n"; 924 925 const llvm::MCAsmInfo *AsmInfo; 926 const llvm::MCDisassembler *Disassmbler; 927 llvm::MCInstPrinter *IP; 928 929 AsmInfo = Target->createAsmInfo(Compiler::Triple); 930 Disassmbler = Target->createMCDisassembler(); 931 IP = Target->createMCInstPrinter(*TM, 932 AsmInfo->getAssemblerDialect(), 933 *AsmInfo); 934 935 const BufferMemoryObject *BufferMObj = new BufferMemoryObject(Func, FuncSize); 936 937 uint64_t Size; 938 uint64_t Index; 939 940 for (Index = 0; Index < FuncSize; Index += Size) { 941 llvm::MCInst Inst; 942 943 if (Disassmbler->getInstruction(Inst, Size, *BufferMObj, Index, 944 /* REMOVED */ llvm::nulls())) { 945 OS->indent(4); 946 OS->write("0x", 2); 947 OS->write_hex((uint32_t)Func + Index); 948 OS->write(": 0x", 4); 949 OS->write_hex(*(uint32_t *)(Func + Index)); 950 IP->printInst(&Inst, *OS); 951 *OS << "\n"; 952 } else { 953 if (Size == 0) 954 Size = 1; // skip illegible bytes 955 } 956 } 957 958 *OS << "\n"; 959 delete BufferMObj; 960 961 delete AsmInfo; 962 delete Disassmbler; 963 delete IP; 964 965#if USE_DISASSEMBLER_FILE 966 // If you want the disassemble results write to file, uncomment this. 967 ((llvm::raw_fd_ostream*)OS)->close(); 968 delete OS; 969#endif 970} 971#endif 972 973 974} // namespace bcc 975