[runtime] Refactor and unify tls access
[mono.git] / mono / mini / mini-llvm.c
1 /*
2  * mini-llvm.c: llvm "Backend" for the mono JIT
3  *
4  * Copyright 2009-2011 Novell Inc (http://www.novell.com)
5  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
6  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
7  */
8
9 #include "config.h"
10
11 #include <mono/metadata/debug-helpers.h>
12 #include <mono/metadata/debug-mono-symfile.h>
13 #include <mono/metadata/mempool-internals.h>
14 #include <mono/metadata/environment.h>
15 #include <mono/metadata/object-internals.h>
16 #include <mono/metadata/abi-details.h>
17 #include <mono/utils/mono-tls.h>
18 #include <mono/utils/mono-dl.h>
19 #include <mono/utils/mono-time.h>
20 #include <mono/utils/freebsd-dwarf.h>
21
22 #ifndef __STDC_LIMIT_MACROS
23 #define __STDC_LIMIT_MACROS
24 #endif
25 #ifndef __STDC_CONSTANT_MACROS
26 #define __STDC_CONSTANT_MACROS
27 #endif
28
29 #include "llvm-c/BitWriter.h"
30 #include "llvm-c/Analysis.h"
31
32 #include "mini-llvm-cpp.h"
33 #include "llvm-jit.h"
34 #include "aot-compiler.h"
35 #include "mini-llvm.h"
36
37 #ifndef DISABLE_JIT
38
39 #ifdef __MINGW32__
40
41 #include <stddef.h>
42 extern void *memset(void *, int, size_t);
43 void bzero (void *to, size_t count) { memset (to, 0, count); }
44
45 #endif
46
47 #if LLVM_API_VERSION < 4
48 #error "The version of the mono llvm repository is too old."
49 #endif
50
51 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
52
53  /*
54   * Information associated by mono with LLVM modules.
55   */
56 typedef struct {
57         LLVMModuleRef lmodule;
58         LLVMValueRef throw_icall, rethrow, match_exc, throw_corlib_exception, resume_eh;
59         GHashTable *llvm_types;
60         LLVMValueRef got_var;
61         const char *got_symbol;
62         const char *get_method_symbol;
63         const char *get_unbox_tramp_symbol;
64         GHashTable *plt_entries;
65         GHashTable *plt_entries_ji;
66         GHashTable *method_to_lmethod;
67         GHashTable *direct_callables;
68         char **bb_names;
69         int bb_names_len;
70         GPtrArray *used;
71         LLVMTypeRef ptr_type;
72         GPtrArray *subprogram_mds;
73         MonoEERef *mono_ee;
74         LLVMExecutionEngineRef ee;
75         gboolean external_symbols;
76         gboolean emit_dwarf;
77         int max_got_offset;
78         LLVMValueRef personality;
79
80         /* For AOT */
81         MonoAssembly *assembly;
82         char *global_prefix;
83         MonoAotFileInfo aot_info;
84         const char *jit_got_symbol;
85         const char *eh_frame_symbol;
86         LLVMValueRef get_method, get_unbox_tramp;
87         LLVMValueRef init_method, init_method_gshared_mrgctx, init_method_gshared_this, init_method_gshared_vtable;
88         LLVMValueRef code_start, code_end;
89         LLVMValueRef inited_var;
90         int max_inited_idx, max_method_idx;
91         gboolean has_jitted_code;
92         gboolean static_link;
93         gboolean llvm_only;
94         GHashTable *idx_to_lmethod;
95         GHashTable *idx_to_unbox_tramp;
96         /* Maps a MonoMethod to LLVM instructions representing it */
97         GHashTable *method_to_callers;
98         LLVMContextRef context;
99         LLVMValueRef sentinel_exception;
100         void *di_builder, *cu;
101         GHashTable *objc_selector_to_var;
102 } MonoLLVMModule;
103
104 /*
105  * Information associated by the backend with mono basic blocks.
106  */
107 typedef struct {
108         LLVMBasicBlockRef bblock, end_bblock;
109         LLVMValueRef finally_ind;
110         gboolean added, invoke_target;
111         /* 
112          * If this bblock is the start of a finally clause, this is a list of bblocks it
113          * needs to branch to in ENDFINALLY.
114          */
115         GSList *call_handler_return_bbs;
116         /*
117          * If this bblock is the start of a finally clause, this is the bblock that
118          * CALL_HANDLER needs to branch to.
119          */
120         LLVMBasicBlockRef call_handler_target_bb;
121         /* The list of switch statements generated by ENDFINALLY instructions */
122         GSList *endfinally_switch_ins_list;
123         GSList *phi_nodes;
124 } BBInfo;
125
126 /*
127  * Structure containing emit state
128  */
129 typedef struct {
130         MonoMemPool *mempool;
131
132         /* Maps method names to the corresponding LLVMValueRef */
133         GHashTable *emitted_method_decls;
134
135         MonoCompile *cfg;
136         LLVMValueRef lmethod;
137         MonoLLVMModule *module;
138         LLVMModuleRef lmodule;
139         BBInfo *bblocks;
140         int sindex, default_index, ex_index;
141         LLVMBuilderRef builder;
142         LLVMValueRef *values, *addresses;
143         MonoType **vreg_cli_types;
144         LLVMCallInfo *linfo;
145         MonoMethodSignature *sig;
146         GSList *builders;
147         GHashTable *region_to_handler;
148         GHashTable *clause_to_handler;
149         LLVMBuilderRef alloca_builder;
150         LLVMValueRef last_alloca;
151         LLVMValueRef rgctx_arg;
152         LLVMValueRef this_arg;
153         LLVMTypeRef *vreg_types;
154         gboolean *is_vphi;
155         LLVMTypeRef method_type;
156         LLVMBasicBlockRef init_bb, inited_bb;
157         gboolean *is_dead;
158         gboolean *unreachable;
159         gboolean llvm_only;
160         gboolean has_got_access;
161         gboolean is_linkonce;
162         int this_arg_pindex, rgctx_arg_pindex;
163         LLVMValueRef imt_rgctx_loc;
164         GHashTable *llvm_types;
165         LLVMValueRef dbg_md;
166         MonoDebugMethodInfo *minfo;
167         char temp_name [32];
168         /* For every clause, the clauses it is nested in */
169         GSList **nested_in;
170         LLVMValueRef ex_var;
171         GHashTable *exc_meta;
172         GHashTable *method_to_callers;
173         GPtrArray *phi_values;
174         GPtrArray *bblock_list;
175         char *method_name;
176         GHashTable *jit_callees;
177         LLVMValueRef long_bb_break_var;
178 } EmitContext;
179
180 typedef struct {
181         MonoBasicBlock *bb;
182         MonoInst *phi;
183         MonoBasicBlock *in_bb;
184         int sreg;
185 } PhiNode;
186
187 /*
188  * Instruction metadata
189  * This is the same as ins_info, but LREG != IREG.
190  */
191 #ifdef MINI_OP
192 #undef MINI_OP
193 #endif
194 #ifdef MINI_OP3
195 #undef MINI_OP3
196 #endif
197 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
198 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
199 #define NONE ' '
200 #define IREG 'i'
201 #define FREG 'f'
202 #define VREG 'v'
203 #define XREG 'x'
204 #define LREG 'l'
205 /* keep in sync with the enum in mini.h */
206 const char
207 llvm_ins_info[] = {
208 #include "mini-ops.h"
209 };
210 #undef MINI_OP
211 #undef MINI_OP3
212
213 #if SIZEOF_VOID_P == 4
214 #define GET_LONG_IMM(ins) (((guint64)(ins)->inst_ms_word << 32) | (guint64)(guint32)(ins)->inst_ls_word)
215 #else
216 #define GET_LONG_IMM(ins) ((ins)->inst_imm)
217 #endif
218
219 #define LLVM_INS_INFO(opcode) (&llvm_ins_info [((opcode) - OP_START - 1) * 4])
220
221 #if 0
222 #define TRACE_FAILURE(msg) do { printf ("%s\n", msg); } while (0)
223 #else
224 #define TRACE_FAILURE(msg)
225 #endif
226
227 #ifdef TARGET_X86
228 #define IS_TARGET_X86 1
229 #else
230 #define IS_TARGET_X86 0
231 #endif
232
233 #ifdef TARGET_AMD64
234 #define IS_TARGET_AMD64 1
235 #else
236 #define IS_TARGET_AMD64 0
237 #endif
238
239 #define ctx_ok(ctx) (!(ctx)->cfg->disable_llvm)
240
241 static LLVMIntPredicate cond_to_llvm_cond [] = {
242         LLVMIntEQ,
243         LLVMIntNE,
244         LLVMIntSLE,
245         LLVMIntSGE,
246         LLVMIntSLT,
247         LLVMIntSGT,
248         LLVMIntULE,
249         LLVMIntUGE,
250         LLVMIntULT,
251         LLVMIntUGT,
252 };
253
254 static LLVMRealPredicate fpcond_to_llvm_cond [] = {
255         LLVMRealOEQ,
256         LLVMRealUNE,
257         LLVMRealOLE,
258         LLVMRealOGE,
259         LLVMRealOLT,
260         LLVMRealOGT,
261         LLVMRealULE,
262         LLVMRealUGE,
263         LLVMRealULT,
264         LLVMRealUGT,
265 };
266
267 static MonoNativeTlsKey current_cfg_tls_id;
268
269 static MonoLLVMModule aot_module;
270
271 static GHashTable *intrins_id_to_name;
272 static GHashTable *intrins_name_to_id;
273
274 static void init_jit_module (MonoDomain *domain);
275
276 static void emit_dbg_loc (EmitContext *ctx, LLVMBuilderRef builder, const unsigned char *cil_code);
277 static LLVMValueRef emit_dbg_subprogram (EmitContext *ctx, MonoCompile *cfg, LLVMValueRef method, const char *name);
278 static void emit_dbg_info (MonoLLVMModule *module, const char *filename, const char *cu_name);
279 static void emit_cond_system_exception (EmitContext *ctx, MonoBasicBlock *bb, const char *exc_type, LLVMValueRef cmp);
280 static LLVMValueRef get_intrinsic (EmitContext *ctx, const char *name);
281 static void decode_llvm_eh_info (EmitContext *ctx, gpointer eh_frame);
282
283 static inline void
284 set_failure (EmitContext *ctx, const char *message)
285 {
286         TRACE_FAILURE (reason);
287         ctx->cfg->exception_message = g_strdup (message);
288         ctx->cfg->disable_llvm = TRUE;
289 }
290
291 /*
292  * IntPtrType:
293  *
294  *   The LLVM type with width == sizeof (gpointer)
295  */
296 static LLVMTypeRef
297 IntPtrType (void)
298 {
299         return sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type ();
300 }
301
302 static LLVMTypeRef
303 ObjRefType (void)
304 {
305         return sizeof (gpointer) == 8 ? LLVMPointerType (LLVMInt64Type (), 0) : LLVMPointerType (LLVMInt32Type (), 0);
306 }
307
308 static LLVMTypeRef
309 ThisType (void)
310 {
311         return sizeof (gpointer) == 8 ? LLVMPointerType (LLVMInt64Type (), 0) : LLVMPointerType (LLVMInt32Type (), 0);
312 }
313
314 /*
315  * get_vtype_size:
316  *
317  *   Return the size of the LLVM representation of the vtype T.
318  */
319 static guint32
320 get_vtype_size (MonoType *t)
321 {
322         int size;
323
324         size = mono_class_value_size (mono_class_from_mono_type (t), NULL);
325
326         /* LLVMArgAsIArgs depends on this since it stores whole words */
327         while (size < 2 * sizeof (gpointer) && mono_is_power_of_two (size) == -1)
328                 size ++;
329
330         return size;
331 }
332
333 /*
334  * simd_class_to_llvm_type:
335  *
336  *   Return the LLVM type corresponding to the Mono.SIMD class KLASS
337  */
338 static LLVMTypeRef
339 simd_class_to_llvm_type (EmitContext *ctx, MonoClass *klass)
340 {
341         if (!strcmp (klass->name, "Vector2d")) {
342                 return LLVMVectorType (LLVMDoubleType (), 2);
343         } else if (!strcmp (klass->name, "Vector2l")) {
344                 return LLVMVectorType (LLVMInt64Type (), 2);
345         } else if (!strcmp (klass->name, "Vector2ul")) {
346                 return LLVMVectorType (LLVMInt64Type (), 2);
347         } else if (!strcmp (klass->name, "Vector4i")) {
348                 return LLVMVectorType (LLVMInt32Type (), 4);
349         } else if (!strcmp (klass->name, "Vector4ui")) {
350                 return LLVMVectorType (LLVMInt32Type (), 4);
351         } else if (!strcmp (klass->name, "Vector4f")) {
352                 return LLVMVectorType (LLVMFloatType (), 4);
353         } else if (!strcmp (klass->name, "Vector8s")) {
354                 return LLVMVectorType (LLVMInt16Type (), 8);
355         } else if (!strcmp (klass->name, "Vector8us")) {
356                 return LLVMVectorType (LLVMInt16Type (), 8);
357         } else if (!strcmp (klass->name, "Vector16sb")) {
358                 return LLVMVectorType (LLVMInt8Type (), 16);
359         } else if (!strcmp (klass->name, "Vector16b")) {
360                 return LLVMVectorType (LLVMInt8Type (), 16);
361         } else if (!strcmp (klass->name, "Vector2")) {
362                 /* System.Numerics */
363                 return LLVMVectorType (LLVMFloatType (), 4);
364         } else if (!strcmp (klass->name, "Vector3")) {
365                 return LLVMVectorType (LLVMFloatType (), 4);
366         } else if (!strcmp (klass->name, "Vector4")) {
367                 return LLVMVectorType (LLVMFloatType (), 4);
368         } else if (!strcmp (klass->name, "Vector`1")) {
369                 MonoType *etype = mono_class_get_generic_class (klass)->context.class_inst->type_argv [0];
370                 switch (etype->type) {
371                 case MONO_TYPE_I1:
372                 case MONO_TYPE_U1:
373                         return LLVMVectorType (LLVMInt8Type (), 16);
374                 case MONO_TYPE_I2:
375                 case MONO_TYPE_U2:
376                         return LLVMVectorType (LLVMInt16Type (), 8);
377                 case MONO_TYPE_I4:
378                 case MONO_TYPE_U4:
379                         return LLVMVectorType (LLVMInt32Type (), 4);
380                 case MONO_TYPE_I8:
381                 case MONO_TYPE_U8:
382                         return LLVMVectorType (LLVMInt64Type (), 2);
383                 case MONO_TYPE_R4:
384                         return LLVMVectorType (LLVMFloatType (), 4);
385                 case MONO_TYPE_R8:
386                         return LLVMVectorType (LLVMDoubleType (), 2);
387                 default:
388                         g_assert_not_reached ();
389                         return NULL;
390                 }
391         } else {
392                 printf ("%s\n", klass->name);
393                 NOT_IMPLEMENTED;
394                 return NULL;
395         }
396 }
397
398 /* Return the 128 bit SIMD type corresponding to the mono type TYPE */
399 static inline G_GNUC_UNUSED LLVMTypeRef
400 type_to_simd_type (int type)
401 {
402         switch (type) {
403         case MONO_TYPE_I1:
404                 return LLVMVectorType (LLVMInt8Type (), 16);
405         case MONO_TYPE_I2:
406                 return LLVMVectorType (LLVMInt16Type (), 8);
407         case MONO_TYPE_I4:
408                 return LLVMVectorType (LLVMInt32Type (), 4);
409         case MONO_TYPE_I8:
410                 return LLVMVectorType (LLVMInt64Type (), 2);
411         case MONO_TYPE_R8:
412                 return LLVMVectorType (LLVMDoubleType (), 2);
413         case MONO_TYPE_R4:
414                 return LLVMVectorType (LLVMFloatType (), 4);
415         default:
416                 g_assert_not_reached ();
417                 return NULL;
418         }
419 }
420
421 static LLVMTypeRef
422 create_llvm_type_for_type (MonoLLVMModule *module, MonoClass *klass)
423 {
424         int i, size, nfields, esize;
425         LLVMTypeRef *eltypes;
426         char *name;
427         MonoType *t;
428         LLVMTypeRef ltype;
429
430         t = &klass->byval_arg;
431
432         if (mini_type_is_hfa (t, &nfields, &esize)) {
433                 /*
434                  * This is needed on arm64 where HFAs are returned in
435                  * registers.
436                  */
437                 size = nfields;
438                 eltypes = g_new (LLVMTypeRef, size);
439                 for (i = 0; i < size; ++i)
440                         eltypes [i] = esize == 4 ? LLVMFloatType () : LLVMDoubleType ();
441         } else {
442                 size = get_vtype_size (t);
443
444                 eltypes = g_new (LLVMTypeRef, size);
445                 for (i = 0; i < size; ++i)
446                         eltypes [i] = LLVMInt8Type ();
447         }
448
449         name = mono_type_full_name (&klass->byval_arg);
450         ltype = LLVMStructCreateNamed (module->context, name);
451         LLVMStructSetBody (ltype, eltypes, size, FALSE);
452         g_free (eltypes);
453         g_free (name);
454
455         return ltype;
456 }
457
458 /*
459  * type_to_llvm_type:
460  *
461  *   Return the LLVM type corresponding to T.
462  */
463 static LLVMTypeRef
464 type_to_llvm_type (EmitContext *ctx, MonoType *t)
465 {
466         t = mini_get_underlying_type (t);
467
468         switch (t->type) {
469         case MONO_TYPE_VOID:
470                 return LLVMVoidType ();
471         case MONO_TYPE_I1:
472                 return LLVMInt8Type ();
473         case MONO_TYPE_I2:
474                 return LLVMInt16Type ();
475         case MONO_TYPE_I4:
476                 return LLVMInt32Type ();
477         case MONO_TYPE_U1:
478                 return LLVMInt8Type ();
479         case MONO_TYPE_U2:
480                 return LLVMInt16Type ();
481         case MONO_TYPE_U4:
482                 return LLVMInt32Type ();
483         case MONO_TYPE_BOOLEAN:
484                 return LLVMInt8Type ();
485         case MONO_TYPE_I8:
486         case MONO_TYPE_U8:
487                 return LLVMInt64Type ();
488         case MONO_TYPE_CHAR:
489                 return LLVMInt16Type ();
490         case MONO_TYPE_R4:
491                 return LLVMFloatType ();
492         case MONO_TYPE_R8:
493                 return LLVMDoubleType ();
494         case MONO_TYPE_I:
495         case MONO_TYPE_U:
496                 return IntPtrType ();
497         case MONO_TYPE_OBJECT:
498         case MONO_TYPE_CLASS:
499         case MONO_TYPE_ARRAY:
500         case MONO_TYPE_SZARRAY:
501         case MONO_TYPE_STRING:
502         case MONO_TYPE_PTR:
503                 return ObjRefType ();
504         case MONO_TYPE_VAR:
505         case MONO_TYPE_MVAR:
506                 /* Because of generic sharing */
507                 return ObjRefType ();
508         case MONO_TYPE_GENERICINST:
509                 if (!mono_type_generic_inst_is_valuetype (t))
510                         return ObjRefType ();
511                 /* Fall through */
512         case MONO_TYPE_VALUETYPE:
513         case MONO_TYPE_TYPEDBYREF: {
514                 MonoClass *klass;
515                 LLVMTypeRef ltype;
516
517                 klass = mono_class_from_mono_type (t);
518
519                 if (MONO_CLASS_IS_SIMD (ctx->cfg, klass))
520                         return simd_class_to_llvm_type (ctx, klass);
521
522                 if (klass->enumtype)
523                         return type_to_llvm_type (ctx, mono_class_enum_basetype (klass));
524
525                 ltype = (LLVMTypeRef)g_hash_table_lookup (ctx->module->llvm_types, klass);
526                 if (!ltype) {
527                         ltype = create_llvm_type_for_type (ctx->module, klass);
528                         g_hash_table_insert (ctx->module->llvm_types, klass, ltype);
529                 }
530                 return ltype;
531         }
532
533         default:
534                 printf ("X: %d\n", t->type);
535                 ctx->cfg->exception_message = g_strdup_printf ("type %s", mono_type_full_name (t));
536                 ctx->cfg->disable_llvm = TRUE;
537                 return NULL;
538         }
539 }
540
541 /*
542  * type_is_unsigned:
543  *
544  *   Return whenever T is an unsigned int type.
545  */
546 static gboolean
547 type_is_unsigned (EmitContext *ctx, MonoType *t)
548 {
549         t = mini_get_underlying_type (t);
550         if (t->byref)
551                 return FALSE;
552         switch (t->type) {
553         case MONO_TYPE_U1:
554         case MONO_TYPE_U2:
555         case MONO_TYPE_CHAR:
556         case MONO_TYPE_U4:
557         case MONO_TYPE_U8:
558                 return TRUE;
559         default:
560                 return FALSE;
561         }
562 }
563
564 /*
565  * type_to_llvm_arg_type:
566  *
567  *   Same as type_to_llvm_type, but treat i8/i16 as i32.
568  */
569 static LLVMTypeRef
570 type_to_llvm_arg_type (EmitContext *ctx, MonoType *t)
571 {
572         LLVMTypeRef ptype = type_to_llvm_type (ctx, t);
573
574         if (ctx->cfg->llvm_only)
575                 return ptype;
576
577         /*
578          * This works on all abis except arm64/ios which passes multiple
579          * arguments in one stack slot.
580          */
581 #ifndef TARGET_ARM64
582         if (ptype == LLVMInt8Type () || ptype == LLVMInt16Type ()) {
583                 /* 
584                  * LLVM generates code which only sets the lower bits, while JITted
585                  * code expects all the bits to be set.
586                  */
587                 ptype = LLVMInt32Type ();
588         }
589 #endif
590
591         return ptype;
592 }
593
594 /*
595  * llvm_type_to_stack_type:
596  *
597  *   Return the LLVM type which needs to be used when a value of type TYPE is pushed
598  * on the IL stack.
599  */
600 static G_GNUC_UNUSED LLVMTypeRef
601 llvm_type_to_stack_type (MonoCompile *cfg, LLVMTypeRef type)
602 {
603         if (type == NULL)
604                 return NULL;
605         if (type == LLVMInt8Type ())
606                 return LLVMInt32Type ();
607         else if (type == LLVMInt16Type ())
608                 return LLVMInt32Type ();
609         else if (!cfg->r4fp && type == LLVMFloatType ())
610                 return LLVMDoubleType ();
611         else
612                 return type;
613 }
614
615 /*
616  * regtype_to_llvm_type:
617  *
618  *   Return the LLVM type corresponding to the regtype C used in instruction 
619  * descriptions.
620  */
621 static LLVMTypeRef
622 regtype_to_llvm_type (char c)
623 {
624         switch (c) {
625         case 'i':
626                 return LLVMInt32Type ();
627         case 'l':
628                 return LLVMInt64Type ();
629         case 'f':
630                 return LLVMDoubleType ();
631         default:
632                 return NULL;
633         }
634 }
635
636 /*
637  * op_to_llvm_type:
638  *
639  *   Return the LLVM type corresponding to the unary/binary opcode OPCODE.
640  */
641 static LLVMTypeRef
642 op_to_llvm_type (int opcode)
643 {
644         switch (opcode) {
645         case OP_ICONV_TO_I1:
646         case OP_LCONV_TO_I1:
647                 return LLVMInt8Type ();
648         case OP_ICONV_TO_U1:
649         case OP_LCONV_TO_U1:
650                 return LLVMInt8Type ();
651         case OP_ICONV_TO_I2:
652         case OP_LCONV_TO_I2:
653                 return LLVMInt16Type ();
654         case OP_ICONV_TO_U2:
655         case OP_LCONV_TO_U2:
656                 return LLVMInt16Type ();
657         case OP_ICONV_TO_I4:
658         case OP_LCONV_TO_I4:
659                 return LLVMInt32Type ();
660         case OP_ICONV_TO_U4:
661         case OP_LCONV_TO_U4:
662                 return LLVMInt32Type ();
663         case OP_ICONV_TO_I8:
664                 return LLVMInt64Type ();
665         case OP_ICONV_TO_R4:
666                 return LLVMFloatType ();
667         case OP_ICONV_TO_R8:
668                 return LLVMDoubleType ();
669         case OP_ICONV_TO_U8:
670                 return LLVMInt64Type ();
671         case OP_FCONV_TO_I4:
672                 return LLVMInt32Type ();
673         case OP_FCONV_TO_I8:
674                 return LLVMInt64Type ();
675         case OP_FCONV_TO_I1:
676         case OP_FCONV_TO_U1:
677         case OP_RCONV_TO_I1:
678         case OP_RCONV_TO_U1:
679                 return LLVMInt8Type ();
680         case OP_FCONV_TO_I2:
681         case OP_FCONV_TO_U2:
682         case OP_RCONV_TO_I2:
683         case OP_RCONV_TO_U2:
684                 return LLVMInt16Type ();
685         case OP_RCONV_TO_U4:
686                 return LLVMInt32Type ();
687         case OP_FCONV_TO_I:
688         case OP_FCONV_TO_U:
689                 return sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type ();
690         case OP_IADD_OVF:
691         case OP_IADD_OVF_UN:
692         case OP_ISUB_OVF:
693         case OP_ISUB_OVF_UN:
694         case OP_IMUL_OVF:
695         case OP_IMUL_OVF_UN:
696                 return LLVMInt32Type ();
697         case OP_LADD_OVF:
698         case OP_LADD_OVF_UN:
699         case OP_LSUB_OVF:
700         case OP_LSUB_OVF_UN:
701         case OP_LMUL_OVF:
702         case OP_LMUL_OVF_UN:
703                 return LLVMInt64Type ();
704         default:
705                 printf ("%s\n", mono_inst_name (opcode));
706                 g_assert_not_reached ();
707                 return NULL;
708         }
709 }               
710
711 #define CLAUSE_START(clause) ((clause)->try_offset)
712 #define CLAUSE_END(clause) (((clause))->try_offset + ((clause))->try_len)
713
714 /*
715  * load_store_to_llvm_type:
716  *
717  *   Return the size/sign/zero extension corresponding to the load/store opcode
718  * OPCODE.
719  */
720 static LLVMTypeRef
721 load_store_to_llvm_type (int opcode, int *size, gboolean *sext, gboolean *zext)
722 {
723         *sext = FALSE;
724         *zext = FALSE;
725
726         switch (opcode) {
727         case OP_LOADI1_MEMBASE:
728         case OP_STOREI1_MEMBASE_REG:
729         case OP_STOREI1_MEMBASE_IMM:
730         case OP_ATOMIC_LOAD_I1:
731         case OP_ATOMIC_STORE_I1:
732                 *size = 1;
733                 *sext = TRUE;
734                 return LLVMInt8Type ();
735         case OP_LOADU1_MEMBASE:
736         case OP_LOADU1_MEM:
737         case OP_ATOMIC_LOAD_U1:
738         case OP_ATOMIC_STORE_U1:
739                 *size = 1;
740                 *zext = TRUE;
741                 return LLVMInt8Type ();
742         case OP_LOADI2_MEMBASE:
743         case OP_STOREI2_MEMBASE_REG:
744         case OP_STOREI2_MEMBASE_IMM:
745         case OP_ATOMIC_LOAD_I2:
746         case OP_ATOMIC_STORE_I2:
747                 *size = 2;
748                 *sext = TRUE;
749                 return LLVMInt16Type ();
750         case OP_LOADU2_MEMBASE:
751         case OP_LOADU2_MEM:
752         case OP_ATOMIC_LOAD_U2:
753         case OP_ATOMIC_STORE_U2:
754                 *size = 2;
755                 *zext = TRUE;
756                 return LLVMInt16Type ();
757         case OP_LOADI4_MEMBASE:
758         case OP_LOADU4_MEMBASE:
759         case OP_LOADI4_MEM:
760         case OP_LOADU4_MEM:
761         case OP_STOREI4_MEMBASE_REG:
762         case OP_STOREI4_MEMBASE_IMM:
763         case OP_ATOMIC_LOAD_I4:
764         case OP_ATOMIC_STORE_I4:
765         case OP_ATOMIC_LOAD_U4:
766         case OP_ATOMIC_STORE_U4:
767                 *size = 4;
768                 return LLVMInt32Type ();
769         case OP_LOADI8_MEMBASE:
770         case OP_LOADI8_MEM:
771         case OP_STOREI8_MEMBASE_REG:
772         case OP_STOREI8_MEMBASE_IMM:
773         case OP_ATOMIC_LOAD_I8:
774         case OP_ATOMIC_STORE_I8:
775         case OP_ATOMIC_LOAD_U8:
776         case OP_ATOMIC_STORE_U8:
777                 *size = 8;
778                 return LLVMInt64Type ();
779         case OP_LOADR4_MEMBASE:
780         case OP_STORER4_MEMBASE_REG:
781         case OP_ATOMIC_LOAD_R4:
782         case OP_ATOMIC_STORE_R4:
783                 *size = 4;
784                 return LLVMFloatType ();
785         case OP_LOADR8_MEMBASE:
786         case OP_STORER8_MEMBASE_REG:
787         case OP_ATOMIC_LOAD_R8:
788         case OP_ATOMIC_STORE_R8:
789                 *size = 8;
790                 return LLVMDoubleType ();
791         case OP_LOAD_MEMBASE:
792         case OP_LOAD_MEM:
793         case OP_STORE_MEMBASE_REG:
794         case OP_STORE_MEMBASE_IMM:
795                 *size = sizeof (gpointer);
796                 return IntPtrType ();
797         default:
798                 g_assert_not_reached ();
799                 return NULL;
800         }
801 }
802
803 /*
804  * ovf_op_to_intrins:
805  *
806  *   Return the LLVM intrinsics corresponding to the overflow opcode OPCODE.
807  */
808 static const char*
809 ovf_op_to_intrins (int opcode)
810 {
811         switch (opcode) {
812         case OP_IADD_OVF:
813                 return "llvm.sadd.with.overflow.i32";
814         case OP_IADD_OVF_UN:
815                 return "llvm.uadd.with.overflow.i32";
816         case OP_ISUB_OVF:
817                 return "llvm.ssub.with.overflow.i32";
818         case OP_ISUB_OVF_UN:
819                 return "llvm.usub.with.overflow.i32";
820         case OP_IMUL_OVF:
821                 return "llvm.smul.with.overflow.i32";
822         case OP_IMUL_OVF_UN:
823                 return "llvm.umul.with.overflow.i32";
824         case OP_LADD_OVF:
825                 return "llvm.sadd.with.overflow.i64";
826         case OP_LADD_OVF_UN:
827                 return "llvm.uadd.with.overflow.i64";
828         case OP_LSUB_OVF:
829                 return "llvm.ssub.with.overflow.i64";
830         case OP_LSUB_OVF_UN:
831                 return "llvm.usub.with.overflow.i64";
832         case OP_LMUL_OVF:
833                 return "llvm.smul.with.overflow.i64";
834         case OP_LMUL_OVF_UN:
835                 return "llvm.umul.with.overflow.i64";
836         default:
837                 g_assert_not_reached ();
838                 return NULL;
839         }
840 }
841
842 static const char*
843 simd_op_to_intrins (int opcode)
844 {
845         switch (opcode) {
846 #if defined(TARGET_X86) || defined(TARGET_AMD64)
847         case OP_MINPD:
848                 return "llvm.x86.sse2.min.pd";
849         case OP_MINPS:
850                 return "llvm.x86.sse.min.ps";
851         case OP_MAXPD:
852                 return "llvm.x86.sse2.max.pd";
853         case OP_MAXPS:
854                 return "llvm.x86.sse.max.ps";
855         case OP_HADDPD:
856                 return "llvm.x86.sse3.hadd.pd";
857         case OP_HADDPS:
858                 return "llvm.x86.sse3.hadd.ps";
859         case OP_HSUBPD:
860                 return "llvm.x86.sse3.hsub.pd";
861         case OP_HSUBPS:
862                 return "llvm.x86.sse3.hsub.ps";
863         case OP_ADDSUBPS:
864                 return "llvm.x86.sse3.addsub.ps";
865         case OP_ADDSUBPD:
866                 return "llvm.x86.sse3.addsub.pd";
867         case OP_EXTRACT_MASK:
868                 return "llvm.x86.sse2.pmovmskb.128";
869         case OP_PSHRW:
870         case OP_PSHRW_REG:
871                 return "llvm.x86.sse2.psrli.w";
872         case OP_PSHRD:
873         case OP_PSHRD_REG:
874                 return "llvm.x86.sse2.psrli.d";
875         case OP_PSHRQ:
876         case OP_PSHRQ_REG:
877                 return "llvm.x86.sse2.psrli.q";
878         case OP_PSHLW:
879         case OP_PSHLW_REG:
880                 return "llvm.x86.sse2.pslli.w";
881         case OP_PSHLD:
882         case OP_PSHLD_REG:
883                 return "llvm.x86.sse2.pslli.d";
884         case OP_PSHLQ:
885         case OP_PSHLQ_REG:
886                 return "llvm.x86.sse2.pslli.q";
887         case OP_PSARW:
888         case OP_PSARW_REG:
889                 return "llvm.x86.sse2.psrai.w";
890         case OP_PSARD:
891         case OP_PSARD_REG:
892                 return "llvm.x86.sse2.psrai.d";
893         case OP_PADDB_SAT:
894                 return "llvm.x86.sse2.padds.b";
895         case OP_PADDW_SAT:
896                 return "llvm.x86.sse2.padds.w";
897         case OP_PSUBB_SAT:
898                 return "llvm.x86.sse2.psubs.b";
899         case OP_PSUBW_SAT:
900                 return "llvm.x86.sse2.psubs.w";
901         case OP_PADDB_SAT_UN:
902                 return "llvm.x86.sse2.paddus.b";
903         case OP_PADDW_SAT_UN:
904                 return "llvm.x86.sse2.paddus.w";
905         case OP_PSUBB_SAT_UN:
906                 return "llvm.x86.sse2.psubus.b";
907         case OP_PSUBW_SAT_UN:
908                 return "llvm.x86.sse2.psubus.w";
909         case OP_PAVGB_UN:
910                 return "llvm.x86.sse2.pavg.b";
911         case OP_PAVGW_UN:
912                 return "llvm.x86.sse2.pavg.w";
913         case OP_SQRTPS:
914                 return "llvm.x86.sse.sqrt.ps";
915         case OP_SQRTPD:
916                 return "llvm.x86.sse2.sqrt.pd";
917         case OP_RSQRTPS:
918                 return "llvm.x86.sse.rsqrt.ps";
919         case OP_RCPPS:
920                 return "llvm.x86.sse.rcp.ps";
921         case OP_CVTDQ2PD:
922                 return "llvm.x86.sse2.cvtdq2pd";
923         case OP_CVTDQ2PS:
924                 return "llvm.x86.sse2.cvtdq2ps";
925         case OP_CVTPD2DQ:
926                 return "llvm.x86.sse2.cvtpd2dq";
927         case OP_CVTPS2DQ:
928                 return "llvm.x86.sse2.cvtps2dq";
929         case OP_CVTPD2PS:
930                 return "llvm.x86.sse2.cvtpd2ps";
931         case OP_CVTPS2PD:
932                 return "llvm.x86.sse2.cvtps2pd";
933         case OP_CVTTPD2DQ:
934                 return "llvm.x86.sse2.cvttpd2dq";
935         case OP_CVTTPS2DQ:
936                 return "llvm.x86.sse2.cvttps2dq";
937         case OP_PACKW:
938                 return "llvm.x86.sse2.packsswb.128";
939         case OP_PACKD:
940                 return "llvm.x86.sse2.packssdw.128";
941         case OP_PACKW_UN:
942                 return "llvm.x86.sse2.packuswb.128";
943         case OP_PACKD_UN:
944                 return "llvm.x86.sse41.packusdw";
945         case OP_PMULW_HIGH:
946                 return "llvm.x86.sse2.pmulh.w";
947         case OP_PMULW_HIGH_UN:
948                 return "llvm.x86.sse2.pmulhu.w";
949         case OP_DPPS:
950                 return "llvm.x86.sse41.dpps";
951 #endif
952         default:
953                 g_assert_not_reached ();
954                 return NULL;
955         }
956 }
957
958 static LLVMTypeRef
959 simd_op_to_llvm_type (int opcode)
960 {
961 #if defined(TARGET_X86) || defined(TARGET_AMD64)
962         switch (opcode) {
963         case OP_EXTRACT_R8:
964         case OP_EXPAND_R8:
965                 return type_to_simd_type (MONO_TYPE_R8);
966         case OP_EXTRACT_I8:
967         case OP_EXPAND_I8:
968                 return type_to_simd_type (MONO_TYPE_I8);
969         case OP_EXTRACT_I4:
970         case OP_EXPAND_I4:
971                 return type_to_simd_type (MONO_TYPE_I4);
972         case OP_EXTRACT_I2:
973         case OP_EXTRACT_U2:
974         case OP_EXTRACTX_U2:
975         case OP_EXPAND_I2:
976                 return type_to_simd_type (MONO_TYPE_I2);
977         case OP_EXTRACT_I1:
978         case OP_EXTRACT_U1:
979         case OP_EXPAND_I1:
980                 return type_to_simd_type (MONO_TYPE_I1);
981         case OP_EXPAND_R4:
982                 return type_to_simd_type (MONO_TYPE_R4);
983         case OP_CVTDQ2PD:
984         case OP_CVTDQ2PS:
985                 return type_to_simd_type (MONO_TYPE_I4);
986         case OP_CVTPD2DQ:
987         case OP_CVTPD2PS:
988         case OP_CVTTPD2DQ:
989                 return type_to_simd_type (MONO_TYPE_R8);
990         case OP_CVTPS2DQ:
991         case OP_CVTPS2PD:
992         case OP_CVTTPS2DQ:
993                 return type_to_simd_type (MONO_TYPE_R4);
994         case OP_EXTRACT_MASK:
995                 return type_to_simd_type (MONO_TYPE_I1);
996         case OP_SQRTPS:
997         case OP_RSQRTPS:
998         case OP_RCPPS:
999         case OP_DUPPS_LOW:
1000         case OP_DUPPS_HIGH:
1001                 return type_to_simd_type (MONO_TYPE_R4);
1002         case OP_SQRTPD:
1003         case OP_DUPPD:
1004                 return type_to_simd_type (MONO_TYPE_R8);
1005         default:
1006                 g_assert_not_reached ();
1007                 return NULL;
1008         }
1009 #else
1010         return NULL;
1011 #endif
1012 }
1013
1014 /*
1015  * get_bb:
1016  *
1017  *   Return the LLVM basic block corresponding to BB.
1018  */
1019 static LLVMBasicBlockRef
1020 get_bb (EmitContext *ctx, MonoBasicBlock *bb)
1021 {
1022         char bb_name_buf [128];
1023         char *bb_name;
1024
1025         if (ctx->bblocks [bb->block_num].bblock == NULL) {
1026                 if (bb->flags & BB_EXCEPTION_HANDLER) {
1027                         int clause_index = (mono_get_block_region_notry (ctx->cfg, bb->region) >> 8) - 1;
1028                         sprintf (bb_name_buf, "EH_CLAUSE%d_BB%d", clause_index, bb->block_num);
1029                         bb_name = bb_name_buf;
1030                 } else if (bb->block_num < 256) {
1031                         if (!ctx->module->bb_names) {
1032                                 ctx->module->bb_names_len = 256;
1033                                 ctx->module->bb_names = g_new0 (char*, ctx->module->bb_names_len);
1034                         }
1035                         if (!ctx->module->bb_names [bb->block_num]) {
1036                                 char *n;
1037
1038                                 n = g_strdup_printf ("BB%d", bb->block_num);
1039                                 mono_memory_barrier ();
1040                                 ctx->module->bb_names [bb->block_num] = n;
1041                         }
1042                         bb_name = ctx->module->bb_names [bb->block_num];
1043                 } else {
1044                         sprintf (bb_name_buf, "BB%d", bb->block_num);
1045                         bb_name = bb_name_buf;
1046                 }
1047
1048                 ctx->bblocks [bb->block_num].bblock = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
1049                 ctx->bblocks [bb->block_num].end_bblock = ctx->bblocks [bb->block_num].bblock;
1050         }
1051
1052         return ctx->bblocks [bb->block_num].bblock;
1053 }
1054
1055 /* 
1056  * get_end_bb:
1057  *
1058  *   Return the last LLVM bblock corresponding to BB.
1059  * This might not be equal to the bb returned by get_bb () since we need to generate
1060  * multiple LLVM bblocks for a mono bblock to handle throwing exceptions.
1061  */
1062 static LLVMBasicBlockRef
1063 get_end_bb (EmitContext *ctx, MonoBasicBlock *bb)
1064 {
1065         get_bb (ctx, bb);
1066         return ctx->bblocks [bb->block_num].end_bblock;
1067 }
1068
1069 static LLVMBasicBlockRef
1070 gen_bb (EmitContext *ctx, const char *prefix)
1071 {
1072         char bb_name [128];
1073
1074         sprintf (bb_name, "%s%d", prefix, ++ ctx->ex_index);
1075         return LLVMAppendBasicBlock (ctx->lmethod, bb_name);
1076 }
1077
1078 /*
1079  * resolve_patch:
1080  *
1081  *   Return the target of the patch identified by TYPE and TARGET.
1082  */
1083 static gpointer
1084 resolve_patch (MonoCompile *cfg, MonoJumpInfoType type, gconstpointer target)
1085 {
1086         MonoJumpInfo ji;
1087         MonoError error;
1088         gpointer res;
1089
1090         memset (&ji, 0, sizeof (ji));
1091         ji.type = type;
1092         ji.data.target = target;
1093
1094         res = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &ji, FALSE, &error);
1095         mono_error_assert_ok (&error);
1096
1097         return res;
1098 }
1099
1100 /*
1101  * convert_full:
1102  *
1103  *   Emit code to convert the LLVM value V to DTYPE.
1104  */
1105 static LLVMValueRef
1106 convert_full (EmitContext *ctx, LLVMValueRef v, LLVMTypeRef dtype, gboolean is_unsigned)
1107 {
1108         LLVMTypeRef stype = LLVMTypeOf (v);
1109
1110         if (stype != dtype) {
1111                 gboolean ext = FALSE;
1112
1113                 /* Extend */
1114                 if (dtype == LLVMInt64Type () && (stype == LLVMInt32Type () || stype == LLVMInt16Type () || stype == LLVMInt8Type ()))
1115                         ext = TRUE;
1116                 else if (dtype == LLVMInt32Type () && (stype == LLVMInt16Type () || stype == LLVMInt8Type ()))
1117                         ext = TRUE;
1118                 else if (dtype == LLVMInt16Type () && (stype == LLVMInt8Type ()))
1119                         ext = TRUE;
1120
1121                 if (ext)
1122                         return is_unsigned ? LLVMBuildZExt (ctx->builder, v, dtype, "") : LLVMBuildSExt (ctx->builder, v, dtype, "");
1123
1124                 if (dtype == LLVMDoubleType () && stype == LLVMFloatType ())
1125                         return LLVMBuildFPExt (ctx->builder, v, dtype, "");
1126
1127                 /* Trunc */
1128                 if (stype == LLVMInt64Type () && (dtype == LLVMInt32Type () || dtype == LLVMInt16Type () || dtype == LLVMInt8Type ()))
1129                         return LLVMBuildTrunc (ctx->builder, v, dtype, "");
1130                 if (stype == LLVMInt32Type () && (dtype == LLVMInt16Type () || dtype == LLVMInt8Type ()))
1131                         return LLVMBuildTrunc (ctx->builder, v, dtype, "");
1132                 if (stype == LLVMInt16Type () && dtype == LLVMInt8Type ())
1133                         return LLVMBuildTrunc (ctx->builder, v, dtype, "");
1134                 if (stype == LLVMDoubleType () && dtype == LLVMFloatType ())
1135                         return LLVMBuildFPTrunc (ctx->builder, v, dtype, "");
1136
1137                 if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind && LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
1138                         return LLVMBuildBitCast (ctx->builder, v, dtype, "");
1139                 if (LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
1140                         return LLVMBuildIntToPtr (ctx->builder, v, dtype, "");
1141                 if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind)
1142                         return LLVMBuildPtrToInt (ctx->builder, v, dtype, "");
1143
1144                 if (mono_arch_is_soft_float ()) {
1145                         if (stype == LLVMInt32Type () && dtype == LLVMFloatType ())
1146                                 return LLVMBuildBitCast (ctx->builder, v, dtype, "");
1147                         if (stype == LLVMInt32Type () && dtype == LLVMDoubleType ())
1148                                 return LLVMBuildBitCast (ctx->builder, LLVMBuildZExt (ctx->builder, v, LLVMInt64Type (), ""), dtype, "");
1149                 }
1150
1151                 if (LLVMGetTypeKind (stype) == LLVMVectorTypeKind && LLVMGetTypeKind (dtype) == LLVMVectorTypeKind)
1152                         return LLVMBuildBitCast (ctx->builder, v, dtype, "");
1153
1154                 LLVMDumpValue (v);
1155                 LLVMDumpValue (LLVMConstNull (dtype));
1156                 g_assert_not_reached ();
1157                 return NULL;
1158         } else {
1159                 return v;
1160         }
1161 }
1162
1163 static LLVMValueRef
1164 convert (EmitContext *ctx, LLVMValueRef v, LLVMTypeRef dtype)
1165 {
1166         return convert_full (ctx, v, dtype, FALSE);
1167 }
1168
1169 /*
1170  * emit_volatile_load:
1171  *
1172  *   If vreg is volatile, emit a load from its address.
1173  */
1174 static LLVMValueRef
1175 emit_volatile_load (EmitContext *ctx, int vreg)
1176 {
1177         MonoType *t;
1178         LLVMValueRef v;
1179
1180 #ifdef TARGET_ARM64
1181         // FIXME: This hack is required because we pass the rgctx in a callee saved
1182         // register on arm64 (x15), and llvm might keep the value in that register
1183         // even through the register is marked as 'reserved' inside llvm.
1184         if (ctx->cfg->rgctx_var && ctx->cfg->rgctx_var->dreg == vreg)
1185                 v = mono_llvm_build_load (ctx->builder, ctx->addresses [vreg], "", TRUE);
1186         else
1187                 v = LLVMBuildLoad (ctx->builder, ctx->addresses [vreg], "");
1188 #else
1189         v = LLVMBuildLoad (ctx->builder, ctx->addresses [vreg], "");
1190 #endif
1191         t = ctx->vreg_cli_types [vreg];
1192         if (t && !t->byref) {
1193                 /* 
1194                  * Might have to zero extend since llvm doesn't have 
1195                  * unsigned types.
1196                  */
1197                 if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_U2 || t->type == MONO_TYPE_CHAR || t->type == MONO_TYPE_BOOLEAN)
1198                         v = LLVMBuildZExt (ctx->builder, v, LLVMInt32Type (), "");
1199                 else if (t->type == MONO_TYPE_I1 || t->type == MONO_TYPE_I2)
1200                         v = LLVMBuildSExt (ctx->builder, v, LLVMInt32Type (), "");
1201                 else if (t->type == MONO_TYPE_U8)
1202                         v = LLVMBuildZExt (ctx->builder, v, LLVMInt64Type (), "");
1203         }
1204
1205         return v;
1206 }
1207
1208 /*
1209  * emit_volatile_store:
1210  *
1211  *   If VREG is volatile, emit a store from its value to its address.
1212  */
1213 static void
1214 emit_volatile_store (EmitContext *ctx, int vreg)
1215 {
1216         MonoInst *var = get_vreg_to_inst (ctx->cfg, vreg);
1217
1218         if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
1219                 g_assert (ctx->addresses [vreg]);
1220                 LLVMBuildStore (ctx->builder, convert (ctx, ctx->values [vreg], type_to_llvm_type (ctx, var->inst_vtype)), ctx->addresses [vreg]);
1221         }
1222 }
1223
1224 static LLVMTypeRef
1225 sig_to_llvm_sig_no_cinfo (EmitContext *ctx, MonoMethodSignature *sig)
1226 {
1227         LLVMTypeRef ret_type;
1228         LLVMTypeRef *param_types = NULL;
1229         LLVMTypeRef res;
1230         int i, pindex;
1231         MonoType *rtype;
1232
1233         rtype = mini_get_underlying_type (sig->ret);
1234         ret_type = type_to_llvm_type (ctx, rtype);
1235         if (!ctx_ok (ctx))
1236                 return NULL;
1237
1238         param_types = g_new0 (LLVMTypeRef, (sig->param_count * 8) + 3);
1239         pindex = 0;
1240
1241         if (sig->hasthis)
1242                 param_types [pindex ++] = ThisType ();
1243         for (i = 0; i < sig->param_count; ++i)
1244                 param_types [pindex ++] = type_to_llvm_arg_type (ctx, sig->params [i]);
1245
1246         if (!ctx_ok (ctx)) {
1247                 g_free (param_types);
1248                 return NULL;
1249         }
1250
1251         res = LLVMFunctionType (ret_type, param_types, pindex, FALSE);
1252         g_free (param_types);
1253
1254         return res;
1255 }
1256
1257 /*
1258  * sig_to_llvm_sig_full:
1259  *
1260  *   Return the LLVM signature corresponding to the mono signature SIG using the
1261  * calling convention information in CINFO. Fill out the parameter mapping information in CINFO.
1262  */
1263 static LLVMTypeRef
1264 sig_to_llvm_sig_full (EmitContext *ctx, MonoMethodSignature *sig, LLVMCallInfo *cinfo)
1265 {
1266         LLVMTypeRef ret_type;
1267         LLVMTypeRef *param_types = NULL;
1268         LLVMTypeRef res;
1269         int i, j, pindex, vret_arg_pindex = 0;
1270         gboolean vretaddr = FALSE;
1271         MonoType *rtype;
1272
1273         if (!cinfo)
1274                 return sig_to_llvm_sig_no_cinfo (ctx, sig);
1275
1276         rtype = mini_get_underlying_type (sig->ret);
1277         ret_type = type_to_llvm_type (ctx, rtype);
1278         if (!ctx_ok (ctx))
1279                 return NULL;
1280
1281         switch (cinfo->ret.storage) {
1282         case LLVMArgVtypeInReg:
1283                 /* LLVM models this by returning an aggregate value */
1284                 if (cinfo->ret.pair_storage [0] == LLVMArgInIReg && cinfo->ret.pair_storage [1] == LLVMArgNone) {
1285                         LLVMTypeRef members [2];
1286
1287                         members [0] = IntPtrType ();
1288                         ret_type = LLVMStructType (members, 1, FALSE);
1289                 } else if (cinfo->ret.pair_storage [0] == LLVMArgNone && cinfo->ret.pair_storage [1] == LLVMArgNone) {
1290                         /* Empty struct */
1291                         ret_type = LLVMVoidType ();
1292                 } else if (cinfo->ret.pair_storage [0] == LLVMArgInIReg && cinfo->ret.pair_storage [1] == LLVMArgInIReg) {
1293                         LLVMTypeRef members [2];
1294
1295                         members [0] = IntPtrType ();
1296                         members [1] = IntPtrType ();
1297                         ret_type = LLVMStructType (members, 2, FALSE);
1298                 } else {
1299                         g_assert_not_reached ();
1300                 }
1301                 break;
1302         case LLVMArgVtypeByVal:
1303                 /* Vtype returned normally by val */
1304                 break;
1305         case LLVMArgVtypeAsScalar: {
1306                 int size = mono_class_value_size (mono_class_from_mono_type (rtype), NULL);
1307                 /* LLVM models this by returning an int */
1308                 if (size < SIZEOF_VOID_P) {
1309                         g_assert (cinfo->ret.nslots == 1);
1310                         ret_type = LLVMIntType (size * 8);
1311                 } else {
1312                         g_assert (cinfo->ret.nslots == 1 || cinfo->ret.nslots == 2);
1313                         ret_type = LLVMIntType (cinfo->ret.nslots * sizeof (mgreg_t) * 8);
1314                 }
1315                 break;
1316         }
1317         case LLVMArgAsIArgs:
1318                 ret_type = LLVMArrayType (IntPtrType (), cinfo->ret.nslots);
1319                 break;
1320         case LLVMArgFpStruct: {
1321                 /* Vtype returned as a fp struct */
1322                 LLVMTypeRef members [16];
1323
1324                 /* Have to create our own structure since we don't map fp structures to LLVM fp structures yet */
1325                 for (i = 0; i < cinfo->ret.nslots; ++i)
1326                         members [i] = cinfo->ret.esize == 8 ? LLVMDoubleType () : LLVMFloatType ();
1327                 ret_type = LLVMStructType (members, cinfo->ret.nslots, FALSE);
1328                 break;
1329         }
1330         case LLVMArgVtypeByRef:
1331                 /* Vtype returned using a hidden argument */
1332                 ret_type = LLVMVoidType ();
1333                 break;
1334         case LLVMArgVtypeRetAddr:
1335         case LLVMArgGsharedvtFixed:
1336         case LLVMArgGsharedvtFixedVtype:
1337         case LLVMArgGsharedvtVariable:
1338                 vretaddr = TRUE;
1339                 ret_type = LLVMVoidType ();
1340                 break;
1341         default:
1342                 break;
1343         }
1344
1345         param_types = g_new0 (LLVMTypeRef, (sig->param_count * 8) + 3);
1346         pindex = 0;
1347         if (cinfo->ret.storage == LLVMArgVtypeByRef) {
1348                 /*
1349                  * Has to be the first argument because of the sret argument attribute
1350                  * FIXME: This might conflict with passing 'this' as the first argument, but
1351                  * this is only used on arm64 which has a dedicated struct return register.
1352                  */
1353                 cinfo->vret_arg_pindex = pindex;
1354                 param_types [pindex] = type_to_llvm_arg_type (ctx, sig->ret);
1355                 if (!ctx_ok (ctx)) {
1356                         g_free (param_types);
1357                         return NULL;
1358                 }
1359                 param_types [pindex] = LLVMPointerType (param_types [pindex], 0);
1360                 pindex ++;
1361         }
1362         if (!ctx->llvm_only && cinfo->rgctx_arg) {
1363                 cinfo->rgctx_arg_pindex = pindex;
1364                 param_types [pindex] = ctx->module->ptr_type;
1365                 pindex ++;
1366         }
1367         if (cinfo->imt_arg) {
1368                 cinfo->imt_arg_pindex = pindex;
1369                 param_types [pindex] = ctx->module->ptr_type;
1370                 pindex ++;
1371         }
1372         if (vretaddr) {
1373                 /* Compute the index in the LLVM signature where the vret arg needs to be passed */
1374                 vret_arg_pindex = pindex;
1375                 if (cinfo->vret_arg_index == 1) {
1376                         /* Add the slots consumed by the first argument */
1377                         LLVMArgInfo *ainfo = &cinfo->args [0];
1378                         switch (ainfo->storage) {
1379                         case LLVMArgVtypeInReg:
1380                                 for (j = 0; j < 2; ++j) {
1381                                         if (ainfo->pair_storage [j] == LLVMArgInIReg)
1382                                                 vret_arg_pindex ++;
1383                                 }
1384                                 break;
1385                         default:
1386                                 vret_arg_pindex ++;
1387                         }
1388                 }
1389
1390                 cinfo->vret_arg_pindex = vret_arg_pindex;
1391         }                               
1392
1393         if (vretaddr && vret_arg_pindex == pindex)
1394                 param_types [pindex ++] = IntPtrType ();
1395         if (sig->hasthis) {
1396                 cinfo->this_arg_pindex = pindex;
1397                 param_types [pindex ++] = ThisType ();
1398                 cinfo->args [0].pindex = cinfo->this_arg_pindex;
1399         }
1400         if (vretaddr && vret_arg_pindex == pindex)
1401                 param_types [pindex ++] = IntPtrType ();
1402         for (i = 0; i < sig->param_count; ++i) {
1403                 LLVMArgInfo *ainfo = &cinfo->args [i + sig->hasthis];
1404
1405                 if (vretaddr && vret_arg_pindex == pindex)
1406                         param_types [pindex ++] = IntPtrType ();
1407                 ainfo->pindex = pindex;
1408
1409                 switch (ainfo->storage) {
1410                 case LLVMArgVtypeInReg:
1411                         for (j = 0; j < 2; ++j) {
1412                                 switch (ainfo->pair_storage [j]) {
1413                                 case LLVMArgInIReg:
1414                                         param_types [pindex ++] = LLVMIntType (sizeof (gpointer) * 8);
1415                                         break;
1416                                 case LLVMArgNone:
1417                                         break;
1418                                 default:
1419                                         g_assert_not_reached ();
1420                                 }
1421                         }
1422                         break;
1423                 case LLVMArgVtypeByVal:
1424                         param_types [pindex] = type_to_llvm_arg_type (ctx, ainfo->type);
1425                         if (!ctx_ok (ctx))
1426                                 break;
1427                         param_types [pindex] = LLVMPointerType (param_types [pindex], 0);
1428                         pindex ++;
1429                         break;
1430                 case LLVMArgAsIArgs:
1431                         param_types [pindex] = LLVMArrayType (IntPtrType (), ainfo->nslots);
1432                         pindex ++;
1433                         break;
1434                 case LLVMArgVtypeByRef:
1435                         param_types [pindex] = type_to_llvm_arg_type (ctx, ainfo->type);
1436                         if (!ctx_ok (ctx))
1437                                 break;
1438                         param_types [pindex] = LLVMPointerType (param_types [pindex], 0);
1439                         pindex ++;
1440                         break;
1441                 case LLVMArgAsFpArgs: {
1442                         int j;
1443
1444                         /* Emit dummy fp arguments if needed so the rest is passed on the stack */
1445                         for (j = 0; j < ainfo->ndummy_fpargs; ++j)
1446                                 param_types [pindex ++] = LLVMDoubleType ();
1447                         for (j = 0; j < ainfo->nslots; ++j)
1448                                 param_types [pindex ++] = ainfo->esize == 8 ? LLVMDoubleType () : LLVMFloatType ();
1449                         break;
1450                 }
1451                 case LLVMArgVtypeAsScalar:
1452                         g_assert_not_reached ();
1453                         break;
1454                 case LLVMArgGsharedvtFixed:
1455                 case LLVMArgGsharedvtFixedVtype:
1456                         param_types [pindex ++] = LLVMPointerType (type_to_llvm_arg_type (ctx, ainfo->type), 0);
1457                         break;
1458                 case LLVMArgGsharedvtVariable:
1459                         param_types [pindex ++] = LLVMPointerType (IntPtrType (), 0);
1460                         break;
1461                 default:
1462                         param_types [pindex ++] = type_to_llvm_arg_type (ctx, ainfo->type);
1463                         break;
1464                 }
1465         }
1466         if (!ctx_ok (ctx)) {
1467                 g_free (param_types);
1468                 return NULL;
1469         }
1470         if (vretaddr && vret_arg_pindex == pindex)
1471                 param_types [pindex ++] = IntPtrType ();
1472         if (ctx->llvm_only && cinfo->rgctx_arg) {
1473                 /* Pass the rgctx as the last argument */
1474                 cinfo->rgctx_arg_pindex = pindex;
1475                 param_types [pindex] = ctx->module->ptr_type;
1476                 pindex ++;
1477         }
1478
1479         res = LLVMFunctionType (ret_type, param_types, pindex, FALSE);
1480         g_free (param_types);
1481
1482         return res;
1483 }
1484
1485 static LLVMTypeRef
1486 sig_to_llvm_sig (EmitContext *ctx, MonoMethodSignature *sig)
1487 {
1488         return sig_to_llvm_sig_full (ctx, sig, NULL);
1489 }
1490
1491 /*
1492  * LLVMFunctionType1:
1493  *
1494  *   Create an LLVM function type from the arguments.
1495  */
1496 static G_GNUC_UNUSED LLVMTypeRef
1497 LLVMFunctionType0 (LLVMTypeRef ReturnType,
1498                                    int IsVarArg)
1499 {
1500         return LLVMFunctionType (ReturnType, NULL, 0, IsVarArg);
1501 }
1502
1503 /*
1504  * LLVMFunctionType1:
1505  *
1506  *   Create an LLVM function type from the arguments.
1507  */
1508 static G_GNUC_UNUSED LLVMTypeRef 
1509 LLVMFunctionType1 (LLVMTypeRef ReturnType,
1510                                    LLVMTypeRef ParamType1,
1511                                    int IsVarArg)
1512 {
1513         LLVMTypeRef param_types [1];
1514
1515         param_types [0] = ParamType1;
1516
1517         return LLVMFunctionType (ReturnType, param_types, 1, IsVarArg);
1518 }
1519
1520 /*
1521  * LLVMFunctionType2:
1522  *
1523  *   Create an LLVM function type from the arguments.
1524  */
1525 static G_GNUC_UNUSED LLVMTypeRef
1526 LLVMFunctionType2 (LLVMTypeRef ReturnType,
1527                                    LLVMTypeRef ParamType1,
1528                                    LLVMTypeRef ParamType2,
1529                                    int IsVarArg)
1530 {
1531         LLVMTypeRef param_types [2];
1532
1533         param_types [0] = ParamType1;
1534         param_types [1] = ParamType2;
1535
1536         return LLVMFunctionType (ReturnType, param_types, 2, IsVarArg);
1537 }
1538
1539 /*
1540  * LLVMFunctionType3:
1541  *
1542  *   Create an LLVM function type from the arguments.
1543  */
1544 static G_GNUC_UNUSED LLVMTypeRef
1545 LLVMFunctionType3 (LLVMTypeRef ReturnType,
1546                                    LLVMTypeRef ParamType1,
1547                                    LLVMTypeRef ParamType2,
1548                                    LLVMTypeRef ParamType3,
1549                                    int IsVarArg)
1550 {
1551         LLVMTypeRef param_types [3];
1552
1553         param_types [0] = ParamType1;
1554         param_types [1] = ParamType2;
1555         param_types [2] = ParamType3;
1556
1557         return LLVMFunctionType (ReturnType, param_types, 3, IsVarArg);
1558 }
1559
1560 static G_GNUC_UNUSED LLVMTypeRef
1561 LLVMFunctionType5 (LLVMTypeRef ReturnType,
1562                                    LLVMTypeRef ParamType1,
1563                                    LLVMTypeRef ParamType2,
1564                                    LLVMTypeRef ParamType3,
1565                                    LLVMTypeRef ParamType4,
1566                                    LLVMTypeRef ParamType5,
1567                                    int IsVarArg)
1568 {
1569         LLVMTypeRef param_types [5];
1570
1571         param_types [0] = ParamType1;
1572         param_types [1] = ParamType2;
1573         param_types [2] = ParamType3;
1574         param_types [3] = ParamType4;
1575         param_types [4] = ParamType5;
1576
1577         return LLVMFunctionType (ReturnType, param_types, 5, IsVarArg);
1578 }
1579
1580 /*
1581  * create_builder:
1582  *
1583  *   Create an LLVM builder and remember it so it can be freed later.
1584  */
1585 static LLVMBuilderRef
1586 create_builder (EmitContext *ctx)
1587 {
1588         LLVMBuilderRef builder = LLVMCreateBuilder ();
1589
1590         ctx->builders = g_slist_prepend_mempool (ctx->cfg->mempool, ctx->builders, builder);
1591
1592         return builder;
1593 }
1594
1595 static char*
1596 get_aotconst_name (MonoJumpInfoType type, gconstpointer data, int got_offset)
1597 {
1598         char *name;
1599
1600         switch (type) {
1601         case MONO_PATCH_INFO_INTERNAL_METHOD:
1602                 name = g_strdup_printf ("jit_icall_%s", data);
1603                 break;
1604         case MONO_PATCH_INFO_RGCTX_SLOT_INDEX: {
1605                 MonoJumpInfoRgctxEntry *entry = (MonoJumpInfoRgctxEntry*)data;
1606                 name = g_strdup_printf ("RGCTX_SLOT_INDEX_%s", mono_rgctx_info_type_to_str (entry->info_type));
1607                 break;
1608         }
1609         default:
1610                 name = g_strdup_printf ("%s_%d", mono_ji_type_to_string (type), got_offset);
1611                 break;
1612         }
1613
1614         return name;
1615 }
1616
1617 static LLVMValueRef
1618 get_aotconst_typed (EmitContext *ctx, MonoJumpInfoType type, gconstpointer data, LLVMTypeRef llvm_type)
1619 {
1620         MonoCompile *cfg;
1621         guint32 got_offset;
1622         LLVMValueRef indexes [2];
1623         LLVMValueRef got_entry_addr, load;
1624         LLVMBuilderRef builder = ctx->builder;
1625         char *name = NULL;
1626
1627         cfg = ctx->cfg;
1628
1629         MonoJumpInfo tmp_ji;
1630         tmp_ji.type = type;
1631         tmp_ji.data.target = data;
1632
1633         MonoJumpInfo *ji = mono_aot_patch_info_dup (&tmp_ji);
1634
1635         ji->next = cfg->patch_info;
1636         cfg->patch_info = ji;
1637
1638         got_offset = mono_aot_get_got_offset (cfg->patch_info);
1639         ctx->module->max_got_offset = MAX (ctx->module->max_got_offset, got_offset);
1640         /* 
1641          * If the got slot is shared, it means its initialized when the aot image is loaded, so we don't need to
1642          * explicitly initialize it.
1643          */
1644         if (!mono_aot_is_shared_got_offset (got_offset)) {
1645                 //mono_print_ji (ji);
1646                 //printf ("\n");
1647                 ctx->has_got_access = TRUE;
1648         }
1649
1650         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1651         indexes [1] = LLVMConstInt (LLVMInt32Type (), (gssize)got_offset, FALSE);
1652         got_entry_addr = LLVMBuildGEP (builder, ctx->module->got_var, indexes, 2, "");
1653
1654         name = get_aotconst_name (type, data, got_offset);
1655         if (llvm_type) {
1656                 load = LLVMBuildLoad (builder, got_entry_addr, "");
1657                 load = convert (ctx, load, llvm_type);
1658                 LLVMSetValueName (load, name ? name : "");
1659         } else {
1660                 load = LLVMBuildLoad (builder, got_entry_addr, name ? name : "");
1661         }
1662         g_free (name);
1663         //set_invariant_load_flag (load);
1664
1665         return load;
1666 }
1667
1668 static LLVMValueRef
1669 get_aotconst (EmitContext *ctx, MonoJumpInfoType type, gconstpointer data)
1670 {
1671         return get_aotconst_typed (ctx, type, data, NULL);
1672 }
1673
1674 static LLVMValueRef
1675 get_callee (EmitContext *ctx, LLVMTypeRef llvm_sig, MonoJumpInfoType type, gconstpointer data)
1676 {
1677         LLVMValueRef callee;
1678         char *callee_name;
1679         if (ctx->llvm_only) {
1680                 callee_name = mono_aot_get_direct_call_symbol (type, data);
1681                 if (callee_name) {
1682                         /* Directly callable */
1683                         // FIXME: Locking
1684                         callee = (LLVMValueRef)g_hash_table_lookup (ctx->module->direct_callables, callee_name);
1685                         if (!callee) {
1686                                 callee = LLVMAddFunction (ctx->lmodule, callee_name, llvm_sig);
1687
1688                                 LLVMSetVisibility (callee, LLVMHiddenVisibility);
1689
1690                                 g_hash_table_insert (ctx->module->direct_callables, (char*)callee_name, callee);
1691                         } else {
1692                                 /* LLVMTypeRef's are uniqued */
1693                                 if (LLVMGetElementType (LLVMTypeOf (callee)) != llvm_sig)
1694                                         return LLVMConstBitCast (callee, LLVMPointerType (llvm_sig, 0));
1695
1696                                 g_free (callee_name);
1697                         }
1698                         return callee;
1699                 }
1700
1701                 /*
1702                  * Calls are made through the GOT.
1703                  */
1704                 return get_aotconst_typed (ctx, type, data, LLVMPointerType (llvm_sig, 0));
1705         } else {
1706                 MonoJumpInfo *ji = NULL;
1707
1708                 callee_name = mono_aot_get_plt_symbol (type, data);
1709                 if (!callee_name)
1710                         return NULL;
1711
1712                 if (ctx->cfg->compile_aot)
1713                         /* Add a patch so referenced wrappers can be compiled in full aot mode */
1714                         mono_add_patch_info (ctx->cfg, 0, type, data);
1715
1716                 // FIXME: Locking
1717                 callee = (LLVMValueRef)g_hash_table_lookup (ctx->module->plt_entries, callee_name);
1718                 if (!callee) {
1719                         callee = LLVMAddFunction (ctx->lmodule, callee_name, llvm_sig);
1720
1721                         LLVMSetVisibility (callee, LLVMHiddenVisibility);
1722
1723                         g_hash_table_insert (ctx->module->plt_entries, (char*)callee_name, callee);
1724                 }
1725
1726                 if (ctx->cfg->compile_aot) {
1727                         ji = g_new0 (MonoJumpInfo, 1);
1728                         ji->type = type;
1729                         ji->data.target = data;
1730
1731                         g_hash_table_insert (ctx->module->plt_entries_ji, ji, callee);
1732                 }
1733
1734                 return callee;
1735         }
1736 }
1737
1738 static LLVMValueRef
1739 emit_jit_callee (EmitContext *ctx, const char *name, LLVMTypeRef llvm_sig, gpointer target)
1740 {
1741 #if LLVM_API_VERSION > 100
1742         LLVMValueRef tramp_var = LLVMAddGlobal (ctx->lmodule, LLVMPointerType (llvm_sig, 0), name);
1743         LLVMSetInitializer (tramp_var, LLVMConstIntToPtr (LLVMConstInt (LLVMInt64Type (), (guint64)(size_t)target, FALSE), LLVMPointerType (llvm_sig, 0)));
1744         LLVMSetLinkage (tramp_var, LLVMExternalLinkage);
1745         LLVMValueRef callee = LLVMBuildLoad (ctx->builder, tramp_var, "");
1746         return callee;
1747 #else
1748         LLVMValueRef callee = LLVMAddFunction (ctx->lmodule, "", llvm_sig);
1749         LLVMAddGlobalMapping (ctx->module->ee, callee, target);
1750         return callee;
1751 #endif
1752 }
1753
1754 static int
1755 get_handler_clause (MonoCompile *cfg, MonoBasicBlock *bb)
1756 {
1757         MonoMethodHeader *header = cfg->header;
1758         MonoExceptionClause *clause;
1759         int i;
1760
1761         /* Directly */
1762         if (bb->region != -1 && MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))
1763                 return (bb->region >> 8) - 1;
1764
1765         /* Indirectly */
1766         for (i = 0; i < header->num_clauses; ++i) {
1767                 clause = &header->clauses [i];
1768                            
1769                 if (MONO_OFFSET_IN_CLAUSE (clause, bb->real_offset) && clause->flags == MONO_EXCEPTION_CLAUSE_NONE)
1770                         return i;
1771         }
1772
1773         return -1;
1774 }
1775
1776 static MonoExceptionClause *
1777 get_most_deep_clause (MonoCompile *cfg, EmitContext *ctx, MonoBasicBlock *bb)
1778 {
1779         if (bb == cfg->bb_init)
1780                 return NULL;
1781         // Since they're sorted by nesting we just need
1782         // the first one that the bb is a member of
1783         for (int i = 0; i < cfg->header->num_clauses; i++) {
1784                 MonoExceptionClause *curr = &cfg->header->clauses [i];
1785
1786                 if (MONO_OFFSET_IN_CLAUSE (curr, bb->real_offset))
1787                         return curr;
1788         }
1789
1790         return NULL;
1791 }
1792         
1793 static void
1794 set_metadata_flag (LLVMValueRef v, const char *flag_name)
1795 {
1796         LLVMValueRef md_arg;
1797         int md_kind;
1798
1799         md_kind = LLVMGetMDKindID (flag_name, strlen (flag_name));
1800         md_arg = LLVMMDString ("mono", 4);
1801         LLVMSetMetadata (v, md_kind, LLVMMDNode (&md_arg, 1));
1802 }
1803
1804 static void
1805 set_invariant_load_flag (LLVMValueRef v)
1806 {
1807         LLVMValueRef md_arg;
1808         int md_kind;
1809         const char *flag_name;
1810
1811         // FIXME: Cache this
1812         flag_name = "invariant.load";
1813         md_kind = LLVMGetMDKindID (flag_name, strlen (flag_name));
1814         md_arg = LLVMMDString ("<index>", strlen ("<index>"));
1815         LLVMSetMetadata (v, md_kind, LLVMMDNode (&md_arg, 1));
1816 }
1817
1818 /*
1819  * emit_call:
1820  *
1821  *   Emit an LLVM call or invoke instruction depending on whenever the call is inside
1822  * a try region.
1823  */
1824 static LLVMValueRef
1825 emit_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, LLVMValueRef callee, LLVMValueRef *args, int pindex)
1826 {
1827         MonoCompile *cfg = ctx->cfg;
1828         LLVMValueRef lcall = NULL;
1829         LLVMBuilderRef builder = *builder_ref;
1830         MonoExceptionClause *clause;
1831
1832         if (ctx->llvm_only) {
1833                 clause = get_most_deep_clause (cfg, ctx, bb);
1834
1835                 if (clause) {
1836                         g_assert (clause->flags == MONO_EXCEPTION_CLAUSE_NONE || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY);
1837
1838                         /*
1839                          * Have to use an invoke instead of a call, branching to the
1840                          * handler bblock of the clause containing this bblock.
1841                          */
1842                         intptr_t key = CLAUSE_END(clause);
1843
1844                         LLVMBasicBlockRef lpad_bb = (LLVMBasicBlockRef)g_hash_table_lookup (ctx->exc_meta, (gconstpointer)key);
1845
1846                         // FIXME: Find the one that has the lowest end bound for the right start address
1847                         // FIXME: Finally + nesting
1848
1849                         if (lpad_bb) {
1850                                 LLVMBasicBlockRef noex_bb = gen_bb (ctx, "CALL_NOEX_BB");
1851
1852                                 /* Use an invoke */
1853                                 lcall = LLVMBuildInvoke (builder, callee, args, pindex, noex_bb, lpad_bb, "");
1854
1855                                 builder = ctx->builder = create_builder (ctx);
1856                                 LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
1857
1858                                 ctx->bblocks [bb->block_num].end_bblock = noex_bb;
1859                         }
1860                 }
1861         } else {
1862                 int clause_index = get_handler_clause (cfg, bb);
1863
1864                 if (clause_index != -1) {
1865                         MonoMethodHeader *header = cfg->header;
1866                         MonoExceptionClause *ec = &header->clauses [clause_index];
1867                         MonoBasicBlock *tblock;
1868                         LLVMBasicBlockRef ex_bb, noex_bb;
1869
1870                         /*
1871                          * Have to use an invoke instead of a call, branching to the
1872                          * handler bblock of the clause containing this bblock.
1873                          */
1874
1875                         g_assert (ec->flags == MONO_EXCEPTION_CLAUSE_NONE || ec->flags == MONO_EXCEPTION_CLAUSE_FINALLY);
1876
1877                         tblock = cfg->cil_offset_to_bb [ec->handler_offset];
1878                         g_assert (tblock);
1879
1880                         ctx->bblocks [tblock->block_num].invoke_target = TRUE;
1881
1882                         ex_bb = get_bb (ctx, tblock);
1883
1884                         noex_bb = gen_bb (ctx, "NOEX_BB");
1885
1886                         /* Use an invoke */
1887                         lcall = LLVMBuildInvoke (builder, callee, args, pindex, noex_bb, ex_bb, "");
1888
1889                         builder = ctx->builder = create_builder (ctx);
1890                         LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
1891
1892                         ctx->bblocks [bb->block_num].end_bblock = noex_bb;
1893                 }
1894         }
1895         
1896         if (!lcall) {
1897                 lcall = LLVMBuildCall (builder, callee, args, pindex, "");
1898                 ctx->builder = builder;
1899         }
1900
1901         if (builder_ref)
1902                 *builder_ref = ctx->builder;
1903
1904         return lcall;
1905 }
1906
1907 static LLVMValueRef
1908 emit_load_general (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef addr, LLVMValueRef base, const char *name, gboolean is_faulting, BarrierKind barrier)
1909 {
1910         const char *intrins_name;
1911         LLVMValueRef args [16], res;
1912         LLVMTypeRef addr_type;
1913         gboolean use_intrinsics = TRUE;
1914
1915 #if LLVM_API_VERSION > 100
1916         if (is_faulting && bb->region != -1 && !ctx->cfg->llvm_only) {
1917                 /* The llvm.mono.load/store intrinsics are not supported by this llvm version, emit an explicit null check instead */
1918                 LLVMValueRef cmp;
1919
1920                 cmp = LLVMBuildICmp (*builder_ref, LLVMIntEQ, base, LLVMConstNull (LLVMTypeOf (base)), "");
1921                 emit_cond_system_exception (ctx, bb, "NullReferenceException", cmp);
1922                 *builder_ref = ctx->builder;
1923                 use_intrinsics = FALSE;
1924         }
1925 #endif
1926
1927         if (is_faulting && bb->region != -1 && !ctx->cfg->llvm_only && use_intrinsics) {
1928                 LLVMAtomicOrdering ordering;
1929
1930                 switch (barrier) {
1931                 case LLVM_BARRIER_NONE:
1932                         ordering = LLVMAtomicOrderingNotAtomic;
1933                         break;
1934                 case LLVM_BARRIER_ACQ:
1935                         ordering = LLVMAtomicOrderingAcquire;
1936                         break;
1937                 case LLVM_BARRIER_SEQ:
1938                         ordering = LLVMAtomicOrderingSequentiallyConsistent;
1939                         break;
1940                 default:
1941                         g_assert_not_reached ();
1942                         break;
1943                 }
1944
1945                 /*
1946                  * We handle loads which can fault by calling a mono specific intrinsic
1947                  * using an invoke, so they are handled properly inside try blocks.
1948                  * We can't use this outside clauses, since LLVM optimizes intrinsics which
1949                  * are marked with IntrReadArgMem.
1950                  */
1951                 switch (size) {
1952                 case 1:
1953                         intrins_name = "llvm.mono.load.i8.p0i8";
1954                         break;
1955                 case 2:
1956                         intrins_name = "llvm.mono.load.i16.p0i16";
1957                         break;
1958                 case 4:
1959                         intrins_name = "llvm.mono.load.i32.p0i32";
1960                         break;
1961                 case 8:
1962                         intrins_name = "llvm.mono.load.i64.p0i64";
1963                         break;
1964                 default:
1965                         g_assert_not_reached ();
1966                 }
1967
1968                 addr_type = LLVMTypeOf (addr);
1969                 if (addr_type == LLVMPointerType (LLVMDoubleType (), 0) || addr_type == LLVMPointerType (LLVMFloatType (), 0))
1970                         addr = LLVMBuildBitCast (*builder_ref, addr, LLVMPointerType (LLVMIntType (size * 8), 0), "");
1971
1972                 args [0] = addr;
1973                 args [1] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1974                 args [2] = LLVMConstInt (LLVMInt1Type (), TRUE, FALSE);
1975                 args [3] = LLVMConstInt (LLVMInt32Type (), ordering, FALSE);
1976                 res = emit_call (ctx, bb, builder_ref, get_intrinsic (ctx, intrins_name), args, 4);
1977
1978                 if (addr_type == LLVMPointerType (LLVMDoubleType (), 0))
1979                         res = LLVMBuildBitCast (*builder_ref, res, LLVMDoubleType (), "");
1980                 else if (addr_type == LLVMPointerType (LLVMFloatType (), 0))
1981                         res = LLVMBuildBitCast (*builder_ref, res, LLVMFloatType (), "");
1982                 
1983                 return res;
1984         } else {
1985                 LLVMValueRef res;
1986
1987                 /* 
1988                  * We emit volatile loads for loads which can fault, because otherwise
1989                  * LLVM will generate invalid code when encountering a load from a
1990                  * NULL address.
1991                  */
1992                 if (barrier != LLVM_BARRIER_NONE)
1993                         res = mono_llvm_build_atomic_load (*builder_ref, addr, name, is_faulting, size, barrier);
1994                 else
1995                         res = mono_llvm_build_load (*builder_ref, addr, name, is_faulting);
1996
1997                 /* Mark it with a custom metadata */
1998                 /*
1999                   if (is_faulting)
2000                   set_metadata_flag (res, "mono.faulting.load");
2001                 */
2002
2003                 return res;
2004         }
2005 }
2006
2007 static LLVMValueRef
2008 emit_load (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef addr, const char *name, gboolean is_faulting)
2009 {
2010         return emit_load_general (ctx, bb, builder_ref, size, addr, addr, name, is_faulting, LLVM_BARRIER_NONE);
2011 }
2012
2013 static void
2014 emit_store_general (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef value, LLVMValueRef addr, LLVMValueRef base, gboolean is_faulting, BarrierKind barrier)
2015 {
2016         const char *intrins_name;
2017         LLVMValueRef args [16];
2018         gboolean use_intrinsics = TRUE;
2019
2020 #if LLVM_API_VERSION > 100
2021         if (is_faulting && bb->region != -1 && !ctx->cfg->llvm_only) {
2022                 /* The llvm.mono.load/store intrinsics are not supported by this llvm version, emit an explicit null check instead */
2023                 LLVMValueRef cmp = LLVMBuildICmp (*builder_ref, LLVMIntEQ, base, LLVMConstNull (LLVMTypeOf (base)), "");
2024                 emit_cond_system_exception (ctx, bb, "NullReferenceException", cmp);
2025                 *builder_ref = ctx->builder;
2026                 use_intrinsics = FALSE;
2027         }
2028 #endif
2029
2030         if (is_faulting && bb->region != -1 && !ctx->cfg->llvm_only && use_intrinsics) {
2031                 LLVMAtomicOrdering ordering;
2032
2033                 switch (barrier) {
2034                 case LLVM_BARRIER_NONE:
2035                         ordering = LLVMAtomicOrderingNotAtomic;
2036                         break;
2037                 case LLVM_BARRIER_REL:
2038                         ordering = LLVMAtomicOrderingRelease;
2039                         break;
2040                 case LLVM_BARRIER_SEQ:
2041                         ordering = LLVMAtomicOrderingSequentiallyConsistent;
2042                         break;
2043                 default:
2044                         g_assert_not_reached ();
2045                         break;
2046                 }
2047
2048                 switch (size) {
2049                 case 1:
2050                         intrins_name = "llvm.mono.store.i8.p0i8";
2051                         break;
2052                 case 2:
2053                         intrins_name = "llvm.mono.store.i16.p0i16";
2054                         break;
2055                 case 4:
2056                         intrins_name = "llvm.mono.store.i32.p0i32";
2057                         break;
2058                 case 8:
2059                         intrins_name = "llvm.mono.store.i64.p0i64";
2060                         break;
2061                 default:
2062                         g_assert_not_reached ();
2063                 }
2064
2065                 if (LLVMTypeOf (value) == LLVMDoubleType () || LLVMTypeOf (value) == LLVMFloatType ()) {
2066                         value = LLVMBuildBitCast (*builder_ref, value, LLVMIntType (size * 8), "");
2067                         addr = LLVMBuildBitCast (*builder_ref, addr, LLVMPointerType (LLVMIntType (size * 8), 0), "");
2068                 }
2069
2070                 args [0] = value;
2071                 args [1] = addr;
2072                 args [2] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2073                 args [3] = LLVMConstInt (LLVMInt1Type (), TRUE, FALSE);
2074                 args [4] = LLVMConstInt (LLVMInt32Type (), ordering, FALSE);
2075                 emit_call (ctx, bb, builder_ref, get_intrinsic (ctx, intrins_name), args, 5);
2076         } else {
2077                 if (barrier != LLVM_BARRIER_NONE)
2078                         mono_llvm_build_aligned_store (*builder_ref, value, addr, barrier, size);
2079                 else
2080                         mono_llvm_build_store (*builder_ref, value, addr, is_faulting, barrier);
2081         }
2082 }
2083
2084 static void
2085 emit_store (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef value, LLVMValueRef addr, LLVMValueRef base, gboolean is_faulting)
2086 {
2087         emit_store_general (ctx, bb, builder_ref, size, value, addr, base, is_faulting, LLVM_BARRIER_NONE);
2088 }
2089
2090 /*
2091  * emit_cond_system_exception:
2092  *
2093  *   Emit code to throw the exception EXC_TYPE if the condition CMP is false.
2094  * Might set the ctx exception.
2095  */
2096 static void
2097 emit_cond_system_exception (EmitContext *ctx, MonoBasicBlock *bb, const char *exc_type, LLVMValueRef cmp)
2098 {
2099         LLVMBasicBlockRef ex_bb, ex2_bb = NULL, noex_bb;
2100         LLVMBuilderRef builder;
2101         MonoClass *exc_class;
2102         LLVMValueRef args [2];
2103         LLVMValueRef callee;
2104         gboolean no_pc = FALSE;
2105
2106         if (IS_TARGET_AMD64)
2107                 /* Some platforms don't require the pc argument */
2108                 no_pc = TRUE;
2109         
2110         ex_bb = gen_bb (ctx, "EX_BB");
2111         if (ctx->llvm_only)
2112                 ex2_bb = gen_bb (ctx, "EX2_BB");
2113         noex_bb = gen_bb (ctx, "NOEX_BB");
2114
2115         LLVMBuildCondBr (ctx->builder, cmp, ex_bb, noex_bb);
2116
2117         exc_class = mono_class_load_from_name (mono_get_corlib (), "System", exc_type);
2118
2119         /* Emit exception throwing code */
2120         ctx->builder = builder = create_builder (ctx);
2121         LLVMPositionBuilderAtEnd (builder, ex_bb);
2122
2123         if (ctx->cfg->llvm_only) {
2124                 static LLVMTypeRef sig;
2125
2126                 if (!sig)
2127                         sig = LLVMFunctionType1 (LLVMVoidType (), LLVMInt32Type (), FALSE);
2128                 callee = get_callee (ctx, sig, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_llvm_throw_corlib_exception");
2129
2130                 LLVMBuildBr (builder, ex2_bb);
2131
2132                 ctx->builder = builder = create_builder (ctx);
2133                 LLVMPositionBuilderAtEnd (ctx->builder, ex2_bb);
2134
2135                 args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token - MONO_TOKEN_TYPE_DEF, FALSE);
2136                 emit_call (ctx, bb, &builder, callee, args, 1);
2137                 LLVMBuildUnreachable (builder);
2138
2139                 ctx->builder = builder = create_builder (ctx);
2140                 LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
2141
2142                 ctx->bblocks [bb->block_num].end_bblock = noex_bb;
2143
2144                 ctx->ex_index ++;
2145                 return;
2146         }
2147
2148         callee = ctx->module->throw_corlib_exception;
2149         if (!callee) {
2150                 LLVMTypeRef sig;
2151                 const char *icall_name;
2152
2153                 if (no_pc)
2154                         sig = LLVMFunctionType1 (LLVMVoidType (), LLVMInt32Type (), FALSE);
2155                 else
2156                         sig = LLVMFunctionType2 (LLVMVoidType (), LLVMInt32Type (), LLVMPointerType (LLVMInt8Type (), 0), FALSE);
2157                 icall_name = "llvm_throw_corlib_exception_abs_trampoline";
2158
2159                 if (ctx->cfg->compile_aot) {
2160                         callee = get_callee (ctx, sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
2161                 } else {
2162                         /*
2163                          * Differences between the LLVM/non-LLVM throw corlib exception trampoline:
2164                          * - On x86, LLVM generated code doesn't push the arguments
2165                          * - The trampoline takes the throw address as an arguments, not a pc offset.
2166                          */
2167                         gpointer target = resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
2168                         callee = emit_jit_callee (ctx, "llvm_throw_corlib_exception_trampoline", sig, target);
2169
2170 #if LLVM_API_VERSION > 100
2171                         /*
2172                          * Make sure that ex_bb starts with the invoke, so the block address points to it, and not to the load 
2173                          * added by emit_jit_callee ().
2174                          */
2175                         ex2_bb = gen_bb (ctx, "EX2_BB");
2176                         LLVMBuildBr (builder, ex2_bb);
2177                         ex_bb = ex2_bb;
2178
2179                         ctx->builder = builder = create_builder (ctx);
2180                         LLVMPositionBuilderAtEnd (ctx->builder, ex2_bb);
2181 #else
2182                         mono_memory_barrier ();
2183                         ctx->module->throw_corlib_exception = callee;
2184 #endif
2185                 }
2186         }
2187
2188         args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token - MONO_TOKEN_TYPE_DEF, FALSE);
2189
2190         /*
2191          * The LLVM mono branch contains changes so a block address can be passed as an
2192          * argument to a call.
2193          */
2194         if (no_pc) {
2195                 emit_call (ctx, bb, &builder, callee, args, 1);
2196         } else {
2197                 args [1] = LLVMBlockAddress (ctx->lmethod, ex_bb);
2198                 emit_call (ctx, bb, &builder, callee, args, 2);
2199         }
2200
2201         LLVMBuildUnreachable (builder);
2202
2203         ctx->builder = builder = create_builder (ctx);
2204         LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
2205
2206         ctx->bblocks [bb->block_num].end_bblock = noex_bb;
2207
2208         ctx->ex_index ++;
2209         return;
2210 }
2211
2212 /*
2213  * emit_args_to_vtype:
2214  *
2215  *   Emit code to store the vtype in the arguments args to the address ADDRESS.
2216  */
2217 static void
2218 emit_args_to_vtype (EmitContext *ctx, LLVMBuilderRef builder, MonoType *t, LLVMValueRef address, LLVMArgInfo *ainfo, LLVMValueRef *args)
2219 {
2220         int j, size, nslots;
2221
2222         size = mono_class_value_size (mono_class_from_mono_type (t), NULL);
2223
2224         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
2225                 address = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (LLVMInt8Type (), 0), "");
2226         }
2227
2228         if (ainfo->storage == LLVMArgAsFpArgs)
2229                 nslots = ainfo->nslots;
2230         else
2231                 nslots = 2;
2232
2233         for (j = 0; j < nslots; ++j) {
2234                 LLVMValueRef index [2], addr, daddr;
2235                 int part_size = size > sizeof (gpointer) ? sizeof (gpointer) : size;
2236                 LLVMTypeRef part_type;
2237
2238                 while (part_size != 1 && part_size != 2 && part_size != 4 && part_size < 8)
2239                         part_size ++;
2240
2241                 if (ainfo->pair_storage [j] == LLVMArgNone)
2242                         continue;
2243
2244                 switch (ainfo->pair_storage [j]) {
2245                 case LLVMArgInIReg: {
2246                         part_type = LLVMIntType (part_size * 8);
2247                         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
2248                                 index [0] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
2249                                 addr = LLVMBuildGEP (builder, address, index, 1, "");
2250                         } else {
2251                                 daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (IntPtrType (), 0), "");
2252                                 index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
2253                                 addr = LLVMBuildGEP (builder, daddr, index, 1, "");
2254                         }
2255                         LLVMBuildStore (builder, convert (ctx, args [j], part_type), LLVMBuildBitCast (ctx->builder, addr, LLVMPointerType (part_type, 0), ""));
2256                         break;
2257                 }
2258                 case LLVMArgInFPReg: {
2259                         LLVMTypeRef arg_type;
2260
2261                         if (ainfo->esize == 8)
2262                                 arg_type = LLVMDoubleType ();
2263                         else
2264                                 arg_type = LLVMFloatType ();
2265
2266                         index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
2267                         daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (arg_type, 0), "");
2268                         addr = LLVMBuildGEP (builder, daddr, index, 1, "");
2269                         LLVMBuildStore (builder, args [j], addr);
2270                         break;
2271                 }
2272                 case LLVMArgNone:
2273                         break;
2274                 default:
2275                         g_assert_not_reached ();
2276                 }
2277
2278                 size -= sizeof (gpointer);
2279         }
2280 }
2281
2282 /*
2283  * emit_vtype_to_args:
2284  *
2285  *   Emit code to load a vtype at address ADDRESS into scalar arguments. Store the arguments
2286  * into ARGS, and the number of arguments into NARGS.
2287  */
2288 static void
2289 emit_vtype_to_args (EmitContext *ctx, LLVMBuilderRef builder, MonoType *t, LLVMValueRef address, LLVMArgInfo *ainfo, LLVMValueRef *args, guint32 *nargs)
2290 {
2291         int pindex = 0;
2292         int j, size, nslots;
2293         LLVMTypeRef arg_type;
2294
2295         size = get_vtype_size (t);
2296
2297         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t)))
2298                 address = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (LLVMInt8Type (), 0), "");
2299
2300         if (ainfo->storage == LLVMArgAsFpArgs)
2301                 nslots = ainfo->nslots;
2302         else
2303                 nslots = 2;
2304         for (j = 0; j < nslots; ++j) {
2305                 LLVMValueRef index [2], addr, daddr;
2306                 int partsize = size > sizeof (gpointer) ? sizeof (gpointer) : size;
2307
2308                 if (ainfo->pair_storage [j] == LLVMArgNone)
2309                         continue;
2310
2311                 switch (ainfo->pair_storage [j]) {
2312                 case LLVMArgInIReg:
2313                         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
2314                                 index [0] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
2315                                 addr = LLVMBuildGEP (builder, address, index, 1, "");
2316                         } else {
2317                                 daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (IntPtrType (), 0), "");
2318                                 index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
2319                                 addr = LLVMBuildGEP (builder, daddr, index, 1, "");
2320                         }
2321                         args [pindex ++] = convert (ctx, LLVMBuildLoad (builder, LLVMBuildBitCast (ctx->builder, addr, LLVMPointerType (LLVMIntType (partsize * 8), 0), ""), ""), IntPtrType ());
2322                         break;
2323                 case LLVMArgInFPReg:
2324                         if (ainfo->esize == 8)
2325                                 arg_type = LLVMDoubleType ();
2326                         else
2327                                 arg_type = LLVMFloatType ();
2328                         daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (arg_type, 0), "");
2329                         index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
2330                         addr = LLVMBuildGEP (builder, daddr, index, 1, "");
2331                         args [pindex ++] = LLVMBuildLoad (builder, addr, "");
2332                         break;
2333                 case LLVMArgNone:
2334                         break;
2335                 default:
2336                         g_assert_not_reached ();
2337                 }
2338                 size -= sizeof (gpointer);
2339         }
2340
2341         *nargs = pindex;
2342 }
2343
2344 static LLVMValueRef
2345 build_alloca_llvm_type_name (EmitContext *ctx, LLVMTypeRef t, int align, const char *name)
2346 {
2347         /*
2348          * Have to place all alloca's at the end of the entry bb, since otherwise they would
2349          * get executed every time control reaches them.
2350          */
2351         LLVMPositionBuilder (ctx->alloca_builder, get_bb (ctx, ctx->cfg->bb_entry), ctx->last_alloca);
2352
2353         ctx->last_alloca = mono_llvm_build_alloca (ctx->alloca_builder, t, NULL, align, name);
2354         return ctx->last_alloca;
2355 }
2356
2357 static LLVMValueRef
2358 build_alloca_llvm_type (EmitContext *ctx, LLVMTypeRef t, int align)
2359 {
2360         return build_alloca_llvm_type_name (ctx, t, align, "");
2361 }
2362
2363 static LLVMValueRef
2364 build_alloca (EmitContext *ctx, MonoType *t)
2365 {
2366         MonoClass *k = mono_class_from_mono_type (t);
2367         int align;
2368
2369         g_assert (!mini_is_gsharedvt_variable_type (t));
2370
2371         if (MONO_CLASS_IS_SIMD (ctx->cfg, k))
2372                 align = 16;
2373         else
2374                 align = mono_class_min_align (k);
2375
2376         /* Sometimes align is not a power of 2 */
2377         while (mono_is_power_of_two (align) == -1)
2378                 align ++;
2379
2380         return build_alloca_llvm_type (ctx, type_to_llvm_type (ctx, t), align);
2381 }
2382
2383 static LLVMValueRef
2384 emit_gsharedvt_ldaddr (EmitContext *ctx, int vreg)
2385 {
2386         /*
2387          * gsharedvt local.
2388          * Compute the address of the local as gsharedvt_locals_var + gsharedvt_info_var->locals_offsets [idx].
2389          */
2390         MonoCompile *cfg = ctx->cfg;
2391         LLVMBuilderRef builder = ctx->builder;
2392         LLVMValueRef offset, offset_var;
2393         LLVMValueRef info_var = ctx->values [cfg->gsharedvt_info_var->dreg];
2394         LLVMValueRef locals_var = ctx->values [cfg->gsharedvt_locals_var->dreg];
2395         LLVMValueRef ptr;
2396         char *name;
2397
2398         g_assert (info_var);
2399         g_assert (locals_var);
2400
2401         int idx = cfg->gsharedvt_vreg_to_idx [vreg] - 1;
2402
2403         offset = LLVMConstInt (LLVMInt32Type (), MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)), FALSE);
2404         ptr = LLVMBuildAdd (builder, convert (ctx, info_var, IntPtrType ()), convert (ctx, offset, IntPtrType ()), "");
2405
2406         name = g_strdup_printf ("gsharedvt_local_%d_offset", vreg);
2407         offset_var = LLVMBuildLoad (builder, convert (ctx, ptr, LLVMPointerType (LLVMInt32Type (), 0)), name);
2408
2409         return LLVMBuildAdd (builder, convert (ctx, locals_var, IntPtrType ()), convert (ctx, offset_var, IntPtrType ()), "");
2410 }
2411
2412 /*
2413  * Put the global into the 'llvm.used' array to prevent it from being optimized away.
2414  */
2415 static void
2416 mark_as_used (MonoLLVMModule *module, LLVMValueRef global)
2417 {
2418         if (!module->used)
2419                 module->used = g_ptr_array_sized_new (16);
2420         g_ptr_array_add (module->used, global);
2421 }
2422
2423 static void
2424 emit_llvm_used (MonoLLVMModule *module)
2425 {
2426         LLVMModuleRef lmodule = module->lmodule;
2427         LLVMTypeRef used_type;
2428         LLVMValueRef used, *used_elem;
2429         int i;
2430                 
2431         if (!module->used)
2432                 return;
2433
2434         used_type = LLVMArrayType (LLVMPointerType (LLVMInt8Type (), 0), module->used->len);
2435         used = LLVMAddGlobal (lmodule, used_type, "llvm.used");
2436         used_elem = g_new0 (LLVMValueRef, module->used->len);
2437         for (i = 0; i < module->used->len; ++i)
2438                 used_elem [i] = LLVMConstBitCast ((LLVMValueRef)g_ptr_array_index (module->used, i), LLVMPointerType (LLVMInt8Type (), 0));
2439         LLVMSetInitializer (used, LLVMConstArray (LLVMPointerType (LLVMInt8Type (), 0), used_elem, module->used->len));
2440         LLVMSetLinkage (used, LLVMAppendingLinkage);
2441         LLVMSetSection (used, "llvm.metadata");
2442 }
2443
2444 /*
2445  * emit_get_method:
2446  *
2447  *   Emit a function mapping method indexes to their code
2448  */
2449 static void
2450 emit_get_method (MonoLLVMModule *module)
2451 {
2452         LLVMModuleRef lmodule = module->lmodule;
2453         LLVMValueRef func, switch_ins, m;
2454         LLVMBasicBlockRef entry_bb, fail_bb, bb, code_start_bb, code_end_bb;
2455         LLVMBasicBlockRef *bbs;
2456         LLVMTypeRef rtype;
2457         LLVMBuilderRef builder = LLVMCreateBuilder ();
2458         char *name;
2459         int i;
2460
2461         /*
2462          * Emit a switch statement. Emitting a table of function addresses is smaller/faster,
2463          * but generating code seems safer.
2464          */
2465         rtype = LLVMPointerType (LLVMInt8Type (), 0);
2466         func = LLVMAddFunction (lmodule, module->get_method_symbol, LLVMFunctionType1 (rtype, LLVMInt32Type (), FALSE));
2467         LLVMSetLinkage (func, LLVMExternalLinkage);
2468         LLVMSetVisibility (func, LLVMHiddenVisibility);
2469         LLVMAddFunctionAttr (func, LLVMNoUnwindAttribute);
2470         module->get_method = func;
2471
2472         entry_bb = LLVMAppendBasicBlock (func, "ENTRY");
2473
2474         /*
2475          * Return llvm_code_start/llvm_code_end when called with -1/-2.
2476          * Hopefully, the toolchain doesn't reorder these functions. If it does,
2477          * then we will have to find another solution.
2478          */
2479
2480         name = g_strdup_printf ("BB_CODE_START");
2481         code_start_bb = LLVMAppendBasicBlock (func, name);
2482         g_free (name);
2483         LLVMPositionBuilderAtEnd (builder, code_start_bb);
2484         LLVMBuildRet (builder, LLVMBuildBitCast (builder, module->code_start, rtype, ""));
2485
2486         name = g_strdup_printf ("BB_CODE_END");
2487         code_end_bb = LLVMAppendBasicBlock (func, name);
2488         g_free (name);
2489         LLVMPositionBuilderAtEnd (builder, code_end_bb);
2490         LLVMBuildRet (builder, LLVMBuildBitCast (builder, module->code_end, rtype, ""));
2491
2492         bbs = g_new0 (LLVMBasicBlockRef, module->max_method_idx + 1);
2493         for (i = 0; i < module->max_method_idx + 1; ++i) {
2494                 name = g_strdup_printf ("BB_%d", i);
2495                 bb = LLVMAppendBasicBlock (func, name);
2496                 g_free (name);
2497                 bbs [i] = bb;
2498
2499                 LLVMPositionBuilderAtEnd (builder, bb);
2500
2501                 m = (LLVMValueRef)g_hash_table_lookup (module->idx_to_lmethod, GINT_TO_POINTER (i));
2502                 if (m)
2503                         LLVMBuildRet (builder, LLVMBuildBitCast (builder, m, rtype, ""));
2504                 else
2505                         LLVMBuildRet (builder, LLVMConstNull (rtype));
2506         }
2507
2508         fail_bb = LLVMAppendBasicBlock (func, "FAIL");
2509         LLVMPositionBuilderAtEnd (builder, fail_bb);
2510         LLVMBuildRet (builder, LLVMConstNull (rtype));
2511
2512         LLVMPositionBuilderAtEnd (builder, entry_bb);
2513
2514         switch_ins = LLVMBuildSwitch (builder, LLVMGetParam (func, 0), fail_bb, 0);
2515         LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), -1, FALSE), code_start_bb);
2516         LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), -2, FALSE), code_end_bb);
2517         for (i = 0; i < module->max_method_idx + 1; ++i) {
2518                 LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), i, FALSE), bbs [i]);
2519         }
2520
2521         mark_as_used (module, func);
2522
2523         LLVMDisposeBuilder (builder);
2524 }
2525
2526 /*
2527  * emit_get_unbox_tramp:
2528  *
2529  *   Emit a function mapping method indexes to their unbox trampoline
2530  */
2531 static void
2532 emit_get_unbox_tramp (MonoLLVMModule *module)
2533 {
2534         LLVMModuleRef lmodule = module->lmodule;
2535         LLVMValueRef func, switch_ins, m;
2536         LLVMBasicBlockRef entry_bb, fail_bb, bb;
2537         LLVMBasicBlockRef *bbs;
2538         LLVMTypeRef rtype;
2539         LLVMBuilderRef builder = LLVMCreateBuilder ();
2540         char *name;
2541         int i;
2542
2543         /* Similar to emit_get_method () */
2544
2545         rtype = LLVMPointerType (LLVMInt8Type (), 0);
2546         func = LLVMAddFunction (lmodule, module->get_unbox_tramp_symbol, LLVMFunctionType1 (rtype, LLVMInt32Type (), FALSE));
2547         LLVMSetLinkage (func, LLVMExternalLinkage);
2548         LLVMSetVisibility (func, LLVMHiddenVisibility);
2549         LLVMAddFunctionAttr (func, LLVMNoUnwindAttribute);
2550         module->get_unbox_tramp = func;
2551
2552         entry_bb = LLVMAppendBasicBlock (func, "ENTRY");
2553
2554         bbs = g_new0 (LLVMBasicBlockRef, module->max_method_idx + 1);
2555         for (i = 0; i < module->max_method_idx + 1; ++i) {
2556                 m = (LLVMValueRef)g_hash_table_lookup (module->idx_to_unbox_tramp, GINT_TO_POINTER (i));
2557                 if (!m)
2558                         continue;
2559
2560                 name = g_strdup_printf ("BB_%d", i);
2561                 bb = LLVMAppendBasicBlock (func, name);
2562                 g_free (name);
2563                 bbs [i] = bb;
2564
2565                 LLVMPositionBuilderAtEnd (builder, bb);
2566
2567                 LLVMBuildRet (builder, LLVMBuildBitCast (builder, m, rtype, ""));
2568         }
2569
2570         fail_bb = LLVMAppendBasicBlock (func, "FAIL");
2571         LLVMPositionBuilderAtEnd (builder, fail_bb);
2572         LLVMBuildRet (builder, LLVMConstNull (rtype));
2573
2574         LLVMPositionBuilderAtEnd (builder, entry_bb);
2575
2576         switch_ins = LLVMBuildSwitch (builder, LLVMGetParam (func, 0), fail_bb, 0);
2577         for (i = 0; i < module->max_method_idx + 1; ++i) {
2578                 m = (LLVMValueRef)g_hash_table_lookup (module->idx_to_unbox_tramp, GINT_TO_POINTER (i));
2579                 if (!m)
2580                         continue;
2581
2582                 LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), i, FALSE), bbs [i]);
2583         }
2584
2585         mark_as_used (module, func);
2586         LLVMDisposeBuilder (builder);
2587 }
2588
2589 /* Add a function to mark the beginning of LLVM code */
2590 static void
2591 emit_llvm_code_start (MonoLLVMModule *module)
2592 {
2593         LLVMModuleRef lmodule = module->lmodule;
2594         LLVMValueRef func;
2595         LLVMBasicBlockRef entry_bb;
2596         LLVMBuilderRef builder;
2597
2598         func = LLVMAddFunction (lmodule, "llvm_code_start", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
2599         LLVMSetLinkage (func, LLVMInternalLinkage);
2600         LLVMAddFunctionAttr (func, LLVMNoUnwindAttribute);
2601         module->code_start = func;
2602         entry_bb = LLVMAppendBasicBlock (func, "ENTRY");
2603         builder = LLVMCreateBuilder ();
2604         LLVMPositionBuilderAtEnd (builder, entry_bb);
2605         LLVMBuildRetVoid (builder);
2606         LLVMDisposeBuilder (builder);
2607 }
2608
2609 static LLVMValueRef
2610 emit_init_icall_wrapper (MonoLLVMModule *module, const char *name, const char *icall_name, int subtype)
2611 {
2612         LLVMModuleRef lmodule = module->lmodule;
2613         LLVMValueRef func, indexes [2], got_entry_addr, args [16], callee;
2614         LLVMBasicBlockRef entry_bb;
2615         LLVMBuilderRef builder;
2616         LLVMTypeRef sig;
2617         MonoJumpInfo *ji;
2618         int got_offset;
2619
2620         switch (subtype) {
2621         case 0:
2622                 func = LLVMAddFunction (lmodule, name, LLVMFunctionType1 (LLVMVoidType (), LLVMInt32Type (), FALSE));
2623                 sig = LLVMFunctionType2 (LLVMVoidType (), IntPtrType (), LLVMInt32Type (), FALSE);
2624                 break;
2625         case 1:
2626         case 3:
2627                 /* mrgctx/vtable */
2628                 func = LLVMAddFunction (lmodule, name, LLVMFunctionType2 (LLVMVoidType (), LLVMInt32Type (), IntPtrType (), FALSE));
2629                 sig = LLVMFunctionType3 (LLVMVoidType (), IntPtrType (), LLVMInt32Type (), IntPtrType (), FALSE);
2630                 break;
2631         case 2:
2632                 func = LLVMAddFunction (lmodule, name, LLVMFunctionType2 (LLVMVoidType (), LLVMInt32Type (), ObjRefType (), FALSE));
2633                 sig = LLVMFunctionType3 (LLVMVoidType (), IntPtrType (), LLVMInt32Type (), ObjRefType (), FALSE);
2634                 break;
2635         default:
2636                 g_assert_not_reached ();
2637         }
2638         LLVMSetLinkage (func, LLVMInternalLinkage);
2639         LLVMAddFunctionAttr (func, LLVMNoInlineAttribute);
2640         mono_llvm_set_preserveall_cc (func);
2641         entry_bb = LLVMAppendBasicBlock (func, "ENTRY");
2642         builder = LLVMCreateBuilder ();
2643         LLVMPositionBuilderAtEnd (builder, entry_bb);
2644
2645         /* get_aotconst */
2646         ji = g_new0 (MonoJumpInfo, 1);
2647         ji->type = MONO_PATCH_INFO_AOT_MODULE;
2648         ji = mono_aot_patch_info_dup (ji);
2649         got_offset = mono_aot_get_got_offset (ji);
2650         module->max_got_offset = MAX (module->max_got_offset, got_offset);
2651         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2652         indexes [1] = LLVMConstInt (LLVMInt32Type (), got_offset, FALSE);
2653         got_entry_addr = LLVMBuildGEP (builder, module->got_var, indexes, 2, "");
2654         args [0] = LLVMBuildPtrToInt (builder, LLVMBuildLoad (builder, got_entry_addr, ""), IntPtrType (), "");
2655         args [1] = LLVMGetParam (func, 0);
2656         if (subtype)
2657                 args [2] = LLVMGetParam (func, 1);
2658
2659         ji = g_new0 (MonoJumpInfo, 1);
2660         ji->type = MONO_PATCH_INFO_INTERNAL_METHOD;
2661         ji->data.name = icall_name;
2662         ji = mono_aot_patch_info_dup (ji);
2663         got_offset = mono_aot_get_got_offset (ji);
2664         module->max_got_offset = MAX (module->max_got_offset, got_offset);
2665         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2666         indexes [1] = LLVMConstInt (LLVMInt32Type (), got_offset, FALSE);
2667         got_entry_addr = LLVMBuildGEP (builder, module->got_var, indexes, 2, "");
2668         callee = LLVMBuildLoad (builder, got_entry_addr, "");
2669         callee = LLVMBuildBitCast (builder, callee, LLVMPointerType (sig, 0), "");
2670         LLVMBuildCall (builder, callee, args, LLVMCountParamTypes (sig), "");
2671
2672         // Set the inited flag
2673         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2674         indexes [1] = LLVMGetParam (func, 0);
2675         LLVMBuildStore (builder, LLVMConstInt (LLVMInt8Type (), 1, FALSE), LLVMBuildGEP (builder, module->inited_var, indexes, 2, ""));
2676
2677         LLVMBuildRetVoid (builder);
2678
2679         LLVMVerifyFunction(func, LLVMAbortProcessAction);
2680         LLVMDisposeBuilder (builder);
2681         return func;
2682 }
2683
2684 /*
2685  * Emit wrappers around the C icalls used to initialize llvm methods, to
2686  * make the calling code smaller and to enable usage of the llvm
2687  * PreserveAll calling convention.
2688  */
2689 static void
2690 emit_init_icall_wrappers (MonoLLVMModule *module)
2691 {
2692         module->init_method = emit_init_icall_wrapper (module, "init_method", "mono_aot_init_llvm_method", 0);
2693         module->init_method_gshared_mrgctx = emit_init_icall_wrapper (module, "init_method_gshared_mrgctx", "mono_aot_init_gshared_method_mrgctx", 1);
2694         module->init_method_gshared_this = emit_init_icall_wrapper (module, "init_method_gshared_this", "mono_aot_init_gshared_method_this", 2);
2695         module->init_method_gshared_vtable = emit_init_icall_wrapper (module, "init_method_gshared_vtable", "mono_aot_init_gshared_method_vtable", 3);
2696 }
2697
2698 static void
2699 emit_llvm_code_end (MonoLLVMModule *module)
2700 {
2701         LLVMModuleRef lmodule = module->lmodule;
2702         LLVMValueRef func;
2703         LLVMBasicBlockRef entry_bb;
2704         LLVMBuilderRef builder;
2705
2706         func = LLVMAddFunction (lmodule, "llvm_code_end", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
2707         LLVMSetLinkage (func, LLVMInternalLinkage);
2708         LLVMAddFunctionAttr (func, LLVMNoUnwindAttribute);
2709         module->code_end = func;
2710         entry_bb = LLVMAppendBasicBlock (func, "ENTRY");
2711         builder = LLVMCreateBuilder ();
2712         LLVMPositionBuilderAtEnd (builder, entry_bb);
2713         LLVMBuildRetVoid (builder);
2714         LLVMDisposeBuilder (builder);
2715 }
2716
2717 static void
2718 emit_div_check (EmitContext *ctx, LLVMBuilderRef builder, MonoBasicBlock *bb, MonoInst *ins, LLVMValueRef lhs, LLVMValueRef rhs)
2719 {
2720         gboolean need_div_check = ctx->cfg->backend->need_div_check;
2721
2722         if (bb->region)
2723                 /* LLVM doesn't know that these can throw an exception since they are not called through an intrinsic */
2724                 need_div_check = TRUE;
2725
2726         if (!need_div_check)
2727                 return;
2728
2729         switch (ins->opcode) {
2730         case OP_IDIV:
2731         case OP_LDIV:
2732         case OP_IREM:
2733         case OP_LREM:
2734         case OP_IDIV_UN:
2735         case OP_LDIV_UN:
2736         case OP_IREM_UN:
2737         case OP_LREM_UN:
2738         case OP_IDIV_IMM:
2739         case OP_LDIV_IMM:
2740         case OP_IREM_IMM:
2741         case OP_LREM_IMM:
2742         case OP_IDIV_UN_IMM:
2743         case OP_LDIV_UN_IMM:
2744         case OP_IREM_UN_IMM:
2745         case OP_LREM_UN_IMM: {
2746                 LLVMValueRef cmp;
2747                 gboolean is_signed = (ins->opcode == OP_IDIV || ins->opcode == OP_LDIV || ins->opcode == OP_IREM || ins->opcode == OP_LREM ||
2748                                                           ins->opcode == OP_IDIV_IMM || ins->opcode == OP_LDIV_IMM || ins->opcode == OP_IREM_IMM || ins->opcode == OP_LREM_IMM);
2749
2750                 cmp = LLVMBuildICmp (builder, LLVMIntEQ, rhs, LLVMConstInt (LLVMTypeOf (rhs), 0, FALSE), "");
2751                 emit_cond_system_exception (ctx, bb, "DivideByZeroException", cmp);
2752                 if (!ctx_ok (ctx))
2753                         break;
2754                 builder = ctx->builder;
2755
2756                 /* b == -1 && a == 0x80000000 */
2757                 if (is_signed) {
2758                         LLVMValueRef c = (LLVMTypeOf (lhs) == LLVMInt32Type ()) ? LLVMConstInt (LLVMTypeOf (lhs), 0x80000000, FALSE) : LLVMConstInt (LLVMTypeOf (lhs), 0x8000000000000000LL, FALSE);
2759                         LLVMValueRef cond1 = LLVMBuildICmp (builder, LLVMIntEQ, rhs, LLVMConstInt (LLVMTypeOf (rhs), -1, FALSE), "");
2760                         LLVMValueRef cond2 = LLVMBuildICmp (builder, LLVMIntEQ, lhs, c, "");
2761
2762                         cmp = LLVMBuildICmp (builder, LLVMIntEQ, LLVMBuildAnd (builder, cond1, cond2, ""), LLVMConstInt (LLVMInt1Type (), 1, FALSE), "");
2763                         emit_cond_system_exception (ctx, bb, "OverflowException", cmp);
2764                         if (!ctx_ok (ctx))
2765                                 break;
2766                         builder = ctx->builder;
2767                 }
2768                 break;
2769         }
2770         default:
2771                 break;
2772         }
2773 }
2774
2775 /*
2776  * emit_init_method:
2777  *
2778  *   Emit code to initialize the GOT slots used by the method.
2779  */
2780 static void
2781 emit_init_method (EmitContext *ctx)
2782 {
2783         LLVMValueRef indexes [16], args [16], callee;
2784         LLVMValueRef inited_var, cmp, call;
2785         LLVMBasicBlockRef inited_bb, notinited_bb;
2786         LLVMBuilderRef builder = ctx->builder;
2787         MonoCompile *cfg = ctx->cfg;
2788
2789         ctx->module->max_inited_idx = MAX (ctx->module->max_inited_idx, cfg->method_index);
2790
2791         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2792         indexes [1] = LLVMConstInt (LLVMInt32Type (), cfg->method_index, FALSE);
2793         inited_var = LLVMBuildLoad (builder, LLVMBuildGEP (builder, ctx->module->inited_var, indexes, 2, ""), "is_inited");
2794
2795         args [0] = inited_var;
2796         args [1] = LLVMConstInt (LLVMInt8Type (), 1, FALSE);
2797         inited_var = LLVMBuildCall (ctx->builder, get_intrinsic (ctx, "llvm.expect.i8"), args, 2, "");
2798
2799         cmp = LLVMBuildICmp (builder, LLVMIntEQ, inited_var, LLVMConstInt (LLVMTypeOf (inited_var), 0, FALSE), "");
2800
2801         inited_bb = ctx->inited_bb;
2802         notinited_bb = gen_bb (ctx, "NOTINITED_BB");
2803
2804         LLVMBuildCondBr (ctx->builder, cmp, notinited_bb, inited_bb);
2805
2806         builder = ctx->builder = create_builder (ctx);
2807         LLVMPositionBuilderAtEnd (ctx->builder, notinited_bb);
2808
2809         // FIXME: Cache
2810         if (ctx->rgctx_arg && cfg->method->is_inflated && mono_method_get_context (cfg->method)->method_inst) {
2811                 args [0] = LLVMConstInt (LLVMInt32Type (), cfg->method_index, 0);
2812                 args [1] = convert (ctx, ctx->rgctx_arg, IntPtrType ());
2813                 callee = ctx->module->init_method_gshared_mrgctx;
2814                 call = LLVMBuildCall (builder, callee, args, 2, "");
2815         } else if (ctx->rgctx_arg) {
2816                 /* A vtable is passed as the rgctx argument */
2817                 args [0] = LLVMConstInt (LLVMInt32Type (), cfg->method_index, 0);
2818                 args [1] = convert (ctx, ctx->rgctx_arg, IntPtrType ());
2819                 callee = ctx->module->init_method_gshared_vtable;
2820                 call = LLVMBuildCall (builder, callee, args, 2, "");
2821         } else if (cfg->gshared) {
2822                 args [0] = LLVMConstInt (LLVMInt32Type (), cfg->method_index, 0);
2823                 args [1] = convert (ctx, ctx->this_arg, ObjRefType ());
2824                 callee = ctx->module->init_method_gshared_this;
2825                 call = LLVMBuildCall (builder, callee, args, 2, "");
2826         } else {
2827                 args [0] = LLVMConstInt (LLVMInt32Type (), cfg->method_index, 0);
2828                 callee = ctx->module->init_method;
2829                 call = LLVMBuildCall (builder, callee, args, 1, "");
2830         }
2831
2832         /*
2833          * This enables llvm to keep arguments in their original registers/
2834          * scratch registers, since the call will not clobber them.
2835          */
2836         mono_llvm_set_call_preserveall_cc (call);
2837
2838         LLVMBuildBr (builder, inited_bb);
2839         ctx->bblocks [cfg->bb_entry->block_num].end_bblock = inited_bb;
2840
2841         builder = ctx->builder = create_builder (ctx);
2842         LLVMPositionBuilderAtEnd (ctx->builder, inited_bb);
2843 }
2844
2845 static void
2846 emit_unbox_tramp (EmitContext *ctx, const char *method_name, LLVMTypeRef method_type, LLVMValueRef method, int method_index)
2847 {
2848         /*
2849          * Emit unbox trampoline using a tail call
2850          */
2851         LLVMValueRef tramp, call, *args;
2852         LLVMBuilderRef builder;
2853         LLVMBasicBlockRef lbb;
2854         LLVMCallInfo *linfo;
2855         char *tramp_name;
2856         int i, nargs;
2857
2858         tramp_name = g_strdup_printf ("ut_%s", method_name);
2859         tramp = LLVMAddFunction (ctx->module->lmodule, tramp_name, method_type);
2860         LLVMSetLinkage (tramp, LLVMInternalLinkage);
2861         LLVMAddFunctionAttr (tramp, LLVMOptimizeForSizeAttribute);
2862         //LLVMAddFunctionAttr (tramp, LLVMNoUnwindAttribute);
2863         linfo = ctx->linfo;
2864         // FIXME: Reduce code duplication with mono_llvm_compile_method () etc.
2865         if (!ctx->llvm_only && ctx->rgctx_arg_pindex != -1)
2866                 LLVMAddAttribute (LLVMGetParam (tramp, ctx->rgctx_arg_pindex), LLVMInRegAttribute);
2867         if (ctx->cfg->vret_addr) {
2868                 LLVMSetValueName (LLVMGetParam (tramp, linfo->vret_arg_pindex), "vret");
2869                 if (linfo->ret.storage == LLVMArgVtypeByRef) {
2870                         LLVMAddAttribute (LLVMGetParam (tramp, linfo->vret_arg_pindex), LLVMStructRetAttribute);
2871                         LLVMAddAttribute (LLVMGetParam (tramp, linfo->vret_arg_pindex), LLVMNoAliasAttribute);
2872                 }
2873         }
2874
2875         lbb = LLVMAppendBasicBlock (tramp, "");
2876         builder = LLVMCreateBuilder ();
2877         LLVMPositionBuilderAtEnd (builder, lbb);
2878
2879         nargs = LLVMCountParamTypes (method_type);
2880         args = g_new0 (LLVMValueRef, nargs);
2881         for (i = 0; i < nargs; ++i) {
2882                 args [i] = LLVMGetParam (tramp, i);
2883                 if (i == ctx->this_arg_pindex) {
2884                         LLVMTypeRef arg_type = LLVMTypeOf (args [i]);
2885
2886                         args [i] = LLVMBuildPtrToInt (builder, args [i], IntPtrType (), "");
2887                         args [i] = LLVMBuildAdd (builder, args [i], LLVMConstInt (IntPtrType (), sizeof (MonoObject), FALSE), "");
2888                         args [i] = LLVMBuildIntToPtr (builder, args [i], arg_type, "");
2889                 }
2890         }
2891         call = LLVMBuildCall (builder, method, args, nargs, "");
2892         if (!ctx->llvm_only && ctx->rgctx_arg_pindex != -1)
2893                 LLVMAddInstrAttribute (call, 1 + ctx->rgctx_arg_pindex, LLVMInRegAttribute);
2894         if (linfo->ret.storage == LLVMArgVtypeByRef)
2895                 LLVMAddInstrAttribute (call, 1 + linfo->vret_arg_pindex, LLVMStructRetAttribute);
2896
2897         // FIXME: This causes assertions in clang
2898         //mono_llvm_set_must_tail (call);
2899         if (LLVMGetReturnType (method_type) == LLVMVoidType ())
2900                 LLVMBuildRetVoid (builder);
2901         else
2902                 LLVMBuildRet (builder, call);
2903
2904         g_hash_table_insert (ctx->module->idx_to_unbox_tramp, GINT_TO_POINTER (method_index), tramp);
2905         LLVMDisposeBuilder (builder);
2906 }
2907
2908 /*
2909  * emit_entry_bb:
2910  *
2911  *   Emit code to load/convert arguments.
2912  */
2913 static void
2914 emit_entry_bb (EmitContext *ctx, LLVMBuilderRef builder)
2915 {
2916         int i, j, pindex;
2917         MonoCompile *cfg = ctx->cfg;
2918         MonoMethodSignature *sig = ctx->sig;
2919         LLVMCallInfo *linfo = ctx->linfo;
2920         MonoBasicBlock *bb;
2921         char **names;
2922
2923         LLVMBuilderRef old_builder = ctx->builder;
2924         ctx->builder = builder;
2925
2926         ctx->alloca_builder = create_builder (ctx);
2927
2928         /*
2929          * Handle indirect/volatile variables by allocating memory for them
2930          * using 'alloca', and storing their address in a temporary.
2931          */
2932         for (i = 0; i < cfg->num_varinfo; ++i) {
2933                 MonoInst *var = cfg->varinfo [i];
2934                 LLVMTypeRef vtype;
2935
2936                 if (var->opcode == OP_GSHAREDVT_LOCAL || var->opcode == OP_GSHAREDVT_ARG_REGOFFSET) {
2937                 } else if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (mini_type_is_vtype (var->inst_vtype) && !MONO_CLASS_IS_SIMD (ctx->cfg, var->klass))) {
2938                         vtype = type_to_llvm_type (ctx, var->inst_vtype);
2939                         if (!ctx_ok (ctx))
2940                                 return;
2941                         /* Could be already created by an OP_VPHI */
2942                         if (!ctx->addresses [var->dreg]) {
2943                                 ctx->addresses [var->dreg] = build_alloca (ctx, var->inst_vtype);
2944                                 //LLVMSetValueName (ctx->addresses [var->dreg], g_strdup_printf ("vreg_loc_%d", var->dreg));
2945                         }
2946                         ctx->vreg_cli_types [var->dreg] = var->inst_vtype;
2947                 }
2948         }
2949
2950         names = g_new (char *, sig->param_count);
2951         mono_method_get_param_names (cfg->method, (const char **) names);
2952
2953         for (i = 0; i < sig->param_count; ++i) {
2954                 LLVMArgInfo *ainfo = &linfo->args [i + sig->hasthis];
2955                 int reg = cfg->args [i + sig->hasthis]->dreg;
2956                 char *name;
2957
2958                 pindex = ainfo->pindex;
2959
2960                 switch (ainfo->storage) {
2961                 case LLVMArgVtypeInReg:
2962                 case LLVMArgAsFpArgs: {
2963                         LLVMValueRef args [8];
2964                         int j;
2965
2966                         pindex += ainfo->ndummy_fpargs;
2967
2968                         /* The argument is received as a set of int/fp arguments, store them into the real argument */
2969                         memset (args, 0, sizeof (args));
2970                         if (ainfo->storage == LLVMArgVtypeInReg) {
2971                                 args [0] = LLVMGetParam (ctx->lmethod, pindex);
2972                                 if (ainfo->pair_storage [1] != LLVMArgNone)
2973                                         args [1] = LLVMGetParam (ctx->lmethod, pindex + 1);
2974                         } else {
2975                                 g_assert (ainfo->nslots <= 8);
2976                                 for (j = 0; j < ainfo->nslots; ++j)
2977                                         args [j] = LLVMGetParam (ctx->lmethod, pindex + j);
2978                         }
2979                         ctx->addresses [reg] = build_alloca (ctx, ainfo->type);
2980
2981                         emit_args_to_vtype (ctx, builder, ainfo->type, ctx->addresses [reg], ainfo, args);
2982
2983                         if (ainfo->storage == LLVMArgVtypeInReg && MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (ainfo->type))) {
2984                                 /* Treat these as normal values */
2985                                 ctx->values [reg] = LLVMBuildLoad (builder, ctx->addresses [reg], "");
2986                         }
2987                         break;
2988                 }
2989                 case LLVMArgVtypeByVal: {
2990                         ctx->addresses [reg] = LLVMGetParam (ctx->lmethod, pindex);
2991
2992                         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (ainfo->type))) {
2993                                 /* Treat these as normal values */
2994                                 ctx->values [reg] = LLVMBuildLoad (builder, ctx->addresses [reg], "");
2995                         }
2996                         break;
2997                 }
2998                 case LLVMArgVtypeByRef: {
2999                         /* The argument is passed by ref */
3000                         ctx->addresses [reg] = LLVMGetParam (ctx->lmethod, pindex);
3001                         break;
3002                 }
3003                 case LLVMArgAsIArgs: {
3004                         LLVMValueRef arg = LLVMGetParam (ctx->lmethod, pindex);
3005                         int size;
3006
3007                         /* The argument is received as an array of ints, store it into the real argument */
3008                         ctx->addresses [reg] = build_alloca (ctx, ainfo->type);
3009
3010                         size = mono_class_value_size (mono_class_from_mono_type (ainfo->type), NULL);
3011                         if (size < SIZEOF_VOID_P) {
3012                                 /* The upper bits of the registers might not be valid */
3013                                 LLVMValueRef val = LLVMBuildExtractValue (builder, arg, 0, "");
3014                                 LLVMValueRef dest = convert (ctx, ctx->addresses [reg], LLVMPointerType (LLVMIntType (size * 8), 0));
3015                                 LLVMBuildStore (ctx->builder, LLVMBuildTrunc (builder, val, LLVMIntType (size * 8), ""), dest);
3016                         } else {
3017                                 LLVMBuildStore (ctx->builder, arg, convert (ctx, ctx->addresses [reg], LLVMPointerType (LLVMTypeOf (arg), 0)));
3018                         }
3019                         break;
3020                 }
3021                 case LLVMArgVtypeAsScalar:
3022                         g_assert_not_reached ();
3023                         break;
3024                 case LLVMArgGsharedvtFixed: {
3025                         /* These are non-gsharedvt arguments passed by ref, the rest of the IR treats them as scalars */
3026                         LLVMValueRef arg = LLVMGetParam (ctx->lmethod, pindex);
3027
3028                         if (names [i])
3029                                 name = g_strdup_printf ("arg_%s", names [i]);
3030                         else
3031                                 name = g_strdup_printf ("arg_%d", i);
3032
3033                         ctx->values [reg] = LLVMBuildLoad (builder, convert (ctx, arg, LLVMPointerType (type_to_llvm_type (ctx, ainfo->type), 0)), name);
3034                         break;
3035                 }
3036                 case LLVMArgGsharedvtFixedVtype: {
3037                         LLVMValueRef arg = LLVMGetParam (ctx->lmethod, pindex);
3038
3039                         if (names [i])
3040                                 name = g_strdup_printf ("vtype_arg_%s", names [i]);
3041                         else
3042                                 name = g_strdup_printf ("vtype_arg_%d", i);
3043
3044                         /* Non-gsharedvt vtype argument passed by ref, the rest of the IR treats it as a vtype */
3045                         g_assert (ctx->addresses [reg]);
3046                         LLVMSetValueName (ctx->addresses [reg], name);
3047                         LLVMBuildStore (builder, LLVMBuildLoad (builder, convert (ctx, arg, LLVMPointerType (type_to_llvm_type (ctx, ainfo->type), 0)), ""), ctx->addresses [reg]);
3048                         break;
3049                 }
3050                 case LLVMArgGsharedvtVariable:
3051                         /* The IR treats these as variables with addresses */
3052                         ctx->addresses [reg] = LLVMGetParam (ctx->lmethod, pindex);
3053                         break;
3054                 default:
3055                         ctx->values [reg] = convert_full (ctx, ctx->values [reg], llvm_type_to_stack_type (cfg, type_to_llvm_type (ctx, ainfo->type)), type_is_unsigned (ctx, ainfo->type));
3056                         break;
3057                 }
3058         }
3059         g_free (names);
3060
3061         if (cfg->vret_addr)
3062                 emit_volatile_store (ctx, cfg->vret_addr->dreg);
3063         if (sig->hasthis)
3064                 emit_volatile_store (ctx, cfg->args [0]->dreg);
3065         for (i = 0; i < sig->param_count; ++i)
3066                 if (!mini_type_is_vtype (sig->params [i]))
3067                         emit_volatile_store (ctx, cfg->args [i + sig->hasthis]->dreg);
3068
3069         if (sig->hasthis && !cfg->rgctx_var && cfg->gshared) {
3070                 LLVMValueRef this_alloc;
3071
3072                 /*
3073                  * The exception handling code needs the location where the this argument was
3074                  * stored for gshared methods. We create a separate alloca to hold it, and mark it
3075                  * with the "mono.this" custom metadata to tell llvm that it needs to save its
3076                  * location into the LSDA.
3077                  */
3078                 this_alloc = mono_llvm_build_alloca (builder, ThisType (), LLVMConstInt (LLVMInt32Type (), 1, FALSE), 0, "");
3079                 /* This volatile store will keep the alloca alive */
3080                 mono_llvm_build_store (builder, ctx->values [cfg->args [0]->dreg], this_alloc, TRUE, LLVM_BARRIER_NONE);
3081
3082                 set_metadata_flag (this_alloc, "mono.this");
3083         }
3084
3085         if (cfg->rgctx_var) {
3086                 LLVMValueRef rgctx_alloc, store;
3087
3088                 /*
3089                  * We handle the rgctx arg similarly to the this pointer.
3090                  */
3091                 g_assert (ctx->addresses [cfg->rgctx_var->dreg]);
3092                 rgctx_alloc = ctx->addresses [cfg->rgctx_var->dreg];
3093                 /* This volatile store will keep the alloca alive */
3094                 store = mono_llvm_build_store (builder, convert (ctx, ctx->rgctx_arg, IntPtrType ()), rgctx_alloc, TRUE, LLVM_BARRIER_NONE);
3095
3096                 set_metadata_flag (rgctx_alloc, "mono.this");
3097         }
3098
3099         /* Initialize the method if needed */
3100         if (cfg->compile_aot && ctx->llvm_only) {
3101                 /* Emit a location for the initialization code */
3102                 ctx->init_bb = gen_bb (ctx, "INIT_BB");
3103                 ctx->inited_bb = gen_bb (ctx, "INITED_BB");
3104
3105                 LLVMBuildBr (ctx->builder, ctx->init_bb);
3106                 builder = ctx->builder = create_builder (ctx);
3107                 LLVMPositionBuilderAtEnd (ctx->builder, ctx->inited_bb);
3108                 ctx->bblocks [cfg->bb_entry->block_num].end_bblock = ctx->inited_bb;
3109         }
3110
3111         /* Compute nesting between clauses */
3112         ctx->nested_in = (GSList**)mono_mempool_alloc0 (cfg->mempool, sizeof (GSList*) * cfg->header->num_clauses);
3113         for (i = 0; i < cfg->header->num_clauses; ++i) {
3114                 for (j = 0; j < cfg->header->num_clauses; ++j) {
3115                         MonoExceptionClause *clause1 = &cfg->header->clauses [i];
3116                         MonoExceptionClause *clause2 = &cfg->header->clauses [j];
3117
3118                         if (i != j && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset)
3119                                 ctx->nested_in [i] = g_slist_prepend_mempool (cfg->mempool, ctx->nested_in [i], GINT_TO_POINTER (j));
3120                 }
3121         }
3122
3123         /*
3124          * For finally clauses, create an indicator variable telling OP_ENDFINALLY whenever
3125          * it needs to continue normally, or return back to the exception handling system.
3126          */
3127         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3128                 int clause_index;
3129                 char name [128];
3130
3131                 if (!(bb->region != -1 && (bb->flags & BB_EXCEPTION_HANDLER)))
3132                         continue;
3133
3134                 clause_index = MONO_REGION_CLAUSE_INDEX (bb->region);
3135                 g_hash_table_insert (ctx->region_to_handler, GUINT_TO_POINTER (mono_get_block_region_notry (cfg, bb->region)), bb);
3136                 g_hash_table_insert (ctx->clause_to_handler, GINT_TO_POINTER (clause_index), bb);
3137
3138                 if (bb->in_scount == 0) {
3139                         LLVMValueRef val;
3140
3141                         sprintf (name, "finally_ind_bb%d", bb->block_num);
3142                         val = LLVMBuildAlloca (builder, LLVMInt32Type (), name);
3143                         LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), val);
3144
3145                         ctx->bblocks [bb->block_num].finally_ind = val;
3146                 } else {
3147                         /* Create a variable to hold the exception var */
3148                         if (!ctx->ex_var)
3149                                 ctx->ex_var = LLVMBuildAlloca (builder, ObjRefType (), "exvar");
3150                 }
3151
3152                 /*
3153                  * Create a new bblock which CALL_HANDLER/landing pads can branch to, because branching to the
3154                  * LLVM bblock containing a landing pad causes problems for the
3155                  * LLVM optimizer passes.
3156                  */
3157                 sprintf (name, "BB%d_CALL_HANDLER_TARGET", bb->block_num);
3158                 ctx->bblocks [bb->block_num].call_handler_target_bb = LLVMAppendBasicBlock (ctx->lmethod, name);
3159         }
3160         ctx->builder = old_builder;
3161 }
3162
3163 static void
3164 process_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, MonoInst *ins)
3165 {
3166         MonoCompile *cfg = ctx->cfg;
3167         LLVMValueRef *values = ctx->values;
3168         LLVMValueRef *addresses = ctx->addresses;
3169         MonoCallInst *call = (MonoCallInst*)ins;
3170         MonoMethodSignature *sig = call->signature;
3171         LLVMValueRef callee = NULL, lcall;
3172         LLVMValueRef *args;
3173         LLVMCallInfo *cinfo;
3174         GSList *l;
3175         int i, len, nargs;
3176         gboolean vretaddr;
3177         LLVMTypeRef llvm_sig;
3178         gpointer target;
3179         gboolean is_virtual, calli, preserveall;
3180         LLVMBuilderRef builder = *builder_ref;
3181
3182         if ((call->signature->call_convention != MONO_CALL_DEFAULT) && !((call->signature->call_convention == MONO_CALL_C) && ctx->llvm_only)) {
3183                 set_failure (ctx, "non-default callconv");
3184                 return;
3185         }
3186
3187         cinfo = call->cinfo;
3188         g_assert (cinfo);
3189         if (call->rgctx_arg_reg)
3190                 cinfo->rgctx_arg = TRUE;
3191         if (call->imt_arg_reg)
3192                 cinfo->imt_arg = TRUE;
3193
3194         vretaddr = (cinfo->ret.storage == LLVMArgVtypeRetAddr || cinfo->ret.storage == LLVMArgVtypeByRef || cinfo->ret.storage == LLVMArgGsharedvtFixed || cinfo->ret.storage == LLVMArgGsharedvtVariable || cinfo->ret.storage == LLVMArgGsharedvtFixedVtype);
3195
3196         llvm_sig = sig_to_llvm_sig_full (ctx, sig, cinfo);
3197         if (!ctx_ok (ctx))
3198                 return;
3199
3200         is_virtual = (ins->opcode == OP_VOIDCALL_MEMBASE || ins->opcode == OP_CALL_MEMBASE || ins->opcode == OP_VCALL_MEMBASE || ins->opcode == OP_LCALL_MEMBASE || ins->opcode == OP_FCALL_MEMBASE || ins->opcode == OP_RCALL_MEMBASE);
3201         calli = !call->fptr_is_patch && (ins->opcode == OP_VOIDCALL_REG || ins->opcode == OP_CALL_REG || ins->opcode == OP_VCALL_REG || ins->opcode == OP_LCALL_REG || ins->opcode == OP_FCALL_REG || ins->opcode == OP_RCALL_REG);
3202         /* Unused */
3203         preserveall = FALSE;
3204
3205         /* FIXME: Avoid creating duplicate methods */
3206
3207         if (ins->flags & MONO_INST_HAS_METHOD) {
3208                 if (is_virtual) {
3209                         callee = NULL;
3210                 } else {
3211                         if (cfg->compile_aot) {
3212                                 callee = get_callee (ctx, llvm_sig, MONO_PATCH_INFO_METHOD, call->method);
3213                                 if (!callee) {
3214                                         set_failure (ctx, "can't encode patch");
3215                                         return;
3216                                 }
3217                                 if (cfg->llvm_only && call->method->klass->image->assembly == ctx->module->assembly) {
3218                                         /*
3219                                          * Collect instructions representing the callee into a hash so they can be replaced
3220                                          * by the llvm method for the callee if the callee turns out to be direct
3221                                          * callable. Currently this only requires it to not fail llvm compilation.
3222                                          */
3223                                         GSList *l = (GSList*)g_hash_table_lookup (ctx->method_to_callers, call->method);
3224                                         l = g_slist_prepend (l, callee);
3225                                         g_hash_table_insert (ctx->method_to_callers, call->method, l);
3226                                 }
3227                         } else {
3228                                 MonoError error;
3229                                 static int tramp_index;
3230                                 char *name;
3231
3232                                 name = g_strdup_printf ("tramp_%d", tramp_index);
3233                                 tramp_index ++;
3234
3235 #if LLVM_API_VERSION > 100
3236                                 /*
3237                                  * Use our trampoline infrastructure for lazy compilation instead of llvm's.
3238                                  * Make all calls through a global. The address of the global will be saved in
3239                                  * MonoJitDomainInfo.llvm_jit_callees and updated when the method it refers to is
3240                                  * compiled.
3241                                  */
3242                                 LLVMValueRef tramp_var = g_hash_table_lookup (ctx->jit_callees, call->method);
3243                                 if (!tramp_var) {
3244                                         target =
3245                                                 mono_create_jit_trampoline (mono_domain_get (),
3246                                                                                                         call->method, &error);
3247                                         if (!is_ok (&error)) {
3248                                                 set_failure (ctx, mono_error_get_message (&error));
3249                                                 mono_error_cleanup (&error);
3250                                                 return;
3251                                         }
3252
3253                                         tramp_var = LLVMAddGlobal (ctx->lmodule, LLVMPointerType (llvm_sig, 0), name);
3254                                         LLVMSetInitializer (tramp_var, LLVMConstIntToPtr (LLVMConstInt (LLVMInt64Type (), (guint64)(size_t)target, FALSE), LLVMPointerType (llvm_sig, 0)));
3255                                         LLVMSetLinkage (tramp_var, LLVMExternalLinkage);
3256                                         g_hash_table_insert (ctx->jit_callees, call->method, tramp_var);
3257                                 }
3258                                 callee = LLVMBuildLoad (builder, tramp_var, "");
3259 #else
3260                                 target =
3261                                         mono_create_jit_trampoline (mono_domain_get (),
3262                                                                     call->method, &error);
3263                                 if (!is_ok (&error)) {
3264                                         g_free (name);
3265                                         set_failure (ctx, mono_error_get_message (&error));
3266                                         mono_error_cleanup (&error);
3267                                         return;
3268                                 }
3269
3270                                 callee = LLVMAddFunction (ctx->lmodule, name, llvm_sig);
3271                                 g_free (name);
3272
3273                                 LLVMAddGlobalMapping (ctx->module->ee, callee, target);
3274 #endif
3275                         }
3276                 }
3277
3278                 if (!cfg->llvm_only && call->method && strstr (call->method->klass->name, "AsyncVoidMethodBuilder")) {
3279                         /* LLVM miscompiles async methods */
3280                         set_failure (ctx, "#13734");
3281                         return;
3282                 }
3283         } else if (calli) {
3284         } else {
3285                 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
3286
3287                 if (info) {
3288                         /*
3289                           MonoJumpInfo ji;
3290
3291                           memset (&ji, 0, sizeof (ji));
3292                           ji.type = MONO_PATCH_INFO_JIT_ICALL_ADDR;
3293                           ji.data.target = info->name;
3294
3295                           target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &ji, FALSE);
3296                         */
3297                         if (cfg->compile_aot) {
3298                                 callee = get_callee (ctx, llvm_sig, MONO_PATCH_INFO_INTERNAL_METHOD, (char*)info->name);
3299                                 if (!callee) {
3300                                         set_failure (ctx, "can't encode patch");
3301                                         return;
3302                                 }
3303                         } else {
3304                                 target = (gpointer)mono_icall_get_wrapper (info);
3305                                 callee = emit_jit_callee (ctx, "", llvm_sig, target);
3306                         }
3307                 } else {
3308                         if (cfg->compile_aot) {
3309                                 callee = NULL;
3310                                 if (cfg->abs_patches) {
3311                                         MonoJumpInfo *abs_ji = (MonoJumpInfo*)g_hash_table_lookup (cfg->abs_patches, call->fptr);
3312                                         if (abs_ji) {
3313                                                 callee = get_callee (ctx, llvm_sig, abs_ji->type, abs_ji->data.target);
3314                                                 if (!callee) {
3315                                                         set_failure (ctx, "can't encode patch");
3316                                                         return;
3317                                                 }
3318                                         }
3319                                 }
3320                                 if (!callee) {
3321                                         set_failure (ctx, "aot");
3322                                         return;
3323                                 }
3324                         } else {
3325 #if LLVM_API_VERSION > 100
3326                                 if (cfg->abs_patches) {
3327                                         MonoJumpInfo *abs_ji = (MonoJumpInfo*)g_hash_table_lookup (cfg->abs_patches, call->fptr);
3328                                         if (abs_ji) {
3329                                                 MonoError error;
3330
3331                                                 target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, abs_ji, FALSE, &error);
3332                                                 mono_error_assert_ok (&error);
3333                                                 callee = emit_jit_callee (ctx, "", llvm_sig, target);
3334                                         } else {
3335                                                 g_assert_not_reached ();
3336                                         }
3337                                 } else {
3338                                         g_assert_not_reached ();
3339                                 }
3340 #else
3341                                 callee = LLVMAddFunction (ctx->lmodule, "", llvm_sig);
3342                                 target = NULL;
3343                                 if (cfg->abs_patches) {
3344                                         MonoJumpInfo *abs_ji = (MonoJumpInfo*)g_hash_table_lookup (cfg->abs_patches, call->fptr);
3345                                         if (abs_ji) {
3346                                                 MonoError error;
3347
3348                                                 /*
3349                                                  * FIXME: Some trampolines might have
3350                                                  * their own calling convention on some platforms.
3351                                                  */
3352                                                 target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, abs_ji, FALSE, &error);
3353                                                 mono_error_assert_ok (&error);
3354                                                 LLVMAddGlobalMapping (ctx->module->ee, callee, target);
3355                                         }
3356                                 }
3357                                 if (!target)
3358                                         LLVMAddGlobalMapping (ctx->module->ee, callee, (gpointer)call->fptr);
3359 #endif
3360                         }
3361                 }
3362         }
3363
3364         if (is_virtual) {
3365                 int size = sizeof (gpointer);
3366                 LLVMValueRef index;
3367
3368                 g_assert (ins->inst_offset % size == 0);
3369                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
3370
3371                 callee = convert (ctx, LLVMBuildLoad (builder, LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (LLVMPointerType (IntPtrType (), 0), 0)), &index, 1, ""), ""), LLVMPointerType (llvm_sig, 0));
3372         } else if (calli) {
3373                 callee = convert (ctx, values [ins->sreg1], LLVMPointerType (llvm_sig, 0));
3374         } else {
3375                 if (ins->flags & MONO_INST_HAS_METHOD) {
3376                 }
3377         }
3378
3379         /* 
3380          * Collect and convert arguments
3381          */
3382         nargs = (sig->param_count * 16) + sig->hasthis + vretaddr + call->rgctx_reg + call->imt_arg_reg;
3383         len = sizeof (LLVMValueRef) * nargs;
3384         args = (LLVMValueRef*)alloca (len);
3385         memset (args, 0, len);
3386         l = call->out_ireg_args;
3387
3388         if (call->rgctx_arg_reg) {
3389                 g_assert (values [call->rgctx_arg_reg]);
3390                 g_assert (cinfo->rgctx_arg_pindex < nargs);
3391                 /*
3392                  * On ARM, the imt/rgctx argument is passed in a caller save register, but some of our trampolines etc. clobber it, leading to
3393                  * problems is LLVM moves the arg assignment earlier. To work around this, save the argument into a stack slot and load
3394                  * it using a volatile load.
3395                  */
3396 #ifdef TARGET_ARM
3397                 if (!ctx->imt_rgctx_loc)
3398                         ctx->imt_rgctx_loc = build_alloca_llvm_type (ctx, ctx->module->ptr_type, sizeof (gpointer));
3399                 LLVMBuildStore (builder, convert (ctx, ctx->values [call->rgctx_arg_reg], ctx->module->ptr_type), ctx->imt_rgctx_loc);
3400                 args [cinfo->rgctx_arg_pindex] = mono_llvm_build_load (builder, ctx->imt_rgctx_loc, "", TRUE);
3401 #else
3402                 args [cinfo->rgctx_arg_pindex] = convert (ctx, values [call->rgctx_arg_reg], ctx->module->ptr_type);
3403 #endif
3404         }
3405         if (call->imt_arg_reg) {
3406                 g_assert (!ctx->llvm_only);
3407                 g_assert (values [call->imt_arg_reg]);
3408                 g_assert (cinfo->imt_arg_pindex < nargs);
3409 #ifdef TARGET_ARM
3410                 if (!ctx->imt_rgctx_loc)
3411                         ctx->imt_rgctx_loc = build_alloca_llvm_type (ctx, ctx->module->ptr_type, sizeof (gpointer));
3412                 LLVMBuildStore (builder, convert (ctx, ctx->values [call->imt_arg_reg], ctx->module->ptr_type), ctx->imt_rgctx_loc);
3413                 args [cinfo->imt_arg_pindex] = mono_llvm_build_load (builder, ctx->imt_rgctx_loc, "", TRUE);
3414 #else
3415                 args [cinfo->imt_arg_pindex] = convert (ctx, values [call->imt_arg_reg], ctx->module->ptr_type);
3416 #endif
3417         }
3418         switch (cinfo->ret.storage) {
3419         case LLVMArgGsharedvtVariable: {
3420                 MonoInst *var = get_vreg_to_inst (cfg, call->inst.dreg);
3421
3422                 if (var && var->opcode == OP_GSHAREDVT_LOCAL) {
3423                         args [cinfo->vret_arg_pindex] = convert (ctx, emit_gsharedvt_ldaddr (ctx, var->dreg), IntPtrType ());
3424                 } else {
3425                         g_assert (addresses [call->inst.dreg]);
3426                         args [cinfo->vret_arg_pindex] = addresses [call->inst.dreg];
3427                 }
3428                 break;
3429         }
3430         default:
3431                 if (vretaddr) {
3432                         if (!addresses [call->inst.dreg])
3433                                 addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
3434                         g_assert (cinfo->vret_arg_pindex < nargs);
3435                         if (cinfo->ret.storage == LLVMArgVtypeByRef)
3436                                 args [cinfo->vret_arg_pindex] = addresses [call->inst.dreg];
3437                         else
3438                                 args [cinfo->vret_arg_pindex] = LLVMBuildPtrToInt (builder, addresses [call->inst.dreg], IntPtrType (), "");
3439                 }
3440                 break;
3441         }
3442
3443         /*
3444          * Sometimes the same method is called with two different signatures (i.e. with and without 'this'), so
3445          * use the real callee for argument type conversion.
3446          */
3447         LLVMTypeRef callee_type = LLVMGetElementType (LLVMTypeOf (callee));
3448         LLVMTypeRef *param_types = (LLVMTypeRef*)g_alloca (sizeof (LLVMTypeRef) * LLVMCountParamTypes (callee_type));
3449         LLVMGetParamTypes (callee_type, param_types);
3450
3451         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3452                 guint32 regpair;
3453                 int reg, pindex;
3454                 LLVMArgInfo *ainfo = &call->cinfo->args [i];
3455
3456                 pindex = ainfo->pindex;
3457
3458                 regpair = (guint32)(gssize)(l->data);
3459                 reg = regpair & 0xffffff;
3460                 args [pindex] = values [reg];
3461                 switch (ainfo->storage) {
3462                 case LLVMArgVtypeInReg:
3463                 case LLVMArgAsFpArgs: {
3464                         guint32 nargs;
3465                         int j;
3466
3467                         for (j = 0; j < ainfo->ndummy_fpargs; ++j)
3468                                 args [pindex + j] = LLVMConstNull (LLVMDoubleType ());
3469                         pindex += ainfo->ndummy_fpargs;
3470
3471                         g_assert (addresses [reg]);
3472                         emit_vtype_to_args (ctx, builder, ainfo->type, addresses [reg], ainfo, args + pindex, &nargs);
3473                         pindex += nargs;
3474
3475                         // FIXME: alignment
3476                         // FIXME: Get rid of the VMOVE
3477                         break;
3478                 }
3479                 case LLVMArgVtypeByVal:
3480                         g_assert (addresses [reg]);
3481                         args [pindex] = addresses [reg];
3482                         break;
3483                 case LLVMArgVtypeByRef: {
3484                         g_assert (addresses [reg]);
3485                         args [pindex] = convert (ctx, addresses [reg], LLVMPointerType (type_to_llvm_arg_type (ctx, ainfo->type), 0));
3486                         break;
3487                 }
3488                 case LLVMArgAsIArgs:
3489                         g_assert (addresses [reg]);
3490                         args [pindex] = LLVMBuildLoad (ctx->builder, convert (ctx, addresses [reg], LLVMPointerType (LLVMArrayType (IntPtrType (), ainfo->nslots), 0)), "");
3491                         break;
3492                 case LLVMArgVtypeAsScalar:
3493                         g_assert_not_reached ();
3494                         break;
3495                 case LLVMArgGsharedvtFixed:
3496                 case LLVMArgGsharedvtFixedVtype:
3497                         g_assert (addresses [reg]);
3498                         args [pindex] = convert (ctx, addresses [reg], LLVMPointerType (type_to_llvm_arg_type (ctx, ainfo->type), 0));
3499                         break;
3500                 case LLVMArgGsharedvtVariable:
3501                         g_assert (addresses [reg]);
3502                         args [pindex] = convert (ctx, addresses [reg], LLVMPointerType (IntPtrType (), 0));
3503                         break;
3504                 default:
3505                         g_assert (args [pindex]);
3506                         if (i == 0 && sig->hasthis)
3507                                 args [pindex] = convert (ctx, args [pindex], param_types [pindex]);
3508                         else
3509                                 args [pindex] = convert (ctx, args [pindex], type_to_llvm_arg_type (ctx, ainfo->type));
3510                         break;
3511                 }
3512                 g_assert (pindex <= nargs);
3513
3514                 l = l->next;
3515         }
3516
3517         // FIXME: Align call sites
3518
3519         /*
3520          * Emit the call
3521          */
3522
3523         lcall = emit_call (ctx, bb, &builder, callee, args, LLVMCountParamTypes (llvm_sig));
3524
3525         if (ins->opcode != OP_TAILCALL && LLVMGetInstructionOpcode (lcall) == LLVMCall)
3526                 mono_llvm_set_call_notail (lcall);
3527
3528         /*
3529          * Modify cconv and parameter attributes to pass rgctx/imt correctly.
3530          */
3531 #if defined(MONO_ARCH_IMT_REG) && defined(MONO_ARCH_RGCTX_REG)
3532         g_assert (MONO_ARCH_IMT_REG == MONO_ARCH_RGCTX_REG);
3533 #endif
3534         /* The two can't be used together, so use only one LLVM calling conv to pass them */
3535         g_assert (!(call->rgctx_arg_reg && call->imt_arg_reg));
3536         if (!sig->pinvoke && !cfg->llvm_only)
3537                 LLVMSetInstructionCallConv (lcall, LLVMMono1CallConv);
3538         if (preserveall)
3539                 mono_llvm_set_call_preserveall_cc (lcall);
3540
3541         if (cinfo->ret.storage == LLVMArgVtypeByRef)
3542                 LLVMAddInstrAttribute (lcall, 1 + cinfo->vret_arg_pindex, LLVMStructRetAttribute);
3543         if (!ctx->llvm_only && call->rgctx_arg_reg)
3544                 LLVMAddInstrAttribute (lcall, 1 + cinfo->rgctx_arg_pindex, LLVMInRegAttribute);
3545         if (call->imt_arg_reg)
3546                 LLVMAddInstrAttribute (lcall, 1 + cinfo->imt_arg_pindex, LLVMInRegAttribute);
3547
3548         /* Add byval attributes if needed */
3549         for (i = 0; i < sig->param_count; ++i) {
3550                 LLVMArgInfo *ainfo = &call->cinfo->args [i + sig->hasthis];
3551
3552                 if (ainfo && ainfo->storage == LLVMArgVtypeByVal)
3553                         LLVMAddInstrAttribute (lcall, 1 + ainfo->pindex, LLVMByValAttribute);
3554         }
3555
3556         /*
3557          * Convert the result
3558          */
3559         switch (cinfo->ret.storage) {
3560         case LLVMArgVtypeInReg: {
3561                 LLVMValueRef regs [2];
3562
3563                 if (LLVMTypeOf (lcall) == LLVMVoidType ())
3564                         /* Empty struct */
3565                         break;
3566
3567                 if (!addresses [ins->dreg])
3568                         addresses [ins->dreg] = build_alloca (ctx, sig->ret);
3569
3570                 regs [0] = LLVMBuildExtractValue (builder, lcall, 0, "");
3571                 if (cinfo->ret.pair_storage [1] != LLVMArgNone)
3572                         regs [1] = LLVMBuildExtractValue (builder, lcall, 1, "");
3573                 emit_args_to_vtype (ctx, builder, sig->ret, addresses [ins->dreg], &cinfo->ret, regs);
3574                 break;
3575         }
3576         case LLVMArgVtypeByVal:
3577                 if (!addresses [call->inst.dreg])
3578                         addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
3579                 LLVMBuildStore (builder, lcall, addresses [call->inst.dreg]);
3580                 break;
3581         case LLVMArgAsIArgs:
3582         case LLVMArgFpStruct:
3583                 if (!addresses [call->inst.dreg])
3584                         addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
3585                 LLVMBuildStore (builder, lcall, convert_full (ctx, addresses [call->inst.dreg], LLVMPointerType (LLVMTypeOf (lcall), 0), FALSE));
3586                 break;
3587         case LLVMArgVtypeAsScalar:
3588                 if (!addresses [call->inst.dreg])
3589                         addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
3590                 LLVMBuildStore (builder, lcall, convert_full (ctx, addresses [call->inst.dreg], LLVMPointerType (LLVMTypeOf (lcall), 0), FALSE));
3591                 break;
3592         case LLVMArgVtypeRetAddr:
3593         case LLVMArgVtypeByRef:
3594                 if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (sig->ret))) {
3595                         /* Some opcodes like STOREX_MEMBASE access these by value */
3596                         g_assert (addresses [call->inst.dreg]);
3597                         values [ins->dreg] = LLVMBuildLoad (builder, convert_full (ctx, addresses [call->inst.dreg], LLVMPointerType (type_to_llvm_type (ctx, sig->ret), 0), FALSE), "");
3598                 }
3599                 break;
3600         case LLVMArgGsharedvtVariable:
3601                 break;
3602         case LLVMArgGsharedvtFixed:
3603         case LLVMArgGsharedvtFixedVtype:
3604                 values [ins->dreg] = LLVMBuildLoad (builder, convert_full (ctx, addresses [call->inst.dreg], LLVMPointerType (type_to_llvm_type (ctx, sig->ret), 0), FALSE), "");
3605                 break;
3606         default:
3607                 if (sig->ret->type != MONO_TYPE_VOID)
3608                         /* If the method returns an unsigned value, need to zext it */
3609                         values [ins->dreg] = convert_full (ctx, lcall, llvm_type_to_stack_type (cfg, type_to_llvm_type (ctx, sig->ret)), type_is_unsigned (ctx, sig->ret));
3610                 break;
3611         }
3612
3613         *builder_ref = ctx->builder;
3614 }
3615
3616 static void
3617 emit_llvmonly_throw (EmitContext *ctx, MonoBasicBlock *bb, gboolean rethrow, LLVMValueRef exc)
3618 {
3619         const char *icall_name = rethrow ? "mono_llvm_rethrow_exception" : "mono_llvm_throw_exception";
3620         LLVMValueRef callee = rethrow ? ctx->module->rethrow : ctx->module->throw_icall;
3621
3622         LLVMTypeRef exc_type = type_to_llvm_type (ctx, &mono_get_exception_class ()->byval_arg);
3623
3624         if (!callee) {
3625                 LLVMTypeRef fun_sig = LLVMFunctionType1 (LLVMVoidType (), exc_type, FALSE);
3626
3627                 if (ctx->cfg->compile_aot) {
3628                         callee = get_callee (ctx, fun_sig, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
3629                 } else {
3630                         callee = LLVMAddFunction (ctx->lmodule, icall_name, fun_sig);
3631                         LLVMAddGlobalMapping (ctx->module->ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
3632                         mono_memory_barrier ();
3633
3634                         if (rethrow)
3635                                 ctx->module->rethrow = callee;
3636                         else
3637                                 ctx->module->throw_icall = callee;
3638                 }
3639         }
3640
3641         LLVMValueRef args [2];
3642
3643         args [0] = convert (ctx, exc, exc_type);
3644         emit_call (ctx, bb, &ctx->builder, callee, args, 1);
3645
3646         LLVMBuildUnreachable (ctx->builder);
3647
3648         ctx->builder = create_builder (ctx);
3649 }
3650
3651 static void
3652 emit_throw (EmitContext *ctx, MonoBasicBlock *bb, gboolean rethrow, LLVMValueRef exc)
3653 {
3654         MonoMethodSignature *throw_sig;
3655         LLVMValueRef callee, arg;
3656         const char *icall_name;
3657                                 
3658         callee = rethrow ? ctx->module->rethrow : ctx->module->throw_icall;
3659         icall_name = rethrow ? "mono_arch_rethrow_exception" : "mono_arch_throw_exception";
3660
3661         if (!callee) {
3662                 throw_sig = mono_metadata_signature_alloc (mono_get_corlib (), 1);
3663                 throw_sig->ret = &mono_get_void_class ()->byval_arg;
3664                 throw_sig->params [0] = &mono_get_object_class ()->byval_arg;
3665                 if (ctx->cfg->compile_aot) {
3666                         callee = get_callee (ctx, sig_to_llvm_sig (ctx, throw_sig), MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
3667                 } else {
3668                         gpointer target;
3669 #ifdef TARGET_X86
3670                         /* 
3671                          * LLVM doesn't push the exception argument, so we need a different
3672                          * trampoline.
3673                          */
3674                         target = resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, rethrow ? "llvm_rethrow_exception_trampoline" : "llvm_throw_exception_trampoline");
3675 #else
3676                         target = resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
3677 #endif
3678                         callee = emit_jit_callee (ctx, icall_name, sig_to_llvm_sig (ctx, throw_sig), target);
3679                 }
3680
3681                 mono_memory_barrier ();
3682 #if LLVM_API_VERSION < 100
3683                 if (rethrow)
3684                         ctx->module->rethrow = callee;
3685                 else
3686                         ctx->module->throw_icall = callee;
3687 #endif
3688         }
3689         arg = convert (ctx, exc, type_to_llvm_type (ctx, &mono_get_object_class ()->byval_arg));
3690         emit_call (ctx, bb, &ctx->builder, callee, &arg, 1);
3691 }
3692
3693 static void
3694 emit_resume_eh (EmitContext *ctx, MonoBasicBlock *bb)
3695 {
3696         const char *icall_name = "mono_llvm_resume_exception";
3697         LLVMValueRef callee = ctx->module->resume_eh;
3698
3699         LLVMTypeRef fun_sig = LLVMFunctionType0 (LLVMVoidType (), FALSE);
3700
3701         if (!callee) {
3702                 if (ctx->cfg->compile_aot) {
3703                         callee = get_callee (ctx, fun_sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
3704                 } else {
3705                         callee = LLVMAddFunction (ctx->lmodule, icall_name, fun_sig);
3706                         LLVMAddGlobalMapping (ctx->module->ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
3707                         mono_memory_barrier ();
3708
3709                         ctx->module->resume_eh = callee;
3710                 }
3711         }
3712
3713         emit_call (ctx, bb, &ctx->builder, callee, NULL, 0);
3714
3715         LLVMBuildUnreachable (ctx->builder);
3716
3717         ctx->builder = create_builder (ctx);
3718 }
3719
3720 static LLVMValueRef
3721 mono_llvm_emit_clear_exception_call (EmitContext *ctx, LLVMBuilderRef builder)
3722 {
3723         const char *icall_name = "mono_llvm_clear_exception";
3724
3725         LLVMTypeRef call_sig = LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE);
3726         LLVMValueRef callee = NULL;
3727
3728         if (!callee) {
3729                 if (ctx->cfg->compile_aot) {
3730                         callee = get_callee (ctx, call_sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
3731                 } else {
3732                         // FIXME: This is broken.
3733                         callee = LLVMAddFunction (ctx->lmodule, icall_name, call_sig);
3734                 }
3735         }
3736
3737         g_assert (builder && callee);
3738
3739         return LLVMBuildCall (builder, callee, NULL, 0, "");
3740 }
3741
3742 static LLVMValueRef
3743 mono_llvm_emit_load_exception_call (EmitContext *ctx, LLVMBuilderRef builder)
3744 {
3745         const char *icall_name = "mono_llvm_load_exception";
3746
3747         LLVMTypeRef call_sig = LLVMFunctionType (ObjRefType (), NULL, 0, FALSE);
3748         LLVMValueRef callee = NULL;
3749
3750         if (!callee) {
3751                 if (ctx->cfg->compile_aot) {
3752                         callee = get_callee (ctx, call_sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
3753                 } else {
3754                         // FIXME: This is broken.
3755                         callee = LLVMAddFunction (ctx->lmodule, icall_name, call_sig);
3756                 }
3757         }
3758
3759         g_assert (builder && callee);
3760
3761         return LLVMBuildCall (builder, callee, NULL, 0, icall_name);
3762 }
3763
3764
3765 static LLVMValueRef
3766 mono_llvm_emit_match_exception_call (EmitContext *ctx, LLVMBuilderRef builder, gint32 region_start, gint32 region_end)
3767 {
3768         const char *icall_name = "mono_llvm_match_exception";
3769
3770         ctx->builder = builder;
3771
3772         const int num_args = 5;
3773         LLVMValueRef args [num_args];
3774         args [0] = convert (ctx, get_aotconst (ctx, MONO_PATCH_INFO_AOT_JIT_INFO, GINT_TO_POINTER (ctx->cfg->method_index)), IntPtrType ());
3775         args [1] = LLVMConstInt (LLVMInt32Type (), region_start, 0);
3776         args [2] = LLVMConstInt (LLVMInt32Type (), region_end, 0);
3777         if (ctx->cfg->rgctx_var) {
3778                 LLVMValueRef rgctx_alloc = ctx->addresses [ctx->cfg->rgctx_var->dreg];
3779                 g_assert (rgctx_alloc);
3780                 args [3] = LLVMBuildLoad (builder, convert (ctx, rgctx_alloc, LLVMPointerType (IntPtrType (), 0)), "");
3781         } else {
3782                 args [3] = LLVMConstInt (IntPtrType (), 0, 0);
3783         }
3784         if (ctx->this_arg)
3785                 args [4] = convert (ctx, ctx->this_arg, IntPtrType ());
3786         else
3787                 args [4] = LLVMConstInt (IntPtrType (), 0, 0);
3788
3789         LLVMTypeRef match_sig = LLVMFunctionType5 (LLVMInt32Type (), IntPtrType (), LLVMInt32Type (), LLVMInt32Type (), IntPtrType (), IntPtrType (), FALSE);
3790         LLVMValueRef callee = ctx->module->match_exc;
3791
3792         if (!callee) {
3793                 if (ctx->cfg->compile_aot) {
3794                         ctx->builder = builder;
3795                         // get_callee expects ctx->builder to be the emitting builder
3796                         callee = get_callee (ctx, match_sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
3797                 } else {
3798                         callee = ctx->module->match_exc = LLVMAddFunction (ctx->lmodule, icall_name, match_sig);
3799                         LLVMAddGlobalMapping (ctx->module->ee, ctx->module->match_exc, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
3800                         ctx->module->match_exc = callee;
3801                         mono_memory_barrier ();
3802                 }
3803         }
3804
3805         g_assert (builder && callee);
3806
3807         g_assert (ctx->ex_var);
3808
3809         return LLVMBuildCall (builder, callee, args, num_args, icall_name);
3810 }
3811
3812 // FIXME: This won't work because the code-finding makes this
3813 // not a constant.
3814 /*#define MONO_PERSONALITY_DEBUG*/
3815
3816 #ifdef MONO_PERSONALITY_DEBUG
3817 static const gboolean use_debug_personality = TRUE;
3818 static const char *default_personality_name = "mono_debug_personality";
3819 #else
3820 static const gboolean use_debug_personality = FALSE;
3821 static const char *default_personality_name = "__gxx_personality_v0";
3822 #endif
3823
3824 static LLVMTypeRef
3825 default_cpp_lpad_exc_signature (void)
3826 {
3827         static gboolean inited = FALSE;
3828         static LLVMTypeRef sig;
3829
3830         if (!sig) {
3831                 LLVMTypeRef signature [2];
3832                 signature [0] = LLVMPointerType (LLVMInt8Type (), 0);
3833                 signature [1] = LLVMInt32Type ();
3834                 sig = LLVMStructType (signature, 2, FALSE);
3835                 inited = TRUE;
3836         }
3837
3838         return sig;
3839 }
3840
3841 static LLVMValueRef
3842 get_mono_personality (EmitContext *ctx)
3843 {
3844         LLVMValueRef personality = NULL;
3845         static gint32 mapping_inited = FALSE;
3846         LLVMTypeRef personality_type = LLVMFunctionType (LLVMInt32Type (), NULL, 0, TRUE);
3847
3848         if (!use_debug_personality) {
3849                 if (ctx->cfg->compile_aot) {
3850                                 personality = get_intrinsic (ctx, default_personality_name);
3851                 } else if (InterlockedCompareExchange (&mapping_inited, 1, 0) == 0) {
3852                                 personality = LLVMAddFunction (ctx->lmodule, default_personality_name, personality_type);
3853                                 LLVMAddGlobalMapping (ctx->module->ee, personality, personality);
3854                 }
3855         } else {
3856                 if (ctx->cfg->compile_aot) {
3857                         personality = get_callee (ctx, personality_type, MONO_PATCH_INFO_INTERNAL_METHOD, default_personality_name);
3858                 } else {
3859                         personality = LLVMAddFunction (ctx->lmodule, default_personality_name, personality_type);
3860                         LLVMAddGlobalMapping (ctx->module->ee, personality, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, default_personality_name));
3861                         mono_memory_barrier ();
3862                 }
3863         }
3864
3865         g_assert (personality);
3866         return personality;
3867 }
3868
3869 static LLVMBasicBlockRef
3870 emit_landing_pad (EmitContext *ctx, int group_index, int group_size)
3871 {
3872         MonoCompile *cfg = ctx->cfg;
3873         LLVMBuilderRef old_builder = ctx->builder;
3874         MonoExceptionClause *group_start = cfg->header->clauses + group_index;
3875
3876         LLVMBuilderRef lpadBuilder = create_builder (ctx);
3877         ctx->builder = lpadBuilder;
3878
3879         MonoBasicBlock *handler_bb = cfg->cil_offset_to_bb [CLAUSE_START (group_start)];
3880         g_assert (handler_bb);
3881
3882         // <resultval> = landingpad <somety> personality <type> <pers_fn> <clause>+
3883         LLVMValueRef personality = get_mono_personality (ctx);
3884         g_assert (personality);
3885
3886         char *bb_name = g_strdup_printf ("LPAD%d_BB", group_index);
3887         LLVMBasicBlockRef lpad_bb = gen_bb (ctx, bb_name);
3888         g_free (bb_name);
3889         LLVMPositionBuilderAtEnd (lpadBuilder, lpad_bb);
3890         LLVMValueRef landing_pad = LLVMBuildLandingPad (lpadBuilder, default_cpp_lpad_exc_signature (), personality, 0, "");
3891         g_assert (landing_pad);
3892
3893         LLVMValueRef cast = LLVMBuildBitCast (lpadBuilder, ctx->module->sentinel_exception, LLVMPointerType (LLVMInt8Type (), 0), "int8TypeInfo");
3894         LLVMAddClause (landing_pad, cast);
3895
3896         LLVMBasicBlockRef resume_bb = gen_bb (ctx, "RESUME_BB");
3897         LLVMBuilderRef resume_builder = create_builder (ctx);
3898         ctx->builder = resume_builder;
3899         LLVMPositionBuilderAtEnd (resume_builder, resume_bb);
3900
3901         emit_resume_eh (ctx, handler_bb);
3902
3903         // Build match
3904         ctx->builder = lpadBuilder;
3905         LLVMPositionBuilderAtEnd (lpadBuilder, lpad_bb);
3906
3907         gboolean finally_only = TRUE;
3908
3909         MonoExceptionClause *group_cursor = group_start;
3910
3911         for (int i = 0; i < group_size; i ++) {
3912                 if (!(group_cursor->flags & MONO_EXCEPTION_CLAUSE_FINALLY))
3913                         finally_only = FALSE;
3914
3915                 group_cursor++;
3916         }
3917
3918         // FIXME:
3919         // Handle landing pad inlining
3920
3921         if (!finally_only) {
3922                 // So at each level of the exception stack we will match the exception again.
3923                 // During that match, we need to compare against the handler types for the current
3924                 // protected region. We send the try start and end so that we can only check against
3925                 // handlers for this lexical protected region.
3926                 LLVMValueRef match = mono_llvm_emit_match_exception_call (ctx, lpadBuilder, group_start->try_offset, group_start->try_offset + group_start->try_len);
3927
3928                 // if returns -1, resume
3929                 LLVMValueRef switch_ins = LLVMBuildSwitch (lpadBuilder, match, resume_bb, group_size);
3930
3931                 // else move to that target bb
3932                 for (int i = 0; i < group_size; i++) {
3933                         MonoExceptionClause *clause = group_start + i;
3934                         int clause_index = clause - cfg->header->clauses;
3935                         MonoBasicBlock *handler_bb = (MonoBasicBlock*)g_hash_table_lookup (ctx->clause_to_handler, GINT_TO_POINTER (clause_index));
3936                         g_assert (handler_bb);
3937                         g_assert (ctx->bblocks [handler_bb->block_num].call_handler_target_bb);
3938                         LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), clause_index, FALSE), ctx->bblocks [handler_bb->block_num].call_handler_target_bb);
3939                 }
3940         } else {
3941                 int clause_index = group_start - cfg->header->clauses;
3942                 MonoBasicBlock *finally_bb = (MonoBasicBlock*)g_hash_table_lookup (ctx->clause_to_handler, GINT_TO_POINTER (clause_index));
3943                 g_assert (finally_bb);
3944
3945                 LLVMBuildBr (ctx->builder, ctx->bblocks [finally_bb->block_num].call_handler_target_bb);
3946         }
3947
3948         ctx->builder = old_builder;
3949
3950         return lpad_bb;
3951 }
3952
3953
3954 static void
3955 emit_llvmonly_handler_start (EmitContext *ctx, MonoBasicBlock *bb, LLVMBasicBlockRef cbb)
3956 {
3957         int clause_index = MONO_REGION_CLAUSE_INDEX (bb->region);
3958         MonoExceptionClause *clause = &ctx->cfg->header->clauses [clause_index];
3959
3960         // Make exception available to catch blocks
3961         if (!(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
3962                 LLVMValueRef mono_exc = mono_llvm_emit_load_exception_call (ctx, ctx->builder);
3963
3964                 g_assert (ctx->ex_var);
3965                 LLVMBuildStore (ctx->builder, LLVMBuildBitCast (ctx->builder, mono_exc, ObjRefType (), ""), ctx->ex_var);
3966
3967                 if (bb->in_scount == 1) {
3968                         MonoInst *exvar = bb->in_stack [0];
3969                         g_assert (!ctx->values [exvar->dreg]);
3970                         g_assert (ctx->ex_var);
3971                         ctx->values [exvar->dreg] = LLVMBuildLoad (ctx->builder, ctx->ex_var, "save_exception");
3972                         emit_volatile_store (ctx, exvar->dreg);
3973                 }
3974
3975                 mono_llvm_emit_clear_exception_call (ctx, ctx->builder);
3976         }
3977
3978         LLVMBuilderRef handler_builder = create_builder (ctx);
3979         LLVMBasicBlockRef target_bb = ctx->bblocks [bb->block_num].call_handler_target_bb;
3980         LLVMPositionBuilderAtEnd (handler_builder, target_bb);
3981
3982         // Make the handler code end with a jump to cbb
3983         LLVMBuildBr (handler_builder, cbb);
3984 }
3985
3986 static void
3987 emit_handler_start (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef builder)
3988 {
3989         MonoCompile *cfg = ctx->cfg;
3990         LLVMValueRef *values = ctx->values;
3991         LLVMModuleRef lmodule = ctx->lmodule;
3992         BBInfo *bblocks = ctx->bblocks;
3993         LLVMTypeRef i8ptr;
3994         LLVMValueRef personality;
3995         LLVMValueRef landing_pad;
3996         LLVMBasicBlockRef target_bb;
3997         MonoInst *exvar;
3998         static int ti_generator;
3999         char ti_name [128];
4000         LLVMValueRef type_info;
4001         int clause_index;
4002         GSList *l;
4003
4004         // <resultval> = landingpad <somety> personality <type> <pers_fn> <clause>+
4005
4006         if (cfg->compile_aot) {
4007                 /* Use a dummy personality function */
4008                 personality = LLVMGetNamedFunction (lmodule, "mono_personality");
4009                 g_assert (personality);
4010         } else {
4011 #if LLVM_API_VERSION > 100
4012                 personality = ctx->module->personality;
4013                 if (!personality) {
4014                         LLVMTypeRef personality_type = LLVMFunctionType (LLVMInt32Type (), NULL, 0, TRUE);
4015                         personality = LLVMAddFunction (ctx->lmodule, "mono_personality", personality_type);
4016                         LLVMAddFunctionAttr (personality, LLVMNoUnwindAttribute);
4017                         LLVMBasicBlockRef entry_bb = LLVMAppendBasicBlock (personality, "ENTRY");
4018                         LLVMBuilderRef builder2 = LLVMCreateBuilder ();
4019                         LLVMPositionBuilderAtEnd (builder2, entry_bb);
4020                         LLVMBuildRet (builder2, LLVMConstInt (LLVMInt32Type (), 0, FALSE));
4021                         ctx->module->personality = personality;
4022                         LLVMDisposeBuilder (builder2);
4023                 }
4024 #else
4025                 static gint32 mapping_inited;
4026
4027                 personality = LLVMGetNamedFunction (lmodule, "mono_personality");
4028
4029                 if (InterlockedCompareExchange (&mapping_inited, 1, 0) == 0)
4030                         LLVMAddGlobalMapping (ctx->module->ee, personality, (gpointer)mono_personality);
4031 #endif
4032         }
4033
4034         i8ptr = LLVMPointerType (LLVMInt8Type (), 0);
4035
4036         clause_index = (mono_get_block_region_notry (cfg, bb->region) >> 8) - 1;
4037
4038         /*
4039          * Create the type info
4040          */
4041         sprintf (ti_name, "type_info_%d", ti_generator);
4042         ti_generator ++;
4043
4044         if (cfg->compile_aot) {
4045                 /* decode_eh_frame () in aot-runtime.c will decode this */
4046                 type_info = LLVMAddGlobal (lmodule, LLVMInt32Type (), ti_name);
4047                 LLVMSetInitializer (type_info, LLVMConstInt (LLVMInt32Type (), clause_index, FALSE));
4048
4049                 /*
4050                  * These symbols are not really used, the clause_index is embedded into the EH tables generated by DwarfMonoException in LLVM.
4051                  */
4052                 LLVMSetLinkage (type_info, LLVMInternalLinkage);
4053         } else {
4054 #if LLVM_API_VERSION > 100
4055                 type_info = LLVMAddGlobal (lmodule, LLVMInt32Type (), ti_name);
4056                 LLVMSetInitializer (type_info, LLVMConstInt (LLVMInt32Type (), clause_index, FALSE));
4057 #else
4058                 gint32 *ti;
4059
4060                 /*
4061                  * After the cfg mempool is freed, the type info will point to stale memory,
4062                  * but this is not a problem, since we decode it once in exception_cb during
4063                  * compilation.
4064                  */
4065                 ti = (gint32*)mono_mempool_alloc (cfg->mempool, sizeof (gint32));
4066                 *(gint32*)ti = clause_index;
4067
4068                 type_info = LLVMAddGlobal (lmodule, i8ptr, ti_name);
4069
4070                 LLVMAddGlobalMapping (ctx->module->ee, type_info, ti);
4071 #endif
4072         }
4073
4074         {
4075                 LLVMTypeRef members [2], ret_type;
4076
4077                 members [0] = i8ptr;
4078                 members [1] = LLVMInt32Type ();
4079                 ret_type = LLVMStructType (members, 2, FALSE);
4080
4081                 landing_pad = LLVMBuildLandingPad (builder, ret_type, personality, 1, "");
4082                 LLVMAddClause (landing_pad, type_info);
4083
4084                 /* Store the exception into the exvar */
4085                 if (ctx->ex_var)
4086                         LLVMBuildStore (builder, convert (ctx, LLVMBuildExtractValue (builder, landing_pad, 0, "ex_obj"), ObjRefType ()), ctx->ex_var);
4087         }
4088
4089         /*
4090          * LLVM throw sites are associated with a one landing pad, and LLVM generated
4091          * code expects control to be transferred to this landing pad even in the
4092          * presence of nested clauses. The landing pad needs to branch to the landing
4093          * pads belonging to nested clauses based on the selector value returned by
4094          * the landing pad instruction, which is passed to the landing pad in a
4095          * register by the EH code.
4096          */
4097         target_bb = bblocks [bb->block_num].call_handler_target_bb;
4098         g_assert (target_bb);
4099
4100         /*
4101          * Branch to the correct landing pad
4102          */
4103         LLVMValueRef ex_selector = LLVMBuildExtractValue (builder, landing_pad, 1, "ex_selector");
4104         LLVMValueRef switch_ins = LLVMBuildSwitch (builder, ex_selector, target_bb, 0);
4105
4106         for (l = ctx->nested_in [clause_index]; l; l = l->next) {
4107                 int nesting_clause_index = GPOINTER_TO_INT (l->data);
4108                 MonoBasicBlock *handler_bb;
4109
4110                 handler_bb = (MonoBasicBlock*)g_hash_table_lookup (ctx->clause_to_handler, GINT_TO_POINTER (nesting_clause_index));
4111                 g_assert (handler_bb);
4112
4113                 g_assert (ctx->bblocks [handler_bb->block_num].call_handler_target_bb);
4114                 LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), nesting_clause_index, FALSE), ctx->bblocks [handler_bb->block_num].call_handler_target_bb);
4115         }
4116
4117         /* Start a new bblock which CALL_HANDLER can branch to */
4118         target_bb = bblocks [bb->block_num].call_handler_target_bb;
4119         if (target_bb) {
4120                 ctx->builder = builder = create_builder (ctx);
4121                 LLVMPositionBuilderAtEnd (ctx->builder, target_bb);
4122
4123                 ctx->bblocks [bb->block_num].end_bblock = target_bb;
4124
4125                 /* Store the exception into the IL level exvar */
4126                 if (bb->in_scount == 1) {
4127                         g_assert (bb->in_scount == 1);
4128                         exvar = bb->in_stack [0];
4129
4130                         // FIXME: This is shared with filter clauses ?
4131                         g_assert (!values [exvar->dreg]);
4132
4133                         g_assert (ctx->ex_var);
4134                         values [exvar->dreg] = LLVMBuildLoad (builder, ctx->ex_var, "");
4135                         emit_volatile_store (ctx, exvar->dreg);
4136                 }
4137         }
4138 }
4139
4140 static void
4141 process_bb (EmitContext *ctx, MonoBasicBlock *bb)
4142 {
4143         MonoCompile *cfg = ctx->cfg;
4144         MonoMethodSignature *sig = ctx->sig;
4145         LLVMValueRef method = ctx->lmethod;
4146         LLVMValueRef *values = ctx->values;
4147         LLVMValueRef *addresses = ctx->addresses;
4148         LLVMCallInfo *linfo = ctx->linfo;
4149         BBInfo *bblocks = ctx->bblocks;
4150         MonoInst *ins;
4151         LLVMBasicBlockRef cbb;
4152         LLVMBuilderRef builder, starting_builder;
4153         gboolean has_terminator;
4154         LLVMValueRef v;
4155         LLVMValueRef lhs, rhs;
4156         int nins = 0;
4157
4158         cbb = get_end_bb (ctx, bb);
4159
4160         builder = create_builder (ctx);
4161         ctx->builder = builder;
4162         LLVMPositionBuilderAtEnd (builder, cbb);
4163
4164         if (!ctx_ok (ctx))
4165                 return;
4166
4167         if (bb->flags & BB_EXCEPTION_HANDLER) {
4168                 if (!ctx->llvm_only && !bblocks [bb->block_num].invoke_target) {
4169                         set_failure (ctx, "handler without invokes");
4170                         return;
4171                 }
4172
4173                 if (ctx->llvm_only)
4174                         emit_llvmonly_handler_start (ctx, bb, cbb);
4175                 else
4176                         emit_handler_start (ctx, bb, builder);
4177                 if (!ctx_ok (ctx))
4178                         return;
4179                 builder = ctx->builder;
4180         }
4181
4182         has_terminator = FALSE;
4183         starting_builder = builder;
4184         for (ins = bb->code; ins; ins = ins->next) {
4185                 const char *spec = LLVM_INS_INFO (ins->opcode);
4186                 char *dname = NULL;
4187                 char dname_buf [128];
4188
4189                 emit_dbg_loc (ctx, builder, ins->cil_code);
4190
4191                 nins ++;
4192                 if (nins > 1000) {
4193                         /*
4194                          * Some steps in llc are non-linear in the size of basic blocks, see #5714.
4195                          * Start a new bblock.
4196                          * Prevent the bblocks to be merged by doing a volatile load + cond branch
4197                          * from localloc-ed memory.
4198                          */
4199                         if (!cfg->llvm_only)
4200                                 ;//set_failure (ctx, "basic block too long");
4201
4202                         if (!ctx->long_bb_break_var) {
4203                                 ctx->long_bb_break_var = build_alloca_llvm_type_name (ctx, LLVMInt32Type (), 0, "long_bb_break");
4204                                 mono_llvm_build_store (ctx->alloca_builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), ctx->long_bb_break_var, TRUE, LLVM_BARRIER_NONE);
4205                         }
4206
4207                         cbb = gen_bb (ctx, "CONT_LONG_BB");
4208                         LLVMBasicBlockRef dummy_bb = gen_bb (ctx, "CONT_LONG_BB_DUMMY");
4209
4210                         LLVMValueRef load = mono_llvm_build_load (builder, ctx->long_bb_break_var, "", TRUE);
4211                         /*
4212                          * The long_bb_break_var is initialized to 0 in the prolog, so this branch will always go to 'cbb'
4213                          * but llvm doesn't know that, so the branch is not going to be eliminated.
4214                          */
4215                         LLVMValueRef cmp = LLVMBuildICmp (builder, LLVMIntEQ, load, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
4216
4217                         LLVMBuildCondBr (builder, cmp, cbb, dummy_bb);
4218
4219                         /* Emit a dummy false bblock which does nothing but contains a volatile store so it cannot be eliminated */
4220                         ctx->builder = builder = create_builder (ctx);
4221                         LLVMPositionBuilderAtEnd (builder, dummy_bb);
4222                         mono_llvm_build_store (builder, LLVMConstInt (LLVMInt32Type (), 1, FALSE), ctx->long_bb_break_var, TRUE, LLVM_BARRIER_NONE);
4223                         LLVMBuildBr (builder, cbb);
4224
4225                         ctx->builder = builder = create_builder (ctx);
4226                         LLVMPositionBuilderAtEnd (builder, cbb);
4227                         ctx->bblocks [bb->block_num].end_bblock = cbb;
4228                         nins = 0;
4229                 }
4230
4231                 if (has_terminator)
4232                         /* There could be instructions after a terminator, skip them */
4233                         break;
4234
4235                 if (spec [MONO_INST_DEST] != ' ' && !MONO_IS_STORE_MEMBASE (ins)) {
4236                         sprintf (dname_buf, "t%d", ins->dreg);
4237                         dname = dname_buf;
4238                 }
4239
4240                 if (spec [MONO_INST_SRC1] != ' ' && spec [MONO_INST_SRC1] != 'v') {
4241                         MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
4242
4243                         if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) && var->opcode != OP_GSHAREDVT_ARG_REGOFFSET) {
4244                                 lhs = emit_volatile_load (ctx, ins->sreg1);
4245                         } else {
4246                                 /* It is ok for SETRET to have an uninitialized argument */
4247                                 if (!values [ins->sreg1] && ins->opcode != OP_SETRET) {
4248                                         set_failure (ctx, "sreg1");
4249                                         return;
4250                                 }
4251                                 lhs = values [ins->sreg1];
4252                         }
4253                 } else {
4254                         lhs = NULL;
4255                 }
4256
4257                 if (spec [MONO_INST_SRC2] != ' ' && spec [MONO_INST_SRC2] != ' ') {
4258                         MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
4259                         if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
4260                                 rhs = emit_volatile_load (ctx, ins->sreg2);
4261                         } else {
4262                                 if (!values [ins->sreg2]) {
4263                                         set_failure (ctx, "sreg2");
4264                                         return;
4265                                 }
4266                                 rhs = values [ins->sreg2];
4267                         }
4268                 } else {
4269                         rhs = NULL;
4270                 }
4271
4272                 //mono_print_ins (ins);
4273                 switch (ins->opcode) {
4274                 case OP_NOP:
4275                 case OP_NOT_NULL:
4276                 case OP_LIVERANGE_START:
4277                 case OP_LIVERANGE_END:
4278                         break;
4279                 case OP_ICONST:
4280                         values [ins->dreg] = LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE);
4281                         break;
4282                 case OP_I8CONST:
4283 #if SIZEOF_VOID_P == 4
4284                         values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
4285 #else
4286                         values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), (gint64)ins->inst_c0, FALSE);
4287 #endif
4288                         break;
4289                 case OP_R8CONST:
4290                         values [ins->dreg] = LLVMConstReal (LLVMDoubleType (), *(double*)ins->inst_p0);
4291                         break;
4292                 case OP_R4CONST:
4293                         if (cfg->r4fp)
4294                                 values [ins->dreg] = LLVMConstReal (LLVMFloatType (), *(float*)ins->inst_p0);
4295                         else
4296                                 values [ins->dreg] = LLVMConstFPExt (LLVMConstReal (LLVMFloatType (), *(float*)ins->inst_p0), LLVMDoubleType ());
4297                         break;
4298                 case OP_DUMMY_ICONST:
4299                         values [ins->dreg] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
4300                         break;
4301                 case OP_DUMMY_I8CONST:
4302                         values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
4303                         break;
4304                 case OP_DUMMY_R8CONST:
4305                         values [ins->dreg] = LLVMConstReal (LLVMDoubleType (), 0.0f);
4306                         break;
4307                 case OP_BR: {
4308                         LLVMBasicBlockRef target_bb = get_bb (ctx, ins->inst_target_bb);
4309                         LLVMBuildBr (builder, target_bb);
4310                         has_terminator = TRUE;
4311                         break;
4312                 }
4313                 case OP_SWITCH: {
4314                         int i;
4315                         LLVMValueRef v;
4316                         char bb_name [128];
4317                         LLVMBasicBlockRef new_bb;
4318                         LLVMBuilderRef new_builder;
4319
4320                         // The default branch is already handled
4321                         // FIXME: Handle it here
4322
4323                         /* Start new bblock */
4324                         sprintf (bb_name, "SWITCH_DEFAULT_BB%d", ctx->default_index ++);
4325                         new_bb = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
4326
4327                         lhs = convert (ctx, lhs, LLVMInt32Type ());
4328                         v = LLVMBuildSwitch (builder, lhs, new_bb, GPOINTER_TO_UINT (ins->klass));
4329                         for (i = 0; i < GPOINTER_TO_UINT (ins->klass); ++i) {
4330                                 MonoBasicBlock *target_bb = ins->inst_many_bb [i];
4331
4332                                 LLVMAddCase (v, LLVMConstInt (LLVMInt32Type (), i, FALSE), get_bb (ctx, target_bb));
4333                         }
4334
4335                         new_builder = create_builder (ctx);
4336                         LLVMPositionBuilderAtEnd (new_builder, new_bb);
4337                         LLVMBuildUnreachable (new_builder);
4338
4339                         has_terminator = TRUE;
4340                         g_assert (!ins->next);
4341                                 
4342                         break;
4343                 }
4344
4345                 case OP_SETRET:
4346                         switch (linfo->ret.storage) {
4347                         case LLVMArgVtypeInReg: {
4348                                 LLVMTypeRef ret_type = LLVMGetReturnType (LLVMGetElementType (LLVMTypeOf (method)));
4349                                 LLVMValueRef val, addr, retval;
4350                                 int i;
4351
4352                                 retval = LLVMGetUndef (ret_type);
4353
4354                                 if (!addresses [ins->sreg1]) {
4355                                         /*
4356                                          * The return type is an LLVM vector type, have to convert between it and the
4357                                          * real return type which is a struct type.
4358                                          */
4359                                         g_assert (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (sig->ret)));
4360                                         /* Convert to 2xi64 first */
4361                                         val = LLVMBuildBitCast (builder, values [ins->sreg1], LLVMVectorType (IntPtrType (), 2), "");
4362
4363                                         for (i = 0; i < 2; ++i) {
4364                                                 if (linfo->ret.pair_storage [i] == LLVMArgInIReg) {
4365                                                         retval = LLVMBuildInsertValue (builder, retval, LLVMBuildExtractElement (builder, val, LLVMConstInt (LLVMInt32Type (), i, FALSE), ""), i, "");
4366                                                 } else {
4367                                                         g_assert (linfo->ret.pair_storage [i] == LLVMArgNone);
4368                                                 }
4369                                         }
4370                                 } else {
4371                                         addr = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (ret_type, 0), "");
4372                                         for (i = 0; i < 2; ++i) {
4373                                                 if (linfo->ret.pair_storage [i] == LLVMArgInIReg) {
4374                                                         LLVMValueRef indexes [2], part_addr;
4375
4376                                                         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
4377                                                         indexes [1] = LLVMConstInt (LLVMInt32Type (), i, FALSE);
4378                                                         part_addr = LLVMBuildGEP (builder, addr, indexes, 2, "");
4379
4380                                                         retval = LLVMBuildInsertValue (builder, retval, LLVMBuildLoad (builder, part_addr, ""), i, "");
4381                                                 } else {
4382                                                         g_assert (linfo->ret.pair_storage [i] == LLVMArgNone);
4383                                                 }
4384                                         }
4385                                 }
4386                                 LLVMBuildRet (builder, retval);
4387                                 break;
4388                         }
4389                         case LLVMArgVtypeAsScalar: {
4390                                 LLVMTypeRef ret_type = LLVMGetReturnType (LLVMGetElementType (LLVMTypeOf (method)));
4391                                 LLVMValueRef retval;
4392
4393                                 g_assert (addresses [ins->sreg1]);
4394
4395                                 retval = LLVMBuildLoad (builder, LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (ret_type, 0), ""), "");
4396                                 LLVMBuildRet (builder, retval);
4397                                 break;
4398                         }
4399                         case LLVMArgVtypeByVal: {
4400                                 LLVMValueRef retval;
4401
4402                                 g_assert (addresses [ins->sreg1]);
4403                                 retval = LLVMBuildLoad (builder, addresses [ins->sreg1], "");
4404                                 LLVMBuildRet (builder, retval);
4405                                 break;
4406                         }
4407                         case LLVMArgVtypeByRef: {
4408                                 LLVMBuildRetVoid (builder);
4409                                 break;
4410                         }
4411                         case LLVMArgGsharedvtFixed: {
4412                                 LLVMTypeRef ret_type = type_to_llvm_type (ctx, sig->ret);
4413                                 /* The return value is in lhs, need to store to the vret argument */
4414                                 /* sreg1 might not be set */
4415                                 if (lhs) {
4416                                         g_assert (cfg->vret_addr);
4417                                         g_assert (values [cfg->vret_addr->dreg]);
4418                                         LLVMBuildStore (builder, convert (ctx, lhs, ret_type), convert (ctx, values [cfg->vret_addr->dreg], LLVMPointerType (ret_type, 0)));
4419                                 }
4420                                 LLVMBuildRetVoid (builder);
4421                                 break;
4422                         }
4423                         case LLVMArgGsharedvtFixedVtype: {
4424                                 /* Already set */
4425                                 LLVMBuildRetVoid (builder);
4426                                 break;
4427                         }
4428                         case LLVMArgGsharedvtVariable: {
4429                                 /* Already set */
4430                                 LLVMBuildRetVoid (builder);
4431                                 break;
4432                         }
4433                         case LLVMArgVtypeRetAddr: {
4434                                 LLVMBuildRetVoid (builder);
4435                                 break;
4436                         }
4437                         case LLVMArgAsIArgs:
4438                         case LLVMArgFpStruct: {
4439                                 LLVMTypeRef ret_type = LLVMGetReturnType (LLVMGetElementType (LLVMTypeOf (method)));
4440                                 LLVMValueRef retval;
4441
4442                                 g_assert (addresses [ins->sreg1]);
4443                                 retval = LLVMBuildLoad (builder, convert (ctx, addresses [ins->sreg1], LLVMPointerType (ret_type, 0)), "");
4444                                 LLVMBuildRet (builder, retval);
4445                                 break;
4446                         }
4447                         case LLVMArgNone:
4448                         case LLVMArgNormal: {
4449                                 if (!lhs || ctx->is_dead [ins->sreg1]) {
4450                                         /*
4451                                          * The method did not set its return value, probably because it
4452                                          * ends with a throw.
4453                                          */
4454                                         if (cfg->vret_addr)
4455                                                 LLVMBuildRetVoid (builder);
4456                                         else
4457                                                 LLVMBuildRet (builder, LLVMConstNull (type_to_llvm_type (ctx, sig->ret)));
4458                                 } else {
4459                                         LLVMBuildRet (builder, convert (ctx, lhs, type_to_llvm_type (ctx, sig->ret)));
4460                                 }
4461                                 has_terminator = TRUE;
4462                                 break;
4463                         }
4464                         default:
4465                                 g_assert_not_reached ();
4466                                 break;
4467                         }
4468                         break;
4469                 case OP_ICOMPARE:
4470                 case OP_FCOMPARE:
4471                 case OP_RCOMPARE:
4472                 case OP_LCOMPARE:
4473                 case OP_COMPARE:
4474                 case OP_ICOMPARE_IMM:
4475                 case OP_LCOMPARE_IMM:
4476                 case OP_COMPARE_IMM: {
4477                         CompRelation rel;
4478                         LLVMValueRef cmp, args [16];
4479                         gboolean likely = (ins->flags & MONO_INST_LIKELY) != 0;
4480
4481                         if (ins->next->opcode == OP_NOP)
4482                                 break;
4483
4484                         if (ins->next->opcode == OP_BR)
4485                                 /* The comparison result is not needed */
4486                                 continue;
4487
4488                         rel = mono_opcode_to_cond (ins->next->opcode);
4489
4490                         if (ins->opcode == OP_ICOMPARE_IMM) {
4491                                 lhs = convert (ctx, lhs, LLVMInt32Type ());
4492                                 rhs = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
4493                         }
4494                         if (ins->opcode == OP_LCOMPARE_IMM) {
4495                                 lhs = convert (ctx, lhs, LLVMInt64Type ());
4496                                 rhs = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
4497                         }
4498                         if (ins->opcode == OP_LCOMPARE) {
4499                                 lhs = convert (ctx, lhs, LLVMInt64Type ());
4500                                 rhs = convert (ctx, rhs, LLVMInt64Type ());
4501                         }
4502                         if (ins->opcode == OP_ICOMPARE) {
4503                                 lhs = convert (ctx, lhs, LLVMInt32Type ());
4504                                 rhs = convert (ctx, rhs, LLVMInt32Type ());
4505                         }
4506
4507                         if (lhs && rhs) {
4508                                 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
4509                                         rhs = convert (ctx, rhs, LLVMTypeOf (lhs));
4510                                 else if (LLVMGetTypeKind (LLVMTypeOf (rhs)) == LLVMPointerTypeKind)
4511                                         lhs = convert (ctx, lhs, LLVMTypeOf (rhs));
4512                         }
4513
4514                         /* We use COMPARE+SETcc/Bcc, llvm uses SETcc+br cond */
4515                         if (ins->opcode == OP_FCOMPARE) {
4516                                 cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMDoubleType ()), convert (ctx, rhs, LLVMDoubleType ()), "");
4517                         } else if (ins->opcode == OP_RCOMPARE) {
4518                                 cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMFloatType ()), convert (ctx, rhs, LLVMFloatType ()), "");
4519                         } else if (ins->opcode == OP_COMPARE_IMM) {
4520                                 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind && ins->inst_imm == 0)
4521                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], lhs, LLVMConstNull (LLVMTypeOf (lhs)), "");
4522                                 else
4523                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), "");
4524                         } else if (ins->opcode == OP_LCOMPARE_IMM) {
4525                                 if (SIZEOF_REGISTER == 4 && COMPILE_LLVM (cfg))  {
4526                                         /* The immediate is encoded in two fields */
4527                                         guint64 l = ((guint64)(guint32)ins->inst_offset << 32) | ((guint32)ins->inst_imm);
4528                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, LLVMInt64Type ()), LLVMConstInt (LLVMInt64Type (), l, FALSE), "");
4529                                 } else {
4530                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, LLVMInt64Type ()), LLVMConstInt (LLVMInt64Type (), ins->inst_imm, FALSE), "");
4531                                 }
4532                         }
4533                         else if (ins->opcode == OP_COMPARE) {
4534                                 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind && LLVMTypeOf (lhs) == LLVMTypeOf (rhs))
4535                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], lhs, rhs, "");
4536                                 else
4537                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, IntPtrType ()), convert (ctx, rhs, IntPtrType ()), "");
4538                         } else
4539                                 cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], lhs, rhs, "");
4540
4541                         if (likely) {
4542                                 args [0] = cmp;
4543                                 args [1] = LLVMConstInt (LLVMInt1Type (), 1, FALSE);
4544                                 cmp = LLVMBuildCall (ctx->builder, get_intrinsic (ctx, "llvm.expect.i1"), args, 2, "");
4545                         }
4546
4547                         if (MONO_IS_COND_BRANCH_OP (ins->next)) {
4548                                 if (ins->next->inst_true_bb == ins->next->inst_false_bb) {
4549                                         /*
4550                                          * If the target bb contains PHI instructions, LLVM requires
4551                                          * two PHI entries for this bblock, while we only generate one.
4552                                          * So convert this to an unconditional bblock. (bxc #171).
4553                                          */
4554                                         LLVMBuildBr (builder, get_bb (ctx, ins->next->inst_true_bb));
4555                                 } else {
4556                                         LLVMBuildCondBr (builder, cmp, get_bb (ctx, ins->next->inst_true_bb), get_bb (ctx, ins->next->inst_false_bb));
4557                                 }
4558                                 has_terminator = TRUE;
4559                         } else if (MONO_IS_SETCC (ins->next)) {
4560                                 sprintf (dname_buf, "t%d", ins->next->dreg);
4561                                 dname = dname_buf;
4562                                 values [ins->next->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
4563
4564                                 /* Add stores for volatile variables */
4565                                 emit_volatile_store (ctx, ins->next->dreg);
4566                         } else if (MONO_IS_COND_EXC (ins->next)) {
4567                                 emit_cond_system_exception (ctx, bb, (const char*)ins->next->inst_p1, cmp);
4568                                 if (!ctx_ok (ctx))
4569                                         break;
4570                                 builder = ctx->builder;
4571                         } else {
4572                                 set_failure (ctx, "next");
4573                                 break;
4574                         }
4575
4576                         ins = ins->next;
4577                         break;
4578                 }
4579                 case OP_FCEQ:
4580                 case OP_FCNEQ:
4581                 case OP_FCLT:
4582                 case OP_FCLT_UN:
4583                 case OP_FCGT:
4584                 case OP_FCGT_UN:
4585                 case OP_FCGE:
4586                 case OP_FCLE: {
4587                         CompRelation rel;
4588                         LLVMValueRef cmp;
4589
4590                         rel = mono_opcode_to_cond (ins->opcode);
4591
4592                         cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMDoubleType ()), convert (ctx, rhs, LLVMDoubleType ()), "");
4593                         values [ins->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
4594                         break;
4595                 }
4596                 case OP_RCEQ:
4597                 case OP_RCLT:
4598                 case OP_RCLT_UN:
4599                 case OP_RCGT:
4600                 case OP_RCGT_UN: {
4601                         CompRelation rel;
4602                         LLVMValueRef cmp;
4603
4604                         rel = mono_opcode_to_cond (ins->opcode);
4605
4606                         cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMFloatType ()), convert (ctx, rhs, LLVMFloatType ()), "");
4607                         values [ins->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
4608                         break;
4609                 }
4610                 case OP_PHI:
4611                 case OP_FPHI:
4612                 case OP_VPHI:
4613                 case OP_XPHI: {
4614                         int i;
4615                         gboolean empty = TRUE;
4616
4617                         /* Check that all input bblocks really branch to us */
4618                         for (i = 0; i < bb->in_count; ++i) {
4619                                 if (bb->in_bb [i]->last_ins && bb->in_bb [i]->last_ins->opcode == OP_NOT_REACHED)
4620                                         ins->inst_phi_args [i + 1] = -1;
4621                                 else
4622                                         empty = FALSE;
4623                         }
4624
4625                         if (empty) {
4626                                 /* LLVM doesn't like phi instructions with zero operands */
4627                                 ctx->is_dead [ins->dreg] = TRUE;
4628                                 break;
4629                         }                                       
4630
4631                         /* Created earlier, insert it now */
4632                         LLVMInsertIntoBuilder (builder, values [ins->dreg]);
4633
4634                         for (i = 0; i < ins->inst_phi_args [0]; i++) {
4635                                 int sreg1 = ins->inst_phi_args [i + 1];
4636                                 int count, j;
4637
4638                                 /* 
4639                                  * Count the number of times the incoming bblock branches to us,
4640                                  * since llvm requires a separate entry for each.
4641                                  */
4642                                 if (bb->in_bb [i]->last_ins && bb->in_bb [i]->last_ins->opcode == OP_SWITCH) {
4643                                         MonoInst *switch_ins = bb->in_bb [i]->last_ins;
4644
4645                                         count = 0;
4646                                         for (j = 0; j < GPOINTER_TO_UINT (switch_ins->klass); ++j) {
4647                                                 if (switch_ins->inst_many_bb [j] == bb)
4648                                                         count ++;
4649                                         }
4650                                 } else {
4651                                         count = 1;
4652                                 }
4653
4654                                 /* Remember for later */
4655                                 for (j = 0; j < count; ++j) {
4656                                         PhiNode *node = (PhiNode*)mono_mempool_alloc0 (ctx->mempool, sizeof (PhiNode));
4657                                         node->bb = bb;
4658                                         node->phi = ins;
4659                                         node->in_bb = bb->in_bb [i];
4660                                         node->sreg = sreg1;
4661                                         bblocks [bb->in_bb [i]->block_num].phi_nodes = g_slist_prepend_mempool (ctx->mempool, bblocks [bb->in_bb [i]->block_num].phi_nodes, node);
4662                                 }
4663                         }
4664                         break;
4665                 }
4666                 case OP_MOVE:
4667                 case OP_LMOVE:
4668                 case OP_XMOVE:
4669                 case OP_SETFRET:
4670                         g_assert (lhs);
4671                         values [ins->dreg] = lhs;
4672                         break;
4673                 case OP_FMOVE:
4674                 case OP_RMOVE: {
4675                         MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
4676                                 
4677                         g_assert (lhs);
4678                         values [ins->dreg] = lhs;
4679
4680                         if (var && var->klass->byval_arg.type == MONO_TYPE_R4) {
4681                                 /* 
4682                                  * This is added by the spilling pass in case of the JIT,
4683                                  * but we have to do it ourselves.
4684                                  */
4685                                 values [ins->dreg] = convert (ctx, values [ins->dreg], LLVMFloatType ());
4686                         }
4687                         break;
4688                 }
4689                 case OP_MOVE_F_TO_I4: {
4690                         values [ins->dreg] = LLVMBuildBitCast (builder, LLVMBuildFPTrunc (builder, lhs, LLVMFloatType (), ""), LLVMInt32Type (), "");
4691                         break;
4692                 }
4693                 case OP_MOVE_I4_TO_F: {
4694                         values [ins->dreg] = LLVMBuildFPExt (builder, LLVMBuildBitCast (builder, lhs, LLVMFloatType (), ""), LLVMDoubleType (), "");
4695                         break;
4696                 }
4697                 case OP_MOVE_F_TO_I8: {
4698                         values [ins->dreg] = LLVMBuildBitCast (builder, lhs, LLVMInt64Type (), "");
4699                         break;
4700                 }
4701                 case OP_MOVE_I8_TO_F: {
4702                         values [ins->dreg] = LLVMBuildBitCast (builder, lhs, LLVMDoubleType (), "");
4703                         break;
4704                 }
4705                 case OP_IADD:
4706                 case OP_ISUB:
4707                 case OP_IAND:
4708                 case OP_IMUL:
4709                 case OP_IDIV:
4710                 case OP_IDIV_UN:
4711                 case OP_IREM:
4712                 case OP_IREM_UN:
4713                 case OP_IOR:
4714                 case OP_IXOR:
4715                 case OP_ISHL:
4716                 case OP_ISHR:
4717                 case OP_ISHR_UN:
4718                 case OP_FADD:
4719                 case OP_FSUB:
4720                 case OP_FMUL:
4721                 case OP_FDIV:
4722                 case OP_LADD:
4723                 case OP_LSUB:
4724                 case OP_LMUL:
4725                 case OP_LDIV:
4726                 case OP_LDIV_UN:
4727                 case OP_LREM:
4728                 case OP_LREM_UN:
4729                 case OP_LAND:
4730                 case OP_LOR:
4731                 case OP_LXOR:
4732                 case OP_LSHL:
4733                 case OP_LSHR:
4734                 case OP_LSHR_UN:
4735                         lhs = convert (ctx, lhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
4736                         rhs = convert (ctx, rhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
4737
4738                         emit_div_check (ctx, builder, bb, ins, lhs, rhs);
4739                         if (!ctx_ok (ctx))
4740                                 break;
4741                         builder = ctx->builder;
4742
4743                         switch (ins->opcode) {
4744                         case OP_IADD:
4745                         case OP_LADD:
4746                                 values [ins->dreg] = LLVMBuildAdd (builder, lhs, rhs, dname);
4747                                 break;
4748                         case OP_ISUB:
4749                         case OP_LSUB:
4750                                 values [ins->dreg] = LLVMBuildSub (builder, lhs, rhs, dname);
4751                                 break;
4752                         case OP_IMUL:
4753                         case OP_LMUL:
4754                                 values [ins->dreg] = LLVMBuildMul (builder, lhs, rhs, dname);
4755                                 break;
4756                         case OP_IREM:
4757                         case OP_LREM:
4758                                 values [ins->dreg] = LLVMBuildSRem (builder, lhs, rhs, dname);
4759                                 break;
4760                         case OP_IREM_UN:
4761                         case OP_LREM_UN:
4762                                 values [ins->dreg] = LLVMBuildURem (builder, lhs, rhs, dname);
4763                                 break;
4764                         case OP_IDIV:
4765                         case OP_LDIV:
4766                                 values [ins->dreg] = LLVMBuildSDiv (builder, lhs, rhs, dname);
4767                                 break;
4768                         case OP_IDIV_UN:
4769                         case OP_LDIV_UN:
4770                                 values [ins->dreg] = LLVMBuildUDiv (builder, lhs, rhs, dname);
4771                                 break;
4772                         case OP_FDIV:
4773                         case OP_RDIV:
4774                                 values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, dname);
4775                                 break;
4776                         case OP_IAND:
4777                         case OP_LAND:
4778                                 values [ins->dreg] = LLVMBuildAnd (builder, lhs, rhs, dname);
4779                                 break;
4780                         case OP_IOR:
4781                         case OP_LOR:
4782                                 values [ins->dreg] = LLVMBuildOr (builder, lhs, rhs, dname);
4783                                 break;
4784                         case OP_IXOR:
4785                         case OP_LXOR:
4786                                 values [ins->dreg] = LLVMBuildXor (builder, lhs, rhs, dname);
4787                                 break;
4788                         case OP_ISHL:
4789                         case OP_LSHL:
4790                                 values [ins->dreg] = LLVMBuildShl (builder, lhs, rhs, dname);
4791                                 break;
4792                         case OP_ISHR:
4793                         case OP_LSHR:
4794                                 values [ins->dreg] = LLVMBuildAShr (builder, lhs, rhs, dname);
4795                                 break;
4796                         case OP_ISHR_UN:
4797                         case OP_LSHR_UN:
4798                                 values [ins->dreg] = LLVMBuildLShr (builder, lhs, rhs, dname);
4799                                 break;
4800
4801                         case OP_FADD:
4802                                 values [ins->dreg] = LLVMBuildFAdd (builder, lhs, rhs, dname);
4803                                 break;
4804                         case OP_FSUB:
4805                                 values [ins->dreg] = LLVMBuildFSub (builder, lhs, rhs, dname);
4806                                 break;
4807                         case OP_FMUL:
4808                                 values [ins->dreg] = LLVMBuildFMul (builder, lhs, rhs, dname);
4809                                 break;
4810
4811                         default:
4812                                 g_assert_not_reached ();
4813                         }
4814                         break;
4815                 case OP_RADD:
4816                 case OP_RSUB:
4817                 case OP_RMUL:
4818                 case OP_RDIV: {
4819                         lhs = convert (ctx, lhs, LLVMFloatType ());
4820                         rhs = convert (ctx, rhs, LLVMFloatType ());
4821                         switch (ins->opcode) {
4822                         case OP_RADD:
4823                                 values [ins->dreg] = LLVMBuildFAdd (builder, lhs, rhs, dname);
4824                                 break;
4825                         case OP_RSUB:
4826                                 values [ins->dreg] = LLVMBuildFSub (builder, lhs, rhs, dname);
4827                                 break;
4828                         case OP_RMUL:
4829                                 values [ins->dreg] = LLVMBuildFMul (builder, lhs, rhs, dname);
4830                                 break;
4831                         case OP_RDIV:
4832                                 values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, dname);
4833                                 break;
4834                         default:
4835                                 g_assert_not_reached ();
4836                                 break;
4837                         }
4838                         break;
4839                 }
4840                 case OP_IADD_IMM:
4841                 case OP_ISUB_IMM:
4842                 case OP_IMUL_IMM:
4843                 case OP_IREM_IMM:
4844                 case OP_IREM_UN_IMM:
4845                 case OP_IDIV_IMM:
4846                 case OP_IDIV_UN_IMM:
4847                 case OP_IAND_IMM:
4848                 case OP_IOR_IMM:
4849                 case OP_IXOR_IMM:
4850                 case OP_ISHL_IMM:
4851                 case OP_ISHR_IMM:
4852                 case OP_ISHR_UN_IMM:
4853                 case OP_LADD_IMM:
4854                 case OP_LSUB_IMM:
4855                 case OP_LMUL_IMM:
4856                 case OP_LREM_IMM:
4857                 case OP_LAND_IMM:
4858                 case OP_LOR_IMM:
4859                 case OP_LXOR_IMM:
4860                 case OP_LSHL_IMM:
4861                 case OP_LSHR_IMM:
4862                 case OP_LSHR_UN_IMM:
4863                 case OP_ADD_IMM:
4864                 case OP_AND_IMM:
4865                 case OP_MUL_IMM:
4866                 case OP_SHL_IMM:
4867                 case OP_SHR_IMM:
4868                 case OP_SHR_UN_IMM: {
4869                         LLVMValueRef imm;
4870
4871                         if (spec [MONO_INST_SRC1] == 'l') {
4872                                 imm = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
4873                         } else {
4874                                 imm = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
4875                         }
4876
4877                         emit_div_check (ctx, builder, bb, ins, lhs, imm);
4878                         if (!ctx_ok (ctx))
4879                                 break;
4880                         builder = ctx->builder;
4881
4882 #if SIZEOF_VOID_P == 4
4883                         if (ins->opcode == OP_LSHL_IMM || ins->opcode == OP_LSHR_IMM || ins->opcode == OP_LSHR_UN_IMM)
4884                                 imm = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
4885 #endif
4886
4887                         if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
4888                                 lhs = convert (ctx, lhs, IntPtrType ());
4889                         imm = convert (ctx, imm, LLVMTypeOf (lhs));
4890                         switch (ins->opcode) {
4891                         case OP_IADD_IMM:
4892                         case OP_LADD_IMM:
4893                         case OP_ADD_IMM:
4894                                 values [ins->dreg] = LLVMBuildAdd (builder, lhs, imm, dname);
4895                                 break;
4896                         case OP_ISUB_IMM:
4897                         case OP_LSUB_IMM:
4898                                 values [ins->dreg] = LLVMBuildSub (builder, lhs, imm, dname);
4899                                 break;
4900                         case OP_IMUL_IMM:
4901                         case OP_MUL_IMM:
4902                         case OP_LMUL_IMM:
4903                                 values [ins->dreg] = LLVMBuildMul (builder, lhs, imm, dname);
4904                                 break;
4905                         case OP_IDIV_IMM:
4906                         case OP_LDIV_IMM:
4907                                 values [ins->dreg] = LLVMBuildSDiv (builder, lhs, imm, dname);
4908                                 break;
4909                         case OP_IDIV_UN_IMM:
4910                         case OP_LDIV_UN_IMM:
4911                                 values [ins->dreg] = LLVMBuildUDiv (builder, lhs, imm, dname);
4912                                 break;
4913                         case OP_IREM_IMM:
4914                         case OP_LREM_IMM:
4915                                 values [ins->dreg] = LLVMBuildSRem (builder, lhs, imm, dname);
4916                                 break;
4917                         case OP_IREM_UN_IMM:
4918                                 values [ins->dreg] = LLVMBuildURem (builder, lhs, imm, dname);
4919                                 break;
4920                         case OP_IAND_IMM:
4921                         case OP_LAND_IMM:
4922                         case OP_AND_IMM:
4923                                 values [ins->dreg] = LLVMBuildAnd (builder, lhs, imm, dname);
4924                                 break;
4925                         case OP_IOR_IMM:
4926                         case OP_LOR_IMM:
4927                                 values [ins->dreg] = LLVMBuildOr (builder, lhs, imm, dname);
4928                                 break;
4929                         case OP_IXOR_IMM:
4930                         case OP_LXOR_IMM:
4931                                 values [ins->dreg] = LLVMBuildXor (builder, lhs, imm, dname);
4932                                 break;
4933                         case OP_ISHL_IMM:
4934                         case OP_LSHL_IMM:
4935                         case OP_SHL_IMM:
4936                                 values [ins->dreg] = LLVMBuildShl (builder, lhs, imm, dname);
4937                                 break;
4938                         case OP_ISHR_IMM:
4939                         case OP_LSHR_IMM:
4940                         case OP_SHR_IMM:
4941                                 values [ins->dreg] = LLVMBuildAShr (builder, lhs, imm, dname);
4942                                 break;
4943                         case OP_ISHR_UN_IMM:
4944                                 /* This is used to implement conv.u4, so the lhs could be an i8 */
4945                                 lhs = convert (ctx, lhs, LLVMInt32Type ());
4946                                 imm = convert (ctx, imm, LLVMInt32Type ());
4947                                 values [ins->dreg] = LLVMBuildLShr (builder, lhs, imm, dname);
4948                                 break;
4949                         case OP_LSHR_UN_IMM:
4950                         case OP_SHR_UN_IMM:
4951                                 values [ins->dreg] = LLVMBuildLShr (builder, lhs, imm, dname);
4952                                 break;
4953                         default:
4954                                 g_assert_not_reached ();
4955                         }
4956                         break;
4957                 }
4958                 case OP_INEG:
4959                         values [ins->dreg] = LLVMBuildSub (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), convert (ctx, lhs, LLVMInt32Type ()), dname);
4960                         break;
4961                 case OP_LNEG:
4962                         values [ins->dreg] = LLVMBuildSub (builder, LLVMConstInt (LLVMInt64Type (), 0, FALSE), lhs, dname);
4963                         break;
4964                 case OP_FNEG:
4965                         lhs = convert (ctx, lhs, LLVMDoubleType ());
4966                         values [ins->dreg] = LLVMBuildFSub (builder, LLVMConstReal (LLVMDoubleType (), 0.0), lhs, dname);
4967                         break;
4968                 case OP_RNEG:
4969                         lhs = convert (ctx, lhs, LLVMFloatType ());
4970                         values [ins->dreg] = LLVMBuildFSub (builder, LLVMConstReal (LLVMFloatType (), 0.0), lhs, dname);
4971                         break;
4972                 case OP_INOT: {
4973                         guint32 v = 0xffffffff;
4974                         values [ins->dreg] = LLVMBuildXor (builder, LLVMConstInt (LLVMInt32Type (), v, FALSE), convert (ctx, lhs, LLVMInt32Type ()), dname);
4975                         break;
4976                 }
4977                 case OP_LNOT: {
4978                         guint64 v = 0xffffffffffffffffLL;
4979                         values [ins->dreg] = LLVMBuildXor (builder, LLVMConstInt (LLVMInt64Type (), v, FALSE), lhs, dname);
4980                         break;
4981                 }
4982 #if defined(TARGET_X86) || defined(TARGET_AMD64)
4983                 case OP_X86_LEA: {
4984                         LLVMValueRef v1, v2;
4985
4986                         v1 = LLVMBuildMul (builder, convert (ctx, rhs, IntPtrType ()), LLVMConstInt (IntPtrType (), (1 << ins->backend.shift_amount), FALSE), "");
4987                         v2 = LLVMBuildAdd (builder, convert (ctx, lhs, IntPtrType ()), v1, "");
4988                         values [ins->dreg] = LLVMBuildAdd (builder, v2, LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), dname);
4989                         break;
4990                 }
4991 #endif
4992
4993                 case OP_ICONV_TO_I1:
4994                 case OP_ICONV_TO_I2:
4995                 case OP_ICONV_TO_I4:
4996                 case OP_ICONV_TO_U1:
4997                 case OP_ICONV_TO_U2:
4998                 case OP_ICONV_TO_U4:
4999                 case OP_LCONV_TO_I1:
5000                 case OP_LCONV_TO_I2:
5001                 case OP_LCONV_TO_U1:
5002                 case OP_LCONV_TO_U2:
5003                 case OP_LCONV_TO_U4: {
5004                         gboolean sign;
5005
5006                         sign = (ins->opcode == OP_ICONV_TO_I1) || (ins->opcode == OP_ICONV_TO_I2) || (ins->opcode == OP_ICONV_TO_I4) || (ins->opcode == OP_LCONV_TO_I1) || (ins->opcode == OP_LCONV_TO_I2);
5007
5008                         /* Have to do two casts since our vregs have type int */
5009                         v = LLVMBuildTrunc (builder, lhs, op_to_llvm_type (ins->opcode), "");
5010                         if (sign)
5011                                 values [ins->dreg] = LLVMBuildSExt (builder, v, LLVMInt32Type (), dname);
5012                         else
5013                                 values [ins->dreg] = LLVMBuildZExt (builder, v, LLVMInt32Type (), dname);
5014                         break;
5015                 }
5016                 case OP_ICONV_TO_I8:
5017                         values [ins->dreg] = LLVMBuildSExt (builder, lhs, LLVMInt64Type (), dname);
5018                         break;
5019                 case OP_ICONV_TO_U8:
5020                         values [ins->dreg] = LLVMBuildZExt (builder, lhs, LLVMInt64Type (), dname);
5021                         break;
5022                 case OP_FCONV_TO_I4:
5023                 case OP_RCONV_TO_I4:
5024                         values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, LLVMInt32Type (), dname);
5025                         break;
5026                 case OP_FCONV_TO_I1:
5027                 case OP_RCONV_TO_I1:
5028                         values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildFPToSI (builder, lhs, LLVMInt8Type (), dname), LLVMInt32Type (), "");
5029                         break;
5030                 case OP_FCONV_TO_U1:
5031                 case OP_RCONV_TO_U1:
5032                         values [ins->dreg] = LLVMBuildZExt (builder, LLVMBuildTrunc (builder, LLVMBuildFPToUI (builder, lhs, IntPtrType (), dname), LLVMInt8Type (), ""), LLVMInt32Type (), "");
5033                         break;
5034                 case OP_FCONV_TO_I2:
5035                 case OP_RCONV_TO_I2:
5036                         values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildFPToSI (builder, lhs, LLVMInt16Type (), dname), LLVMInt32Type (), "");
5037                         break;
5038                 case OP_FCONV_TO_U2:
5039                 case OP_RCONV_TO_U2:
5040                         values [ins->dreg] = LLVMBuildZExt (builder, LLVMBuildFPToUI (builder, lhs, LLVMInt16Type (), dname), LLVMInt32Type (), "");
5041                         break;
5042                 case OP_RCONV_TO_U4:
5043                         values [ins->dreg] = LLVMBuildFPToUI (builder, lhs, LLVMInt32Type (), dname);
5044                         break;
5045                 case OP_FCONV_TO_I8:
5046                 case OP_RCONV_TO_I8:
5047                         values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, LLVMInt64Type (), dname);
5048                         break;
5049                 case OP_FCONV_TO_I:
5050                         values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, IntPtrType (), dname);
5051                         break;
5052                 case OP_ICONV_TO_R8:
5053                 case OP_LCONV_TO_R8:
5054                         values [ins->dreg] = LLVMBuildSIToFP (builder, lhs, LLVMDoubleType (), dname);
5055                         break;
5056                 case OP_ICONV_TO_R_UN:
5057                 case OP_LCONV_TO_R_UN:
5058                         values [ins->dreg] = LLVMBuildUIToFP (builder, lhs, LLVMDoubleType (), dname);
5059                         break;
5060 #if SIZEOF_VOID_P == 4
5061                 case OP_LCONV_TO_U:
5062 #endif
5063                 case OP_LCONV_TO_I4:
5064                         values [ins->dreg] = LLVMBuildTrunc (builder, lhs, LLVMInt32Type (), dname);
5065                         break;
5066                 case OP_ICONV_TO_R4:
5067                 case OP_LCONV_TO_R4:
5068                         v = LLVMBuildSIToFP (builder, lhs, LLVMFloatType (), "");
5069                         if (cfg->r4fp)
5070                                 values [ins->dreg] = v;
5071                         else
5072                                 values [ins->dreg] = LLVMBuildFPExt (builder, v, LLVMDoubleType (), dname);
5073                         break;
5074                 case OP_FCONV_TO_R4:
5075                         v = LLVMBuildFPTrunc (builder, lhs, LLVMFloatType (), "");
5076                         if (cfg->r4fp)
5077                                 values [ins->dreg] = v;
5078                         else
5079                                 values [ins->dreg] = LLVMBuildFPExt (builder, v, LLVMDoubleType (), dname);
5080                         break;
5081                 case OP_RCONV_TO_R8:
5082                         values [ins->dreg] = LLVMBuildFPExt (builder, lhs, LLVMDoubleType (), dname);
5083                         break;
5084                 case OP_RCONV_TO_R4:
5085                         values [ins->dreg] = lhs;
5086                         break;
5087                 case OP_SEXT_I4:
5088                         values [ins->dreg] = LLVMBuildSExt (builder, convert (ctx, lhs, LLVMInt32Type ()), LLVMInt64Type (), dname);
5089                         break;
5090                 case OP_ZEXT_I4:
5091                         values [ins->dreg] = LLVMBuildZExt (builder, convert (ctx, lhs, LLVMInt32Type ()), LLVMInt64Type (), dname);
5092                         break;
5093                 case OP_TRUNC_I4:
5094                         values [ins->dreg] = LLVMBuildTrunc (builder, lhs, LLVMInt32Type (), dname);
5095                         break;
5096                 case OP_LOCALLOC_IMM: {
5097                         LLVMValueRef v;
5098
5099                         guint32 size = ins->inst_imm;
5100                         size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
5101
5102                         v = mono_llvm_build_alloca (builder, LLVMInt8Type (), LLVMConstInt (LLVMInt32Type (), size, FALSE), MONO_ARCH_FRAME_ALIGNMENT, "");
5103
5104                         if (ins->flags & MONO_INST_INIT) {
5105                                 LLVMValueRef args [5];
5106
5107                                 args [0] = v;
5108                                 args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
5109                                 args [2] = LLVMConstInt (LLVMInt32Type (), size, FALSE);
5110                                 args [3] = LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT, FALSE);
5111                                 args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
5112                                 LLVMBuildCall (builder, get_intrinsic (ctx, "llvm.memset.p0i8.i32"), args, 5, "");
5113                         }
5114
5115                         values [ins->dreg] = v;
5116                         break;
5117                 }
5118                 case OP_LOCALLOC: {
5119                         LLVMValueRef v, size;
5120                                 
5121                         size = LLVMBuildAnd (builder, LLVMBuildAdd (builder, convert (ctx, lhs, LLVMInt32Type ()), LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT - 1, FALSE), ""), LLVMConstInt (LLVMInt32Type (), ~ (MONO_ARCH_FRAME_ALIGNMENT - 1), FALSE), "");
5122
5123                         v = mono_llvm_build_alloca (builder, LLVMInt8Type (), size, MONO_ARCH_FRAME_ALIGNMENT, "");
5124
5125                         if (ins->flags & MONO_INST_INIT) {
5126                                 LLVMValueRef args [5];
5127
5128                                 args [0] = v;
5129                                 args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
5130                                 args [2] = size;
5131                                 args [3] = LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT, FALSE);
5132                                 args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
5133                                 LLVMBuildCall (builder, get_intrinsic (ctx, "llvm.memset.p0i8.i32"), args, 5, "");
5134                         }
5135                         values [ins->dreg] = v;
5136                         break;
5137                 }
5138
5139                 case OP_LOADI1_MEMBASE:
5140                 case OP_LOADU1_MEMBASE:
5141                 case OP_LOADI2_MEMBASE:
5142                 case OP_LOADU2_MEMBASE:
5143                 case OP_LOADI4_MEMBASE:
5144                 case OP_LOADU4_MEMBASE:
5145                 case OP_LOADI8_MEMBASE:
5146                 case OP_LOADR4_MEMBASE:
5147                 case OP_LOADR8_MEMBASE:
5148                 case OP_LOAD_MEMBASE:
5149                 case OP_LOADI8_MEM:
5150                 case OP_LOADU1_MEM:
5151                 case OP_LOADU2_MEM:
5152                 case OP_LOADI4_MEM:
5153                 case OP_LOADU4_MEM:
5154                 case OP_LOAD_MEM: {
5155                         int size = 8;
5156                         LLVMValueRef base, index, addr;
5157                         LLVMTypeRef t;
5158                         gboolean sext = FALSE, zext = FALSE;
5159                         gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
5160
5161                         t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
5162
5163                         if (sext || zext)
5164                                 dname = (char*)"";
5165
5166                         if ((ins->opcode == OP_LOADI8_MEM) || (ins->opcode == OP_LOAD_MEM) || (ins->opcode == OP_LOADI4_MEM) || (ins->opcode == OP_LOADU4_MEM) || (ins->opcode == OP_LOADU1_MEM) || (ins->opcode == OP_LOADU2_MEM)) {
5167                                 addr = LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE);
5168                                 base = addr;
5169                         } else {
5170                                 /* _MEMBASE */
5171                                 base = lhs;
5172
5173                                 if (ins->inst_offset == 0) {
5174                                         addr = base;
5175                                 } else if (ins->inst_offset % size != 0) {
5176                                         /* Unaligned load */
5177                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
5178                                         addr = LLVMBuildGEP (builder, convert (ctx, base, LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
5179                                 } else {
5180                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
5181                                         addr = LLVMBuildGEP (builder, convert (ctx, base, LLVMPointerType (t, 0)), &index, 1, "");
5182                                 }
5183                         }
5184
5185                         addr = convert (ctx, addr, LLVMPointerType (t, 0));
5186
5187                         values [ins->dreg] = emit_load_general (ctx, bb, &builder, size, addr, base, dname, is_volatile, LLVM_BARRIER_NONE);
5188
5189                         if (!is_volatile && (ins->flags & MONO_INST_INVARIANT_LOAD)) {
5190                                 /*
5191                                  * These will signal LLVM that these loads do not alias any stores, and
5192                                  * they can't fail, allowing them to be hoisted out of loops.
5193                                  */
5194                                 set_invariant_load_flag (values [ins->dreg]);
5195 #if LLVM_API_VERSION < 100
5196                                 set_metadata_flag (values [ins->dreg], "mono.nofail.load");
5197 #endif
5198                         }
5199
5200                         if (sext)
5201                                 values [ins->dreg] = LLVMBuildSExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
5202                         else if (zext)
5203                                 values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
5204                         else if (!cfg->r4fp && ins->opcode == OP_LOADR4_MEMBASE)
5205                                 values [ins->dreg] = LLVMBuildFPExt (builder, values [ins->dreg], LLVMDoubleType (), dname);
5206                         break;
5207                 }
5208                                 
5209                 case OP_STOREI1_MEMBASE_REG:
5210                 case OP_STOREI2_MEMBASE_REG:
5211                 case OP_STOREI4_MEMBASE_REG:
5212                 case OP_STOREI8_MEMBASE_REG:
5213                 case OP_STORER4_MEMBASE_REG:
5214                 case OP_STORER8_MEMBASE_REG:
5215                 case OP_STORE_MEMBASE_REG: {
5216                         int size = 8;
5217                         LLVMValueRef index, addr, base;
5218                         LLVMTypeRef t;
5219                         gboolean sext = FALSE, zext = FALSE;
5220                         gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
5221
5222                         if (!values [ins->inst_destbasereg]) {
5223                                 set_failure (ctx, "inst_destbasereg");
5224                                 break;
5225                         }
5226
5227                         t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
5228
5229                         base = values [ins->inst_destbasereg];
5230                         if (ins->inst_offset % size != 0) {
5231                                 /* Unaligned store */
5232                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
5233                                 addr = LLVMBuildGEP (builder, convert (ctx, base, LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
5234                         } else {
5235                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
5236                                 addr = LLVMBuildGEP (builder, convert (ctx, base, LLVMPointerType (t, 0)), &index, 1, "");
5237                         }
5238                         emit_store (ctx, bb, &builder, size, convert (ctx, values [ins->sreg1], t), convert (ctx, addr, LLVMPointerType (t, 0)), base, is_volatile);
5239                         break;
5240                 }
5241
5242                 case OP_STOREI1_MEMBASE_IMM:
5243                 case OP_STOREI2_MEMBASE_IMM:
5244                 case OP_STOREI4_MEMBASE_IMM:
5245                 case OP_STOREI8_MEMBASE_IMM:
5246                 case OP_STORE_MEMBASE_IMM: {
5247                         int size = 8;
5248                         LLVMValueRef index, addr, base;
5249                         LLVMTypeRef t;
5250                         gboolean sext = FALSE, zext = FALSE;
5251                         gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
5252
5253                         t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
5254
5255                         base = values [ins->inst_destbasereg];
5256                         if (ins->inst_offset % size != 0) {
5257                                 /* Unaligned store */
5258                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
5259                                 addr = LLVMBuildGEP (builder, convert (ctx, base, LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
5260                         } else {
5261                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
5262                                 addr = LLVMBuildGEP (builder, convert (ctx, base, LLVMPointerType (t, 0)), &index, 1, "");
5263                         }
5264                         emit_store (ctx, bb, &builder, size, convert (ctx, LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), t), convert (ctx, addr, LLVMPointerType (t, 0)), base, is_volatile);
5265                         break;
5266                 }
5267
5268                 case OP_CHECK_THIS:
5269                         emit_load_general (ctx, bb, &builder, sizeof (gpointer), convert (ctx, lhs, LLVMPointerType (IntPtrType (), 0)), lhs, "", TRUE, LLVM_BARRIER_NONE);
5270                         break;
5271                 case OP_OUTARG_VTRETADDR:
5272                         break;
5273                 case OP_VOIDCALL:
5274                 case OP_CALL:
5275                 case OP_LCALL:
5276                 case OP_FCALL:
5277                 case OP_RCALL:
5278                 case OP_VCALL:
5279                 case OP_VOIDCALL_MEMBASE:
5280                 case OP_CALL_MEMBASE:
5281                 case OP_LCALL_MEMBASE:
5282                 case OP_FCALL_MEMBASE:
5283                 case OP_RCALL_MEMBASE:
5284                 case OP_VCALL_MEMBASE:
5285                 case OP_VOIDCALL_REG:
5286                 case OP_CALL_REG:
5287                 case OP_LCALL_REG:
5288                 case OP_FCALL_REG:
5289                 case OP_RCALL_REG:
5290                 case OP_VCALL_REG: {
5291                         process_call (ctx, bb, &builder, ins);
5292                         break;
5293                 }
5294                 case OP_AOTCONST: {
5295                         guint32 got_offset;
5296                         LLVMValueRef indexes [2];
5297                         MonoJumpInfo *tmp_ji, *ji;
5298                         LLVMValueRef got_entry_addr;
5299                         char *name;
5300
5301                         /* 
5302                          * FIXME: Can't allocate from the cfg mempool since that is freed if
5303                          * the LLVM compile fails.
5304                          */
5305                         tmp_ji = g_new0 (MonoJumpInfo, 1);
5306                         tmp_ji->type = (MonoJumpInfoType)ins->inst_c1;
5307                         tmp_ji->data.target = ins->inst_p0;
5308
5309                         ji = mono_aot_patch_info_dup (tmp_ji);
5310                         g_free (tmp_ji);
5311
5312                         if (ji->type == MONO_PATCH_INFO_ICALL_ADDR) {
5313                                 char *symbol = mono_aot_get_direct_call_symbol (MONO_PATCH_INFO_ICALL_ADDR_CALL, ji->data.target);
5314                                 if (symbol) {
5315                                         /*
5316                                          * Avoid emitting a got entry for these since the method is directly called, and it might not be
5317                                          * resolvable at runtime using dlsym ().
5318                                          */
5319                                         g_free (symbol);
5320                                         values [ins->dreg] = LLVMConstInt (IntPtrType (), 0, FALSE);
5321                                         break;
5322                                 }
5323                         }
5324
5325                         ji->next = cfg->patch_info;
5326                         cfg->patch_info = ji;
5327                                    
5328                         //mono_add_patch_info (cfg, 0, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
5329                         got_offset = mono_aot_get_got_offset (cfg->patch_info);
5330                         ctx->module->max_got_offset = MAX (ctx->module->max_got_offset, got_offset);
5331                         if (!mono_aot_is_shared_got_offset (got_offset)) {
5332                                 //mono_print_ji (ji);
5333                                 //printf ("\n");
5334                                 ctx->has_got_access = TRUE;
5335                         }
5336  
5337                         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
5338                         indexes [1] = LLVMConstInt (LLVMInt32Type (), (gssize)got_offset, FALSE);
5339                         got_entry_addr = LLVMBuildGEP (builder, ctx->module->got_var, indexes, 2, "");
5340
5341                         name = get_aotconst_name (ji->type, ji->data.target, got_offset);
5342                         values [ins->dreg] = LLVMBuildLoad (builder, got_entry_addr, name);
5343                         g_free (name);
5344                         /* Can't use this in llvmonly mode since the got slots are initialized by the methods themselves */
5345                         if (!cfg->llvm_only)
5346                                 set_invariant_load_flag (values [ins->dreg]);
5347                         break;
5348                 }
5349                 case OP_NOT_REACHED:
5350                         LLVMBuildUnreachable (builder);
5351                         has_terminator = TRUE;
5352                         g_assert (bb->block_num < cfg->max_block_num);
5353                         ctx->unreachable [bb->block_num] = TRUE;
5354                         /* Might have instructions after this */
5355                         while (ins->next) {
5356                                 MonoInst *next = ins->next;
5357                                 /* 
5358                                  * FIXME: If later code uses the regs defined by these instructions,
5359                                  * compilation will fail.
5360                                  */
5361                                 MONO_DELETE_INS (bb, next);
5362                         }                               
5363                         break;
5364                 case OP_LDADDR: {
5365                         MonoInst *var = ins->inst_i0;
5366
5367                         if (var->opcode == OP_VTARG_ADDR) {
5368                                 /* The variable contains the vtype address */
5369                                 values [ins->dreg] = values [var->dreg];
5370                         } else if (var->opcode == OP_GSHAREDVT_LOCAL) {
5371                                 values [ins->dreg] = emit_gsharedvt_ldaddr (ctx, var->dreg);
5372                         } else {
5373                                 values [ins->dreg] = addresses [var->dreg];
5374                         }
5375                         break;
5376                 }
5377                 case OP_SIN: {
5378                         LLVMValueRef args [1];
5379
5380                         args [0] = convert (ctx, lhs, LLVMDoubleType ());
5381                         values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, "llvm.sin.f64"), args, 1, dname);
5382                         break;
5383                 }
5384                 case OP_COS: {
5385                         LLVMValueRef args [1];
5386
5387                         args [0] = convert (ctx, lhs, LLVMDoubleType ());
5388                         values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, "llvm.cos.f64"), args, 1, dname);
5389                         break;
5390                 }
5391                 case OP_SQRT: {
5392                         LLVMValueRef args [1];
5393
5394                         args [0] = convert (ctx, lhs, LLVMDoubleType ());
5395                         values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, "llvm.sqrt.f64"), args, 1, dname);
5396                         break;
5397                 }
5398                 case OP_ABS: {
5399                         LLVMValueRef args [1];
5400
5401                         args [0] = convert (ctx, lhs, LLVMDoubleType ());
5402                         values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, "fabs"), args, 1, dname);
5403                         break;
5404                 }
5405
5406                 case OP_IMIN:
5407                 case OP_LMIN:
5408                 case OP_IMAX:
5409                 case OP_LMAX:
5410                 case OP_IMIN_UN:
5411                 case OP_LMIN_UN:
5412                 case OP_IMAX_UN:
5413                 case OP_LMAX_UN: {
5414                         LLVMValueRef v;
5415
5416                         lhs = convert (ctx, lhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
5417                         rhs = convert (ctx, rhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
5418
5419                         switch (ins->opcode) {
5420                         case OP_IMIN:
5421                         case OP_LMIN:
5422                                 v = LLVMBuildICmp (builder, LLVMIntSLE, lhs, rhs, "");
5423                                 break;
5424                         case OP_IMAX:
5425                         case OP_LMAX:
5426                                 v = LLVMBuildICmp (builder, LLVMIntSGE, lhs, rhs, "");
5427                                 break;
5428                         case OP_IMIN_UN:
5429                         case OP_LMIN_UN:
5430                                 v = LLVMBuildICmp (builder, LLVMIntULE, lhs, rhs, "");
5431                                 break;
5432                         case OP_IMAX_UN:
5433                         case OP_LMAX_UN:
5434                                 v = LLVMBuildICmp (builder, LLVMIntUGE, lhs, rhs, "");
5435                                 break;
5436                         default:
5437                                 g_assert_not_reached ();
5438                                 break;
5439                         }
5440                         values [ins->dreg] = LLVMBuildSelect (builder, v, lhs, rhs, dname);
5441                         break;
5442                 }
5443
5444 /*
5445  * See the ARM64 comment in mono/utils/atomic.h for an explanation of why this
5446  * hack is necessary (for now).
5447  */
5448 #ifdef TARGET_ARM64
5449 #define ARM64_ATOMIC_FENCE_FIX mono_llvm_build_fence (builder, LLVM_BARRIER_SEQ)
5450 #else
5451 #define ARM64_ATOMIC_FENCE_FIX
5452 #endif
5453
5454                 case OP_ATOMIC_EXCHANGE_I4:
5455                 case OP_ATOMIC_EXCHANGE_I8: {
5456                         LLVMValueRef args [2];
5457                         LLVMTypeRef t;
5458                                 
5459                         if (ins->opcode == OP_ATOMIC_EXCHANGE_I4)
5460                                 t = LLVMInt32Type ();
5461                         else
5462                                 t = LLVMInt64Type ();
5463
5464                         g_assert (ins->inst_offset == 0);
5465
5466                         args [0] = convert (ctx, lhs, LLVMPointerType (t, 0));
5467                         args [1] = convert (ctx, rhs, t);
5468
5469                         ARM64_ATOMIC_FENCE_FIX;
5470                         values [ins->dreg] = mono_llvm_build_atomic_rmw (builder, LLVM_ATOMICRMW_OP_XCHG, args [0], args [1]);
5471                         ARM64_ATOMIC_FENCE_FIX;
5472                         break;
5473                 }
5474                 case OP_ATOMIC_ADD_I4:
5475                 case OP_ATOMIC_ADD_I8: {
5476                         LLVMValueRef args [2];
5477                         LLVMTypeRef t;
5478                                 
5479                         if (ins->opcode == OP_ATOMIC_ADD_I4)
5480                                 t = LLVMInt32Type ();
5481                         else
5482                                 t = LLVMInt64Type ();
5483
5484                         g_assert (ins->inst_offset == 0);
5485
5486                         args [0] = convert (ctx, lhs, LLVMPointerType (t, 0));
5487                         args [1] = convert (ctx, rhs, t);
5488                         ARM64_ATOMIC_FENCE_FIX;
5489                         values [ins->dreg] = LLVMBuildAdd (builder, mono_llvm_build_atomic_rmw (builder, LLVM_ATOMICRMW_OP_ADD, args [0], args [1]), args [1], dname);
5490                         ARM64_ATOMIC_FENCE_FIX;
5491                         break;
5492                 }
5493                 case OP_ATOMIC_CAS_I4:
5494                 case OP_ATOMIC_CAS_I8: {
5495                         LLVMValueRef args [3], val;
5496                         LLVMTypeRef t;
5497                                 
5498                         if (ins->opcode == OP_ATOMIC_CAS_I4)
5499                                 t = LLVMInt32Type ();
5500                         else
5501                                 t = LLVMInt64Type ();
5502
5503                         args [0] = convert (ctx, lhs, LLVMPointerType (t, 0));
5504                         /* comparand */
5505                         args [1] = convert (ctx, values [ins->sreg3], t);
5506                         /* new value */
5507                         args [2] = convert (ctx, values [ins->sreg2], t);
5508                         ARM64_ATOMIC_FENCE_FIX;
5509                         val = mono_llvm_build_cmpxchg (builder, args [0], args [1], args [2]);
5510                         ARM64_ATOMIC_FENCE_FIX;
5511                         /* cmpxchg returns a pair */
5512                         values [ins->dreg] = LLVMBuildExtractValue (builder, val, 0, "");
5513                         break;
5514                 }
5515                 case OP_MEMORY_BARRIER: {
5516                         mono_llvm_build_fence (builder, (BarrierKind) ins->backend.memory_barrier_kind);
5517                         break;
5518                 }
5519                 case OP_ATOMIC_LOAD_I1:
5520                 case OP_ATOMIC_LOAD_I2:
5521                 case OP_ATOMIC_LOAD_I4:
5522                 case OP_ATOMIC_LOAD_I8:
5523                 case OP_ATOMIC_LOAD_U1:
5524                 case OP_ATOMIC_LOAD_U2:
5525                 case OP_ATOMIC_LOAD_U4:
5526                 case OP_ATOMIC_LOAD_U8:
5527                 case OP_ATOMIC_LOAD_R4:
5528                 case OP_ATOMIC_LOAD_R8: {
5529 #if LLVM_API_VERSION > 100
5530                         int size;
5531                         gboolean sext, zext;
5532                         LLVMTypeRef t;
5533                         gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
5534                         BarrierKind barrier = (BarrierKind) ins->backend.memory_barrier_kind;
5535                         LLVMValueRef index, addr;
5536
5537                         t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
5538
5539                         if (sext || zext)
5540                                 dname = (char *)"";
5541
5542                         if (ins->inst_offset != 0) {
5543                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
5544                                 addr = LLVMBuildGEP (builder, convert (ctx, lhs, LLVMPointerType (t, 0)), &index, 1, "");
5545                         } else {
5546                                 addr = lhs;
5547                         }
5548
5549                         addr = convert (ctx, addr, LLVMPointerType (t, 0));
5550
5551                         ARM64_ATOMIC_FENCE_FIX;
5552                         values [ins->dreg] = emit_load_general (ctx, bb, &builder, size, addr, lhs, dname, is_volatile, barrier);
5553                         ARM64_ATOMIC_FENCE_FIX;
5554
5555                         if (sext)
5556                                 values [ins->dreg] = LLVMBuildSExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
5557                         else if (zext)
5558                                 values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
5559                         break;
5560 #else
5561                         set_failure (ctx, "atomic mono.load intrinsic");
5562                         break;
5563 #endif
5564                 }
5565                 case OP_ATOMIC_STORE_I1:
5566                 case OP_ATOMIC_STORE_I2:
5567                 case OP_ATOMIC_STORE_I4:
5568                 case OP_ATOMIC_STORE_I8:
5569                 case OP_ATOMIC_STORE_U1:
5570                 case OP_ATOMIC_STORE_U2:
5571                 case OP_ATOMIC_STORE_U4:
5572                 case OP_ATOMIC_STORE_U8:
5573                 case OP_ATOMIC_STORE_R4:
5574                 case OP_ATOMIC_STORE_R8: {
5575                         int size;
5576                         gboolean sext, zext;
5577                         LLVMTypeRef t;
5578                         gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
5579                         BarrierKind barrier = (BarrierKind) ins->backend.memory_barrier_kind;
5580                         LLVMValueRef index, addr, value, base;
5581
5582 #if LLVM_API_VERSION < 100
5583                         if (!cfg->llvm_only) {
5584                                 set_failure (ctx, "atomic mono.store intrinsic");
5585                                 break;
5586                         }
5587 #endif
5588
5589                         if (!values [ins->inst_destbasereg]) {
5590                             set_failure (ctx, "inst_destbasereg");
5591                                 break;
5592                         }
5593
5594                         t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
5595
5596                         base = values [ins->inst_destbasereg];
5597                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
5598                         addr = LLVMBuildGEP (builder, convert (ctx, base, LLVMPointerType (t, 0)), &index, 1, "");
5599                         value = convert (ctx, values [ins->sreg1], t);
5600
5601                         ARM64_ATOMIC_FENCE_FIX;
5602                         emit_store_general (ctx, bb, &builder, size, value, addr, base, is_volatile, barrier);
5603                         ARM64_ATOMIC_FENCE_FIX;
5604                         break;
5605                 }
5606                 case OP_RELAXED_NOP: {
5607 #if defined(TARGET_AMD64) || defined(TARGET_X86)
5608                         emit_call (ctx, bb, &builder, get_intrinsic (ctx, "llvm.x86.sse2.pause"), NULL, 0);
5609                         break;
5610 #else
5611                         break;
5612 #endif
5613                 }
5614                 case OP_GC_SAFE_POINT: {
5615                         LLVMValueRef val, cmp, callee;
5616                         LLVMBasicBlockRef poll_bb, cont_bb;
5617                         static LLVMTypeRef sig;
5618                         const char *icall_name = "mono_threads_state_poll";
5619
5620                         if (!sig)
5621                                 sig = LLVMFunctionType0 (LLVMVoidType (), FALSE);
5622
5623                         /*
5624                          * if (!*sreg1)
5625                          *   mono_threads_state_poll ();
5626                          * FIXME: Use a preserveall wrapper
5627                          */
5628                         val = mono_llvm_build_load (builder, convert (ctx, lhs, LLVMPointerType (IntPtrType (), 0)), "", TRUE);
5629                         cmp = LLVMBuildICmp (builder, LLVMIntEQ, val, LLVMConstNull (LLVMTypeOf (val)), "");
5630                         poll_bb = gen_bb (ctx, "POLL_BB");
5631                         cont_bb = gen_bb (ctx, "CONT_BB");
5632                         LLVMBuildCondBr (builder, cmp, cont_bb, poll_bb);
5633
5634                         ctx->builder = builder = create_builder (ctx);
5635                         LLVMPositionBuilderAtEnd (builder, poll_bb);
5636
5637                         if (ctx->cfg->compile_aot) {
5638                                 callee = get_callee (ctx, sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
5639                         } else {
5640                                 gpointer target = resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
5641                                 callee = emit_jit_callee (ctx, icall_name, sig, target);
5642                         }
5643                         LLVMBuildCall (builder, callee, NULL, 0, "");
5644                         LLVMBuildBr (builder, cont_bb);
5645
5646                         ctx->builder = builder = create_builder (ctx);
5647                         LLVMPositionBuilderAtEnd (builder, cont_bb);
5648                         ctx->bblocks [bb->block_num].end_bblock = cont_bb;
5649                         break;
5650                 }
5651
5652                         /*
5653                          * Overflow opcodes.
5654                          */
5655                 case OP_IADD_OVF:
5656                 case OP_IADD_OVF_UN:
5657                 case OP_ISUB_OVF:
5658                 case OP_ISUB_OVF_UN:
5659                 case OP_IMUL_OVF:
5660                 case OP_IMUL_OVF_UN:
5661                 case OP_LADD_OVF:
5662                 case OP_LADD_OVF_UN:
5663                 case OP_LSUB_OVF:
5664                 case OP_LSUB_OVF_UN:
5665                 case OP_LMUL_OVF:
5666                 case OP_LMUL_OVF_UN:
5667                         {
5668                                 LLVMValueRef args [2], val, ovf, func;
5669
5670                                 args [0] = convert (ctx, lhs, op_to_llvm_type (ins->opcode));
5671                                 args [1] = convert (ctx, rhs, op_to_llvm_type (ins->opcode));
5672                                 func = get_intrinsic (ctx, ovf_op_to_intrins (ins->opcode));
5673                                 g_assert (func);
5674                                 val = LLVMBuildCall (builder, func, args, 2, "");
5675                                 values [ins->dreg] = LLVMBuildExtractValue (builder, val, 0, dname);
5676                                 ovf = LLVMBuildExtractValue (builder, val, 1, "");
5677                                 emit_cond_system_exception (ctx, bb, "OverflowException", ovf);
5678                                 if (!ctx_ok (ctx))
5679                                         break;
5680                                 builder = ctx->builder;
5681                                 break;
5682                         }
5683
5684                         /* 
5685                          * Valuetypes.
5686                          *   We currently model them using arrays. Promotion to local vregs is 
5687                          * disabled for them in mono_handle_global_vregs () in the LLVM case, 
5688                          * so we always have an entry in cfg->varinfo for them.
5689                          * FIXME: Is this needed ?
5690                          */
5691                 case OP_VZERO: {
5692                         MonoClass *klass = ins->klass;
5693                         LLVMValueRef args [5];
5694
5695                         if (!klass) {
5696                                 // FIXME:
5697                                 set_failure (ctx, "!klass");
5698                                 break;
5699                         }
5700
5701                         if (!addresses [ins->dreg])
5702                                 addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
5703                         args [0] = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
5704                         args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
5705                         args [2] = LLVMConstInt (LLVMInt32Type (), mono_class_value_size (klass, NULL), FALSE);
5706                         // FIXME: Alignment
5707                         args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
5708                         args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
5709                         LLVMBuildCall (builder, get_intrinsic (ctx, "llvm.memset.p0i8.i32"), args, 5, "");
5710                         break;
5711                 }
5712                 case OP_DUMMY_VZERO:
5713                         break;
5714
5715                 case OP_STOREV_MEMBASE:
5716                 case OP_LOADV_MEMBASE:
5717                 case OP_VMOVE: {
5718                         MonoClass *klass = ins->klass;
5719                         LLVMValueRef src = NULL, dst, args [5];
5720                         gboolean done = FALSE;
5721
5722                         if (!klass) {
5723                                 // FIXME:
5724                                 set_failure (ctx, "!klass");
5725                                 break;
5726                         }
5727
5728                         if (mini_is_gsharedvt_klass (klass)) {
5729                                 // FIXME:
5730                                 set_failure (ctx, "gsharedvt");
5731                                 break;
5732                         }
5733
5734                         switch (ins->opcode) {
5735                         case OP_STOREV_MEMBASE:
5736                                 if (cfg->gen_write_barriers && klass->has_references && ins->inst_destbasereg != cfg->frame_reg &&
5737                                         LLVMGetInstructionOpcode (values [ins->inst_destbasereg]) != LLVMAlloca) {
5738                                         /* Decomposed earlier */
5739                                         g_assert_not_reached ();
5740                                         break;
5741                                 }
5742                                 if (!addresses [ins->sreg1]) {
5743                                         /* SIMD */
5744                                         g_assert (values [ins->sreg1]);
5745                                         dst = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_destbasereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (type_to_llvm_type (ctx, &klass->byval_arg), 0));
5746                                         LLVMBuildStore (builder, values [ins->sreg1], dst);
5747                                         done = TRUE;
5748                                 } else {
5749                                         src = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMInt8Type (), 0), "");
5750                                         dst = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_destbasereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (LLVMInt8Type (), 0));
5751                                 }
5752                                 break;
5753                         case OP_LOADV_MEMBASE:
5754                                 if (!addresses [ins->dreg])
5755                                         addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
5756                                 src = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_basereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (LLVMInt8Type (), 0));
5757                                 dst = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
5758                                 break;
5759                         case OP_VMOVE:
5760                                 if (!addresses [ins->sreg1])
5761                                         addresses [ins->sreg1] = build_alloca (ctx, &klass->byval_arg);
5762                                 if (!addresses [ins->dreg])
5763                                         addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
5764                                 src = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMInt8Type (), 0), "");
5765                                 dst = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
5766                                 break;
5767                         default:
5768                                 g_assert_not_reached ();
5769                         }
5770                         if (!ctx_ok (ctx))
5771                                 break;
5772
5773                         if (done)
5774                                 break;
5775
5776                         args [0] = dst;
5777                         args [1] = src;
5778                         args [2] = LLVMConstInt (LLVMInt32Type (), mono_class_value_size (klass, NULL), FALSE);
5779                         args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
5780                         // FIXME: Alignment
5781                         args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
5782                         args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
5783                         LLVMBuildCall (builder, get_intrinsic (ctx, "llvm.memcpy.p0i8.p0i8.i32"), args, 5, "");
5784                         break;
5785                 }
5786                 case OP_LLVM_OUTARG_VT: {
5787                         LLVMArgInfo *ainfo = (LLVMArgInfo*)ins->inst_p0;
5788                         MonoType *t = mini_get_underlying_type (ins->inst_vtype);
5789
5790                         if (ainfo->storage == LLVMArgGsharedvtVariable) {
5791                                         MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
5792
5793                                         if (var && var->opcode == OP_GSHAREDVT_LOCAL) {
5794                                                 addresses [ins->dreg] = convert (ctx, emit_gsharedvt_ldaddr (ctx, var->dreg), LLVMPointerType (IntPtrType (), 0));
5795                                         } else {
5796                                                 g_assert (addresses [ins->sreg1]);
5797                                                 addresses [ins->dreg] = addresses [ins->sreg1];
5798                                         }
5799                         } else if (ainfo->storage == LLVMArgGsharedvtFixed) {
5800                                 if (!addresses [ins->sreg1]) {
5801                                         addresses [ins->sreg1] = build_alloca (ctx, t);
5802                                         g_assert (values [ins->sreg1]);
5803                                 }
5804                                 LLVMBuildStore (builder, convert (ctx, values [ins->sreg1], LLVMGetElementType (LLVMTypeOf (addresses [ins->sreg1]))), addresses [ins->sreg1]);
5805                                 addresses [ins->dreg] = addresses [ins->sreg1];
5806                         } else {
5807                                 if (!addresses [ins->sreg1]) {
5808                                         addresses [ins->sreg1] = build_alloca (ctx, t);
5809                                         g_assert (values [ins->sreg1]);
5810                                         LLVMBuildStore (builder, convert (ctx, values [ins->sreg1], type_to_llvm_type (ctx, t)), addresses [ins->sreg1]);
5811                                 }
5812                                 addresses [ins->dreg] = addresses [ins->sreg1];
5813                         }
5814                         break;
5815                 }
5816                 case OP_OBJC_GET_SELECTOR: {
5817                         const char *name = (const char*)ins->inst_p0;
5818                         LLVMValueRef var;
5819
5820                         if (!ctx->module->objc_selector_to_var) {
5821                                 ctx->module->objc_selector_to_var = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
5822
5823                                 LLVMValueRef info_var = LLVMAddGlobal (ctx->lmodule, LLVMArrayType (LLVMInt8Type (), 8), "@OBJC_IMAGE_INFO");
5824                                 int32_t objc_imageinfo [] = { 0, 16 };
5825                                 LLVMSetInitializer (info_var, mono_llvm_create_constant_data_array ((uint8_t *) &objc_imageinfo, 8));
5826                                 LLVMSetLinkage (info_var, LLVMPrivateLinkage);
5827                                 LLVMSetExternallyInitialized (info_var, TRUE);
5828                                 LLVMSetSection (info_var, "__DATA, __objc_imageinfo,regular,no_dead_strip");
5829                                 LLVMSetAlignment (info_var, sizeof (mgreg_t));
5830                                 mark_as_used (ctx->module, info_var);
5831                         }
5832
5833                         var = g_hash_table_lookup (ctx->module->objc_selector_to_var, name);
5834                         if (!var) {
5835                                 LLVMValueRef indexes [16];
5836
5837                                 LLVMValueRef name_var = LLVMAddGlobal (ctx->lmodule, LLVMArrayType (LLVMInt8Type (), strlen (name) + 1), "@OBJC_METH_VAR_NAME_");
5838                                 LLVMSetInitializer (name_var, mono_llvm_create_constant_data_array ((const uint8_t*)name, strlen (name) + 1));
5839                                 LLVMSetLinkage (name_var, LLVMPrivateLinkage);
5840                                 LLVMSetSection (name_var, "__TEXT,__objc_methname,cstring_literals");
5841                                 mark_as_used (ctx->module, name_var);
5842
5843                                 LLVMValueRef ref_var = LLVMAddGlobal (ctx->lmodule, LLVMPointerType (LLVMInt8Type (), 0), "@OBJC_SELECTOR_REFERENCES_");
5844
5845                                 indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, 0);
5846                                 indexes [1] = LLVMConstInt (LLVMInt32Type (), 0, 0);
5847                                 LLVMSetInitializer (ref_var, LLVMConstGEP (name_var, indexes, 2));
5848                                 LLVMSetLinkage (ref_var, LLVMPrivateLinkage);
5849                                 LLVMSetExternallyInitialized (ref_var, TRUE);
5850                                 LLVMSetSection (ref_var, "__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
5851                                 LLVMSetAlignment (ref_var, sizeof (mgreg_t));
5852                                 mark_as_used (ctx->module, ref_var);
5853
5854                                 g_hash_table_insert (ctx->module->objc_selector_to_var, g_strdup (name), ref_var);
5855                                 var = ref_var;
5856                         }
5857
5858                         values [ins->dreg] = LLVMBuildLoad (builder, var, "");
5859                         break;
5860                 }
5861
5862                         /* 
5863                          * SIMD
5864                          */
5865 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5866                 case OP_XZERO: {
5867                         values [ins->dreg] = LLVMConstNull (type_to_llvm_type (ctx, &ins->klass->byval_arg));
5868                         break;
5869                 }
5870                 case OP_LOADX_MEMBASE: {
5871                         LLVMTypeRef t = type_to_llvm_type (ctx, &ins->klass->byval_arg);
5872                         LLVMValueRef src;
5873
5874                         src = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_basereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (t, 0));
5875                         values [ins->dreg] = mono_llvm_build_aligned_load (builder, src, "", FALSE, 1);
5876                         break;
5877                 }
5878                 case OP_STOREX_MEMBASE: {
5879                         LLVMTypeRef t = LLVMTypeOf (values [ins->sreg1]);
5880                         LLVMValueRef dest;
5881
5882                         dest = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_destbasereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (t, 0));
5883                         mono_llvm_build_aligned_store (builder, values [ins->sreg1], dest, FALSE, 1);
5884                         break;
5885                 }
5886                 case OP_PADDB:
5887                 case OP_PADDW:
5888                 case OP_PADDD:
5889                 case OP_PADDQ:
5890                         values [ins->dreg] = LLVMBuildAdd (builder, lhs, rhs, "");
5891                         break;
5892                 case OP_ADDPD:
5893                 case OP_ADDPS:
5894                         values [ins->dreg] = LLVMBuildFAdd (builder, lhs, rhs, "");
5895                         break;
5896                 case OP_PSUBB:
5897                 case OP_PSUBW:
5898                 case OP_PSUBD:
5899                 case OP_PSUBQ:
5900                         values [ins->dreg] = LLVMBuildSub (builder, lhs, rhs, "");
5901                         break;
5902                 case OP_SUBPD:
5903                 case OP_SUBPS:
5904                         values [ins->dreg] = LLVMBuildFSub (builder, lhs, rhs, "");
5905                         break;
5906                 case OP_MULPD:
5907                 case OP_MULPS:
5908                         values [ins->dreg] = LLVMBuildFMul (builder, lhs, rhs, "");
5909                         break;
5910                 case OP_DIVPD:
5911                 case OP_DIVPS:
5912                         values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, "");
5913                         break;
5914                 case OP_PAND:
5915                         values [ins->dreg] = LLVMBuildAnd (builder, lhs, rhs, "");
5916                         break;
5917                 case OP_POR:
5918                         values [ins->dreg] = LLVMBuildOr (builder, lhs, rhs, "");
5919                         break;
5920                 case OP_PXOR:
5921                         values [ins->dreg] = LLVMBuildXor (builder, lhs, rhs, "");
5922                         break;
5923                 case OP_PMULW:
5924                 case OP_PMULD:
5925                         values [ins->dreg] = LLVMBuildMul (builder, lhs, rhs, "");
5926                         break;
5927                 case OP_ANDPS:
5928                 case OP_ANDNPS:
5929                 case OP_ORPS:
5930                 case OP_XORPS:
5931                 case OP_ANDPD:
5932                 case OP_ANDNPD:
5933                 case OP_ORPD:
5934                 case OP_XORPD: {
5935                         LLVMTypeRef t, rt;
5936                         LLVMValueRef v = NULL;
5937
5938                         switch (ins->opcode) {
5939                         case OP_ANDPS:
5940                         case OP_ANDNPS:
5941                         case OP_ORPS:
5942                         case OP_XORPS:
5943                                 t = LLVMVectorType (LLVMInt32Type (), 4);
5944                                 rt = LLVMVectorType (LLVMFloatType (), 4);
5945                                 break;
5946                         case OP_ANDPD:
5947                         case OP_ANDNPD:
5948                         case OP_ORPD:
5949                         case OP_XORPD:
5950                                 t = LLVMVectorType (LLVMInt64Type (), 2);
5951                                 rt = LLVMVectorType (LLVMDoubleType (), 2);
5952                                 break;
5953                         default:
5954                                 t = LLVMInt32Type ();
5955                                 rt = LLVMInt32Type ();
5956                                 g_assert_not_reached ();
5957                         }
5958
5959                         lhs = LLVMBuildBitCast (builder, lhs, t, "");
5960                         rhs = LLVMBuildBitCast (builder, rhs, t, "");
5961                         switch (ins->opcode) {
5962                         case OP_ANDPS:
5963                         case OP_ANDPD:
5964                                 v = LLVMBuildAnd (builder, lhs, rhs, "");
5965                                 break;
5966                         case OP_ORPS:
5967                         case OP_ORPD:
5968                                 v = LLVMBuildOr (builder, lhs, rhs, "");
5969                                 break;
5970                         case OP_XORPS:
5971                         case OP_XORPD:
5972                                 v = LLVMBuildXor (builder, lhs, rhs, "");
5973                                 break;
5974                         case OP_ANDNPS:
5975                         case OP_ANDNPD:
5976                                 v = LLVMBuildAnd (builder, rhs, LLVMBuildNot (builder, lhs, ""), "");
5977                                 break;
5978                         }
5979                         values [ins->dreg] = LLVMBuildBitCast (builder, v, rt, "");
5980                         break;
5981                 }
5982                 case OP_PMIND_UN:
5983                 case OP_PMINW_UN:
5984                 case OP_PMINB_UN: {
5985                         LLVMValueRef cmp = LLVMBuildICmp (builder, LLVMIntULT, lhs, rhs, "");
5986                         values [ins->dreg] = LLVMBuildSelect (builder, cmp, lhs, rhs, "");
5987                         break;
5988                 }
5989                 case OP_PMAXD_UN:
5990                 case OP_PMAXW_UN:
5991                 case OP_PMAXB_UN: {
5992                         LLVMValueRef cmp = LLVMBuildICmp (builder, LLVMIntUGT, lhs, rhs, "");
5993                         values [ins->dreg] = LLVMBuildSelect (builder, cmp, lhs, rhs, "");
5994                         break;
5995                 }
5996                 case OP_PMINW: {
5997                         LLVMValueRef cmp = LLVMBuildICmp (builder, LLVMIntSLT, lhs, rhs, "");
5998                         values [ins->dreg] = LLVMBuildSelect (builder, cmp, lhs, rhs, "");
5999                         break;
6000                 }
6001                 case OP_MINPD:
6002                 case OP_MINPS:
6003                 case OP_MAXPD:
6004                 case OP_MAXPS:
6005                 case OP_ADDSUBPD:
6006                 case OP_ADDSUBPS:
6007                 case OP_HADDPD:
6008                 case OP_HADDPS:
6009                 case OP_HSUBPD:
6010                 case OP_HSUBPS:
6011                 case OP_PADDB_SAT:
6012                 case OP_PADDW_SAT:
6013                 case OP_PSUBB_SAT:
6014                 case OP_PSUBW_SAT:
6015                 case OP_PADDB_SAT_UN:
6016                 case OP_PADDW_SAT_UN:
6017                 case OP_PSUBB_SAT_UN:
6018                 case OP_PSUBW_SAT_UN:
6019                 case OP_PAVGB_UN:
6020                 case OP_PAVGW_UN:
6021                 case OP_PACKW:
6022                 case OP_PACKD:
6023                 case OP_PACKW_UN:
6024                 case OP_PACKD_UN:
6025                 case OP_PMULW_HIGH:
6026                 case OP_PMULW_HIGH_UN: {
6027                         LLVMValueRef args [2];
6028
6029                         args [0] = lhs;
6030                         args [1] = rhs;
6031
6032                         values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, simd_op_to_intrins (ins->opcode)), args, 2, dname);
6033                         break;
6034                 }
6035                 case OP_PCMPEQB:
6036                 case OP_PCMPEQW:
6037                 case OP_PCMPEQD:
6038                 case OP_PCMPEQQ: {
6039                         values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildICmp (builder, LLVMIntEQ, lhs, rhs, ""), LLVMTypeOf (lhs), "");
6040                         break;
6041                 }
6042                 case OP_PCMPGTB: {
6043                         values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildICmp (builder, LLVMIntSGT, lhs, rhs, ""), LLVMTypeOf (lhs), "");
6044                         break;
6045                 }
6046                 case OP_EXTRACT_R8:
6047                 case OP_EXTRACT_I8:
6048                 case OP_EXTRACT_I4:
6049                 case OP_EXTRACT_I2:
6050                 case OP_EXTRACT_U2:
6051                 case OP_EXTRACTX_U2:
6052                 case OP_EXTRACT_I1:
6053                 case OP_EXTRACT_U1: {
6054                         LLVMTypeRef t;
6055                         gboolean zext = FALSE;
6056
6057                         t = simd_op_to_llvm_type (ins->opcode);
6058
6059                         switch (ins->opcode) {
6060                         case OP_EXTRACT_R8:
6061                         case OP_EXTRACT_I8:
6062                         case OP_EXTRACT_I4:
6063                         case OP_EXTRACT_I2:
6064                         case OP_EXTRACT_I1:
6065                                 break;
6066                         case OP_EXTRACT_U2:
6067                         case OP_EXTRACTX_U2:
6068                         case OP_EXTRACT_U1:
6069                                 zext = TRUE;
6070                                 break;
6071                         default:
6072                                 t = LLVMInt32Type ();
6073                                 g_assert_not_reached ();
6074                         }
6075
6076                         lhs = LLVMBuildBitCast (builder, lhs, t, "");
6077                         values [ins->dreg] = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), "");
6078                         if (zext)
6079                                 values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), "");
6080                         break;
6081                 }
6082
6083                 case OP_EXPAND_I1:
6084                 case OP_EXPAND_I2:
6085                 case OP_EXPAND_I4:
6086                 case OP_EXPAND_I8:
6087                 case OP_EXPAND_R4:
6088                 case OP_EXPAND_R8: {
6089                         LLVMTypeRef t = simd_op_to_llvm_type (ins->opcode);
6090                         LLVMValueRef mask [16], v;
6091                         int i;
6092
6093                         for (i = 0; i < 16; ++i)
6094                                 mask [i] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
6095
6096                         v = convert (ctx, values [ins->sreg1], LLVMGetElementType (t));
6097
6098                         values [ins->dreg] = LLVMBuildInsertElement (builder, LLVMConstNull (t), v, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
6099                         values [ins->dreg] = LLVMBuildShuffleVector (builder, values [ins->dreg], LLVMGetUndef (t), LLVMConstVector (mask, LLVMGetVectorSize (t)), "");
6100                         break;
6101                 }
6102
6103                 case OP_INSERT_I1:
6104                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt8Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
6105                         break;
6106                 case OP_INSERT_I2:
6107                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt16Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
6108                         break;
6109                 case OP_INSERT_I4:
6110                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt32Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
6111                         break;
6112                 case OP_INSERT_I8:
6113                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt64Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
6114                         break;
6115                 case OP_INSERT_R4:
6116                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMFloatType ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
6117                         break;
6118                 case OP_INSERT_R8:
6119                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMDoubleType ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
6120                         break;
6121
6122 #if 0
6123                         // Requires a later llvm version
6124                 case OP_CVTDQ2PD: {
6125                         LLVMValueRef indexes [16];
6126
6127                         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
6128                         indexes [1] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
6129                         LLVMValueRef mask = LLVMConstVector (indexes, 2);
6130                         LLVMValueRef shuffle = LLVMBuildShuffleVector (builder, lhs, LLVMConstNull (LLVMTypeOf (lhs)), mask, "");
6131                         values [ins->dreg] = LLVMBuildSIToFP (builder, shuffle, LLVMVectorType (LLVMDoubleType (), 2), dname);
6132                         break;
6133                 }
6134                 case OP_CVTPS2PD: {
6135                         LLVMValueRef indexes [16];
6136
6137                         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
6138                         indexes [1] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
6139                         LLVMValueRef mask = LLVMConstVector (indexes, 2);
6140                         LLVMValueRef shuffle = LLVMBuildShuffleVector (builder, lhs, LLVMConstNull (LLVMTypeOf (lhs)), mask, "");
6141                         values [ins->dreg] = LLVMBuildFPExt (builder, shuffle, LLVMVectorType (LLVMDoubleType (), 2), dname);
6142                         break;
6143                 }
6144                 case OP_CVTTPS2DQ:
6145                         values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, LLVMVectorType (LLVMInt32Type (), 4), dname);
6146                         break;
6147 #endif
6148
6149                 case OP_CVTDQ2PD:
6150                 case OP_CVTDQ2PS:
6151                 case OP_CVTPD2DQ:
6152                 case OP_CVTPS2DQ:
6153                 case OP_CVTPD2PS:
6154                 case OP_CVTPS2PD:
6155                 case OP_CVTTPD2DQ:
6156                 case OP_CVTTPS2DQ:
6157                 case OP_EXTRACT_MASK:
6158                 case OP_SQRTPS:
6159                 case OP_SQRTPD:
6160                 case OP_RSQRTPS:
6161                 case OP_RCPPS: {
6162                         LLVMValueRef v;
6163
6164                         v = convert (ctx, values [ins->sreg1], simd_op_to_llvm_type (ins->opcode));
6165
6166                         values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, simd_op_to_intrins (ins->opcode)), &v, 1, dname);
6167                         break;
6168                 }
6169                 case OP_COMPPS:
6170                 case OP_COMPPD: {
6171                         LLVMRealPredicate op;
6172
6173                         switch (ins->inst_c0) {
6174                         case SIMD_COMP_EQ:
6175                                 op = LLVMRealOEQ;
6176                                 break;
6177                         case SIMD_COMP_LT:
6178                                 op = LLVMRealOLT;
6179                                 break;
6180                         case SIMD_COMP_LE:
6181                                 op = LLVMRealOLE;
6182                                 break;
6183                         case SIMD_COMP_UNORD:
6184                                 op = LLVMRealUNO;
6185                                 break;
6186                         case SIMD_COMP_NEQ:
6187                                 op = LLVMRealUNE;
6188                                 break;
6189                         case SIMD_COMP_NLT:
6190                                 op = LLVMRealUGE;
6191                                 break;
6192                         case SIMD_COMP_NLE:
6193                                 op = LLVMRealUGT;
6194                                 break;
6195                         case SIMD_COMP_ORD:
6196                                 op = LLVMRealORD;
6197                                 break;
6198                         default:
6199                                 g_assert_not_reached ();
6200                         }
6201
6202                         LLVMValueRef cmp = LLVMBuildFCmp (builder, op, lhs, rhs, "");
6203                         if (ins->opcode == OP_COMPPD)
6204                                 values [ins->dreg] = LLVMBuildBitCast (builder, LLVMBuildSExt (builder, cmp, LLVMVectorType (LLVMInt64Type (), 2), ""), LLVMTypeOf (lhs), "");
6205                         else
6206                                 values [ins->dreg] = LLVMBuildBitCast (builder, LLVMBuildSExt (builder, cmp, LLVMVectorType (LLVMInt32Type (), 4), ""), LLVMTypeOf (lhs), "");
6207                         break;
6208                 }
6209                 case OP_ICONV_TO_X:
6210                         /* This is only used for implementing shifts by non-immediate */
6211                         values [ins->dreg] = lhs;
6212                         break;
6213
6214                 case OP_PSHRW:
6215                 case OP_PSHRD:
6216                 case OP_PSHRQ:
6217                 case OP_PSARW:
6218                 case OP_PSARD:
6219                 case OP_PSHLW:
6220                 case OP_PSHLD:
6221                 case OP_PSHLQ: {
6222                         LLVMValueRef args [3];
6223
6224                         args [0] = lhs;
6225                         args [1] = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
6226
6227                         values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, simd_op_to_intrins (ins->opcode)), args, 2, dname);
6228                         break;
6229                 }
6230
6231                 case OP_PSHRW_REG:
6232                 case OP_PSHRD_REG:
6233                 case OP_PSHRQ_REG:
6234                 case OP_PSARW_REG:
6235                 case OP_PSARD_REG:
6236                 case OP_PSHLW_REG:
6237                 case OP_PSHLD_REG:
6238                 case OP_PSHLQ_REG: {
6239                         LLVMValueRef args [3];
6240
6241                         args [0] = lhs;
6242                         args [1] = values [ins->sreg2];
6243
6244                         values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, simd_op_to_intrins (ins->opcode)), args, 2, dname);
6245                         break;
6246                 }
6247
6248                 case OP_SHUFPS:
6249                 case OP_SHUFPD:
6250                 case OP_PSHUFLED:
6251                 case OP_PSHUFLEW_LOW:
6252                 case OP_PSHUFLEW_HIGH: {
6253                         int mask [16];
6254                         LLVMValueRef v1 = NULL, v2 = NULL, mask_values [16];
6255                         int i, mask_size = 0;
6256                         int imask = ins->inst_c0;
6257         
6258                         /* Convert the x86 shuffle mask to LLVM's */
6259                         switch (ins->opcode) {
6260                         case OP_SHUFPS:
6261                                 mask_size = 4;
6262                                 mask [0] = ((imask >> 0) & 3);
6263                                 mask [1] = ((imask >> 2) & 3);
6264                                 mask [2] = ((imask >> 4) & 3) + 4;
6265                                 mask [3] = ((imask >> 6) & 3) + 4;
6266                                 v1 = values [ins->sreg1];
6267                                 v2 = values [ins->sreg2];
6268                                 break;
6269                         case OP_SHUFPD:
6270                                 mask_size = 2;
6271                                 mask [0] = ((imask >> 0) & 1);
6272                                 mask [1] = ((imask >> 1) & 1) + 2;
6273                                 v1 = values [ins->sreg1];
6274                                 v2 = values [ins->sreg2];
6275                                 break;
6276                         case OP_PSHUFLEW_LOW:
6277                                 mask_size = 8;
6278                                 mask [0] = ((imask >> 0) & 3);
6279                                 mask [1] = ((imask >> 2) & 3);
6280                                 mask [2] = ((imask >> 4) & 3);
6281                                 mask [3] = ((imask >> 6) & 3);
6282                                 mask [4] = 4 + 0;
6283                                 mask [5] = 4 + 1;
6284                                 mask [6] = 4 + 2;
6285                                 mask [7] = 4 + 3;
6286                                 v1 = values [ins->sreg1];
6287                                 v2 = LLVMGetUndef (LLVMTypeOf (v1));
6288                                 break;
6289                         case OP_PSHUFLEW_HIGH:
6290                                 mask_size = 8;
6291                                 mask [0] = 0;
6292                                 mask [1] = 1;
6293                                 mask [2] = 2;
6294                                 mask [3] = 3;
6295                                 mask [4] = 4 + ((imask >> 0) & 3);
6296                                 mask [5] = 4 + ((imask >> 2) & 3);
6297                                 mask [6] = 4 + ((imask >> 4) & 3);
6298                                 mask [7] = 4 + ((imask >> 6) & 3);
6299                                 v1 = values [ins->sreg1];
6300                                 v2 = LLVMGetUndef (LLVMTypeOf (v1));
6301                                 break;
6302                         case OP_PSHUFLED:
6303                                 mask_size = 4;
6304                                 mask [0] = ((imask >> 0) & 3);
6305                                 mask [1] = ((imask >> 2) & 3);
6306                                 mask [2] = ((imask >> 4) & 3);
6307                                 mask [3] = ((imask >> 6) & 3);
6308                                 v1 = values [ins->sreg1];
6309                                 v2 = LLVMGetUndef (LLVMTypeOf (v1));
6310                                 break;
6311                         default:
6312                                 g_assert_not_reached ();
6313                         }
6314                         for (i = 0; i < mask_size; ++i)
6315                                 mask_values [i] = LLVMConstInt (LLVMInt32Type (), mask [i], FALSE);
6316
6317                         values [ins->dreg] =
6318                                 LLVMBuildShuffleVector (builder, v1, v2,
6319                                                                                 LLVMConstVector (mask_values, mask_size), dname);
6320                         break;
6321                 }
6322
6323                 case OP_UNPACK_LOWB:
6324                 case OP_UNPACK_LOWW:
6325                 case OP_UNPACK_LOWD:
6326                 case OP_UNPACK_LOWQ:
6327                 case OP_UNPACK_LOWPS:
6328                 case OP_UNPACK_LOWPD:
6329                 case OP_UNPACK_HIGHB:
6330                 case OP_UNPACK_HIGHW:
6331                 case OP_UNPACK_HIGHD:
6332                 case OP_UNPACK_HIGHQ:
6333                 case OP_UNPACK_HIGHPS:
6334                 case OP_UNPACK_HIGHPD: {
6335                         int mask [16];
6336                         LLVMValueRef mask_values [16];
6337                         int i, mask_size = 0;
6338                         gboolean low = FALSE;
6339
6340                         switch (ins->opcode) {
6341                         case OP_UNPACK_LOWB:
6342                                 mask_size = 16;
6343                                 low = TRUE;
6344                                 break;
6345                         case OP_UNPACK_LOWW:
6346                                 mask_size = 8;
6347                                 low = TRUE;
6348                                 break;
6349                         case OP_UNPACK_LOWD:
6350                         case OP_UNPACK_LOWPS:
6351                                 mask_size = 4;
6352                                 low = TRUE;
6353                                 break;
6354                         case OP_UNPACK_LOWQ:
6355                         case OP_UNPACK_LOWPD:
6356                                 mask_size = 2;
6357                                 low = TRUE;
6358                                 break;
6359                         case OP_UNPACK_HIGHB:
6360                                 mask_size = 16;
6361                                 break;
6362                         case OP_UNPACK_HIGHW:
6363                                 mask_size = 8;
6364                                 break;
6365                         case OP_UNPACK_HIGHD:
6366                         case OP_UNPACK_HIGHPS:
6367                                 mask_size = 4;
6368                                 break;
6369                         case OP_UNPACK_HIGHQ:
6370                         case OP_UNPACK_HIGHPD:
6371                                 mask_size = 2;
6372                                 break;
6373                         default:
6374                                 g_assert_not_reached ();
6375                         }
6376
6377                         if (low) {
6378                                 for (i = 0; i < (mask_size / 2); ++i) {
6379                                         mask [(i * 2)] = i;
6380                                         mask [(i * 2) + 1] = mask_size + i;
6381                                 }
6382                         } else {
6383                                 for (i = 0; i < (mask_size / 2); ++i) {
6384                                         mask [(i * 2)] = (mask_size / 2) + i;
6385                                         mask [(i * 2) + 1] = mask_size + (mask_size / 2) + i;
6386                                 }
6387                         }
6388
6389                         for (i = 0; i < mask_size; ++i)
6390                                 mask_values [i] = LLVMConstInt (LLVMInt32Type (), mask [i], FALSE);
6391                         
6392                         values [ins->dreg] =
6393                                 LLVMBuildShuffleVector (builder, values [ins->sreg1], values [ins->sreg2],
6394                                                                                 LLVMConstVector (mask_values, mask_size), dname);
6395                         break;
6396                 }
6397
6398                 case OP_DUPPD: {
6399                         LLVMTypeRef t = simd_op_to_llvm_type (ins->opcode);
6400                         LLVMValueRef v, val;
6401
6402                         v = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
6403                         val = LLVMConstNull (t);
6404                         val = LLVMBuildInsertElement (builder, val, v, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
6405                         val = LLVMBuildInsertElement (builder, val, v, LLVMConstInt (LLVMInt32Type (), 1, FALSE), dname);
6406
6407                         values [ins->dreg] = val;
6408                         break;
6409                 }
6410                 case OP_DUPPS_LOW:
6411                 case OP_DUPPS_HIGH: {
6412                         LLVMTypeRef t = simd_op_to_llvm_type (ins->opcode);
6413                         LLVMValueRef v1, v2, val;
6414                         
6415
6416                         if (ins->opcode == OP_DUPPS_LOW) {
6417                                 v1 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
6418                                 v2 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 2, FALSE), "");
6419                         } else {
6420                                 v1 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 1, FALSE), "");
6421                                 v2 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 3, FALSE), "");
6422                         }
6423                         val = LLVMConstNull (t);
6424                         val = LLVMBuildInsertElement (builder, val, v1, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
6425                         val = LLVMBuildInsertElement (builder, val, v1, LLVMConstInt (LLVMInt32Type (), 1, FALSE), "");
6426                         val = LLVMBuildInsertElement (builder, val, v2, LLVMConstInt (LLVMInt32Type (), 2, FALSE), "");
6427                         val = LLVMBuildInsertElement (builder, val, v2, LLVMConstInt (LLVMInt32Type (), 3, FALSE), "");
6428                         
6429                         values [ins->dreg] = val;
6430                         break;
6431                 }
6432
6433                 case OP_DPPS: {
6434                         LLVMValueRef args [3];
6435
6436                         args [0] = lhs;
6437                         args [1] = rhs;
6438                         /* 0xf1 == multiply all 4 elements, add them together, and store the result to the lowest element */
6439                         args [2] = LLVMConstInt (LLVMInt32Type (), 0xf1, FALSE);
6440
6441                         values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, simd_op_to_intrins (ins->opcode)), args, 3, dname);
6442                         break;
6443                 }
6444
6445 #endif /* SIMD */
6446
6447                 case OP_DUMMY_USE:
6448                         break;
6449
6450                         /*
6451                          * EXCEPTION HANDLING
6452                          */
6453                 case OP_IMPLICIT_EXCEPTION:
6454                         /* This marks a place where an implicit exception can happen */
6455                         if (bb->region != -1)
6456                                 set_failure (ctx, "implicit-exception");
6457                         break;
6458                 case OP_THROW:
6459                 case OP_RETHROW: {
6460                         gboolean rethrow = (ins->opcode == OP_RETHROW);
6461                         if (ctx->llvm_only) {
6462                                 emit_llvmonly_throw (ctx, bb, rethrow, lhs);
6463                                 has_terminator = TRUE;
6464                                 ctx->unreachable [bb->block_num] = TRUE;
6465                         } else {
6466                                 emit_throw (ctx, bb, rethrow, lhs);
6467                                 builder = ctx->builder;
6468                         }
6469                         break;
6470                 }
6471                 case OP_CALL_HANDLER: {
6472                         /* 
6473                          * We don't 'call' handlers, but instead simply branch to them.
6474                          * The code generated by ENDFINALLY will branch back to us.
6475                          */
6476                         LLVMBasicBlockRef noex_bb;
6477                         GSList *bb_list;
6478                         BBInfo *info = &bblocks [ins->inst_target_bb->block_num];
6479
6480                         bb_list = info->call_handler_return_bbs;
6481
6482                         /* 
6483                          * Set the indicator variable for the finally clause.
6484                          */
6485                         lhs = info->finally_ind;
6486                         g_assert (lhs);
6487                         LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), g_slist_length (bb_list) + 1, FALSE), lhs);
6488                                 
6489                         /* Branch to the finally clause */
6490                         LLVMBuildBr (builder, info->call_handler_target_bb);
6491
6492                         noex_bb = gen_bb (ctx, "CALL_HANDLER_CONT_BB");
6493                         info->call_handler_return_bbs = g_slist_append_mempool (cfg->mempool, info->call_handler_return_bbs, noex_bb);
6494
6495                         builder = ctx->builder = create_builder (ctx);
6496                         LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
6497
6498                         bblocks [bb->block_num].end_bblock = noex_bb;
6499                         break;
6500                 }
6501                 case OP_START_HANDLER: {
6502                         break;
6503                 }
6504                 case OP_ENDFINALLY: {
6505                         LLVMBasicBlockRef resume_bb;
6506                         MonoBasicBlock *handler_bb;
6507                         LLVMValueRef val, switch_ins, callee;
6508                         GSList *bb_list;
6509                         BBInfo *info;
6510
6511                         handler_bb = (MonoBasicBlock*)g_hash_table_lookup (ctx->region_to_handler, GUINT_TO_POINTER (mono_get_block_region_notry (cfg, bb->region)));
6512                         g_assert (handler_bb);
6513                         info = &bblocks [handler_bb->block_num];
6514                         lhs = info->finally_ind;
6515                         g_assert (lhs);
6516
6517                         bb_list = info->call_handler_return_bbs;
6518
6519                         resume_bb = gen_bb (ctx, "ENDFINALLY_RESUME_BB");
6520
6521                         /* Load the finally variable */
6522                         val = LLVMBuildLoad (builder, lhs, "");
6523
6524                         /* Reset the variable */
6525                         LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), lhs);
6526
6527                         /* Branch to either resume_bb, or to the bblocks in bb_list */
6528                         switch_ins = LLVMBuildSwitch (builder, val, resume_bb, g_slist_length (bb_list));
6529                         /* 
6530                          * The other targets are added at the end to handle OP_CALL_HANDLER
6531                          * opcodes processed later.
6532                          */
6533                         info->endfinally_switch_ins_list = g_slist_append_mempool (cfg->mempool, info->endfinally_switch_ins_list, switch_ins);
6534
6535                         builder = ctx->builder = create_builder (ctx);
6536                         LLVMPositionBuilderAtEnd (ctx->builder, resume_bb);
6537
6538                         if (ctx->llvm_only) {
6539                                 emit_resume_eh (ctx, bb);
6540                         } else {
6541                                 if (ctx->cfg->compile_aot) {
6542                                         callee = get_callee (ctx, LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE), MONO_PATCH_INFO_INTERNAL_METHOD, "llvm_resume_unwind_trampoline");
6543                                 } else {
6544 #if LLVM_API_VERSION > 100
6545                                         MonoJitICallInfo *info;
6546
6547                                         info = mono_find_jit_icall_by_name ("llvm_resume_unwind_trampoline");
6548                                         g_assert (info);
6549                                         gpointer target = (void*)info->func;
6550                                         LLVMTypeRef icall_sig = LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE);
6551                                         callee = emit_jit_callee (ctx, "llvm_resume_unwind_trampoline", icall_sig, target);
6552 #else
6553                                         callee = LLVMGetNamedFunction (ctx->lmodule, "llvm_resume_unwind_trampoline");
6554 #endif
6555                                 }
6556                                 LLVMBuildCall (builder, callee, NULL, 0, "");
6557                                 LLVMBuildUnreachable (builder);
6558                         }
6559
6560                         has_terminator = TRUE;
6561                         break;
6562                 }
6563                 case OP_IL_SEQ_POINT:
6564                         break;
6565                 default: {
6566                         char reason [128];
6567
6568                         sprintf (reason, "opcode %s", mono_inst_name (ins->opcode));
6569                         set_failure (ctx, reason);
6570                         break;
6571                 }
6572                 }
6573
6574                 if (!ctx_ok (ctx))
6575                         break;
6576
6577                 /* Convert the value to the type required by phi nodes */
6578                 if (spec [MONO_INST_DEST] != ' ' && !MONO_IS_STORE_MEMBASE (ins) && ctx->vreg_types [ins->dreg]) {
6579                         if (ctx->is_vphi [ins->dreg])
6580                                 /* vtypes */
6581                                 values [ins->dreg] = addresses [ins->dreg];
6582                         else
6583                                 values [ins->dreg] = convert (ctx, values [ins->dreg], ctx->vreg_types [ins->dreg]);
6584                 }
6585
6586                 /* Add stores for volatile variables */
6587                 if (spec [MONO_INST_DEST] != ' ' && spec [MONO_INST_DEST] != 'v' && !MONO_IS_STORE_MEMBASE (ins))
6588                         emit_volatile_store (ctx, ins->dreg);
6589         }
6590
6591         if (!ctx_ok (ctx))
6592                 return;
6593
6594         if (!has_terminator && bb->next_bb && (bb == cfg->bb_entry || bb->in_count > 0)) {
6595                 LLVMBuildBr (builder, get_bb (ctx, bb->next_bb));
6596         }
6597
6598         if (bb == cfg->bb_exit && sig->ret->type == MONO_TYPE_VOID) {
6599                 emit_dbg_loc (ctx, builder, cfg->header->code + cfg->header->code_size - 1);
6600                 LLVMBuildRetVoid (builder);
6601         }
6602
6603         if (bb == cfg->bb_entry)
6604                 ctx->last_alloca = LLVMGetLastInstruction (get_bb (ctx, cfg->bb_entry));
6605 }
6606
6607 /*
6608  * mono_llvm_check_method_supported:
6609  *
6610  *   Do some quick checks to decide whenever cfg->method can be compiled by LLVM, to avoid
6611  * compiling a method twice.
6612  */
6613 void
6614 mono_llvm_check_method_supported (MonoCompile *cfg)
6615 {
6616         int i, j;
6617
6618         if (cfg->llvm_only)
6619                 return;
6620
6621         if (cfg->method->save_lmf) {
6622                 cfg->exception_message = g_strdup ("lmf");
6623                 cfg->disable_llvm = TRUE;
6624         }
6625         if (cfg->disable_llvm)
6626                 return;
6627
6628         /*
6629          * Nested clauses where one of the clauses is a finally clause is
6630          * not supported, because LLVM can't figure out the control flow,
6631          * probably because we resume exception handling by calling our
6632          * own function instead of using the 'resume' llvm instruction.
6633          */
6634         for (i = 0; i < cfg->header->num_clauses; ++i) {
6635                 for (j = 0; j < cfg->header->num_clauses; ++j) {
6636                         MonoExceptionClause *clause1 = &cfg->header->clauses [i];
6637                         MonoExceptionClause *clause2 = &cfg->header->clauses [j];
6638
6639                         // FIXME: Nested try clauses fail in some cases too, i.e. #37273
6640                         if (i != j && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset) {
6641                                 //(clause1->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause2->flags == MONO_EXCEPTION_CLAUSE_FINALLY)) {
6642                                 cfg->exception_message = g_strdup ("nested clauses");
6643                                 cfg->disable_llvm = TRUE;
6644                                 break;
6645                         }
6646                 }
6647         }
6648         if (cfg->disable_llvm)
6649                 return;
6650
6651         /* FIXME: */
6652         if (cfg->method->dynamic) {
6653                 cfg->exception_message = g_strdup ("dynamic.");
6654                 cfg->disable_llvm = TRUE;
6655         }
6656         if (cfg->disable_llvm)
6657                 return;
6658 }
6659
6660 static LLVMCallInfo*
6661 get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
6662 {
6663         LLVMCallInfo *linfo;
6664         int i;
6665
6666         if (cfg->gsharedvt && cfg->llvm_only && mini_is_gsharedvt_variable_signature (sig)) {
6667                 int i, n, pindex;
6668
6669                 /*
6670                  * Gsharedvt methods have the following calling convention:
6671                  * - all arguments are passed by ref, even non generic ones
6672                  * - the return value is returned by ref too, using a vret
6673                  *   argument passed after 'this'.
6674                  */
6675                 n = sig->param_count + sig->hasthis;
6676                 linfo = (LLVMCallInfo*)mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
6677
6678                 pindex = 0;
6679                 if (sig->hasthis)
6680                         linfo->args [pindex ++].storage = LLVMArgNormal;
6681
6682                 if (sig->ret->type != MONO_TYPE_VOID) {
6683                         if (mini_is_gsharedvt_variable_type (sig->ret))
6684                                 linfo->ret.storage = LLVMArgGsharedvtVariable;
6685                         else if (mini_type_is_vtype (sig->ret))
6686                                 linfo->ret.storage = LLVMArgGsharedvtFixedVtype;
6687                         else
6688                                 linfo->ret.storage = LLVMArgGsharedvtFixed;
6689                         linfo->vret_arg_index = pindex;
6690                 } else {
6691                         linfo->ret.storage = LLVMArgNone;
6692                 }
6693
6694                 for (i = 0; i < sig->param_count; ++i) {
6695                         if (sig->params [i]->byref)
6696                                 linfo->args [pindex].storage = LLVMArgNormal;
6697                         else if (mini_is_gsharedvt_variable_type (sig->params [i]))
6698                                 linfo->args [pindex].storage = LLVMArgGsharedvtVariable;
6699                         else if (mini_type_is_vtype (sig->params [i]))
6700                                 linfo->args [pindex].storage = LLVMArgGsharedvtFixedVtype;
6701                         else
6702                                 linfo->args [pindex].storage = LLVMArgGsharedvtFixed;
6703                         linfo->args [pindex].type = sig->params [i];
6704                         pindex ++;
6705                 }
6706                 return linfo;
6707         }
6708
6709
6710         linfo = mono_arch_get_llvm_call_info (cfg, sig);
6711         for (i = 0; i < sig->param_count; ++i)
6712                 linfo->args [i + sig->hasthis].type = sig->params [i];
6713
6714         return linfo;
6715 }
6716
6717 static void
6718 emit_method_inner (EmitContext *ctx);
6719
6720 static void
6721 free_ctx (EmitContext *ctx)
6722 {
6723         GSList *l;
6724
6725         g_free (ctx->values);
6726         g_free (ctx->addresses);
6727         g_free (ctx->vreg_types);
6728         g_free (ctx->is_vphi);
6729         g_free (ctx->vreg_cli_types);
6730         g_free (ctx->is_dead);
6731         g_free (ctx->unreachable);
6732         g_ptr_array_free (ctx->phi_values, TRUE);
6733         g_free (ctx->bblocks);
6734         g_hash_table_destroy (ctx->region_to_handler);
6735         g_hash_table_destroy (ctx->clause_to_handler);
6736         g_hash_table_destroy (ctx->jit_callees);
6737
6738         GHashTableIter iter;
6739         g_hash_table_iter_init (&iter, ctx->method_to_callers);
6740         while (g_hash_table_iter_next (&iter, NULL, (gpointer)&l))
6741                 g_slist_free (l);
6742
6743         g_hash_table_destroy (ctx->method_to_callers);
6744
6745         g_free (ctx->method_name);
6746         g_ptr_array_free (ctx->bblock_list, TRUE);
6747
6748         for (l = ctx->builders; l; l = l->next) {
6749                 LLVMBuilderRef builder = (LLVMBuilderRef)l->data;
6750                 LLVMDisposeBuilder (builder);
6751         }
6752
6753         g_free (ctx);
6754 }
6755
6756 /*
6757  * mono_llvm_emit_method:
6758  *
6759  *   Emit LLVM IL from the mono IL, and compile it to native code using LLVM.
6760  */
6761 void
6762 mono_llvm_emit_method (MonoCompile *cfg)
6763 {
6764         EmitContext *ctx;
6765         char *method_name;
6766         gboolean is_linkonce = FALSE;
6767         int i;
6768
6769         /* The code below might acquire the loader lock, so use it for global locking */
6770         mono_loader_lock ();
6771
6772         /* Used to communicate with the callbacks */
6773         mono_native_tls_set_value (current_cfg_tls_id, cfg);
6774
6775         ctx = g_new0 (EmitContext, 1);
6776         ctx->cfg = cfg;
6777         ctx->mempool = cfg->mempool;
6778
6779         /*
6780          * This maps vregs to the LLVM instruction defining them
6781          */
6782         ctx->values = g_new0 (LLVMValueRef, cfg->next_vreg);
6783         /*
6784          * This maps vregs for volatile variables to the LLVM instruction defining their
6785          * address.
6786          */
6787         ctx->addresses = g_new0 (LLVMValueRef, cfg->next_vreg);
6788         ctx->vreg_types = g_new0 (LLVMTypeRef, cfg->next_vreg);
6789         ctx->is_vphi = g_new0 (gboolean, cfg->next_vreg);
6790         ctx->vreg_cli_types = g_new0 (MonoType*, cfg->next_vreg);
6791         ctx->phi_values = g_ptr_array_sized_new (256);
6792         /* 
6793          * This signals whenever the vreg was defined by a phi node with no input vars
6794          * (i.e. all its input bblocks end with NOT_REACHABLE).
6795          */
6796         ctx->is_dead = g_new0 (gboolean, cfg->next_vreg);
6797         /* Whenever the bblock is unreachable */
6798         ctx->unreachable = g_new0 (gboolean, cfg->max_block_num);
6799         ctx->bblock_list = g_ptr_array_sized_new (256);
6800
6801         ctx->region_to_handler = g_hash_table_new (NULL, NULL);
6802         ctx->clause_to_handler = g_hash_table_new (NULL, NULL);
6803         ctx->method_to_callers = g_hash_table_new (NULL, NULL);
6804         ctx->jit_callees = g_hash_table_new (NULL, NULL);
6805         if (cfg->compile_aot) {
6806                 ctx->module = &aot_module;
6807
6808                 method_name = NULL;
6809                 /*
6810                  * Allow the linker to discard duplicate copies of wrappers, generic instances etc. by using the 'linkonce'
6811                  * linkage for them. This requires the following:
6812                  * - the method needs to have a unique mangled name
6813                  * - llvmonly mode, since the code in aot-runtime.c would initialize got slots in the wrong aot image etc.
6814                  */
6815                 is_linkonce = ctx->module->llvm_only && ctx->module->static_link && mono_aot_is_linkonce_method (cfg->method);
6816                 if (is_linkonce) {
6817                         method_name = mono_aot_get_mangled_method_name (cfg->method);
6818                         if (!method_name)
6819                                 is_linkonce = FALSE;
6820                         /*
6821                         if (method_name)
6822                                 printf ("%s %s\n", mono_method_full_name (cfg->method, 1), method_name);
6823                         else
6824                                 printf ("%s\n", mono_method_full_name (cfg->method, 1));
6825                         */
6826                 }
6827                 if (!method_name)
6828                         method_name = mono_aot_get_method_name (cfg);
6829                 cfg->llvm_method_name = g_strdup (method_name);
6830         } else {
6831                 init_jit_module (cfg->domain);
6832                 ctx->module = (MonoLLVMModule*)domain_jit_info (cfg->domain)->llvm_module;
6833                 method_name = mono_method_full_name (cfg->method, TRUE);
6834         }
6835         ctx->method_name = method_name;
6836         ctx->is_linkonce = is_linkonce;
6837
6838 #if LLVM_API_VERSION > 100
6839         if (cfg->compile_aot)
6840                 ctx->lmodule = ctx->module->lmodule;
6841         else
6842                 ctx->lmodule = LLVMModuleCreateWithName ("jit-module");
6843 #else
6844         ctx->lmodule = ctx->module->lmodule;
6845 #endif
6846         ctx->llvm_only = ctx->module->llvm_only;
6847
6848         emit_method_inner (ctx);
6849
6850         if (!ctx_ok (ctx)) {
6851                 if (ctx->lmethod) {
6852                         /* Need to add unused phi nodes as they can be referenced by other values */
6853                         LLVMBasicBlockRef phi_bb = LLVMAppendBasicBlock (ctx->lmethod, "PHI_BB");
6854                         LLVMBuilderRef builder;
6855
6856                         builder = create_builder (ctx);
6857                         LLVMPositionBuilderAtEnd (builder, phi_bb);
6858
6859                         for (i = 0; i < ctx->phi_values->len; ++i) {
6860                                 LLVMValueRef v = (LLVMValueRef)g_ptr_array_index (ctx->phi_values, i);
6861                                 if (LLVMGetInstructionParent (v) == NULL)
6862                                         LLVMInsertIntoBuilder (builder, v);
6863                         }
6864                 
6865                         LLVMDeleteFunction (ctx->lmethod);
6866                 }
6867         }
6868
6869         free_ctx (ctx);
6870
6871         mono_native_tls_set_value (current_cfg_tls_id, NULL);
6872
6873         mono_loader_unlock ();
6874 }
6875
6876 static void
6877 emit_method_inner (EmitContext *ctx)
6878 {
6879         MonoCompile *cfg = ctx->cfg;
6880         MonoMethodSignature *sig;
6881         MonoBasicBlock *bb;
6882         LLVMTypeRef method_type;
6883         LLVMValueRef method = NULL;
6884         LLVMValueRef *values = ctx->values;
6885         int i, max_block_num, bb_index;
6886         gboolean last = FALSE;
6887         LLVMCallInfo *linfo;
6888         LLVMModuleRef lmodule = ctx->lmodule;
6889         BBInfo *bblocks;
6890         GPtrArray *bblock_list = ctx->bblock_list;
6891         MonoMethodHeader *header;
6892         MonoExceptionClause *clause;
6893         char **names;
6894
6895         if (cfg->gsharedvt && !cfg->llvm_only) {
6896                 set_failure (ctx, "gsharedvt");
6897                 return;
6898         }
6899
6900 #if 1
6901         {
6902                 static int count = 0;
6903                 count ++;
6904
6905                 if (g_getenv ("LLVM_COUNT")) {
6906                         if (count == atoi (g_getenv ("LLVM_COUNT"))) {
6907                                 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
6908                                 fflush (stdout);
6909                                 last = TRUE;
6910                         }
6911                         if (count > atoi (g_getenv ("LLVM_COUNT"))) {
6912                                 set_failure (ctx, "count");
6913                                 return;
6914                         }
6915                 }
6916         }
6917 #endif
6918
6919         sig = mono_method_signature (cfg->method);
6920         ctx->sig = sig;
6921
6922         linfo = get_llvm_call_info (cfg, sig);
6923         ctx->linfo = linfo;
6924         if (!ctx_ok (ctx))
6925                 return;
6926
6927         if (cfg->rgctx_var)
6928                 linfo->rgctx_arg = TRUE;
6929         ctx->method_type = method_type = sig_to_llvm_sig_full (ctx, sig, linfo);
6930         if (!ctx_ok (ctx))
6931                 return;
6932
6933         method = LLVMAddFunction (lmodule, ctx->method_name, method_type);
6934         ctx->lmethod = method;
6935
6936         if (!cfg->llvm_only)
6937                 LLVMSetFunctionCallConv (method, LLVMMono1CallConv);
6938         LLVMSetLinkage (method, LLVMPrivateLinkage);
6939
6940         LLVMAddFunctionAttr (method, LLVMUWTable);
6941
6942         if (cfg->compile_aot) {
6943                 LLVMSetLinkage (method, LLVMInternalLinkage);
6944                 if (ctx->module->external_symbols) {
6945                         LLVMSetLinkage (method, LLVMExternalLinkage);
6946                         LLVMSetVisibility (method, LLVMHiddenVisibility);
6947                 }
6948                 if (ctx->is_linkonce) {
6949                         LLVMSetLinkage (method, LLVMLinkOnceAnyLinkage);
6950                         LLVMSetVisibility (method, LLVMDefaultVisibility);
6951                 }
6952         } else {
6953 #if LLVM_API_VERSION > 100
6954                 LLVMSetLinkage (method, LLVMExternalLinkage);
6955 #else
6956                 LLVMSetLinkage (method, LLVMPrivateLinkage);
6957 #endif
6958         }
6959
6960         if (cfg->method->save_lmf && !cfg->llvm_only) {
6961                 set_failure (ctx, "lmf");
6962                 return;
6963         }
6964
6965         if (sig->pinvoke && cfg->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && !cfg->llvm_only) {
6966                 set_failure (ctx, "pinvoke signature");
6967                 return;
6968         }
6969
6970         header = cfg->header;
6971         for (i = 0; i < header->num_clauses; ++i) {
6972                 clause = &header->clauses [i];
6973                 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_NONE) {
6974                     set_failure (ctx, "non-finally/catch clause.");
6975                         return;
6976                 }
6977         }
6978         if (header->num_clauses || (cfg->method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) || cfg->no_inline)
6979                 /* We can't handle inlined methods with clauses */
6980                 LLVMAddFunctionAttr (method, LLVMNoInlineAttribute);
6981
6982         if (linfo->rgctx_arg) {
6983                 ctx->rgctx_arg = LLVMGetParam (method, linfo->rgctx_arg_pindex);
6984                 ctx->rgctx_arg_pindex = linfo->rgctx_arg_pindex;
6985                 /*
6986                  * We mark the rgctx parameter with the inreg attribute, which is mapped to
6987                  * MONO_ARCH_RGCTX_REG in the Mono calling convention in llvm, i.e.
6988                  * CC_X86_64_Mono in X86CallingConv.td.
6989                  */
6990                 if (!ctx->llvm_only)
6991                         LLVMAddAttribute (ctx->rgctx_arg, LLVMInRegAttribute);
6992                 LLVMSetValueName (ctx->rgctx_arg, "rgctx");
6993         } else {
6994                 ctx->rgctx_arg_pindex = -1;
6995         }
6996         if (cfg->vret_addr) {
6997                 values [cfg->vret_addr->dreg] = LLVMGetParam (method, linfo->vret_arg_pindex);
6998                 LLVMSetValueName (values [cfg->vret_addr->dreg], "vret");
6999                 if (linfo->ret.storage == LLVMArgVtypeByRef) {
7000                         LLVMAddAttribute (LLVMGetParam (method, linfo->vret_arg_pindex), LLVMStructRetAttribute);
7001                         LLVMAddAttribute (LLVMGetParam (method, linfo->vret_arg_pindex), LLVMNoAliasAttribute);
7002                 }
7003         }
7004
7005         if (sig->hasthis) {
7006                 ctx->this_arg_pindex = linfo->this_arg_pindex;
7007                 ctx->this_arg = LLVMGetParam (method, linfo->this_arg_pindex);
7008                 values [cfg->args [0]->dreg] = ctx->this_arg;
7009                 LLVMSetValueName (values [cfg->args [0]->dreg], "this");
7010         }
7011
7012         names = g_new (char *, sig->param_count);
7013         mono_method_get_param_names (cfg->method, (const char **) names);
7014
7015         /* Set parameter names/attributes */
7016         for (i = 0; i < sig->param_count; ++i) {
7017                 LLVMArgInfo *ainfo = &linfo->args [i + sig->hasthis];
7018                 char *name;
7019                 int pindex = ainfo->pindex + ainfo->ndummy_fpargs;
7020                 int j;
7021
7022                 for (j = 0; j < ainfo->ndummy_fpargs; ++j) {
7023                         name = g_strdup_printf ("dummy_%d_%d", i, j);
7024                         LLVMSetValueName (LLVMGetParam (method, ainfo->pindex + j), name);
7025                         g_free (name);
7026                 }
7027
7028                 if (ainfo->storage == LLVMArgVtypeInReg && ainfo->pair_storage [0] == LLVMArgNone && ainfo->pair_storage [1] == LLVMArgNone)
7029                         continue;
7030
7031                 values [cfg->args [i + sig->hasthis]->dreg] = LLVMGetParam (method, pindex);
7032                 if (ainfo->storage == LLVMArgGsharedvtFixed || ainfo->storage == LLVMArgGsharedvtFixedVtype) {
7033                         if (names [i] && names [i][0] != '\0')
7034                                 name = g_strdup_printf ("p_arg_%s", names [i]);
7035                         else
7036                                 name = g_strdup_printf ("p_arg_%d", i);
7037                 } else {
7038                         if (names [i] && names [i][0] != '\0')
7039                                 name = g_strdup_printf ("arg_%s", names [i]);
7040                         else
7041                                 name = g_strdup_printf ("arg_%d", i);
7042                 }
7043                 LLVMSetValueName (values [cfg->args [i + sig->hasthis]->dreg], name);
7044                 g_free (name);
7045                 if (ainfo->storage == LLVMArgVtypeByVal)
7046                         LLVMAddAttribute (LLVMGetParam (method, pindex), LLVMByValAttribute);
7047
7048                 if (ainfo->storage == LLVMArgVtypeByRef) {
7049                         /* For OP_LDADDR */
7050                         cfg->args [i + sig->hasthis]->opcode = OP_VTARG_ADDR;
7051                 }
7052         }
7053         g_free (names);
7054
7055         if (ctx->module->emit_dwarf && cfg->compile_aot && mono_debug_enabled ()) {
7056                 ctx->minfo = mono_debug_lookup_method (cfg->method);
7057                 ctx->dbg_md = emit_dbg_subprogram (ctx, cfg, method, ctx->method_name);
7058         }
7059
7060         max_block_num = 0;
7061         for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
7062                 max_block_num = MAX (max_block_num, bb->block_num);
7063         ctx->bblocks = bblocks = g_new0 (BBInfo, max_block_num + 1);
7064
7065         /* Add branches between non-consecutive bblocks */
7066         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7067                 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
7068                         bb->next_bb != bb->last_ins->inst_false_bb) {
7069                         
7070                         MonoInst *inst = (MonoInst*)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7071                         inst->opcode = OP_BR;
7072                         inst->inst_target_bb = bb->last_ins->inst_false_bb;
7073                         mono_bblock_add_inst (bb, inst);
7074                 }
7075         }
7076
7077         /*
7078          * The INDIRECT flag added by OP_LDADDR inhibits optimizations, even if the LDADDR
7079          * was later optimized away, so clear these flags, and add them back for the still
7080          * present OP_LDADDR instructions.
7081          */
7082         for (i = 0; i < cfg->next_vreg; ++i) {
7083                 MonoInst *ins;
7084
7085                 ins = get_vreg_to_inst (cfg, i);
7086                 if (ins && ins != cfg->rgctx_var)
7087                         ins->flags &= ~MONO_INST_INDIRECT;
7088         }
7089
7090         /*
7091          * Make a first pass over the code to precreate PHI nodes/set INDIRECT flags.
7092          */
7093         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7094                 MonoInst *ins;
7095                 LLVMBuilderRef builder;
7096                 char *dname;
7097                 char dname_buf[128];
7098
7099                 builder = create_builder (ctx);
7100
7101                 for (ins = bb->code; ins; ins = ins->next) {
7102                         switch (ins->opcode) {
7103                         case OP_PHI:
7104                         case OP_FPHI:
7105                         case OP_VPHI:
7106                         case OP_XPHI: {
7107                                 LLVMTypeRef phi_type = llvm_type_to_stack_type (cfg, type_to_llvm_type (ctx, &ins->klass->byval_arg));
7108
7109                                 if (!ctx_ok (ctx))
7110                                         return;
7111
7112                                 if (ins->opcode == OP_VPHI) {
7113                                         /* Treat valuetype PHI nodes as operating on the address itself */
7114                                         g_assert (ins->klass);
7115                                         phi_type = LLVMPointerType (type_to_llvm_type (ctx, &ins->klass->byval_arg), 0);
7116                                 }
7117
7118                                 /* 
7119                                  * Have to precreate these, as they can be referenced by
7120                                  * earlier instructions.
7121                                  */
7122                                 sprintf (dname_buf, "t%d", ins->dreg);
7123                                 dname = dname_buf;
7124                                 values [ins->dreg] = LLVMBuildPhi (builder, phi_type, dname);
7125
7126                                 if (ins->opcode == OP_VPHI)
7127                                         ctx->addresses [ins->dreg] = values [ins->dreg];
7128
7129                                 g_ptr_array_add (ctx->phi_values, values [ins->dreg]);
7130
7131                                 /* 
7132                                  * Set the expected type of the incoming arguments since these have
7133                                  * to have the same type.
7134                                  */
7135                                 for (i = 0; i < ins->inst_phi_args [0]; i++) {
7136                                         int sreg1 = ins->inst_phi_args [i + 1];
7137                                         
7138                                         if (sreg1 != -1) {
7139                                                 if (ins->opcode == OP_VPHI)
7140                                                         ctx->is_vphi [sreg1] = TRUE;
7141                                                 ctx->vreg_types [sreg1] = phi_type;
7142                                         }
7143                                 }
7144                                 break;
7145                                 }
7146                         case OP_LDADDR:
7147                                 ((MonoInst*)ins->inst_p0)->flags |= MONO_INST_INDIRECT;
7148                                 break;
7149                         default:
7150                                 break;
7151                         }
7152                 }
7153         }
7154
7155         /* 
7156          * Create an ordering for bblocks, use the depth first order first, then
7157          * put the exception handling bblocks last.
7158          */
7159         for (bb_index = 0; bb_index < cfg->num_bblocks; ++bb_index) {
7160                 bb = cfg->bblocks [bb_index];
7161                 if (!(bb->region != -1 && !MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))) {
7162                         g_ptr_array_add (bblock_list, bb);
7163                         bblocks [bb->block_num].added = TRUE;
7164                 }
7165         }
7166
7167         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7168                 if (!bblocks [bb->block_num].added)
7169                         g_ptr_array_add (bblock_list, bb);
7170         }
7171
7172         /*
7173          * Second pass: generate code.
7174          */
7175         // Emit entry point
7176         LLVMBuilderRef entry_builder = create_builder (ctx);
7177         LLVMBasicBlockRef entry_bb = get_bb (ctx, cfg->bb_entry);
7178         LLVMPositionBuilderAtEnd (entry_builder, entry_bb);
7179         emit_entry_bb (ctx, entry_builder);
7180
7181         // Make landing pads first
7182         ctx->exc_meta = g_hash_table_new_full (NULL, NULL, NULL, NULL);
7183
7184         if (ctx->llvm_only) {
7185                 size_t group_index = 0;
7186                 while (group_index < cfg->header->num_clauses) {
7187                         int count = 0;
7188                         size_t cursor = group_index;
7189                         while (cursor < cfg->header->num_clauses &&
7190                                    CLAUSE_START (&cfg->header->clauses [cursor]) == CLAUSE_START (&cfg->header->clauses [group_index]) &&
7191                                    CLAUSE_END (&cfg->header->clauses [cursor]) == CLAUSE_END (&cfg->header->clauses [group_index])) {
7192                                 count++;
7193                                 cursor++;
7194                         }
7195
7196                         LLVMBasicBlockRef lpad_bb = emit_landing_pad (ctx, group_index, count);
7197                         intptr_t key = CLAUSE_END (&cfg->header->clauses [group_index]);
7198                         g_hash_table_insert (ctx->exc_meta, (gpointer)key, lpad_bb);
7199
7200                         group_index = cursor;
7201                 }
7202         }
7203
7204         for (bb_index = 0; bb_index < bblock_list->len; ++bb_index) {
7205                 bb = (MonoBasicBlock*)g_ptr_array_index (bblock_list, bb_index);
7206
7207                 // Prune unreachable mono BBs.
7208                 if (!(bb == cfg->bb_entry || bb->in_count > 0))
7209                         continue;
7210
7211                 process_bb (ctx, bb);
7212                 if (!ctx_ok (ctx))
7213                         return;
7214         }
7215         g_hash_table_destroy (ctx->exc_meta);
7216
7217         mono_memory_barrier ();
7218
7219         /* Add incoming phi values */
7220         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7221                 GSList *l, *ins_list;
7222
7223                 ins_list = bblocks [bb->block_num].phi_nodes;
7224
7225                 for (l = ins_list; l; l = l->next) {
7226                         PhiNode *node = (PhiNode*)l->data;
7227                         MonoInst *phi = node->phi;
7228                         int sreg1 = node->sreg;
7229                         LLVMBasicBlockRef in_bb;
7230
7231                         if (sreg1 == -1)
7232                                 continue;
7233
7234                         in_bb = get_end_bb (ctx, node->in_bb);
7235
7236                         if (ctx->unreachable [node->in_bb->block_num])
7237                                 continue;
7238
7239                         if (!values [sreg1]) {
7240                                 /* Can happen with values in EH clauses */
7241                                 set_failure (ctx, "incoming phi sreg1");
7242                                 return;
7243                         }
7244
7245                         if (phi->opcode == OP_VPHI) {
7246                                 g_assert (LLVMTypeOf (ctx->addresses [sreg1]) == LLVMTypeOf (values [phi->dreg]));
7247                                 LLVMAddIncoming (values [phi->dreg], &ctx->addresses [sreg1], &in_bb, 1);
7248                         } else {
7249                                 if (LLVMTypeOf (values [sreg1]) != LLVMTypeOf (values [phi->dreg])) {
7250                                         set_failure (ctx, "incoming phi arg type mismatch");
7251                                         return;
7252                                 }
7253                                 g_assert (LLVMTypeOf (values [sreg1]) == LLVMTypeOf (values [phi->dreg]));
7254                                 LLVMAddIncoming (values [phi->dreg], &values [sreg1], &in_bb, 1);
7255                         }
7256                 }
7257         }
7258
7259         /* Nullify empty phi instructions */
7260         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7261                 GSList *l, *ins_list;
7262
7263                 ins_list = bblocks [bb->block_num].phi_nodes;
7264
7265                 for (l = ins_list; l; l = l->next) {
7266                         PhiNode *node = (PhiNode*)l->data;
7267                         MonoInst *phi = node->phi;
7268                         LLVMValueRef phi_ins = values [phi->dreg];
7269
7270                         if (!phi_ins)
7271                                 /* Already removed */
7272                                 continue;
7273
7274                         if (LLVMCountIncoming (phi_ins) == 0) {
7275                                 mono_llvm_replace_uses_of (phi_ins, LLVMConstNull (LLVMTypeOf (phi_ins)));
7276                                 LLVMInstructionEraseFromParent (phi_ins);
7277                                 values [phi->dreg] = NULL;
7278                         }
7279                 }
7280         }
7281
7282         /* Create the SWITCH statements for ENDFINALLY instructions */
7283         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7284                 BBInfo *info = &bblocks [bb->block_num];
7285                 GSList *l;
7286                 for (l = info->endfinally_switch_ins_list; l; l = l->next) {
7287                         LLVMValueRef switch_ins = (LLVMValueRef)l->data;
7288                         GSList *bb_list = info->call_handler_return_bbs;
7289
7290                         GSList *bb_list_iter;
7291                         i = 0;
7292                         for (bb_list_iter = bb_list; bb_list_iter; bb_list_iter = g_slist_next (bb_list_iter)) {
7293                                 LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), i + 1, FALSE), (LLVMBasicBlockRef)bb_list_iter->data);
7294                                 i ++;
7295                         }
7296                 }
7297         }
7298
7299         /* Initialize the method if needed */
7300         if (cfg->compile_aot && ctx->llvm_only) {
7301                 // FIXME: Add more shared got entries
7302                 ctx->builder = create_builder (ctx);
7303                 LLVMPositionBuilderAtEnd (ctx->builder, ctx->init_bb);
7304
7305                 ctx->module->max_method_idx = MAX (ctx->module->max_method_idx, cfg->method_index);
7306
7307                 // FIXME: beforefieldinit
7308                 /*
7309                  * NATIVE_TO_MANAGED methods might be called on a thread not attached to the runtime, so they are initialized when loaded
7310                  * in load_method ().
7311                  */
7312                 if ((ctx->has_got_access || mono_class_get_cctor (cfg->method->klass)) && !(cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)) {
7313                         /*
7314                          * linkonce methods shouldn't have initialization,
7315                          * because they might belong to assemblies which
7316                          * haven't been loaded yet.
7317                          */
7318                         g_assert (!ctx->is_linkonce);
7319                         emit_init_method (ctx);
7320                 } else {
7321                         LLVMBuildBr (ctx->builder, ctx->inited_bb);
7322                 }
7323         }
7324
7325         if (cfg->llvm_only) {
7326                 GHashTableIter iter;
7327                 MonoMethod *method;
7328                 GSList *callers, *l, *l2;
7329
7330                 /*
7331                  * Add the contents of ctx->method_to_callers to module->method_to_callers.
7332                  * We can't do this earlier, as it contains llvm instructions which can be
7333                  * freed if compilation fails.
7334                  * FIXME: Get rid of this when all methods can be llvm compiled.
7335                  */
7336                 g_hash_table_iter_init (&iter, ctx->method_to_callers);
7337                 while (g_hash_table_iter_next (&iter, (void**)&method, (void**)&callers)) {
7338                         for (l = callers; l; l = l->next) {
7339                                 l2 = (GSList*)g_hash_table_lookup (ctx->module->method_to_callers, method);
7340                                 l2 = g_slist_prepend (l2, l->data);
7341                                 g_hash_table_insert (ctx->module->method_to_callers, method, l2);
7342                         }
7343                 }
7344         }
7345
7346         if (cfg->verbose_level > 1)
7347                 mono_llvm_dump_value (method);
7348
7349         if (cfg->compile_aot && !cfg->llvm_only)
7350                 mark_as_used (ctx->module, method);
7351
7352         if (!cfg->llvm_only) {
7353                 LLVMValueRef md_args [16];
7354                 LLVMValueRef md_node;
7355                 int method_index;
7356
7357                 if (cfg->compile_aot)
7358                         method_index = mono_aot_get_method_index (cfg->orig_method);
7359                 else
7360                         method_index = 1;
7361                 md_args [0] = LLVMMDString (ctx->method_name, strlen (ctx->method_name));
7362                 md_args [1] = LLVMConstInt (LLVMInt32Type (), method_index, FALSE);
7363                 md_node = LLVMMDNode (md_args, 2);
7364                 LLVMAddNamedMetadataOperand (lmodule, "mono.function_indexes", md_node);
7365                 //LLVMSetMetadata (method, md_kind, LLVMMDNode (&md_arg, 1));
7366         }
7367
7368         if (cfg->compile_aot) {
7369                 /* Don't generate native code, keep the LLVM IR */
7370                 if (cfg->verbose_level)
7371                         printf ("%s emitted as %s\n", mono_method_full_name (cfg->method, TRUE), ctx->method_name);
7372
7373 #if LLVM_API_VERSION < 100
7374                 /* VerifyFunction can't handle some of the debug info created by DIBuilder in llvm 3.9 */
7375                 int err = LLVMVerifyFunction(ctx->lmethod, LLVMPrintMessageAction);
7376                 g_assert (err == 0);
7377 #endif
7378         } else {
7379                 //LLVMVerifyFunction(method, 0);
7380 #if LLVM_API_VERSION > 100
7381                 MonoDomain *domain = mono_domain_get ();
7382                 MonoJitDomainInfo *domain_info;
7383                 int nvars = g_hash_table_size (ctx->jit_callees);
7384                 LLVMValueRef *callee_vars = g_new0 (LLVMValueRef, nvars); 
7385                 gpointer *callee_addrs = g_new0 (gpointer, nvars);
7386                 GHashTableIter iter;
7387                 LLVMValueRef var;
7388                 MonoMethod *callee;
7389                 gpointer eh_frame;
7390
7391                 /*
7392                  * Compute the addresses of the LLVM globals pointing to the
7393                  * methods called by the current method. Pass it to the trampoline
7394                  * code so it can update them after their corresponding method was
7395                  * compiled.
7396                  */
7397                 g_hash_table_iter_init (&iter, ctx->jit_callees);
7398                 i = 0;
7399                 while (g_hash_table_iter_next (&iter, NULL, (void**)&var))
7400                         callee_vars [i ++] = var;
7401
7402                 cfg->native_code = mono_llvm_compile_method (ctx->module->mono_ee, ctx->lmethod, nvars, callee_vars, callee_addrs, &eh_frame);
7403
7404                 decode_llvm_eh_info (ctx, eh_frame);
7405
7406                 mono_domain_lock (domain);
7407                 domain_info = domain_jit_info (domain);
7408                 if (!domain_info->llvm_jit_callees)
7409                         domain_info->llvm_jit_callees = g_hash_table_new (NULL, NULL);
7410                 g_hash_table_iter_init (&iter, ctx->jit_callees);
7411                 i = 0;
7412                 while (g_hash_table_iter_next (&iter, (void**)&callee, (void**)&var)) {
7413                         GSList *addrs = g_hash_table_lookup (domain_info->llvm_jit_callees, callee);
7414                         addrs = g_slist_prepend (addrs, callee_addrs [i]);
7415                         g_hash_table_insert (domain_info->llvm_jit_callees, callee, addrs);
7416                         i ++;
7417                 }
7418                 mono_domain_unlock (domain);
7419 #else
7420                 mono_llvm_optimize_method (ctx->module->mono_ee, ctx->lmethod);
7421
7422                 if (cfg->verbose_level > 1)
7423                         mono_llvm_dump_value (ctx->lmethod);
7424
7425                 cfg->native_code = (unsigned char*)LLVMGetPointerToGlobal (ctx->module->ee, ctx->lmethod);
7426
7427                 /* Set by emit_cb */
7428                 g_assert (cfg->code_len);
7429 #endif
7430         }
7431
7432         if (ctx->module->method_to_lmethod)
7433                 g_hash_table_insert (ctx->module->method_to_lmethod, cfg->method, ctx->lmethod);
7434         if (ctx->module->idx_to_lmethod)
7435                 g_hash_table_insert (ctx->module->idx_to_lmethod, GINT_TO_POINTER (cfg->method_index), ctx->lmethod);
7436
7437         if (ctx->llvm_only && cfg->orig_method->klass->valuetype && !(cfg->orig_method->flags & METHOD_ATTRIBUTE_STATIC))
7438                 emit_unbox_tramp (ctx, ctx->method_name, ctx->method_type, ctx->lmethod, cfg->method_index);
7439 }
7440
7441 /*
7442  * mono_llvm_create_vars:
7443  *
7444  *   Same as mono_arch_create_vars () for LLVM.
7445  */
7446 void
7447 mono_llvm_create_vars (MonoCompile *cfg)
7448 {
7449         MonoMethodSignature *sig;
7450
7451         sig = mono_method_signature (cfg->method);
7452         if (cfg->gsharedvt && cfg->llvm_only) {
7453                 if (mini_is_gsharedvt_variable_signature (sig) && sig->ret->type != MONO_TYPE_VOID) {
7454                         cfg->vret_addr = mono_compile_create_var (cfg, &mono_get_intptr_class ()->byval_arg, OP_ARG);
7455                         if (G_UNLIKELY (cfg->verbose_level > 1)) {
7456                                 printf ("vret_addr = ");
7457                                 mono_print_ins (cfg->vret_addr);
7458                         }
7459                 }
7460         } else {
7461                 mono_arch_create_vars (cfg);
7462         }
7463 }
7464
7465 /*
7466  * mono_llvm_emit_call:
7467  *
7468  *   Same as mono_arch_emit_call () for LLVM.
7469  */
7470 void
7471 mono_llvm_emit_call (MonoCompile *cfg, MonoCallInst *call)
7472 {
7473         MonoInst *in;
7474         MonoMethodSignature *sig;
7475         int i, n, stack_size;
7476         LLVMArgInfo *ainfo;
7477
7478         stack_size = 0;
7479
7480         sig = call->signature;
7481         n = sig->param_count + sig->hasthis;
7482
7483         call->cinfo = get_llvm_call_info (cfg, sig);
7484
7485         if (cfg->disable_llvm)
7486                 return;
7487
7488         if (sig->call_convention == MONO_CALL_VARARG) {
7489                 cfg->exception_message = g_strdup ("varargs");
7490                 cfg->disable_llvm = TRUE;
7491         }
7492
7493         for (i = 0; i < n; ++i) {
7494                 MonoInst *ins;
7495
7496                 ainfo = call->cinfo->args + i;
7497
7498                 in = call->args [i];
7499                         
7500                 /* Simply remember the arguments */
7501                 switch (ainfo->storage) {
7502                 case LLVMArgNormal: {
7503                         MonoType *t = (sig->hasthis && i == 0) ? &mono_get_intptr_class ()->byval_arg : ainfo->type;
7504                         int opcode;
7505
7506                         opcode = mono_type_to_regmove (cfg, t);
7507                         if (opcode == OP_FMOVE) {
7508                                 MONO_INST_NEW (cfg, ins, OP_FMOVE);
7509                                 ins->dreg = mono_alloc_freg (cfg);
7510                         } else if (opcode == OP_LMOVE) {
7511                                 MONO_INST_NEW (cfg, ins, OP_LMOVE);
7512                                 ins->dreg = mono_alloc_lreg (cfg);
7513                         } else if (opcode == OP_RMOVE) {
7514                                 MONO_INST_NEW (cfg, ins, OP_RMOVE);
7515                                 ins->dreg = mono_alloc_freg (cfg);
7516                         } else {
7517                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
7518                                 ins->dreg = mono_alloc_ireg (cfg);
7519                         }
7520                         ins->sreg1 = in->dreg;
7521                         break;
7522                 }
7523                 case LLVMArgVtypeByVal:
7524                 case LLVMArgVtypeByRef:
7525                 case LLVMArgVtypeInReg:
7526                 case LLVMArgVtypeAsScalar:
7527                 case LLVMArgAsIArgs:
7528                 case LLVMArgAsFpArgs:
7529                 case LLVMArgGsharedvtVariable:
7530                 case LLVMArgGsharedvtFixed:
7531                 case LLVMArgGsharedvtFixedVtype:
7532                         MONO_INST_NEW (cfg, ins, OP_LLVM_OUTARG_VT);
7533                         ins->dreg = mono_alloc_ireg (cfg);
7534                         ins->sreg1 = in->dreg;
7535                         ins->inst_p0 = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMArgInfo));
7536                         memcpy (ins->inst_p0, ainfo, sizeof (LLVMArgInfo));
7537                         ins->inst_vtype = ainfo->type;
7538                         ins->klass = mono_class_from_mono_type (ainfo->type);
7539                         break;
7540                 default:
7541                         cfg->exception_message = g_strdup ("ainfo->storage");
7542                         cfg->disable_llvm = TRUE;
7543                         return;
7544                 }
7545
7546                 if (!cfg->disable_llvm) {
7547                         MONO_ADD_INS (cfg->cbb, ins);
7548                         mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, 0, FALSE);
7549                 }
7550         }
7551 }
7552
7553 static unsigned char*
7554 alloc_cb (LLVMValueRef function, int size)
7555 {
7556         MonoCompile *cfg;
7557
7558         cfg = (MonoCompile*)mono_native_tls_get_value (current_cfg_tls_id);
7559
7560         if (cfg) {
7561                 // FIXME: dynamic
7562                 return (unsigned char*)mono_domain_code_reserve (cfg->domain, size);
7563         } else {
7564                 return (unsigned char*)mono_domain_code_reserve (mono_domain_get (), size);
7565         }
7566 }
7567
7568 static void
7569 emitted_cb (LLVMValueRef function, void *start, void *end)
7570 {
7571         MonoCompile *cfg;
7572
7573         cfg = (MonoCompile*)mono_native_tls_get_value (current_cfg_tls_id);
7574         g_assert (cfg);
7575         cfg->code_len = (guint8*)end - (guint8*)start;
7576 }
7577
7578 static void
7579 exception_cb (void *data)
7580 {
7581         MonoCompile *cfg;
7582         MonoJitExceptionInfo *ei;
7583         guint32 ei_len, i, j, nested_len, nindex;
7584         gpointer *type_info;
7585         int this_reg, this_offset;
7586
7587         cfg = (MonoCompile*)mono_native_tls_get_value (current_cfg_tls_id);
7588         g_assert (cfg);
7589
7590         /*
7591          * data points to a DWARF FDE structure, convert it to our unwind format and
7592          * save it.
7593          * An alternative would be to save it directly, and modify our unwinder to work
7594          * with it.
7595          */
7596         cfg->encoded_unwind_ops = mono_unwind_decode_fde ((guint8*)data, &cfg->encoded_unwind_ops_len, NULL, &ei, &ei_len, &type_info, &this_reg, &this_offset);
7597         if (cfg->verbose_level > 1)
7598                 mono_print_unwind_info (cfg->encoded_unwind_ops, cfg->encoded_unwind_ops_len);
7599
7600         /* Count nested clauses */
7601         nested_len = 0;
7602         for (i = 0; i < ei_len; ++i) {
7603                 gint32 cindex1 = *(gint32*)type_info [i];
7604                 MonoExceptionClause *clause1 = &cfg->header->clauses [cindex1];
7605
7606                 for (j = 0; j < cfg->header->num_clauses; ++j) {
7607                         int cindex2 = j;
7608                         MonoExceptionClause *clause2 = &cfg->header->clauses [cindex2];
7609
7610                         if (cindex1 != cindex2 && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset) {
7611                                 nested_len ++;
7612                         }
7613                 }
7614         }
7615
7616         cfg->llvm_ex_info = (MonoJitExceptionInfo*)mono_mempool_alloc0 (cfg->mempool, (ei_len + nested_len) * sizeof (MonoJitExceptionInfo));
7617         cfg->llvm_ex_info_len = ei_len + nested_len;
7618         memcpy (cfg->llvm_ex_info, ei, ei_len * sizeof (MonoJitExceptionInfo));
7619         /* Fill the rest of the information from the type info */
7620         for (i = 0; i < ei_len; ++i) {
7621                 gint32 clause_index = *(gint32*)type_info [i];
7622                 MonoExceptionClause *clause = &cfg->header->clauses [clause_index];
7623
7624                 cfg->llvm_ex_info [i].flags = clause->flags;
7625                 cfg->llvm_ex_info [i].data.catch_class = clause->data.catch_class;
7626                 cfg->llvm_ex_info [i].clause_index = clause_index;
7627         }
7628
7629         /*
7630          * For nested clauses, the LLVM produced exception info associates the try interval with
7631          * the innermost handler, while mono expects it to be associated with all nesting clauses.
7632          * So add new clauses which use the IL info (catch class etc.) from the nesting clause,
7633          * and everything else from the nested clause.
7634          */
7635         nindex = ei_len;
7636         for (i = 0; i < ei_len; ++i) {
7637                 gint32 cindex1 = *(gint32*)type_info [i];
7638                 MonoExceptionClause *clause1 = &cfg->header->clauses [cindex1];
7639
7640                 for (j = 0; j < cfg->header->num_clauses; ++j) {
7641                         int cindex2 = j;
7642                         MonoExceptionClause *clause2 = &cfg->header->clauses [cindex2];
7643                         MonoJitExceptionInfo *nesting_ei, *nested_ei;
7644
7645                         if (cindex1 != cindex2 && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset) {
7646                                 /* clause1 is the nested clause */
7647                                 nested_ei = &cfg->llvm_ex_info [i];
7648                                 nesting_ei = &cfg->llvm_ex_info [nindex];
7649                                 nindex ++;
7650
7651                                 memcpy (nesting_ei, nested_ei, sizeof (MonoJitExceptionInfo));
7652
7653                                 nesting_ei->flags = clause2->flags;
7654                                 nesting_ei->data.catch_class = clause2->data.catch_class;
7655                                 nesting_ei->clause_index = cindex2;
7656                         }
7657                 }
7658         }
7659         g_assert (nindex == ei_len + nested_len);
7660         cfg->llvm_this_reg = this_reg;
7661         cfg->llvm_this_offset = this_offset;
7662
7663         /* type_info [i] is cfg mempool allocated, no need to free it */
7664
7665         g_free (ei);
7666         g_free (type_info);
7667 }
7668
7669 #if LLVM_API_VERSION > 100
7670 /*
7671  * decode_llvm_eh_info:
7672  *
7673  *   Decode the EH table emitted by llvm in jit mode, and store
7674  * the result into cfg.
7675  */
7676 static void
7677 decode_llvm_eh_info (EmitContext *ctx, gpointer eh_frame)
7678 {
7679         MonoCompile *cfg = ctx->cfg;
7680         guint8 *cie, *fde;
7681         int fde_len;
7682         MonoLLVMFDEInfo info;
7683         MonoJitExceptionInfo *ei;
7684         guint8 *p = eh_frame;
7685         int version, fde_count, fde_offset;
7686         guint32 ei_len, i, nested_len;
7687         gpointer *type_info;
7688         gint32 *table;
7689
7690         /*
7691          * Decode the one element EH table emitted by the MonoException class
7692          * in llvm.
7693          */
7694
7695         /* Similar to decode_llvm_mono_eh_frame () in aot-runtime.c */
7696
7697         version = *p;
7698         g_assert (version == 3);
7699         p ++;
7700         p ++;
7701         p = (guint8 *)ALIGN_PTR_TO (p, 4);
7702
7703         fde_count = *(guint32*)p;
7704         p += 4;
7705         table = (gint32*)p;
7706
7707         g_assert (fde_count <= 2);
7708
7709         /* The first entry is the real method */
7710         g_assert (table [0] == 1);
7711         fde_offset = table [1];
7712         table += fde_count * 2;
7713         /* Extra entry */
7714         cfg->code_len = table [0];
7715         fde_len = table [1] - fde_offset;
7716         table += 2;
7717
7718         fde = (guint8*)eh_frame + fde_offset;
7719         cie = (guint8*)table;
7720
7721         mono_unwind_decode_llvm_mono_fde (fde, fde_len, cie, cfg->native_code, &info);
7722
7723         cfg->encoded_unwind_ops = info.unw_info;
7724         cfg->encoded_unwind_ops_len = info.unw_info_len;
7725         if (cfg->verbose_level > 1)
7726                 mono_print_unwind_info (cfg->encoded_unwind_ops, cfg->encoded_unwind_ops_len);
7727         if (info.this_reg != -1) {
7728                 cfg->llvm_this_reg = info.this_reg;
7729                 cfg->llvm_this_offset = info.this_offset;
7730         }
7731
7732         ei = info.ex_info;
7733         ei_len = info.ex_info_len;
7734         type_info = info.type_info;
7735
7736         // Nested clauses are currently disabled
7737         nested_len = 0;
7738
7739         cfg->llvm_ex_info = (MonoJitExceptionInfo*)mono_mempool_alloc0 (cfg->mempool, (ei_len + nested_len) * sizeof (MonoJitExceptionInfo));
7740         cfg->llvm_ex_info_len = ei_len + nested_len;
7741         memcpy (cfg->llvm_ex_info, ei, ei_len * sizeof (MonoJitExceptionInfo));
7742         /* Fill the rest of the information from the type info */
7743         for (i = 0; i < ei_len; ++i) {
7744                 gint32 clause_index = *(gint32*)type_info [i];
7745                 MonoExceptionClause *clause = &cfg->header->clauses [clause_index];
7746
7747                 cfg->llvm_ex_info [i].flags = clause->flags;
7748                 cfg->llvm_ex_info [i].data.catch_class = clause->data.catch_class;
7749                 cfg->llvm_ex_info [i].clause_index = clause_index;
7750         }
7751 }
7752 #endif
7753
7754 static char*
7755 dlsym_cb (const char *name, void **symbol)
7756 {
7757         MonoDl *current;
7758         char *err;
7759
7760         err = NULL;
7761         if (!strcmp (name, "__bzero")) {
7762                 *symbol = (void*)bzero;
7763         } else {
7764                 current = mono_dl_open (NULL, 0, NULL);
7765                 g_assert (current);
7766
7767                 err = mono_dl_symbol (current, name, symbol);
7768
7769                 mono_dl_close (current);
7770         }
7771 #ifdef MONO_ARCH_HAVE_CREATE_LLVM_NATIVE_THUNK
7772         *symbol = (char*)mono_arch_create_llvm_native_thunk (mono_domain_get (), (guint8*)(*symbol));
7773 #endif
7774         return err;
7775 }
7776
7777 static inline void
7778 AddFunc (LLVMModuleRef module, const char *name, LLVMTypeRef ret_type, LLVMTypeRef *param_types, int nparams)
7779 {
7780         LLVMAddFunction (module, name, LLVMFunctionType (ret_type, param_types, nparams, FALSE));
7781 }
7782
7783 static inline void
7784 AddFunc2 (LLVMModuleRef module, const char *name, LLVMTypeRef ret_type, LLVMTypeRef param_type1, LLVMTypeRef param_type2)
7785 {
7786         LLVMTypeRef param_types [4];
7787
7788         param_types [0] = param_type1;
7789         param_types [1] = param_type2;
7790
7791         AddFunc (module, name, ret_type, param_types, 2);
7792 }
7793
7794 typedef enum {
7795         INTRINS_MEMSET,
7796         INTRINS_MEMCPY,
7797         INTRINS_SADD_OVF_I32,
7798         INTRINS_UADD_OVF_I32,
7799         INTRINS_SSUB_OVF_I32,
7800         INTRINS_USUB_OVF_I32,
7801         INTRINS_SMUL_OVF_I32,
7802         INTRINS_UMUL_OVF_I32,
7803         INTRINS_SADD_OVF_I64,
7804         INTRINS_UADD_OVF_I64,
7805         INTRINS_SSUB_OVF_I64,
7806         INTRINS_USUB_OVF_I64,
7807         INTRINS_SMUL_OVF_I64,
7808         INTRINS_UMUL_OVF_I64,
7809         INTRINS_SIN,
7810         INTRINS_COS,
7811         INTRINS_SQRT,
7812         INTRINS_FABS,
7813         INTRINS_EXPECT_I8,
7814         INTRINS_EXPECT_I1,
7815 #if defined(TARGET_AMD64) || defined(TARGET_X86)
7816         INTRINS_SSE_PMOVMSKB,
7817         INTRINS_SSE_PSRLI_W,
7818         INTRINS_SSE_PSRAI_W,
7819         INTRINS_SSE_PSLLI_W,
7820         INTRINS_SSE_PSRLI_D,
7821         INTRINS_SSE_PSRAI_D,
7822         INTRINS_SSE_PSLLI_D,
7823         INTRINS_SSE_PSRLI_Q,
7824         INTRINS_SSE_PSLLI_Q,
7825         INTRINS_SSE_SQRT_PD,
7826         INTRINS_SSE_SQRT_PS,
7827         INTRINS_SSE_RSQRT_PS,
7828         INTRINS_SSE_RCP_PS,
7829         INTRINS_SSE_CVTTPD2DQ,
7830         INTRINS_SSE_CVTTPS2DQ,
7831         INTRINS_SSE_CVTDQ2PD,
7832         INTRINS_SSE_CVTDQ2PS,
7833         INTRINS_SSE_CVTPD2DQ,
7834         INTRINS_SSE_CVTPS2DQ,
7835         INTRINS_SSE_CVTPD2PS,
7836         INTRINS_SSE_CVTPS2PD,
7837         INTRINS_SSE_CMPPD,
7838         INTRINS_SSE_CMPPS,
7839         INTRINS_SSE_PACKSSWB,
7840         INTRINS_SSE_PACKUSWB,
7841         INTRINS_SSE_PACKSSDW,
7842         INTRINS_SSE_PACKUSDW,
7843         INTRINS_SSE_MINPS,
7844         INTRINS_SSE_MAXPS,
7845         INTRINS_SSE_HADDPS,
7846         INTRINS_SSE_HSUBPS,
7847         INTRINS_SSE_ADDSUBPS,
7848         INTRINS_SSE_MINPD,
7849         INTRINS_SSE_MAXPD,
7850         INTRINS_SSE_HADDPD,
7851         INTRINS_SSE_HSUBPD,
7852         INTRINS_SSE_ADDSUBPD,
7853         INTRINS_SSE_PADDSW,
7854         INTRINS_SSE_PSUBSW,
7855         INTRINS_SSE_PADDUSW,
7856         INTRINS_SSE_PSUBUSW,
7857         INTRINS_SSE_PAVGW,
7858         INTRINS_SSE_PMULHW,
7859         INTRINS_SSE_PMULHU,
7860         INTRINS_SE_PADDSB,
7861         INTRINS_SSE_PSUBSB,
7862         INTRINS_SSE_PADDUSB,
7863         INTRINS_SSE_PSUBUSB,
7864         INTRINS_SSE_PAVGB,
7865         INTRINS_SSE_PAUSE,
7866         INTRINS_SSE_DPPS,
7867 #endif
7868         INTRINS_NUM
7869 } IntrinsicId;
7870
7871 typedef struct {
7872         IntrinsicId id;
7873         const char *name;
7874 } IntrinsicDesc;
7875
7876 static IntrinsicDesc intrinsics[] = {
7877         {INTRINS_MEMSET, "llvm.memset.p0i8.i32"},
7878         {INTRINS_MEMCPY, "llvm.memcpy.p0i8.p0i8.i32"},
7879         {INTRINS_SADD_OVF_I32, "llvm.sadd.with.overflow.i32"},
7880         {INTRINS_UADD_OVF_I32, "llvm.uadd.with.overflow.i32"},
7881         {INTRINS_SSUB_OVF_I32, "llvm.ssub.with.overflow.i32"},
7882         {INTRINS_USUB_OVF_I32, "llvm.usub.with.overflow.i32"},
7883         {INTRINS_SMUL_OVF_I32, "llvm.smul.with.overflow.i32"},
7884         {INTRINS_UMUL_OVF_I32, "llvm.umul.with.overflow.i32"},
7885         {INTRINS_SADD_OVF_I64, "llvm.sadd.with.overflow.i64"},
7886         {INTRINS_UADD_OVF_I64, "llvm.uadd.with.overflow.i64"},
7887         {INTRINS_SSUB_OVF_I64, "llvm.ssub.with.overflow.i64"},
7888         {INTRINS_USUB_OVF_I64, "llvm.usub.with.overflow.i64"},
7889         {INTRINS_SMUL_OVF_I64, "llvm.smul.with.overflow.i64"},
7890         {INTRINS_UMUL_OVF_I64, "llvm.umul.with.overflow.i64"},
7891         {INTRINS_SIN, "llvm.sin.f64"},
7892         {INTRINS_COS, "llvm.cos.f64"},
7893         {INTRINS_SQRT, "llvm.sqrt.f64"},
7894         /* This isn't an intrinsic, instead llvm seems to special case it by name */
7895         {INTRINS_FABS, "fabs"},
7896         {INTRINS_EXPECT_I8, "llvm.expect.i8"},
7897         {INTRINS_EXPECT_I1, "llvm.expect.i1"},
7898 #if defined(TARGET_AMD64) || defined(TARGET_X86)
7899         {INTRINS_SSE_PMOVMSKB, "llvm.x86.sse2.pmovmskb.128"},
7900         {INTRINS_SSE_PSRLI_W, "llvm.x86.sse2.psrli.w"},
7901         {INTRINS_SSE_PSRAI_W, "llvm.x86.sse2.psrai.w"},
7902         {INTRINS_SSE_PSLLI_W, "llvm.x86.sse2.pslli.w"},
7903         {INTRINS_SSE_PSRLI_D, "llvm.x86.sse2.psrli.d"},
7904         {INTRINS_SSE_PSRAI_D, "llvm.x86.sse2.psrai.d"},
7905         {INTRINS_SSE_PSLLI_D, "llvm.x86.sse2.pslli.d"},
7906         {INTRINS_SSE_PSRLI_Q, "llvm.x86.sse2.psrli.q"},
7907         {INTRINS_SSE_PSLLI_Q, "llvm.x86.sse2.pslli.q"},
7908         {INTRINS_SSE_SQRT_PD, "llvm.x86.sse2.sqrt.pd"},
7909         {INTRINS_SSE_SQRT_PS, "llvm.x86.sse.sqrt.ps"},
7910         {INTRINS_SSE_RSQRT_PS, "llvm.x86.sse.rsqrt.ps"},
7911         {INTRINS_SSE_RCP_PS, "llvm.x86.sse.rcp.ps"},
7912         {INTRINS_SSE_CVTTPD2DQ, "llvm.x86.sse2.cvttpd2dq"},
7913         {INTRINS_SSE_CVTTPS2DQ, "llvm.x86.sse2.cvttps2dq"},
7914         {INTRINS_SSE_CVTDQ2PD, "llvm.x86.sse2.cvtdq2pd"},
7915         {INTRINS_SSE_CVTDQ2PS, "llvm.x86.sse2.cvtdq2ps"},
7916         {INTRINS_SSE_CVTPD2DQ, "llvm.x86.sse2.cvtpd2dq"},
7917         {INTRINS_SSE_CVTPS2DQ, "llvm.x86.sse2.cvtps2dq"},
7918         {INTRINS_SSE_CVTPD2PS, "llvm.x86.sse2.cvtpd2ps"},
7919         {INTRINS_SSE_CVTPS2PD, "llvm.x86.sse2.cvtps2pd"},
7920         {INTRINS_SSE_CMPPD, "llvm.x86.sse2.cmp.pd"},
7921         {INTRINS_SSE_CMPPS, "llvm.x86.sse.cmp.ps"},
7922         {INTRINS_SSE_PACKSSWB, "llvm.x86.sse2.packsswb.128"},
7923         {INTRINS_SSE_PACKUSWB, "llvm.x86.sse2.packuswb.128"},
7924         {INTRINS_SSE_PACKSSDW, "llvm.x86.sse2.packssdw.128"},
7925         {INTRINS_SSE_PACKUSDW, "llvm.x86.sse41.packusdw"},
7926         {INTRINS_SSE_MINPS, "llvm.x86.sse.min.ps"},
7927         {INTRINS_SSE_MAXPS, "llvm.x86.sse.max.ps"},
7928         {INTRINS_SSE_HADDPS, "llvm.x86.sse3.hadd.ps"},
7929         {INTRINS_SSE_HSUBPS, "llvm.x86.sse3.hsub.ps"},
7930         {INTRINS_SSE_ADDSUBPS, "llvm.x86.sse3.addsub.ps"},
7931         {INTRINS_SSE_MINPD, "llvm.x86.sse2.min.pd"},
7932         {INTRINS_SSE_MAXPD, "llvm.x86.sse2.max.pd"},
7933         {INTRINS_SSE_HADDPD, "llvm.x86.sse3.hadd.pd"},
7934         {INTRINS_SSE_HSUBPD, "llvm.x86.sse3.hsub.pd"},
7935         {INTRINS_SSE_ADDSUBPD, "llvm.x86.sse3.addsub.pd"},
7936         {INTRINS_SSE_PADDSW, "llvm.x86.sse2.padds.w"},
7937         {INTRINS_SSE_PSUBSW, "llvm.x86.sse2.psubs.w"},
7938         {INTRINS_SSE_PADDUSW, "llvm.x86.sse2.paddus.w"},
7939         {INTRINS_SSE_PSUBUSW, "llvm.x86.sse2.psubus.w"},
7940         {INTRINS_SSE_PAVGW, "llvm.x86.sse2.pavg.w"},
7941         {INTRINS_SSE_PMULHW, "llvm.x86.sse2.pmulh.w"},
7942         {INTRINS_SSE_PMULHU, "llvm.x86.sse2.pmulhu.w"},
7943         {INTRINS_SE_PADDSB, "llvm.x86.sse2.padds.b"},
7944         {INTRINS_SSE_PSUBSB, "llvm.x86.sse2.psubs.b"},
7945         {INTRINS_SSE_PADDUSB, "llvm.x86.sse2.paddus.b"},
7946         {INTRINS_SSE_PSUBUSB, "llvm.x86.sse2.psubus.b"},
7947         {INTRINS_SSE_PAVGB, "llvm.x86.sse2.pavg.b"},
7948         {INTRINS_SSE_PAUSE, "llvm.x86.sse2.pause"},
7949         {INTRINS_SSE_DPPS, "llvm.x86.sse41.dpps"}
7950 #endif
7951 };
7952
7953 static void
7954 add_sse_binary (LLVMModuleRef module, const char *name, int type)
7955 {
7956         LLVMTypeRef ret_type = type_to_simd_type (type);
7957         AddFunc2 (module, name, ret_type, ret_type, ret_type);
7958 }
7959
7960 static void
7961 add_intrinsic (LLVMModuleRef module, int id)
7962 {
7963         const char *name;
7964 #if defined(TARGET_AMD64) || defined(TARGET_X86)
7965         LLVMTypeRef ret_type, arg_types [16];
7966 #endif
7967
7968         name = g_hash_table_lookup (intrins_id_to_name, GINT_TO_POINTER (id));
7969         g_assert (name);
7970
7971         switch (id) {
7972         case INTRINS_MEMSET: {
7973                 LLVMTypeRef params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMInt8Type (), LLVMInt32Type (), LLVMInt32Type (), LLVMInt1Type () };
7974
7975                 AddFunc (module, name, LLVMVoidType (), params, 5);
7976                 break;
7977         }
7978         case INTRINS_MEMCPY: {
7979                 LLVMTypeRef params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMPointerType (LLVMInt8Type (), 0), LLVMInt32Type (), LLVMInt32Type (), LLVMInt1Type () };
7980
7981                 AddFunc (module, name, LLVMVoidType (), params, 5);
7982                 break;
7983         }
7984         case INTRINS_SADD_OVF_I32:
7985         case INTRINS_UADD_OVF_I32:
7986         case INTRINS_SSUB_OVF_I32:
7987         case INTRINS_USUB_OVF_I32:
7988         case INTRINS_SMUL_OVF_I32:
7989         case INTRINS_UMUL_OVF_I32: {
7990                 LLVMTypeRef ovf_res_i32 [] = { LLVMInt32Type (), LLVMInt1Type () };
7991                 LLVMTypeRef params [] = { LLVMInt32Type (), LLVMInt32Type () };
7992                 LLVMTypeRef ret_type = LLVMStructType (ovf_res_i32, 2, FALSE);
7993
7994                 AddFunc (module, name, ret_type, params, 2);
7995                 break;
7996         }
7997         case INTRINS_SADD_OVF_I64:
7998         case INTRINS_UADD_OVF_I64:
7999         case INTRINS_SSUB_OVF_I64:
8000         case INTRINS_USUB_OVF_I64:
8001         case INTRINS_SMUL_OVF_I64:
8002         case INTRINS_UMUL_OVF_I64: {
8003                 LLVMTypeRef ovf_res_i64 [] = { LLVMInt64Type (), LLVMInt1Type () };
8004                 LLVMTypeRef params [] = { LLVMInt64Type (), LLVMInt64Type () };
8005                 LLVMTypeRef ret_type = LLVMStructType (ovf_res_i64, 2, FALSE);
8006
8007                 AddFunc (module, name, ret_type, params, 2);
8008                 break;
8009         }
8010         case INTRINS_SIN:
8011         case INTRINS_COS:
8012         case INTRINS_SQRT:
8013         case INTRINS_FABS: {
8014                 LLVMTypeRef params [] = { LLVMDoubleType () };
8015
8016                 AddFunc (module, name, LLVMDoubleType (), params, 1);
8017                 break;
8018         }
8019         case INTRINS_EXPECT_I8:
8020                 AddFunc2 (module, name, LLVMInt8Type (), LLVMInt8Type (), LLVMInt8Type ());
8021                 break;
8022         case INTRINS_EXPECT_I1:
8023                 AddFunc2 (module, name, LLVMInt1Type (), LLVMInt1Type (), LLVMInt1Type ());
8024                 break;
8025 #if defined(TARGET_AMD64) || defined(TARGET_X86)
8026         case INTRINS_SSE_PMOVMSKB:
8027                 /* pmovmskb */
8028                 ret_type = LLVMInt32Type ();
8029                 arg_types [0] = type_to_simd_type (MONO_TYPE_I1);
8030                 AddFunc (module, name, ret_type, arg_types, 1);
8031                 break;
8032         case INTRINS_SSE_PSRLI_W:
8033         case INTRINS_SSE_PSRAI_W:
8034         case INTRINS_SSE_PSLLI_W:
8035                 /* shifts */
8036                 ret_type = type_to_simd_type (MONO_TYPE_I2);
8037                 arg_types [0] = ret_type;
8038                 arg_types [1] = LLVMInt32Type ();
8039                 AddFunc (module, name, ret_type, arg_types, 2);
8040                 break;
8041         case INTRINS_SSE_PSRLI_D:
8042         case INTRINS_SSE_PSRAI_D:
8043         case INTRINS_SSE_PSLLI_D:
8044                 ret_type = type_to_simd_type (MONO_TYPE_I4);
8045                 arg_types [0] = ret_type;
8046                 arg_types [1] = LLVMInt32Type ();
8047                 AddFunc (module, name, ret_type, arg_types, 2);
8048                 break;
8049         case INTRINS_SSE_PSRLI_Q:
8050         case INTRINS_SSE_PSLLI_Q:
8051                 ret_type = type_to_simd_type (MONO_TYPE_I8);
8052                 arg_types [0] = ret_type;
8053                 arg_types [1] = LLVMInt32Type ();
8054                 AddFunc (module, name, ret_type, arg_types, 2);
8055                 break;
8056         case INTRINS_SSE_SQRT_PD:
8057                 /* Unary ops */
8058                 ret_type = type_to_simd_type (MONO_TYPE_R8);
8059                 arg_types [0] = ret_type;
8060                 AddFunc (module, name, ret_type, arg_types, 1);
8061                 break;
8062         case INTRINS_SSE_SQRT_PS:
8063                 ret_type = type_to_simd_type (MONO_TYPE_R4);
8064                 arg_types [0] = ret_type;
8065                 AddFunc (module, name, ret_type, arg_types, 1);
8066                 break;
8067         case INTRINS_SSE_RSQRT_PS:
8068                 ret_type = type_to_simd_type (MONO_TYPE_R4);
8069                 arg_types [0] = ret_type;
8070                 AddFunc (module, name, ret_type, arg_types, 1);
8071                 break;
8072         case INTRINS_SSE_RCP_PS:
8073                 ret_type = type_to_simd_type (MONO_TYPE_R4);
8074                 arg_types [0] = ret_type;
8075                 AddFunc (module, name, ret_type, arg_types, 1);
8076                 break;
8077         case INTRINS_SSE_CVTTPD2DQ:
8078                 ret_type = type_to_simd_type (MONO_TYPE_I4);
8079                 arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
8080                 AddFunc (module, name, ret_type, arg_types, 1);
8081                 break;
8082         case INTRINS_SSE_CVTTPS2DQ:
8083                 ret_type = type_to_simd_type (MONO_TYPE_I4);
8084                 arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
8085                 AddFunc (module, name, ret_type, arg_types, 1);
8086                 break;
8087         case INTRINS_SSE_CVTDQ2PD:
8088                 /* Conversion ops */
8089                 ret_type = type_to_simd_type (MONO_TYPE_R8);
8090                 arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
8091                 AddFunc (module, name, ret_type, arg_types, 1);
8092                 break;
8093         case INTRINS_SSE_CVTDQ2PS:
8094                 ret_type = type_to_simd_type (MONO_TYPE_R4);
8095                 arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
8096                 AddFunc (module, name, ret_type, arg_types, 1);
8097                 break;
8098         case INTRINS_SSE_CVTPD2DQ:
8099                 ret_type = type_to_simd_type (MONO_TYPE_I4);
8100                 arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
8101                 AddFunc (module, name, ret_type, arg_types, 1);
8102                 break;
8103         case INTRINS_SSE_CVTPS2DQ:
8104                 ret_type = type_to_simd_type (MONO_TYPE_I4);
8105                 arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
8106                 AddFunc (module, name, ret_type, arg_types, 1);
8107                 break;
8108         case INTRINS_SSE_CVTPD2PS:
8109                 ret_type = type_to_simd_type (MONO_TYPE_R4);
8110                 arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
8111                 AddFunc (module, name, ret_type, arg_types, 1);
8112                 break;
8113         case INTRINS_SSE_CVTPS2PD:
8114                 ret_type = type_to_simd_type (MONO_TYPE_R8);
8115                 arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
8116                 AddFunc (module, name, ret_type, arg_types, 1);
8117                 break;
8118         case INTRINS_SSE_CMPPD:
8119                 /* cmp pd/ps */
8120                 ret_type = type_to_simd_type (MONO_TYPE_R8);
8121                 arg_types [0] = ret_type;
8122                 arg_types [1] = ret_type;
8123                 arg_types [2] = LLVMInt8Type ();
8124                 AddFunc (module, name, ret_type, arg_types, 3);
8125                 break;
8126         case INTRINS_SSE_CMPPS:
8127                 ret_type = type_to_simd_type (MONO_TYPE_R4);
8128                 arg_types [0] = ret_type;
8129                 arg_types [1] = ret_type;
8130                 arg_types [2] = LLVMInt8Type ();
8131                 AddFunc (module, name, ret_type, arg_types, 3);
8132                 break;
8133         case INTRINS_SSE_PACKSSWB:
8134         case INTRINS_SSE_PACKUSWB:
8135         case INTRINS_SSE_PACKSSDW:
8136                 /* pack */
8137                 ret_type = type_to_simd_type (MONO_TYPE_I1);
8138                 arg_types [0] = type_to_simd_type (MONO_TYPE_I2);
8139                 arg_types [1] = type_to_simd_type (MONO_TYPE_I2);
8140                 AddFunc (module, name, ret_type, arg_types, 2);
8141                 break;
8142         case INTRINS_SSE_PACKUSDW:
8143                 ret_type = type_to_simd_type (MONO_TYPE_I2);
8144                 arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
8145                 arg_types [1] = type_to_simd_type (MONO_TYPE_I4);
8146                 AddFunc (module, name, ret_type, arg_types, 2);
8147                 break;
8148                 /* SSE Binary ops */
8149         case INTRINS_SSE_PADDSW:
8150         case INTRINS_SSE_PSUBSW:
8151         case INTRINS_SSE_PADDUSW:
8152         case INTRINS_SSE_PSUBUSW:
8153         case INTRINS_SSE_PAVGW:
8154         case INTRINS_SSE_PMULHW:
8155         case INTRINS_SSE_PMULHU:
8156                 add_sse_binary (module, name, MONO_TYPE_I2);
8157                 break;
8158         case INTRINS_SSE_MINPS:
8159         case INTRINS_SSE_MAXPS:
8160         case INTRINS_SSE_HADDPS:
8161         case INTRINS_SSE_HSUBPS:
8162         case INTRINS_SSE_ADDSUBPS:
8163                 add_sse_binary (module, name, MONO_TYPE_R4);
8164                 break;
8165         case INTRINS_SSE_MINPD:
8166         case INTRINS_SSE_MAXPD:
8167         case INTRINS_SSE_HADDPD:
8168         case INTRINS_SSE_HSUBPD:
8169         case INTRINS_SSE_ADDSUBPD:
8170                 add_sse_binary (module, name, MONO_TYPE_R8);
8171                 break;
8172         case INTRINS_SE_PADDSB:
8173         case INTRINS_SSE_PSUBSB:
8174         case INTRINS_SSE_PADDUSB:
8175         case INTRINS_SSE_PSUBUSB:
8176         case INTRINS_SSE_PAVGB:
8177                 add_sse_binary (module, name, MONO_TYPE_I1);
8178                 break;
8179         case INTRINS_SSE_PAUSE:
8180                 AddFunc (module, "llvm.x86.sse2.pause", LLVMVoidType (), NULL, 0);
8181                 break;
8182         case INTRINS_SSE_DPPS:
8183                 ret_type = type_to_simd_type (MONO_TYPE_R4);
8184                 arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
8185                 arg_types [1] = type_to_simd_type (MONO_TYPE_R4);
8186                 arg_types [2] = LLVMInt32Type ();
8187                 AddFunc (module, name, ret_type, arg_types, 3);
8188                 break;
8189 #endif
8190         default:
8191                 g_assert_not_reached ();
8192                 break;
8193         }
8194 }
8195
8196 static LLVMValueRef
8197 get_intrinsic (EmitContext *ctx, const char *name)
8198 {
8199 #if LLVM_API_VERSION > 100
8200         LLVMValueRef res;
8201
8202         /*
8203          * Every method is emitted into its own module so
8204          * we can add intrinsics on demand.
8205          */
8206         res = LLVMGetNamedFunction (ctx->lmodule, name);
8207         if (!res) {
8208                 int id = -1;
8209
8210                 /* No locking needed */
8211                 id = GPOINTER_TO_INT (g_hash_table_lookup (intrins_name_to_id, name));
8212                 id --;
8213                 if (id == -1)
8214                         printf ("%s\n", name);
8215                 g_assert (id != -1);
8216                 add_intrinsic (ctx->lmodule, id);
8217                 res = LLVMGetNamedFunction (ctx->lmodule, name);
8218                 g_assert (res);
8219         }
8220
8221         return res;
8222 #else
8223         LLVMValueRef res;
8224
8225         res = LLVMGetNamedFunction (ctx->lmodule, name);
8226         g_assert (res);
8227         return res;
8228 #endif
8229 }
8230
8231 static void
8232 add_intrinsics (LLVMModuleRef module)
8233 {
8234         int i;
8235
8236         /* Emit declarations of instrinsics */
8237         /*
8238          * It would be nicer to emit only the intrinsics actually used, but LLVM's Module
8239          * type doesn't seem to do any locking.
8240          */
8241         for (i = 0; i < INTRINS_NUM; ++i)
8242                 add_intrinsic (module, i);
8243
8244         /* EH intrinsics */
8245         {
8246                 AddFunc (module, "mono_personality", LLVMVoidType (), NULL, 0);
8247
8248                 AddFunc (module, "llvm_resume_unwind_trampoline", LLVMVoidType (), NULL, 0);
8249         }
8250
8251         /* Load/Store intrinsics */
8252         {
8253                 LLVMTypeRef arg_types [5];
8254                 int i;
8255                 char name [128];
8256
8257                 for (i = 1; i <= 8; i *= 2) {
8258                         arg_types [0] = LLVMPointerType (LLVMIntType (i * 8), 0);
8259                         arg_types [1] = LLVMInt32Type ();
8260                         arg_types [2] = LLVMInt1Type ();
8261                         arg_types [3] = LLVMInt32Type ();
8262                         sprintf (name, "llvm.mono.load.i%d.p0i%d", i * 8, i * 8);
8263                         AddFunc (module, name, LLVMIntType (i * 8), arg_types, 4);
8264
8265                         arg_types [0] = LLVMIntType (i * 8);
8266                         arg_types [1] = LLVMPointerType (LLVMIntType (i * 8), 0);
8267                         arg_types [2] = LLVMInt32Type ();
8268                         arg_types [3] = LLVMInt1Type ();
8269                         arg_types [4] = LLVMInt32Type ();
8270                         sprintf (name, "llvm.mono.store.i%d.p0i%d", i * 8, i * 8);
8271                         AddFunc (module, name, LLVMVoidType (), arg_types, 5);
8272                 }
8273         }
8274 }
8275
8276 static void
8277 add_types (MonoLLVMModule *module)
8278 {
8279         module->ptr_type = LLVMPointerType (sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type (), 0);
8280 }
8281
8282 void
8283 mono_llvm_init (void)
8284 {
8285         GHashTable *h;
8286         int i;
8287
8288         mono_native_tls_alloc (&current_cfg_tls_id, NULL);
8289
8290         h = g_hash_table_new (NULL, NULL);
8291         for (i = 0; i < INTRINS_NUM; ++i)
8292                 g_hash_table_insert (h, GINT_TO_POINTER (intrinsics [i].id), (gpointer)intrinsics [i].name);
8293         intrins_id_to_name = h;
8294
8295         h = g_hash_table_new (g_str_hash, g_str_equal);
8296         for (i = 0; i < INTRINS_NUM; ++i)
8297                 g_hash_table_insert (h, (gpointer)intrinsics [i].name, GINT_TO_POINTER (intrinsics [i].id + 1));
8298         intrins_name_to_id = h;
8299 }
8300
8301 static void
8302 init_jit_module (MonoDomain *domain)
8303 {
8304         MonoJitDomainInfo *dinfo;
8305         MonoLLVMModule *module;
8306         char *name;
8307
8308         dinfo = domain_jit_info (domain);
8309         if (dinfo->llvm_module)
8310                 return;
8311
8312         mono_loader_lock ();
8313
8314         if (dinfo->llvm_module) {
8315                 mono_loader_unlock ();
8316                 return;
8317         }
8318
8319         module = g_new0 (MonoLLVMModule, 1);
8320
8321         name = g_strdup_printf ("mono-%s", domain->friendly_name);
8322         module->lmodule = LLVMModuleCreateWithName (name);
8323         module->context = LLVMGetGlobalContext ();
8324
8325         module->mono_ee = (MonoEERef*)mono_llvm_create_ee (LLVMCreateModuleProviderForExistingModule (module->lmodule), alloc_cb, emitted_cb, exception_cb, dlsym_cb, &module->ee);
8326
8327         add_intrinsics (module->lmodule);
8328         add_types (module);
8329
8330         module->llvm_types = g_hash_table_new (NULL, NULL);
8331
8332 #if LLVM_API_VERSION < 100
8333         MonoJitICallInfo *info;
8334
8335         info = mono_find_jit_icall_by_name ("llvm_resume_unwind_trampoline");
8336         g_assert (info);
8337         LLVMAddGlobalMapping (module->ee, LLVMGetNamedFunction (module->lmodule, "llvm_resume_unwind_trampoline"), (void*)info->func);
8338 #endif
8339
8340         mono_memory_barrier ();
8341
8342         dinfo->llvm_module = module;
8343
8344         mono_loader_unlock ();
8345 }
8346
8347 void
8348 mono_llvm_cleanup (void)
8349 {
8350         MonoLLVMModule *module = &aot_module;
8351
8352         if (module->lmodule)
8353                 LLVMDisposeModule (module->lmodule);
8354
8355         if (module->context)
8356                 LLVMContextDispose (module->context);
8357 }
8358
8359 void
8360 mono_llvm_free_domain_info (MonoDomain *domain)
8361 {
8362         MonoJitDomainInfo *info = domain_jit_info (domain);
8363         MonoLLVMModule *module = (MonoLLVMModule*)info->llvm_module;
8364         int i;
8365
8366         if (!module)
8367                 return;
8368
8369         if (module->llvm_types)
8370                 g_hash_table_destroy (module->llvm_types);
8371
8372         mono_llvm_dispose_ee (module->mono_ee);
8373
8374         if (module->bb_names) {
8375                 for (i = 0; i < module->bb_names_len; ++i)
8376                         g_free (module->bb_names [i]);
8377                 g_free (module->bb_names);
8378         }
8379         //LLVMDisposeModule (module->module);
8380
8381         g_free (module);
8382
8383         info->llvm_module = NULL;
8384 }
8385
8386 void
8387 mono_llvm_create_aot_module (MonoAssembly *assembly, const char *global_prefix, gboolean emit_dwarf, gboolean static_link, gboolean llvm_only)
8388 {
8389         MonoLLVMModule *module = &aot_module;
8390
8391         /* Delete previous module */
8392         if (module->plt_entries)
8393                 g_hash_table_destroy (module->plt_entries);
8394         if (module->lmodule)
8395                 LLVMDisposeModule (module->lmodule);
8396
8397         memset (module, 0, sizeof (aot_module));
8398
8399         module->lmodule = LLVMModuleCreateWithName ("aot");
8400         module->assembly = assembly;
8401         module->global_prefix = g_strdup (global_prefix);
8402         module->got_symbol = g_strdup_printf ("%s_llvm_got", global_prefix);
8403         module->eh_frame_symbol = g_strdup_printf ("%s_eh_frame", global_prefix);
8404         module->get_method_symbol = g_strdup_printf ("%s_get_method", global_prefix);
8405         module->get_unbox_tramp_symbol = g_strdup_printf ("%s_get_unbox_tramp", global_prefix);
8406         module->external_symbols = TRUE;
8407         module->emit_dwarf = emit_dwarf;
8408         module->static_link = static_link;
8409         module->llvm_only = llvm_only;
8410         /* The first few entries are reserved */
8411         module->max_got_offset = 16;
8412         module->context = LLVMGetGlobalContext ();
8413
8414         if (llvm_only)
8415                 /* clang ignores our debug info because it has an invalid version */
8416                 module->emit_dwarf = FALSE;
8417
8418         add_intrinsics (module->lmodule);
8419         add_types (module);
8420
8421 #if LLVM_API_VERSION > 100
8422         if (module->emit_dwarf) {
8423                 char *dir, *build_info, *s, *cu_name;
8424
8425                 module->di_builder = mono_llvm_create_di_builder (module->lmodule);
8426
8427                 // FIXME:
8428                 dir = g_strdup (".");
8429                 build_info = mono_get_runtime_build_info ();
8430                 s = g_strdup_printf ("Mono AOT Compiler %s (LLVM)", build_info);
8431                 cu_name = g_path_get_basename (assembly->image->name);
8432                 module->cu = mono_llvm_di_create_compile_unit (module->di_builder, cu_name, dir, s);
8433                 g_free (dir);
8434                 g_free (build_info);
8435                 g_free (s);
8436         }
8437 #endif
8438
8439         /* Add GOT */
8440         /*
8441          * We couldn't compute the type of the LLVM global representing the got because
8442          * its size is only known after all the methods have been emitted. So create
8443          * a dummy variable, and replace all uses it with the real got variable when
8444          * its size is known in mono_llvm_emit_aot_module ().
8445          */
8446         {
8447                 LLVMTypeRef got_type = LLVMArrayType (module->ptr_type, 0);
8448
8449                 module->got_var = LLVMAddGlobal (module->lmodule, got_type, "mono_dummy_got");
8450                 LLVMSetInitializer (module->got_var, LLVMConstNull (got_type));
8451         }
8452
8453         /* Add initialization array */
8454         if (llvm_only) {
8455                 LLVMTypeRef inited_type = LLVMArrayType (LLVMInt8Type (), 0);
8456
8457                 module->inited_var = LLVMAddGlobal (aot_module.lmodule, inited_type, "mono_inited_tmp");
8458                 LLVMSetInitializer (module->inited_var, LLVMConstNull (inited_type));
8459         }
8460
8461         if (llvm_only)
8462                 emit_init_icall_wrappers (module);
8463
8464         emit_llvm_code_start (module);
8465
8466         /* Add a dummy personality function */
8467         if (!use_debug_personality) {
8468                 LLVMValueRef personality = LLVMAddFunction (module->lmodule, default_personality_name, LLVMFunctionType (LLVMInt32Type (), NULL, 0, TRUE));
8469                 LLVMSetLinkage (personality, LLVMExternalLinkage);
8470                 mark_as_used (module, personality);
8471         }
8472
8473         /* Add a reference to the c++ exception we throw/catch */
8474         {
8475                 LLVMTypeRef exc = LLVMPointerType (LLVMInt8Type (), 0);
8476                 module->sentinel_exception = LLVMAddGlobal (module->lmodule, exc, "_ZTIPi");
8477                 LLVMSetLinkage (module->sentinel_exception, LLVMExternalLinkage);
8478                 mono_llvm_set_is_constant (module->sentinel_exception);
8479         }
8480
8481         module->llvm_types = g_hash_table_new (NULL, NULL);
8482         module->plt_entries = g_hash_table_new (g_str_hash, g_str_equal);
8483         module->plt_entries_ji = g_hash_table_new (NULL, NULL);
8484         module->direct_callables = g_hash_table_new (g_str_hash, g_str_equal);
8485         module->method_to_lmethod = g_hash_table_new (NULL, NULL);
8486         module->idx_to_lmethod = g_hash_table_new (NULL, NULL);
8487         module->idx_to_unbox_tramp = g_hash_table_new (NULL, NULL);
8488         module->method_to_callers = g_hash_table_new (NULL, NULL);
8489 }
8490
8491 static LLVMValueRef
8492 llvm_array_from_uints (LLVMTypeRef el_type, guint32 *values, int nvalues)
8493 {
8494         int i;
8495         LLVMValueRef res, *vals;
8496
8497         vals = g_new0 (LLVMValueRef, nvalues);
8498         for (i = 0; i < nvalues; ++i)
8499                 vals [i] = LLVMConstInt (LLVMInt32Type (), values [i], FALSE);
8500         res = LLVMConstArray (LLVMInt32Type (), vals, nvalues);
8501         g_free (vals);
8502         return res;
8503 }
8504
8505 static LLVMValueRef
8506 llvm_array_from_bytes (guint8 *values, int nvalues)
8507 {
8508         int i;
8509         LLVMValueRef res, *vals;
8510
8511         vals = g_new0 (LLVMValueRef, nvalues);
8512         for (i = 0; i < nvalues; ++i)
8513                 vals [i] = LLVMConstInt (LLVMInt8Type (), values [i], FALSE);
8514         res = LLVMConstArray (LLVMInt8Type (), vals, nvalues);
8515         g_free (vals);
8516         return res;
8517 }
8518 /*
8519  * mono_llvm_emit_aot_file_info:
8520  *
8521  *   Emit the MonoAotFileInfo structure.
8522  * Same as emit_aot_file_info () in aot-compiler.c.
8523  */
8524 void
8525 mono_llvm_emit_aot_file_info (MonoAotFileInfo *info, gboolean has_jitted_code)
8526 {
8527         MonoLLVMModule *module = &aot_module;
8528
8529         /* Save these for later */
8530         memcpy (&module->aot_info, info, sizeof (MonoAotFileInfo));
8531         module->has_jitted_code = has_jitted_code;
8532 }
8533
8534 /*
8535  * mono_llvm_emit_aot_data:
8536  *
8537  *   Emit the binary data DATA pointed to by symbol SYMBOL.
8538  */
8539 void
8540 mono_llvm_emit_aot_data (const char *symbol, guint8 *data, int data_len)
8541 {
8542         MonoLLVMModule *module = &aot_module;
8543         LLVMTypeRef type;
8544         LLVMValueRef d;
8545
8546         type = LLVMArrayType (LLVMInt8Type (), data_len);
8547         d = LLVMAddGlobal (module->lmodule, type, symbol);
8548         LLVMSetVisibility (d, LLVMHiddenVisibility);
8549         LLVMSetLinkage (d, LLVMInternalLinkage);
8550         LLVMSetInitializer (d, mono_llvm_create_constant_data_array (data, data_len));
8551         mono_llvm_set_is_constant (d);
8552 }
8553
8554 /* Add a reference to a global defined in JITted code */
8555 static LLVMValueRef
8556 AddJitGlobal (MonoLLVMModule *module, LLVMTypeRef type, const char *name)
8557 {
8558         char *s;
8559         LLVMValueRef v;
8560
8561         s = g_strdup_printf ("%s%s", module->global_prefix, name);
8562         v = LLVMAddGlobal (module->lmodule, LLVMInt8Type (), s);
8563         g_free (s);
8564         return v;
8565 }
8566
8567 static void
8568 emit_aot_file_info (MonoLLVMModule *module)
8569 {
8570         LLVMTypeRef file_info_type;
8571         LLVMTypeRef *eltypes, eltype;
8572         LLVMValueRef info_var;
8573         LLVMValueRef *fields;
8574         int i, nfields, tindex;
8575         MonoAotFileInfo *info;
8576         LLVMModuleRef lmodule = module->lmodule;
8577
8578         info = &module->aot_info;
8579
8580         /* Create an LLVM type to represent MonoAotFileInfo */
8581         nfields = 2 + MONO_AOT_FILE_INFO_NUM_SYMBOLS + 16 + 5;
8582         eltypes = g_new (LLVMTypeRef, nfields);
8583         tindex = 0;
8584         eltypes [tindex ++] = LLVMInt32Type ();
8585         eltypes [tindex ++] = LLVMInt32Type ();
8586         /* Symbols */
8587         for (i = 0; i < MONO_AOT_FILE_INFO_NUM_SYMBOLS; ++i)
8588                 eltypes [tindex ++] = LLVMPointerType (LLVMInt8Type (), 0);
8589         /* Scalars */
8590         for (i = 0; i < 15; ++i)
8591                 eltypes [tindex ++] = LLVMInt32Type ();
8592         /* Arrays */
8593         eltypes [tindex ++] = LLVMArrayType (LLVMInt32Type (), MONO_AOT_TABLE_NUM);
8594         for (i = 0; i < 4; ++i)
8595                 eltypes [tindex ++] = LLVMArrayType (LLVMInt32Type (), MONO_AOT_TRAMP_NUM);
8596         eltypes [tindex ++] = LLVMArrayType (LLVMInt8Type (), 16);
8597         g_assert (tindex == nfields);
8598         file_info_type = LLVMStructCreateNamed (module->context, "MonoAotFileInfo");
8599         LLVMStructSetBody (file_info_type, eltypes, nfields, FALSE);
8600
8601         info_var = LLVMAddGlobal (lmodule, file_info_type, "mono_aot_file_info");
8602         if (module->static_link) {
8603                 LLVMSetVisibility (info_var, LLVMHiddenVisibility);
8604                 LLVMSetLinkage (info_var, LLVMInternalLinkage);
8605         }
8606         fields = g_new (LLVMValueRef, nfields);
8607         tindex = 0;
8608         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->version, FALSE);
8609         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->dummy, FALSE);
8610
8611         /* Symbols */
8612         /*
8613          * We use LLVMGetNamedGlobal () for symbol which are defined in LLVM code, and LLVMAddGlobal ()
8614          * for symbols defined in the .s file emitted by the aot compiler.
8615          */
8616         eltype = eltypes [tindex];
8617         if (module->llvm_only)
8618                 fields [tindex ++] = LLVMConstNull (eltype);
8619         else
8620                 fields [tindex ++] = AddJitGlobal (module, eltype, "jit_got");
8621         fields [tindex ++] = module->got_var;
8622         /* llc defines this directly */
8623         if (!module->llvm_only) {
8624                 fields [tindex ++] = LLVMAddGlobal (lmodule, eltype, module->eh_frame_symbol);
8625                 fields [tindex ++] = LLVMConstNull (eltype);
8626                 fields [tindex ++] = LLVMConstNull (eltype);
8627         } else {
8628                 fields [tindex ++] = LLVMConstNull (eltype);
8629                 fields [tindex ++] = module->get_method;
8630                 fields [tindex ++] = module->get_unbox_tramp;
8631         }
8632         if (module->has_jitted_code) {
8633                 fields [tindex ++] = AddJitGlobal (module, eltype, "jit_code_start");
8634                 fields [tindex ++] = AddJitGlobal (module, eltype, "jit_code_end");
8635         } else {
8636                 fields [tindex ++] = LLVMConstNull (eltype);
8637                 fields [tindex ++] = LLVMConstNull (eltype);
8638         }
8639         if (!module->llvm_only)
8640                 fields [tindex ++] = AddJitGlobal (module, eltype, "method_addresses");
8641         else
8642                 fields [tindex ++] = LLVMConstNull (eltype);
8643         if (info->flags & MONO_AOT_FILE_FLAG_SEPARATE_DATA) {
8644                 for (i = 0; i < MONO_AOT_TABLE_NUM; ++i)
8645                         fields [tindex ++] = LLVMConstNull (eltype);
8646         } else {
8647                 fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "blob");
8648                 fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "class_name_table");
8649                 fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "class_info_offsets");
8650                 fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "method_info_offsets");
8651                 fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "ex_info_offsets");
8652                 fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "extra_method_info_offsets");
8653                 fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "extra_method_table");
8654                 fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "got_info_offsets");
8655                 fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "llvm_got_info_offsets");
8656                 fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "image_table");
8657         }
8658         /* Not needed (mem_end) */
8659         fields [tindex ++] = LLVMConstNull (eltype);
8660         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "assembly_guid");
8661         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "runtime_version");
8662         if (info->trampoline_size [0]) {
8663                 fields [tindex ++] = AddJitGlobal (module, eltype, "specific_trampolines");
8664                 fields [tindex ++] = AddJitGlobal (module, eltype, "static_rgctx_trampolines");
8665                 fields [tindex ++] = AddJitGlobal (module, eltype, "imt_trampolines");
8666                 fields [tindex ++] = AddJitGlobal (module, eltype, "gsharedvt_arg_trampolines");
8667         } else {
8668                 fields [tindex ++] = LLVMConstNull (eltype);
8669                 fields [tindex ++] = LLVMConstNull (eltype);
8670                 fields [tindex ++] = LLVMConstNull (eltype);
8671                 fields [tindex ++] = LLVMConstNull (eltype);
8672         }
8673         if (module->static_link && !module->llvm_only)
8674                 fields [tindex ++] = AddJitGlobal (module, eltype, "globals");
8675         else
8676                 fields [tindex ++] = LLVMConstNull (eltype);
8677         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "assembly_name");
8678         if (!module->llvm_only) {
8679                 fields [tindex ++] = AddJitGlobal (module, eltype, "plt");
8680                 fields [tindex ++] = AddJitGlobal (module, eltype, "plt_end");
8681                 fields [tindex ++] = AddJitGlobal (module, eltype, "unwind_info");
8682                 fields [tindex ++] = AddJitGlobal (module, eltype, "unbox_trampolines");
8683                 fields [tindex ++] = AddJitGlobal (module, eltype, "unbox_trampolines_end");
8684                 fields [tindex ++] = AddJitGlobal (module, eltype, "unbox_trampoline_addresses");
8685         } else {
8686                 fields [tindex ++] = LLVMConstNull (eltype);
8687                 fields [tindex ++] = LLVMConstNull (eltype);
8688                 fields [tindex ++] = LLVMConstNull (eltype);
8689                 fields [tindex ++] = LLVMConstNull (eltype);
8690                 fields [tindex ++] = LLVMConstNull (eltype);
8691                 fields [tindex ++] = LLVMConstNull (eltype);
8692         }
8693
8694         for (i = 0; i < MONO_AOT_FILE_INFO_NUM_SYMBOLS; ++i)
8695                 fields [2 + i] = LLVMConstBitCast (fields [2 + i], eltype);
8696
8697         /* Scalars */
8698         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->plt_got_offset_base, FALSE);
8699         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->got_size, FALSE);
8700         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->plt_size, FALSE);
8701         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->nmethods, FALSE);
8702         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->flags, FALSE);
8703         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->opts, FALSE);
8704         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->simd_opts, FALSE);
8705         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->gc_name_index, FALSE);
8706         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->num_rgctx_fetch_trampolines, FALSE);
8707         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->double_align, FALSE);
8708         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->long_align, FALSE);
8709         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->generic_tramp_num, FALSE);
8710         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->tramp_page_size, FALSE);
8711         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->nshared_got_entries, FALSE);
8712         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->datafile_size, FALSE);
8713         /* Arrays */
8714         fields [tindex ++] = llvm_array_from_uints (LLVMInt32Type (), info->table_offsets, MONO_AOT_TABLE_NUM);
8715         fields [tindex ++] = llvm_array_from_uints (LLVMInt32Type (), info->num_trampolines, MONO_AOT_TRAMP_NUM);
8716         fields [tindex ++] = llvm_array_from_uints (LLVMInt32Type (), info->trampoline_got_offset_base, MONO_AOT_TRAMP_NUM);
8717         fields [tindex ++] = llvm_array_from_uints (LLVMInt32Type (), info->trampoline_size, MONO_AOT_TRAMP_NUM);
8718         fields [tindex ++] = llvm_array_from_uints (LLVMInt32Type (), info->tramp_page_code_offsets, MONO_AOT_TRAMP_NUM);
8719
8720         fields [tindex ++] = llvm_array_from_bytes (info->aotid, 16);
8721         g_assert (tindex == nfields);
8722
8723         LLVMSetInitializer (info_var, LLVMConstNamedStruct (file_info_type, fields, nfields));
8724
8725         if (module->static_link) {
8726                 char *s, *p;
8727                 LLVMValueRef var;
8728
8729                 s = g_strdup_printf ("mono_aot_module_%s_info", module->assembly->aname.name);
8730                 /* Get rid of characters which cannot occur in symbols */
8731                 p = s;
8732                 for (p = s; *p; ++p) {
8733                         if (!(isalnum (*p) || *p == '_'))
8734                                 *p = '_';
8735                 }
8736                 var = LLVMAddGlobal (module->lmodule, LLVMPointerType (LLVMInt8Type (), 0), s);
8737                 g_free (s);
8738                 LLVMSetInitializer (var, LLVMConstBitCast (LLVMGetNamedGlobal (module->lmodule, "mono_aot_file_info"), LLVMPointerType (LLVMInt8Type (), 0)));
8739                 LLVMSetLinkage (var, LLVMExternalLinkage);
8740         }
8741 }
8742
8743 /*
8744  * Emit the aot module into the LLVM bitcode file FILENAME.
8745  */
8746 void
8747 mono_llvm_emit_aot_module (const char *filename, const char *cu_name)
8748 {
8749         LLVMTypeRef got_type, inited_type;
8750         LLVMValueRef real_got, real_inited;
8751         MonoLLVMModule *module = &aot_module;
8752
8753         emit_llvm_code_end (module);
8754
8755         /* 
8756          * Create the real got variable and replace all uses of the dummy variable with
8757          * the real one.
8758          */
8759         got_type = LLVMArrayType (module->ptr_type, module->max_got_offset + 1);
8760         real_got = LLVMAddGlobal (module->lmodule, got_type, module->got_symbol);
8761         LLVMSetInitializer (real_got, LLVMConstNull (got_type));
8762         if (module->external_symbols) {
8763                 LLVMSetLinkage (real_got, LLVMExternalLinkage);
8764                 LLVMSetVisibility (real_got, LLVMHiddenVisibility);
8765         } else {
8766                 LLVMSetLinkage (real_got, LLVMInternalLinkage);
8767         }
8768         mono_llvm_replace_uses_of (module->got_var, real_got);
8769
8770         mark_as_used (&aot_module, real_got);
8771
8772         /* Delete the dummy got so it doesn't become a global */
8773         LLVMDeleteGlobal (module->got_var);
8774         module->got_var = real_got;
8775
8776         /*
8777          * Same for the init_var
8778          */
8779         if (module->llvm_only) {
8780                 inited_type = LLVMArrayType (LLVMInt8Type (), module->max_inited_idx + 1);
8781                 real_inited = LLVMAddGlobal (module->lmodule, inited_type, "mono_inited");
8782                 LLVMSetInitializer (real_inited, LLVMConstNull (inited_type));
8783                 LLVMSetLinkage (real_inited, LLVMInternalLinkage);
8784                 mono_llvm_replace_uses_of (module->inited_var, real_inited);
8785                 LLVMDeleteGlobal (module->inited_var);
8786         }
8787
8788         if (module->llvm_only) {
8789                 emit_get_method (&aot_module);
8790                 emit_get_unbox_tramp (&aot_module);
8791         }
8792
8793         emit_llvm_used (&aot_module);
8794         emit_dbg_info (&aot_module, filename, cu_name);
8795         emit_aot_file_info (&aot_module);
8796
8797         /*
8798          * Replace GOT entries for directly callable methods with the methods themselves.
8799          * It would be easier to implement this by predefining all methods before compiling
8800          * their bodies, but that couldn't handle the case when a method fails to compile
8801          * with llvm.
8802          */
8803         if (module->llvm_only) {
8804                 GHashTableIter iter;
8805                 MonoMethod *method;
8806                 GSList *callers, *l;
8807
8808                 g_hash_table_iter_init (&iter, module->method_to_callers);
8809                 while (g_hash_table_iter_next (&iter, (void**)&method, (void**)&callers)) {
8810                         LLVMValueRef lmethod;
8811
8812                         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
8813                                 continue;
8814
8815                         lmethod = (LLVMValueRef)g_hash_table_lookup (module->method_to_lmethod, method);
8816                         if (lmethod) {
8817                                 for (l = callers; l; l = l->next) {
8818                                         LLVMValueRef caller = (LLVMValueRef)l->data;
8819
8820                                         mono_llvm_replace_uses_of (caller, lmethod);
8821                                 }
8822                         }
8823                 }
8824         }
8825
8826         /* Replace PLT entries for directly callable methods with the methods themselves */
8827         {
8828                 GHashTableIter iter;
8829                 MonoJumpInfo *ji;
8830                 LLVMValueRef callee;
8831
8832                 g_hash_table_iter_init (&iter, module->plt_entries_ji);
8833                 while (g_hash_table_iter_next (&iter, (void**)&ji, (void**)&callee)) {
8834                         if (mono_aot_is_direct_callable (ji)) {
8835                                 LLVMValueRef lmethod;
8836
8837                                 lmethod = (LLVMValueRef)g_hash_table_lookup (module->method_to_lmethod, ji->data.method);
8838                                 /* The types might not match because the caller might pass an rgctx */
8839                                 if (lmethod && LLVMTypeOf (callee) == LLVMTypeOf (lmethod)) {
8840                                         mono_llvm_replace_uses_of (callee, lmethod);
8841                                         mono_aot_mark_unused_llvm_plt_entry (ji);
8842                                 }
8843                         }
8844                 }
8845         }
8846
8847 #if 1
8848         {
8849                 char *verifier_err;
8850
8851                 if (LLVMVerifyModule (module->lmodule, LLVMReturnStatusAction, &verifier_err)) {
8852                         printf ("%s\n", verifier_err);
8853                         g_assert_not_reached ();
8854                 }
8855         }
8856 #endif
8857
8858         LLVMWriteBitcodeToFile (module->lmodule, filename);
8859 }
8860
8861
8862 static LLVMValueRef
8863 md_string (const char *s)
8864 {
8865         return LLVMMDString (s, strlen (s));
8866 }
8867
8868 /* Debugging support */
8869
8870 static void
8871 emit_dbg_info (MonoLLVMModule *module, const char *filename, const char *cu_name)
8872 {
8873         LLVMModuleRef lmodule = module->lmodule;
8874         LLVMValueRef args [16], ver;
8875
8876         /*
8877          * This can only be enabled when LLVM code is emitted into a separate object
8878          * file, since the AOT compiler also emits dwarf info,
8879          * and the abbrev indexes will not be correct since llvm has added its own
8880          * abbrevs.
8881          */
8882         if (!module->emit_dwarf)
8883                 return;
8884
8885 #if LLVM_API_VERSION > 100
8886         mono_llvm_di_builder_finalize (module->di_builder);
8887 #else
8888         LLVMValueRef cu_args [16], cu;
8889         int n_cuargs;
8890         char *build_info, *s, *dir;
8891
8892         /*
8893          * Emit dwarf info in the form of LLVM metadata. There is some
8894          * out-of-date documentation at:
8895          * http://llvm.org/docs/SourceLevelDebugging.html
8896          * but most of this was gathered from the llvm and
8897          * clang sources.
8898          */
8899
8900         n_cuargs = 0;
8901         cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), DW_TAG_compile_unit, FALSE);
8902         /* CU name/compilation dir */
8903         dir = g_path_get_dirname (filename);
8904         args [0] = LLVMMDString (cu_name, strlen (cu_name));
8905         args [1] = LLVMMDString (dir, strlen (dir));
8906         cu_args [n_cuargs ++] = LLVMMDNode (args, 2);
8907         g_free (dir);
8908         /* Language */
8909         cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), DW_LANG_C99, FALSE);
8910         /* Producer */
8911         build_info = mono_get_runtime_build_info ();
8912         s = g_strdup_printf ("Mono AOT Compiler %s (LLVM)", build_info);
8913         cu_args [n_cuargs ++] = LLVMMDString (s, strlen (s));
8914         g_free (build_info);
8915         /* Optimized */
8916         cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
8917         /* Flags */
8918         cu_args [n_cuargs ++] = LLVMMDString ("", strlen (""));
8919         /* Runtime version */
8920         cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
8921         /* Enums */
8922         cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
8923         cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
8924         /* Subprograms */
8925         if (module->subprogram_mds) {
8926                 LLVMValueRef *mds;
8927                 int i;
8928
8929                 mds = g_new0 (LLVMValueRef, module->subprogram_mds->len);
8930                 for (i = 0; i < module->subprogram_mds->len; ++i)
8931                         mds [i] = (LLVMValueRef)g_ptr_array_index (module->subprogram_mds, i);
8932                 cu_args [n_cuargs ++] = LLVMMDNode (mds, module->subprogram_mds->len);
8933         } else {
8934                 cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
8935         }
8936         /* GVs */
8937         cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
8938         /* Imported modules */
8939         cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
8940         /* SplitName */
8941         cu_args [n_cuargs ++] = LLVMMDString ("", strlen (""));
8942         /* DebugEmissionKind = FullDebug */
8943         cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
8944         cu = LLVMMDNode (cu_args, n_cuargs);
8945         LLVMAddNamedMetadataOperand (lmodule, "llvm.dbg.cu", cu);
8946 #endif
8947
8948 #if LLVM_API_VERSION > 100
8949         args [0] = LLVMConstInt (LLVMInt32Type (), 2, FALSE);
8950         args [1] = LLVMMDString ("Dwarf Version", strlen ("Dwarf Version"));
8951         args [2] = LLVMConstInt (LLVMInt32Type (), 2, FALSE);
8952         ver = LLVMMDNode (args, 3);
8953         LLVMAddNamedMetadataOperand (lmodule, "llvm.module.flags", ver);
8954
8955         args [0] = LLVMConstInt (LLVMInt32Type (), 2, FALSE);
8956         args [1] = LLVMMDString ("Debug Info Version", strlen ("Debug Info Version"));
8957         args [2] = LLVMConstInt (LLVMInt64Type (), 3, FALSE);
8958         ver = LLVMMDNode (args, 3);
8959         LLVMAddNamedMetadataOperand (lmodule, "llvm.module.flags", ver);
8960 #else
8961         args [0] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
8962         args [1] = LLVMMDString ("Dwarf Version", strlen ("Dwarf Version"));
8963         args [2] = LLVMConstInt (LLVMInt32Type (), 2, FALSE);
8964         ver = LLVMMDNode (args, 3);
8965         LLVMAddNamedMetadataOperand (lmodule, "llvm.module.flags", ver);
8966
8967         args [0] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
8968         args [1] = LLVMMDString ("Debug Info Version", strlen ("Debug Info Version"));
8969         args [2] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
8970         ver = LLVMMDNode (args, 3);
8971         LLVMAddNamedMetadataOperand (lmodule, "llvm.module.flags", ver);
8972 #endif
8973 }
8974
8975 static LLVMValueRef
8976 emit_dbg_subprogram (EmitContext *ctx, MonoCompile *cfg, LLVMValueRef method, const char *name)
8977 {
8978         MonoLLVMModule *module = ctx->module;
8979         MonoDebugMethodInfo *minfo = ctx->minfo;
8980         char *source_file, *dir, *filename;
8981         LLVMValueRef md, args [16], ctx_args [16], md_args [64], type_args [16], ctx_md, type_md;
8982         MonoSymSeqPoint *sym_seq_points;
8983         int n_seq_points;
8984
8985         if (!minfo)
8986                 return NULL;
8987
8988         mono_debug_symfile_get_seq_points (minfo, &source_file, NULL, NULL, &sym_seq_points, &n_seq_points);
8989         if (!source_file)
8990                 source_file = g_strdup ("<unknown>");
8991         dir = g_path_get_dirname (source_file);
8992         filename = g_path_get_basename (source_file);
8993
8994 #if LLVM_API_VERSION > 100
8995         return mono_llvm_di_create_function (module->di_builder, module->cu, method, cfg->method->name, name, dir, filename, n_seq_points ? sym_seq_points [0].line : 1);
8996 #endif
8997
8998         ctx_args [0] = LLVMConstInt (LLVMInt32Type (), 0x29, FALSE);
8999         args [0] = md_string (filename);
9000         args [1] = md_string (dir);
9001         ctx_args [1] = LLVMMDNode (args, 2);
9002         ctx_md = LLVMMDNode (ctx_args, 2);
9003
9004         type_args [0] = LLVMConstInt (LLVMInt32Type (), DW_TAG_subroutine_type, FALSE);
9005         type_args [1] = NULL;
9006         type_args [2] = NULL;
9007         type_args [3] = LLVMMDString ("", 0);
9008         type_args [4] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
9009         type_args [5] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
9010         type_args [6] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
9011         type_args [7] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
9012         type_args [8] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
9013         type_args [9] = NULL;
9014         type_args [10] = NULL;
9015         type_args [11] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
9016         type_args [12] = NULL;
9017         type_args [13] = NULL;
9018         type_args [14] = NULL;
9019         type_md = LLVMMDNode (type_args, 14);
9020
9021         /* http://llvm.org/docs/SourceLevelDebugging.html#subprogram-descriptors */
9022         md_args [0] = LLVMConstInt (LLVMInt32Type (), DW_TAG_subprogram, FALSE);
9023         /* Source directory + file pair */
9024         args [0] = md_string (filename);
9025         args [1] = md_string (dir);
9026         md_args [1] = LLVMMDNode (args ,2);
9027         md_args [2] = ctx_md;
9028         md_args [3] = md_string (cfg->method->name);
9029         md_args [4] = md_string (name);
9030         md_args [5] = md_string (name);
9031         /* Line number */
9032         if (n_seq_points)
9033                 md_args [6] = LLVMConstInt (LLVMInt32Type (), sym_seq_points [0].line, FALSE);
9034         else
9035                 md_args [6] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
9036         /* Type */
9037         md_args [7] = type_md;
9038         /* static */
9039         md_args [8] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
9040         /* not extern */
9041         md_args [9] = LLVMConstInt (LLVMInt1Type (), 1, FALSE);
9042         /* Virtuality */
9043         md_args [10] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
9044         /* Index into a virtual function */
9045         md_args [11] = NULL;
9046         md_args [12] = NULL;
9047         /* Flags */
9048         md_args [13] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
9049         /* isOptimized */
9050         md_args [14] = LLVMConstInt (LLVMInt1Type (), 1, FALSE);
9051         /* Pointer to LLVM function */
9052         md_args [15] = method;
9053         /* Function template parameter */
9054         md_args [16] = NULL;
9055         /* Function declaration descriptor */
9056         md_args [17] = NULL;
9057         /* List of function variables */
9058         md_args [18] = LLVMMDNode (args, 0);
9059         /* Line number */
9060         md_args [19] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
9061         md = LLVMMDNode (md_args, 20);
9062
9063         if (!module->subprogram_mds)
9064                 module->subprogram_mds = g_ptr_array_new ();
9065         g_ptr_array_add (module->subprogram_mds, md);
9066
9067         g_free (dir);
9068         g_free (filename);
9069         g_free (source_file);
9070         g_free (sym_seq_points);
9071
9072         return md;
9073 }
9074
9075 static void
9076 emit_dbg_loc (EmitContext *ctx, LLVMBuilderRef builder, const unsigned char *cil_code)
9077 {
9078         MonoCompile *cfg = ctx->cfg;
9079
9080         if (ctx->minfo && cil_code && cil_code >= cfg->header->code && cil_code < cfg->header->code + cfg->header->code_size) {
9081                 MonoDebugSourceLocation *loc;
9082                 LLVMValueRef loc_md;
9083
9084                 loc = mono_debug_symfile_lookup_location (ctx->minfo, cil_code - cfg->header->code);
9085
9086                 if (loc) {
9087 #if LLVM_API_VERSION > 100
9088                         loc_md = mono_llvm_di_create_location (ctx->module->di_builder, ctx->dbg_md, loc->row, loc->column);
9089                         mono_llvm_di_set_location (builder, loc_md);
9090 #else
9091                         LLVMValueRef md_args [16];
9092                         int nmd_args;
9093
9094                         nmd_args = 0;
9095                         md_args [nmd_args ++] = LLVMConstInt (LLVMInt32Type (), loc->row, FALSE);
9096                         md_args [nmd_args ++] = LLVMConstInt (LLVMInt32Type (), loc->column, FALSE);
9097                         md_args [nmd_args ++] = ctx->dbg_md;
9098                         md_args [nmd_args ++] = NULL;
9099                         loc_md = LLVMMDNode (md_args, nmd_args);
9100                         LLVMSetCurrentDebugLocation (builder, loc_md);
9101 #endif
9102                         mono_debug_symfile_free_location (loc);
9103                 }
9104         }
9105 }
9106
9107 void
9108 default_mono_llvm_unhandled_exception (void)
9109 {
9110         MonoJitTlsData *jit_tls = mono_get_jit_tls ();
9111         MonoObject *target = mono_gchandle_get_target (jit_tls->thrown_exc);
9112
9113         mono_unhandled_exception (target);
9114         mono_invoke_unhandled_exception_hook (target);
9115         g_assert_not_reached ();
9116 }
9117
9118 /*
9119   DESIGN:
9120   - Emit LLVM IR from the mono IR using the LLVM C API.
9121   - The original arch specific code remains, so we can fall back to it if we run
9122     into something we can't handle.
9123 */
9124
9125 /*  
9126   A partial list of issues:
9127   - Handling of opcodes which can throw exceptions.
9128
9129       In the mono JIT, these are implemented using code like this:
9130           method:
9131       <compare>
9132           throw_pos:
9133           b<cond> ex_label
9134           <rest of code>
9135       ex_label:
9136           push throw_pos - method
9137           call <exception trampoline>
9138
9139           The problematic part is push throw_pos - method, which cannot be represented
9140       in the LLVM IR, since it does not support label values.
9141           -> this can be implemented in AOT mode using inline asm + labels, but cannot
9142           be implemented in JIT mode ?
9143           -> a possible but slower implementation would use the normal exception 
9144       throwing code but it would need to control the placement of the throw code
9145       (it needs to be exactly after the compare+branch).
9146           -> perhaps add a PC offset intrinsics ?
9147
9148   - efficient implementation of .ovf opcodes.
9149
9150           These are currently implemented as:
9151           <ins which sets the condition codes>
9152           b<cond> ex_label
9153
9154           Some overflow opcodes are now supported by LLVM SVN.
9155
9156   - exception handling, unwinding.
9157     - SSA is disabled for methods with exception handlers    
9158         - How to obtain unwind info for LLVM compiled methods ?
9159           -> this is now solved by converting the unwind info generated by LLVM
9160              into our format.
9161         - LLVM uses the c++ exception handling framework, while we use our home grown
9162       code, and couldn't use the c++ one:
9163       - its not supported under VC++, other exotic platforms.
9164           - it might be impossible to support filter clauses with it.
9165
9166   - trampolines.
9167   
9168     The trampolines need a predictable call sequence, since they need to disasm
9169     the calling code to obtain register numbers / offsets.
9170
9171     LLVM currently generates this code in non-JIT mode:
9172            mov    -0x98(%rax),%eax
9173            callq  *%rax
9174     Here, the vtable pointer is lost. 
9175     -> solution: use one vtable trampoline per class.
9176
9177   - passing/receiving the IMT pointer/RGCTX.
9178     -> solution: pass them as normal arguments ?
9179
9180   - argument passing.
9181   
9182           LLVM does not allow the specification of argument registers etc. This means
9183       that all calls are made according to the platform ABI.
9184
9185   - passing/receiving vtypes.
9186
9187       Vtypes passed/received in registers are handled by the front end by using
9188           a signature with scalar arguments, and loading the parts of the vtype into those
9189           arguments.
9190
9191           Vtypes passed on the stack are handled using the 'byval' attribute.
9192
9193   - ldaddr.
9194
9195     Supported though alloca, we need to emit the load/store code.
9196
9197   - types.
9198
9199     The mono JIT uses pointer sized iregs/double fregs, while LLVM uses precisely
9200     typed registers, so we have to keep track of the precise LLVM type of each vreg.
9201     This is made easier because the IR is already in SSA form.
9202     An additional problem is that our IR is not consistent with types, i.e. i32/ia64 
9203         types are frequently used incorrectly.
9204 */
9205
9206 /*
9207   AOT SUPPORT:
9208   Emit LLVM bytecode into a .bc file, compile it using llc into a .s file, then link
9209   it with the file containing the methods emitted by the JIT and the AOT data
9210   structures.
9211 */
9212
9213 /* FIXME: Normalize some aspects of the mono IR to allow easier translation, like:
9214  *   - each bblock should end with a branch
9215  *   - setting the return value, making cfg->ret non-volatile
9216  * - avoid some transformations in the JIT which make it harder for us to generate
9217  *   code.
9218  * - use pointer types to help optimizations.
9219  */
9220
9221 #else /* DISABLE_JIT */
9222
9223 void
9224 mono_llvm_cleanup (void)
9225 {
9226 }
9227
9228 void
9229 mono_llvm_free_domain_info (MonoDomain *domain)
9230 {
9231 }
9232
9233 void
9234 mono_llvm_init (void)
9235 {
9236 }
9237
9238 void
9239 default_mono_llvm_unhandled_exception (void)
9240 {
9241 }
9242
9243 #endif /* DISABLE_JIT */