update rx (mobile builds).
[mono-project.git] / mono / mini / mini-llvm-cpp.cpp
blob2b90cd692daac6ad903e56bbb23cbbb5ff4cc6e2
1 //
2 // mini-llvm-cpp.cpp: C++ support classes for the mono LLVM integration
3 //
4 // (C) 2009-2011 Novell, Inc.
5 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
6 //
8 //
9 // We need to override some stuff in LLVM, but this cannot be done using the C
10 // interface, so we have to use some C++ code here.
11 // The things which we override are:
12 // - the default JIT code manager used by LLVM doesn't allocate memory using
13 // MAP_32BIT, we require it.
14 // - add some callbacks so we can obtain the size of methods and their exception
15 // tables.
19 // Mono's internal header files are not C++ clean, so avoid including them if
20 // possible
23 #include "config.h"
24 //undef those as llvm defines them on its own config.h as well.
25 #undef PACKAGE_BUGREPORT
26 #undef PACKAGE_NAME
27 #undef PACKAGE_STRING
28 #undef PACKAGE_TARNAME
29 #undef PACKAGE_VERSION
31 #include <stdint.h>
33 #include <llvm/Support/raw_ostream.h>
34 #include <llvm/PassManager.h>
35 #include <llvm/ExecutionEngine/ExecutionEngine.h>
36 #include <llvm/ExecutionEngine/JITMemoryManager.h>
37 #include <llvm/ExecutionEngine/JITEventListener.h>
38 #include <llvm/Target/TargetOptions.h>
39 #include <llvm/Target/TargetData.h>
40 #include <llvm/Target/TargetRegisterInfo.h>
41 #include <llvm/Analysis/Verifier.h>
42 #include <llvm/Analysis/Passes.h>
43 #include <llvm/Transforms/Scalar.h>
44 #include <llvm/Support/CommandLine.h>
45 #include "llvm/Support/PassNameParser.h"
46 #include "llvm/Support/PrettyStackTrace.h"
47 #include <llvm/CodeGen/Passes.h>
48 #include <llvm/CodeGen/MachineFunctionPass.h>
49 #include <llvm/CodeGen/MachineFunction.h>
50 #include <llvm/CodeGen/MachineFrameInfo.h>
51 //#include <llvm/LinkAllPasses.h>
53 #include "llvm-c/Core.h"
54 #include "llvm-c/ExecutionEngine.h"
56 #include "mini-llvm-cpp.h"
58 #define LLVM_CHECK_VERSION(major,minor) \
59 ((LLVM_MAJOR_VERSION > (major)) || \
60 ((LLVM_MAJOR_VERSION == (major)) && (LLVM_MINOR_VERSION >= (minor))))
62 // extern "C" void LLVMInitializeARMTargetInfo();
63 // extern "C" void LLVMInitializeARMTarget ();
64 // extern "C" void LLVMInitializeARMTargetMC ();
66 using namespace llvm;
68 class MonoJITMemoryManager : public JITMemoryManager
70 private:
71 JITMemoryManager *mm;
73 public:
74 /* Callbacks installed by mono */
75 AllocCodeMemoryCb *alloc_cb;
76 DlSymCb *dlsym_cb;
78 MonoJITMemoryManager ();
79 ~MonoJITMemoryManager ();
81 void setMemoryWritable (void);
83 void setMemoryExecutable (void);
85 void AllocateGOT();
87 unsigned char *getGOTBase() const {
88 return mm->getGOTBase ();
91 void setPoisonMemory(bool) {
94 unsigned char *startFunctionBody(const Function *F,
95 uintptr_t &ActualSize);
97 unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
98 unsigned Alignment);
100 void endFunctionBody(const Function *F, unsigned char *FunctionStart,
101 unsigned char *FunctionEnd);
103 unsigned char *allocateSpace(intptr_t Size, unsigned Alignment);
105 uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment);
107 void deallocateMemForFunction(const Function *F);
109 unsigned char*startExceptionTable(const Function* F,
110 uintptr_t &ActualSize);
112 void endExceptionTable(const Function *F, unsigned char *TableStart,
113 unsigned char *TableEnd,
114 unsigned char* FrameRegister);
116 virtual void deallocateFunctionBody(void*) {
119 virtual void deallocateExceptionTable(void*) {
122 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
123 unsigned SectionID) {
124 // FIXME:
125 assert(0);
126 return NULL;
129 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
130 unsigned SectionID) {
131 // FIXME:
132 assert(0);
133 return NULL;
136 virtual void* getPointerToNamedFunction(const std::string &Name, bool AbortOnFailure) {
137 void *res;
138 char *err;
140 err = dlsym_cb (Name.c_str (), &res);
141 if (err) {
142 outs () << "Unable to resolve: " << Name << ": " << err << "\n";
143 assert(0);
144 return NULL;
146 return res;
150 MonoJITMemoryManager::MonoJITMemoryManager ()
152 mm = JITMemoryManager::CreateDefaultMemManager ();
155 MonoJITMemoryManager::~MonoJITMemoryManager ()
157 delete mm;
160 void
161 MonoJITMemoryManager::setMemoryWritable (void)
165 void
166 MonoJITMemoryManager::setMemoryExecutable (void)
170 void
171 MonoJITMemoryManager::AllocateGOT()
173 mm->AllocateGOT ();
176 unsigned char *
177 MonoJITMemoryManager::startFunctionBody(const Function *F,
178 uintptr_t &ActualSize)
180 // FIXME: This leaks memory
181 if (ActualSize == 0)
182 ActualSize = 128;
183 return alloc_cb (wrap (F), ActualSize);
186 unsigned char *
187 MonoJITMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
188 unsigned Alignment)
190 return alloc_cb (wrap (F), StubSize);
193 void
194 MonoJITMemoryManager::endFunctionBody(const Function *F, unsigned char *FunctionStart,
195 unsigned char *FunctionEnd)
199 unsigned char *
200 MonoJITMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
202 return new unsigned char [Size];
205 uint8_t *
206 MonoJITMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
208 return new unsigned char [Size];
211 void
212 MonoJITMemoryManager::deallocateMemForFunction(const Function *F)
216 unsigned char*
217 MonoJITMemoryManager::startExceptionTable(const Function* F,
218 uintptr_t &ActualSize)
220 return startFunctionBody(F, ActualSize);
223 void
224 MonoJITMemoryManager::endExceptionTable(const Function *F, unsigned char *TableStart,
225 unsigned char *TableEnd,
226 unsigned char* FrameRegister)
230 class MonoJITEventListener : public JITEventListener {
232 public:
233 FunctionEmittedCb *emitted_cb;
235 MonoJITEventListener (FunctionEmittedCb *cb) {
236 emitted_cb = cb;
239 virtual void NotifyFunctionEmitted(const Function &F,
240 void *Code, size_t Size,
241 const EmittedFunctionDetails &Details) {
243 * X86TargetMachine::setCodeModelForJIT() sets the code model to Large on amd64,
244 * which means the JIT will generate calls of the form
245 * mov reg, <imm>
246 * call *reg
247 * Our trampoline code can't patch this. Passing CodeModel::Small to createJIT
248 * doesn't seem to work, we need Default. A discussion is here:
249 * http://lists.cs.uiuc.edu/pipermail/llvmdev/2009-December/027999.html
250 * There seems to no way to get the TargeMachine used by an EE either, so we
251 * install a profiler hook and reset the code model here.
252 * This should be inside an ifdef, but we can't include our config.h either,
253 * since its definitions conflict with LLVM's config.h.
256 //#if defined(TARGET_X86) || defined(TARGET_AMD64)
257 #ifndef LLVM_MONO_BRANCH
258 /* The LLVM mono branch contains a workaround, so this is not needed */
259 if (Details.MF->getTarget ().getCodeModel () == CodeModel::Large) {
260 Details.MF->getTarget ().setCodeModel (CodeModel::Default);
262 #endif
263 //#endif
265 emitted_cb (wrap (&F), Code, (char*)Code + Size);
269 static MonoJITMemoryManager *mono_mm;
270 static MonoJITEventListener *mono_event_listener;
272 static FunctionPassManager *fpm;
274 void
275 mono_llvm_optimize_method (LLVMValueRef method)
277 verifyFunction (*(unwrap<Function> (method)));
278 fpm->run (*unwrap<Function> (method));
281 void
282 mono_llvm_dump_value (LLVMValueRef value)
284 /* Same as LLVMDumpValue (), but print to stdout */
285 fflush (stdout);
286 outs () << (*unwrap<Value> (value));
289 /* Missing overload for building an alloca with an alignment */
290 LLVMValueRef
291 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty,
292 LLVMValueRef ArraySize,
293 int alignment, const char *Name)
295 return wrap (unwrap (builder)->Insert (new AllocaInst (unwrap (Ty), unwrap (ArraySize), alignment), Name));
298 LLVMValueRef
299 mono_llvm_build_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
300 const char *Name, gboolean is_volatile)
302 return wrap(unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name));
305 LLVMValueRef
306 mono_llvm_build_aligned_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
307 const char *Name, gboolean is_volatile, int alignment)
309 LoadInst *ins;
311 ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
312 ins->setAlignment (alignment);
314 return wrap(ins);
317 LLVMValueRef
318 mono_llvm_build_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
319 gboolean is_volatile)
321 return wrap(unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile));
324 LLVMValueRef
325 mono_llvm_build_aligned_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
326 gboolean is_volatile, int alignment)
328 StoreInst *ins;
330 ins = unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile);
331 ins->setAlignment (alignment);
333 return wrap (ins);
336 LLVMValueRef
337 mono_llvm_build_cmpxchg (LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp, LLVMValueRef val)
339 AtomicCmpXchgInst *ins;
341 ins = unwrap(builder)->CreateAtomicCmpXchg (unwrap(ptr), unwrap (cmp), unwrap (val), SequentiallyConsistent);
342 return wrap (ins);
345 LLVMValueRef
346 mono_llvm_build_atomic_rmw (LLVMBuilderRef builder, AtomicRMWOp op, LLVMValueRef ptr, LLVMValueRef val)
348 AtomicRMWInst::BinOp aop = AtomicRMWInst::Xchg;
349 AtomicRMWInst *ins;
351 switch (op) {
352 case LLVM_ATOMICRMW_OP_XCHG:
353 aop = AtomicRMWInst::Xchg;
354 break;
355 case LLVM_ATOMICRMW_OP_ADD:
356 aop = AtomicRMWInst::Add;
357 break;
358 default:
359 g_assert_not_reached ();
360 break;
363 ins = unwrap (builder)->CreateAtomicRMW (aop, unwrap (ptr), unwrap (val), AcquireRelease);
364 return wrap (ins);
367 LLVMValueRef
368 mono_llvm_build_fence (LLVMBuilderRef builder)
370 FenceInst *ins;
372 ins = unwrap (builder)->CreateFence (AcquireRelease);
373 return wrap (ins);
376 void
377 mono_llvm_replace_uses_of (LLVMValueRef var, LLVMValueRef v)
379 Value *V = ConstantExpr::getTruncOrBitCast (unwrap<Constant> (v), unwrap (var)->getType ());
380 unwrap (var)->replaceAllUsesWith (V);
383 static cl::list<const PassInfo*, bool, PassNameParser>
384 PassList(cl::desc("Optimizations available:"));
386 static void
387 force_pass_linking (void)
389 // Make sure the rest is linked in, but never executed
390 if (getenv ("FOO") != (char*)-1)
391 return;
393 // This is a subset of the passes in LinkAllPasses.h
394 // The utility passes and the interprocedural passes are commented out
396 (void) llvm::createAAEvalPass();
397 (void) llvm::createAggressiveDCEPass();
398 (void) llvm::createAliasAnalysisCounterPass();
399 (void) llvm::createAliasDebugger();
401 (void) llvm::createArgumentPromotionPass();
402 (void) llvm::createStructRetPromotionPass();
404 (void) llvm::createBasicAliasAnalysisPass();
405 (void) llvm::createLibCallAliasAnalysisPass(0);
406 (void) llvm::createScalarEvolutionAliasAnalysisPass();
407 (void) llvm::createBlockPlacementPass();
408 (void) llvm::createBreakCriticalEdgesPass();
409 (void) llvm::createCFGSimplificationPass();
411 (void) llvm::createConstantMergePass();
412 (void) llvm::createConstantPropagationPass();
415 (void) llvm::createDeadArgEliminationPass();
417 (void) llvm::createDeadCodeEliminationPass();
418 (void) llvm::createDeadInstEliminationPass();
419 (void) llvm::createDeadStoreEliminationPass();
421 (void) llvm::createDeadTypeEliminationPass();
422 (void) llvm::createDomOnlyPrinterPass();
423 (void) llvm::createDomPrinterPass();
424 (void) llvm::createDomOnlyViewerPass();
425 (void) llvm::createDomViewerPass();
426 (void) llvm::createEdgeProfilerPass();
427 (void) llvm::createOptimalEdgeProfilerPass();
428 (void) llvm::createFunctionInliningPass();
429 (void) llvm::createAlwaysInlinerPass();
430 (void) llvm::createGlobalDCEPass();
431 (void) llvm::createGlobalOptimizerPass();
432 (void) llvm::createGlobalsModRefPass();
433 (void) llvm::createIPConstantPropagationPass();
434 (void) llvm::createIPSCCPPass();
436 (void) llvm::createIndVarSimplifyPass();
437 (void) llvm::createInstructionCombiningPass();
439 (void) llvm::createInternalizePass(false);
441 (void) llvm::createLCSSAPass();
442 (void) llvm::createLICMPass();
443 (void) llvm::createLazyValueInfoPass();
444 (void) llvm::createLoopDependenceAnalysisPass();
446 (void) llvm::createLoopExtractorPass();
448 (void) llvm::createLoopSimplifyPass();
449 (void) llvm::createLoopStrengthReducePass();
450 (void) llvm::createLoopUnrollPass();
451 (void) llvm::createLoopUnswitchPass();
452 (void) llvm::createLoopRotatePass();
453 (void) llvm::createLowerInvokePass();
455 (void) llvm::createLowerSetJmpPass();
457 (void) llvm::createLowerSwitchPass();
458 (void) llvm::createNoAAPass();
460 (void) llvm::createNoProfileInfoPass();
461 (void) llvm::createProfileEstimatorPass();
462 (void) llvm::createProfileVerifierPass();
463 (void) llvm::createProfileLoaderPass();
465 (void) llvm::createPromoteMemoryToRegisterPass();
466 (void) llvm::createDemoteRegisterToMemoryPass();
468 (void) llvm::createPruneEHPass();
469 (void) llvm::createPostDomOnlyPrinterPass();
470 (void) llvm::createPostDomPrinterPass();
471 (void) llvm::createPostDomOnlyViewerPass();
472 (void) llvm::createPostDomViewerPass();
474 (void) llvm::createReassociatePass();
475 (void) llvm::createSCCPPass();
476 (void) llvm::createScalarReplAggregatesPass();
477 (void) llvm::createSimplifyLibCallsPass();
479 (void) llvm::createSingleLoopExtractorPass();
480 (void) llvm::createStripSymbolsPass();
481 (void) llvm::createStripNonDebugSymbolsPass();
482 (void) llvm::createStripDeadDebugInfoPass();
483 (void) llvm::createStripDeadPrototypesPass();
484 (void) llvm::createTailCallEliminationPass();
485 (void) llvm::createTailDuplicationPass();
486 (void) llvm::createJumpThreadingPass();
489 (void) llvm::createUnifyFunctionExitNodesPass();
491 (void) llvm::createInstCountPass();
492 (void) llvm::createCodeGenPreparePass();
493 (void) llvm::createGVNPass();
494 (void) llvm::createMemCpyOptPass();
495 (void) llvm::createLoopDeletionPass();
497 (void) llvm::createPostDomTree();
498 (void) llvm::createPostDomFrontier();
499 (void) llvm::createInstructionNamerPass();
500 (void) llvm::createPartialSpecializationPass();
501 (void) llvm::createFunctionAttrsPass();
502 (void) llvm::createMergeFunctionsPass();
503 (void) llvm::createPrintModulePass(0);
504 (void) llvm::createPrintFunctionPass("", 0);
505 (void) llvm::createDbgInfoPrinterPass();
506 (void) llvm::createModuleDebugInfoPrinterPass();
507 (void) llvm::createPartialInliningPass();
508 (void) llvm::createGEPSplitterPass();
509 (void) llvm::createLintPass();
511 (void) llvm::createSinkingPass();
514 LLVMExecutionEngineRef
515 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb, DlSymCb *dlsym_cb)
517 std::string Error;
519 force_pass_linking ();
521 #ifdef TARGET_ARM
522 LLVMInitializeARMTarget ();
523 LLVMInitializeARMTargetInfo ();
524 LLVMInitializeARMTargetMC ();
525 #else
526 LLVMInitializeX86Target ();
527 LLVMInitializeX86TargetInfo ();
528 LLVMInitializeX86TargetMC ();
529 #endif
531 mono_mm = new MonoJITMemoryManager ();
532 mono_mm->alloc_cb = alloc_cb;
533 mono_mm->dlsym_cb = dlsym_cb;
535 //JITExceptionHandling = true;
536 // PrettyStackTrace installs signal handlers which trip up libgc
537 DisablePrettyStackTrace = true;
540 * The Default code model doesn't seem to work on amd64,
541 * test_0_fields_with_big_offsets (among others) crashes, because LLVM tries to call
542 * memset using a normal pcrel code which is in 32bit memory, while memset isn't.
545 TargetOptions opts;
546 opts.JITExceptionHandling = 1;
548 EngineBuilder b (unwrap (MP));
549 #ifdef TARGET_AMD64
550 ExecutionEngine *EE = b.setJITMemoryManager (mono_mm).setTargetOptions (opts).setCodeModel (CodeModel::Large).setAllocateGVsWithCode (true).create ();
551 #else
552 ExecutionEngine *EE = b.setJITMemoryManager (mono_mm).setTargetOptions (opts).setAllocateGVsWithCode (true).create ();
553 #endif
554 g_assert (EE);
556 #if 0
557 ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, CodeGenOpt::Default, true, Reloc::Default, CodeModel::Large);
558 if (!EE) {
559 errs () << "Unable to create LLVM ExecutionEngine: " << Error << "\n";
560 g_assert_not_reached ();
562 #endif
564 EE->InstallExceptionTableRegister (exception_cb);
565 mono_event_listener = new MonoJITEventListener (emitted_cb);
566 EE->RegisterJITEventListener (mono_event_listener);
568 fpm = new FunctionPassManager (unwrap (MP));
570 fpm->add(new TargetData(*EE->getTargetData()));
572 PassRegistry &Registry = *PassRegistry::getPassRegistry();
573 initializeCore(Registry);
574 initializeScalarOpts(Registry);
575 //initializeIPO(Registry);
576 initializeAnalysis(Registry);
577 initializeIPA(Registry);
578 initializeTransformUtils(Registry);
579 initializeInstCombine(Registry);
580 //initializeInstrumentation(Registry);
581 initializeTarget(Registry);
583 llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "", false);
585 if (PassList.size() > 0) {
586 /* Use the passes specified by the env variable */
587 /* Only the passes in force_pass_linking () can be used */
588 for (unsigned i = 0; i < PassList.size(); ++i) {
589 const PassInfo *PassInf = PassList[i];
590 Pass *P = 0;
592 if (PassInf->getNormalCtor())
593 P = PassInf->getNormalCtor()();
594 fpm->add (P);
596 } else {
597 /* Use the same passes used by 'opt' by default, without the ipo passes */
598 const char *opts = "-simplifycfg -domtree -domfrontier -scalarrepl -instcombine -simplifycfg -basiccg -domtree -domfrontier -scalarrepl -simplify-libcalls -instcombine -simplifycfg -instcombine -simplifycfg -reassociate -domtree -loops -loop-simplify -domfrontier -loop-simplify -lcssa -loop-rotate -licm -lcssa -loop-unswitch -instcombine -scalar-evolution -loop-simplify -lcssa -iv-users -indvars -loop-deletion -loop-simplify -lcssa -loop-unroll -instcombine -memdep -gvn -memdep -memcpyopt -sccp -instcombine -domtree -memdep -dse -adce -gvn -simplifycfg -preverify -domtree -verify";
599 char **args;
600 int i;
602 args = g_strsplit (opts, " ", 1000);
603 for (i = 0; args [i]; i++)
605 llvm::cl::ParseCommandLineOptions (i, args, "", false);
606 g_strfreev (args);
608 for (unsigned i = 0; i < PassList.size(); ++i) {
609 const PassInfo *PassInf = PassList[i];
610 Pass *P = 0;
612 if (PassInf->getNormalCtor())
613 P = PassInf->getNormalCtor()();
614 fpm->add (P);
618 fpm->add(createInstructionCombiningPass());
619 fpm->add(createReassociatePass());
620 fpm->add(createGVNPass());
621 fpm->add(createCFGSimplificationPass());
625 return wrap(EE);
628 void
629 mono_llvm_dispose_ee (LLVMExecutionEngineRef ee)
631 delete unwrap (ee);
633 delete fpm;