2 // mini-llvm-cpp.cpp: C++ support classes for the mono LLVM integration
4 // (C) 2009 Novell, Inc.
8 // We need to override some stuff in LLVM, but this cannot be done using the C
9 // interface, so we have to use some C++ code here.
10 // The things which we override are:
11 // - the default JIT code manager used by LLVM doesn't allocate memory using
12 // MAP_32BIT, we require it.
13 // - add some callbacks so we can obtain the size of methods and their exception
18 // Mono's internal header files are not C++ clean, so avoid including them if
24 #include <llvm/Support/raw_ostream.h>
25 #include <llvm/PassManager.h>
26 #include <llvm/ExecutionEngine/ExecutionEngine.h>
27 #include <llvm/ExecutionEngine/JITMemoryManager.h>
28 #include <llvm/ExecutionEngine/JITEventListener.h>
29 #include <llvm/Target/TargetOptions.h>
30 #include <llvm/Target/TargetData.h>
31 #include <llvm/Target/TargetRegisterInfo.h>
32 #include <llvm/Analysis/Verifier.h>
33 #include <llvm/Transforms/Scalar.h>
34 #include <llvm/Support/CommandLine.h>
35 #include "llvm/Support/PassNameParser.h"
36 #include "llvm/Support/PrettyStackTrace.h"
37 #include <llvm/CodeGen/Passes.h>
38 #include <llvm/CodeGen/MachineFunctionPass.h>
39 #include <llvm/CodeGen/MachineFunction.h>
40 #include <llvm/CodeGen/MachineFrameInfo.h>
41 #include <llvm/Support/StandardPasses.h>
42 //#include <llvm/LinkAllPasses.h>
44 #include "llvm-c/Core.h"
45 #include "llvm-c/ExecutionEngine.h"
47 #include "mini-llvm-cpp.h"
49 extern "C" void LLVMInitializeX86TargetInfo();
53 class MonoJITMemoryManager : public JITMemoryManager
59 /* Callbacks installed by mono */
60 AllocCodeMemoryCb *alloc_cb;
62 MonoJITMemoryManager ();
63 ~MonoJITMemoryManager ();
65 void setMemoryWritable (void);
67 void setMemoryExecutable (void);
71 unsigned char *getGOTBase() const {
72 return mm->getGOTBase ();
75 #if LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION < 7
76 void *getDlsymTable() const {
77 return mm->getDlsymTable ();
80 void SetDlsymTable(void *ptr);
83 void setPoisonMemory(bool) {
86 unsigned char *startFunctionBody(const Function *F,
87 uintptr_t &ActualSize);
89 unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
92 void endFunctionBody(const Function *F, unsigned char *FunctionStart,
93 unsigned char *FunctionEnd);
95 unsigned char *allocateSpace(intptr_t Size, unsigned Alignment);
97 uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment);
99 void deallocateMemForFunction(const Function *F);
101 unsigned char*startExceptionTable(const Function* F,
102 uintptr_t &ActualSize);
104 void endExceptionTable(const Function *F, unsigned char *TableStart,
105 unsigned char *TableEnd,
106 unsigned char* FrameRegister);
108 #if LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION >= 7
109 virtual void deallocateFunctionBody(void*) {
112 virtual void deallocateExceptionTable(void*) {
117 MonoJITMemoryManager::MonoJITMemoryManager ()
120 mm = JITMemoryManager::CreateDefaultMemManager ();
123 MonoJITMemoryManager::~MonoJITMemoryManager ()
128 MonoJITMemoryManager::setMemoryWritable (void)
133 MonoJITMemoryManager::setMemoryExecutable (void)
138 MonoJITMemoryManager::AllocateGOT()
143 #if LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION < 7
145 MonoJITMemoryManager::SetDlsymTable(void *ptr)
147 mm->SetDlsymTable (ptr);
152 MonoJITMemoryManager::startFunctionBody(const Function *F,
153 uintptr_t &ActualSize)
155 return alloc_cb (wrap (F), ActualSize);
159 MonoJITMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
162 return alloc_cb (wrap (F), StubSize);
166 MonoJITMemoryManager::endFunctionBody(const Function *F, unsigned char *FunctionStart,
167 unsigned char *FunctionEnd)
172 MonoJITMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
174 return new unsigned char [Size];
178 MonoJITMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
180 return new unsigned char [Size];
184 MonoJITMemoryManager::deallocateMemForFunction(const Function *F)
189 MonoJITMemoryManager::startExceptionTable(const Function* F,
190 uintptr_t &ActualSize)
192 return alloc_cb (wrap (F), ActualSize);
196 MonoJITMemoryManager::endExceptionTable(const Function *F, unsigned char *TableStart,
197 unsigned char *TableEnd,
198 unsigned char* FrameRegister)
202 static MonoJITMemoryManager *mono_mm;
204 static FunctionPassManager *fpm;
207 mono_llvm_optimize_method (LLVMValueRef method)
209 verifyFunction (*(unwrap<Function> (method)));
210 fpm->run (*unwrap<Function> (method));
214 mono_llvm_dump_value (LLVMValueRef value)
216 /* Same as LLVMDumpValue (), but print to stdout */
217 outs () << (*unwrap<Value> (value));
220 /* Missing overload for building an alloca with an alignment */
222 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty,
223 LLVMValueRef ArraySize,
224 int alignment, const char *Name)
226 return wrap (unwrap (builder)->Insert (new AllocaInst (unwrap (Ty), unwrap (ArraySize), alignment), Name));
230 mono_llvm_build_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
231 const char *Name, gboolean is_volatile)
233 return wrap(unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name));
237 mono_llvm_build_aligned_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
238 const char *Name, gboolean is_volatile, int alignment)
242 ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
243 ins->setAlignment (alignment);
249 mono_llvm_build_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
250 gboolean is_volatile)
252 return wrap(unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile));
256 mono_llvm_replace_uses_of (LLVMValueRef var, LLVMValueRef v)
258 Value *V = ConstantExpr::getTruncOrBitCast (unwrap<Constant> (v), unwrap (var)->getType ());
259 unwrap (var)->replaceAllUsesWith (V);
262 static cl::list<const PassInfo*, bool, PassNameParser>
263 PassList(cl::desc("Optimizations available:"));
265 class MonoJITEventListener : public JITEventListener {
268 FunctionEmittedCb *emitted_cb;
270 MonoJITEventListener (FunctionEmittedCb *cb) {
274 virtual void NotifyFunctionEmitted(const Function &F,
275 void *Code, size_t Size,
276 const EmittedFunctionDetails &Details) {
278 * X86TargetMachine::setCodeModelForJIT() sets the code model to Large on amd64,
279 * which means the JIT will generate calls of the form
282 * Our trampoline code can't patch this. Passing CodeModel::Small to createJIT
283 * doesn't seem to work, we need Default. A discussion is here:
284 * http://lists.cs.uiuc.edu/pipermail/llvmdev/2009-December/027999.html
285 * There seems to no way to get the TargeMachine used by an EE either, so we
286 * install a profiler hook and reset the code model here.
287 * This should be inside an ifdef, but we can't include our config.h either,
288 * since its definitions conflict with LLVM's config.h.
291 //#if defined(TARGET_X86) || defined(TARGET_AMD64)
292 #ifndef LLVM_MONO_BRANCH
293 /* The LLVM mono branch contains a workaround, so this is not needed */
294 if (Details.MF->getTarget ().getCodeModel () == CodeModel::Large) {
295 Details.MF->getTarget ().setCodeModel (CodeModel::Default);
300 emitted_cb (wrap (&F), Code, (char*)Code + Size);
304 LLVMExecutionEngineRef
305 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb)
309 LLVMInitializeX86Target ();
310 LLVMInitializeX86TargetInfo ();
312 llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "", false);
314 mono_mm = new MonoJITMemoryManager ();
315 mono_mm->alloc_cb = alloc_cb;
317 #if LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION < 8
318 DwarfExceptionHandling = true;
320 JITExceptionHandling = true;
322 // PrettyStackTrace installs signal handlers which trip up libgc
323 DisablePrettyStackTrace = true;
325 ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, CodeGenOpt::Default);
327 errs () << "Unable to create LLVM ExecutionEngine: " << Error << "\n";
328 g_assert_not_reached ();
330 EE->InstallExceptionTableRegister (exception_cb);
331 EE->RegisterJITEventListener (new MonoJITEventListener (emitted_cb));
333 fpm = new FunctionPassManager (unwrap (MP));
335 fpm->add(new TargetData(*EE->getTargetData()));
336 /* Add a random set of passes */
337 /* Make this run-time configurable */
338 //createStandardFunctionPasses (fpm, 2);
339 fpm->add(createInstructionCombiningPass());
340 fpm->add(createReassociatePass());
341 fpm->add(createGVNPass());
342 fpm->add(createCFGSimplificationPass());
344 /* Add passes specified by the env variable */
345 /* FIXME: This can only add passes which are linked in, thus are already used */
346 for (unsigned i = 0; i < PassList.size(); ++i) {
347 const PassInfo *PassInf = PassList[i];
350 if (PassInf->getNormalCtor())
351 P = PassInf->getNormalCtor()();
359 mono_llvm_dispose_ee (LLVMExecutionEngineRef ee)