llvm.org GIT mirror llvm / d5dd8ce
Reinstate "Nuke the old JIT." Approved by Jim Grosbach, Lang Hames, Rafael Espindola. This reinstates commits r215111, 215115, 215116, 215117, 215136. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216982 91177308-0d34-0410-b5e6-96231b3b80d8 Eric Christopher 5 years ago
259 changed file(s) with 1085 addition(s) and 13491 deletion(s). Raw diff Collapse all Expand all
16681668 $(TARGET:%=$(ObjDir)/%GenMCCodeEmitter.inc.tmp): \
16691669 $(ObjDir)/%GenMCCodeEmitter.inc.tmp: %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
16701670 $(Echo) "Building $(
1671 $(Verb) $(LLVMTableGen) -gen-emitter -mc-emitter -o $(call SYSPATH, $@) $<
1671 $(Verb) $(LLVMTableGen) -gen-emitter -o $(call SYSPATH, $@) $<
16721672
16731673 $(TARGET:%=$(ObjDir)/%GenMCPseudoLowering.inc.tmp): \
16741674 $(ObjDir)/%GenMCPseudoLowering.inc.tmp: %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
16751675 $(Echo) "Building $(
16761676 $(Verb) $(LLVMTableGen) -gen-pseudo-lowering -o $(call SYSPATH, $@) $<
1677
1678 $(TARGET:%=$(ObjDir)/%GenCodeEmitter.inc.tmp): \
1679 $(ObjDir)/%GenCodeEmitter.inc.tmp: %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
1680 $(Echo) "Building $(
1681 $(Verb) $(LLVMTableGen) -gen-emitter -o $(call SYSPATH, $@) $<
16821677
16831678 $(TARGET:%=$(ObjDir)/%GenDAGISel.inc.tmp): \
16841679 $(ObjDir)/%GenDAGISel.inc.tmp : %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
1212
1313 LEVEL := ../../..
1414 LIBRARYNAME := llvm_executionengine
15 UsedComponents := executionengine jit interpreter native
15 UsedComponents := executionengine mcjit interpreter native
1616 UsedOcamlInterfaces := llvm llvm_target
1717
1818 include ../Makefile.ocaml
2626 /* Force the LLVM interpreter and JIT to be linked in. */
2727 void llvm_initialize(void) {
2828 LLVMLinkInInterpreter();
29 LLVMLinkInJIT();
29 LLVMLinkInMCJIT();
3030 }
3131
3232 /* unit -> bool */
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
19651965 // Build engine with JIT
19661966 llvm::EngineBuilder factory(std::move(Owner));
19671967 factory.setEngineKind(llvm::EngineKind::JIT);
1968 factory.setAllocateGVsWithCode(false);
19691968 factory.setTargetOptions(Opts);
19701969 factory.setMCJITMemoryManager(MemMgr);
1971 factory.setUseMCJIT(true);
19721970 llvm::ExecutionEngine *executionEngine = factory.create();
19731971
19741972 {
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"
126125
127126 // Import result of execution:
128127 outs() << "Result: " << gv.IntVal << "\n";
129 EE->freeMachineCodeForFunction(FooF);
130128 delete EE;
131129 llvm_shutdown();
132130 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
138138 /// getMemoryforGV - Allocate memory for a global variable.
139139 virtual char *getMemoryForGV(const GlobalVariable *GV);
140140
141 // To avoid having libexecutionengine depend on the JIT and interpreter
142 // libraries, the execution engine implementations set these functions to ctor
143 // pointers at startup time if they are linked in.
144 static ExecutionEngine *(*JITCtor)(
145 std::unique_ptr M,
146 std::string *ErrorStr,
147 JITMemoryManager *JMM,
148 bool GVsWithCode,
149 TargetMachine *TM);
150141 static ExecutionEngine *(*MCJITCtor)(
151142 std::unique_ptr M,
152143 std::string *ErrorStr,
335326 /// getFunctionAddress instead.
336327 virtual void *getPointerToFunction(Function *F) = 0;
337328
338 /// getPointerToBasicBlock - The different EE's represent basic blocks in
339 /// different ways. Return the representation for a blockaddress of the
340 /// specified block.
341 ///
342 /// This function will not be implemented for the MCJIT execution engine.
343 virtual void *getPointerToBasicBlock(BasicBlock *BB) = 0;
344
345329 /// getPointerToFunctionOrStub - If the specified function has been
346330 /// code-gen'd, return a pointer to the function. If not, compile it, or use
347331 /// a stub to implement lazy compilation if available. See
388372 Type *Ty);
389373
390374 void InitializeMemory(const Constant *Init, void *Addr);
391
392 /// recompileAndRelinkFunction - This method is used to force a function which
393 /// has already been compiled to be compiled again, possibly after it has been
394 /// modified. Then the entry to the old copy is overwritten with a branch to
395 /// the new copy. If there was no old copy, this acts just like
396 /// VM::getPointerToFunction().
397 virtual void *recompileAndRelinkFunction(Function *F) = 0;
398
399 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
400 /// corresponding to the machine code emitted to execute this function, useful
401 /// for garbage-collecting generated code.
402 virtual void freeMachineCodeForFunction(Function *F) = 0;
403375
404376 /// getOrEmitGlobalVariable - Return the address of the specified global
405377 /// variable, possibly emitting it to memory if needed. This is used by the
537509 CodeGenOpt::Level OptLevel;
538510 RTDyldMemoryManager *MCJMM;
539511 JITMemoryManager *JMM;
540 bool AllocateGVsWithCode;
541512 TargetOptions Options;
542513 Reloc::Model RelocModel;
543514 CodeModel::Model CMModel;
544515 std::string MArch;
545516 std::string MCPU;
546517 SmallVector MAttrs;
547 bool UseMCJIT;
548518 bool VerifyModules;
549519
550520 /// InitEngine - Does the common initialization of default options.
625595 return *this;
626596 }
627597
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
640598 /// setMArch - Override the architecture set by the Module's triple.
641599 EngineBuilder &setMArch(StringRef march) {
642600 MArch.assign(march.begin(), march.end());
646604 /// setMCPU - Target a specific cpu type.
647605 EngineBuilder &setMCPU(StringRef mcpu) {
648606 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;
656607 return *this;
657608 }
658609
+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
837837 return UseUnderscoreLongJmp;
838838 }
839839
840 /// Return whether the target can generate code for jump tables.
841 bool supportJumpTables() const {
842 return SupportJumpTables;
843 }
844
845840 /// Return integer threshold on number of blocks to use jump tables rather
846841 /// than if sequence.
847842 int getMinimumJumpTableEntries() const {
10281023 /// llvm.longjmp or the version without _. Defaults to false.
10291024 void setUseUnderscoreLongJmp(bool Val) {
10301025 UseUnderscoreLongJmp = Val;
1031 }
1032
1033 /// Indicate whether the target can generate code for jump tables.
1034 void setSupportJumpTables(bool Val) {
1035 SupportJumpTables = Val;
10361026 }
10371027
10381028 /// Indicate the number of blocks to generate jump tables rather than if
15471537 ///
15481538 /// Defaults to false.
15491539 bool UseUnderscoreLongJmp;
1550
1551 /// Whether the target can generate code for jumptables. If it's not true,
1552 /// then each jumptable must be lowered into if-then-else's.
1553 bool SupportJumpTables;
15541540
15551541 /// Number of blocks threshold to use jump tables.
15561542 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.
3333 * @{
3434 */
3535
36 void LLVMLinkInJIT(void);
3736 void LLVMLinkInMCJIT(void);
3837 void LLVMLinkInInterpreter(void);
3938
188188
189189 bool BasicTTI::shouldBuildLookupTables() const {
190190 const TargetLoweringBase *TLI = getTLI();
191 return TLI->supportJumpTables() &&
192 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
193 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
191 return TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
192 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other);
194193 }
195194
196195 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
22292229 }
22302230
22312231 static inline bool areJTsAllowed(const TargetLowering &TLI) {
2232 return TLI.supportJumpTables() &&
2233 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
2234 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
2232 return TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
2233 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other);
22352234 }
22362235
22372236 static APInt ComputeRange(const APInt &First, const APInt &Last) {
719719 PrefLoopAlignment = 0;
720720 MinStackArgumentAlignment = 1;
721721 InsertFencesForAtomic = false;
722 SupportJumpTables = true;
723722 MinimumJumpTableEntries = 4;
724723
725724 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 std::unique_ptr M,
52 std::string *ErrorStr,
53 JITMemoryManager *JMM,
54 bool GVsWithCode,
55 TargetMachine *TM) = nullptr;
5650 ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
5751 std::unique_ptrM,
5852 std::string *ErrorStr,
410404 MCJMM = nullptr;
411405 JMM = nullptr;
412406 Options = TargetOptions();
413 AllocateGVsWithCode = false;
414407 RelocModel = Reloc::Default;
415408 CMModel = CodeModel::JITDefault;
416 UseMCJIT = false;
417409
418410 // IR module verification is enabled by default in debug builds, and disabled
419411 // by default in release builds.
446438 return nullptr;
447439 }
448440 }
449
450 if (MCJMM && ! UseMCJIT) {
451 if (ErrorStr)
452 *ErrorStr =
453 "Cannot create a legacy JIT with a runtime dyld memory "
454 "manager.";
455 return nullptr;
456 }
457441
458442 // Unless the interpreter was explicitly selected or the JIT is not linked,
459443 // try making a JIT.
466450 }
467451
468452 ExecutionEngine *EE = nullptr;
469 if (UseMCJIT && ExecutionEngine::MCJITCtor)
453 if (ExecutionEngine::MCJITCtor)
470454 EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr,
471455 MCJMM ? MCJMM : JMM, TheTM.release());
472 else if (ExecutionEngine::JITCtor)
473 EE = ExecutionEngine::JITCtor(std::move(M), ErrorStr, JMM,
474 AllocateGVsWithCode, TheTM.release());
475
476456 if (EE) {
477457 EE->setVerifyModules(VerifyModules);
478458 return EE;
489469 return nullptr;
490470 }
491471
492 if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::JITCtor &&
493 !ExecutionEngine::MCJITCtor) {
472 if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::MCJITCtor) {
494473 if (ErrorStr)
495474 *ErrorStr = "JIT has not been linked in.";
496475 }
836815 Result = PTOGV(getPointerToFunctionOrStub(const_cast(F)));
837816 else if (const GlobalVariable *GV = dyn_cast(C))
838817 Result = PTOGV(getOrEmitGlobalVariable(const_cast(GV)));
839 else if (const BlockAddress *BA = dyn_cast(C))
840 Result = PTOGV(getPointerToBasicBlock(const_cast(
841 BA->getBasicBlock())));
842818 else
843819 llvm_unreachable("Unknown constant pointer type!");
844820 break;
183183 EngineBuilder builder(std::unique_ptr(unwrap(M)));
184184 builder.setEngineKind(EngineKind::JIT)
185185 .setErrorStr(&Error)
186 .setUseMCJIT(true)
187186 .setOptLevel((CodeGenOpt::Level)options.OptLevel)
188187 .setCodeModel(unwrap(options.CodeModel))
189188 .setTargetOptions(targetOptions);
266265 }
267266
268267 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
269 unwrap(EE)->freeMachineCodeForFunction(unwrap(F));
270268 }
271269
272270 void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
305303
306304 void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE,
307305 LLVMValueRef Fn) {
308 return unwrap(EE)->recompileAndRelinkFunction(unwrap(Fn));
306 return nullptr;
309307 }
310308
311309 LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
120120 // FIXME: not implemented.
121121 return nullptr;
122122 }
123
124 /// recompileAndRelinkFunction - For the interpreter, functions are always
125 /// up-to-date.
126 ///
127 void *recompileAndRelinkFunction(Function *F) override {
128 return getPointerToFunction(F);
129 }
130
131 /// freeMachineCodeForFunction - The interpreter does not generate any code.
132 ///
133 void freeMachineCodeForFunction(Function *F) override { }
134123
135124 // Methods used to execute code:
136125 // Place a call on the stack
213202 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
214203
215204 void *getPointerToFunction(Function *F) override { return (void*)F; }
216 void *getPointerToBasicBlock(BasicBlock *BB) override { return (void*)BB; }
217205
218206 void initializeExecutionEngine() { }
219207 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
-694
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 /// This is the factory method for creating a JIT for the current machine, it
72 /// does not fall back to the interpreter.
73 ExecutionEngine *JIT::createJIT(std::unique_ptr M,
74 std::string *ErrorStr,
75 JITMemoryManager *JMM,
76 bool GVsWithCode,
77 TargetMachine *TM) {
78 // Try to register the program as a source of symbols to resolve against.
79 //
80 // FIXME: Don't do this here.
81 sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
82
83 // If the target supports JIT code generation, create the JIT.
84 if (TargetJITInfo *TJ = TM->getSubtargetImpl()->getJITInfo()) {
85 return new JIT(std::move(M), *TM, *TJ, JMM, GVsWithCode);
86 } else {
87 if (ErrorStr)
88 *ErrorStr = "target does not support JIT code generation";
89 return nullptr;
90 }
91 }
92
93 namespace {
94 /// This class supports the global getPointerToNamedFunction(), which allows
95 /// bugpoint or gdb users to search for a function by name without any context.
96 class JitPool {
97 SmallPtrSet JITs; // Optimize for process containing just 1 JIT.
98 mutable sys::Mutex Lock;
99 public:
100 void Add(JIT *jit) {
101 MutexGuard guard(Lock);
102 JITs.insert(jit);
103 }
104 void Remove(JIT *jit) {
105 MutexGuard guard(Lock);
106 JITs.erase(jit);
107 }
108 void *getPointerToNamedFunction(const char *Name) const {
109 MutexGuard guard(Lock);
110 assert(JITs.size() != 0 && "No Jit registered");
111 //search function in every instance of JIT
112 for (JIT *Jit : JITs) {
113 if (Function *F = Jit->FindFunctionNamed(Name))
114 return Jit->getPointerToFunction(F);
115 }
116 // The function is not available : fallback on the first created (will
117 // search in symbol of the current program/library)
118 return (*JITs.begin())->getPointerToNamedFunction(Name);
119 }
120 };
121 ManagedStatic AllJits;
122 }
123 extern "C" {
124 // getPointerToNamedFunction - This function is used as a global wrapper to
125 // JIT::getPointerToNamedFunction for the purpose of resolving symbols when
126 // bugpoint is debugging the JIT. In that scenario, we are loading an .so and
127 // need to resolve function(s) that are being mis-codegenerated, so we need to
128 // resolve their addresses at runtime, and this is the way to do it.
129 void *getPointerToNamedFunction(const char *Name) {
130 return AllJits->getPointerToNamedFunction(Name);
131 }
132 }
133
134 JIT::JIT(std::unique_ptr M, TargetMachine &tm, TargetJITInfo &tji,
135 JITMemoryManager *jmm, bool GVsWithCode)
136 : ExecutionEngine(std::move(M)), TM(tm), TJI(tji),
137 JMM(jmm ? jmm : JITMemoryManager::CreateDefaultMemManager()),
138 AllocateGVsWithCode(GVsWithCode), isAlreadyCodeGenerating(false) {
139 setDataLayout(TM.getSubtargetImpl()->getDataLayout());
140
141 Module *Mod = Modules.back().get();
142 jitstate = new JITState(Mod);
143
144 // Initialize JCE
145 JCE = createEmitter(*this, JMM, TM);
146
147 // Register in global list of all JITs.
148 AllJits->Add(this);
149
150 // Add target data
151 MutexGuard locked(lock);
152 FunctionPassManager &PM = jitstate->getPM();
153 Mod->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
154 PM.add(new DataLayoutPass(Mod));
155
156 // Turn the machine code intermediate representation into bytes in memory that
157 // may be executed.
158 if (TM.addPassesToEmitMachineCode(PM, *JCE, !getVerifyModules())) {
159 report_fatal_error("Target does not support machine code emission!");
160 }
161
162 // Initialize passes.
163 PM.doInitialization();
164 }
165
166 JIT::~JIT() {
167 // Cleanup.
168 AllJits->Remove(this);
169 delete jitstate;
170 delete JCE;
171 // JMM is a ownership of JCE, so we no need delete JMM here.
172 delete &TM;
173 }
174
175 /// Add a new Module to the JIT. If we previously removed the last Module, we
176 /// need re-initialize jitstate with a valid Module.
177 void JIT::addModule(std::unique_ptr M) {
178 MutexGuard locked(lock);
179
180 if (Modules.empty()) {
181 assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
182
183 jitstate = new JITState(M.get());
184
185 FunctionPassManager &PM = jitstate->getPM();
186 M->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
187 PM.add(new DataLayoutPass(M.get()));
188
189 // Turn the machine code intermediate representation into bytes in memory
190 // that may be executed.
191 if (TM.addPassesToEmitMachineCode(PM, *JCE, !getVerifyModules())) {
192 report_fatal_error("Target does not support machine code emission!");
193 }
194
195 // Initialize passes.
196 PM.doInitialization();
197 }
198
199 ExecutionEngine::addModule(std::move(M));
200 }
201
202 /// If we are removing the last Module, invalidate the jitstate since the
203 /// PassManager it contains references a released Module.
204 bool JIT::removeModule(Module *M) {
205 bool result = ExecutionEngine::removeModule(M);
206
207 MutexGuard locked(lock);
208
209 if (jitstate && jitstate->getModule() == M) {
210 delete jitstate;
211 jitstate = nullptr;
212 }
213
214 if (!jitstate && !Modules.empty()) {
215 jitstate = new JITState(Modules[0].get());
216
217 FunctionPassManager &PM = jitstate->getPM();
218 M->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
219 PM.add(new DataLayoutPass(M));
220
221 // Turn the machine code intermediate representation into bytes in memory
222 // that may be executed.
223 if (TM.addPassesToEmitMachineCode(PM, *JCE, !getVerifyModules())) {
224 report_fatal_error("Target does not support machine code emission!");
225 }
226
227 // Initialize passes.
228 PM.doInitialization();
229 }
230 return result;
231 }
232
233 /// run - Start execution with the specified function and arguments.
234 ///
235 GenericValue JIT::runFunction(Function *F,
236 const std::vector &ArgValues) {
237 assert(F && "Function *F was null at entry to run()");
238
239 void *FPtr = getPointerToFunction(F);
240 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
241 FunctionType *FTy = F->getFunctionType();
242 Type *RetTy = FTy->getReturnType();
243
244 assert((FTy->getNumParams() == ArgValues.size() ||
245 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
246 "Wrong number of arguments passed into function!");
247 assert(FTy->getNumParams() == ArgValues.size() &&
248 "This doesn't support passing arguments through varargs (yet)!");
249
250 // Handle some common cases first. These cases correspond to common `main'
251 // prototypes.
252 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
253 switch (ArgValues.size()) {
254 case 3:
255 if (FTy->getParamType(0)->isIntegerTy(32) &&
256 FTy->getParamType(1)->isPointerTy() &&
257 FTy->getParamType(2)->isPointerTy()) {
258 int (*PF)(int, char **, const char **) =
259 (int(*)(int, char **, const char **))(intptr_t)FPtr;
260
261 // Call the function.
262 GenericValue rv;
263 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
264 (char **)GVTOP(ArgValues[1]),
265 (const char **)GVTOP(ArgValues[2])));
266 return rv;
267 }
268 break;
269 case 2:
270 if (FTy->getParamType(0)->isIntegerTy(32) &&
271 FTy->getParamType(1)->isPointerTy()) {
272 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
273
274 // Call the function.
275 GenericValue rv;
276 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
277 (char **)GVTOP(ArgValues[1])));
278 return rv;
279 }
280 break;
281 case 1:
282 if (FTy->getParamType(0)->isIntegerTy(32)) {
283 GenericValue rv;
284 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
285 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
286 return rv;
287 }
288 if (FTy->getParamType(0)->isPointerTy()) {
289 GenericValue rv;
290 int (*PF)(char *) = (int(*)(char *))(intptr_t)FPtr;
291 rv.IntVal = APInt(32, PF((char*)GVTOP(ArgValues[0])));
292 return rv;
293 }
294 break;
295 }
296 }
297
298 // Handle cases where no arguments are passed first.
299 if (ArgValues.empty()) {
300 GenericValue rv;
301 switch (RetTy->getTypeID()) {
302 default: llvm_unreachable("Unknown return type for function call!");
303 case Type::IntegerTyID: {
304 unsigned BitWidth = cast(RetTy)->getBitWidth();
305 if (BitWidth == 1)
306 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
307 else if (BitWidth <= 8)
308 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
309 else if (BitWidth <= 16)
310 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
311 else if (BitWidth <= 32)
312 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
313 else if (BitWidth <= 64)
314 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
315 else
316 llvm_unreachable("Integer types > 64 bits not supported");
317 return rv;
318 }
319 case Type::VoidTyID:
320 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
321 return rv;
322 case Type::FloatTyID:
323 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
324 return rv;
325 case Type::DoubleTyID:
326 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
327 return rv;
328 case Type::X86_FP80TyID:
329 case Type::FP128TyID:
330 case Type::PPC_FP128TyID:
331 llvm_unreachable("long double not supported yet");
332 case Type::PointerTyID:
333 return PTOGV(((void*(*)())(intptr_t)FPtr)());
334 }
335 }
336
337 // Okay, this is not one of our quick and easy cases. Because we don't have a
338 // full FFI, we have to codegen a nullary stub function that just calls the
339 // function we are interested in, passing in constants for all of the
340 // arguments. Make this function and return.
341
342 // First, create the function.
343 FunctionType *STy=FunctionType::get(RetTy, false);
344 Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
345 F->getParent());
346
347 // Insert a basic block.
348 BasicBlock *StubBB = BasicBlock::Create(F->getContext(), "", Stub);
349
350 // Convert all of the GenericValue arguments over to constants. Note that we
351 // currently don't support varargs.
352 SmallVector Args;
353 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
354 Constant *C = nullptr;
355 Type *ArgTy = FTy->getParamType(i);
356 const GenericValue &AV = ArgValues[i];
357 switch (ArgTy->getTypeID()) {
358 default: llvm_unreachable("Unknown argument type for function call!");
359 case Type::IntegerTyID:
360 C = ConstantInt::get(F->getContext(), AV.IntVal);
361 break;
362 case Type::FloatTyID:
363 C = ConstantFP::get(F->getContext(), APFloat(AV.FloatVal));
364 break;
365 case Type::DoubleTyID:
366 C = ConstantFP::get(F->getContext(), APFloat(AV.DoubleVal));
367 break;
368 case Type::PPC_FP128TyID:
369 case Type::X86_FP80TyID:
370 case Type::FP128TyID:
371 C = ConstantFP::get(F->getContext(), APFloat(ArgTy->getFltSemantics(),
372 AV.IntVal));
373 break;
374 case Type::PointerTyID:
375 void *ArgPtr = GVTOP(AV);
376 if (sizeof(void*) == 4)
377 C = ConstantInt::get(Type::getInt32Ty(F->getContext()),
378 (int)(intptr_t)ArgPtr);
379 else
380 C = ConstantInt::get(Type::getInt64Ty(F->getContext()),
381 (intptr_t)ArgPtr);
382 // Cast the integer to pointer
383 C = ConstantExpr::getIntToPtr(C, ArgTy);
384 break;
385 }
386 Args.push_back(C);
387 }
388
389 CallInst *TheCall = CallInst::Create(F, Args, "", StubBB);
390 TheCall->setCallingConv(F->getCallingConv());
391 TheCall->setTailCall();
392 if (!TheCall->getType()->isVoidTy())
393 // Return result of the call.
394 ReturnInst::Create(F->getContext(), TheCall, StubBB);
395 else
396 ReturnInst::Create(F->getContext(), StubBB); // Just return void.
397
398 // Finally, call our nullary stub function.
399 GenericValue Result = runFunction(Stub, std::vector());
400 // Erase it, since no other function can have a reference to it.
401 Stub->eraseFromParent();
402 // And return the result.
403 return Result;
404 }
405
406 void JIT::RegisterJITEventListener(JITEventListener *L) {
407 if (!L)
408 return;
409 MutexGuard locked(lock);
410 EventListeners.push_back(L);
411 }
412 void JIT::UnregisterJITEventListener(JITEventListener *L) {
413 if (!L)
414 return;
415 MutexGuard locked(lock);
416 std::vector::reverse_iterator I=
417 std::find(EventListeners.rbegin(), EventListeners.rend(), L);
418 if (I != EventListeners.rend()) {
419 std::swap(*I, EventListeners.back());
420 EventListeners.pop_back();
421 }
422 }
423 void JIT::NotifyFunctionEmitted(
424 const Function &F,
425 void *Code, size_t Size,
426 const JITEvent_EmittedFunctionDetails &Details) {
427 MutexGuard locked(lock);
428 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
429 EventListeners[I]->NotifyFunctionEmitted(F, Code, Size, Details);
430 }
431 }
432
433 void JIT::NotifyFreeingMachineCode(void *OldPtr) {
434 MutexGuard locked(lock);
435 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
436 EventListeners[I]->NotifyFreeingMachineCode(OldPtr);
437 }
438 }
439
440 /// runJITOnFunction - Run the FunctionPassManager full of
441 /// just-in-time compilation passes on F, hopefully filling in
442 /// GlobalAddress[F] with the address of F's machine code.
443 ///
444 void JIT::runJITOnFunction(Function *F, MachineCodeInfo *MCI) {
445 MutexGuard locked(lock);
446
447 class MCIListener : public JITEventListener {
448 MachineCodeInfo *const MCI;
449 public:
450 MCIListener(MachineCodeInfo *mci) : MCI(mci) {}
451 void NotifyFunctionEmitted(const Function &, void *Code, size_t Size,
452 const EmittedFunctionDetails &) override {
453 MCI->setAddress(Code);
454 MCI->setSize(Size);
455 }
456 };
457 MCIListener MCIL(MCI);
458 if (MCI)
459 RegisterJITEventListener(&MCIL);
460
461 runJITOnFunctionUnlocked(F);
462
463 if (MCI)
464 UnregisterJITEventListener(&MCIL);
465 }
466
467 void JIT::runJITOnFunctionUnlocked(Function *F) {
468 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
469
470 jitTheFunctionUnlocked(F);
471
472 // If the function referred to another function that had not yet been
473 // read from bitcode, and we are jitting non-lazily, emit it now.
474 while (!jitstate->getPendingFunctions().empty()) {
475 Function *PF = jitstate->getPendingFunctions().back();
476 jitstate->getPendingFunctions().pop_back();
477
478 assert(!PF->hasAvailableExternallyLinkage() &&
479 "Externally-defined function should not be in pending list.");
480
481 jitTheFunctionUnlocked(PF);
482
483 // Now that the function has been jitted, ask the JITEmitter to rewrite
484 // the stub with real address of the function.
485 updateFunctionStubUnlocked(PF);
486 }
487 }
488
489 void JIT::jitTheFunctionUnlocked(Function *F) {
490 isAlreadyCodeGenerating = true;
491 jitstate->getPM().run(*F);
492 isAlreadyCodeGenerating = false;
493
494 // clear basic block addresses after this function is done
495 getBasicBlockAddressMap().clear();
496 }
497
498 /// getPointerToFunction - This method is used to get the address of the
499 /// specified function, compiling it if necessary.
500 ///
501 void *JIT::getPointerToFunction(Function *F) {
502
503 if (void *Addr = getPointerToGlobalIfAvailable(F))
504 return Addr; // Check if function already code gen'd
505
506 MutexGuard locked(lock);
507
508 // Now that this thread owns the lock, make sure we read in the function if it
509 // exists in this Module.
510 std::string ErrorMsg;
511 if (F->Materialize(&ErrorMsg)) {
512 report_fatal_error("Error reading function '" + F->getName()+
513 "' from bitcode file: " + ErrorMsg);
514 }
515
516 // ... and check if another thread has already code gen'd the function.
517 if (void *Addr = getPointerToGlobalIfAvailable(F))
518 return Addr;
519
520 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
521 bool AbortOnFailure = !F->hasExternalWeakLinkage();
522 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
523 addGlobalMapping(F, Addr);
524 return Addr;
525 }
526
527 runJITOnFunctionUnlocked(F);
528
529 void *Addr = getPointerToGlobalIfAvailable(F);
530 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
531 return Addr;
532 }
533
534 void JIT::addPointerToBasicBlock(const BasicBlock *BB, void *Addr) {
535 MutexGuard locked(lock);
536
537 BasicBlockAddressMapTy::iterator I =
538 getBasicBlockAddressMap().find(BB);
539 if (I == getBasicBlockAddressMap().end()) {
540 getBasicBlockAddressMap()[BB] = Addr;
541 } else {
542 // ignore repeats: some BBs can be split into few MBBs?
543 }
544 }
545
546 void JIT::clearPointerToBasicBlock(const BasicBlock *BB) {
547 MutexGuard locked(lock);
548 getBasicBlockAddressMap().erase(BB);
549 }
550
551 void *JIT::getPointerToBasicBlock(BasicBlock *BB) {
552 // make sure it's function is compiled by JIT
553 (void)getPointerToFunction(BB->getParent());
554
555 // resolve basic block address
556 MutexGuard locked(lock);
557
558 BasicBlockAddressMapTy::iterator I =
559 getBasicBlockAddressMap().find(BB);
560 if (I != getBasicBlockAddressMap().end()) {
561 return I->second;
562 } else {
563 llvm_unreachable("JIT does not have BB address for address-of-label, was"
564 " it eliminated by optimizer?");
565 }
566 }
567
568 void *JIT::getPointerToNamedFunction(const std::string &Name,
569 bool AbortOnFailure){
570 if (!isSymbolSearchingDisabled()) {
571 void *ptr = JMM->getPointerToNamedFunction(Name, false);
572 if (ptr)
573 return ptr;
574 }
575
576 /// If a LazyFunctionCreator is installed, use it to get/create the function.
577 if (LazyFunctionCreator)
578 if (void *RP = LazyFunctionCreator(Name))
579 return RP;
580
581 if (AbortOnFailure) {
582 report_fatal_error("Program used external function '"+Name+
583 "' which could not be resolved!");
584 }
585 return nullptr;
586 }
587
588
589 /// getOrEmitGlobalVariable - Return the address of the specified global
590 /// variable, possibly emitting it to memory if needed. This is used by the
591 /// Emitter.
592 void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
593 MutexGuard locked(lock);
594
595 void *Ptr = getPointerToGlobalIfAvailable(GV);
596 if (Ptr) return Ptr;
597
598 // If the global is external, just remember the address.
599 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage()) {
600 #if HAVE___DSO_HANDLE
601 if (GV->getName() == "__dso_handle")
602 return (void*)&__dso_handle;
603 #endif
604 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName());
605 if (!Ptr) {
606 report_fatal_error("Could not resolve external global address: "
607 +GV->getName());
608 }
609 addGlobalMapping(GV, Ptr);
610 } else {
611 // If the global hasn't been emitted to memory yet, allocate space and
612 // emit it into memory.
613 Ptr = getMemoryForGV(GV);
614 addGlobalMapping(GV, Ptr);
615 EmitGlobalVariable(GV); // Initialize the variable.
616 }
617 return Ptr;
618 }
619
620 /// recompileAndRelinkFunction - This method is used to force a function
621 /// which has already been compiled, to be compiled again, possibly
622 /// after it has been modified. Then the entry to the old copy is overwritten
623 /// with a branch to the new copy. If there was no old copy, this acts
624 /// just like JIT::getPointerToFunction().
625 ///
626 void *JIT::recompileAndRelinkFunction(Function *F) {
627 void *OldAddr = getPointerToGlobalIfAvailable(F);
628
629 // If it's not already compiled there is no reason to patch it up.
630 if (!OldAddr) return getPointerToFunction(F);
631
632 // Delete the old function mapping.
633 addGlobalMapping(F, nullptr);
634
635 // Recodegen the function
636 runJITOnFunction(F);
637
638 // Update state, forward the old function to the new function.
639 void *Addr = getPointerToGlobalIfAvailable(F);
640 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
641 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
642 return Addr;
643 }
644
645 /// getMemoryForGV - This method abstracts memory allocation of global
646 /// variable so that the JIT can allocate thread local variables depending
647 /// on the target.
648 ///
649 char* JIT::getMemoryForGV(const GlobalVariable* GV) {
650 char *Ptr;
651
652 // GlobalVariable's which are not "constant" will cause trouble in a server
653 // situation. It's returned in the same block of memory as code which may
654 // not be writable.
655 if (isGVCompilationDisabled() && !GV->isConstant()) {
656 report_fatal_error("Compilation of non-internal GlobalValue is disabled!");
657 }
658
659 // Some applications require globals and code to live together, so they may
660 // be allocated into the same buffer, but in general globals are allocated
661 // through the memory manager which puts them near the code but not in the
662 // same buffer.
663 Type *GlobalType = GV->getType()->getElementType();
664 size_t S = getDataLayout()->getTypeAllocSize(GlobalType);
665 size_t A = getDataLayout()->getPreferredAlignment(GV);
666 if (GV->isThreadLocal()) {
667 MutexGuard locked(lock);
668 Ptr = TJI.allocateThreadLocalMemory(S);
669 } else if (TJI.allocateSeparateGVMemory()) {
670 if (A <= 8) {
671 Ptr = (char*)malloc(S);
672 } else {
673 // Allocate S+A bytes of memory, then use an aligned pointer within that
674 // space.
675 Ptr = (char*)malloc(S+A);
676 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
677 Ptr = Ptr + (MisAligned ? (A-MisAligned) : 0);
678 }
679 } else if (AllocateGVsWithCode) {
680 Ptr = (char*)JCE->allocateSpace(S, A);
681 } else {
682 Ptr = (char*)JCE->allocateGlobal(S, A);
683 }
684 return Ptr;
685 }
686
687 void JIT::addPendingFunction(Function *F) {
688 MutexGuard locked(lock);
689 jitstate->getPendingFunctions().push_back(F);
690 }
691
692
693 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 LLVM_LIB_EXECUTIONENGINE_JIT_JIT_H
14 #define LLVM_LIB_EXECUTIONENGINE_JIT_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(std::unique_ptr 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(std::unique_ptr 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(std::unique_ptr 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
-1248
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 (void *C : F2C->second) {
465 S2RMap.UnregisterStubResolver(C);
466 bool Erased = CallSiteToFunctionMap.erase(C);
467 (void)Erased;
468 assert(Erased && "Missing call site->function mapping");
469 }
470 FunctionToCallSitesMap.erase(F2C);
471 }
472
473 void JITResolverState::EraseAllCallSitesPrelocked() {
474 StubToResolverMapTy &S2RMap = *StubToResolverMap;
475 for (CallSiteToFunctionMapTy::const_iterator
476 I = CallSiteToFunctionMap.begin(),
477 E = CallSiteToFunctionMap.end(); I != E; ++I) {
478 S2RMap.UnregisterStubResolver(I->first);
479 }
480 CallSiteToFunctionMap.clear();
481 FunctionToCallSitesMap.clear();
482 }
483
484 JITResolver::~JITResolver() {
485 // No need to lock because we're in the destructor, and state isn't shared.
486 state.EraseAllCallSitesPrelocked();
487 assert(!StubToResolverMap->ResolverHasStubs(this) &&
488 "Resolver destroyed with stubs still alive.");
489 }
490
491 /// getLazyFunctionStubIfAvailable - This returns a pointer to a function stub
492 /// if it has already been created.
493 void *JITResolver::getLazyFunctionStubIfAvailable(Function *F) {
494 MutexGuard locked(TheJIT->lock);
495
496 // If we already have a stub for this function, recycle it.
497 return state.getFunctionToLazyStubMap().lookup(F);
498 }
499
500 /// getFunctionStub - This returns a pointer to a function stub, creating
501 /// one on demand as needed.
502 void *JITResolver::getLazyFunctionStub(Function *F) {
503 MutexGuard locked(TheJIT->lock);
504
505 // If we already have a lazy stub for this function, recycle it.
506 void *&Stub = state.getFunctionToLazyStubMap()[F];
507 if (Stub) return Stub;
508
509 // Call the lazy resolver function if we are JIT'ing lazily. Otherwise we
510 // must resolve the symbol now.
511 void *Actual = TheJIT->isCompilingLazily()
512 ? (void *)(intptr_t)LazyResolverFn : (void *)nullptr;
513
514 // If this is an external declaration, attempt to resolve the address now
515 // to place in the stub.
516 if (isNonGhostDeclaration(F) || F->hasAvailableExternallyLinkage()) {
517 Actual = TheJIT->getPointerToFunction(F);
518
519 // If we resolved the symbol to a null address (eg. a weak external)
520 // don't emit a stub. Return a null pointer to the application.
521 if (!Actual) return nullptr;
522 }
523
524 TargetJITInfo::StubLayout SL = TheJIT->getJITInfo().getStubLayout();
525 JE.startGVStub(F, SL.Size, SL.Alignment);
526 // Codegen a new stub, calling the lazy resolver or the actual address of the
527 // external function, if it was resolved.
528 Stub = TheJIT->getJITInfo().emitFunctionStub(F, Actual, JE);
529 JE.finishGVStub();
530
531 if (Actual != (void*)(intptr_t)LazyResolverFn) {
532 // If we are getting the stub for an external function, we really want the
533 // address of the stub in the GlobalAddressMap for the JIT, not the address
534 // of the external function.
535 TheJIT->updateGlobalMapping(F, Stub);
536 }
537
538 DEBUG(dbgs() << "JIT: Lazy stub emitted at [" << Stub << "] for function '"
539 << F->getName() << "'\n");
540
541 if (TheJIT->isCompilingLazily()) {
542 // Register this JITResolver as the one corresponding to this call site so
543 // JITCompilerFn will be able to find it.
544 StubToResolverMap->RegisterStubResolver(Stub, this);
545
546 // Finally, keep track of the stub-to-Function mapping so that the
547 // JITCompilerFn knows which function to compile!
548 state.AddCallSite(Stub, F);
549 } else if (!Actual) {
550 // If we are JIT'ing non-lazily but need to call a function that does not
551 // exist yet, add it to the JIT's work list so that we can fill in the
552 // stub address later.
553 assert(!isNonGhostDeclaration(F) && !F->hasAvailableExternallyLinkage() &&
554 "'Actual' should have been set above.");
555 TheJIT->addPendingFunction(F);
556 }
557
558 return Stub;
559 }
560
561 /// getGlobalValueIndirectSym - Return a lazy pointer containing the specified
562 /// GV address.
563 void *JITResolver::getGlobalValueIndirectSym(GlobalValue *GV, void *GVAddress) {
564 MutexGuard locked(TheJIT->lock);
565
566 // If we already have a stub for this global variable, recycle it.
567 void *&IndirectSym = state.getGlobalToIndirectSymMap()[GV];
568 if (IndirectSym) return IndirectSym;
569
570 // Otherwise, codegen a new indirect symbol.
571 IndirectSym = TheJIT->getJITInfo().emitGlobalValueIndirectSym(GV, GVAddress,
572 JE);
573
574 DEBUG(dbgs() << "JIT: Indirect symbol emitted at [" << IndirectSym
575 << "] for GV '" << GV->getName() << "'\n");
576
577 return IndirectSym;
578 }
579
580 /// getExternalFunctionStub - Return a stub for the function at the
581 /// specified address, created lazily on demand.
582 void *JITResolver::getExternalFunctionStub(void *FnAddr) {
583 // If we already have a stub for this function, recycle it.
584 void *&Stub = ExternalFnToStubMap[FnAddr];
585 if (Stub) return Stub;
586
587 TargetJITInfo::StubLayout SL = TheJIT->getJITInfo().getStubLayout();
588 JE.startGVStub(nullptr, SL.Size, SL.Alignment);
589 Stub = TheJIT->getJITInfo().emitFunctionStub(nullptr, FnAddr, JE);
590 JE.finishGVStub();
591
592 DEBUG(dbgs() << "JIT: Stub emitted at [" << Stub
593 << "] for external function at '" << FnAddr << "'\n");
594 return Stub;
595 }
596
597 unsigned JITResolver::getGOTIndexForAddr(void* addr) {
598 unsigned idx = revGOTMap[addr];
599 if (!idx) {
600 idx = ++nextGOTIndex;
601 revGOTMap[addr] = idx;
602 DEBUG(dbgs() << "JIT: Adding GOT entry " << idx << " for addr ["
603 << addr << "]\n");
604 }
605 return idx;
606 }
607
608 /// JITCompilerFn - This function is called when a lazy compilation stub has
609 /// been entered. It looks up which function this stub corresponds to, compiles
610 /// it if necessary, then returns the resultant function pointer.
611 void *JITResolver::JITCompilerFn(void *Stub) {
612 JITResolver *JR = StubToResolverMap->getResolverFromStub(Stub);
613 assert(JR && "Unable to find the corresponding JITResolver to the call site");
614
615 Function* F = nullptr;
616 void* ActualPtr = nullptr;
617
618 {
619 // Only lock for getting the Function. The call getPointerToFunction made
620 // in this function might trigger function materializing, which requires
621 // JIT lock to be unlocked.
622 MutexGuard locked(JR->TheJIT->lock);
623
624 // The address given to us for the stub may not be exactly right, it might
625 // be a little bit after the stub. As such, use upper_bound to find it.
626 std::pair I =
627 JR->state.LookupFunctionFromCallSite(Stub);
628 F = I.second;
629 ActualPtr = I.first;
630 }
631
632 // If we have already code generated the function, just return the address.
633 void *Result = JR->TheJIT->getPointerToGlobalIfAvailable(F);
634
635 if (!Result) {
636 // Otherwise we don't have it, do lazy compilation now.
637
638 // If lazy compilation is disabled, emit a useful error message and abort.
639 if (!JR->TheJIT->isCompilingLazily()) {
640 report_fatal_error("LLVM JIT requested to do lazy compilation of"
641 " function '"
642 + F->getName() + "' when lazy compiles are disabled!");
643 }
644
645 DEBUG(dbgs() << "JIT: Lazily resolving function '" << F->getName()
646 << "' In stub ptr = " << Stub << " actual ptr = "
647 << ActualPtr << "\n");
648 (void)ActualPtr;
649
650 Result = JR->TheJIT->getPointerToFunction(F);
651 }
652
653 // Reacquire the lock to update the GOT map.
654 MutexGuard locked(JR->TheJIT->lock);
655
656 // We might like to remove the call site from the CallSiteToFunction map, but
657 // we can't do that! Multiple threads could be stuck, waiting to acquire the
658 // lock above. As soon as the 1st function finishes compiling the function,
659 // the next one will be released, and needs to be able to find the function it
660 // needs to call.
661
662 // FIXME: We could rewrite all references to this stub if we knew them.
663
664 // What we will do is set the compiled function address to map to the
665 // same GOT entry as the stub so that later clients may update the GOT
666 // if they see it still using the stub address.
667 // Note: this is done so the Resolver doesn't have to manage GOT memory
668 // Do this without allocating map space if the target isn't using a GOT
669 if(JR->revGOTMap.find(Stub) != JR->revGOTMap.end())
670 JR->revGOTMap[Result] = JR->revGOTMap[Stub];
671
672 return Result;
673 }
674
675 //===----------------------------------------------------------------------===//
676 // JITEmitter code.
677 //
678
679 static GlobalObject *getSimpleAliasee(Constant *C) {
680 C = C->stripPointerCasts();
681 return dyn_cast(C);
682 }
683
684 void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
685 bool MayNeedFarStub) {
686 if (GlobalVariable *GV = dyn_cast(V))
687 return TheJIT->getOrEmitGlobalVariable(GV);
688
689 if (GlobalAlias *GA = dyn_cast(V)) {
690 // We can only handle simple cases.
691 if (GlobalValue *GV = getSimpleAliasee(GA->getAliasee()))
692 return TheJIT->getPointerToGlobal(GV);
693 return nullptr;
694 }
695
696 // If we have already compiled the function, return a pointer to its body.
697 Function *F = cast(V);
698
699 void *FnStub = Resolver.getLazyFunctionStubIfAvailable(F);
700 if (FnStub) {
701 // Return the function stub if it's already created. We do this first so
702 // that we're returning the same address for the function as any previous
703 // call. TODO: Yes, this is wrong. The lazy stub isn't guaranteed to be
704 // close enough to call.
705 return FnStub;
706 }
707
708 // If we know the target can handle arbitrary-distance calls, try to
709 // return a direct pointer.
710 if (!MayNeedFarStub) {
711 // If we have code, go ahead and return that.
712 void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
713 if (ResultPtr) return ResultPtr;
714
715 // If this is an external function pointer, we can force the JIT to
716 // 'compile' it, which really just adds it to the map.
717 if (isNonGhostDeclaration(F) || F->hasAvailableExternallyLinkage())
718 return TheJIT->getPointerToFunction(F);
719 }
720
721 // Otherwise, we may need a to emit a stub, and, conservatively, we always do
722 // so. Note that it's possible to return null from getLazyFunctionStub in the
723 // case of a weak extern that fails to resolve.
724 return Resolver.getLazyFunctionStub(F);
725 }
726
727 void *JITEmitter::getPointerToGVIndirectSym(GlobalValue *V, void *Reference) {
728 // Make sure GV is emitted first, and create a stub containing the fully
729 // resolved address.
730 void *GVAddress = getPointerToGlobal(V, Reference, false);
731 void *StubAddr = Resolver.getGlobalValueIndirectSym(V, GVAddress);
732 return StubAddr;
733 }
734
735 void JITEmitter::processDebugLoc(DebugLoc DL, bool BeforePrintingInsn) {
736 if (DL.isUnknown()) return;
737 if (!BeforePrintingInsn) return;
738
739 const LLVMContext &Context = EmissionDetails.MF->getFunction()->getContext();
740
741 if (DL.getScope(Context) != nullptr && PrevDL != DL) {
742 JITEvent_EmittedFunctionDetails::LineStart NextLine;
743 NextLine.Address = getCurrentPCValue();
744 NextLine.Loc = DL;
745 EmissionDetails.LineStarts.push_back(NextLine);
746 }
747
748 PrevDL = DL;
749 }
750
751 static unsigned GetConstantPoolSizeInBytes(MachineConstantPool *MCP,
752 const DataLayout *TD) {
753 const std::vector &Constants = MCP->getConstants();
754 if (Constants.empty()) return 0;
755
756 unsigned Size = 0;
757 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
758 MachineConstantPoolEntry CPE = Constants[i];
759 unsigned AlignMask = CPE.getAlignment() - 1;
760 Size = (Size + AlignMask) & ~AlignMask;
761 Type *Ty = CPE.getType();
762 Size += TD->getTypeAllocSize(Ty);
763 }
764 return Size;
765 }
766
767 void JITEmitter::startFunction(MachineFunction &F) {
768 DEBUG(dbgs() << "JIT: Starting CodeGen of Function "
769 << F.getName() << "\n");
770
771 uintptr_t ActualSize = 0;
772 // Set the memory writable, if it's not already
773 MemMgr->setMemoryWritable();
774
775 if (SizeEstimate > 0) {
776 // SizeEstimate will be non-zero on reallocation attempts.
777 ActualSize = SizeEstimate;
778 }
779
780 BufferBegin = CurBufferPtr = MemMgr->startFunctionBody(F.getFunction(),
781 ActualSize);
782 BufferEnd = BufferBegin+ActualSize;
783 EmittedFunctions[F.getFunction()].FunctionBody = BufferBegin;
784
785 // Ensure the constant pool/jump table info is at least 4-byte aligned.
786 emitAlignment(16);
787
788 emitConstantPool(F.getConstantPool());
789 if (MachineJumpTableInfo *MJTI = F.getJumpTableInfo())
790 initJumpTableInfo(MJTI);
791
792 // About to start emitting the machine code for the function.
793 emitAlignment(std::max(F.getFunction()->getAlignment(), 8U));
794 TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr);
795 EmittedFunctions[F.getFunction()].Code = CurBufferPtr;
796
797 MBBLocations.clear();
798
799 EmissionDetails.MF = &F;
800 EmissionDetails.LineStarts.clear();
801 }
802
803 bool JITEmitter::finishFunction(MachineFunction &F) {
804 if (CurBufferPtr == BufferEnd) {
805 // We must call endFunctionBody before retrying, because
806 // deallocateMemForFunction requires it.
807 MemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr);
808 retryWithMoreMemory(F);
809 return true;
810 }
811
812 if (MachineJumpTableInfo *MJTI = F.getJumpTableInfo())
813 emitJumpTableInfo(MJTI);
814
815 // FnStart is the start of the text, not the start of the constant pool and
816 // other per-function data.
817 uint8_t *FnStart =
818 (uint8_t *)TheJIT->getPointerToGlobalIfAvailable(F.getFunction());
819
820 // FnEnd is the end of the function's machine code.
821 uint8_t *FnEnd = CurBufferPtr;
822
823 if (!Relocations.empty()) {
824 CurFn = F.getFunction();
825 NumRelos += Relocations.size();
826
827 // Resolve the relocations to concrete pointers.
828 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
829 MachineRelocation &MR = Relocations[i];
830 void *ResultPtr = nullptr;
831 if (!MR.letTargetResolve()) {
832 if (MR.isExternalSymbol()) {
833 ResultPtr = TheJIT->getPointerToNamedFunction(MR.getExternalSymbol(),
834 false);
835 DEBUG(dbgs() << "JIT: Map \'" << MR.getExternalSymbol() << "\' to ["
836 << ResultPtr << "]\n");
837
838 // If the target REALLY wants a stub for this function, emit it now.
839 if (MR.mayNeedFarStub()) {
840 ResultPtr = Resolver.getExternalFunctionStub(ResultPtr);
841 }
842 } else if (MR.isGlobalValue()) {
843 ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
844 BufferBegin+MR.getMachineCodeOffset(),
845 MR.mayNeedFarStub());
846 } else if (MR.isIndirectSymbol()) {
847 ResultPtr = getPointerToGVIndirectSym(
848 MR.getGlobalValue(), BufferBegin+MR.getMachineCodeOffset());
849 } else if (MR.isBasicBlock()) {
850 ResultPtr = (void*)getMachineBasicBlockAddress(MR.getBasicBlock());
851 } else if (MR.isConstantPoolIndex()) {
852 ResultPtr =
853 (void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex());
854 } else {
855 assert(MR.isJumpTableIndex());
856 ResultPtr=(void*)getJumpTableEntryAddress(MR.getJumpTableIndex());
857 }
858
859 MR.setResultPointer(ResultPtr);
860 }
861
862 // if we are managing the GOT and the relocation wants an index,
863 // give it one
864 if (MR.isGOTRelative() && MemMgr->isManagingGOT()) {
865 unsigned idx = Resolver.getGOTIndexForAddr(ResultPtr);
866 MR.setGOTIndex(idx);
867 if (((void**)MemMgr->getGOTBase())[idx] != ResultPtr) {
868 DEBUG(dbgs() << "JIT: GOT was out of date for " << ResultPtr
869 << " pointing at " << ((void**)MemMgr->getGOTBase())[idx]
870 << "\n");
871 ((void**)MemMgr->getGOTBase())[idx] = ResultPtr;
872 }
873 }
874 }
875
876 CurFn = nullptr;
877 TheJIT->getJITInfo().relocate(BufferBegin, &Relocations[0],
878 Relocations.size(), MemMgr->getGOTBase());
879 }
880
881 // Update the GOT entry for F to point to the new code.
882 if (MemMgr->isManagingGOT()) {
883 unsigned idx = Resolver.getGOTIndexForAddr((void*)BufferBegin);
884 if (((void**)MemMgr->getGOTBase())[idx] != (void*)BufferBegin) {
885 DEBUG(dbgs() << "JIT: GOT was out of date for " << (void*)BufferBegin
886 << " pointing at " << ((void**)MemMgr->getGOTBase())[idx]
887 << "\n");
888 ((void**)MemMgr->getGOTBase())[idx] = (void*)BufferBegin;
889 }
890 }
891
892 // CurBufferPtr may have moved beyond FnEnd, due to memory allocation for
893 // global variables that were referenced in the relocations.
894 MemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr);
895
896 if (CurBufferPtr == BufferEnd) {
897 retryWithMoreMemory(F);
898 return true;
899 } else {
900 // Now that we've succeeded in emitting the function, reset the
901 // SizeEstimate back down to zero.
902 SizeEstimate = 0;
903 }
904
905 BufferBegin = CurBufferPtr = nullptr;
906 NumBytes += FnEnd-FnStart;
907
908 // Invalidate the icache if necessary.
909 sys::Memory::InvalidateInstructionCache(FnStart, FnEnd-FnStart);
910
911 TheJIT->NotifyFunctionEmitted(*F.getFunction(), FnStart, FnEnd-FnStart,
912 EmissionDetails);
913
914 // Reset the previous debug location.
915 PrevDL = DebugLoc();
916
917 DEBUG(dbgs() << "JIT: Finished CodeGen of [" << (void*)FnStart
918 << "] Function: " << F.getName()
919 << ": " << (FnEnd-FnStart) << " bytes of text, "
920 << Relocations.size() << " relocations\n");
921
922 Relocations.clear();
923 ConstPoolAddresses.clear();
924
925 // Mark code region readable and executable if it's not so already.
926 MemMgr->setMemoryExecutable();
927
928 DEBUG({
929 dbgs() << "JIT: Binary code:\n";
930 uint8_t* q = FnStart;
931 for (int i = 0; q < FnEnd; q += 4, ++i) {
932 if (i == 4)
933 i = 0;
934 if (i == 0)
935 dbgs() << "JIT: " << (long)(q - FnStart) << ": ";
936 bool Done = false;
937 for (int j = 3; j >= 0; --j) {
938 if (q + j >= FnEnd)
939 Done = true;
940 else
941 dbgs() << (unsigned short)q[j];
942 }
943 if (Done)
944 break;
945 dbgs() << ' ';
946 if (i == 3)
947 dbgs() << '\n';
948 }
949 dbgs()<< '\n';
950 });
951
952 if (MMI)
953 MMI->EndFunction();
954
955 return false;
956 }
957
958 void JITEmitter::retryWithMoreMemory(MachineFunction &F) {
959 DEBUG(dbgs() << "JIT: Ran out of space for native code. Reattempting.\n");
960 Relocations.clear(); // Clear the old relocations or we'll reapply them.
961 ConstPoolAddresses.clear();
962 ++NumRetries;
963 deallocateMemForFunction(F.getFunction());
964 // Try again with at least twice as much free space.
965 SizeEstimate = (uintptr_t)(2 * (BufferEnd - BufferBegin));
966
967 for (MachineFunction::iterator MBB = F.begin(), E = F.end(); MBB != E; ++MBB){
968 if (MBB->hasAddressTaken())
969 TheJIT->clearPointerToBasicBlock(MBB->getBasicBlock());
970 }
971 }
972
973 /// deallocateMemForFunction - Deallocate all memory for the specified
974 /// function body. Also drop any references the function has to stubs.
975 /// May be called while the Function is being destroyed inside ~Value().
976 void JITEmitter::deallocateMemForFunction(const Function *F) {
977 ValueMap::iterator
978 Emitted = EmittedFunctions.find(F);
979 if (Emitted != EmittedFunctions.end()) {
980 MemMgr->deallocateFunctionBody(Emitted->second.FunctionBody);
981 TheJIT->NotifyFreeingMachineCode(Emitted->second.Code);
982
983 EmittedFunctions.erase(Emitted);
984 }
985 }
986
987
988 void *JITEmitter::allocateSpace(uintptr_t Size, unsigned Alignment) {
989 if (BufferBegin)
990 return JITCodeEmitter::allocateSpace(Size, Alignment);
991
992 // create a new memory block if there is no active one.
993 // care must be taken so that BufferBegin is invalidated when a
994 // block is trimmed
995 BufferBegin = CurBufferPtr = MemMgr->allocateSpace(Size, Alignment);
996 BufferEnd = BufferBegin+Size;
997 return CurBufferPtr;
998 }
999
1000 void *JITEmitter::allocateGlobal(uintptr_t Size, unsigned Alignment) {
1001 // Delegate this call through the memory manager.
1002 return MemMgr->allocateGlobal(Size, Alignment);
1003 }
1004
1005 void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
1006 if (TheJIT->getJITInfo().hasCustomConstantPool())
1007 return;
1008
1009 const std::vector &Constants = MCP->getConstants();
1010 if (Constants.empty()) return;
1011
1012 unsigned Size = GetConstantPoolSizeInBytes(MCP, TheJIT->getDataLayout());
1013 unsigned Align = MCP->getConstantPoolAlignment();
1014 ConstantPoolBase = allocateSpace(Size, Align);
1015 ConstantPool = MCP;
1016
1017 if (!ConstantPoolBase) return; // Buffer overflow.
1018
1019 DEBUG(dbgs() << "JIT: Emitted constant pool at [" << ConstantPoolBase
1020 << "] (size: " << Size << ", alignment: " << Align << ")\n");
1021
1022 // Initialize the memory for all of the constant pool entries.
1023 unsigned Offset = 0;
1024 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
1025 MachineConstantPoolEntry CPE = Constants[i];
1026 unsigned AlignMask = CPE.getAlignment() - 1;
1027 Offset = (Offset + AlignMask) & ~AlignMask;
1028
1029 uintptr_t CAddr = (uintptr_t)ConstantPoolBase + Offset;
1030 ConstPoolAddresses.push_back(CAddr);
1031 if (CPE.isMachineConstantPoolEntry()) {
1032 // FIXME: add support to lower machine constant pool values into bytes!
1033 report_fatal_error("Initialize memory with machine specific constant pool"
1034 "entry has not been implemented!");
1035 }
1036 TheJIT->InitializeMemory(CPE.Val.ConstVal, (void*)CAddr);
1037 DEBUG(dbgs() << "JIT: CP" << i << " at [0x";
1038 dbgs().write_hex(CAddr) << "]\n");
1039
1040 Type *Ty = CPE.Val.ConstVal->getType();
1041 Offset += TheJIT->getDataLayout()->getTypeAllocSize(Ty);
1042 }
1043 }
1044
1045 void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) {
1046 if (TheJIT->getJITInfo().hasCustomJumpTables())
1047 return;
1048 if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_Inline)
1049 return;
1050
1051 const std::vector &JT = MJTI->getJumpTables();
1052 if (JT.empty()) return;
1053
1054 unsigned NumEntries = 0;
1055 for (unsigned i = 0, e = JT.size(); i != e; ++i)
1056 NumEntries += JT[i].MBBs.size();
1057
1058 unsigned EntrySize = MJTI->getEntrySize(*TheJIT->getDataLayout());
1059
1060 // Just allocate space for all the jump tables now. We will fix up the actual
1061 // MBB entries in the tables after we emit the code for each block, since then
1062 // we will know the final locations of the MBBs in memory.
1063 JumpTable = MJTI;
1064 JumpTableBase = allocateSpace(NumEntries * EntrySize,
1065 MJTI->getEntryAlignment(*TheJIT->getDataLayout()));
1066 }
1067
1068 void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) {
1069 if (TheJIT->getJITInfo().hasCustomJumpTables())
1070 return;
1071
1072 const std::vector &JT = MJTI->getJumpTables();
1073 if (JT.empty() || !JumpTableBase) return;
1074
1075
1076 switch (MJTI->getEntryKind()) {
1077 case MachineJumpTableInfo::EK_Inline:
1078 return;
1079 case MachineJumpTableInfo::EK_BlockAddress: {
1080 // EK_BlockAddress - Each entry is a plain address of block, e.g.:
1081 // .word LBB123
1082 assert(MJTI->getEntrySize(*TheJIT->getDataLayout()) == sizeof(void*) &&
1083 "Cross JIT'ing?");
1084
1085 // For each jump table, map each target in the jump table to the address of
1086 // an emitted MachineBasicBlock.
1087 intptr_t *SlotPtr = (intptr_t*)JumpTableBase;
1088
1089 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
1090 const std::vector &MBBs = JT[i].MBBs;
1091 // Store the address of the basic block for this jump table slot in the
1092 // memory we allocated for the jump table in 'initJumpTableInfo'
1093 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi)
1094 *SlotPtr++ = getMachineBasicBlockAddress(MBBs[mi]);
1095 }
1096 break;
1097 }
1098
1099 case MachineJumpTableInfo::EK_Custom32:
1100 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
1101 case MachineJumpTableInfo::EK_LabelDifference32: {
1102 assert(MJTI->getEntrySize(*TheJIT->getDataLayout()) == 4&&"Cross JIT'ing?");
1103 // For each jump table, place the offset from the beginning of the table
1104 // to the target address.
1105 int *SlotPtr = (int*)JumpTableBase;
1106
1107 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
1108 const std::vector &MBBs = JT[i].MBBs;
1109 // Store the offset of the basic block for this jump table slot in the
1110 // memory we allocated for the jump table in 'initJumpTableInfo'
1111 uintptr_t Base = (uintptr_t)SlotPtr;
1112 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
1113 uintptr_t MBBAddr = getMachineBasicBlockAddress(MBBs[mi]);
1114 /// FIXME: USe EntryKind instead of magic "getPICJumpTableEntry" hook.
1115 *SlotPtr++ = TheJIT->getJITInfo().getPICJumpTableEntry(MBBAddr, Base);
1116 }
1117 }
1118 break;
1119 }
1120 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
1121 llvm_unreachable(
1122 "JT Info emission not implemented for GPRel64BlockAddress yet.");
1123 }
1124 }
1125
1126 void JITEmitter::startGVStub(const GlobalValue* GV,
1127 unsigned StubSize, unsigned Alignment) {
1128 SavedBufferBegin = BufferBegin;
1129 SavedBufferEnd = BufferEnd;
1130 SavedCurBufferPtr = CurBufferPtr;
1131
1132 BufferBegin = CurBufferPtr = MemMgr->allocateStub(GV, StubSize, Alignment);
1133 BufferEnd = BufferBegin+StubSize+1;
1134 }
1135
1136 void JITEmitter::startGVStub(void *Buffer, unsigned StubSize) {
1137 SavedBufferBegin = BufferBegin;
1138 SavedBufferEnd = BufferEnd;
1139 SavedCurBufferPtr = CurBufferPtr;
1140
1141 BufferBegin = CurBufferPtr = (uint8_t *)Buffer;
1142 BufferEnd = BufferBegin+StubSize+1;
1143 }
1144
1145 void JITEmitter::finishGVStub() {
1146 assert(CurBufferPtr != BufferEnd && "Stub overflowed allocated space.");
1147 NumBytes += getCurrentPCOffset();
1148 BufferBegin = SavedBufferBegin;
1149 BufferEnd = SavedBufferEnd;
1150 CurBufferPtr = SavedCurBufferPtr;
1151 }
1152
1153 void *JITEmitter::allocIndirectGV(const GlobalValue *GV,
1154 const uint8_t *Buffer, size_t Size,
1155 unsigned Alignment) {
1156 uint8_t *IndGV = MemMgr->allocateStub(GV, Size, Alignment);
1157 memcpy(IndGV, Buffer, Size);
1158 return IndGV;
1159 }
1160
1161 // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
1162 // in the constant pool that was last emitted with the 'emitConstantPool'
1163 // method.
1164 //
1165 uintptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const {
1166 assert(ConstantNum < ConstantPool->getConstants().size() &&
1167 "Invalid ConstantPoolIndex!");
1168 return ConstPoolAddresses[ConstantNum];
1169 }
1170
1171 // getJumpTableEntryAddress - Return the address of the JumpTable with index
1172 // 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo'
1173 //
1174 uintptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const {
1175 const std::vector &JT = JumpTable->getJumpTables();
1176 assert(Index < JT.size() && "Invalid jump table index!");
1177
1178 unsigned EntrySize = JumpTable->getEntrySize(*TheJIT->getDataLayout());
1179
1180 unsigned Offset = 0;
1181 for (unsigned i = 0; i < Index; ++i)
1182 Offset += JT[i].MBBs.size();
1183
1184 Offset *= EntrySize;
1185
1186 return (uintptr_t)((char *)JumpTableBase + Offset);
1187 }
1188
1189 void JITEmitter::EmittedFunctionConfig::onDelete(
1190 JITEmitter *Emitter, const Function *F) {
1191 Emitter->deallocateMemForFunction(F);
1192 }
1193 void JITEmitter::EmittedFunctionConfig::onRAUW(
1194 JITEmitter *, const Function*, const Function*) {
1195 llvm_unreachable("The JIT doesn't know how to handle a"
1196 " RAUW on a value it has emitted.");
1197 }
1198
1199
1200 //===----------------------------------------------------------------------===//
1201 // Public interface to this file
1202 //===----------------------------------------------------------------------===//
1203
1204 JITCodeEmitter *JIT::createEmitter(JIT &jit, JITMemoryManager *JMM,
1205 TargetMachine &tm) {
1206 return new JITEmitter(jit, JMM, tm);
1207 }
1208
1209 // getPointerToFunctionOrStub - If the specified function has been
1210 // code-gen'd, return a pointer to the function. If not, compile it, or use
1211 // a stub to implement lazy compilation if available.
1212 //
1213 void *JIT::getPointerToFunctionOrStub(Function *F) {
1214 // If we have already code generated the function, just return the address.
1215 if (void *Addr = getPointerToGlobalIfAvailable(F))
1216 return Addr;
1217
1218 // Get a stub if the target supports it.
1219 JITEmitter *JE = static_cast(getCodeEmitter());
1220 return JE->getJITResolver().getLazyFunctionStub(F);
1221 }
1222
1223 void JIT::updateFunctionStubUnlocked(Function *F) {
1224 // Get the empty stub we generated earlier.
1225 JITEmitter *JE = static_