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/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 ();
68 class MonoJITMemoryManager
: public JITMemoryManager
74 /* Callbacks installed by mono */
75 AllocCodeMemoryCb
*alloc_cb
;
78 MonoJITMemoryManager ();
79 ~MonoJITMemoryManager ();
81 void setMemoryWritable (void);
83 void setMemoryExecutable (void);
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
,
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
) {
129 virtual uint8_t *allocateDataSection(uintptr_t Size
, unsigned Alignment
,
130 unsigned SectionID
) {
136 virtual void* getPointerToNamedFunction(const std::string
&Name
, bool AbortOnFailure
) {
137 if (!strcmp (Name
.c_str (), "__bzero")) {
143 err
= dlsym_cb (Name
.c_str (), &res
);
145 outs () << "Unable to resolve: " << Name
<< ": " << err
<< "\n";
154 MonoJITMemoryManager::MonoJITMemoryManager ()
156 mm
= JITMemoryManager::CreateDefaultMemManager ();
159 MonoJITMemoryManager::~MonoJITMemoryManager ()
165 MonoJITMemoryManager::setMemoryWritable (void)
170 MonoJITMemoryManager::setMemoryExecutable (void)
175 MonoJITMemoryManager::AllocateGOT()
181 MonoJITMemoryManager::startFunctionBody(const Function
*F
,
182 uintptr_t &ActualSize
)
184 // FIXME: This leaks memory
187 return alloc_cb (wrap (F
), ActualSize
);
191 MonoJITMemoryManager::allocateStub(const GlobalValue
* F
, unsigned StubSize
,
194 return alloc_cb (wrap (F
), StubSize
);
198 MonoJITMemoryManager::endFunctionBody(const Function
*F
, unsigned char *FunctionStart
,
199 unsigned char *FunctionEnd
)
204 MonoJITMemoryManager::allocateSpace(intptr_t Size
, unsigned Alignment
)
206 return new unsigned char [Size
];
210 MonoJITMemoryManager::allocateGlobal(uintptr_t Size
, unsigned Alignment
)
212 return new unsigned char [Size
];
216 MonoJITMemoryManager::deallocateMemForFunction(const Function
*F
)
221 MonoJITMemoryManager::startExceptionTable(const Function
* F
,
222 uintptr_t &ActualSize
)
224 return startFunctionBody(F
, ActualSize
);
228 MonoJITMemoryManager::endExceptionTable(const Function
*F
, unsigned char *TableStart
,
229 unsigned char *TableEnd
,
230 unsigned char* FrameRegister
)
234 class MonoJITEventListener
: public JITEventListener
{
237 FunctionEmittedCb
*emitted_cb
;
239 MonoJITEventListener (FunctionEmittedCb
*cb
) {
243 virtual void NotifyFunctionEmitted(const Function
&F
,
244 void *Code
, size_t Size
,
245 const EmittedFunctionDetails
&Details
) {
247 * X86TargetMachine::setCodeModelForJIT() sets the code model to Large on amd64,
248 * which means the JIT will generate calls of the form
251 * Our trampoline code can't patch this. Passing CodeModel::Small to createJIT
252 * doesn't seem to work, we need Default. A discussion is here:
253 * http://lists.cs.uiuc.edu/pipermail/llvmdev/2009-December/027999.html
254 * There seems to no way to get the TargeMachine used by an EE either, so we
255 * install a profiler hook and reset the code model here.
256 * This should be inside an ifdef, but we can't include our config.h either,
257 * since its definitions conflict with LLVM's config.h.
260 //#if defined(TARGET_X86) || defined(TARGET_AMD64)
261 #ifndef LLVM_MONO_BRANCH
262 /* The LLVM mono branch contains a workaround, so this is not needed */
263 if (Details
.MF
->getTarget ().getCodeModel () == CodeModel::Large
) {
264 Details
.MF
->getTarget ().setCodeModel (CodeModel::Default
);
269 emitted_cb (wrap (&F
), Code
, (char*)Code
+ Size
);
273 static MonoJITMemoryManager
*mono_mm
;
274 static MonoJITEventListener
*mono_event_listener
;
276 static FunctionPassManager
*fpm
;
279 mono_llvm_optimize_method (LLVMValueRef method
)
281 verifyFunction (*(unwrap
<Function
> (method
)));
282 fpm
->run (*unwrap
<Function
> (method
));
286 mono_llvm_dump_value (LLVMValueRef value
)
288 /* Same as LLVMDumpValue (), but print to stdout */
290 outs () << (*unwrap
<Value
> (value
));
293 /* Missing overload for building an alloca with an alignment */
295 mono_llvm_build_alloca (LLVMBuilderRef builder
, LLVMTypeRef Ty
,
296 LLVMValueRef ArraySize
,
297 int alignment
, const char *Name
)
299 return wrap (unwrap (builder
)->Insert (new AllocaInst (unwrap (Ty
), unwrap (ArraySize
), alignment
), Name
));
303 mono_llvm_build_load (LLVMBuilderRef builder
, LLVMValueRef PointerVal
,
304 const char *Name
, gboolean is_volatile
)
306 return wrap(unwrap(builder
)->CreateLoad(unwrap(PointerVal
), is_volatile
, Name
));
310 mono_llvm_build_aligned_load (LLVMBuilderRef builder
, LLVMValueRef PointerVal
,
311 const char *Name
, gboolean is_volatile
, int alignment
)
315 ins
= unwrap(builder
)->CreateLoad(unwrap(PointerVal
), is_volatile
, Name
);
316 ins
->setAlignment (alignment
);
322 mono_llvm_build_store (LLVMBuilderRef builder
, LLVMValueRef Val
, LLVMValueRef PointerVal
,
323 gboolean is_volatile
)
325 return wrap(unwrap(builder
)->CreateStore(unwrap(Val
), unwrap(PointerVal
), is_volatile
));
329 mono_llvm_build_aligned_store (LLVMBuilderRef builder
, LLVMValueRef Val
, LLVMValueRef PointerVal
,
330 gboolean is_volatile
, int alignment
)
334 ins
= unwrap(builder
)->CreateStore(unwrap(Val
), unwrap(PointerVal
), is_volatile
);
335 ins
->setAlignment (alignment
);
341 mono_llvm_build_cmpxchg (LLVMBuilderRef builder
, LLVMValueRef ptr
, LLVMValueRef cmp
, LLVMValueRef val
)
343 AtomicCmpXchgInst
*ins
;
345 ins
= unwrap(builder
)->CreateAtomicCmpXchg (unwrap(ptr
), unwrap (cmp
), unwrap (val
), SequentiallyConsistent
);
350 mono_llvm_build_atomic_rmw (LLVMBuilderRef builder
, AtomicRMWOp op
, LLVMValueRef ptr
, LLVMValueRef val
)
352 AtomicRMWInst::BinOp aop
= AtomicRMWInst::Xchg
;
356 case LLVM_ATOMICRMW_OP_XCHG
:
357 aop
= AtomicRMWInst::Xchg
;
359 case LLVM_ATOMICRMW_OP_ADD
:
360 aop
= AtomicRMWInst::Add
;
363 g_assert_not_reached ();
367 ins
= unwrap (builder
)->CreateAtomicRMW (aop
, unwrap (ptr
), unwrap (val
), AcquireRelease
);
372 mono_llvm_build_fence (LLVMBuilderRef builder
)
376 ins
= unwrap (builder
)->CreateFence (AcquireRelease
);
381 mono_llvm_replace_uses_of (LLVMValueRef var
, LLVMValueRef v
)
383 Value
*V
= ConstantExpr::getTruncOrBitCast (unwrap
<Constant
> (v
), unwrap (var
)->getType ());
384 unwrap (var
)->replaceAllUsesWith (V
);
387 static cl::list
<const PassInfo
*, bool, PassNameParser
>
388 PassList(cl::desc("Optimizations available:"));
391 force_pass_linking (void)
393 // Make sure the rest is linked in, but never executed
394 if (getenv ("FOO") != (char*)-1)
397 // This is a subset of the passes in LinkAllPasses.h
398 // The utility passes and the interprocedural passes are commented out
400 (void) llvm::createAAEvalPass();
401 (void) llvm::createAggressiveDCEPass();
402 (void) llvm::createAliasAnalysisCounterPass();
403 (void) llvm::createAliasDebugger();
405 (void) llvm::createArgumentPromotionPass();
406 (void) llvm::createStructRetPromotionPass();
408 (void) llvm::createBasicAliasAnalysisPass();
409 (void) llvm::createLibCallAliasAnalysisPass(0);
410 (void) llvm::createScalarEvolutionAliasAnalysisPass();
411 (void) llvm::createBlockPlacementPass();
412 (void) llvm::createBreakCriticalEdgesPass();
413 (void) llvm::createCFGSimplificationPass();
415 (void) llvm::createConstantMergePass();
416 (void) llvm::createConstantPropagationPass();
419 (void) llvm::createDeadArgEliminationPass();
421 (void) llvm::createDeadCodeEliminationPass();
422 (void) llvm::createDeadInstEliminationPass();
423 (void) llvm::createDeadStoreEliminationPass();
425 (void) llvm::createDeadTypeEliminationPass();
426 (void) llvm::createDomOnlyPrinterPass();
427 (void) llvm::createDomPrinterPass();
428 (void) llvm::createDomOnlyViewerPass();
429 (void) llvm::createDomViewerPass();
430 (void) llvm::createEdgeProfilerPass();
431 (void) llvm::createOptimalEdgeProfilerPass();
432 (void) llvm::createFunctionInliningPass();
433 (void) llvm::createAlwaysInlinerPass();
434 (void) llvm::createGlobalDCEPass();
435 (void) llvm::createGlobalOptimizerPass();
436 (void) llvm::createGlobalsModRefPass();
437 (void) llvm::createIPConstantPropagationPass();
438 (void) llvm::createIPSCCPPass();
440 (void) llvm::createIndVarSimplifyPass();
441 (void) llvm::createInstructionCombiningPass();
443 (void) llvm::createInternalizePass(false);
445 (void) llvm::createLCSSAPass();
446 (void) llvm::createLICMPass();
447 (void) llvm::createLazyValueInfoPass();
448 (void) llvm::createLoopDependenceAnalysisPass();
450 (void) llvm::createLoopExtractorPass();
452 (void) llvm::createLoopSimplifyPass();
453 (void) llvm::createLoopStrengthReducePass();
454 (void) llvm::createLoopUnrollPass();
455 (void) llvm::createLoopUnswitchPass();
456 (void) llvm::createLoopRotatePass();
457 (void) llvm::createLowerInvokePass();
459 (void) llvm::createLowerSetJmpPass();
461 (void) llvm::createLowerSwitchPass();
462 (void) llvm::createNoAAPass();
464 (void) llvm::createNoProfileInfoPass();
465 (void) llvm::createProfileEstimatorPass();
466 (void) llvm::createProfileVerifierPass();
467 (void) llvm::createProfileLoaderPass();
469 (void) llvm::createPromoteMemoryToRegisterPass();
470 (void) llvm::createDemoteRegisterToMemoryPass();
472 (void) llvm::createPruneEHPass();
473 (void) llvm::createPostDomOnlyPrinterPass();
474 (void) llvm::createPostDomPrinterPass();
475 (void) llvm::createPostDomOnlyViewerPass();
476 (void) llvm::createPostDomViewerPass();
478 (void) llvm::createReassociatePass();
479 (void) llvm::createSCCPPass();
480 (void) llvm::createScalarReplAggregatesPass();
481 (void) llvm::createSimplifyLibCallsPass();
483 (void) llvm::createSingleLoopExtractorPass();
484 (void) llvm::createStripSymbolsPass();
485 (void) llvm::createStripNonDebugSymbolsPass();
486 (void) llvm::createStripDeadDebugInfoPass();
487 (void) llvm::createStripDeadPrototypesPass();
488 (void) llvm::createTailCallEliminationPass();
489 (void) llvm::createTailDuplicationPass();
490 (void) llvm::createJumpThreadingPass();
493 (void) llvm::createUnifyFunctionExitNodesPass();
495 (void) llvm::createInstCountPass();
496 (void) llvm::createCodeGenPreparePass();
497 (void) llvm::createGVNPass();
498 (void) llvm::createMemCpyOptPass();
499 (void) llvm::createLoopDeletionPass();
501 (void) llvm::createPostDomTree();
502 (void) llvm::createPostDomFrontier();
503 (void) llvm::createInstructionNamerPass();
504 (void) llvm::createPartialSpecializationPass();
505 (void) llvm::createFunctionAttrsPass();
506 (void) llvm::createMergeFunctionsPass();
507 (void) llvm::createPrintModulePass(0);
508 (void) llvm::createPrintFunctionPass("", 0);
509 (void) llvm::createDbgInfoPrinterPass();
510 (void) llvm::createModuleDebugInfoPrinterPass();
511 (void) llvm::createPartialInliningPass();
512 (void) llvm::createGEPSplitterPass();
513 (void) llvm::createLintPass();
515 (void) llvm::createSinkingPass();
518 LLVMExecutionEngineRef
519 mono_llvm_create_ee (LLVMModuleProviderRef MP
, AllocCodeMemoryCb
*alloc_cb
, FunctionEmittedCb
*emitted_cb
, ExceptionTableCb
*exception_cb
, DlSymCb
*dlsym_cb
)
523 force_pass_linking ();
526 LLVMInitializeARMTarget ();
527 LLVMInitializeARMTargetInfo ();
528 LLVMInitializeARMTargetMC ();
530 LLVMInitializeX86Target ();
531 LLVMInitializeX86TargetInfo ();
532 LLVMInitializeX86TargetMC ();
535 mono_mm
= new MonoJITMemoryManager ();
536 mono_mm
->alloc_cb
= alloc_cb
;
537 mono_mm
->dlsym_cb
= dlsym_cb
;
539 //JITExceptionHandling = true;
540 // PrettyStackTrace installs signal handlers which trip up libgc
541 DisablePrettyStackTrace
= true;
544 * The Default code model doesn't seem to work on amd64,
545 * test_0_fields_with_big_offsets (among others) crashes, because LLVM tries to call
546 * memset using a normal pcrel code which is in 32bit memory, while memset isn't.
550 opts
.JITExceptionHandling
= 1;
552 EngineBuilder
b (unwrap (MP
));
554 ExecutionEngine
*EE
= b
.setJITMemoryManager (mono_mm
).setTargetOptions (opts
).setCodeModel (CodeModel::Large
).setAllocateGVsWithCode (true).create ();
556 ExecutionEngine
*EE
= b
.setJITMemoryManager (mono_mm
).setTargetOptions (opts
).setAllocateGVsWithCode (true).create ();
561 ExecutionEngine
*EE
= ExecutionEngine::createJIT (unwrap (MP
), &Error
, mono_mm
, CodeGenOpt::Default
, true, Reloc::Default
, CodeModel::Large
);
563 errs () << "Unable to create LLVM ExecutionEngine: " << Error
<< "\n";
564 g_assert_not_reached ();
568 EE
->InstallExceptionTableRegister (exception_cb
);
569 mono_event_listener
= new MonoJITEventListener (emitted_cb
);
570 EE
->RegisterJITEventListener (mono_event_listener
);
572 fpm
= new FunctionPassManager (unwrap (MP
));
574 fpm
->add(new TargetData(*EE
->getTargetData()));
576 PassRegistry
&Registry
= *PassRegistry::getPassRegistry();
577 initializeCore(Registry
);
578 initializeScalarOpts(Registry
);
579 //initializeIPO(Registry);
580 initializeAnalysis(Registry
);
581 initializeIPA(Registry
);
582 initializeTransformUtils(Registry
);
583 initializeInstCombine(Registry
);
584 //initializeInstrumentation(Registry);
585 initializeTarget(Registry
);
587 llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "", false);
589 if (PassList
.size() > 0) {
590 /* Use the passes specified by the env variable */
591 /* Only the passes in force_pass_linking () can be used */
592 for (unsigned i
= 0; i
< PassList
.size(); ++i
) {
593 const PassInfo
*PassInf
= PassList
[i
];
596 if (PassInf
->getNormalCtor())
597 P
= PassInf
->getNormalCtor()();
601 /* Use the same passes used by 'opt' by default, without the ipo passes */
602 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";
606 args
= g_strsplit (opts
, " ", 1000);
607 for (i
= 0; args
[i
]; i
++)
609 llvm::cl::ParseCommandLineOptions (i
, args
, "", false);
612 for (unsigned i
= 0; i
< PassList
.size(); ++i
) {
613 const PassInfo
*PassInf
= PassList
[i
];
616 if (PassInf
->getNormalCtor())
617 P
= PassInf
->getNormalCtor()();
622 fpm->add(createInstructionCombiningPass());
623 fpm->add(createReassociatePass());
624 fpm->add(createGVNPass());
625 fpm->add(createCFGSimplificationPass());
633 mono_llvm_dispose_ee (LLVMExecutionEngineRef ee
)