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