19 #include "llvm/Config/config.h"
44 # include <AvailabilityMacros.h>
45 # if defined(MAC_OS_X_VERSION_10_4) && \
46 ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
47 (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
48 __APPLE_CC__ >= 5330))
49 # ifndef HAVE___DSO_HANDLE
50 # define HAVE___DSO_HANDLE 1
56 extern void *__dso_handle
__attribute__ ((__visibility__ (
"hidden")));
61 static struct RegisterJIT {
74 std::string *ErrorStr,
85 return new JIT(M, *TM, *TJ, JMM, GVsWithCode);
88 *ErrorStr =
"target does not support JIT code generation";
104 void Remove(
JIT *jit) {
110 assert(JITs.size() != 0 &&
"No Jit registered");
115 if (
Function *
F = (*Jit)->FindFunctionNamed(Name))
116 return (*Jit)->getPointerToFunction(
F);
132 return AllJits->getPointerToNamedFunction(Name);
140 AllocateGVsWithCode(GVsWithCode), isAlreadyCodeGenerating(
false) {
141 setDataLayout(
TM.getDataLayout());
146 JCE = createEmitter(*
this, JMM,
TM);
158 if (
TM.addPassesToEmitMachineCode(PM, *JCE)) {
163 PM.doInitialization();
168 AllJits->Remove(
this);
181 assert(!jitstate &&
"jitstate should be NULL if Modules vector is empty!");
195 PM.doInitialization();
208 if (jitstate && jitstate->getModule() == M) {
213 if (!jitstate && !
Modules.empty()) {
226 PM.doInitialization();
234 const std::vector<GenericValue> &ArgValues) {
235 assert(F &&
"Function *F was null at entry to run()");
238 assert(FPtr &&
"Pointer to fn's code was null after getPointerToFunction");
244 "Wrong number of arguments passed into function!");
246 "This doesn't support passing arguments through varargs (yet)!");
251 switch (ArgValues.size()) {
256 int (*PF)(int,
char **,
const char **) =
257 (
int(*)(int,
char **,
const char **))(
intptr_t)FPtr;
262 (
char **)
GVTOP(ArgValues[1]),
263 (
const char **)
GVTOP(ArgValues[2])));
270 int (*PF)(int,
char **) = (
int(*)(int,
char **))(
intptr_t)FPtr;
275 (
char **)
GVTOP(ArgValues[1])));
282 int (*PF)(int) = (
int(*)(int))(
intptr_t)FPtr;
288 int (*PF)(
char *) = (
int(*)(
char *))(
intptr_t)FPtr;
297 if (ArgValues.empty()) {
302 unsigned BitWidth = cast<IntegerType>(RetTy)->
getBitWidth();
305 else if (BitWidth <= 8)
307 else if (BitWidth <= 16)
309 else if (BitWidth <= 32)
311 else if (BitWidth <= 64)
351 for (
unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
373 void *ArgPtr =
GVTOP(AV);
374 if (
sizeof(
void*) == 4)
408 EventListeners.push_back(L);
415 std::find(EventListeners.rbegin(), EventListeners.rend(), L);
416 if (I != EventListeners.rend()) {
418 EventListeners.pop_back();
423 void *
Code,
size_t Size,
426 for (
unsigned I = 0, S = EventListeners.size();
I < S; ++
I) {
427 EventListeners[
I]->NotifyFunctionEmitted(F, Code, Size, Details);
433 for (
unsigned I = 0, S = EventListeners.size();
I < S; ++
I) {
434 EventListeners[
I]->NotifyFreeingMachineCode(OldPtr);
450 void *
Code,
size_t Size,
451 const EmittedFunctionDetails &) {
456 MCIListener MCIL(MCI);
460 runJITOnFunctionUnlocked(F, locked);
467 assert(!isAlreadyCodeGenerating &&
"Error: Recursive compilation detected!");
469 jitTheFunction(F, locked);
473 while (!jitstate->getPendingFunctions(locked).empty()) {
474 Function *PF = jitstate->getPendingFunctions(locked).
back();
475 jitstate->getPendingFunctions(locked).pop_back();
478 "Externally-defined function should not be in pending list.");
480 jitTheFunction(PF, locked);
484 updateFunctionStub(PF);
489 isAlreadyCodeGenerating =
true;
490 jitstate->getPM(locked).run(*F);
491 isAlreadyCodeGenerating =
false;
509 std::string ErrorMsg;
512 "' from bitcode file: " + ErrorMsg);
526 runJITOnFunctionUnlocked(F, locked);
529 assert(Addr &&
"Code generation didn't add function to GlobalAddress table!");
563 " it eliminated by optimizer?");
568 bool AbortOnFailure){
580 if (AbortOnFailure) {
582 "' which could not be resolved!");
599 #if HAVE___DSO_HANDLE
600 if (GV->
getName() ==
"__dso_handle")
601 return (
void*)&__dso_handle;
639 assert(Addr &&
"Code generation didn't add function to GlobalAddress table!");
675 unsigned MisAligned = ((
intptr_t)Ptr & (A-1));
676 Ptr = Ptr + (MisAligned ? (A-MisAligned) : 0);
678 }
else if (AllocateGVsWithCode) {
688 jitstate->getPendingFunctions(locked).push_back(F);
const_iterator end(StringRef path)
Get end iterator over path.
static void * SearchForAddressOfSymbol(const char *symbolName)
Search through libraries for address of a symbol.
void NotifyFunctionEmitted(const Function &F, void *Code, size_t Size, const JITEvent_EmittedFunctionDetails &Details)
LLVMContext & getContext() const
virtual void replaceMachineCodeForFunction(void *Old, void *New)=0
virtual void UnregisterJITEventListener(JITEventListener *L)
void * getPointerToFunction(Function *F)
BasicBlockAddressMapTy & getBasicBlockAddressMap(const MutexGuard &)
The main container class for the LLVM Intermediate Representation.
unsigned getNumParams() const
virtual bool removeModule(Module *M)
2: 32-bit floating point type
void addPointerToBasicBlock(const BasicBlock *BB, void *Addr)
addPointerToBasicBlock - Adds address of the specific basic block.
static ExecutionEngine * createJIT(Module *M, std::string *ErrorStr, JITMemoryManager *JMM, bool GVsWithCode, TargetMachine *TM)
bool hasAvailableExternallyLinkage() const
void EmitGlobalVariable(const GlobalVariable *GV)
const Function * getParent() const
Return the enclosing method, or null if none.
virtual ~JITEventListener()
4: 80-bit floating point type (X87)
virtual void * allocateSpace(uintptr_t Size, unsigned Alignment)
static IntegerType * getInt64Ty(LLVMContext &C)
virtual bool addPassesToEmitMachineCode(PassManagerBase &, JITCodeEmitter &, bool=true)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
CallingConv::ID getCallingConv() const
StringRef getName() const
void setCallingConv(CallingConv::ID CC)
static unsigned getBitWidth(Type *Ty, const DataLayout *TD)
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
#define llvm_unreachable(msg)
void *(* LazyFunctionCreator)(const std::string &)
const BasicBlock & back() const
iterator find(const KeyT &Val)
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
void runJITOnFunction(Function *F, MachineCodeInfo *MCI=0)
static Constant * getIntToPtr(Constant *C, Type *Ty)
Type * getElementType() const
10: Arbitrary bit width integers
Guard a section of code with a Mutex.
Type * getParamType(unsigned i) const
Parameter type accessors.
friend const_iterator end(StringRef path)
Get end iterator over path.
bool isGVCompilationDisabled() const
LLVM Basic Block Representation.
LLVM Constant Representation.
static ManagedStatic< sys::SmartRWMutex< true > > Lock
bool isSymbolSearchingDisabled() const
static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=0)
void * getOrEmitGlobalVariable(const GlobalVariable *GV)
6: 128-bit floating point type (two 64-bits, PowerPC)
void * getPointerToGlobalIfAvailable(const GlobalValue *GV)
void setTailCall(bool isTC=true)
void addPendingFunction(Function *F)
void clearPointerToBasicBlock(const BasicBlock *BB)
clearPointerToBasicBlock - Removes address of specific basic block.
virtual void addModule(Module *M)
virtual TargetJITInfo * getJITInfo()
unsigned getPreferredAlignment(const GlobalVariable *GV) const
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
void * GVTOP(const GenericValue &GV)
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=0)
SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
bool hasExternalWeakLinkage() const
uint64_t getTypeAllocSize(Type *Ty) const
virtual bool allocateSeparateGVMemory() const
void * getPointerToBasicBlock(BasicBlock *BB)
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
void * recompileAndRelinkFunction(Function *F)
static Constant * get(Type *Ty, double V)
const fltSemantics & getFltSemantics() const
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
GenericValue PTOGV(void *P)
Class for arbitrary precision integers.
void NotifyFreeingMachineCode(void *OldPtr)
PointerType * getType() const
getType - Global values are always pointers.
virtual void * allocateGlobal(uintptr_t Size, unsigned Alignment)=0
void addGlobalMapping(const GlobalValue *GV, void *Addr)
void *malloc(size_t size);
static IntegerType * getInt32Ty(LLVMContext &C)
bool isDeclaration() const
const DataLayout * getDataLayout() const
std::reverse_iterator< const_iterator > reverse_iterator
virtual const DataLayout * getDataLayout() const
FunctionType * getFunctionType() const
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=0, BasicBlock *InsertBefore=0)
Creates a new BasicBlock.
SmallVector< Module *, 1 > Modules
Rename collisions when linking (static functions).
virtual void addModule(Module *M)
virtual void eraseFromParent()
static ReturnInst * Create(LLVMContext &C, Value *retVal=0, Instruction *InsertBefore=0)
virtual char * allocateThreadLocalMemory(size_t size)
3: 64-bit floating point type
Type * getReturnType() const
void *__dso_handle __attribute__((__visibility__("hidden")))
virtual void * getPointerToNamedFunction(const std::string &Name, bool AbortOnFailure=true)
virtual void * getPointerToNamedFunction(const std::string &Name, bool AbortOnFailure=true)
virtual char * getMemoryForGV(const GlobalVariable *GV)
getMemoryforGV - Allocate memory for a global variable.
bool Materialize(std::string *ErrInfo=0)
void * getPointerToNamedFunction(const char *Name)
virtual bool removeModule(Module *M)
virtual void RegisterJITEventListener(JITEventListener *L)
bool erase(const KeyT &Val)
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=0)
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.
5: 128-bit floating point type (112-bit mantissa)
virtual GenericValue runFunction(Function *F, const std::vector< GenericValue > &ArgValues)