Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 1 | //===-- JITEmitter.cpp - Write machine code to executable memory ----------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 10 | // 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 Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 3785fad | 2003-08-05 17:00:32 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "jit" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 16 | #include "JIT.h" |
Jeffrey Yasskin | 1e86132 | 2009-10-20 18:13:21 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/OwningPtr.h" |
Evan Cheng | 47c01a0 | 2008-11-07 09:02:17 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallPtrSet.h" |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/ValueMap.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/JITCodeEmitter.h" |
| 24 | #include "llvm/CodeGen/MachineCodeInfo.h" |
| 25 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 26 | #include "llvm/CodeGen/MachineFunction.h" |
| 27 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 28 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 29 | #include "llvm/CodeGen/MachineRelocation.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/DebugInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/ExecutionEngine/GenericValue.h" |
| 32 | #include "llvm/ExecutionEngine/JITEventListener.h" |
| 33 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Constants.h" |
| 35 | #include "llvm/IR/DataLayout.h" |
| 36 | #include "llvm/IR/DerivedTypes.h" |
| 37 | #include "llvm/IR/Module.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Debug.h" |
| 39 | #include "llvm/Support/Disassembler.h" |
| 40 | #include "llvm/Support/ErrorHandling.h" |
| 41 | #include "llvm/Support/ManagedStatic.h" |
| 42 | #include "llvm/Support/Memory.h" |
| 43 | #include "llvm/Support/MutexGuard.h" |
| 44 | #include "llvm/Support/ValueHandle.h" |
| 45 | #include "llvm/Support/raw_ostream.h" |
| 46 | #include "llvm/Target/TargetInstrInfo.h" |
| 47 | #include "llvm/Target/TargetJITInfo.h" |
| 48 | #include "llvm/Target/TargetMachine.h" |
| 49 | #include "llvm/Target/TargetOptions.h" |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 50 | #include <algorithm> |
Evan Cheng | a7916f5 | 2008-11-05 23:44:08 +0000 | [diff] [blame] | 51 | #ifndef NDEBUG |
| 52 | #include <iomanip> |
| 53 | #endif |
Chris Lattner | c19aade | 2003-12-08 08:06:28 +0000 | [diff] [blame] | 54 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 3634373 | 2006-12-19 22:43:32 +0000 | [diff] [blame] | 56 | STATISTIC(NumBytes, "Number of bytes of machine code compiled"); |
| 57 | STATISTIC(NumRelos, "Number of relocations applied"); |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 58 | STATISTIC(NumRetries, "Number of retries with more memory"); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 59 | |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 60 | |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 61 | // A declaration may stop being a declaration once it's fully read from bitcode. |
| 62 | // This function returns true if F is fully read and is still a declaration. |
| 63 | static bool isNonGhostDeclaration(const Function *F) { |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 64 | return F->isDeclaration() && !F->isMaterializable(); |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 67 | //===----------------------------------------------------------------------===// |
| 68 | // JIT lazy compilation code. |
| 69 | // |
| 70 | namespace { |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 71 | class JITEmitter; |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 72 | class JITResolverState; |
| 73 | |
| 74 | template<typename ValueTy> |
| 75 | struct NoRAUWValueMapConfig : public ValueMapConfig<ValueTy> { |
| 76 | typedef JITResolverState *ExtraData; |
| 77 | static void onRAUW(JITResolverState *, Value *Old, Value *New) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 78 | llvm_unreachable("The JIT doesn't know how to handle a" |
| 79 | " RAUW on a value it has emitted."); |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 80 | } |
| 81 | }; |
| 82 | |
| 83 | struct CallSiteValueMapConfig : public NoRAUWValueMapConfig<Function*> { |
| 84 | typedef JITResolverState *ExtraData; |
| 85 | static void onDelete(JITResolverState *JRS, Function *F); |
| 86 | }; |
| 87 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 88 | class JITResolverState { |
Nick Lewycky | e2bcf13 | 2009-04-27 05:09:44 +0000 | [diff] [blame] | 89 | public: |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 90 | typedef ValueMap<Function*, void*, NoRAUWValueMapConfig<Function*> > |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 91 | FunctionToLazyStubMapTy; |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 92 | typedef std::map<void*, AssertingVH<Function> > CallSiteToFunctionMapTy; |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 93 | typedef ValueMap<Function *, SmallPtrSet<void*, 1>, |
| 94 | CallSiteValueMapConfig> FunctionToCallSitesMapTy; |
Nick Lewycky | e2bcf13 | 2009-04-27 05:09:44 +0000 | [diff] [blame] | 95 | typedef std::map<AssertingVH<GlobalValue>, void*> GlobalToIndirectSymMapTy; |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 96 | private: |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 97 | /// FunctionToLazyStubMap - Keep track of the lazy stub created for a |
| 98 | /// particular function so that we can reuse them if necessary. |
| 99 | FunctionToLazyStubMapTy FunctionToLazyStubMap; |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 100 | |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 101 | /// CallSiteToFunctionMap - Keep track of the function that each lazy call |
| 102 | /// site corresponds to, and vice versa. |
| 103 | CallSiteToFunctionMapTy CallSiteToFunctionMap; |
| 104 | FunctionToCallSitesMapTy FunctionToCallSitesMap; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 105 | |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 106 | /// GlobalToIndirectSymMap - Keep track of the indirect symbol created for a |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 107 | /// particular GlobalVariable so that we can reuse them if necessary. |
Nick Lewycky | e2bcf13 | 2009-04-27 05:09:44 +0000 | [diff] [blame] | 108 | GlobalToIndirectSymMapTy GlobalToIndirectSymMap; |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 109 | |
Benjamin Kramer | e04690e | 2012-06-16 21:55:52 +0000 | [diff] [blame] | 110 | #ifndef NDEBUG |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 111 | /// Instance of the JIT this ResolverState serves. |
| 112 | JIT *TheJIT; |
Benjamin Kramer | e04690e | 2012-06-16 21:55:52 +0000 | [diff] [blame] | 113 | #endif |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 114 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 115 | public: |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 116 | JITResolverState(JIT *jit) : FunctionToLazyStubMap(this), |
Benjamin Kramer | e04690e | 2012-06-16 21:55:52 +0000 | [diff] [blame] | 117 | FunctionToCallSitesMap(this) { |
| 118 | #ifndef NDEBUG |
| 119 | TheJIT = jit; |
| 120 | #endif |
| 121 | } |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 122 | |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 123 | FunctionToLazyStubMapTy& getFunctionToLazyStubMap( |
| 124 | const MutexGuard& locked) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 125 | assert(locked.holds(TheJIT->lock)); |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 126 | return FunctionToLazyStubMap; |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 127 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 128 | |
Jim Grosbach | 124d033 | 2011-03-16 01:21:55 +0000 | [diff] [blame] | 129 | GlobalToIndirectSymMapTy& getGlobalToIndirectSymMap(const MutexGuard& lck) { |
| 130 | assert(lck.holds(TheJIT->lock)); |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 131 | return GlobalToIndirectSymMap; |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 132 | } |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 133 | |
Jay Foad | 5b24017 | 2011-04-13 12:46:01 +0000 | [diff] [blame] | 134 | std::pair<void *, Function *> LookupFunctionFromCallSite( |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 135 | const MutexGuard &locked, void *CallSite) const { |
| 136 | assert(locked.holds(TheJIT->lock)); |
| 137 | |
Jim Grosbach | 124d033 | 2011-03-16 01:21:55 +0000 | [diff] [blame] | 138 | // The address given to us for the stub may not be exactly right, it |
| 139 | // might be a little bit after the stub. As such, use upper_bound to |
| 140 | // find it. |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 141 | CallSiteToFunctionMapTy::const_iterator I = |
| 142 | CallSiteToFunctionMap.upper_bound(CallSite); |
| 143 | assert(I != CallSiteToFunctionMap.begin() && |
| 144 | "This is not a known call site!"); |
| 145 | --I; |
| 146 | return *I; |
| 147 | } |
| 148 | |
| 149 | void AddCallSite(const MutexGuard &locked, void *CallSite, Function *F) { |
| 150 | assert(locked.holds(TheJIT->lock)); |
| 151 | |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 152 | bool Inserted = CallSiteToFunctionMap.insert( |
| 153 | std::make_pair(CallSite, F)).second; |
| 154 | (void)Inserted; |
| 155 | assert(Inserted && "Pair was already in CallSiteToFunctionMap"); |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 156 | FunctionToCallSitesMap[F].insert(CallSite); |
| 157 | } |
| 158 | |
Jeffrey Yasskin | 8e98d12 | 2010-03-04 00:32:33 +0000 | [diff] [blame] | 159 | void EraseAllCallSitesForPrelocked(Function *F); |
| 160 | |
| 161 | // Erases _all_ call sites regardless of their function. This is used to |
| 162 | // unregister the stub addresses from the StubToResolverMap in |
| 163 | // ~JITResolver(). |
| 164 | void EraseAllCallSitesPrelocked(); |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 165 | }; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 167 | /// JITResolver - Keep track of, and resolve, call sites for functions that |
| 168 | /// have not yet been compiled. |
| 169 | class JITResolver { |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 170 | typedef JITResolverState::FunctionToLazyStubMapTy FunctionToLazyStubMapTy; |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 171 | typedef JITResolverState::CallSiteToFunctionMapTy CallSiteToFunctionMapTy; |
Nick Lewycky | e2bcf13 | 2009-04-27 05:09:44 +0000 | [diff] [blame] | 172 | typedef JITResolverState::GlobalToIndirectSymMapTy GlobalToIndirectSymMapTy; |
| 173 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 174 | /// LazyResolverFn - The target lazy resolver function that we actually |
| 175 | /// rewrite instructions to use. |
| 176 | TargetJITInfo::LazyResolverFn LazyResolverFn; |
| 177 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 178 | JITResolverState state; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 179 | |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 180 | /// ExternalFnToStubMap - This is the equivalent of FunctionToLazyStubMap |
| 181 | /// for external functions. TODO: Of course, external functions don't need |
| 182 | /// a lazy stub. It's actually here to make it more likely that far calls |
| 183 | /// succeed, but no single stub can guarantee that. I'll remove this in a |
| 184 | /// subsequent checkin when I actually fix far calls. |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 185 | std::map<void*, void*> ExternalFnToStubMap; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 186 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 187 | /// revGOTMap - map addresses to indexes in the GOT |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 188 | std::map<void*, unsigned> revGOTMap; |
| 189 | unsigned nextGOTIndex; |
| 190 | |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 191 | JITEmitter &JE; |
| 192 | |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 193 | /// Instance of JIT corresponding to this Resolver. |
| 194 | JIT *TheJIT; |
| 195 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 196 | public: |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 197 | explicit JITResolver(JIT &jit, JITEmitter &je) |
Benjamin Kramer | e04690e | 2012-06-16 21:55:52 +0000 | [diff] [blame] | 198 | : state(&jit), nextGOTIndex(0), JE(je), TheJIT(&jit) { |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 199 | LazyResolverFn = jit.getJITInfo().getLazyResolverFunction(JITCompilerFn); |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 200 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 201 | |
Jeffrey Yasskin | 8e98d12 | 2010-03-04 00:32:33 +0000 | [diff] [blame] | 202 | ~JITResolver(); |
| 203 | |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 204 | /// getLazyFunctionStubIfAvailable - This returns a pointer to a function's |
| 205 | /// lazy-compilation stub if it has already been created. |
| 206 | void *getLazyFunctionStubIfAvailable(Function *F); |
Evan Cheng | 704bff9 | 2008-11-13 21:50:50 +0000 | [diff] [blame] | 207 | |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 208 | /// getLazyFunctionStub - This returns a pointer to a function's |
| 209 | /// lazy-compilation stub, creating one on demand as needed. |
| 210 | void *getLazyFunctionStub(Function *F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 211 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 212 | /// getExternalFunctionStub - Return a stub for the function at the |
| 213 | /// specified address, created lazily on demand. |
| 214 | void *getExternalFunctionStub(void *FnAddr); |
| 215 | |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 216 | /// getGlobalValueIndirectSym - Return an indirect symbol containing the |
Evan Cheng | c96a8e7 | 2008-11-05 01:50:32 +0000 | [diff] [blame] | 217 | /// specified GV address. |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 218 | void *getGlobalValueIndirectSym(GlobalValue *V, void *GVAddress); |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 219 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 220 | /// getGOTIndexForAddress - Return a new or existing index in the GOT for |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 221 | /// an address. This function only manages slots, it does not manage the |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 222 | /// contents of the slots or the memory associated with the GOT. |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 223 | unsigned getGOTIndexForAddr(void *addr); |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 225 | /// JITCompilerFn - This function is called to resolve a stub to a compiled |
| 226 | /// address. If the LLVM Function corresponding to the stub has not yet |
| 227 | /// been compiled, this function compiles it first. |
| 228 | static void *JITCompilerFn(void *Stub); |
| 229 | }; |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 230 | |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 231 | class StubToResolverMapTy { |
| 232 | /// Map a stub address to a specific instance of a JITResolver so that |
| 233 | /// lazily-compiled functions can find the right resolver to use. |
| 234 | /// |
| 235 | /// Guarded by Lock. |
| 236 | std::map<void*, JITResolver*> Map; |
| 237 | |
| 238 | /// Guards Map from concurrent accesses. |
| 239 | mutable sys::Mutex Lock; |
| 240 | |
| 241 | public: |
| 242 | /// Registers a Stub to be resolved by Resolver. |
| 243 | void RegisterStubResolver(void *Stub, JITResolver *Resolver) { |
| 244 | MutexGuard guard(Lock); |
| 245 | Map.insert(std::make_pair(Stub, Resolver)); |
| 246 | } |
| 247 | /// Unregisters the Stub when it's invalidated. |
| 248 | void UnregisterStubResolver(void *Stub) { |
| 249 | MutexGuard guard(Lock); |
| 250 | Map.erase(Stub); |
| 251 | } |
| 252 | /// Returns the JITResolver instance that owns the Stub. |
| 253 | JITResolver *getResolverFromStub(void *Stub) const { |
| 254 | MutexGuard guard(Lock); |
| 255 | // The address given to us for the stub may not be exactly right, it might |
| 256 | // be a little bit after the stub. As such, use upper_bound to find it. |
| 257 | // This is the same trick as in LookupFunctionFromCallSite from |
| 258 | // JITResolverState. |
| 259 | std::map<void*, JITResolver*>::const_iterator I = Map.upper_bound(Stub); |
| 260 | assert(I != Map.begin() && "This is not a known stub!"); |
| 261 | --I; |
| 262 | return I->second; |
| 263 | } |
Jeffrey Yasskin | 8e98d12 | 2010-03-04 00:32:33 +0000 | [diff] [blame] | 264 | /// True if any stubs refer to the given resolver. Only used in an assert(). |
| 265 | /// O(N) |
| 266 | bool ResolverHasStubs(JITResolver* Resolver) const { |
| 267 | MutexGuard guard(Lock); |
| 268 | for (std::map<void*, JITResolver*>::const_iterator I = Map.begin(), |
| 269 | E = Map.end(); I != E; ++I) { |
| 270 | if (I->second == Resolver) |
| 271 | return true; |
| 272 | } |
| 273 | return false; |
| 274 | } |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 275 | }; |
| 276 | /// This needs to be static so that a lazy call stub can access it with no |
| 277 | /// context except the address of the stub. |
| 278 | ManagedStatic<StubToResolverMapTy> StubToResolverMap; |
| 279 | |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 280 | /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is |
| 281 | /// used to output functions to memory for execution. |
| 282 | class JITEmitter : public JITCodeEmitter { |
| 283 | JITMemoryManager *MemMgr; |
| 284 | |
Jeffrey Yasskin | 32d7e6e | 2009-12-15 22:42:46 +0000 | [diff] [blame] | 285 | // When outputting a function stub in the context of some other function, we |
| 286 | // save BufferBegin/BufferEnd/CurBufferPtr here. |
| 287 | uint8_t *SavedBufferBegin, *SavedBufferEnd, *SavedCurBufferPtr; |
| 288 | |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 289 | // When reattempting to JIT a function after running out of space, we store |
| 290 | // the estimated size of the function we're trying to JIT here, so we can |
| 291 | // ask the memory manager for at least this much space. When we |
| 292 | // successfully emit the function, we reset this back to zero. |
| 293 | uintptr_t SizeEstimate; |
| 294 | |
| 295 | /// Relocations - These are the relocations that the function needs, as |
| 296 | /// emitted. |
| 297 | std::vector<MachineRelocation> Relocations; |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 298 | |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 299 | /// MBBLocations - This vector is a mapping from MBB ID's to their address. |
| 300 | /// It is filled in by the StartMachineBasicBlock callback and queried by |
| 301 | /// the getMachineBasicBlockAddress callback. |
| 302 | std::vector<uintptr_t> MBBLocations; |
| 303 | |
| 304 | /// ConstantPool - The constant pool for the current function. |
| 305 | /// |
| 306 | MachineConstantPool *ConstantPool; |
| 307 | |
| 308 | /// ConstantPoolBase - A pointer to the first entry in the constant pool. |
| 309 | /// |
| 310 | void *ConstantPoolBase; |
| 311 | |
| 312 | /// ConstPoolAddresses - Addresses of individual constant pool entries. |
| 313 | /// |
| 314 | SmallVector<uintptr_t, 8> ConstPoolAddresses; |
| 315 | |
| 316 | /// JumpTable - The jump tables for the current function. |
| 317 | /// |
| 318 | MachineJumpTableInfo *JumpTable; |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 319 | |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 320 | /// JumpTableBase - A pointer to the first entry in the jump table. |
| 321 | /// |
| 322 | void *JumpTableBase; |
| 323 | |
| 324 | /// Resolver - This contains info about the currently resolved functions. |
| 325 | JITResolver Resolver; |
| 326 | |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 327 | /// LabelLocations - This vector is a mapping from Label ID's to their |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 328 | /// address. |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 329 | DenseMap<MCSymbol*, uintptr_t> LabelLocations; |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 330 | |
| 331 | /// MMI - Machine module info for exception informations |
| 332 | MachineModuleInfo* MMI; |
| 333 | |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 334 | // CurFn - The llvm function being emitted. Only valid during |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 335 | // finishFunction(). |
| 336 | const Function *CurFn; |
| 337 | |
| 338 | /// Information about emitted code, which is passed to the |
| 339 | /// JITEventListeners. This is reset in startFunction and used in |
| 340 | /// finishFunction. |
| 341 | JITEvent_EmittedFunctionDetails EmissionDetails; |
| 342 | |
| 343 | struct EmittedCode { |
| 344 | void *FunctionBody; // Beginning of the function's allocation. |
| 345 | void *Code; // The address the function's code actually starts at. |
| 346 | void *ExceptionTable; |
| 347 | EmittedCode() : FunctionBody(0), Code(0), ExceptionTable(0) {} |
| 348 | }; |
| 349 | struct EmittedFunctionConfig : public ValueMapConfig<const Function*> { |
| 350 | typedef JITEmitter *ExtraData; |
| 351 | static void onDelete(JITEmitter *, const Function*); |
| 352 | static void onRAUW(JITEmitter *, const Function*, const Function*); |
| 353 | }; |
| 354 | ValueMap<const Function *, EmittedCode, |
| 355 | EmittedFunctionConfig> EmittedFunctions; |
| 356 | |
Nicolas Geoffray | 4eb3739 | 2010-04-14 22:06:37 +0000 | [diff] [blame] | 357 | DebugLoc PrevDL; |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 358 | |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 359 | /// Instance of the JIT |
| 360 | JIT *TheJIT; |
| 361 | |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 362 | public: |
| 363 | JITEmitter(JIT &jit, JITMemoryManager *JMM, TargetMachine &TM) |
| 364 | : SizeEstimate(0), Resolver(jit, *this), MMI(0), CurFn(0), |
Rafael Espindola | e449654 | 2013-05-07 20:53:59 +0000 | [diff] [blame] | 365 | EmittedFunctions(this), TheJIT(&jit) { |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 366 | MemMgr = JMM ? JMM : JITMemoryManager::CreateDefaultMemManager(); |
| 367 | if (jit.getJITInfo().needsGOT()) { |
| 368 | MemMgr->AllocateGOT(); |
David Greene | c9ec993 | 2010-01-05 01:23:36 +0000 | [diff] [blame] | 369 | DEBUG(dbgs() << "JIT is managing a GOT\n"); |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 372 | } |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 373 | ~JITEmitter() { |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 374 | delete MemMgr; |
| 375 | } |
| 376 | |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 377 | JITResolver &getJITResolver() { return Resolver; } |
| 378 | |
| 379 | virtual void startFunction(MachineFunction &F); |
| 380 | virtual bool finishFunction(MachineFunction &F); |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 381 | |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 382 | void emitConstantPool(MachineConstantPool *MCP); |
| 383 | void initJumpTableInfo(MachineJumpTableInfo *MJTI); |
| 384 | void emitJumpTableInfo(MachineJumpTableInfo *MJTI); |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 385 | |
Jeffrey Yasskin | 32d7e6e | 2009-12-15 22:42:46 +0000 | [diff] [blame] | 386 | void startGVStub(const GlobalValue* GV, |
| 387 | unsigned StubSize, unsigned Alignment = 1); |
| 388 | void startGVStub(void *Buffer, unsigned StubSize); |
| 389 | void finishGVStub(); |
| 390 | virtual void *allocIndirectGV(const GlobalValue *GV, |
| 391 | const uint8_t *Buffer, size_t Size, |
| 392 | unsigned Alignment); |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 393 | |
| 394 | /// allocateSpace - Reserves space in the current block if any, or |
| 395 | /// allocate a new one of the given size. |
| 396 | virtual void *allocateSpace(uintptr_t Size, unsigned Alignment); |
| 397 | |
| 398 | /// allocateGlobal - Allocate memory for a global. Unlike allocateSpace, |
| 399 | /// this method does not allocate memory in the current output buffer, |
| 400 | /// because a global may live longer than the current function. |
| 401 | virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment); |
| 402 | |
| 403 | virtual void addRelocation(const MachineRelocation &MR) { |
| 404 | Relocations.push_back(MR); |
| 405 | } |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 406 | |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 407 | virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) { |
| 408 | if (MBBLocations.size() <= (unsigned)MBB->getNumber()) |
| 409 | MBBLocations.resize((MBB->getNumber()+1)*2); |
| 410 | MBBLocations[MBB->getNumber()] = getCurrentPCValue(); |
Chris Lattner | 68feb22 | 2010-07-11 23:07:28 +0000 | [diff] [blame] | 411 | if (MBB->hasAddressTaken()) |
| 412 | TheJIT->addPointerToBasicBlock(MBB->getBasicBlock(), |
| 413 | (void*)getCurrentPCValue()); |
David Greene | c9ec993 | 2010-01-05 01:23:36 +0000 | [diff] [blame] | 414 | DEBUG(dbgs() << "JIT: Emitting BB" << MBB->getNumber() << " at [" |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 415 | << (void*) getCurrentPCValue() << "]\n"); |
| 416 | } |
| 417 | |
| 418 | virtual uintptr_t getConstantPoolEntryAddress(unsigned Entry) const; |
| 419 | virtual uintptr_t getJumpTableEntryAddress(unsigned Entry) const; |
| 420 | |
Chris Lattner | 68feb22 | 2010-07-11 23:07:28 +0000 | [diff] [blame] | 421 | virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const{ |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 422 | assert(MBBLocations.size() > (unsigned)MBB->getNumber() && |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 423 | MBBLocations[MBB->getNumber()] && "MBB not emitted!"); |
| 424 | return MBBLocations[MBB->getNumber()]; |
| 425 | } |
| 426 | |
| 427 | /// retryWithMoreMemory - Log a retry and deallocate all memory for the |
| 428 | /// given function. Increase the minimum allocation size so that we get |
| 429 | /// more memory next time. |
| 430 | void retryWithMoreMemory(MachineFunction &F); |
| 431 | |
| 432 | /// deallocateMemForFunction - Deallocate all memory for the specified |
| 433 | /// function body. |
| 434 | void deallocateMemForFunction(const Function *F); |
| 435 | |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 436 | virtual void processDebugLoc(DebugLoc DL, bool BeforePrintingInsn); |
| 437 | |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 438 | virtual void emitLabel(MCSymbol *Label) { |
| 439 | LabelLocations[Label] = getCurrentPCValue(); |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Bill Wendling | 47639fc | 2010-04-16 08:46:10 +0000 | [diff] [blame] | 442 | virtual DenseMap<MCSymbol*, uintptr_t> *getLabelLocations() { |
| 443 | return &LabelLocations; |
| 444 | } |
| 445 | |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 446 | virtual uintptr_t getLabelAddress(MCSymbol *Label) const { |
| 447 | assert(LabelLocations.count(Label) && "Label not emitted!"); |
| 448 | return LabelLocations.find(Label)->second; |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 449 | } |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 450 | |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 451 | virtual void setModuleInfo(MachineModuleInfo* Info) { |
| 452 | MMI = Info; |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 455 | private: |
Jeffrey Yasskin | 2d27441 | 2009-11-07 08:51:52 +0000 | [diff] [blame] | 456 | void *getPointerToGlobal(GlobalValue *GV, void *Reference, |
| 457 | bool MayNeedFarStub); |
| 458 | void *getPointerToGVIndirectSym(GlobalValue *V, void *Reference); |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 459 | }; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 462 | void CallSiteValueMapConfig::onDelete(JITResolverState *JRS, Function *F) { |
Jeffrey Yasskin | 8e98d12 | 2010-03-04 00:32:33 +0000 | [diff] [blame] | 463 | JRS->EraseAllCallSitesForPrelocked(F); |
| 464 | } |
| 465 | |
Jeffrey Yasskin | 8e98d12 | 2010-03-04 00:32:33 +0000 | [diff] [blame] | 466 | void JITResolverState::EraseAllCallSitesForPrelocked(Function *F) { |
| 467 | FunctionToCallSitesMapTy::iterator F2C = FunctionToCallSitesMap.find(F); |
| 468 | if (F2C == FunctionToCallSitesMap.end()) |
| 469 | return; |
| 470 | StubToResolverMapTy &S2RMap = *StubToResolverMap; |
| 471 | for (SmallPtrSet<void*, 1>::const_iterator I = F2C->second.begin(), |
| 472 | E = F2C->second.end(); I != E; ++I) { |
| 473 | S2RMap.UnregisterStubResolver(*I); |
| 474 | bool Erased = CallSiteToFunctionMap.erase(*I); |
| 475 | (void)Erased; |
| 476 | assert(Erased && "Missing call site->function mapping"); |
| 477 | } |
| 478 | FunctionToCallSitesMap.erase(F2C); |
| 479 | } |
| 480 | |
| 481 | void JITResolverState::EraseAllCallSitesPrelocked() { |
| 482 | StubToResolverMapTy &S2RMap = *StubToResolverMap; |
| 483 | for (CallSiteToFunctionMapTy::const_iterator |
| 484 | I = CallSiteToFunctionMap.begin(), |
| 485 | E = CallSiteToFunctionMap.end(); I != E; ++I) { |
| 486 | S2RMap.UnregisterStubResolver(I->first); |
| 487 | } |
| 488 | CallSiteToFunctionMap.clear(); |
| 489 | FunctionToCallSitesMap.clear(); |
| 490 | } |
| 491 | |
| 492 | JITResolver::~JITResolver() { |
| 493 | // No need to lock because we're in the destructor, and state isn't shared. |
| 494 | state.EraseAllCallSitesPrelocked(); |
| 495 | assert(!StubToResolverMap->ResolverHasStubs(this) && |
| 496 | "Resolver destroyed with stubs still alive."); |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 499 | /// getLazyFunctionStubIfAvailable - This returns a pointer to a function stub |
Evan Cheng | 704bff9 | 2008-11-13 21:50:50 +0000 | [diff] [blame] | 500 | /// if it has already been created. |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 501 | void *JITResolver::getLazyFunctionStubIfAvailable(Function *F) { |
Evan Cheng | 704bff9 | 2008-11-13 21:50:50 +0000 | [diff] [blame] | 502 | MutexGuard locked(TheJIT->lock); |
| 503 | |
| 504 | // If we already have a stub for this function, recycle it. |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 505 | return state.getFunctionToLazyStubMap(locked).lookup(F); |
Evan Cheng | 704bff9 | 2008-11-13 21:50:50 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 508 | /// getFunctionStub - This returns a pointer to a function stub, creating |
| 509 | /// one on demand as needed. |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 510 | void *JITResolver::getLazyFunctionStub(Function *F) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 511 | MutexGuard locked(TheJIT->lock); |
| 512 | |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 513 | // If we already have a lazy stub for this function, recycle it. |
| 514 | void *&Stub = state.getFunctionToLazyStubMap(locked)[F]; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 515 | if (Stub) return Stub; |
| 516 | |
Jeffrey Yasskin | dc85724 | 2009-10-27 20:30:28 +0000 | [diff] [blame] | 517 | // Call the lazy resolver function if we are JIT'ing lazily. Otherwise we |
| 518 | // must resolve the symbol now. |
| 519 | void *Actual = TheJIT->isCompilingLazily() |
| 520 | ? (void *)(intptr_t)LazyResolverFn : (void *)0; |
| 521 | |
Nate Begeman | b9c6c9b | 2009-03-07 06:41:19 +0000 | [diff] [blame] | 522 | // If this is an external declaration, attempt to resolve the address now |
| 523 | // to place in the stub. |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 524 | if (isNonGhostDeclaration(F) || F->hasAvailableExternallyLinkage()) { |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 525 | Actual = TheJIT->getPointerToFunction(F); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 526 | |
Dan Gohman | 69f9378 | 2009-01-05 05:32:42 +0000 | [diff] [blame] | 527 | // If we resolved the symbol to a null address (eg. a weak external) |
Jeffrey Yasskin | 6f348e4 | 2009-11-09 22:34:19 +0000 | [diff] [blame] | 528 | // don't emit a stub. Return a null pointer to the application. |
| 529 | if (!Actual) return 0; |
Dan Gohman | 69f9378 | 2009-01-05 05:32:42 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 532 | TargetJITInfo::StubLayout SL = TheJIT->getJITInfo().getStubLayout(); |
Jeffrey Yasskin | 32d7e6e | 2009-12-15 22:42:46 +0000 | [diff] [blame] | 533 | JE.startGVStub(F, SL.Size, SL.Alignment); |
Nate Begeman | b9c6c9b | 2009-03-07 06:41:19 +0000 | [diff] [blame] | 534 | // Codegen a new stub, calling the lazy resolver or the actual address of the |
| 535 | // external function, if it was resolved. |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 536 | Stub = TheJIT->getJITInfo().emitFunctionStub(F, Actual, JE); |
Jeffrey Yasskin | 32d7e6e | 2009-12-15 22:42:46 +0000 | [diff] [blame] | 537 | JE.finishGVStub(); |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 538 | |
Chris Lattner | 870286a | 2006-06-01 17:29:22 +0000 | [diff] [blame] | 539 | if (Actual != (void*)(intptr_t)LazyResolverFn) { |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 540 | // If we are getting the stub for an external function, we really want the |
| 541 | // address of the stub in the GlobalAddressMap for the JIT, not the address |
| 542 | // of the external function. |
| 543 | TheJIT->updateGlobalMapping(F, Stub); |
| 544 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 545 | |
David Greene | c9ec993 | 2010-01-05 01:23:36 +0000 | [diff] [blame] | 546 | DEBUG(dbgs() << "JIT: Lazy stub emitted at [" << Stub << "] for function '" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 547 | << F->getName() << "'\n"); |
Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 548 | |
Jeffrey Yasskin | 8e98d12 | 2010-03-04 00:32:33 +0000 | [diff] [blame] | 549 | if (TheJIT->isCompilingLazily()) { |
| 550 | // Register this JITResolver as the one corresponding to this call site so |
| 551 | // JITCompilerFn will be able to find it. |
| 552 | StubToResolverMap->RegisterStubResolver(Stub, this); |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 553 | |
Jeffrey Yasskin | 8e98d12 | 2010-03-04 00:32:33 +0000 | [diff] [blame] | 554 | // Finally, keep track of the stub-to-Function mapping so that the |
| 555 | // JITCompilerFn knows which function to compile! |
| 556 | state.AddCallSite(locked, Stub, F); |
| 557 | } else if (!Actual) { |
| 558 | // If we are JIT'ing non-lazily but need to call a function that does not |
| 559 | // exist yet, add it to the JIT's work list so that we can fill in the |
| 560 | // stub address later. |
| 561 | assert(!isNonGhostDeclaration(F) && !F->hasAvailableExternallyLinkage() && |
| 562 | "'Actual' should have been set above."); |
| 563 | TheJIT->addPendingFunction(F); |
| 564 | } |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 565 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 566 | return Stub; |
| 567 | } |
| 568 | |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 569 | /// getGlobalValueIndirectSym - Return a lazy pointer containing the specified |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 570 | /// GV address. |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 571 | void *JITResolver::getGlobalValueIndirectSym(GlobalValue *GV, void *GVAddress) { |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 572 | MutexGuard locked(TheJIT->lock); |
| 573 | |
| 574 | // If we already have a stub for this global variable, recycle it. |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 575 | void *&IndirectSym = state.getGlobalToIndirectSymMap(locked)[GV]; |
| 576 | if (IndirectSym) return IndirectSym; |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 577 | |
Evan Cheng | e4d783d | 2008-11-10 23:26:16 +0000 | [diff] [blame] | 578 | // Otherwise, codegen a new indirect symbol. |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 579 | IndirectSym = TheJIT->getJITInfo().emitGlobalValueIndirectSym(GV, GVAddress, |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 580 | JE); |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 581 | |
David Greene | c9ec993 | 2010-01-05 01:23:36 +0000 | [diff] [blame] | 582 | DEBUG(dbgs() << "JIT: Indirect symbol emitted at [" << IndirectSym |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 583 | << "] for GV '" << GV->getName() << "'\n"); |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 584 | |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 585 | return IndirectSym; |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 588 | /// getExternalFunctionStub - Return a stub for the function at the |
| 589 | /// specified address, created lazily on demand. |
| 590 | void *JITResolver::getExternalFunctionStub(void *FnAddr) { |
| 591 | // If we already have a stub for this function, recycle it. |
| 592 | void *&Stub = ExternalFnToStubMap[FnAddr]; |
| 593 | if (Stub) return Stub; |
| 594 | |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 595 | TargetJITInfo::StubLayout SL = TheJIT->getJITInfo().getStubLayout(); |
Jeffrey Yasskin | 32d7e6e | 2009-12-15 22:42:46 +0000 | [diff] [blame] | 596 | JE.startGVStub(0, SL.Size, SL.Alignment); |
Jeffrey Yasskin | e637c19 | 2009-11-07 00:00:10 +0000 | [diff] [blame] | 597 | Stub = TheJIT->getJITInfo().emitFunctionStub(0, FnAddr, JE); |
Jeffrey Yasskin | 32d7e6e | 2009-12-15 22:42:46 +0000 | [diff] [blame] | 598 | JE.finishGVStub(); |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 599 | |
David Greene | c9ec993 | 2010-01-05 01:23:36 +0000 | [diff] [blame] | 600 | DEBUG(dbgs() << "JIT: Stub emitted at [" << Stub |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 601 | << "] for external function at '" << FnAddr << "'\n"); |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 602 | return Stub; |
| 603 | } |
| 604 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 605 | unsigned JITResolver::getGOTIndexForAddr(void* addr) { |
| 606 | unsigned idx = revGOTMap[addr]; |
| 607 | if (!idx) { |
| 608 | idx = ++nextGOTIndex; |
| 609 | revGOTMap[addr] = idx; |
David Greene | c9ec993 | 2010-01-05 01:23:36 +0000 | [diff] [blame] | 610 | DEBUG(dbgs() << "JIT: Adding GOT entry " << idx << " for addr [" |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 611 | << addr << "]\n"); |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 612 | } |
| 613 | return idx; |
| 614 | } |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 615 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 616 | /// JITCompilerFn - This function is called when a lazy compilation stub has |
| 617 | /// been entered. It looks up which function this stub corresponds to, compiles |
| 618 | /// it if necessary, then returns the resultant function pointer. |
| 619 | void *JITResolver::JITCompilerFn(void *Stub) { |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 620 | JITResolver *JR = StubToResolverMap->getResolverFromStub(Stub); |
| 621 | assert(JR && "Unable to find the corresponding JITResolver to the call site"); |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 622 | |
Nicolas Geoffray | dcb31e1 | 2008-10-03 07:27:08 +0000 | [diff] [blame] | 623 | Function* F = 0; |
| 624 | void* ActualPtr = 0; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 625 | |
Nicolas Geoffray | dcb31e1 | 2008-10-03 07:27:08 +0000 | [diff] [blame] | 626 | { |
| 627 | // Only lock for getting the Function. The call getPointerToFunction made |
| 628 | // in this function might trigger function materializing, which requires |
| 629 | // JIT lock to be unlocked. |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 630 | MutexGuard locked(JR->TheJIT->lock); |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 631 | |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 632 | // The address given to us for the stub may not be exactly right, it might |
| 633 | // be a little bit after the stub. As such, use upper_bound to find it. |
Jay Foad | 5b24017 | 2011-04-13 12:46:01 +0000 | [diff] [blame] | 634 | std::pair<void*, Function*> I = |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 635 | JR->state.LookupFunctionFromCallSite(locked, Stub); |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 636 | F = I.second; |
| 637 | ActualPtr = I.first; |
Nicolas Geoffray | dcb31e1 | 2008-10-03 07:27:08 +0000 | [diff] [blame] | 638 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 639 | |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 640 | // If we have already code generated the function, just return the address. |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 641 | void *Result = JR->TheJIT->getPointerToGlobalIfAvailable(F); |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 642 | |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 643 | if (!Result) { |
| 644 | // Otherwise we don't have it, do lazy compilation now. |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 645 | |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 646 | // If lazy compilation is disabled, emit a useful error message and abort. |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 647 | if (!JR->TheJIT->isCompilingLazily()) { |
Jim Grosbach | 124d033 | 2011-03-16 01:21:55 +0000 | [diff] [blame] | 648 | report_fatal_error("LLVM JIT requested to do lazy compilation of" |
| 649 | " function '" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 650 | + F->getName() + "' when lazy compiles are disabled!"); |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 651 | } |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 652 | |
David Greene | c9ec993 | 2010-01-05 01:23:36 +0000 | [diff] [blame] | 653 | DEBUG(dbgs() << "JIT: Lazily resolving function '" << F->getName() |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 654 | << "' In stub ptr = " << Stub << " actual ptr = " |
| 655 | << ActualPtr << "\n"); |
Duncan Sands | 1f6a329 | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 656 | (void)ActualPtr; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 657 | |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 658 | Result = JR->TheJIT->getPointerToFunction(F); |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 659 | } |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 660 | |
| 661 | // Reacquire the lock to update the GOT map. |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 662 | MutexGuard locked(JR->TheJIT->lock); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 663 | |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 664 | // We might like to remove the call site from the CallSiteToFunction map, but |
| 665 | // we can't do that! Multiple threads could be stuck, waiting to acquire the |
| 666 | // lock above. As soon as the 1st function finishes compiling the function, |
| 667 | // the next one will be released, and needs to be able to find the function it |
| 668 | // needs to call. |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 669 | |
| 670 | // FIXME: We could rewrite all references to this stub if we knew them. |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 671 | |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 672 | // What we will do is set the compiled function address to map to the |
| 673 | // same GOT entry as the stub so that later clients may update the GOT |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 674 | // if they see it still using the stub address. |
| 675 | // Note: this is done so the Resolver doesn't have to manage GOT memory |
| 676 | // Do this without allocating map space if the target isn't using a GOT |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 677 | if(JR->revGOTMap.find(Stub) != JR->revGOTMap.end()) |
| 678 | JR->revGOTMap[Result] = JR->revGOTMap[Stub]; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 679 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 680 | return Result; |
| 681 | } |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 682 | |
Chris Lattner | 8ac66c1 | 2008-04-04 05:51:42 +0000 | [diff] [blame] | 683 | //===----------------------------------------------------------------------===// |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 684 | // JITEmitter code. |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 685 | // |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 686 | void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference, |
Jeffrey Yasskin | 2d27441 | 2009-11-07 08:51:52 +0000 | [diff] [blame] | 687 | bool MayNeedFarStub) { |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 688 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 689 | return TheJIT->getOrEmitGlobalVariable(GV); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 690 | |
Chris Lattner | 18e0459 | 2008-06-25 20:21:35 +0000 | [diff] [blame] | 691 | if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) |
Anton Korobeynikov | 19e861a | 2008-09-09 20:05:04 +0000 | [diff] [blame] | 692 | return TheJIT->getPointerToGlobal(GA->resolveAliasedGlobal(false)); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 693 | |
| 694 | // If we have already compiled the function, return a pointer to its body. |
| 695 | Function *F = cast<Function>(V); |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 696 | |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 697 | void *FnStub = Resolver.getLazyFunctionStubIfAvailable(F); |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 698 | if (FnStub) { |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 699 | // Return the function stub if it's already created. We do this first so |
| 700 | // that we're returning the same address for the function as any previous |
| 701 | // call. TODO: Yes, this is wrong. The lazy stub isn't guaranteed to be |
| 702 | // close enough to call. |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 703 | return FnStub; |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 704 | } |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 705 | |
Jeffrey Yasskin | e03a39b | 2009-11-19 23:42:58 +0000 | [diff] [blame] | 706 | // If we know the target can handle arbitrary-distance calls, try to |
| 707 | // return a direct pointer. |
| 708 | if (!MayNeedFarStub) { |
| 709 | // If we have code, go ahead and return that. |
| 710 | void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F); |
| 711 | if (ResultPtr) return ResultPtr; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 712 | |
Jeffrey Yasskin | e03a39b | 2009-11-19 23:42:58 +0000 | [diff] [blame] | 713 | // If this is an external function pointer, we can force the JIT to |
| 714 | // 'compile' it, which really just adds it to the map. |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 715 | if (isNonGhostDeclaration(F) || F->hasAvailableExternallyLinkage()) |
Jeffrey Yasskin | e03a39b | 2009-11-19 23:42:58 +0000 | [diff] [blame] | 716 | return TheJIT->getPointerToFunction(F); |
| 717 | } |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 718 | |
Jeffrey Yasskin | 39c75f2 | 2010-03-04 19:45:09 +0000 | [diff] [blame] | 719 | // Otherwise, we may need a to emit a stub, and, conservatively, we always do |
| 720 | // so. Note that it's possible to return null from getLazyFunctionStub in the |
| 721 | // case of a weak extern that fails to resolve. |
| 722 | return Resolver.getLazyFunctionStub(F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Jeffrey Yasskin | 2d27441 | 2009-11-07 08:51:52 +0000 | [diff] [blame] | 725 | void *JITEmitter::getPointerToGVIndirectSym(GlobalValue *V, void *Reference) { |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 726 | // Make sure GV is emitted first, and create a stub containing the fully |
| 727 | // resolved address. |
Jeffrey Yasskin | 2d27441 | 2009-11-07 08:51:52 +0000 | [diff] [blame] | 728 | void *GVAddress = getPointerToGlobal(V, Reference, false); |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 729 | void *StubAddr = Resolver.getGlobalValueIndirectSym(V, GVAddress); |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 730 | return StubAddr; |
| 731 | } |
| 732 | |
Devang Patel | 02c0423 | 2009-10-06 03:04:58 +0000 | [diff] [blame] | 733 | void JITEmitter::processDebugLoc(DebugLoc DL, bool BeforePrintingInsn) { |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 734 | if (DL.isUnknown()) return; |
| 735 | if (!BeforePrintingInsn) return; |
Jim Grosbach | 124d033 | 2011-03-16 01:21:55 +0000 | [diff] [blame] | 736 | |
Chris Lattner | 134d8ee | 2010-07-22 21:17:55 +0000 | [diff] [blame] | 737 | const LLVMContext &Context = EmissionDetails.MF->getFunction()->getContext(); |
Nicolas Geoffray | 4eb3739 | 2010-04-14 22:06:37 +0000 | [diff] [blame] | 738 | |
| 739 | if (DL.getScope(Context) != 0 && PrevDL != DL) { |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 740 | JITEvent_EmittedFunctionDetails::LineStart NextLine; |
| 741 | NextLine.Address = getCurrentPCValue(); |
| 742 | NextLine.Loc = DL; |
| 743 | EmissionDetails.LineStarts.push_back(NextLine); |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 744 | } |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 745 | |
Nicolas Geoffray | 4eb3739 | 2010-04-14 22:06:37 +0000 | [diff] [blame] | 746 | PrevDL = DL; |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 749 | static unsigned GetConstantPoolSizeInBytes(MachineConstantPool *MCP, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 750 | const DataLayout *TD) { |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 751 | const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants(); |
| 752 | if (Constants.empty()) return 0; |
| 753 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 754 | unsigned Size = 0; |
| 755 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
| 756 | MachineConstantPoolEntry CPE = Constants[i]; |
| 757 | unsigned AlignMask = CPE.getAlignment() - 1; |
| 758 | Size = (Size + AlignMask) & ~AlignMask; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 759 | Type *Ty = CPE.getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 760 | Size += TD->getTypeAllocSize(Ty); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 761 | } |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 762 | return Size; |
| 763 | } |
| 764 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 765 | void JITEmitter::startFunction(MachineFunction &F) { |
David Greene | c9ec993 | 2010-01-05 01:23:36 +0000 | [diff] [blame] | 766 | DEBUG(dbgs() << "JIT: Starting CodeGen of Function " |
Craig Topper | 96601ca | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 767 | << F.getName() << "\n"); |
Evan Cheng | eb5d95a | 2008-11-06 17:46:04 +0000 | [diff] [blame] | 768 | |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 769 | uintptr_t ActualSize = 0; |
Jim Grosbach | cce6c29 | 2008-10-03 16:17:20 +0000 | [diff] [blame] | 770 | // Set the memory writable, if it's not already |
| 771 | MemMgr->setMemoryWritable(); |
Jim Grosbach | 124d033 | 2011-03-16 01:21:55 +0000 | [diff] [blame] | 772 | |
Chris Lattner | 134d8ee | 2010-07-22 21:17:55 +0000 | [diff] [blame] | 773 | if (SizeEstimate > 0) { |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 774 | // SizeEstimate will be non-zero on reallocation attempts. |
| 775 | ActualSize = SizeEstimate; |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 778 | BufferBegin = CurBufferPtr = MemMgr->startFunctionBody(F.getFunction(), |
| 779 | ActualSize); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 780 | BufferEnd = BufferBegin+ActualSize; |
Jeffrey Yasskin | 1e86132 | 2009-10-20 18:13:21 +0000 | [diff] [blame] | 781 | EmittedFunctions[F.getFunction()].FunctionBody = BufferBegin; |
| 782 | |
Evan Cheng | 9a1e9b9 | 2006-11-16 20:04:54 +0000 | [diff] [blame] | 783 | // Ensure the constant pool/jump table info is at least 4-byte aligned. |
| 784 | emitAlignment(16); |
| 785 | |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 786 | emitConstantPool(F.getConstantPool()); |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 787 | if (MachineJumpTableInfo *MJTI = F.getJumpTableInfo()) |
| 788 | initJumpTableInfo(MJTI); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 789 | |
| 790 | // About to start emitting the machine code for the function. |
Chris Lattner | 0eb4d6b | 2006-05-03 01:03:20 +0000 | [diff] [blame] | 791 | emitAlignment(std::max(F.getFunction()->getAlignment(), 8U)); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 792 | TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr); |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 793 | EmittedFunctions[F.getFunction()].Code = CurBufferPtr; |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 794 | |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 795 | MBBLocations.clear(); |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 796 | |
| 797 | EmissionDetails.MF = &F; |
| 798 | EmissionDetails.LineStarts.clear(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 801 | bool JITEmitter::finishFunction(MachineFunction &F) { |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 802 | if (CurBufferPtr == BufferEnd) { |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 803 | // We must call endFunctionBody before retrying, because |
| 804 | // deallocateMemForFunction requires it. |
| 805 | MemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr); |
| 806 | retryWithMoreMemory(F); |
| 807 | return true; |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 808 | } |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 809 | |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 810 | if (MachineJumpTableInfo *MJTI = F.getJumpTableInfo()) |
| 811 | emitJumpTableInfo(MJTI); |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 812 | |
Chris Lattner | a827953 | 2006-06-16 18:09:26 +0000 | [diff] [blame] | 813 | // FnStart is the start of the text, not the start of the constant pool and |
| 814 | // other per-function data. |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 815 | uint8_t *FnStart = |
| 816 | (uint8_t *)TheJIT->getPointerToGlobalIfAvailable(F.getFunction()); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 817 | |
Argyrios Kyrtzidis | 19fee41 | 2009-04-30 23:01:58 +0000 | [diff] [blame] | 818 | // FnEnd is the end of the function's machine code. |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 819 | uint8_t *FnEnd = CurBufferPtr; |
Argyrios Kyrtzidis | 19fee41 | 2009-04-30 23:01:58 +0000 | [diff] [blame] | 820 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 821 | if (!Relocations.empty()) { |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 822 | CurFn = F.getFunction(); |
Chris Lattner | e884dc2 | 2005-07-20 16:29:20 +0000 | [diff] [blame] | 823 | NumRelos += Relocations.size(); |
| 824 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 825 | // Resolve the relocations to concrete pointers. |
| 826 | for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { |
| 827 | MachineRelocation &MR = Relocations[i]; |
Evan Cheng | 9200605 | 2008-11-03 07:14:02 +0000 | [diff] [blame] | 828 | void *ResultPtr = 0; |
Evan Cheng | ef5784e | 2008-10-29 23:54:46 +0000 | [diff] [blame] | 829 | if (!MR.letTargetResolve()) { |
Evan Cheng | d7398c9 | 2008-11-08 07:37:34 +0000 | [diff] [blame] | 830 | if (MR.isExternalSymbol()) { |
Dan Gohman | 69f9378 | 2009-01-05 05:32:42 +0000 | [diff] [blame] | 831 | ResultPtr = TheJIT->getPointerToNamedFunction(MR.getExternalSymbol(), |
| 832 | false); |
David Greene | c9ec993 | 2010-01-05 01:23:36 +0000 | [diff] [blame] | 833 | DEBUG(dbgs() << "JIT: Map \'" << MR.getExternalSymbol() << "\' to [" |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 834 | << ResultPtr << "]\n"); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 835 | |
Evan Cheng | ef5784e | 2008-10-29 23:54:46 +0000 | [diff] [blame] | 836 | // If the target REALLY wants a stub for this function, emit it now. |
Jeffrey Yasskin | 2d27441 | 2009-11-07 08:51:52 +0000 | [diff] [blame] | 837 | if (MR.mayNeedFarStub()) { |
Jeffrey Yasskin | 6f348e4 | 2009-11-09 22:34:19 +0000 | [diff] [blame] | 838 | ResultPtr = Resolver.getExternalFunctionStub(ResultPtr); |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 839 | } |
Evan Cheng | ef5784e | 2008-10-29 23:54:46 +0000 | [diff] [blame] | 840 | } else if (MR.isGlobalValue()) { |
| 841 | ResultPtr = getPointerToGlobal(MR.getGlobalValue(), |
| 842 | BufferBegin+MR.getMachineCodeOffset(), |
Jeffrey Yasskin | 2d27441 | 2009-11-07 08:51:52 +0000 | [diff] [blame] | 843 | MR.mayNeedFarStub()); |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 844 | } else if (MR.isIndirectSymbol()) { |
Jeffrey Yasskin | 2d27441 | 2009-11-07 08:51:52 +0000 | [diff] [blame] | 845 | ResultPtr = getPointerToGVIndirectSym( |
| 846 | MR.getGlobalValue(), BufferBegin+MR.getMachineCodeOffset()); |
Evan Cheng | ef5784e | 2008-10-29 23:54:46 +0000 | [diff] [blame] | 847 | } else if (MR.isBasicBlock()) { |
| 848 | ResultPtr = (void*)getMachineBasicBlockAddress(MR.getBasicBlock()); |
| 849 | } else if (MR.isConstantPoolIndex()) { |
Jim Grosbach | 124d033 | 2011-03-16 01:21:55 +0000 | [diff] [blame] | 850 | ResultPtr = |
| 851 | (void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex()); |
Evan Cheng | ef5784e | 2008-10-29 23:54:46 +0000 | [diff] [blame] | 852 | } else { |
| 853 | assert(MR.isJumpTableIndex()); |
| 854 | ResultPtr=(void*)getJumpTableEntryAddress(MR.getJumpTableIndex()); |
| 855 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 856 | |
Evan Cheng | ef5784e | 2008-10-29 23:54:46 +0000 | [diff] [blame] | 857 | MR.setResultPointer(ResultPtr); |
| 858 | } |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 859 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 860 | // if we are managing the GOT and the relocation wants an index, |
| 861 | // give it one |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 862 | if (MR.isGOTRelative() && MemMgr->isManagingGOT()) { |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 863 | unsigned idx = Resolver.getGOTIndexForAddr(ResultPtr); |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 864 | MR.setGOTIndex(idx); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 865 | if (((void**)MemMgr->getGOTBase())[idx] != ResultPtr) { |
David Greene | c9ec993 | 2010-01-05 01:23:36 +0000 | [diff] [blame] | 866 | DEBUG(dbgs() << "JIT: GOT was out of date for " << ResultPtr |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 867 | << " pointing at " << ((void**)MemMgr->getGOTBase())[idx] |
| 868 | << "\n"); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 869 | ((void**)MemMgr->getGOTBase())[idx] = ResultPtr; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 870 | } |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 871 | } |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 874 | CurFn = 0; |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 875 | TheJIT->getJITInfo().relocate(BufferBegin, &Relocations[0], |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 876 | Relocations.size(), MemMgr->getGOTBase()); |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Chris Lattner | d2d5c76 | 2006-05-03 18:55:56 +0000 | [diff] [blame] | 879 | // Update the GOT entry for F to point to the new code. |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 880 | if (MemMgr->isManagingGOT()) { |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 881 | unsigned idx = Resolver.getGOTIndexForAddr((void*)BufferBegin); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 882 | if (((void**)MemMgr->getGOTBase())[idx] != (void*)BufferBegin) { |
David Greene | c9ec993 | 2010-01-05 01:23:36 +0000 | [diff] [blame] | 883 | DEBUG(dbgs() << "JIT: GOT was out of date for " << (void*)BufferBegin |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 884 | << " pointing at " << ((void**)MemMgr->getGOTBase())[idx] |
| 885 | << "\n"); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 886 | ((void**)MemMgr->getGOTBase())[idx] = (void*)BufferBegin; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
Argyrios Kyrtzidis | 19fee41 | 2009-04-30 23:01:58 +0000 | [diff] [blame] | 890 | // CurBufferPtr may have moved beyond FnEnd, due to memory allocation for |
| 891 | // global variables that were referenced in the relocations. |
| 892 | MemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr); |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 893 | |
| 894 | if (CurBufferPtr == BufferEnd) { |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 895 | retryWithMoreMemory(F); |
| 896 | return true; |
| 897 | } else { |
| 898 | // Now that we've succeeded in emitting the function, reset the |
| 899 | // SizeEstimate back down to zero. |
| 900 | SizeEstimate = 0; |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 903 | BufferBegin = CurBufferPtr = 0; |
| 904 | NumBytes += FnEnd-FnStart; |
| 905 | |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 906 | // Invalidate the icache if necessary. |
Chris Lattner | bc52cad | 2008-06-25 17:18:44 +0000 | [diff] [blame] | 907 | sys::Memory::InvalidateInstructionCache(FnStart, FnEnd-FnStart); |
Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 908 | |
Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 909 | TheJIT->NotifyFunctionEmitted(*F.getFunction(), FnStart, FnEnd-FnStart, |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 910 | EmissionDetails); |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 911 | |
Nicolas Geoffray | 4eb3739 | 2010-04-14 22:06:37 +0000 | [diff] [blame] | 912 | // Reset the previous debug location. |
| 913 | PrevDL = DebugLoc(); |
| 914 | |
David Greene | c9ec993 | 2010-01-05 01:23:36 +0000 | [diff] [blame] | 915 | DEBUG(dbgs() << "JIT: Finished CodeGen of [" << (void*)FnStart |
Craig Topper | 96601ca | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 916 | << "] Function: " << F.getName() |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 917 | << ": " << (FnEnd-FnStart) << " bytes of text, " |
| 918 | << Relocations.size() << " relocations\n"); |
Argyrios Kyrtzidis | b3a847d | 2009-05-18 21:06:40 +0000 | [diff] [blame] | 919 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 920 | Relocations.clear(); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 921 | ConstPoolAddresses.clear(); |
Anton Korobeynikov | 8cd4c3e | 2007-01-19 17:25:17 +0000 | [diff] [blame] | 922 | |
Evan Cheng | bc4707a | 2008-09-18 07:54:21 +0000 | [diff] [blame] | 923 | // Mark code region readable and executable if it's not so already. |
Jim Grosbach | cce6c29 | 2008-10-03 16:17:20 +0000 | [diff] [blame] | 924 | MemMgr->setMemoryExecutable(); |
Evan Cheng | bc4707a | 2008-09-18 07:54:21 +0000 | [diff] [blame] | 925 | |
Bill Wendling | 69c128f | 2010-04-18 00:52:08 +0000 | [diff] [blame] | 926 | DEBUG({ |
| 927 | if (sys::hasDisassembler()) { |
| 928 | dbgs() << "JIT: Disassembled code:\n"; |
| 929 | dbgs() << sys::disassembleBuffer(FnStart, FnEnd-FnStart, |
| 930 | (uintptr_t)FnStart); |
| 931 | } else { |
| 932 | dbgs() << "JIT: Binary code:\n"; |
| 933 | uint8_t* q = FnStart; |
| 934 | for (int i = 0; q < FnEnd; q += 4, ++i) { |
| 935 | if (i == 4) |
| 936 | i = 0; |
| 937 | if (i == 0) |
| 938 | dbgs() << "JIT: " << (long)(q - FnStart) << ": "; |
| 939 | bool Done = false; |
| 940 | for (int j = 3; j >= 0; --j) { |
| 941 | if (q + j >= FnEnd) |
| 942 | Done = true; |
| 943 | else |
| 944 | dbgs() << (unsigned short)q[j]; |
| 945 | } |
| 946 | if (Done) |
| 947 | break; |
| 948 | dbgs() << ' '; |
| 949 | if (i == 3) |
| 950 | dbgs() << '\n'; |
Evan Cheng | e7c3551 | 2008-11-12 08:22:43 +0000 | [diff] [blame] | 951 | } |
Bill Wendling | 69c128f | 2010-04-18 00:52:08 +0000 | [diff] [blame] | 952 | dbgs()<< '\n'; |
Evan Cheng | a7916f5 | 2008-11-05 23:44:08 +0000 | [diff] [blame] | 953 | } |
Bill Wendling | 69c128f | 2010-04-18 00:52:08 +0000 | [diff] [blame] | 954 | }); |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 955 | |
Evan Cheng | 252ddfb | 2008-09-02 08:14:01 +0000 | [diff] [blame] | 956 | if (MMI) |
| 957 | MMI->EndFunction(); |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 958 | |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 959 | return false; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 962 | void JITEmitter::retryWithMoreMemory(MachineFunction &F) { |
David Greene | c9ec993 | 2010-01-05 01:23:36 +0000 | [diff] [blame] | 963 | DEBUG(dbgs() << "JIT: Ran out of space for native code. Reattempting.\n"); |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 964 | Relocations.clear(); // Clear the old relocations or we'll reapply them. |
| 965 | ConstPoolAddresses.clear(); |
| 966 | ++NumRetries; |
| 967 | deallocateMemForFunction(F.getFunction()); |
| 968 | // Try again with at least twice as much free space. |
| 969 | SizeEstimate = (uintptr_t)(2 * (BufferEnd - BufferBegin)); |
Chris Lattner | 68feb22 | 2010-07-11 23:07:28 +0000 | [diff] [blame] | 970 | |
| 971 | for (MachineFunction::iterator MBB = F.begin(), E = F.end(); MBB != E; ++MBB){ |
| 972 | if (MBB->hasAddressTaken()) |
| 973 | TheJIT->clearPointerToBasicBlock(MBB->getBasicBlock()); |
| 974 | } |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 977 | /// deallocateMemForFunction - Deallocate all memory for the specified |
| 978 | /// function body. Also drop any references the function has to stubs. |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 979 | /// May be called while the Function is being destroyed inside ~Value(). |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 980 | void JITEmitter::deallocateMemForFunction(const Function *F) { |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 981 | ValueMap<const Function *, EmittedCode, EmittedFunctionConfig>::iterator |
| 982 | Emitted = EmittedFunctions.find(F); |
Jeffrey Yasskin | 1e86132 | 2009-10-20 18:13:21 +0000 | [diff] [blame] | 983 | if (Emitted != EmittedFunctions.end()) { |
| 984 | MemMgr->deallocateFunctionBody(Emitted->second.FunctionBody); |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 985 | TheJIT->NotifyFreeingMachineCode(Emitted->second.Code); |
| 986 | |
Jeffrey Yasskin | 1e86132 | 2009-10-20 18:13:21 +0000 | [diff] [blame] | 987 | EmittedFunctions.erase(Emitted); |
| 988 | } |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 992 | void *JITEmitter::allocateSpace(uintptr_t Size, unsigned Alignment) { |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 993 | if (BufferBegin) |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 994 | return JITCodeEmitter::allocateSpace(Size, Alignment); |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 995 | |
| 996 | // create a new memory block if there is no active one. |
| 997 | // care must be taken so that BufferBegin is invalidated when a |
| 998 | // block is trimmed |
| 999 | BufferBegin = CurBufferPtr = MemMgr->allocateSpace(Size, Alignment); |
| 1000 | BufferEnd = BufferBegin+Size; |
| 1001 | return CurBufferPtr; |
| 1002 | } |
| 1003 | |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 1004 | void *JITEmitter::allocateGlobal(uintptr_t Size, unsigned Alignment) { |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 1005 | // Delegate this call through the memory manager. |
| 1006 | return MemMgr->allocateGlobal(Size, Alignment); |
| 1007 | } |
| 1008 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 1009 | void JITEmitter::emitConstantPool(MachineConstantPool *MCP) { |
Evan Cheng | 47c01a0 | 2008-11-07 09:02:17 +0000 | [diff] [blame] | 1010 | if (TheJIT->getJITInfo().hasCustomConstantPool()) |
Jim Grosbach | 8fe9535 | 2008-10-30 23:44:39 +0000 | [diff] [blame] | 1011 | return; |
Evan Cheng | 47c01a0 | 2008-11-07 09:02:17 +0000 | [diff] [blame] | 1012 | |
Chris Lattner | fa77d43 | 2006-02-09 04:22:52 +0000 | [diff] [blame] | 1013 | const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants(); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 1014 | if (Constants.empty()) return; |
| 1015 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1016 | unsigned Size = GetConstantPoolSizeInBytes(MCP, TheJIT->getDataLayout()); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 1017 | unsigned Align = MCP->getConstantPoolAlignment(); |
Evan Cheng | 97b8c40 | 2008-04-12 00:22:01 +0000 | [diff] [blame] | 1018 | ConstantPoolBase = allocateSpace(Size, Align); |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 1019 | ConstantPool = MCP; |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 1020 | |
| 1021 | if (ConstantPoolBase == 0) return; // Buffer overflow. |
| 1022 | |
David Greene | c9ec993 | 2010-01-05 01:23:36 +0000 | [diff] [blame] | 1023 | DEBUG(dbgs() << "JIT: Emitted constant pool at [" << ConstantPoolBase |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1024 | << "] (size: " << Size << ", alignment: " << Align << ")\n"); |
Evan Cheng | 97b8c40 | 2008-04-12 00:22:01 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 1026 | // Initialize the memory for all of the constant pool entries. |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 1027 | unsigned Offset = 0; |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 1028 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 1029 | MachineConstantPoolEntry CPE = Constants[i]; |
| 1030 | unsigned AlignMask = CPE.getAlignment() - 1; |
| 1031 | Offset = (Offset + AlignMask) & ~AlignMask; |
| 1032 | |
| 1033 | uintptr_t CAddr = (uintptr_t)ConstantPoolBase + Offset; |
| 1034 | ConstPoolAddresses.push_back(CAddr); |
| 1035 | if (CPE.isMachineConstantPoolEntry()) { |
Evan Cheng | cd5731d | 2006-09-12 20:59:59 +0000 | [diff] [blame] | 1036 | // FIXME: add support to lower machine constant pool values into bytes! |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 1037 | report_fatal_error("Initialize memory with machine specific constant pool" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1038 | "entry has not been implemented!"); |
Evan Cheng | cd5731d | 2006-09-12 20:59:59 +0000 | [diff] [blame] | 1039 | } |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 1040 | TheJIT->InitializeMemory(CPE.Val.ConstVal, (void*)CAddr); |
David Greene | c9ec993 | 2010-01-05 01:23:36 +0000 | [diff] [blame] | 1041 | DEBUG(dbgs() << "JIT: CP" << i << " at [0x"; |
| 1042 | dbgs().write_hex(CAddr) << "]\n"); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 1043 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1044 | Type *Ty = CPE.Val.ConstVal->getType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1045 | Offset += TheJIT->getDataLayout()->getTypeAllocSize(Ty); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1049 | void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) { |
Evan Cheng | 47c01a0 | 2008-11-07 09:02:17 +0000 | [diff] [blame] | 1050 | if (TheJIT->getJITInfo().hasCustomJumpTables()) |
| 1051 | return; |
Richard Osborne | 95da605 | 2010-03-11 14:58:16 +0000 | [diff] [blame] | 1052 | if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_Inline) |
| 1053 | return; |
Evan Cheng | 47c01a0 | 2008-11-07 09:02:17 +0000 | [diff] [blame] | 1054 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1055 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 1056 | if (JT.empty()) return; |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 1057 | |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 1058 | unsigned NumEntries = 0; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1059 | for (unsigned i = 0, e = JT.size(); i != e; ++i) |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 1060 | NumEntries += JT[i].MBBs.size(); |
| 1061 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1062 | unsigned EntrySize = MJTI->getEntrySize(*TheJIT->getDataLayout()); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 1063 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1064 | // Just allocate space for all the jump tables now. We will fix up the actual |
| 1065 | // MBB entries in the tables after we emit the code for each block, since then |
| 1066 | // we will know the final locations of the MBBs in memory. |
| 1067 | JumpTable = MJTI; |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 1068 | JumpTableBase = allocateSpace(NumEntries * EntrySize, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1069 | MJTI->getEntryAlignment(*TheJIT->getDataLayout())); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Jim Laskey | b92767a | 2006-12-14 22:53:42 +0000 | [diff] [blame] | 1072 | void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) { |
Evan Cheng | 47c01a0 | 2008-11-07 09:02:17 +0000 | [diff] [blame] | 1073 | if (TheJIT->getJITInfo().hasCustomJumpTables()) |
| 1074 | return; |
| 1075 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1076 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 1077 | if (JT.empty() || JumpTableBase == 0) return; |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 1078 | |
Jim Grosbach | 124d033 | 2011-03-16 01:21:55 +0000 | [diff] [blame] | 1079 | |
Chris Lattner | 13af11a | 2010-01-26 03:47:15 +0000 | [diff] [blame] | 1080 | switch (MJTI->getEntryKind()) { |
Richard Osborne | 95da605 | 2010-03-11 14:58:16 +0000 | [diff] [blame] | 1081 | case MachineJumpTableInfo::EK_Inline: |
| 1082 | return; |
Chris Lattner | 13af11a | 2010-01-26 03:47:15 +0000 | [diff] [blame] | 1083 | case MachineJumpTableInfo::EK_BlockAddress: { |
| 1084 | // EK_BlockAddress - Each entry is a plain address of block, e.g.: |
| 1085 | // .word LBB123 |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1086 | assert(MJTI->getEntrySize(*TheJIT->getDataLayout()) == sizeof(void*) && |
Chris Lattner | 13af11a | 2010-01-26 03:47:15 +0000 | [diff] [blame] | 1087 | "Cross JIT'ing?"); |
Jim Grosbach | 124d033 | 2011-03-16 01:21:55 +0000 | [diff] [blame] | 1088 | |
Chris Lattner | 13af11a | 2010-01-26 03:47:15 +0000 | [diff] [blame] | 1089 | // For each jump table, map each target in the jump table to the address of |
| 1090 | // an emitted MachineBasicBlock. |
| 1091 | intptr_t *SlotPtr = (intptr_t*)JumpTableBase; |
Jim Grosbach | 124d033 | 2011-03-16 01:21:55 +0000 | [diff] [blame] | 1092 | |
Chris Lattner | 13af11a | 2010-01-26 03:47:15 +0000 | [diff] [blame] | 1093 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
| 1094 | const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs; |
| 1095 | // Store the address of the basic block for this jump table slot in the |
| 1096 | // memory we allocated for the jump table in 'initJumpTableInfo' |
| 1097 | for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) |
| 1098 | *SlotPtr++ = getMachineBasicBlockAddress(MBBs[mi]); |
| 1099 | } |
| 1100 | break; |
| 1101 | } |
Jim Grosbach | 124d033 | 2011-03-16 01:21:55 +0000 | [diff] [blame] | 1102 | |
Chris Lattner | 85fe078 | 2010-01-26 04:05:28 +0000 | [diff] [blame] | 1103 | case MachineJumpTableInfo::EK_Custom32: |
Chris Lattner | 13af11a | 2010-01-26 03:47:15 +0000 | [diff] [blame] | 1104 | case MachineJumpTableInfo::EK_GPRel32BlockAddress: |
| 1105 | case MachineJumpTableInfo::EK_LabelDifference32: { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1106 | assert(MJTI->getEntrySize(*TheJIT->getDataLayout()) == 4&&"Cross JIT'ing?"); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1107 | // For each jump table, place the offset from the beginning of the table |
| 1108 | // to the target address. |
| 1109 | int *SlotPtr = (int*)JumpTableBase; |
Chris Lattner | 32ca55f | 2006-05-03 00:13:06 +0000 | [diff] [blame] | 1110 | |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1111 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
| 1112 | const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs; |
| 1113 | // Store the offset of the basic block for this jump table slot in the |
| 1114 | // memory we allocated for the jump table in 'initJumpTableInfo' |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 1115 | uintptr_t Base = (uintptr_t)SlotPtr; |
Evan Cheng | 2a3e08b | 2008-01-05 02:26:58 +0000 | [diff] [blame] | 1116 | for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) { |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 1117 | uintptr_t MBBAddr = getMachineBasicBlockAddress(MBBs[mi]); |
Chris Lattner | 13af11a | 2010-01-26 03:47:15 +0000 | [diff] [blame] | 1118 | /// FIXME: USe EntryKind instead of magic "getPICJumpTableEntry" hook. |
Evan Cheng | 2a3e08b | 2008-01-05 02:26:58 +0000 | [diff] [blame] | 1119 | *SlotPtr++ = TheJIT->getJITInfo().getPICJumpTableEntry(MBBAddr, Base); |
| 1120 | } |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1121 | } |
Chris Lattner | 13af11a | 2010-01-26 03:47:15 +0000 | [diff] [blame] | 1122 | break; |
| 1123 | } |
Akira Hatanaka | 6c2cf8b | 2012-02-03 04:33:00 +0000 | [diff] [blame] | 1124 | case MachineJumpTableInfo::EK_GPRel64BlockAddress: |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1125 | llvm_unreachable( |
Akira Hatanaka | 6c2cf8b | 2012-02-03 04:33:00 +0000 | [diff] [blame] | 1126 | "JT Info emission not implemented for GPRel64BlockAddress yet."); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | |
Jeffrey Yasskin | 32d7e6e | 2009-12-15 22:42:46 +0000 | [diff] [blame] | 1130 | void JITEmitter::startGVStub(const GlobalValue* GV, |
Jeffrey Yasskin | 0261d79 | 2009-11-23 22:49:00 +0000 | [diff] [blame] | 1131 | unsigned StubSize, unsigned Alignment) { |
Jeffrey Yasskin | 32d7e6e | 2009-12-15 22:42:46 +0000 | [diff] [blame] | 1132 | SavedBufferBegin = BufferBegin; |
| 1133 | SavedBufferEnd = BufferEnd; |
| 1134 | SavedCurBufferPtr = CurBufferPtr; |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 1135 | |
Evan Cheng | ce4a70b | 2008-11-08 08:02:53 +0000 | [diff] [blame] | 1136 | BufferBegin = CurBufferPtr = MemMgr->allocateStub(GV, StubSize, Alignment); |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 1137 | BufferEnd = BufferBegin+StubSize+1; |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Jeffrey Yasskin | 32d7e6e | 2009-12-15 22:42:46 +0000 | [diff] [blame] | 1140 | void JITEmitter::startGVStub(void *Buffer, unsigned StubSize) { |
| 1141 | SavedBufferBegin = BufferBegin; |
| 1142 | SavedBufferEnd = BufferEnd; |
| 1143 | SavedCurBufferPtr = CurBufferPtr; |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 1144 | |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 1145 | BufferBegin = CurBufferPtr = (uint8_t *)Buffer; |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1146 | BufferEnd = BufferBegin+StubSize+1; |
| 1147 | } |
| 1148 | |
Jeffrey Yasskin | 32d7e6e | 2009-12-15 22:42:46 +0000 | [diff] [blame] | 1149 | void JITEmitter::finishGVStub() { |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 1150 | assert(CurBufferPtr != BufferEnd && "Stub overflowed allocated space."); |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 1151 | NumBytes += getCurrentPCOffset(); |
Jeffrey Yasskin | 32d7e6e | 2009-12-15 22:42:46 +0000 | [diff] [blame] | 1152 | BufferBegin = SavedBufferBegin; |
| 1153 | BufferEnd = SavedBufferEnd; |
| 1154 | CurBufferPtr = SavedCurBufferPtr; |
| 1155 | } |
| 1156 | |
| 1157 | void *JITEmitter::allocIndirectGV(const GlobalValue *GV, |
| 1158 | const uint8_t *Buffer, size_t Size, |
| 1159 | unsigned Alignment) { |
| 1160 | uint8_t *IndGV = MemMgr->allocateStub(GV, Size, Alignment); |
| 1161 | memcpy(IndGV, Buffer, Size); |
| 1162 | return IndGV; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 1165 | // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry |
| 1166 | // in the constant pool that was last emitted with the 'emitConstantPool' |
| 1167 | // method. |
| 1168 | // |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 1169 | uintptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const { |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 1170 | assert(ConstantNum < ConstantPool->getConstants().size() && |
Misha Brukman | 3c94497 | 2005-04-22 04:08:30 +0000 | [diff] [blame] | 1171 | "Invalid ConstantPoolIndex!"); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 1172 | return ConstPoolAddresses[ConstantNum]; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1175 | // getJumpTableEntryAddress - Return the address of the JumpTable with index |
| 1176 | // 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo' |
| 1177 | // |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 1178 | uintptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1179 | const std::vector<MachineJumpTableEntry> &JT = JumpTable->getJumpTables(); |
| 1180 | assert(Index < JT.size() && "Invalid jump table index!"); |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 1181 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1182 | unsigned EntrySize = JumpTable->getEntrySize(*TheJIT->getDataLayout()); |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 1183 | |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 1184 | unsigned Offset = 0; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1185 | for (unsigned i = 0; i < Index; ++i) |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1186 | Offset += JT[i].MBBs.size(); |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 1187 | |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1188 | Offset *= EntrySize; |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 1189 | |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 1190 | return (uintptr_t)((char *)JumpTableBase + Offset); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 1193 | void JITEmitter::EmittedFunctionConfig::onDelete( |
| 1194 | JITEmitter *Emitter, const Function *F) { |
| 1195 | Emitter->deallocateMemForFunction(F); |
| 1196 | } |
| 1197 | void JITEmitter::EmittedFunctionConfig::onRAUW( |
| 1198 | JITEmitter *, const Function*, const Function*) { |
| 1199 | llvm_unreachable("The JIT doesn't know how to handle a" |
| 1200 | " RAUW on a value it has emitted."); |
| 1201 | } |
| 1202 | |
| 1203 | |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1204 | //===----------------------------------------------------------------------===// |
| 1205 | // Public interface to this file |
| 1206 | //===----------------------------------------------------------------------===// |
| 1207 | |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 1208 | JITCodeEmitter *JIT::createEmitter(JIT &jit, JITMemoryManager *JMM, |
| 1209 | TargetMachine &tm) { |
| 1210 | return new JITEmitter(jit, JMM, tm); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1213 | // getPointerToFunctionOrStub - If the specified function has been |
| 1214 | // code-gen'd, return a pointer to the function. If not, compile it, or use |
| 1215 | // a stub to implement lazy compilation if available. |
| 1216 | // |
| 1217 | void *JIT::getPointerToFunctionOrStub(Function *F) { |
| 1218 | // If we have already code generated the function, just return the address. |
| 1219 | if (void *Addr = getPointerToGlobalIfAvailable(F)) |
| 1220 | return Addr; |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 1221 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 1222 | // Get a stub if the target supports it. |
Sean Silva | 8ac1995 | 2012-10-11 23:30:38 +0000 | [diff] [blame] | 1223 | JITEmitter *JE = static_cast<JITEmitter*>(getCodeEmitter()); |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 1224 | return JE->getJITResolver().getLazyFunctionStub(F); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1227 | void JIT::updateFunctionStub(Function *F) { |
| 1228 | // Get the empty stub we generated earlier. |
Sean Silva | 8ac1995 | 2012-10-11 23:30:38 +0000 | [diff] [blame] | 1229 | JITEmitter *JE = static_cast<JITEmitter*>(getCodeEmitter()); |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 1230 | void *Stub = JE->getJITResolver().getLazyFunctionStub(F); |
| 1231 | void *Addr = getPointerToGlobalIfAvailable(F); |
Jeffrey Yasskin | aad0d52 | 2009-12-17 21:35:29 +0000 | [diff] [blame] | 1232 | assert(Addr != Stub && "Function must have non-stub address to be updated."); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1233 | |
| 1234 | // Tell the target jit info to rewrite the stub at the specified address, |
| 1235 | // rather than creating a new one. |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 1236 | TargetJITInfo::StubLayout layout = getJITInfo().getStubLayout(); |
Jeffrey Yasskin | 32d7e6e | 2009-12-15 22:42:46 +0000 | [diff] [blame] | 1237 | JE->startGVStub(Stub, layout.Size); |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 1238 | getJITInfo().emitFunctionStub(F, Addr, *getCodeEmitter()); |
Jeffrey Yasskin | 32d7e6e | 2009-12-15 22:42:46 +0000 | [diff] [blame] | 1239 | JE->finishGVStub(); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1242 | /// freeMachineCodeForFunction - release machine code memory for given Function. |
| 1243 | /// |
| 1244 | void JIT::freeMachineCodeForFunction(Function *F) { |
| 1245 | // Delete translation for this from the ExecutionEngine, so it will get |
| 1246 | // retranslated next time it is used. |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 1247 | updateGlobalMapping(F, 0); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1248 | |
| 1249 | // Free the actual memory for the function body and related stuff. |
Sean Silva | 8ac1995 | 2012-10-11 23:30:38 +0000 | [diff] [blame] | 1250 | static_cast<JITEmitter*>(JCE)->deallocateMemForFunction(F); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1251 | } |