/3rd_party/llvm/lib/ExecutionEngine/JIT/JIT.cpp

https://code.google.com/p/softart/ · C++ · 692 lines · 480 code · 92 blank · 120 comment · 103 complexity · 00548d3534851280ec627b4ed6f56a21 MD5 · raw file

  1. //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This tool implements a just-in-time compiler for LLVM, allowing direct
  11. // execution of LLVM bitcode in an efficient manner.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "JIT.h"
  15. #include "llvm/ADT/SmallPtrSet.h"
  16. #include "llvm/CodeGen/JITCodeEmitter.h"
  17. #include "llvm/CodeGen/MachineCodeInfo.h"
  18. #include "llvm/Config/config.h"
  19. #include "llvm/ExecutionEngine/GenericValue.h"
  20. #include "llvm/ExecutionEngine/JITEventListener.h"
  21. #include "llvm/ExecutionEngine/JITMemoryManager.h"
  22. #include "llvm/IR/Constants.h"
  23. #include "llvm/IR/DataLayout.h"
  24. #include "llvm/IR/DerivedTypes.h"
  25. #include "llvm/IR/Function.h"
  26. #include "llvm/IR/GlobalVariable.h"
  27. #include "llvm/IR/Instructions.h"
  28. #include "llvm/Support/Dwarf.h"
  29. #include "llvm/Support/DynamicLibrary.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include "llvm/Support/ManagedStatic.h"
  32. #include "llvm/Support/MutexGuard.h"
  33. #include "llvm/Target/TargetJITInfo.h"
  34. #include "llvm/Target/TargetMachine.h"
  35. using namespace llvm;
  36. #ifdef __APPLE__
  37. // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
  38. // of atexit). It passes the address of linker generated symbol __dso_handle
  39. // to the function.
  40. // This configuration change happened at version 5330.
  41. # include <AvailabilityMacros.h>
  42. # if defined(MAC_OS_X_VERSION_10_4) && \
  43. ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
  44. (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
  45. __APPLE_CC__ >= 5330))
  46. # ifndef HAVE___DSO_HANDLE
  47. # define HAVE___DSO_HANDLE 1
  48. # endif
  49. # endif
  50. #endif
  51. #if HAVE___DSO_HANDLE
  52. extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
  53. #endif
  54. namespace {
  55. static struct RegisterJIT {
  56. RegisterJIT() { JIT::Register(); }
  57. } JITRegistrator;
  58. }
  59. extern "C" void LLVMLinkInJIT() {
  60. }
  61. /// createJIT - This is the factory method for creating a JIT for the current
  62. /// machine, it does not fall back to the interpreter. This takes ownership
  63. /// of the module.
  64. ExecutionEngine *JIT::createJIT(Module *M,
  65. std::string *ErrorStr,
  66. JITMemoryManager *JMM,
  67. bool GVsWithCode,
  68. TargetMachine *TM) {
  69. // Try to register the program as a source of symbols to resolve against.
  70. //
  71. // FIXME: Don't do this here.
  72. sys::DynamicLibrary::LoadLibraryPermanently(0, NULL);
  73. // If the target supports JIT code generation, create the JIT.
  74. if (TargetJITInfo *TJ = TM->getJITInfo()) {
  75. return new JIT(M, *TM, *TJ, JMM, GVsWithCode);
  76. } else {
  77. if (ErrorStr)
  78. *ErrorStr = "target does not support JIT code generation";
  79. return 0;
  80. }
  81. }
  82. namespace {
  83. /// This class supports the global getPointerToNamedFunction(), which allows
  84. /// bugpoint or gdb users to search for a function by name without any context.
  85. class JitPool {
  86. SmallPtrSet<JIT*, 1> JITs; // Optimize for process containing just 1 JIT.
  87. mutable sys::Mutex Lock;
  88. public:
  89. void Add(JIT *jit) {
  90. MutexGuard guard(Lock);
  91. JITs.insert(jit);
  92. }
  93. void Remove(JIT *jit) {
  94. MutexGuard guard(Lock);
  95. JITs.erase(jit);
  96. }
  97. void *getPointerToNamedFunction(const char *Name) const {
  98. MutexGuard guard(Lock);
  99. assert(JITs.size() != 0 && "No Jit registered");
  100. //search function in every instance of JIT
  101. for (SmallPtrSet<JIT*, 1>::const_iterator Jit = JITs.begin(),
  102. end = JITs.end();
  103. Jit != end; ++Jit) {
  104. if (Function *F = (*Jit)->FindFunctionNamed(Name))
  105. return (*Jit)->getPointerToFunction(F);
  106. }
  107. // The function is not available : fallback on the first created (will
  108. // search in symbol of the current program/library)
  109. return (*JITs.begin())->getPointerToNamedFunction(Name);
  110. }
  111. };
  112. ManagedStatic<JitPool> AllJits;
  113. }
  114. extern "C" {
  115. // getPointerToNamedFunction - This function is used as a global wrapper to
  116. // JIT::getPointerToNamedFunction for the purpose of resolving symbols when
  117. // bugpoint is debugging the JIT. In that scenario, we are loading an .so and
  118. // need to resolve function(s) that are being mis-codegenerated, so we need to
  119. // resolve their addresses at runtime, and this is the way to do it.
  120. void *getPointerToNamedFunction(const char *Name) {
  121. return AllJits->getPointerToNamedFunction(Name);
  122. }
  123. }
  124. JIT::JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
  125. JITMemoryManager *jmm, bool GVsWithCode)
  126. : ExecutionEngine(M), TM(tm), TJI(tji),
  127. JMM(jmm ? jmm : JITMemoryManager::CreateDefaultMemManager()),
  128. AllocateGVsWithCode(GVsWithCode), isAlreadyCodeGenerating(false) {
  129. setDataLayout(TM.getDataLayout());
  130. jitstate = new JITState(M);
  131. // Initialize JCE
  132. JCE = createEmitter(*this, JMM, TM);
  133. // Register in global list of all JITs.
  134. AllJits->Add(this);
  135. // Add target data
  136. MutexGuard locked(lock);
  137. FunctionPassManager &PM = jitstate->getPM(locked);
  138. PM.add(new DataLayout(*TM.getDataLayout()));
  139. // Turn the machine code intermediate representation into bytes in memory that
  140. // may be executed.
  141. if (TM.addPassesToEmitMachineCode(PM, *JCE)) {
  142. report_fatal_error("Target does not support machine code emission!");
  143. }
  144. // Initialize passes.
  145. PM.doInitialization();
  146. }
  147. JIT::~JIT() {
  148. // Cleanup.
  149. AllJits->Remove(this);
  150. delete jitstate;
  151. delete JCE;
  152. // JMM is a ownership of JCE, so we no need delete JMM here.
  153. delete &TM;
  154. }
  155. /// addModule - Add a new Module to the JIT. If we previously removed the last
  156. /// Module, we need re-initialize jitstate with a valid Module.
  157. void JIT::addModule(Module *M) {
  158. MutexGuard locked(lock);
  159. if (Modules.empty()) {
  160. assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
  161. jitstate = new JITState(M);
  162. FunctionPassManager &PM = jitstate->getPM(locked);
  163. PM.add(new DataLayout(*TM.getDataLayout()));
  164. // Turn the machine code intermediate representation into bytes in memory
  165. // that may be executed.
  166. if (TM.addPassesToEmitMachineCode(PM, *JCE)) {
  167. report_fatal_error("Target does not support machine code emission!");
  168. }
  169. // Initialize passes.
  170. PM.doInitialization();
  171. }
  172. ExecutionEngine::addModule(M);
  173. }
  174. /// removeModule - If we are removing the last Module, invalidate the jitstate
  175. /// since the PassManager it contains references a released Module.
  176. bool JIT::removeModule(Module *M) {
  177. bool result = ExecutionEngine::removeModule(M);
  178. MutexGuard locked(lock);
  179. if (jitstate && jitstate->getModule() == M) {
  180. delete jitstate;
  181. jitstate = 0;
  182. }
  183. if (!jitstate && !Modules.empty()) {
  184. jitstate = new JITState(Modules[0]);
  185. FunctionPassManager &PM = jitstate->getPM(locked);
  186. PM.add(new DataLayout(*TM.getDataLayout()));
  187. // Turn the machine code intermediate representation into bytes in memory
  188. // that may be executed.
  189. if (TM.addPassesToEmitMachineCode(PM, *JCE)) {
  190. report_fatal_error("Target does not support machine code emission!");
  191. }
  192. // Initialize passes.
  193. PM.doInitialization();
  194. }
  195. return result;
  196. }
  197. /// run - Start execution with the specified function and arguments.
  198. ///
  199. GenericValue JIT::runFunction(Function *F,
  200. const std::vector<GenericValue> &ArgValues) {
  201. assert(F && "Function *F was null at entry to run()");
  202. void *FPtr = getPointerToFunction(F);
  203. assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
  204. FunctionType *FTy = F->getFunctionType();
  205. Type *RetTy = FTy->getReturnType();
  206. assert((FTy->getNumParams() == ArgValues.size() ||
  207. (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
  208. "Wrong number of arguments passed into function!");
  209. assert(FTy->getNumParams() == ArgValues.size() &&
  210. "This doesn't support passing arguments through varargs (yet)!");
  211. // Handle some common cases first. These cases correspond to common `main'
  212. // prototypes.
  213. if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
  214. switch (ArgValues.size()) {
  215. case 3:
  216. if (FTy->getParamType(0)->isIntegerTy(32) &&
  217. FTy->getParamType(1)->isPointerTy() &&
  218. FTy->getParamType(2)->isPointerTy()) {
  219. int (*PF)(int, char **, const char **) =
  220. (int(*)(int, char **, const char **))(intptr_t)FPtr;
  221. // Call the function.
  222. GenericValue rv;
  223. rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
  224. (char **)GVTOP(ArgValues[1]),
  225. (const char **)GVTOP(ArgValues[2])));
  226. return rv;
  227. }
  228. break;
  229. case 2:
  230. if (FTy->getParamType(0)->isIntegerTy(32) &&
  231. FTy->getParamType(1)->isPointerTy()) {
  232. int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
  233. // Call the function.
  234. GenericValue rv;
  235. rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
  236. (char **)GVTOP(ArgValues[1])));
  237. return rv;
  238. }
  239. break;
  240. case 1:
  241. if (FTy->getParamType(0)->isIntegerTy(32)) {
  242. GenericValue rv;
  243. int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
  244. rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
  245. return rv;
  246. }
  247. if (FTy->getParamType(0)->isPointerTy()) {
  248. GenericValue rv;
  249. int (*PF)(char *) = (int(*)(char *))(intptr_t)FPtr;
  250. rv.IntVal = APInt(32, PF((char*)GVTOP(ArgValues[0])));
  251. return rv;
  252. }
  253. break;
  254. }
  255. }
  256. // Handle cases where no arguments are passed first.
  257. if (ArgValues.empty()) {
  258. GenericValue rv;
  259. switch (RetTy->getTypeID()) {
  260. default: llvm_unreachable("Unknown return type for function call!");
  261. case Type::IntegerTyID: {
  262. unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
  263. if (BitWidth == 1)
  264. rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
  265. else if (BitWidth <= 8)
  266. rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
  267. else if (BitWidth <= 16)
  268. rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
  269. else if (BitWidth <= 32)
  270. rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
  271. else if (BitWidth <= 64)
  272. rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
  273. else
  274. llvm_unreachable("Integer types > 64 bits not supported");
  275. return rv;
  276. }
  277. case Type::VoidTyID:
  278. rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
  279. return rv;
  280. case Type::FloatTyID:
  281. rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
  282. return rv;
  283. case Type::DoubleTyID:
  284. rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
  285. return rv;
  286. case Type::X86_FP80TyID:
  287. case Type::FP128TyID:
  288. case Type::PPC_FP128TyID:
  289. llvm_unreachable("long double not supported yet");
  290. case Type::PointerTyID:
  291. return PTOGV(((void*(*)())(intptr_t)FPtr)());
  292. }
  293. }
  294. // Okay, this is not one of our quick and easy cases. Because we don't have a
  295. // full FFI, we have to codegen a nullary stub function that just calls the
  296. // function we are interested in, passing in constants for all of the
  297. // arguments. Make this function and return.
  298. // First, create the function.
  299. FunctionType *STy=FunctionType::get(RetTy, false);
  300. Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
  301. F->getParent());
  302. // Insert a basic block.
  303. BasicBlock *StubBB = BasicBlock::Create(F->getContext(), "", Stub);
  304. // Convert all of the GenericValue arguments over to constants. Note that we
  305. // currently don't support varargs.
  306. SmallVector<Value*, 8> Args;
  307. for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
  308. Constant *C = 0;
  309. Type *ArgTy = FTy->getParamType(i);
  310. const GenericValue &AV = ArgValues[i];
  311. switch (ArgTy->getTypeID()) {
  312. default: llvm_unreachable("Unknown argument type for function call!");
  313. case Type::IntegerTyID:
  314. C = ConstantInt::get(F->getContext(), AV.IntVal);
  315. break;
  316. case Type::FloatTyID:
  317. C = ConstantFP::get(F->getContext(), APFloat(AV.FloatVal));
  318. break;
  319. case Type::DoubleTyID:
  320. C = ConstantFP::get(F->getContext(), APFloat(AV.DoubleVal));
  321. break;
  322. case Type::PPC_FP128TyID:
  323. case Type::X86_FP80TyID:
  324. case Type::FP128TyID:
  325. C = ConstantFP::get(F->getContext(), APFloat(ArgTy->getFltSemantics(),
  326. AV.IntVal));
  327. break;
  328. case Type::PointerTyID:
  329. void *ArgPtr = GVTOP(AV);
  330. if (sizeof(void*) == 4)
  331. C = ConstantInt::get(Type::getInt32Ty(F->getContext()),
  332. (int)(intptr_t)ArgPtr);
  333. else
  334. C = ConstantInt::get(Type::getInt64Ty(F->getContext()),
  335. (intptr_t)ArgPtr);
  336. // Cast the integer to pointer
  337. C = ConstantExpr::getIntToPtr(C, ArgTy);
  338. break;
  339. }
  340. Args.push_back(C);
  341. }
  342. CallInst *TheCall = CallInst::Create(F, Args, "", StubBB);
  343. TheCall->setCallingConv(F->getCallingConv());
  344. TheCall->setTailCall();
  345. if (!TheCall->getType()->isVoidTy())
  346. // Return result of the call.
  347. ReturnInst::Create(F->getContext(), TheCall, StubBB);
  348. else
  349. ReturnInst::Create(F->getContext(), StubBB); // Just return void.
  350. // Finally, call our nullary stub function.
  351. GenericValue Result = runFunction(Stub, std::vector<GenericValue>());
  352. // Erase it, since no other function can have a reference to it.
  353. Stub->eraseFromParent();
  354. // And return the result.
  355. return Result;
  356. }
  357. void JIT::RegisterJITEventListener(JITEventListener *L) {
  358. if (L == NULL)
  359. return;
  360. MutexGuard locked(lock);
  361. EventListeners.push_back(L);
  362. }
  363. void JIT::UnregisterJITEventListener(JITEventListener *L) {
  364. if (L == NULL)
  365. return;
  366. MutexGuard locked(lock);
  367. std::vector<JITEventListener*>::reverse_iterator I=
  368. std::find(EventListeners.rbegin(), EventListeners.rend(), L);
  369. if (I != EventListeners.rend()) {
  370. std::swap(*I, EventListeners.back());
  371. EventListeners.pop_back();
  372. }
  373. }
  374. void JIT::NotifyFunctionEmitted(
  375. const Function &F,
  376. void *Code, size_t Size,
  377. const JITEvent_EmittedFunctionDetails &Details) {
  378. MutexGuard locked(lock);
  379. for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
  380. EventListeners[I]->NotifyFunctionEmitted(F, Code, Size, Details);
  381. }
  382. }
  383. void JIT::NotifyFreeingMachineCode(void *OldPtr) {
  384. MutexGuard locked(lock);
  385. for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
  386. EventListeners[I]->NotifyFreeingMachineCode(OldPtr);
  387. }
  388. }
  389. /// runJITOnFunction - Run the FunctionPassManager full of
  390. /// just-in-time compilation passes on F, hopefully filling in
  391. /// GlobalAddress[F] with the address of F's machine code.
  392. ///
  393. void JIT::runJITOnFunction(Function *F, MachineCodeInfo *MCI) {
  394. MutexGuard locked(lock);
  395. class MCIListener : public JITEventListener {
  396. MachineCodeInfo *const MCI;
  397. public:
  398. MCIListener(MachineCodeInfo *mci) : MCI(mci) {}
  399. virtual void NotifyFunctionEmitted(const Function &,
  400. void *Code, size_t Size,
  401. const EmittedFunctionDetails &) {
  402. MCI->setAddress(Code);
  403. MCI->setSize(Size);
  404. }
  405. };
  406. MCIListener MCIL(MCI);
  407. if (MCI)
  408. RegisterJITEventListener(&MCIL);
  409. runJITOnFunctionUnlocked(F, locked);
  410. if (MCI)
  411. UnregisterJITEventListener(&MCIL);
  412. }
  413. void JIT::runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked) {
  414. assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
  415. jitTheFunction(F, locked);
  416. // If the function referred to another function that had not yet been
  417. // read from bitcode, and we are jitting non-lazily, emit it now.
  418. while (!jitstate->getPendingFunctions(locked).empty()) {
  419. Function *PF = jitstate->getPendingFunctions(locked).back();
  420. jitstate->getPendingFunctions(locked).pop_back();
  421. assert(!PF->hasAvailableExternallyLinkage() &&
  422. "Externally-defined function should not be in pending list.");
  423. jitTheFunction(PF, locked);
  424. // Now that the function has been jitted, ask the JITEmitter to rewrite
  425. // the stub with real address of the function.
  426. updateFunctionStub(PF);
  427. }
  428. }
  429. void JIT::jitTheFunction(Function *F, const MutexGuard &locked) {
  430. isAlreadyCodeGenerating = true;
  431. jitstate->getPM(locked).run(*F);
  432. isAlreadyCodeGenerating = false;
  433. // clear basic block addresses after this function is done
  434. getBasicBlockAddressMap(locked).clear();
  435. }
  436. /// getPointerToFunction - This method is used to get the address of the
  437. /// specified function, compiling it if necessary.
  438. ///
  439. void *JIT::getPointerToFunction(Function *F) {
  440. if (void *Addr = getPointerToGlobalIfAvailable(F))
  441. return Addr; // Check if function already code gen'd
  442. MutexGuard locked(lock);
  443. // Now that this thread owns the lock, make sure we read in the function if it
  444. // exists in this Module.
  445. std::string ErrorMsg;
  446. if (F->Materialize(&ErrorMsg)) {
  447. report_fatal_error("Error reading function '" + F->getName()+
  448. "' from bitcode file: " + ErrorMsg);
  449. }
  450. // ... and check if another thread has already code gen'd the function.
  451. if (void *Addr = getPointerToGlobalIfAvailable(F))
  452. return Addr;
  453. if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
  454. bool AbortOnFailure = !F->hasExternalWeakLinkage();
  455. void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
  456. addGlobalMapping(F, Addr);
  457. return Addr;
  458. }
  459. runJITOnFunctionUnlocked(F, locked);
  460. void *Addr = getPointerToGlobalIfAvailable(F);
  461. assert(Addr && "Code generation didn't add function to GlobalAddress table!");
  462. return Addr;
  463. }
  464. void JIT::addPointerToBasicBlock(const BasicBlock *BB, void *Addr) {
  465. MutexGuard locked(lock);
  466. BasicBlockAddressMapTy::iterator I =
  467. getBasicBlockAddressMap(locked).find(BB);
  468. if (I == getBasicBlockAddressMap(locked).end()) {
  469. getBasicBlockAddressMap(locked)[BB] = Addr;
  470. } else {
  471. // ignore repeats: some BBs can be split into few MBBs?
  472. }
  473. }
  474. void JIT::clearPointerToBasicBlock(const BasicBlock *BB) {
  475. MutexGuard locked(lock);
  476. getBasicBlockAddressMap(locked).erase(BB);
  477. }
  478. void *JIT::getPointerToBasicBlock(BasicBlock *BB) {
  479. // make sure it's function is compiled by JIT
  480. (void)getPointerToFunction(BB->getParent());
  481. // resolve basic block address
  482. MutexGuard locked(lock);
  483. BasicBlockAddressMapTy::iterator I =
  484. getBasicBlockAddressMap(locked).find(BB);
  485. if (I != getBasicBlockAddressMap(locked).end()) {
  486. return I->second;
  487. } else {
  488. llvm_unreachable("JIT does not have BB address for address-of-label, was"
  489. " it eliminated by optimizer?");
  490. }
  491. }
  492. void *JIT::getPointerToNamedFunction(const std::string &Name,
  493. bool AbortOnFailure){
  494. if (!isSymbolSearchingDisabled()) {
  495. void *ptr = JMM->getPointerToNamedFunction(Name, false);
  496. if (ptr)
  497. return ptr;
  498. }
  499. /// If a LazyFunctionCreator is installed, use it to get/create the function.
  500. if (LazyFunctionCreator)
  501. if (void *RP = LazyFunctionCreator(Name))
  502. return RP;
  503. if (AbortOnFailure) {
  504. report_fatal_error("Program used external function '"+Name+
  505. "' which could not be resolved!");
  506. }
  507. return 0;
  508. }
  509. /// getOrEmitGlobalVariable - Return the address of the specified global
  510. /// variable, possibly emitting it to memory if needed. This is used by the
  511. /// Emitter.
  512. void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
  513. MutexGuard locked(lock);
  514. void *Ptr = getPointerToGlobalIfAvailable(GV);
  515. if (Ptr) return Ptr;
  516. // If the global is external, just remember the address.
  517. if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage()) {
  518. #if HAVE___DSO_HANDLE
  519. if (GV->getName() == "__dso_handle")
  520. return (void*)&__dso_handle;
  521. #endif
  522. Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName());
  523. if (Ptr == 0) {
  524. report_fatal_error("Could not resolve external global address: "
  525. +GV->getName());
  526. }
  527. addGlobalMapping(GV, Ptr);
  528. } else {
  529. // If the global hasn't been emitted to memory yet, allocate space and
  530. // emit it into memory.
  531. Ptr = getMemoryForGV(GV);
  532. addGlobalMapping(GV, Ptr);
  533. EmitGlobalVariable(GV); // Initialize the variable.
  534. }
  535. return Ptr;
  536. }
  537. /// recompileAndRelinkFunction - This method is used to force a function
  538. /// which has already been compiled, to be compiled again, possibly
  539. /// after it has been modified. Then the entry to the old copy is overwritten
  540. /// with a branch to the new copy. If there was no old copy, this acts
  541. /// just like JIT::getPointerToFunction().
  542. ///
  543. void *JIT::recompileAndRelinkFunction(Function *F) {
  544. void *OldAddr = getPointerToGlobalIfAvailable(F);
  545. // If it's not already compiled there is no reason to patch it up.
  546. if (OldAddr == 0) { return getPointerToFunction(F); }
  547. // Delete the old function mapping.
  548. addGlobalMapping(F, 0);
  549. // Recodegen the function
  550. runJITOnFunction(F);
  551. // Update state, forward the old function to the new function.
  552. void *Addr = getPointerToGlobalIfAvailable(F);
  553. assert(Addr && "Code generation didn't add function to GlobalAddress table!");
  554. TJI.replaceMachineCodeForFunction(OldAddr, Addr);
  555. return Addr;
  556. }
  557. /// getMemoryForGV - This method abstracts memory allocation of global
  558. /// variable so that the JIT can allocate thread local variables depending
  559. /// on the target.
  560. ///
  561. char* JIT::getMemoryForGV(const GlobalVariable* GV) {
  562. char *Ptr;
  563. // GlobalVariable's which are not "constant" will cause trouble in a server
  564. // situation. It's returned in the same block of memory as code which may
  565. // not be writable.
  566. if (isGVCompilationDisabled() && !GV->isConstant()) {
  567. report_fatal_error("Compilation of non-internal GlobalValue is disabled!");
  568. }
  569. // Some applications require globals and code to live together, so they may
  570. // be allocated into the same buffer, but in general globals are allocated
  571. // through the memory manager which puts them near the code but not in the
  572. // same buffer.
  573. Type *GlobalType = GV->getType()->getElementType();
  574. size_t S = getDataLayout()->getTypeAllocSize(GlobalType);
  575. size_t A = getDataLayout()->getPreferredAlignment(GV);
  576. if (GV->isThreadLocal()) {
  577. MutexGuard locked(lock);
  578. Ptr = TJI.allocateThreadLocalMemory(S);
  579. } else if (TJI.allocateSeparateGVMemory()) {
  580. if (A <= 8) {
  581. Ptr = (char*)malloc(S);
  582. } else {
  583. // Allocate S+A bytes of memory, then use an aligned pointer within that
  584. // space.
  585. Ptr = (char*)malloc(S+A);
  586. unsigned MisAligned = ((intptr_t)Ptr & (A-1));
  587. Ptr = Ptr + (MisAligned ? (A-MisAligned) : 0);
  588. }
  589. } else if (AllocateGVsWithCode) {
  590. Ptr = (char*)JCE->allocateSpace(S, A);
  591. } else {
  592. Ptr = (char*)JCE->allocateGlobal(S, A);
  593. }
  594. return Ptr;
  595. }
  596. void JIT::addPendingFunction(Function *F) {
  597. MutexGuard locked(lock);
  598. jitstate->getPendingFunctions(locked).push_back(F);
  599. }
  600. JITEventListener::~JITEventListener() {}