blob: 83ec9784b98ebc869b09db45408b7661ed98afde [file] [log] [blame]
Chris Lattner4d326fa2003-12-20 01:46:27 +00001//===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerbd199fb2002-12-24 00:01:05 +00009//
Chris Lattner4d326fa2003-12-20 01:46:27 +000010// This tool implements a just-in-time compiler for LLVM, allowing direct
Gabor Greifa99be512007-07-05 17:07:56 +000011// execution of LLVM bitcode in an efficient manner.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner4d326fa2003-12-20 01:46:27 +000015#include "JIT.h"
Jeffrey Yasskin40966a72010-02-11 01:07:39 +000016#include "llvm/ADT/SmallPtrSet.h"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000017#include "llvm/CodeGen/JITCodeEmitter.h"
Argyrios Kyrtzidisb3a847d2009-05-18 21:06:40 +000018#include "llvm/CodeGen/MachineCodeInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/Config/config.h"
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +000020#include "llvm/ExecutionEngine/GenericValue.h"
21#include "llvm/ExecutionEngine/JITEventListener.h"
Danil Malyshev30b9e322012-03-28 21:46:36 +000022#include "llvm/ExecutionEngine/JITMemoryManager.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000023#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"
Stephen Hines36b56882014-04-23 16:57:46 -070029#include "llvm/IR/Module.h"
Chris Lattner44e3dd12009-03-03 20:10:23 +000030#include "llvm/Support/Dwarf.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/Support/DynamicLibrary.h"
Torok Edwin31e24662009-07-07 17:32:34 +000032#include "llvm/Support/ErrorHandling.h"
Jeffrey Yasskin40966a72010-02-11 01:07:39 +000033#include "llvm/Support/ManagedStatic.h"
Chris Lattner44e3dd12009-03-03 20:10:23 +000034#include "llvm/Support/MutexGuard.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000035#include "llvm/Target/TargetJITInfo.h"
36#include "llvm/Target/TargetMachine.h"
Anton Korobeynikov3b30a6e2007-07-30 20:02:02 +000037
Chris Lattnerc19aade2003-12-08 08:06:28 +000038using namespace llvm;
Misha Brukmanabb027c2003-05-27 21:40:39 +000039
Jim Grosbach4f9fc852011-03-15 20:25:54 +000040#ifdef __APPLE__
Anton Korobeynikov3b30a6e2007-07-30 20:02:02 +000041// 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
43// to the function.
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
52# endif
53# endif
Evan Cheng5f42c552006-07-21 23:06:20 +000054#endif
Anton Korobeynikov3b30a6e2007-07-30 20:02:02 +000055
56#if HAVE___DSO_HANDLE
57extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
Nate Begemanb76ea742006-07-22 16:59:38 +000058#endif
Evan Cheng5f42c552006-07-21 23:06:20 +000059
Dan Gohman844731a2008-05-13 00:00:25 +000060namespace {
61
Chris Lattner2fe4bb02006-03-22 06:07:50 +000062static struct RegisterJIT {
63 RegisterJIT() { JIT::Register(); }
64} JITRegistrator;
65
Dan Gohman844731a2008-05-13 00:00:25 +000066}
67
Bob Wilsone46161f2009-06-24 21:09:18 +000068extern "C" void LLVMLinkInJIT() {
Jeff Cohen2f519142006-03-24 02:53:49 +000069}
70
Chris Lattner34c94332007-12-06 01:34:04 +000071/// 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
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000073/// of the module.
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000074ExecutionEngine *JIT::createJIT(Module *M,
Reid Kleckner4b1511b2009-07-18 00:42:18 +000075 std::string *ErrorStr,
76 JITMemoryManager *JMM,
Eric Christopher88b5aca2009-11-17 21:58:16 +000077 bool GVsWithCode,
Dylan Noblesmithc5b28582011-05-13 21:51:29 +000078 TargetMachine *TM) {
Nick Lewycky83a82ac2010-08-17 16:19:18 +000079 // Try to register the program as a source of symbols to resolve against.
Dylan Noblesmith2ea29ba2011-05-13 21:36:16 +000080 //
81 // FIXME: Don't do this here.
Stephen Hinesdce4a402014-05-29 02:49:00 -070082 sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
Nick Lewycky83a82ac2010-08-17 16:19:18 +000083
Dylan Noblesmith2ea29ba2011-05-13 21:36:16 +000084 // If the target supports JIT code generation, create the JIT.
Reid Kleckner4b1511b2009-07-18 00:42:18 +000085 if (TargetJITInfo *TJ = TM->getJITInfo()) {
Dylan Noblesmith9ea47172011-12-12 04:20:36 +000086 return new JIT(M, *TM, *TJ, JMM, GVsWithCode);
Reid Kleckner4b1511b2009-07-18 00:42:18 +000087 } else {
88 if (ErrorStr)
89 *ErrorStr = "target does not support JIT code generation";
Stephen Hinesdce4a402014-05-29 02:49:00 -070090 return nullptr;
Reid Kleckner4b1511b2009-07-18 00:42:18 +000091 }
Chris Lattner34c94332007-12-06 01:34:04 +000092}
93
Jeffrey Yasskin40966a72010-02-11 01:07:39 +000094namespace {
95/// This class supports the global getPointerToNamedFunction(), which allows
96/// bugpoint or gdb users to search for a function by name without any context.
97class JitPool {
98 SmallPtrSet<JIT*, 1> JITs; // Optimize for process containing just 1 JIT.
99 mutable sys::Mutex Lock;
100public:
101 void Add(JIT *jit) {
102 MutexGuard guard(Lock);
103 JITs.insert(jit);
104 }
105 void Remove(JIT *jit) {
106 MutexGuard guard(Lock);
107 JITs.erase(jit);
108 }
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(),
114 end = JITs.end();
115 Jit != end; ++Jit) {
116 if (Function *F = (*Jit)->FindFunctionNamed(Name))
117 return (*Jit)->getPointerToFunction(F);
118 }
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);
122 }
123};
124ManagedStatic<JitPool> AllJits;
125}
126extern "C" {
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);
134 }
135}
136
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000137JIT::JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
Danil Malyshev30b9e322012-03-28 21:46:36 +0000138 JITMemoryManager *jmm, bool GVsWithCode)
139 : ExecutionEngine(M), TM(tm), TJI(tji),
140 JMM(jmm ? jmm : JITMemoryManager::CreateDefaultMemManager()),
141 AllocateGVsWithCode(GVsWithCode), isAlreadyCodeGenerating(false) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000142 setDataLayout(TM.getDataLayout());
Misha Brukmanabb027c2003-05-27 21:40:39 +0000143
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000144 jitstate = new JITState(M);
Nate Begemanf049e072008-05-21 16:34:48 +0000145
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000146 // Initialize JCE
Reid Kleckner27632172009-09-20 23:52:43 +0000147 JCE = createEmitter(*this, JMM, TM);
Misha Brukmanf976c852005-04-21 22:55:34 +0000148
Jeffrey Yasskin40966a72010-02-11 01:07:39 +0000149 // Register in global list of all JITs.
150 AllJits->Add(this);
151
Brian Gaeke50872d52004-04-14 17:45:52 +0000152 // Add target data
Reid Spenceree448632005-07-12 15:51:55 +0000153 MutexGuard locked(lock);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700154 FunctionPassManager &PM = jitstate->getPM();
Stephen Hines36b56882014-04-23 16:57:46 -0700155 M->setDataLayout(TM.getDataLayout());
156 PM.add(new DataLayoutPass(M));
Brian Gaeke50872d52004-04-14 17:45:52 +0000157
Chris Lattner4d326fa2003-12-20 01:46:27 +0000158 // Turn the machine code intermediate representation into bytes in memory that
159 // may be executed.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700160 if (TM.addPassesToEmitMachineCode(PM, *JCE, !getVerifyModules())) {
Chris Lattner75361b62010-04-07 22:58:41 +0000161 report_fatal_error("Target does not support machine code emission!");
Chris Lattner4d326fa2003-12-20 01:46:27 +0000162 }
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000163
Chris Lattner1911fd42006-09-04 04:14:57 +0000164 // Initialize passes.
165 PM.doInitialization();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000166}
167
Chris Lattner4d326fa2003-12-20 01:46:27 +0000168JIT::~JIT() {
Duncan Sandsb35fd442010-10-21 08:57:29 +0000169 // Cleanup.
Jeffrey Yasskin40966a72010-02-11 01:07:39 +0000170 AllJits->Remove(this);
Nate Begemanf049e072008-05-21 16:34:48 +0000171 delete jitstate;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000172 delete JCE;
Danil Malyshev30b9e322012-03-28 21:46:36 +0000173 // JMM is a ownership of JCE, so we no need delete JMM here.
Chris Lattner4d326fa2003-12-20 01:46:27 +0000174 delete &TM;
175}
176
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000177/// 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.
179void JIT::addModule(Module *M) {
Nate Begemanf049e072008-05-21 16:34:48 +0000180 MutexGuard locked(lock);
181
182 if (Modules.empty()) {
183 assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
184
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000185 jitstate = new JITState(M);
Nate Begemanf049e072008-05-21 16:34:48 +0000186
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700187 FunctionPassManager &PM = jitstate->getPM();
Stephen Hines36b56882014-04-23 16:57:46 -0700188 M->setDataLayout(TM.getDataLayout());
189 PM.add(new DataLayoutPass(M));
Nate Begemanf049e072008-05-21 16:34:48 +0000190
191 // Turn the machine code intermediate representation into bytes in memory
192 // that may be executed.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700193 if (TM.addPassesToEmitMachineCode(PM, *JCE, !getVerifyModules())) {
Chris Lattner75361b62010-04-07 22:58:41 +0000194 report_fatal_error("Target does not support machine code emission!");
Nate Begemanf049e072008-05-21 16:34:48 +0000195 }
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000196
Nate Begemanf049e072008-05-21 16:34:48 +0000197 // Initialize passes.
198 PM.doInitialization();
199 }
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000200
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000201 ExecutionEngine::addModule(M);
Nate Begemanf049e072008-05-21 16:34:48 +0000202}
203
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000204/// removeModule - If we are removing the last Module, invalidate the jitstate
205/// since the PassManager it contains references a released Module.
206bool JIT::removeModule(Module *M) {
207 bool result = ExecutionEngine::removeModule(M);
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000208
Nate Begemanf049e072008-05-21 16:34:48 +0000209 MutexGuard locked(lock);
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000210
Chad Rosier9402be92012-07-25 19:06:29 +0000211 if (jitstate && jitstate->getModule() == M) {
Nate Begemanf049e072008-05-21 16:34:48 +0000212 delete jitstate;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700213 jitstate = nullptr;
Nate Begemanf049e072008-05-21 16:34:48 +0000214 }
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000215
Nate Begemand6b7a242009-02-18 08:31:02 +0000216 if (!jitstate && !Modules.empty()) {
217 jitstate = new JITState(Modules[0]);
218
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700219 FunctionPassManager &PM = jitstate->getPM();
Stephen Hines36b56882014-04-23 16:57:46 -0700220 M->setDataLayout(TM.getDataLayout());
221 PM.add(new DataLayoutPass(M));
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000222
Nate Begemand6b7a242009-02-18 08:31:02 +0000223 // Turn the machine code intermediate representation into bytes in memory
224 // that may be executed.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700225 if (TM.addPassesToEmitMachineCode(PM, *JCE, !getVerifyModules())) {
Chris Lattner75361b62010-04-07 22:58:41 +0000226 report_fatal_error("Target does not support machine code emission!");
Nate Begemand6b7a242009-02-18 08:31:02 +0000227 }
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000228
Nate Begemand6b7a242009-02-18 08:31:02 +0000229 // Initialize passes.
230 PM.doInitialization();
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000231 }
Nate Begemanf049e072008-05-21 16:34:48 +0000232 return result;
233}
234
Brian Gaeke70975ee2003-09-05 18:42:01 +0000235/// run - Start execution with the specified function and arguments.
Chris Lattner05a1a302003-08-21 21:32:12 +0000236///
Chris Lattnerff0f1bb2003-12-26 06:13:47 +0000237GenericValue JIT::runFunction(Function *F,
238 const std::vector<GenericValue> &ArgValues) {
Chris Lattnerb47130c2004-08-15 23:29:50 +0000239 assert(F && "Function *F was null at entry to run()");
Chris Lattnerb47130c2004-08-15 23:29:50 +0000240
241 void *FPtr = getPointerToFunction(F);
Chris Lattner7c45d782004-08-15 23:31:43 +0000242 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000243 FunctionType *FTy = F->getFunctionType();
244 Type *RetTy = FTy->getReturnType();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000245
Dan Gohman07ab52b2009-02-19 02:55:18 +0000246 assert((FTy->getNumParams() == ArgValues.size() ||
247 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
248 "Wrong number of arguments passed into function!");
Chris Lattnere5eab142004-08-15 23:53:06 +0000249 assert(FTy->getNumParams() == ArgValues.size() &&
250 "This doesn't support passing arguments through varargs (yet)!");
251
Misha Brukmanec843022004-10-22 23:35:57 +0000252 // Handle some common cases first. These cases correspond to common `main'
Chris Lattnere5eab142004-08-15 23:53:06 +0000253 // prototypes.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000254 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000255 switch (ArgValues.size()) {
256 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000257 if (FTy->getParamType(0)->isIntegerTy(32) &&
Duncan Sands1df98592010-02-16 11:11:14 +0000258 FTy->getParamType(1)->isPointerTy() &&
259 FTy->getParamType(2)->isPointerTy()) {
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000260 int (*PF)(int, char **, const char **) =
Chris Lattner870286a2006-06-01 17:29:22 +0000261 (int(*)(int, char **, const char **))(intptr_t)FPtr;
Misha Brukmanec843022004-10-22 23:35:57 +0000262
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000263 // Call the function.
Chris Lattnere5eab142004-08-15 23:53:06 +0000264 GenericValue rv;
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000265 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
Reid Spencer38f6a152007-03-06 03:11:31 +0000266 (char **)GVTOP(ArgValues[1]),
267 (const char **)GVTOP(ArgValues[2])));
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000268 return rv;
269 }
270 break;
Chris Lattner174f2262004-08-16 01:07:04 +0000271 case 2:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000272 if (FTy->getParamType(0)->isIntegerTy(32) &&
Duncan Sands1df98592010-02-16 11:11:14 +0000273 FTy->getParamType(1)->isPointerTy()) {
Chris Lattner870286a2006-06-01 17:29:22 +0000274 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
Misha Brukmanec843022004-10-22 23:35:57 +0000275
Chris Lattner174f2262004-08-16 01:07:04 +0000276 // Call the function.
277 GenericValue rv;
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000278 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
Reid Spencer38f6a152007-03-06 03:11:31 +0000279 (char **)GVTOP(ArgValues[1])));
Chris Lattner174f2262004-08-16 01:07:04 +0000280 return rv;
281 }
282 break;
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000283 case 1:
Nuno Lopes847a32b2012-08-02 12:09:32 +0000284 if (FTy->getParamType(0)->isIntegerTy(32)) {
Chris Lattnere5eab142004-08-15 23:53:06 +0000285 GenericValue rv;
Chris Lattner870286a2006-06-01 17:29:22 +0000286 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
Reid Spencer38f6a152007-03-06 03:11:31 +0000287 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000288 return rv;
289 }
Nuno Lopes847a32b2012-08-02 12:09:32 +0000290 if (FTy->getParamType(0)->isPointerTy()) {
291 GenericValue rv;
292 int (*PF)(char *) = (int(*)(char *))(intptr_t)FPtr;
293 rv.IntVal = APInt(32, PF((char*)GVTOP(ArgValues[0])));
294 return rv;
295 }
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000296 break;
Chris Lattnere5eab142004-08-15 23:53:06 +0000297 }
298 }
299
300 // Handle cases where no arguments are passed first.
301 if (ArgValues.empty()) {
302 GenericValue rv;
303 switch (RetTy->getTypeID()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000304 default: llvm_unreachable("Unknown return type for function call!");
Reid Spencera54b7cb2007-01-12 07:05:14 +0000305 case Type::IntegerTyID: {
306 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
307 if (BitWidth == 1)
Reid Spencer38f6a152007-03-06 03:11:31 +0000308 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000309 else if (BitWidth <= 8)
Reid Spencer38f6a152007-03-06 03:11:31 +0000310 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000311 else if (BitWidth <= 16)
Reid Spencer38f6a152007-03-06 03:11:31 +0000312 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000313 else if (BitWidth <= 32)
Reid Spencer38f6a152007-03-06 03:11:31 +0000314 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000315 else if (BitWidth <= 64)
Reid Spencer38f6a152007-03-06 03:11:31 +0000316 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000317 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000318 llvm_unreachable("Integer types > 64 bits not supported");
Chris Lattnerd297aea2004-08-15 23:34:48 +0000319 return rv;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000320 }
Chris Lattnere5eab142004-08-15 23:53:06 +0000321 case Type::VoidTyID:
Reid Spencer38f6a152007-03-06 03:11:31 +0000322 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
Chris Lattnere5eab142004-08-15 23:53:06 +0000323 return rv;
Chris Lattnere5eab142004-08-15 23:53:06 +0000324 case Type::FloatTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000325 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000326 return rv;
327 case Type::DoubleTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000328 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000329 return rv;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000330 case Type::X86_FP80TyID:
331 case Type::FP128TyID:
332 case Type::PPC_FP128TyID:
Torok Edwinc23197a2009-07-14 16:55:14 +0000333 llvm_unreachable("long double not supported yet");
Chris Lattnere5eab142004-08-15 23:53:06 +0000334 case Type::PointerTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000335 return PTOGV(((void*(*)())(intptr_t)FPtr)());
Chris Lattnerd297aea2004-08-15 23:34:48 +0000336 }
Chris Lattnerff0f1bb2003-12-26 06:13:47 +0000337 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000338
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000339 // 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.
343
344 // First, create the function.
Owen Andersondebcb012009-07-29 22:17:13 +0000345 FunctionType *STy=FunctionType::get(RetTy, false);
Gabor Greif051a9502008-04-06 20:25:17 +0000346 Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
347 F->getParent());
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000348
349 // Insert a basic block.
Owen Anderson1d0be152009-08-13 21:58:54 +0000350 BasicBlock *StubBB = BasicBlock::Create(F->getContext(), "", Stub);
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000351
352 // Convert all of the GenericValue arguments over to constants. Note that we
353 // currently don't support varargs.
Chris Lattner990b8492007-02-13 06:01:22 +0000354 SmallVector<Value*, 8> Args;
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000355 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700356 Constant *C = nullptr;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000357 Type *ArgTy = FTy->getParamType(i);
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000358 const GenericValue &AV = ArgValues[i];
359 switch (ArgTy->getTypeID()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000360 default: llvm_unreachable("Unknown argument type for function call!");
Chris Lattner02a260a2008-04-20 00:41:09 +0000361 case Type::IntegerTyID:
Owen Andersoneed707b2009-07-24 23:12:02 +0000362 C = ConstantInt::get(F->getContext(), AV.IntVal);
Chris Lattner02a260a2008-04-20 00:41:09 +0000363 break;
364 case Type::FloatTyID:
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000365 C = ConstantFP::get(F->getContext(), APFloat(AV.FloatVal));
Chris Lattner02a260a2008-04-20 00:41:09 +0000366 break;
367 case Type::DoubleTyID:
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000368 C = ConstantFP::get(F->getContext(), APFloat(AV.DoubleVal));
Chris Lattner02a260a2008-04-20 00:41:09 +0000369 break;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000370 case Type::PPC_FP128TyID:
371 case Type::X86_FP80TyID:
Chris Lattner02a260a2008-04-20 00:41:09 +0000372 case Type::FP128TyID:
Tim Northover0a29cb02013-01-22 09:46:31 +0000373 C = ConstantFP::get(F->getContext(), APFloat(ArgTy->getFltSemantics(),
374 AV.IntVal));
Chris Lattner02a260a2008-04-20 00:41:09 +0000375 break;
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000376 case Type::PointerTyID:
377 void *ArgPtr = GVTOP(AV);
Chris Lattner02a260a2008-04-20 00:41:09 +0000378 if (sizeof(void*) == 4)
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000379 C = ConstantInt::get(Type::getInt32Ty(F->getContext()),
Owen Anderson1d0be152009-08-13 21:58:54 +0000380 (int)(intptr_t)ArgPtr);
Chris Lattner02a260a2008-04-20 00:41:09 +0000381 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000382 C = ConstantInt::get(Type::getInt64Ty(F->getContext()),
383 (intptr_t)ArgPtr);
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000384 // Cast the integer to pointer
Owen Andersonbaf3c402009-07-29 18:55:55 +0000385 C = ConstantExpr::getIntToPtr(C, ArgTy);
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000386 break;
387 }
388 Args.push_back(C);
389 }
390
Jay Foada3efbb12011-07-15 08:37:34 +0000391 CallInst *TheCall = CallInst::Create(F, Args, "", StubBB);
Chris Lattnercdfc51f2008-11-23 08:00:11 +0000392 TheCall->setCallingConv(F->getCallingConv());
Chris Lattnera471e042005-05-06 06:48:54 +0000393 TheCall->setTailCall();
Benjamin Kramerf0127052010-01-05 13:12:22 +0000394 if (!TheCall->getType()->isVoidTy())
Owen Anderson1d0be152009-08-13 21:58:54 +0000395 // Return result of the call.
396 ReturnInst::Create(F->getContext(), TheCall, StubBB);
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000397 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000398 ReturnInst::Create(F->getContext(), StubBB); // Just return void.
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000399
Jeffrey Yasskin3174f672010-02-12 23:05:31 +0000400 // 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.
405 return Result;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000406}
Chris Lattner4d326fa2003-12-20 01:46:27 +0000407
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +0000408void JIT::RegisterJITEventListener(JITEventListener *L) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700409 if (!L)
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +0000410 return;
411 MutexGuard locked(lock);
412 EventListeners.push_back(L);
413}
414void JIT::UnregisterJITEventListener(JITEventListener *L) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700415 if (!L)
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +0000416 return;
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();
423 }
424}
425void JIT::NotifyFunctionEmitted(
426 const Function &F,
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);
432 }
433}
434
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000435void JIT::NotifyFreeingMachineCode(void *OldPtr) {
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +0000436 MutexGuard locked(lock);
437 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000438 EventListeners[I]->NotifyFreeingMachineCode(OldPtr);
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +0000439 }
440}
441
Chris Lattner4d326fa2003-12-20 01:46:27 +0000442/// 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.
445///
Argyrios Kyrtzidisb3a847d2009-05-18 21:06:40 +0000446void JIT::runJITOnFunction(Function *F, MachineCodeInfo *MCI) {
Reid Spenceree448632005-07-12 15:51:55 +0000447 MutexGuard locked(lock);
Argyrios Kyrtzidisb3a847d2009-05-18 21:06:40 +0000448
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +0000449 class MCIListener : public JITEventListener {
450 MachineCodeInfo *const MCI;
451 public:
452 MCIListener(MachineCodeInfo *mci) : MCI(mci) {}
Stephen Hines36b56882014-04-23 16:57:46 -0700453 void NotifyFunctionEmitted(const Function &, void *Code, size_t Size,
454 const EmittedFunctionDetails &) override {
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +0000455 MCI->setAddress(Code);
456 MCI->setSize(Size);
457 }
458 };
459 MCIListener MCIL(MCI);
Jeffrey Yasskin92fdf452009-12-22 23:18:18 +0000460 if (MCI)
461 RegisterJITEventListener(&MCIL);
Argyrios Kyrtzidisb3a847d2009-05-18 21:06:40 +0000462
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700463 runJITOnFunctionUnlocked(F);
Argyrios Kyrtzidisb3a847d2009-05-18 21:06:40 +0000464
Jeffrey Yasskin92fdf452009-12-22 23:18:18 +0000465 if (MCI)
466 UnregisterJITEventListener(&MCIL);
Dan Gohman21afcda2009-02-06 21:25:08 +0000467}
468
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700469void JIT::runJITOnFunctionUnlocked(Function *F) {
Chris Lattner17f218e2007-08-13 20:08:16 +0000470 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
Chris Lattner4d326fa2003-12-20 01:46:27 +0000471
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700472 jitTheFunctionUnlocked(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000473
Nate Begemand6b7a242009-02-18 08:31:02 +0000474 // If the function referred to another function that had not yet been
Jeffrey Yasskindc857242009-10-27 20:30:28 +0000475 // read from bitcode, and we are jitting non-lazily, emit it now.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700476 while (!jitstate->getPendingFunctions().empty()) {
477 Function *PF = jitstate->getPendingFunctions().back();
478 jitstate->getPendingFunctions().pop_back();
Nate Begemand6b7a242009-02-18 08:31:02 +0000479
Jeffrey Yasskinaad0d522009-12-17 21:35:29 +0000480 assert(!PF->hasAvailableExternallyLinkage() &&
481 "Externally-defined function should not be in pending list.");
482
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700483 jitTheFunctionUnlocked(PF);
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000484
Nate Begemand6b7a242009-02-18 08:31:02 +0000485 // Now that the function has been jitted, ask the JITEmitter to rewrite
486 // the stub with real address of the function.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700487 updateFunctionStubUnlocked(PF);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000488 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000489}
490
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700491void JIT::jitTheFunctionUnlocked(Function *F) {
Chris Lattner68feb222010-07-11 23:07:28 +0000492 isAlreadyCodeGenerating = true;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700493 jitstate->getPM().run(*F);
Chris Lattner68feb222010-07-11 23:07:28 +0000494 isAlreadyCodeGenerating = false;
495
496 // clear basic block addresses after this function is done
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700497 getBasicBlockAddressMap().clear();
Chris Lattner68feb222010-07-11 23:07:28 +0000498}
499
Chris Lattner4d326fa2003-12-20 01:46:27 +0000500/// getPointerToFunction - This method is used to get the address of the
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000501/// specified function, compiling it if necessary.
Chris Lattner4d326fa2003-12-20 01:46:27 +0000502///
503void *JIT::getPointerToFunction(Function *F) {
Reid Spenceree448632005-07-12 15:51:55 +0000504
Chris Lattnerc07ed132003-12-20 03:36:47 +0000505 if (void *Addr = getPointerToGlobalIfAvailable(F))
506 return Addr; // Check if function already code gen'd
Chris Lattner4d326fa2003-12-20 01:46:27 +0000507
Dan Gohmana3ac0c12009-02-19 02:40:15 +0000508 MutexGuard locked(lock);
509
Jeffrey Yasskinaad0d522009-12-17 21:35:29 +0000510 // 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;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000513 if (F->Materialize(&ErrorMsg)) {
Chris Lattner75361b62010-04-07 22:58:41 +0000514 report_fatal_error("Error reading function '" + F->getName()+
Jeffrey Yasskinaad0d522009-12-17 21:35:29 +0000515 "' from bitcode file: " + ErrorMsg);
Nicolas Geoffrayfd7d9912008-04-20 08:33:02 +0000516 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000517
Jeffrey Yasskinaad0d522009-12-17 21:35:29 +0000518 // ... and check if another thread has already code gen'd the function.
519 if (void *Addr = getPointerToGlobalIfAvailable(F))
520 return Addr;
521
Chris Lattner46264f02009-10-25 23:06:42 +0000522 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
Jeffrey Yasskin6f348e42009-11-09 22:34:19 +0000523 bool AbortOnFailure = !F->hasExternalWeakLinkage();
Dan Gohman69f93782009-01-05 05:32:42 +0000524 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000525 addGlobalMapping(F, Addr);
526 return Addr;
527 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000528
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700529 runJITOnFunctionUnlocked(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000530
531 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000532 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
533 return Addr;
534}
535
Chris Lattner68feb222010-07-11 23:07:28 +0000536void JIT::addPointerToBasicBlock(const BasicBlock *BB, void *Addr) {
537 MutexGuard locked(lock);
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000538
Chris Lattner68feb222010-07-11 23:07:28 +0000539 BasicBlockAddressMapTy::iterator I =
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700540 getBasicBlockAddressMap().find(BB);
541 if (I == getBasicBlockAddressMap().end()) {
542 getBasicBlockAddressMap()[BB] = Addr;
Chris Lattner68feb222010-07-11 23:07:28 +0000543 } else {
544 // ignore repeats: some BBs can be split into few MBBs?
545 }
546}
547
548void JIT::clearPointerToBasicBlock(const BasicBlock *BB) {
549 MutexGuard locked(lock);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700550 getBasicBlockAddressMap().erase(BB);
Chris Lattner68feb222010-07-11 23:07:28 +0000551}
552
553void *JIT::getPointerToBasicBlock(BasicBlock *BB) {
554 // make sure it's function is compiled by JIT
555 (void)getPointerToFunction(BB->getParent());
556
557 // resolve basic block address
558 MutexGuard locked(lock);
Jim Grosbach4f9fc852011-03-15 20:25:54 +0000559
Chris Lattner68feb222010-07-11 23:07:28 +0000560 BasicBlockAddressMapTy::iterator I =
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700561 getBasicBlockAddressMap().find(BB);
562 if (I != getBasicBlockAddressMap().end()) {
Chris Lattner68feb222010-07-11 23:07:28 +0000563 return I->second;
564 } else {
Craig Topper85814382012-02-07 05:05:23 +0000565 llvm_unreachable("JIT does not have BB address for address-of-label, was"
566 " it eliminated by optimizer?");
Chris Lattner68feb222010-07-11 23:07:28 +0000567 }
568}
569
Danil Malyshev30b9e322012-03-28 21:46:36 +0000570void *JIT::getPointerToNamedFunction(const std::string &Name,
571 bool AbortOnFailure){
572 if (!isSymbolSearchingDisabled()) {
573 void *ptr = JMM->getPointerToNamedFunction(Name, false);
574 if (ptr)
575 return ptr;
576 }
577
578 /// If a LazyFunctionCreator is installed, use it to get/create the function.
579 if (LazyFunctionCreator)
580 if (void *RP = LazyFunctionCreator(Name))
581 return RP;
582
583 if (AbortOnFailure) {
584 report_fatal_error("Program used external function '"+Name+
585 "' which could not be resolved!");
586 }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700587 return nullptr;
Danil Malyshev30b9e322012-03-28 21:46:36 +0000588}
589
590
Chris Lattnerc07ed132003-12-20 03:36:47 +0000591/// getOrEmitGlobalVariable - Return the address of the specified global
592/// variable, possibly emitting it to memory if needed. This is used by the
593/// Emitter.
594void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
Reid Spenceree448632005-07-12 15:51:55 +0000595 MutexGuard locked(lock);
596
Chris Lattnerc07ed132003-12-20 03:36:47 +0000597 void *Ptr = getPointerToGlobalIfAvailable(GV);
598 if (Ptr) return Ptr;
599
600 // If the global is external, just remember the address.
Jeffrey Yasskin898e9df2009-12-13 20:30:32 +0000601 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage()) {
Anton Korobeynikov3b30a6e2007-07-30 20:02:02 +0000602#if HAVE___DSO_HANDLE
Evan Cheng5f42c552006-07-21 23:06:20 +0000603 if (GV->getName() == "__dso_handle")
604 return (void*)&__dso_handle;
Evan Chengb82ab942006-07-22 00:42:03 +0000605#endif
Daniel Dunbarfbee5792009-07-21 08:54:24 +0000606 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName());
Stephen Hinesdce4a402014-05-29 02:49:00 -0700607 if (!Ptr) {
Chris Lattner75361b62010-04-07 22:58:41 +0000608 report_fatal_error("Could not resolve external global address: "
Torok Edwin31e24662009-07-07 17:32:34 +0000609 +GV->getName());
Chris Lattnerc07ed132003-12-20 03:36:47 +0000610 }
Nate Begeman66941982009-03-04 19:10:38 +0000611 addGlobalMapping(GV, Ptr);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000612 } else {
Dale Johannesendd947ea2008-08-07 01:30:15 +0000613 // If the global hasn't been emitted to memory yet, allocate space and
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000614 // emit it into memory.
615 Ptr = getMemoryForGV(GV);
Dale Johannesendd947ea2008-08-07 01:30:15 +0000616 addGlobalMapping(GV, Ptr);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000617 EmitGlobalVariable(GV); // Initialize the variable.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000618 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000619 return Ptr;
620}
621
Chris Lattner4d326fa2003-12-20 01:46:27 +0000622/// 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().
627///
628void *JIT::recompileAndRelinkFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000629 void *OldAddr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000630
Chris Lattnerc07ed132003-12-20 03:36:47 +0000631 // If it's not already compiled there is no reason to patch it up.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700632 if (!OldAddr) return getPointerToFunction(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000633
Chris Lattnerc07ed132003-12-20 03:36:47 +0000634 // Delete the old function mapping.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700635 addGlobalMapping(F, nullptr);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000636
Chris Lattnerc07ed132003-12-20 03:36:47 +0000637 // Recodegen the function
Chris Lattner4d326fa2003-12-20 01:46:27 +0000638 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000639
640 // Update state, forward the old function to the new function.
641 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000642 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
643 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
644 return Addr;
645}
Misha Brukman895eddf2004-11-07 23:58:46 +0000646
Nicolas Geoffray46fa1392008-10-25 15:41:43 +0000647/// getMemoryForGV - This method abstracts memory allocation of global
648/// variable so that the JIT can allocate thread local variables depending
649/// on the target.
650///
651char* JIT::getMemoryForGV(const GlobalVariable* GV) {
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000652 char *Ptr;
653
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
656 // not be writable.
657 if (isGVCompilationDisabled() && !GV->isConstant()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000658 report_fatal_error("Compilation of non-internal GlobalValue is disabled!");
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000659 }
660
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
664 // same buffer.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000665 Type *GlobalType = GV->getType()->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +0000666 size_t S = getDataLayout()->getTypeAllocSize(GlobalType);
667 size_t A = getDataLayout()->getPreferredAlignment(GV);
Nicolas Geoffray46fa1392008-10-25 15:41:43 +0000668 if (GV->isThreadLocal()) {
669 MutexGuard locked(lock);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000670 Ptr = TJI.allocateThreadLocalMemory(S);
671 } else if (TJI.allocateSeparateGVMemory()) {
672 if (A <= 8) {
673 Ptr = (char*)malloc(S);
674 } else {
675 // Allocate S+A bytes of memory, then use an aligned pointer within that
676 // space.
677 Ptr = (char*)malloc(S+A);
678 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
679 Ptr = Ptr + (MisAligned ? (A-MisAligned) : 0);
680 }
681 } else if (AllocateGVsWithCode) {
682 Ptr = (char*)JCE->allocateSpace(S, A);
Nicolas Geoffray46fa1392008-10-25 15:41:43 +0000683 } else {
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000684 Ptr = (char*)JCE->allocateGlobal(S, A);
Nicolas Geoffray46fa1392008-10-25 15:41:43 +0000685 }
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000686 return Ptr;
Nicolas Geoffray46fa1392008-10-25 15:41:43 +0000687}
Nate Begemand6b7a242009-02-18 08:31:02 +0000688
689void JIT::addPendingFunction(Function *F) {
690 MutexGuard locked(lock);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700691 jitstate->getPendingFunctions().push_back(F);
Nate Begemand6b7a242009-02-18 08:31:02 +0000692}
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +0000693
694
695JITEventListener::~JITEventListener() {}