2 // mini-llvm-cpp.cpp: C++ support classes for the mono LLVM integration
4 // (C) 2009-2011 Novell, Inc.
5 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
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
19 // Mono's internal header files are not C++ clean, so avoid including them if
27 #include <llvm/Support/raw_ostream.h>
28 #include <llvm/Support/Host.h>
29 #include <llvm/PassManager.h>
30 #include <llvm/ExecutionEngine/ExecutionEngine.h>
31 #include <llvm/ExecutionEngine/JITMemoryManager.h>
32 #include <llvm/ExecutionEngine/JITEventListener.h>
33 #include <llvm/Target/TargetOptions.h>
34 #include <llvm/Target/TargetRegisterInfo.h>
35 #include <llvm/IR/Verifier.h>
36 #include <llvm/Analysis/Passes.h>
37 #include <llvm/Transforms/Scalar.h>
38 #include <llvm/Support/CommandLine.h>
39 #include <llvm/IR/LegacyPassNameParser.h>
40 #include <llvm/Support/PrettyStackTrace.h>
41 #include <llvm/CodeGen/Passes.h>
42 #include <llvm/CodeGen/MachineFunctionPass.h>
43 #include <llvm/CodeGen/MachineFunction.h>
44 #include <llvm/CodeGen/MachineFrameInfo.h>
45 #include <llvm/IR/Function.h>
46 #include <llvm/IR/IRBuilder.h>
47 #include <llvm/IR/Module.h>
49 #include <llvm-c/Core.h>
50 #include <llvm-c/ExecutionEngine.h>
52 #include "mini-llvm-cpp.h"
54 // extern "C" void LLVMInitializeARMTargetInfo();
55 // extern "C" void LLVMInitializeARMTarget ();
56 // extern "C" void LLVMInitializeARMTargetMC ();
60 #ifndef MONO_CROSS_COMPILE
63 mono_llvm_cpp_throw_exception (void)
67 /* The generated code catches an int32* */
71 static void (*unhandled_exception)() = default_mono_llvm_unhandled_exception;
74 mono_llvm_set_unhandled_exception_handler (void)
76 std::set_terminate (unhandled_exception);
79 class MonoJITMemoryManager : public JITMemoryManager
85 /* Callbacks installed by mono */
86 AllocCodeMemoryCb *alloc_cb;
88 ExceptionTableCb *exception_cb;
90 MonoJITMemoryManager ();
91 ~MonoJITMemoryManager ();
93 void setMemoryWritable (void);
95 void setMemoryExecutable (void);
99 unsigned char *getGOTBase() const {
100 return mm->getGOTBase ();
103 void setPoisonMemory(bool) {
106 unsigned char *startFunctionBody(const Function *F,
107 uintptr_t &ActualSize);
109 unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
112 void endFunctionBody(const Function *F, unsigned char *FunctionStart,
113 unsigned char *FunctionEnd);
115 unsigned char *allocateSpace(intptr_t Size, unsigned Alignment);
117 uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment);
119 void deallocateMemForFunction(const Function *F);
121 unsigned char*startExceptionTable(const Function* F,
122 uintptr_t &ActualSize);
124 void endExceptionTable(const Function *F, unsigned char *TableStart,
125 unsigned char *TableEnd,
126 unsigned char* FrameRegister);
128 virtual void deallocateFunctionBody(void*) {
131 virtual void deallocateExceptionTable(void*) {
134 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
135 StringRef SectionName) {
141 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
142 StringRef SectionName, bool IsReadOnly) {
148 virtual bool applyPermissions(std::string*) {
154 virtual bool finalizeMemory(std::string *ErrMsg = 0) {
160 virtual void* getPointerToNamedFunction(const std::string &Name, bool AbortOnFailure) {
164 err = dlsym_cb (Name.c_str (), &res);
166 outs () << "Unable to resolve: " << Name << ": " << err << "\n";
174 MonoJITMemoryManager::MonoJITMemoryManager ()
176 mm = JITMemoryManager::CreateDefaultMemManager ();
179 MonoJITMemoryManager::~MonoJITMemoryManager ()
185 MonoJITMemoryManager::setMemoryWritable (void)
190 MonoJITMemoryManager::setMemoryExecutable (void)
195 MonoJITMemoryManager::AllocateGOT()
201 MonoJITMemoryManager::startFunctionBody(const Function *F,
202 uintptr_t &ActualSize)
204 // FIXME: This leaks memory
207 return alloc_cb (wrap (F), ActualSize);
211 MonoJITMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
214 return alloc_cb (wrap (F), StubSize);
218 MonoJITMemoryManager::endFunctionBody(const Function *F, unsigned char *FunctionStart,
219 unsigned char *FunctionEnd)
224 MonoJITMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
226 return new unsigned char [Size];
230 MonoJITMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
232 return new unsigned char [Size];
236 MonoJITMemoryManager::deallocateMemForFunction(const Function *F)
241 MonoJITMemoryManager::startExceptionTable(const Function* F,
242 uintptr_t &ActualSize)
244 return startFunctionBody(F, ActualSize);
248 MonoJITMemoryManager::endExceptionTable(const Function *F, unsigned char *TableStart,
249 unsigned char *TableEnd,
250 unsigned char* FrameRegister)
252 exception_cb (FrameRegister);
257 class MonoJITMemoryManager {
260 #endif /* !MONO_CROSS_COMPILE */
262 class MonoJITEventListener : public JITEventListener {
265 FunctionEmittedCb *emitted_cb;
267 MonoJITEventListener (FunctionEmittedCb *cb) {
271 virtual void NotifyFunctionEmitted(const Function &F,
272 void *Code, size_t Size,
273 const EmittedFunctionDetails &Details) {
274 emitted_cb (wrap (&F), Code, (char*)Code + Size);
281 MonoJITMemoryManager *mm;
282 MonoJITEventListener *listener;
283 FunctionPassManager *fpm;
287 mono_llvm_optimize_method (MonoEERef eeref, LLVMValueRef method)
289 MonoEE *mono_ee = (MonoEE*)eeref;
292 * The verifier does some checks on the whole module, leading to quadratic behavior.
294 //verifyFunction (*(unwrap<Function> (method)));
295 mono_ee->fpm->run (*unwrap<Function> (method));
299 mono_llvm_dump_value (LLVMValueRef value)
301 /* Same as LLVMDumpValue (), but print to stdout */
303 outs () << (*unwrap<Value> (value));
306 /* Missing overload for building an alloca with an alignment */
308 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty,
309 LLVMValueRef ArraySize,
310 int alignment, const char *Name)
312 return wrap (unwrap (builder)->Insert (new AllocaInst (unwrap (Ty), unwrap (ArraySize), alignment), Name));
316 mono_llvm_build_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
317 const char *Name, gboolean is_volatile, BarrierKind barrier)
319 LoadInst *ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
322 case LLVM_BARRIER_NONE:
324 case LLVM_BARRIER_ACQ:
325 ins->setOrdering(Acquire);
327 case LLVM_BARRIER_SEQ:
328 ins->setOrdering(SequentiallyConsistent);
331 g_assert_not_reached ();
339 mono_llvm_build_aligned_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
340 const char *Name, gboolean is_volatile, int alignment)
344 ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
345 ins->setAlignment (alignment);
351 mono_llvm_build_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
352 gboolean is_volatile, BarrierKind barrier)
354 StoreInst *ins = unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile);
357 case LLVM_BARRIER_NONE:
359 case LLVM_BARRIER_REL:
360 ins->setOrdering(Release);
362 case LLVM_BARRIER_SEQ:
363 ins->setOrdering(SequentiallyConsistent);
366 g_assert_not_reached ();
374 mono_llvm_build_aligned_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
375 gboolean is_volatile, int alignment)
379 ins = unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile);
380 ins->setAlignment (alignment);
386 mono_llvm_build_cmpxchg (LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp, LLVMValueRef val)
388 AtomicCmpXchgInst *ins;
390 ins = unwrap(builder)->CreateAtomicCmpXchg (unwrap(ptr), unwrap (cmp), unwrap (val), SequentiallyConsistent, SequentiallyConsistent);
395 mono_llvm_build_atomic_rmw (LLVMBuilderRef builder, AtomicRMWOp op, LLVMValueRef ptr, LLVMValueRef val)
397 AtomicRMWInst::BinOp aop = AtomicRMWInst::Xchg;
401 case LLVM_ATOMICRMW_OP_XCHG:
402 aop = AtomicRMWInst::Xchg;
404 case LLVM_ATOMICRMW_OP_ADD:
405 aop = AtomicRMWInst::Add;
408 g_assert_not_reached ();
412 ins = unwrap (builder)->CreateAtomicRMW (aop, unwrap (ptr), unwrap (val), SequentiallyConsistent);
417 mono_llvm_build_fence (LLVMBuilderRef builder, BarrierKind kind)
420 AtomicOrdering ordering;
422 g_assert (kind != LLVM_BARRIER_NONE);
425 case LLVM_BARRIER_ACQ:
428 case LLVM_BARRIER_REL:
431 case LLVM_BARRIER_SEQ:
432 ordering = SequentiallyConsistent;
435 g_assert_not_reached ();
439 ins = unwrap (builder)->CreateFence (ordering);
444 mono_llvm_set_must_tail (LLVMValueRef call_ins)
446 CallInst *ins = (CallInst*)unwrap (call_ins);
448 ins->setTailCallKind (CallInst::TailCallKind::TCK_MustTail);
452 mono_llvm_replace_uses_of (LLVMValueRef var, LLVMValueRef v)
454 Value *V = ConstantExpr::getTruncOrBitCast (unwrap<Constant> (v), unwrap (var)->getType ());
455 unwrap (var)->replaceAllUsesWith (V);
459 mono_llvm_create_constant_data_array (const uint8_t *data, int len)
461 return wrap(ConstantDataArray::get (*unwrap(LLVMGetGlobalContext ()), makeArrayRef(data, len)));
465 mono_llvm_set_is_constant (LLVMValueRef global_var)
467 unwrap<GlobalVariable>(global_var)->setConstant (true);
471 mono_llvm_set_preserveall_cc (LLVMValueRef func)
473 unwrap<Function>(func)->setCallingConv (CallingConv::PreserveAll);
477 mono_llvm_set_call_preserveall_cc (LLVMValueRef func)
479 unwrap<CallInst>(func)->setCallingConv (CallingConv::PreserveAll);
482 static cl::list<const PassInfo*, bool, PassNameParser>
483 PassList(cl::desc("Optimizations available:"));
486 force_pass_linking (void)
488 // Make sure the rest is linked in, but never executed
489 if (g_getenv ("FOO") != (char*)-1)
492 // This is a subset of the passes in LinkAllPasses.h
493 // The utility passes and the interprocedural passes are commented out
495 (void) llvm::createAAEvalPass();
496 (void) llvm::createAggressiveDCEPass();
497 (void) llvm::createAliasAnalysisCounterPass();
498 (void) llvm::createAliasDebugger();
500 (void) llvm::createArgumentPromotionPass();
501 (void) llvm::createStructRetPromotionPass();
503 (void) llvm::createBasicAliasAnalysisPass();
504 (void) llvm::createLibCallAliasAnalysisPass(0);
505 (void) llvm::createScalarEvolutionAliasAnalysisPass();
506 //(void) llvm::createBlockPlacementPass();
507 (void) llvm::createBreakCriticalEdgesPass();
508 (void) llvm::createCFGSimplificationPass();
510 (void) llvm::createConstantMergePass();
511 (void) llvm::createConstantPropagationPass();
514 (void) llvm::createDeadArgEliminationPass();
516 (void) llvm::createDeadCodeEliminationPass();
517 (void) llvm::createDeadInstEliminationPass();
518 (void) llvm::createDeadStoreEliminationPass();
520 (void) llvm::createDeadTypeEliminationPass();
521 (void) llvm::createDomOnlyPrinterPass();
522 (void) llvm::createDomPrinterPass();
523 (void) llvm::createDomOnlyViewerPass();
524 (void) llvm::createDomViewerPass();
525 (void) llvm::createEdgeProfilerPass();
526 (void) llvm::createOptimalEdgeProfilerPass();
527 (void) llvm::createFunctionInliningPass();
528 (void) llvm::createAlwaysInlinerPass();
529 (void) llvm::createGlobalDCEPass();
530 (void) llvm::createGlobalOptimizerPass();
531 (void) llvm::createGlobalsModRefPass();
532 (void) llvm::createIPConstantPropagationPass();
533 (void) llvm::createIPSCCPPass();
535 (void) llvm::createIndVarSimplifyPass();
536 (void) llvm::createInstructionCombiningPass();
538 (void) llvm::createInternalizePass(false);
540 (void) llvm::createLCSSAPass();
541 (void) llvm::createLICMPass();
542 (void) llvm::createLazyValueInfoPass();
543 //(void) llvm::createLoopDependenceAnalysisPass();
545 (void) llvm::createLoopExtractorPass();
547 (void) llvm::createLoopSimplifyPass();
548 (void) llvm::createLoopStrengthReducePass();
549 (void) llvm::createLoopUnrollPass();
550 (void) llvm::createLoopUnswitchPass();
551 (void) llvm::createLoopRotatePass();
552 (void) llvm::createLowerInvokePass();
554 (void) llvm::createLowerSetJmpPass();
556 (void) llvm::createLowerSwitchPass();
557 (void) llvm::createNoAAPass();
559 (void) llvm::createNoProfileInfoPass();
560 (void) llvm::createProfileEstimatorPass();
561 (void) llvm::createProfileVerifierPass();
562 (void) llvm::createProfileLoaderPass();
564 (void) llvm::createPromoteMemoryToRegisterPass();
565 (void) llvm::createDemoteRegisterToMemoryPass();
567 (void) llvm::createPruneEHPass();
568 (void) llvm::createPostDomOnlyPrinterPass();
569 (void) llvm::createPostDomPrinterPass();
570 (void) llvm::createPostDomOnlyViewerPass();
571 (void) llvm::createPostDomViewerPass();
573 (void) llvm::createReassociatePass();
574 (void) llvm::createSCCPPass();
575 (void) llvm::createScalarReplAggregatesPass();
576 //(void) llvm::createSimplifyLibCallsPass();
578 (void) llvm::createSingleLoopExtractorPass();
579 (void) llvm::createStripSymbolsPass();
580 (void) llvm::createStripNonDebugSymbolsPass();
581 (void) llvm::createStripDeadDebugInfoPass();
582 (void) llvm::createStripDeadPrototypesPass();
583 (void) llvm::createTailCallEliminationPass();
584 (void) llvm::createTailDuplicationPass();
585 (void) llvm::createJumpThreadingPass();
588 (void) llvm::createUnifyFunctionExitNodesPass();
590 (void) llvm::createInstCountPass();
591 (void) llvm::createCodeGenPreparePass();
592 (void) llvm::createGVNPass();
593 (void) llvm::createMemCpyOptPass();
594 (void) llvm::createLoopDeletionPass();
596 (void) llvm::createPostDomTree();
597 (void) llvm::createPostDomFrontier();
598 (void) llvm::createInstructionNamerPass();
599 (void) llvm::createPartialSpecializationPass();
600 (void) llvm::createFunctionAttrsPass();
601 (void) llvm::createMergeFunctionsPass();
602 (void) llvm::createPrintModulePass(0);
603 (void) llvm::createPrintFunctionPass("", 0);
604 (void) llvm::createDbgInfoPrinterPass();
605 (void) llvm::createModuleDebugInfoPrinterPass();
606 (void) llvm::createPartialInliningPass();
607 (void) llvm::createGEPSplitterPass();
608 (void) llvm::createLintPass();
610 (void) llvm::createSinkingPass();
613 #ifndef MONO_CROSS_COMPILE
615 static gboolean inited;
623 force_pass_linking ();
626 LLVMInitializeARMTarget ();
627 LLVMInitializeARMTargetInfo ();
628 LLVMInitializeARMTargetMC ();
629 #elif defined(TARGET_X86)
630 LLVMInitializeX86Target ();
631 LLVMInitializeX86TargetInfo ();
632 LLVMInitializeX86TargetMC ();
633 #elif defined(TARGET_POWERPC)
634 LLVMInitializePowerPCTarget ();
635 LLVMInitializePowerPCTargetInfo ();
636 LLVMInitializePowerPCTargetMC ();
638 #error Unsupported mono-llvm target
641 PassRegistry &Registry = *PassRegistry::getPassRegistry();
642 initializeCore(Registry);
643 initializeScalarOpts(Registry);
644 initializeAnalysis(Registry);
645 initializeIPA(Registry);
646 initializeTransformUtils(Registry);
647 initializeInstCombine(Registry);
648 initializeTarget(Registry);
650 llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "");
656 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb, DlSymCb *dlsym_cb, LLVMExecutionEngineRef *ee)
663 mono_ee = new MonoEE ();
665 MonoJITMemoryManager *mono_mm = new MonoJITMemoryManager ();
666 mono_mm->alloc_cb = alloc_cb;
667 mono_mm->dlsym_cb = dlsym_cb;
668 mono_mm->exception_cb = exception_cb;
669 mono_ee->mm = mono_mm;
672 * The Default code model doesn't seem to work on amd64,
673 * test_0_fields_with_big_offsets (among others) crashes, because LLVM tries to call
674 * memset using a normal pcrel code which is in 32bit memory, while memset isn't.
678 opts.JITExceptionHandling = 1;
680 StringRef cpu_name = sys::getHostCPUName ();
682 // EngineBuilder no longer has a copy assignment operator (?)
683 std::unique_ptr<Module> Owner(unwrap(MP));
684 EngineBuilder b (std::move(Owner));
685 ExecutionEngine *EE = b.setJITMemoryManager (mono_mm).setTargetOptions (opts).setAllocateGVsWithCode (true).setMCPU (cpu_name).create ();
690 MonoJITEventListener *listener = new MonoJITEventListener (emitted_cb);
691 EE->RegisterJITEventListener (listener);
692 mono_ee->listener = listener;
694 FunctionPassManager *fpm = new FunctionPassManager (unwrap (MP));
697 fpm->add(new DataLayoutPass(*EE->getDataLayout()));
699 if (PassList.size() > 0) {
700 /* Use the passes specified by the env variable */
701 /* Only the passes in force_pass_linking () can be used */
702 for (unsigned i = 0; i < PassList.size(); ++i) {
703 const PassInfo *PassInf = PassList[i];
706 if (PassInf->getNormalCtor())
707 P = PassInf->getNormalCtor()();
711 /* Use the same passes used by 'opt' by default, without the ipo passes */
712 const char *opts = "-simplifycfg -domtree -domfrontier -scalarrepl -instcombine -simplifycfg -domtree -domfrontier -scalarrepl -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";
716 args = g_strsplit (opts, " ", 1000);
717 for (i = 0; args [i]; i++)
719 llvm::cl::ParseCommandLineOptions (i, args, "");
722 for (unsigned i = 0; i < PassList.size(); ++i) {
723 const PassInfo *PassInf = PassList[i];
726 if (PassInf->getNormalCtor())
727 P = PassInf->getNormalCtor()();
728 g_assert (P->getPassKind () == llvm::PT_Function || P->getPassKind () == llvm::PT_Loop);
733 fpm->add(createInstructionCombiningPass());
734 fpm->add(createReassociatePass());
735 fpm->add(createGVNPass());
736 fpm->add(createCFGSimplificationPass());
746 mono_llvm_dispose_ee (MonoEERef *eeref)
748 MonoEE *mono_ee = (MonoEE*)eeref;
752 //delete mono_ee->mm;
753 delete mono_ee->listener;
760 mono_llvm_cpp_throw_exception (void)
765 mono_llvm_set_unhandled_exception_handler (void)
770 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb, DlSymCb *dlsym_cb, LLVMExecutionEngineRef *ee)
772 g_assert_not_reached ();
777 mono_llvm_dispose_ee (MonoEERef *eeref)
779 g_assert_not_reached ();
784 LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
787 g_assert_not_reached ();
791 LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global)
793 g_assert_not_reached ();
797 #endif /* !MONO_CROSS_COMPILE */