Merge pull request #925 from ermshiperete/novell-bug-602934
[mono.git] / mono / mini / mini-llvm-cpp.cpp
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 //
7
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.
16 //
17
18 //
19 // Mono's internal header files are not C++ clean, so avoid including them if 
20 // possible
21 //
22
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
30
31 #include <stdint.h>
32
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 #if LLVM_API_VERSION >= 1
41 #include <llvm/IR/Verifier.h>
42 #else
43 #include <llvm/Analysis/Verifier.h>
44 #endif
45 #include <llvm/Analysis/Passes.h>
46 #include <llvm/Transforms/Scalar.h>
47 #include <llvm/Support/CommandLine.h>
48 #if LLVM_API_VERSION >= 1
49 #include "llvm/IR/LegacyPassNameParser.h"
50 #else
51 #include "llvm/Support/PassNameParser.h"
52 #endif
53 #include "llvm/Support/PrettyStackTrace.h"
54 #include <llvm/CodeGen/Passes.h>
55 #include <llvm/CodeGen/MachineFunctionPass.h>
56 #include <llvm/CodeGen/MachineFunction.h>
57 #include <llvm/CodeGen/MachineFrameInfo.h>
58 #include <llvm/IR/Function.h>
59 #include <llvm/IR/IRBuilder.h>
60 #include <llvm/IR/Module.h>
61 //#include <llvm/LinkAllPasses.h>
62
63 #include "llvm-c/Core.h"
64 #include "llvm-c/ExecutionEngine.h"
65
66 #include "mini-llvm-cpp.h"
67
68 #define LLVM_CHECK_VERSION(major,minor) \
69         ((LLVM_MAJOR_VERSION > (major)) ||                                                                      \
70          ((LLVM_MAJOR_VERSION == (major)) && (LLVM_MINOR_VERSION >= (minor))))
71
72 // extern "C" void LLVMInitializeARMTargetInfo();
73 // extern "C" void LLVMInitializeARMTarget ();
74 // extern "C" void LLVMInitializeARMTargetMC ();
75
76 using namespace llvm;
77
78 #ifndef MONO_CROSS_COMPILE
79
80 class MonoJITMemoryManager : public JITMemoryManager
81 {
82 private:
83         JITMemoryManager *mm;
84
85 public:
86         /* Callbacks installed by mono */
87         AllocCodeMemoryCb *alloc_cb;
88         DlSymCb *dlsym_cb;
89
90         MonoJITMemoryManager ();
91         ~MonoJITMemoryManager ();
92
93         void setMemoryWritable (void);
94
95         void setMemoryExecutable (void);
96
97         void AllocateGOT();
98
99     unsigned char *getGOTBase() const {
100                 return mm->getGOTBase ();
101     }
102
103         void setPoisonMemory(bool) {
104         }
105
106         unsigned char *startFunctionBody(const Function *F, 
107                                                                          uintptr_t &ActualSize);
108   
109         unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
110                                                                  unsigned Alignment);
111   
112         void endFunctionBody(const Function *F, unsigned char *FunctionStart,
113                                                  unsigned char *FunctionEnd);
114
115         unsigned char *allocateSpace(intptr_t Size, unsigned Alignment);
116
117         uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment);
118   
119         void deallocateMemForFunction(const Function *F);
120   
121         unsigned char*startExceptionTable(const Function* F,
122                                                                           uintptr_t &ActualSize);
123   
124         void endExceptionTable(const Function *F, unsigned char *TableStart,
125                                                    unsigned char *TableEnd, 
126                                                    unsigned char* FrameRegister);
127
128         virtual void deallocateFunctionBody(void*) {
129         }
130
131         virtual void deallocateExceptionTable(void*) {
132         }
133
134         virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
135                                                                                  StringRef SectionName) {
136                 // FIXME:
137                 assert(0);
138                 return NULL;
139         }
140
141         virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
142                                                                                  StringRef SectionName, bool IsReadOnly) {
143                 // FIXME:
144                 assert(0);
145                 return NULL;
146         }
147
148         virtual bool applyPermissions(std::string*) {
149                 // FIXME:
150                 assert(0);
151                 return false;
152         }
153
154         virtual bool finalizeMemory(std::string *ErrMsg = 0) {
155                 // FIXME:
156                 assert(0);
157                 return false;
158         }
159
160         virtual void* getPointerToNamedFunction(const std::string &Name, bool AbortOnFailure) {
161                 void *res;
162                 char *err;
163
164                 err = dlsym_cb (Name.c_str (), &res);
165                 if (err) {
166                         outs () << "Unable to resolve: " << Name << ": " << err << "\n";
167                         assert(0);
168                         return NULL;
169                 }
170                 return res;
171         }
172 };
173
174 MonoJITMemoryManager::MonoJITMemoryManager ()
175 {
176         mm = JITMemoryManager::CreateDefaultMemManager ();
177 }
178
179 MonoJITMemoryManager::~MonoJITMemoryManager ()
180 {
181         delete mm;
182 }
183
184 void
185 MonoJITMemoryManager::setMemoryWritable (void)
186 {
187 }
188
189 void
190 MonoJITMemoryManager::setMemoryExecutable (void)
191 {
192 }
193
194 void
195 MonoJITMemoryManager::AllocateGOT()
196 {
197         mm->AllocateGOT ();
198 }
199
200 unsigned char *
201 MonoJITMemoryManager::startFunctionBody(const Function *F, 
202                                         uintptr_t &ActualSize)
203 {
204         // FIXME: This leaks memory
205         if (ActualSize == 0)
206                 ActualSize = 128;
207         return alloc_cb (wrap (F), ActualSize);
208 }
209   
210 unsigned char *
211 MonoJITMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
212                            unsigned Alignment)
213 {
214         return alloc_cb (wrap (F), StubSize);
215 }
216   
217 void
218 MonoJITMemoryManager::endFunctionBody(const Function *F, unsigned char *FunctionStart,
219                                   unsigned char *FunctionEnd)
220 {
221 }
222
223 unsigned char *
224 MonoJITMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
225 {
226         return new unsigned char [Size];
227 }
228
229 uint8_t *
230 MonoJITMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
231 {
232         return new unsigned char [Size];
233 }
234
235 void
236 MonoJITMemoryManager::deallocateMemForFunction(const Function *F)
237 {
238 }
239   
240 unsigned char*
241 MonoJITMemoryManager::startExceptionTable(const Function* F,
242                                           uintptr_t &ActualSize)
243 {
244         return startFunctionBody(F, ActualSize);
245 }
246   
247 void
248 MonoJITMemoryManager::endExceptionTable(const Function *F, unsigned char *TableStart,
249                                         unsigned char *TableEnd, 
250                                         unsigned char* FrameRegister)
251 {
252 }
253
254 #else
255
256 class MonoJITMemoryManager {
257 };
258
259 #endif /* !MONO_CROSS_COMPILE */
260
261 class MonoJITEventListener : public JITEventListener {
262
263 public:
264         FunctionEmittedCb *emitted_cb;
265
266         MonoJITEventListener (FunctionEmittedCb *cb) {
267                 emitted_cb = cb;
268         }
269
270         virtual void NotifyFunctionEmitted(const Function &F,
271                                                                            void *Code, size_t Size,
272                                                                            const EmittedFunctionDetails &Details) {
273                 /*
274                  * X86TargetMachine::setCodeModelForJIT() sets the code model to Large on amd64,
275                  * which means the JIT will generate calls of the form
276                  * mov reg, <imm>
277                  * call *reg
278                  * Our trampoline code can't patch this. Passing CodeModel::Small to createJIT
279                  * doesn't seem to work, we need Default. A discussion is here:
280                  * http://lists.cs.uiuc.edu/pipermail/llvmdev/2009-December/027999.html
281                  * There seems to no way to get the TargeMachine used by an EE either, so we
282                  * install a profiler hook and reset the code model here.
283                  * This should be inside an ifdef, but we can't include our config.h either,
284                  * since its definitions conflict with LLVM's config.h.
285                  * The LLVM mono branch contains a workaround.
286                  */
287                 emitted_cb (wrap (&F), Code, (char*)Code + Size);
288         }
289 };
290
291 class MonoEE {
292 public:
293         ExecutionEngine *EE;
294         MonoJITMemoryManager *mm;
295         MonoJITEventListener *listener;
296         FunctionPassManager *fpm;
297 };
298
299 void
300 mono_llvm_optimize_method (MonoEERef eeref, LLVMValueRef method)
301 {
302         MonoEE *mono_ee = (MonoEE*)eeref;
303
304         /*
305          * The verifier does some checks on the whole module, leading to quadratic behavior.
306          */
307         //verifyFunction (*(unwrap<Function> (method)));
308         mono_ee->fpm->run (*unwrap<Function> (method));
309 }
310
311 void
312 mono_llvm_dump_value (LLVMValueRef value)
313 {
314         /* Same as LLVMDumpValue (), but print to stdout */
315         fflush (stdout);
316         outs () << (*unwrap<Value> (value));
317 }
318
319 /* Missing overload for building an alloca with an alignment */
320 LLVMValueRef
321 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty, 
322                                                 LLVMValueRef ArraySize,
323                                                 int alignment, const char *Name)
324 {
325         return wrap (unwrap (builder)->Insert (new AllocaInst (unwrap (Ty), unwrap (ArraySize), alignment), Name));
326 }
327
328 LLVMValueRef 
329 mono_llvm_build_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
330                                           const char *Name, gboolean is_volatile)
331 {
332         return wrap(unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name));
333 }
334
335 LLVMValueRef 
336 mono_llvm_build_aligned_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
337                                                           const char *Name, gboolean is_volatile, int alignment)
338 {
339         LoadInst *ins;
340
341         ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
342         ins->setAlignment (alignment);
343
344         return wrap(ins);
345 }
346
347 LLVMValueRef 
348 mono_llvm_build_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
349                                           gboolean is_volatile)
350 {
351         return wrap(unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile));
352 }
353
354 LLVMValueRef 
355 mono_llvm_build_aligned_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
356                                                            gboolean is_volatile, int alignment)
357 {
358         StoreInst *ins;
359
360         ins = unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile);
361         ins->setAlignment (alignment);
362
363         return wrap (ins);
364 }
365
366 LLVMValueRef
367 mono_llvm_build_cmpxchg (LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp, LLVMValueRef val)
368 {
369         AtomicCmpXchgInst *ins;
370
371 #if LLVM_API_VERSION >= 1
372         ins = unwrap(builder)->CreateAtomicCmpXchg (unwrap(ptr), unwrap (cmp), unwrap (val), SequentiallyConsistent, SequentiallyConsistent);
373 #else
374         ins = unwrap(builder)->CreateAtomicCmpXchg (unwrap(ptr), unwrap (cmp), unwrap (val), SequentiallyConsistent);
375 #endif
376         return wrap (ins);
377 }
378
379 LLVMValueRef
380 mono_llvm_build_atomic_rmw (LLVMBuilderRef builder, AtomicRMWOp op, LLVMValueRef ptr, LLVMValueRef val)
381 {
382         AtomicRMWInst::BinOp aop = AtomicRMWInst::Xchg;
383         AtomicRMWInst *ins;
384
385         switch (op) {
386         case LLVM_ATOMICRMW_OP_XCHG:
387                 aop = AtomicRMWInst::Xchg;
388                 break;
389         case LLVM_ATOMICRMW_OP_ADD:
390                 aop = AtomicRMWInst::Add;
391                 break;
392         default:
393                 g_assert_not_reached ();
394                 break;
395         }
396
397         ins = unwrap (builder)->CreateAtomicRMW (aop, unwrap (ptr), unwrap (val), AcquireRelease);
398         return wrap (ins);
399 }
400
401 LLVMValueRef
402 mono_llvm_build_fence (LLVMBuilderRef builder)
403 {
404         FenceInst *ins;
405
406         ins = unwrap (builder)->CreateFence (AcquireRelease);
407         return wrap (ins);
408 }
409
410 void
411 mono_llvm_replace_uses_of (LLVMValueRef var, LLVMValueRef v)
412 {
413         Value *V = ConstantExpr::getTruncOrBitCast (unwrap<Constant> (v), unwrap (var)->getType ());
414         unwrap (var)->replaceAllUsesWith (V);
415 }
416
417 static cl::list<const PassInfo*, bool, PassNameParser>
418 PassList(cl::desc("Optimizations available:"));
419
420 static void
421 force_pass_linking (void)
422 {
423         // Make sure the rest is linked in, but never executed
424         if (g_getenv ("FOO") != (char*)-1)
425                 return;
426
427         // This is a subset of the passes in LinkAllPasses.h
428         // The utility passes and the interprocedural passes are commented out
429
430       (void) llvm::createAAEvalPass();
431       (void) llvm::createAggressiveDCEPass();
432       (void) llvm::createAliasAnalysisCounterPass();
433       (void) llvm::createAliasDebugger();
434           /*
435       (void) llvm::createArgumentPromotionPass();
436       (void) llvm::createStructRetPromotionPass();
437           */
438       (void) llvm::createBasicAliasAnalysisPass();
439       (void) llvm::createLibCallAliasAnalysisPass(0);
440       (void) llvm::createScalarEvolutionAliasAnalysisPass();
441       //(void) llvm::createBlockPlacementPass();
442       (void) llvm::createBreakCriticalEdgesPass();
443       (void) llvm::createCFGSimplificationPass();
444           /*
445       (void) llvm::createConstantMergePass();
446       (void) llvm::createConstantPropagationPass();
447           */
448           /*
449       (void) llvm::createDeadArgEliminationPass();
450           */
451       (void) llvm::createDeadCodeEliminationPass();
452       (void) llvm::createDeadInstEliminationPass();
453       (void) llvm::createDeadStoreEliminationPass();
454           /*
455       (void) llvm::createDeadTypeEliminationPass();
456       (void) llvm::createDomOnlyPrinterPass();
457       (void) llvm::createDomPrinterPass();
458       (void) llvm::createDomOnlyViewerPass();
459       (void) llvm::createDomViewerPass();
460       (void) llvm::createEdgeProfilerPass();
461       (void) llvm::createOptimalEdgeProfilerPass();
462       (void) llvm::createFunctionInliningPass();
463       (void) llvm::createAlwaysInlinerPass();
464       (void) llvm::createGlobalDCEPass();
465       (void) llvm::createGlobalOptimizerPass();
466       (void) llvm::createGlobalsModRefPass();
467       (void) llvm::createIPConstantPropagationPass();
468       (void) llvm::createIPSCCPPass();
469           */
470       (void) llvm::createIndVarSimplifyPass();
471       (void) llvm::createInstructionCombiningPass();
472           /*
473       (void) llvm::createInternalizePass(false);
474           */
475       (void) llvm::createLCSSAPass();
476       (void) llvm::createLICMPass();
477       (void) llvm::createLazyValueInfoPass();
478       //(void) llvm::createLoopDependenceAnalysisPass();
479           /*
480       (void) llvm::createLoopExtractorPass();
481           */
482       (void) llvm::createLoopSimplifyPass();
483       (void) llvm::createLoopStrengthReducePass();
484       (void) llvm::createLoopUnrollPass();
485       (void) llvm::createLoopUnswitchPass();
486       (void) llvm::createLoopRotatePass();
487       (void) llvm::createLowerInvokePass();
488           /*
489       (void) llvm::createLowerSetJmpPass();
490           */
491       (void) llvm::createLowerSwitchPass();
492       (void) llvm::createNoAAPass();
493           /*
494       (void) llvm::createNoProfileInfoPass();
495       (void) llvm::createProfileEstimatorPass();
496       (void) llvm::createProfileVerifierPass();
497       (void) llvm::createProfileLoaderPass();
498           */
499       (void) llvm::createPromoteMemoryToRegisterPass();
500       (void) llvm::createDemoteRegisterToMemoryPass();
501           /*
502       (void) llvm::createPruneEHPass();
503       (void) llvm::createPostDomOnlyPrinterPass();
504       (void) llvm::createPostDomPrinterPass();
505       (void) llvm::createPostDomOnlyViewerPass();
506       (void) llvm::createPostDomViewerPass();
507           */
508       (void) llvm::createReassociatePass();
509       (void) llvm::createSCCPPass();
510       (void) llvm::createScalarReplAggregatesPass();
511       //(void) llvm::createSimplifyLibCallsPass();
512           /*
513       (void) llvm::createSingleLoopExtractorPass();
514       (void) llvm::createStripSymbolsPass();
515       (void) llvm::createStripNonDebugSymbolsPass();
516       (void) llvm::createStripDeadDebugInfoPass();
517       (void) llvm::createStripDeadPrototypesPass();
518       (void) llvm::createTailCallEliminationPass();
519       (void) llvm::createTailDuplicationPass();
520       (void) llvm::createJumpThreadingPass();
521           */
522           /*
523       (void) llvm::createUnifyFunctionExitNodesPass();
524           */
525       (void) llvm::createInstCountPass();
526       (void) llvm::createCodeGenPreparePass();
527       (void) llvm::createGVNPass();
528       (void) llvm::createMemCpyOptPass();
529       (void) llvm::createLoopDeletionPass();
530           /*
531       (void) llvm::createPostDomTree();
532       (void) llvm::createPostDomFrontier();
533       (void) llvm::createInstructionNamerPass();
534       (void) llvm::createPartialSpecializationPass();
535       (void) llvm::createFunctionAttrsPass();
536       (void) llvm::createMergeFunctionsPass();
537       (void) llvm::createPrintModulePass(0);
538       (void) llvm::createPrintFunctionPass("", 0);
539       (void) llvm::createDbgInfoPrinterPass();
540       (void) llvm::createModuleDebugInfoPrinterPass();
541       (void) llvm::createPartialInliningPass();
542       (void) llvm::createGEPSplitterPass();
543       (void) llvm::createLintPass();
544           */
545       (void) llvm::createSinkingPass();
546 }
547
548 #ifndef MONO_CROSS_COMPILE
549
550 static gboolean inited;
551
552 static void
553 init_llvm (void)
554 {
555         if (inited)
556                 return;
557
558   force_pass_linking ();
559
560 #ifdef TARGET_ARM
561   LLVMInitializeARMTarget ();
562   LLVMInitializeARMTargetInfo ();
563   LLVMInitializeARMTargetMC ();
564 #else
565   LLVMInitializeX86Target ();
566   LLVMInitializeX86TargetInfo ();
567   LLVMInitializeX86TargetMC ();
568 #endif
569
570   PassRegistry &Registry = *PassRegistry::getPassRegistry();
571   initializeCore(Registry);
572   initializeScalarOpts(Registry);
573   initializeAnalysis(Registry);
574   initializeIPA(Registry);
575   initializeTransformUtils(Registry);
576   initializeInstCombine(Registry);
577   initializeTarget(Registry);
578
579   llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "");
580
581   inited = true;
582 }
583
584 MonoEERef
585 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb, DlSymCb *dlsym_cb, LLVMExecutionEngineRef *ee)
586 {
587   std::string Error;
588   MonoEE *mono_ee;
589
590   init_llvm ();
591
592   mono_ee = new MonoEE ();
593
594   MonoJITMemoryManager *mono_mm = new MonoJITMemoryManager ();
595   mono_mm->alloc_cb = alloc_cb;
596   mono_mm->dlsym_cb = dlsym_cb;
597   mono_ee->mm = mono_mm;
598
599   /*
600    * The Default code model doesn't seem to work on amd64,
601    * test_0_fields_with_big_offsets (among others) crashes, because LLVM tries to call
602    * memset using a normal pcrel code which is in 32bit memory, while memset isn't.
603    */
604
605   TargetOptions opts;
606   opts.JITExceptionHandling = 1;
607
608   EngineBuilder b (unwrap (MP));
609 #ifdef TARGET_AMD64
610   ExecutionEngine *EE = b.setJITMemoryManager (mono_mm).setTargetOptions (opts).setCodeModel (CodeModel::Large).setAllocateGVsWithCode (true).create ();
611 #else
612   ExecutionEngine *EE = b.setJITMemoryManager (mono_mm).setTargetOptions (opts).setAllocateGVsWithCode (true).create ();
613 #endif
614   g_assert (EE);
615   mono_ee->EE = EE;
616
617   EE->InstallExceptionTableRegister (exception_cb);
618   MonoJITEventListener *listener = new MonoJITEventListener (emitted_cb);
619   EE->RegisterJITEventListener (listener);
620   mono_ee->listener = listener;
621
622   FunctionPassManager *fpm = new FunctionPassManager (unwrap (MP));
623   mono_ee->fpm = fpm;
624
625 #if LLVM_API_VERSION >= 1
626   fpm->add(new DataLayoutPass(*EE->getDataLayout()));
627 #else
628   fpm->add(new DataLayout(*EE->getDataLayout()));
629 #endif
630
631   if (PassList.size() > 0) {
632           /* Use the passes specified by the env variable */
633           /* Only the passes in force_pass_linking () can be used */
634           for (unsigned i = 0; i < PassList.size(); ++i) {
635                   const PassInfo *PassInf = PassList[i];
636                   Pass *P = 0;
637
638                   if (PassInf->getNormalCtor())
639                           P = PassInf->getNormalCtor()();
640                   fpm->add (P);
641           }
642   } else {
643           /* Use the same passes used by 'opt' by default, without the ipo passes */
644           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";
645           char **args;
646           int i;
647
648           args = g_strsplit (opts, " ", 1000);
649           for (i = 0; args [i]; i++)
650                   ;
651           llvm::cl::ParseCommandLineOptions (i, args, "");
652           g_strfreev (args);
653
654           for (unsigned i = 0; i < PassList.size(); ++i) {
655                   const PassInfo *PassInf = PassList[i];
656                   Pass *P = 0;
657
658                   if (PassInf->getNormalCtor())
659                           P = PassInf->getNormalCtor()();
660                   g_assert (P->getPassKind () == llvm::PT_Function || P->getPassKind () == llvm::PT_Loop);
661                   fpm->add (P);
662           }
663
664           /*
665           fpm->add(createInstructionCombiningPass());
666           fpm->add(createReassociatePass());
667           fpm->add(createGVNPass());
668           fpm->add(createCFGSimplificationPass());
669           */
670   }
671
672   *ee = wrap (EE);
673
674   return mono_ee;
675 }
676
677 void
678 mono_llvm_dispose_ee (MonoEERef *eeref)
679 {
680         MonoEE *mono_ee = (MonoEE*)eeref;
681
682         delete mono_ee->EE;
683         delete mono_ee->fpm;
684         //delete mono_ee->mm;
685         delete mono_ee->listener;
686         delete mono_ee;
687 }
688
689 #else
690
691 MonoEERef
692 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb, DlSymCb *dlsym_cb, LLVMExecutionEngineRef *ee)
693 {
694         g_assert_not_reached ();
695         return NULL;
696 }
697
698 void
699 mono_llvm_dispose_ee (MonoEERef *eeref)
700 {
701         g_assert_not_reached ();
702 }
703
704 /* Not linked in */
705 void
706 LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
707                                          void* Addr)
708 {
709         g_assert_not_reached ();
710 }
711
712 void*
713 LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global)
714 {
715         g_assert_not_reached ();
716         return NULL;
717 }
718
719 #endif /* !MONO_CROSS_COMPILE */