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