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"
41 // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
42 // of atexit). It passes the address of linker generated symbol __dso_handle
44 // This configuration change happened at version 5330.
45 # include <AvailabilityMacros.h>
46 # if defined(MAC_OS_X_VERSION_10_4) && \
47 ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
48 (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
49 __APPLE_CC__ >= 5330))
50 # ifndef HAVE___DSO_HANDLE
51 # define HAVE___DSO_HANDLE 1
57 extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
62 static struct RegisterJIT {
63 RegisterJIT() { JIT::Register(); }
68 extern "C" void LLVMLinkInJIT() {
71 /// createJIT - This is the factory method for creating a JIT for the current
72 /// machine, it does not fall back to the interpreter. This takes ownership
74 ExecutionEngine *JIT::createJIT(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(0, NULL);
84 // If the target supports JIT code generation, create the JIT.
85 if (TargetJITInfo *TJ = TM->getJITInfo()) {
86 return new JIT(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 (SmallPtrSet<JIT*, 1>::const_iterator Jit = JITs.begin(),
116 if (Function *F = (*Jit)->FindFunctionNamed(Name))
117 return (*Jit)->getPointerToFunction(F);
119 // The function is not available : fallback on the first created (will
120 // search in symbol of the current program/library)
121 return (*JITs.begin())->getPointerToNamedFunction(Name);
124 ManagedStatic<JitPool> AllJits;
127 // getPointerToNamedFunction - This function is used as a global wrapper to
128 // JIT::getPointerToNamedFunction for the purpose of resolving symbols when
129 // bugpoint is debugging the JIT. In that scenario, we are loading an .so and
130 // need to resolve function(s) that are being mis-codegenerated, so we need to
131 // resolve their addresses at runtime, and this is the way to do it.
132 void *getPointerToNamedFunction(const char *Name) {
133 return AllJits->getPointerToNamedFunction(Name);
137 JIT::JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
138 JITMemoryManager *jmm, bool GVsWithCode)
139 : ExecutionEngine(M), TM(tm), TJI(tji),
140 JMM(jmm ? jmm : JITMemoryManager::CreateDefaultMemManager()),
141 AllocateGVsWithCode(GVsWithCode), isAlreadyCodeGenerating(false) {
142 setDataLayout(TM.getDataLayout());
144 jitstate = new JITState(M);
147 JCE = createEmitter(*this, JMM, TM);
149 // Register in global list of all JITs.
153 MutexGuard locked(lock);
154 FunctionPassManager &PM = jitstate->getPM(locked);
155 M->setDataLayout(TM.getDataLayout());
156 PM.add(new DataLayoutPass(M));
158 // Turn the machine code intermediate representation into bytes in memory that
160 if (TM.addPassesToEmitMachineCode(PM, *JCE)) {
161 report_fatal_error("Target does not support machine code emission!");
164 // Initialize passes.
165 PM.doInitialization();
170 AllJits->Remove(this);
173 // JMM is a ownership of JCE, so we no need delete JMM here.
177 /// addModule - Add a new Module to the JIT. If we previously removed the last
178 /// Module, we need re-initialize jitstate with a valid Module.
179 void JIT::addModule(Module *M) {
180 MutexGuard locked(lock);
182 if (Modules.empty()) {
183 assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
185 jitstate = new JITState(M);
187 FunctionPassManager &PM = jitstate->getPM(locked);
188 M->setDataLayout(TM.getDataLayout());
189 PM.add(new DataLayoutPass(M));
191 // Turn the machine code intermediate representation into bytes in memory
192 // that may be executed.
193 if (TM.addPassesToEmitMachineCode(PM, *JCE)) {
194 report_fatal_error("Target does not support machine code emission!");
197 // Initialize passes.
198 PM.doInitialization();
201 ExecutionEngine::addModule(M);
204 /// removeModule - If we are removing the last Module, invalidate the jitstate
205 /// since the PassManager it contains references a released Module.
206 bool JIT::removeModule(Module *M) {
207 bool result = ExecutionEngine::removeModule(M);
209 MutexGuard locked(lock);
211 if (jitstate && jitstate->getModule() == M) {
216 if (!jitstate && !Modules.empty()) {
217 jitstate = new JITState(Modules[0]);
219 FunctionPassManager &PM = jitstate->getPM(locked);
220 M->setDataLayout(TM.getDataLayout());
221 PM.add(new DataLayoutPass(M));
223 // Turn the machine code intermediate representation into bytes in memory
224 // that may be executed.
225 if (TM.addPassesToEmitMachineCode(PM, *JCE)) {
226 report_fatal_error("Target does not support machine code emission!");
229 // Initialize passes.
230 PM.doInitialization();
235 /// run - Start execution with the specified function and arguments.
237 GenericValue JIT::runFunction(Function *F,
238 const std::vector<GenericValue> &ArgValues) {
239 assert(F && "Function *F was null at entry to run()");
241 void *FPtr = getPointerToFunction(F);
242 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
243 FunctionType *FTy = F->getFunctionType();
244 Type *RetTy = FTy->getReturnType();
246 assert((FTy->getNumParams() == ArgValues.size() ||
247 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
248 "Wrong number of arguments passed into function!");
249 assert(FTy->getNumParams() == ArgValues.size() &&
250 "This doesn't support passing arguments through varargs (yet)!");
252 // Handle some common cases first. These cases correspond to common `main'
254 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
255 switch (ArgValues.size()) {
257 if (FTy->getParamType(0)->isIntegerTy(32) &&
258 FTy->getParamType(1)->isPointerTy() &&
259 FTy->getParamType(2)->isPointerTy()) {
260 int (*PF)(int, char **, const char **) =
261 (int(*)(int, char **, const char **))(intptr_t)FPtr;
263 // Call the function.
265 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
266 (char **)GVTOP(ArgValues[1]),
267 (const char **)GVTOP(ArgValues[2])));
272 if (FTy->getParamType(0)->isIntegerTy(32) &&
273 FTy->getParamType(1)->isPointerTy()) {
274 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
276 // Call the function.
278 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
279 (char **)GVTOP(ArgValues[1])));
284 if (FTy->getParamType(0)->isIntegerTy(32)) {
286 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
287 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
290 if (FTy->getParamType(0)->isPointerTy()) {
292 int (*PF)(char *) = (int(*)(char *))(intptr_t)FPtr;
293 rv.IntVal = APInt(32, PF((char*)GVTOP(ArgValues[0])));
300 // Handle cases where no arguments are passed first.
301 if (ArgValues.empty()) {
303 switch (RetTy->getTypeID()) {
304 default: llvm_unreachable("Unknown return type for function call!");
305 case Type::IntegerTyID: {
306 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
308 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
309 else if (BitWidth <= 8)
310 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
311 else if (BitWidth <= 16)
312 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
313 else if (BitWidth <= 32)
314 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
315 else if (BitWidth <= 64)
316 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
318 llvm_unreachable("Integer types > 64 bits not supported");
322 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
324 case Type::FloatTyID:
325 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
327 case Type::DoubleTyID:
328 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
330 case Type::X86_FP80TyID:
331 case Type::FP128TyID:
332 case Type::PPC_FP128TyID:
333 llvm_unreachable("long double not supported yet");
334 case Type::PointerTyID:
335 return PTOGV(((void*(*)())(intptr_t)FPtr)());
339 // Okay, this is not one of our quick and easy cases. Because we don't have a
340 // full FFI, we have to codegen a nullary stub function that just calls the
341 // function we are interested in, passing in constants for all of the
342 // arguments. Make this function and return.
344 // First, create the function.
345 FunctionType *STy=FunctionType::get(RetTy, false);
346 Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
349 // Insert a basic block.
350 BasicBlock *StubBB = BasicBlock::Create(F->getContext(), "", Stub);
352 // Convert all of the GenericValue arguments over to constants. Note that we
353 // currently don't support varargs.
354 SmallVector<Value*, 8> Args;
355 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
357 Type *ArgTy = FTy->getParamType(i);
358 const GenericValue &AV = ArgValues[i];
359 switch (ArgTy->getTypeID()) {
360 default: llvm_unreachable("Unknown argument type for function call!");
361 case Type::IntegerTyID:
362 C = ConstantInt::get(F->getContext(), AV.IntVal);
364 case Type::FloatTyID:
365 C = ConstantFP::get(F->getContext(), APFloat(AV.FloatVal));
367 case Type::DoubleTyID:
368 C = ConstantFP::get(F->getContext(), APFloat(AV.DoubleVal));
370 case Type::PPC_FP128TyID:
371 case Type::X86_FP80TyID:
372 case Type::FP128TyID:
373 C = ConstantFP::get(F->getContext(), APFloat(ArgTy->getFltSemantics(),
376 case Type::PointerTyID:
377 void *ArgPtr = GVTOP(AV);
378 if (sizeof(void*) == 4)
379 C = ConstantInt::get(Type::getInt32Ty(F->getContext()),
380 (int)(intptr_t)ArgPtr);
382 C = ConstantInt::get(Type::getInt64Ty(F->getContext()),
384 // Cast the integer to pointer
385 C = ConstantExpr::getIntToPtr(C, ArgTy);
391 CallInst *TheCall = CallInst::Create(F, Args, "", StubBB);
392 TheCall->setCallingConv(F->getCallingConv());
393 TheCall->setTailCall();
394 if (!TheCall->getType()->isVoidTy())
395 // Return result of the call.
396 ReturnInst::Create(F->getContext(), TheCall, StubBB);
398 ReturnInst::Create(F->getContext(), StubBB); // Just return void.
400 // Finally, call our nullary stub function.
401 GenericValue Result = runFunction(Stub, std::vector<GenericValue>());
402 // Erase it, since no other function can have a reference to it.
403 Stub->eraseFromParent();
404 // And return the result.
408 void JIT::RegisterJITEventListener(JITEventListener *L) {
411 MutexGuard locked(lock);
412 EventListeners.push_back(L);
414 void JIT::UnregisterJITEventListener(JITEventListener *L) {
417 MutexGuard locked(lock);
418 std::vector<JITEventListener*>::reverse_iterator I=
419 std::find(EventListeners.rbegin(), EventListeners.rend(), L);
420 if (I != EventListeners.rend()) {
421 std::swap(*I, EventListeners.back());
422 EventListeners.pop_back();
425 void JIT::NotifyFunctionEmitted(
427 void *Code, size_t Size,
428 const JITEvent_EmittedFunctionDetails &Details) {
429 MutexGuard locked(lock);
430 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
431 EventListeners[I]->NotifyFunctionEmitted(F, Code, Size, Details);
435 void JIT::NotifyFreeingMachineCode(void *OldPtr) {
436 MutexGuard locked(lock);
437 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
438 EventListeners[I]->NotifyFreeingMachineCode(OldPtr);
442 /// runJITOnFunction - Run the FunctionPassManager full of
443 /// just-in-time compilation passes on F, hopefully filling in
444 /// GlobalAddress[F] with the address of F's machine code.
446 void JIT::runJITOnFunction(Function *F, MachineCodeInfo *MCI) {
447 MutexGuard locked(lock);
449 class MCIListener : public JITEventListener {
450 MachineCodeInfo *const MCI;
452 MCIListener(MachineCodeInfo *mci) : MCI(mci) {}
453 void NotifyFunctionEmitted(const Function &, void *Code, size_t Size,
454 const EmittedFunctionDetails &) override {
455 MCI->setAddress(Code);
459 MCIListener MCIL(MCI);
461 RegisterJITEventListener(&MCIL);
463 runJITOnFunctionUnlocked(F, locked);
466 UnregisterJITEventListener(&MCIL);
469 void JIT::runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked) {
470 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
472 jitTheFunction(F, locked);
474 // If the function referred to another function that had not yet been
475 // read from bitcode, and we are jitting non-lazily, emit it now.
476 while (!jitstate->getPendingFunctions(locked).empty()) {
477 Function *PF = jitstate->getPendingFunctions(locked).back();
478 jitstate->getPendingFunctions(locked).pop_back();
480 assert(!PF->hasAvailableExternallyLinkage() &&
481 "Externally-defined function should not be in pending list.");
483 jitTheFunction(PF, locked);
485 // Now that the function has been jitted, ask the JITEmitter to rewrite
486 // the stub with real address of the function.
487 updateFunctionStub(PF);
491 void JIT::jitTheFunction(Function *F, const MutexGuard &locked) {
492 isAlreadyCodeGenerating = true;
493 jitstate->getPM(locked).run(*F);
494 isAlreadyCodeGenerating = false;
496 // clear basic block addresses after this function is done
497 getBasicBlockAddressMap(locked).clear();
500 /// getPointerToFunction - This method is used to get the address of the
501 /// specified function, compiling it if necessary.
503 void *JIT::getPointerToFunction(Function *F) {
505 if (void *Addr = getPointerToGlobalIfAvailable(F))
506 return Addr; // Check if function already code gen'd
508 MutexGuard locked(lock);
510 // Now that this thread owns the lock, make sure we read in the function if it
511 // exists in this Module.
512 std::string ErrorMsg;
513 if (F->Materialize(&ErrorMsg)) {
514 report_fatal_error("Error reading function '" + F->getName()+
515 "' from bitcode file: " + ErrorMsg);
518 // ... and check if another thread has already code gen'd the function.
519 if (void *Addr = getPointerToGlobalIfAvailable(F))
522 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
523 bool AbortOnFailure = !F->hasExternalWeakLinkage();
524 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
525 addGlobalMapping(F, Addr);
529 runJITOnFunctionUnlocked(F, locked);
531 void *Addr = getPointerToGlobalIfAvailable(F);
532 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
536 void JIT::addPointerToBasicBlock(const BasicBlock *BB, void *Addr) {
537 MutexGuard locked(lock);
539 BasicBlockAddressMapTy::iterator I =
540 getBasicBlockAddressMap(locked).find(BB);
541 if (I == getBasicBlockAddressMap(locked).end()) {
542 getBasicBlockAddressMap(locked)[BB] = Addr;
544 // ignore repeats: some BBs can be split into few MBBs?
548 void JIT::clearPointerToBasicBlock(const BasicBlock *BB) {
549 MutexGuard locked(lock);
550 getBasicBlockAddressMap(locked).erase(BB);
553 void *JIT::getPointerToBasicBlock(BasicBlock *BB) {
554 // make sure it's function is compiled by JIT
555 (void)getPointerToFunction(BB->getParent());
557 // resolve basic block address
558 MutexGuard locked(lock);
560 BasicBlockAddressMapTy::iterator I =
561 getBasicBlockAddressMap(locked).find(BB);
562 if (I != getBasicBlockAddressMap(locked).end()) {
565 llvm_unreachable("JIT does not have BB address for address-of-label, was"
566 " it eliminated by optimizer?");
570 void *JIT::getPointerToNamedFunction(const std::string &Name,
571 bool AbortOnFailure){
572 if (!isSymbolSearchingDisabled()) {
573 void *ptr = JMM->getPointerToNamedFunction(Name, false);
578 /// If a LazyFunctionCreator is installed, use it to get/create the function.
579 if (LazyFunctionCreator)
580 if (void *RP = LazyFunctionCreator(Name))
583 if (AbortOnFailure) {
584 report_fatal_error("Program used external function '"+Name+
585 "' which could not be resolved!");
591 /// getOrEmitGlobalVariable - Return the address of the specified global
592 /// variable, possibly emitting it to memory if needed. This is used by the
594 void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
595 MutexGuard locked(lock);
597 void *Ptr = getPointerToGlobalIfAvailable(GV);
600 // If the global is external, just remember the address.
601 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage()) {
602 #if HAVE___DSO_HANDLE
603 if (GV->getName() == "__dso_handle")
604 return (void*)&__dso_handle;
606 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName());
608 report_fatal_error("Could not resolve external global address: "
611 addGlobalMapping(GV, Ptr);
613 // If the global hasn't been emitted to memory yet, allocate space and
614 // emit it into memory.
615 Ptr = getMemoryForGV(GV);
616 addGlobalMapping(GV, Ptr);
617 EmitGlobalVariable(GV); // Initialize the variable.
622 /// recompileAndRelinkFunction - This method is used to force a function
623 /// which has already been compiled, to be compiled again, possibly
624 /// after it has been modified. Then the entry to the old copy is overwritten
625 /// with a branch to the new copy. If there was no old copy, this acts
626 /// just like JIT::getPointerToFunction().
628 void *JIT::recompileAndRelinkFunction(Function *F) {
629 void *OldAddr = getPointerToGlobalIfAvailable(F);
631 // If it's not already compiled there is no reason to patch it up.
632 if (OldAddr == 0) { return getPointerToFunction(F); }
634 // Delete the old function mapping.
635 addGlobalMapping(F, 0);
637 // Recodegen the function
640 // Update state, forward the old function to the new function.
641 void *Addr = getPointerToGlobalIfAvailable(F);
642 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
643 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
647 /// getMemoryForGV - This method abstracts memory allocation of global
648 /// variable so that the JIT can allocate thread local variables depending
651 char* JIT::getMemoryForGV(const GlobalVariable* GV) {
654 // GlobalVariable's which are not "constant" will cause trouble in a server
655 // situation. It's returned in the same block of memory as code which may
657 if (isGVCompilationDisabled() && !GV->isConstant()) {
658 report_fatal_error("Compilation of non-internal GlobalValue is disabled!");
661 // Some applications require globals and code to live together, so they may
662 // be allocated into the same buffer, but in general globals are allocated
663 // through the memory manager which puts them near the code but not in the
665 Type *GlobalType = GV->getType()->getElementType();
666 size_t S = getDataLayout()->getTypeAllocSize(GlobalType);
667 size_t A = getDataLayout()->getPreferredAlignment(GV);
668 if (GV->isThreadLocal()) {
669 MutexGuard locked(lock);
670 Ptr = TJI.allocateThreadLocalMemory(S);
671 } else if (TJI.allocateSeparateGVMemory()) {
673 Ptr = (char*)malloc(S);
675 // Allocate S+A bytes of memory, then use an aligned pointer within that
677 Ptr = (char*)malloc(S+A);
678 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
679 Ptr = Ptr + (MisAligned ? (A-MisAligned) : 0);
681 } else if (AllocateGVsWithCode) {
682 Ptr = (char*)JCE->allocateSpace(S, A);
684 Ptr = (char*)JCE->allocateGlobal(S, A);
689 void JIT::addPendingFunction(Function *F) {
690 MutexGuard locked(lock);
691 jitstate->getPendingFunctions(locked).push_back(F);
695 JITEventListener::~JITEventListener() {}