1 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tool implements a just-in-time compiler for LLVM, allowing direct
11 // execution of LLVM bitcode in an efficient manner.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/CodeGen/JITCodeEmitter.h"
18 #include "llvm/CodeGen/MachineCodeInfo.h"
19 #include "llvm/Config/config.h"
20 #include "llvm/ExecutionEngine/GenericValue.h"
21 #include "llvm/ExecutionEngine/JITEventListener.h"
22 #include "llvm/ExecutionEngine/JITMemoryManager.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/GlobalVariable.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/Support/Dwarf.h"
31 #include "llvm/Support/DynamicLibrary.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/ManagedStatic.h"
34 #include "llvm/Support/MutexGuard.h"
35 #include "llvm/Target/TargetJITInfo.h"
36 #include "llvm/Target/TargetMachine.h"
37 #include "llvm/Target/TargetSubtargetInfo.h"
42 // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
43 // of atexit). It passes the address of linker generated symbol __dso_handle
45 // This configuration change happened at version 5330.
46 # include <AvailabilityMacros.h>
47 # if defined(MAC_OS_X_VERSION_10_4) && \
48 ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
49 (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
50 __APPLE_CC__ >= 5330))
51 # ifndef HAVE___DSO_HANDLE
52 # define HAVE___DSO_HANDLE 1
58 extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
63 static struct RegisterJIT {
64 RegisterJIT() { JIT::Register(); }
69 extern "C" void LLVMLinkInJIT() {
72 /// This is the factory method for creating a JIT for the current machine, it
73 /// does not fall back to the interpreter.
74 ExecutionEngine *JIT::createJIT(std::unique_ptr<Module> M,
75 std::string *ErrorStr,
76 JITMemoryManager *JMM,
79 // Try to register the program as a source of symbols to resolve against.
81 // FIXME: Don't do this here.
82 sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
84 // If the target supports JIT code generation, create the JIT.
85 if (TargetJITInfo *TJ = TM->getSubtargetImpl()->getJITInfo()) {
86 return new JIT(std::move(M), *TM, *TJ, JMM, GVsWithCode);
89 *ErrorStr = "target does not support JIT code generation";
95 /// This class supports the global getPointerToNamedFunction(), which allows
96 /// bugpoint or gdb users to search for a function by name without any context.
98 SmallPtrSet<JIT*, 1> JITs; // Optimize for process containing just 1 JIT.
99 mutable sys::Mutex Lock;
102 MutexGuard guard(Lock);
105 void Remove(JIT *jit) {
106 MutexGuard guard(Lock);
109 void *getPointerToNamedFunction(const char *Name) const {
110 MutexGuard guard(Lock);
111 assert(JITs.size() != 0 && "No Jit registered");
112 //search function in every instance of JIT
113 for (JIT *Jit : JITs) {
114 if (Function *F = Jit->FindFunctionNamed(Name))
115 return Jit->getPointerToFunction(F);
117 // The function is not available : fallback on the first created (will
118 // search in symbol of the current program/library)
119 return (*JITs.begin())->getPointerToNamedFunction(Name);
122 ManagedStatic<JitPool> AllJits;
125 // getPointerToNamedFunction - This function is used as a global wrapper to
126 // JIT::getPointerToNamedFunction for the purpose of resolving symbols when
127 // bugpoint is debugging the JIT. In that scenario, we are loading an .so and
128 // need to resolve function(s) that are being mis-codegenerated, so we need to
129 // resolve their addresses at runtime, and this is the way to do it.
130 void *getPointerToNamedFunction(const char *Name) {
131 return AllJits->getPointerToNamedFunction(Name);
135 JIT::JIT(std::unique_ptr<Module> M, TargetMachine &tm, TargetJITInfo &tji,
136 JITMemoryManager *jmm, bool GVsWithCode)
137 : ExecutionEngine(std::move(M)), TM(tm), TJI(tji),
138 JMM(jmm ? jmm : JITMemoryManager::CreateDefaultMemManager()),
139 AllocateGVsWithCode(GVsWithCode), isAlreadyCodeGenerating(false) {
140 setDataLayout(TM.getSubtargetImpl()->getDataLayout());
142 Module *Mod = Modules.back().get();
143 jitstate = new JITState(Mod);
146 JCE = createEmitter(*this, JMM, TM);
148 // Register in global list of all JITs.
152 MutexGuard locked(lock);
153 FunctionPassManager &PM = jitstate->getPM();
154 Mod->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
155 PM.add(new DataLayoutPass(Mod));
157 // Turn the machine code intermediate representation into bytes in memory that
159 if (TM.addPassesToEmitMachineCode(PM, *JCE, !getVerifyModules())) {
160 report_fatal_error("Target does not support machine code emission!");
163 // Initialize passes.
164 PM.doInitialization();
169 AllJits->Remove(this);
172 // JMM is a ownership of JCE, so we no need delete JMM here.
176 /// Add a new Module to the JIT. If we previously removed the last Module, we
177 /// need re-initialize jitstate with a valid Module.
178 void JIT::addModule(std::unique_ptr<Module> M) {
179 MutexGuard locked(lock);
181 if (Modules.empty()) {
182 assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
184 jitstate = new JITState(M.get());
186 FunctionPassManager &PM = jitstate->getPM();
187 M->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
188 PM.add(new DataLayoutPass(M.get()));
190 // Turn the machine code intermediate representation into bytes in memory
191 // that may be executed.
192 if (TM.addPassesToEmitMachineCode(PM, *JCE, !getVerifyModules())) {
193 report_fatal_error("Target does not support machine code emission!");
196 // Initialize passes.
197 PM.doInitialization();
200 ExecutionEngine::addModule(std::move(M));
203 /// If we are removing the last Module, invalidate the jitstate since the
204 /// PassManager it contains references a released Module.
205 bool JIT::removeModule(Module *M) {
206 bool result = ExecutionEngine::removeModule(M);
208 MutexGuard locked(lock);
210 if (jitstate && jitstate->getModule() == M) {
215 if (!jitstate && !Modules.empty()) {
216 jitstate = new JITState(Modules[0].get());
218 FunctionPassManager &PM = jitstate->getPM();
219 M->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
220 PM.add(new DataLayoutPass(M));
222 // Turn the machine code intermediate representation into bytes in memory
223 // that may be executed.
224 if (TM.addPassesToEmitMachineCode(PM, *JCE, !getVerifyModules())) {
225 report_fatal_error("Target does not support machine code emission!");
228 // Initialize passes.
229 PM.doInitialization();
234 /// run - Start execution with the specified function and arguments.
236 GenericValue JIT::runFunction(Function *F,
237 const std::vector<GenericValue> &ArgValues) {
238 assert(F && "Function *F was null at entry to run()");
240 void *FPtr = getPointerToFunction(F);
241 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
242 FunctionType *FTy = F->getFunctionType();
243 Type *RetTy = FTy->getReturnType();
245 assert((FTy->getNumParams() == ArgValues.size() ||
246 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
247 "Wrong number of arguments passed into function!");
248 assert(FTy->getNumParams() == ArgValues.size() &&
249 "This doesn't support passing arguments through varargs (yet)!");
251 // Handle some common cases first. These cases correspond to common `main'
253 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
254 switch (ArgValues.size()) {
256 if (FTy->getParamType(0)->isIntegerTy(32) &&
257 FTy->getParamType(1)->isPointerTy() &&
258 FTy->getParamType(2)->isPointerTy()) {
259 int (*PF)(int, char **, const char **) =
260 (int(*)(int, char **, const char **))(intptr_t)FPtr;
262 // Call the function.
264 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
265 (char **)GVTOP(ArgValues[1]),
266 (const char **)GVTOP(ArgValues[2])));
271 if (FTy->getParamType(0)->isIntegerTy(32) &&
272 FTy->getParamType(1)->isPointerTy()) {
273 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
275 // Call the function.
277 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
278 (char **)GVTOP(ArgValues[1])));
283 if (FTy->getParamType(0)->isIntegerTy(32)) {
285 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
286 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
289 if (FTy->getParamType(0)->isPointerTy()) {
291 int (*PF)(char *) = (int(*)(char *))(intptr_t)FPtr;
292 rv.IntVal = APInt(32, PF((char*)GVTOP(ArgValues[0])));
299 // Handle cases where no arguments are passed first.
300 if (ArgValues.empty()) {
302 switch (RetTy->getTypeID()) {
303 default: llvm_unreachable("Unknown return type for function call!");
304 case Type::IntegerTyID: {
305 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
307 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
308 else if (BitWidth <= 8)
309 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
310 else if (BitWidth <= 16)
311 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
312 else if (BitWidth <= 32)
313 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
314 else if (BitWidth <= 64)
315 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
317 llvm_unreachable("Integer types > 64 bits not supported");
321 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
323 case Type::FloatTyID:
324 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
326 case Type::DoubleTyID:
327 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
329 case Type::X86_FP80TyID:
330 case Type::FP128TyID:
331 case Type::PPC_FP128TyID:
332 llvm_unreachable("long double not supported yet");
333 case Type::PointerTyID:
334 return PTOGV(((void*(*)())(intptr_t)FPtr)());
338 // Okay, this is not one of our quick and easy cases. Because we don't have a
339 // full FFI, we have to codegen a nullary stub function that just calls the
340 // function we are interested in, passing in constants for all of the
341 // arguments. Make this function and return.
343 // First, create the function.
344 FunctionType *STy=FunctionType::get(RetTy, false);
345 Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
348 // Insert a basic block.
349 BasicBlock *StubBB = BasicBlock::Create(F->getContext(), "", Stub);
351 // Convert all of the GenericValue arguments over to constants. Note that we
352 // currently don't support varargs.
353 SmallVector<Value*, 8> Args;
354 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
355 Constant *C = nullptr;
356 Type *ArgTy = FTy->getParamType(i);
357 const GenericValue &AV = ArgValues[i];
358 switch (ArgTy->getTypeID()) {
359 default: llvm_unreachable("Unknown argument type for function call!");
360 case Type::IntegerTyID:
361 C = ConstantInt::get(F->getContext(), AV.IntVal);
363 case Type::FloatTyID:
364 C = ConstantFP::get(F->getContext(), APFloat(AV.FloatVal));
366 case Type::DoubleTyID:
367 C = ConstantFP::get(F->getContext(), APFloat(AV.DoubleVal));
369 case Type::PPC_FP128TyID:
370 case Type::X86_FP80TyID:
371 case Type::FP128TyID:
372 C = ConstantFP::get(F->getContext(), APFloat(ArgTy->getFltSemantics(),
375 case Type::PointerTyID:
376 void *ArgPtr = GVTOP(AV);
377 if (sizeof(void*) == 4)
378 C = ConstantInt::get(Type::getInt32Ty(F->getContext()),
379 (int)(intptr_t)ArgPtr);
381 C = ConstantInt::get(Type::getInt64Ty(F->getContext()),
383 // Cast the integer to pointer
384 C = ConstantExpr::getIntToPtr(C, ArgTy);
390 CallInst *TheCall = CallInst::Create(F, Args, "", StubBB);
391 TheCall->setCallingConv(F->getCallingConv());
392 TheCall->setTailCall();
393 if (!TheCall->getType()->isVoidTy())
394 // Return result of the call.
395 ReturnInst::Create(F->getContext(), TheCall, StubBB);
397 ReturnInst::Create(F->getContext(), StubBB); // Just return void.
399 // Finally, call our nullary stub function.
400 GenericValue Result = runFunction(Stub, std::vector<GenericValue>());
401 // Erase it, since no other function can have a reference to it.
402 Stub->eraseFromParent();
403 // And return the result.
407 void JIT::RegisterJITEventListener(JITEventListener *L) {
410 MutexGuard locked(lock);
411 EventListeners.push_back(L);
413 void JIT::UnregisterJITEventListener(JITEventListener *L) {
416 MutexGuard locked(lock);
417 std::vector<JITEventListener*>::reverse_iterator I=
418 std::find(EventListeners.rbegin(), EventListeners.rend(), L);
419 if (I != EventListeners.rend()) {
420 std::swap(*I, EventListeners.back());
421 EventListeners.pop_back();
424 void JIT::NotifyFunctionEmitted(
426 void *Code, size_t Size,
427 const JITEvent_EmittedFunctionDetails &Details) {
428 MutexGuard locked(lock);
429 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
430 EventListeners[I]->NotifyFunctionEmitted(F, Code, Size, Details);
434 void JIT::NotifyFreeingMachineCode(void *OldPtr) {
435 MutexGuard locked(lock);
436 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
437 EventListeners[I]->NotifyFreeingMachineCode(OldPtr);
441 /// runJITOnFunction - Run the FunctionPassManager full of
442 /// just-in-time compilation passes on F, hopefully filling in
443 /// GlobalAddress[F] with the address of F's machine code.
445 void JIT::runJITOnFunction(Function *F, MachineCodeInfo *MCI) {
446 MutexGuard locked(lock);
448 class MCIListener : public JITEventListener {
449 MachineCodeInfo *const MCI;
451 MCIListener(MachineCodeInfo *mci) : MCI(mci) {}
452 void NotifyFunctionEmitted(const Function &, void *Code, size_t Size,
453 const EmittedFunctionDetails &) override {
454 MCI->setAddress(Code);
458 MCIListener MCIL(MCI);
460 RegisterJITEventListener(&MCIL);
462 runJITOnFunctionUnlocked(F);
465 UnregisterJITEventListener(&MCIL);
468 void JIT::runJITOnFunctionUnlocked(Function *F) {
469 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
471 jitTheFunctionUnlocked(F);
473 // If the function referred to another function that had not yet been
474 // read from bitcode, and we are jitting non-lazily, emit it now.
475 while (!jitstate->getPendingFunctions().empty()) {
476 Function *PF = jitstate->getPendingFunctions().back();
477 jitstate->getPendingFunctions().pop_back();
479 assert(!PF->hasAvailableExternallyLinkage() &&
480 "Externally-defined function should not be in pending list.");
482 jitTheFunctionUnlocked(PF);
484 // Now that the function has been jitted, ask the JITEmitter to rewrite
485 // the stub with real address of the function.
486 updateFunctionStubUnlocked(PF);
490 void JIT::jitTheFunctionUnlocked(Function *F) {
491 isAlreadyCodeGenerating = true;
492 jitstate->getPM().run(*F);
493 isAlreadyCodeGenerating = false;
495 // clear basic block addresses after this function is done
496 getBasicBlockAddressMap().clear();
499 /// getPointerToFunction - This method is used to get the address of the
500 /// specified function, compiling it if necessary.
502 void *JIT::getPointerToFunction(Function *F) {
504 if (void *Addr = getPointerToGlobalIfAvailable(F))
505 return Addr; // Check if function already code gen'd
507 MutexGuard locked(lock);
509 // Now that this thread owns the lock, make sure we read in the function if it
510 // exists in this Module.
511 std::string ErrorMsg;
512 if (F->Materialize(&ErrorMsg)) {
513 report_fatal_error("Error reading function '" + F->getName()+
514 "' from bitcode file: " + ErrorMsg);
517 // ... and check if another thread has already code gen'd the function.
518 if (void *Addr = getPointerToGlobalIfAvailable(F))
521 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
522 bool AbortOnFailure = !F->hasExternalWeakLinkage();
523 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
524 addGlobalMapping(F, Addr);
528 runJITOnFunctionUnlocked(F);
530 void *Addr = getPointerToGlobalIfAvailable(F);
531 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
535 void JIT::addPointerToBasicBlock(const BasicBlock *BB, void *Addr) {
536 MutexGuard locked(lock);
538 BasicBlockAddressMapTy::iterator I =
539 getBasicBlockAddressMap().find(BB);
540 if (I == getBasicBlockAddressMap().end()) {
541 getBasicBlockAddressMap()[BB] = Addr;
543 // ignore repeats: some BBs can be split into few MBBs?
547 void JIT::clearPointerToBasicBlock(const BasicBlock *BB) {
548 MutexGuard locked(lock);
549 getBasicBlockAddressMap().erase(BB);
552 void *JIT::getPointerToBasicBlock(BasicBlock *BB) {
553 // make sure it's function is compiled by JIT
554 (void)getPointerToFunction(BB->getParent());
556 // resolve basic block address
557 MutexGuard locked(lock);
559 BasicBlockAddressMapTy::iterator I =
560 getBasicBlockAddressMap().find(BB);
561 if (I != getBasicBlockAddressMap().end()) {
564 llvm_unreachable("JIT does not have BB address for address-of-label, was"
565 " it eliminated by optimizer?");
569 void *JIT::getPointerToNamedFunction(const std::string &Name,
570 bool AbortOnFailure){
571 if (!isSymbolSearchingDisabled()) {
572 void *ptr = JMM->getPointerToNamedFunction(Name, false);
577 /// If a LazyFunctionCreator is installed, use it to get/create the function.
578 if (LazyFunctionCreator)
579 if (void *RP = LazyFunctionCreator(Name))
582 if (AbortOnFailure) {
583 report_fatal_error("Program used external function '"+Name+
584 "' which could not be resolved!");
590 /// getOrEmitGlobalVariable - Return the address of the specified global
591 /// variable, possibly emitting it to memory if needed. This is used by the
593 void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
594 MutexGuard locked(lock);
596 void *Ptr = getPointerToGlobalIfAvailable(GV);
599 // If the global is external, just remember the address.
600 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage()) {
601 #if HAVE___DSO_HANDLE
602 if (GV->getName() == "__dso_handle")
603 return (void*)&__dso_handle;
605 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName());
607 report_fatal_error("Could not resolve external global address: "
610 addGlobalMapping(GV, Ptr);
612 // If the global hasn't been emitted to memory yet, allocate space and
613 // emit it into memory.
614 Ptr = getMemoryForGV(GV);
615 addGlobalMapping(GV, Ptr);
616 EmitGlobalVariable(GV); // Initialize the variable.
621 /// recompileAndRelinkFunction - This method is used to force a function
622 /// which has already been compiled, to be compiled again, possibly
623 /// after it has been modified. Then the entry to the old copy is overwritten
624 /// with a branch to the new copy. If there was no old copy, this acts
625 /// just like JIT::getPointerToFunction().
627 void *JIT::recompileAndRelinkFunction(Function *F) {
628 void *OldAddr = getPointerToGlobalIfAvailable(F);
630 // If it's not already compiled there is no reason to patch it up.
631 if (!OldAddr) return getPointerToFunction(F);
633 // Delete the old function mapping.
634 addGlobalMapping(F, nullptr);
636 // Recodegen the function
639 // Update state, forward the old function to the new function.
640 void *Addr = getPointerToGlobalIfAvailable(F);
641 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
642 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
646 /// getMemoryForGV - This method abstracts memory allocation of global
647 /// variable so that the JIT can allocate thread local variables depending
650 char* JIT::getMemoryForGV(const GlobalVariable* GV) {
653 // GlobalVariable's which are not "constant" will cause trouble in a server
654 // situation. It's returned in the same block of memory as code which may
656 if (isGVCompilationDisabled() && !GV->isConstant()) {
657 report_fatal_error("Compilation of non-internal GlobalValue is disabled!");
660 // Some applications require globals and code to live together, so they may
661 // be allocated into the same buffer, but in general globals are allocated
662 // through the memory manager which puts them near the code but not in the
664 Type *GlobalType = GV->getType()->getElementType();
665 size_t S = getDataLayout()->getTypeAllocSize(GlobalType);
666 size_t A = getDataLayout()->getPreferredAlignment(GV);
667 if (GV->isThreadLocal()) {
668 MutexGuard locked(lock);
669 Ptr = TJI.allocateThreadLocalMemory(S);
670 } else if (TJI.allocateSeparateGVMemory()) {
672 Ptr = (char*)malloc(S);
674 // Allocate S+A bytes of memory, then use an aligned pointer within that
676 Ptr = (char*)malloc(S+A);
677 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
678 Ptr = Ptr + (MisAligned ? (A-MisAligned) : 0);
680 } else if (AllocateGVsWithCode) {
681 Ptr = (char*)JCE->allocateSpace(S, A);
683 Ptr = (char*)JCE->allocateGlobal(S, A);
688 void JIT::addPendingFunction(Function *F) {
689 MutexGuard locked(lock);
690 jitstate->getPendingFunctions().push_back(F);
694 JITEventListener::~JITEventListener() {}