llvm.org GIT mirror llvm / 875710a
Nuke the old JIT. I am sure we will be finding bits and pieces of dead code for years to come, but this is a good start. Thanks to Lang Hames for making MCJIT a good replacement! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215111 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
225 changed file(s) with 1082 addition(s) and 13383 deletion(s). Raw diff Collapse all Expand all
16721672 $(TARGET:%=$(ObjDir)/%GenMCCodeEmitter.inc.tmp): \
16731673 $(ObjDir)/%GenMCCodeEmitter.inc.tmp: %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
16741674 $(Echo) "Building $(
1675 $(Verb) $(LLVMTableGen) -gen-emitter -mc-emitter -o $(call SYSPATH, $@) $<
1675 $(Verb) $(LLVMTableGen) -gen-emitter -o $(call SYSPATH, $@) $<
16761676
16771677 $(TARGET:%=$(ObjDir)/%GenMCPseudoLowering.inc.tmp): \
16781678 $(ObjDir)/%GenMCPseudoLowering.inc.tmp: %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
16791679 $(Echo) "Building $(
16801680 $(Verb) $(LLVMTableGen) -gen-pseudo-lowering -o $(call SYSPATH, $@) $<
1681
1682 $(TARGET:%=$(ObjDir)/%GenCodeEmitter.inc.tmp): \
1683 $(ObjDir)/%GenCodeEmitter.inc.tmp: %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
1684 $(Echo) "Building $(
1685 $(Verb) $(LLVMTableGen) -gen-emitter -o $(call SYSPATH, $@) $<
16861681
16871682 $(TARGET:%=$(ObjDir)/%GenDAGISel.inc.tmp): \
16881683 $(ObjDir)/%GenDAGISel.inc.tmp : %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
7777 **Output**: C++ code, implementing the target's CodeEmitter
7878 class by overriding the virtual functions as ``CodeEmitter::function()``.
7979
80 **Usage**: Used to include directly at the end of ``CodeEmitter.cpp``, and
81 with option `-mc-emitter` to be included in ``MCCodeEmitter.cpp``.
80 **Usage**: Used to include directly at the end of ``MCCodeEmitter.cpp``.
8281
8382 RegisterInfo
8483 ------------
2525
2626 #include "BrainF.h"
2727 #include "llvm/Bitcode/ReaderWriter.h"
28 #include "llvm/ExecutionEngine/ExecutionEngine.h"
2829 #include "llvm/ExecutionEngine/GenericValue.h"
29 #include "llvm/ExecutionEngine/JIT.h"
3030 #include "llvm/IR/Constants.h"
3131 #include "llvm/IR/Verifier.h"
3232 #include "llvm/Support/CommandLine.h"
11 BitWriter
22 Core
33 ExecutionEngine
4 JIT
54 MC
65 Support
76 nativecodegen
19631963 // Build engine with JIT
19641964 llvm::EngineBuilder factory(module);
19651965 factory.setEngineKind(llvm::EngineKind::JIT);
1966 factory.setAllocateGVsWithCode(false);
19671966 factory.setTargetOptions(Opts);
19681967 factory.setMCJITMemoryManager(MemMgr);
1969 factory.setUseMCJIT(true);
19701968 llvm::ExecutionEngine *executionEngine = factory.create();
19711969
19721970 {
11 Core
22 ExecutionEngine
33 Interpreter
4 JIT
54 MC
65 Support
76 nativecodegen
2525 #include "llvm/IR/Verifier.h"
2626 #include "llvm/ExecutionEngine/GenericValue.h"
2727 #include "llvm/ExecutionEngine/Interpreter.h"
28 #include "llvm/ExecutionEngine/JIT.h"
2928 #include "llvm/IR/Constants.h"
3029 #include "llvm/IR/DerivedTypes.h"
3130 #include "llvm/IR/Instructions.h"
11 Core
22 ExecutionEngine
33 Interpreter
4 JIT
54 MC
65 Support
76 nativecodegen
3535
3636 #include "llvm/ExecutionEngine/GenericValue.h"
3737 #include "llvm/ExecutionEngine/Interpreter.h"
38 #include "llvm/ExecutionEngine/JIT.h"
3938 #include "llvm/IR/Constants.h"
4039 #include "llvm/IR/DerivedTypes.h"
4140 #include "llvm/IR/IRBuilder.h"
125124
126125 // Import result of execution:
127126 outs() << "Result: " << gv.IntVal << "\n";
128 EE->freeMachineCodeForFunction(FooF);
129127 delete EE;
130128 llvm_shutdown();
131129 return 0;
22 Core
33 ExecutionEngine
44 InstCombine
5 JIT
65 MC
76 ScalarOpts
87 Support
0 #include "llvm/Analysis/Passes.h"
11 #include "llvm/ExecutionEngine/ExecutionEngine.h"
2 #include "llvm/ExecutionEngine/JIT.h"
32 #include "llvm/IR/DataLayout.h"
43 #include "llvm/IR/DerivedTypes.h"
54 #include "llvm/IR/IRBuilder.h"
22 Core
33 ExecutionEngine
44 InstCombine
5 JIT
65 MC
76 ScalarOpts
87 Support
0 #include "llvm/Analysis/Passes.h"
11 #include "llvm/ExecutionEngine/ExecutionEngine.h"
2 #include "llvm/ExecutionEngine/JIT.h"
32 #include "llvm/IR/DataLayout.h"
43 #include "llvm/IR/DerivedTypes.h"
54 #include "llvm/IR/IRBuilder.h"
22 Core
33 ExecutionEngine
44 InstCombine
5 JIT
65 MC
76 ScalarOpts
87 Support
0 #include "llvm/Analysis/Passes.h"
11 #include "llvm/ExecutionEngine/ExecutionEngine.h"
2 #include "llvm/ExecutionEngine/JIT.h"
32 #include "llvm/IR/DataLayout.h"
43 #include "llvm/IR/DerivedTypes.h"
54 #include "llvm/IR/IRBuilder.h"
22 Core
33 ExecutionEngine
44 InstCombine
5 JIT
65 MC
76 ScalarOpts
87 Support
0 #include "llvm/Analysis/Passes.h"
11 #include "llvm/ExecutionEngine/ExecutionEngine.h"
2 #include "llvm/ExecutionEngine/JIT.h"
32 #include "llvm/IR/DataLayout.h"
43 #include "llvm/IR/DerivedTypes.h"
54 #include "llvm/IR/IRBuilder.h"
11
22 #include "llvm/Analysis/Passes.h"
33 #include "llvm/ExecutionEngine/ExecutionEngine.h"
4 #include "llvm/ExecutionEngine/JIT.h"
54 #include "llvm/IR/DataLayout.h"
65 #include "llvm/IR/DerivedTypes.h"
76 #include "llvm/IR/IRBuilder.h"
896896 std::string ErrStr;
897897 ExecutionEngine *NewEngine = EngineBuilder(M)
898898 .setErrorStr(&ErrStr)
899 .setUseMCJIT(true)
900899 .setMCJITMemoryManager(new HelpingMemoryManager(this))
901900 .create();
902901 if (!NewEngine) {
0 #include "llvm/Analysis/Passes.h"
11 #include "llvm/ExecutionEngine/ExecutionEngine.h"
2 #include "llvm/ExecutionEngine/JIT.h"
32 #include "llvm/ExecutionEngine/MCJIT.h"
43 #include "llvm/ExecutionEngine/ObjectCache.h"
54 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
5049 DumpModulesOnExit("dump-modules",
5150 cl::desc("Dump IR from modules to stderr on shutdown"),
5251 cl::init(false));
53
54 cl::opt UseMCJIT(
55 "use-mcjit", cl::desc("Use the MCJIT execution engine"),
56 cl::init(true));
5752
5853 cl::opt EnableLazyCompilation(
5954 "enable-lazy-compilation", cl::desc("Enable lazy compilation when using the MCJIT engine"),
792787 };
793788
794789 //===----------------------------------------------------------------------===//
795 // Helper class for JIT execution engine
796 //===----------------------------------------------------------------------===//
797
798 class JITHelper : public BaseHelper {
799 public:
800 JITHelper(LLVMContext &Context) {
801 // Make the module, which holds all the code.
802 if (!InputIR.empty()) {
803 TheModule = parseInputIR(InputIR, Context);
804 } else {
805 TheModule = new Module("my cool jit", Context);
806 }
807
808 // Create the JIT. This takes ownership of the module.
809 std::string ErrStr;
810 TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
811 if (!TheExecutionEngine) {
812 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
813 exit(1);
814 }
815
816 TheFPM = new FunctionPassManager(TheModule);
817
818 // Set up the optimizer pipeline. Start with registering info about how the
819 // target lays out data structures.
820 TheFPM->add(new DataLayout(*TheExecutionEngine->getDataLayout()));
821 // Provide basic AliasAnalysis support for GVN.
822 TheFPM->add(createBasicAliasAnalysisPass());
823 // Promote allocas to registers.
824 TheFPM->add(createPromoteMemoryToRegisterPass());
825 // Do simple "peephole" optimizations and bit-twiddling optzns.
826 TheFPM->add(createInstructionCombiningPass());
827 // Reassociate expressions.
828 TheFPM->add(createReassociatePass());
829 // Eliminate Common SubExpressions.
830 TheFPM->add(createGVNPass());
831 // Simplify the control flow graph (deleting unreachable blocks, etc).
832 TheFPM->add(createCFGSimplificationPass());
833
834 TheFPM->doInitialization();
835 }
836
837 virtual ~JITHelper() {
838 if (TheFPM)
839 delete TheFPM;
840 if (TheExecutionEngine)
841 delete TheExecutionEngine;
842 }
843
844 virtual Function *getFunction(const std::string FnName) {
845 assert(TheModule);
846 return TheModule->getFunction(FnName);
847 }
848
849 virtual Module *getModuleForNewFunction() {
850 assert(TheModule);
851 return TheModule;
852 }
853
854 virtual void *getPointerToFunction(Function* F) {
855 assert(TheExecutionEngine);
856 return TheExecutionEngine->getPointerToFunction(F);
857 }
858
859 virtual void *getPointerToNamedFunction(const std::string &Name) {
860 return TheExecutionEngine->getPointerToNamedFunction(Name);
861 }
862
863 virtual void runFPM(Function &F) {
864 assert(TheFPM);
865 TheFPM->run(F);
866 }
867
868 virtual void closeCurrentModule() {
869 // This should never be called for JIT
870 assert(false);
871 }
872
873 virtual void dump() {
874 assert(TheModule);
875 TheModule->dump();
876 }
877
878 private:
879 Module *TheModule;
880 ExecutionEngine *TheExecutionEngine;
881 FunctionPassManager *TheFPM;
882 };
883
884 //===----------------------------------------------------------------------===//
885790 // MCJIT helper class
886791 //===----------------------------------------------------------------------===//
887792
1033938 std::string ErrStr;
1034939 ExecutionEngine *EE = EngineBuilder(M)
1035940 .setErrorStr(&ErrStr)
1036 .setUseMCJIT(true)
1037941 .setMCJITMemoryManager(new HelpingMemoryManager(this))
1038942 .create();
1039943 if (!EE) {
11931097 Value *OperandV = Operand->Codegen();
11941098 if (OperandV == 0) return 0;
11951099 Function *F;
1196 if (UseMCJIT)
1197 F = TheHelper->getFunction(MakeLegalFunctionName(std::string("unary")+Opcode));
1198 else
1199 F = TheHelper->getFunction(std::string("unary")+Opcode);
1100 F = TheHelper->getFunction(
1101 MakeLegalFunctionName(std::string("unary") + Opcode));
12001102 if (F == 0)
12011103 return ErrorV("Unknown unary operator");
12021104
12451147 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
12461148 // a call to it.
12471149 Function *F;
1248 if (UseMCJIT)
1249 F = TheHelper->getFunction(MakeLegalFunctionName(std::string("binary")+Op));
1250 else
1251 F = TheHelper->getFunction(std::string("binary")+Op);
1150 F = TheHelper->getFunction(MakeLegalFunctionName(std::string("binary")+Op));
12521151 assert(F && "binary operator not found!");
12531152
12541153 Value *Ops[] = { L, R };
14811380 Doubles, false);
14821381
14831382 std::string FnName;
1484 if (UseMCJIT)
1485 FnName = MakeLegalFunctionName(Name);
1486 else
1487 FnName = Name;
1383 FnName = MakeLegalFunctionName(Name);
14881384
14891385 Module* M = TheHelper->getModuleForNewFunction();
14901386 Function *F = Function::Create(FT, Function::ExternalLinkage, FnName, M);
15591455 // Validate the generated code, checking for consistency.
15601456 verifyFunction(*TheFunction);
15611457
1562 // Optimize the function.
1563 if (!UseMCJIT)
1564 TheHelper->runFPM(*TheFunction);
1565
15661458 return TheFunction;
15671459 }
15681460
15801472
15811473 static void HandleDefinition() {
15821474 if (FunctionAST *F = ParseDefinition()) {
1583 if (UseMCJIT && EnableLazyCompilation)
1475 if (EnableLazyCompilation)
15841476 TheHelper->closeCurrentModule();
15851477 Function *LF = F->Codegen();
15861478 if (LF && VerboseOutput) {
16701562
16711563 int main(int argc, char **argv) {
16721564 InitializeNativeTarget();
1673 if (UseMCJIT) {
1674 InitializeNativeTargetAsmPrinter();
1675 InitializeNativeTargetAsmParser();
1676 }
1565 InitializeNativeTargetAsmPrinter();
1566 InitializeNativeTargetAsmParser();
16771567 LLVMContext &Context = getGlobalContext();
16781568
16791569 cl::ParseCommandLineOptions(argc, argv,
16891579 BinopPrecedence['*'] = 40; // highest.
16901580
16911581 // Make the Helper, which holds all the code.
1692 if (UseMCJIT)
1693 TheHelper = new MCJITHelper(Context);
1694 else
1695 TheHelper = new JITHelper(Context);
1582 TheHelper = new MCJITHelper(Context);
16961583
16971584 // Prime the first token.
16981585 if (!SuppressPrompts)
777777 std::string ErrStr;
778778 ExecutionEngine *NewEngine = EngineBuilder(OpenModule)
779779 .setErrorStr(&ErrStr)
780 .setUseMCJIT(true)
781780 .setMCJITMemoryManager(new HelpingMemoryManager(this))
782781 .create();
783782 if (!NewEngine) {
11
22 #include "llvm/Analysis/Passes.h"
33 #include "llvm/ExecutionEngine/ExecutionEngine.h"
4 #include "llvm/ExecutionEngine/JIT.h"
54 #include "llvm/IR/DataLayout.h"
65 #include "llvm/IR/DerivedTypes.h"
76 #include "llvm/IR/IRBuilder.h"
807807 std::string ErrStr;
808808 ExecutionEngine *NewEngine = EngineBuilder(M)
809809 .setErrorStr(&ErrStr)
810 .setUseMCJIT(true)
811810 .setMCJITMemoryManager(new HelpingMemoryManager(this))
812811 .create();
813812 if (!NewEngine) {
11 Core
22 ExecutionEngine
33 Interpreter
4 JIT
54 Support
65 nativecodegen
76 )
1818
1919 #include "llvm/ExecutionEngine/GenericValue.h"
2020 #include "llvm/ExecutionEngine/Interpreter.h"
21 #include "llvm/ExecutionEngine/JIT.h"
2221 #include "llvm/IR/Constants.h"
2322 #include "llvm/IR/DerivedTypes.h"
2423 #include "llvm/IR/Instructions.h"
+0
-344
include/llvm/CodeGen/JITCodeEmitter.h less more
None //===-- llvm/CodeGen/JITCodeEmitter.h - Code emission ----------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines an abstract interface that is used by the machine code
10 // emission framework to output the code. This allows machine code emission to
11 // be separated from concerns such as resolution of call targets, and where the
12 // machine code will be written (memory or disk, f.e.).
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_CODEGEN_JITCODEEMITTER_H
17 #define LLVM_CODEGEN_JITCODEEMITTER_H
18
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/CodeGen/MachineCodeEmitter.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/MathExtras.h"
23 #include
24
25 namespace llvm {
26
27 class MachineBasicBlock;
28 class MachineConstantPool;
29 class MachineJumpTableInfo;
30 class MachineFunction;
31 class MachineModuleInfo;
32 class MachineRelocation;
33 class Value;
34 class GlobalValue;
35 class Function;
36
37 /// JITCodeEmitter - This class defines two sorts of methods: those for
38 /// emitting the actual bytes of machine code, and those for emitting auxiliary
39 /// structures, such as jump tables, relocations, etc.
40 ///
41 /// Emission of machine code is complicated by the fact that we don't (in
42 /// general) know the size of the machine code that we're about to emit before
43 /// we emit it. As such, we preallocate a certain amount of memory, and set the
44 /// BufferBegin/BufferEnd pointers to the start and end of the buffer. As we
45 /// emit machine instructions, we advance the CurBufferPtr to indicate the
46 /// location of the next byte to emit. In the case of a buffer overflow (we
47 /// need to emit more machine code than we have allocated space for), the
48 /// CurBufferPtr will saturate to BufferEnd and ignore stores. Once the entire
49 /// function has been emitted, the overflow condition is checked, and if it has
50 /// occurred, more memory is allocated, and we reemit the code into it.
51 ///
52 class JITCodeEmitter : public MachineCodeEmitter {
53 void anchor() override;
54 public:
55 virtual ~JITCodeEmitter() {}
56
57 /// startFunction - This callback is invoked when the specified function is
58 /// about to be code generated. This initializes the BufferBegin/End/Ptr
59 /// fields.
60 ///
61 void startFunction(MachineFunction &F) override = 0;
62
63 /// finishFunction - This callback is invoked when the specified function has
64 /// finished code generation. If a buffer overflow has occurred, this method
65 /// returns true (the callee is required to try again), otherwise it returns
66 /// false.
67 ///
68 bool finishFunction(MachineFunction &F) override = 0;
69
70 /// allocIndirectGV - Allocates and fills storage for an indirect
71 /// GlobalValue, and returns the address.
72 virtual void *allocIndirectGV(const GlobalValue *GV,
73 const uint8_t *Buffer, size_t Size,
74 unsigned Alignment) = 0;
75
76 /// emitByte - This callback is invoked when a byte needs to be written to the
77 /// output stream.
78 ///
79 void emitByte(uint8_t B) {
80 if (CurBufferPtr != BufferEnd)
81 *CurBufferPtr++ = B;
82 }
83
84 /// emitWordLE - This callback is invoked when a 32-bit word needs to be
85 /// written to the output stream in little-endian format.
86 ///
87 void emitWordLE(uint32_t W) {
88 if (4 <= BufferEnd-CurBufferPtr) {
89 *CurBufferPtr++ = (uint8_t)(W >> 0);
90 *CurBufferPtr++ = (uint8_t)(W >> 8);
91 *CurBufferPtr++ = (uint8_t)(W >> 16);
92 *CurBufferPtr++ = (uint8_t)(W >> 24);
93 } else {
94 CurBufferPtr = BufferEnd;
95 }
96 }
97
98 /// emitWordBE - This callback is invoked when a 32-bit word needs to be
99 /// written to the output stream in big-endian format.
100 ///
101 void emitWordBE(uint32_t W) {
102 if (4 <= BufferEnd-CurBufferPtr) {
103 *CurBufferPtr++ = (uint8_t)(W >> 24);
104 *CurBufferPtr++ = (uint8_t)(W >> 16);
105 *CurBufferPtr++ = (uint8_t)(W >> 8);
106 *CurBufferPtr++ = (uint8_t)(W >> 0);
107 } else {
108 CurBufferPtr = BufferEnd;
109 }
110 }
111
112 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
113 /// written to the output stream in little-endian format.
114 ///
115 void emitDWordLE(uint64_t W) {
116 if (8 <= BufferEnd-CurBufferPtr) {
117 *CurBufferPtr++ = (uint8_t)(W >> 0);
118 *CurBufferPtr++ = (uint8_t)(W >> 8);
119 *CurBufferPtr++ = (uint8_t)(W >> 16);
120 *CurBufferPtr++ = (uint8_t)(W >> 24);
121 *CurBufferPtr++ = (uint8_t)(W >> 32);
122 *CurBufferPtr++ = (uint8_t)(W >> 40);
123 *CurBufferPtr++ = (uint8_t)(W >> 48);
124 *CurBufferPtr++ = (uint8_t)(W >> 56);
125 } else {
126 CurBufferPtr = BufferEnd;
127 }
128 }
129
130 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
131 /// written to the output stream in big-endian format.
132 ///
133 void emitDWordBE(uint64_t W) {
134 if (8 <= BufferEnd-CurBufferPtr) {
135 *CurBufferPtr++ = (uint8_t)(W >> 56);
136 *CurBufferPtr++ = (uint8_t)(W >> 48);
137 *CurBufferPtr++ = (uint8_t)(W >> 40);
138 *CurBufferPtr++ = (uint8_t)(W >> 32);
139 *CurBufferPtr++ = (uint8_t)(W >> 24);
140 *CurBufferPtr++ = (uint8_t)(W >> 16);
141 *CurBufferPtr++ = (uint8_t)(W >> 8);
142 *CurBufferPtr++ = (uint8_t)(W >> 0);
143 } else {
144 CurBufferPtr = BufferEnd;
145 }
146 }
147
148 /// emitAlignment - Move the CurBufferPtr pointer up to the specified
149 /// alignment (saturated to BufferEnd of course).
150 void emitAlignment(unsigned Alignment) {
151 if (Alignment == 0) Alignment = 1;
152 uint8_t *NewPtr = (uint8_t*)RoundUpToAlignment((uintptr_t)CurBufferPtr,
153 Alignment);
154 CurBufferPtr = std::min(NewPtr, BufferEnd);
155 }
156
157 /// emitAlignmentWithFill - Similar to emitAlignment, except that the
158 /// extra bytes are filled with the provided byte.
159 void emitAlignmentWithFill(unsigned Alignment, uint8_t Fill) {
160 if (Alignment == 0) Alignment = 1;
161 uint8_t *NewPtr = (uint8_t*)RoundUpToAlignment((uintptr_t)CurBufferPtr,
162 Alignment);
163 // Fail if we don't have room.
164 if (NewPtr > BufferEnd) {
165 CurBufferPtr = BufferEnd;
166 return;
167 }
168 while (CurBufferPtr < NewPtr) {
169 *CurBufferPtr++ = Fill;
170 }
171 }
172
173 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
174 /// written to the output stream.
175 void emitULEB128Bytes(uint64_t Value, unsigned PadTo = 0) {
176 do {
177 uint8_t Byte = Value & 0x7f;
178 Value >>= 7;
179 if (Value || PadTo != 0) Byte |= 0x80;
180 emitByte(Byte);
181 } while (Value);
182
183 if (PadTo) {
184 do {
185 uint8_t Byte = (PadTo > 1) ? 0x80 : 0x0;
186 emitByte(Byte);
187 } while (--PadTo);
188 }
189 }
190
191 /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
192 /// written to the output stream.
193 void emitSLEB128Bytes(int64_t Value) {
194 int32_t Sign = Value >> (8 * sizeof(Value) - 1);
195 bool IsMore;
196
197 do {
198 uint8_t Byte = Value & 0x7f;
199 Value >>= 7;
200 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
201 if (IsMore) Byte |= 0x80;
202 emitByte(Byte);
203 } while (IsMore);
204 }
205
206 /// emitString - This callback is invoked when a String needs to be
207 /// written to the output stream.
208 void emitString(const std::string &String) {
209 for (size_t i = 0, N = String.size(); i < N; ++i) {
210 uint8_t C = String[i];
211 emitByte(C);
212 }
213 emitByte(0);
214 }
215
216 /// emitInt32 - Emit a int32 directive.
217 void emitInt32(uint32_t Value) {
218 if (4 <= BufferEnd-CurBufferPtr) {
219 *((uint32_t*)CurBufferPtr) = Value;
220 CurBufferPtr += 4;
221 } else {
222 CurBufferPtr = BufferEnd;
223 }
224 }
225
226 /// emitInt64 - Emit a int64 directive.
227 void emitInt64(uint64_t Value) {
228 if (8 <= BufferEnd-CurBufferPtr) {
229 *((uint64_t*)CurBufferPtr) = Value;
230 CurBufferPtr += 8;
231 } else {
232 CurBufferPtr = BufferEnd;
233 }
234 }
235
236 /// emitInt32At - Emit the Int32 Value in Addr.
237 void emitInt32At(uintptr_t *Addr, uintptr_t Value) {
238 if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
239 (*(uint32_t*)Addr) = (uint32_t)Value;
240 }
241
242 /// emitInt64At - Emit the Int64 Value in Addr.
243 void emitInt64At(uintptr_t *Addr, uintptr_t Value) {
244 if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
245 (*(uint64_t*)Addr) = (uint64_t)Value;
246 }
247
248
249 /// emitLabel - Emits a label
250 void emitLabel(MCSymbol *Label) override = 0;
251
252 /// allocateSpace - Allocate a block of space in the current output buffer,
253 /// returning null (and setting conditions to indicate buffer overflow) on
254 /// failure. Alignment is the alignment in bytes of the buffer desired.
255 void *allocateSpace(uintptr_t Size, unsigned Alignment) override {
256 emitAlignment(Alignment);
257 void *Result;
258
259 // Check for buffer overflow.
260 if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
261 CurBufferPtr = BufferEnd;
262 Result = nullptr;
263 } else {
264 // Allocate the space.
265 Result = CurBufferPtr;
266 CurBufferPtr += Size;
267 }
268
269 return Result;
270 }
271
272 /// allocateGlobal - Allocate memory for a global. Unlike allocateSpace,
273 /// this method does not allocate memory in the current output buffer,
274 /// because a global may live longer than the current function.
275 virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0;
276
277 /// StartMachineBasicBlock - This should be called by the target when a new
278 /// basic block is about to be emitted. This way the MCE knows where the
279 /// start of the block is, and can implement getMachineBasicBlockAddress.
280 void StartMachineBasicBlock(MachineBasicBlock *MBB) override = 0;
281
282 /// getCurrentPCValue - This returns the address that the next emitted byte
283 /// will be output to.
284 ///
285 uintptr_t getCurrentPCValue() const override {
286 return (uintptr_t)CurBufferPtr;
287 }
288
289 /// getCurrentPCOffset - Return the offset from the start of the emitted
290 /// buffer that we are currently writing to.
291 uintptr_t getCurrentPCOffset() const override {
292 return CurBufferPtr-BufferBegin;
293 }
294
295 /// earlyResolveAddresses - True if the code emitter can use symbol addresses
296 /// during code emission time. The JIT is capable of doing this because it
297 /// creates jump tables or constant pools in memory on the fly while the
298 /// object code emitters rely on a linker to have real addresses and should
299 /// use relocations instead.
300 bool earlyResolveAddresses() const override { return true; }
301
302 /// addRelocation - Whenever a relocatable address is needed, it should be
303 /// noted with this interface.
304 void addRelocation(const MachineRelocation &MR) override = 0;
305
306 /// FIXME: These should all be handled with relocations!
307
308 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
309 /// the constant pool that was last emitted with the emitConstantPool method.
310 ///
311 uintptr_t getConstantPoolEntryAddress(unsigned Index) const override = 0;
312
313 /// getJumpTableEntryAddress - Return the address of the jump table with index
314 /// 'Index' in the function that last called initJumpTableInfo.
315 ///
316 uintptr_t getJumpTableEntryAddress(unsigned Index) const override = 0;
317
318 /// getMachineBasicBlockAddress - Return the address of the specified
319 /// MachineBasicBlock, only usable after the label for the MBB has been
320 /// emitted.
321 ///
322 uintptr_t
323 getMachineBasicBlockAddress(MachineBasicBlock *MBB) const override = 0;
324
325 /// getLabelAddress - Return the address of the specified Label, only usable
326 /// after the Label has been emitted.
327 ///
328 uintptr_t getLabelAddress(MCSymbol *Label) const override = 0;
329
330 /// Specifies the MachineModuleInfo object. This is used for exception handling
331 /// purposes.
332 void setModuleInfo(MachineModuleInfo* Info) override = 0;
333
334 /// getLabelLocations - Return the label locations map of the label IDs to
335 /// their address.
336 virtual DenseMap *getLabelLocations() {
337 return nullptr;
338 }
339 };
340
341 } // End llvm namespace
342
343 #endif
140140 // To avoid having libexecutionengine depend on the JIT and interpreter
141141 // libraries, the execution engine implementations set these functions to ctor
142142 // pointers at startup time if they are linked in.
143 static ExecutionEngine *(*JITCtor)(
144 Module *M,
145 std::string *ErrorStr,
146 JITMemoryManager *JMM,
147 bool GVsWithCode,
148 TargetMachine *TM);
149143 static ExecutionEngine *(*MCJITCtor)(
150144 Module *M,
151145 std::string *ErrorStr,
334328 /// getFunctionAddress instead.
335329 virtual void *getPointerToFunction(Function *F) = 0;
336330
337 /// getPointerToBasicBlock - The different EE's represent basic blocks in
338 /// different ways. Return the representation for a blockaddress of the
339 /// specified block.
340 ///
341 /// This function will not be implemented for the MCJIT execution engine.
342 virtual void *getPointerToBasicBlock(BasicBlock *BB) = 0;
343
344331 /// getPointerToFunctionOrStub - If the specified function has been
345332 /// code-gen'd, return a pointer to the function. If not, compile it, or use
346333 /// a stub to implement lazy compilation if available. See
387374 Type *Ty);
388375
389376 void InitializeMemory(const Constant *Init, void *Addr);
390
391 /// recompileAndRelinkFunction - This method is used to force a function which
392 /// has already been compiled to be compiled again, possibly after it has been
393 /// modified. Then the entry to the old copy is overwritten with a branch to
394 /// the new copy. If there was no old copy, this acts just like
395 /// VM::getPointerToFunction().
396 virtual void *recompileAndRelinkFunction(Function *F) = 0;
397
398 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
399 /// corresponding to the machine code emitted to execute this function, useful
400 /// for garbage-collecting generated code.
401 virtual void freeMachineCodeForFunction(Function *F) = 0;
402377
403378 /// getOrEmitGlobalVariable - Return the address of the specified global
404379 /// variable, possibly emitting it to memory if needed. This is used by the
536511 CodeGenOpt::Level OptLevel;
537512 RTDyldMemoryManager *MCJMM;
538513 JITMemoryManager *JMM;
539 bool AllocateGVsWithCode;
540514 TargetOptions Options;
541515 Reloc::Model RelocModel;
542516 CodeModel::Model CMModel;
543517 std::string MArch;
544518 std::string MCPU;
545519 SmallVector MAttrs;
546 bool UseMCJIT;
547520 bool VerifyModules;
548521
549522 /// InitEngine - Does the common initialization of default options.
625598 return *this;
626599 }
627600
628 /// setAllocateGVsWithCode - Sets whether global values should be allocated
629 /// into the same buffer as code. For most applications this should be set
630 /// to false. Allocating globals with code breaks freeMachineCodeForFunction
631 /// and is probably unsafe and bad for performance. However, we have clients
632 /// who depend on this behavior, so we must support it. This option defaults
633 /// to false so that users of the new API can safely use the new memory
634 /// manager and free machine code.
635 EngineBuilder &setAllocateGVsWithCode(bool a) {
636 AllocateGVsWithCode = a;
637 return *this;
638 }
639
640601 /// setMArch - Override the architecture set by the Module's triple.
641602 EngineBuilder &setMArch(StringRef march) {
642603 MArch.assign(march.begin(), march.end());
646607 /// setMCPU - Target a specific cpu type.
647608 EngineBuilder &setMCPU(StringRef mcpu) {
648609 MCPU.assign(mcpu.begin(), mcpu.end());
649 return *this;
650 }
651
652 /// setUseMCJIT - Set whether the MC-JIT implementation should be used
653 /// (experimental).
654 EngineBuilder &setUseMCJIT(bool Value) {
655 UseMCJIT = Value;
656610 return *this;
657611 }
658612
+0
-38
include/llvm/ExecutionEngine/JIT.h less more
None //===-- JIT.h - Abstract Execution Engine Interface -------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file forces the JIT to link in on certain operating systems.
10 // (Windows).
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_EXECUTIONENGINE_JIT_H
15 #define LLVM_EXECUTIONENGINE_JIT_H
16
17 #include "llvm/ExecutionEngine/ExecutionEngine.h"
18 #include
19
20 extern "C" void LLVMLinkInJIT();
21
22 namespace {
23 struct ForceJITLinking {
24 ForceJITLinking() {
25 // We must reference JIT in such a way that compilers will not
26 // delete it all as dead code, even with whole program optimization,
27 // yet is effectively a NO-OP. As the compiler isn't smart enough
28 // to know that getenv() never returns -1, this will do the job.
29 if (std::getenv("bar") != (char*) -1)
30 return;
31
32 LLVMLinkInJIT();
33 }
34 } ForceJITLinking;
35 }
36
37 #endif
+0
-136
include/llvm/Target/TargetJITInfo.h less more
None //===- Target/TargetJITInfo.h - Target Information for JIT ------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file exposes an abstract interface used by the Just-In-Time code
10 // generator to perform target-specific activities, such as emitting stubs. If
11 // a TargetMachine supports JIT code generation, it should provide one of these
12 // objects through the getJITInfo() method.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_TARGET_TARGETJITINFO_H
17 #define LLVM_TARGET_TARGETJITINFO_H
18
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include
22
23 namespace llvm {
24 class Function;
25 class GlobalValue;
26 class JITCodeEmitter;
27 class MachineRelocation;
28
29 /// TargetJITInfo - Target specific information required by the Just-In-Time
30 /// code generator.
31 class TargetJITInfo {
32 virtual void anchor();
33 public:
34 virtual ~TargetJITInfo() {}
35
36 /// replaceMachineCodeForFunction - Make it so that calling the function
37 /// whose machine code is at OLD turns into a call to NEW, perhaps by
38 /// overwriting OLD with a branch to NEW. This is used for self-modifying
39 /// code.
40 ///
41 virtual void replaceMachineCodeForFunction(void *Old, void *New) = 0;
42
43 /// emitGlobalValueIndirectSym - Use the specified JITCodeEmitter object
44 /// to emit an indirect symbol which contains the address of the specified
45 /// ptr.
46 virtual void *emitGlobalValueIndirectSym(const GlobalValue* GV, void *ptr,
47 JITCodeEmitter &JCE) {
48 llvm_unreachable("This target doesn't implement "
49 "emitGlobalValueIndirectSym!");
50 }
51
52 /// Records the required size and alignment for a call stub in bytes.
53 struct StubLayout {
54 size_t Size;
55 size_t Alignment;
56 };
57 /// Returns the maximum size and alignment for a call stub on this target.
58 virtual StubLayout getStubLayout() {
59 llvm_unreachable("This target doesn't implement getStubLayout!");
60 }
61
62 /// emitFunctionStub - Use the specified JITCodeEmitter object to emit a
63 /// small native function that simply calls the function at the specified
64 /// address. The JITCodeEmitter must already have storage allocated for the
65 /// stub. Return the address of the resultant function, which may have been
66 /// aligned from the address the JCE was set up to emit at.
67 virtual void *emitFunctionStub(const Function* F, void *Target,
68 JITCodeEmitter &JCE) {
69 llvm_unreachable("This target doesn't implement emitFunctionStub!");
70 }
71
72 /// getPICJumpTableEntry - Returns the value of the jumptable entry for the
73 /// specific basic block.
74 virtual uintptr_t getPICJumpTableEntry(uintptr_t BB, uintptr_t JTBase) {
75 llvm_unreachable("This target doesn't implement getPICJumpTableEntry!");
76 }
77
78 /// LazyResolverFn - This typedef is used to represent the function that
79 /// unresolved call points should invoke. This is a target specific
80 /// function that knows how to walk the stack and find out which stub the
81 /// call is coming from.
82 typedef void (*LazyResolverFn)();
83
84 /// JITCompilerFn - This typedef is used to represent the JIT function that
85 /// lazily compiles the function corresponding to a stub. The JIT keeps
86 /// track of the mapping between stubs and LLVM Functions, the target
87 /// provides the ability to figure out the address of a stub that is called
88 /// by the LazyResolverFn.
89 typedef void* (*JITCompilerFn)(void *);
90
91 /// getLazyResolverFunction - This method is used to initialize the JIT,
92 /// giving the target the function that should be used to compile a
93 /// function, and giving the JIT the target function used to do the lazy
94 /// resolving.
95 virtual LazyResolverFn getLazyResolverFunction(JITCompilerFn) {
96 llvm_unreachable("Not implemented for this target!");
97 }
98
99 /// relocate - Before the JIT can run a block of code that has been emitted,
100 /// it must rewrite the code to contain the actual addresses of any
101 /// referenced global symbols.
102 virtual void relocate(void *Function, MachineRelocation *MR,
103 unsigned NumRelocs, unsigned char* GOTBase) {
104 assert(NumRelocs == 0 && "This target does not have relocations!");
105 }
106
107 /// allocateThreadLocalMemory - Each target has its own way of
108 /// handling thread local variables. This method returns a value only
109 /// meaningful to the target.
110 virtual char* allocateThreadLocalMemory(size_t size) {
111 llvm_unreachable("This target does not implement thread local storage!");
112 }
113
114 /// needsGOT - Allows a target to specify that it would like the
115 /// JIT to manage a GOT for it.
116 bool needsGOT() const { return useGOT; }
117
118 /// hasCustomConstantPool - Allows a target to specify that constant
119 /// pool address resolution is handled by the target.
120 virtual bool hasCustomConstantPool() const { return false; }
121
122 /// hasCustomJumpTables - Allows a target to specify that jumptables
123 /// are emitted by the target.
124 virtual bool hasCustomJumpTables() const { return false; }
125
126 /// allocateSeparateGVMemory - If true, globals should be placed in
127 /// separately allocated heap memory rather than in the same
128 /// code memory allocated by JITCodeEmitter.
129 virtual bool allocateSeparateGVMemory() const { return false; }
130 protected:
131 bool useGOT;
132 };
133 } // End llvm namespace
134
135 #endif
827827 return UseUnderscoreLongJmp;
828828 }
829829
830 /// Return whether the target can generate code for jump tables.
831 bool supportJumpTables() const {
832 return SupportJumpTables;
833 }
834
835830 /// Return integer threshold on number of blocks to use jump tables rather
836831 /// than if sequence.
837832 int getMinimumJumpTableEntries() const {
998993 /// llvm.longjmp or the version without _. Defaults to false.
999994 void setUseUnderscoreLongJmp(bool Val) {
1000995 UseUnderscoreLongJmp = Val;
1001 }
1002
1003 /// Indicate whether the target can generate code for jump tables.
1004 void setSupportJumpTables(bool Val) {
1005 SupportJumpTables = Val;
1006996 }
1007997
1008998 /// Indicate the number of blocks to generate jump tables rather than if
15071497 ///
15081498 /// Defaults to false.
15091499 bool UseUnderscoreLongJmp;
1510
1511 /// Whether the target can generate code for jumptables. If it's not true,
1512 /// then each jumptable must be lowered into if-then-else's.
1513 bool SupportJumpTables;
15141500
15151501 /// Number of blocks threshold to use jump tables.
15161502 int MinimumJumpTableEntries;
2323 namespace llvm {
2424
2525 class InstrItineraryData;
26 class JITCodeEmitter;
2726 class GlobalValue;
2827 class Mangler;
2928 class MCAsmInfo;
3534 class TargetLibraryInfo;
3635 class TargetFrameLowering;
3736 class TargetIntrinsicInfo;
38 class TargetJITInfo;
3937 class TargetLowering;
4038 class TargetPassConfig;
4139 class TargetRegisterInfo;
10098 virtual const TargetSubtargetInfo *getSubtargetImpl() const {
10199 return nullptr;
102100 }
103 TargetSubtargetInfo *getSubtargetImpl() {
104 const TargetMachine *TM = this;
105 return const_cast(TM->getSubtargetImpl());
106 }
107101
108102 /// getSubtarget - This method returns a pointer to the specified type of
109103 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
200194 return true;
201195 }
202196
203 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
204 /// get machine code emitted. This uses a JITCodeEmitter object to handle
205 /// actually outputting the machine code and resolving things like the address
206 /// of functions. This method returns true if machine code emission is
207 /// not supported.
208 ///
209 virtual bool addPassesToEmitMachineCode(PassManagerBase &,
210 JITCodeEmitter &,
211 bool /*DisableVerify*/ = true) {
212 return true;
213 }
214
215197 /// addPassesToEmitMC - Add passes to the specified pass manager to get
216198 /// machine code emitted with the MCJIT. This method returns true if machine
217199 /// code is not supported. It fills the MCContext Ctx pointer which can be
258240 AnalysisID StartAfter = nullptr,
259241 AnalysisID StopAfter = nullptr) override;
260242
261 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
262 /// get machine code emitted. This uses a JITCodeEmitter object to handle
263 /// actually outputting the machine code and resolving things like the address
264 /// of functions. This method returns true if machine code emission is
265 /// not supported.
266 ///
267 bool addPassesToEmitMachineCode(PassManagerBase &PM, JITCodeEmitter &MCE,
268 bool DisableVerify = true) override;
269
270243 /// addPassesToEmitMC - Add passes to the specified pass manager to get
271244 /// machine code emitted with the MCJIT. This method returns true if machine
272245 /// code is not supported. It fills the MCContext Ctx pointer which can be
274247 ///
275248 bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
276249 raw_ostream &OS, bool DisableVerify = true) override;
277
278 /// addCodeEmitter - This pass should be overridden by the target to add a
279 /// code emitter, if supported. If this is not supported, 'true' should be
280 /// returned.
281 virtual bool addCodeEmitter(PassManagerBase &,
282 JITCodeEmitter &) {
283 return true;
284 }
285250 };
286251
287252 } // End llvm namespace
2525 class SUnit;
2626 class TargetFrameLowering;
2727 class TargetInstrInfo;
28 class TargetJITInfo;
2928 class TargetLowering;
3029 class TargetRegisterClass;
3130 class TargetRegisterInfo;
7776 /// details of graph coloring register allocation removed from it.
7877 ///
7978 virtual const TargetRegisterInfo *getRegisterInfo() const { return nullptr; }
80
81 /// getJITInfo - If this target supports a JIT, return information for it,
82 /// otherwise return null.
83 ///
84 virtual TargetJITInfo *getJITInfo() { return nullptr; }
8579
8680 /// getInstrItineraryData - Returns instruction itinerary data for the target
8781 /// or specific subtarget.
187187
188188 bool BasicTTI::shouldBuildLookupTables() const {
189189 const TargetLoweringBase *TLI = getTLI();
190 return TLI->supportJumpTables() &&
191 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
192 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
190 return TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
191 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other);
193192 }
194193
195194 bool BasicTTI::haveFastSqrt(Type *Ty) const {
2626 InlineSpiller.cpp
2727 InterferenceCache.cpp
2828 IntrinsicLowering.cpp
29 JITCodeEmitter.cpp
3029 JumpInstrTables.cpp
3130 LLVMTargetMachine.cpp
3231 LatencyPriorityQueue.cpp
+0
-14
lib/CodeGen/JITCodeEmitter.cpp less more
None //===-- llvm/CodeGen/JITCodeEmitter.cpp - Code emission --------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/CodeGen/JITCodeEmitter.h"
10
11 using namespace llvm;
12
13 void JITCodeEmitter::anchor() { }
225225 return false;
226226 }
227227
228 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
229 /// get machine code emitted. This uses a JITCodeEmitter object to handle
230 /// actually outputting the machine code and resolving things like the address
231 /// of functions. This method should return true if machine code emission is
232 /// not supported.
233 ///
234 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
235 JITCodeEmitter &JCE,
236 bool DisableVerify) {
237 // Add common CodeGen passes.
238 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, nullptr,
239 nullptr);
240 if (!Context)
241 return true;
242
243 addCodeEmitter(PM, JCE);
244
245 return false; // success!
246 }
247
248228 /// addPassesToEmitMC - Add passes to the specified pass manager to get
249229 /// machine code emitted with the MCJIT. This method returns true if machine
250230 /// code is not supported. It fills the MCContext Ctx pointer which can be
22272227 }
22282228
22292229 static inline bool areJTsAllowed(const TargetLowering &TLI) {
2230 return TLI.supportJumpTables() &&
2231 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
2232 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
2230 return TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
2231 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other);
22332232 }
22342233
22352234 static APInt ComputeRange(const APInt &First, const APInt &Last) {
718718 PrefLoopAlignment = 0;
719719 MinStackArgumentAlignment = 1;
720720 InsertFencesForAtomic = false;
721 SupportJumpTables = true;
722721 MinimumJumpTableEntries = 4;
723722
724723 InitLibcallNames(LibcallRoutineNames, Triple(TM.getTargetTriple()));
77 )
88
99 add_subdirectory(Interpreter)
10 add_subdirectory(JIT)
1110 add_subdirectory(MCJIT)
1211 add_subdirectory(RuntimeDyld)
1312
4747 void ObjectBuffer::anchor() {}
4848 void ObjectBufferStream::anchor() {}
4949
50 ExecutionEngine *(*ExecutionEngine::JITCtor)(
51 Module *M,
52 std::string *ErrorStr,
53 JITMemoryManager *JMM,
54 bool GVsWithCode,
55 TargetMachine *TM) = nullptr;
5650 ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
5751 Module *M,
5852 std::string *ErrorStr,
416410 MCJMM = nullptr;
417411 JMM = nullptr;
418412 Options = TargetOptions();
419 AllocateGVsWithCode = false;
420413 RelocModel = Reloc::Default;
421414 CMModel = CodeModel::JITDefault;
422 UseMCJIT = false;
423415
424416 // IR module verification is enabled by default in debug builds, and disabled
425417 // by default in release builds.
452444 return nullptr;
453445 }
454446 }
455
456 if (MCJMM && ! UseMCJIT) {
457 if (ErrorStr)
458 *ErrorStr =
459 "Cannot create a legacy JIT with a runtime dyld memory "
460 "manager.";
461 return nullptr;
462 }
463447
464448 // Unless the interpreter was explicitly selected or the JIT is not linked,
465449 // try making a JIT.
472456 }
473457
474458 ExecutionEngine *EE = nullptr;
475 if (UseMCJIT && ExecutionEngine::MCJITCtor)
459 if (ExecutionEngine::MCJITCtor)
476460 EE = ExecutionEngine::MCJITCtor(M, ErrorStr, MCJMM ? MCJMM : JMM,
477461 TheTM.release());
478 else if (ExecutionEngine::JITCtor)
479 EE = ExecutionEngine::JITCtor(M, ErrorStr, JMM,
480 AllocateGVsWithCode, TheTM.release());
481462
482463 if (EE) {
483464 EE->setVerifyModules(VerifyModules);
495476 return nullptr;
496477 }
497478
498 if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::JITCtor &&
499 !ExecutionEngine::MCJITCtor) {
479 if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::MCJITCtor) {
500480 if (ErrorStr)
501481 *ErrorStr = "JIT has not been linked in.";
502482 }
842822 Result = PTOGV(getPointerToFunctionOrStub(const_cast(F)));
843823 else if (const GlobalVariable *GV = dyn_cast(C))
844824 Result = PTOGV(getOrEmitGlobalVariable(const_cast(GV)));
845 else if (const BlockAddress *BA = dyn_cast(C))
846 Result = PTOGV(getPointerToBasicBlock(const_cast(
847 BA->getBasicBlock())));
848825 else
849826 llvm_unreachable("Unknown constant pointer type!");
850827 break;
191191 EngineBuilder builder(unwrap(M));
192192 builder.setEngineKind(EngineKind::JIT)
193193 .setErrorStr(&Error)
194 .setUseMCJIT(true)
195194 .setOptLevel((CodeGenOpt::Level)options.OptLevel)
196195 .setCodeModel(unwrap(options.CodeModel))
197196 .setTargetOptions(targetOptions);
274273 }
275274
276275 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
277 unwrap(EE)->freeMachineCodeForFunction(unwrap(F));
278276 }
279277
280278 void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
313311
314312 void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE,
315313 LLVMValueRef Fn) {
316 return unwrap(EE)->recompileAndRelinkFunction(unwrap(Fn));
314 return nullptr;
317315 }
318316
319317 LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
119119 // FIXME: not implemented.
120120 return nullptr;
121121 }
122
123 /// recompileAndRelinkFunction - For the interpreter, functions are always
124 /// up-to-date.
125 ///
126 void *recompileAndRelinkFunction(Function *F) override {
127 return getPointerToFunction(F);
128 }
129
130 /// freeMachineCodeForFunction - The interpreter does not generate any code.
131 ///
132 void freeMachineCodeForFunction(Function *F) override { }
133122
134123 // Methods used to execute code:
135124 // Place a call on the stack
212201 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
213202
214203 void *getPointerToFunction(Function *F) override { return (void*)F; }
215 void *getPointerToBasicBlock(BasicBlock *BB) override { return (void*)BB; }
216204
217205 void initializeExecutionEngine() { }
218206 void initializeExternalFunctions();
+0
-8
lib/ExecutionEngine/JIT/CMakeLists.txt less more
None # TODO: Support other architectures. See Makefile.
1 add_definitions(-DENABLE_X86_JIT)
2
3 add_llvm_library(LLVMJIT
4 JIT.cpp
5 JITEmitter.cpp
6 JITMemoryManager.cpp
7 )
+0
-696
lib/ExecutionEngine/JIT/JIT.cpp less more
None //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This tool implements a just-in-time compiler for LLVM, allowing direct
10 // execution of LLVM bitcode in an efficient manner.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "JIT.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/CodeGen/JITCodeEmitter.h"
17 #include "llvm/CodeGen/MachineCodeInfo.h"
18 #include "llvm/Config/config.h"
19 #include "llvm/ExecutionEngine/GenericValue.h"
20 #include "llvm/ExecutionEngine/JITEventListener.h"
21 #include "llvm/ExecutionEngine/JITMemoryManager.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/Support/Dwarf.h"
30 #include "llvm/Support/DynamicLibrary.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/ManagedStatic.h"
33 #include "llvm/Support/MutexGuard.h"
34 #include "llvm/Target/TargetJITInfo.h"
35 #include "llvm/Target/TargetMachine.h"
36 #include "llvm/Target/TargetSubtargetInfo.h"
37
38 using namespace llvm;
39
40 #ifdef __APPLE__
41 // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
42 // of atexit). It passes the address of linker generated symbol __dso_handle
43 // to the function.
44 // This configuration change happened at version 5330.
45 # include
46 # if defined(MAC_OS_X_VERSION_10_4) && \
47 ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
48 (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
49 __APPLE_CC__ >= 5330))
50 # ifndef HAVE___DSO_HANDLE
51 # define HAVE___DSO_HANDLE 1
52 # endif
53 # endif
54 #endif
55
56 #if HAVE___DSO_HANDLE
57 extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
58 #endif
59
60 namespace {
61
62 static struct RegisterJIT {
63 RegisterJIT() { JIT::Register(); }
64 } JITRegistrator;
65
66 }
67
68 extern "C" void LLVMLinkInJIT() {
69 }
70
71 /// createJIT - This is the factory method for creating a JIT for the current
72 /// machine, it does not fall back to the interpreter. This takes ownership
73 /// of the module.
74 ExecutionEngine *JIT::createJIT(Module *M,
75 std::string *ErrorStr,
76 JITMemoryManager *JMM,
77 bool GVsWithCode,
78 TargetMachine *TM) {
79 // Try to register the program as a source of symbols to resolve against.
80 //
81 // FIXME: Don't do this here.
82 sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
83
84 // If the target supports JIT code generation, create the JIT.
85 if (TargetJITInfo *TJ = TM->getSubtargetImpl()->getJITInfo()) {
86 return new JIT(M, *TM, *TJ, JMM, GVsWithCode);
87 } else {
88 if (ErrorStr)
89 *ErrorStr = "target does not support JIT code generation";
90 return nullptr;
91 }
92 }
93
94 namespace {
95 /// This class supports the global getPointerToNamedFunction(), which allows
96 /// bugpoint or gdb users to search for a function by name without any context.
97 class JitPool {
98 SmallPtrSet JITs; // Optimize for process containing just 1 JIT.
99 mutable sys::Mutex Lock;
100 public:
101 void Add(JIT *jit) {
102 MutexGuard guard(Lock);
103 JITs.insert(jit);
104 }
105 void Remove(JIT *jit) {
106 MutexGuard guard(Lock);
107 JITs.erase(jit);
108 }
109 void *getPointerToNamedFunction(const char *Name) const {
110 MutexGuard guard(Lock);
111 assert(JITs.size() != 0 && "No Jit registered");
112 //search function in every instance of JIT
113 for (SmallPtrSet::const_iterator Jit = JITs.begin(),
114 end = JITs.end();
115 Jit != end; ++Jit) {
116 if (Function *F = (*Jit)->FindFunctionNamed(Name))
117 return (*Jit)->getPointerToFunction(F);
118 }
119 // The function is not available : fallback on the first created (will
120 // search in symbol of the current program/library)
121 return (*JITs.begin())->getPointerToNamedFunction(Name);
122 }
123 };
124 ManagedStatic AllJits;
125 }
126 extern "C" {
127 // getPointerToNamedFunction - This function is used as a global wrapper to
128 // JIT::getPointerToNamedFunction for the purpose of resolving symbols when
129 // bugpoint is debugging the JIT. In that scenario, we are loading an .so and
130 // need to resolve function(s) that are being mis-codegenerated, so we need to
131 // resolve their addresses at runtime, and this is the way to do it.
132 void *getPointerToNamedFunction(const char *Name) {
133 return AllJits->getPointerToNamedFunction(Name);
134 }
135 }
136
137 JIT::JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
138 JITMemoryManager *jmm, bool GVsWithCode)
139 : ExecutionEngine(M), TM(tm), TJI(tji),
140 JMM(jmm ? jmm : JITMemoryManager::CreateDefaultMemManager()),
141 AllocateGVsWithCode(GVsWithCode), isAlreadyCodeGenerating(false) {
142 setDataLayout(TM.getSubtargetImpl()->getDataLayout());
143
144 jitstate = new JITState(M);
145
146 // Initialize JCE
147 JCE = createEmitter(*this, JMM, TM);
148
149 // Register in global list of all JITs.
150 AllJits->Add(this);
151
152 // Add target data
153 MutexGuard locked(lock);
154 FunctionPassManager &PM = jitstate->getPM();
155 M->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
156 PM.add(new DataLayoutPass(M));
157
158 // Turn the machine code intermediate representation into bytes in memory that
159 // may be executed.
160 if (TM.addPassesToEmitMachineCode(PM, *JCE, !getVerifyModules())) {
161 report_fatal_error("Target does not support machine code emission!");
162 }
163
164 // Initialize passes.
165 PM.doInitialization();
166 }
167
168 JIT::~JIT() {
169 // Cleanup.
170 AllJits->Remove(this);
171 delete jitstate;
172 delete JCE;
173 // JMM is a ownership of JCE, so we no need delete JMM here.
174 delete &TM;
175 }
176
177 /// addModule - Add a new Module to the JIT. If we previously removed the last
178 /// Module, we need re-initialize jitstate with a valid Module.
179 void JIT::addModule(Module *M) {
180 MutexGuard locked(lock);
181
182 if (Modules.empty()) {
183 assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
184
185 jitstate = new JITState(M);
186
187 FunctionPassManager &PM = jitstate->getPM();
188 M->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
189 PM.add(new DataLayoutPass(M));
190
191 // Turn the machine code intermediate representation into bytes in memory
192 // that may be executed.
193 if (TM.addPassesToEmitMachineCode(PM, *JCE, !getVerifyModules())) {
194 report_fatal_error("Target does not support machine code emission!");
195 }
196
197 // Initialize passes.
198 PM.doInitialization();
199 }
200
201 ExecutionEngine::addModule(M);
202 }
203
204 /// removeModule - If we are removing the last Module, invalidate the jitstate
205 /// since the PassManager it contains references a released Module.
206 bool JIT::removeModule(Module *M) {
207 bool result = ExecutionEngine::removeModule(M);
208
209 MutexGuard locked(lock);
210
211 if (jitstate && jitstate->getModule() == M) {
212 delete jitstate;
213 jitstate = nullptr;
214 }
215
216 if (!jitstate && !Modules.empty()) {
217 jitstate = new JITState(Modules[0]);
218
219 FunctionPassManager &PM = jitstate->getPM();
220 M->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
221 PM.add(new DataLayoutPass(M));
222
223 // Turn the machine code intermediate representation into bytes in memory
224 // that may be executed.
225 if (TM.addPassesToEmitMachineCode(PM, *JCE, !getVerifyModules())) {
226 report_fatal_error("Target does not support machine code emission!");
227 }
228
229 // Initialize passes.
230 PM.doInitialization();
231 }
232 return result;
233 }
234
235 /// run - Start execution with the specified function and arguments.
236 ///
237 GenericValue JIT::runFunction(Function *F,
238 const std::vector &ArgValues) {
239 assert(F && "Function *F was null at entry to run()");
240
241 void *FPtr = getPointerToFunction(F);
242 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
243 FunctionType *FTy = F->getFunctionType();
244 Type *RetTy = FTy->getReturnType();
245
246 assert((FTy->getNumParams() == ArgValues.size() ||
247 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
248 "Wrong number of arguments passed into function!");
249 assert(FTy->getNumParams() == ArgValues.size() &&
250 "This doesn't support passing arguments through varargs (yet)!");
251
252 // Handle some common cases first. These cases correspond to common `main'
253 // prototypes.
254 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
255 switch (ArgValues.size()) {
256 case 3:
257 if (FTy->getParamType(0)->isIntegerTy(32) &&
258 FTy->getParamType(1)->isPointerTy() &&
259 FTy->getParamType(2)->isPointerTy()) {
260 int (*PF)(int, char **, const char **) =
261 (int(*)(int, char **, const char **))(intptr_t)FPtr;
262
263 // Call the function.
264 GenericValue rv;
265 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
266 (char **)GVTOP(ArgValues[1]),
267 (const char **)GVTOP(ArgValues[2])));
268 return rv;
269 }
270 break;
271 case 2:
272 if (FTy->getParamType(0)->isIntegerTy(32) &&
273 FTy->getParamType(1)->isPointerTy()) {
274 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
275
276 // Call the function.
277 GenericValue rv;
278 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
279 (char **)GVTOP(ArgValues[1])));
280 return rv;
281 }
282 break;
283 case 1:
284 if (FTy->getParamType(0)->isIntegerTy(32)) {
285 GenericValue rv;
286 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
287 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
288 return rv;
289 }
290 if (FTy->getParamType(0)->isPointerTy()) {
291 GenericValue rv;
292 int (*PF)(char *) = (int(*)(char *))(intptr_t)FPtr;
293 rv.IntVal = APInt(32, PF((char*)GVTOP(ArgValues[0])));
294 return rv;
295 }
296 break;
297 }
298 }
299
300 // Handle cases where no arguments are passed first.
301 if (ArgValues.empty()) {
302 GenericValue rv;
303 switch (RetTy->getTypeID()) {
304 default: llvm_unreachable("Unknown return type for function call!");
305 case Type::IntegerTyID: {
306 unsigned BitWidth = cast(RetTy)->getBitWidth();
307 if (BitWidth == 1)
308 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
309 else if (BitWidth <= 8)
310 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
311 else if (BitWidth <= 16)
312 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
313 else if (BitWidth <= 32)
314 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
315 else if (BitWidth <= 64)
316 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
317 else
318 llvm_unreachable("Integer types > 64 bits not supported");
319 return rv;
320 }
321 case Type::VoidTyID:
322 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
323 return rv;
324 case Type::FloatTyID:
325 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
326 return rv;
327 case Type::DoubleTyID:
328 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
329 return rv;
330 case Type::X86_FP80TyID:
331 case Type::FP128TyID:
332 case Type::PPC_FP128TyID:
333 llvm_unreachable("long double not supported yet");
334 case Type::PointerTyID:
335 return PTOGV(((void*(*)())(intptr_t)FPtr)());
336 }
337 }
338
339 // Okay, this is not one of our quick and easy cases. Because we don't have a
340 // full FFI, we have to codegen a nullary stub function that just calls the
341 // function we are interested in, passing in constants for all of the
342 // arguments. Make this function and return.
343
344 // First, create the function.
345 FunctionType *STy=FunctionType::get(RetTy, false);
346 Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
347 F->getParent());
348
349 // Insert a basic block.
350 BasicBlock *StubBB = BasicBlock::Create(F->getContext(), "", Stub);
351
352 // Convert all of the GenericValue arguments over to constants. Note that we
353 // currently don't support varargs.
354 SmallVector Args;
355 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
356 Constant *C = nullptr;
357 Type *ArgTy = FTy->getParamType(i);
358 const GenericValue &AV = ArgValues[i];
359 switch (ArgTy->getTypeID()) {
360 default: llvm_unreachable("Unknown argument type for function call!");
361 case Type::IntegerTyID:
362 C = ConstantInt::get(F->getContext(), AV.IntVal);
363 break;
364 case Type::FloatTyID:
365 C = ConstantFP::get(F->getContext(), APFloat(AV.FloatVal));
366 break;
367 case Type::DoubleTyID:
368 C = ConstantFP::get(F->getContext(), APFloat(AV.DoubleVal));
369 break;
370 case Type::PPC_FP128TyID:
371 case Type::X86_FP80TyID:
372 case Type::FP128TyID:
373 C = ConstantFP::get(F->getContext(), APFloat(ArgTy->getFltSemantics(),
374 AV.IntVal));
375 break;
376 case Type::PointerTyID:
377 void *ArgPtr = GVTOP(AV);
378 if (sizeof(void*) == 4)
379 C = ConstantInt::get(Type::getInt32Ty(F->getContext()),
380 (int)(intptr_t)ArgPtr);
381 else
382 C = ConstantInt::get(Type::getInt64Ty(F->getContext()),
383 (intptr_t)ArgPtr);
384 // Cast the integer to pointer
385 C = ConstantExpr::getIntToPtr(C, ArgTy);
386 break;
387 }
388 Args.push_back(C);
389 }
390
391 CallInst *TheCall = CallInst::Create(F, Args, "", StubBB);
392 TheCall->setCallingConv(F->getCallingConv());
393 TheCall->setTailCall();
394 if (!TheCall->getType()->isVoidTy())
395 // Return result of the call.
396 ReturnInst::Create(F->getContext(), TheCall, StubBB);
397 else
398 ReturnInst::Create(F->getContext(), StubBB); // Just return void.
399
400 // Finally, call our nullary stub function.
401 GenericValue Result = runFunction(Stub, std::vector());
402 // Erase it, since no other function can have a reference to it.
403 Stub->eraseFromParent();
404 // And return the result.
405 return Result;
406 }
407
408 void JIT::RegisterJITEventListener(JITEventListener *L) {
409 if (!L)
410 return;
411 MutexGuard locked(lock);
412 EventListeners.push_back(L);
413 }
414 void JIT::UnregisterJITEventListener(JITEventListener *L) {
415 if (!L)
416 return;
417 MutexGuard locked(lock);
418 std::vector::reverse_iterator I=
419 std::find(EventListeners.rbegin(), EventListeners.rend(), L);
420 if (I != EventListeners.rend()) {
421 std::swap(*I, EventListeners.back());
422 EventListeners.pop_back();
423 }
424 }
425 void JIT::NotifyFunctionEmitted(
426 const Function &F,
427 void *Code, size_t Size,
428 const JITEvent_EmittedFunctionDetails &Details) {
429 MutexGuard locked(lock);
430 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
431 EventListeners[I]->NotifyFunctionEmitted(F, Code, Size, Details);
432 }
433 }
434
435 void JIT::NotifyFreeingMachineCode(void *OldPtr) {
436 MutexGuard locked(lock);
437 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
438 EventListeners[I]->NotifyFreeingMachineCode(OldPtr);
439 }
440 }
441
442 /// runJITOnFunction - Run the FunctionPassManager full of
443 /// just-in-time compilation passes on F, hopefully filling in
444 /// GlobalAddress[F] with the address of F's machine code.
445 ///
446 void JIT::runJITOnFunction(Function *F, MachineCodeInfo *MCI) {
447 MutexGuard locked(lock);
448
449 class MCIListener : public JITEventListener {
450 MachineCodeInfo *const MCI;
451 public:
452 MCIListener(MachineCodeInfo *mci) : MCI(mci) {}
453 void NotifyFunctionEmitted(const Function &, void *Code, size_t Size,
454 const EmittedFunctionDetails &) override {
455 MCI->setAddress(Code);
456 MCI->setSize(Size);
457 }
458 };
459 MCIListener MCIL(MCI);
460 if (MCI)
461 RegisterJITEventListener(&MCIL);
462
463 runJITOnFunctionUnlocked(F);
464
465 if (MCI)
466 UnregisterJITEventListener(&MCIL);
467 }
468
469 void JIT::runJITOnFunctionUnlocked(Function *F) {
470 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
471
472 jitTheFunctionUnlocked(F);
473
474 // If the function referred to another function that had not yet been
475 // read from bitcode, and we are jitting non-lazily, emit it now.
476 while (!jitstate->getPendingFunctions().empty()) {
477 Function *PF = jitstate->getPendingFunctions().back();
478 jitstate->getPendingFunctions().pop_back();
479
480 assert(!PF->hasAvailableExternallyLinkage() &&
481 "Externally-defined function should not be in pending list.");
482
483 jitTheFunctionUnlocked(PF);
484
485 // Now that the function has been jitted, ask the JITEmitter to rewrite
486 // the stub with real address of the function.
487 updateFunctionStubUnlocked(PF);
488 }
489 }
490
491 void JIT::jitTheFunctionUnlocked(Function *F) {
492 isAlreadyCodeGenerating = true;
493 jitstate->getPM().run(*F);
494 isAlreadyCodeGenerating = false;
495
496 // clear basic block addresses after this function is done
497 getBasicBlockAddressMap().clear();
498 }
499
500 /// getPointerToFunction - This method is used to get the address of the
501 /// specified function, compiling it if necessary.
502 ///
503 void *JIT::getPointerToFunction(Function *F) {
504
505 if (void *Addr = getPointerToGlobalIfAvailable(F))
506 return Addr; // Check if function already code gen'd
507
508 MutexGuard locked(lock);
509
510 // Now that this thread owns the lock, make sure we read in the function if it
511 // exists in this Module.
512 std::string ErrorMsg;
513 if (F->Materialize(&ErrorMsg)) {
514 report_fatal_error("Error reading function '" + F->getName()+
515 "' from bitcode file: " + ErrorMsg);
516 }
517
518 // ... and check if another thread has already code gen'd the function.
519 if (void *Addr = getPointerToGlobalIfAvailable(F))
520 return Addr;
521
522 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
523 bool AbortOnFailure = !F->hasExternalWeakLinkage();
524 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
525 addGlobalMapping(F, Addr);
526 return Addr;
527 }
528
529 runJITOnFunctionUnlocked(F);
530
531 void *Addr = getPointerToGlobalIfAvailable(F);
532 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
533 return Addr;
534 }
535
536 void JIT::addPointerToBasicBlock(const BasicBlock *BB, void *Addr) {
537 MutexGuard locked(lock);
538
539 BasicBlockAddressMapTy::iterator I =
540 getBasicBlockAddressMap().find(BB);
541 if (I == getBasicBlockAddressMap().end()) {
542 getBasicBlockAddressMap()[BB] = Addr;
543 } else {
544 // ignore repeats: some BBs can be split into few MBBs?
545 }
546 }
547
548 void JIT::clearPointerToBasicBlock(const BasicBlock *BB) {
549 MutexGuard locked(lock);
550 getBasicBlockAddressMap().erase(BB);
551 }
552
553 void *JIT::getPointerToBasicBlock(BasicBlock *BB) {
554 // make sure it's function is compiled by JIT
555 (void)getPointerToFunction(BB->getParent());
556
557 // resolve basic block address
558 MutexGuard locked(lock);
559
560 BasicBlockAddressMapTy::iterator I =
561 getBasicBlockAddressMap().find(BB);
562 if (I != getBasicBlockAddressMap().end()) {
563 return I->second;
564 } else {
565 llvm_unreachable("JIT does not have BB address for address-of-label, was"
566 " it eliminated by optimizer?");
567 }
568 }
569
570 void *JIT::getPointerToNamedFunction(const std::string &Name,
571 bool AbortOnFailure){
572 if (!isSymbolSearchingDisabled()) {
573 void *ptr = JMM->getPointerToNamedFunction(Name, false);
574 if (ptr)
575 return ptr;
576 }
577
578 /// If a LazyFunctionCreator is installed, use it to get/create the function.
579 if (LazyFunctionCreator)
580 if (void *RP = LazyFunctionCreator(Name))
581 return RP;
582
583 if (AbortOnFailure) {
584 report_fatal_error("Program used external function '"+Name+
585 "' which could not be resolved!");
586 }
587 return nullptr;
588 }
589
590
591 /// getOrEmitGlobalVariable - Return the address of the specified global
592 /// variable, possibly emitting it to memory if needed. This is used by the
593 /// Emitter.
594 void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
595 MutexGuard locked(lock);
596
597 void *Ptr = getPointerToGlobalIfAvailable(GV);
598 if (Ptr) return Ptr;
599
600 // If the global is external, just remember the address.
601 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage()) {
602 #if HAVE___DSO_HANDLE
603 if (GV->getName() == "__dso_handle")
604 return (void*)&__dso_handle;
605 #endif
606 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName());
607 if (!Ptr) {
608 report_fatal_error("Could not resolve external global address: "
609 +GV->getName());
610 }
611 addGlobalMapping(GV, Ptr);
612 } else {
613 // If the global hasn't been emitted to memory yet, allocate space and
614 // emit it into memory.
615 Ptr = getMemoryForGV(GV);
616 addGlobalMapping(GV, Ptr);
617 EmitGlobalVariable(GV); // Initialize the variable.
618 }
619 return Ptr;
620 }
621
622 /// recompileAndRelinkFunction - This method is used to force a function
623 /// which has already been compiled, to be compiled again, possibly
624 /// after it has been modified. Then the entry to the old copy is overwritten
625 /// with a branch to the new copy. If there was no old copy, this acts
626 /// just like JIT::getPointerToFunction().
627 ///
628 void *JIT::recompileAndRelinkFunction(Function *F) {
629 void *OldAddr = getPointerToGlobalIfAvailable(F);
630
631 // If it's not already compiled there is no reason to patch it up.
632 if (!OldAddr) return getPointerToFunction(F);
633
634 // Delete the old function mapping.
635 addGlobalMapping(F, nullptr);
636
637 // Recodegen the function
638 runJITOnFunction(F);
639
640 // Update state, forward the old function to the new function.
641 void *Addr = getPointerToGlobalIfAvailable(F);
642 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
643 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
644 return Addr;
645 }
646
647 /// getMemoryForGV - This method abstracts memory allocation of global
648 /// variable so that the JIT can allocate thread local variables depending
649 /// on the target.
650 ///
651 char* JIT::getMemoryForGV(const GlobalVariable* GV) {
652 char *Ptr;
653
654 // GlobalVariable's which are not "constant" will cause trouble in a server
655 // situation. It's returned in the same block of memory as code which may
656 // not be writable.
657 if (isGVCompilationDisabled() && !GV->isConstant()) {
658 report_fatal_error("Compilation of non-internal GlobalValue is disabled!");
659 }
660
661 // Some applications require globals and code to live together, so they may
662 // be allocated into the same buffer, but in general globals are allocated
663 // through the memory manager which puts them near the code but not in the
664 // same buffer.
665 Type *GlobalType = GV->getType()->getElementType();
666 size_t S = getDataLayout()->getTypeAllocSize(GlobalType);
667 size_t A = getDataLayout()->getPreferredAlignment(GV);
668 if (GV->isThreadLocal()) {
669 MutexGuard locked(lock);
670 Ptr = TJI.allocateThreadLocalMemory(S);
671 } else if (TJI.allocateSeparateGVMemory()) {
672 if (A <= 8) {
673 Ptr = (char*)malloc(S);
674 } else {
675 // Allocate S+A bytes of memory, then use an aligned pointer within that
676 // space.
677 Ptr = (char*)malloc(S+A);
678 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
679 Ptr = Ptr + (MisAligned ? (A-MisAligned) : 0);
680 }
681 } else if (AllocateGVsWithCode) {
682 Ptr = (char*)JCE->allocateSpace(S, A);
683 } else {
684 Ptr = (char*)JCE->allocateGlobal(S, A);
685 }
686 return Ptr;
687 }
688
689 void JIT::addPendingFunction(Function *F) {
690 MutexGuard locked(lock);
691 jitstate->getPendingFunctions().push_back(F);
692 }
693
694
695 JITEventListener::~JITEventListener() {}
+0
-214
lib/ExecutionEngine/JIT/JIT.h less more
None //===-- JIT.h - Class definition for the JIT --------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the top-level JIT data structure.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef JIT_H
14 #define JIT_H
15
16 #include "llvm/ExecutionEngine/ExecutionEngine.h"
17 #include "llvm/IR/ValueHandle.h"
18 #include "llvm/PassManager.h"
19
20 namespace llvm {
21
22 class Function;
23 struct JITEvent_EmittedFunctionDetails;
24 class MachineCodeEmitter;
25 class MachineCodeInfo;
26 class TargetJITInfo;
27 class TargetMachine;
28
29 class JITState {
30 private:
31 FunctionPassManager PM; // Passes to compile a function
32 Module *M; // Module used to create the PM
33
34 /// PendingFunctions - Functions which have not been code generated yet, but
35 /// were called from a function being code generated.
36 std::vector > PendingFunctions;
37
38 public:
39 explicit JITState(Module *M) : PM(M), M(M) {}
40
41 FunctionPassManager &getPM() {
42 return PM;
43 }
44
45 Module *getModule() const { return M; }
46 std::vector > &getPendingFunctions() {
47 return PendingFunctions;
48 }
49 };
50
51
52 class JIT : public ExecutionEngine {
53 /// types
54 typedef ValueMap
55 BasicBlockAddressMapTy;
56 /// data
57 TargetMachine &TM; // The current target we are compiling to
58 TargetJITInfo &TJI; // The JITInfo for the target we are compiling to
59 JITCodeEmitter *JCE; // JCE object
60 JITMemoryManager *JMM;
61 std::vector EventListeners;
62
63 /// AllocateGVsWithCode - Some applications require that global variables and
64 /// code be allocated into the same region of memory, in which case this flag
65 /// should be set to true. Doing so breaks freeMachineCodeForFunction.
66 bool AllocateGVsWithCode;
67
68 /// True while the JIT is generating code. Used to assert against recursive
69 /// entry.
70 bool isAlreadyCodeGenerating;
71
72 JITState *jitstate;
73
74 /// BasicBlockAddressMap - A mapping between LLVM basic blocks and their
75 /// actualized version, only filled for basic blocks that have their address
76 /// taken.
77 BasicBlockAddressMapTy BasicBlockAddressMap;
78
79
80 JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
81 JITMemoryManager *JMM, bool AllocateGVsWithCode);
82 public:
83 ~JIT();
84
85 static void Register() {
86 JITCtor = createJIT;
87 }
88
89 /// getJITInfo - Return the target JIT information structure.
90 ///
91 TargetJITInfo &getJITInfo() const { return TJI; }
92
93 void addModule(Module *M) override;
94
95 /// removeModule - Remove a Module from the list of modules. Returns true if
96 /// M is found.
97 bool removeModule(Module *M) override;
98
99 /// runFunction - Start execution with the specified function and arguments.
100 ///
101 GenericValue runFunction(Function *F,
102 const std::vector &ArgValues) override;
103
104 /// getPointerToNamedFunction - This method returns the address of the
105 /// specified function by using the MemoryManager. As such it is only
106 /// useful for resolving library symbols, not code generated symbols.
107 ///
108 /// If AbortOnFailure is false and no function with the given name is
109 /// found, this function silently returns a null pointer. Otherwise,
110 /// it prints a message to stderr and aborts.
111 ///
112 void *getPointerToNamedFunction(const std::string &Name,
113 bool AbortOnFailure = true) override;
114
115 // CompilationCallback - Invoked the first time that a call site is found,
116 // which causes lazy compilation of the target function.
117 //
118 static void CompilationCallback();
119
120 /// getPointerToFunction - This returns the address of the specified function,
121 /// compiling it if necessary.
122 ///
123 void *getPointerToFunction(Function *F) override;
124
125 /// addPointerToBasicBlock - Adds address of the specific basic block.
126 void addPointerToBasicBlock(const BasicBlock *BB, void *Addr);
127
128 /// clearPointerToBasicBlock - Removes address of specific basic block.
129 void clearPointerToBasicBlock(const BasicBlock *BB);
130
131 /// getPointerToBasicBlock - This returns the address of the specified basic
132 /// block, assuming function is compiled.
133 void *getPointerToBasicBlock(BasicBlock *BB) override;
134
135 /// getOrEmitGlobalVariable - Return the address of the specified global
136 /// variable, possibly emitting it to memory if needed. This is used by the
137 /// Emitter.
138 void *getOrEmitGlobalVariable(const GlobalVariable *GV) override;
139
140 /// getPointerToFunctionOrStub - If the specified function has been
141 /// code-gen'd, return a pointer to the function. If not, compile it, or use
142 /// a stub to implement lazy compilation if available.
143 ///
144 void *getPointerToFunctionOrStub(Function *F) override;
145
146 /// recompileAndRelinkFunction - This method is used to force a function
147 /// which has already been compiled, to be compiled again, possibly
148 /// after it has been modified. Then the entry to the old copy is overwritten
149 /// with a branch to the new copy. If there was no old copy, this acts
150 /// just like JIT::getPointerToFunction().
151 ///
152 void *recompileAndRelinkFunction(Function *F) override;
153
154 /// freeMachineCodeForFunction - deallocate memory used to code-generate this
155 /// Function.
156 ///
157 void freeMachineCodeForFunction(Function *F) override;
158
159 /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd
160 /// function was encountered. Add it to a pending list to be processed after
161 /// the current function.
162 ///
163 void addPendingFunction(Function *F);
164
165 /// getCodeEmitter - Return the code emitter this JIT is emitting into.
166 ///
167 JITCodeEmitter *getCodeEmitter() const { return JCE; }
168
169 static ExecutionEngine *createJIT(Module *M,
170 std::string *ErrorStr,
171 JITMemoryManager *JMM,
172 bool GVsWithCode,
173 TargetMachine *TM);
174
175 // Run the JIT on F and return information about the generated code
176 void runJITOnFunction(Function *F, MachineCodeInfo *MCI = nullptr) override;
177
178 void RegisterJITEventListener(JITEventListener *L) override;
179 void UnregisterJITEventListener(JITEventListener *L) override;
180
181 TargetMachine *getTargetMachine() override { return &TM; }
182
183 /// These functions correspond to the methods on JITEventListener. They
184 /// iterate over the registered listeners and call the corresponding method on
185 /// each.
186 void NotifyFunctionEmitted(
187 const Function &F, void *Code, size_t Size,
188 const JITEvent_EmittedFunctionDetails &Details);
189 void NotifyFreeingMachineCode(void *OldPtr);
190
191 BasicBlockAddressMapTy &
192 getBasicBlockAddressMap() {
193 return BasicBlockAddressMap;
194 }
195
196
197 private:
198 static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM,
199 TargetMachine &tm);
200 void runJITOnFunctionUnlocked(Function *F);
201 void updateFunctionStubUnlocked(Function *F);
202 void jitTheFunctionUnlocked(Function *F);
203
204 protected:
205
206 /// getMemoryforGV - Allocate memory for a global variable.
207 char* getMemoryForGV(const GlobalVariable* GV) override;
208
209 };
210
211 } // End llvm namespace
212
213 #endif
+0
-1249
lib/ExecutionEngine/JIT/JITEmitter.cpp less more
None //===-- JITEmitter.cpp - Write machine code to executable memory ----------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines a MachineCodeEmitter object that is used by the JIT to
10 // write machine code to memory and remember where relocatable values are.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "JIT.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/CodeGen/JITCodeEmitter.h"
20 #include "llvm/CodeGen/MachineCodeInfo.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineJumpTableInfo.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/CodeGen/MachineRelocation.h"
26 #include "llvm/ExecutionEngine/GenericValue.h"
27 #include "llvm/ExecutionEngine/JITEventListener.h"
28 #include "llvm/ExecutionEngine/JITMemoryManager.h"
29 #include "llvm/IR/Constants.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/DebugInfo.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/Operator.h"
35 #include "llvm/IR/ValueHandle.h"
36 #include "llvm/IR/ValueMap.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/ManagedStatic.h"
40 #include "llvm/Support/Memory.h"
41 #include "llvm/Support/MutexGuard.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Target/TargetInstrInfo.h"
44 #include "llvm/Target/TargetJITInfo.h"
45 #include "llvm/Target/TargetMachine.h"
46 #include "llvm/Target/TargetOptions.h"
47 #include
48 #ifndef NDEBUG
49 #include
50 #endif
51 using namespace llvm;
52
53 #define DEBUG_TYPE "jit"
54
55 STATISTIC(NumBytes, "Number of bytes of machine code compiled");
56 STATISTIC(NumRelos, "Number of relocations applied");
57 STATISTIC(NumRetries, "Number of retries with more memory");
58
59
60 // A declaration may stop being a declaration once it's fully read from bitcode.
61 // This function returns true if F is fully read and is still a declaration.
62 static bool isNonGhostDeclaration(const Function *F) {
63 return F->isDeclaration() && !F->isMaterializable();
64 }
65
66 //===----------------------------------------------------------------------===//
67 // JIT lazy compilation code.
68 //
69 namespace {
70 class JITEmitter;
71 class JITResolverState;
72
73 template
74 struct NoRAUWValueMapConfig : public ValueMapConfig {
75 typedef JITResolverState *ExtraData;
76 static void onRAUW(JITResolverState *, Value *Old, Value *New) {
77 llvm_unreachable("The JIT doesn't know how to handle a"
78 " RAUW on a value it has emitted.");
79 }
80 };
81
82 struct CallSiteValueMapConfig : public NoRAUWValueMapConfig {
83 typedef JITResolverState *ExtraData;
84 static void onDelete(JITResolverState *JRS, Function *F);
85 };
86
87 class JITResolverState {
88 public:
89 typedef ValueMap >
90 FunctionToLazyStubMapTy;
91 typedef std::map > CallSiteToFunctionMapTy;
92 typedef ValueMap,
93 CallSiteValueMapConfig> FunctionToCallSitesMapTy;
94 typedef std::map, void*> GlobalToIndirectSymMapTy;
95 private:
96 /// FunctionToLazyStubMap - Keep track of the lazy stub created for a
97 /// particular function so that we can reuse them if necessary.
98 FunctionToLazyStubMapTy FunctionToLazyStubMap;
99
100 /// CallSiteToFunctionMap - Keep track of the function that each lazy call
101 /// site corresponds to, and vice versa.
102 CallSiteToFunctionMapTy CallSiteToFunctionMap;
103 FunctionToCallSitesMapTy FunctionToCallSitesMap;
104
105 /// GlobalToIndirectSymMap - Keep track of the indirect symbol created for a
106 /// particular GlobalVariable so that we can reuse them if necessary.
107 GlobalToIndirectSymMapTy GlobalToIndirectSymMap;
108
109 #ifndef NDEBUG
110 /// Instance of the JIT this ResolverState serves.
111 JIT *TheJIT;
112 #endif
113
114 public:
115 JITResolverState(JIT *jit) : FunctionToLazyStubMap(this),
116 FunctionToCallSitesMap(this) {
117 #ifndef NDEBUG
118 TheJIT = jit;
119 #endif
120 }
121
122 FunctionToLazyStubMapTy& getFunctionToLazyStubMap() {
123 return FunctionToLazyStubMap;
124 }
125
126 GlobalToIndirectSymMapTy& getGlobalToIndirectSymMap() {
127 return GlobalToIndirectSymMap;
128 }
129
130 std::pair LookupFunctionFromCallSite(
131 void *CallSite) const {
132 // The address given to us for the stub may not be exactly right, it
133 // might be a little bit after the stub. As such, use upper_bound to
134 // find it.
135 CallSiteToFunctionMapTy::const_iterator I =
136 CallSiteToFunctionMap.upper_bound(CallSite);
137 assert(I != CallSiteToFunctionMap.begin() &&
138 "This is not a known call site!");
139 --I;
140 return *I;
141 }
142
143 void AddCallSite(void *CallSite, Function *F) {
144 bool Inserted = CallSiteToFunctionMap.insert(
145 std::make_pair(CallSite, F)).second;
146 (void)Inserted;
147 assert(Inserted && "Pair was already in CallSiteToFunctionMap");
148 FunctionToCallSitesMap[F].insert(CallSite);
149 }
150
151 void EraseAllCallSitesForPrelocked(Function *F);
152
153 // Erases _all_ call sites regardless of their function. This is used to
154 // unregister the stub addresses from the StubToResolverMap in
155 // ~JITResolver().
156 void EraseAllCallSitesPrelocked();
157 };
158
159 /// JITResolver - Keep track of, and resolve, call sites for functions that
160 /// have not yet been compiled.
161 class JITResolver {
162 typedef JITResolverState::FunctionToLazyStubMapTy FunctionToLazyStubMapTy;
163 typedef JITResolverState::CallSiteToFunctionMapTy CallSiteToFunctionMapTy;
164 typedef JITResolverState::GlobalToIndirectSymMapTy GlobalToIndirectSymMapTy;
165
166 /// LazyResolverFn - The target lazy resolver function that we actually
167 /// rewrite instructions to use.
168 TargetJITInfo::LazyResolverFn LazyResolverFn;
169
170 JITResolverState state;
171
172 /// ExternalFnToStubMap - This is the equivalent of FunctionToLazyStubMap
173 /// for external functions. TODO: Of course, external functions don't need
174 /// a lazy stub. It's actually here to make it more likely that far calls
175 /// succeed, but no single stub can guarantee that. I'll remove this in a
176 /// subsequent checkin when I actually fix far calls.
177 std::map ExternalFnToStubMap;
178
179 /// revGOTMap - map addresses to indexes in the GOT
180 std::map revGOTMap;
181 unsigned nextGOTIndex;
182
183 JITEmitter &JE;
184
185 /// Instance of JIT corresponding to this Resolver.
186 JIT *TheJIT;
187
188 public:
189 explicit JITResolver(JIT &jit, JITEmitter &je)
190 : state(&jit), nextGOTIndex(0), JE(je), TheJIT(&jit) {
191 LazyResolverFn = jit.getJITInfo().getLazyResolverFunction(JITCompilerFn);
192 }
193
194 ~JITResolver();
195
196 /// getLazyFunctionStubIfAvailable - This returns a pointer to a function's
197 /// lazy-compilation stub if it has already been created.
198 void *getLazyFunctionStubIfAvailable(Function *F);
199
200 /// getLazyFunctionStub - This returns a pointer to a function's
201 /// lazy-compilation stub, creating one on demand as needed.
202 void *getLazyFunctionStub(Function *F);
203
204 /// getExternalFunctionStub - Return a stub for the function at the
205 /// specified address, created lazily on demand.
206 void *getExternalFunctionStub(void *FnAddr);
207
208 /// getGlobalValueIndirectSym - Return an indirect symbol containing the
209 /// specified GV address.
210 void *getGlobalValueIndirectSym(GlobalValue *V, void *GVAddress);
211
212 /// getGOTIndexForAddress - Return a new or existing index in the GOT for
213 /// an address. This function only manages slots, it does not manage the
214 /// contents of the slots or the memory associated with the GOT.
215 unsigned getGOTIndexForAddr(void *addr);
216
217 /// JITCompilerFn - This function is called to resolve a stub to a compiled
218 /// address. If the LLVM Function corresponding to the stub has not yet
219 /// been compiled, this function compiles it first.
220 static void *JITCompilerFn(void *Stub);
221 };
222
223 class StubToResolverMapTy {
224 /// Map a stub address to a specific instance of a JITResolver so that
225 /// lazily-compiled functions can find the right resolver to use.
226 ///
227 /// Guarded by Lock.
228 std::map Map;
229
230 /// Guards Map from concurrent accesses.
231 mutable sys::Mutex Lock;
232
233 public:
234 /// Registers a Stub to be resolved by Resolver.
235 void RegisterStubResolver(void *Stub, JITResolver *Resolver) {
236 MutexGuard guard(Lock);
237 Map.insert(std::make_pair(Stub, Resolver));
238 }
239 /// Unregisters the Stub when it's invalidated.
240 void UnregisterStubResolver(void *Stub) {
241 MutexGuard guard(Lock);
242 Map.erase(Stub);
243 }
244 /// Returns the JITResolver instance that owns the Stub.
245 JITResolver *getResolverFromStub(void *Stub) const {
246 MutexGuard guard(Lock);
247 // The address given to us for the stub may not be exactly right, it might
248 // be a little bit after the stub. As such, use upper_bound to find it.
249 // This is the same trick as in LookupFunctionFromCallSite from
250 // JITResolverState.
251 std::map::const_iterator I = Map.upper_bound(Stub);
252 assert(I != Map.begin() && "This is not a known stub!");
253 --I;
254 return I->second;
255 }
256 /// True if any stubs refer to the given resolver. Only used in an assert().
257 /// O(N)
258 bool ResolverHasStubs(JITResolver* Resolver) const {
259 MutexGuard guard(Lock);
260 for (std::map::const_iterator I = Map.begin(),
261 E = Map.end(); I != E; ++I) {
262 if (I->second == Resolver)
263 return true;
264 }
265 return false;
266 }
267 };
268 /// This needs to be static so that a lazy call stub can access it with no
269 /// context except the address of the stub.
270 ManagedStatic StubToResolverMap;
271
272 /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
273 /// used to output functions to memory for execution.
274 class JITEmitter : public JITCodeEmitter {
275 JITMemoryManager *MemMgr;
276
277 // When outputting a function stub in the context of some other function, we
278 // save BufferBegin/BufferEnd/CurBufferPtr here.
279 uint8_t *SavedBufferBegin, *SavedBufferEnd, *SavedCurBufferPtr;
280
281 // When reattempting to JIT a function after running out of space, we store
282 // the estimated size of the function we're trying to JIT here, so we can
283 // ask the memory manager for at least this much space. When we
284 // successfully emit the function, we reset this back to zero.
285 uintptr_t SizeEstimate;
286
287 /// Relocations - These are the relocations that the function needs, as
288 /// emitted.
289 std::vector Relocations;
290
291 /// MBBLocations - This vector is a mapping from MBB ID's to their address.
292 /// It is filled in by the StartMachineBasicBlock callback and queried by
293 /// the getMachineBasicBlockAddress callback.
294 std::vector MBBLocations;
295
296 /// ConstantPool - The constant pool for the current function.
297 ///
298 MachineConstantPool *ConstantPool;
299
300 /// ConstantPoolBase - A pointer to the first entry in the constant pool.
301 ///
302 void *ConstantPoolBase;
303
304 /// ConstPoolAddresses - Addresses of individual constant pool entries.
305 ///
306 SmallVector ConstPoolAddresses;
307
308 /// JumpTable - The jump tables for the current function.
309 ///
310 MachineJumpTableInfo *JumpTable;
311
312 /// JumpTableBase - A pointer to the first entry in the jump table.
313 ///
314 void *JumpTableBase;
315
316 /// Resolver - This contains info about the currently resolved functions.
317 JITResolver Resolver;
318
319 /// LabelLocations - This vector is a mapping from Label ID's to their
320 /// address.
321 DenseMap LabelLocations;
322
323 /// MMI - Machine module info for exception informations
324 MachineModuleInfo* MMI;
325
326 // CurFn - The llvm function being emitted. Only valid during
327 // finishFunction().
328 const Function *CurFn;
329
330 /// Information about emitted code, which is passed to the
331 /// JITEventListeners. This is reset in startFunction and used in
332 /// finishFunction.
333 JITEvent_EmittedFunctionDetails EmissionDetails;
334
335 struct EmittedCode {
336 void *FunctionBody; // Beginning of the function's allocation.
337 void *Code; // The address the function's code actually starts at.
338 void *ExceptionTable;
339 EmittedCode() : FunctionBody(nullptr), Code(nullptr),
340 ExceptionTable(nullptr) {}
341 };
342 struct EmittedFunctionConfig : public ValueMapConfig {
343 typedef JITEmitter *ExtraData;
344 static void onDelete(JITEmitter *, const Function*);
345 static void onRAUW(JITEmitter *, const Function*, const Function*);
346 };
347 ValueMap
348 EmittedFunctionConfig> EmittedFunctions;
349
350 DebugLoc PrevDL;
351
352 /// Instance of the JIT
353 JIT *TheJIT;
354
355 public:
356 JITEmitter(JIT &jit, JITMemoryManager *JMM, TargetMachine &TM)
357 : SizeEstimate(0), Resolver(jit, *this), MMI(nullptr), CurFn(nullptr),
358 EmittedFunctions(this), TheJIT(&jit) {
359 MemMgr = JMM ? JMM : JITMemoryManager::CreateDefaultMemManager();
360 if (jit.getJITInfo().needsGOT()) {
361 MemMgr->AllocateGOT();
362 DEBUG(dbgs() << "JIT is managing a GOT\n");
363 }
364
365 }
366 ~JITEmitter() {
367 delete MemMgr;
368 }
369
370 JITResolver &getJITResolver() { return Resolver; }
371
372 void startFunction(MachineFunction &F) override;
373 bool finishFunction(MachineFunction &F) override;
374
375 void emitConstantPool(MachineConstantPool *MCP);
376 void initJumpTableInfo(MachineJumpTableInfo *MJTI);
377 void emitJumpTableInfo(MachineJumpTableInfo *MJTI);
378
379 void startGVStub(const GlobalValue* GV,
380 unsigned StubSize, unsigned Alignment = 1);
381 void startGVStub(void *Buffer, unsigned StubSize);
382 void finishGVStub();
383 void *allocIndirectGV(const GlobalValue *GV, const uint8_t *Buffer,
384 size_t Size, unsigned Alignment) override;
385
386 /// allocateSpace - Reserves space in the current block if any, or
387 /// allocate a new one of the given size.
388 void *allocateSpace(uintptr_t Size, unsigned Alignment) override;
389
390 /// allocateGlobal - Allocate memory for a global. Unlike allocateSpace,
391 /// this method does not allocate memory in the current output buffer,
392 /// because a global may live longer than the current function.
393 void *allocateGlobal(uintptr_t Size, unsigned Alignment) override;
394
395 void addRelocation(const MachineRelocation &MR) override {
396 Relocations.push_back(MR);
397 }
398
399 void StartMachineBasicBlock(MachineBasicBlock *MBB) override {
400 if (MBBLocations.size() <= (unsigned)MBB->getNumber())
401 MBBLocations.resize((MBB->getNumber()+1)*2);
402 MBBLocations[MBB->getNumber()] = getCurrentPCValue();
403 if (MBB->hasAddressTaken())
404 TheJIT->addPointerToBasicBlock(MBB->getBasicBlock(),
405 (void*)getCurrentPCValue());
406 DEBUG(dbgs() << "JIT: Emitting BB" << MBB->getNumber() << " at ["
407 << (void*) getCurrentPCValue() << "]\n");
408 }
409
410 uintptr_t getConstantPoolEntryAddress(unsigned Entry) const override;
411 uintptr_t getJumpTableEntryAddress(unsigned Entry) const override;
412
413 uintptr_t
414 getMachineBasicBlockAddress(MachineBasicBlock *MBB) const override {
415 assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
416 MBBLocations[MBB->getNumber()] && "MBB not emitted!");
417 return MBBLocations[MBB->getNumber()];
418 }
419
420 /// retryWithMoreMemory - Log a retry and deallocate all memory for the
421 /// given function. Increase the minimum allocation size so that we get
422 /// more memory next time.
423 void retryWithMoreMemory(MachineFunction &F);
424
425 /// deallocateMemForFunction - Deallocate all memory for the specified
426 /// function body.
427 void deallocateMemForFunction(const Function *F);
428
429 void processDebugLoc(DebugLoc DL, bool BeforePrintingInsn) override;
430
431 void emitLabel(MCSymbol *Label) override {
432 LabelLocations[Label] = getCurrentPCValue();
433 }
434
435 DenseMap *getLabelLocations() override {
436 return &LabelLocations;
437 }
438
439 uintptr_t getLabelAddress(MCSymbol *Label) const override {
440 assert(LabelLocations.count(Label) && "Label not emitted!");
441 return LabelLocations.find(Label)->second;
442 }
443
444 void setModuleInfo(MachineModuleInfo* Info) override {
445 MMI = Info;
446 }
447
448 private:
449 void *getPointerToGlobal(GlobalValue *GV, void *Reference,
450 bool MayNeedFarStub);
451 void *getPointerToGVIndirectSym(GlobalValue *V, void *Reference);
452 };
453 }
454
455 void CallSiteValueMapConfig::onDelete(JITResolverState *JRS, Function *F) {
456 JRS->EraseAllCallSitesForPrelocked(F);
457 }
458
459 void JITResolverState::EraseAllCallSitesForPrelocked(Function *F) {
460 FunctionToCallSitesMapTy::iterator F2C = FunctionToCallSitesMap.find(F);
461 if (F2C == FunctionToCallSitesMap.end())
462 return;
463 StubToResolverMapTy &S2RMap = *StubToResolverMap;
464 for (SmallPtrSet::const_iterator I = F2C->second.begin(),
465 E = F2C->second.end(); I != E; ++I) {
466 S2RMap.UnregisterStubResolver(*I);
467 bool Erased = CallSiteToFunctionMap.erase(*I);
468 (void)Erased;
469 assert(Erased && "Missing call site->function mapping");
470 }
471 FunctionToCallSitesMap.erase(F2C);
472 }
473
474 void JITResolverState::EraseAllCallSitesPrelocked() {
475 StubToResolverMapTy &S2RMap = *StubToResolverMap;
476 for (CallSiteToFunctionMapTy::const_iterator
477 I = CallSiteToFunctionMap.begin(),
478 E = CallSiteToFunctionMap.end(); I != E; ++I) {
479 S2RMap.UnregisterStubResolver(I->first);
480 }
481 CallSiteToFunctionMap.clear();
482 FunctionToCallSitesMap.clear();
483 }
484
485 JITResolver::~JITResolver() {
486 // No need to lock because we're in the destructor, and state isn't shared.
487 state.EraseAllCallSitesPrelocked();
488 assert(!StubToResolverMap->ResolverHasStubs(this) &&
489 "Resolver destroyed with stubs still alive.");
490 }
491
492 /// getLazyFunctionStubIfAvailable - This returns a pointer to a function stub
493 /// if it has already been created.
494 void *JITResolver::getLazyFunctionStubIfAvailable(Function *F) {
495 MutexGuard locked(TheJIT->lock);
496
497 // If we already have a stub for this function, recycle it.
498 return state.getFunctionToLazyStubMap().lookup(F);
499 }
500
501 /// getFunctionStub - This returns a pointer to a function stub, creating
502 /// one on demand as needed.
503 void *JITResolver::getLazyFunctionStub(Function *F) {
504 MutexGuard locked(TheJIT->lock);
505
506 // If we already have a lazy stub for this function, recycle it.
507 void *&Stub = state.getFunctionToLazyStubMap()[F];
508 if (Stub) return Stub;
509
510 // Call the lazy resolver function if we are JIT'ing lazily. Otherwise we
511 // must resolve the symbol now.
512 void *Actual = TheJIT->isCompilingLazily()
513 ? (void *)(intptr_t)LazyResolverFn : (void *)nullptr;
514
515 // If this is an external declaration, attempt to resolve the address now
516 // to place in the stub.
517 if (isNonGhostDeclaration(F) || F->hasAvailableExternallyLinkage()) {
518 Actual = TheJIT->getPointerToFunction(F);
519
520 // If we resolved the symbol to a null address (eg. a weak external)
521 // don't emit a stub. Return a null pointer to the application.
522 if (!Actual) return nullptr;
523 }
524
525 TargetJITInfo::StubLayout SL = TheJIT->getJITInfo().getStubLayout();
526 JE.startGVStub(F, SL.Size, SL.Alignment);
527 // Codegen a new stub, calling the lazy resolver or the actual address of the
528 // external function, if it was resolved.
529 Stub = TheJIT->getJITInfo().emitFunctionStub(F, Actual, JE);
530 JE.finishGVStub();
531
532 if (Actual != (void*)(intptr_t)LazyResolverFn) {
533 // If we are getting the stub for an external function, we really want the
534 // address of the stub in the GlobalAddressMap for the JIT, not the address
535 // of the external function.
536 TheJIT->updateGlobalMapping(F, Stub);
537 }
538
539 DEBUG(dbgs() << "JIT: Lazy stub emitted at [" << Stub << "] for function '"
540 << F->getName() << "'\n");
541
542 if (TheJIT->isCompilingLazily()) {
543 // Register this JITResolver as the one corresponding to this call site so
544 // JITCompilerFn will be able to find it.
545 StubToResolverMap->RegisterStubResolver(Stub, this);
546
547 // Finally, keep track of the stub-to-Function mapping so that the
548 // JITCompilerFn knows which function to compile!
549 state.AddCallSite(Stub, F);
550 } else if (!Actual) {
551 // If we are JIT'ing non-lazily but need to call a function that does not
552 // exist yet, add it to the JIT's work list so that we can fill in the
553 // stub address later.
554 assert(!isNonGhostDeclaration(F) && !F->hasAvailableExternallyLinkage() &&
555 "'Actual' should have been set above.");
556 TheJIT->addPendingFunction(F);
557 }
558
559 return Stub;
560 }
561
562 /// getGlobalValueIndirectSym - Return a lazy pointer containing the specified
563 /// GV address.
564 void *JITResolver::getGlobalValueIndirectSym(GlobalValue *GV, void *GVAddress) {
565 MutexGuard locked(TheJIT->lock);
566
567 // If we already have a stub for this global variable, recycle it.
568 void *&IndirectSym = state.getGlobalToIndirectSymMap()[GV];
569 if (IndirectSym) return IndirectSym;
570
571 // Otherwise, codegen a new indirect symbol.
572 IndirectSym = TheJIT->getJITInfo().emitGlobalValueIndirectSym(GV, GVAddress,
573 JE);
574
575 DEBUG(dbgs() << "JIT: Indirect symbol emitted at [" << IndirectSym
576 << "] for GV '" << GV->getName() << "'\n");
577
578 return IndirectSym;
579 }
580
581 /// getExternalFunctionStub - Return a stub for the function at the
582 /// specified address, created lazily on demand.
583 void *JITResolver::getExternalFunctionStub(void *FnAddr) {
584 // If we already have a stub for this function, recycle it.
585 void *&Stub = ExternalFnToStubMap[FnAddr];
586 if (Stub) return Stub;
587
588 TargetJITInfo::StubLayout SL = TheJIT->getJITInfo().getStubLayout();
589 JE.startGVStub(nullptr, SL.Size, SL.Alignment);
590 Stub = TheJIT->getJITInfo().emitFunctionStub(nullptr, FnAddr, JE);
591 JE.finishGVStub();
592
593 DEBUG(dbgs() << "JIT: Stub emitted at [" << Stub
594 << "] for external function at '" << FnAddr << "'\n");
595 return Stub;
596 }
597
598 unsigned JITResolver::getGOTIndexForAddr(void* addr) {
599 unsigned idx = revGOTMap[addr];
600 if (!idx) {
601 idx = ++nextGOTIndex;
602 revGOTMap[addr] = idx;
603 DEBUG(dbgs() << "JIT: Adding GOT entry " << idx << " for addr ["
604 << addr << "]\n");
605 }
606 return idx;
607 }
608
609 /// JITCompilerFn - This function is called when a lazy compilation stub has
610 /// been entered. It looks up which function this stub corresponds to, compiles
611 /// it if necessary, then returns the resultant function pointer.
612 void *JITResolver::JITCompilerFn(void *Stub) {
613 JITResolver *JR = StubToResolverMap->getResolverFromStub(Stub);
614 assert(JR && "Unable to find the corresponding JITResolver to the call site");
615
616 Function* F = nullptr;
617 void* ActualPtr = nullptr;
618
619 {
620 // Only lock for getting the Function. The call getPointerToFunction made
621 // in this function might trigger function materializing, which requires
622 // JIT lock to be unlocked.
623 MutexGuard locked(JR->TheJIT->lock);
624
625 // The address given to us for the stub may not be exactly right, it might
626 // be a little bit after the stub. As such, use upper_bound to find it.
627 std::pair I =
628 JR->state.LookupFunctionFromCallSite(Stub);
629 F = I.second;
630 ActualPtr = I.first;
631 }
632
633 // If we have already code generated the function, just return the address.
634 void *Result = JR->TheJIT->getPointerToGlobalIfAvailable(F);
635
636 if (!Result) {
637 // Otherwise we don't have it, do lazy compilation now.
638
639 // If lazy compilation is disabled, emit a useful error message and abort.
640 if (!JR->TheJIT->isCompilingLazily()) {
641 report_fatal_error("LLVM JIT requested to do lazy compilation of"
642 " function '"
643 + F->getName() + "' when lazy compiles are disabled!");
644 }
645
646 DEBUG(dbgs() << "JIT: Lazily resolving function '" << F->getName()
647 << "' In stub ptr = " << Stub << " actual ptr = "
648 << ActualPtr << "\n");
649 (void)ActualPtr;
650
651 Result = JR->TheJIT->getPointerToFunction(F);
652 }
653
654 // Reacquire the lock to update the GOT map.
655 MutexGuard locked(JR->TheJIT->lock);
656
657 // We might like to remove the call site from the CallSiteToFunction map, but
658 // we can't do that! Multiple threads could be stuck, waiting to acquire the
659 // lock above. As soon as the 1st function finishes compiling the function,
660 // the next one will be released, and needs to be able to find the function it
661 // needs to call.
662
663 // FIXME: We could rewrite all references to this stub if we knew them.
664
665 // What we will do is set the compiled function address to map to the
666 // same GOT entry as the stub so that later clients may update the GOT
667 // if they see it still using the stub address.
668 // Note: this is done so the Resolver doesn't have to manage GOT memory
669 // Do this without allocating map space if the target isn't using a GOT
670 if(JR->revGOTMap.find(Stub) != JR->revGOTMap.end())
671 JR->revGOTMap[Result] = JR->revGOTMap[Stub];
672
673 return Result;
674 }
675
676 //===----------------------------------------------------------------------===//
677 // JITEmitter code.
678 //
679
680 static GlobalObject *getSimpleAliasee(Constant *C) {
681 C = C->stripPointerCasts();
682 return dyn_cast(C);
683 }
684
685 void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
686 bool MayNeedFarStub) {
687 if (GlobalVariable *GV = dyn_cast(V))
688 return TheJIT->getOrEmitGlobalVariable(GV);
689
690 if (GlobalAlias *GA = dyn_cast(V)) {
691 // We can only handle simple cases.
692 if (GlobalValue *GV = getSimpleAliasee(GA->getAliasee()))
693 return TheJIT->getPointerToGlobal(GV);
694 return nullptr;
695 }
696
697 // If we have already compiled the function, return a pointer to its body.
698 Function *F = cast(V);
699
700 void *FnStub = Resolver.getLazyFunctionStubIfAvailable(F);
701 if (FnStub) {
702 // Return the function stub if it's already created. We do this first so
703 // that we're returning the same address for the function as any previous
704 // call. TODO: Yes, this is wrong. The lazy stub isn't guaranteed to be
705 // close enough to call.
706 return FnStub;
707 }
708
709 // If we know the target can handle arbitrary-distance calls, try to
710 // return a direct pointer.
711 if (!MayNeedFarStub) {
712 // If we have code, go ahead and return that.
713 void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
714 if (ResultPtr) return ResultPtr;
715
716 // If this is an external function pointer, we can force the JIT to
717 // 'compile' it, which really just adds it to the map.
718 if (isNonGhostDeclaration(F) || F->hasAvailableExternallyLinkage())
719 return TheJIT->getPointerToFunction(F);
720 }
721
722 // Otherwise, we may need a to emit a stub, and, conservatively, we always do
723 // so. Note that it's possible to return null from getLazyFunctionStub in the
724 // case of a weak extern that fails to resolve.
725 return Resolver.getLazyFunctionStub(F);
726 }
727
728 void *JITEmitter::getPointerToGVIndirectSym(GlobalValue *V, void *Reference) {
729 // Make sure GV is emitted first, and create a stub containing the fully
730 // resolved address.
731 void *GVAddress = getPointerToGlobal(V, Reference, false);
732 void *StubAddr = Resolver.getGlobalValueIndirectSym(V, GVAddress);
733 return StubAddr;
734 }
735
736 void JITEmitter::processDebugLoc(DebugLoc DL, bool BeforePrintingInsn) {
737 if (DL.isUnknown()) return;
738 if (!BeforePrintingInsn) return;
739
740 const LLVMContext &Context = EmissionDetails.MF->getFunction()->getContext();
741
742 if (DL.getScope(Context) != nullptr && PrevDL != DL) {
743 JITEvent_EmittedFunctionDetails::LineStart NextLine;
744 NextLine.Address = getCurrentPCValue();
745 NextLine.Loc = DL;
746 EmissionDetails.LineStarts.push_back(NextLine);
747 }
748
749 PrevDL = DL;
750 }
751
752 static unsigned GetConstantPoolSizeInBytes(MachineConstantPool *MCP,
753 const DataLayout *TD) {
754 const std::vector &Constants = MCP->getConstants();
755 if (Constants.empty()) return 0;
756
757 unsigned Size = 0;
758 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
759 MachineConstantPoolEntry CPE = Constants[i];
760 unsigned AlignMask = CPE.getAlignment() - 1;
761 Size = (Size + AlignMask) & ~AlignMask;
762 Type *Ty = CPE.getType();
763 Size += TD->getTypeAllocSize(Ty);
764 }
765 return Size;
766 }
767
768 void JITEmitter::startFunction(MachineFunction &F) {
769 DEBUG(dbgs() << "JIT: Starting CodeGen of Function "
770 << F.getName() << "\n");
771
772 uintptr_t ActualSize = 0;
773 // Set the memory writable, if it's not already
774 MemMgr->setMemoryWritable();
775
776 if (SizeEstimate > 0) {
777 // SizeEstimate will be non-zero on reallocation attempts.
778 ActualSize = SizeEstimate;
779 }
780
781 BufferBegin = CurBufferPtr = MemMgr->startFunctionBody(F.getFunction(),
782 ActualSize);
783 BufferEnd = BufferBegin+ActualSize;
784 EmittedFunctions[F.getFunction()].FunctionBody = BufferBegin;
785
786 // Ensure the constant pool/jump table info is at least 4-byte aligned.
787 emitAlignment(16);
788
789 emitConstantPool(F.getConstantPool());
790 if (MachineJumpTableInfo *MJTI = F.getJumpTableInfo())
791 initJumpTableInfo(MJTI);
792
793 // About to start emitting the machine code for the function.
794 emitAlignment(std::max(F.getFunction()->getAlignment(), 8U));
795 TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr);
796 EmittedFunctions[F.getFunction()].Code = CurBufferPtr;
797
798 MBBLocations.clear();
799
800 EmissionDetails.MF = &F;
801 EmissionDetails.LineStarts.clear();
802 }
803
804 bool JITEmitter::finishFunction(MachineFunction &F) {
805 if (CurBufferPtr == BufferEnd) {
806 // We must call endFunctionBody before retrying, because
807 // deallocateMemForFunction requires it.
808 MemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr);
809 retryWithMoreMemory(F);
810 return true;
811 }
812
813 if (MachineJumpTableInfo *MJTI = F.getJumpTableInfo())
814 emitJumpTableInfo(MJTI);
815
816 // FnStart is the start of the text, not the start of the constant pool and
817 // other per-function data.
818 uint8_t *FnStart =
819 (uint8_t *)TheJIT->getPointerToGlobalIfAvailable(F.getFunction());
820
821 // FnEnd is the end of the function's machine code.
822 uint8_t *FnEnd = CurBufferPtr;
823
824 if (!Relocations.empty()) {
825 CurFn = F.getFunction();
826 NumRelos += Relocations.size();
827
828 // Resolve the relocations to concrete pointers.
829 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
830 MachineRelocation &MR = Relocations[i];
831 void *ResultPtr = nullptr;
832 if (!MR.letTargetResolve()) {
833 if (MR.isExternalSymbol()) {
834 ResultPtr = TheJIT->getPointerToNamedFunction(MR.getExternalSymbol(),
835 false);
836 DEBUG(dbgs() << "JIT: Map \'" << MR.getExternalSymbol() << "\' to ["
837 << ResultPtr << "]\n");
838
839 // If the target REALLY wants a stub for this function, emit it now.
840 if (MR.mayNeedFarStub()) {
841 ResultPtr = Resolver.getExternalFunctionStub(ResultPtr);
842 }
843 } else if (MR.isGlobalValue()) {
844 ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
845 BufferBegin+MR.getMachineCodeOffset(),
846 MR.mayNeedFarStub());
847 } else if (MR.isIndirectSymbol()) {
848 ResultPtr = getPointerToGVIndirectSym(
849 MR.getGlobalValue(), BufferBegin+MR.getMachineCodeOffset());
850 } else if (MR.isBasicBlock()) {
851 ResultPtr = (void*)getMachineBasicBlockAddress(MR.getBasicBlock());
852 } else if (MR.isConstantPoolIndex()) {
853 ResultPtr =
854 (void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex());
855 } else {
856 assert(MR.isJumpTableIndex());
857 ResultPtr=(void*)getJumpTableEntryAddress(MR.getJumpTableIndex());
858 }
859
860 MR.setResultPointer(ResultPtr);
861 }
862
863 // if we are managing the GOT and the relocation wants an index,
864 // give it one
865 if (MR.isGOTRelative() && MemMgr->isManagingGOT()) {
866 unsigned idx = Resolver.getGOTIndexForAddr(ResultPtr);
867 MR.setGOTIndex(idx);
868 if (((void**)MemMgr->getGOTBase())[idx] != ResultPtr) {
869 DEBUG(dbgs() << "JIT: GOT was out of date for " << ResultPtr
870 << " pointing at " << ((void**)MemMgr->getGOTBase())[idx]
871 << "\n");
872 ((void**)MemMgr->getGOTBase())[idx] = ResultPtr;
873 }
874 }
875 }
876
877 CurFn = nullptr;
878 TheJIT->getJITInfo().relocate(BufferBegin, &Relocations[0],
879 Relocations.size(), MemMgr->getGOTBase());
880 }
881
882 // Update the GOT entry for F to point to the new code.
883 if (MemMgr->isManagingGOT()) {
884 unsigned idx = Resolver.getGOTIndexForAddr((void*)BufferBegin);
885 if (((void**)MemMgr->getGOTBase())[idx] != (void*)BufferBegin) {
886 DEBUG(dbgs() << "JIT: GOT was out of date for " << (void*)BufferBegin
887 << " pointing at " << ((void**)MemMgr->getGOTBase())[idx]
888 << "\n");
889 ((void**)MemMgr->getGOTBase())[idx] = (void*)BufferBegin;
890 }
891 }
892
893 // CurBufferPtr may have moved beyond FnEnd, due to memory allocation for
894 // global variables that were referenced in the relocations.
895 MemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr);
896
897 if (CurBufferPtr == BufferEnd) {
898 retryWithMoreMemory(F);
899 return true;
900 } else {
901 // Now that we've succeeded in emitting the function, reset the
902 // SizeEstimate back down to zero.
903 SizeEstimate = 0;
904 }
905
906 BufferBegin = CurBufferPtr = nullptr;
907 NumBytes += FnEnd-FnStart;
908
909 // Invalidate the icache if necessary.
910 sys::Memory::InvalidateInstructionCache(FnStart, FnEnd-FnStart);
911
912 TheJIT->NotifyFunctionEmitted(*F.getFunction(), FnStart, FnEnd-FnStart,
913 EmissionDetails);
914
915 // Reset the previous debug location.
916 PrevDL = DebugLoc();
917
918 DEBUG(dbgs() << "JIT: Finished CodeGen of [" << (void*)FnStart
919 << "] Function: " << F.getName()
920 << ": " << (FnEnd-FnStart) << " bytes of text, "
921 << Relocations.size() << " relocations\n");
922
923 Relocations.clear();
924 ConstPoolAddresses.clear();
925
926 // Mark code region readable and executable if it's not so already.
927 MemMgr->setMemoryExecutable();
928
929 DEBUG({
930 dbgs() << "JIT: Binary code:\n";
931 uint8_t* q = FnStart;
932 for (int i = 0; q < FnEnd; q += 4, ++i) {
933 if (i == 4)
934 i = 0;
935 if (i == 0)
936 dbgs() << "JIT: " << (long)(q - FnStart) << ": ";
937 bool Done = false;
938 for (int j = 3; j >= 0; --j) {
939 if (q + j >= FnEnd)
940 Done = true;
941 else
942 dbgs() << (unsigned short)q[j];
943 }
944 if (Done)
945 break;
946 dbgs() << ' ';
947 if (i == 3)
948 dbgs() << '\n';
949 }
950 dbgs()<< '\n';
951 });
952
953 if (MMI)
954 MMI->EndFunction();
955
956 return false;
957 }
958
959 void JITEmitter::retryWithMoreMemory(MachineFunction &F) {
960 DEBUG(dbgs() << "JIT: Ran out of space for native code. Reattempting.\n");
961 Relocations.clear(); // Clear the old relocations or we'll reapply them.
962 ConstPoolAddresses.clear();
963 ++NumRetries;
964 deallocateMemForFunction(F.getFunction());
965 // Try again with at least twice as much free space.
966 SizeEstimate = (uintptr_t)(2 * (BufferEnd - BufferBegin));
967
968 for (MachineFunction::iterator MBB = F.begin(), E = F.end(); MBB != E; ++MBB){
969 if (MBB->hasAddressTaken())
970 TheJIT->clearPointerToBasicBlock(MBB->getBasicBlock());
971 }
972 }
973
974 /// deallocateMemForFunction - Deallocate all memory for the specified
975 /// function body. Also drop any references the function has to stubs.
976 /// May be called while the Function is being destroyed inside ~Value().
977 void JITEmitter::deallocateMemForFunction(const Function *F) {
978 ValueMap::iterator
979 Emitted = EmittedFunctions.find(F);
980 if (Emitted != EmittedFunctions.end()) {
981 MemMgr->deallocateFunctionBody(Emitted->second.FunctionBody);
982 TheJIT->NotifyFreeingMachineCode(Emitted->second.Code);
983
984 EmittedFunctions.erase(Emitted);
985 }
986 }
987
988
989 void *JITEmitter::allocateSpace(uintptr_t Size, unsigned Alignment) {
990 if (BufferBegin)
991 return JITCodeEmitter::allocateSpace(Size, Alignment);
992
993 // create a new memory block if there is no active one.
994 // care must be taken so that BufferBegin is invalidated when a
995 // block is trimmed
996 BufferBegin = CurBufferPtr = MemMgr->allocateSpace(Size, Alignment);
997 BufferEnd = BufferBegin+Size;
998 return CurBufferPtr;
999 }
1000
1001 void *JITEmitter::allocateGlobal(uintptr_t Size, unsigned Alignment) {
1002 // Delegate this call through the memory manager.
1003 return MemMgr->allocateGlobal(Size, Alignment);
1004 }
1005
1006 void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
1007 if (TheJIT->getJITInfo().hasCustomConstantPool())
1008 return;
1009
1010 const std::vector &Constants = MCP->getConstants();
1011 if (Constants.empty()) return;
1012
1013 unsigned Size = GetConstantPoolSizeInBytes(MCP, TheJIT->getDataLayout());
1014 unsigned Align = MCP->getConstantPoolAlignment();
1015 ConstantPoolBase = allocateSpace(Size, Align);
1016 ConstantPool = MCP;
1017
1018 if (!ConstantPoolBase) return; // Buffer overflow.
1019
1020 DEBUG(dbgs() << "JIT: Emitted constant pool at [" << ConstantPoolBase
1021 << "] (size: " << Size << ", alignment: " << Align << ")\n");
1022
1023 // Initialize the memory for all of the constant pool entries.
1024 unsigned Offset = 0;
1025 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
1026 MachineConstantPoolEntry CPE = Constants[i];
1027 unsigned AlignMask = CPE.getAlignment() - 1;
1028 Offset = (Offset + AlignMask) & ~AlignMask;
1029
1030 uintptr_t CAddr = (uintptr_t)ConstantPoolBase + Offset;
1031 ConstPoolAddresses.push_back(CAddr);
1032 if (CPE.isMachineConstantPoolEntry()) {
1033 // FIXME: add support to lower machine constant pool values into bytes!
1034 report_fatal_error("Initialize memory with machine specific constant pool"
1035 "entry has not been implemented!");
1036 }
1037 TheJIT->InitializeMemory(CPE.Val.ConstVal, (void*)CAddr);
1038 DEBUG(dbgs() << "JIT: CP" << i << " at [0x";
1039 dbgs().write_hex(CAddr) << "]\n");
1040
1041 Type *Ty = CPE.Val.ConstVal->getType();
1042 Offset += TheJIT->getDataLayout()->getTypeAllocSize(Ty);
1043 }
1044 }
1045
1046 void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) {
1047 if (TheJIT->getJITInfo().hasCustomJumpTables())
1048 return;
1049 if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_Inline)
1050 return;
1051
1052 const std::vector &JT = MJTI->getJumpTables();
1053 if (JT.empty()) return;
1054
1055 unsigned NumEntries = 0;
1056 for (unsigned i = 0, e = JT.size(); i != e; ++i)
1057 NumEntries += JT[i].MBBs.size();
1058
1059 unsigned EntrySize = MJTI->getEntrySize(*TheJIT->getDataLayout());
1060
1061 // Just allocate space for all the jump tables now. We will fix up the actual
1062 // MBB entries in the tables after we emit the code for each block, since then
1063 // we will know the final locations of the MBBs in memory.
1064 JumpTable = MJTI;
1065 JumpTableBase = allocateSpace(NumEntries * EntrySize,
1066 MJTI->getEntryAlignment(*TheJIT->getDataLayout()));
1067 }
1068
1069 void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) {
1070 if (TheJIT->getJITInfo().hasCustomJumpTables())
1071 return;
1072
1073 const std::vector &JT = MJTI->getJumpTables();
1074 if (JT.empty() || !JumpTableBase) return;
1075
1076
1077 switch (MJTI->getEntryKind()) {
1078 case MachineJumpTableInfo::EK_Inline:
1079 return;
1080 case MachineJumpTableInfo::EK_BlockAddress: {
1081 // EK_BlockAddress - Each entry is a plain address of block, e.g.:
1082 // .word LBB123
1083 assert(MJTI->getEntrySize(*TheJIT->getDataLayout()) == sizeof(void*) &&
1084 "Cross JIT'ing?");
1085
1086 // For each jump table, map each target in the jump table to the address of
1087 // an emitted MachineBasicBlock.
1088 intptr_t *SlotPtr = (intptr_t*)JumpTableBase;
1089
1090 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
1091 const std::vector &MBBs = JT[i].MBBs;
1092 // Store the address of the basic block for this jump table slot in the
1093 // memory we allocated for the jump table in 'initJumpTableInfo'
1094 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi)
1095 *SlotPtr++ = getMachineBasicBlockAddress(MBBs[mi]);
1096 }
1097 break;
1098 }
1099
1100 case MachineJumpTableInfo::EK_Custom32:
1101 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
1102 case MachineJumpTableInfo::EK_LabelDifference32: {
1103 assert(MJTI->getEntrySize(*TheJIT->getDataLayout()) == 4&&"Cross JIT'ing?");
1104 // For each jump table, place the offset from the beginning of the table
1105 // to the target address.
1106 int *SlotPtr = (int*)JumpTableBase;
1107
1108 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
1109 const std::vector &MBBs = JT[i].MBBs;
1110 // Store the offset of the basic block for this jump table slot in the
1111 // memory we allocated for the jump table in 'initJumpTableInfo'
1112 uintptr_t Base = (uintptr_t)SlotPtr;
1113 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
1114 uintptr_t MBBAddr = getMachineBasicBlockAddress(MBBs[mi]);
1115 /// FIXME: USe EntryKind instead of magic "getPICJumpTableEntry" hook.
1116 *SlotPtr++ = TheJIT->getJITInfo().getPICJumpTableEntry(MBBAddr, Base);
1117 }
1118 }
1119 break;
1120 }
1121 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
1122 llvm_unreachable(
1123 "JT Info emission not implemented for GPRel64BlockAddress yet.");
1124 }
1125 }
1126
1127 void JITEmitter::startGVStub(const GlobalValue* GV,
1128 unsigned StubSize, unsigned Alignment) {
1129 SavedBufferBegin = BufferBegin;
1130 SavedBufferEnd = BufferEnd;
1131 SavedCurBufferPtr = CurBufferPtr;
1132
1133 BufferBegin = CurBufferPtr = MemMgr->allocateStub(GV, StubSize, Alignment);
1134 BufferEnd = BufferBegin+StubSize+1;
1135 }
1136
1137 void JITEmitter::startGVStub(void *Buffer, unsigned StubSize) {
1138 SavedBufferBegin = BufferBegin;
1139 SavedBufferEnd = BufferEnd;
1140 SavedCurBufferPtr = CurBufferPtr;
1141
1142 BufferBegin = CurBufferPtr = (uint8_t *)Buffer;
1143 BufferEnd = BufferBegin+StubSize+1;
1144 }
1145
1146 void JITEmitter::finishGVStub() {
1147 assert(CurBufferPtr != BufferEnd && "Stub overflowed allocated space.");
1148 NumBytes += getCurrentPCOffset();
1149 BufferBegin = SavedBufferBegin;
1150 BufferEnd = SavedBufferEnd;
1151 CurBufferPtr = SavedCurBufferPtr;
1152 }
1153
1154 void *JITEmitter::allocIndirectGV(const GlobalValue *GV,
1155 const uint8_t *Buffer, size_t Size,
1156 unsigned Alignment) {
1157 uint8_t *IndGV = MemMgr->allocateStub(GV, Size, Alignment);
1158 memcpy(IndGV, Buffer, Size);
1159 return IndGV;
1160 }
1161
1162 // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
1163 // in the constant pool that was last emitted with the 'emitConstantPool'
1164 // method.
1165 //
1166 uintptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const {
1167 assert(ConstantNum < ConstantPool->getConstants().size() &&
1168 "Invalid ConstantPoolIndex!");
1169 return ConstPoolAddresses[ConstantNum];
1170 }
1171
1172 // getJumpTableEntryAddress - Return the address of the JumpTable with index
1173 // 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo'
1174 //
1175 uintptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const {
1176 const std::vector &JT = JumpTable->getJumpTables();
1177 assert(Index < JT.size() && "Invalid jump table index!");
1178
1179 unsigned EntrySize = JumpTable->getEntrySize(*TheJIT->getDataLayout());
1180
1181 unsigned Offset = 0;
1182 for (unsigned i = 0; i < Index; ++i)
1183 Offset += JT[i].MBBs.size();
1184
1185 Offset *= EntrySize;
1186
1187 return (uintptr_t)((char *)JumpTableBase + Offset);
1188 }
1189
1190 void JITEmitter::EmittedFunctionConfig::onDelete(
1191 JITEmitter *Emitter, const Function *F) {
1192 Emitter->deallocateMemForFunction(F);
1193 }
1194 void JITEmitter::EmittedFunctionConfig::onRAUW(
1195 JITEmitter *, const Function*, const Function*) {
1196 llvm_unreachable("The JIT doesn't know how to handle a"
1197 " RAUW on a value it has emitted.");
1198 }
1199
1200
1201 //===----------------------------------------------------------------------===//
1202 // Public interface to this file
1203 //===----------------------------------------------------------------------===//
1204
1205 JITCodeEmitter *JIT::createEmitter(JIT &jit, JITMemoryManager *JMM,
1206 TargetMachine &tm) {
1207 return new JITEmitter(jit, JMM, tm);
1208 }
1209
1210 // getPointerToFunctionOrStub - If the specified function has been
1211 // code-gen'd, return a pointer to the function. If not, compile it, or use
1212 // a stub to implement lazy compilation if available.
1213 //
1214 void *JIT::getPointerToFunctionOrStub(Function *F) {
1215 // If we have already code generated the function, just return the address.
1216 if (void *Addr = getPointerToGlobalIfAvailable(F))
1217 return Addr;
1218
1219 // Get a stub if the target supports it.
1220 JITEmitter *JE = static_cast(getCodeEmitter());
1221 return JE->getJITResolver().getLazyFunctionStub(F);
1222 }
1223
1224 void JIT::updateFunctionStubUnlocked(Function *F) {
1225 // Get the empty stub we generated earlier.
1226 JITEmitter *JE = static_cast(getCodeEmitter());
1227 void *Stub = JE->getJITResolver().getLazyFunctionStub(F);
1228 void *Addr = getPointerToGlobalIfAvailable(F);
1229 assert(Addr != Stub && "Function must have non-stub address to be updated.");
1230
1231 // Tell the target jit info to rewrite the stub at the specified address,
1232 // rather than creating a new one.
1233 TargetJITInfo::StubLayout layout = getJITInfo().getStubLayout();
1234 JE->startGVStub(Stub, layout.Size);
1235 getJITInfo().emitFunctionStub(F, Addr, *getCodeEmitter());
1236 JE->finishGVStub();
1237 }
1238
1239 /// freeMachineCodeForFunction - release machine code memory for given Function.
1240 ///
1241 void JIT::freeMachineCodeForFunction(Function *F) {
1242 // Delete translation for this from the ExecutionEngine, so it will get
1243 // retranslated next time it is used.
1244 updateGlobalMapping(F, nullptr);
1245
1246 // Free the actual memory for the function body and related stuff.
1247 static_cast(JCE)->deallocateMemForFunction(F);
1248 }
+0
-904
lib/ExecutionEngine/JIT/JITMemoryManager.cpp less more
None //===-- JITMemoryManager.cpp - Memory Allocator for JIT'd code ------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the DefaultJITMemoryManager class.
10