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" |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 17 | #include "JITDebugRegisterer.h" |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 18 | #include "JITDwarfEmitter.h" |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/OwningPtr.h" |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 21 | #include "llvm/Module.h" |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/JITCodeEmitter.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineConstantPool.h" |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineRelocation.h" |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 29 | #include "llvm/ExecutionEngine/GenericValue.h" |
Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 30 | #include "llvm/ExecutionEngine/JITEventListener.h" |
| 31 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
Argyrios Kyrtzidis | b3a847d | 2009-05-18 21:06:40 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineCodeInfo.h" |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetJITInfo.h" |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetMachine.h" |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetOptions.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 39 | #include "llvm/Support/MutexGuard.h" |
Nick Lewycky | 848b314 | 2009-04-19 18:32:03 +0000 | [diff] [blame] | 40 | #include "llvm/Support/ValueHandle.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | fd58e6e | 2007-01-23 10:26:08 +0000 | [diff] [blame] | 42 | #include "llvm/System/Disassembler.h" |
Chris Lattner | bc52cad | 2008-06-25 17:18:44 +0000 | [diff] [blame] | 43 | #include "llvm/System/Memory.h" |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | 47c01a0 | 2008-11-07 09:02:17 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/SmallPtrSet.h" |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 47 | #include "llvm/ADT/Statistic.h" |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 48 | #include <algorithm> |
Evan Cheng | a7916f5 | 2008-11-05 23:44:08 +0000 | [diff] [blame] | 49 | #ifndef NDEBUG |
| 50 | #include <iomanip> |
| 51 | #endif |
Chris Lattner | c19aade | 2003-12-08 08:06:28 +0000 | [diff] [blame] | 52 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 3634373 | 2006-12-19 22:43:32 +0000 | [diff] [blame] | 54 | STATISTIC(NumBytes, "Number of bytes of machine code compiled"); |
| 55 | STATISTIC(NumRelos, "Number of relocations applied"); |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 56 | STATISTIC(NumRetries, "Number of retries with more memory"); |
Chris Lattner | 3634373 | 2006-12-19 22:43:32 +0000 | [diff] [blame] | 57 | static JIT *TheJIT = 0; |
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 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 60 | //===----------------------------------------------------------------------===// |
| 61 | // JIT lazy compilation code. |
| 62 | // |
| 63 | namespace { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 64 | class JITResolverState { |
Nick Lewycky | e2bcf13 | 2009-04-27 05:09:44 +0000 | [diff] [blame] | 65 | public: |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 66 | typedef DenseMap<AssertingVH<Function>, void*> FunctionToStubMapTy; |
| 67 | typedef std::map<void*, AssertingVH<Function> > CallSiteToFunctionMapTy; |
| 68 | typedef DenseMap<AssertingVH<Function>, SmallPtrSet<void*, 1> > |
| 69 | FunctionToCallSitesMapTy; |
Nick Lewycky | e2bcf13 | 2009-04-27 05:09:44 +0000 | [diff] [blame] | 70 | typedef std::map<AssertingVH<GlobalValue>, void*> GlobalToIndirectSymMapTy; |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 71 | private: |
| 72 | /// FunctionToStubMap - Keep track of the stub created for a particular |
| 73 | /// function so that we can reuse them if necessary. |
Nick Lewycky | e2bcf13 | 2009-04-27 05:09:44 +0000 | [diff] [blame] | 74 | FunctionToStubMapTy FunctionToStubMap; |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 75 | |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 76 | /// CallSiteToFunctionMap - Keep track of the function that each lazy call |
| 77 | /// site corresponds to, and vice versa. |
| 78 | CallSiteToFunctionMapTy CallSiteToFunctionMap; |
| 79 | FunctionToCallSitesMapTy FunctionToCallSitesMap; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 80 | |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 81 | /// GlobalToIndirectSymMap - Keep track of the indirect symbol created for a |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 82 | /// particular GlobalVariable so that we can reuse them if necessary. |
Nick Lewycky | e2bcf13 | 2009-04-27 05:09:44 +0000 | [diff] [blame] | 83 | GlobalToIndirectSymMapTy GlobalToIndirectSymMap; |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 84 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 85 | public: |
Nick Lewycky | e2bcf13 | 2009-04-27 05:09:44 +0000 | [diff] [blame] | 86 | FunctionToStubMapTy& getFunctionToStubMap(const MutexGuard& locked) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 87 | assert(locked.holds(TheJIT->lock)); |
| 88 | return FunctionToStubMap; |
| 89 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 90 | |
Nick Lewycky | e2bcf13 | 2009-04-27 05:09:44 +0000 | [diff] [blame] | 91 | GlobalToIndirectSymMapTy& getGlobalToIndirectSymMap(const MutexGuard& locked) { |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 92 | assert(locked.holds(TheJIT->lock)); |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 93 | return GlobalToIndirectSymMap; |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 94 | } |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 95 | |
| 96 | pair<void *, Function *> LookupFunctionFromCallSite( |
| 97 | const MutexGuard &locked, void *CallSite) const { |
| 98 | assert(locked.holds(TheJIT->lock)); |
| 99 | |
| 100 | // The address given to us for the stub may not be exactly right, it might be |
| 101 | // a little bit after the stub. As such, use upper_bound to find it. |
| 102 | CallSiteToFunctionMapTy::const_iterator I = |
| 103 | CallSiteToFunctionMap.upper_bound(CallSite); |
| 104 | assert(I != CallSiteToFunctionMap.begin() && |
| 105 | "This is not a known call site!"); |
| 106 | --I; |
| 107 | return *I; |
| 108 | } |
| 109 | |
| 110 | void AddCallSite(const MutexGuard &locked, void *CallSite, Function *F) { |
| 111 | assert(locked.holds(TheJIT->lock)); |
| 112 | |
| 113 | assert(CallSiteToFunctionMap.insert(std::make_pair(CallSite, F)).second && |
| 114 | "Pair was already in CallSiteToFunctionMap"); |
| 115 | FunctionToCallSitesMap[F].insert(CallSite); |
| 116 | } |
| 117 | |
| 118 | // Returns the Function of the stub if a stub was erased, or NULL if there |
| 119 | // was no stub. This function uses the call-site->function map to find a |
| 120 | // relevant function, but asserts that only stubs and not other call sites |
| 121 | // will be passed in. |
| 122 | Function *EraseStub(const MutexGuard &locked, void *Stub) { |
| 123 | CallSiteToFunctionMapTy::iterator C2F_I = |
| 124 | CallSiteToFunctionMap.find(Stub); |
| 125 | if (C2F_I == CallSiteToFunctionMap.end()) { |
| 126 | // Not a stub. |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | Function *const F = C2F_I->second; |
| 131 | #ifndef NDEBUG |
| 132 | void *RealStub = FunctionToStubMap.lookup(F); |
| 133 | assert(RealStub == Stub && |
| 134 | "Call-site that wasn't a stub pass in to EraseStub"); |
| 135 | #endif |
| 136 | FunctionToStubMap.erase(F); |
| 137 | CallSiteToFunctionMap.erase(C2F_I); |
| 138 | |
| 139 | // Remove the stub from the function->call-sites map, and remove the whole |
| 140 | // entry from the map if that was the last call site. |
| 141 | FunctionToCallSitesMapTy::iterator F2C_I = FunctionToCallSitesMap.find(F); |
| 142 | assert(F2C_I != FunctionToCallSitesMap.end() && |
| 143 | "FunctionToCallSitesMap broken"); |
| 144 | assert(F2C_I->second.erase(Stub) && |
| 145 | "FunctionToCallSitesMap broken"); |
| 146 | if (F2C_I->second.empty()) |
| 147 | FunctionToCallSitesMap.erase(F2C_I); |
| 148 | |
| 149 | return F; |
| 150 | } |
| 151 | |
| 152 | void EraseAllCallSites(const MutexGuard &locked, Function *F) { |
| 153 | assert(locked.holds(TheJIT->lock)); |
| 154 | FunctionToCallSitesMapTy::iterator F2C = FunctionToCallSitesMap.find(F); |
| 155 | if (F2C == FunctionToCallSitesMap.end()) |
| 156 | return; |
| 157 | for (SmallPtrSet<void*, 1>::const_iterator I = F2C->second.begin(), |
| 158 | E = F2C->second.end(); I != E; ++I) { |
| 159 | assert(CallSiteToFunctionMap.erase(*I) == 1 && |
| 160 | "Missing call site->function mapping"); |
| 161 | } |
| 162 | FunctionToCallSitesMap.erase(F2C); |
| 163 | } |
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 { |
Nick Lewycky | e2bcf13 | 2009-04-27 05:09:44 +0000 | [diff] [blame] | 169 | typedef JITResolverState::FunctionToStubMapTy FunctionToStubMapTy; |
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 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 179 | /// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for |
| 180 | /// external functions. |
| 181 | std::map<void*, void*> ExternalFnToStubMap; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 182 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 183 | /// revGOTMap - map addresses to indexes in the GOT |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 184 | std::map<void*, unsigned> revGOTMap; |
| 185 | unsigned nextGOTIndex; |
| 186 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 187 | static JITResolver *TheJITResolver; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 188 | public: |
Dan Gohman | 950a4c4 | 2008-03-25 22:06:05 +0000 | [diff] [blame] | 189 | explicit JITResolver(JIT &jit) : nextGOTIndex(0) { |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 190 | TheJIT = &jit; |
| 191 | |
| 192 | LazyResolverFn = jit.getJITInfo().getLazyResolverFunction(JITCompilerFn); |
| 193 | assert(TheJITResolver == 0 && "Multiple JIT resolvers?"); |
| 194 | TheJITResolver = this; |
| 195 | } |
| 196 | |
| 197 | ~JITResolver() { |
| 198 | TheJITResolver = 0; |
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 | |
Evan Cheng | 704bff9 | 2008-11-13 21:50:50 +0000 | [diff] [blame] | 201 | /// getFunctionStubIfAvailable - This returns a pointer to a function stub |
| 202 | /// if it has already been created. |
| 203 | void *getFunctionStubIfAvailable(Function *F); |
| 204 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 205 | /// getFunctionStub - This returns a pointer to a function stub, creating |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 206 | /// one on demand as needed. If empty is true, create a function stub |
| 207 | /// pointing at address 0, to be filled in later. |
Nate Begeman | b9c6c9b | 2009-03-07 06:41:19 +0000 | [diff] [blame] | 208 | void *getFunctionStub(Function *F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 209 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 210 | /// getExternalFunctionStub - Return a stub for the function at the |
| 211 | /// specified address, created lazily on demand. |
| 212 | void *getExternalFunctionStub(void *FnAddr); |
| 213 | |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 214 | /// getGlobalValueIndirectSym - Return an indirect symbol containing the |
Evan Cheng | c96a8e7 | 2008-11-05 01:50:32 +0000 | [diff] [blame] | 215 | /// specified GV address. |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 216 | void *getGlobalValueIndirectSym(GlobalValue *V, void *GVAddress); |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 217 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 218 | /// AddCallbackAtLocation - If the target is capable of rewriting an |
| 219 | /// instruction without the use of a stub, record the location of the use so |
| 220 | /// we know which function is being used at the location. |
| 221 | void *AddCallbackAtLocation(Function *F, void *Location) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 222 | MutexGuard locked(TheJIT->lock); |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 223 | /// Get the target-specific JIT resolver function. |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 224 | state.AddCallSite(locked, Location, F); |
Chris Lattner | 870286a | 2006-06-01 17:29:22 +0000 | [diff] [blame] | 225 | return (void*)(intptr_t)LazyResolverFn; |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 226 | } |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 227 | |
| 228 | void getRelocatableGVs(SmallVectorImpl<GlobalValue*> &GVs, |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 229 | SmallVectorImpl<void*> &Ptrs); |
| 230 | |
| 231 | GlobalValue *invalidateStub(void *Stub); |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 232 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 233 | /// getGOTIndexForAddress - Return a new or existing index in the GOT for |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 234 | /// an address. This function only manages slots, it does not manage the |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 235 | /// contents of the slots or the memory associated with the GOT. |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 236 | unsigned getGOTIndexForAddr(void *addr); |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 238 | /// JITCompilerFn - This function is called to resolve a stub to a compiled |
| 239 | /// address. If the LLVM Function corresponding to the stub has not yet |
| 240 | /// been compiled, this function compiles it first. |
| 241 | static void *JITCompilerFn(void *Stub); |
| 242 | }; |
| 243 | } |
| 244 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 245 | JITResolver *JITResolver::TheJITResolver = 0; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 246 | |
Evan Cheng | 704bff9 | 2008-11-13 21:50:50 +0000 | [diff] [blame] | 247 | /// getFunctionStubIfAvailable - This returns a pointer to a function stub |
| 248 | /// if it has already been created. |
| 249 | void *JITResolver::getFunctionStubIfAvailable(Function *F) { |
| 250 | MutexGuard locked(TheJIT->lock); |
| 251 | |
| 252 | // If we already have a stub for this function, recycle it. |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 253 | return state.getFunctionToStubMap(locked).lookup(F); |
Evan Cheng | 704bff9 | 2008-11-13 21:50:50 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 256 | /// getFunctionStub - This returns a pointer to a function stub, creating |
| 257 | /// one on demand as needed. |
Nate Begeman | b9c6c9b | 2009-03-07 06:41:19 +0000 | [diff] [blame] | 258 | void *JITResolver::getFunctionStub(Function *F) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 259 | MutexGuard locked(TheJIT->lock); |
| 260 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 261 | // If we already have a stub for this function, recycle it. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 262 | void *&Stub = state.getFunctionToStubMap(locked)[F]; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 263 | if (Stub) return Stub; |
| 264 | |
Nate Begeman | b9c6c9b | 2009-03-07 06:41:19 +0000 | [diff] [blame] | 265 | // Call the lazy resolver function unless we are JIT'ing non-lazily, in which |
| 266 | // case we must resolve the symbol now. |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 267 | void *Actual = TheJIT->isLazyCompilationDisabled() |
Nate Begeman | b9c6c9b | 2009-03-07 06:41:19 +0000 | [diff] [blame] | 268 | ? (void *)0 : (void *)(intptr_t)LazyResolverFn; |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 269 | |
Nate Begeman | b9c6c9b | 2009-03-07 06:41:19 +0000 | [diff] [blame] | 270 | // If this is an external declaration, attempt to resolve the address now |
| 271 | // to place in the stub. |
Dan Gohman | 69f9378 | 2009-01-05 05:32:42 +0000 | [diff] [blame] | 272 | if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode()) { |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 273 | Actual = TheJIT->getPointerToFunction(F); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 274 | |
Dan Gohman | 69f9378 | 2009-01-05 05:32:42 +0000 | [diff] [blame] | 275 | // If we resolved the symbol to a null address (eg. a weak external) |
Nate Begeman | b9c6c9b | 2009-03-07 06:41:19 +0000 | [diff] [blame] | 276 | // don't emit a stub. Return a null pointer to the application. If dlsym |
| 277 | // stubs are enabled, not being able to resolve the address is not |
| 278 | // meaningful. |
| 279 | if (!Actual && !TheJIT->areDlsymStubsEnabled()) return 0; |
Dan Gohman | 69f9378 | 2009-01-05 05:32:42 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Nate Begeman | b9c6c9b | 2009-03-07 06:41:19 +0000 | [diff] [blame] | 282 | // Codegen a new stub, calling the lazy resolver or the actual address of the |
| 283 | // external function, if it was resolved. |
Nicolas Geoffray | 51cc3c1 | 2008-04-16 20:46:05 +0000 | [diff] [blame] | 284 | Stub = TheJIT->getJITInfo().emitFunctionStub(F, Actual, |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 285 | *TheJIT->getCodeEmitter()); |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 870286a | 2006-06-01 17:29:22 +0000 | [diff] [blame] | 287 | if (Actual != (void*)(intptr_t)LazyResolverFn) { |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 288 | // If we are getting the stub for an external function, we really want the |
| 289 | // address of the stub in the GlobalAddressMap for the JIT, not the address |
| 290 | // of the external function. |
| 291 | TheJIT->updateGlobalMapping(F, Stub); |
| 292 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 293 | |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 294 | DEBUG(errs() << "JIT: Stub emitted at [" << Stub << "] for function '" |
| 295 | << F->getName() << "'\n"); |
Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 296 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 297 | // Finally, keep track of the stub-to-Function mapping so that the |
| 298 | // JITCompilerFn knows which function to compile! |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 299 | state.AddCallSite(locked, Stub, F); |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 300 | |
Nate Begeman | b9c6c9b | 2009-03-07 06:41:19 +0000 | [diff] [blame] | 301 | // If we are JIT'ing non-lazily but need to call a function that does not |
| 302 | // exist yet, add it to the JIT's work list so that we can fill in the stub |
| 303 | // address later. |
| 304 | if (!Actual && TheJIT->isLazyCompilationDisabled()) |
| 305 | if (!F->isDeclaration() || F->hasNotBeenReadFromBitcode()) |
| 306 | TheJIT->addPendingFunction(F); |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 308 | return Stub; |
| 309 | } |
| 310 | |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 311 | /// getGlobalValueIndirectSym - Return a lazy pointer containing the specified |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 312 | /// GV address. |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 313 | void *JITResolver::getGlobalValueIndirectSym(GlobalValue *GV, void *GVAddress) { |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 314 | MutexGuard locked(TheJIT->lock); |
| 315 | |
| 316 | // If we already have a stub for this global variable, recycle it. |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 317 | void *&IndirectSym = state.getGlobalToIndirectSymMap(locked)[GV]; |
| 318 | if (IndirectSym) return IndirectSym; |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 319 | |
Evan Cheng | e4d783d | 2008-11-10 23:26:16 +0000 | [diff] [blame] | 320 | // Otherwise, codegen a new indirect symbol. |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 321 | IndirectSym = TheJIT->getJITInfo().emitGlobalValueIndirectSym(GV, GVAddress, |
Evan Cheng | c96a8e7 | 2008-11-05 01:50:32 +0000 | [diff] [blame] | 322 | *TheJIT->getCodeEmitter()); |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 323 | |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 324 | DEBUG(errs() << "JIT: Indirect symbol emitted at [" << IndirectSym |
| 325 | << "] for GV '" << GV->getName() << "'\n"); |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 326 | |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 327 | return IndirectSym; |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 330 | /// getExternalFunctionStub - Return a stub for the function at the |
| 331 | /// specified address, created lazily on demand. |
| 332 | void *JITResolver::getExternalFunctionStub(void *FnAddr) { |
| 333 | // If we already have a stub for this function, recycle it. |
| 334 | void *&Stub = ExternalFnToStubMap[FnAddr]; |
| 335 | if (Stub) return Stub; |
| 336 | |
Nicolas Geoffray | 51cc3c1 | 2008-04-16 20:46:05 +0000 | [diff] [blame] | 337 | Stub = TheJIT->getJITInfo().emitFunctionStub(0, FnAddr, |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 338 | *TheJIT->getCodeEmitter()); |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 339 | |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 340 | DEBUG(errs() << "JIT: Stub emitted at [" << Stub |
| 341 | << "] for external function at '" << FnAddr << "'\n"); |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 342 | return Stub; |
| 343 | } |
| 344 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 345 | unsigned JITResolver::getGOTIndexForAddr(void* addr) { |
| 346 | unsigned idx = revGOTMap[addr]; |
| 347 | if (!idx) { |
| 348 | idx = ++nextGOTIndex; |
| 349 | revGOTMap[addr] = idx; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 350 | DEBUG(errs() << "JIT: Adding GOT entry " << idx << " for addr [" |
| 351 | << addr << "]\n"); |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 352 | } |
| 353 | return idx; |
| 354 | } |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 355 | |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 356 | void JITResolver::getRelocatableGVs(SmallVectorImpl<GlobalValue*> &GVs, |
| 357 | SmallVectorImpl<void*> &Ptrs) { |
| 358 | MutexGuard locked(TheJIT->lock); |
| 359 | |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 360 | const FunctionToStubMapTy &FM = state.getFunctionToStubMap(locked); |
Nick Lewycky | e2bcf13 | 2009-04-27 05:09:44 +0000 | [diff] [blame] | 361 | GlobalToIndirectSymMapTy &GM = state.getGlobalToIndirectSymMap(locked); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 362 | |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 363 | for (FunctionToStubMapTy::const_iterator i = FM.begin(), e = FM.end(); |
| 364 | i != e; ++i){ |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 365 | Function *F = i->first; |
| 366 | if (F->isDeclaration() && F->hasExternalLinkage()) { |
| 367 | GVs.push_back(i->first); |
| 368 | Ptrs.push_back(i->second); |
| 369 | } |
| 370 | } |
Nick Lewycky | e2bcf13 | 2009-04-27 05:09:44 +0000 | [diff] [blame] | 371 | for (GlobalToIndirectSymMapTy::iterator i = GM.begin(), e = GM.end(); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 372 | i != e; ++i) { |
| 373 | GVs.push_back(i->first); |
| 374 | Ptrs.push_back(i->second); |
| 375 | } |
| 376 | } |
| 377 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 378 | GlobalValue *JITResolver::invalidateStub(void *Stub) { |
| 379 | MutexGuard locked(TheJIT->lock); |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 380 | |
Nick Lewycky | e2bcf13 | 2009-04-27 05:09:44 +0000 | [diff] [blame] | 381 | GlobalToIndirectSymMapTy &GM = state.getGlobalToIndirectSymMap(locked); |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 382 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 383 | // Look up the cheap way first, to see if it's a function stub we are |
| 384 | // invalidating. If so, remove it from both the forward and reverse maps. |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 385 | if (Function *F = state.EraseStub(locked, Stub)) { |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 386 | return F; |
| 387 | } |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 388 | |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 389 | // Otherwise, it might be an indirect symbol stub. Find it and remove it. |
Nick Lewycky | e2bcf13 | 2009-04-27 05:09:44 +0000 | [diff] [blame] | 390 | for (GlobalToIndirectSymMapTy::iterator i = GM.begin(), e = GM.end(); |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 391 | i != e; ++i) { |
| 392 | if (i->second != Stub) |
| 393 | continue; |
| 394 | GlobalValue *GV = i->first; |
| 395 | GM.erase(i); |
| 396 | return GV; |
| 397 | } |
| 398 | |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 399 | // Lastly, check to see if it's in the ExternalFnToStubMap. |
| 400 | for (std::map<void *, void *>::iterator i = ExternalFnToStubMap.begin(), |
| 401 | e = ExternalFnToStubMap.end(); i != e; ++i) { |
| 402 | if (i->second != Stub) |
| 403 | continue; |
| 404 | ExternalFnToStubMap.erase(i); |
| 405 | break; |
| 406 | } |
| 407 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 408 | return 0; |
| 409 | } |
| 410 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 411 | /// JITCompilerFn - This function is called when a lazy compilation stub has |
| 412 | /// been entered. It looks up which function this stub corresponds to, compiles |
| 413 | /// it if necessary, then returns the resultant function pointer. |
| 414 | void *JITResolver::JITCompilerFn(void *Stub) { |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 415 | JITResolver &JR = *TheJITResolver; |
Nicolas Geoffray | dcb31e1 | 2008-10-03 07:27:08 +0000 | [diff] [blame] | 416 | |
| 417 | Function* F = 0; |
| 418 | void* ActualPtr = 0; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 419 | |
Nicolas Geoffray | dcb31e1 | 2008-10-03 07:27:08 +0000 | [diff] [blame] | 420 | { |
| 421 | // Only lock for getting the Function. The call getPointerToFunction made |
| 422 | // in this function might trigger function materializing, which requires |
| 423 | // JIT lock to be unlocked. |
| 424 | MutexGuard locked(TheJIT->lock); |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 425 | |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 426 | // The address given to us for the stub may not be exactly right, it might |
| 427 | // be a little bit after the stub. As such, use upper_bound to find it. |
| 428 | pair<void*, Function*> I = |
| 429 | JR.state.LookupFunctionFromCallSite(locked, Stub); |
| 430 | F = I.second; |
| 431 | ActualPtr = I.first; |
Nicolas Geoffray | dcb31e1 | 2008-10-03 07:27:08 +0000 | [diff] [blame] | 432 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 433 | |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 434 | // If we have already code generated the function, just return the address. |
| 435 | void *Result = TheJIT->getPointerToGlobalIfAvailable(F); |
Chris Lattner | 9cab56d | 2006-11-09 19:32:13 +0000 | [diff] [blame] | 436 | |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 437 | if (!Result) { |
| 438 | // Otherwise we don't have it, do lazy compilation now. |
| 439 | |
| 440 | // If lazy compilation is disabled, emit a useful error message and abort. |
| 441 | if (TheJIT->isLazyCompilationDisabled()) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 442 | llvm_report_error("LLVM JIT requested to do lazy compilation of function '" |
| 443 | + F->getName() + "' when lazy compiles are disabled!"); |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 446 | DEBUG(errs() << "JIT: Lazily resolving function '" << F->getName() |
| 447 | << "' In stub ptr = " << Stub << " actual ptr = " |
| 448 | << ActualPtr << "\n"); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 449 | |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 450 | Result = TheJIT->getPointerToFunction(F); |
| 451 | } |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 452 | |
| 453 | // Reacquire the lock to update the GOT map. |
Nicolas Geoffray | dcb31e1 | 2008-10-03 07:27:08 +0000 | [diff] [blame] | 454 | MutexGuard locked(TheJIT->lock); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 455 | |
Jeffrey Yasskin | ebbcef9 | 2009-10-19 18:49:59 +0000 | [diff] [blame] | 456 | // We might like to remove the call site from the CallSiteToFunction map, but |
| 457 | // we can't do that! Multiple threads could be stuck, waiting to acquire the |
| 458 | // lock above. As soon as the 1st function finishes compiling the function, |
| 459 | // the next one will be released, and needs to be able to find the function it |
| 460 | // needs to call. |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 461 | |
| 462 | // 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] | 463 | |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 464 | // What we will do is set the compiled function address to map to the |
| 465 | // 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] | 466 | // if they see it still using the stub address. |
| 467 | // Note: this is done so the Resolver doesn't have to manage GOT memory |
| 468 | // Do this without allocating map space if the target isn't using a GOT |
| 469 | if(JR.revGOTMap.find(Stub) != JR.revGOTMap.end()) |
| 470 | JR.revGOTMap[Result] = JR.revGOTMap[Stub]; |
| 471 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 472 | return Result; |
| 473 | } |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 474 | |
Chris Lattner | 8ac66c1 | 2008-04-04 05:51:42 +0000 | [diff] [blame] | 475 | //===----------------------------------------------------------------------===// |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 476 | // JITEmitter code. |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 477 | // |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 478 | namespace { |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 479 | /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is |
| 480 | /// used to output functions to memory for execution. |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 481 | class JITEmitter : public JITCodeEmitter { |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 482 | JITMemoryManager *MemMgr; |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 483 | |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 484 | // When outputting a function stub in the context of some other function, we |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 485 | // save BufferBegin/BufferEnd/CurBufferPtr here. |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 486 | uint8_t *SavedBufferBegin, *SavedBufferEnd, *SavedCurBufferPtr; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 487 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 488 | // When reattempting to JIT a function after running out of space, we store |
| 489 | // the estimated size of the function we're trying to JIT here, so we can |
| 490 | // ask the memory manager for at least this much space. When we |
| 491 | // successfully emit the function, we reset this back to zero. |
| 492 | uintptr_t SizeEstimate; |
| 493 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 494 | /// Relocations - These are the relocations that the function needs, as |
| 495 | /// emitted. |
| 496 | std::vector<MachineRelocation> Relocations; |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 497 | |
| 498 | /// MBBLocations - This vector is a mapping from MBB ID's to their address. |
| 499 | /// It is filled in by the StartMachineBasicBlock callback and queried by |
| 500 | /// the getMachineBasicBlockAddress callback. |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 501 | std::vector<uintptr_t> MBBLocations; |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 502 | |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 503 | /// ConstantPool - The constant pool for the current function. |
| 504 | /// |
| 505 | MachineConstantPool *ConstantPool; |
| 506 | |
| 507 | /// ConstantPoolBase - A pointer to the first entry in the constant pool. |
| 508 | /// |
| 509 | void *ConstantPoolBase; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 510 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 511 | /// ConstPoolAddresses - Addresses of individual constant pool entries. |
| 512 | /// |
| 513 | SmallVector<uintptr_t, 8> ConstPoolAddresses; |
| 514 | |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 515 | /// JumpTable - The jump tables for the current function. |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 516 | /// |
| 517 | MachineJumpTableInfo *JumpTable; |
| 518 | |
| 519 | /// JumpTableBase - A pointer to the first entry in the jump table. |
| 520 | /// |
| 521 | void *JumpTableBase; |
Evan Cheng | 2a3e08b | 2008-01-05 02:26:58 +0000 | [diff] [blame] | 522 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 523 | /// Resolver - This contains info about the currently resolved functions. |
| 524 | JITResolver Resolver; |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 525 | |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 526 | /// DE - The dwarf emitter for the jit. |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 527 | OwningPtr<JITDwarfEmitter> DE; |
| 528 | |
| 529 | /// DR - The debug registerer for the jit. |
| 530 | OwningPtr<JITDebugRegisterer> DR; |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 531 | |
| 532 | /// LabelLocations - This vector is a mapping from Label ID's to their |
| 533 | /// address. |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 534 | std::vector<uintptr_t> LabelLocations; |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 535 | |
| 536 | /// MMI - Machine module info for exception informations |
| 537 | MachineModuleInfo* MMI; |
| 538 | |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 539 | // GVSet - a set to keep track of which globals have been seen |
Evan Cheng | 47c01a0 | 2008-11-07 09:02:17 +0000 | [diff] [blame] | 540 | SmallPtrSet<const GlobalVariable*, 8> GVSet; |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 541 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 542 | // CurFn - The llvm function being emitted. Only valid during |
| 543 | // finishFunction(). |
| 544 | const Function *CurFn; |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 545 | |
| 546 | /// Information about emitted code, which is passed to the |
| 547 | /// JITEventListeners. This is reset in startFunction and used in |
| 548 | /// finishFunction. |
| 549 | JITEvent_EmittedFunctionDetails EmissionDetails; |
| 550 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 551 | // CurFnStubUses - For a given Function, a vector of stubs that it |
| 552 | // references. This facilitates the JIT detecting that a stub is no |
| 553 | // longer used, so that it may be deallocated. |
| 554 | DenseMap<const Function *, SmallVector<void*, 1> > CurFnStubUses; |
| 555 | |
| 556 | // StubFnRefs - For a given pointer to a stub, a set of Functions which |
| 557 | // reference the stub. When the count of a stub's references drops to zero, |
| 558 | // the stub is unused. |
| 559 | DenseMap<void *, SmallPtrSet<const Function*, 1> > StubFnRefs; |
| 560 | |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 561 | // ExtFnStubs - A map of external function names to stubs which have entries |
| 562 | // in the JITResolver's ExternalFnToStubMap. |
| 563 | StringMap<void *> ExtFnStubs; |
Argyrios Kyrtzidis | b3a847d | 2009-05-18 21:06:40 +0000 | [diff] [blame] | 564 | |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 565 | DebugLocTuple PrevDLT; |
| 566 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 567 | public: |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 568 | JITEmitter(JIT &jit, JITMemoryManager *JMM, TargetMachine &TM) |
Xerxes Ranby | 3d47db5 | 2009-08-25 10:12:55 +0000 | [diff] [blame] | 569 | : SizeEstimate(0), Resolver(jit), MMI(0), CurFn(0) { |
Chris Lattner | 9f2f142 | 2007-12-06 01:08:09 +0000 | [diff] [blame] | 570 | MemMgr = JMM ? JMM : JITMemoryManager::CreateDefaultMemManager(); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 571 | if (jit.getJITInfo().needsGOT()) { |
| 572 | MemMgr->AllocateGOT(); |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 573 | DEBUG(errs() << "JIT is managing a GOT\n"); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 574 | } |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 575 | |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 576 | if (DwarfExceptionHandling || JITEmitDebugInfo) { |
| 577 | DE.reset(new JITDwarfEmitter(jit)); |
| 578 | } |
| 579 | if (JITEmitDebugInfo) { |
| 580 | DR.reset(new JITDebugRegisterer(TM)); |
| 581 | } |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 582 | } |
| 583 | ~JITEmitter() { |
| 584 | delete MemMgr; |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 585 | } |
Evan Cheng | a044dfc | 2008-08-20 00:28:12 +0000 | [diff] [blame] | 586 | |
| 587 | /// classof - Methods for support type inquiry through isa, cast, and |
| 588 | /// dyn_cast: |
| 589 | /// |
| 590 | static inline bool classof(const JITEmitter*) { return true; } |
| 591 | static inline bool classof(const MachineCodeEmitter*) { return true; } |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 592 | |
| 593 | JITResolver &getJITResolver() { return Resolver; } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 594 | |
| 595 | virtual void startFunction(MachineFunction &F); |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 596 | virtual bool finishFunction(MachineFunction &F); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 597 | |
| 598 | void emitConstantPool(MachineConstantPool *MCP); |
| 599 | void initJumpTableInfo(MachineJumpTableInfo *MJTI); |
Jim Laskey | b92767a | 2006-12-14 22:53:42 +0000 | [diff] [blame] | 600 | void emitJumpTableInfo(MachineJumpTableInfo *MJTI); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 601 | |
Evan Cheng | ce4a70b | 2008-11-08 08:02:53 +0000 | [diff] [blame] | 602 | virtual void startGVStub(const GlobalValue* GV, unsigned StubSize, |
Nicolas Geoffray | 51cc3c1 | 2008-04-16 20:46:05 +0000 | [diff] [blame] | 603 | unsigned Alignment = 1); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 604 | virtual void startGVStub(const GlobalValue* GV, void *Buffer, |
| 605 | unsigned StubSize); |
Evan Cheng | ce4a70b | 2008-11-08 08:02:53 +0000 | [diff] [blame] | 606 | virtual void* finishGVStub(const GlobalValue *GV); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 607 | |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 608 | /// allocateSpace - Reserves space in the current block if any, or |
| 609 | /// allocate a new one of the given size. |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 610 | virtual void *allocateSpace(uintptr_t Size, unsigned Alignment); |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 611 | |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 612 | /// allocateGlobal - Allocate memory for a global. Unlike allocateSpace, |
| 613 | /// this method does not allocate memory in the current output buffer, |
| 614 | /// because a global may live longer than the current function. |
| 615 | virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment); |
| 616 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 617 | virtual void addRelocation(const MachineRelocation &MR) { |
| 618 | Relocations.push_back(MR); |
| 619 | } |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 620 | |
| 621 | virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) { |
| 622 | if (MBBLocations.size() <= (unsigned)MBB->getNumber()) |
| 623 | MBBLocations.resize((MBB->getNumber()+1)*2); |
| 624 | MBBLocations[MBB->getNumber()] = getCurrentPCValue(); |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 625 | DEBUG(errs() << "JIT: Emitting BB" << MBB->getNumber() << " at [" |
| 626 | << (void*) getCurrentPCValue() << "]\n"); |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 627 | } |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 628 | |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 629 | virtual uintptr_t getConstantPoolEntryAddress(unsigned Entry) const; |
| 630 | virtual uintptr_t getJumpTableEntryAddress(unsigned Entry) const; |
Evan Cheng | 2a3e08b | 2008-01-05 02:26:58 +0000 | [diff] [blame] | 631 | |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 632 | virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const { |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 633 | assert(MBBLocations.size() > (unsigned)MBB->getNumber() && |
| 634 | MBBLocations[MBB->getNumber()] && "MBB not emitted!"); |
| 635 | return MBBLocations[MBB->getNumber()]; |
| 636 | } |
| 637 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 638 | /// retryWithMoreMemory - Log a retry and deallocate all memory for the |
| 639 | /// given function. Increase the minimum allocation size so that we get |
| 640 | /// more memory next time. |
| 641 | void retryWithMoreMemory(MachineFunction &F); |
| 642 | |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 643 | /// deallocateMemForFunction - Deallocate all memory for the specified |
| 644 | /// function body. |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 645 | void deallocateMemForFunction(const Function *F); |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 646 | |
| 647 | /// AddStubToCurrentFunction - Mark the current function being JIT'd as |
| 648 | /// using the stub at the specified address. Allows |
| 649 | /// deallocateMemForFunction to also remove stubs no longer referenced. |
| 650 | void AddStubToCurrentFunction(void *Stub); |
| 651 | |
| 652 | /// getExternalFnStubs - Accessor for the JIT to find stubs emitted for |
| 653 | /// MachineRelocations that reference external functions by name. |
| 654 | const StringMap<void*> &getExternalFnStubs() const { return ExtFnStubs; } |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 655 | |
Devang Patel | 02c0423 | 2009-10-06 03:04:58 +0000 | [diff] [blame] | 656 | virtual void processDebugLoc(DebugLoc DL, bool BeforePrintingInsn); |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 657 | |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 658 | virtual void emitLabel(uint64_t LabelID) { |
| 659 | if (LabelLocations.size() <= LabelID) |
| 660 | LabelLocations.resize((LabelID+1)*2); |
| 661 | LabelLocations[LabelID] = getCurrentPCValue(); |
| 662 | } |
| 663 | |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 664 | virtual uintptr_t getLabelAddress(uint64_t LabelID) const { |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 665 | assert(LabelLocations.size() > (unsigned)LabelID && |
| 666 | LabelLocations[LabelID] && "Label not emitted!"); |
| 667 | return LabelLocations[LabelID]; |
| 668 | } |
| 669 | |
| 670 | virtual void setModuleInfo(MachineModuleInfo* Info) { |
| 671 | MMI = Info; |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 672 | if (DE.get()) DE->setModuleInfo(Info); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Dan Gohman | a9ad041 | 2009-08-12 22:10:57 +0000 | [diff] [blame] | 675 | void setMemoryExecutable() { |
Jim Grosbach | cce6c29 | 2008-10-03 16:17:20 +0000 | [diff] [blame] | 676 | MemMgr->setMemoryExecutable(); |
| 677 | } |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 678 | |
Dan Gohman | a9ad041 | 2009-08-12 22:10:57 +0000 | [diff] [blame] | 679 | JITMemoryManager *getMemMgr() const { return MemMgr; } |
Jim Grosbach | cce6c29 | 2008-10-03 16:17:20 +0000 | [diff] [blame] | 680 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 681 | private: |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 682 | void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub); |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 683 | void *getPointerToGVIndirectSym(GlobalValue *V, void *Reference, |
Evan Cheng | e4d783d | 2008-11-10 23:26:16 +0000 | [diff] [blame] | 684 | bool NoNeedStub); |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 685 | unsigned addSizeOfGlobal(const GlobalVariable *GV, unsigned Size); |
| 686 | unsigned addSizeOfGlobalsInConstantVal(const Constant *C, unsigned Size); |
| 687 | unsigned addSizeOfGlobalsInInitializer(const Constant *Init, unsigned Size); |
| 688 | unsigned GetSizeOfGlobalsInBytes(MachineFunction &MF); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 689 | }; |
| 690 | } |
| 691 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 692 | void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference, |
| 693 | bool DoesntNeedStub) { |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 694 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 695 | return TheJIT->getOrEmitGlobalVariable(GV); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 696 | |
Chris Lattner | 18e0459 | 2008-06-25 20:21:35 +0000 | [diff] [blame] | 697 | if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) |
Anton Korobeynikov | 19e861a | 2008-09-09 20:05:04 +0000 | [diff] [blame] | 698 | return TheJIT->getPointerToGlobal(GA->resolveAliasedGlobal(false)); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 699 | |
| 700 | // If we have already compiled the function, return a pointer to its body. |
| 701 | Function *F = cast<Function>(V); |
Evan Cheng | 704bff9 | 2008-11-13 21:50:50 +0000 | [diff] [blame] | 702 | void *ResultPtr; |
Jeffrey Yasskin | ea5ed00 | 2009-10-06 00:35:55 +0000 | [diff] [blame] | 703 | if (!DoesntNeedStub) { |
Evan Cheng | 704bff9 | 2008-11-13 21:50:50 +0000 | [diff] [blame] | 704 | // Return the function stub if it's already created. |
| 705 | ResultPtr = Resolver.getFunctionStubIfAvailable(F); |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 706 | if (ResultPtr) |
| 707 | AddStubToCurrentFunction(ResultPtr); |
| 708 | } else { |
Evan Cheng | 704bff9 | 2008-11-13 21:50:50 +0000 | [diff] [blame] | 709 | ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F); |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 710 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 711 | if (ResultPtr) return ResultPtr; |
| 712 | |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 713 | // If this is an external function pointer, we can force the JIT to |
Nate Begeman | b9c6c9b | 2009-03-07 06:41:19 +0000 | [diff] [blame] | 714 | // 'compile' it, which really just adds it to the map. In dlsym mode, |
| 715 | // external functions are forced through a stub, regardless of reloc type. |
| 716 | if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode() && |
| 717 | DoesntNeedStub && !TheJIT->areDlsymStubsEnabled()) |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 718 | return TheJIT->getPointerToFunction(F); |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 719 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 720 | // Okay, the function has not been compiled yet, if the target callback |
| 721 | // mechanism is capable of rewriting the instruction directly, prefer to do |
Nate Begeman | b9c6c9b | 2009-03-07 06:41:19 +0000 | [diff] [blame] | 722 | // that instead of emitting a stub. This uses the lazy resolver, so is not |
| 723 | // legal if lazy compilation is disabled. |
| 724 | if (DoesntNeedStub && !TheJIT->isLazyCompilationDisabled()) |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 725 | return Resolver.AddCallbackAtLocation(F, Reference); |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 726 | |
Nate Begeman | b9c6c9b | 2009-03-07 06:41:19 +0000 | [diff] [blame] | 727 | // Otherwise, we have to emit a stub. |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 728 | void *StubAddr = Resolver.getFunctionStub(F); |
| 729 | |
| 730 | // Add the stub to the current function's list of referenced stubs, so we can |
Nate Begeman | b9c6c9b | 2009-03-07 06:41:19 +0000 | [diff] [blame] | 731 | // deallocate them if the current function is ever freed. It's possible to |
| 732 | // return null from getFunctionStub in the case of a weak extern that fails |
| 733 | // to resolve. |
| 734 | if (StubAddr) |
| 735 | AddStubToCurrentFunction(StubAddr); |
| 736 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 737 | return StubAddr; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 740 | void *JITEmitter::getPointerToGVIndirectSym(GlobalValue *V, void *Reference, |
Evan Cheng | e4d783d | 2008-11-10 23:26:16 +0000 | [diff] [blame] | 741 | bool NoNeedStub) { |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 742 | // Make sure GV is emitted first, and create a stub containing the fully |
| 743 | // resolved address. |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 744 | void *GVAddress = getPointerToGlobal(V, Reference, true); |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 745 | void *StubAddr = Resolver.getGlobalValueIndirectSym(V, GVAddress); |
| 746 | |
| 747 | // Add the stub to the current function's list of referenced stubs, so we can |
| 748 | // deallocate them if the current function is ever freed. |
| 749 | AddStubToCurrentFunction(StubAddr); |
| 750 | |
| 751 | return StubAddr; |
| 752 | } |
| 753 | |
| 754 | void JITEmitter::AddStubToCurrentFunction(void *StubAddr) { |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 755 | assert(CurFn && "Stub added to current function, but current function is 0!"); |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 756 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 757 | SmallVectorImpl<void*> &StubsUsed = CurFnStubUses[CurFn]; |
| 758 | StubsUsed.push_back(StubAddr); |
| 759 | |
| 760 | SmallPtrSet<const Function *, 1> &FnRefs = StubFnRefs[StubAddr]; |
| 761 | FnRefs.insert(CurFn); |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Devang Patel | 02c0423 | 2009-10-06 03:04:58 +0000 | [diff] [blame] | 764 | void JITEmitter::processDebugLoc(DebugLoc DL, bool BeforePrintingInsn) { |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 765 | if (!DL.isUnknown()) { |
| 766 | DebugLocTuple CurDLT = EmissionDetails.MF->getDebugLocTuple(DL); |
| 767 | |
Devang Patel | 02c0423 | 2009-10-06 03:04:58 +0000 | [diff] [blame] | 768 | if (BeforePrintingInsn) { |
Devang Patel | 1619dc3 | 2009-10-13 23:28:53 +0000 | [diff] [blame] | 769 | if (CurDLT.Scope != 0 && PrevDLT != CurDLT) { |
Devang Patel | 02c0423 | 2009-10-06 03:04:58 +0000 | [diff] [blame] | 770 | JITEvent_EmittedFunctionDetails::LineStart NextLine; |
| 771 | NextLine.Address = getCurrentPCValue(); |
| 772 | NextLine.Loc = DL; |
| 773 | EmissionDetails.LineStarts.push_back(NextLine); |
| 774 | } |
| 775 | |
| 776 | PrevDLT = CurDLT; |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 777 | } |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 781 | static unsigned GetConstantPoolSizeInBytes(MachineConstantPool *MCP, |
| 782 | const TargetData *TD) { |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 783 | const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants(); |
| 784 | if (Constants.empty()) return 0; |
| 785 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 786 | unsigned Size = 0; |
| 787 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
| 788 | MachineConstantPoolEntry CPE = Constants[i]; |
| 789 | unsigned AlignMask = CPE.getAlignment() - 1; |
| 790 | Size = (Size + AlignMask) & ~AlignMask; |
| 791 | const Type *Ty = CPE.getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 792 | Size += TD->getTypeAllocSize(Ty); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 793 | } |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 794 | return Size; |
| 795 | } |
| 796 | |
| 797 | static unsigned GetJumpTableSizeInBytes(MachineJumpTableInfo *MJTI) { |
| 798 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 799 | if (JT.empty()) return 0; |
| 800 | |
| 801 | unsigned NumEntries = 0; |
| 802 | for (unsigned i = 0, e = JT.size(); i != e; ++i) |
| 803 | NumEntries += JT[i].MBBs.size(); |
| 804 | |
| 805 | unsigned EntrySize = MJTI->getEntrySize(); |
| 806 | |
| 807 | return NumEntries * EntrySize; |
| 808 | } |
| 809 | |
Nicolas Geoffray | 580631a | 2008-04-20 23:39:44 +0000 | [diff] [blame] | 810 | static uintptr_t RoundUpToAlign(uintptr_t Size, unsigned Alignment) { |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 811 | if (Alignment == 0) Alignment = 1; |
Nicolas Geoffray | 580631a | 2008-04-20 23:39:44 +0000 | [diff] [blame] | 812 | // Since we do not know where the buffer will be allocated, be pessimistic. |
| 813 | return Size + Alignment; |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 814 | } |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 815 | |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 816 | /// addSizeOfGlobal - add the size of the global (plus any alignment padding) |
| 817 | /// into the running total Size. |
| 818 | |
| 819 | unsigned JITEmitter::addSizeOfGlobal(const GlobalVariable *GV, unsigned Size) { |
| 820 | const Type *ElTy = GV->getType()->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 821 | size_t GVSize = (size_t)TheJIT->getTargetData()->getTypeAllocSize(ElTy); |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 822 | size_t GVAlign = |
| 823 | (size_t)TheJIT->getTargetData()->getPreferredAlignment(GV); |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 824 | DEBUG(errs() << "JIT: Adding in size " << GVSize << " alignment " << GVAlign); |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 825 | DEBUG(GV->dump()); |
| 826 | // Assume code section ends with worst possible alignment, so first |
| 827 | // variable needs maximal padding. |
| 828 | if (Size==0) |
| 829 | Size = 1; |
| 830 | Size = ((Size+GVAlign-1)/GVAlign)*GVAlign; |
| 831 | Size += GVSize; |
| 832 | return Size; |
| 833 | } |
| 834 | |
| 835 | /// addSizeOfGlobalsInConstantVal - find any globals that we haven't seen yet |
| 836 | /// but are referenced from the constant; put them in GVSet and add their |
| 837 | /// size into the running total Size. |
| 838 | |
| 839 | unsigned JITEmitter::addSizeOfGlobalsInConstantVal(const Constant *C, |
| 840 | unsigned Size) { |
| 841 | // If its undefined, return the garbage. |
| 842 | if (isa<UndefValue>(C)) |
| 843 | return Size; |
| 844 | |
| 845 | // If the value is a ConstantExpr |
| 846 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 847 | Constant *Op0 = CE->getOperand(0); |
| 848 | switch (CE->getOpcode()) { |
| 849 | case Instruction::GetElementPtr: |
| 850 | case Instruction::Trunc: |
| 851 | case Instruction::ZExt: |
| 852 | case Instruction::SExt: |
| 853 | case Instruction::FPTrunc: |
| 854 | case Instruction::FPExt: |
| 855 | case Instruction::UIToFP: |
| 856 | case Instruction::SIToFP: |
| 857 | case Instruction::FPToUI: |
| 858 | case Instruction::FPToSI: |
| 859 | case Instruction::PtrToInt: |
| 860 | case Instruction::IntToPtr: |
| 861 | case Instruction::BitCast: { |
| 862 | Size = addSizeOfGlobalsInConstantVal(Op0, Size); |
| 863 | break; |
| 864 | } |
| 865 | case Instruction::Add: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 866 | case Instruction::FAdd: |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 867 | case Instruction::Sub: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 868 | case Instruction::FSub: |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 869 | case Instruction::Mul: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 870 | case Instruction::FMul: |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 871 | case Instruction::UDiv: |
| 872 | case Instruction::SDiv: |
| 873 | case Instruction::URem: |
| 874 | case Instruction::SRem: |
| 875 | case Instruction::And: |
| 876 | case Instruction::Or: |
| 877 | case Instruction::Xor: { |
| 878 | Size = addSizeOfGlobalsInConstantVal(Op0, Size); |
| 879 | Size = addSizeOfGlobalsInConstantVal(CE->getOperand(1), Size); |
| 880 | break; |
| 881 | } |
| 882 | default: { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 883 | std::string msg; |
| 884 | raw_string_ostream Msg(msg); |
| 885 | Msg << "ConstantExpr not handled: " << *CE; |
| 886 | llvm_report_error(Msg.str()); |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | if (C->getType()->getTypeID() == Type::PointerTyID) |
| 892 | if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C)) |
Evan Cheng | 47c01a0 | 2008-11-07 09:02:17 +0000 | [diff] [blame] | 893 | if (GVSet.insert(GV)) |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 894 | Size = addSizeOfGlobal(GV, Size); |
| 895 | |
| 896 | return Size; |
| 897 | } |
| 898 | |
| 899 | /// addSizeOfGLobalsInInitializer - handle any globals that we haven't seen yet |
| 900 | /// but are referenced from the given initializer. |
| 901 | |
| 902 | unsigned JITEmitter::addSizeOfGlobalsInInitializer(const Constant *Init, |
| 903 | unsigned Size) { |
| 904 | if (!isa<UndefValue>(Init) && |
| 905 | !isa<ConstantVector>(Init) && |
| 906 | !isa<ConstantAggregateZero>(Init) && |
| 907 | !isa<ConstantArray>(Init) && |
| 908 | !isa<ConstantStruct>(Init) && |
| 909 | Init->getType()->isFirstClassType()) |
| 910 | Size = addSizeOfGlobalsInConstantVal(Init, Size); |
| 911 | return Size; |
| 912 | } |
| 913 | |
| 914 | /// GetSizeOfGlobalsInBytes - walk the code for the function, looking for |
| 915 | /// globals; then walk the initializers of those globals looking for more. |
| 916 | /// If their size has not been considered yet, add it into the running total |
| 917 | /// Size. |
| 918 | |
| 919 | unsigned JITEmitter::GetSizeOfGlobalsInBytes(MachineFunction &MF) { |
| 920 | unsigned Size = 0; |
| 921 | GVSet.clear(); |
| 922 | |
| 923 | for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); |
| 924 | MBB != E; ++MBB) { |
| 925 | for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end(); |
| 926 | I != E; ++I) { |
| 927 | const TargetInstrDesc &Desc = I->getDesc(); |
| 928 | const MachineInstr &MI = *I; |
| 929 | unsigned NumOps = Desc.getNumOperands(); |
| 930 | for (unsigned CurOp = 0; CurOp < NumOps; CurOp++) { |
| 931 | const MachineOperand &MO = MI.getOperand(CurOp); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 932 | if (MO.isGlobal()) { |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 933 | GlobalValue* V = MO.getGlobal(); |
| 934 | const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V); |
| 935 | if (!GV) |
| 936 | continue; |
| 937 | // If seen in previous function, it will have an entry here. |
| 938 | if (TheJIT->getPointerToGlobalIfAvailable(GV)) |
| 939 | continue; |
| 940 | // If seen earlier in this function, it will have an entry here. |
| 941 | // FIXME: it should be possible to combine these tables, by |
| 942 | // assuming the addresses of the new globals in this module |
| 943 | // start at 0 (or something) and adjusting them after codegen |
| 944 | // complete. Another possibility is to grab a marker bit in GV. |
Evan Cheng | 47c01a0 | 2008-11-07 09:02:17 +0000 | [diff] [blame] | 945 | if (GVSet.insert(GV)) |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 946 | // A variable as yet unseen. Add in its size. |
| 947 | Size = addSizeOfGlobal(GV, Size); |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | } |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 952 | DEBUG(errs() << "JIT: About to look through initializers\n"); |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 953 | // Look for more globals that are referenced only from initializers. |
| 954 | // GVSet.end is computed each time because the set can grow as we go. |
Evan Cheng | 47c01a0 | 2008-11-07 09:02:17 +0000 | [diff] [blame] | 955 | for (SmallPtrSet<const GlobalVariable *, 8>::iterator I = GVSet.begin(); |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 956 | I != GVSet.end(); I++) { |
| 957 | const GlobalVariable* GV = *I; |
| 958 | if (GV->hasInitializer()) |
| 959 | Size = addSizeOfGlobalsInInitializer(GV->getInitializer(), Size); |
| 960 | } |
| 961 | |
| 962 | return Size; |
| 963 | } |
| 964 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 965 | void JITEmitter::startFunction(MachineFunction &F) { |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 966 | DEBUG(errs() << "JIT: Starting CodeGen of Function " |
| 967 | << F.getFunction()->getName() << "\n"); |
Evan Cheng | eb5d95a | 2008-11-06 17:46:04 +0000 | [diff] [blame] | 968 | |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 969 | uintptr_t ActualSize = 0; |
Jim Grosbach | cce6c29 | 2008-10-03 16:17:20 +0000 | [diff] [blame] | 970 | // Set the memory writable, if it's not already |
| 971 | MemMgr->setMemoryWritable(); |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 972 | if (MemMgr->NeedsExactSize()) { |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 973 | DEBUG(errs() << "JIT: ExactSize\n"); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 974 | const TargetInstrInfo* TII = F.getTarget().getInstrInfo(); |
| 975 | MachineJumpTableInfo *MJTI = F.getJumpTableInfo(); |
| 976 | MachineConstantPool *MCP = F.getConstantPool(); |
| 977 | |
| 978 | // Ensure the constant pool/jump table info is at least 4-byte aligned. |
Nicolas Geoffray | 580631a | 2008-04-20 23:39:44 +0000 | [diff] [blame] | 979 | ActualSize = RoundUpToAlign(ActualSize, 16); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 980 | |
| 981 | // Add the alignment of the constant pool |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 982 | ActualSize = RoundUpToAlign(ActualSize, MCP->getConstantPoolAlignment()); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 983 | |
| 984 | // Add the constant pool size |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 985 | ActualSize += GetConstantPoolSizeInBytes(MCP, TheJIT->getTargetData()); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 986 | |
| 987 | // Add the aligment of the jump table info |
Nicolas Geoffray | 580631a | 2008-04-20 23:39:44 +0000 | [diff] [blame] | 988 | ActualSize = RoundUpToAlign(ActualSize, MJTI->getAlignment()); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 989 | |
| 990 | // Add the jump table size |
| 991 | ActualSize += GetJumpTableSizeInBytes(MJTI); |
| 992 | |
| 993 | // Add the alignment for the function |
Nicolas Geoffray | 580631a | 2008-04-20 23:39:44 +0000 | [diff] [blame] | 994 | ActualSize = RoundUpToAlign(ActualSize, |
| 995 | std::max(F.getFunction()->getAlignment(), 8U)); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 996 | |
| 997 | // Add the function size |
| 998 | ActualSize += TII->GetFunctionSizeInBytes(F); |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 999 | |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1000 | DEBUG(errs() << "JIT: ActualSize before globals " << ActualSize << "\n"); |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 1001 | // Add the size of the globals that will be allocated after this function. |
| 1002 | // These are all the ones referenced from this function that were not |
| 1003 | // previously allocated. |
| 1004 | ActualSize += GetSizeOfGlobalsInBytes(F); |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1005 | DEBUG(errs() << "JIT: ActualSize after globals " << ActualSize << "\n"); |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 1006 | } else if (SizeEstimate > 0) { |
| 1007 | // SizeEstimate will be non-zero on reallocation attempts. |
| 1008 | ActualSize = SizeEstimate; |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 1011 | BufferBegin = CurBufferPtr = MemMgr->startFunctionBody(F.getFunction(), |
| 1012 | ActualSize); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1013 | BufferEnd = BufferBegin+ActualSize; |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 1014 | |
Evan Cheng | 9a1e9b9 | 2006-11-16 20:04:54 +0000 | [diff] [blame] | 1015 | // Ensure the constant pool/jump table info is at least 4-byte aligned. |
| 1016 | emitAlignment(16); |
| 1017 | |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 1018 | emitConstantPool(F.getConstantPool()); |
| 1019 | initJumpTableInfo(F.getJumpTableInfo()); |
| 1020 | |
| 1021 | // About to start emitting the machine code for the function. |
Chris Lattner | 0eb4d6b | 2006-05-03 01:03:20 +0000 | [diff] [blame] | 1022 | emitAlignment(std::max(F.getFunction()->getAlignment(), 8U)); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 1023 | TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr); |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 1024 | |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 1025 | MBBLocations.clear(); |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 1026 | |
| 1027 | EmissionDetails.MF = &F; |
| 1028 | EmissionDetails.LineStarts.clear(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 1031 | bool JITEmitter::finishFunction(MachineFunction &F) { |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1032 | if (CurBufferPtr == BufferEnd) { |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 1033 | // We must call endFunctionBody before retrying, because |
| 1034 | // deallocateMemForFunction requires it. |
| 1035 | MemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr); |
| 1036 | retryWithMoreMemory(F); |
| 1037 | return true; |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1038 | } |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 1039 | |
Jim Laskey | b92767a | 2006-12-14 22:53:42 +0000 | [diff] [blame] | 1040 | emitJumpTableInfo(F.getJumpTableInfo()); |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 1041 | |
Chris Lattner | a827953 | 2006-06-16 18:09:26 +0000 | [diff] [blame] | 1042 | // FnStart is the start of the text, not the start of the constant pool and |
| 1043 | // other per-function data. |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 1044 | uint8_t *FnStart = |
| 1045 | (uint8_t *)TheJIT->getPointerToGlobalIfAvailable(F.getFunction()); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1046 | |
Argyrios Kyrtzidis | 19fee41 | 2009-04-30 23:01:58 +0000 | [diff] [blame] | 1047 | // FnEnd is the end of the function's machine code. |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 1048 | uint8_t *FnEnd = CurBufferPtr; |
Argyrios Kyrtzidis | 19fee41 | 2009-04-30 23:01:58 +0000 | [diff] [blame] | 1049 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 1050 | if (!Relocations.empty()) { |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 1051 | CurFn = F.getFunction(); |
Chris Lattner | e884dc2 | 2005-07-20 16:29:20 +0000 | [diff] [blame] | 1052 | NumRelos += Relocations.size(); |
| 1053 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 1054 | // Resolve the relocations to concrete pointers. |
| 1055 | for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { |
| 1056 | MachineRelocation &MR = Relocations[i]; |
Evan Cheng | 9200605 | 2008-11-03 07:14:02 +0000 | [diff] [blame] | 1057 | void *ResultPtr = 0; |
Evan Cheng | ef5784e | 2008-10-29 23:54:46 +0000 | [diff] [blame] | 1058 | if (!MR.letTargetResolve()) { |
Evan Cheng | d7398c9 | 2008-11-08 07:37:34 +0000 | [diff] [blame] | 1059 | if (MR.isExternalSymbol()) { |
Dan Gohman | 69f9378 | 2009-01-05 05:32:42 +0000 | [diff] [blame] | 1060 | ResultPtr = TheJIT->getPointerToNamedFunction(MR.getExternalSymbol(), |
| 1061 | false); |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1062 | DEBUG(errs() << "JIT: Map \'" << MR.getExternalSymbol() << "\' to [" |
| 1063 | << ResultPtr << "]\n"); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 1064 | |
Evan Cheng | ef5784e | 2008-10-29 23:54:46 +0000 | [diff] [blame] | 1065 | // If the target REALLY wants a stub for this function, emit it now. |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 1066 | if (!MR.doesntNeedStub()) { |
| 1067 | if (!TheJIT->areDlsymStubsEnabled()) { |
| 1068 | ResultPtr = Resolver.getExternalFunctionStub(ResultPtr); |
| 1069 | } else { |
| 1070 | void *&Stub = ExtFnStubs[MR.getExternalSymbol()]; |
| 1071 | if (!Stub) { |
| 1072 | Stub = Resolver.getExternalFunctionStub((void *)&Stub); |
| 1073 | AddStubToCurrentFunction(Stub); |
| 1074 | } |
| 1075 | ResultPtr = Stub; |
| 1076 | } |
| 1077 | } |
Evan Cheng | ef5784e | 2008-10-29 23:54:46 +0000 | [diff] [blame] | 1078 | } else if (MR.isGlobalValue()) { |
| 1079 | ResultPtr = getPointerToGlobal(MR.getGlobalValue(), |
| 1080 | BufferBegin+MR.getMachineCodeOffset(), |
| 1081 | MR.doesntNeedStub()); |
Evan Cheng | 5594f12 | 2008-11-10 01:52:24 +0000 | [diff] [blame] | 1082 | } else if (MR.isIndirectSymbol()) { |
| 1083 | ResultPtr = getPointerToGVIndirectSym(MR.getGlobalValue(), |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 1084 | BufferBegin+MR.getMachineCodeOffset(), |
| 1085 | MR.doesntNeedStub()); |
Evan Cheng | ef5784e | 2008-10-29 23:54:46 +0000 | [diff] [blame] | 1086 | } else if (MR.isBasicBlock()) { |
| 1087 | ResultPtr = (void*)getMachineBasicBlockAddress(MR.getBasicBlock()); |
| 1088 | } else if (MR.isConstantPoolIndex()) { |
| 1089 | ResultPtr = (void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex()); |
| 1090 | } else { |
| 1091 | assert(MR.isJumpTableIndex()); |
| 1092 | ResultPtr=(void*)getJumpTableEntryAddress(MR.getJumpTableIndex()); |
| 1093 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1094 | |
Evan Cheng | ef5784e | 2008-10-29 23:54:46 +0000 | [diff] [blame] | 1095 | MR.setResultPointer(ResultPtr); |
| 1096 | } |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 1097 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 1098 | // if we are managing the GOT and the relocation wants an index, |
| 1099 | // give it one |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 1100 | if (MR.isGOTRelative() && MemMgr->isManagingGOT()) { |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 1101 | unsigned idx = Resolver.getGOTIndexForAddr(ResultPtr); |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 1102 | MR.setGOTIndex(idx); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 1103 | if (((void**)MemMgr->getGOTBase())[idx] != ResultPtr) { |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1104 | DEBUG(errs() << "JIT: GOT was out of date for " << ResultPtr |
| 1105 | << " pointing at " << ((void**)MemMgr->getGOTBase())[idx] |
| 1106 | << "\n"); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 1107 | ((void**)MemMgr->getGOTBase())[idx] = ResultPtr; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 1108 | } |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 1109 | } |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 1112 | CurFn = 0; |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 1113 | TheJIT->getJITInfo().relocate(BufferBegin, &Relocations[0], |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 1114 | Relocations.size(), MemMgr->getGOTBase()); |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Chris Lattner | d2d5c76 | 2006-05-03 18:55:56 +0000 | [diff] [blame] | 1117 | // Update the GOT entry for F to point to the new code. |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 1118 | if (MemMgr->isManagingGOT()) { |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 1119 | unsigned idx = Resolver.getGOTIndexForAddr((void*)BufferBegin); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 1120 | if (((void**)MemMgr->getGOTBase())[idx] != (void*)BufferBegin) { |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1121 | DEBUG(errs() << "JIT: GOT was out of date for " << (void*)BufferBegin |
| 1122 | << " pointing at " << ((void**)MemMgr->getGOTBase())[idx] |
| 1123 | << "\n"); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 1124 | ((void**)MemMgr->getGOTBase())[idx] = (void*)BufferBegin; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | |
Argyrios Kyrtzidis | 19fee41 | 2009-04-30 23:01:58 +0000 | [diff] [blame] | 1128 | // CurBufferPtr may have moved beyond FnEnd, due to memory allocation for |
| 1129 | // global variables that were referenced in the relocations. |
| 1130 | MemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr); |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 1131 | |
| 1132 | if (CurBufferPtr == BufferEnd) { |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 1133 | retryWithMoreMemory(F); |
| 1134 | return true; |
| 1135 | } else { |
| 1136 | // Now that we've succeeded in emitting the function, reset the |
| 1137 | // SizeEstimate back down to zero. |
| 1138 | SizeEstimate = 0; |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 1141 | BufferBegin = CurBufferPtr = 0; |
| 1142 | NumBytes += FnEnd-FnStart; |
| 1143 | |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 1144 | // Invalidate the icache if necessary. |
Chris Lattner | bc52cad | 2008-06-25 17:18:44 +0000 | [diff] [blame] | 1145 | sys::Memory::InvalidateInstructionCache(FnStart, FnEnd-FnStart); |
Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 1146 | |
Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 1147 | TheJIT->NotifyFunctionEmitted(*F.getFunction(), FnStart, FnEnd-FnStart, |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 1148 | EmissionDetails); |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 1149 | |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 1150 | DEBUG(errs() << "JIT: Finished CodeGen of [" << (void*)FnStart |
| 1151 | << "] Function: " << F.getFunction()->getName() |
| 1152 | << ": " << (FnEnd-FnStart) << " bytes of text, " |
| 1153 | << Relocations.size() << " relocations\n"); |
Argyrios Kyrtzidis | b3a847d | 2009-05-18 21:06:40 +0000 | [diff] [blame] | 1154 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 1155 | Relocations.clear(); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 1156 | ConstPoolAddresses.clear(); |
Anton Korobeynikov | 8cd4c3e | 2007-01-19 17:25:17 +0000 | [diff] [blame] | 1157 | |
Evan Cheng | bc4707a | 2008-09-18 07:54:21 +0000 | [diff] [blame] | 1158 | // Mark code region readable and executable if it's not so already. |
Jim Grosbach | cce6c29 | 2008-10-03 16:17:20 +0000 | [diff] [blame] | 1159 | MemMgr->setMemoryExecutable(); |
Evan Cheng | bc4707a | 2008-09-18 07:54:21 +0000 | [diff] [blame] | 1160 | |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1161 | DEBUG( |
Evan Cheng | e7c3551 | 2008-11-12 08:22:43 +0000 | [diff] [blame] | 1162 | if (sys::hasDisassembler()) { |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1163 | errs() << "JIT: Disassembled code:\n"; |
| 1164 | errs() << sys::disassembleBuffer(FnStart, FnEnd-FnStart, |
| 1165 | (uintptr_t)FnStart); |
Evan Cheng | e7c3551 | 2008-11-12 08:22:43 +0000 | [diff] [blame] | 1166 | } else { |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1167 | errs() << "JIT: Binary code:\n"; |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 1168 | uint8_t* q = FnStart; |
Evan Cheng | e7c3551 | 2008-11-12 08:22:43 +0000 | [diff] [blame] | 1169 | for (int i = 0; q < FnEnd; q += 4, ++i) { |
| 1170 | if (i == 4) |
| 1171 | i = 0; |
| 1172 | if (i == 0) |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1173 | errs() << "JIT: " << (long)(q - FnStart) << ": "; |
Evan Cheng | e7c3551 | 2008-11-12 08:22:43 +0000 | [diff] [blame] | 1174 | bool Done = false; |
| 1175 | for (int j = 3; j >= 0; --j) { |
| 1176 | if (q + j >= FnEnd) |
| 1177 | Done = true; |
| 1178 | else |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1179 | errs() << (unsigned short)q[j]; |
Evan Cheng | e7c3551 | 2008-11-12 08:22:43 +0000 | [diff] [blame] | 1180 | } |
| 1181 | if (Done) |
| 1182 | break; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1183 | errs() << ' '; |
Evan Cheng | e7c3551 | 2008-11-12 08:22:43 +0000 | [diff] [blame] | 1184 | if (i == 3) |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1185 | errs() << '\n'; |
Evan Cheng | a7916f5 | 2008-11-05 23:44:08 +0000 | [diff] [blame] | 1186 | } |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1187 | errs()<< '\n'; |
Evan Cheng | a7916f5 | 2008-11-05 23:44:08 +0000 | [diff] [blame] | 1188 | } |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1189 | ); |
| 1190 | |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 1191 | if (DwarfExceptionHandling || JITEmitDebugInfo) { |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 1192 | uintptr_t ActualSize = 0; |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 1193 | SavedBufferBegin = BufferBegin; |
| 1194 | SavedBufferEnd = BufferEnd; |
| 1195 | SavedCurBufferPtr = CurBufferPtr; |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 1196 | |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 1197 | if (MemMgr->NeedsExactSize()) { |
| 1198 | ActualSize = DE->GetDwarfTableSizeInBytes(F, *this, FnStart, FnEnd); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 1199 | } |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 1200 | |
| 1201 | BufferBegin = CurBufferPtr = MemMgr->startExceptionTable(F.getFunction(), |
| 1202 | ActualSize); |
| 1203 | BufferEnd = BufferBegin+ActualSize; |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 1204 | uint8_t *EhStart; |
| 1205 | uint8_t *FrameRegister = DE->EmitDwarfTable(F, *this, FnStart, FnEnd, |
| 1206 | EhStart); |
Chris Lattner | 0fdaa0b | 2008-03-07 20:05:43 +0000 | [diff] [blame] | 1207 | MemMgr->endExceptionTable(F.getFunction(), BufferBegin, CurBufferPtr, |
| 1208 | FrameRegister); |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 1209 | uint8_t *EhEnd = CurBufferPtr; |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 1210 | BufferBegin = SavedBufferBegin; |
| 1211 | BufferEnd = SavedBufferEnd; |
| 1212 | CurBufferPtr = SavedCurBufferPtr; |
| 1213 | |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 1214 | if (DwarfExceptionHandling) { |
| 1215 | TheJIT->RegisterTable(FrameRegister); |
| 1216 | } |
| 1217 | |
| 1218 | if (JITEmitDebugInfo) { |
| 1219 | DebugInfo I; |
| 1220 | I.FnStart = FnStart; |
| 1221 | I.FnEnd = FnEnd; |
| 1222 | I.EhStart = EhStart; |
| 1223 | I.EhEnd = EhEnd; |
| 1224 | DR->RegisterFunction(F.getFunction(), I); |
| 1225 | } |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 1226 | } |
Evan Cheng | 252ddfb | 2008-09-02 08:14:01 +0000 | [diff] [blame] | 1227 | |
| 1228 | if (MMI) |
| 1229 | MMI->EndFunction(); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 1230 | |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 1231 | return false; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 1234 | void JITEmitter::retryWithMoreMemory(MachineFunction &F) { |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1235 | DEBUG(errs() << "JIT: Ran out of space for native code. Reattempting.\n"); |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 1236 | Relocations.clear(); // Clear the old relocations or we'll reapply them. |
| 1237 | ConstPoolAddresses.clear(); |
| 1238 | ++NumRetries; |
| 1239 | deallocateMemForFunction(F.getFunction()); |
| 1240 | // Try again with at least twice as much free space. |
| 1241 | SizeEstimate = (uintptr_t)(2 * (BufferEnd - BufferBegin)); |
| 1242 | } |
| 1243 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 1244 | /// deallocateMemForFunction - Deallocate all memory for the specified |
| 1245 | /// function body. Also drop any references the function has to stubs. |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 1246 | void JITEmitter::deallocateMemForFunction(const Function *F) { |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 1247 | MemMgr->deallocateMemForFunction(F); |
| 1248 | |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 1249 | // TODO: Do we need to unregister exception handling information from libgcc |
| 1250 | // here? |
| 1251 | |
| 1252 | if (JITEmitDebugInfo) { |
| 1253 | DR->UnregisterFunction(F); |
| 1254 | } |
| 1255 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 1256 | // If the function did not reference any stubs, return. |
| 1257 | if (CurFnStubUses.find(F) == CurFnStubUses.end()) |
| 1258 | return; |
| 1259 | |
| 1260 | // For each referenced stub, erase the reference to this function, and then |
| 1261 | // erase the list of referenced stubs. |
| 1262 | SmallVectorImpl<void *> &StubList = CurFnStubUses[F]; |
| 1263 | for (unsigned i = 0, e = StubList.size(); i != e; ++i) { |
| 1264 | void *Stub = StubList[i]; |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 1265 | |
| 1266 | // If we already invalidated this stub for this function, continue. |
| 1267 | if (StubFnRefs.count(Stub) == 0) |
| 1268 | continue; |
| 1269 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 1270 | SmallPtrSet<const Function *, 1> &FnRefs = StubFnRefs[Stub]; |
| 1271 | FnRefs.erase(F); |
| 1272 | |
| 1273 | // If this function was the last reference to the stub, invalidate the stub |
| 1274 | // in the JITResolver. Were there a memory manager deallocateStub routine, |
| 1275 | // we could call that at this point too. |
| 1276 | if (FnRefs.empty()) { |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1277 | DEBUG(errs() << "\nJIT: Invalidated Stub at [" << Stub << "]\n"); |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 1278 | StubFnRefs.erase(Stub); |
| 1279 | |
| 1280 | // Invalidate the stub. If it is a GV stub, update the JIT's global |
| 1281 | // mapping for that GV to zero, otherwise, search the string map of |
| 1282 | // external function names to stubs and remove the entry for this stub. |
Nate Begeman | b9c6c9b | 2009-03-07 06:41:19 +0000 | [diff] [blame] | 1283 | GlobalValue *GV = Resolver.invalidateStub(Stub); |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 1284 | if (GV) { |
| 1285 | TheJIT->updateGlobalMapping(GV, 0); |
| 1286 | } else { |
| 1287 | for (StringMapIterator<void*> i = ExtFnStubs.begin(), |
| 1288 | e = ExtFnStubs.end(); i != e; ++i) { |
| 1289 | if (i->second == Stub) { |
| 1290 | ExtFnStubs.erase(i); |
| 1291 | break; |
| 1292 | } |
| 1293 | } |
| 1294 | } |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 1295 | } |
| 1296 | } |
| 1297 | CurFnStubUses.erase(F); |
| 1298 | } |
| 1299 | |
| 1300 | |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 1301 | void* JITEmitter::allocateSpace(uintptr_t Size, unsigned Alignment) { |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 1302 | if (BufferBegin) |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 1303 | return JITCodeEmitter::allocateSpace(Size, Alignment); |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 1304 | |
| 1305 | // create a new memory block if there is no active one. |
| 1306 | // care must be taken so that BufferBegin is invalidated when a |
| 1307 | // block is trimmed |
| 1308 | BufferBegin = CurBufferPtr = MemMgr->allocateSpace(Size, Alignment); |
| 1309 | BufferEnd = BufferBegin+Size; |
| 1310 | return CurBufferPtr; |
| 1311 | } |
| 1312 | |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 1313 | void* JITEmitter::allocateGlobal(uintptr_t Size, unsigned Alignment) { |
| 1314 | // Delegate this call through the memory manager. |
| 1315 | return MemMgr->allocateGlobal(Size, Alignment); |
| 1316 | } |
| 1317 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 1318 | void JITEmitter::emitConstantPool(MachineConstantPool *MCP) { |
Evan Cheng | 47c01a0 | 2008-11-07 09:02:17 +0000 | [diff] [blame] | 1319 | if (TheJIT->getJITInfo().hasCustomConstantPool()) |
Jim Grosbach | 8fe9535 | 2008-10-30 23:44:39 +0000 | [diff] [blame] | 1320 | return; |
Evan Cheng | 47c01a0 | 2008-11-07 09:02:17 +0000 | [diff] [blame] | 1321 | |
Chris Lattner | fa77d43 | 2006-02-09 04:22:52 +0000 | [diff] [blame] | 1322 | const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants(); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 1323 | if (Constants.empty()) return; |
| 1324 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 1325 | unsigned Size = GetConstantPoolSizeInBytes(MCP, TheJIT->getTargetData()); |
| 1326 | unsigned Align = MCP->getConstantPoolAlignment(); |
Evan Cheng | 97b8c40 | 2008-04-12 00:22:01 +0000 | [diff] [blame] | 1327 | ConstantPoolBase = allocateSpace(Size, Align); |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 1328 | ConstantPool = MCP; |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 1329 | |
| 1330 | if (ConstantPoolBase == 0) return; // Buffer overflow. |
| 1331 | |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1332 | DEBUG(errs() << "JIT: Emitted constant pool at [" << ConstantPoolBase |
| 1333 | << "] (size: " << Size << ", alignment: " << Align << ")\n"); |
Evan Cheng | 97b8c40 | 2008-04-12 00:22:01 +0000 | [diff] [blame] | 1334 | |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 1335 | // Initialize the memory for all of the constant pool entries. |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 1336 | unsigned Offset = 0; |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 1337 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 1338 | MachineConstantPoolEntry CPE = Constants[i]; |
| 1339 | unsigned AlignMask = CPE.getAlignment() - 1; |
| 1340 | Offset = (Offset + AlignMask) & ~AlignMask; |
| 1341 | |
| 1342 | uintptr_t CAddr = (uintptr_t)ConstantPoolBase + Offset; |
| 1343 | ConstPoolAddresses.push_back(CAddr); |
| 1344 | if (CPE.isMachineConstantPoolEntry()) { |
Evan Cheng | cd5731d | 2006-09-12 20:59:59 +0000 | [diff] [blame] | 1345 | // FIXME: add support to lower machine constant pool values into bytes! |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1346 | llvm_report_error("Initialize memory with machine specific constant pool" |
| 1347 | "entry has not been implemented!"); |
Evan Cheng | cd5731d | 2006-09-12 20:59:59 +0000 | [diff] [blame] | 1348 | } |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 1349 | TheJIT->InitializeMemory(CPE.Val.ConstVal, (void*)CAddr); |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1350 | DEBUG(errs() << "JIT: CP" << i << " at [0x"; |
| 1351 | errs().write_hex(CAddr) << "]\n"); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 1352 | |
| 1353 | const Type *Ty = CPE.Val.ConstVal->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1354 | Offset += TheJIT->getTargetData()->getTypeAllocSize(Ty); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1358 | void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) { |
Evan Cheng | 47c01a0 | 2008-11-07 09:02:17 +0000 | [diff] [blame] | 1359 | if (TheJIT->getJITInfo().hasCustomJumpTables()) |
| 1360 | return; |
| 1361 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1362 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 1363 | if (JT.empty()) return; |
| 1364 | |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 1365 | unsigned NumEntries = 0; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1366 | for (unsigned i = 0, e = JT.size(); i != e; ++i) |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 1367 | NumEntries += JT[i].MBBs.size(); |
| 1368 | |
| 1369 | unsigned EntrySize = MJTI->getEntrySize(); |
| 1370 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1371 | // Just allocate space for all the jump tables now. We will fix up the actual |
| 1372 | // MBB entries in the tables after we emit the code for each block, since then |
| 1373 | // we will know the final locations of the MBBs in memory. |
| 1374 | JumpTable = MJTI; |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 1375 | JumpTableBase = allocateSpace(NumEntries * EntrySize, MJTI->getAlignment()); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
Jim Laskey | b92767a | 2006-12-14 22:53:42 +0000 | [diff] [blame] | 1378 | void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) { |
Evan Cheng | 47c01a0 | 2008-11-07 09:02:17 +0000 | [diff] [blame] | 1379 | if (TheJIT->getJITInfo().hasCustomJumpTables()) |
| 1380 | return; |
| 1381 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1382 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 1383 | if (JT.empty() || JumpTableBase == 0) return; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1384 | |
Jim Laskey | b92767a | 2006-12-14 22:53:42 +0000 | [diff] [blame] | 1385 | if (TargetMachine::getRelocationModel() == Reloc::PIC_) { |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1386 | assert(MJTI->getEntrySize() == 4 && "Cross JIT'ing?"); |
| 1387 | // For each jump table, place the offset from the beginning of the table |
| 1388 | // to the target address. |
| 1389 | int *SlotPtr = (int*)JumpTableBase; |
Chris Lattner | 32ca55f | 2006-05-03 00:13:06 +0000 | [diff] [blame] | 1390 | |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1391 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
| 1392 | const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs; |
| 1393 | // Store the offset of the basic block for this jump table slot in the |
| 1394 | // memory we allocated for the jump table in 'initJumpTableInfo' |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 1395 | uintptr_t Base = (uintptr_t)SlotPtr; |
Evan Cheng | 2a3e08b | 2008-01-05 02:26:58 +0000 | [diff] [blame] | 1396 | for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) { |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 1397 | uintptr_t MBBAddr = getMachineBasicBlockAddress(MBBs[mi]); |
Evan Cheng | 2a3e08b | 2008-01-05 02:26:58 +0000 | [diff] [blame] | 1398 | *SlotPtr++ = TheJIT->getJITInfo().getPICJumpTableEntry(MBBAddr, Base); |
| 1399 | } |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1400 | } |
| 1401 | } else { |
| 1402 | assert(MJTI->getEntrySize() == sizeof(void*) && "Cross JIT'ing?"); |
| 1403 | |
| 1404 | // For each jump table, map each target in the jump table to the address of |
| 1405 | // an emitted MachineBasicBlock. |
| 1406 | intptr_t *SlotPtr = (intptr_t*)JumpTableBase; |
| 1407 | |
| 1408 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
| 1409 | const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs; |
| 1410 | // Store the address of the basic block for this jump table slot in the |
| 1411 | // memory we allocated for the jump table in 'initJumpTableInfo' |
| 1412 | for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) |
| 1413 | *SlotPtr++ = getMachineBasicBlockAddress(MBBs[mi]); |
| 1414 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1415 | } |
| 1416 | } |
| 1417 | |
Evan Cheng | ce4a70b | 2008-11-08 08:02:53 +0000 | [diff] [blame] | 1418 | void JITEmitter::startGVStub(const GlobalValue* GV, unsigned StubSize, |
| 1419 | unsigned Alignment) { |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 1420 | SavedBufferBegin = BufferBegin; |
| 1421 | SavedBufferEnd = BufferEnd; |
| 1422 | SavedCurBufferPtr = CurBufferPtr; |
| 1423 | |
Evan Cheng | ce4a70b | 2008-11-08 08:02:53 +0000 | [diff] [blame] | 1424 | BufferBegin = CurBufferPtr = MemMgr->allocateStub(GV, StubSize, Alignment); |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 1425 | BufferEnd = BufferBegin+StubSize+1; |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1428 | void JITEmitter::startGVStub(const GlobalValue* GV, void *Buffer, |
| 1429 | unsigned StubSize) { |
| 1430 | SavedBufferBegin = BufferBegin; |
| 1431 | SavedBufferEnd = BufferEnd; |
| 1432 | SavedCurBufferPtr = CurBufferPtr; |
| 1433 | |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 1434 | BufferBegin = CurBufferPtr = (uint8_t *)Buffer; |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1435 | BufferEnd = BufferBegin+StubSize+1; |
| 1436 | } |
| 1437 | |
Evan Cheng | ce4a70b | 2008-11-08 08:02:53 +0000 | [diff] [blame] | 1438 | void *JITEmitter::finishGVStub(const GlobalValue* GV) { |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 1439 | NumBytes += getCurrentPCOffset(); |
| 1440 | std::swap(SavedBufferBegin, BufferBegin); |
| 1441 | BufferEnd = SavedBufferEnd; |
| 1442 | CurBufferPtr = SavedCurBufferPtr; |
| 1443 | return SavedBufferBegin; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 1446 | // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry |
| 1447 | // in the constant pool that was last emitted with the 'emitConstantPool' |
| 1448 | // method. |
| 1449 | // |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 1450 | uintptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const { |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 1451 | assert(ConstantNum < ConstantPool->getConstants().size() && |
Misha Brukman | 3c94497 | 2005-04-22 04:08:30 +0000 | [diff] [blame] | 1452 | "Invalid ConstantPoolIndex!"); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 1453 | return ConstPoolAddresses[ConstantNum]; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1456 | // getJumpTableEntryAddress - Return the address of the JumpTable with index |
| 1457 | // 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo' |
| 1458 | // |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 1459 | uintptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1460 | const std::vector<MachineJumpTableEntry> &JT = JumpTable->getJumpTables(); |
| 1461 | assert(Index < JT.size() && "Invalid jump table index!"); |
| 1462 | |
| 1463 | unsigned Offset = 0; |
| 1464 | unsigned EntrySize = JumpTable->getEntrySize(); |
| 1465 | |
| 1466 | for (unsigned i = 0; i < Index; ++i) |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1467 | Offset += JT[i].MBBs.size(); |
| 1468 | |
| 1469 | Offset *= EntrySize; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1470 | |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 1471 | return (uintptr_t)((char *)JumpTableBase + Offset); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1474 | //===----------------------------------------------------------------------===// |
| 1475 | // Public interface to this file |
| 1476 | //===----------------------------------------------------------------------===// |
| 1477 | |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 1478 | JITCodeEmitter *JIT::createEmitter(JIT &jit, JITMemoryManager *JMM, |
| 1479 | TargetMachine &tm) { |
| 1480 | return new JITEmitter(jit, JMM, tm); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 1483 | // getPointerToNamedFunction - This function is used as a global wrapper to |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 1484 | // JIT::getPointerToNamedFunction for the purpose of resolving symbols when |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 1485 | // bugpoint is debugging the JIT. In that scenario, we are loading an .so and |
| 1486 | // need to resolve function(s) that are being mis-codegenerated, so we need to |
| 1487 | // resolve their addresses at runtime, and this is the way to do it. |
| 1488 | extern "C" { |
| 1489 | void *getPointerToNamedFunction(const char *Name) { |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1490 | if (Function *F = TheJIT->FindFunctionNamed(Name)) |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 1491 | return TheJIT->getPointerToFunction(F); |
| 1492 | return TheJIT->getPointerToNamedFunction(Name); |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 1493 | } |
| 1494 | } |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1495 | |
| 1496 | // getPointerToFunctionOrStub - If the specified function has been |
| 1497 | // code-gen'd, return a pointer to the function. If not, compile it, or use |
| 1498 | // a stub to implement lazy compilation if available. |
| 1499 | // |
| 1500 | void *JIT::getPointerToFunctionOrStub(Function *F) { |
| 1501 | // If we have already code generated the function, just return the address. |
| 1502 | if (void *Addr = getPointerToGlobalIfAvailable(F)) |
| 1503 | return Addr; |
| 1504 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 1505 | // Get a stub if the target supports it. |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 1506 | assert(isa<JITEmitter>(JCE) && "Unexpected MCE?"); |
Evan Cheng | a044dfc | 2008-08-20 00:28:12 +0000 | [diff] [blame] | 1507 | JITEmitter *JE = cast<JITEmitter>(getCodeEmitter()); |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 1508 | return JE->getJITResolver().getFunctionStub(F); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1511 | void JIT::updateFunctionStub(Function *F) { |
| 1512 | // Get the empty stub we generated earlier. |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 1513 | assert(isa<JITEmitter>(JCE) && "Unexpected MCE?"); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1514 | JITEmitter *JE = cast<JITEmitter>(getCodeEmitter()); |
| 1515 | void *Stub = JE->getJITResolver().getFunctionStub(F); |
| 1516 | |
| 1517 | // Tell the target jit info to rewrite the stub at the specified address, |
| 1518 | // rather than creating a new one. |
| 1519 | void *Addr = getPointerToGlobalIfAvailable(F); |
| 1520 | getJITInfo().emitFunctionStubAtAddr(F, Addr, Stub, *getCodeEmitter()); |
| 1521 | } |
| 1522 | |
| 1523 | /// updateDlsymStubTable - Emit the data necessary to relocate the stubs |
| 1524 | /// that were emitted during code generation. |
| 1525 | /// |
| 1526 | void JIT::updateDlsymStubTable() { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 1527 | assert(isa<JITEmitter>(JCE) && "Unexpected MCE?"); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1528 | JITEmitter *JE = cast<JITEmitter>(getCodeEmitter()); |
| 1529 | |
| 1530 | SmallVector<GlobalValue*, 8> GVs; |
| 1531 | SmallVector<void*, 8> Ptrs; |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 1532 | const StringMap<void *> &ExtFns = JE->getExternalFnStubs(); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1533 | |
| 1534 | JE->getJITResolver().getRelocatableGVs(GVs, Ptrs); |
| 1535 | |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 1536 | unsigned nStubs = GVs.size() + ExtFns.size(); |
| 1537 | |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1538 | // If there are no relocatable stubs, return. |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 1539 | if (nStubs == 0) |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1540 | return; |
| 1541 | |
| 1542 | // If there are no new relocatable stubs, return. |
| 1543 | void *CurTable = JE->getMemMgr()->getDlsymTable(); |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 1544 | if (CurTable && (*(unsigned *)CurTable == nStubs)) |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1545 | return; |
| 1546 | |
| 1547 | // Calculate the size of the stub info |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 1548 | unsigned offset = 4 + 4 * nStubs + sizeof(intptr_t) * nStubs; |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1549 | |
| 1550 | SmallVector<unsigned, 8> Offsets; |
| 1551 | for (unsigned i = 0; i != GVs.size(); ++i) { |
| 1552 | Offsets.push_back(offset); |
Daniel Dunbar | fbee579 | 2009-07-21 08:54:24 +0000 | [diff] [blame] | 1553 | offset += GVs[i]->getName().size() + 1; |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1554 | } |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 1555 | for (StringMapConstIterator<void*> i = ExtFns.begin(), e = ExtFns.end(); |
| 1556 | i != e; ++i) { |
| 1557 | Offsets.push_back(offset); |
| 1558 | offset += strlen(i->first()) + 1; |
| 1559 | } |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1560 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 1561 | // Allocate space for the new "stub", which contains the dlsym table. |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1562 | JE->startGVStub(0, offset, 4); |
| 1563 | |
| 1564 | // Emit the number of records |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 1565 | JE->emitInt32(nStubs); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1566 | |
| 1567 | // Emit the string offsets |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 1568 | for (unsigned i = 0; i != nStubs; ++i) |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 1569 | JE->emitInt32(Offsets[i]); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1570 | |
Nate Begeman | 6694198 | 2009-03-04 19:10:38 +0000 | [diff] [blame] | 1571 | // Emit the pointers. Verify that they are at least 2-byte aligned, and set |
| 1572 | // the low bit to 0 == GV, 1 == Function, so that the client code doing the |
| 1573 | // relocation can write the relocated pointer at the appropriate place in |
| 1574 | // the stub. |
| 1575 | for (unsigned i = 0; i != GVs.size(); ++i) { |
| 1576 | intptr_t Ptr = (intptr_t)Ptrs[i]; |
| 1577 | assert((Ptr & 1) == 0 && "Stub pointers must be at least 2-byte aligned!"); |
| 1578 | |
| 1579 | if (isa<Function>(GVs[i])) |
| 1580 | Ptr |= (intptr_t)1; |
| 1581 | |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 1582 | if (sizeof(Ptr) == 8) |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 1583 | JE->emitInt64(Ptr); |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 1584 | else |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 1585 | JE->emitInt32(Ptr); |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 1586 | } |
| 1587 | for (StringMapConstIterator<void*> i = ExtFns.begin(), e = ExtFns.end(); |
| 1588 | i != e; ++i) { |
| 1589 | intptr_t Ptr = (intptr_t)i->second | 1; |
| 1590 | |
| 1591 | if (sizeof(Ptr) == 8) |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 1592 | JE->emitInt64(Ptr); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1593 | else |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 1594 | JE->emitInt32(Ptr); |
Nate Begeman | 6694198 | 2009-03-04 19:10:38 +0000 | [diff] [blame] | 1595 | } |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1596 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 1597 | // Emit the strings. |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1598 | for (unsigned i = 0; i != GVs.size(); ++i) |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 1599 | JE->emitString(GVs[i]->getName()); |
Nate Begeman | 841c6a4 | 2009-03-11 07:03:43 +0000 | [diff] [blame] | 1600 | for (StringMapConstIterator<void*> i = ExtFns.begin(), e = ExtFns.end(); |
| 1601 | i != e; ++i) |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 1602 | JE->emitString(i->first()); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1603 | |
Nate Begeman | 50cd6fd | 2009-03-05 06:34:37 +0000 | [diff] [blame] | 1604 | // Tell the JIT memory manager where it is. The JIT Memory Manager will |
| 1605 | // deallocate space for the old one, if one existed. |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 1606 | JE->getMemMgr()->SetDlsymTable(JE->finishGVStub(0)); |
| 1607 | } |
| 1608 | |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1609 | /// freeMachineCodeForFunction - release machine code memory for given Function. |
| 1610 | /// |
| 1611 | void JIT::freeMachineCodeForFunction(Function *F) { |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 1612 | |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1613 | // Delete translation for this from the ExecutionEngine, so it will get |
| 1614 | // retranslated next time it is used. |
Chris Lattner | 8ac66c1 | 2008-04-04 05:51:42 +0000 | [diff] [blame] | 1615 | void *OldPtr = updateGlobalMapping(F, 0); |
| 1616 | |
| 1617 | if (OldPtr) |
Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 1618 | TheJIT->NotifyFreeingMachineCode(*F, OldPtr); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1619 | |
| 1620 | // Free the actual memory for the function body and related stuff. |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 1621 | assert(isa<JITEmitter>(JCE) && "Unexpected MCE?"); |
| 1622 | cast<JITEmitter>(JCE)->deallocateMemForFunction(F); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 1623 | } |