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