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
24 //undef those as llvm defines them on its own config.h as well.
25 #undef PACKAGE_BUGREPORT
28 #undef PACKAGE_TARNAME
29 #undef PACKAGE_VERSION
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/TargetRegisterInfo.h>
40 #include <llvm/Analysis/Verifier.h>
41 #include <llvm/Analysis/Passes.h>
42 #include <llvm/Transforms/Scalar.h>
43 #include <llvm/Support/CommandLine.h>
44 #include "llvm/Support/PassNameParser.h"
45 #include "llvm/Support/PrettyStackTrace.h"
46 #include <llvm/CodeGen/Passes.h>
47 #include <llvm/CodeGen/MachineFunctionPass.h>
48 #include <llvm/CodeGen/MachineFunction.h>
49 #include <llvm/CodeGen/MachineFrameInfo.h>
50 #include <llvm/IR/Function.h>
51 #include <llvm/IR/IRBuilder.h>
52 #include <llvm/IR/Module.h>
53 //#include <llvm/LinkAllPasses.h>
55 #include "llvm-c/Core.h"
56 #include "llvm-c/ExecutionEngine.h"
58 #include "mini-llvm-cpp.h"
60 #define LLVM_CHECK_VERSION(major,minor) \
61 ((LLVM_MAJOR_VERSION > (major)) || \
62 ((LLVM_MAJOR_VERSION == (major)) && (LLVM_MINOR_VERSION >= (minor))))
64 // extern "C" void LLVMInitializeARMTargetInfo();
65 // extern "C" void LLVMInitializeARMTarget ();
66 // extern "C" void LLVMInitializeARMTargetMC ();
70 #ifndef MONO_CROSS_COMPILE
72 class MonoJITMemoryManager : public JITMemoryManager
78 /* Callbacks installed by mono */
79 AllocCodeMemoryCb *alloc_cb;
82 MonoJITMemoryManager ();
83 ~MonoJITMemoryManager ();
85 void setMemoryWritable (void);
87 void setMemoryExecutable (void);
91 unsigned char *getGOTBase() const {
92 return mm->getGOTBase ();
95 void setPoisonMemory(bool) {
98 unsigned char *startFunctionBody(const Function *F,
99 uintptr_t &ActualSize);
101 unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
104 void endFunctionBody(const Function *F, unsigned char *FunctionStart,
105 unsigned char *FunctionEnd);
107 unsigned char *allocateSpace(intptr_t Size, unsigned Alignment);
109 uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment);
111 void deallocateMemForFunction(const Function *F);
113 unsigned char*startExceptionTable(const Function* F,
114 uintptr_t &ActualSize);
116 void endExceptionTable(const Function *F, unsigned char *TableStart,
117 unsigned char *TableEnd,
118 unsigned char* FrameRegister);
120 virtual void deallocateFunctionBody(void*) {
123 virtual void deallocateExceptionTable(void*) {
126 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
127 StringRef SectionName) {
133 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
134 StringRef SectionName, bool IsReadOnly) {
140 virtual bool applyPermissions(std::string*) {
146 virtual bool finalizeMemory(std::string *ErrMsg = 0) {
152 virtual void* getPointerToNamedFunction(const std::string &Name, bool AbortOnFailure) {
156 err = dlsym_cb (Name.c_str (), &res);
158 outs () << "Unable to resolve: " << Name << ": " << err << "\n";
166 MonoJITMemoryManager::MonoJITMemoryManager ()
168 mm = JITMemoryManager::CreateDefaultMemManager ();
171 MonoJITMemoryManager::~MonoJITMemoryManager ()
177 MonoJITMemoryManager::setMemoryWritable (void)
182 MonoJITMemoryManager::setMemoryExecutable (void)
187 MonoJITMemoryManager::AllocateGOT()
193 MonoJITMemoryManager::startFunctionBody(const Function *F,
194 uintptr_t &ActualSize)
196 // FIXME: This leaks memory
199 return alloc_cb (wrap (F), ActualSize);
203 MonoJITMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
206 return alloc_cb (wrap (F), StubSize);
210 MonoJITMemoryManager::endFunctionBody(const Function *F, unsigned char *FunctionStart,
211 unsigned char *FunctionEnd)
216 MonoJITMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
218 return new unsigned char [Size];
222 MonoJITMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
224 return new unsigned char [Size];
228 MonoJITMemoryManager::deallocateMemForFunction(const Function *F)
233 MonoJITMemoryManager::startExceptionTable(const Function* F,
234 uintptr_t &ActualSize)
236 return startFunctionBody(F, ActualSize);
240 MonoJITMemoryManager::endExceptionTable(const Function *F, unsigned char *TableStart,
241 unsigned char *TableEnd,
242 unsigned char* FrameRegister)
248 class MonoJITMemoryManager {
251 #endif /* !MONO_CROSS_COMPILE */
253 class MonoJITEventListener : public JITEventListener {
256 FunctionEmittedCb *emitted_cb;
258 MonoJITEventListener (FunctionEmittedCb *cb) {
262 virtual void NotifyFunctionEmitted(const Function &F,
263 void *Code, size_t Size,
264 const EmittedFunctionDetails &Details) {
266 * X86TargetMachine::setCodeModelForJIT() sets the code model to Large on amd64,
267 * which means the JIT will generate calls of the form
270 * Our trampoline code can't patch this. Passing CodeModel::Small to createJIT
271 * doesn't seem to work, we need Default. A discussion is here:
272 * http://lists.cs.uiuc.edu/pipermail/llvmdev/2009-December/027999.html
273 * There seems to no way to get the TargeMachine used by an EE either, so we
274 * install a profiler hook and reset the code model here.
275 * This should be inside an ifdef, but we can't include our config.h either,
276 * since its definitions conflict with LLVM's config.h.
277 * The LLVM mono branch contains a workaround.
279 emitted_cb (wrap (&F), Code, (char*)Code + Size);
286 MonoJITMemoryManager *mm;
287 MonoJITEventListener *listener;
288 FunctionPassManager *fpm;
292 mono_llvm_optimize_method (MonoEERef eeref, LLVMValueRef method)
294 MonoEE *mono_ee = (MonoEE*)eeref;
297 * The verifier does some checks on the whole module, leading to quadratic behavior.
299 //verifyFunction (*(unwrap<Function> (method)));
300 mono_ee->fpm->run (*unwrap<Function> (method));
304 mono_llvm_dump_value (LLVMValueRef value)
306 /* Same as LLVMDumpValue (), but print to stdout */
308 outs () << (*unwrap<Value> (value));
311 /* Missing overload for building an alloca with an alignment */
313 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty,
314 LLVMValueRef ArraySize,
315 int alignment, const char *Name)
317 return wrap (unwrap (builder)->Insert (new AllocaInst (unwrap (Ty), unwrap (ArraySize), alignment), Name));
321 mono_llvm_build_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
322 const char *Name, gboolean is_volatile)
324 return wrap(unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name));
328 mono_llvm_build_aligned_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
329 const char *Name, gboolean is_volatile, int alignment)
333 ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
334 ins->setAlignment (alignment);
340 mono_llvm_build_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
341 gboolean is_volatile)
343 return wrap(unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile));
347 mono_llvm_build_aligned_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
348 gboolean is_volatile, int alignment)
352 ins = unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile);
353 ins->setAlignment (alignment);
359 mono_llvm_build_cmpxchg (LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp, LLVMValueRef val)
361 AtomicCmpXchgInst *ins;
363 ins = unwrap(builder)->CreateAtomicCmpXchg (unwrap(ptr), unwrap (cmp), unwrap (val), SequentiallyConsistent);
368 mono_llvm_build_atomic_rmw (LLVMBuilderRef builder, AtomicRMWOp op, LLVMValueRef ptr, LLVMValueRef val)
370 AtomicRMWInst::BinOp aop = AtomicRMWInst::Xchg;
374 case LLVM_ATOMICRMW_OP_XCHG:
375 aop = AtomicRMWInst::Xchg;
377 case LLVM_ATOMICRMW_OP_ADD:
378 aop = AtomicRMWInst::Add;
381 g_assert_not_reached ();
385 ins = unwrap (builder)->CreateAtomicRMW (aop, unwrap (ptr), unwrap (val), AcquireRelease);
390 mono_llvm_build_fence (LLVMBuilderRef builder)
394 ins = unwrap (builder)->CreateFence (AcquireRelease);
399 mono_llvm_replace_uses_of (LLVMValueRef var, LLVMValueRef v)
401 Value *V = ConstantExpr::getTruncOrBitCast (unwrap<Constant> (v), unwrap (var)->getType ());
402 unwrap (var)->replaceAllUsesWith (V);
405 static cl::list<const PassInfo*, bool, PassNameParser>
406 PassList(cl::desc("Optimizations available:"));
409 force_pass_linking (void)
411 // Make sure the rest is linked in, but never executed
412 if (g_getenv ("FOO") != (char*)-1)
415 // This is a subset of the passes in LinkAllPasses.h
416 // The utility passes and the interprocedural passes are commented out
418 (void) llvm::createAAEvalPass();
419 (void) llvm::createAggressiveDCEPass();
420 (void) llvm::createAliasAnalysisCounterPass();
421 (void) llvm::createAliasDebugger();
423 (void) llvm::createArgumentPromotionPass();
424 (void) llvm::createStructRetPromotionPass();
426 (void) llvm::createBasicAliasAnalysisPass();
427 (void) llvm::createLibCallAliasAnalysisPass(0);
428 (void) llvm::createScalarEvolutionAliasAnalysisPass();
429 //(void) llvm::createBlockPlacementPass();
430 (void) llvm::createBreakCriticalEdgesPass();
431 (void) llvm::createCFGSimplificationPass();
433 (void) llvm::createConstantMergePass();
434 (void) llvm::createConstantPropagationPass();
437 (void) llvm::createDeadArgEliminationPass();
439 (void) llvm::createDeadCodeEliminationPass();
440 (void) llvm::createDeadInstEliminationPass();
441 (void) llvm::createDeadStoreEliminationPass();
443 (void) llvm::createDeadTypeEliminationPass();
444 (void) llvm::createDomOnlyPrinterPass();
445 (void) llvm::createDomPrinterPass();
446 (void) llvm::createDomOnlyViewerPass();
447 (void) llvm::createDomViewerPass();
448 (void) llvm::createEdgeProfilerPass();
449 (void) llvm::createOptimalEdgeProfilerPass();
450 (void) llvm::createFunctionInliningPass();
451 (void) llvm::createAlwaysInlinerPass();
452 (void) llvm::createGlobalDCEPass();
453 (void) llvm::createGlobalOptimizerPass();
454 (void) llvm::createGlobalsModRefPass();
455 (void) llvm::createIPConstantPropagationPass();
456 (void) llvm::createIPSCCPPass();
458 (void) llvm::createIndVarSimplifyPass();
459 (void) llvm::createInstructionCombiningPass();
461 (void) llvm::createInternalizePass(false);
463 (void) llvm::createLCSSAPass();
464 (void) llvm::createLICMPass();
465 (void) llvm::createLazyValueInfoPass();
466 //(void) llvm::createLoopDependenceAnalysisPass();
468 (void) llvm::createLoopExtractorPass();
470 (void) llvm::createLoopSimplifyPass();
471 (void) llvm::createLoopStrengthReducePass();
472 (void) llvm::createLoopUnrollPass();
473 (void) llvm::createLoopUnswitchPass();
474 (void) llvm::createLoopRotatePass();
475 (void) llvm::createLowerInvokePass();
477 (void) llvm::createLowerSetJmpPass();
479 (void) llvm::createLowerSwitchPass();
480 (void) llvm::createNoAAPass();
482 (void) llvm::createNoProfileInfoPass();
483 (void) llvm::createProfileEstimatorPass();
484 (void) llvm::createProfileVerifierPass();
485 (void) llvm::createProfileLoaderPass();
487 (void) llvm::createPromoteMemoryToRegisterPass();
488 (void) llvm::createDemoteRegisterToMemoryPass();
490 (void) llvm::createPruneEHPass();
491 (void) llvm::createPostDomOnlyPrinterPass();
492 (void) llvm::createPostDomPrinterPass();
493 (void) llvm::createPostDomOnlyViewerPass();
494 (void) llvm::createPostDomViewerPass();
496 (void) llvm::createReassociatePass();
497 (void) llvm::createSCCPPass();
498 (void) llvm::createScalarReplAggregatesPass();
499 //(void) llvm::createSimplifyLibCallsPass();
501 (void) llvm::createSingleLoopExtractorPass();
502 (void) llvm::createStripSymbolsPass();
503 (void) llvm::createStripNonDebugSymbolsPass();
504 (void) llvm::createStripDeadDebugInfoPass();
505 (void) llvm::createStripDeadPrototypesPass();
506 (void) llvm::createTailCallEliminationPass();
507 (void) llvm::createTailDuplicationPass();
508 (void) llvm::createJumpThreadingPass();
511 (void) llvm::createUnifyFunctionExitNodesPass();
513 (void) llvm::createInstCountPass();
514 (void) llvm::createCodeGenPreparePass();
515 (void) llvm::createGVNPass();
516 (void) llvm::createMemCpyOptPass();
517 (void) llvm::createLoopDeletionPass();
519 (void) llvm::createPostDomTree();
520 (void) llvm::createPostDomFrontier();
521 (void) llvm::createInstructionNamerPass();
522 (void) llvm::createPartialSpecializationPass();
523 (void) llvm::createFunctionAttrsPass();
524 (void) llvm::createMergeFunctionsPass();
525 (void) llvm::createPrintModulePass(0);
526 (void) llvm::createPrintFunctionPass("", 0);
527 (void) llvm::createDbgInfoPrinterPass();
528 (void) llvm::createModuleDebugInfoPrinterPass();
529 (void) llvm::createPartialInliningPass();
530 (void) llvm::createGEPSplitterPass();
531 (void) llvm::createLintPass();
533 (void) llvm::createSinkingPass();
536 #ifndef MONO_CROSS_COMPILE
538 static gboolean inited;
546 force_pass_linking ();
549 LLVMInitializeARMTarget ();
550 LLVMInitializeARMTargetInfo ();
551 LLVMInitializeARMTargetMC ();
553 LLVMInitializeX86Target ();
554 LLVMInitializeX86TargetInfo ();
555 LLVMInitializeX86TargetMC ();
558 PassRegistry &Registry = *PassRegistry::getPassRegistry();
559 initializeCore(Registry);
560 initializeScalarOpts(Registry);
561 initializeAnalysis(Registry);
562 initializeIPA(Registry);
563 initializeTransformUtils(Registry);
564 initializeInstCombine(Registry);
565 initializeTarget(Registry);
567 llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "");
573 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb, DlSymCb *dlsym_cb, LLVMExecutionEngineRef *ee)
580 mono_ee = new MonoEE ();
582 MonoJITMemoryManager *mono_mm = new MonoJITMemoryManager ();
583 mono_mm->alloc_cb = alloc_cb;
584 mono_mm->dlsym_cb = dlsym_cb;
585 mono_ee->mm = mono_mm;
588 * The Default code model doesn't seem to work on amd64,
589 * test_0_fields_with_big_offsets (among others) crashes, because LLVM tries to call
590 * memset using a normal pcrel code which is in 32bit memory, while memset isn't.
594 opts.JITExceptionHandling = 1;
596 EngineBuilder b (unwrap (MP));
598 ExecutionEngine *EE = b.setJITMemoryManager (mono_mm).setTargetOptions (opts).setCodeModel (CodeModel::Large).setAllocateGVsWithCode (true).create ();
600 ExecutionEngine *EE = b.setJITMemoryManager (mono_mm).setTargetOptions (opts).setAllocateGVsWithCode (true).create ();
605 EE->InstallExceptionTableRegister (exception_cb);
606 MonoJITEventListener *listener = new MonoJITEventListener (emitted_cb);
607 EE->RegisterJITEventListener (listener);
608 mono_ee->listener = listener;
610 FunctionPassManager *fpm = new FunctionPassManager (unwrap (MP));
613 fpm->add(new DataLayout(*EE->getDataLayout()));
615 if (PassList.size() > 0) {
616 /* Use the passes specified by the env variable */
617 /* Only the passes in force_pass_linking () can be used */
618 for (unsigned i = 0; i < PassList.size(); ++i) {
619 const PassInfo *PassInf = PassList[i];
622 if (PassInf->getNormalCtor())
623 P = PassInf->getNormalCtor()();
627 /* Use the same passes used by 'opt' by default, without the ipo passes */
628 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";
632 args = g_strsplit (opts, " ", 1000);
633 for (i = 0; args [i]; i++)
635 llvm::cl::ParseCommandLineOptions (i, args, "");
638 for (unsigned i = 0; i < PassList.size(); ++i) {
639 const PassInfo *PassInf = PassList[i];
642 if (PassInf->getNormalCtor())
643 P = PassInf->getNormalCtor()();
644 g_assert (P->getPassKind () == llvm::PT_Function || P->getPassKind () == llvm::PT_Loop);
649 fpm->add(createInstructionCombiningPass());
650 fpm->add(createReassociatePass());
651 fpm->add(createGVNPass());
652 fpm->add(createCFGSimplificationPass());
662 mono_llvm_dispose_ee (MonoEERef *eeref)
664 MonoEE *mono_ee = (MonoEE*)eeref;
668 //delete mono_ee->mm;
669 delete mono_ee->listener;
676 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb, DlSymCb *dlsym_cb, LLVMExecutionEngineRef *ee)
678 g_assert_not_reached ();
683 mono_llvm_dispose_ee (MonoEERef *eeref)
685 g_assert_not_reached ();
690 LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
693 g_assert_not_reached ();
697 LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global)
699 g_assert_not_reached ();
703 #endif /* !MONO_CROSS_COMPILE */