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