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 #if LLVM_API_VERSION >= 1
36 #include <llvm/IR/Verifier.h>
38 #include <llvm/Analysis/Verifier.h>
40 #include <llvm/Analysis/Passes.h>
41 #include <llvm/Transforms/Scalar.h>
42 #include <llvm/Support/CommandLine.h>
43 #if LLVM_API_VERSION >= 1
44 #include "llvm/IR/LegacyPassNameParser.h"
46 #include "llvm/Support/PassNameParser.h"
48 #include "llvm/Support/PrettyStackTrace.h"
49 #include <llvm/CodeGen/Passes.h>
50 #include <llvm/CodeGen/MachineFunctionPass.h>
51 #include <llvm/CodeGen/MachineFunction.h>
52 #include <llvm/CodeGen/MachineFrameInfo.h>
53 #include <llvm/IR/Function.h>
54 #include <llvm/IR/IRBuilder.h>
55 #include <llvm/IR/Module.h>
56 //#include <llvm/LinkAllPasses.h>
58 #include "llvm-c/Core.h"
59 #include "llvm-c/ExecutionEngine.h"
61 #include "mini-llvm-cpp.h"
63 // extern "C" void LLVMInitializeARMTargetInfo();
64 // extern "C" void LLVMInitializeARMTarget ();
65 // extern "C" void LLVMInitializeARMTargetMC ();
69 #ifndef MONO_CROSS_COMPILE
71 class MonoJITMemoryManager : public JITMemoryManager
77 /* Callbacks installed by mono */
78 AllocCodeMemoryCb *alloc_cb;
81 MonoJITMemoryManager ();
82 ~MonoJITMemoryManager ();
84 void setMemoryWritable (void);
86 void setMemoryExecutable (void);
90 unsigned char *getGOTBase() const {
91 return mm->getGOTBase ();
94 void setPoisonMemory(bool) {
97 unsigned char *startFunctionBody(const Function *F,
98 uintptr_t &ActualSize);
100 unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
103 void endFunctionBody(const Function *F, unsigned char *FunctionStart,
104 unsigned char *FunctionEnd);
106 unsigned char *allocateSpace(intptr_t Size, unsigned Alignment);
108 uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment);
110 void deallocateMemForFunction(const Function *F);
112 unsigned char*startExceptionTable(const Function* F,
113 uintptr_t &ActualSize);
115 void endExceptionTable(const Function *F, unsigned char *TableStart,
116 unsigned char *TableEnd,
117 unsigned char* FrameRegister);
119 virtual void deallocateFunctionBody(void*) {
122 virtual void deallocateExceptionTable(void*) {
125 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
126 StringRef SectionName) {
132 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
133 StringRef SectionName, bool IsReadOnly) {
139 virtual bool applyPermissions(std::string*) {
145 virtual bool finalizeMemory(std::string *ErrMsg = 0) {
151 virtual void* getPointerToNamedFunction(const std::string &Name, bool AbortOnFailure) {
155 err = dlsym_cb (Name.c_str (), &res);
157 outs () << "Unable to resolve: " << Name << ": " << err << "\n";
165 MonoJITMemoryManager::MonoJITMemoryManager ()
167 mm = JITMemoryManager::CreateDefaultMemManager ();
170 MonoJITMemoryManager::~MonoJITMemoryManager ()
176 MonoJITMemoryManager::setMemoryWritable (void)
181 MonoJITMemoryManager::setMemoryExecutable (void)
186 MonoJITMemoryManager::AllocateGOT()
192 MonoJITMemoryManager::startFunctionBody(const Function *F,
193 uintptr_t &ActualSize)
195 // FIXME: This leaks memory
198 return alloc_cb (wrap (F), ActualSize);
202 MonoJITMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
205 return alloc_cb (wrap (F), StubSize);
209 MonoJITMemoryManager::endFunctionBody(const Function *F, unsigned char *FunctionStart,
210 unsigned char *FunctionEnd)
215 MonoJITMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
217 return new unsigned char [Size];
221 MonoJITMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
223 return new unsigned char [Size];
227 MonoJITMemoryManager::deallocateMemForFunction(const Function *F)
232 MonoJITMemoryManager::startExceptionTable(const Function* F,
233 uintptr_t &ActualSize)
235 return startFunctionBody(F, ActualSize);
239 MonoJITMemoryManager::endExceptionTable(const Function *F, unsigned char *TableStart,
240 unsigned char *TableEnd,
241 unsigned char* FrameRegister)
247 class MonoJITMemoryManager {
250 #endif /* !MONO_CROSS_COMPILE */
252 class MonoJITEventListener : public JITEventListener {
255 FunctionEmittedCb *emitted_cb;
257 MonoJITEventListener (FunctionEmittedCb *cb) {
261 virtual void NotifyFunctionEmitted(const Function &F,
262 void *Code, size_t Size,
263 const EmittedFunctionDetails &Details) {
265 * X86TargetMachine::setCodeModelForJIT() sets the code model to Large on amd64,
266 * which means the JIT will generate calls of the form
269 * Our trampoline code can't patch this. Passing CodeModel::Small to createJIT
270 * doesn't seem to work, we need Default. A discussion is here:
271 * http://lists.cs.uiuc.edu/pipermail/llvmdev/2009-December/027999.html
272 * There seems to no way to get the TargeMachine used by an EE either, so we
273 * install a profiler hook and reset the code model here.
274 * This should be inside an ifdef, but we can't include our config.h either,
275 * since its definitions conflict with LLVM's config.h.
276 * The LLVM mono branch contains a workaround.
278 emitted_cb (wrap (&F), Code, (char*)Code + Size);
285 MonoJITMemoryManager *mm;
286 MonoJITEventListener *listener;
287 FunctionPassManager *fpm;
291 mono_llvm_optimize_method (MonoEERef eeref, LLVMValueRef method)
293 MonoEE *mono_ee = (MonoEE*)eeref;
296 * The verifier does some checks on the whole module, leading to quadratic behavior.
298 //verifyFunction (*(unwrap<Function> (method)));
299 mono_ee->fpm->run (*unwrap<Function> (method));
303 mono_llvm_dump_value (LLVMValueRef value)
305 /* Same as LLVMDumpValue (), but print to stdout */
307 outs () << (*unwrap<Value> (value));
310 /* Missing overload for building an alloca with an alignment */
312 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty,
313 LLVMValueRef ArraySize,
314 int alignment, const char *Name)
316 return wrap (unwrap (builder)->Insert (new AllocaInst (unwrap (Ty), unwrap (ArraySize), alignment), Name));
320 mono_llvm_build_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
321 const char *Name, gboolean is_volatile, BarrierKind barrier)
323 LoadInst *ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
326 case LLVM_BARRIER_NONE:
328 case LLVM_BARRIER_ACQ:
329 ins->setOrdering(Acquire);
331 case LLVM_BARRIER_SEQ:
332 ins->setOrdering(SequentiallyConsistent);
335 g_assert_not_reached ();
343 mono_llvm_build_aligned_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
344 const char *Name, gboolean is_volatile, int alignment)
348 ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
349 ins->setAlignment (alignment);
355 mono_llvm_build_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
356 gboolean is_volatile, BarrierKind barrier)
358 StoreInst *ins = unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile);
361 case LLVM_BARRIER_NONE:
363 case LLVM_BARRIER_REL:
364 ins->setOrdering(Release);
366 case LLVM_BARRIER_SEQ:
367 ins->setOrdering(SequentiallyConsistent);
370 g_assert_not_reached ();
378 mono_llvm_build_aligned_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
379 gboolean is_volatile, int alignment)
383 ins = unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile);
384 ins->setAlignment (alignment);
390 mono_llvm_build_cmpxchg (LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp, LLVMValueRef val)
392 AtomicCmpXchgInst *ins;
394 #if LLVM_API_VERSION >= 1
395 ins = unwrap(builder)->CreateAtomicCmpXchg (unwrap(ptr), unwrap (cmp), unwrap (val), SequentiallyConsistent, SequentiallyConsistent);
397 ins = unwrap(builder)->CreateAtomicCmpXchg (unwrap(ptr), unwrap (cmp), unwrap (val), SequentiallyConsistent);
403 mono_llvm_build_atomic_rmw (LLVMBuilderRef builder, AtomicRMWOp op, LLVMValueRef ptr, LLVMValueRef val)
405 AtomicRMWInst::BinOp aop = AtomicRMWInst::Xchg;
409 case LLVM_ATOMICRMW_OP_XCHG:
410 aop = AtomicRMWInst::Xchg;
412 case LLVM_ATOMICRMW_OP_ADD:
413 aop = AtomicRMWInst::Add;
416 g_assert_not_reached ();
420 ins = unwrap (builder)->CreateAtomicRMW (aop, unwrap (ptr), unwrap (val), SequentiallyConsistent);
425 mono_llvm_build_fence (LLVMBuilderRef builder, BarrierKind kind)
428 AtomicOrdering ordering;
430 g_assert (kind != LLVM_BARRIER_NONE);
433 case LLVM_BARRIER_ACQ:
436 case LLVM_BARRIER_REL:
439 case LLVM_BARRIER_SEQ:
440 ordering = SequentiallyConsistent;
443 g_assert_not_reached ();
447 ins = unwrap (builder)->CreateFence (ordering);
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);
458 static cl::list<const PassInfo*, bool, PassNameParser>
459 PassList(cl::desc("Optimizations available:"));
462 force_pass_linking (void)
464 // Make sure the rest is linked in, but never executed
465 if (g_getenv ("FOO") != (char*)-1)
468 // This is a subset of the passes in LinkAllPasses.h
469 // The utility passes and the interprocedural passes are commented out
471 (void) llvm::createAAEvalPass();
472 (void) llvm::createAggressiveDCEPass();
473 (void) llvm::createAliasAnalysisCounterPass();
474 (void) llvm::createAliasDebugger();
476 (void) llvm::createArgumentPromotionPass();
477 (void) llvm::createStructRetPromotionPass();
479 (void) llvm::createBasicAliasAnalysisPass();
480 (void) llvm::createLibCallAliasAnalysisPass(0);
481 (void) llvm::createScalarEvolutionAliasAnalysisPass();
482 //(void) llvm::createBlockPlacementPass();
483 (void) llvm::createBreakCriticalEdgesPass();
484 (void) llvm::createCFGSimplificationPass();
486 (void) llvm::createConstantMergePass();
487 (void) llvm::createConstantPropagationPass();
490 (void) llvm::createDeadArgEliminationPass();
492 (void) llvm::createDeadCodeEliminationPass();
493 (void) llvm::createDeadInstEliminationPass();
494 (void) llvm::createDeadStoreEliminationPass();
496 (void) llvm::createDeadTypeEliminationPass();
497 (void) llvm::createDomOnlyPrinterPass();
498 (void) llvm::createDomPrinterPass();
499 (void) llvm::createDomOnlyViewerPass();
500 (void) llvm::createDomViewerPass();
501 (void) llvm::createEdgeProfilerPass();
502 (void) llvm::createOptimalEdgeProfilerPass();
503 (void) llvm::createFunctionInliningPass();
504 (void) llvm::createAlwaysInlinerPass();
505 (void) llvm::createGlobalDCEPass();
506 (void) llvm::createGlobalOptimizerPass();
507 (void) llvm::createGlobalsModRefPass();
508 (void) llvm::createIPConstantPropagationPass();
509 (void) llvm::createIPSCCPPass();
511 (void) llvm::createIndVarSimplifyPass();
512 (void) llvm::createInstructionCombiningPass();
514 (void) llvm::createInternalizePass(false);
516 (void) llvm::createLCSSAPass();
517 (void) llvm::createLICMPass();
518 (void) llvm::createLazyValueInfoPass();
519 //(void) llvm::createLoopDependenceAnalysisPass();
521 (void) llvm::createLoopExtractorPass();
523 (void) llvm::createLoopSimplifyPass();
524 (void) llvm::createLoopStrengthReducePass();
525 (void) llvm::createLoopUnrollPass();
526 (void) llvm::createLoopUnswitchPass();
527 (void) llvm::createLoopRotatePass();
528 (void) llvm::createLowerInvokePass();
530 (void) llvm::createLowerSetJmpPass();
532 (void) llvm::createLowerSwitchPass();
533 (void) llvm::createNoAAPass();
535 (void) llvm::createNoProfileInfoPass();
536 (void) llvm::createProfileEstimatorPass();
537 (void) llvm::createProfileVerifierPass();
538 (void) llvm::createProfileLoaderPass();
540 (void) llvm::createPromoteMemoryToRegisterPass();
541 (void) llvm::createDemoteRegisterToMemoryPass();
543 (void) llvm::createPruneEHPass();
544 (void) llvm::createPostDomOnlyPrinterPass();
545 (void) llvm::createPostDomPrinterPass();
546 (void) llvm::createPostDomOnlyViewerPass();
547 (void) llvm::createPostDomViewerPass();
549 (void) llvm::createReassociatePass();
550 (void) llvm::createSCCPPass();
551 (void) llvm::createScalarReplAggregatesPass();
552 //(void) llvm::createSimplifyLibCallsPass();
554 (void) llvm::createSingleLoopExtractorPass();
555 (void) llvm::createStripSymbolsPass();
556 (void) llvm::createStripNonDebugSymbolsPass();
557 (void) llvm::createStripDeadDebugInfoPass();
558 (void) llvm::createStripDeadPrototypesPass();
559 (void) llvm::createTailCallEliminationPass();
560 (void) llvm::createTailDuplicationPass();
561 (void) llvm::createJumpThreadingPass();
564 (void) llvm::createUnifyFunctionExitNodesPass();
566 (void) llvm::createInstCountPass();
567 (void) llvm::createCodeGenPreparePass();
568 (void) llvm::createGVNPass();
569 (void) llvm::createMemCpyOptPass();
570 (void) llvm::createLoopDeletionPass();
572 (void) llvm::createPostDomTree();
573 (void) llvm::createPostDomFrontier();
574 (void) llvm::createInstructionNamerPass();
575 (void) llvm::createPartialSpecializationPass();
576 (void) llvm::createFunctionAttrsPass();
577 (void) llvm::createMergeFunctionsPass();
578 (void) llvm::createPrintModulePass(0);
579 (void) llvm::createPrintFunctionPass("", 0);
580 (void) llvm::createDbgInfoPrinterPass();
581 (void) llvm::createModuleDebugInfoPrinterPass();
582 (void) llvm::createPartialInliningPass();
583 (void) llvm::createGEPSplitterPass();
584 (void) llvm::createLintPass();
586 (void) llvm::createSinkingPass();
589 #ifndef MONO_CROSS_COMPILE
591 static gboolean inited;
599 force_pass_linking ();
602 LLVMInitializeARMTarget ();
603 LLVMInitializeARMTargetInfo ();
604 LLVMInitializeARMTargetMC ();
606 LLVMInitializeX86Target ();
607 LLVMInitializeX86TargetInfo ();
608 LLVMInitializeX86TargetMC ();
611 PassRegistry &Registry = *PassRegistry::getPassRegistry();
612 initializeCore(Registry);
613 initializeScalarOpts(Registry);
614 initializeAnalysis(Registry);
615 initializeIPA(Registry);
616 initializeTransformUtils(Registry);
617 initializeInstCombine(Registry);
618 initializeTarget(Registry);
620 llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "");
626 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb, DlSymCb *dlsym_cb, LLVMExecutionEngineRef *ee)
633 mono_ee = new MonoEE ();
635 MonoJITMemoryManager *mono_mm = new MonoJITMemoryManager ();
636 mono_mm->alloc_cb = alloc_cb;
637 mono_mm->dlsym_cb = dlsym_cb;
638 mono_ee->mm = mono_mm;
641 * The Default code model doesn't seem to work on amd64,
642 * test_0_fields_with_big_offsets (among others) crashes, because LLVM tries to call
643 * memset using a normal pcrel code which is in 32bit memory, while memset isn't.
647 opts.JITExceptionHandling = 1;
649 #if LLVM_API_VERSION >= 2
650 StringRef cpu_name = sys::getHostCPUName ();
652 // EngineBuilder no longer has a copy assignment operator (?)
653 std::unique_ptr<Module> Owner(unwrap(MP));
654 EngineBuilder b (std::move(Owner));
656 ExecutionEngine *EE = b.setJITMemoryManager (mono_mm).setTargetOptions (opts).setAllocateGVsWithCode (true).setMCPU (cpu_name).setCodeModel (CodeModel::Large).create ();
658 ExecutionEngine *EE = b.setJITMemoryManager (mono_mm).setTargetOptions (opts).setAllocateGVsWithCode (true).setMCPU (cpu_name).create ();
663 EngineBuilder b (unwrap (MP));
664 EngineBuilder &eb = b;
665 eb = eb.setJITMemoryManager (mono_mm).setTargetOptions (opts).setAllocateGVsWithCode (true);
666 #if LLVM_API_VERSION >= 1
667 StringRef cpu_name = sys::getHostCPUName ();
668 eb = eb.setMCPU (cpu_name);
671 eb = eb.setCodeModel (CodeModel::Large);
674 ExecutionEngine *EE = eb.create ();
680 EE->InstallExceptionTableRegister (exception_cb);
681 MonoJITEventListener *listener = new MonoJITEventListener (emitted_cb);
682 EE->RegisterJITEventListener (listener);
683 mono_ee->listener = listener;
685 FunctionPassManager *fpm = new FunctionPassManager (unwrap (MP));
688 #if LLVM_API_VERSION >= 1
689 fpm->add(new DataLayoutPass(*EE->getDataLayout()));
691 fpm->add(new DataLayout(*EE->getDataLayout()));
694 if (PassList.size() > 0) {
695 /* Use the passes specified by the env variable */
696 /* Only the passes in force_pass_linking () can be used */
697 for (unsigned i = 0; i < PassList.size(); ++i) {
698 const PassInfo *PassInf = PassList[i];
701 if (PassInf->getNormalCtor())
702 P = PassInf->getNormalCtor()();
706 /* Use the same passes used by 'opt' by default, without the ipo passes */
707 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";
711 args = g_strsplit (opts, " ", 1000);
712 for (i = 0; args [i]; i++)
714 llvm::cl::ParseCommandLineOptions (i, args, "");
717 for (unsigned i = 0; i < PassList.size(); ++i) {
718 const PassInfo *PassInf = PassList[i];
721 if (PassInf->getNormalCtor())
722 P = PassInf->getNormalCtor()();
723 g_assert (P->getPassKind () == llvm::PT_Function || P->getPassKind () == llvm::PT_Loop);
728 fpm->add(createInstructionCombiningPass());
729 fpm->add(createReassociatePass());
730 fpm->add(createGVNPass());
731 fpm->add(createCFGSimplificationPass());
741 mono_llvm_dispose_ee (MonoEERef *eeref)
743 MonoEE *mono_ee = (MonoEE*)eeref;
747 //delete mono_ee->mm;
748 delete mono_ee->listener;
755 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb, DlSymCb *dlsym_cb, LLVMExecutionEngineRef *ee)
757 g_assert_not_reached ();
762 mono_llvm_dispose_ee (MonoEERef *eeref)
764 g_assert_not_reached ();
769 LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
772 g_assert_not_reached ();
776 LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global)
778 g_assert_not_reached ();
782 #endif /* !MONO_CROSS_COMPILE */