blob: 3bf6db8ee8fe752e4c23d9a16d4b47ff1d2dffc7 [file] [log] [blame]
Chris Lattner166f2262004-11-22 22:00:25 +00001//===-- JITEmitter.cpp - Write machine code to executable memory ----------===//
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 Lattner5be478f2004-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 Lattnerbd199fb2002-12-24 00:01:05 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner3785fad2003-08-05 17:00:32 +000015#define DEBUG_TYPE "jit"
Chris Lattner4d326fa2003-12-20 01:46:27 +000016#include "JIT.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000017#include "JITDwarfEmitter.h"
Reid Kleckner27632172009-09-20 23:52:43 +000018#include "llvm/ADT/OwningPtr.h"
Dale Johannesendd947ea2008-08-07 01:30:15 +000019#include "llvm/Constants.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000020#include "llvm/DebugInfo.h"
Dale Johannesendd947ea2008-08-07 01:30:15 +000021#include "llvm/DerivedTypes.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000022#include "llvm/Module.h"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000023#include "llvm/CodeGen/JITCodeEmitter.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000024#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling47639fc2010-04-16 08:46:10 +000025#include "llvm/CodeGen/MachineCodeInfo.h"
Chris Lattner1cc08382003-01-13 01:00:12 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman37efe672006-04-22 18:53:45 +000027#include "llvm/CodeGen/MachineJumpTableInfo.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000028#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner5be478f2004-11-20 03:46:14 +000029#include "llvm/CodeGen/MachineRelocation.h"
Dale Johannesendd947ea2008-08-07 01:30:15 +000030#include "llvm/ExecutionEngine/GenericValue.h"
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +000031#include "llvm/ExecutionEngine/JITEventListener.h"
32#include "llvm/ExecutionEngine/JITMemoryManager.h"
Chris Lattner1cc08382003-01-13 01:00:12 +000033#include "llvm/Target/TargetData.h"
Bill Wendling47639fc2010-04-16 08:46:10 +000034#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner5be478f2004-11-20 03:46:14 +000035#include "llvm/Target/TargetJITInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000036#include "llvm/Target/TargetMachine.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000037#include "llvm/Target/TargetOptions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000038#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000039#include "llvm/Support/ErrorHandling.h"
Jeffrey Yasskin40966a72010-02-11 01:07:39 +000040#include "llvm/Support/ManagedStatic.h"
Chris Lattnere7fd5532006-05-08 22:00:52 +000041#include "llvm/Support/MutexGuard.h"
Nick Lewycky848b3142009-04-19 18:32:03 +000042#include "llvm/Support/ValueHandle.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000043#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000044#include "llvm/Support/Disassembler.h"
45#include "llvm/Support/Memory.h"
Jeffrey Yasskin1e861322009-10-20 18:13:21 +000046#include "llvm/ADT/DenseMap.h"
Evan Cheng47c01a02008-11-07 09:02:17 +000047#include "llvm/ADT/SmallPtrSet.h"
Nate Begemand6b7a242009-02-18 08:31:02 +000048#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000049#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +000050#include "llvm/ADT/ValueMap.h"
Andrew Lenhartha00269b2005-07-29 23:40:16 +000051#include <algorithm>
Evan Chenga7916f52008-11-05 23:44:08 +000052#ifndef NDEBUG
53#include <iomanip>
54#endif
Chris Lattnerc19aade2003-12-08 08:06:28 +000055using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000056
Chris Lattner36343732006-12-19 22:43:32 +000057STATISTIC(NumBytes, "Number of bytes of machine code compiled");
58STATISTIC(NumRelos, "Number of relocations applied");
Reid Kleckner10b4fc52009-07-23 21:46:56 +000059STATISTIC(NumRetries, "Number of retries with more memory");
Chris Lattner54266522004-11-20 23:57:07 +000060
Andrew Lenhartha00269b2005-07-29 23:40:16 +000061
Jeffrey Yasskinc5818fb2009-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 Yasskinf0356fe2010-01-27 20:34:15 +000065 return F->isDeclaration() && !F->isMaterializable();
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +000066}
67
Chris Lattner54266522004-11-20 23:57:07 +000068//===----------------------------------------------------------------------===//
69// JIT lazy compilation code.
70//
71namespace {
Jeffrey Yasskine637c192009-11-07 00:00:10 +000072 class JITEmitter;
Jeffrey Yasskin23e5fcf2009-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 Topper85814382012-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 Yasskin23e5fcf2009-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 Spenceree448632005-07-12 15:51:55 +000089 class JITResolverState {
Nick Lewyckye2bcf132009-04-27 05:09:44 +000090 public:
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +000091 typedef ValueMap<Function*, void*, NoRAUWValueMapConfig<Function*> >
Jeffrey Yasskin108c8382009-11-23 23:35:19 +000092 FunctionToLazyStubMapTy;
Jeffrey Yasskinebbcef92009-10-19 18:49:59 +000093 typedef std::map<void*, AssertingVH<Function> > CallSiteToFunctionMapTy;
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +000094 typedef ValueMap<Function *, SmallPtrSet<void*, 1>,
95 CallSiteValueMapConfig> FunctionToCallSitesMapTy;
Nick Lewyckye2bcf132009-04-27 05:09:44 +000096 typedef std::map<AssertingVH<GlobalValue>, void*> GlobalToIndirectSymMapTy;
Reid Spenceree448632005-07-12 15:51:55 +000097 private:
Jeffrey Yasskin108c8382009-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 Spenceree448632005-07-12 15:51:55 +0000101
Jeffrey Yasskinebbcef92009-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 Cohen00b168892005-07-27 06:12:32 +0000106
Evan Cheng5594f122008-11-10 01:52:24 +0000107 /// GlobalToIndirectSymMap - Keep track of the indirect symbol created for a
Evan Chengbe8c03f2008-01-04 10:46:51 +0000108 /// particular GlobalVariable so that we can reuse them if necessary.
Nick Lewyckye2bcf132009-04-27 05:09:44 +0000109 GlobalToIndirectSymMapTy GlobalToIndirectSymMap;
Evan Chengbe8c03f2008-01-04 10:46:51 +0000110
Benjamin Kramere04690e2012-06-16 21:55:52 +0000111#ifndef NDEBUG
Jeffrey Yasskin40966a72010-02-11 01:07:39 +0000112 /// Instance of the JIT this ResolverState serves.
113 JIT *TheJIT;
Benjamin Kramere04690e2012-06-16 21:55:52 +0000114#endif
Jeffrey Yasskin40966a72010-02-11 01:07:39 +0000115
Reid Spenceree448632005-07-12 15:51:55 +0000116 public:
Jeffrey Yasskin40966a72010-02-11 01:07:39 +0000117 JITResolverState(JIT *jit) : FunctionToLazyStubMap(this),
Benjamin Kramere04690e2012-06-16 21:55:52 +0000118 FunctionToCallSitesMap(this) {
119#ifndef NDEBUG
120 TheJIT = jit;
121#endif
122 }
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000123
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000124 FunctionToLazyStubMapTy& getFunctionToLazyStubMap(
125 const MutexGuard& locked) {
Reid Spenceree448632005-07-12 15:51:55 +0000126 assert(locked.holds(TheJIT->lock));
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000127 return FunctionToLazyStubMap;
Reid Spenceree448632005-07-12 15:51:55 +0000128 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000129
Jim Grosbach124d0332011-03-16 01:21:55 +0000130 GlobalToIndirectSymMapTy& getGlobalToIndirectSymMap(const MutexGuard& lck) {
131 assert(lck.holds(TheJIT->lock));
Evan Cheng5594f122008-11-10 01:52:24 +0000132 return GlobalToIndirectSymMap;
Evan Chengbe8c03f2008-01-04 10:46:51 +0000133 }
Jeffrey Yasskinebbcef92009-10-19 18:49:59 +0000134
Jay Foad5b240172011-04-13 12:46:01 +0000135 std::pair<void *, Function *> LookupFunctionFromCallSite(
Jeffrey Yasskinebbcef92009-10-19 18:49:59 +0000136 const MutexGuard &locked, void *CallSite) const {
137 assert(locked.holds(TheJIT->lock));
138
Jim Grosbach124d0332011-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 Yasskinebbcef92009-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 Yasskin23e5fcf2009-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 Yasskinebbcef92009-10-19 18:49:59 +0000157 FunctionToCallSitesMap[F].insert(CallSite);
158 }
159
Jeffrey Yasskin8e98d122010-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 Spenceree448632005-07-12 15:51:55 +0000166 };
Jeff Cohen00b168892005-07-27 06:12:32 +0000167
Chris Lattner54266522004-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 Yasskin108c8382009-11-23 23:35:19 +0000171 typedef JITResolverState::FunctionToLazyStubMapTy FunctionToLazyStubMapTy;
Jeffrey Yasskinebbcef92009-10-19 18:49:59 +0000172 typedef JITResolverState::CallSiteToFunctionMapTy CallSiteToFunctionMapTy;
Nick Lewyckye2bcf132009-04-27 05:09:44 +0000173 typedef JITResolverState::GlobalToIndirectSymMapTy GlobalToIndirectSymMapTy;
174
Chris Lattner5e225582004-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 Spenceree448632005-07-12 15:51:55 +0000179 JITResolverState state;
Chris Lattner54266522004-11-20 23:57:07 +0000180
Jeffrey Yasskin108c8382009-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 Lattnerd91ff7c2005-04-18 01:44:27 +0000186 std::map<void*, void*> ExternalFnToStubMap;
Andrew Lenharth6a974612005-07-28 12:44:13 +0000187
Evan Cheng1606e8e2009-03-13 07:51:59 +0000188 /// revGOTMap - map addresses to indexes in the GOT
Andrew Lenharth6a974612005-07-28 12:44:13 +0000189 std::map<void*, unsigned> revGOTMap;
190 unsigned nextGOTIndex;
191
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000192 JITEmitter &JE;
193
Jeffrey Yasskin40966a72010-02-11 01:07:39 +0000194 /// Instance of JIT corresponding to this Resolver.
195 JIT *TheJIT;
196
Chris Lattner54266522004-11-20 23:57:07 +0000197 public:
Jeffrey Yasskin40966a72010-02-11 01:07:39 +0000198 explicit JITResolver(JIT &jit, JITEmitter &je)
Benjamin Kramere04690e2012-06-16 21:55:52 +0000199 : state(&jit), nextGOTIndex(0), JE(je), TheJIT(&jit) {
Chris Lattnere7484012007-02-24 02:57:03 +0000200 LazyResolverFn = jit.getJITInfo().getLazyResolverFunction(JITCompilerFn);
Chris Lattner5e225582004-11-21 03:37:42 +0000201 }
Chris Lattner54266522004-11-20 23:57:07 +0000202
Jeffrey Yasskin8e98d122010-03-04 00:32:33 +0000203 ~JITResolver();
204
Jeffrey Yasskin108c8382009-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 Cheng704bff92008-11-13 21:50:50 +0000208
Jeffrey Yasskin108c8382009-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 Lattner54266522004-11-20 23:57:07 +0000212
Chris Lattnerd91ff7c2005-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 Cheng5594f122008-11-10 01:52:24 +0000217 /// getGlobalValueIndirectSym - Return an indirect symbol containing the
Evan Chengc96a8e72008-11-05 01:50:32 +0000218 /// specified GV address.
Evan Cheng5594f122008-11-10 01:52:24 +0000219 void *getGlobalValueIndirectSym(GlobalValue *V, void *GVAddress);
Evan Chengbe8c03f2008-01-04 10:46:51 +0000220
Andrew Lenharth6a974612005-07-28 12:44:13 +0000221 /// getGOTIndexForAddress - Return a new or existing index in the GOT for
Chris Lattner8907b4b2007-12-05 23:39:57 +0000222 /// an address. This function only manages slots, it does not manage the
Andrew Lenharth6a974612005-07-28 12:44:13 +0000223 /// contents of the slots or the memory associated with the GOT.
Chris Lattner8907b4b2007-12-05 23:39:57 +0000224 unsigned getGOTIndexForAddr(void *addr);
Andrew Lenharth6a974612005-07-28 12:44:13 +0000225
Chris Lattner54266522004-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 Yasskine637c192009-11-07 00:00:10 +0000231
Jeffrey Yasskin40966a72010-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 Yasskin8e98d122010-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 Yasskin40966a72010-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 Yasskine637c192009-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 Yasskin32d7e6e2009-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 Yasskine637c192009-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 Christopher116664a2009-11-12 03:12:18 +0000299
Jeffrey Yasskine637c192009-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 Christopher116664a2009-11-12 03:12:18 +0000320
Jeffrey Yasskine637c192009-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 Christopher116664a2009-11-12 03:12:18 +0000331 /// LabelLocations - This vector is a mapping from Label ID's to their
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000332 /// address.
Chris Lattner16112732010-03-14 01:41:15 +0000333 DenseMap<MCSymbol*, uintptr_t> LabelLocations;
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000334
335 /// MMI - Machine module info for exception informations
336 MachineModuleInfo* MMI;
337
Eric Christopher116664a2009-11-12 03:12:18 +0000338 // CurFn - The llvm function being emitted. Only valid during
Jeffrey Yasskine637c192009-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 Geoffray4eb37392010-04-14 22:06:37 +0000361 DebugLoc PrevDL;
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000362
Jeffrey Yasskin40966a72010-02-11 01:07:39 +0000363 /// Instance of the JIT
364 JIT *TheJIT;
365
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000366 bool JITExceptionHandling;
367
Jeffrey Yasskine637c192009-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 Lewycky8a8d4792011-12-02 22:16:29 +0000371 EmittedFunctions(this), TheJIT(&jit),
Rafael Espindola611caf52012-01-05 22:07:43 +0000372 JITExceptionHandling(TM.Options.JITExceptionHandling) {
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000373 MemMgr = JMM ? JMM : JITMemoryManager::CreateDefaultMemManager();
374 if (jit.getJITInfo().needsGOT()) {
375 MemMgr->AllocateGOT();
David Greenec9ec9932010-01-05 01:23:36 +0000376 DEBUG(dbgs() << "JIT is managing a GOT\n");
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000377 }
378
Rafael Espindola611caf52012-01-05 22:07:43 +0000379 if (JITExceptionHandling) {
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000380 DE.reset(new JITDwarfEmitter(jit));
381 }
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000382 }
Eric Christopher116664a2009-11-12 03:12:18 +0000383 ~JITEmitter() {
Jeffrey Yasskine637c192009-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 Yasskine637c192009-11-07 00:00:10 +0000390 static inline bool classof(const MachineCodeEmitter*) { return true; }
Eric Christopher116664a2009-11-12 03:12:18 +0000391
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000392 JITResolver &getJITResolver() { return Resolver; }
393
394 virtual void startFunction(MachineFunction &F);
395 virtual bool finishFunction(MachineFunction &F);
Eric Christopher116664a2009-11-12 03:12:18 +0000396
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000397 void emitConstantPool(MachineConstantPool *MCP);
398 void initJumpTableInfo(MachineJumpTableInfo *MJTI);
399 void emitJumpTableInfo(MachineJumpTableInfo *MJTI);
Eric Christopher116664a2009-11-12 03:12:18 +0000400
Jeffrey Yasskin32d7e6e2009-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 Yasskine637c192009-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 Christopher116664a2009-11-12 03:12:18 +0000421
Jeffrey Yasskine637c192009-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 Lattner68feb222010-07-11 23:07:28 +0000426 if (MBB->hasAddressTaken())
427 TheJIT->addPointerToBasicBlock(MBB->getBasicBlock(),
428 (void*)getCurrentPCValue());
David Greenec9ec9932010-01-05 01:23:36 +0000429 DEBUG(dbgs() << "JIT: Emitting BB" << MBB->getNumber() << " at ["
Jeffrey Yasskine637c192009-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 Lattner68feb222010-07-11 23:07:28 +0000436 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const{
Eric Christopher116664a2009-11-12 03:12:18 +0000437 assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
Jeffrey Yasskine637c192009-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 Yasskine637c192009-11-07 00:00:10 +0000451 virtual void processDebugLoc(DebugLoc DL, bool BeforePrintingInsn);
452
Chris Lattner16112732010-03-14 01:41:15 +0000453 virtual void emitLabel(MCSymbol *Label) {
454 LabelLocations[Label] = getCurrentPCValue();
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000455 }
456
Bill Wendling47639fc2010-04-16 08:46:10 +0000457 virtual DenseMap<MCSymbol*, uintptr_t> *getLabelLocations() {
458 return &LabelLocations;
459 }
460
Chris Lattner16112732010-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 Yasskine637c192009-11-07 00:00:10 +0000464 }
Eric Christopher116664a2009-11-12 03:12:18 +0000465
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000466 virtual void setModuleInfo(MachineModuleInfo* Info) {
467 MMI = Info;
468 if (DE.get()) DE->setModuleInfo(Info);
469 }
470
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000471 private:
Jeffrey Yasskin2d274412009-11-07 08:51:52 +0000472 void *getPointerToGlobal(GlobalValue *GV, void *Reference,
473 bool MayNeedFarStub);
474 void *getPointerToGVIndirectSym(GlobalValue *V, void *Reference);
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000475 };
Chris Lattner54266522004-11-20 23:57:07 +0000476}
477
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000478void CallSiteValueMapConfig::onDelete(JITResolverState *JRS, Function *F) {
Jeffrey Yasskin8e98d122010-03-04 00:32:33 +0000479 JRS->EraseAllCallSitesForPrelocked(F);
480}
481
Jeffrey Yasskin8e98d122010-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 Yasskin23e5fcf2009-10-23 22:37:43 +0000513}
514
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000515/// getLazyFunctionStubIfAvailable - This returns a pointer to a function stub
Evan Cheng704bff92008-11-13 21:50:50 +0000516/// if it has already been created.
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000517void *JITResolver::getLazyFunctionStubIfAvailable(Function *F) {
Evan Cheng704bff92008-11-13 21:50:50 +0000518 MutexGuard locked(TheJIT->lock);
519
520 // If we already have a stub for this function, recycle it.
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000521 return state.getFunctionToLazyStubMap(locked).lookup(F);
Evan Cheng704bff92008-11-13 21:50:50 +0000522}
523
Chris Lattner54266522004-11-20 23:57:07 +0000524/// getFunctionStub - This returns a pointer to a function stub, creating
525/// one on demand as needed.
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000526void *JITResolver::getLazyFunctionStub(Function *F) {
Reid Spenceree448632005-07-12 15:51:55 +0000527 MutexGuard locked(TheJIT->lock);
528
Jeffrey Yasskin108c8382009-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 Lattner54266522004-11-20 23:57:07 +0000531 if (Stub) return Stub;
532
Jeffrey Yasskindc857242009-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 Begemanb9c6c9b2009-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 Yasskinc5818fb2009-12-22 23:47:23 +0000540 if (isNonGhostDeclaration(F) || F->hasAvailableExternallyLinkage()) {
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000541 Actual = TheJIT->getPointerToFunction(F);
Misha Brukmanf976c852005-04-21 22:55:34 +0000542
Dan Gohman69f93782009-01-05 05:32:42 +0000543 // If we resolved the symbol to a null address (eg. a weak external)
Jeffrey Yasskin6f348e42009-11-09 22:34:19 +0000544 // don't emit a stub. Return a null pointer to the application.
545 if (!Actual) return 0;
Dan Gohman69f93782009-01-05 05:32:42 +0000546 }
547
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000548 TargetJITInfo::StubLayout SL = TheJIT->getJITInfo().getStubLayout();
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +0000549 JE.startGVStub(F, SL.Size, SL.Alignment);
Nate Begemanb9c6c9b2009-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 Yasskine637c192009-11-07 00:00:10 +0000552 Stub = TheJIT->getJITInfo().emitFunctionStub(F, Actual, JE);
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +0000553 JE.finishGVStub();
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000554
Chris Lattner870286a2006-06-01 17:29:22 +0000555 if (Actual != (void*)(intptr_t)LazyResolverFn) {
Chris Lattnerb43dbdc2004-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 Lattner54266522004-11-20 23:57:07 +0000561
David Greenec9ec9932010-01-05 01:23:36 +0000562 DEBUG(dbgs() << "JIT: Lazy stub emitted at [" << Stub << "] for function '"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000563 << F->getName() << "'\n");
Chris Lattnercb479412004-11-21 03:44:32 +0000564
Jeffrey Yasskin8e98d122010-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 Yasskin40966a72010-02-11 01:07:39 +0000569
Jeffrey Yasskin8e98d122010-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 Yasskine5f87982009-10-13 21:32:57 +0000581
Chris Lattner54266522004-11-20 23:57:07 +0000582 return Stub;
583}
584
Evan Cheng5594f122008-11-10 01:52:24 +0000585/// getGlobalValueIndirectSym - Return a lazy pointer containing the specified
Evan Chengbe8c03f2008-01-04 10:46:51 +0000586/// GV address.
Evan Cheng5594f122008-11-10 01:52:24 +0000587void *JITResolver::getGlobalValueIndirectSym(GlobalValue *GV, void *GVAddress) {
Evan Chengbe8c03f2008-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 Cheng5594f122008-11-10 01:52:24 +0000591 void *&IndirectSym = state.getGlobalToIndirectSymMap(locked)[GV];
592 if (IndirectSym) return IndirectSym;
Evan Chengbe8c03f2008-01-04 10:46:51 +0000593
Evan Chenge4d783d2008-11-10 23:26:16 +0000594 // Otherwise, codegen a new indirect symbol.
Evan Cheng5594f122008-11-10 01:52:24 +0000595 IndirectSym = TheJIT->getJITInfo().emitGlobalValueIndirectSym(GV, GVAddress,
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000596 JE);
Evan Chengbe8c03f2008-01-04 10:46:51 +0000597
David Greenec9ec9932010-01-05 01:23:36 +0000598 DEBUG(dbgs() << "JIT: Indirect symbol emitted at [" << IndirectSym
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000599 << "] for GV '" << GV->getName() << "'\n");
Evan Chengbe8c03f2008-01-04 10:46:51 +0000600
Evan Cheng5594f122008-11-10 01:52:24 +0000601 return IndirectSym;
Evan Chengbe8c03f2008-01-04 10:46:51 +0000602}
603
Chris Lattnerd91ff7c2005-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 Yasskin108c8382009-11-23 23:35:19 +0000611 TargetJITInfo::StubLayout SL = TheJIT->getJITInfo().getStubLayout();
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +0000612 JE.startGVStub(0, SL.Size, SL.Alignment);
Jeffrey Yasskine637c192009-11-07 00:00:10 +0000613 Stub = TheJIT->getJITInfo().emitFunctionStub(0, FnAddr, JE);
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +0000614 JE.finishGVStub();
Evan Cheng55fc2802006-07-25 20:40:54 +0000615
David Greenec9ec9932010-01-05 01:23:36 +0000616 DEBUG(dbgs() << "JIT: Stub emitted at [" << Stub
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000617 << "] for external function at '" << FnAddr << "'\n");
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000618 return Stub;
619}
620
Andrew Lenharth6a974612005-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 Greenec9ec9932010-01-05 01:23:36 +0000626 DEBUG(dbgs() << "JIT: Adding GOT entry " << idx << " for addr ["
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000627 << addr << "]\n");
Andrew Lenharth6a974612005-07-28 12:44:13 +0000628 }
629 return idx;
630}
Chris Lattnerd91ff7c2005-04-18 01:44:27 +0000631
Chris Lattner54266522004-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 Yasskin40966a72010-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 Christopher116664a2009-11-12 03:12:18 +0000638
Nicolas Geoffraydcb31e12008-10-03 07:27:08 +0000639 Function* F = 0;
640 void* ActualPtr = 0;
Misha Brukmanf976c852005-04-21 22:55:34 +0000641
Nicolas Geoffraydcb31e12008-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 Yasskin40966a72010-02-11 01:07:39 +0000646 MutexGuard locked(JR->TheJIT->lock);
Reid Spenceree448632005-07-12 15:51:55 +0000647
Jeffrey Yasskinebbcef92009-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 Foad5b240172011-04-13 12:46:01 +0000650 std::pair<void*, Function*> I =
Jeffrey Yasskin40966a72010-02-11 01:07:39 +0000651 JR->state.LookupFunctionFromCallSite(locked, Stub);
Jeffrey Yasskinebbcef92009-10-19 18:49:59 +0000652 F = I.second;
653 ActualPtr = I.first;
Nicolas Geoffraydcb31e12008-10-03 07:27:08 +0000654 }
Chris Lattner54266522004-11-20 23:57:07 +0000655
Evan Cheng9da60f92007-06-30 00:10:37 +0000656 // If we have already code generated the function, just return the address.
Jeffrey Yasskin40966a72010-02-11 01:07:39 +0000657 void *Result = JR->TheJIT->getPointerToGlobalIfAvailable(F);
Eric Christopher116664a2009-11-12 03:12:18 +0000658
Evan Cheng9da60f92007-06-30 00:10:37 +0000659 if (!Result) {
660 // Otherwise we don't have it, do lazy compilation now.
Eric Christopher116664a2009-11-12 03:12:18 +0000661
Evan Cheng9da60f92007-06-30 00:10:37 +0000662 // If lazy compilation is disabled, emit a useful error message and abort.
Jeffrey Yasskin40966a72010-02-11 01:07:39 +0000663 if (!JR->TheJIT->isCompilingLazily()) {
Jim Grosbach124d0332011-03-16 01:21:55 +0000664 report_fatal_error("LLVM JIT requested to do lazy compilation of"
665 " function '"
Torok Edwin7d696d82009-07-11 13:10:19 +0000666 + F->getName() + "' when lazy compiles are disabled!");
Evan Cheng9da60f92007-06-30 00:10:37 +0000667 }
Eric Christopher116664a2009-11-12 03:12:18 +0000668
David Greenec9ec9932010-01-05 01:23:36 +0000669 DEBUG(dbgs() << "JIT: Lazily resolving function '" << F->getName()
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000670 << "' In stub ptr = " << Stub << " actual ptr = "
671 << ActualPtr << "\n");
Duncan Sands1f6a3292011-08-12 14:54:45 +0000672 (void)ActualPtr;
Chris Lattner54266522004-11-20 23:57:07 +0000673
Jeffrey Yasskin40966a72010-02-11 01:07:39 +0000674 Result = JR->TheJIT->getPointerToFunction(F);
Evan Cheng9da60f92007-06-30 00:10:37 +0000675 }
Jeffrey Yasskinebbcef92009-10-19 18:49:59 +0000676
677 // Reacquire the lock to update the GOT map.
Jeffrey Yasskin40966a72010-02-11 01:07:39 +0000678 MutexGuard locked(JR->TheJIT->lock);
Chris Lattner54266522004-11-20 23:57:07 +0000679
Jeffrey Yasskinebbcef92009-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 Lattner54266522004-11-20 23:57:07 +0000685
686 // FIXME: We could rewrite all references to this stub if we knew them.
Andrew Lenharth6a974612005-07-28 12:44:13 +0000687
Jeff Cohend29b6aa2005-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 Lenharth6a974612005-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 Yasskin40966a72010-02-11 01:07:39 +0000693 if(JR->revGOTMap.find(Stub) != JR->revGOTMap.end())
694 JR->revGOTMap[Result] = JR->revGOTMap[Stub];
Andrew Lenharth6a974612005-07-28 12:44:13 +0000695
Chris Lattner54266522004-11-20 23:57:07 +0000696 return Result;
697}
Chris Lattner688506d2003-08-14 18:35:27 +0000698
Chris Lattner8ac66c12008-04-04 05:51:42 +0000699//===----------------------------------------------------------------------===//
Chris Lattner166f2262004-11-22 22:00:25 +0000700// JITEmitter code.
Chris Lattner54266522004-11-20 23:57:07 +0000701//
Chris Lattner166f2262004-11-22 22:00:25 +0000702void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
Jeffrey Yasskin2d274412009-11-07 08:51:52 +0000703 bool MayNeedFarStub) {
Nate Begemand6b7a242009-02-18 08:31:02 +0000704 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
Chris Lattner54266522004-11-20 23:57:07 +0000705 return TheJIT->getOrEmitGlobalVariable(GV);
Nate Begemand6b7a242009-02-18 08:31:02 +0000706
Chris Lattner18e04592008-06-25 20:21:35 +0000707 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000708 return TheJIT->getPointerToGlobal(GA->resolveAliasedGlobal(false));
Chris Lattner54266522004-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 Christopher116664a2009-11-12 03:12:18 +0000712
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000713 void *FnStub = Resolver.getLazyFunctionStubIfAvailable(F);
Eric Christopher116664a2009-11-12 03:12:18 +0000714 if (FnStub) {
Jeffrey Yasskin108c8382009-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 Christopher116664a2009-11-12 03:12:18 +0000719 return FnStub;
Nate Begeman50cd6fd2009-03-05 06:34:37 +0000720 }
Eric Christopher116664a2009-11-12 03:12:18 +0000721
Jeffrey Yasskine03a39b2009-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 Lattner54266522004-11-20 23:57:07 +0000728
Jeffrey Yasskine03a39b2009-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 Yasskinc5818fb2009-12-22 23:47:23 +0000731 if (isNonGhostDeclaration(F) || F->hasAvailableExternallyLinkage())
Jeffrey Yasskine03a39b2009-11-19 23:42:58 +0000732 return TheJIT->getPointerToFunction(F);
733 }
Chris Lattnerb43dbdc2004-11-22 07:24:43 +0000734
Jeffrey Yasskin39c75f22010-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 Lattner54266522004-11-20 23:57:07 +0000739}
740
Jeffrey Yasskin2d274412009-11-07 08:51:52 +0000741void *JITEmitter::getPointerToGVIndirectSym(GlobalValue *V, void *Reference) {
Nate Begeman50cd6fd2009-03-05 06:34:37 +0000742 // Make sure GV is emitted first, and create a stub containing the fully
743 // resolved address.
Jeffrey Yasskin2d274412009-11-07 08:51:52 +0000744 void *GVAddress = getPointerToGlobal(V, Reference, false);
Nate Begeman50cd6fd2009-03-05 06:34:37 +0000745 void *StubAddr = Resolver.getGlobalValueIndirectSym(V, GVAddress);
Nate Begeman50cd6fd2009-03-05 06:34:37 +0000746 return StubAddr;
747}
748
Devang Patel02c04232009-10-06 03:04:58 +0000749void JITEmitter::processDebugLoc(DebugLoc DL, bool BeforePrintingInsn) {
Chris Lattnerde4845c2010-04-02 19:42:39 +0000750 if (DL.isUnknown()) return;
751 if (!BeforePrintingInsn) return;
Jim Grosbach124d0332011-03-16 01:21:55 +0000752
Chris Lattner134d8ee2010-07-22 21:17:55 +0000753 const LLVMContext &Context = EmissionDetails.MF->getFunction()->getContext();
Nicolas Geoffray4eb37392010-04-14 22:06:37 +0000754
755 if (DL.getScope(Context) != 0 && PrevDL != DL) {
Chris Lattnerde4845c2010-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 Yasskin32360a72009-07-16 21:07:26 +0000760 }
Chris Lattnerde4845c2010-04-02 19:42:39 +0000761
Nicolas Geoffray4eb37392010-04-14 22:06:37 +0000762 PrevDL = DL;
Jeffrey Yasskin32360a72009-07-16 21:07:26 +0000763}
764
Evan Cheng1606e8e2009-03-13 07:51:59 +0000765static unsigned GetConstantPoolSizeInBytes(MachineConstantPool *MCP,
766 const TargetData *TD) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000767 const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants();
768 if (Constants.empty()) return 0;
769
Evan Cheng1606e8e2009-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 Lattnerdb125cf2011-07-18 04:54:35 +0000775 Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000776 Size += TD->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000777 }
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000778 return Size;
779}
780
Chris Lattner166f2262004-11-22 22:00:25 +0000781void JITEmitter::startFunction(MachineFunction &F) {
David Greenec9ec9932010-01-05 01:23:36 +0000782 DEBUG(dbgs() << "JIT: Starting CodeGen of Function "
Craig Topper96601ca2012-08-22 06:07:19 +0000783 << F.getName() << "\n");
Evan Chengeb5d95a2008-11-06 17:46:04 +0000784
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000785 uintptr_t ActualSize = 0;
Jim Grosbachcce6c292008-10-03 16:17:20 +0000786 // Set the memory writable, if it's not already
787 MemMgr->setMemoryWritable();
Jim Grosbach124d0332011-03-16 01:21:55 +0000788
Chris Lattner134d8ee2010-07-22 21:17:55 +0000789 if (SizeEstimate > 0) {
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000790 // SizeEstimate will be non-zero on reallocation attempts.
791 ActualSize = SizeEstimate;
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000792 }
793
Chris Lattner8907b4b2007-12-05 23:39:57 +0000794 BufferBegin = CurBufferPtr = MemMgr->startFunctionBody(F.getFunction(),
795 ActualSize);
Chris Lattnere993cc22006-05-11 23:08:08 +0000796 BufferEnd = BufferBegin+ActualSize;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000797 EmittedFunctions[F.getFunction()].FunctionBody = BufferBegin;
798
Evan Cheng9a1e9b92006-11-16 20:04:54 +0000799 // Ensure the constant pool/jump table info is at least 4-byte aligned.
800 emitAlignment(16);
801
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000802 emitConstantPool(F.getConstantPool());
Chris Lattner071c62f2010-01-25 23:26:13 +0000803 if (MachineJumpTableInfo *MJTI = F.getJumpTableInfo())
804 initJumpTableInfo(MJTI);
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000805
806 // About to start emitting the machine code for the function.
Chris Lattner0eb4d6b2006-05-03 01:03:20 +0000807 emitAlignment(std::max(F.getFunction()->getAlignment(), 8U));
Chris Lattnerf75f9be2006-05-02 23:22:24 +0000808 TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr);
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000809 EmittedFunctions[F.getFunction()].Code = CurBufferPtr;
Evan Cheng55fc2802006-07-25 20:40:54 +0000810
Chris Lattnerb4432f32006-05-03 17:10:41 +0000811 MBBLocations.clear();
Jeffrey Yasskin32360a72009-07-16 21:07:26 +0000812
813 EmissionDetails.MF = &F;
814 EmissionDetails.LineStarts.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000815}
816
Chris Lattner43b429b2006-05-02 18:27:26 +0000817bool JITEmitter::finishFunction(MachineFunction &F) {
Chris Lattnere993cc22006-05-11 23:08:08 +0000818 if (CurBufferPtr == BufferEnd) {
Reid Kleckner10b4fc52009-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 Lattnere993cc22006-05-11 23:08:08 +0000824 }
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000825
Chris Lattner071c62f2010-01-25 23:26:13 +0000826 if (MachineJumpTableInfo *MJTI = F.getJumpTableInfo())
827 emitJumpTableInfo(MJTI);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000828
Chris Lattnera8279532006-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 Lopes186c6702009-06-04 00:15:51 +0000831 uint8_t *FnStart =
832 (uint8_t *)TheJIT->getPointerToGlobalIfAvailable(F.getFunction());
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000833
Argyrios Kyrtzidis19fee412009-04-30 23:01:58 +0000834 // FnEnd is the end of the function's machine code.
Bruno Cardoso Lopes186c6702009-06-04 00:15:51 +0000835 uint8_t *FnEnd = CurBufferPtr;
Argyrios Kyrtzidis19fee412009-04-30 23:01:58 +0000836
Chris Lattner5be478f2004-11-20 03:46:14 +0000837 if (!Relocations.empty()) {
Nate Begeman50cd6fd2009-03-05 06:34:37 +0000838 CurFn = F.getFunction();
Chris Lattnere884dc22005-07-20 16:29:20 +0000839 NumRelos += Relocations.size();
840
Chris Lattner5be478f2004-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 Cheng92006052008-11-03 07:14:02 +0000844 void *ResultPtr = 0;
Evan Chengef5784e2008-10-29 23:54:46 +0000845 if (!MR.letTargetResolve()) {
Evan Chengd7398c92008-11-08 07:37:34 +0000846 if (MR.isExternalSymbol()) {
Dan Gohman69f93782009-01-05 05:32:42 +0000847 ResultPtr = TheJIT->getPointerToNamedFunction(MR.getExternalSymbol(),
848 false);
David Greenec9ec9932010-01-05 01:23:36 +0000849 DEBUG(dbgs() << "JIT: Map \'" << MR.getExternalSymbol() << "\' to ["
Eric Christopher116664a2009-11-12 03:12:18 +0000850 << ResultPtr << "]\n");
Misha Brukmanf976c852005-04-21 22:55:34 +0000851
Evan Chengef5784e2008-10-29 23:54:46 +0000852 // If the target REALLY wants a stub for this function, emit it now.
Jeffrey Yasskin2d274412009-11-07 08:51:52 +0000853 if (MR.mayNeedFarStub()) {
Jeffrey Yasskin6f348e42009-11-09 22:34:19 +0000854 ResultPtr = Resolver.getExternalFunctionStub(ResultPtr);
Nate Begeman841c6a42009-03-11 07:03:43 +0000855 }
Evan Chengef5784e2008-10-29 23:54:46 +0000856 } else if (MR.isGlobalValue()) {
857 ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
858 BufferBegin+MR.getMachineCodeOffset(),
Jeffrey Yasskin2d274412009-11-07 08:51:52 +0000859 MR.mayNeedFarStub());
Evan Cheng5594f122008-11-10 01:52:24 +0000860 } else if (MR.isIndirectSymbol()) {
Jeffrey Yasskin2d274412009-11-07 08:51:52 +0000861 ResultPtr = getPointerToGVIndirectSym(
862 MR.getGlobalValue(), BufferBegin+MR.getMachineCodeOffset());
Evan Chengef5784e2008-10-29 23:54:46 +0000863 } else if (MR.isBasicBlock()) {
864 ResultPtr = (void*)getMachineBasicBlockAddress(MR.getBasicBlock());
865 } else if (MR.isConstantPoolIndex()) {
Jim Grosbach124d0332011-03-16 01:21:55 +0000866 ResultPtr =
867 (void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex());
Evan Chengef5784e2008-10-29 23:54:46 +0000868 } else {
869 assert(MR.isJumpTableIndex());
870 ResultPtr=(void*)getJumpTableEntryAddress(MR.getJumpTableIndex());
871 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000872
Evan Chengef5784e2008-10-29 23:54:46 +0000873 MR.setResultPointer(ResultPtr);
874 }
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000875
Andrew Lenharth6a974612005-07-28 12:44:13 +0000876 // if we are managing the GOT and the relocation wants an index,
877 // give it one
Chris Lattner8907b4b2007-12-05 23:39:57 +0000878 if (MR.isGOTRelative() && MemMgr->isManagingGOT()) {
Chris Lattnere7484012007-02-24 02:57:03 +0000879 unsigned idx = Resolver.getGOTIndexForAddr(ResultPtr);
Andrew Lenharth6a974612005-07-28 12:44:13 +0000880 MR.setGOTIndex(idx);
Chris Lattner8907b4b2007-12-05 23:39:57 +0000881 if (((void**)MemMgr->getGOTBase())[idx] != ResultPtr) {
David Greenec9ec9932010-01-05 01:23:36 +0000882 DEBUG(dbgs() << "JIT: GOT was out of date for " << ResultPtr
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000883 << " pointing at " << ((void**)MemMgr->getGOTBase())[idx]
884 << "\n");
Chris Lattner8907b4b2007-12-05 23:39:57 +0000885 ((void**)MemMgr->getGOTBase())[idx] = ResultPtr;
Andrew Lenharth6a974612005-07-28 12:44:13 +0000886 }
Andrew Lenharth16ec33c2005-07-22 20:48:12 +0000887 }
Chris Lattner5be478f2004-11-20 03:46:14 +0000888 }
889
Nate Begeman50cd6fd2009-03-05 06:34:37 +0000890 CurFn = 0;
Chris Lattner43b429b2006-05-02 18:27:26 +0000891 TheJIT->getJITInfo().relocate(BufferBegin, &Relocations[0],
Chris Lattner8907b4b2007-12-05 23:39:57 +0000892 Relocations.size(), MemMgr->getGOTBase());
Chris Lattner5be478f2004-11-20 03:46:14 +0000893 }
894
Chris Lattnerd2d5c762006-05-03 18:55:56 +0000895 // Update the GOT entry for F to point to the new code.
Chris Lattner8907b4b2007-12-05 23:39:57 +0000896 if (MemMgr->isManagingGOT()) {
Chris Lattnere7484012007-02-24 02:57:03 +0000897 unsigned idx = Resolver.getGOTIndexForAddr((void*)BufferBegin);
Chris Lattner8907b4b2007-12-05 23:39:57 +0000898 if (((void**)MemMgr->getGOTBase())[idx] != (void*)BufferBegin) {
David Greenec9ec9932010-01-05 01:23:36 +0000899 DEBUG(dbgs() << "JIT: GOT was out of date for " << (void*)BufferBegin
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000900 << " pointing at " << ((void**)MemMgr->getGOTBase())[idx]
901 << "\n");
Chris Lattner8907b4b2007-12-05 23:39:57 +0000902 ((void**)MemMgr->getGOTBase())[idx] = (void*)BufferBegin;
Andrew Lenharth6a974612005-07-28 12:44:13 +0000903 }
904 }
905
Argyrios Kyrtzidis19fee412009-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 Cheng5788d1a2008-12-10 02:32:19 +0000909
910 if (CurBufferPtr == BufferEnd) {
Reid Kleckner10b4fc52009-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 Cheng5788d1a2008-12-10 02:32:19 +0000917 }
918
Nuno Lopescef75272008-10-21 11:42:16 +0000919 BufferBegin = CurBufferPtr = 0;
920 NumBytes += FnEnd-FnStart;
921
Evan Cheng55fc2802006-07-25 20:40:54 +0000922 // Invalidate the icache if necessary.
Chris Lattnerbc52cad2008-06-25 17:18:44 +0000923 sys::Memory::InvalidateInstructionCache(FnStart, FnEnd-FnStart);
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +0000924
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +0000925 TheJIT->NotifyFunctionEmitted(*F.getFunction(), FnStart, FnEnd-FnStart,
Jeffrey Yasskin32360a72009-07-16 21:07:26 +0000926 EmissionDetails);
Evan Cheng55fc2802006-07-25 20:40:54 +0000927
Nicolas Geoffray4eb37392010-04-14 22:06:37 +0000928 // Reset the previous debug location.
929 PrevDL = DebugLoc();
930
David Greenec9ec9932010-01-05 01:23:36 +0000931 DEBUG(dbgs() << "JIT: Finished CodeGen of [" << (void*)FnStart
Craig Topper96601ca2012-08-22 06:07:19 +0000932 << "] Function: " << F.getName()
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000933 << ": " << (FnEnd-FnStart) << " bytes of text, "
934 << Relocations.size() << " relocations\n");
Argyrios Kyrtzidisb3a847d2009-05-18 21:06:40 +0000935
Chris Lattner5be478f2004-11-20 03:46:14 +0000936 Relocations.clear();
Evan Cheng1606e8e2009-03-13 07:51:59 +0000937 ConstPoolAddresses.clear();
Anton Korobeynikov8cd4c3e2007-01-19 17:25:17 +0000938
Evan Chengbc4707a2008-09-18 07:54:21 +0000939 // Mark code region readable and executable if it's not so already.
Jim Grosbachcce6c292008-10-03 16:17:20 +0000940 MemMgr->setMemoryExecutable();
Evan Chengbc4707a2008-09-18 07:54:21 +0000941
Bill Wendling69c128f2010-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 Chenge7c35512008-11-12 08:22:43 +0000967 }
Bill Wendling69c128f2010-04-18 00:52:08 +0000968 dbgs()<< '\n';
Evan Chenga7916f52008-11-05 23:44:08 +0000969 }
Bill Wendling69c128f2010-04-18 00:52:08 +0000970 });
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000971
Rafael Espindola611caf52012-01-05 22:07:43 +0000972 if (JITExceptionHandling) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000973 uintptr_t ActualSize = 0;
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +0000974 SavedBufferBegin = BufferBegin;
975 SavedBufferEnd = BufferEnd;
976 SavedCurBufferPtr = CurBufferPtr;
Reid Kleckner27632172009-09-20 23:52:43 +0000977
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000978 BufferBegin = CurBufferPtr = MemMgr->startExceptionTable(F.getFunction(),
979 ActualSize);
980 BufferEnd = BufferBegin+ActualSize;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000981 EmittedFunctions[F.getFunction()].ExceptionTable = BufferBegin;
Reid Kleckner27632172009-09-20 23:52:43 +0000982 uint8_t *EhStart;
983 uint8_t *FrameRegister = DE->EmitDwarfTable(F, *this, FnStart, FnEnd,
984 EhStart);
Chris Lattner0fdaa0b2008-03-07 20:05:43 +0000985 MemMgr->endExceptionTable(F.getFunction(), BufferBegin, CurBufferPtr,
986 FrameRegister);
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +0000987 BufferBegin = SavedBufferBegin;
988 BufferEnd = SavedBufferEnd;
989 CurBufferPtr = SavedCurBufferPtr;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000990
Duncan Sands57b6e9e2010-05-02 15:36:26 +0000991 if (JITExceptionHandling) {
Eric Christopher515c67e2011-03-04 23:37:39 +0000992 TheJIT->RegisterTable(F.getFunction(), FrameRegister);
Reid Kleckner27632172009-09-20 23:52:43 +0000993 }
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000994 }
Evan Cheng252ddfb2008-09-02 08:14:01 +0000995
996 if (MMI)
997 MMI->EndFunction();
Eric Christopher116664a2009-11-12 03:12:18 +0000998
Chris Lattner43b429b2006-05-02 18:27:26 +0000999 return false;
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001000}
1001
Reid Kleckner10b4fc52009-07-23 21:46:56 +00001002void JITEmitter::retryWithMoreMemory(MachineFunction &F) {
David Greenec9ec9932010-01-05 01:23:36 +00001003 DEBUG(dbgs() << "JIT: Ran out of space for native code. Reattempting.\n");
Reid Kleckner10b4fc52009-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 Lattner68feb222010-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 Kleckner10b4fc52009-07-23 21:46:56 +00001015}
1016
Nate Begeman50cd6fd2009-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 Yasskin7a9034c2009-10-27 00:03:05 +00001019/// May be called while the Function is being destroyed inside ~Value().
Reid Kleckner10b4fc52009-07-23 21:46:56 +00001020void JITEmitter::deallocateMemForFunction(const Function *F) {
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +00001021 ValueMap<const Function *, EmittedCode, EmittedFunctionConfig>::iterator
1022 Emitted = EmittedFunctions.find(F);
Jeffrey Yasskin1e861322009-10-20 18:13:21 +00001023 if (Emitted != EmittedFunctions.end()) {
1024 MemMgr->deallocateFunctionBody(Emitted->second.FunctionBody);
1025 MemMgr->deallocateExceptionTable(Emitted->second.ExceptionTable);
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +00001026 TheJIT->NotifyFreeingMachineCode(Emitted->second.Code);
1027
Jeffrey Yasskin1e861322009-10-20 18:13:21 +00001028 EmittedFunctions.erase(Emitted);
1029 }
Nate Begeman50cd6fd2009-03-05 06:34:37 +00001030
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001031 if (JITExceptionHandling) {
Eric Christopher515c67e2011-03-04 23:37:39 +00001032 TheJIT->DeregisterTable(F);
1033 }
Nate Begeman50cd6fd2009-03-05 06:34:37 +00001034}
1035
1036
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001037void *JITEmitter::allocateSpace(uintptr_t Size, unsigned Alignment) {
Nuno Lopescef75272008-10-21 11:42:16 +00001038 if (BufferBegin)
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001039 return JITCodeEmitter::allocateSpace(Size, Alignment);
Nuno Lopescef75272008-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 Lewycky8a8d4792011-12-02 22:16:29 +00001049void *JITEmitter::allocateGlobal(uintptr_t Size, unsigned Alignment) {
Jeffrey Yasskin489393d2009-07-08 21:59:57 +00001050 // Delegate this call through the memory manager.
1051 return MemMgr->allocateGlobal(Size, Alignment);
1052}
1053
Chris Lattner166f2262004-11-22 22:00:25 +00001054void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
Evan Cheng47c01a02008-11-07 09:02:17 +00001055 if (TheJIT->getJITInfo().hasCustomConstantPool())
Jim Grosbach8fe95352008-10-30 23:44:39 +00001056 return;
Evan Cheng47c01a02008-11-07 09:02:17 +00001057
Chris Lattnerfa77d432006-02-09 04:22:52 +00001058 const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants();
Chris Lattner2c0a6a12003-11-30 04:23:21 +00001059 if (Constants.empty()) return;
1060
Evan Cheng1606e8e2009-03-13 07:51:59 +00001061 unsigned Size = GetConstantPoolSizeInBytes(MCP, TheJIT->getTargetData());
1062 unsigned Align = MCP->getConstantPoolAlignment();
Evan Cheng97b8c402008-04-12 00:22:01 +00001063 ConstantPoolBase = allocateSpace(Size, Align);
Chris Lattner239862c2006-02-09 04:49:59 +00001064 ConstantPool = MCP;
Chris Lattnerf75f9be2006-05-02 23:22:24 +00001065
1066 if (ConstantPoolBase == 0) return; // Buffer overflow.
1067
David Greenec9ec9932010-01-05 01:23:36 +00001068 DEBUG(dbgs() << "JIT: Emitted constant pool at [" << ConstantPoolBase
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001069 << "] (size: " << Size << ", alignment: " << Align << ")\n");
Evan Cheng97b8c402008-04-12 00:22:01 +00001070
Chris Lattner239862c2006-02-09 04:49:59 +00001071 // Initialize the memory for all of the constant pool entries.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001072 unsigned Offset = 0;
Chris Lattner3029f922006-02-09 04:46:04 +00001073 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Evan Cheng1606e8e2009-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 Chengcd5731d2006-09-12 20:59:59 +00001081 // FIXME: add support to lower machine constant pool values into bytes!
Chris Lattner75361b62010-04-07 22:58:41 +00001082 report_fatal_error("Initialize memory with machine specific constant pool"
Torok Edwin7d696d82009-07-11 13:10:19 +00001083 "entry has not been implemented!");
Evan Chengcd5731d2006-09-12 20:59:59 +00001084 }
Evan Cheng1606e8e2009-03-13 07:51:59 +00001085 TheJIT->InitializeMemory(CPE.Val.ConstVal, (void*)CAddr);
David Greenec9ec9932010-01-05 01:23:36 +00001086 DEBUG(dbgs() << "JIT: CP" << i << " at [0x";
1087 dbgs().write_hex(CAddr) << "]\n");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001088
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001089 Type *Ty = CPE.Val.ConstVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00001090 Offset += TheJIT->getTargetData()->getTypeAllocSize(Ty);
Chris Lattner1cc08382003-01-13 01:00:12 +00001091 }
1092}
1093
Nate Begeman37efe672006-04-22 18:53:45 +00001094void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) {
Evan Cheng47c01a02008-11-07 09:02:17 +00001095 if (TheJIT->getJITInfo().hasCustomJumpTables())
1096 return;
Richard Osborne95da6052010-03-11 14:58:16 +00001097 if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_Inline)
1098 return;
Evan Cheng47c01a02008-11-07 09:02:17 +00001099
Nate Begeman37efe672006-04-22 18:53:45 +00001100 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1101 if (JT.empty()) return;
Eric Christopher116664a2009-11-12 03:12:18 +00001102
Chris Lattnerf75f9be2006-05-02 23:22:24 +00001103 unsigned NumEntries = 0;
Nate Begeman37efe672006-04-22 18:53:45 +00001104 for (unsigned i = 0, e = JT.size(); i != e; ++i)
Chris Lattnerf75f9be2006-05-02 23:22:24 +00001105 NumEntries += JT[i].MBBs.size();
1106
Chris Lattner071c62f2010-01-25 23:26:13 +00001107 unsigned EntrySize = MJTI->getEntrySize(*TheJIT->getTargetData());
Chris Lattnerf75f9be2006-05-02 23:22:24 +00001108
Nate Begeman37efe672006-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 Lattner071c62f2010-01-25 23:26:13 +00001113 JumpTableBase = allocateSpace(NumEntries * EntrySize,
1114 MJTI->getEntryAlignment(*TheJIT->getTargetData()));
Nate Begeman37efe672006-04-22 18:53:45 +00001115}
1116
Jim Laskeyb92767a2006-12-14 22:53:42 +00001117void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) {
Evan Cheng47c01a02008-11-07 09:02:17 +00001118 if (TheJIT->getJITInfo().hasCustomJumpTables())
1119 return;
1120
Nate Begeman37efe672006-04-22 18:53:45 +00001121 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
Chris Lattnerf75f9be2006-05-02 23:22:24 +00001122 if (JT.empty() || JumpTableBase == 0) return;
Eric Christopher116664a2009-11-12 03:12:18 +00001123
Jim Grosbach124d0332011-03-16 01:21:55 +00001124
Chris Lattner13af11a2010-01-26 03:47:15 +00001125 switch (MJTI->getEntryKind()) {
Richard Osborne95da6052010-03-11 14:58:16 +00001126 case MachineJumpTableInfo::EK_Inline:
1127 return;
Chris Lattner13af11a2010-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 Grosbach124d0332011-03-16 01:21:55 +00001133
Chris Lattner13af11a2010-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 Grosbach124d0332011-03-16 01:21:55 +00001137
Chris Lattner13af11a2010-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 Grosbach124d0332011-03-16 01:21:55 +00001147
Chris Lattner85fe0782010-01-26 04:05:28 +00001148 case MachineJumpTableInfo::EK_Custom32:
Chris Lattner13af11a2010-01-26 03:47:15 +00001149 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
1150 case MachineJumpTableInfo::EK_LabelDifference32: {
Chris Lattner071c62f2010-01-25 23:26:13 +00001151 assert(MJTI->getEntrySize(*TheJIT->getTargetData()) == 4&&"Cross JIT'ing?");
Jim Laskeyacd80ac2006-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 Lattner32ca55f2006-05-03 00:13:06 +00001155
Jim Laskeyacd80ac2006-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 Cheng5788d1a2008-12-10 02:32:19 +00001160 uintptr_t Base = (uintptr_t)SlotPtr;
Evan Cheng2a3e08b2008-01-05 02:26:58 +00001161 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
Evan Cheng5788d1a2008-12-10 02:32:19 +00001162 uintptr_t MBBAddr = getMachineBasicBlockAddress(MBBs[mi]);
Chris Lattner13af11a2010-01-26 03:47:15 +00001163 /// FIXME: USe EntryKind instead of magic "getPICJumpTableEntry" hook.
Evan Cheng2a3e08b2008-01-05 02:26:58 +00001164 *SlotPtr++ = TheJIT->getJITInfo().getPICJumpTableEntry(MBBAddr, Base);
1165 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001166 }
Chris Lattner13af11a2010-01-26 03:47:15 +00001167 break;
1168 }
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00001169 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
Craig Topper85814382012-02-07 05:05:23 +00001170 llvm_unreachable(
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00001171 "JT Info emission not implemented for GPRel64BlockAddress yet.");
Nate Begeman37efe672006-04-22 18:53:45 +00001172 }
1173}
1174
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +00001175void JITEmitter::startGVStub(const GlobalValue* GV,
Jeffrey Yasskin0261d792009-11-23 22:49:00 +00001176 unsigned StubSize, unsigned Alignment) {
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +00001177 SavedBufferBegin = BufferBegin;
1178 SavedBufferEnd = BufferEnd;
1179 SavedCurBufferPtr = CurBufferPtr;
Eric Christopher116664a2009-11-12 03:12:18 +00001180
Evan Chengce4a70b2008-11-08 08:02:53 +00001181 BufferBegin = CurBufferPtr = MemMgr->allocateStub(GV, StubSize, Alignment);
Chris Lattner43b429b2006-05-02 18:27:26 +00001182 BufferEnd = BufferBegin+StubSize+1;
Chris Lattner6125fdd2003-05-09 03:30:07 +00001183}
1184
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +00001185void JITEmitter::startGVStub(void *Buffer, unsigned StubSize) {
1186 SavedBufferBegin = BufferBegin;
1187 SavedBufferEnd = BufferEnd;
1188 SavedCurBufferPtr = CurBufferPtr;
Eric Christopher116664a2009-11-12 03:12:18 +00001189
Bruno Cardoso Lopes186c6702009-06-04 00:15:51 +00001190 BufferBegin = CurBufferPtr = (uint8_t *)Buffer;
Nate Begemand6b7a242009-02-18 08:31:02 +00001191 BufferEnd = BufferBegin+StubSize+1;
1192}
1193
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +00001194void JITEmitter::finishGVStub() {
Jeffrey Yasskin108c8382009-11-23 23:35:19 +00001195 assert(CurBufferPtr != BufferEnd && "Stub overflowed allocated space.");
Chris Lattner43b429b2006-05-02 18:27:26 +00001196 NumBytes += getCurrentPCOffset();
Jeffrey Yasskin32d7e6e2009-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 Lattnerbba1b6d2003-06-01 23:24:36 +00001208}
1209
Chris Lattnerbba1b6d2003-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 Cheng5788d1a2008-12-10 02:32:19 +00001214uintptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const {
Chris Lattner239862c2006-02-09 04:49:59 +00001215 assert(ConstantNum < ConstantPool->getConstants().size() &&
Misha Brukman3c944972005-04-22 04:08:30 +00001216 "Invalid ConstantPoolIndex!");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001217 return ConstPoolAddresses[ConstantNum];
Chris Lattnerbba1b6d2003-06-01 23:24:36 +00001218}
1219
Nate Begeman37efe672006-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 Cheng5788d1a2008-12-10 02:32:19 +00001223uintptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const {
Nate Begeman37efe672006-04-22 18:53:45 +00001224 const std::vector<MachineJumpTableEntry> &JT = JumpTable->getJumpTables();
1225 assert(Index < JT.size() && "Invalid jump table index!");
Eric Christopher116664a2009-11-12 03:12:18 +00001226
Chris Lattner071c62f2010-01-25 23:26:13 +00001227 unsigned EntrySize = JumpTable->getEntrySize(*TheJIT->getTargetData());
Eric Christopher116664a2009-11-12 03:12:18 +00001228
Chris Lattner071c62f2010-01-25 23:26:13 +00001229 unsigned Offset = 0;
Nate Begeman37efe672006-04-22 18:53:45 +00001230 for (unsigned i = 0; i < Index; ++i)
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001231 Offset += JT[i].MBBs.size();
Eric Christopher116664a2009-11-12 03:12:18 +00001232
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001233 Offset *= EntrySize;
Eric Christopher116664a2009-11-12 03:12:18 +00001234
Evan Cheng5788d1a2008-12-10 02:32:19 +00001235 return (uintptr_t)((char *)JumpTableBase + Offset);
Nate Begeman37efe672006-04-22 18:53:45 +00001236}
1237
Jeffrey Yasskin7a9034c2009-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 Lattnere993cc22006-05-11 23:08:08 +00001249//===----------------------------------------------------------------------===//
1250// Public interface to this file
1251//===----------------------------------------------------------------------===//
1252
Reid Kleckner27632172009-09-20 23:52:43 +00001253JITCodeEmitter *JIT::createEmitter(JIT &jit, JITMemoryManager *JMM,
1254 TargetMachine &tm) {
1255 return new JITEmitter(jit, JMM, tm);
Chris Lattnere993cc22006-05-11 23:08:08 +00001256}
1257
Chris Lattnere993cc22006-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 Christopher116664a2009-11-12 03:12:18 +00001266
Chris Lattnere7484012007-02-24 02:57:03 +00001267 // Get a stub if the target supports it.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001268 assert(isa<JITEmitter>(JCE) && "Unexpected MCE?");
Evan Chenga044dfc2008-08-20 00:28:12 +00001269 JITEmitter *JE = cast<JITEmitter>(getCodeEmitter());
Jeffrey Yasskin108c8382009-11-23 23:35:19 +00001270 return JE->getJITResolver().getLazyFunctionStub(F);
Chris Lattnere993cc22006-05-11 23:08:08 +00001271}
1272
Nate Begemand6b7a242009-02-18 08:31:02 +00001273void JIT::updateFunctionStub(Function *F) {
1274 // Get the empty stub we generated earlier.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001275 assert(isa<JITEmitter>(JCE) && "Unexpected MCE?");
Nate Begemand6b7a242009-02-18 08:31:02 +00001276 JITEmitter *JE = cast<JITEmitter>(getCodeEmitter());
Jeffrey Yasskin108c8382009-11-23 23:35:19 +00001277 void *Stub = JE->getJITResolver().getLazyFunctionStub(F);
1278 void *Addr = getPointerToGlobalIfAvailable(F);
Jeffrey Yasskinaad0d522009-12-17 21:35:29 +00001279 assert(Addr != Stub && "Function must have non-stub address to be updated.");
Nate Begemand6b7a242009-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 Yasskin108c8382009-11-23 23:35:19 +00001283 TargetJITInfo::StubLayout layout = getJITInfo().getStubLayout();
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +00001284 JE->startGVStub(Stub, layout.Size);
Jeffrey Yasskin108c8382009-11-23 23:35:19 +00001285 getJITInfo().emitFunctionStub(F, Addr, *getCodeEmitter());
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +00001286 JE->finishGVStub();
Nate Begemand6b7a242009-02-18 08:31:02 +00001287}
1288
Chris Lattnere993cc22006-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 Yasskin7a9034c2009-10-27 00:03:05 +00001294 updateGlobalMapping(F, 0);
Chris Lattnere993cc22006-05-11 23:08:08 +00001295
1296 // Free the actual memory for the function body and related stuff.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001297 assert(isa<JITEmitter>(JCE) && "Unexpected MCE?");
1298 cast<JITEmitter>(JCE)->deallocateMemForFunction(F);
Chris Lattnere993cc22006-05-11 23:08:08 +00001299}