2010-05-11 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / mini / mini-llvm-cpp.cpp
blobb7853f796895ad325a6ea7be83caec56ca218640
1 //
2 // mini-llvm-cpp.cpp: C++ support classes for the mono LLVM integration
3 //
4 // (C) 2009 Novell, Inc.
5 //
7 //
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
14 // tables.
18 // Mono's internal header files are not C++ clean, so avoid including them if
19 // possible
22 #include <stdint.h>
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/Analysis/Verifier.h>
32 #include <llvm/Transforms/Scalar.h>
33 #include <llvm/Support/CommandLine.h>
34 #include "llvm/Support/PassNameParser.h"
35 #include "llvm/Support/PrettyStackTrace.h"
36 #include <llvm/CodeGen/Passes.h>
37 #include <llvm/CodeGen/MachineFunctionPass.h>
38 #include <llvm/CodeGen/MachineFunction.h>
39 //#include <llvm/LinkAllPasses.h>
41 #include "llvm-c/Core.h"
42 #include "llvm-c/ExecutionEngine.h"
44 #include "mini-llvm-cpp.h"
46 extern "C" void LLVMInitializeX86TargetInfo();
48 using namespace llvm;
50 class MonoJITMemoryManager : public JITMemoryManager
52 private:
53 JITMemoryManager *mm;
55 public:
56 /* Callbacks installed by mono */
57 AllocCodeMemoryCb *alloc_cb;
58 FunctionEmittedCb *emitted_cb;
60 MonoJITMemoryManager ();
61 ~MonoJITMemoryManager ();
63 void setMemoryWritable (void);
65 void setMemoryExecutable (void);
67 void AllocateGOT();
69 unsigned char *getGOTBase() const {
70 return mm->getGOTBase ();
73 #if LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION < 7
74 void *getDlsymTable() const {
75 return mm->getDlsymTable ();
78 void SetDlsymTable(void *ptr);
79 #endif
81 void setPoisonMemory(bool) {
84 unsigned char *startFunctionBody(const Function *F,
85 uintptr_t &ActualSize);
87 unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
88 unsigned Alignment);
90 void endFunctionBody(const Function *F, unsigned char *FunctionStart,
91 unsigned char *FunctionEnd);
93 unsigned char *allocateSpace(intptr_t Size, unsigned Alignment);
95 uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment);
97 void deallocateMemForFunction(const Function *F);
99 unsigned char*startExceptionTable(const Function* F,
100 uintptr_t &ActualSize);
102 void endExceptionTable(const Function *F, unsigned char *TableStart,
103 unsigned char *TableEnd,
104 unsigned char* FrameRegister);
106 #if LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION >= 7
107 virtual void deallocateFunctionBody(void*) {
110 virtual void deallocateExceptionTable(void*) {
112 #endif
115 MonoJITMemoryManager::MonoJITMemoryManager ()
117 SizeRequired = true;
118 mm = JITMemoryManager::CreateDefaultMemManager ();
121 MonoJITMemoryManager::~MonoJITMemoryManager ()
125 void
126 MonoJITMemoryManager::setMemoryWritable (void)
130 void
131 MonoJITMemoryManager::setMemoryExecutable (void)
135 void
136 MonoJITMemoryManager::AllocateGOT()
138 mm->AllocateGOT ();
141 #if LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION < 7
142 void
143 MonoJITMemoryManager::SetDlsymTable(void *ptr)
145 mm->SetDlsymTable (ptr);
147 #endif
149 unsigned char *
150 MonoJITMemoryManager::startFunctionBody(const Function *F,
151 uintptr_t &ActualSize)
153 return alloc_cb (wrap (F), ActualSize);
156 unsigned char *
157 MonoJITMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
158 unsigned Alignment)
160 return alloc_cb (wrap (F), StubSize);
163 void
164 MonoJITMemoryManager::endFunctionBody(const Function *F, unsigned char *FunctionStart,
165 unsigned char *FunctionEnd)
167 emitted_cb (wrap (F), FunctionStart, FunctionEnd);
170 unsigned char *
171 MonoJITMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
173 return new unsigned char [Size];
176 uint8_t *
177 MonoJITMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
179 return new unsigned char [Size];
182 void
183 MonoJITMemoryManager::deallocateMemForFunction(const Function *F)
187 unsigned char*
188 MonoJITMemoryManager::startExceptionTable(const Function* F,
189 uintptr_t &ActualSize)
191 return alloc_cb (wrap (F), ActualSize);
194 void
195 MonoJITMemoryManager::endExceptionTable(const Function *F, unsigned char *TableStart,
196 unsigned char *TableEnd,
197 unsigned char* FrameRegister)
201 static MonoJITMemoryManager *mono_mm;
203 static FunctionPassManager *fpm;
205 void
206 mono_llvm_optimize_method (LLVMValueRef method)
208 verifyFunction (*(unwrap<Function> (method)));
209 fpm->run (*unwrap<Function> (method));
212 void
213 mono_llvm_dump_value (LLVMValueRef value)
215 /* Same as LLVMDumpValue (), but print to stdout */
216 outs () << (*unwrap<Value> (value));
219 /* Missing overload for building an alloca with an alignment */
220 LLVMValueRef
221 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty,
222 LLVMValueRef ArraySize,
223 int alignment, const char *Name)
225 return wrap (unwrap (builder)->Insert (new AllocaInst (unwrap (Ty), unwrap (ArraySize), alignment), Name));
228 LLVMValueRef
229 mono_llvm_build_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
230 const char *Name, gboolean is_volatile)
232 return wrap(unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name));
235 LLVMValueRef
236 mono_llvm_build_aligned_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
237 const char *Name, gboolean is_volatile, int alignment)
239 LoadInst *ins;
241 ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
242 ins->setAlignment (alignment);
244 return wrap(ins);
247 void
248 mono_llvm_replace_uses_of (LLVMValueRef var, LLVMValueRef v)
250 Value *V = ConstantExpr::getTruncOrBitCast (unwrap<Constant> (v), unwrap (var)->getType ());
251 unwrap (var)->replaceAllUsesWith (V);
254 static cl::list<const PassInfo*, bool, PassNameParser>
255 PassList(cl::desc("Optimizations available:"));
257 class MonoJITEventListener : public JITEventListener {
258 virtual void NotifyFunctionEmitted(const Function &F,
259 void *Code, size_t Size,
260 const EmittedFunctionDetails &Details) {
262 * X86TargetMachine::setCodeModelForJIT() sets the code model to Large on amd64,
263 * which means the JIT will generate calls of the form
264 * mov reg, <imm>
265 * call *reg
266 * Our trampoline code can't patch this. Passing CodeModel::Small to createJIT
267 * doesn't seem to work, we need Default. A discussion is here:
268 * http://lists.cs.uiuc.edu/pipermail/llvmdev/2009-December/027999.html
269 * There seems to no way to get the TargeMachine used by an EE either, so we
270 * install a profiler hook and reset the code model here.
271 * This should be inside an ifdef, but we can't include our config.h either,
272 * since its definitions conflict with LLVM's config.h.
275 //#if defined(TARGET_X86) || defined(TARGET_AMD64)
276 if (Details.MF->getTarget ().getCodeModel () == CodeModel::Large) {
277 Details.MF->getTarget ().setCodeModel (CodeModel::Default);
279 //#endif
283 LLVMExecutionEngineRef
284 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb)
286 std::string Error;
288 LLVMInitializeX86Target ();
289 LLVMInitializeX86TargetInfo ();
291 llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "", false);
293 mono_mm = new MonoJITMemoryManager ();
294 mono_mm->alloc_cb = alloc_cb;
295 mono_mm->emitted_cb = emitted_cb;
297 DwarfExceptionHandling = true;
298 // PrettyStackTrace installs signal handlers which trip up libgc
299 DisablePrettyStackTrace = true;
301 ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, CodeGenOpt::Default);
302 if (!EE) {
303 errs () << "Unable to create LLVM ExecutionEngine: " << Error << "\n";
304 g_assert_not_reached ();
306 EE->InstallExceptionTableRegister (exception_cb);
307 EE->RegisterJITEventListener (new MonoJITEventListener ());
309 fpm = new FunctionPassManager (unwrap (MP));
311 fpm->add(new TargetData(*EE->getTargetData()));
312 /* Add a random set of passes */
313 /* Make this run-time configurable */
314 fpm->add(createInstructionCombiningPass());
315 fpm->add(createReassociatePass());
316 fpm->add(createGVNPass());
317 fpm->add(createCFGSimplificationPass());
319 /* Add passes specified by the env variable */
320 /* FIXME: This can only add passes which are linked in, thus are already used */
321 for (unsigned i = 0; i < PassList.size(); ++i) {
322 const PassInfo *PassInf = PassList[i];
323 Pass *P = 0;
325 if (PassInf->getNormalCtor())
326 P = PassInf->getNormalCtor()();
327 fpm->add (P);
330 return wrap(EE);
333 void
334 mono_llvm_dispose_ee (LLVMExecutionEngineRef ee)
336 delete unwrap (ee);
338 delete fpm;