blob: 3bf6db8ee8fe752e4c23d9a16d4b47ff1d2dffc7 [file] [log] [blame]
Chris Lattnerc5753052004-11-22 22:00:25 +00001//===-- JITEmitter.cpp - Write machine code to executable memory ----------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman10468d82005-04-21 22:55:34 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner996fe012002-12-24 00:01:05 +00009//
Chris Lattner6cf7a432004-11-20 03:46:14 +000010// This file defines a MachineCodeEmitter object that is used by the JIT to
11// write machine code to memory and remember where relocatable values are.
Chris Lattner996fe012002-12-24 00:01:05 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattneree937c82003-08-05 17:00:32 +000015#define DEBUG_TYPE "jit"
Chris Lattner9bcae072003-12-20 01:46:27 +000016#include "JIT.h"
Nicolas Geoffray21ad4942008-02-13 18:39:37 +000017#include "JITDwarfEmitter.h"
Reid Kleckner9a10db82009-09-20 23:52:43 +000018#include "llvm/ADT/OwningPtr.h"
Dale Johannesenb086d382008-08-07 01:30:15 +000019#include "llvm/Constants.h"
Bill Wendlinge38859d2012-06-28 00:05:13 +000020#include "llvm/DebugInfo.h"
Dale Johannesenb086d382008-08-07 01:30:15 +000021#include "llvm/DerivedTypes.h"
Bill Wendlinge38859d2012-06-28 00:05:13 +000022#include "llvm/Module.h"
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000023#include "llvm/CodeGen/JITCodeEmitter.h"
Chris Lattner996fe012002-12-24 00:01:05 +000024#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling929f3c02010-04-16 08:46:10 +000025#include "llvm/CodeGen/MachineCodeInfo.h"
Chris Lattner4ba3bbb2003-01-13 01:00:12 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000027#include "llvm/CodeGen/MachineJumpTableInfo.h"
Nicolas Geoffray21ad4942008-02-13 18:39:37 +000028#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner6cf7a432004-11-20 03:46:14 +000029#include "llvm/CodeGen/MachineRelocation.h"
Dale Johannesenb086d382008-08-07 01:30:15 +000030#include "llvm/ExecutionEngine/GenericValue.h"
Jeffrey Yasskin0b08f3d2009-06-25 02:04:04 +000031#include "llvm/ExecutionEngine/JITEventListener.h"
32#include "llvm/ExecutionEngine/JITMemoryManager.h"
Chris Lattner4ba3bbb2003-01-13 01:00:12 +000033#include "llvm/Target/TargetData.h"
Bill Wendling929f3c02010-04-16 08:46:10 +000034#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner6cf7a432004-11-20 03:46:14 +000035#include "llvm/Target/TargetJITInfo.h"
Jim Laskey70323a82006-12-14 19:17:33 +000036#include "llvm/Target/TargetMachine.h"
Nicolas Geoffray21ad4942008-02-13 18:39:37 +000037#include "llvm/Target/TargetOptions.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000038#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000039#include "llvm/Support/ErrorHandling.h"
Jeffrey Yasskine0913882010-02-11 01:07:39 +000040#include "llvm/Support/ManagedStatic.h"
Chris Lattner6d8dd182006-05-08 22:00:52 +000041#include "llvm/Support/MutexGuard.h"
Nick Lewycky0575dbb2009-04-19 18:32:03 +000042#include "llvm/Support/ValueHandle.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000043#include "llvm/Support/raw_ostream.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000044#include "llvm/Support/Disassembler.h"
45#include "llvm/Support/Memory.h"
Jeffrey Yasskin27c66922009-10-20 18:13:21 +000046#include "llvm/ADT/DenseMap.h"
Evan Cheng00203152008-11-07 09:02:17 +000047#include "llvm/ADT/SmallPtrSet.h"
Nate Begeman18d85e72009-02-18 08:31:02 +000048#include "llvm/ADT/SmallVector.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000049#include "llvm/ADT/Statistic.h"
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +000050#include "llvm/ADT/ValueMap.h"
Andrew Lenharth09402182005-07-29 23:40:16 +000051#include <algorithm>
Evan Chengd1c5c7f2008-11-05 23:44:08 +000052#ifndef NDEBUG
53#include <iomanip>
54#endif
Chris Lattnerc0e1b072003-12-08 08:06:28 +000055using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000056
Chris Lattnerc346ecd2006-12-19 22:43:32 +000057STATISTIC(NumBytes, "Number of bytes of machine code compiled");
58STATISTIC(NumRelos, "Number of relocations applied");
Reid Kleckner4b3a3562009-07-23 21:46:56 +000059STATISTIC(NumRetries, "Number of retries with more memory");
Chris Lattner60d815a2004-11-20 23:57:07 +000060
Andrew Lenharth09402182005-07-29 23:40:16 +000061
Jeffrey Yasskin65234292009-12-22 23:47:23 +000062// A declaration may stop being a declaration once it's fully read from bitcode.
63// This function returns true if F is fully read and is still a declaration.
64static bool isNonGhostDeclaration(const Function *F) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000065 return F->isDeclaration() && !F->isMaterializable();
Jeffrey Yasskin65234292009-12-22 23:47:23 +000066}
67
Chris Lattner60d815a2004-11-20 23:57:07 +000068//===----------------------------------------------------------------------===//
69// JIT lazy compilation code.
70//
71namespace {
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +000072 class JITEmitter;
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +000073 class JITResolverState;
74
75 template<typename ValueTy>
76 struct NoRAUWValueMapConfig : public ValueMapConfig<ValueTy> {
77 typedef JITResolverState *ExtraData;
78 static void onRAUW(JITResolverState *, Value *Old, Value *New) {
Craig Toppera2886c22012-02-07 05:05:23 +000079 llvm_unreachable("The JIT doesn't know how to handle a"
80 " RAUW on a value it has emitted.");
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +000081 }
82 };
83
84 struct CallSiteValueMapConfig : public NoRAUWValueMapConfig<Function*> {
85 typedef JITResolverState *ExtraData;
86 static void onDelete(JITResolverState *JRS, Function *F);
87 };
88
Reid Spencer79876f52005-07-12 15:51:55 +000089 class JITResolverState {
Nick Lewyckyf44a5bf2009-04-27 05:09:44 +000090 public:
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +000091 typedef ValueMap<Function*, void*, NoRAUWValueMapConfig<Function*> >
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +000092 FunctionToLazyStubMapTy;
Jeffrey Yasskin7d2edad2009-10-19 18:49:59 +000093 typedef std::map<void*, AssertingVH<Function> > CallSiteToFunctionMapTy;
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +000094 typedef ValueMap<Function *, SmallPtrSet<void*, 1>,
95 CallSiteValueMapConfig> FunctionToCallSitesMapTy;
Nick Lewyckyf44a5bf2009-04-27 05:09:44 +000096 typedef std::map<AssertingVH<GlobalValue>, void*> GlobalToIndirectSymMapTy;
Reid Spencer79876f52005-07-12 15:51:55 +000097 private:
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +000098 /// FunctionToLazyStubMap - Keep track of the lazy stub created for a
99 /// particular function so that we can reuse them if necessary.
100 FunctionToLazyStubMapTy FunctionToLazyStubMap;
Reid Spencer79876f52005-07-12 15:51:55 +0000101
Jeffrey Yasskin7d2edad2009-10-19 18:49:59 +0000102 /// CallSiteToFunctionMap - Keep track of the function that each lazy call
103 /// site corresponds to, and vice versa.
104 CallSiteToFunctionMapTy CallSiteToFunctionMap;
105 FunctionToCallSitesMapTy FunctionToCallSitesMap;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000106
Evan Cheng0d9db402008-11-10 01:52:24 +0000107 /// GlobalToIndirectSymMap - Keep track of the indirect symbol created for a
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000108 /// particular GlobalVariable so that we can reuse them if necessary.
Nick Lewyckyf44a5bf2009-04-27 05:09:44 +0000109 GlobalToIndirectSymMapTy GlobalToIndirectSymMap;
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000110
Benjamin Kramerd0b767f2012-06-16 21:55:52 +0000111#ifndef NDEBUG
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000112 /// Instance of the JIT this ResolverState serves.
113 JIT *TheJIT;
Benjamin Kramerd0b767f2012-06-16 21:55:52 +0000114#endif
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000115
Reid Spencer79876f52005-07-12 15:51:55 +0000116 public:
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000117 JITResolverState(JIT *jit) : FunctionToLazyStubMap(this),
Benjamin Kramerd0b767f2012-06-16 21:55:52 +0000118 FunctionToCallSitesMap(this) {
119#ifndef NDEBUG
120 TheJIT = jit;
121#endif
122 }
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000123
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000124 FunctionToLazyStubMapTy& getFunctionToLazyStubMap(
125 const MutexGuard& locked) {
Reid Spencer79876f52005-07-12 15:51:55 +0000126 assert(locked.holds(TheJIT->lock));
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000127 return FunctionToLazyStubMap;
Reid Spencer79876f52005-07-12 15:51:55 +0000128 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000129
Jim Grosbachc91fa6d2011-03-16 01:21:55 +0000130 GlobalToIndirectSymMapTy& getGlobalToIndirectSymMap(const MutexGuard& lck) {
131 assert(lck.holds(TheJIT->lock));
Evan Cheng0d9db402008-11-10 01:52:24 +0000132 return GlobalToIndirectSymMap;
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000133 }
Jeffrey Yasskin7d2edad2009-10-19 18:49:59 +0000134
Jay Foad3b422a12011-04-13 12:46:01 +0000135 std::pair<void *, Function *> LookupFunctionFromCallSite(
Jeffrey Yasskin7d2edad2009-10-19 18:49:59 +0000136 const MutexGuard &locked, void *CallSite) const {
137 assert(locked.holds(TheJIT->lock));
138
Jim Grosbachc91fa6d2011-03-16 01:21:55 +0000139 // The address given to us for the stub may not be exactly right, it
140 // might be a little bit after the stub. As such, use upper_bound to
141 // find it.
Jeffrey Yasskin7d2edad2009-10-19 18:49:59 +0000142 CallSiteToFunctionMapTy::const_iterator I =
143 CallSiteToFunctionMap.upper_bound(CallSite);
144 assert(I != CallSiteToFunctionMap.begin() &&
145 "This is not a known call site!");
146 --I;
147 return *I;
148 }
149
150 void AddCallSite(const MutexGuard &locked, void *CallSite, Function *F) {
151 assert(locked.holds(TheJIT->lock));
152
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000153 bool Inserted = CallSiteToFunctionMap.insert(
154 std::make_pair(CallSite, F)).second;
155 (void)Inserted;
156 assert(Inserted && "Pair was already in CallSiteToFunctionMap");
Jeffrey Yasskin7d2edad2009-10-19 18:49:59 +0000157 FunctionToCallSitesMap[F].insert(CallSite);
158 }
159
Jeffrey Yasskin950e0fb2010-03-04 00:32:33 +0000160 void EraseAllCallSitesForPrelocked(Function *F);
161
162 // Erases _all_ call sites regardless of their function. This is used to
163 // unregister the stub addresses from the StubToResolverMap in
164 // ~JITResolver().
165 void EraseAllCallSitesPrelocked();
Reid Spencer79876f52005-07-12 15:51:55 +0000166 };
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000167
Chris Lattner60d815a2004-11-20 23:57:07 +0000168 /// JITResolver - Keep track of, and resolve, call sites for functions that
169 /// have not yet been compiled.
170 class JITResolver {
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000171 typedef JITResolverState::FunctionToLazyStubMapTy FunctionToLazyStubMapTy;
Jeffrey Yasskin7d2edad2009-10-19 18:49:59 +0000172 typedef JITResolverState::CallSiteToFunctionMapTy CallSiteToFunctionMapTy;
Nick Lewyckyf44a5bf2009-04-27 05:09:44 +0000173 typedef JITResolverState::GlobalToIndirectSymMapTy GlobalToIndirectSymMapTy;
174
Chris Lattner65f66382004-11-21 03:37:42 +0000175 /// LazyResolverFn - The target lazy resolver function that we actually
176 /// rewrite instructions to use.
177 TargetJITInfo::LazyResolverFn LazyResolverFn;
178
Reid Spencer79876f52005-07-12 15:51:55 +0000179 JITResolverState state;
Chris Lattner60d815a2004-11-20 23:57:07 +0000180
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000181 /// ExternalFnToStubMap - This is the equivalent of FunctionToLazyStubMap
182 /// for external functions. TODO: Of course, external functions don't need
183 /// a lazy stub. It's actually here to make it more likely that far calls
184 /// succeed, but no single stub can guarantee that. I'll remove this in a
185 /// subsequent checkin when I actually fix far calls.
Chris Lattnered5189d2005-04-18 01:44:27 +0000186 std::map<void*, void*> ExternalFnToStubMap;
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000187
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000188 /// revGOTMap - map addresses to indexes in the GOT
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000189 std::map<void*, unsigned> revGOTMap;
190 unsigned nextGOTIndex;
191
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000192 JITEmitter &JE;
193
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000194 /// Instance of JIT corresponding to this Resolver.
195 JIT *TheJIT;
196
Chris Lattner60d815a2004-11-20 23:57:07 +0000197 public:
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000198 explicit JITResolver(JIT &jit, JITEmitter &je)
Benjamin Kramerd0b767f2012-06-16 21:55:52 +0000199 : state(&jit), nextGOTIndex(0), JE(je), TheJIT(&jit) {
Chris Lattner05858a92007-02-24 02:57:03 +0000200 LazyResolverFn = jit.getJITInfo().getLazyResolverFunction(JITCompilerFn);
Chris Lattner65f66382004-11-21 03:37:42 +0000201 }
Chris Lattner60d815a2004-11-20 23:57:07 +0000202
Jeffrey Yasskin950e0fb2010-03-04 00:32:33 +0000203 ~JITResolver();
204
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000205 /// getLazyFunctionStubIfAvailable - This returns a pointer to a function's
206 /// lazy-compilation stub if it has already been created.
207 void *getLazyFunctionStubIfAvailable(Function *F);
Evan Chengbf9a0552008-11-13 21:50:50 +0000208
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000209 /// getLazyFunctionStub - This returns a pointer to a function's
210 /// lazy-compilation stub, creating one on demand as needed.
211 void *getLazyFunctionStub(Function *F);
Chris Lattner60d815a2004-11-20 23:57:07 +0000212
Chris Lattnered5189d2005-04-18 01:44:27 +0000213 /// getExternalFunctionStub - Return a stub for the function at the
214 /// specified address, created lazily on demand.
215 void *getExternalFunctionStub(void *FnAddr);
216
Evan Cheng0d9db402008-11-10 01:52:24 +0000217 /// getGlobalValueIndirectSym - Return an indirect symbol containing the
Evan Cheng132de192008-11-05 01:50:32 +0000218 /// specified GV address.
Evan Cheng0d9db402008-11-10 01:52:24 +0000219 void *getGlobalValueIndirectSym(GlobalValue *V, void *GVAddress);
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000220
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000221 /// getGOTIndexForAddress - Return a new or existing index in the GOT for
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000222 /// an address. This function only manages slots, it does not manage the
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000223 /// contents of the slots or the memory associated with the GOT.
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000224 unsigned getGOTIndexForAddr(void *addr);
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000225
Chris Lattner60d815a2004-11-20 23:57:07 +0000226 /// JITCompilerFn - This function is called to resolve a stub to a compiled
227 /// address. If the LLVM Function corresponding to the stub has not yet
228 /// been compiled, this function compiles it first.
229 static void *JITCompilerFn(void *Stub);
230 };
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000231
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000232 class StubToResolverMapTy {
233 /// Map a stub address to a specific instance of a JITResolver so that
234 /// lazily-compiled functions can find the right resolver to use.
235 ///
236 /// Guarded by Lock.
237 std::map<void*, JITResolver*> Map;
238
239 /// Guards Map from concurrent accesses.
240 mutable sys::Mutex Lock;
241
242 public:
243 /// Registers a Stub to be resolved by Resolver.
244 void RegisterStubResolver(void *Stub, JITResolver *Resolver) {
245 MutexGuard guard(Lock);
246 Map.insert(std::make_pair(Stub, Resolver));
247 }
248 /// Unregisters the Stub when it's invalidated.
249 void UnregisterStubResolver(void *Stub) {
250 MutexGuard guard(Lock);
251 Map.erase(Stub);
252 }
253 /// Returns the JITResolver instance that owns the Stub.
254 JITResolver *getResolverFromStub(void *Stub) const {
255 MutexGuard guard(Lock);
256 // The address given to us for the stub may not be exactly right, it might
257 // be a little bit after the stub. As such, use upper_bound to find it.
258 // This is the same trick as in LookupFunctionFromCallSite from
259 // JITResolverState.
260 std::map<void*, JITResolver*>::const_iterator I = Map.upper_bound(Stub);
261 assert(I != Map.begin() && "This is not a known stub!");
262 --I;
263 return I->second;
264 }
Jeffrey Yasskin950e0fb2010-03-04 00:32:33 +0000265 /// True if any stubs refer to the given resolver. Only used in an assert().
266 /// O(N)
267 bool ResolverHasStubs(JITResolver* Resolver) const {
268 MutexGuard guard(Lock);
269 for (std::map<void*, JITResolver*>::const_iterator I = Map.begin(),
270 E = Map.end(); I != E; ++I) {
271 if (I->second == Resolver)
272 return true;
273 }
274 return false;
275 }
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000276 };
277 /// This needs to be static so that a lazy call stub can access it with no
278 /// context except the address of the stub.
279 ManagedStatic<StubToResolverMapTy> StubToResolverMap;
280
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000281 /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
282 /// used to output functions to memory for execution.
283 class JITEmitter : public JITCodeEmitter {
284 JITMemoryManager *MemMgr;
285
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +0000286 // When outputting a function stub in the context of some other function, we
287 // save BufferBegin/BufferEnd/CurBufferPtr here.
288 uint8_t *SavedBufferBegin, *SavedBufferEnd, *SavedCurBufferPtr;
289
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000290 // When reattempting to JIT a function after running out of space, we store
291 // the estimated size of the function we're trying to JIT here, so we can
292 // ask the memory manager for at least this much space. When we
293 // successfully emit the function, we reset this back to zero.
294 uintptr_t SizeEstimate;
295
296 /// Relocations - These are the relocations that the function needs, as
297 /// emitted.
298 std::vector<MachineRelocation> Relocations;
Eric Christophercb5e2272009-11-12 03:12:18 +0000299
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000300 /// MBBLocations - This vector is a mapping from MBB ID's to their address.
301 /// It is filled in by the StartMachineBasicBlock callback and queried by
302 /// the getMachineBasicBlockAddress callback.
303 std::vector<uintptr_t> MBBLocations;
304
305 /// ConstantPool - The constant pool for the current function.
306 ///
307 MachineConstantPool *ConstantPool;
308
309 /// ConstantPoolBase - A pointer to the first entry in the constant pool.
310 ///
311 void *ConstantPoolBase;
312
313 /// ConstPoolAddresses - Addresses of individual constant pool entries.
314 ///
315 SmallVector<uintptr_t, 8> ConstPoolAddresses;
316
317 /// JumpTable - The jump tables for the current function.
318 ///
319 MachineJumpTableInfo *JumpTable;
Eric Christophercb5e2272009-11-12 03:12:18 +0000320
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000321 /// JumpTableBase - A pointer to the first entry in the jump table.
322 ///
323 void *JumpTableBase;
324
325 /// Resolver - This contains info about the currently resolved functions.
326 JITResolver Resolver;
327
328 /// DE - The dwarf emitter for the jit.
329 OwningPtr<JITDwarfEmitter> DE;
330
Eric Christophercb5e2272009-11-12 03:12:18 +0000331 /// LabelLocations - This vector is a mapping from Label ID's to their
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000332 /// address.
Chris Lattner34adc8d2010-03-14 01:41:15 +0000333 DenseMap<MCSymbol*, uintptr_t> LabelLocations;
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000334
335 /// MMI - Machine module info for exception informations
336 MachineModuleInfo* MMI;
337
Eric Christophercb5e2272009-11-12 03:12:18 +0000338 // CurFn - The llvm function being emitted. Only valid during
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000339 // finishFunction().
340 const Function *CurFn;
341
342 /// Information about emitted code, which is passed to the
343 /// JITEventListeners. This is reset in startFunction and used in
344 /// finishFunction.
345 JITEvent_EmittedFunctionDetails EmissionDetails;
346
347 struct EmittedCode {
348 void *FunctionBody; // Beginning of the function's allocation.
349 void *Code; // The address the function's code actually starts at.
350 void *ExceptionTable;
351 EmittedCode() : FunctionBody(0), Code(0), ExceptionTable(0) {}
352 };
353 struct EmittedFunctionConfig : public ValueMapConfig<const Function*> {
354 typedef JITEmitter *ExtraData;
355 static void onDelete(JITEmitter *, const Function*);
356 static void onRAUW(JITEmitter *, const Function*, const Function*);
357 };
358 ValueMap<const Function *, EmittedCode,
359 EmittedFunctionConfig> EmittedFunctions;
360
Nicolas Geoffray99bfbca2010-04-14 22:06:37 +0000361 DebugLoc PrevDL;
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000362
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000363 /// Instance of the JIT
364 JIT *TheJIT;
365
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000366 bool JITExceptionHandling;
367
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000368 public:
369 JITEmitter(JIT &jit, JITMemoryManager *JMM, TargetMachine &TM)
370 : SizeEstimate(0), Resolver(jit, *this), MMI(0), CurFn(0),
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000371 EmittedFunctions(this), TheJIT(&jit),
Rafael Espindolaafcf5712012-01-05 22:07:43 +0000372 JITExceptionHandling(TM.Options.JITExceptionHandling) {
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000373 MemMgr = JMM ? JMM : JITMemoryManager::CreateDefaultMemManager();
374 if (jit.getJITInfo().needsGOT()) {
375 MemMgr->AllocateGOT();
David Greene85814ac2010-01-05 01:23:36 +0000376 DEBUG(dbgs() << "JIT is managing a GOT\n");
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000377 }
378
Rafael Espindolaafcf5712012-01-05 22:07:43 +0000379 if (JITExceptionHandling) {
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000380 DE.reset(new JITDwarfEmitter(jit));
381 }
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000382 }
Eric Christophercb5e2272009-11-12 03:12:18 +0000383 ~JITEmitter() {
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000384 delete MemMgr;
385 }
386
387 /// classof - Methods for support type inquiry through isa, cast, and
388 /// dyn_cast:
389 ///
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000390 static inline bool classof(const MachineCodeEmitter*) { return true; }
Eric Christophercb5e2272009-11-12 03:12:18 +0000391
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000392 JITResolver &getJITResolver() { return Resolver; }
393
394 virtual void startFunction(MachineFunction &F);
395 virtual bool finishFunction(MachineFunction &F);
Eric Christophercb5e2272009-11-12 03:12:18 +0000396
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000397 void emitConstantPool(MachineConstantPool *MCP);
398 void initJumpTableInfo(MachineJumpTableInfo *MJTI);
399 void emitJumpTableInfo(MachineJumpTableInfo *MJTI);
Eric Christophercb5e2272009-11-12 03:12:18 +0000400
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +0000401 void startGVStub(const GlobalValue* GV,
402 unsigned StubSize, unsigned Alignment = 1);
403 void startGVStub(void *Buffer, unsigned StubSize);
404 void finishGVStub();
405 virtual void *allocIndirectGV(const GlobalValue *GV,
406 const uint8_t *Buffer, size_t Size,
407 unsigned Alignment);
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000408
409 /// allocateSpace - Reserves space in the current block if any, or
410 /// allocate a new one of the given size.
411 virtual void *allocateSpace(uintptr_t Size, unsigned Alignment);
412
413 /// allocateGlobal - Allocate memory for a global. Unlike allocateSpace,
414 /// this method does not allocate memory in the current output buffer,
415 /// because a global may live longer than the current function.
416 virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment);
417
418 virtual void addRelocation(const MachineRelocation &MR) {
419 Relocations.push_back(MR);
420 }
Eric Christophercb5e2272009-11-12 03:12:18 +0000421
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000422 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
423 if (MBBLocations.size() <= (unsigned)MBB->getNumber())
424 MBBLocations.resize((MBB->getNumber()+1)*2);
425 MBBLocations[MBB->getNumber()] = getCurrentPCValue();
Chris Lattnerb6df00c2010-07-11 23:07:28 +0000426 if (MBB->hasAddressTaken())
427 TheJIT->addPointerToBasicBlock(MBB->getBasicBlock(),
428 (void*)getCurrentPCValue());
David Greene85814ac2010-01-05 01:23:36 +0000429 DEBUG(dbgs() << "JIT: Emitting BB" << MBB->getNumber() << " at ["
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000430 << (void*) getCurrentPCValue() << "]\n");
431 }
432
433 virtual uintptr_t getConstantPoolEntryAddress(unsigned Entry) const;
434 virtual uintptr_t getJumpTableEntryAddress(unsigned Entry) const;
435
Chris Lattnerb6df00c2010-07-11 23:07:28 +0000436 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const{
Eric Christophercb5e2272009-11-12 03:12:18 +0000437 assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000438 MBBLocations[MBB->getNumber()] && "MBB not emitted!");
439 return MBBLocations[MBB->getNumber()];
440 }
441
442 /// retryWithMoreMemory - Log a retry and deallocate all memory for the
443 /// given function. Increase the minimum allocation size so that we get
444 /// more memory next time.
445 void retryWithMoreMemory(MachineFunction &F);
446
447 /// deallocateMemForFunction - Deallocate all memory for the specified
448 /// function body.
449 void deallocateMemForFunction(const Function *F);
450
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000451 virtual void processDebugLoc(DebugLoc DL, bool BeforePrintingInsn);
452
Chris Lattner34adc8d2010-03-14 01:41:15 +0000453 virtual void emitLabel(MCSymbol *Label) {
454 LabelLocations[Label] = getCurrentPCValue();
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000455 }
456
Bill Wendling929f3c02010-04-16 08:46:10 +0000457 virtual DenseMap<MCSymbol*, uintptr_t> *getLabelLocations() {
458 return &LabelLocations;
459 }
460
Chris Lattner34adc8d2010-03-14 01:41:15 +0000461 virtual uintptr_t getLabelAddress(MCSymbol *Label) const {
462 assert(LabelLocations.count(Label) && "Label not emitted!");
463 return LabelLocations.find(Label)->second;
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000464 }
Eric Christophercb5e2272009-11-12 03:12:18 +0000465
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000466 virtual void setModuleInfo(MachineModuleInfo* Info) {
467 MMI = Info;
468 if (DE.get()) DE->setModuleInfo(Info);
469 }
470
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000471 private:
Jeffrey Yasskindb5f24c2009-11-07 08:51:52 +0000472 void *getPointerToGlobal(GlobalValue *GV, void *Reference,
473 bool MayNeedFarStub);
474 void *getPointerToGVIndirectSym(GlobalValue *V, void *Reference);
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000475 };
Chris Lattner60d815a2004-11-20 23:57:07 +0000476}
477
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000478void CallSiteValueMapConfig::onDelete(JITResolverState *JRS, Function *F) {
Jeffrey Yasskin950e0fb2010-03-04 00:32:33 +0000479 JRS->EraseAllCallSitesForPrelocked(F);
480}
481
Jeffrey Yasskin950e0fb2010-03-04 00:32:33 +0000482void JITResolverState::EraseAllCallSitesForPrelocked(Function *F) {
483 FunctionToCallSitesMapTy::iterator F2C = FunctionToCallSitesMap.find(F);
484 if (F2C == FunctionToCallSitesMap.end())
485 return;
486 StubToResolverMapTy &S2RMap = *StubToResolverMap;
487 for (SmallPtrSet<void*, 1>::const_iterator I = F2C->second.begin(),
488 E = F2C->second.end(); I != E; ++I) {
489 S2RMap.UnregisterStubResolver(*I);
490 bool Erased = CallSiteToFunctionMap.erase(*I);
491 (void)Erased;
492 assert(Erased && "Missing call site->function mapping");
493 }
494 FunctionToCallSitesMap.erase(F2C);
495}
496
497void JITResolverState::EraseAllCallSitesPrelocked() {
498 StubToResolverMapTy &S2RMap = *StubToResolverMap;
499 for (CallSiteToFunctionMapTy::const_iterator
500 I = CallSiteToFunctionMap.begin(),
501 E = CallSiteToFunctionMap.end(); I != E; ++I) {
502 S2RMap.UnregisterStubResolver(I->first);
503 }
504 CallSiteToFunctionMap.clear();
505 FunctionToCallSitesMap.clear();
506}
507
508JITResolver::~JITResolver() {
509 // No need to lock because we're in the destructor, and state isn't shared.
510 state.EraseAllCallSitesPrelocked();
511 assert(!StubToResolverMap->ResolverHasStubs(this) &&
512 "Resolver destroyed with stubs still alive.");
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000513}
514
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000515/// getLazyFunctionStubIfAvailable - This returns a pointer to a function stub
Evan Chengbf9a0552008-11-13 21:50:50 +0000516/// if it has already been created.
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000517void *JITResolver::getLazyFunctionStubIfAvailable(Function *F) {
Evan Chengbf9a0552008-11-13 21:50:50 +0000518 MutexGuard locked(TheJIT->lock);
519
520 // If we already have a stub for this function, recycle it.
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000521 return state.getFunctionToLazyStubMap(locked).lookup(F);
Evan Chengbf9a0552008-11-13 21:50:50 +0000522}
523
Chris Lattner60d815a2004-11-20 23:57:07 +0000524/// getFunctionStub - This returns a pointer to a function stub, creating
525/// one on demand as needed.
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000526void *JITResolver::getLazyFunctionStub(Function *F) {
Reid Spencer79876f52005-07-12 15:51:55 +0000527 MutexGuard locked(TheJIT->lock);
528
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000529 // If we already have a lazy stub for this function, recycle it.
530 void *&Stub = state.getFunctionToLazyStubMap(locked)[F];
Chris Lattner60d815a2004-11-20 23:57:07 +0000531 if (Stub) return Stub;
532
Jeffrey Yasskin4567db42009-10-27 20:30:28 +0000533 // Call the lazy resolver function if we are JIT'ing lazily. Otherwise we
534 // must resolve the symbol now.
535 void *Actual = TheJIT->isCompilingLazily()
536 ? (void *)(intptr_t)LazyResolverFn : (void *)0;
537
Nate Begeman52b696c2009-03-07 06:41:19 +0000538 // If this is an external declaration, attempt to resolve the address now
539 // to place in the stub.
Jeffrey Yasskin65234292009-12-22 23:47:23 +0000540 if (isNonGhostDeclaration(F) || F->hasAvailableExternallyLinkage()) {
Chris Lattner50c2e112004-11-22 07:24:43 +0000541 Actual = TheJIT->getPointerToFunction(F);
Misha Brukman10468d82005-04-21 22:55:34 +0000542
Dan Gohmand32ec012009-01-05 05:32:42 +0000543 // If we resolved the symbol to a null address (eg. a weak external)
Jeffrey Yasskin8483f122009-11-09 22:34:19 +0000544 // don't emit a stub. Return a null pointer to the application.
545 if (!Actual) return 0;
Dan Gohmand32ec012009-01-05 05:32:42 +0000546 }
547
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000548 TargetJITInfo::StubLayout SL = TheJIT->getJITInfo().getStubLayout();
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +0000549 JE.startGVStub(F, SL.Size, SL.Alignment);
Nate Begeman52b696c2009-03-07 06:41:19 +0000550 // Codegen a new stub, calling the lazy resolver or the actual address of the
551 // external function, if it was resolved.
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000552 Stub = TheJIT->getJITInfo().emitFunctionStub(F, Actual, JE);
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +0000553 JE.finishGVStub();
Chris Lattner50c2e112004-11-22 07:24:43 +0000554
Chris Lattner71819be2006-06-01 17:29:22 +0000555 if (Actual != (void*)(intptr_t)LazyResolverFn) {
Chris Lattner50c2e112004-11-22 07:24:43 +0000556 // If we are getting the stub for an external function, we really want the
557 // address of the stub in the GlobalAddressMap for the JIT, not the address
558 // of the external function.
559 TheJIT->updateGlobalMapping(F, Stub);
560 }
Chris Lattner60d815a2004-11-20 23:57:07 +0000561
David Greene85814ac2010-01-05 01:23:36 +0000562 DEBUG(dbgs() << "JIT: Lazy stub emitted at [" << Stub << "] for function '"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000563 << F->getName() << "'\n");
Chris Lattner9de8e222004-11-21 03:44:32 +0000564
Jeffrey Yasskin950e0fb2010-03-04 00:32:33 +0000565 if (TheJIT->isCompilingLazily()) {
566 // Register this JITResolver as the one corresponding to this call site so
567 // JITCompilerFn will be able to find it.
568 StubToResolverMap->RegisterStubResolver(Stub, this);
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000569
Jeffrey Yasskin950e0fb2010-03-04 00:32:33 +0000570 // Finally, keep track of the stub-to-Function mapping so that the
571 // JITCompilerFn knows which function to compile!
572 state.AddCallSite(locked, Stub, F);
573 } else if (!Actual) {
574 // If we are JIT'ing non-lazily but need to call a function that does not
575 // exist yet, add it to the JIT's work list so that we can fill in the
576 // stub address later.
577 assert(!isNonGhostDeclaration(F) && !F->hasAvailableExternallyLinkage() &&
578 "'Actual' should have been set above.");
579 TheJIT->addPendingFunction(F);
580 }
Jeffrey Yasskind162dba2009-10-13 21:32:57 +0000581
Chris Lattner60d815a2004-11-20 23:57:07 +0000582 return Stub;
583}
584
Evan Cheng0d9db402008-11-10 01:52:24 +0000585/// getGlobalValueIndirectSym - Return a lazy pointer containing the specified
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000586/// GV address.
Evan Cheng0d9db402008-11-10 01:52:24 +0000587void *JITResolver::getGlobalValueIndirectSym(GlobalValue *GV, void *GVAddress) {
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000588 MutexGuard locked(TheJIT->lock);
589
590 // If we already have a stub for this global variable, recycle it.
Evan Cheng0d9db402008-11-10 01:52:24 +0000591 void *&IndirectSym = state.getGlobalToIndirectSymMap(locked)[GV];
592 if (IndirectSym) return IndirectSym;
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000593
Evan Chengb172c662008-11-10 23:26:16 +0000594 // Otherwise, codegen a new indirect symbol.
Evan Cheng0d9db402008-11-10 01:52:24 +0000595 IndirectSym = TheJIT->getJITInfo().emitGlobalValueIndirectSym(GV, GVAddress,
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000596 JE);
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000597
David Greene85814ac2010-01-05 01:23:36 +0000598 DEBUG(dbgs() << "JIT: Indirect symbol emitted at [" << IndirectSym
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000599 << "] for GV '" << GV->getName() << "'\n");
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000600
Evan Cheng0d9db402008-11-10 01:52:24 +0000601 return IndirectSym;
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000602}
603
Chris Lattnered5189d2005-04-18 01:44:27 +0000604/// getExternalFunctionStub - Return a stub for the function at the
605/// specified address, created lazily on demand.
606void *JITResolver::getExternalFunctionStub(void *FnAddr) {
607 // If we already have a stub for this function, recycle it.
608 void *&Stub = ExternalFnToStubMap[FnAddr];
609 if (Stub) return Stub;
610
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000611 TargetJITInfo::StubLayout SL = TheJIT->getJITInfo().getStubLayout();
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +0000612 JE.startGVStub(0, SL.Size, SL.Alignment);
Jeffrey Yasskinba78dcd2009-11-07 00:00:10 +0000613 Stub = TheJIT->getJITInfo().emitFunctionStub(0, FnAddr, JE);
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +0000614 JE.finishGVStub();
Evan Chengf6acb342006-07-25 20:40:54 +0000615
David Greene85814ac2010-01-05 01:23:36 +0000616 DEBUG(dbgs() << "JIT: Stub emitted at [" << Stub
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000617 << "] for external function at '" << FnAddr << "'\n");
Chris Lattnered5189d2005-04-18 01:44:27 +0000618 return Stub;
619}
620
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000621unsigned JITResolver::getGOTIndexForAddr(void* addr) {
622 unsigned idx = revGOTMap[addr];
623 if (!idx) {
624 idx = ++nextGOTIndex;
625 revGOTMap[addr] = idx;
David Greene85814ac2010-01-05 01:23:36 +0000626 DEBUG(dbgs() << "JIT: Adding GOT entry " << idx << " for addr ["
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000627 << addr << "]\n");
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000628 }
629 return idx;
630}
Chris Lattnered5189d2005-04-18 01:44:27 +0000631
Chris Lattner60d815a2004-11-20 23:57:07 +0000632/// JITCompilerFn - This function is called when a lazy compilation stub has
633/// been entered. It looks up which function this stub corresponds to, compiles
634/// it if necessary, then returns the resultant function pointer.
635void *JITResolver::JITCompilerFn(void *Stub) {
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000636 JITResolver *JR = StubToResolverMap->getResolverFromStub(Stub);
637 assert(JR && "Unable to find the corresponding JITResolver to the call site");
Eric Christophercb5e2272009-11-12 03:12:18 +0000638
Nicolas Geoffray74056ae2008-10-03 07:27:08 +0000639 Function* F = 0;
640 void* ActualPtr = 0;
Misha Brukman10468d82005-04-21 22:55:34 +0000641
Nicolas Geoffray74056ae2008-10-03 07:27:08 +0000642 {
643 // Only lock for getting the Function. The call getPointerToFunction made
644 // in this function might trigger function materializing, which requires
645 // JIT lock to be unlocked.
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000646 MutexGuard locked(JR->TheJIT->lock);
Reid Spencer79876f52005-07-12 15:51:55 +0000647
Jeffrey Yasskin7d2edad2009-10-19 18:49:59 +0000648 // The address given to us for the stub may not be exactly right, it might
649 // be a little bit after the stub. As such, use upper_bound to find it.
Jay Foad3b422a12011-04-13 12:46:01 +0000650 std::pair<void*, Function*> I =
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000651 JR->state.LookupFunctionFromCallSite(locked, Stub);
Jeffrey Yasskin7d2edad2009-10-19 18:49:59 +0000652 F = I.second;
653 ActualPtr = I.first;
Nicolas Geoffray74056ae2008-10-03 07:27:08 +0000654 }
Chris Lattner60d815a2004-11-20 23:57:07 +0000655
Evan Chenga1a3cc12007-06-30 00:10:37 +0000656 // If we have already code generated the function, just return the address.
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000657 void *Result = JR->TheJIT->getPointerToGlobalIfAvailable(F);
Eric Christophercb5e2272009-11-12 03:12:18 +0000658
Evan Chenga1a3cc12007-06-30 00:10:37 +0000659 if (!Result) {
660 // Otherwise we don't have it, do lazy compilation now.
Eric Christophercb5e2272009-11-12 03:12:18 +0000661
Evan Chenga1a3cc12007-06-30 00:10:37 +0000662 // If lazy compilation is disabled, emit a useful error message and abort.
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000663 if (!JR->TheJIT->isCompilingLazily()) {
Jim Grosbachc91fa6d2011-03-16 01:21:55 +0000664 report_fatal_error("LLVM JIT requested to do lazy compilation of"
665 " function '"
Torok Edwinccb29cd2009-07-11 13:10:19 +0000666 + F->getName() + "' when lazy compiles are disabled!");
Evan Chenga1a3cc12007-06-30 00:10:37 +0000667 }
Eric Christophercb5e2272009-11-12 03:12:18 +0000668
David Greene85814ac2010-01-05 01:23:36 +0000669 DEBUG(dbgs() << "JIT: Lazily resolving function '" << F->getName()
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000670 << "' In stub ptr = " << Stub << " actual ptr = "
671 << ActualPtr << "\n");
Duncan Sandsa41634e2011-08-12 14:54:45 +0000672 (void)ActualPtr;
Chris Lattner60d815a2004-11-20 23:57:07 +0000673
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000674 Result = JR->TheJIT->getPointerToFunction(F);
Evan Chenga1a3cc12007-06-30 00:10:37 +0000675 }
Jeffrey Yasskin7d2edad2009-10-19 18:49:59 +0000676
677 // Reacquire the lock to update the GOT map.
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000678 MutexGuard locked(JR->TheJIT->lock);
Chris Lattner60d815a2004-11-20 23:57:07 +0000679
Jeffrey Yasskin7d2edad2009-10-19 18:49:59 +0000680 // We might like to remove the call site from the CallSiteToFunction map, but
681 // we can't do that! Multiple threads could be stuck, waiting to acquire the
682 // lock above. As soon as the 1st function finishes compiling the function,
683 // the next one will be released, and needs to be able to find the function it
684 // needs to call.
Chris Lattner60d815a2004-11-20 23:57:07 +0000685
686 // FIXME: We could rewrite all references to this stub if we knew them.
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000687
Jeff Cohen546fd592005-07-30 18:33:25 +0000688 // What we will do is set the compiled function address to map to the
689 // same GOT entry as the stub so that later clients may update the GOT
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000690 // if they see it still using the stub address.
691 // Note: this is done so the Resolver doesn't have to manage GOT memory
692 // Do this without allocating map space if the target isn't using a GOT
Jeffrey Yasskine0913882010-02-11 01:07:39 +0000693 if(JR->revGOTMap.find(Stub) != JR->revGOTMap.end())
694 JR->revGOTMap[Result] = JR->revGOTMap[Stub];
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000695
Chris Lattner60d815a2004-11-20 23:57:07 +0000696 return Result;
697}
Chris Lattnerf26be842003-08-14 18:35:27 +0000698
Chris Lattner318d3ef2008-04-04 05:51:42 +0000699//===----------------------------------------------------------------------===//
Chris Lattnerc5753052004-11-22 22:00:25 +0000700// JITEmitter code.
Chris Lattner60d815a2004-11-20 23:57:07 +0000701//
Chris Lattnerc5753052004-11-22 22:00:25 +0000702void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
Jeffrey Yasskindb5f24c2009-11-07 08:51:52 +0000703 bool MayNeedFarStub) {
Nate Begeman18d85e72009-02-18 08:31:02 +0000704 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
Chris Lattner60d815a2004-11-20 23:57:07 +0000705 return TheJIT->getOrEmitGlobalVariable(GV);
Nate Begeman18d85e72009-02-18 08:31:02 +0000706
Chris Lattner58ecfbd2008-06-25 20:21:35 +0000707 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Anton Korobeynikov1a114042008-09-09 20:05:04 +0000708 return TheJIT->getPointerToGlobal(GA->resolveAliasedGlobal(false));
Chris Lattner60d815a2004-11-20 23:57:07 +0000709
710 // If we have already compiled the function, return a pointer to its body.
711 Function *F = cast<Function>(V);
Eric Christophercb5e2272009-11-12 03:12:18 +0000712
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000713 void *FnStub = Resolver.getLazyFunctionStubIfAvailable(F);
Eric Christophercb5e2272009-11-12 03:12:18 +0000714 if (FnStub) {
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000715 // Return the function stub if it's already created. We do this first so
716 // that we're returning the same address for the function as any previous
717 // call. TODO: Yes, this is wrong. The lazy stub isn't guaranteed to be
718 // close enough to call.
Eric Christophercb5e2272009-11-12 03:12:18 +0000719 return FnStub;
Nate Begeman359df742009-03-05 06:34:37 +0000720 }
Eric Christophercb5e2272009-11-12 03:12:18 +0000721
Jeffrey Yasskin34fb6832009-11-19 23:42:58 +0000722 // If we know the target can handle arbitrary-distance calls, try to
723 // return a direct pointer.
724 if (!MayNeedFarStub) {
725 // If we have code, go ahead and return that.
726 void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
727 if (ResultPtr) return ResultPtr;
Chris Lattner60d815a2004-11-20 23:57:07 +0000728
Jeffrey Yasskin34fb6832009-11-19 23:42:58 +0000729 // If this is an external function pointer, we can force the JIT to
730 // 'compile' it, which really just adds it to the map.
Jeffrey Yasskin65234292009-12-22 23:47:23 +0000731 if (isNonGhostDeclaration(F) || F->hasAvailableExternallyLinkage())
Jeffrey Yasskin34fb6832009-11-19 23:42:58 +0000732 return TheJIT->getPointerToFunction(F);
733 }
Chris Lattner50c2e112004-11-22 07:24:43 +0000734
Jeffrey Yasskinc3b7d1e2010-03-04 19:45:09 +0000735 // Otherwise, we may need a to emit a stub, and, conservatively, we always do
736 // so. Note that it's possible to return null from getLazyFunctionStub in the
737 // case of a weak extern that fails to resolve.
738 return Resolver.getLazyFunctionStub(F);
Chris Lattner60d815a2004-11-20 23:57:07 +0000739}
740
Jeffrey Yasskindb5f24c2009-11-07 08:51:52 +0000741void *JITEmitter::getPointerToGVIndirectSym(GlobalValue *V, void *Reference) {
Nate Begeman359df742009-03-05 06:34:37 +0000742 // Make sure GV is emitted first, and create a stub containing the fully
743 // resolved address.
Jeffrey Yasskindb5f24c2009-11-07 08:51:52 +0000744 void *GVAddress = getPointerToGlobal(V, Reference, false);
Nate Begeman359df742009-03-05 06:34:37 +0000745 void *StubAddr = Resolver.getGlobalValueIndirectSym(V, GVAddress);
Nate Begeman359df742009-03-05 06:34:37 +0000746 return StubAddr;
747}
748
Devang Pateleb43b172009-10-06 03:04:58 +0000749void JITEmitter::processDebugLoc(DebugLoc DL, bool BeforePrintingInsn) {
Chris Lattner915c5f92010-04-02 19:42:39 +0000750 if (DL.isUnknown()) return;
751 if (!BeforePrintingInsn) return;
Jim Grosbachc91fa6d2011-03-16 01:21:55 +0000752
Chris Lattner8f3adc92010-07-22 21:17:55 +0000753 const LLVMContext &Context = EmissionDetails.MF->getFunction()->getContext();
Nicolas Geoffray99bfbca2010-04-14 22:06:37 +0000754
755 if (DL.getScope(Context) != 0 && PrevDL != DL) {
Chris Lattner915c5f92010-04-02 19:42:39 +0000756 JITEvent_EmittedFunctionDetails::LineStart NextLine;
757 NextLine.Address = getCurrentPCValue();
758 NextLine.Loc = DL;
759 EmissionDetails.LineStarts.push_back(NextLine);
Jeffrey Yasskinefad8e42009-07-16 21:07:26 +0000760 }
Chris Lattner915c5f92010-04-02 19:42:39 +0000761
Nicolas Geoffray99bfbca2010-04-14 22:06:37 +0000762 PrevDL = DL;
Jeffrey Yasskinefad8e42009-07-16 21:07:26 +0000763}
764
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000765static unsigned GetConstantPoolSizeInBytes(MachineConstantPool *MCP,
766 const TargetData *TD) {
Nicolas Geoffray85a9f192008-04-18 20:59:31 +0000767 const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants();
768 if (Constants.empty()) return 0;
769
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000770 unsigned Size = 0;
771 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
772 MachineConstantPoolEntry CPE = Constants[i];
773 unsigned AlignMask = CPE.getAlignment() - 1;
774 Size = (Size + AlignMask) & ~AlignMask;
Chris Lattner229907c2011-07-18 04:54:35 +0000775 Type *Ty = CPE.getType();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000776 Size += TD->getTypeAllocSize(Ty);
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000777 }
Nicolas Geoffray85a9f192008-04-18 20:59:31 +0000778 return Size;
779}
780
Chris Lattnerc5753052004-11-22 22:00:25 +0000781void JITEmitter::startFunction(MachineFunction &F) {
David Greene85814ac2010-01-05 01:23:36 +0000782 DEBUG(dbgs() << "JIT: Starting CodeGen of Function "
Craig Toppera538d832012-08-22 06:07:19 +0000783 << F.getName() << "\n");
Evan Cheng972fd1a2008-11-06 17:46:04 +0000784
Nicolas Geoffray85a9f192008-04-18 20:59:31 +0000785 uintptr_t ActualSize = 0;
Jim Grosbachb22ef712008-10-03 16:17:20 +0000786 // Set the memory writable, if it's not already
787 MemMgr->setMemoryWritable();
Jim Grosbachc91fa6d2011-03-16 01:21:55 +0000788
Chris Lattner8f3adc92010-07-22 21:17:55 +0000789 if (SizeEstimate > 0) {
Reid Kleckner4b3a3562009-07-23 21:46:56 +0000790 // SizeEstimate will be non-zero on reallocation attempts.
791 ActualSize = SizeEstimate;
Nicolas Geoffray85a9f192008-04-18 20:59:31 +0000792 }
793
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000794 BufferBegin = CurBufferPtr = MemMgr->startFunctionBody(F.getFunction(),
795 ActualSize);
Chris Lattner873ef132006-05-11 23:08:08 +0000796 BufferEnd = BufferBegin+ActualSize;
Jeffrey Yasskin27c66922009-10-20 18:13:21 +0000797 EmittedFunctions[F.getFunction()].FunctionBody = BufferBegin;
798
Evan Chenge03ca9b2006-11-16 20:04:54 +0000799 // Ensure the constant pool/jump table info is at least 4-byte aligned.
800 emitAlignment(16);
801
Chris Lattnerb8065a92006-05-02 23:22:24 +0000802 emitConstantPool(F.getConstantPool());
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000803 if (MachineJumpTableInfo *MJTI = F.getJumpTableInfo())
804 initJumpTableInfo(MJTI);
Chris Lattnerb8065a92006-05-02 23:22:24 +0000805
806 // About to start emitting the machine code for the function.
Chris Lattner37c39b92006-05-03 01:03:20 +0000807 emitAlignment(std::max(F.getFunction()->getAlignment(), 8U));
Chris Lattnerb8065a92006-05-02 23:22:24 +0000808 TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr);
Jeffrey Yasskinbf43f652009-10-27 00:03:05 +0000809 EmittedFunctions[F.getFunction()].Code = CurBufferPtr;
Evan Chengf6acb342006-07-25 20:40:54 +0000810
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000811 MBBLocations.clear();
Jeffrey Yasskinefad8e42009-07-16 21:07:26 +0000812
813 EmissionDetails.MF = &F;
814 EmissionDetails.LineStarts.clear();
Chris Lattner996fe012002-12-24 00:01:05 +0000815}
816
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000817bool JITEmitter::finishFunction(MachineFunction &F) {
Chris Lattner873ef132006-05-11 23:08:08 +0000818 if (CurBufferPtr == BufferEnd) {
Reid Kleckner4b3a3562009-07-23 21:46:56 +0000819 // We must call endFunctionBody before retrying, because
820 // deallocateMemForFunction requires it.
821 MemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr);
822 retryWithMoreMemory(F);
823 return true;
Chris Lattner873ef132006-05-11 23:08:08 +0000824 }
Reid Kleckner4b3a3562009-07-23 21:46:56 +0000825
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000826 if (MachineJumpTableInfo *MJTI = F.getJumpTableInfo())
827 emitJumpTableInfo(MJTI);
Reid Kleckner4b3a3562009-07-23 21:46:56 +0000828
Chris Lattnere7962662006-06-16 18:09:26 +0000829 // FnStart is the start of the text, not the start of the constant pool and
830 // other per-function data.
Bruno Cardoso Lopes8a1be5e2009-06-04 00:15:51 +0000831 uint8_t *FnStart =
832 (uint8_t *)TheJIT->getPointerToGlobalIfAvailable(F.getFunction());
Chris Lattner996fe012002-12-24 00:01:05 +0000833
Argyrios Kyrtzidis30481e22009-04-30 23:01:58 +0000834 // FnEnd is the end of the function's machine code.
Bruno Cardoso Lopes8a1be5e2009-06-04 00:15:51 +0000835 uint8_t *FnEnd = CurBufferPtr;
Argyrios Kyrtzidis30481e22009-04-30 23:01:58 +0000836
Chris Lattner6cf7a432004-11-20 03:46:14 +0000837 if (!Relocations.empty()) {
Nate Begeman359df742009-03-05 06:34:37 +0000838 CurFn = F.getFunction();
Chris Lattner05732c82005-07-20 16:29:20 +0000839 NumRelos += Relocations.size();
840
Chris Lattner6cf7a432004-11-20 03:46:14 +0000841 // Resolve the relocations to concrete pointers.
842 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
843 MachineRelocation &MR = Relocations[i];
Evan Cheng3a9aead2008-11-03 07:14:02 +0000844 void *ResultPtr = 0;
Evan Cheng4421a112008-10-29 23:54:46 +0000845 if (!MR.letTargetResolve()) {
Evan Cheng1b889b12008-11-08 07:37:34 +0000846 if (MR.isExternalSymbol()) {
Dan Gohmand32ec012009-01-05 05:32:42 +0000847 ResultPtr = TheJIT->getPointerToNamedFunction(MR.getExternalSymbol(),
848 false);
David Greene85814ac2010-01-05 01:23:36 +0000849 DEBUG(dbgs() << "JIT: Map \'" << MR.getExternalSymbol() << "\' to ["
Eric Christophercb5e2272009-11-12 03:12:18 +0000850 << ResultPtr << "]\n");
Misha Brukman10468d82005-04-21 22:55:34 +0000851
Evan Cheng4421a112008-10-29 23:54:46 +0000852 // If the target REALLY wants a stub for this function, emit it now.
Jeffrey Yasskindb5f24c2009-11-07 08:51:52 +0000853 if (MR.mayNeedFarStub()) {
Jeffrey Yasskin8483f122009-11-09 22:34:19 +0000854 ResultPtr = Resolver.getExternalFunctionStub(ResultPtr);
Nate Begeman664cf272009-03-11 07:03:43 +0000855 }
Evan Cheng4421a112008-10-29 23:54:46 +0000856 } else if (MR.isGlobalValue()) {
857 ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
858 BufferBegin+MR.getMachineCodeOffset(),
Jeffrey Yasskindb5f24c2009-11-07 08:51:52 +0000859 MR.mayNeedFarStub());
Evan Cheng0d9db402008-11-10 01:52:24 +0000860 } else if (MR.isIndirectSymbol()) {
Jeffrey Yasskindb5f24c2009-11-07 08:51:52 +0000861 ResultPtr = getPointerToGVIndirectSym(
862 MR.getGlobalValue(), BufferBegin+MR.getMachineCodeOffset());
Evan Cheng4421a112008-10-29 23:54:46 +0000863 } else if (MR.isBasicBlock()) {
864 ResultPtr = (void*)getMachineBasicBlockAddress(MR.getBasicBlock());
865 } else if (MR.isConstantPoolIndex()) {
Jim Grosbachc91fa6d2011-03-16 01:21:55 +0000866 ResultPtr =
867 (void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex());
Evan Cheng4421a112008-10-29 23:54:46 +0000868 } else {
869 assert(MR.isJumpTableIndex());
870 ResultPtr=(void*)getJumpTableEntryAddress(MR.getJumpTableIndex());
871 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000872
Evan Cheng4421a112008-10-29 23:54:46 +0000873 MR.setResultPointer(ResultPtr);
874 }
Andrew Lenharth940b07c2005-07-22 20:48:12 +0000875
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000876 // if we are managing the GOT and the relocation wants an index,
877 // give it one
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000878 if (MR.isGOTRelative() && MemMgr->isManagingGOT()) {
Chris Lattner05858a92007-02-24 02:57:03 +0000879 unsigned idx = Resolver.getGOTIndexForAddr(ResultPtr);
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000880 MR.setGOTIndex(idx);
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000881 if (((void**)MemMgr->getGOTBase())[idx] != ResultPtr) {
David Greene85814ac2010-01-05 01:23:36 +0000882 DEBUG(dbgs() << "JIT: GOT was out of date for " << ResultPtr
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000883 << " pointing at " << ((void**)MemMgr->getGOTBase())[idx]
884 << "\n");
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000885 ((void**)MemMgr->getGOTBase())[idx] = ResultPtr;
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000886 }
Andrew Lenharth940b07c2005-07-22 20:48:12 +0000887 }
Chris Lattner6cf7a432004-11-20 03:46:14 +0000888 }
889
Nate Begeman359df742009-03-05 06:34:37 +0000890 CurFn = 0;
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000891 TheJIT->getJITInfo().relocate(BufferBegin, &Relocations[0],
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000892 Relocations.size(), MemMgr->getGOTBase());
Chris Lattner6cf7a432004-11-20 03:46:14 +0000893 }
894
Chris Lattner005d7172006-05-03 18:55:56 +0000895 // Update the GOT entry for F to point to the new code.
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000896 if (MemMgr->isManagingGOT()) {
Chris Lattner05858a92007-02-24 02:57:03 +0000897 unsigned idx = Resolver.getGOTIndexForAddr((void*)BufferBegin);
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000898 if (((void**)MemMgr->getGOTBase())[idx] != (void*)BufferBegin) {
David Greene85814ac2010-01-05 01:23:36 +0000899 DEBUG(dbgs() << "JIT: GOT was out of date for " << (void*)BufferBegin
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000900 << " pointing at " << ((void**)MemMgr->getGOTBase())[idx]
901 << "\n");
Chris Lattner55d8c3f2007-12-05 23:39:57 +0000902 ((void**)MemMgr->getGOTBase())[idx] = (void*)BufferBegin;
Andrew Lenharth3444cf52005-07-28 12:44:13 +0000903 }
904 }
905
Argyrios Kyrtzidis30481e22009-04-30 23:01:58 +0000906 // CurBufferPtr may have moved beyond FnEnd, due to memory allocation for
907 // global variables that were referenced in the relocations.
908 MemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr);
Evan Cheng0b773192008-12-10 02:32:19 +0000909
910 if (CurBufferPtr == BufferEnd) {
Reid Kleckner4b3a3562009-07-23 21:46:56 +0000911 retryWithMoreMemory(F);
912 return true;
913 } else {
914 // Now that we've succeeded in emitting the function, reset the
915 // SizeEstimate back down to zero.
916 SizeEstimate = 0;
Evan Cheng0b773192008-12-10 02:32:19 +0000917 }
918
Nuno Lopes94844e22008-10-21 11:42:16 +0000919 BufferBegin = CurBufferPtr = 0;
920 NumBytes += FnEnd-FnStart;
921
Evan Chengf6acb342006-07-25 20:40:54 +0000922 // Invalidate the icache if necessary.
Chris Lattnerd3406fc2008-06-25 17:18:44 +0000923 sys::Memory::InvalidateInstructionCache(FnStart, FnEnd-FnStart);
Jeffrey Yasskin0b08f3d2009-06-25 02:04:04 +0000924
Jeffrey Yasskin0b08f3d2009-06-25 02:04:04 +0000925 TheJIT->NotifyFunctionEmitted(*F.getFunction(), FnStart, FnEnd-FnStart,
Jeffrey Yasskinefad8e42009-07-16 21:07:26 +0000926 EmissionDetails);
Evan Chengf6acb342006-07-25 20:40:54 +0000927
Nicolas Geoffray99bfbca2010-04-14 22:06:37 +0000928 // Reset the previous debug location.
929 PrevDL = DebugLoc();
930
David Greene85814ac2010-01-05 01:23:36 +0000931 DEBUG(dbgs() << "JIT: Finished CodeGen of [" << (void*)FnStart
Craig Toppera538d832012-08-22 06:07:19 +0000932 << "] Function: " << F.getName()
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000933 << ": " << (FnEnd-FnStart) << " bytes of text, "
934 << Relocations.size() << " relocations\n");
Argyrios Kyrtzidisc65c5252009-05-18 21:06:40 +0000935
Chris Lattner6cf7a432004-11-20 03:46:14 +0000936 Relocations.clear();
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000937 ConstPoolAddresses.clear();
Anton Korobeynikov3e956972007-01-19 17:25:17 +0000938
Evan Cheng5cc53c32008-09-18 07:54:21 +0000939 // Mark code region readable and executable if it's not so already.
Jim Grosbachb22ef712008-10-03 16:17:20 +0000940 MemMgr->setMemoryExecutable();
Evan Cheng5cc53c32008-09-18 07:54:21 +0000941
Bill Wendling47d74202010-04-18 00:52:08 +0000942 DEBUG({
943 if (sys::hasDisassembler()) {
944 dbgs() << "JIT: Disassembled code:\n";
945 dbgs() << sys::disassembleBuffer(FnStart, FnEnd-FnStart,
946 (uintptr_t)FnStart);
947 } else {
948 dbgs() << "JIT: Binary code:\n";
949 uint8_t* q = FnStart;
950 for (int i = 0; q < FnEnd; q += 4, ++i) {
951 if (i == 4)
952 i = 0;
953 if (i == 0)
954 dbgs() << "JIT: " << (long)(q - FnStart) << ": ";
955 bool Done = false;
956 for (int j = 3; j >= 0; --j) {
957 if (q + j >= FnEnd)
958 Done = true;
959 else
960 dbgs() << (unsigned short)q[j];
961 }
962 if (Done)
963 break;
964 dbgs() << ' ';
965 if (i == 3)
966 dbgs() << '\n';
Evan Chenge4df8752008-11-12 08:22:43 +0000967 }
Bill Wendling47d74202010-04-18 00:52:08 +0000968 dbgs()<< '\n';
Evan Chengd1c5c7f2008-11-05 23:44:08 +0000969 }
Bill Wendling47d74202010-04-18 00:52:08 +0000970 });
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000971
Rafael Espindolaafcf5712012-01-05 22:07:43 +0000972 if (JITExceptionHandling) {
Nicolas Geoffray85a9f192008-04-18 20:59:31 +0000973 uintptr_t ActualSize = 0;
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +0000974 SavedBufferBegin = BufferBegin;
975 SavedBufferEnd = BufferEnd;
976 SavedCurBufferPtr = CurBufferPtr;
Reid Kleckner9a10db82009-09-20 23:52:43 +0000977
Nicolas Geoffray21ad4942008-02-13 18:39:37 +0000978 BufferBegin = CurBufferPtr = MemMgr->startExceptionTable(F.getFunction(),
979 ActualSize);
980 BufferEnd = BufferBegin+ActualSize;
Jeffrey Yasskin27c66922009-10-20 18:13:21 +0000981 EmittedFunctions[F.getFunction()].ExceptionTable = BufferBegin;
Reid Kleckner9a10db82009-09-20 23:52:43 +0000982 uint8_t *EhStart;
983 uint8_t *FrameRegister = DE->EmitDwarfTable(F, *this, FnStart, FnEnd,
984 EhStart);
Chris Lattnerc1969de2008-03-07 20:05:43 +0000985 MemMgr->endExceptionTable(F.getFunction(), BufferBegin, CurBufferPtr,
986 FrameRegister);
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +0000987 BufferBegin = SavedBufferBegin;
988 BufferEnd = SavedBufferEnd;
989 CurBufferPtr = SavedCurBufferPtr;
Nicolas Geoffray21ad4942008-02-13 18:39:37 +0000990
Duncan Sands211427b2010-05-02 15:36:26 +0000991 if (JITExceptionHandling) {
Eric Christopherf045b7a2011-03-04 23:37:39 +0000992 TheJIT->RegisterTable(F.getFunction(), FrameRegister);
Reid Kleckner9a10db82009-09-20 23:52:43 +0000993 }
Nicolas Geoffray21ad4942008-02-13 18:39:37 +0000994 }
Evan Cheng52b18122008-09-02 08:14:01 +0000995
996 if (MMI)
997 MMI->EndFunction();
Eric Christophercb5e2272009-11-12 03:12:18 +0000998
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000999 return false;
Chris Lattner996fe012002-12-24 00:01:05 +00001000}
1001
Reid Kleckner4b3a3562009-07-23 21:46:56 +00001002void JITEmitter::retryWithMoreMemory(MachineFunction &F) {
David Greene85814ac2010-01-05 01:23:36 +00001003 DEBUG(dbgs() << "JIT: Ran out of space for native code. Reattempting.\n");
Reid Kleckner4b3a3562009-07-23 21:46:56 +00001004 Relocations.clear(); // Clear the old relocations or we'll reapply them.
1005 ConstPoolAddresses.clear();
1006 ++NumRetries;
1007 deallocateMemForFunction(F.getFunction());
1008 // Try again with at least twice as much free space.
1009 SizeEstimate = (uintptr_t)(2 * (BufferEnd - BufferBegin));
Chris Lattnerb6df00c2010-07-11 23:07:28 +00001010
1011 for (MachineFunction::iterator MBB = F.begin(), E = F.end(); MBB != E; ++MBB){
1012 if (MBB->hasAddressTaken())
1013 TheJIT->clearPointerToBasicBlock(MBB->getBasicBlock());
1014 }
Reid Kleckner4b3a3562009-07-23 21:46:56 +00001015}
1016
Nate Begeman359df742009-03-05 06:34:37 +00001017/// deallocateMemForFunction - Deallocate all memory for the specified
1018/// function body. Also drop any references the function has to stubs.
Jeffrey Yasskinbf43f652009-10-27 00:03:05 +00001019/// May be called while the Function is being destroyed inside ~Value().
Reid Kleckner4b3a3562009-07-23 21:46:56 +00001020void JITEmitter::deallocateMemForFunction(const Function *F) {
Jeffrey Yasskinbf43f652009-10-27 00:03:05 +00001021 ValueMap<const Function *, EmittedCode, EmittedFunctionConfig>::iterator
1022 Emitted = EmittedFunctions.find(F);
Jeffrey Yasskin27c66922009-10-20 18:13:21 +00001023 if (Emitted != EmittedFunctions.end()) {
1024 MemMgr->deallocateFunctionBody(Emitted->second.FunctionBody);
1025 MemMgr->deallocateExceptionTable(Emitted->second.ExceptionTable);
Jeffrey Yasskinbf43f652009-10-27 00:03:05 +00001026 TheJIT->NotifyFreeingMachineCode(Emitted->second.Code);
1027
Jeffrey Yasskin27c66922009-10-20 18:13:21 +00001028 EmittedFunctions.erase(Emitted);
1029 }
Nate Begeman359df742009-03-05 06:34:37 +00001030
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001031 if (JITExceptionHandling) {
Eric Christopherf045b7a2011-03-04 23:37:39 +00001032 TheJIT->DeregisterTable(F);
1033 }
Nate Begeman359df742009-03-05 06:34:37 +00001034}
1035
1036
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001037void *JITEmitter::allocateSpace(uintptr_t Size, unsigned Alignment) {
Nuno Lopes94844e22008-10-21 11:42:16 +00001038 if (BufferBegin)
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +00001039 return JITCodeEmitter::allocateSpace(Size, Alignment);
Nuno Lopes94844e22008-10-21 11:42:16 +00001040
1041 // create a new memory block if there is no active one.
1042 // care must be taken so that BufferBegin is invalidated when a
1043 // block is trimmed
1044 BufferBegin = CurBufferPtr = MemMgr->allocateSpace(Size, Alignment);
1045 BufferEnd = BufferBegin+Size;
1046 return CurBufferPtr;
1047}
1048
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001049void *JITEmitter::allocateGlobal(uintptr_t Size, unsigned Alignment) {
Jeffrey Yasskin70415d92009-07-08 21:59:57 +00001050 // Delegate this call through the memory manager.
1051 return MemMgr->allocateGlobal(Size, Alignment);
1052}
1053
Chris Lattnerc5753052004-11-22 22:00:25 +00001054void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
Evan Cheng00203152008-11-07 09:02:17 +00001055 if (TheJIT->getJITInfo().hasCustomConstantPool())
Jim Grosbach25896042008-10-30 23:44:39 +00001056 return;
Evan Cheng00203152008-11-07 09:02:17 +00001057
Chris Lattnerba972642006-02-09 04:22:52 +00001058 const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants();
Chris Lattner98e72b42003-11-30 04:23:21 +00001059 if (Constants.empty()) return;
1060
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001061 unsigned Size = GetConstantPoolSizeInBytes(MCP, TheJIT->getTargetData());
1062 unsigned Align = MCP->getConstantPoolAlignment();
Evan Cheng213ea6b2008-04-12 00:22:01 +00001063 ConstantPoolBase = allocateSpace(Size, Align);
Chris Lattner729ffe92006-02-09 04:49:59 +00001064 ConstantPool = MCP;
Chris Lattnerb8065a92006-05-02 23:22:24 +00001065
1066 if (ConstantPoolBase == 0) return; // Buffer overflow.
1067
David Greene85814ac2010-01-05 01:23:36 +00001068 DEBUG(dbgs() << "JIT: Emitted constant pool at [" << ConstantPoolBase
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001069 << "] (size: " << Size << ", alignment: " << Align << ")\n");
Evan Cheng213ea6b2008-04-12 00:22:01 +00001070
Chris Lattner729ffe92006-02-09 04:49:59 +00001071 // Initialize the memory for all of the constant pool entries.
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001072 unsigned Offset = 0;
Chris Lattnerf6190822006-02-09 04:46:04 +00001073 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001074 MachineConstantPoolEntry CPE = Constants[i];
1075 unsigned AlignMask = CPE.getAlignment() - 1;
1076 Offset = (Offset + AlignMask) & ~AlignMask;
1077
1078 uintptr_t CAddr = (uintptr_t)ConstantPoolBase + Offset;
1079 ConstPoolAddresses.push_back(CAddr);
1080 if (CPE.isMachineConstantPoolEntry()) {
Evan Cheng3228e752006-09-12 20:59:59 +00001081 // FIXME: add support to lower machine constant pool values into bytes!
Chris Lattner2104b8d2010-04-07 22:58:41 +00001082 report_fatal_error("Initialize memory with machine specific constant pool"
Torok Edwinccb29cd2009-07-11 13:10:19 +00001083 "entry has not been implemented!");
Evan Cheng3228e752006-09-12 20:59:59 +00001084 }
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001085 TheJIT->InitializeMemory(CPE.Val.ConstVal, (void*)CAddr);
David Greene85814ac2010-01-05 01:23:36 +00001086 DEBUG(dbgs() << "JIT: CP" << i << " at [0x";
1087 dbgs().write_hex(CAddr) << "]\n");
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001088
Chris Lattner229907c2011-07-18 04:54:35 +00001089 Type *Ty = CPE.Val.ConstVal->getType();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001090 Offset += TheJIT->getTargetData()->getTypeAllocSize(Ty);
Chris Lattner4ba3bbb2003-01-13 01:00:12 +00001091 }
1092}
1093
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001094void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) {
Evan Cheng00203152008-11-07 09:02:17 +00001095 if (TheJIT->getJITInfo().hasCustomJumpTables())
1096 return;
Richard Osborne6d3e92d2010-03-11 14:58:16 +00001097 if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_Inline)
1098 return;
Evan Cheng00203152008-11-07 09:02:17 +00001099
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001100 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1101 if (JT.empty()) return;
Eric Christophercb5e2272009-11-12 03:12:18 +00001102
Chris Lattnerb8065a92006-05-02 23:22:24 +00001103 unsigned NumEntries = 0;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001104 for (unsigned i = 0, e = JT.size(); i != e; ++i)
Chris Lattnerb8065a92006-05-02 23:22:24 +00001105 NumEntries += JT[i].MBBs.size();
1106
Chris Lattnerb6db2c62010-01-25 23:26:13 +00001107 unsigned EntrySize = MJTI->getEntrySize(*TheJIT->getTargetData());
Chris Lattnerb8065a92006-05-02 23:22:24 +00001108
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001109 // Just allocate space for all the jump tables now. We will fix up the actual
1110 // MBB entries in the tables after we emit the code for each block, since then
1111 // we will know the final locations of the MBBs in memory.
1112 JumpTable = MJTI;
Chris Lattnerb6db2c62010-01-25 23:26:13 +00001113 JumpTableBase = allocateSpace(NumEntries * EntrySize,
1114 MJTI->getEntryAlignment(*TheJIT->getTargetData()));
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001115}
1116
Jim Laskey39589552006-12-14 22:53:42 +00001117void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) {
Evan Cheng00203152008-11-07 09:02:17 +00001118 if (TheJIT->getJITInfo().hasCustomJumpTables())
1119 return;
1120
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001121 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
Chris Lattnerb8065a92006-05-02 23:22:24 +00001122 if (JT.empty() || JumpTableBase == 0) return;
Eric Christophercb5e2272009-11-12 03:12:18 +00001123
Jim Grosbachc91fa6d2011-03-16 01:21:55 +00001124
Chris Lattner7a260702010-01-26 03:47:15 +00001125 switch (MJTI->getEntryKind()) {
Richard Osborne6d3e92d2010-03-11 14:58:16 +00001126 case MachineJumpTableInfo::EK_Inline:
1127 return;
Chris Lattner7a260702010-01-26 03:47:15 +00001128 case MachineJumpTableInfo::EK_BlockAddress: {
1129 // EK_BlockAddress - Each entry is a plain address of block, e.g.:
1130 // .word LBB123
1131 assert(MJTI->getEntrySize(*TheJIT->getTargetData()) == sizeof(void*) &&
1132 "Cross JIT'ing?");
Jim Grosbachc91fa6d2011-03-16 01:21:55 +00001133
Chris Lattner7a260702010-01-26 03:47:15 +00001134 // For each jump table, map each target in the jump table to the address of
1135 // an emitted MachineBasicBlock.
1136 intptr_t *SlotPtr = (intptr_t*)JumpTableBase;
Jim Grosbachc91fa6d2011-03-16 01:21:55 +00001137
Chris Lattner7a260702010-01-26 03:47:15 +00001138 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
1139 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
1140 // Store the address of the basic block for this jump table slot in the
1141 // memory we allocated for the jump table in 'initJumpTableInfo'
1142 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi)
1143 *SlotPtr++ = getMachineBasicBlockAddress(MBBs[mi]);
1144 }
1145 break;
1146 }
Jim Grosbachc91fa6d2011-03-16 01:21:55 +00001147
Chris Lattner5fc41602010-01-26 04:05:28 +00001148 case MachineJumpTableInfo::EK_Custom32:
Chris Lattner7a260702010-01-26 03:47:15 +00001149 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
1150 case MachineJumpTableInfo::EK_LabelDifference32: {
Chris Lattnerb6db2c62010-01-25 23:26:13 +00001151 assert(MJTI->getEntrySize(*TheJIT->getTargetData()) == 4&&"Cross JIT'ing?");
Jim Laskey70323a82006-12-14 19:17:33 +00001152 // For each jump table, place the offset from the beginning of the table
1153 // to the target address.
1154 int *SlotPtr = (int*)JumpTableBase;
Chris Lattner0574e472006-05-03 00:13:06 +00001155
Jim Laskey70323a82006-12-14 19:17:33 +00001156 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
1157 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
1158 // Store the offset of the basic block for this jump table slot in the
1159 // memory we allocated for the jump table in 'initJumpTableInfo'
Evan Cheng0b773192008-12-10 02:32:19 +00001160 uintptr_t Base = (uintptr_t)SlotPtr;
Evan Cheng880b0802008-01-05 02:26:58 +00001161 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
Evan Cheng0b773192008-12-10 02:32:19 +00001162 uintptr_t MBBAddr = getMachineBasicBlockAddress(MBBs[mi]);
Chris Lattner7a260702010-01-26 03:47:15 +00001163 /// FIXME: USe EntryKind instead of magic "getPICJumpTableEntry" hook.
Evan Cheng880b0802008-01-05 02:26:58 +00001164 *SlotPtr++ = TheJIT->getJITInfo().getPICJumpTableEntry(MBBAddr, Base);
1165 }
Jim Laskey70323a82006-12-14 19:17:33 +00001166 }
Chris Lattner7a260702010-01-26 03:47:15 +00001167 break;
1168 }
Akira Hatanakaf0b08442012-02-03 04:33:00 +00001169 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
Craig Toppera2886c22012-02-07 05:05:23 +00001170 llvm_unreachable(
Akira Hatanakaf0b08442012-02-03 04:33:00 +00001171 "JT Info emission not implemented for GPRel64BlockAddress yet.");
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001172 }
1173}
1174
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +00001175void JITEmitter::startGVStub(const GlobalValue* GV,
Jeffrey Yasskin19b48372009-11-23 22:49:00 +00001176 unsigned StubSize, unsigned Alignment) {
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +00001177 SavedBufferBegin = BufferBegin;
1178 SavedBufferEnd = BufferEnd;
1179 SavedCurBufferPtr = CurBufferPtr;
Eric Christophercb5e2272009-11-12 03:12:18 +00001180
Evan Chengb31a7172008-11-08 08:02:53 +00001181 BufferBegin = CurBufferPtr = MemMgr->allocateStub(GV, StubSize, Alignment);
Chris Lattnerc9aa3712006-05-02 18:27:26 +00001182 BufferEnd = BufferBegin+StubSize+1;
Chris Lattnerfdbd98b2003-05-09 03:30:07 +00001183}
1184
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +00001185void JITEmitter::startGVStub(void *Buffer, unsigned StubSize) {
1186 SavedBufferBegin = BufferBegin;
1187 SavedBufferEnd = BufferEnd;
1188 SavedCurBufferPtr = CurBufferPtr;
Eric Christophercb5e2272009-11-12 03:12:18 +00001189
Bruno Cardoso Lopes8a1be5e2009-06-04 00:15:51 +00001190 BufferBegin = CurBufferPtr = (uint8_t *)Buffer;
Nate Begeman18d85e72009-02-18 08:31:02 +00001191 BufferEnd = BufferBegin+StubSize+1;
1192}
1193
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +00001194void JITEmitter::finishGVStub() {
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +00001195 assert(CurBufferPtr != BufferEnd && "Stub overflowed allocated space.");
Chris Lattnerc9aa3712006-05-02 18:27:26 +00001196 NumBytes += getCurrentPCOffset();
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +00001197 BufferBegin = SavedBufferBegin;
1198 BufferEnd = SavedBufferEnd;
1199 CurBufferPtr = SavedCurBufferPtr;
1200}
1201
1202void *JITEmitter::allocIndirectGV(const GlobalValue *GV,
1203 const uint8_t *Buffer, size_t Size,
1204 unsigned Alignment) {
1205 uint8_t *IndGV = MemMgr->allocateStub(GV, Size, Alignment);
1206 memcpy(IndGV, Buffer, Size);
1207 return IndGV;
Chris Lattner6b689e32003-06-01 23:24:36 +00001208}
1209
Chris Lattner6b689e32003-06-01 23:24:36 +00001210// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
1211// in the constant pool that was last emitted with the 'emitConstantPool'
1212// method.
1213//
Evan Cheng0b773192008-12-10 02:32:19 +00001214uintptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const {
Chris Lattner729ffe92006-02-09 04:49:59 +00001215 assert(ConstantNum < ConstantPool->getConstants().size() &&
Misha Brukman5191b4b2005-04-22 04:08:30 +00001216 "Invalid ConstantPoolIndex!");
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001217 return ConstPoolAddresses[ConstantNum];
Chris Lattner6b689e32003-06-01 23:24:36 +00001218}
1219
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001220// getJumpTableEntryAddress - Return the address of the JumpTable with index
1221// 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo'
1222//
Evan Cheng0b773192008-12-10 02:32:19 +00001223uintptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const {
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001224 const std::vector<MachineJumpTableEntry> &JT = JumpTable->getJumpTables();
1225 assert(Index < JT.size() && "Invalid jump table index!");
Eric Christophercb5e2272009-11-12 03:12:18 +00001226
Chris Lattnerb6db2c62010-01-25 23:26:13 +00001227 unsigned EntrySize = JumpTable->getEntrySize(*TheJIT->getTargetData());
Eric Christophercb5e2272009-11-12 03:12:18 +00001228
Chris Lattnerb6db2c62010-01-25 23:26:13 +00001229 unsigned Offset = 0;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001230 for (unsigned i = 0; i < Index; ++i)
Jim Laskey70323a82006-12-14 19:17:33 +00001231 Offset += JT[i].MBBs.size();
Eric Christophercb5e2272009-11-12 03:12:18 +00001232
Jim Laskey70323a82006-12-14 19:17:33 +00001233 Offset *= EntrySize;
Eric Christophercb5e2272009-11-12 03:12:18 +00001234
Evan Cheng0b773192008-12-10 02:32:19 +00001235 return (uintptr_t)((char *)JumpTableBase + Offset);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001236}
1237
Jeffrey Yasskinbf43f652009-10-27 00:03:05 +00001238void JITEmitter::EmittedFunctionConfig::onDelete(
1239 JITEmitter *Emitter, const Function *F) {
1240 Emitter->deallocateMemForFunction(F);
1241}
1242void JITEmitter::EmittedFunctionConfig::onRAUW(
1243 JITEmitter *, const Function*, const Function*) {
1244 llvm_unreachable("The JIT doesn't know how to handle a"
1245 " RAUW on a value it has emitted.");
1246}
1247
1248
Chris Lattner873ef132006-05-11 23:08:08 +00001249//===----------------------------------------------------------------------===//
1250// Public interface to this file
1251//===----------------------------------------------------------------------===//
1252
Reid Kleckner9a10db82009-09-20 23:52:43 +00001253JITCodeEmitter *JIT::createEmitter(JIT &jit, JITMemoryManager *JMM,
1254 TargetMachine &tm) {
1255 return new JITEmitter(jit, JMM, tm);
Chris Lattner873ef132006-05-11 23:08:08 +00001256}
1257
Chris Lattner873ef132006-05-11 23:08:08 +00001258// getPointerToFunctionOrStub - If the specified function has been
1259// code-gen'd, return a pointer to the function. If not, compile it, or use
1260// a stub to implement lazy compilation if available.
1261//
1262void *JIT::getPointerToFunctionOrStub(Function *F) {
1263 // If we have already code generated the function, just return the address.
1264 if (void *Addr = getPointerToGlobalIfAvailable(F))
1265 return Addr;
Eric Christophercb5e2272009-11-12 03:12:18 +00001266
Chris Lattner05858a92007-02-24 02:57:03 +00001267 // Get a stub if the target supports it.
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +00001268 assert(isa<JITEmitter>(JCE) && "Unexpected MCE?");
Evan Chengd5834e92008-08-20 00:28:12 +00001269 JITEmitter *JE = cast<JITEmitter>(getCodeEmitter());
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +00001270 return JE->getJITResolver().getLazyFunctionStub(F);
Chris Lattner873ef132006-05-11 23:08:08 +00001271}
1272
Nate Begeman18d85e72009-02-18 08:31:02 +00001273void JIT::updateFunctionStub(Function *F) {
1274 // Get the empty stub we generated earlier.
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +00001275 assert(isa<JITEmitter>(JCE) && "Unexpected MCE?");
Nate Begeman18d85e72009-02-18 08:31:02 +00001276 JITEmitter *JE = cast<JITEmitter>(getCodeEmitter());
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +00001277 void *Stub = JE->getJITResolver().getLazyFunctionStub(F);
1278 void *Addr = getPointerToGlobalIfAvailable(F);
Jeffrey Yasskin2b73a4e2009-12-17 21:35:29 +00001279 assert(Addr != Stub && "Function must have non-stub address to be updated.");
Nate Begeman18d85e72009-02-18 08:31:02 +00001280
1281 // Tell the target jit info to rewrite the stub at the specified address,
1282 // rather than creating a new one.
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +00001283 TargetJITInfo::StubLayout layout = getJITInfo().getStubLayout();
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +00001284 JE->startGVStub(Stub, layout.Size);
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +00001285 getJITInfo().emitFunctionStub(F, Addr, *getCodeEmitter());
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +00001286 JE->finishGVStub();
Nate Begeman18d85e72009-02-18 08:31:02 +00001287}
1288
Chris Lattner873ef132006-05-11 23:08:08 +00001289/// freeMachineCodeForFunction - release machine code memory for given Function.
1290///
1291void JIT::freeMachineCodeForFunction(Function *F) {
1292 // Delete translation for this from the ExecutionEngine, so it will get
1293 // retranslated next time it is used.
Jeffrey Yasskinbf43f652009-10-27 00:03:05 +00001294 updateGlobalMapping(F, 0);
Chris Lattner873ef132006-05-11 23:08:08 +00001295
1296 // Free the actual memory for the function body and related stuff.
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +00001297 assert(isa<JITEmitter>(JCE) && "Unexpected MCE?");
1298 cast<JITEmitter>(JCE)->deallocateMemForFunction(F);
Chris Lattner873ef132006-05-11 23:08:08 +00001299}