Merge pull request #1659 from alexanderkyte/stringbuilder-referencesource
[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  */
7
8 #include "mini.h"
9 #include <mono/metadata/debug-helpers.h>
10 #include <mono/metadata/debug-mono-symfile.h>
11 #include <mono/metadata/mempool-internals.h>
12 #include <mono/utils/mono-tls.h>
13 #include <mono/utils/mono-dl.h>
14 #include <mono/utils/mono-time.h>
15 #include <mono/utils/freebsd-dwarf.h>
16
17 #ifndef __STDC_LIMIT_MACROS
18 #define __STDC_LIMIT_MACROS
19 #endif
20 #ifndef __STDC_CONSTANT_MACROS
21 #define __STDC_CONSTANT_MACROS
22 #endif
23
24 #include "llvm-c/Core.h"
25 #include "llvm-c/ExecutionEngine.h"
26 #include "llvm-c/BitWriter.h"
27 #include "llvm-c/Analysis.h"
28
29 #include "mini-llvm-cpp.h"
30
31 #ifdef __MINGW32__
32
33 #include <stddef.h>
34 extern void *memset(void *, int, size_t);
35 void bzero (void *to, size_t count) { memset (to, 0, count); }
36
37 #endif
38
39 #if LLVM_API_VERSION < 4
40 #error "The version of the mono llvm repository is too old."
41 #endif
42
43  /*
44   * Information associated by mono with LLVM modules.
45   */
46 typedef struct {
47         LLVMModuleRef module;
48         LLVMValueRef throw, rethrow, throw_corlib_exception;
49         GHashTable *llvm_types;
50         LLVMValueRef got_var;
51         const char *got_symbol;
52         GHashTable *plt_entries;
53         GHashTable *plt_entries_ji;
54         GHashTable *method_to_lmethod;
55         char **bb_names;
56         int bb_names_len;
57         GPtrArray *used;
58         LLVMTypeRef ptr_type;
59         GPtrArray *subprogram_mds;
60         MonoEERef *mono_ee;
61         LLVMExecutionEngineRef ee;
62         gboolean external_symbols;
63         gboolean emit_dwarf;
64         int max_got_offset;
65 } MonoLLVMModule;
66
67 /*
68  * Information associated by the backend with mono basic blocks.
69  */
70 typedef struct {
71         LLVMBasicBlockRef bblock, end_bblock;
72         LLVMValueRef finally_ind;
73         gboolean added, invoke_target;
74         /* 
75          * If this bblock is the start of a finally clause, this is a list of bblocks it
76          * needs to branch to in ENDFINALLY.
77          */
78         GSList *call_handler_return_bbs;
79         /*
80          * If this bblock is the start of a finally clause, this is the bblock that
81          * CALL_HANDLER needs to branch to.
82          */
83         LLVMBasicBlockRef call_handler_target_bb;
84         /* The list of switch statements generated by ENDFINALLY instructions */
85         GSList *endfinally_switch_ins_list;
86         GSList *phi_nodes;
87 } BBInfo;
88
89 /*
90  * Structure containing emit state
91  */
92 typedef struct {
93         MonoMemPool *mempool;
94
95         /* Maps method names to the corresponding LLVMValueRef */
96         GHashTable *emitted_method_decls;
97
98         MonoCompile *cfg;
99         LLVMValueRef lmethod;
100         MonoLLVMModule *lmodule;
101         LLVMModuleRef module;
102         BBInfo *bblocks;
103         int sindex, default_index, ex_index;
104         LLVMBuilderRef builder;
105         LLVMValueRef *values, *addresses;
106         MonoType **vreg_cli_types;
107         LLVMCallInfo *linfo;
108         MonoMethodSignature *sig;
109         GSList *builders;
110         GHashTable *region_to_handler;
111         LLVMBuilderRef alloca_builder;
112         LLVMValueRef last_alloca;
113         LLVMValueRef rgctx_arg;
114         LLVMTypeRef *vreg_types;
115         gboolean *is_dead;
116         gboolean *unreachable;
117         int *pindexes;
118         LLVMValueRef imt_rgctx_loc;
119         GHashTable *llvm_types;
120         LLVMValueRef dbg_md;
121         MonoDebugMethodInfo *minfo;
122         char temp_name [32];
123 } EmitContext;
124
125 typedef struct {
126         MonoBasicBlock *bb;
127         MonoInst *phi;
128         MonoBasicBlock *in_bb;
129         int sreg;
130 } PhiNode;
131
132 /*
133  * Instruction metadata
134  * This is the same as ins_info, but LREG != IREG.
135  */
136 #ifdef MINI_OP
137 #undef MINI_OP
138 #endif
139 #ifdef MINI_OP3
140 #undef MINI_OP3
141 #endif
142 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
143 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
144 #define NONE ' '
145 #define IREG 'i'
146 #define FREG 'f'
147 #define VREG 'v'
148 #define XREG 'x'
149 #define LREG 'l'
150 /* keep in sync with the enum in mini.h */
151 const char
152 llvm_ins_info[] = {
153 #include "mini-ops.h"
154 };
155 #undef MINI_OP
156 #undef MINI_OP3
157
158 #if SIZEOF_VOID_P == 4
159 #define GET_LONG_IMM(ins) (((guint64)(ins)->inst_ms_word << 32) | (guint64)(guint32)(ins)->inst_ls_word)
160 #else
161 #define GET_LONG_IMM(ins) ((ins)->inst_imm)
162 #endif
163
164 #define LLVM_INS_INFO(opcode) (&llvm_ins_info [((opcode) - OP_START - 1) * 4])
165
166 #if 0
167 #define TRACE_FAILURE(msg) do { printf ("%s\n", msg); } while (0)
168 #else
169 #define TRACE_FAILURE(msg)
170 #endif
171
172 #ifdef TARGET_X86
173 #define IS_TARGET_X86 1
174 #else
175 #define IS_TARGET_X86 0
176 #endif
177
178 #ifdef TARGET_AMD64
179 #define IS_TARGET_AMD64 1
180 #else
181 #define IS_TARGET_AMD64 0
182 #endif
183
184 #define LLVM_FAILURE(ctx, reason) do { \
185         TRACE_FAILURE (reason); \
186         (ctx)->cfg->exception_message = g_strdup (reason); \
187         (ctx)->cfg->disable_llvm = TRUE; \
188         goto FAILURE; \
189 } while (0)
190
191 #define CHECK_FAILURE(ctx) do { \
192     if ((ctx)->cfg->disable_llvm) \
193                 goto FAILURE; \
194 } while (0)
195
196 static LLVMIntPredicate cond_to_llvm_cond [] = {
197         LLVMIntEQ,
198         LLVMIntNE,
199         LLVMIntSLE,
200         LLVMIntSGE,
201         LLVMIntSLT,
202         LLVMIntSGT,
203         LLVMIntULE,
204         LLVMIntUGE,
205         LLVMIntULT,
206         LLVMIntUGT,
207 };
208
209 static LLVMRealPredicate fpcond_to_llvm_cond [] = {
210         LLVMRealOEQ,
211         LLVMRealUNE,
212         LLVMRealOLE,
213         LLVMRealOGE,
214         LLVMRealOLT,
215         LLVMRealOGT,
216         LLVMRealULE,
217         LLVMRealUGE,
218         LLVMRealULT,
219         LLVMRealUGT,
220 };
221
222 static MonoNativeTlsKey current_cfg_tls_id;
223
224 static MonoLLVMModule aot_module;
225 static int memset_param_count, memcpy_param_count;
226 static const char *memset_func_name;
227 static const char *memcpy_func_name;
228
229 static void init_jit_module (MonoDomain *domain);
230
231 static void emit_dbg_loc (EmitContext *ctx, LLVMBuilderRef builder, const unsigned char *cil_code);
232 static LLVMValueRef emit_dbg_subprogram (EmitContext *ctx, MonoCompile *cfg, LLVMValueRef method, const char *name);
233 static void emit_dbg_info (MonoLLVMModule *lmodule, const char *filename, const char *cu_name);
234
235 /*
236  * IntPtrType:
237  *
238  *   The LLVM type with width == sizeof (gpointer)
239  */
240 static LLVMTypeRef
241 IntPtrType (void)
242 {
243         return sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type ();
244 }
245
246 static LLVMTypeRef
247 ObjRefType (void)
248 {
249         return sizeof (gpointer) == 8 ? LLVMPointerType (LLVMInt64Type (), 0) : LLVMPointerType (LLVMInt32Type (), 0);
250 }
251
252 static LLVMTypeRef
253 ThisType (void)
254 {
255         return sizeof (gpointer) == 8 ? LLVMPointerType (LLVMInt64Type (), 0) : LLVMPointerType (LLVMInt32Type (), 0);
256 }
257
258 /*
259  * get_vtype_size:
260  *
261  *   Return the size of the LLVM representation of the vtype T.
262  */
263 static guint32
264 get_vtype_size (MonoType *t)
265 {
266         int size;
267
268         size = mono_class_value_size (mono_class_from_mono_type (t), NULL);
269
270         while (size < 2 * sizeof (gpointer) && mono_is_power_of_two (size) == -1)
271                 size ++;
272
273         return size;
274 }
275
276 /*
277  * simd_class_to_llvm_type:
278  *
279  *   Return the LLVM type corresponding to the Mono.SIMD class KLASS
280  */
281 static LLVMTypeRef
282 simd_class_to_llvm_type (EmitContext *ctx, MonoClass *klass)
283 {
284         if (!strcmp (klass->name, "Vector2d")) {
285                 return LLVMVectorType (LLVMDoubleType (), 2);
286         } else if (!strcmp (klass->name, "Vector2l")) {
287                 return LLVMVectorType (LLVMInt64Type (), 2);
288         } else if (!strcmp (klass->name, "Vector2ul")) {
289                 return LLVMVectorType (LLVMInt64Type (), 2);
290         } else if (!strcmp (klass->name, "Vector4i")) {
291                 return LLVMVectorType (LLVMInt32Type (), 4);
292         } else if (!strcmp (klass->name, "Vector4ui")) {
293                 return LLVMVectorType (LLVMInt32Type (), 4);
294         } else if (!strcmp (klass->name, "Vector4f")) {
295                 return LLVMVectorType (LLVMFloatType (), 4);
296         } else if (!strcmp (klass->name, "Vector8s")) {
297                 return LLVMVectorType (LLVMInt16Type (), 8);
298         } else if (!strcmp (klass->name, "Vector8us")) {
299                 return LLVMVectorType (LLVMInt16Type (), 8);
300         } else if (!strcmp (klass->name, "Vector16sb")) {
301                 return LLVMVectorType (LLVMInt8Type (), 16);
302         } else if (!strcmp (klass->name, "Vector16b")) {
303                 return LLVMVectorType (LLVMInt8Type (), 16);
304         } else {
305                 printf ("%s\n", klass->name);
306                 NOT_IMPLEMENTED;
307                 return NULL;
308         }
309 }
310
311 /* Return the 128 bit SIMD type corresponding to the mono type TYPE */
312 static inline G_GNUC_UNUSED LLVMTypeRef
313 type_to_simd_type (int type)
314 {
315         switch (type) {
316         case MONO_TYPE_I1:
317                 return LLVMVectorType (LLVMInt8Type (), 16);
318         case MONO_TYPE_I2:
319                 return LLVMVectorType (LLVMInt16Type (), 8);
320         case MONO_TYPE_I4:
321                 return LLVMVectorType (LLVMInt32Type (), 4);
322         case MONO_TYPE_I8:
323                 return LLVMVectorType (LLVMInt64Type (), 2);
324         case MONO_TYPE_R8:
325                 return LLVMVectorType (LLVMDoubleType (), 2);
326         case MONO_TYPE_R4:
327                 return LLVMVectorType (LLVMFloatType (), 4);
328         default:
329                 g_assert_not_reached ();
330                 return NULL;
331         }
332 }
333
334 static LLVMTypeRef
335 create_llvm_type_for_type (MonoClass *klass)
336 {
337         int i, size, nfields, esize;
338         LLVMTypeRef *eltypes;
339         char *name;
340         MonoType *t;
341         LLVMTypeRef ltype;
342
343         t = &klass->byval_arg;
344
345         if (mini_type_is_hfa (t, &nfields, &esize)) {
346                 /*
347                  * This is needed on arm64 where HFAs are returned in
348                  * registers.
349                  */
350                 size = nfields;
351                 eltypes = g_new (LLVMTypeRef, size);
352                 for (i = 0; i < size; ++i)
353                         eltypes [i] = esize == 4 ? LLVMFloatType () : LLVMDoubleType ();
354         } else {
355                 size = get_vtype_size (t);
356
357                 eltypes = g_new (LLVMTypeRef, size);
358                 for (i = 0; i < size; ++i)
359                         eltypes [i] = LLVMInt8Type ();
360         }
361
362         name = mono_type_full_name (&klass->byval_arg);
363         ltype = LLVMStructCreateNamed (LLVMGetGlobalContext (), name);
364         LLVMStructSetBody (ltype, eltypes, size, FALSE);
365         g_free (eltypes);
366         g_free (name);
367
368         return ltype;
369 }
370
371 /*
372  * type_to_llvm_type:
373  *
374  *   Return the LLVM type corresponding to T.
375  */
376 static LLVMTypeRef
377 type_to_llvm_type (EmitContext *ctx, MonoType *t)
378 {
379         if (t->byref)
380                 return LLVMPointerType (LLVMInt8Type (), 0);
381
382         t = mini_get_underlying_type (ctx->cfg, t);
383         switch (t->type) {
384         case MONO_TYPE_VOID:
385                 return LLVMVoidType ();
386         case MONO_TYPE_I1:
387                 return LLVMInt8Type ();
388         case MONO_TYPE_I2:
389                 return LLVMInt16Type ();
390         case MONO_TYPE_I4:
391                 return LLVMInt32Type ();
392         case MONO_TYPE_U1:
393                 return LLVMInt8Type ();
394         case MONO_TYPE_U2:
395                 return LLVMInt16Type ();
396         case MONO_TYPE_U4:
397                 return LLVMInt32Type ();
398         case MONO_TYPE_BOOLEAN:
399                 return LLVMInt8Type ();
400         case MONO_TYPE_I8:
401         case MONO_TYPE_U8:
402                 return LLVMInt64Type ();
403         case MONO_TYPE_CHAR:
404                 return LLVMInt16Type ();
405         case MONO_TYPE_R4:
406                 return LLVMFloatType ();
407         case MONO_TYPE_R8:
408                 return LLVMDoubleType ();
409         case MONO_TYPE_I:
410         case MONO_TYPE_U:
411                 return IntPtrType ();
412         case MONO_TYPE_OBJECT:
413         case MONO_TYPE_CLASS:
414         case MONO_TYPE_ARRAY:
415         case MONO_TYPE_SZARRAY:
416         case MONO_TYPE_STRING:
417         case MONO_TYPE_PTR:
418                 return ObjRefType ();
419         case MONO_TYPE_VAR:
420         case MONO_TYPE_MVAR:
421                 /* Because of generic sharing */
422                 return ObjRefType ();
423         case MONO_TYPE_GENERICINST:
424                 if (!mono_type_generic_inst_is_valuetype (t))
425                         return ObjRefType ();
426                 /* Fall through */
427         case MONO_TYPE_VALUETYPE:
428         case MONO_TYPE_TYPEDBYREF: {
429                 MonoClass *klass;
430                 LLVMTypeRef ltype;
431
432                 klass = mono_class_from_mono_type (t);
433
434                 if (MONO_CLASS_IS_SIMD (ctx->cfg, klass))
435                         return simd_class_to_llvm_type (ctx, klass);
436
437                 if (klass->enumtype)
438                         return type_to_llvm_type (ctx, mono_class_enum_basetype (klass));
439
440                 ltype = g_hash_table_lookup (ctx->lmodule->llvm_types, klass);
441                 if (!ltype) {
442                         ltype = create_llvm_type_for_type (klass);
443                         g_hash_table_insert (ctx->lmodule->llvm_types, klass, ltype);
444                 }
445                 return ltype;
446         }
447
448         default:
449                 printf ("X: %d\n", t->type);
450                 ctx->cfg->exception_message = g_strdup_printf ("type %s", mono_type_full_name (t));
451                 ctx->cfg->disable_llvm = TRUE;
452                 return NULL;
453         }
454 }
455
456 /*
457  * type_is_unsigned:
458  *
459  *   Return whenever T is an unsigned int type.
460  */
461 static gboolean
462 type_is_unsigned (EmitContext *ctx, MonoType *t)
463 {
464         if (t->byref)
465                 return FALSE;
466         switch (t->type) {
467         case MONO_TYPE_U1:
468         case MONO_TYPE_U2:
469         case MONO_TYPE_CHAR:
470         case MONO_TYPE_U4:
471         case MONO_TYPE_U8:
472                 return TRUE;
473         default:
474                 return FALSE;
475         }
476 }
477
478 /*
479  * type_to_llvm_arg_type:
480  *
481  *   Same as type_to_llvm_type, but treat i8/i16 as i32.
482  */
483 static LLVMTypeRef
484 type_to_llvm_arg_type (EmitContext *ctx, MonoType *t)
485 {
486         LLVMTypeRef ptype = type_to_llvm_type (ctx, t);
487
488         /*
489          * This works on all abis except arm64/ios which passes multiple
490          * arguments in one stack slot.
491          */
492 #ifndef TARGET_ARM64
493         if (ptype == LLVMInt8Type () || ptype == LLVMInt16Type ()) {
494                 /* 
495                  * LLVM generates code which only sets the lower bits, while JITted
496                  * code expects all the bits to be set.
497                  */
498                 ptype = LLVMInt32Type ();
499         }
500 #endif
501
502         return ptype;
503 }
504
505 /*
506  * llvm_type_to_stack_type:
507  *
508  *   Return the LLVM type which needs to be used when a value of type TYPE is pushed
509  * on the IL stack.
510  */
511 static G_GNUC_UNUSED LLVMTypeRef
512 llvm_type_to_stack_type (MonoCompile *cfg, LLVMTypeRef type)
513 {
514         if (type == NULL)
515                 return NULL;
516         if (type == LLVMInt8Type ())
517                 return LLVMInt32Type ();
518         else if (type == LLVMInt16Type ())
519                 return LLVMInt32Type ();
520         else if (!cfg->r4fp && type == LLVMFloatType ())
521                 return LLVMDoubleType ();
522         else
523                 return type;
524 }
525
526 /*
527  * regtype_to_llvm_type:
528  *
529  *   Return the LLVM type corresponding to the regtype C used in instruction 
530  * descriptions.
531  */
532 static LLVMTypeRef
533 regtype_to_llvm_type (char c)
534 {
535         switch (c) {
536         case 'i':
537                 return LLVMInt32Type ();
538         case 'l':
539                 return LLVMInt64Type ();
540         case 'f':
541                 return LLVMDoubleType ();
542         default:
543                 return NULL;
544         }
545 }
546
547 /*
548  * op_to_llvm_type:
549  *
550  *   Return the LLVM type corresponding to the unary/binary opcode OPCODE.
551  */
552 static LLVMTypeRef
553 op_to_llvm_type (int opcode)
554 {
555         switch (opcode) {
556         case OP_ICONV_TO_I1:
557         case OP_LCONV_TO_I1:
558                 return LLVMInt8Type ();
559         case OP_ICONV_TO_U1:
560         case OP_LCONV_TO_U1:
561                 return LLVMInt8Type ();
562         case OP_ICONV_TO_I2:
563         case OP_LCONV_TO_I2:
564                 return LLVMInt16Type ();
565         case OP_ICONV_TO_U2:
566         case OP_LCONV_TO_U2:
567                 return LLVMInt16Type ();
568         case OP_ICONV_TO_I4:
569         case OP_LCONV_TO_I4:
570                 return LLVMInt32Type ();
571         case OP_ICONV_TO_U4:
572         case OP_LCONV_TO_U4:
573                 return LLVMInt32Type ();
574         case OP_ICONV_TO_I8:
575                 return LLVMInt64Type ();
576         case OP_ICONV_TO_R4:
577                 return LLVMFloatType ();
578         case OP_ICONV_TO_R8:
579                 return LLVMDoubleType ();
580         case OP_ICONV_TO_U8:
581                 return LLVMInt64Type ();
582         case OP_FCONV_TO_I4:
583                 return LLVMInt32Type ();
584         case OP_FCONV_TO_I8:
585                 return LLVMInt64Type ();
586         case OP_FCONV_TO_I1:
587         case OP_FCONV_TO_U1:
588         case OP_RCONV_TO_I1:
589         case OP_RCONV_TO_U1:
590                 return LLVMInt8Type ();
591         case OP_FCONV_TO_I2:
592         case OP_FCONV_TO_U2:
593         case OP_RCONV_TO_I2:
594         case OP_RCONV_TO_U2:
595                 return LLVMInt16Type ();
596         case OP_FCONV_TO_I:
597         case OP_FCONV_TO_U:
598                 return sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type ();
599         case OP_IADD_OVF:
600         case OP_IADD_OVF_UN:
601         case OP_ISUB_OVF:
602         case OP_ISUB_OVF_UN:
603         case OP_IMUL_OVF:
604         case OP_IMUL_OVF_UN:
605                 return LLVMInt32Type ();
606         case OP_LADD_OVF:
607         case OP_LADD_OVF_UN:
608         case OP_LSUB_OVF:
609         case OP_LSUB_OVF_UN:
610         case OP_LMUL_OVF:
611         case OP_LMUL_OVF_UN:
612                 return LLVMInt64Type ();
613         default:
614                 printf ("%s\n", mono_inst_name (opcode));
615                 g_assert_not_reached ();
616                 return NULL;
617         }
618 }               
619
620 /*
621  * load_store_to_llvm_type:
622  *
623  *   Return the size/sign/zero extension corresponding to the load/store opcode
624  * OPCODE.
625  */
626 static LLVMTypeRef
627 load_store_to_llvm_type (int opcode, int *size, gboolean *sext, gboolean *zext)
628 {
629         *sext = FALSE;
630         *zext = FALSE;
631
632         switch (opcode) {
633         case OP_LOADI1_MEMBASE:
634         case OP_STOREI1_MEMBASE_REG:
635         case OP_STOREI1_MEMBASE_IMM:
636         case OP_ATOMIC_LOAD_I1:
637         case OP_ATOMIC_STORE_I1:
638                 *size = 1;
639                 *sext = TRUE;
640                 return LLVMInt8Type ();
641         case OP_LOADU1_MEMBASE:
642         case OP_LOADU1_MEM:
643         case OP_ATOMIC_LOAD_U1:
644         case OP_ATOMIC_STORE_U1:
645                 *size = 1;
646                 *zext = TRUE;
647                 return LLVMInt8Type ();
648         case OP_LOADI2_MEMBASE:
649         case OP_STOREI2_MEMBASE_REG:
650         case OP_STOREI2_MEMBASE_IMM:
651         case OP_ATOMIC_LOAD_I2:
652         case OP_ATOMIC_STORE_I2:
653                 *size = 2;
654                 *sext = TRUE;
655                 return LLVMInt16Type ();
656         case OP_LOADU2_MEMBASE:
657         case OP_LOADU2_MEM:
658         case OP_ATOMIC_LOAD_U2:
659         case OP_ATOMIC_STORE_U2:
660                 *size = 2;
661                 *zext = TRUE;
662                 return LLVMInt16Type ();
663         case OP_LOADI4_MEMBASE:
664         case OP_LOADU4_MEMBASE:
665         case OP_LOADI4_MEM:
666         case OP_LOADU4_MEM:
667         case OP_STOREI4_MEMBASE_REG:
668         case OP_STOREI4_MEMBASE_IMM:
669         case OP_ATOMIC_LOAD_I4:
670         case OP_ATOMIC_STORE_I4:
671         case OP_ATOMIC_LOAD_U4:
672         case OP_ATOMIC_STORE_U4:
673                 *size = 4;
674                 return LLVMInt32Type ();
675         case OP_LOADI8_MEMBASE:
676         case OP_LOADI8_MEM:
677         case OP_STOREI8_MEMBASE_REG:
678         case OP_STOREI8_MEMBASE_IMM:
679         case OP_ATOMIC_LOAD_I8:
680         case OP_ATOMIC_STORE_I8:
681         case OP_ATOMIC_LOAD_U8:
682         case OP_ATOMIC_STORE_U8:
683                 *size = 8;
684                 return LLVMInt64Type ();
685         case OP_LOADR4_MEMBASE:
686         case OP_STORER4_MEMBASE_REG:
687         case OP_ATOMIC_LOAD_R4:
688         case OP_ATOMIC_STORE_R4:
689                 *size = 4;
690                 return LLVMFloatType ();
691         case OP_LOADR8_MEMBASE:
692         case OP_STORER8_MEMBASE_REG:
693         case OP_ATOMIC_LOAD_R8:
694         case OP_ATOMIC_STORE_R8:
695                 *size = 8;
696                 return LLVMDoubleType ();
697         case OP_LOAD_MEMBASE:
698         case OP_LOAD_MEM:
699         case OP_STORE_MEMBASE_REG:
700         case OP_STORE_MEMBASE_IMM:
701                 *size = sizeof (gpointer);
702                 return IntPtrType ();
703         default:
704                 g_assert_not_reached ();
705                 return NULL;
706         }
707 }
708
709 /*
710  * ovf_op_to_intrins:
711  *
712  *   Return the LLVM intrinsics corresponding to the overflow opcode OPCODE.
713  */
714 static const char*
715 ovf_op_to_intrins (int opcode)
716 {
717         switch (opcode) {
718         case OP_IADD_OVF:
719                 return "llvm.sadd.with.overflow.i32";
720         case OP_IADD_OVF_UN:
721                 return "llvm.uadd.with.overflow.i32";
722         case OP_ISUB_OVF:
723                 return "llvm.ssub.with.overflow.i32";
724         case OP_ISUB_OVF_UN:
725                 return "llvm.usub.with.overflow.i32";
726         case OP_IMUL_OVF:
727                 return "llvm.smul.with.overflow.i32";
728         case OP_IMUL_OVF_UN:
729                 return "llvm.umul.with.overflow.i32";
730         case OP_LADD_OVF:
731                 return "llvm.sadd.with.overflow.i64";
732         case OP_LADD_OVF_UN:
733                 return "llvm.uadd.with.overflow.i64";
734         case OP_LSUB_OVF:
735                 return "llvm.ssub.with.overflow.i64";
736         case OP_LSUB_OVF_UN:
737                 return "llvm.usub.with.overflow.i64";
738         case OP_LMUL_OVF:
739                 return "llvm.smul.with.overflow.i64";
740         case OP_LMUL_OVF_UN:
741                 return "llvm.umul.with.overflow.i64";
742         default:
743                 g_assert_not_reached ();
744                 return NULL;
745         }
746 }
747
748 static const char*
749 simd_op_to_intrins (int opcode)
750 {
751         switch (opcode) {
752 #if defined(TARGET_X86) || defined(TARGET_AMD64)
753         case OP_MINPD:
754                 return "llvm.x86.sse2.min.pd";
755         case OP_MINPS:
756                 return "llvm.x86.sse.min.ps";
757         case OP_PMIND_UN:
758                 return "llvm.x86.sse41.pminud";
759         case OP_PMINW_UN:
760                 return "llvm.x86.sse41.pminuw";
761         case OP_PMINB_UN:
762                 return "llvm.x86.sse2.pminu.b";
763         case OP_PMINW:
764                 return "llvm.x86.sse2.pmins.w";
765         case OP_MAXPD:
766                 return "llvm.x86.sse2.max.pd";
767         case OP_MAXPS:
768                 return "llvm.x86.sse.max.ps";
769         case OP_HADDPD:
770                 return "llvm.x86.sse3.hadd.pd";
771         case OP_HADDPS:
772                 return "llvm.x86.sse3.hadd.ps";
773         case OP_HSUBPD:
774                 return "llvm.x86.sse3.hsub.pd";
775         case OP_HSUBPS:
776                 return "llvm.x86.sse3.hsub.ps";
777         case OP_PMAXD_UN:
778                 return "llvm.x86.sse41.pmaxud";
779         case OP_PMAXW_UN:
780                 return "llvm.x86.sse41.pmaxuw";
781         case OP_PMAXB_UN:
782                 return "llvm.x86.sse2.pmaxu.b";
783         case OP_ADDSUBPS:
784                 return "llvm.x86.sse3.addsub.ps";
785         case OP_ADDSUBPD:
786                 return "llvm.x86.sse3.addsub.pd";
787         case OP_EXTRACT_MASK:
788                 return "llvm.x86.sse2.pmovmskb.128";
789         case OP_PSHRW:
790         case OP_PSHRW_REG:
791                 return "llvm.x86.sse2.psrli.w";
792         case OP_PSHRD:
793         case OP_PSHRD_REG:
794                 return "llvm.x86.sse2.psrli.d";
795         case OP_PSHRQ:
796         case OP_PSHRQ_REG:
797                 return "llvm.x86.sse2.psrli.q";
798         case OP_PSHLW:
799         case OP_PSHLW_REG:
800                 return "llvm.x86.sse2.pslli.w";
801         case OP_PSHLD:
802         case OP_PSHLD_REG:
803                 return "llvm.x86.sse2.pslli.d";
804         case OP_PSHLQ:
805         case OP_PSHLQ_REG:
806                 return "llvm.x86.sse2.pslli.q";
807         case OP_PSARW:
808         case OP_PSARW_REG:
809                 return "llvm.x86.sse2.psrai.w";
810         case OP_PSARD:
811         case OP_PSARD_REG:
812                 return "llvm.x86.sse2.psrai.d";
813         case OP_PADDB_SAT:
814                 return "llvm.x86.sse2.padds.b";
815         case OP_PADDW_SAT:
816                 return "llvm.x86.sse2.padds.w";
817         case OP_PSUBB_SAT:
818                 return "llvm.x86.sse2.psubs.b";
819         case OP_PSUBW_SAT:
820                 return "llvm.x86.sse2.psubs.w";
821         case OP_PADDB_SAT_UN:
822                 return "llvm.x86.sse2.paddus.b";
823         case OP_PADDW_SAT_UN:
824                 return "llvm.x86.sse2.paddus.w";
825         case OP_PSUBB_SAT_UN:
826                 return "llvm.x86.sse2.psubus.b";
827         case OP_PSUBW_SAT_UN:
828                 return "llvm.x86.sse2.psubus.w";
829         case OP_PAVGB_UN:
830                 return "llvm.x86.sse2.pavg.b";
831         case OP_PAVGW_UN:
832                 return "llvm.x86.sse2.pavg.w";
833         case OP_SQRTPS:
834                 return "llvm.x86.sse.sqrt.ps";
835         case OP_SQRTPD:
836                 return "llvm.x86.sse2.sqrt.pd";
837         case OP_RSQRTPS:
838                 return "llvm.x86.sse.rsqrt.ps";
839         case OP_RCPPS:
840                 return "llvm.x86.sse.rcp.ps";
841         case OP_CVTDQ2PD:
842                 return "llvm.x86.sse2.cvtdq2pd";
843         case OP_CVTDQ2PS:
844                 return "llvm.x86.sse2.cvtdq2ps";
845         case OP_CVTPD2DQ:
846                 return "llvm.x86.sse2.cvtpd2dq";
847         case OP_CVTPS2DQ:
848                 return "llvm.x86.sse2.cvtps2dq";
849         case OP_CVTPD2PS:
850                 return "llvm.x86.sse2.cvtpd2ps";
851         case OP_CVTPS2PD:
852                 return "llvm.x86.sse2.cvtps2pd";
853         case OP_CVTTPD2DQ:
854                 return "llvm.x86.sse2.cvttpd2dq";
855         case OP_CVTTPS2DQ:
856                 return "llvm.x86.sse2.cvttps2dq";
857         case OP_COMPPS:
858                 return "llvm.x86.sse.cmp.ps";
859         case OP_COMPPD:
860                 return "llvm.x86.sse2.cmp.pd";
861         case OP_PACKW:
862                 return "llvm.x86.sse2.packsswb.128";
863         case OP_PACKD:
864                 return "llvm.x86.sse2.packssdw.128";
865         case OP_PACKW_UN:
866                 return "llvm.x86.sse2.packuswb.128";
867         case OP_PACKD_UN:
868                 return "llvm.x86.sse41.packusdw";
869         case OP_PMULW_HIGH:
870                 return "llvm.x86.sse2.pmulh.w";
871         case OP_PMULW_HIGH_UN:
872                 return "llvm.x86.sse2.pmulhu.w";
873 #endif
874         default:
875                 g_assert_not_reached ();
876                 return NULL;
877         }
878 }
879
880 static LLVMTypeRef
881 simd_op_to_llvm_type (int opcode)
882 {
883 #if defined(TARGET_X86) || defined(TARGET_AMD64)
884         switch (opcode) {
885         case OP_EXTRACT_R8:
886         case OP_EXPAND_R8:
887                 return type_to_simd_type (MONO_TYPE_R8);
888         case OP_EXTRACT_I8:
889         case OP_EXPAND_I8:
890                 return type_to_simd_type (MONO_TYPE_I8);
891         case OP_EXTRACT_I4:
892         case OP_EXPAND_I4:
893                 return type_to_simd_type (MONO_TYPE_I4);
894         case OP_EXTRACT_I2:
895         case OP_EXTRACT_U2:
896         case OP_EXTRACTX_U2:
897         case OP_EXPAND_I2:
898                 return type_to_simd_type (MONO_TYPE_I2);
899         case OP_EXTRACT_I1:
900         case OP_EXTRACT_U1:
901         case OP_EXPAND_I1:
902                 return type_to_simd_type (MONO_TYPE_I1);
903         case OP_EXPAND_R4:
904                 return type_to_simd_type (MONO_TYPE_R4);
905         case OP_CVTDQ2PD:
906         case OP_CVTDQ2PS:
907                 return type_to_simd_type (MONO_TYPE_I4);
908         case OP_CVTPD2DQ:
909         case OP_CVTPD2PS:
910         case OP_CVTTPD2DQ:
911                 return type_to_simd_type (MONO_TYPE_R8);
912         case OP_CVTPS2DQ:
913         case OP_CVTPS2PD:
914         case OP_CVTTPS2DQ:
915                 return type_to_simd_type (MONO_TYPE_R4);
916         case OP_EXTRACT_MASK:
917                 return type_to_simd_type (MONO_TYPE_I1);
918         case OP_SQRTPS:
919         case OP_RSQRTPS:
920         case OP_RCPPS:
921         case OP_DUPPS_LOW:
922         case OP_DUPPS_HIGH:
923                 return type_to_simd_type (MONO_TYPE_R4);
924         case OP_SQRTPD:
925         case OP_DUPPD:
926                 return type_to_simd_type (MONO_TYPE_R8);
927         default:
928                 g_assert_not_reached ();
929                 return NULL;
930         }
931 #else
932         return NULL;
933 #endif
934 }
935
936 /*
937  * get_bb:
938  *
939  *   Return the LLVM basic block corresponding to BB.
940  */
941 static LLVMBasicBlockRef
942 get_bb (EmitContext *ctx, MonoBasicBlock *bb)
943 {
944         char bb_name_buf [128];
945         char *bb_name;
946
947         if (ctx->bblocks [bb->block_num].bblock == NULL) {
948                 if (bb->flags & BB_EXCEPTION_HANDLER) {
949                         int clause_index = (mono_get_block_region_notry (ctx->cfg, bb->region) >> 8) - 1;
950                         sprintf (bb_name_buf, "EH_CLAUSE%d_BB%d", clause_index, bb->block_num);
951                         bb_name = bb_name_buf;
952                 } else if (bb->block_num < 256) {
953                         if (!ctx->lmodule->bb_names) {
954                                 ctx->lmodule->bb_names_len = 256;
955                                 ctx->lmodule->bb_names = g_new0 (char*, ctx->lmodule->bb_names_len);
956                         }
957                         if (!ctx->lmodule->bb_names [bb->block_num]) {
958                                 char *n;
959
960                                 n = g_strdup_printf ("BB%d", bb->block_num);
961                                 mono_memory_barrier ();
962                                 ctx->lmodule->bb_names [bb->block_num] = n;
963                         }
964                         bb_name = ctx->lmodule->bb_names [bb->block_num];
965                 } else {
966                         sprintf (bb_name_buf, "BB%d", bb->block_num);
967                         bb_name = bb_name_buf;
968                 }
969
970                 ctx->bblocks [bb->block_num].bblock = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
971                 ctx->bblocks [bb->block_num].end_bblock = ctx->bblocks [bb->block_num].bblock;
972         }
973
974         return ctx->bblocks [bb->block_num].bblock;
975 }
976
977 /* 
978  * get_end_bb:
979  *
980  *   Return the last LLVM bblock corresponding to BB.
981  * This might not be equal to the bb returned by get_bb () since we need to generate
982  * multiple LLVM bblocks for a mono bblock to handle throwing exceptions.
983  */
984 static LLVMBasicBlockRef
985 get_end_bb (EmitContext *ctx, MonoBasicBlock *bb)
986 {
987         get_bb (ctx, bb);
988         return ctx->bblocks [bb->block_num].end_bblock;
989 }
990
991 static LLVMBasicBlockRef
992 gen_bb (EmitContext *ctx, const char *prefix)
993 {
994         char bb_name [128];
995
996         sprintf (bb_name, "%s%d", prefix, ++ ctx->ex_index);
997         return LLVMAppendBasicBlock (ctx->lmethod, bb_name);
998 }
999
1000 /*
1001  * resolve_patch:
1002  *
1003  *   Return the target of the patch identified by TYPE and TARGET.
1004  */
1005 static gpointer
1006 resolve_patch (MonoCompile *cfg, MonoJumpInfoType type, gconstpointer target)
1007 {
1008         MonoJumpInfo ji;
1009
1010         memset (&ji, 0, sizeof (ji));
1011         ji.type = type;
1012         ji.data.target = target;
1013
1014         return mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &ji, FALSE);
1015 }
1016
1017 /*
1018  * convert_full:
1019  *
1020  *   Emit code to convert the LLVM value V to DTYPE.
1021  */
1022 static LLVMValueRef
1023 convert_full (EmitContext *ctx, LLVMValueRef v, LLVMTypeRef dtype, gboolean is_unsigned)
1024 {
1025         LLVMTypeRef stype = LLVMTypeOf (v);
1026
1027         if (stype != dtype) {
1028                 gboolean ext = FALSE;
1029
1030                 /* Extend */
1031                 if (dtype == LLVMInt64Type () && (stype == LLVMInt32Type () || stype == LLVMInt16Type () || stype == LLVMInt8Type ()))
1032                         ext = TRUE;
1033                 else if (dtype == LLVMInt32Type () && (stype == LLVMInt16Type () || stype == LLVMInt8Type ()))
1034                         ext = TRUE;
1035                 else if (dtype == LLVMInt16Type () && (stype == LLVMInt8Type ()))
1036                         ext = TRUE;
1037
1038                 if (ext)
1039                         return is_unsigned ? LLVMBuildZExt (ctx->builder, v, dtype, "") : LLVMBuildSExt (ctx->builder, v, dtype, "");
1040
1041                 if (dtype == LLVMDoubleType () && stype == LLVMFloatType ())
1042                         return LLVMBuildFPExt (ctx->builder, v, dtype, "");
1043
1044                 /* Trunc */
1045                 if (stype == LLVMInt64Type () && (dtype == LLVMInt32Type () || dtype == LLVMInt16Type () || dtype == LLVMInt8Type ()))
1046                         return LLVMBuildTrunc (ctx->builder, v, dtype, "");
1047                 if (stype == LLVMInt32Type () && (dtype == LLVMInt16Type () || dtype == LLVMInt8Type ()))
1048                         return LLVMBuildTrunc (ctx->builder, v, dtype, "");
1049                 if (stype == LLVMInt16Type () && dtype == LLVMInt8Type ())
1050                         return LLVMBuildTrunc (ctx->builder, v, dtype, "");
1051                 if (stype == LLVMDoubleType () && dtype == LLVMFloatType ())
1052                         return LLVMBuildFPTrunc (ctx->builder, v, dtype, "");
1053
1054                 if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind && LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
1055                         return LLVMBuildBitCast (ctx->builder, v, dtype, "");
1056                 if (LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
1057                         return LLVMBuildIntToPtr (ctx->builder, v, dtype, "");
1058                 if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind)
1059                         return LLVMBuildPtrToInt (ctx->builder, v, dtype, "");
1060
1061                 if (mono_arch_is_soft_float ()) {
1062                         if (stype == LLVMInt32Type () && dtype == LLVMFloatType ())
1063                                 return LLVMBuildBitCast (ctx->builder, v, dtype, "");
1064                         if (stype == LLVMInt32Type () && dtype == LLVMDoubleType ())
1065                                 return LLVMBuildBitCast (ctx->builder, LLVMBuildZExt (ctx->builder, v, LLVMInt64Type (), ""), dtype, "");
1066                 }
1067
1068                 if (LLVMGetTypeKind (stype) == LLVMVectorTypeKind && LLVMGetTypeKind (dtype) == LLVMVectorTypeKind)
1069                         return LLVMBuildBitCast (ctx->builder, v, dtype, "");
1070
1071                 LLVMDumpValue (v);
1072                 LLVMDumpValue (LLVMConstNull (dtype));
1073                 g_assert_not_reached ();
1074                 return NULL;
1075         } else {
1076                 return v;
1077         }
1078 }
1079
1080 static LLVMValueRef
1081 convert (EmitContext *ctx, LLVMValueRef v, LLVMTypeRef dtype)
1082 {
1083         return convert_full (ctx, v, dtype, FALSE);
1084 }
1085
1086 /*
1087  * emit_volatile_load:
1088  *
1089  *   If vreg is volatile, emit a load from its address.
1090  */
1091 static LLVMValueRef
1092 emit_volatile_load (EmitContext *ctx, int vreg)
1093 {
1094         MonoType *t;
1095
1096         LLVMValueRef v = LLVMBuildLoad (ctx->builder, ctx->addresses [vreg], "");
1097         t = ctx->vreg_cli_types [vreg];
1098         if (t && !t->byref) {
1099                 /* 
1100                  * Might have to zero extend since llvm doesn't have 
1101                  * unsigned types.
1102                  */
1103                 if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_U2 || t->type == MONO_TYPE_CHAR || t->type == MONO_TYPE_BOOLEAN)
1104                         v = LLVMBuildZExt (ctx->builder, v, LLVMInt32Type (), "");
1105                 else if (t->type == MONO_TYPE_U8)
1106                         v = LLVMBuildZExt (ctx->builder, v, LLVMInt64Type (), "");
1107         }
1108
1109         return v;
1110 }
1111
1112 /*
1113  * emit_volatile_store:
1114  *
1115  *   If VREG is volatile, emit a store from its value to its address.
1116  */
1117 static void
1118 emit_volatile_store (EmitContext *ctx, int vreg)
1119 {
1120         MonoInst *var = get_vreg_to_inst (ctx->cfg, vreg);
1121
1122         if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
1123                 g_assert (ctx->addresses [vreg]);
1124                 LLVMBuildStore (ctx->builder, convert (ctx, ctx->values [vreg], type_to_llvm_type (ctx, var->inst_vtype)), ctx->addresses [vreg]);
1125         }
1126 }
1127
1128 typedef struct {
1129         /* 
1130          * Maps parameter indexes in the original signature to parameter indexes
1131          * in the LLVM signature.
1132          */
1133         int *pindexes;
1134         /* The indexes of various special arguments in the LLVM signature */
1135         int vret_arg_pindex, this_arg_pindex, rgctx_arg_pindex, imt_arg_pindex;
1136 } LLVMSigInfo;
1137
1138 /*
1139  * sig_to_llvm_sig_full:
1140  *
1141  *   Return the LLVM signature corresponding to the mono signature SIG using the
1142  * calling convention information in CINFO. Return parameter mapping information in SINFO.
1143  */
1144 static LLVMTypeRef
1145 sig_to_llvm_sig_full (EmitContext *ctx, MonoMethodSignature *sig, LLVMCallInfo *cinfo,
1146                                           LLVMSigInfo *sinfo)
1147 {
1148         LLVMTypeRef ret_type;
1149         LLVMTypeRef *param_types = NULL;
1150         LLVMTypeRef res;
1151         int i, j, pindex, vret_arg_pindex = 0;
1152         int *pindexes;
1153         gboolean vretaddr = FALSE;
1154         MonoType *rtype;
1155
1156         if (sinfo)
1157                 memset (sinfo, 0, sizeof (LLVMSigInfo));
1158
1159         rtype = mini_get_underlying_type (ctx->cfg, sig->ret);
1160         ret_type = type_to_llvm_type (ctx, rtype);
1161         CHECK_FAILURE (ctx);
1162
1163         if (cinfo) {
1164                 if (cinfo->ret.storage == LLVMArgVtypeInReg) {
1165                         /* LLVM models this by returning an aggregate value */
1166                         if (cinfo->ret.pair_storage [0] == LLVMArgInIReg && cinfo->ret.pair_storage [1] == LLVMArgNone) {
1167                                 LLVMTypeRef members [2];
1168
1169                                 members [0] = IntPtrType ();
1170                                 ret_type = LLVMStructType (members, 1, FALSE);
1171                         } else {
1172                                 g_assert_not_reached ();
1173                         }
1174                 } else if (cinfo->ret.storage == LLVMArgVtypeByVal) {
1175                         /* Vtype returned normally by val */
1176                 } else if (cinfo->ret.storage == LLVMArgFpStruct) {
1177                         /* Vtype returned as a fp struct */
1178                         LLVMTypeRef members [16];
1179
1180                         /* Have to create our own structure since we don't map fp structures to LLVM fp structures yet */
1181                         for (i = 0; i < cinfo->ret.nslots; ++i)
1182                                 members [i] = cinfo->ret.esize == 8 ? LLVMDoubleType () : LLVMFloatType ();
1183                         ret_type = LLVMStructType (members, cinfo->ret.nslots, FALSE);
1184                 } else if (mini_type_is_vtype (ctx->cfg, rtype)) {
1185                         g_assert (cinfo->ret.storage == LLVMArgVtypeRetAddr);
1186                         vretaddr = TRUE;
1187                         ret_type = LLVMVoidType ();
1188                 }
1189         }
1190
1191         pindexes = g_new0 (int, sig->param_count);
1192         param_types = g_new0 (LLVMTypeRef, (sig->param_count * 8) + 3);
1193         pindex = 0;
1194         if (cinfo && cinfo->rgctx_arg) {
1195                 if (sinfo)
1196                         sinfo->rgctx_arg_pindex = pindex;
1197                 param_types [pindex] = ctx->lmodule->ptr_type;
1198                 pindex ++;
1199         }
1200         if (cinfo && cinfo->imt_arg) {
1201                 if (sinfo)
1202                         sinfo->imt_arg_pindex = pindex;
1203                 param_types [pindex] = ctx->lmodule->ptr_type;
1204                 pindex ++;
1205         }
1206         if (vretaddr) {
1207                 /* Compute the index in the LLVM signature where the vret arg needs to be passed */
1208                 vret_arg_pindex = pindex;
1209                 if (cinfo->vret_arg_index == 1) {
1210                         /* Add the slots consumed by the first argument */
1211                         LLVMArgInfo *ainfo = &cinfo->args [0];
1212                         switch (ainfo->storage) {
1213                         case LLVMArgVtypeInReg:
1214                                 for (j = 0; j < 2; ++j) {
1215                                         if (ainfo->pair_storage [j] == LLVMArgInIReg)
1216                                                 vret_arg_pindex ++;
1217                                 }
1218                                 break;
1219                         default:
1220                                 vret_arg_pindex ++;
1221                         }
1222                 }
1223
1224                 if (sinfo)
1225                         sinfo->vret_arg_pindex = vret_arg_pindex;
1226         }                               
1227
1228         if (vretaddr && vret_arg_pindex == pindex)
1229                 param_types [pindex ++] = IntPtrType ();
1230         if (sig->hasthis) {
1231                 if (sinfo)
1232                         sinfo->this_arg_pindex = pindex;
1233                 param_types [pindex ++] = ThisType ();
1234         }
1235         if (vretaddr && vret_arg_pindex == pindex)
1236                 param_types [pindex ++] = IntPtrType ();
1237         for (i = 0; i < sig->param_count; ++i) {
1238                 LLVMArgInfo *ainfo = cinfo ? &cinfo->args [i + sig->hasthis] : NULL;
1239
1240                 if (vretaddr && vret_arg_pindex == pindex)
1241                         param_types [pindex ++] = IntPtrType ();
1242                 pindexes [i] = pindex;
1243
1244                 if (!ainfo) {
1245                         param_types [pindex ++] = type_to_llvm_arg_type (ctx, sig->params [i]);
1246                         continue;
1247                 }
1248
1249                 switch (ainfo->storage) {
1250                 case LLVMArgVtypeInReg:
1251                         for (j = 0; j < 2; ++j) {
1252                                 switch (ainfo->pair_storage [j]) {
1253                                 case LLVMArgInIReg:
1254                                         param_types [pindex ++] = LLVMIntType (sizeof (gpointer) * 8);
1255                                         break;
1256                                 case LLVMArgNone:
1257                                         break;
1258                                 default:
1259                                         g_assert_not_reached ();
1260                                 }
1261                         }
1262                         break;
1263                 case LLVMArgVtypeByVal:
1264                         param_types [pindex] = type_to_llvm_arg_type (ctx, sig->params [i]);
1265                         CHECK_FAILURE (ctx);
1266                         param_types [pindex] = LLVMPointerType (param_types [pindex], 0);
1267                         pindex ++;
1268                         break;
1269                 case LLVMArgAsIArgs:
1270                         param_types [pindex] = LLVMArrayType (IntPtrType (), ainfo->nslots);
1271                         pindex ++;
1272                         break;
1273                 case LLVMArgAsFpArgs: {
1274                         int j;
1275
1276                         for (j = 0; j < ainfo->nslots; ++j)
1277                                 param_types [pindex + j] = ainfo->esize == 8 ? LLVMDoubleType () : LLVMFloatType ();
1278                         pindex += ainfo->nslots;
1279                         break;
1280                 }
1281                 default:
1282                         param_types [pindex ++] = type_to_llvm_arg_type (ctx, sig->params [i]);
1283                         break;
1284                 }
1285         }
1286         if (vretaddr && vret_arg_pindex == pindex)
1287                 param_types [pindex ++] = IntPtrType ();
1288
1289         CHECK_FAILURE (ctx);
1290
1291         res = LLVMFunctionType (ret_type, param_types, pindex, FALSE);
1292         g_free (param_types);
1293
1294         if (sinfo) {
1295                 sinfo->pindexes = pindexes;
1296         } else {
1297                 g_free (pindexes);
1298         }
1299
1300         return res;
1301
1302  FAILURE:
1303         g_free (param_types);
1304
1305         return NULL;
1306 }
1307
1308 static LLVMTypeRef
1309 sig_to_llvm_sig (EmitContext *ctx, MonoMethodSignature *sig)
1310 {
1311         return sig_to_llvm_sig_full (ctx, sig, NULL, NULL);
1312 }
1313
1314 /*
1315  * LLVMFunctionType1:
1316  *
1317  *   Create an LLVM function type from the arguments.
1318  */
1319 static G_GNUC_UNUSED LLVMTypeRef 
1320 LLVMFunctionType1(LLVMTypeRef ReturnType,
1321                                   LLVMTypeRef ParamType1,
1322                                   int IsVarArg)
1323 {
1324         LLVMTypeRef param_types [1];
1325
1326         param_types [0] = ParamType1;
1327
1328         return LLVMFunctionType (ReturnType, param_types, 1, IsVarArg);
1329 }
1330
1331 /*
1332  * LLVMFunctionType2:
1333  *
1334  *   Create an LLVM function type from the arguments.
1335  */
1336 static G_GNUC_UNUSED LLVMTypeRef
1337 LLVMFunctionType2(LLVMTypeRef ReturnType,
1338                                   LLVMTypeRef ParamType1,
1339                                   LLVMTypeRef ParamType2,
1340                                   int IsVarArg)
1341 {
1342         LLVMTypeRef param_types [2];
1343
1344         param_types [0] = ParamType1;
1345         param_types [1] = ParamType2;
1346
1347         return LLVMFunctionType (ReturnType, param_types, 2, IsVarArg);
1348 }
1349
1350 /*
1351  * LLVMFunctionType3:
1352  *
1353  *   Create an LLVM function type from the arguments.
1354  */
1355 static G_GNUC_UNUSED LLVMTypeRef
1356 LLVMFunctionType3(LLVMTypeRef ReturnType,
1357                                   LLVMTypeRef ParamType1,
1358                                   LLVMTypeRef ParamType2,
1359                                   LLVMTypeRef ParamType3,
1360                                   int IsVarArg)
1361 {
1362         LLVMTypeRef param_types [3];
1363
1364         param_types [0] = ParamType1;
1365         param_types [1] = ParamType2;
1366         param_types [2] = ParamType3;
1367
1368         return LLVMFunctionType (ReturnType, param_types, 3, IsVarArg);
1369 }
1370
1371 /*
1372  * create_builder:
1373  *
1374  *   Create an LLVM builder and remember it so it can be freed later.
1375  */
1376 static LLVMBuilderRef
1377 create_builder (EmitContext *ctx)
1378 {
1379         LLVMBuilderRef builder = LLVMCreateBuilder ();
1380
1381         ctx->builders = g_slist_prepend_mempool (ctx->cfg->mempool, ctx->builders, builder);
1382
1383         return builder;
1384 }
1385
1386 static LLVMValueRef
1387 get_plt_entry (EmitContext *ctx, LLVMTypeRef llvm_sig, MonoJumpInfoType type, gconstpointer data)
1388 {
1389         char *callee_name = mono_aot_get_plt_symbol (type, data);
1390         LLVMValueRef callee;
1391         MonoJumpInfo *ji = NULL;
1392
1393         if (!callee_name)
1394                 return NULL;
1395
1396         if (ctx->cfg->compile_aot)
1397                 /* Add a patch so referenced wrappers can be compiled in full aot mode */
1398                 mono_add_patch_info (ctx->cfg, 0, type, data);
1399
1400         // FIXME: Locking
1401         callee = g_hash_table_lookup (ctx->lmodule->plt_entries, callee_name);
1402         if (!callee) {
1403                 callee = LLVMAddFunction (ctx->module, callee_name, llvm_sig);
1404
1405                 LLVMSetVisibility (callee, LLVMHiddenVisibility);
1406
1407                 g_hash_table_insert (ctx->lmodule->plt_entries, (char*)callee_name, callee);
1408         }
1409
1410         if (ctx->cfg->compile_aot) {
1411                 ji = g_new0 (MonoJumpInfo, 1);
1412                 ji->type = type;
1413                 ji->data.target = data;
1414
1415                 g_hash_table_insert (ctx->lmodule->plt_entries_ji, ji, callee);
1416         }
1417
1418         return callee;
1419 }
1420
1421 static int
1422 get_handler_clause (MonoCompile *cfg, MonoBasicBlock *bb)
1423 {
1424         MonoMethodHeader *header = cfg->header;
1425         MonoExceptionClause *clause;
1426         int i;
1427
1428         /* Directly */
1429         if (bb->region != -1 && MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))
1430                 return (bb->region >> 8) - 1;
1431
1432         /* Indirectly */
1433         for (i = 0; i < header->num_clauses; ++i) {
1434                 clause = &header->clauses [i];
1435                            
1436                 if (MONO_OFFSET_IN_CLAUSE (clause, bb->real_offset) && clause->flags == MONO_EXCEPTION_CLAUSE_NONE)
1437                         return i;
1438         }
1439
1440         return -1;
1441 }
1442
1443 static void
1444 set_metadata_flag (LLVMValueRef v, const char *flag_name)
1445 {
1446         LLVMValueRef md_arg;
1447         int md_kind;
1448
1449         md_kind = LLVMGetMDKindID (flag_name, strlen (flag_name));
1450         md_arg = LLVMMDString ("mono", 4);
1451         LLVMSetMetadata (v, md_kind, LLVMMDNode (&md_arg, 1));
1452 }
1453
1454 static void
1455 set_invariant_load_flag (LLVMValueRef v)
1456 {
1457         LLVMValueRef md_arg;
1458         int md_kind;
1459         const char *flag_name;
1460
1461         // FIXME: Cache this
1462         flag_name = "invariant.load";
1463         md_kind = LLVMGetMDKindID (flag_name, strlen (flag_name));
1464         md_arg = LLVMMDString ("<index>", strlen ("<index>"));
1465         LLVMSetMetadata (v, md_kind, LLVMMDNode (&md_arg, 1));
1466 }
1467
1468 /*
1469  * emit_call:
1470  *
1471  *   Emit an LLVM call or invoke instruction depending on whenever the call is inside
1472  * a try region.
1473  */
1474 static LLVMValueRef
1475 emit_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, LLVMValueRef callee, LLVMValueRef *args, int pindex)
1476 {
1477         MonoCompile *cfg = ctx->cfg;
1478         LLVMValueRef lcall;
1479         LLVMBuilderRef builder = *builder_ref;
1480         int clause_index;
1481
1482         clause_index = get_handler_clause (cfg, bb);
1483
1484         if (clause_index != -1) {
1485                 MonoMethodHeader *header = cfg->header;
1486                 MonoExceptionClause *ec = &header->clauses [clause_index];
1487                 MonoBasicBlock *tblock;
1488                 LLVMBasicBlockRef ex_bb, noex_bb;
1489
1490                 /*
1491                  * Have to use an invoke instead of a call, branching to the
1492                  * handler bblock of the clause containing this bblock.
1493                  */
1494
1495                 g_assert (ec->flags == MONO_EXCEPTION_CLAUSE_NONE || ec->flags == MONO_EXCEPTION_CLAUSE_FINALLY);
1496
1497                 tblock = cfg->cil_offset_to_bb [ec->handler_offset];
1498                 g_assert (tblock);
1499
1500                 ctx->bblocks [tblock->block_num].invoke_target = TRUE;
1501
1502                 ex_bb = get_bb (ctx, tblock);
1503
1504                 noex_bb = gen_bb (ctx, "NOEX_BB");
1505
1506                 /* Use an invoke */
1507                 lcall = LLVMBuildInvoke (builder, callee, args, pindex, noex_bb, ex_bb, "");
1508
1509                 builder = ctx->builder = create_builder (ctx);
1510                 LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
1511
1512                 ctx->bblocks [bb->block_num].end_bblock = noex_bb;
1513         } else {
1514                 lcall = LLVMBuildCall (builder, callee, args, pindex, "");
1515                 ctx->builder = builder;
1516         }
1517
1518         *builder_ref = ctx->builder;
1519
1520         return lcall;
1521 }
1522
1523 static LLVMValueRef
1524 emit_load_general (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef addr, const char *name, gboolean is_faulting, BarrierKind barrier)
1525 {
1526         const char *intrins_name;
1527         LLVMValueRef args [16], res;
1528         LLVMTypeRef addr_type;
1529
1530         if (is_faulting && bb->region != -1) {
1531                 LLVMAtomicOrdering ordering;
1532
1533                 switch (barrier) {
1534                 case LLVM_BARRIER_NONE:
1535                         ordering = LLVMAtomicOrderingNotAtomic;
1536                         break;
1537                 case LLVM_BARRIER_ACQ:
1538                         ordering = LLVMAtomicOrderingAcquire;
1539                         break;
1540                 case LLVM_BARRIER_SEQ:
1541                         ordering = LLVMAtomicOrderingSequentiallyConsistent;
1542                         break;
1543                 default:
1544                         g_assert_not_reached ();
1545                         break;
1546                 }
1547
1548                 /*
1549                  * We handle loads which can fault by calling a mono specific intrinsic
1550                  * using an invoke, so they are handled properly inside try blocks.
1551                  * We can't use this outside clauses, since LLVM optimizes intrinsics which
1552                  * are marked with IntrReadArgMem.
1553                  */
1554                 switch (size) {
1555                 case 1:
1556                         intrins_name = "llvm.mono.load.i8.p0i8";
1557                         break;
1558                 case 2:
1559                         intrins_name = "llvm.mono.load.i16.p0i16";
1560                         break;
1561                 case 4:
1562                         intrins_name = "llvm.mono.load.i32.p0i32";
1563                         break;
1564                 case 8:
1565                         intrins_name = "llvm.mono.load.i64.p0i64";
1566                         break;
1567                 default:
1568                         g_assert_not_reached ();
1569                 }
1570
1571                 addr_type = LLVMTypeOf (addr);
1572                 if (addr_type == LLVMPointerType (LLVMDoubleType (), 0) || addr_type == LLVMPointerType (LLVMFloatType (), 0))
1573                         addr = LLVMBuildBitCast (*builder_ref, addr, LLVMPointerType (LLVMIntType (size * 8), 0), "");
1574
1575                 args [0] = addr;
1576                 args [1] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1577                 args [2] = LLVMConstInt (LLVMInt1Type (), TRUE, FALSE);
1578                 args [3] = LLVMConstInt (LLVMInt32Type (), ordering, FALSE);
1579                 res = emit_call (ctx, bb, builder_ref, LLVMGetNamedFunction (ctx->module, intrins_name), args, 4);
1580
1581                 if (addr_type == LLVMPointerType (LLVMDoubleType (), 0))
1582                         res = LLVMBuildBitCast (*builder_ref, res, LLVMDoubleType (), "");
1583                 else if (addr_type == LLVMPointerType (LLVMFloatType (), 0))
1584                         res = LLVMBuildBitCast (*builder_ref, res, LLVMFloatType (), "");
1585                 
1586                 return res;
1587         } else {
1588                 LLVMValueRef res;
1589
1590                 /* 
1591                  * We emit volatile loads for loads which can fault, because otherwise
1592                  * LLVM will generate invalid code when encountering a load from a
1593                  * NULL address.
1594                  */
1595                  res = mono_llvm_build_load (*builder_ref, addr, name, is_faulting, barrier);
1596
1597                  /* Mark it with a custom metadata */
1598                  /*
1599                  if (is_faulting)
1600                          set_metadata_flag (res, "mono.faulting.load");
1601                  */
1602
1603                  return res;
1604         }
1605 }
1606
1607 static LLVMValueRef
1608 emit_load (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef addr, const char *name, gboolean is_faulting)
1609 {
1610         return emit_load_general (ctx, bb, builder_ref, size, addr, name, is_faulting, LLVM_BARRIER_NONE);
1611 }
1612
1613 static void
1614 emit_store_general (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef value, LLVMValueRef addr, gboolean is_faulting, BarrierKind barrier)
1615 {
1616         const char *intrins_name;
1617         LLVMValueRef args [16];
1618
1619         if (is_faulting && bb->region != -1) {
1620                 LLVMAtomicOrdering ordering;
1621
1622                 switch (barrier) {
1623                 case LLVM_BARRIER_NONE:
1624                         ordering = LLVMAtomicOrderingNotAtomic;
1625                         break;
1626                 case LLVM_BARRIER_REL:
1627                         ordering = LLVMAtomicOrderingRelease;
1628                         break;
1629                 case LLVM_BARRIER_SEQ:
1630                         ordering = LLVMAtomicOrderingSequentiallyConsistent;
1631                         break;
1632                 default:
1633                         g_assert_not_reached ();
1634                         break;
1635                 }
1636
1637                 switch (size) {
1638                 case 1:
1639                         intrins_name = "llvm.mono.store.i8.p0i8";
1640                         break;
1641                 case 2:
1642                         intrins_name = "llvm.mono.store.i16.p0i16";
1643                         break;
1644                 case 4:
1645                         intrins_name = "llvm.mono.store.i32.p0i32";
1646                         break;
1647                 case 8:
1648                         intrins_name = "llvm.mono.store.i64.p0i64";
1649                         break;
1650                 default:
1651                         g_assert_not_reached ();
1652                 }
1653
1654                 if (LLVMTypeOf (value) == LLVMDoubleType () || LLVMTypeOf (value) == LLVMFloatType ()) {
1655                         value = LLVMBuildBitCast (*builder_ref, value, LLVMIntType (size * 8), "");
1656                         addr = LLVMBuildBitCast (*builder_ref, addr, LLVMPointerType (LLVMIntType (size * 8), 0), "");
1657                 }
1658
1659                 args [0] = value;
1660                 args [1] = addr;
1661                 args [2] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1662                 args [3] = LLVMConstInt (LLVMInt1Type (), TRUE, FALSE);
1663                 args [4] = LLVMConstInt (LLVMInt32Type (), ordering, FALSE);
1664                 emit_call (ctx, bb, builder_ref, LLVMGetNamedFunction (ctx->module, intrins_name), args, 5);
1665         } else {
1666                 mono_llvm_build_store (*builder_ref, value, addr, is_faulting, barrier);
1667         }
1668 }
1669
1670 static void
1671 emit_store (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef value, LLVMValueRef addr, gboolean is_faulting)
1672 {
1673         emit_store_general (ctx, bb, builder_ref, size, value, addr, is_faulting, LLVM_BARRIER_NONE);
1674 }
1675
1676 /*
1677  * emit_cond_system_exception:
1678  *
1679  *   Emit code to throw the exception EXC_TYPE if the condition CMP is false.
1680  * Might set the ctx exception.
1681  */
1682 static void
1683 emit_cond_system_exception (EmitContext *ctx, MonoBasicBlock *bb, const char *exc_type, LLVMValueRef cmp)
1684 {
1685         LLVMBasicBlockRef ex_bb, noex_bb;
1686         LLVMBuilderRef builder;
1687         MonoClass *exc_class;
1688         LLVMValueRef args [2];
1689         
1690         ex_bb = gen_bb (ctx, "EX_BB");
1691         noex_bb = gen_bb (ctx, "NOEX_BB");
1692
1693         LLVMBuildCondBr (ctx->builder, cmp, ex_bb, noex_bb);
1694
1695         exc_class = mono_class_from_name (mono_get_corlib (), "System", exc_type);
1696         g_assert (exc_class);
1697
1698         /* Emit exception throwing code */
1699         builder = create_builder (ctx);
1700         LLVMPositionBuilderAtEnd (builder, ex_bb);
1701
1702         if (!ctx->lmodule->throw_corlib_exception) {
1703                 LLVMValueRef callee;
1704                 LLVMTypeRef sig;
1705                 const char *icall_name;
1706
1707                 MonoMethodSignature *throw_sig = mono_metadata_signature_alloc (mono_get_corlib (), 2);
1708                 throw_sig->ret = &mono_get_void_class ()->byval_arg;
1709                 throw_sig->params [0] = &mono_get_int32_class ()->byval_arg;
1710                 icall_name = "llvm_throw_corlib_exception_abs_trampoline";
1711                 /* This will become i8* */
1712                 throw_sig->params [1] = &mono_get_byte_class ()->this_arg;
1713                 sig = sig_to_llvm_sig (ctx, throw_sig);
1714
1715                 if (ctx->cfg->compile_aot) {
1716                         callee = get_plt_entry (ctx, sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
1717                 } else {
1718                         callee = LLVMAddFunction (ctx->module, "llvm_throw_corlib_exception_trampoline", sig_to_llvm_sig (ctx, throw_sig));
1719
1720                         /*
1721                          * Differences between the LLVM/non-LLVM throw corlib exception trampoline:
1722                          * - On x86, LLVM generated code doesn't push the arguments
1723                          * - The trampoline takes the throw address as an arguments, not a pc offset.
1724                          */
1725                         LLVMAddGlobalMapping (ctx->lmodule->ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
1726                 }
1727
1728                 mono_memory_barrier ();
1729                 ctx->lmodule->throw_corlib_exception = callee;
1730         }
1731
1732         if (IS_TARGET_X86 || IS_TARGET_AMD64)
1733                 args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token - MONO_TOKEN_TYPE_DEF, FALSE);
1734         else
1735                 args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token, FALSE);
1736
1737         /*
1738          * The LLVM mono branch contains changes so a block address can be passed as an
1739          * argument to a call.
1740          */
1741         args [1] = LLVMBlockAddress (ctx->lmethod, ex_bb);
1742         emit_call (ctx, bb, &builder, ctx->lmodule->throw_corlib_exception, args, 2);
1743
1744         LLVMBuildUnreachable (builder);
1745
1746         ctx->builder = create_builder (ctx);
1747         LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
1748
1749         ctx->bblocks [bb->block_num].end_bblock = noex_bb;
1750
1751         ctx->ex_index ++;
1752         return;
1753 }
1754
1755 /*
1756  * emit_args_to_vtype:
1757  *
1758  *   Emit code to store the vtype in the arguments args to the address ADDRESS.
1759  */
1760 static void
1761 emit_args_to_vtype (EmitContext *ctx, LLVMBuilderRef builder, MonoType *t, LLVMValueRef address, LLVMArgInfo *ainfo, LLVMValueRef *args)
1762 {
1763         int j, size, nslots;
1764
1765         size = get_vtype_size (t);
1766
1767         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
1768                 address = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (LLVMInt8Type (), 0), "");
1769         }
1770
1771         if (ainfo->storage == LLVMArgAsFpArgs)
1772                 nslots = ainfo->nslots;
1773         else
1774                 nslots = 2;
1775
1776         for (j = 0; j < nslots; ++j) {
1777                 LLVMValueRef index [2], addr, daddr;
1778                 int part_size = size > sizeof (gpointer) ? sizeof (gpointer) : size;
1779                 LLVMTypeRef part_type;
1780
1781                 if (ainfo->pair_storage [j] == LLVMArgNone)
1782                         continue;
1783
1784                 switch (ainfo->pair_storage [j]) {
1785                 case LLVMArgInIReg: {
1786                         part_type = LLVMIntType (part_size * 8);
1787                         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
1788                                 index [0] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
1789                                 addr = LLVMBuildGEP (builder, address, index, 1, "");
1790                         } else {
1791                                 daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (IntPtrType (), 0), "");
1792                                 index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
1793                                 addr = LLVMBuildGEP (builder, daddr, index, 1, "");
1794                         }
1795                         LLVMBuildStore (builder, convert (ctx, args [j], part_type), LLVMBuildBitCast (ctx->builder, addr, LLVMPointerType (part_type, 0), ""));
1796                         break;
1797                 }
1798                 case LLVMArgInFPReg: {
1799                         LLVMTypeRef arg_type;
1800
1801                         if (ainfo->esize == 8)
1802                                 arg_type = LLVMDoubleType ();
1803                         else
1804                                 arg_type = LLVMFloatType ();
1805
1806                         index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
1807                         daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (arg_type, 0), "");
1808                         addr = LLVMBuildGEP (builder, daddr, index, 1, "");
1809                         LLVMBuildStore (builder, args [j], addr);
1810                         break;
1811                 }
1812                 case LLVMArgNone:
1813                         break;
1814                 default:
1815                         g_assert_not_reached ();
1816                 }
1817
1818                 size -= sizeof (gpointer);
1819         }
1820 }
1821
1822 /*
1823  * emit_vtype_to_args:
1824  *
1825  *   Emit code to load a vtype at address ADDRESS into scalar arguments. Store the arguments
1826  * into ARGS, and the number of arguments into NARGS.
1827  */
1828 static void
1829 emit_vtype_to_args (EmitContext *ctx, LLVMBuilderRef builder, MonoType *t, LLVMValueRef address, LLVMArgInfo *ainfo, LLVMValueRef *args, guint32 *nargs)
1830 {
1831         int pindex = 0;
1832         int j, size, nslots;
1833         LLVMTypeRef arg_type;
1834
1835         size = get_vtype_size (t);
1836
1837         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t)))
1838                 address = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (LLVMInt8Type (), 0), "");
1839
1840         if (ainfo->storage == LLVMArgAsFpArgs)
1841                 nslots = ainfo->nslots;
1842         else
1843                 nslots = 2;
1844         for (j = 0; j < nslots; ++j) {
1845                 LLVMValueRef index [2], addr, daddr;
1846                 int partsize = size > sizeof (gpointer) ? sizeof (gpointer) : size;
1847
1848                 if (ainfo->pair_storage [j] == LLVMArgNone)
1849                         continue;
1850
1851                 switch (ainfo->pair_storage [j]) {
1852                 case LLVMArgInIReg:
1853                         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
1854                                 index [0] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
1855                                 addr = LLVMBuildGEP (builder, address, index, 1, "");
1856                         } else {
1857                                 daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (IntPtrType (), 0), "");
1858                                 index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
1859                                 addr = LLVMBuildGEP (builder, daddr, index, 1, "");
1860                         }
1861                         args [pindex ++] = convert (ctx, LLVMBuildLoad (builder, LLVMBuildBitCast (ctx->builder, addr, LLVMPointerType (LLVMIntType (partsize * 8), 0), ""), ""), IntPtrType ());
1862                         break;
1863                 case LLVMArgInFPReg:
1864                         if (ainfo->esize == 8)
1865                                 arg_type = LLVMDoubleType ();
1866                         else
1867                                 arg_type = LLVMFloatType ();
1868                         daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (arg_type, 0), "");
1869                         index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
1870                         addr = LLVMBuildGEP (builder, daddr, index, 1, "");
1871                         args [pindex ++] = LLVMBuildLoad (builder, addr, "");
1872                         break;
1873                 case LLVMArgNone:
1874                         break;
1875                 default:
1876                         g_assert_not_reached ();
1877                 }
1878                 size -= sizeof (gpointer);
1879         }
1880
1881         *nargs = pindex;
1882 }
1883
1884 static LLVMValueRef
1885 build_alloca_llvm_type (EmitContext *ctx, LLVMTypeRef t, int align)
1886 {
1887         /*
1888          * Have to place all alloca's at the end of the entry bb, since otherwise they would
1889          * get executed every time control reaches them.
1890          */
1891         LLVMPositionBuilder (ctx->alloca_builder, get_bb (ctx, ctx->cfg->bb_entry), ctx->last_alloca);
1892
1893         ctx->last_alloca = mono_llvm_build_alloca (ctx->alloca_builder, t, NULL, align, "");
1894         return ctx->last_alloca;
1895 }
1896
1897 static LLVMValueRef
1898 build_alloca (EmitContext *ctx, MonoType *t)
1899 {
1900         MonoClass *k = mono_class_from_mono_type (t);
1901         int align;
1902
1903         if (MONO_CLASS_IS_SIMD (ctx->cfg, k))
1904                 align = 16;
1905         else
1906                 align = mono_class_min_align (k);
1907
1908         /* Sometimes align is not a power of 2 */
1909         while (mono_is_power_of_two (align) == -1)
1910                 align ++;
1911
1912         return build_alloca_llvm_type (ctx, type_to_llvm_type (ctx, t), align);
1913 }
1914
1915 /*
1916  * Put the global into the 'llvm.used' array to prevent it from being optimized away.
1917  */
1918 static void
1919 mark_as_used (MonoLLVMModule *lmodule, LLVMValueRef global)
1920 {
1921         if (!lmodule->used)
1922                 lmodule->used = g_ptr_array_sized_new (16);
1923         g_ptr_array_add (lmodule->used, global);
1924 }
1925
1926 static void
1927 emit_llvm_used (MonoLLVMModule *lmodule)
1928 {
1929         LLVMModuleRef module = lmodule->module;
1930         LLVMTypeRef used_type;
1931         LLVMValueRef used, *used_elem;
1932         int i;
1933                 
1934         if (!lmodule->used)
1935                 return;
1936
1937         used_type = LLVMArrayType (LLVMPointerType (LLVMInt8Type (), 0), lmodule->used->len);
1938         used = LLVMAddGlobal (module, used_type, "llvm.used");
1939         used_elem = g_new0 (LLVMValueRef, lmodule->used->len);
1940         for (i = 0; i < lmodule->used->len; ++i)
1941                 used_elem [i] = LLVMConstBitCast (g_ptr_array_index (lmodule->used, i), LLVMPointerType (LLVMInt8Type (), 0));
1942         LLVMSetInitializer (used, LLVMConstArray (LLVMPointerType (LLVMInt8Type (), 0), used_elem, lmodule->used->len));
1943         LLVMSetLinkage (used, LLVMAppendingLinkage);
1944         LLVMSetSection (used, "llvm.metadata");
1945 }
1946
1947 /*
1948  * emit_entry_bb:
1949  *
1950  *   Emit code to load/convert arguments.
1951  */
1952 static void
1953 emit_entry_bb (EmitContext *ctx, LLVMBuilderRef builder)
1954 {
1955         int i, pindex;
1956         MonoCompile *cfg = ctx->cfg;
1957         MonoMethodSignature *sig = ctx->sig;
1958         LLVMCallInfo *linfo = ctx->linfo;
1959         MonoBasicBlock *bb;
1960
1961         ctx->alloca_builder = create_builder (ctx);
1962
1963         /*
1964          * Handle indirect/volatile variables by allocating memory for them
1965          * using 'alloca', and storing their address in a temporary.
1966          */
1967         for (i = 0; i < cfg->num_varinfo; ++i) {
1968                 MonoInst *var = cfg->varinfo [i];
1969                 LLVMTypeRef vtype;
1970
1971                 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || mini_type_is_vtype (cfg, var->inst_vtype)) {
1972                         vtype = type_to_llvm_type (ctx, var->inst_vtype);
1973                         CHECK_FAILURE (ctx);
1974                         /* Could be already created by an OP_VPHI */
1975                         if (!ctx->addresses [var->dreg])
1976                                 ctx->addresses [var->dreg] = build_alloca (ctx, var->inst_vtype);
1977                         ctx->vreg_cli_types [var->dreg] = var->inst_vtype;
1978                 }
1979         }
1980
1981         for (i = 0; i < sig->param_count; ++i) {
1982                 LLVMArgInfo *ainfo = &linfo->args [i + sig->hasthis];
1983                 int reg = cfg->args [i + sig->hasthis]->dreg;
1984
1985                 switch (ainfo->storage) {
1986                 case LLVMArgVtypeInReg:
1987                 case LLVMArgAsFpArgs: {
1988                         LLVMValueRef args [8];
1989                         int j;
1990
1991                         /* The argument is received as a set of int/fp arguments, store them into the real argument */
1992                         memset (args, 0, sizeof (args));
1993                         pindex = ctx->pindexes [i];
1994                         if (ainfo->storage == LLVMArgVtypeInReg) {
1995                                 args [0] = LLVMGetParam (ctx->lmethod, pindex);
1996                                 if (ainfo->pair_storage [1] != LLVMArgNone)
1997                                         args [1] = LLVMGetParam (ctx->lmethod, pindex + 1);
1998                         } else {
1999                                 g_assert (ainfo->nslots <= 8);
2000                                 for (j = 0; j < ainfo->nslots; ++j)
2001                                         args [j] = LLVMGetParam (ctx->lmethod, ctx->pindexes [i] + j);
2002                         }
2003                         ctx->addresses [reg] = build_alloca (ctx, sig->params [i]);
2004
2005                         emit_args_to_vtype (ctx, builder, sig->params [i], ctx->addresses [reg], ainfo, args);
2006
2007                         if (ainfo->storage == LLVMArgVtypeInReg && MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (sig->params [i]))) {
2008                                 /* Treat these as normal values */
2009                                 ctx->values [reg] = LLVMBuildLoad (builder, ctx->addresses [reg], "");
2010                         }
2011                         break;
2012                 }
2013                 case LLVMArgVtypeByVal: {
2014                         ctx->addresses [reg] = LLVMGetParam (ctx->lmethod, ctx->pindexes [i]);
2015
2016                         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (sig->params [i]))) {
2017                                 /* Treat these as normal values */
2018                                 ctx->values [reg] = LLVMBuildLoad (builder, ctx->addresses [reg], "");
2019                         }
2020                         break;
2021                 }
2022                 case LLVMArgAsIArgs: {
2023                         LLVMValueRef arg = LLVMGetParam (ctx->lmethod, ctx->pindexes [i]);
2024
2025                         ctx->addresses [reg] = build_alloca (ctx, sig->params [i]);
2026
2027                         /* The argument is received as an array of ints, store it into the real argument */
2028                         LLVMBuildStore (ctx->builder, arg, convert (ctx, ctx->addresses [reg], LLVMPointerType (LLVMTypeOf (arg), 0)));
2029                         break;
2030                 }
2031                 default:
2032                         ctx->values [reg] = convert_full (ctx, ctx->values [reg], llvm_type_to_stack_type (cfg, type_to_llvm_type (ctx, sig->params [i])), type_is_unsigned (ctx, sig->params [i]));
2033                         break;
2034                 }
2035         }
2036
2037         if (cfg->vret_addr)
2038                 emit_volatile_store (ctx, cfg->vret_addr->dreg);
2039         if (sig->hasthis)
2040                 emit_volatile_store (ctx, cfg->args [0]->dreg);
2041         for (i = 0; i < sig->param_count; ++i)
2042                 if (!mini_type_is_vtype (cfg, sig->params [i]))
2043                         emit_volatile_store (ctx, cfg->args [i + sig->hasthis]->dreg);
2044
2045         if (sig->hasthis && !cfg->rgctx_var && cfg->generic_sharing_context) {
2046                 LLVMValueRef this_alloc;
2047
2048                 /*
2049                  * The exception handling code needs the location where the this argument was
2050                  * stored for gshared methods. We create a separate alloca to hold it, and mark it
2051                  * with the "mono.this" custom metadata to tell llvm that it needs to save its
2052                  * location into the LSDA.
2053                  */
2054                 this_alloc = mono_llvm_build_alloca (builder, ThisType (), LLVMConstInt (LLVMInt32Type (), 1, FALSE), 0, "");
2055                 /* This volatile store will keep the alloca alive */
2056                 mono_llvm_build_store (builder, ctx->values [cfg->args [0]->dreg], this_alloc, TRUE, LLVM_BARRIER_NONE);
2057
2058                 set_metadata_flag (this_alloc, "mono.this");
2059         }
2060
2061         if (cfg->rgctx_var) {
2062                 LLVMValueRef rgctx_alloc, store;
2063
2064                 /*
2065                  * We handle the rgctx arg similarly to the this pointer.
2066                  */
2067                 g_assert (ctx->addresses [cfg->rgctx_var->dreg]);
2068                 rgctx_alloc = ctx->addresses [cfg->rgctx_var->dreg];
2069                 /* This volatile store will keep the alloca alive */
2070                 store = mono_llvm_build_store (builder, convert (ctx, ctx->rgctx_arg, IntPtrType ()), rgctx_alloc, TRUE, LLVM_BARRIER_NONE);
2071
2072                 set_metadata_flag (rgctx_alloc, "mono.this");
2073         }
2074
2075         /*
2076          * For finally clauses, create an indicator variable telling OP_ENDFINALLY whenever
2077          * it needs to continue normally, or return back to the exception handling system.
2078          */
2079         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2080                 if (bb->region != -1 && (bb->flags & BB_EXCEPTION_HANDLER))
2081                         g_hash_table_insert (ctx->region_to_handler, GUINT_TO_POINTER (mono_get_block_region_notry (cfg, bb->region)), bb);
2082                 if (bb->region != -1 && (bb->flags & BB_EXCEPTION_HANDLER) && bb->in_scount == 0) {
2083                         char name [128];
2084                         LLVMValueRef val;
2085
2086                         sprintf (name, "finally_ind_bb%d", bb->block_num);
2087                         val = LLVMBuildAlloca (builder, LLVMInt32Type (), name);
2088                         LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), val);
2089
2090                         ctx->bblocks [bb->block_num].finally_ind = val;
2091
2092                         /*
2093                          * Create a new bblock which CALL_HANDLER can branch to, because branching to the
2094                          * LLVM bblock containing the call to llvm.eh.selector causes problems for the
2095                          * LLVM optimizer passes.
2096                          */
2097                         sprintf (name, "BB_%d_CALL_HANDLER_TARGET", bb->block_num);
2098                         ctx->bblocks [bb->block_num].call_handler_target_bb = LLVMAppendBasicBlock (ctx->lmethod, name);
2099                 }
2100         }
2101
2102  FAILURE:
2103         ;
2104 }
2105
2106 /* Have to export this for AOT */
2107 void
2108 mono_personality (void)
2109 {
2110         /* Not used */
2111         g_assert_not_reached ();
2112 }
2113
2114 static void
2115 process_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, MonoInst *ins)
2116 {
2117         MonoCompile *cfg = ctx->cfg;
2118         LLVMModuleRef module = ctx->module;
2119         LLVMValueRef *values = ctx->values;
2120         LLVMValueRef *addresses = ctx->addresses;
2121         MonoCallInst *call = (MonoCallInst*)ins;
2122         MonoMethodSignature *sig = call->signature;
2123         LLVMValueRef callee = NULL, lcall;
2124         LLVMValueRef *args;
2125         LLVMCallInfo *cinfo;
2126         GSList *l;
2127         int i, len, nargs;
2128         gboolean vretaddr;
2129         LLVMTypeRef llvm_sig;
2130         gpointer target;
2131         gboolean virtual, calli;
2132         LLVMBuilderRef builder = *builder_ref;
2133         LLVMSigInfo sinfo;
2134
2135         if (call->signature->call_convention != MONO_CALL_DEFAULT)
2136                 LLVM_FAILURE (ctx, "non-default callconv");
2137
2138         cinfo = call->cinfo;
2139         if (call->rgctx_arg_reg)
2140                 cinfo->rgctx_arg = TRUE;
2141         if (call->imt_arg_reg)
2142                 cinfo->imt_arg = TRUE;
2143
2144         vretaddr = cinfo && cinfo->ret.storage == LLVMArgVtypeRetAddr;
2145
2146         llvm_sig = sig_to_llvm_sig_full (ctx, sig, cinfo, &sinfo);
2147         CHECK_FAILURE (ctx);
2148
2149         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);
2150         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);
2151
2152         /* FIXME: Avoid creating duplicate methods */
2153
2154         if (ins->flags & MONO_INST_HAS_METHOD) {
2155                 if (virtual) {
2156                         callee = NULL;
2157                 } else {
2158                         if (cfg->compile_aot) {
2159                                 callee = get_plt_entry (ctx, llvm_sig, MONO_PATCH_INFO_METHOD, call->method);
2160                                 if (!callee)
2161                                         LLVM_FAILURE (ctx, "can't encode patch");
2162                         } else {
2163                                 callee = LLVMAddFunction (module, "", llvm_sig);
2164  
2165                                 target =
2166                                         mono_create_jit_trampoline_in_domain (mono_domain_get (),
2167                                                                                                                   call->method);
2168                                 LLVMAddGlobalMapping (ctx->lmodule->ee, callee, target);
2169                         }
2170                 }
2171
2172                 if (call->method && strstr (call->method->klass->name, "AsyncVoidMethodBuilder"))
2173                         /* LLVM miscompiles async methods */
2174                         LLVM_FAILURE (ctx, "#13734");
2175         } else if (calli) {
2176         } else {
2177                 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
2178
2179                 if (info) {
2180                         /*
2181                           MonoJumpInfo ji;
2182
2183                           memset (&ji, 0, sizeof (ji));
2184                           ji.type = MONO_PATCH_INFO_JIT_ICALL_ADDR;
2185                           ji.data.target = info->name;
2186
2187                           target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &ji, FALSE);
2188                         */
2189                         if (cfg->compile_aot) {
2190                                 callee = get_plt_entry (ctx, llvm_sig, MONO_PATCH_INFO_INTERNAL_METHOD, (char*)info->name);
2191                                 if (!callee)
2192                                         LLVM_FAILURE (ctx, "can't encode patch");
2193                         } else {
2194                                 callee = LLVMAddFunction (module, "", llvm_sig);
2195                                 target = (gpointer)mono_icall_get_wrapper (info);
2196                                 LLVMAddGlobalMapping (ctx->lmodule->ee, callee, target);
2197                         }
2198                 } else {
2199                         if (cfg->compile_aot) {
2200                                 callee = NULL;
2201                                 if (cfg->abs_patches) {
2202                                         MonoJumpInfo *abs_ji = g_hash_table_lookup (cfg->abs_patches, call->fptr);
2203                                         if (abs_ji) {
2204                                                 callee = get_plt_entry (ctx, llvm_sig, abs_ji->type, abs_ji->data.target);
2205                                                 if (!callee)
2206                                                         LLVM_FAILURE (ctx, "can't encode patch");
2207                                         }
2208                                 }
2209                                 if (!callee)
2210                                         LLVM_FAILURE (ctx, "aot");
2211                         } else {
2212                                 callee = LLVMAddFunction (module, "", llvm_sig);
2213                                 target = NULL;
2214                                 if (cfg->abs_patches) {
2215                                         MonoJumpInfo *abs_ji = g_hash_table_lookup (cfg->abs_patches, call->fptr);
2216                                         if (abs_ji) {
2217                                                 /*
2218                                                  * FIXME: Some trampolines might have
2219                                                  * their own calling convention on some platforms.
2220                                                  */
2221 #ifndef TARGET_AMD64
2222                                                 if (abs_ji->type == MONO_PATCH_INFO_MONITOR_ENTER || abs_ji->type == MONO_PATCH_INFO_MONITOR_ENTER_V4 ||
2223                                                                 abs_ji->type == MONO_PATCH_INFO_MONITOR_EXIT || abs_ji->type == MONO_PATCH_INFO_GENERIC_CLASS_INIT)
2224                                                         LLVM_FAILURE (ctx, "trampoline with own cconv");
2225 #endif
2226                                                 target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, abs_ji, FALSE);
2227                                                 LLVMAddGlobalMapping (ctx->lmodule->ee, callee, target);
2228                                         }
2229                                 }
2230                                 if (!target)
2231                                         LLVMAddGlobalMapping (ctx->lmodule->ee, callee, (gpointer)call->fptr);
2232                         }
2233                 }
2234         }
2235
2236         if (virtual) {
2237                 int size = sizeof (gpointer);
2238                 LLVMValueRef index;
2239
2240                 g_assert (ins->inst_offset % size == 0);
2241                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
2242
2243                 callee = convert (ctx, LLVMBuildLoad (builder, LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (LLVMPointerType (IntPtrType (), 0), 0)), &index, 1, ""), ""), LLVMPointerType (llvm_sig, 0));
2244         } else if (calli) {
2245                 callee = convert (ctx, values [ins->sreg1], LLVMPointerType (llvm_sig, 0));
2246         } else {
2247                 if (ins->flags & MONO_INST_HAS_METHOD) {
2248                 }
2249         }
2250
2251         /* 
2252          * Collect and convert arguments
2253          */
2254         nargs = (sig->param_count * 16) + sig->hasthis + vretaddr + call->rgctx_reg + call->imt_arg_reg;
2255         len = sizeof (LLVMValueRef) * nargs;
2256         args = alloca (len);
2257         memset (args, 0, len);
2258         l = call->out_ireg_args;
2259
2260         if (call->rgctx_arg_reg) {
2261                 g_assert (values [call->rgctx_arg_reg]);
2262                 g_assert (sinfo.rgctx_arg_pindex < nargs);
2263                 /*
2264                  * On ARM, the imt/rgctx argument is passed in a caller save register, but some of our trampolines etc. clobber it, leading to
2265                  * problems is LLVM moves the arg assignment earlier. To work around this, save the argument into a stack slot and load
2266                  * it using a volatile load.
2267                  */
2268 #ifdef TARGET_ARM
2269                 if (!ctx->imt_rgctx_loc)
2270                         ctx->imt_rgctx_loc = build_alloca_llvm_type (ctx, ctx->lmodule->ptr_type, sizeof (gpointer));
2271                 LLVMBuildStore (builder, convert (ctx, ctx->values [call->rgctx_arg_reg], ctx->lmodule->ptr_type), ctx->imt_rgctx_loc);
2272                 args [sinfo.rgctx_arg_pindex] = mono_llvm_build_load (builder, ctx->imt_rgctx_loc, "", TRUE, LLVM_BARRIER_NONE);
2273 #else
2274                 args [sinfo.rgctx_arg_pindex] = convert (ctx, values [call->rgctx_arg_reg], ctx->lmodule->ptr_type);
2275 #endif
2276         }
2277         if (call->imt_arg_reg) {
2278                 g_assert (values [call->imt_arg_reg]);
2279                 g_assert (sinfo.imt_arg_pindex < nargs);
2280 #ifdef TARGET_ARM
2281                 if (!ctx->imt_rgctx_loc)
2282                         ctx->imt_rgctx_loc = build_alloca_llvm_type (ctx, ctx->lmodule->ptr_type, sizeof (gpointer));
2283                 LLVMBuildStore (builder, convert (ctx, ctx->values [call->imt_arg_reg], ctx->lmodule->ptr_type), ctx->imt_rgctx_loc);
2284                 args [sinfo.imt_arg_pindex] = mono_llvm_build_load (builder, ctx->imt_rgctx_loc, "", TRUE, LLVM_BARRIER_NONE);
2285 #else
2286                 args [sinfo.imt_arg_pindex] = convert (ctx, values [call->imt_arg_reg], ctx->lmodule->ptr_type);
2287 #endif
2288         }
2289
2290         if (vretaddr) {
2291                 if (!addresses [call->inst.dreg])
2292                         addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
2293                 g_assert (sinfo.vret_arg_pindex < nargs);
2294                 args [sinfo.vret_arg_pindex] = LLVMBuildPtrToInt (builder, addresses [call->inst.dreg], IntPtrType (), "");
2295         }
2296
2297         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2298                 guint32 regpair;
2299                 int reg, pindex;
2300                 LLVMArgInfo *ainfo = call->cinfo ? &call->cinfo->args [i] : NULL;
2301
2302                 if (sig->hasthis) {
2303                         if (i == 0)
2304                                 pindex = sinfo.this_arg_pindex;
2305                         else
2306                                 pindex = sinfo.pindexes [i - 1];
2307                 } else {
2308                         pindex = sinfo.pindexes [i];
2309                 }
2310
2311                 regpair = (guint32)(gssize)(l->data);
2312                 reg = regpair & 0xffffff;
2313                 args [pindex] = values [reg];
2314                 switch (ainfo->storage) {
2315                 case LLVMArgVtypeInReg:
2316                 case LLVMArgAsFpArgs: {
2317                         guint32 nargs;
2318
2319                         g_assert (addresses [reg]);
2320                         emit_vtype_to_args (ctx, builder, sig->params [i - sig->hasthis], addresses [reg], ainfo, args + pindex, &nargs);
2321                         pindex += nargs;
2322
2323                         // FIXME: alignment
2324                         // FIXME: Get rid of the VMOVE
2325                         break;
2326                 }
2327                 case LLVMArgVtypeByVal:
2328                         g_assert (addresses [reg]);
2329                         args [pindex] = addresses [reg];
2330                         break;
2331                 case LLVMArgAsIArgs:
2332                         g_assert (addresses [reg]);
2333                         args [pindex] = LLVMBuildLoad (ctx->builder, convert (ctx, addresses [reg], LLVMPointerType (LLVMArrayType (IntPtrType (), ainfo->nslots), 0)), "");
2334                         break;
2335                 default:
2336                         g_assert (args [pindex]);
2337                         if (i == 0 && sig->hasthis)
2338                                 args [pindex] = convert (ctx, args [pindex], ThisType ());
2339                         else
2340                                 args [pindex] = convert (ctx, args [pindex], type_to_llvm_arg_type (ctx, sig->params [i - sig->hasthis]));
2341                         break;
2342                 }
2343                 g_assert (pindex <= nargs);
2344
2345                 l = l->next;
2346         }
2347
2348         // FIXME: Align call sites
2349
2350         /*
2351          * Emit the call
2352          */
2353
2354         lcall = emit_call (ctx, bb, &builder, callee, args, LLVMCountParamTypes (llvm_sig));
2355
2356         /*
2357          * Modify cconv and parameter attributes to pass rgctx/imt correctly.
2358          */
2359 #if defined(MONO_ARCH_IMT_REG) && defined(MONO_ARCH_RGCTX_REG)
2360         g_assert (MONO_ARCH_IMT_REG == MONO_ARCH_RGCTX_REG);
2361 #endif
2362         /* The two can't be used together, so use only one LLVM calling conv to pass them */
2363         g_assert (!(call->rgctx_arg_reg && call->imt_arg_reg));
2364         if (!sig->pinvoke)
2365                 LLVMSetInstructionCallConv (lcall, LLVMMono1CallConv);
2366
2367         if (call->rgctx_arg_reg)
2368                 LLVMAddInstrAttribute (lcall, 1 + sinfo.rgctx_arg_pindex, LLVMInRegAttribute);
2369         if (call->imt_arg_reg)
2370                 LLVMAddInstrAttribute (lcall, 1 + sinfo.imt_arg_pindex, LLVMInRegAttribute);
2371
2372         /* Add byval attributes if needed */
2373         for (i = 0; i < sig->param_count; ++i) {
2374                 LLVMArgInfo *ainfo = call->cinfo ? &call->cinfo->args [i + sig->hasthis] : NULL;
2375
2376                 if (ainfo && ainfo->storage == LLVMArgVtypeByVal) {
2377                         LLVMAddInstrAttribute (lcall, 1 + sinfo.pindexes [i], LLVMByValAttribute);
2378                 }
2379         }
2380
2381         /*
2382          * Convert the result
2383          */
2384         if (cinfo) {
2385                 switch (cinfo->ret.storage) {
2386                 case LLVMArgVtypeInReg: {
2387                         LLVMValueRef regs [2];
2388
2389                         if (!addresses [ins->dreg])
2390                                 addresses [ins->dreg] = build_alloca (ctx, sig->ret);
2391
2392                         regs [0] = LLVMBuildExtractValue (builder, lcall, 0, "");
2393                         if (cinfo->ret.pair_storage [1] != LLVMArgNone)
2394                                 regs [1] = LLVMBuildExtractValue (builder, lcall, 1, "");
2395                         emit_args_to_vtype (ctx, builder, sig->ret, addresses [ins->dreg], &cinfo->ret, regs);
2396                         break;
2397                 }
2398                 case LLVMArgVtypeByVal:
2399                         if (!addresses [call->inst.dreg])
2400                                 addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
2401                         LLVMBuildStore (builder, lcall, addresses [call->inst.dreg]);
2402                         break;
2403                 case LLVMArgFpStruct:
2404                         if (!addresses [call->inst.dreg])
2405                                 addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
2406                         LLVMBuildStore (builder, lcall, convert_full (ctx, addresses [call->inst.dreg], LLVMPointerType (LLVMTypeOf (lcall), 0), FALSE));
2407                         break;
2408                 default:
2409                         if (sig->ret->type != MONO_TYPE_VOID && !vretaddr)
2410                                 /* If the method returns an unsigned value, need to zext it */
2411                                 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));
2412                         break;
2413                 }
2414         } else {
2415                 if (sig->ret->type != MONO_TYPE_VOID && !vretaddr)
2416                         /* If the method returns an unsigned value, need to zext it */
2417                         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));
2418         }
2419
2420         if (vretaddr) {
2421                 if (!addresses [call->inst.dreg])
2422                         addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
2423                 g_assert (sinfo.vret_arg_pindex < nargs);
2424                 args [sinfo.vret_arg_pindex] = LLVMBuildPtrToInt (builder, addresses [call->inst.dreg], IntPtrType (), "");
2425         }
2426
2427         *builder_ref = ctx->builder;
2428
2429         g_free (sinfo.pindexes);
2430         
2431         return;
2432  FAILURE:
2433         return;
2434 }
2435
2436 static void
2437 process_bb (EmitContext *ctx, MonoBasicBlock *bb)
2438 {
2439         MonoCompile *cfg = ctx->cfg;
2440         MonoMethodSignature *sig = ctx->sig;
2441         LLVMValueRef method = ctx->lmethod;
2442         LLVMValueRef *values = ctx->values;
2443         LLVMValueRef *addresses = ctx->addresses;
2444         LLVMCallInfo *linfo = ctx->linfo;
2445         LLVMModuleRef module = ctx->module;
2446         BBInfo *bblocks = ctx->bblocks;
2447         MonoInst *ins;
2448         LLVMBasicBlockRef cbb;
2449         LLVMBuilderRef builder, starting_builder;
2450         gboolean has_terminator;
2451         LLVMValueRef v;
2452         LLVMValueRef lhs, rhs;
2453         int nins = 0;
2454
2455         cbb = get_bb (ctx, bb);
2456         builder = create_builder (ctx);
2457         ctx->builder = builder;
2458         LLVMPositionBuilderAtEnd (builder, cbb);
2459
2460         if (bb == cfg->bb_entry)
2461                 emit_entry_bb (ctx, builder);
2462         CHECK_FAILURE (ctx);
2463
2464         if (bb->flags & BB_EXCEPTION_HANDLER) {
2465                 LLVMTypeRef i8ptr;
2466                 LLVMValueRef personality;
2467                 LLVMBasicBlockRef target_bb;
2468                 MonoInst *exvar;
2469                 static gint32 mapping_inited;
2470                 static int ti_generator;
2471                 char ti_name [128];
2472                 MonoClass **ti;
2473                 LLVMValueRef type_info;
2474                 int clause_index;
2475
2476                 if (!bblocks [bb->block_num].invoke_target) {
2477                         /*
2478                          * LLVM asserts if llvm.eh.selector is called from a bblock which
2479                          * doesn't have an invoke pointing at it.
2480                          * Update: LLVM no longer asserts, but some tests in exceptions.exe now fail.
2481                          */
2482                         LLVM_FAILURE (ctx, "handler without invokes");
2483                 }
2484
2485                 // <resultval> = landingpad <somety> personality <type> <pers_fn> <clause>+
2486
2487                 if (cfg->compile_aot) {
2488                         /* Use a dummy personality function */
2489                         personality = LLVMGetNamedFunction (module, "mono_aot_personality");
2490                         g_assert (personality);
2491                 } else {
2492                         personality = LLVMGetNamedFunction (module, "mono_personality");
2493                         if (InterlockedCompareExchange (&mapping_inited, 1, 0) == 0)
2494                                 LLVMAddGlobalMapping (ctx->lmodule->ee, personality, mono_personality);
2495                 }
2496
2497                 i8ptr = LLVMPointerType (LLVMInt8Type (), 0);
2498
2499                 clause_index = (mono_get_block_region_notry (cfg, bb->region) >> 8) - 1;
2500
2501                 /*
2502                  * Create the type info
2503                  */
2504                 sprintf (ti_name, "type_info_%d", ti_generator);
2505                 ti_generator ++;
2506
2507                 if (cfg->compile_aot) {
2508                         /* decode_eh_frame () in aot-runtime.c will decode this */
2509                         type_info = LLVMAddGlobal (module, LLVMInt32Type (), ti_name);
2510                         LLVMSetInitializer (type_info, LLVMConstInt (LLVMInt32Type (), clause_index, FALSE));
2511
2512                         /*
2513                          * These symbols are not really used, the clause_index is embedded into the EH tables generated by DwarfMonoException in LLVM.
2514                          */
2515                         LLVMSetLinkage (type_info, LLVMInternalLinkage);
2516
2517                         /* 
2518                          * Enabling this causes llc to crash:
2519                          * http://llvm.org/bugs/show_bug.cgi?id=6102
2520                          */
2521                         //LLVM_FAILURE (ctx, "aot+clauses");
2522 #ifdef TARGET_ARM
2523                         // test_0_invalid_unbox_arrays () fails
2524                         LLVM_FAILURE (ctx, "aot+clauses");
2525 #endif
2526                 } else {
2527                         /*
2528                          * After the cfg mempool is freed, the type info will point to stale memory,
2529                          * but this is not a problem, since we decode it once in exception_cb during
2530                          * compilation.
2531                          */
2532                         ti = mono_mempool_alloc (cfg->mempool, sizeof (gint32));
2533                         *(gint32*)ti = clause_index;
2534
2535                         type_info = LLVMAddGlobal (module, i8ptr, ti_name);
2536
2537                         LLVMAddGlobalMapping (ctx->lmodule->ee, type_info, ti);
2538                 }
2539
2540                 {
2541                         LLVMTypeRef members [2], ret_type;
2542                         LLVMValueRef landing_pad;
2543
2544                         members [0] = i8ptr;
2545                         members [1] = LLVMInt32Type ();
2546                         ret_type = LLVMStructType (members, 2, FALSE);
2547
2548                         landing_pad = LLVMBuildLandingPad (builder, ret_type, personality, 1, "");
2549                         LLVMAddClause (landing_pad, type_info);
2550
2551                         /* Store the exception into the exvar */
2552                         if (bb->in_scount == 1) {
2553                                 g_assert (bb->in_scount == 1);
2554                                 exvar = bb->in_stack [0];
2555
2556                                 // FIXME: This is shared with filter clauses ?
2557                                 g_assert (!values [exvar->dreg]);
2558
2559                                 values [exvar->dreg] = LLVMBuildExtractValue (builder, landing_pad, 0, "ex_obj");
2560                                 emit_volatile_store (ctx, exvar->dreg);
2561                         }
2562                 }
2563
2564                 /* Start a new bblock which CALL_HANDLER can branch to */
2565                 target_bb = bblocks [bb->block_num].call_handler_target_bb;
2566                 if (target_bb) {
2567                         LLVMBuildBr (builder, target_bb);
2568
2569                         ctx->builder = builder = create_builder (ctx);
2570                         LLVMPositionBuilderAtEnd (ctx->builder, target_bb);
2571
2572                         ctx->bblocks [bb->block_num].end_bblock = target_bb;
2573                 }
2574         }
2575
2576         has_terminator = FALSE;
2577         starting_builder = builder;
2578         for (ins = bb->code; ins; ins = ins->next) {
2579                 const char *spec = LLVM_INS_INFO (ins->opcode);
2580                 char *dname = NULL;
2581                 char dname_buf [128];
2582
2583                 emit_dbg_loc (ctx, builder, ins->cil_code);
2584
2585                 nins ++;
2586                 if (nins > 5000 && builder == starting_builder) {
2587                         /* some steps in llc are non-linear in the size of basic blocks, see #5714 */
2588                         LLVM_FAILURE (ctx, "basic block too long");
2589                 }
2590
2591                 if (has_terminator)
2592                         /* There could be instructions after a terminator, skip them */
2593                         break;
2594
2595                 if (spec [MONO_INST_DEST] != ' ' && !MONO_IS_STORE_MEMBASE (ins)) {
2596                         sprintf (dname_buf, "t%d", ins->dreg);
2597                         dname = dname_buf;
2598                 }
2599
2600                 if (spec [MONO_INST_SRC1] != ' ' && spec [MONO_INST_SRC1] != 'v') {
2601                         MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
2602
2603                         if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
2604                                 lhs = emit_volatile_load (ctx, ins->sreg1);
2605                         } else {
2606                                 /* It is ok for SETRET to have an uninitialized argument */
2607                                 if (!values [ins->sreg1] && ins->opcode != OP_SETRET)
2608                                         LLVM_FAILURE (ctx, "sreg1");
2609                                 lhs = values [ins->sreg1];
2610                         }
2611                 } else {
2612                         lhs = NULL;
2613                 }
2614
2615                 if (spec [MONO_INST_SRC2] != ' ' && spec [MONO_INST_SRC2] != ' ') {
2616                         MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
2617                         if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
2618                                 rhs = emit_volatile_load (ctx, ins->sreg2);
2619                         } else {
2620                                 if (!values [ins->sreg2])
2621                                         LLVM_FAILURE (ctx, "sreg2");
2622                                 rhs = values [ins->sreg2];
2623                         }
2624                 } else {
2625                         rhs = NULL;
2626                 }
2627
2628                 //mono_print_ins (ins);
2629                 switch (ins->opcode) {
2630                 case OP_NOP:
2631                 case OP_NOT_NULL:
2632                 case OP_LIVERANGE_START:
2633                 case OP_LIVERANGE_END:
2634                         break;
2635                 case OP_ICONST:
2636                         values [ins->dreg] = LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE);
2637                         break;
2638                 case OP_I8CONST:
2639 #if SIZEOF_VOID_P == 4
2640                         values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
2641 #else
2642                         values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), (gint64)ins->inst_c0, FALSE);
2643 #endif
2644                         break;
2645                 case OP_R8CONST:
2646                         values [ins->dreg] = LLVMConstReal (LLVMDoubleType (), *(double*)ins->inst_p0);
2647                         break;
2648                 case OP_R4CONST:
2649                         if (cfg->r4fp)
2650                                 values [ins->dreg] = LLVMConstReal (LLVMFloatType (), *(float*)ins->inst_p0);
2651                         else
2652                                 values [ins->dreg] = LLVMConstFPExt (LLVMConstReal (LLVMFloatType (), *(float*)ins->inst_p0), LLVMDoubleType ());
2653                         break;
2654                 case OP_DUMMY_ICONST:
2655                         values [ins->dreg] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2656                         break;
2657                 case OP_DUMMY_I8CONST:
2658                         values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
2659                         break;
2660                 case OP_DUMMY_R8CONST:
2661                         values [ins->dreg] = LLVMConstReal (LLVMDoubleType (), 0.0f);
2662                         break;
2663                 case OP_BR:
2664                         LLVMBuildBr (builder, get_bb (ctx, ins->inst_target_bb));
2665                         has_terminator = TRUE;
2666                         break;
2667                 case OP_SWITCH: {
2668                         int i;
2669                         LLVMValueRef v;
2670                         char bb_name [128];
2671                         LLVMBasicBlockRef new_bb;
2672                         LLVMBuilderRef new_builder;
2673
2674                         // The default branch is already handled
2675                         // FIXME: Handle it here
2676
2677                         /* Start new bblock */
2678                         sprintf (bb_name, "SWITCH_DEFAULT_BB%d", ctx->default_index ++);
2679                         new_bb = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
2680
2681                         lhs = convert (ctx, lhs, LLVMInt32Type ());
2682                         v = LLVMBuildSwitch (builder, lhs, new_bb, GPOINTER_TO_UINT (ins->klass));
2683                         for (i = 0; i < GPOINTER_TO_UINT (ins->klass); ++i) {
2684                                 MonoBasicBlock *target_bb = ins->inst_many_bb [i];
2685
2686                                 LLVMAddCase (v, LLVMConstInt (LLVMInt32Type (), i, FALSE), get_bb (ctx, target_bb));
2687                         }
2688
2689                         new_builder = create_builder (ctx);
2690                         LLVMPositionBuilderAtEnd (new_builder, new_bb);
2691                         LLVMBuildUnreachable (new_builder);
2692
2693                         has_terminator = TRUE;
2694                         g_assert (!ins->next);
2695                                 
2696                         break;
2697                 }
2698
2699                 case OP_SETRET:
2700                         if (linfo->ret.storage == LLVMArgVtypeInReg) {
2701                                 LLVMTypeRef ret_type = LLVMGetReturnType (LLVMGetElementType (LLVMTypeOf (method)));
2702                                 LLVMValueRef part1, retval;
2703                                 int size;
2704
2705                                 size = get_vtype_size (sig->ret);
2706
2707                                 g_assert (addresses [ins->sreg1]);
2708
2709                                 g_assert (linfo->ret.pair_storage [0] == LLVMArgInIReg);
2710                                 g_assert (linfo->ret.pair_storage [1] == LLVMArgNone);
2711                                         
2712                                 part1 = convert (ctx, LLVMBuildLoad (builder, LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMIntType (size * 8), 0), ""), ""), IntPtrType ());
2713
2714                                 retval = LLVMBuildInsertValue (builder, LLVMGetUndef (ret_type), part1, 0, "");
2715
2716                                 LLVMBuildRet (builder, retval);
2717                                 break;
2718                         }
2719
2720                         if (linfo->ret.storage == LLVMArgVtypeByVal) {
2721                                 LLVMValueRef retval;
2722
2723                                 g_assert (addresses [ins->sreg1]);
2724                                 retval = LLVMBuildLoad (builder, addresses [ins->sreg1], "");
2725                                 LLVMBuildRet (builder, retval);
2726                                 break;
2727                         }
2728
2729                         if (linfo->ret.storage == LLVMArgVtypeRetAddr) {
2730                                 LLVMBuildRetVoid (builder);
2731                                 break;
2732                         }
2733
2734                         if (linfo->ret.storage == LLVMArgFpStruct) {
2735                                 LLVMTypeRef ret_type = LLVMGetReturnType (LLVMGetElementType (LLVMTypeOf (method)));
2736                                 LLVMValueRef retval;
2737
2738                                 g_assert (addresses [ins->sreg1]);
2739                                 retval = LLVMBuildLoad (builder, convert (ctx, addresses [ins->sreg1], LLVMPointerType (ret_type, 0)), "");
2740                                 LLVMBuildRet (builder, retval);
2741                                 break;
2742                         }
2743
2744                         if (!lhs || ctx->is_dead [ins->sreg1]) {
2745                                 /* 
2746                                  * The method did not set its return value, probably because it
2747                                  * ends with a throw.
2748                                  */
2749                                 if (cfg->vret_addr)
2750                                         LLVMBuildRetVoid (builder);
2751                                 else
2752                                         LLVMBuildRet (builder, LLVMConstNull (type_to_llvm_type (ctx, sig->ret)));
2753                         } else {
2754                                 LLVMBuildRet (builder, convert (ctx, lhs, type_to_llvm_type (ctx, sig->ret)));
2755                         }
2756                         has_terminator = TRUE;
2757                         break;
2758                 case OP_ICOMPARE:
2759                 case OP_FCOMPARE:
2760                 case OP_RCOMPARE:
2761                 case OP_LCOMPARE:
2762                 case OP_COMPARE:
2763                 case OP_ICOMPARE_IMM:
2764                 case OP_LCOMPARE_IMM:
2765                 case OP_COMPARE_IMM: {
2766                         CompRelation rel;
2767                         LLVMValueRef cmp;
2768
2769                         if (ins->next->opcode == OP_NOP)
2770                                 break;
2771
2772                         if (ins->next->opcode == OP_BR)
2773                                 /* The comparison result is not needed */
2774                                 continue;
2775
2776                         rel = mono_opcode_to_cond (ins->next->opcode);
2777
2778                         if (ins->opcode == OP_ICOMPARE_IMM) {
2779                                 lhs = convert (ctx, lhs, LLVMInt32Type ());
2780                                 rhs = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
2781                         }
2782                         if (ins->opcode == OP_LCOMPARE_IMM) {
2783                                 lhs = convert (ctx, lhs, LLVMInt64Type ());
2784                                 rhs = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
2785                         }
2786                         if (ins->opcode == OP_LCOMPARE) {
2787                                 lhs = convert (ctx, lhs, LLVMInt64Type ());
2788                                 rhs = convert (ctx, rhs, LLVMInt64Type ());
2789                         }
2790                         if (ins->opcode == OP_ICOMPARE) {
2791                                 lhs = convert (ctx, lhs, LLVMInt32Type ());
2792                                 rhs = convert (ctx, rhs, LLVMInt32Type ());
2793                         }
2794
2795                         if (lhs && rhs) {
2796                                 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
2797                                         rhs = convert (ctx, rhs, LLVMTypeOf (lhs));
2798                                 else if (LLVMGetTypeKind (LLVMTypeOf (rhs)) == LLVMPointerTypeKind)
2799                                         lhs = convert (ctx, lhs, LLVMTypeOf (rhs));
2800                         }
2801
2802                         /* We use COMPARE+SETcc/Bcc, llvm uses SETcc+br cond */
2803                         if (ins->opcode == OP_FCOMPARE) {
2804                                 cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMDoubleType ()), convert (ctx, rhs, LLVMDoubleType ()), "");
2805                         } else if (ins->opcode == OP_RCOMPARE) {
2806                                 cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMFloatType ()), convert (ctx, rhs, LLVMFloatType ()), "");
2807                         } else if (ins->opcode == OP_COMPARE_IMM) {
2808                                 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind && ins->inst_imm == 0)
2809                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], lhs, LLVMConstNull (LLVMTypeOf (lhs)), "");
2810                                 else
2811                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), "");
2812                         } else if (ins->opcode == OP_LCOMPARE_IMM) {
2813                                 if (SIZEOF_REGISTER == 4 && COMPILE_LLVM (cfg))  {
2814                                         /* The immediate is encoded in two fields */
2815                                         guint64 l = ((guint64)(guint32)ins->inst_offset << 32) | ((guint32)ins->inst_imm);
2816                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, LLVMInt64Type ()), LLVMConstInt (LLVMInt64Type (), l, FALSE), "");
2817                                 } else {
2818                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, LLVMInt64Type ()), LLVMConstInt (LLVMInt64Type (), ins->inst_imm, FALSE), "");
2819                                 }
2820                         }
2821                         else if (ins->opcode == OP_COMPARE) {
2822                                 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind && LLVMTypeOf (lhs) == LLVMTypeOf (rhs))
2823                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], lhs, rhs, "");
2824                                 else
2825                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, IntPtrType ()), convert (ctx, rhs, IntPtrType ()), "");
2826                         } else
2827                                 cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], lhs, rhs, "");
2828
2829                         if (MONO_IS_COND_BRANCH_OP (ins->next)) {
2830                                 if (ins->next->inst_true_bb == ins->next->inst_false_bb) {
2831                                         /*
2832                                          * If the target bb contains PHI instructions, LLVM requires
2833                                          * two PHI entries for this bblock, while we only generate one.
2834                                          * So convert this to an unconditional bblock. (bxc #171).
2835                                          */
2836                                         LLVMBuildBr (builder, get_bb (ctx, ins->next->inst_true_bb));
2837                                 } else {
2838                                         LLVMBuildCondBr (builder, cmp, get_bb (ctx, ins->next->inst_true_bb), get_bb (ctx, ins->next->inst_false_bb));
2839                                 }
2840                                 has_terminator = TRUE;
2841                         } else if (MONO_IS_SETCC (ins->next)) {
2842                                 sprintf (dname_buf, "t%d", ins->next->dreg);
2843                                 dname = dname_buf;
2844                                 values [ins->next->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
2845
2846                                 /* Add stores for volatile variables */
2847                                 emit_volatile_store (ctx, ins->next->dreg);
2848                         } else if (MONO_IS_COND_EXC (ins->next)) {
2849                                 emit_cond_system_exception (ctx, bb, ins->next->inst_p1, cmp);
2850                                 CHECK_FAILURE (ctx);
2851                                 builder = ctx->builder;
2852                         } else {
2853                                 LLVM_FAILURE (ctx, "next");
2854                         }
2855
2856                         ins = ins->next;
2857                         break;
2858                 }
2859                 case OP_FCEQ:
2860                 case OP_FCLT:
2861                 case OP_FCLT_UN:
2862                 case OP_FCGT:
2863                 case OP_FCGT_UN: {
2864                         CompRelation rel;
2865                         LLVMValueRef cmp;
2866
2867                         rel = mono_opcode_to_cond (ins->opcode);
2868
2869                         cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMDoubleType ()), convert (ctx, rhs, LLVMDoubleType ()), "");
2870                         values [ins->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
2871                         break;
2872                 }
2873                 case OP_RCEQ:
2874                 case OP_RCLT:
2875                 case OP_RCLT_UN:
2876                 case OP_RCGT:
2877                 case OP_RCGT_UN: {
2878                         CompRelation rel;
2879                         LLVMValueRef cmp;
2880
2881                         rel = mono_opcode_to_cond (ins->opcode);
2882
2883                         cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMFloatType ()), convert (ctx, rhs, LLVMFloatType ()), "");
2884                         values [ins->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
2885                         break;
2886                 }
2887                 case OP_PHI:
2888                 case OP_FPHI:
2889                 case OP_VPHI:
2890                 case OP_XPHI: {
2891                         int i;
2892                         gboolean empty = TRUE;
2893
2894                         /* Check that all input bblocks really branch to us */
2895                         for (i = 0; i < bb->in_count; ++i) {
2896                                 if (bb->in_bb [i]->last_ins && bb->in_bb [i]->last_ins->opcode == OP_NOT_REACHED)
2897                                         ins->inst_phi_args [i + 1] = -1;
2898                                 else
2899                                         empty = FALSE;
2900                         }
2901
2902                         if (empty) {
2903                                 /* LLVM doesn't like phi instructions with zero operands */
2904                                 ctx->is_dead [ins->dreg] = TRUE;
2905                                 break;
2906                         }                                       
2907
2908                         /* Created earlier, insert it now */
2909                         LLVMInsertIntoBuilder (builder, values [ins->dreg]);
2910
2911                         for (i = 0; i < ins->inst_phi_args [0]; i++) {
2912                                 int sreg1 = ins->inst_phi_args [i + 1];
2913                                 int count, j;
2914
2915                                 /* 
2916                                  * Count the number of times the incoming bblock branches to us,
2917                                  * since llvm requires a separate entry for each.
2918                                  */
2919                                 if (bb->in_bb [i]->last_ins && bb->in_bb [i]->last_ins->opcode == OP_SWITCH) {
2920                                         MonoInst *switch_ins = bb->in_bb [i]->last_ins;
2921
2922                                         count = 0;
2923                                         for (j = 0; j < GPOINTER_TO_UINT (switch_ins->klass); ++j) {
2924                                                 if (switch_ins->inst_many_bb [j] == bb)
2925                                                         count ++;
2926                                         }
2927                                 } else {
2928                                         count = 1;
2929                                 }
2930
2931                                 /* Remember for later */
2932                                 for (j = 0; j < count; ++j) {
2933                                         PhiNode *node = mono_mempool_alloc0 (ctx->mempool, sizeof (PhiNode));
2934                                         node->bb = bb;
2935                                         node->phi = ins;
2936                                         node->in_bb = bb->in_bb [i];
2937                                         node->sreg = sreg1;
2938                                         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);
2939                                 }
2940                         }
2941                         break;
2942                 }
2943                 case OP_MOVE:
2944                 case OP_LMOVE:
2945                 case OP_XMOVE:
2946                 case OP_SETFRET:
2947                         g_assert (lhs);
2948                         values [ins->dreg] = lhs;
2949                         break;
2950                 case OP_FMOVE:
2951                 case OP_RMOVE: {
2952                         MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
2953                                 
2954                         g_assert (lhs);
2955                         values [ins->dreg] = lhs;
2956
2957                         if (var && var->klass->byval_arg.type == MONO_TYPE_R4) {
2958                                 /* 
2959                                  * This is added by the spilling pass in case of the JIT,
2960                                  * but we have to do it ourselves.
2961                                  */
2962                                 values [ins->dreg] = convert (ctx, values [ins->dreg], LLVMFloatType ());
2963                         }
2964                         break;
2965                 }
2966                 case OP_MOVE_F_TO_I4: {
2967                         values [ins->dreg] = LLVMBuildBitCast (builder, LLVMBuildFPTrunc (builder, lhs, LLVMFloatType (), ""), LLVMInt32Type (), "");
2968                         break;
2969                 }
2970                 case OP_MOVE_I4_TO_F: {
2971                         values [ins->dreg] = LLVMBuildFPExt (builder, LLVMBuildBitCast (builder, lhs, LLVMFloatType (), ""), LLVMDoubleType (), "");
2972                         break;
2973                 }
2974                 case OP_MOVE_F_TO_I8: {
2975                         values [ins->dreg] = LLVMBuildBitCast (builder, lhs, LLVMInt64Type (), "");
2976                         break;
2977                 }
2978                 case OP_MOVE_I8_TO_F: {
2979                         values [ins->dreg] = LLVMBuildBitCast (builder, lhs, LLVMDoubleType (), "");
2980                         break;
2981                 }
2982                 case OP_IADD:
2983                 case OP_ISUB:
2984                 case OP_IAND:
2985                 case OP_IMUL:
2986                 case OP_IDIV:
2987                 case OP_IDIV_UN:
2988                 case OP_IREM:
2989                 case OP_IREM_UN:
2990                 case OP_IOR:
2991                 case OP_IXOR:
2992                 case OP_ISHL:
2993                 case OP_ISHR:
2994                 case OP_ISHR_UN:
2995                 case OP_FADD:
2996                 case OP_FSUB:
2997                 case OP_FMUL:
2998                 case OP_FDIV:
2999                 case OP_LADD:
3000                 case OP_LSUB:
3001                 case OP_LMUL:
3002                 case OP_LDIV:
3003                 case OP_LDIV_UN:
3004                 case OP_LREM:
3005                 case OP_LREM_UN:
3006                 case OP_LAND:
3007                 case OP_LOR:
3008                 case OP_LXOR:
3009                 case OP_LSHL:
3010                 case OP_LSHR:
3011                 case OP_LSHR_UN:
3012                         lhs = convert (ctx, lhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
3013                         rhs = convert (ctx, rhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
3014
3015 #ifdef MONO_ARCH_NEED_DIV_CHECK
3016                         switch (ins->opcode) {
3017                         case OP_IDIV:
3018                         case OP_LDIV:
3019                         case OP_IREM:
3020                         case OP_LREM:
3021                         case OP_IDIV_UN:
3022                         case OP_LDIV_UN:
3023                         case OP_IREM_UN:
3024                         case OP_LREM_UN: {
3025                                 LLVMValueRef cmp;
3026
3027                                 cmp = LLVMBuildICmp (builder, LLVMIntEQ, rhs, LLVMConstInt (LLVMTypeOf (rhs), 0, FALSE), "");
3028                                 emit_cond_system_exception (ctx, bb, "DivideByZeroException", cmp);
3029                                 CHECK_FAILURE (ctx);
3030                                 builder = ctx->builder;
3031                                 break;
3032                         }
3033                         default:
3034                                 break;
3035                         }
3036 #endif
3037
3038                         switch (ins->opcode) {
3039                         case OP_IADD:
3040                         case OP_LADD:
3041                                 values [ins->dreg] = LLVMBuildAdd (builder, lhs, rhs, dname);
3042                                 break;
3043                         case OP_ISUB:
3044                         case OP_LSUB:
3045                                 values [ins->dreg] = LLVMBuildSub (builder, lhs, rhs, dname);
3046                                 break;
3047                         case OP_IMUL:
3048                         case OP_LMUL:
3049                                 values [ins->dreg] = LLVMBuildMul (builder, lhs, rhs, dname);
3050                                 break;
3051                         case OP_IREM:
3052                         case OP_LREM:
3053                                 values [ins->dreg] = LLVMBuildSRem (builder, lhs, rhs, dname);
3054                                 break;
3055                         case OP_IREM_UN:
3056                         case OP_LREM_UN:
3057                                 values [ins->dreg] = LLVMBuildURem (builder, lhs, rhs, dname);
3058                                 break;
3059                         case OP_IDIV:
3060                         case OP_LDIV:
3061                                 values [ins->dreg] = LLVMBuildSDiv (builder, lhs, rhs, dname);
3062                                 break;
3063                         case OP_IDIV_UN:
3064                         case OP_LDIV_UN:
3065                                 values [ins->dreg] = LLVMBuildUDiv (builder, lhs, rhs, dname);
3066                                 break;
3067                         case OP_FDIV:
3068                         case OP_RDIV:
3069                                 values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, dname);
3070                                 break;
3071                         case OP_IAND:
3072                         case OP_LAND:
3073                                 values [ins->dreg] = LLVMBuildAnd (builder, lhs, rhs, dname);
3074                                 break;
3075                         case OP_IOR:
3076                         case OP_LOR:
3077                                 values [ins->dreg] = LLVMBuildOr (builder, lhs, rhs, dname);
3078                                 break;
3079                         case OP_IXOR:
3080                         case OP_LXOR:
3081                                 values [ins->dreg] = LLVMBuildXor (builder, lhs, rhs, dname);
3082                                 break;
3083                         case OP_ISHL:
3084                         case OP_LSHL:
3085                                 values [ins->dreg] = LLVMBuildShl (builder, lhs, rhs, dname);
3086                                 break;
3087                         case OP_ISHR:
3088                         case OP_LSHR:
3089                                 values [ins->dreg] = LLVMBuildAShr (builder, lhs, rhs, dname);
3090                                 break;
3091                         case OP_ISHR_UN:
3092                         case OP_LSHR_UN:
3093                                 values [ins->dreg] = LLVMBuildLShr (builder, lhs, rhs, dname);
3094                                 break;
3095
3096                         case OP_FADD:
3097                                 values [ins->dreg] = LLVMBuildFAdd (builder, lhs, rhs, dname);
3098                                 break;
3099                         case OP_FSUB:
3100                                 values [ins->dreg] = LLVMBuildFSub (builder, lhs, rhs, dname);
3101                                 break;
3102                         case OP_FMUL:
3103                                 values [ins->dreg] = LLVMBuildFMul (builder, lhs, rhs, dname);
3104                                 break;
3105
3106                         default:
3107                                 g_assert_not_reached ();
3108                         }
3109                         break;
3110                 case OP_RADD:
3111                 case OP_RSUB:
3112                 case OP_RMUL:
3113                 case OP_RDIV: {
3114                         lhs = convert (ctx, lhs, LLVMFloatType ());
3115                         rhs = convert (ctx, rhs, LLVMFloatType ());
3116                         switch (ins->opcode) {
3117                         case OP_RADD:
3118                                 values [ins->dreg] = LLVMBuildFAdd (builder, lhs, rhs, dname);
3119                                 break;
3120                         case OP_RSUB:
3121                                 values [ins->dreg] = LLVMBuildFSub (builder, lhs, rhs, dname);
3122                                 break;
3123                         case OP_RMUL:
3124                                 values [ins->dreg] = LLVMBuildFMul (builder, lhs, rhs, dname);
3125                                 break;
3126                         case OP_RDIV:
3127                                 values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, dname);
3128                                 break;
3129                         default:
3130                                 g_assert_not_reached ();
3131                                 break;
3132                         }
3133                         break;
3134                 }
3135                 case OP_IADD_IMM:
3136                 case OP_ISUB_IMM:
3137                 case OP_IMUL_IMM:
3138                 case OP_IREM_IMM:
3139                 case OP_IREM_UN_IMM:
3140                 case OP_IDIV_IMM:
3141                 case OP_IDIV_UN_IMM:
3142                 case OP_IAND_IMM:
3143                 case OP_IOR_IMM:
3144                 case OP_IXOR_IMM:
3145                 case OP_ISHL_IMM:
3146                 case OP_ISHR_IMM:
3147                 case OP_ISHR_UN_IMM:
3148                 case OP_LADD_IMM:
3149                 case OP_LSUB_IMM:
3150                 case OP_LREM_IMM:
3151                 case OP_LAND_IMM:
3152                 case OP_LOR_IMM:
3153                 case OP_LXOR_IMM:
3154                 case OP_LSHL_IMM:
3155                 case OP_LSHR_IMM:
3156                 case OP_LSHR_UN_IMM:
3157                 case OP_ADD_IMM:
3158                 case OP_AND_IMM:
3159                 case OP_MUL_IMM:
3160                 case OP_SHL_IMM:
3161                 case OP_SHR_IMM:
3162                 case OP_SHR_UN_IMM: {
3163                         LLVMValueRef imm;
3164
3165                         if (spec [MONO_INST_SRC1] == 'l') {
3166                                 imm = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
3167                         } else {
3168                                 imm = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
3169                         }
3170
3171 #if SIZEOF_VOID_P == 4
3172                         if (ins->opcode == OP_LSHL_IMM || ins->opcode == OP_LSHR_IMM || ins->opcode == OP_LSHR_UN_IMM)
3173                                 imm = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
3174 #endif
3175
3176                         if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
3177                                 lhs = convert (ctx, lhs, IntPtrType ());
3178                         imm = convert (ctx, imm, LLVMTypeOf (lhs));
3179                         switch (ins->opcode) {
3180                         case OP_IADD_IMM:
3181                         case OP_LADD_IMM:
3182                         case OP_ADD_IMM:
3183                                 values [ins->dreg] = LLVMBuildAdd (builder, lhs, imm, dname);
3184                                 break;
3185                         case OP_ISUB_IMM:
3186                         case OP_LSUB_IMM:
3187                                 values [ins->dreg] = LLVMBuildSub (builder, lhs, imm, dname);
3188                                 break;
3189                         case OP_IMUL_IMM:
3190                         case OP_MUL_IMM:
3191                                 values [ins->dreg] = LLVMBuildMul (builder, lhs, imm, dname);
3192                                 break;
3193                         case OP_IDIV_IMM:
3194                         case OP_LDIV_IMM:
3195                                 values [ins->dreg] = LLVMBuildSDiv (builder, lhs, imm, dname);
3196                                 break;
3197                         case OP_IDIV_UN_IMM:
3198                         case OP_LDIV_UN_IMM:
3199                                 values [ins->dreg] = LLVMBuildUDiv (builder, lhs, imm, dname);
3200                                 break;
3201                         case OP_IREM_IMM:
3202                         case OP_LREM_IMM:
3203                                 values [ins->dreg] = LLVMBuildSRem (builder, lhs, imm, dname);
3204                                 break;
3205                         case OP_IREM_UN_IMM:
3206                                 values [ins->dreg] = LLVMBuildURem (builder, lhs, imm, dname);
3207                                 break;
3208                         case OP_IAND_IMM:
3209                         case OP_LAND_IMM:
3210                         case OP_AND_IMM:
3211                                 values [ins->dreg] = LLVMBuildAnd (builder, lhs, imm, dname);
3212                                 break;
3213                         case OP_IOR_IMM:
3214                         case OP_LOR_IMM:
3215                                 values [ins->dreg] = LLVMBuildOr (builder, lhs, imm, dname);
3216                                 break;
3217                         case OP_IXOR_IMM:
3218                         case OP_LXOR_IMM:
3219                                 values [ins->dreg] = LLVMBuildXor (builder, lhs, imm, dname);
3220                                 break;
3221                         case OP_ISHL_IMM:
3222                         case OP_LSHL_IMM:
3223                         case OP_SHL_IMM:
3224                                 values [ins->dreg] = LLVMBuildShl (builder, lhs, imm, dname);
3225                                 break;
3226                         case OP_ISHR_IMM:
3227                         case OP_LSHR_IMM:
3228                         case OP_SHR_IMM:
3229                                 values [ins->dreg] = LLVMBuildAShr (builder, lhs, imm, dname);
3230                                 break;
3231                         case OP_ISHR_UN_IMM:
3232                                 /* This is used to implement conv.u4, so the lhs could be an i8 */
3233                                 lhs = convert (ctx, lhs, LLVMInt32Type ());
3234                                 imm = convert (ctx, imm, LLVMInt32Type ());
3235                                 values [ins->dreg] = LLVMBuildLShr (builder, lhs, imm, dname);
3236                                 break;
3237                         case OP_LSHR_UN_IMM:
3238                         case OP_SHR_UN_IMM:
3239                                 values [ins->dreg] = LLVMBuildLShr (builder, lhs, imm, dname);
3240                                 break;
3241                         default:
3242                                 g_assert_not_reached ();
3243                         }
3244                         break;
3245                 }
3246                 case OP_INEG:
3247                         values [ins->dreg] = LLVMBuildSub (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), convert (ctx, lhs, LLVMInt32Type ()), dname);
3248                         break;
3249                 case OP_LNEG:
3250                         values [ins->dreg] = LLVMBuildSub (builder, LLVMConstInt (LLVMInt64Type (), 0, FALSE), lhs, dname);
3251                         break;
3252                 case OP_FNEG:
3253                         lhs = convert (ctx, lhs, LLVMDoubleType ());
3254                         values [ins->dreg] = LLVMBuildFSub (builder, LLVMConstReal (LLVMDoubleType (), 0.0), lhs, dname);
3255                         break;
3256                 case OP_RNEG:
3257                         lhs = convert (ctx, lhs, LLVMFloatType ());
3258                         values [ins->dreg] = LLVMBuildFSub (builder, LLVMConstReal (LLVMFloatType (), 0.0), lhs, dname);
3259                         break;
3260                 case OP_INOT: {
3261                         guint32 v = 0xffffffff;
3262                         values [ins->dreg] = LLVMBuildXor (builder, LLVMConstInt (LLVMInt32Type (), v, FALSE), convert (ctx, lhs, LLVMInt32Type ()), dname);
3263                         break;
3264                 }
3265                 case OP_LNOT: {
3266                         guint64 v = 0xffffffffffffffffLL;
3267                         values [ins->dreg] = LLVMBuildXor (builder, LLVMConstInt (LLVMInt64Type (), v, FALSE), lhs, dname);
3268                         break;
3269                 }
3270 #if defined(TARGET_X86) || defined(TARGET_AMD64)
3271                 case OP_X86_LEA: {
3272                         LLVMValueRef v1, v2;
3273
3274                         v1 = LLVMBuildMul (builder, convert (ctx, rhs, IntPtrType ()), LLVMConstInt (IntPtrType (), (1 << ins->backend.shift_amount), FALSE), "");
3275                         v2 = LLVMBuildAdd (builder, convert (ctx, lhs, IntPtrType ()), v1, "");
3276                         values [ins->dreg] = LLVMBuildAdd (builder, v2, LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), dname);
3277                         break;
3278                 }
3279 #endif
3280
3281                 case OP_ICONV_TO_I1:
3282                 case OP_ICONV_TO_I2:
3283                 case OP_ICONV_TO_I4:
3284                 case OP_ICONV_TO_U1:
3285                 case OP_ICONV_TO_U2:
3286                 case OP_ICONV_TO_U4:
3287                 case OP_LCONV_TO_I1:
3288                 case OP_LCONV_TO_I2:
3289                 case OP_LCONV_TO_U1:
3290                 case OP_LCONV_TO_U2:
3291                 case OP_LCONV_TO_U4: {
3292                         gboolean sign;
3293
3294                         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);
3295
3296                         /* Have to do two casts since our vregs have type int */
3297                         v = LLVMBuildTrunc (builder, lhs, op_to_llvm_type (ins->opcode), "");
3298                         if (sign)
3299                                 values [ins->dreg] = LLVMBuildSExt (builder, v, LLVMInt32Type (), dname);
3300                         else
3301                                 values [ins->dreg] = LLVMBuildZExt (builder, v, LLVMInt32Type (), dname);
3302                         break;
3303                 }
3304                 case OP_ICONV_TO_I8:
3305                         values [ins->dreg] = LLVMBuildSExt (builder, lhs, LLVMInt64Type (), dname);
3306                         break;
3307                 case OP_ICONV_TO_U8:
3308                         values [ins->dreg] = LLVMBuildZExt (builder, lhs, LLVMInt64Type (), dname);
3309                         break;
3310                 case OP_FCONV_TO_I4:
3311                 case OP_RCONV_TO_I4:
3312                         values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, LLVMInt32Type (), dname);
3313                         break;
3314                 case OP_FCONV_TO_I1:
3315                 case OP_RCONV_TO_I1:
3316                         values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildFPToSI (builder, lhs, LLVMInt8Type (), dname), LLVMInt32Type (), "");
3317                         break;
3318                 case OP_FCONV_TO_U1:
3319                 case OP_RCONV_TO_U1:
3320                         values [ins->dreg] = LLVMBuildZExt (builder, LLVMBuildFPToUI (builder, lhs, LLVMInt8Type (), dname), LLVMInt32Type (), "");
3321                         break;
3322                 case OP_FCONV_TO_I2:
3323                 case OP_RCONV_TO_I2:
3324                         values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildFPToSI (builder, lhs, LLVMInt16Type (), dname), LLVMInt32Type (), "");
3325                         break;
3326                 case OP_FCONV_TO_U2:
3327                 case OP_RCONV_TO_U2:
3328                         values [ins->dreg] = LLVMBuildZExt (builder, LLVMBuildFPToUI (builder, lhs, LLVMInt16Type (), dname), LLVMInt32Type (), "");
3329                         break;
3330                 case OP_FCONV_TO_I8:
3331                 case OP_RCONV_TO_I8:
3332                         values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, LLVMInt64Type (), dname);
3333                         break;
3334                 case OP_FCONV_TO_I:
3335                         values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, IntPtrType (), dname);
3336                         break;
3337                 case OP_ICONV_TO_R8:
3338                 case OP_LCONV_TO_R8:
3339                         values [ins->dreg] = LLVMBuildSIToFP (builder, lhs, LLVMDoubleType (), dname);
3340                         break;
3341                 case OP_LCONV_TO_R_UN:
3342                         values [ins->dreg] = LLVMBuildUIToFP (builder, lhs, LLVMDoubleType (), dname);
3343                         break;
3344 #if SIZEOF_VOID_P == 4
3345                 case OP_LCONV_TO_U:
3346 #endif
3347                 case OP_LCONV_TO_I4:
3348                         values [ins->dreg] = LLVMBuildTrunc (builder, lhs, LLVMInt32Type (), dname);
3349                         break;
3350                 case OP_ICONV_TO_R4:
3351                 case OP_LCONV_TO_R4:
3352                         v = LLVMBuildSIToFP (builder, lhs, LLVMFloatType (), "");
3353                         if (cfg->r4fp)
3354                                 values [ins->dreg] = v;
3355                         else
3356                                 values [ins->dreg] = LLVMBuildFPExt (builder, v, LLVMDoubleType (), dname);
3357                         break;
3358                 case OP_FCONV_TO_R4:
3359                         v = LLVMBuildFPTrunc (builder, lhs, LLVMFloatType (), "");
3360                         if (cfg->r4fp)
3361                                 values [ins->dreg] = v;
3362                         else
3363                                 values [ins->dreg] = LLVMBuildFPExt (builder, v, LLVMDoubleType (), dname);
3364                         break;
3365                 case OP_RCONV_TO_R8:
3366                         values [ins->dreg] = LLVMBuildFPExt (builder, lhs, LLVMDoubleType (), dname);
3367                         break;
3368                 case OP_RCONV_TO_R4:
3369                         values [ins->dreg] = lhs;
3370                         break;
3371                 case OP_SEXT_I4:
3372                         values [ins->dreg] = LLVMBuildSExt (builder, convert (ctx, lhs, LLVMInt32Type ()), LLVMInt64Type (), dname);
3373                         break;
3374                 case OP_ZEXT_I4:
3375                         values [ins->dreg] = LLVMBuildZExt (builder, convert (ctx, lhs, LLVMInt32Type ()), LLVMInt64Type (), dname);
3376                         break;
3377                 case OP_TRUNC_I4:
3378                         values [ins->dreg] = LLVMBuildTrunc (builder, lhs, LLVMInt32Type (), dname);
3379                         break;
3380                 case OP_LOCALLOC_IMM: {
3381                         LLVMValueRef v;
3382
3383                         guint32 size = ins->inst_imm;
3384                         size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
3385
3386                         v = mono_llvm_build_alloca (builder, LLVMInt8Type (), LLVMConstInt (LLVMInt32Type (), size, FALSE), MONO_ARCH_FRAME_ALIGNMENT, "");
3387
3388                         if (ins->flags & MONO_INST_INIT) {
3389                                 LLVMValueRef args [5];
3390
3391                                 args [0] = v;
3392                                 args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
3393                                 args [2] = LLVMConstInt (LLVMInt32Type (), size, FALSE);
3394                                 args [3] = LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT, FALSE);
3395                                 args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
3396                                 LLVMBuildCall (builder, LLVMGetNamedFunction (module, memset_func_name), args, memset_param_count, "");
3397                         }
3398
3399                         values [ins->dreg] = v;
3400                         break;
3401                 }
3402                 case OP_LOCALLOC: {
3403                         LLVMValueRef v, size;
3404                                 
3405                         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), "");
3406
3407                         v = mono_llvm_build_alloca (builder, LLVMInt8Type (), size, MONO_ARCH_FRAME_ALIGNMENT, "");
3408
3409                         if (ins->flags & MONO_INST_INIT) {
3410                                 LLVMValueRef args [5];
3411
3412                                 args [0] = v;
3413                                 args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
3414                                 args [2] = size;
3415                                 args [3] = LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT, FALSE);
3416                                 args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
3417                                 LLVMBuildCall (builder, LLVMGetNamedFunction (module, memset_func_name), args, memset_param_count, "");
3418                         }
3419                         values [ins->dreg] = v;
3420                         break;
3421                 }
3422
3423                 case OP_LOADI1_MEMBASE:
3424                 case OP_LOADU1_MEMBASE:
3425                 case OP_LOADI2_MEMBASE:
3426                 case OP_LOADU2_MEMBASE:
3427                 case OP_LOADI4_MEMBASE:
3428                 case OP_LOADU4_MEMBASE:
3429                 case OP_LOADI8_MEMBASE:
3430                 case OP_LOADR4_MEMBASE:
3431                 case OP_LOADR8_MEMBASE:
3432                 case OP_LOAD_MEMBASE:
3433                 case OP_LOADI8_MEM:
3434                 case OP_LOADU1_MEM:
3435                 case OP_LOADU2_MEM:
3436                 case OP_LOADI4_MEM:
3437                 case OP_LOADU4_MEM:
3438                 case OP_LOAD_MEM: {
3439                         int size = 8;
3440                         LLVMValueRef base, index, addr;
3441                         LLVMTypeRef t;
3442                         gboolean sext = FALSE, zext = FALSE;
3443                         gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
3444
3445                         t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
3446
3447                         if (sext || zext)
3448                                 dname = (char*)"";
3449
3450                         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)) {
3451                                 addr = LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE);
3452                         } else {
3453                                 /* _MEMBASE */
3454                                 base = lhs;
3455
3456                                 if (ins->inst_offset == 0) {
3457                                         addr = base;
3458                                 } else if (ins->inst_offset % size != 0) {
3459                                         /* Unaligned load */
3460                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
3461                                         addr = LLVMBuildGEP (builder, convert (ctx, base, LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
3462                                 } else {
3463                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
3464                                         addr = LLVMBuildGEP (builder, convert (ctx, base, LLVMPointerType (t, 0)), &index, 1, "");
3465                                 }
3466                         }
3467
3468                         addr = convert (ctx, addr, LLVMPointerType (t, 0));
3469
3470                         values [ins->dreg] = emit_load (ctx, bb, &builder, size, addr, dname, is_volatile);
3471
3472                         if (!is_volatile && (ins->flags & MONO_INST_INVARIANT_LOAD)) {
3473                                 /*
3474                                  * These will signal LLVM that these loads do not alias any stores, and
3475                                  * they can't fail, allowing them to be hoisted out of loops.
3476                                  */
3477                                 set_invariant_load_flag (values [ins->dreg]);
3478                                 set_metadata_flag (values [ins->dreg], "mono.nofail.load");
3479                         }
3480
3481                         if (sext)
3482                                 values [ins->dreg] = LLVMBuildSExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
3483                         else if (zext)
3484                                 values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
3485                         else if (!cfg->r4fp && ins->opcode == OP_LOADR4_MEMBASE)
3486                                 values [ins->dreg] = LLVMBuildFPExt (builder, values [ins->dreg], LLVMDoubleType (), dname);
3487                         break;
3488                 }
3489                                 
3490                 case OP_STOREI1_MEMBASE_REG:
3491                 case OP_STOREI2_MEMBASE_REG:
3492                 case OP_STOREI4_MEMBASE_REG:
3493                 case OP_STOREI8_MEMBASE_REG:
3494                 case OP_STORER4_MEMBASE_REG:
3495                 case OP_STORER8_MEMBASE_REG:
3496                 case OP_STORE_MEMBASE_REG: {
3497                         int size = 8;
3498                         LLVMValueRef index, addr;
3499                         LLVMTypeRef t;
3500                         gboolean sext = FALSE, zext = FALSE;
3501                         gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
3502
3503                         if (!values [ins->inst_destbasereg])
3504                                 LLVM_FAILURE (ctx, "inst_destbasereg");
3505
3506                         t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
3507
3508                         if (ins->inst_offset % size != 0) {
3509                                 /* Unaligned store */
3510                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
3511                                 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
3512                         } else {
3513                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
3514                                 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, "");
3515                         }
3516                         emit_store (ctx, bb, &builder, size, convert (ctx, values [ins->sreg1], t), convert (ctx, addr, LLVMPointerType (t, 0)), is_volatile);
3517                         break;
3518                 }
3519
3520                 case OP_STOREI1_MEMBASE_IMM:
3521                 case OP_STOREI2_MEMBASE_IMM:
3522                 case OP_STOREI4_MEMBASE_IMM:
3523                 case OP_STOREI8_MEMBASE_IMM:
3524                 case OP_STORE_MEMBASE_IMM: {
3525                         int size = 8;
3526                         LLVMValueRef index, addr;
3527                         LLVMTypeRef t;
3528                         gboolean sext = FALSE, zext = FALSE;
3529                         gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
3530
3531                         t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
3532
3533                         if (ins->inst_offset % size != 0) {
3534                                 /* Unaligned store */
3535                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
3536                                 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
3537                         } else {
3538                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
3539                                 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, "");
3540                         }
3541                         emit_store (ctx, bb, &builder, size, convert (ctx, LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), t), convert (ctx, addr, LLVMPointerType (t, 0)), is_volatile);
3542                         break;
3543                 }
3544
3545                 case OP_CHECK_THIS:
3546                         emit_load (ctx, bb, &builder, sizeof (gpointer), convert (ctx, lhs, LLVMPointerType (IntPtrType (), 0)), "", TRUE);
3547                         break;
3548                 case OP_OUTARG_VTRETADDR:
3549                         break;
3550                 case OP_VOIDCALL:
3551                 case OP_CALL:
3552                 case OP_LCALL:
3553                 case OP_FCALL:
3554                 case OP_RCALL:
3555                 case OP_VCALL:
3556                 case OP_VOIDCALL_MEMBASE:
3557                 case OP_CALL_MEMBASE:
3558                 case OP_LCALL_MEMBASE:
3559                 case OP_FCALL_MEMBASE:
3560                 case OP_RCALL_MEMBASE:
3561                 case OP_VCALL_MEMBASE:
3562                 case OP_VOIDCALL_REG:
3563                 case OP_CALL_REG:
3564                 case OP_LCALL_REG:
3565                 case OP_FCALL_REG:
3566                 case OP_RCALL_REG:
3567                 case OP_VCALL_REG: {
3568                         process_call (ctx, bb, &builder, ins);
3569                         CHECK_FAILURE (ctx);
3570                         break;
3571                 }
3572                 case OP_AOTCONST: {
3573                         guint32 got_offset;
3574                         LLVMValueRef indexes [2];
3575                         MonoJumpInfo *ji;
3576                         LLVMValueRef got_entry_addr;
3577
3578                         /* 
3579                          * FIXME: Can't allocate from the cfg mempool since that is freed if
3580                          * the LLVM compile fails.
3581                          */
3582                         ji = g_new0 (MonoJumpInfo, 1);
3583                         ji->type = (MonoJumpInfoType)ins->inst_i1;
3584                         ji->data.target = ins->inst_p0;
3585
3586                         ji = mono_aot_patch_info_dup (ji);
3587
3588                         ji->next = cfg->patch_info;
3589                         cfg->patch_info = ji;
3590                                    
3591                         //mono_add_patch_info (cfg, 0, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3592                         got_offset = mono_aot_get_got_offset (cfg->patch_info);
3593                         ctx->lmodule->max_got_offset = MAX (ctx->lmodule->max_got_offset, got_offset);
3594  
3595                         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
3596                         indexes [1] = LLVMConstInt (LLVMInt32Type (), (gssize)got_offset, FALSE);
3597                         got_entry_addr = LLVMBuildGEP (builder, ctx->lmodule->got_var, indexes, 2, "");
3598
3599                         values [ins->dreg] = LLVMBuildLoad (builder, got_entry_addr, dname);
3600                         set_invariant_load_flag (values [ins->dreg]);
3601                         break;
3602                 }
3603                 case OP_NOT_REACHED:
3604                         LLVMBuildUnreachable (builder);
3605                         has_terminator = TRUE;
3606                         g_assert (bb->block_num < cfg->max_block_num);
3607                         ctx->unreachable [bb->block_num] = TRUE;
3608                         /* Might have instructions after this */
3609                         while (ins->next) {
3610                                 MonoInst *next = ins->next;
3611                                 /* 
3612                                  * FIXME: If later code uses the regs defined by these instructions,
3613                                  * compilation will fail.
3614                                  */
3615                                 MONO_DELETE_INS (bb, next);
3616                         }                               
3617                         break;
3618                 case OP_LDADDR: {
3619                         MonoInst *var = ins->inst_p0;
3620
3621                         values [ins->dreg] = addresses [var->dreg];
3622                         break;
3623                 }
3624                 case OP_SIN: {
3625                         LLVMValueRef args [1];
3626
3627                         args [0] = convert (ctx, lhs, LLVMDoubleType ());
3628                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.sin.f64"), args, 1, dname);
3629                         break;
3630                 }
3631                 case OP_COS: {
3632                         LLVMValueRef args [1];
3633
3634                         args [0] = convert (ctx, lhs, LLVMDoubleType ());
3635                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.cos.f64"), args, 1, dname);
3636                         break;
3637                 }
3638                 case OP_SQRT: {
3639                         LLVMValueRef args [1];
3640
3641 #if 0
3642                         /* This no longer seems to happen */
3643                         /*
3644                          * LLVM optimizes sqrt(nan) into undefined in
3645                          * lib/Analysis/ConstantFolding.cpp
3646                          * Also, sqrt(NegativeInfinity) is optimized into 0.
3647                          */
3648                         LLVM_FAILURE (ctx, "sqrt");
3649 #endif
3650                         args [0] = convert (ctx, lhs, LLVMDoubleType ());
3651                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.sqrt.f64"), args, 1, dname);
3652                         break;
3653                 }
3654                 case OP_ABS: {
3655                         LLVMValueRef args [1];
3656
3657                         args [0] = convert (ctx, lhs, LLVMDoubleType ());
3658                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "fabs"), args, 1, dname);
3659                         break;
3660                 }
3661
3662                 case OP_IMIN:
3663                 case OP_LMIN:
3664                 case OP_IMAX:
3665                 case OP_LMAX:
3666                 case OP_IMIN_UN:
3667                 case OP_LMIN_UN:
3668                 case OP_IMAX_UN:
3669                 case OP_LMAX_UN: {
3670                         LLVMValueRef v;
3671
3672                         lhs = convert (ctx, lhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
3673                         rhs = convert (ctx, rhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
3674
3675                         switch (ins->opcode) {
3676                         case OP_IMIN:
3677                         case OP_LMIN:
3678                                 v = LLVMBuildICmp (builder, LLVMIntSLE, lhs, rhs, "");
3679                                 break;
3680                         case OP_IMAX:
3681                         case OP_LMAX:
3682                                 v = LLVMBuildICmp (builder, LLVMIntSGE, lhs, rhs, "");
3683                                 break;
3684                         case OP_IMIN_UN:
3685                         case OP_LMIN_UN:
3686                                 v = LLVMBuildICmp (builder, LLVMIntULE, lhs, rhs, "");
3687                                 break;
3688                         case OP_IMAX_UN:
3689                         case OP_LMAX_UN:
3690                                 v = LLVMBuildICmp (builder, LLVMIntUGE, lhs, rhs, "");
3691                                 break;
3692                         default:
3693                                 g_assert_not_reached ();
3694                                 break;
3695                         }
3696                         values [ins->dreg] = LLVMBuildSelect (builder, v, lhs, rhs, dname);
3697                         break;
3698                 }
3699                 case OP_ATOMIC_EXCHANGE_I4:
3700                 case OP_ATOMIC_EXCHANGE_I8: {
3701                         LLVMValueRef args [2];
3702                         LLVMTypeRef t;
3703                                 
3704                         if (ins->opcode == OP_ATOMIC_EXCHANGE_I4)
3705                                 t = LLVMInt32Type ();
3706                         else
3707                                 t = LLVMInt64Type ();
3708
3709                         g_assert (ins->inst_offset == 0);
3710
3711                         args [0] = convert (ctx, lhs, LLVMPointerType (t, 0));
3712                         args [1] = convert (ctx, rhs, t);
3713
3714                         values [ins->dreg] = mono_llvm_build_atomic_rmw (builder, LLVM_ATOMICRMW_OP_XCHG, args [0], args [1]);
3715                         break;
3716                 }
3717                 case OP_ATOMIC_ADD_I4:
3718                 case OP_ATOMIC_ADD_I8: {
3719                         LLVMValueRef args [2];
3720                         LLVMTypeRef t;
3721                                 
3722                         if (ins->opcode == OP_ATOMIC_ADD_I4)
3723                                 t = LLVMInt32Type ();
3724                         else
3725                                 t = LLVMInt64Type ();
3726
3727                         g_assert (ins->inst_offset == 0);
3728
3729                         args [0] = convert (ctx, lhs, LLVMPointerType (t, 0));
3730                         args [1] = convert (ctx, rhs, t);
3731                         values [ins->dreg] = LLVMBuildAdd (builder, mono_llvm_build_atomic_rmw (builder, LLVM_ATOMICRMW_OP_ADD, args [0], args [1]), args [1], dname);
3732                         break;
3733                 }
3734                 case OP_ATOMIC_CAS_I4:
3735                 case OP_ATOMIC_CAS_I8: {
3736                         LLVMValueRef args [3], val;
3737                         LLVMTypeRef t;
3738                                 
3739                         if (ins->opcode == OP_ATOMIC_CAS_I4)
3740                                 t = LLVMInt32Type ();
3741                         else
3742                                 t = LLVMInt64Type ();
3743
3744                         args [0] = convert (ctx, lhs, LLVMPointerType (t, 0));
3745                         /* comparand */
3746                         args [1] = convert (ctx, values [ins->sreg3], t);
3747                         /* new value */
3748                         args [2] = convert (ctx, values [ins->sreg2], t);
3749                         val = mono_llvm_build_cmpxchg (builder, args [0], args [1], args [2]);
3750                         /* cmpxchg returns a pair */
3751                         values [ins->dreg] = LLVMBuildExtractValue (builder, val, 0, "");
3752                         break;
3753                 }
3754                 case OP_MEMORY_BARRIER: {
3755                         mono_llvm_build_fence (builder, (BarrierKind) ins->backend.memory_barrier_kind);
3756                         break;
3757                 }
3758                 case OP_ATOMIC_LOAD_I1:
3759                 case OP_ATOMIC_LOAD_I2:
3760                 case OP_ATOMIC_LOAD_I4:
3761                 case OP_ATOMIC_LOAD_I8:
3762                 case OP_ATOMIC_LOAD_U1:
3763                 case OP_ATOMIC_LOAD_U2:
3764                 case OP_ATOMIC_LOAD_U4:
3765                 case OP_ATOMIC_LOAD_U8:
3766                 case OP_ATOMIC_LOAD_R4:
3767                 case OP_ATOMIC_LOAD_R8: {
3768                         LLVM_FAILURE (ctx, "atomic mono.load intrinsic");
3769
3770                         int size;
3771                         gboolean sext, zext;
3772                         LLVMTypeRef t;
3773                         gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
3774                         BarrierKind barrier = (BarrierKind) ins->backend.memory_barrier_kind;
3775                         LLVMValueRef index, addr;
3776
3777                         t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
3778
3779                         if (sext || zext)
3780                                 dname = (char *)"";
3781
3782                         if (ins->inst_offset != 0) {
3783                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
3784                                 addr = LLVMBuildGEP (builder, convert (ctx, lhs, LLVMPointerType (t, 0)), &index, 1, "");
3785                         } else {
3786                                 addr = lhs;
3787                         }
3788
3789                         addr = convert (ctx, addr, LLVMPointerType (t, 0));
3790
3791                         values [ins->dreg] = emit_load_general (ctx, bb, &builder, size, addr, dname, is_volatile, barrier);
3792
3793                         if (sext)
3794                                 values [ins->dreg] = LLVMBuildSExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
3795                         else if (zext)
3796                                 values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
3797                         break;
3798                 }
3799                 case OP_ATOMIC_STORE_I1:
3800                 case OP_ATOMIC_STORE_I2:
3801                 case OP_ATOMIC_STORE_I4:
3802                 case OP_ATOMIC_STORE_I8:
3803                 case OP_ATOMIC_STORE_U1:
3804                 case OP_ATOMIC_STORE_U2:
3805                 case OP_ATOMIC_STORE_U4:
3806                 case OP_ATOMIC_STORE_U8:
3807                 case OP_ATOMIC_STORE_R4:
3808                 case OP_ATOMIC_STORE_R8: {
3809                         LLVM_FAILURE (ctx, "atomic mono.load intrinsic");
3810
3811                         int size;
3812                         gboolean sext, zext;
3813                         LLVMTypeRef t;
3814                         gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
3815                         BarrierKind barrier = (BarrierKind) ins->backend.memory_barrier_kind;
3816                         LLVMValueRef index, addr, value;
3817
3818                         if (!values [ins->inst_destbasereg])
3819                                 LLVM_FAILURE (ctx, "inst_destbasereg");
3820
3821                         t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
3822
3823                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
3824                         addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, "");
3825                         value = convert (ctx, values [ins->sreg1], t);
3826
3827                         emit_store_general (ctx, bb, &builder, size, value, addr, is_volatile, barrier);
3828                         break;
3829                 }
3830                 case OP_RELAXED_NOP: {
3831 #if defined(TARGET_AMD64) || defined(TARGET_X86)
3832                         emit_call (ctx, bb, &builder, LLVMGetNamedFunction (ctx->module, "llvm.x86.sse2.pause"), NULL, 0);
3833                         break;
3834 #else
3835                         break;
3836 #endif
3837                 }
3838                 case OP_TLS_GET: {
3839 #if (defined(TARGET_AMD64) || defined(TARGET_X86)) && defined(__linux__)
3840 #ifdef TARGET_AMD64
3841                         // 257 == FS segment register
3842                         LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 257);
3843 #else
3844                         // 256 == GS segment register
3845                         LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 256);
3846 #endif
3847                         // FIXME: XEN
3848                         values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildIntToPtr (builder, LLVMConstInt (IntPtrType (), ins->inst_offset, TRUE), ptrtype, ""), "");
3849 #elif defined(TARGET_AMD64) && defined(TARGET_OSX)
3850                         /* See mono_amd64_emit_tls_get () */
3851                         int offset = mono_amd64_get_tls_gs_offset () + (ins->inst_offset * 8);
3852
3853                         // 256 == GS segment register
3854                         LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 256);
3855                         values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildIntToPtr (builder, LLVMConstInt (IntPtrType (), offset, TRUE), ptrtype, ""), "");
3856 #else
3857                         LLVM_FAILURE (ctx, "opcode tls-get");
3858 #endif
3859
3860                         break;
3861                 }
3862                 case OP_TLS_GET_REG: {
3863 #if defined(TARGET_AMD64) && defined(TARGET_OSX)
3864                         /* See emit_tls_get_reg () */
3865                         // 256 == GS segment register
3866                         LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 256);
3867                         values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildIntToPtr (builder, convert (ctx, lhs, LLVMInt32Type ()), ptrtype, ""), "");
3868 #else
3869                         LLVM_FAILURE (ctx, "opcode tls-get");
3870 #endif
3871                         break;
3872                 }
3873
3874                         /*
3875                          * Overflow opcodes.
3876                          */
3877                 case OP_IADD_OVF:
3878                 case OP_IADD_OVF_UN:
3879                 case OP_ISUB_OVF:
3880                 case OP_ISUB_OVF_UN:
3881                 case OP_IMUL_OVF:
3882                 case OP_IMUL_OVF_UN:
3883 #if SIZEOF_VOID_P == 8
3884                 case OP_LADD_OVF:
3885                 case OP_LADD_OVF_UN:
3886                 case OP_LSUB_OVF:
3887                 case OP_LSUB_OVF_UN:
3888                 case OP_LMUL_OVF:
3889                 case OP_LMUL_OVF_UN:
3890 #endif
3891                         {
3892                                 LLVMValueRef args [2], val, ovf, func;
3893
3894                                 args [0] = convert (ctx, lhs, op_to_llvm_type (ins->opcode));
3895                                 args [1] = convert (ctx, rhs, op_to_llvm_type (ins->opcode));
3896                                 func = LLVMGetNamedFunction (module, ovf_op_to_intrins (ins->opcode));
3897                                 g_assert (func);
3898                                 val = LLVMBuildCall (builder, func, args, 2, "");
3899                                 values [ins->dreg] = LLVMBuildExtractValue (builder, val, 0, dname);
3900                                 ovf = LLVMBuildExtractValue (builder, val, 1, "");
3901                                 emit_cond_system_exception (ctx, bb, "OverflowException", ovf);
3902                                 CHECK_FAILURE (ctx);
3903                                 builder = ctx->builder;
3904                                 break;
3905                         }
3906
3907                         /* 
3908                          * Valuetypes.
3909                          *   We currently model them using arrays. Promotion to local vregs is 
3910                          * disabled for them in mono_handle_global_vregs () in the LLVM case, 
3911                          * so we always have an entry in cfg->varinfo for them.
3912                          * FIXME: Is this needed ?
3913                          */
3914                 case OP_VZERO: {
3915                         MonoClass *klass = ins->klass;
3916                         LLVMValueRef args [5];
3917
3918                         if (!klass) {
3919                                 // FIXME:
3920                                 LLVM_FAILURE (ctx, "!klass");
3921                                 break;
3922                         }
3923
3924                         if (!addresses [ins->dreg])
3925                                 addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
3926                         args [0] = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
3927                         args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
3928                         args [2] = LLVMConstInt (LLVMInt32Type (), mono_class_value_size (klass, NULL), FALSE);
3929                         // FIXME: Alignment
3930                         args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
3931                         args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
3932                         LLVMBuildCall (builder, LLVMGetNamedFunction (module, memset_func_name), args, memset_param_count, "");
3933                         break;
3934                 }
3935                 case OP_DUMMY_VZERO:
3936                         break;
3937
3938                 case OP_STOREV_MEMBASE:
3939                 case OP_LOADV_MEMBASE:
3940                 case OP_VMOVE: {
3941                         MonoClass *klass = ins->klass;
3942                         LLVMValueRef src = NULL, dst, args [5];
3943                         gboolean done = FALSE;
3944
3945                         if (!klass) {
3946                                 // FIXME:
3947                                 LLVM_FAILURE (ctx, "!klass");
3948                                 break;
3949                         }
3950
3951                         if (mini_is_gsharedvt_klass (cfg, klass)) {
3952                                 // FIXME:
3953                                 LLVM_FAILURE (ctx, "gsharedvt");
3954                                 break;
3955                         }
3956
3957                         switch (ins->opcode) {
3958                         case OP_STOREV_MEMBASE:
3959                                 if (cfg->gen_write_barriers && klass->has_references && ins->inst_destbasereg != cfg->frame_reg &&
3960                                         LLVMGetInstructionOpcode (values [ins->inst_destbasereg]) != LLVMAlloca) {
3961                                         /* Decomposed earlier */
3962                                         g_assert_not_reached ();
3963                                         break;
3964                                 }
3965                                 if (!addresses [ins->sreg1]) {
3966                                         /* SIMD */
3967                                         g_assert (values [ins->sreg1]);
3968                                         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));
3969                                         LLVMBuildStore (builder, values [ins->sreg1], dst);
3970                                         done = TRUE;
3971                                 } else {
3972                                         src = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMInt8Type (), 0), "");
3973                                         dst = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_destbasereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (LLVMInt8Type (), 0));
3974                                 }
3975                                 break;
3976                         case OP_LOADV_MEMBASE:
3977                                 if (!addresses [ins->dreg])
3978                                         addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
3979                                 src = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_basereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (LLVMInt8Type (), 0));
3980                                 dst = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
3981                                 break;
3982                         case OP_VMOVE:
3983                                 if (!addresses [ins->sreg1])
3984                                         addresses [ins->sreg1] = build_alloca (ctx, &klass->byval_arg);
3985                                 if (!addresses [ins->dreg])
3986                                         addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
3987                                 src = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMInt8Type (), 0), "");
3988                                 dst = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
3989                                 break;
3990                         default:
3991                                 g_assert_not_reached ();
3992                         }
3993                         CHECK_FAILURE (ctx);
3994
3995                         if (done)
3996                                 break;
3997
3998                         args [0] = dst;
3999                         args [1] = src;
4000                         args [2] = LLVMConstInt (LLVMInt32Type (), mono_class_value_size (klass, NULL), FALSE);
4001                         args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
4002                         // FIXME: Alignment
4003                         args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
4004                         args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
4005                         LLVMBuildCall (builder, LLVMGetNamedFunction (module, memcpy_func_name), args, memcpy_param_count, "");
4006                         break;
4007                 }
4008                 case OP_LLVM_OUTARG_VT:
4009                         if (!addresses [ins->sreg1]) {
4010                                 addresses [ins->sreg1] = build_alloca (ctx, &ins->klass->byval_arg);
4011                                 g_assert (values [ins->sreg1]);
4012                                 LLVMBuildStore (builder, values [ins->sreg1], addresses [ins->sreg1]);
4013                         }
4014                         addresses [ins->dreg] = addresses [ins->sreg1];
4015                         break;
4016
4017                         /* 
4018                          * SIMD
4019                          */
4020 #if defined(TARGET_X86) || defined(TARGET_AMD64)
4021                 case OP_XZERO: {
4022                         values [ins->dreg] = LLVMConstNull (type_to_llvm_type (ctx, &ins->klass->byval_arg));
4023                         break;
4024                 }
4025                 case OP_LOADX_MEMBASE: {
4026                         LLVMTypeRef t = type_to_llvm_type (ctx, &ins->klass->byval_arg);
4027                         LLVMValueRef src;
4028
4029                         src = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_basereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (t, 0));
4030                         values [ins->dreg] = mono_llvm_build_aligned_load (builder, src, "", FALSE, 1);
4031                         break;
4032                 }
4033                 case OP_STOREX_MEMBASE: {
4034                         LLVMTypeRef t = LLVMTypeOf (values [ins->sreg1]);
4035                         LLVMValueRef dest;
4036
4037                         dest = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_destbasereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (t, 0));
4038                         mono_llvm_build_aligned_store (builder, values [ins->sreg1], dest, FALSE, 1);
4039                         break;
4040                 }
4041                 case OP_PADDB:
4042                 case OP_PADDW:
4043                 case OP_PADDD:
4044                 case OP_PADDQ:
4045                         values [ins->dreg] = LLVMBuildAdd (builder, lhs, rhs, "");
4046                         break;
4047                 case OP_ADDPD:
4048                 case OP_ADDPS:
4049                         values [ins->dreg] = LLVMBuildFAdd (builder, lhs, rhs, "");
4050                         break;
4051                 case OP_PSUBB:
4052                 case OP_PSUBW:
4053                 case OP_PSUBD:
4054                 case OP_PSUBQ:
4055                         values [ins->dreg] = LLVMBuildSub (builder, lhs, rhs, "");
4056                         break;
4057                 case OP_SUBPD:
4058                 case OP_SUBPS:
4059                         values [ins->dreg] = LLVMBuildFSub (builder, lhs, rhs, "");
4060                         break;
4061                 case OP_MULPD:
4062                 case OP_MULPS:
4063                         values [ins->dreg] = LLVMBuildFMul (builder, lhs, rhs, "");
4064                         break;
4065                 case OP_DIVPD:
4066                 case OP_DIVPS:
4067                         values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, "");
4068                         break;
4069                 case OP_PAND:
4070                         values [ins->dreg] = LLVMBuildAnd (builder, lhs, rhs, "");
4071                         break;
4072                 case OP_POR:
4073                         values [ins->dreg] = LLVMBuildOr (builder, lhs, rhs, "");
4074                         break;
4075                 case OP_PXOR:
4076                         values [ins->dreg] = LLVMBuildXor (builder, lhs, rhs, "");
4077                         break;
4078                 case OP_PMULW:
4079                 case OP_PMULD:
4080                         values [ins->dreg] = LLVMBuildMul (builder, lhs, rhs, "");
4081                         break;
4082                 case OP_ANDPS:
4083                 case OP_ANDNPS:
4084                 case OP_ORPS:
4085                 case OP_XORPS:
4086                 case OP_ANDPD:
4087                 case OP_ANDNPD:
4088                 case OP_ORPD:
4089                 case OP_XORPD: {
4090                         LLVMTypeRef t, rt;
4091                         LLVMValueRef v = NULL;
4092
4093                         switch (ins->opcode) {
4094                         case OP_ANDPS:
4095                         case OP_ANDNPS:
4096                         case OP_ORPS:
4097                         case OP_XORPS:
4098                                 t = LLVMVectorType (LLVMInt32Type (), 4);
4099                                 rt = LLVMVectorType (LLVMFloatType (), 4);
4100                                 break;
4101                         case OP_ANDPD:
4102                         case OP_ANDNPD:
4103                         case OP_ORPD:
4104                         case OP_XORPD:
4105                                 t = LLVMVectorType (LLVMInt64Type (), 2);
4106                                 rt = LLVMVectorType (LLVMDoubleType (), 2);
4107                                 break;
4108                         default:
4109                                 t = LLVMInt32Type ();
4110                                 rt = LLVMInt32Type ();
4111                                 g_assert_not_reached ();
4112                         }
4113
4114                         lhs = LLVMBuildBitCast (builder, lhs, t, "");
4115                         rhs = LLVMBuildBitCast (builder, rhs, t, "");
4116                         switch (ins->opcode) {
4117                         case OP_ANDPS:
4118                         case OP_ANDPD:
4119                                 v = LLVMBuildAnd (builder, lhs, rhs, "");
4120                                 break;
4121                         case OP_ORPS:
4122                         case OP_ORPD:
4123                                 v = LLVMBuildOr (builder, lhs, rhs, "");
4124                                 break;
4125                         case OP_XORPS:
4126                         case OP_XORPD:
4127                                 v = LLVMBuildXor (builder, lhs, rhs, "");
4128                                 break;
4129                         case OP_ANDNPS:
4130                         case OP_ANDNPD:
4131                                 v = LLVMBuildAnd (builder, rhs, LLVMBuildNot (builder, lhs, ""), "");
4132                                 break;
4133                         }
4134                         values [ins->dreg] = LLVMBuildBitCast (builder, v, rt, "");
4135                         break;
4136                 }
4137                 case OP_MINPD:
4138                 case OP_MINPS:
4139                 case OP_MAXPD:
4140                 case OP_MAXPS:
4141                 case OP_ADDSUBPD:
4142                 case OP_ADDSUBPS:
4143                 case OP_PMIND_UN:
4144                 case OP_PMINW_UN:
4145                 case OP_PMINB_UN:
4146                 case OP_PMINW:
4147                 case OP_PMAXD_UN:
4148                 case OP_PMAXW_UN:
4149                 case OP_PMAXB_UN:
4150                 case OP_HADDPD:
4151                 case OP_HADDPS:
4152                 case OP_HSUBPD:
4153                 case OP_HSUBPS:
4154                 case OP_PADDB_SAT:
4155                 case OP_PADDW_SAT:
4156                 case OP_PSUBB_SAT:
4157                 case OP_PSUBW_SAT:
4158                 case OP_PADDB_SAT_UN:
4159                 case OP_PADDW_SAT_UN:
4160                 case OP_PSUBB_SAT_UN:
4161                 case OP_PSUBW_SAT_UN:
4162                 case OP_PAVGB_UN:
4163                 case OP_PAVGW_UN:
4164                 case OP_PACKW:
4165                 case OP_PACKD:
4166                 case OP_PACKW_UN:
4167                 case OP_PACKD_UN:
4168                 case OP_PMULW_HIGH:
4169                 case OP_PMULW_HIGH_UN: {
4170                         LLVMValueRef args [2];
4171
4172                         args [0] = lhs;
4173                         args [1] = rhs;
4174
4175                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, simd_op_to_intrins (ins->opcode)), args, 2, dname);
4176                         break;
4177                 }
4178                 case OP_PCMPEQB:
4179                 case OP_PCMPEQW:
4180                 case OP_PCMPEQD:
4181                 case OP_PCMPEQQ: {
4182                         values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildICmp (builder, LLVMIntEQ, lhs, rhs, ""), LLVMTypeOf (lhs), "");
4183                         break;
4184                 }
4185                 case OP_PCMPGTB: {
4186                         values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildICmp (builder, LLVMIntSGT, lhs, rhs, ""), LLVMTypeOf (lhs), "");
4187                         break;
4188                 }
4189                 case OP_EXTRACT_R8:
4190                 case OP_EXTRACT_I8:
4191                 case OP_EXTRACT_I4:
4192                 case OP_EXTRACT_I2:
4193                 case OP_EXTRACT_U2:
4194                 case OP_EXTRACTX_U2:
4195                 case OP_EXTRACT_I1:
4196                 case OP_EXTRACT_U1: {
4197                         LLVMTypeRef t;
4198                         gboolean zext = FALSE;
4199
4200                         t = simd_op_to_llvm_type (ins->opcode);
4201
4202                         switch (ins->opcode) {
4203                         case OP_EXTRACT_R8:
4204                         case OP_EXTRACT_I8:
4205                         case OP_EXTRACT_I4:
4206                         case OP_EXTRACT_I2:
4207                         case OP_EXTRACT_I1:
4208                                 break;
4209                         case OP_EXTRACT_U2:
4210                         case OP_EXTRACTX_U2:
4211                         case OP_EXTRACT_U1:
4212                                 zext = TRUE;
4213                                 break;
4214                         default:
4215                                 t = LLVMInt32Type ();
4216                                 g_assert_not_reached ();
4217                         }
4218
4219                         lhs = LLVMBuildBitCast (builder, lhs, t, "");
4220                         values [ins->dreg] = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), "");
4221                         if (zext)
4222                                 values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), "");
4223                         break;
4224                 }
4225
4226                 case OP_EXPAND_I1:
4227                 case OP_EXPAND_I2:
4228                 case OP_EXPAND_I4:
4229                 case OP_EXPAND_I8:
4230                 case OP_EXPAND_R4:
4231                 case OP_EXPAND_R8: {
4232                         LLVMTypeRef t = simd_op_to_llvm_type (ins->opcode);
4233                         LLVMValueRef mask [16], v;
4234                         int i;
4235
4236                         for (i = 0; i < 16; ++i)
4237                                 mask [i] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
4238
4239                         v = convert (ctx, values [ins->sreg1], LLVMGetElementType (t));
4240
4241                         values [ins->dreg] = LLVMBuildInsertElement (builder, LLVMConstNull (t), v, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
4242                         values [ins->dreg] = LLVMBuildShuffleVector (builder, values [ins->dreg], LLVMGetUndef (t), LLVMConstVector (mask, LLVMGetVectorSize (t)), "");
4243                         break;
4244                 }
4245
4246                 case OP_INSERT_I1:
4247                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt8Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
4248                         break;
4249                 case OP_INSERT_I2:
4250                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt16Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
4251                         break;
4252                 case OP_INSERT_I4:
4253                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt32Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
4254                         break;
4255                 case OP_INSERT_I8:
4256                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt64Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
4257                         break;
4258                 case OP_INSERT_R4:
4259                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMFloatType ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
4260                         break;
4261                 case OP_INSERT_R8:
4262                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMDoubleType ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
4263                         break;
4264
4265                 case OP_CVTDQ2PD:
4266                 case OP_CVTDQ2PS:
4267                 case OP_CVTPD2DQ:
4268                 case OP_CVTPS2DQ:
4269                 case OP_CVTPD2PS:
4270                 case OP_CVTPS2PD:
4271                 case OP_CVTTPD2DQ:
4272                 case OP_CVTTPS2DQ:
4273                 case OP_EXTRACT_MASK:
4274                 case OP_SQRTPS:
4275                 case OP_SQRTPD:
4276                 case OP_RSQRTPS:
4277                 case OP_RCPPS: {
4278                         LLVMValueRef v;
4279
4280                         v = convert (ctx, values [ins->sreg1], simd_op_to_llvm_type (ins->opcode));
4281
4282                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, simd_op_to_intrins (ins->opcode)), &v, 1, dname);
4283                         break;
4284                 }
4285
4286                 case OP_COMPPS:
4287                 case OP_COMPPD: {
4288                         LLVMValueRef args [3];
4289
4290                         args [0] = lhs;
4291                         args [1] = rhs;
4292                         args [2] = LLVMConstInt (LLVMInt8Type (), ins->inst_c0, FALSE);
4293
4294                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, simd_op_to_intrins (ins->opcode)), args, 3, dname);
4295                         break;
4296                 }
4297
4298                 case OP_ICONV_TO_X:
4299                         /* This is only used for implementing shifts by non-immediate */
4300                         values [ins->dreg] = lhs;
4301                         break;
4302
4303                 case OP_PSHRW:
4304                 case OP_PSHRD:
4305                 case OP_PSHRQ:
4306                 case OP_PSARW:
4307                 case OP_PSARD:
4308                 case OP_PSHLW:
4309                 case OP_PSHLD:
4310                 case OP_PSHLQ: {
4311                         LLVMValueRef args [3];
4312
4313                         args [0] = lhs;
4314                         args [1] = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
4315
4316                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, simd_op_to_intrins (ins->opcode)), args, 2, dname);
4317                         break;
4318                 }
4319
4320                 case OP_PSHRW_REG:
4321                 case OP_PSHRD_REG:
4322                 case OP_PSHRQ_REG:
4323                 case OP_PSARW_REG:
4324                 case OP_PSARD_REG:
4325                 case OP_PSHLW_REG:
4326                 case OP_PSHLD_REG:
4327                 case OP_PSHLQ_REG: {
4328                         LLVMValueRef args [3];
4329
4330                         args [0] = lhs;
4331                         args [1] = values [ins->sreg2];
4332
4333                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, simd_op_to_intrins (ins->opcode)), args, 2, dname);
4334                         break;
4335                 }
4336
4337                 case OP_SHUFPS:
4338                 case OP_SHUFPD:
4339                 case OP_PSHUFLED:
4340                 case OP_PSHUFLEW_LOW:
4341                 case OP_PSHUFLEW_HIGH: {
4342                         int mask [16];
4343                         LLVMValueRef v1 = NULL, v2 = NULL, mask_values [16];
4344                         int i, mask_size = 0;
4345                         int imask = ins->inst_c0;
4346         
4347                         /* Convert the x86 shuffle mask to LLVM's */
4348                         switch (ins->opcode) {
4349                         case OP_SHUFPS:
4350                                 mask_size = 4;
4351                                 mask [0] = ((imask >> 0) & 3);
4352                                 mask [1] = ((imask >> 2) & 3);
4353                                 mask [2] = ((imask >> 4) & 3) + 4;
4354                                 mask [3] = ((imask >> 6) & 3) + 4;
4355                                 v1 = values [ins->sreg1];
4356                                 v2 = values [ins->sreg2];
4357                                 break;
4358                         case OP_SHUFPD:
4359                                 mask_size = 2;
4360                                 mask [0] = ((imask >> 0) & 1);
4361                                 mask [1] = ((imask >> 1) & 1) + 2;
4362                                 v1 = values [ins->sreg1];
4363                                 v2 = values [ins->sreg2];
4364                                 break;
4365                         case OP_PSHUFLEW_LOW:
4366                                 mask_size = 8;
4367                                 mask [0] = ((imask >> 0) & 3);
4368                                 mask [1] = ((imask >> 2) & 3);
4369                                 mask [2] = ((imask >> 4) & 3);
4370                                 mask [3] = ((imask >> 6) & 3);
4371                                 mask [4] = 4 + 0;
4372                                 mask [5] = 4 + 1;
4373                                 mask [6] = 4 + 2;
4374                                 mask [7] = 4 + 3;
4375                                 v1 = values [ins->sreg1];
4376                                 v2 = LLVMGetUndef (LLVMTypeOf (v1));
4377                                 break;
4378                         case OP_PSHUFLEW_HIGH:
4379                                 mask_size = 8;
4380                                 mask [0] = 0;
4381                                 mask [1] = 1;
4382                                 mask [2] = 2;
4383                                 mask [3] = 3;
4384                                 mask [4] = 4 + ((imask >> 0) & 3);
4385                                 mask [5] = 4 + ((imask >> 2) & 3);
4386                                 mask [6] = 4 + ((imask >> 4) & 3);
4387                                 mask [7] = 4 + ((imask >> 6) & 3);
4388                                 v1 = values [ins->sreg1];
4389                                 v2 = LLVMGetUndef (LLVMTypeOf (v1));
4390                                 break;
4391                         case OP_PSHUFLED:
4392                                 mask_size = 4;
4393                                 mask [0] = ((imask >> 0) & 3);
4394                                 mask [1] = ((imask >> 2) & 3);
4395                                 mask [2] = ((imask >> 4) & 3);
4396                                 mask [3] = ((imask >> 6) & 3);
4397                                 v1 = values [ins->sreg1];
4398                                 v2 = LLVMGetUndef (LLVMTypeOf (v1));
4399                                 break;
4400                         default:
4401                                 g_assert_not_reached ();
4402                         }
4403                         for (i = 0; i < mask_size; ++i)
4404                                 mask_values [i] = LLVMConstInt (LLVMInt32Type (), mask [i], FALSE);
4405
4406                         values [ins->dreg] =
4407                                 LLVMBuildShuffleVector (builder, v1, v2,
4408                                                                                 LLVMConstVector (mask_values, mask_size), dname);
4409                         break;
4410                 }
4411
4412                 case OP_UNPACK_LOWB:
4413                 case OP_UNPACK_LOWW:
4414                 case OP_UNPACK_LOWD:
4415                 case OP_UNPACK_LOWQ:
4416                 case OP_UNPACK_LOWPS:
4417                 case OP_UNPACK_LOWPD:
4418                 case OP_UNPACK_HIGHB:
4419                 case OP_UNPACK_HIGHW:
4420                 case OP_UNPACK_HIGHD:
4421                 case OP_UNPACK_HIGHQ:
4422                 case OP_UNPACK_HIGHPS:
4423                 case OP_UNPACK_HIGHPD: {
4424                         int mask [16];
4425                         LLVMValueRef mask_values [16];
4426                         int i, mask_size = 0;
4427                         gboolean low = FALSE;
4428
4429                         switch (ins->opcode) {
4430                         case OP_UNPACK_LOWB:
4431                                 mask_size = 16;
4432                                 low = TRUE;
4433                                 break;
4434                         case OP_UNPACK_LOWW:
4435                                 mask_size = 8;
4436                                 low = TRUE;
4437                                 break;
4438                         case OP_UNPACK_LOWD:
4439                         case OP_UNPACK_LOWPS:
4440                                 mask_size = 4;
4441                                 low = TRUE;
4442                                 break;
4443                         case OP_UNPACK_LOWQ:
4444                         case OP_UNPACK_LOWPD:
4445                                 mask_size = 2;
4446                                 low = TRUE;
4447                                 break;
4448                         case OP_UNPACK_HIGHB:
4449                                 mask_size = 16;
4450                                 break;
4451                         case OP_UNPACK_HIGHW:
4452                                 mask_size = 8;
4453                                 break;
4454                         case OP_UNPACK_HIGHD:
4455                         case OP_UNPACK_HIGHPS:
4456                                 mask_size = 4;
4457                                 break;
4458                         case OP_UNPACK_HIGHQ:
4459                         case OP_UNPACK_HIGHPD:
4460                                 mask_size = 2;
4461                                 break;
4462                         default:
4463                                 g_assert_not_reached ();
4464                         }
4465
4466                         if (low) {
4467                                 for (i = 0; i < (mask_size / 2); ++i) {
4468                                         mask [(i * 2)] = i;
4469                                         mask [(i * 2) + 1] = mask_size + i;
4470                                 }
4471                         } else {
4472                                 for (i = 0; i < (mask_size / 2); ++i) {
4473                                         mask [(i * 2)] = (mask_size / 2) + i;
4474                                         mask [(i * 2) + 1] = mask_size + (mask_size / 2) + i;
4475                                 }
4476                         }
4477
4478                         for (i = 0; i < mask_size; ++i)
4479                                 mask_values [i] = LLVMConstInt (LLVMInt32Type (), mask [i], FALSE);
4480                         
4481                         values [ins->dreg] =
4482                                 LLVMBuildShuffleVector (builder, values [ins->sreg1], values [ins->sreg2],
4483                                                                                 LLVMConstVector (mask_values, mask_size), dname);
4484                         break;
4485                 }
4486
4487                 case OP_DUPPD: {
4488                         LLVMTypeRef t = simd_op_to_llvm_type (ins->opcode);
4489                         LLVMValueRef v, val;
4490
4491                         v = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
4492                         val = LLVMConstNull (t);
4493                         val = LLVMBuildInsertElement (builder, val, v, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
4494                         val = LLVMBuildInsertElement (builder, val, v, LLVMConstInt (LLVMInt32Type (), 1, FALSE), dname);
4495
4496                         values [ins->dreg] = val;
4497                         break;
4498                 }
4499                 case OP_DUPPS_LOW:
4500                 case OP_DUPPS_HIGH: {
4501                         LLVMTypeRef t = simd_op_to_llvm_type (ins->opcode);
4502                         LLVMValueRef v1, v2, val;
4503                         
4504
4505                         if (ins->opcode == OP_DUPPS_LOW) {
4506                                 v1 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
4507                                 v2 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 2, FALSE), "");
4508                         } else {
4509                                 v1 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 1, FALSE), "");
4510                                 v2 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 3, FALSE), "");
4511                         }
4512                         val = LLVMConstNull (t);
4513                         val = LLVMBuildInsertElement (builder, val, v1, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
4514                         val = LLVMBuildInsertElement (builder, val, v1, LLVMConstInt (LLVMInt32Type (), 1, FALSE), "");
4515                         val = LLVMBuildInsertElement (builder, val, v2, LLVMConstInt (LLVMInt32Type (), 2, FALSE), "");
4516                         val = LLVMBuildInsertElement (builder, val, v2, LLVMConstInt (LLVMInt32Type (), 3, FALSE), "");
4517                         
4518                         values [ins->dreg] = val;
4519                         break;
4520                 }
4521
4522 #endif /* SIMD */
4523
4524                 case OP_DUMMY_USE:
4525                         break;
4526
4527                         /*
4528                          * EXCEPTION HANDLING
4529                          */
4530                 case OP_IMPLICIT_EXCEPTION:
4531                         /* This marks a place where an implicit exception can happen */
4532                         if (bb->region != -1)
4533                                 LLVM_FAILURE (ctx, "implicit-exception");
4534                         break;
4535                 case OP_THROW:
4536                 case OP_RETHROW: {
4537                         MonoMethodSignature *throw_sig;
4538                         LLVMValueRef callee, arg;
4539                         gboolean rethrow = (ins->opcode == OP_RETHROW);
4540                         const char *icall_name;
4541                                 
4542                         callee = rethrow ? ctx->lmodule->rethrow : ctx->lmodule->throw;
4543                         icall_name = rethrow ? "mono_arch_rethrow_exception" : "mono_arch_throw_exception";
4544
4545                         if (!callee) {
4546                                 throw_sig = mono_metadata_signature_alloc (mono_get_corlib (), 1);
4547                                 throw_sig->ret = &mono_get_void_class ()->byval_arg;
4548                                 throw_sig->params [0] = &mono_get_object_class ()->byval_arg;
4549                                 if (cfg->compile_aot) {
4550                                         callee = get_plt_entry (ctx, sig_to_llvm_sig (ctx, throw_sig), MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
4551                                 } else {
4552                                         callee = LLVMAddFunction (module, icall_name, sig_to_llvm_sig (ctx, throw_sig));
4553
4554 #ifdef TARGET_X86
4555                                         /* 
4556                                          * LLVM doesn't push the exception argument, so we need a different
4557                                          * trampoline.
4558                                          */
4559                                         LLVMAddGlobalMapping (ctx->lmodule->ee, callee, resolve_patch (cfg, MONO_PATCH_INFO_INTERNAL_METHOD, rethrow ? "llvm_rethrow_exception_trampoline" : "llvm_throw_exception_trampoline"));
4560 #else
4561                                         LLVMAddGlobalMapping (ctx->lmodule->ee, callee, resolve_patch (cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
4562 #endif
4563                                 }
4564
4565                                 mono_memory_barrier ();
4566                                 if (rethrow)
4567                                         ctx->lmodule->rethrow = callee;
4568                                 else
4569                                         ctx->lmodule->throw = callee;
4570                         }
4571                         arg = convert (ctx, lhs, type_to_llvm_type (ctx, &mono_get_object_class ()->byval_arg));
4572                         emit_call (ctx, bb, &builder, callee, &arg, 1);
4573                         break;
4574                 }
4575                 case OP_CALL_HANDLER: {
4576                         /* 
4577                          * We don't 'call' handlers, but instead simply branch to them.
4578                          * The code generated by ENDFINALLY will branch back to us.
4579                          */
4580                         LLVMBasicBlockRef noex_bb;
4581                         GSList *bb_list;
4582                         BBInfo *info = &bblocks [ins->inst_target_bb->block_num];
4583
4584                         bb_list = info->call_handler_return_bbs;
4585
4586                         /* 
4587                          * Set the indicator variable for the finally clause.
4588                          */
4589                         lhs = info->finally_ind;
4590                         g_assert (lhs);
4591                         LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), g_slist_length (bb_list) + 1, FALSE), lhs);
4592                                 
4593                         /* Branch to the finally clause */
4594                         LLVMBuildBr (builder, info->call_handler_target_bb);
4595
4596                         noex_bb = gen_bb (ctx, "CALL_HANDLER_CONT_BB");
4597                         info->call_handler_return_bbs = g_slist_append_mempool (cfg->mempool, info->call_handler_return_bbs, noex_bb);
4598
4599                         builder = ctx->builder = create_builder (ctx);
4600                         LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
4601
4602                         bblocks [bb->block_num].end_bblock = noex_bb;
4603                         break;
4604                 }
4605                 case OP_START_HANDLER: {
4606                         break;
4607                 }
4608                 case OP_ENDFINALLY: {
4609                         LLVMBasicBlockRef resume_bb;
4610                         MonoBasicBlock *handler_bb;
4611                         LLVMValueRef val, switch_ins, callee;
4612                         GSList *bb_list;
4613                         BBInfo *info;
4614
4615                         handler_bb = g_hash_table_lookup (ctx->region_to_handler, GUINT_TO_POINTER (mono_get_block_region_notry (cfg, bb->region)));
4616                         g_assert (handler_bb);
4617                         info = &bblocks [handler_bb->block_num];
4618                         lhs = info->finally_ind;
4619                         g_assert (lhs);
4620
4621                         bb_list = info->call_handler_return_bbs;
4622
4623                         resume_bb = gen_bb (ctx, "ENDFINALLY_RESUME_BB");
4624
4625                         /* Load the finally variable */
4626                         val = LLVMBuildLoad (builder, lhs, "");
4627
4628                         /* Reset the variable */
4629                         LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), lhs);
4630
4631                         /* Branch to either resume_bb, or to the bblocks in bb_list */
4632                         switch_ins = LLVMBuildSwitch (builder, val, resume_bb, g_slist_length (bb_list));
4633                         /* 
4634                          * The other targets are added at the end to handle OP_CALL_HANDLER
4635                          * opcodes processed later.
4636                          */
4637                         info->endfinally_switch_ins_list = g_slist_append_mempool (cfg->mempool, info->endfinally_switch_ins_list, switch_ins);
4638
4639                         builder = ctx->builder = create_builder (ctx);
4640                         LLVMPositionBuilderAtEnd (ctx->builder, resume_bb);
4641
4642                         if (ctx->cfg->compile_aot) {
4643                                 callee = get_plt_entry (ctx, LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE), MONO_PATCH_INFO_INTERNAL_METHOD, "llvm_resume_unwind_trampoline");
4644                         } else {
4645                                 callee = LLVMGetNamedFunction (module, "llvm_resume_unwind_trampoline");
4646                         }
4647                         LLVMBuildCall (builder, callee, NULL, 0, "");
4648
4649                         LLVMBuildUnreachable (builder);
4650                         has_terminator = TRUE;
4651                         break;
4652                 }
4653                 default: {
4654                         char reason [128];
4655
4656                         sprintf (reason, "opcode %s", mono_inst_name (ins->opcode));
4657                         LLVM_FAILURE (ctx, reason);
4658                         break;
4659                 }
4660                 }
4661
4662                 /* Convert the value to the type required by phi nodes */
4663                 if (spec [MONO_INST_DEST] != ' ' && !MONO_IS_STORE_MEMBASE (ins) && ctx->vreg_types [ins->dreg]) {
4664                         if (!values [ins->dreg])
4665                                 /* vtypes */
4666                                 values [ins->dreg] = addresses [ins->dreg];
4667                         else
4668                                 values [ins->dreg] = convert (ctx, values [ins->dreg], ctx->vreg_types [ins->dreg]);
4669                 }
4670
4671                 /* Add stores for volatile variables */
4672                 if (spec [MONO_INST_DEST] != ' ' && spec [MONO_INST_DEST] != 'v' && !MONO_IS_STORE_MEMBASE (ins))
4673                         emit_volatile_store (ctx, ins->dreg);
4674         }
4675
4676         if (!has_terminator && bb->next_bb && (bb == cfg->bb_entry || bb->in_count > 0))
4677                 LLVMBuildBr (builder, get_bb (ctx, bb->next_bb));
4678
4679         if (bb == cfg->bb_exit && sig->ret->type == MONO_TYPE_VOID) {
4680                 emit_dbg_loc (ctx, builder, cfg->header->code + cfg->header->code_size - 1);
4681                 LLVMBuildRetVoid (builder);
4682         }
4683
4684         if (bb == cfg->bb_entry)
4685                 ctx->last_alloca = LLVMGetLastInstruction (get_bb (ctx, cfg->bb_entry));
4686
4687         return;
4688
4689  FAILURE:
4690         return;
4691 }
4692
4693 /*
4694  * mono_llvm_check_method_supported:
4695  *
4696  *   Do some quick checks to decide whenever cfg->method can be compiled by LLVM, to avoid
4697  * compiling a method twice.
4698  */
4699 void
4700 mono_llvm_check_method_supported (MonoCompile *cfg)
4701 {
4702         MonoMethodHeader *header = cfg->header;
4703         MonoExceptionClause *clause;
4704         int i;
4705
4706         if (cfg->method->save_lmf) {
4707                 cfg->exception_message = g_strdup ("lmf");
4708                 cfg->disable_llvm = TRUE;
4709         }
4710         if (cfg->disable_llvm)
4711                 return;
4712
4713 #if 1
4714         for (i = 0; i < header->num_clauses; ++i) {
4715                 clause = &header->clauses [i];
4716
4717                 if (i > 0 && clause->try_offset <= header->clauses [i - 1].handler_offset + header->clauses [i - 1].handler_len) {
4718                         /*
4719                          * FIXME: Some tests still fail with nested clauses.
4720                          */
4721                         cfg->exception_message = g_strdup ("nested clauses");
4722                         cfg->disable_llvm = TRUE;
4723                         break;
4724                 }
4725         }
4726         if (cfg->disable_llvm)
4727                 return;
4728 #endif
4729
4730         /* FIXME: */
4731         if (cfg->method->dynamic) {
4732                 cfg->exception_message = g_strdup ("dynamic.");
4733                 cfg->disable_llvm = TRUE;
4734         }
4735         if (cfg->disable_llvm)
4736                 return;
4737 }
4738
4739 /*
4740  * mono_llvm_emit_method:
4741  *
4742  *   Emit LLVM IL from the mono IL, and compile it to native code using LLVM.
4743  */
4744 void
4745 mono_llvm_emit_method (MonoCompile *cfg)
4746 {
4747         EmitContext *ctx;
4748         MonoMethodSignature *sig;
4749         MonoBasicBlock *bb;
4750         LLVMTypeRef method_type;
4751         LLVMValueRef method = NULL;
4752         char *method_name;
4753         LLVMValueRef *values;
4754         int i, max_block_num, bb_index;
4755         gboolean last = FALSE;
4756         GPtrArray *phi_values;
4757         LLVMCallInfo *linfo;
4758         GSList *l;
4759         LLVMModuleRef module;
4760         BBInfo *bblocks;
4761         GPtrArray *bblock_list;
4762         MonoMethodHeader *header;
4763         MonoExceptionClause *clause;
4764         LLVMSigInfo sinfo;
4765         char **names;
4766
4767         /* The code below might acquire the loader lock, so use it for global locking */
4768         mono_loader_lock ();
4769
4770         /* Used to communicate with the callbacks */
4771         mono_native_tls_set_value (current_cfg_tls_id, cfg);
4772
4773         ctx = g_new0 (EmitContext, 1);
4774         ctx->cfg = cfg;
4775         ctx->mempool = cfg->mempool;
4776
4777         /*
4778          * This maps vregs to the LLVM instruction defining them
4779          */
4780         values = g_new0 (LLVMValueRef, cfg->next_vreg);
4781         /*
4782          * This maps vregs for volatile variables to the LLVM instruction defining their
4783          * address.
4784          */
4785         ctx->addresses = g_new0 (LLVMValueRef, cfg->next_vreg);
4786         ctx->vreg_types = g_new0 (LLVMTypeRef, cfg->next_vreg);
4787         ctx->vreg_cli_types = g_new0 (MonoType*, cfg->next_vreg);
4788         phi_values = g_ptr_array_sized_new (256);
4789         /* 
4790          * This signals whenever the vreg was defined by a phi node with no input vars
4791          * (i.e. all its input bblocks end with NOT_REACHABLE).
4792          */
4793         ctx->is_dead = g_new0 (gboolean, cfg->next_vreg);
4794         /* Whenever the bblock is unreachable */
4795         ctx->unreachable = g_new0 (gboolean, cfg->max_block_num);
4796
4797         bblock_list = g_ptr_array_sized_new (256);
4798
4799         ctx->values = values;
4800         ctx->region_to_handler = g_hash_table_new (NULL, NULL);
4801  
4802         if (cfg->compile_aot) {
4803                 ctx->lmodule = &aot_module;
4804                 method_name = mono_aot_get_method_name (cfg);
4805                 cfg->llvm_method_name = g_strdup (method_name);
4806         } else {
4807                 init_jit_module (cfg->domain);
4808                 ctx->lmodule = domain_jit_info (cfg->domain)->llvm_module;
4809                 method_name = mono_method_full_name (cfg->method, TRUE);
4810         }
4811
4812         module = ctx->module = ctx->lmodule->module;
4813
4814         if (cfg->gsharedvt)
4815                 LLVM_FAILURE (ctx, "gsharedvt");
4816
4817 #if 1
4818         {
4819                 static int count = 0;
4820                 count ++;
4821
4822                 if (g_getenv ("LLVM_COUNT")) {
4823                         if (count == atoi (g_getenv ("LLVM_COUNT"))) {
4824                                 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
4825                                 fflush (stdout);
4826                                 last = TRUE;
4827                         }
4828                         if (count > atoi (g_getenv ("LLVM_COUNT")))
4829                                 LLVM_FAILURE (ctx, "");
4830                 }
4831         }
4832 #endif
4833
4834         sig = mono_method_signature (cfg->method);
4835         ctx->sig = sig;
4836
4837         linfo = mono_arch_get_llvm_call_info (cfg, sig);
4838         ctx->linfo = linfo;
4839         CHECK_FAILURE (ctx);
4840
4841         if (cfg->rgctx_var)
4842                 linfo->rgctx_arg = TRUE;
4843         method_type = sig_to_llvm_sig_full (ctx, sig, linfo, &sinfo);
4844         CHECK_FAILURE (ctx);
4845
4846         /* 
4847          * This maps parameter indexes in the original signature to the indexes in
4848          * the LLVM signature.
4849          */
4850         ctx->pindexes = sinfo.pindexes;
4851
4852         method = LLVMAddFunction (module, method_name, method_type);
4853         ctx->lmethod = method;
4854
4855         LLVMSetFunctionCallConv (method, LLVMMono1CallConv);
4856         LLVMSetLinkage (method, LLVMPrivateLinkage);
4857
4858         LLVMAddFunctionAttr (method, LLVMUWTable);
4859
4860         if (cfg->compile_aot) {
4861                 LLVMSetLinkage (method, LLVMInternalLinkage);
4862                 if (ctx->lmodule->external_symbols) {
4863                         LLVMSetLinkage (method, LLVMExternalLinkage);
4864                         LLVMSetVisibility (method, LLVMHiddenVisibility);
4865                 }
4866         } else {
4867                 LLVMSetLinkage (method, LLVMPrivateLinkage);
4868         }
4869
4870         if (cfg->method->save_lmf)
4871                 LLVM_FAILURE (ctx, "lmf");
4872
4873         if (sig->pinvoke && cfg->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE)
4874                 LLVM_FAILURE (ctx, "pinvoke signature");
4875
4876         header = cfg->header;
4877         for (i = 0; i < header->num_clauses; ++i) {
4878                 clause = &header->clauses [i];
4879                 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_NONE)
4880                         LLVM_FAILURE (ctx, "non-finally/catch clause.");
4881         }
4882         if (header->num_clauses || (cfg->method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING))
4883                 /* We can't handle inlined methods with clauses */
4884                 LLVMAddFunctionAttr (method, LLVMNoInlineAttribute);
4885
4886         if (linfo->rgctx_arg) {
4887                 ctx->rgctx_arg = LLVMGetParam (method, sinfo.rgctx_arg_pindex);
4888                 /*
4889                  * We mark the rgctx parameter with the inreg attribute, which is mapped to
4890                  * MONO_ARCH_RGCTX_REG in the Mono calling convention in llvm, i.e.
4891                  * CC_X86_64_Mono in X86CallingConv.td.
4892                  */
4893                 LLVMAddAttribute (ctx->rgctx_arg, LLVMInRegAttribute);
4894                 LLVMSetValueName (ctx->rgctx_arg, "rgctx");
4895         }
4896         if (cfg->vret_addr) {
4897                 values [cfg->vret_addr->dreg] = LLVMGetParam (method, sinfo.vret_arg_pindex);
4898                 LLVMSetValueName (values [cfg->vret_addr->dreg], "vret");
4899         }
4900         if (sig->hasthis) {
4901                 values [cfg->args [0]->dreg] = LLVMGetParam (method, sinfo.this_arg_pindex);
4902                 LLVMSetValueName (values [cfg->args [0]->dreg], "this");
4903         }
4904
4905         names = g_new (char *, sig->param_count);
4906         mono_method_get_param_names (cfg->method, (const char **) names);
4907
4908         for (i = 0; i < sig->param_count; ++i) {
4909                 char *name;
4910
4911                 values [cfg->args [i + sig->hasthis]->dreg] = LLVMGetParam (method, sinfo.pindexes [i]);
4912                 if (names [i] && names [i][0] != '\0')
4913                         name = g_strdup_printf ("arg_%s", names [i]);
4914                 else
4915                         name = g_strdup_printf ("arg_%d", i);
4916                 LLVMSetValueName (values [cfg->args [i + sig->hasthis]->dreg], name);
4917                 g_free (name);
4918                 if (linfo->args [i + sig->hasthis].storage == LLVMArgVtypeByVal)
4919                         LLVMAddAttribute (LLVMGetParam (method, sinfo.pindexes [i]), LLVMByValAttribute);
4920         }
4921         g_free (names);
4922
4923         if (ctx->lmodule->emit_dwarf && cfg->compile_aot && mono_debug_enabled ()) {
4924                 ctx->minfo = mono_debug_lookup_method (cfg->method);
4925                 ctx->dbg_md = emit_dbg_subprogram (ctx, cfg, method, method_name);
4926         }
4927
4928         max_block_num = 0;
4929         for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
4930                 max_block_num = MAX (max_block_num, bb->block_num);
4931         ctx->bblocks = bblocks = g_new0 (BBInfo, max_block_num + 1);
4932
4933         /* Add branches between non-consecutive bblocks */
4934         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4935                 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
4936                         bb->next_bb != bb->last_ins->inst_false_bb) {
4937                         
4938                         MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
4939                         inst->opcode = OP_BR;
4940                         inst->inst_target_bb = bb->last_ins->inst_false_bb;
4941                         mono_bblock_add_inst (bb, inst);
4942                 }
4943         }
4944
4945         /*
4946          * The INDIRECT flag added by OP_LDADDR inhibits optimizations, even if the LDADDR
4947          * was later optimized away, so clear these flags, and add them back for the still
4948          * present OP_LDADDR instructions.
4949          */
4950         for (i = 0; i < cfg->next_vreg; ++i) {
4951                 MonoInst *ins;
4952
4953                 ins = get_vreg_to_inst (cfg, i);
4954                 if (ins && ins != cfg->rgctx_var)
4955                         ins->flags &= ~MONO_INST_INDIRECT;
4956         }
4957
4958         /*
4959          * Make a first pass over the code to precreate PHI nodes/set INDIRECT flags.
4960          */
4961         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4962                 MonoInst *ins;
4963                 LLVMBuilderRef builder;
4964                 char *dname;
4965                 char dname_buf[128];
4966
4967                 builder = create_builder (ctx);
4968
4969                 for (ins = bb->code; ins; ins = ins->next) {
4970                         switch (ins->opcode) {
4971                         case OP_PHI:
4972                         case OP_FPHI:
4973                         case OP_VPHI:
4974                         case OP_XPHI: {
4975                                 LLVMTypeRef phi_type = llvm_type_to_stack_type (cfg, type_to_llvm_type (ctx, &ins->klass->byval_arg));
4976
4977                                 CHECK_FAILURE (ctx);
4978
4979                                 if (ins->opcode == OP_VPHI) {
4980                                         /* Treat valuetype PHI nodes as operating on the address itself */
4981                                         g_assert (ins->klass);
4982                                         phi_type = LLVMPointerType (type_to_llvm_type (ctx, &ins->klass->byval_arg), 0);
4983                                 }
4984
4985                                 /* 
4986                                  * Have to precreate these, as they can be referenced by
4987                                  * earlier instructions.
4988                                  */
4989                                 sprintf (dname_buf, "t%d", ins->dreg);
4990                                 dname = dname_buf;
4991                                 values [ins->dreg] = LLVMBuildPhi (builder, phi_type, dname);
4992
4993                                 if (ins->opcode == OP_VPHI)
4994                                         ctx->addresses [ins->dreg] = values [ins->dreg];
4995
4996                                 g_ptr_array_add (phi_values, values [ins->dreg]);
4997
4998                                 /* 
4999                                  * Set the expected type of the incoming arguments since these have
5000                                  * to have the same type.
5001                                  */
5002                                 for (i = 0; i < ins->inst_phi_args [0]; i++) {
5003                                         int sreg1 = ins->inst_phi_args [i + 1];
5004                                         
5005                                         if (sreg1 != -1)
5006                                                 ctx->vreg_types [sreg1] = phi_type;
5007                                 }
5008                                 break;
5009                                 }
5010                         case OP_LDADDR:
5011                                 ((MonoInst*)ins->inst_p0)->flags |= MONO_INST_INDIRECT;
5012                                 break;
5013                         default:
5014                                 break;
5015                         }
5016                 }
5017         }
5018
5019         /* 
5020          * Create an ordering for bblocks, use the depth first order first, then
5021          * put the exception handling bblocks last.
5022          */
5023         for (bb_index = 0; bb_index < cfg->num_bblocks; ++bb_index) {
5024                 bb = cfg->bblocks [bb_index];
5025                 if (!(bb->region != -1 && !MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))) {
5026                         g_ptr_array_add (bblock_list, bb);
5027                         bblocks [bb->block_num].added = TRUE;
5028                 }
5029         }
5030
5031         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5032                 if (!bblocks [bb->block_num].added)
5033                         g_ptr_array_add (bblock_list, bb);
5034         }
5035
5036         /*
5037          * Second pass: generate code.
5038          */
5039         for (bb_index = 0; bb_index < bblock_list->len; ++bb_index) {
5040                 bb = g_ptr_array_index (bblock_list, bb_index);
5041
5042                 if (!(bb == cfg->bb_entry || bb->in_count > 0))
5043                         continue;
5044
5045                 process_bb (ctx, bb);
5046                 CHECK_FAILURE (ctx);
5047         }
5048
5049         /* Add incoming phi values */
5050         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5051                 GSList *l, *ins_list;
5052
5053                 ins_list = bblocks [bb->block_num].phi_nodes;
5054
5055                 for (l = ins_list; l; l = l->next) {
5056                         PhiNode *node = l->data;
5057                         MonoInst *phi = node->phi;
5058                         int sreg1 = node->sreg;
5059                         LLVMBasicBlockRef in_bb;
5060
5061                         if (sreg1 == -1)
5062                                 continue;
5063
5064                         in_bb = get_end_bb (ctx, node->in_bb);
5065
5066                         if (ctx->unreachable [node->in_bb->block_num])
5067                                 continue;
5068
5069                         if (!values [sreg1])
5070                                 /* Can happen with values in EH clauses */
5071                                 LLVM_FAILURE (ctx, "incoming phi sreg1");
5072
5073                         if (phi->opcode == OP_VPHI) {
5074                                 g_assert (LLVMTypeOf (ctx->addresses [sreg1]) == LLVMTypeOf (values [phi->dreg]));
5075                                 LLVMAddIncoming (values [phi->dreg], &ctx->addresses [sreg1], &in_bb, 1);
5076                         } else {
5077                                 if (LLVMTypeOf (values [sreg1]) != LLVMTypeOf (values [phi->dreg]))
5078                                         // FIXME:
5079                                         LLVM_FAILURE (ctx, "incoming phi arg type mismatch");
5080                                 g_assert (LLVMTypeOf (values [sreg1]) == LLVMTypeOf (values [phi->dreg]));
5081                                 LLVMAddIncoming (values [phi->dreg], &values [sreg1], &in_bb, 1);
5082                         }
5083                 }
5084         }
5085
5086         /* Create the SWITCH statements for ENDFINALLY instructions */
5087         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5088                 BBInfo *info = &bblocks [bb->block_num];
5089                 GSList *l;
5090                 for (l = info->endfinally_switch_ins_list; l; l = l->next) {
5091                         LLVMValueRef switch_ins = l->data;
5092                         GSList *bb_list = info->call_handler_return_bbs;
5093
5094                         for (i = 0; i < g_slist_length (bb_list); ++i)
5095                                 LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), i + 1, FALSE), g_slist_nth (bb_list, i)->data);
5096                 }
5097         }
5098
5099         if (cfg->verbose_level > 1)
5100                 mono_llvm_dump_value (method);
5101
5102         if (cfg->compile_aot)
5103                 mark_as_used (ctx->lmodule, method);
5104
5105         if (cfg->compile_aot) {
5106                 LLVMValueRef md_args [16];
5107                 LLVMValueRef md_node;
5108                 int method_index;
5109
5110                 method_index = mono_aot_get_method_index (cfg->orig_method);
5111                 md_args [0] = LLVMMDString (method_name, strlen (method_name));
5112                 md_args [1] = LLVMConstInt (LLVMInt32Type (), method_index, FALSE);
5113                 md_node = LLVMMDNode (md_args, 2);
5114                 LLVMAddNamedMetadataOperand (module, "mono.function_indexes", md_node);
5115                 //LLVMSetMetadata (method, md_kind, LLVMMDNode (&md_arg, 1));
5116         }
5117
5118         if (cfg->compile_aot) {
5119                 /* Don't generate native code, keep the LLVM IR */
5120                 if (cfg->compile_aot && cfg->verbose_level)
5121                         printf ("%s emitted as %s\n", mono_method_full_name (cfg->method, TRUE), method_name);
5122
5123                 //LLVMVerifyFunction(method, 0);
5124         } else {
5125                 mono_llvm_optimize_method (ctx->lmodule->mono_ee, method);
5126
5127                 if (cfg->verbose_level > 1)
5128                         mono_llvm_dump_value (method);
5129
5130                 cfg->native_code = LLVMGetPointerToGlobal (ctx->lmodule->ee, method);
5131
5132                 /* Set by emit_cb */
5133                 g_assert (cfg->code_len);
5134
5135                 /* FIXME: Free the LLVM IL for the function */
5136         }
5137
5138         if (ctx->lmodule->method_to_lmethod)
5139                 g_hash_table_insert (ctx->lmodule->method_to_lmethod, cfg->method, method);
5140
5141         goto CLEANUP;
5142
5143  FAILURE:
5144
5145         if (method) {
5146                 /* Need to add unused phi nodes as they can be referenced by other values */
5147                 LLVMBasicBlockRef phi_bb = LLVMAppendBasicBlock (method, "PHI_BB");
5148                 LLVMBuilderRef builder;
5149
5150                 builder = create_builder (ctx);
5151                 LLVMPositionBuilderAtEnd (builder, phi_bb);
5152
5153                 for (i = 0; i < phi_values->len; ++i) {
5154                         LLVMValueRef v = g_ptr_array_index (phi_values, i);
5155                         if (LLVMGetInstructionParent (v) == NULL)
5156                                 LLVMInsertIntoBuilder (builder, v);
5157                 }
5158                 
5159                 LLVMDeleteFunction (method);
5160         }
5161
5162  CLEANUP:
5163         g_free (values);
5164         g_free (ctx->addresses);
5165         g_free (ctx->vreg_types);
5166         g_free (ctx->vreg_cli_types);
5167         g_free (ctx->pindexes);
5168         g_free (ctx->is_dead);
5169         g_free (ctx->unreachable);
5170         g_ptr_array_free (phi_values, TRUE);
5171         g_free (ctx->bblocks);
5172         g_hash_table_destroy (ctx->region_to_handler);
5173         g_free (method_name);
5174         g_ptr_array_free (bblock_list, TRUE);
5175
5176         for (l = ctx->builders; l; l = l->next) {
5177                 LLVMBuilderRef builder = l->data;
5178                 LLVMDisposeBuilder (builder);
5179         }
5180
5181         g_free (ctx);
5182
5183         mono_native_tls_set_value (current_cfg_tls_id, NULL);
5184
5185         mono_loader_unlock ();
5186 }
5187
5188 /*
5189  * mono_llvm_emit_call:
5190  *
5191  *   Same as mono_arch_emit_call () for LLVM.
5192  */
5193 void
5194 mono_llvm_emit_call (MonoCompile *cfg, MonoCallInst *call)
5195 {
5196         MonoInst *in;
5197         MonoMethodSignature *sig;
5198         int i, n, stack_size;
5199         LLVMArgInfo *ainfo;
5200
5201         stack_size = 0;
5202
5203         sig = call->signature;
5204         n = sig->param_count + sig->hasthis;
5205
5206         call->cinfo = mono_arch_get_llvm_call_info (cfg, sig);
5207
5208         if (cfg->disable_llvm)
5209                 return;
5210
5211         if (sig->call_convention == MONO_CALL_VARARG) {
5212                 cfg->exception_message = g_strdup ("varargs");
5213                 cfg->disable_llvm = TRUE;
5214         }
5215
5216         for (i = 0; i < n; ++i) {
5217                 MonoInst *ins;
5218
5219                 ainfo = call->cinfo->args + i;
5220
5221                 in = call->args [i];
5222                         
5223                 /* Simply remember the arguments */
5224                 switch (ainfo->storage) {
5225                 case LLVMArgInIReg:
5226                 case LLVMArgInFPReg: {
5227                         MonoType *t = (sig->hasthis && i == 0) ? &mono_get_intptr_class ()->byval_arg : sig->params [i - sig->hasthis];
5228                         int opcode;
5229
5230                         opcode = mono_type_to_regmove (cfg, t);
5231                         if (opcode == OP_FMOVE) {
5232                                 MONO_INST_NEW (cfg, ins, OP_FMOVE);
5233                                 ins->dreg = mono_alloc_freg (cfg);
5234                         } else if (opcode == OP_LMOVE) {
5235                                 MONO_INST_NEW (cfg, ins, OP_LMOVE);
5236                                 ins->dreg = mono_alloc_lreg (cfg);
5237                         } else {
5238                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
5239                                 ins->dreg = mono_alloc_ireg (cfg);
5240                         }
5241                         ins->sreg1 = in->dreg;
5242                         break;
5243                 }
5244                 case LLVMArgVtypeByVal:
5245                 case LLVMArgVtypeInReg:
5246                 case LLVMArgAsIArgs:
5247                 case LLVMArgAsFpArgs:
5248                         MONO_INST_NEW (cfg, ins, OP_LLVM_OUTARG_VT);
5249                         ins->dreg = mono_alloc_ireg (cfg);
5250                         ins->sreg1 = in->dreg;
5251                         ins->klass = mono_class_from_mono_type (sig->params [i - sig->hasthis]);
5252                         break;
5253                 default:
5254                         call->cinfo = mono_arch_get_llvm_call_info (cfg, sig);
5255                         cfg->exception_message = g_strdup ("ainfo->storage");
5256                         cfg->disable_llvm = TRUE;
5257                         return;
5258                 }
5259
5260                 if (!cfg->disable_llvm) {
5261                         MONO_ADD_INS (cfg->cbb, ins);
5262                         mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, 0, FALSE);
5263                 }
5264         }
5265 }
5266
5267 static unsigned char*
5268 alloc_cb (LLVMValueRef function, int size)
5269 {
5270         MonoCompile *cfg;
5271
5272         cfg = mono_native_tls_get_value (current_cfg_tls_id);
5273
5274         if (cfg) {
5275                 // FIXME: dynamic
5276                 return mono_domain_code_reserve (cfg->domain, size);
5277         } else {
5278                 return mono_domain_code_reserve (mono_domain_get (), size);
5279         }
5280 }
5281
5282 static void
5283 emitted_cb (LLVMValueRef function, void *start, void *end)
5284 {
5285         MonoCompile *cfg;
5286
5287         cfg = mono_native_tls_get_value (current_cfg_tls_id);
5288         g_assert (cfg);
5289         cfg->code_len = (guint8*)end - (guint8*)start;
5290 }
5291
5292 static void
5293 exception_cb (void *data)
5294 {
5295         MonoCompile *cfg;
5296         MonoJitExceptionInfo *ei;
5297         guint32 ei_len, i, j, nested_len, nindex;
5298         gpointer *type_info;
5299         int this_reg, this_offset;
5300
5301         cfg = mono_native_tls_get_value (current_cfg_tls_id);
5302         g_assert (cfg);
5303
5304         /*
5305          * data points to a DWARF FDE structure, convert it to our unwind format and
5306          * save it.
5307          * An alternative would be to save it directly, and modify our unwinder to work
5308          * with it.
5309          */
5310         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);
5311         if (cfg->verbose_level > 1)
5312                 mono_print_unwind_info (cfg->encoded_unwind_ops, cfg->encoded_unwind_ops_len);
5313
5314         /* Count nested clauses */
5315         nested_len = 0;
5316         for (i = 0; i < ei_len; ++i) {
5317                 for (j = 0; j < ei_len; ++j) {
5318                         gint32 cindex1 = *(gint32*)type_info [i];
5319                         MonoExceptionClause *clause1 = &cfg->header->clauses [cindex1];
5320                         gint32 cindex2 = *(gint32*)type_info [j];
5321                         MonoExceptionClause *clause2 = &cfg->header->clauses [cindex2];
5322
5323                         if (cindex1 != cindex2 && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset) {
5324                                 nested_len ++;
5325                         }
5326                 }
5327         }
5328
5329         cfg->llvm_ex_info = mono_mempool_alloc0 (cfg->mempool, (ei_len + nested_len) * sizeof (MonoJitExceptionInfo));
5330         cfg->llvm_ex_info_len = ei_len + nested_len;
5331         memcpy (cfg->llvm_ex_info, ei, ei_len * sizeof (MonoJitExceptionInfo));
5332         /* Fill the rest of the information from the type info */
5333         for (i = 0; i < ei_len; ++i) {
5334                 gint32 clause_index = *(gint32*)type_info [i];
5335                 MonoExceptionClause *clause = &cfg->header->clauses [clause_index];
5336
5337                 cfg->llvm_ex_info [i].flags = clause->flags;
5338                 cfg->llvm_ex_info [i].data.catch_class = clause->data.catch_class;
5339         }
5340
5341         /*
5342          * For nested clauses, the LLVM produced exception info associates the try interval with
5343          * the innermost handler, while mono expects it to be associated with all nesting clauses.
5344          */
5345         /* FIXME: These should be order with the normal clauses */
5346         nindex = ei_len;
5347         for (i = 0; i < ei_len; ++i) {
5348                 for (j = 0; j < ei_len; ++j) {
5349                         gint32 cindex1 = *(gint32*)type_info [i];
5350                         MonoExceptionClause *clause1 = &cfg->header->clauses [cindex1];
5351                         gint32 cindex2 = *(gint32*)type_info [j];
5352                         MonoExceptionClause *clause2 = &cfg->header->clauses [cindex2];
5353
5354                         if (cindex1 != cindex2 && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset) {
5355                                 /* 
5356                                  * The try interval comes from the nested clause, everything else from the
5357                                  * nesting clause.
5358                                  */
5359                                 memcpy (&cfg->llvm_ex_info [nindex], &cfg->llvm_ex_info [j], sizeof (MonoJitExceptionInfo));
5360                                 cfg->llvm_ex_info [nindex].try_start = cfg->llvm_ex_info [i].try_start;
5361                                 cfg->llvm_ex_info [nindex].try_end = cfg->llvm_ex_info [i].try_end;
5362                                 nindex ++;
5363                         }
5364                 }
5365         }
5366         g_assert (nindex == ei_len + nested_len);
5367         cfg->llvm_this_reg = this_reg;
5368         cfg->llvm_this_offset = this_offset;
5369
5370         /* type_info [i] is cfg mempool allocated, no need to free it */
5371
5372         g_free (ei);
5373         g_free (type_info);
5374 }
5375
5376 static char*
5377 dlsym_cb (const char *name, void **symbol)
5378 {
5379         MonoDl *current;
5380         char *err;
5381
5382         err = NULL;
5383         if (!strcmp (name, "__bzero")) {
5384                 *symbol = (void*)bzero;
5385         } else {
5386                 current = mono_dl_open (NULL, 0, NULL);
5387                 g_assert (current);
5388
5389                 err = mono_dl_symbol (current, name, symbol);
5390
5391                 mono_dl_close (current);
5392         }
5393 #ifdef MONO_ARCH_HAVE_CREATE_LLVM_NATIVE_THUNK
5394         *symbol = (char*)mono_arch_create_llvm_native_thunk (mono_domain_get (), (guint8*)(*symbol));
5395 #endif
5396         return err;
5397 }
5398
5399 static inline void
5400 AddFunc (LLVMModuleRef module, const char *name, LLVMTypeRef ret_type, LLVMTypeRef *param_types, int nparams)
5401 {
5402         LLVMAddFunction (module, name, LLVMFunctionType (ret_type, param_types, nparams, FALSE));
5403 }
5404
5405 static inline void
5406 AddFunc2 (LLVMModuleRef module, const char *name, LLVMTypeRef ret_type, LLVMTypeRef param_type1, LLVMTypeRef param_type2)
5407 {
5408         LLVMTypeRef param_types [4];
5409
5410         param_types [0] = param_type1;
5411         param_types [1] = param_type2;
5412
5413         AddFunc (module, name, ret_type, param_types, 2);
5414 }
5415
5416 static void
5417 add_intrinsics (LLVMModuleRef module)
5418 {
5419         /* Emit declarations of instrinsics */
5420         /*
5421          * It would be nicer to emit only the intrinsics actually used, but LLVM's Module
5422          * type doesn't seem to do any locking.
5423          */
5424         {
5425                 LLVMTypeRef memset_params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMInt8Type (), LLVMInt32Type (), LLVMInt32Type (), LLVMInt1Type () };
5426
5427                 memset_param_count = 5;
5428                 memset_func_name = "llvm.memset.p0i8.i32";
5429
5430                 LLVMAddFunction (module, memset_func_name, LLVMFunctionType (LLVMVoidType (), memset_params, memset_param_count, FALSE));
5431         }
5432
5433         {
5434                 LLVMTypeRef memcpy_params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMPointerType (LLVMInt8Type (), 0), LLVMInt32Type (), LLVMInt32Type (), LLVMInt1Type () };
5435
5436                 memcpy_param_count = 5;
5437                 memcpy_func_name = "llvm.memcpy.p0i8.p0i8.i32";
5438
5439                 LLVMAddFunction (module, memcpy_func_name, LLVMFunctionType (LLVMVoidType (), memcpy_params, memcpy_param_count, FALSE));
5440         }
5441
5442         {
5443                 LLVMTypeRef params [] = { LLVMDoubleType () };
5444
5445                 LLVMAddFunction (module, "llvm.sin.f64", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
5446                 LLVMAddFunction (module, "llvm.cos.f64", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
5447                 LLVMAddFunction (module, "llvm.sqrt.f64", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
5448
5449                 /* This isn't an intrinsic, instead llvm seems to special case it by name */
5450                 LLVMAddFunction (module, "fabs", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
5451         }
5452
5453         {
5454                 LLVMTypeRef ovf_res_i32 [] = { LLVMInt32Type (), LLVMInt1Type () };
5455                 LLVMTypeRef ovf_params_i32 [] = { LLVMInt32Type (), LLVMInt32Type () };
5456
5457                 LLVMAddFunction (module, "llvm.sadd.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
5458                 LLVMAddFunction (module, "llvm.uadd.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
5459                 LLVMAddFunction (module, "llvm.ssub.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
5460                 LLVMAddFunction (module, "llvm.usub.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
5461                 LLVMAddFunction (module, "llvm.smul.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
5462                 LLVMAddFunction (module, "llvm.umul.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
5463         }
5464
5465         {
5466                 LLVMTypeRef ovf_res_i64 [] = { LLVMInt64Type (), LLVMInt1Type () };
5467                 LLVMTypeRef ovf_params_i64 [] = { LLVMInt64Type (), LLVMInt64Type () };
5468
5469                 LLVMAddFunction (module, "llvm.sadd.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
5470                 LLVMAddFunction (module, "llvm.uadd.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
5471                 LLVMAddFunction (module, "llvm.ssub.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
5472                 LLVMAddFunction (module, "llvm.usub.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
5473                 LLVMAddFunction (module, "llvm.smul.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
5474                 LLVMAddFunction (module, "llvm.umul.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
5475         }
5476
5477         /* EH intrinsics */
5478         {
5479                 LLVMTypeRef arg_types [2];
5480                 LLVMTypeRef ret_type;
5481
5482                 arg_types [0] = LLVMPointerType (LLVMInt8Type (), 0);
5483                 arg_types [1] = LLVMPointerType (LLVMInt8Type (), 0);
5484                 ret_type = LLVMInt32Type ();
5485
5486                 LLVMAddFunction (module, "mono_personality", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
5487
5488                 LLVMAddFunction (module, "llvm_resume_unwind_trampoline", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
5489         }
5490
5491         /* SSE intrinsics */
5492 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5493         {
5494                 LLVMTypeRef ret_type, arg_types [16];
5495
5496                 /* Binary ops */
5497                 ret_type = type_to_simd_type (MONO_TYPE_I4);
5498                 arg_types [0] = ret_type;
5499                 arg_types [1] = ret_type;
5500                 AddFunc (module, "llvm.x86.sse41.pminud", ret_type, arg_types, 2);
5501                 AddFunc (module, "llvm.x86.sse41.pmaxud", ret_type, arg_types, 2);
5502
5503                 ret_type = type_to_simd_type (MONO_TYPE_I2);
5504                 arg_types [0] = ret_type;
5505                 arg_types [1] = ret_type;
5506                 AddFunc (module, "llvm.x86.sse41.pminuw", ret_type, arg_types, 2);
5507                 AddFunc (module, "llvm.x86.sse2.pmins.w", ret_type, arg_types, 2);
5508                 AddFunc (module, "llvm.x86.sse41.pmaxuw", ret_type, arg_types, 2);
5509                 AddFunc (module, "llvm.x86.sse2.padds.w", ret_type, arg_types, 2);
5510                 AddFunc (module, "llvm.x86.sse2.psubs.w", ret_type, arg_types, 2);
5511                 AddFunc (module, "llvm.x86.sse2.paddus.w", ret_type, arg_types, 2);
5512                 AddFunc (module, "llvm.x86.sse2.psubus.w", ret_type, arg_types, 2);
5513                 AddFunc (module, "llvm.x86.sse2.pavg.w", ret_type, arg_types, 2);
5514                 AddFunc (module, "llvm.x86.sse2.pmulh.w", ret_type, arg_types, 2);
5515                 AddFunc (module, "llvm.x86.sse2.pmulhu.w", ret_type, arg_types, 2);
5516
5517                 ret_type = type_to_simd_type (MONO_TYPE_I1);
5518                 arg_types [0] = ret_type;
5519                 arg_types [1] = ret_type;
5520                 AddFunc (module, "llvm.x86.sse2.pminu.b", ret_type, arg_types, 2);
5521                 AddFunc (module, "llvm.x86.sse2.pmaxu.b", ret_type, arg_types, 2);
5522                 AddFunc (module, "llvm.x86.sse2.padds.b", ret_type, arg_types, 2);
5523                 AddFunc (module, "llvm.x86.sse2.psubs.b", ret_type, arg_types, 2);
5524                 AddFunc (module, "llvm.x86.sse2.paddus.b", ret_type, arg_types, 2);
5525                 AddFunc (module, "llvm.x86.sse2.psubus.b", ret_type, arg_types, 2);
5526                 AddFunc (module, "llvm.x86.sse2.pavg.b", ret_type, arg_types, 2);
5527
5528                 ret_type = type_to_simd_type (MONO_TYPE_R8);
5529                 arg_types [0] = ret_type;
5530                 arg_types [1] = ret_type;
5531                 AddFunc (module, "llvm.x86.sse2.min.pd", ret_type, arg_types, 2);
5532                 AddFunc (module, "llvm.x86.sse2.max.pd", ret_type, arg_types, 2);
5533                 AddFunc (module, "llvm.x86.sse3.hadd.pd", ret_type, arg_types, 2);
5534                 AddFunc (module, "llvm.x86.sse3.hsub.pd", ret_type, arg_types, 2);
5535                 AddFunc (module, "llvm.x86.sse3.addsub.pd", ret_type, arg_types, 2);
5536
5537                 ret_type = type_to_simd_type (MONO_TYPE_R4);
5538                 arg_types [0] = ret_type;
5539                 arg_types [1] = ret_type;
5540                 AddFunc (module, "llvm.x86.sse.min.ps", ret_type, arg_types, 2);
5541                 AddFunc (module, "llvm.x86.sse.max.ps", ret_type, arg_types, 2);
5542                 AddFunc (module, "llvm.x86.sse3.hadd.ps", ret_type, arg_types, 2);
5543                 AddFunc (module, "llvm.x86.sse3.hsub.ps", ret_type, arg_types, 2);
5544                 AddFunc (module, "llvm.x86.sse3.addsub.ps", ret_type, arg_types, 2);
5545
5546                 /* pack */
5547                 ret_type = type_to_simd_type (MONO_TYPE_I1);
5548                 arg_types [0] = type_to_simd_type (MONO_TYPE_I2);
5549                 arg_types [1] = type_to_simd_type (MONO_TYPE_I2);
5550                 AddFunc (module, "llvm.x86.sse2.packsswb.128", ret_type, arg_types, 2);
5551                 AddFunc (module, "llvm.x86.sse2.packuswb.128", ret_type, arg_types, 2);
5552                 ret_type = type_to_simd_type (MONO_TYPE_I2);
5553                 arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
5554                 arg_types [1] = type_to_simd_type (MONO_TYPE_I4);
5555                 AddFunc (module, "llvm.x86.sse2.packssdw.128", ret_type, arg_types, 2);
5556                 AddFunc (module, "llvm.x86.sse41.packusdw", ret_type, arg_types, 2);
5557
5558                 /* cmp pd/ps */
5559                 ret_type = type_to_simd_type (MONO_TYPE_R8);
5560                 arg_types [0] = ret_type;
5561                 arg_types [1] = ret_type;
5562                 arg_types [2] = LLVMInt8Type ();
5563                 AddFunc (module, "llvm.x86.sse2.cmp.pd", ret_type, arg_types, 3);
5564                 ret_type = type_to_simd_type (MONO_TYPE_R4);
5565                 arg_types [0] = ret_type;
5566                 arg_types [1] = ret_type;
5567                 arg_types [2] = LLVMInt8Type ();
5568                 AddFunc (module, "llvm.x86.sse.cmp.ps", ret_type, arg_types, 3);
5569
5570                 /* Conversion ops */
5571                 ret_type = type_to_simd_type (MONO_TYPE_R8);
5572                 arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
5573                 AddFunc (module, "llvm.x86.sse2.cvtdq2pd", ret_type, arg_types, 1);
5574                 ret_type = type_to_simd_type (MONO_TYPE_R4);
5575                 arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
5576                 AddFunc (module, "llvm.x86.sse2.cvtdq2ps", ret_type, arg_types, 1);
5577                 ret_type = type_to_simd_type (MONO_TYPE_I4);
5578                 arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
5579                 AddFunc (module, "llvm.x86.sse2.cvtpd2dq", ret_type, arg_types, 1);
5580                 ret_type = type_to_simd_type (MONO_TYPE_I4);
5581                 arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
5582                 AddFunc (module, "llvm.x86.sse2.cvtps2dq", ret_type, arg_types, 1);
5583                 ret_type = type_to_simd_type (MONO_TYPE_R4);
5584                 arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
5585                 AddFunc (module, "llvm.x86.sse2.cvtpd2ps", ret_type, arg_types, 1);
5586                 ret_type = type_to_simd_type (MONO_TYPE_R8);
5587                 arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
5588                 AddFunc (module, "llvm.x86.sse2.cvtps2pd", ret_type, arg_types, 1);
5589
5590                 ret_type = type_to_simd_type (MONO_TYPE_I4);
5591                 arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
5592                 AddFunc (module, "llvm.x86.sse2.cvttpd2dq", ret_type, arg_types, 1);
5593                 ret_type = type_to_simd_type (MONO_TYPE_I4);
5594                 arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
5595                 AddFunc (module, "llvm.x86.sse2.cvttps2dq", ret_type, arg_types, 1);
5596
5597                 /* Unary ops */
5598                 ret_type = type_to_simd_type (MONO_TYPE_R8);
5599                 arg_types [0] = ret_type;
5600                 AddFunc (module, "llvm.x86.sse2.sqrt.pd", ret_type, arg_types, 1);
5601                 ret_type = type_to_simd_type (MONO_TYPE_R4);
5602                 arg_types [0] = ret_type;
5603                 AddFunc (module, "llvm.x86.sse.sqrt.ps", ret_type, arg_types, 1);
5604                 ret_type = type_to_simd_type (MONO_TYPE_R4);
5605                 arg_types [0] = ret_type;
5606                 AddFunc (module, "llvm.x86.sse.rsqrt.ps", ret_type, arg_types, 1);
5607                 ret_type = type_to_simd_type (MONO_TYPE_R4);
5608                 arg_types [0] = ret_type;
5609                 AddFunc (module, "llvm.x86.sse.rcp.ps", ret_type, arg_types, 1);
5610
5611                 /* shifts */
5612                 ret_type = type_to_simd_type (MONO_TYPE_I2);
5613                 arg_types [0] = ret_type;
5614                 arg_types [1] = LLVMInt32Type ();
5615                 AddFunc (module, "llvm.x86.sse2.psrli.w", ret_type, arg_types, 2);
5616                 AddFunc (module, "llvm.x86.sse2.psrai.w", ret_type, arg_types, 2);
5617                 AddFunc (module, "llvm.x86.sse2.pslli.w", ret_type, arg_types, 2);
5618                 ret_type = type_to_simd_type (MONO_TYPE_I4);
5619                 arg_types [0] = ret_type;
5620                 arg_types [1] = LLVMInt32Type ();
5621                 AddFunc (module, "llvm.x86.sse2.psrli.d", ret_type, arg_types, 2);
5622                 AddFunc (module, "llvm.x86.sse2.psrai.d", ret_type, arg_types, 2);
5623                 AddFunc (module, "llvm.x86.sse2.pslli.d", ret_type, arg_types, 2);
5624                 ret_type = type_to_simd_type (MONO_TYPE_I8);
5625                 arg_types [0] = ret_type;
5626                 arg_types [1] = LLVMInt32Type ();
5627                 AddFunc (module, "llvm.x86.sse2.psrli.q", ret_type, arg_types, 2);
5628                 AddFunc (module, "llvm.x86.sse2.pslli.q", ret_type, arg_types, 2);
5629
5630                 /* pmovmskb */
5631                 ret_type = LLVMInt32Type ();
5632                 arg_types [0] = type_to_simd_type (MONO_TYPE_I1);
5633                 AddFunc (module, "llvm.x86.sse2.pmovmskb.128", ret_type, arg_types, 1);
5634         }
5635
5636         AddFunc (module, "llvm.x86.sse2.pause", LLVMVoidType (), NULL, 0);
5637 #endif
5638
5639         /* Load/Store intrinsics */
5640         {
5641                 LLVMTypeRef arg_types [5];
5642                 int i;
5643                 char name [128];
5644
5645                 for (i = 1; i <= 8; i *= 2) {
5646                         arg_types [0] = LLVMPointerType (LLVMIntType (i * 8), 0);
5647                         arg_types [1] = LLVMInt32Type ();
5648                         arg_types [2] = LLVMInt1Type ();
5649                         arg_types [3] = LLVMInt32Type ();
5650                         sprintf (name, "llvm.mono.load.i%d.p0i%d", i * 8, i * 8);
5651                         LLVMAddFunction (module, name, LLVMFunctionType (LLVMIntType (i * 8), arg_types, 4, FALSE));
5652
5653                         arg_types [0] = LLVMIntType (i * 8);
5654                         arg_types [1] = LLVMPointerType (LLVMIntType (i * 8), 0);
5655                         arg_types [2] = LLVMInt32Type ();
5656                         arg_types [3] = LLVMInt1Type ();
5657                         arg_types [4] = LLVMInt32Type ();
5658                         sprintf (name, "llvm.mono.store.i%d.p0i%d", i * 8, i * 8);
5659                         LLVMAddFunction (module, name, LLVMFunctionType (LLVMVoidType (), arg_types, 5, FALSE));
5660                 }
5661         }
5662 }
5663
5664 static void
5665 add_types (MonoLLVMModule *lmodule)
5666 {
5667         lmodule->ptr_type = LLVMPointerType (sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type (), 0);
5668 }
5669
5670 void
5671 mono_llvm_init (void)
5672 {
5673         mono_native_tls_alloc (&current_cfg_tls_id, NULL);
5674 }
5675
5676 static void
5677 init_jit_module (MonoDomain *domain)
5678 {
5679         MonoJitICallInfo *info;
5680         MonoJitDomainInfo *dinfo;
5681         MonoLLVMModule *module;
5682         char *name;
5683
5684         dinfo = domain_jit_info (domain);
5685         if (dinfo->llvm_module)
5686                 return;
5687
5688         mono_loader_lock ();
5689
5690         if (dinfo->llvm_module) {
5691                 mono_loader_unlock ();
5692                 return;
5693         }
5694
5695         module = g_new0 (MonoLLVMModule, 1);
5696
5697         name = g_strdup_printf ("mono-%s", domain->friendly_name);
5698         module->module = LLVMModuleCreateWithName (name);
5699
5700         module->mono_ee = mono_llvm_create_ee (LLVMCreateModuleProviderForExistingModule (module->module), alloc_cb, emitted_cb, exception_cb, dlsym_cb, &module->ee);
5701
5702         add_intrinsics (module->module);
5703         add_types (module);
5704
5705         module->llvm_types = g_hash_table_new (NULL, NULL);
5706
5707         info = mono_find_jit_icall_by_name ("llvm_resume_unwind_trampoline");
5708         g_assert (info);
5709         LLVMAddGlobalMapping (module->ee, LLVMGetNamedFunction (module->module, "llvm_resume_unwind_trampoline"), (void*)info->func);
5710
5711         mono_memory_barrier ();
5712
5713         dinfo->llvm_module = module;
5714
5715         mono_loader_unlock ();
5716 }
5717
5718 void
5719 mono_llvm_cleanup (void)
5720 {
5721         if (aot_module.module)
5722                 LLVMDisposeModule (aot_module.module);
5723
5724         LLVMContextDispose (LLVMGetGlobalContext ());
5725 }
5726
5727 void
5728 mono_llvm_free_domain_info (MonoDomain *domain)
5729 {
5730         MonoJitDomainInfo *info = domain_jit_info (domain);
5731         MonoLLVMModule *module = info->llvm_module;
5732         int i;
5733
5734         if (!module)
5735                 return;
5736
5737         if (module->llvm_types)
5738                 g_hash_table_destroy (module->llvm_types);
5739
5740         mono_llvm_dispose_ee (module->mono_ee);
5741
5742         if (module->bb_names) {
5743                 for (i = 0; i < module->bb_names_len; ++i)
5744                         g_free (module->bb_names [i]);
5745                 g_free (module->bb_names);
5746         }
5747         //LLVMDisposeModule (module->module);
5748
5749         g_free (module);
5750
5751         info->llvm_module = NULL;
5752 }
5753
5754 void
5755 mono_llvm_create_aot_module (const char *got_symbol, gboolean external_symbols, gboolean emit_dwarf)
5756 {
5757         /* Delete previous module */
5758         if (aot_module.plt_entries)
5759                 g_hash_table_destroy (aot_module.plt_entries);
5760         if (aot_module.module)
5761                 LLVMDisposeModule (aot_module.module);
5762
5763         memset (&aot_module, 0, sizeof (aot_module));
5764
5765         aot_module.module = LLVMModuleCreateWithName ("aot");
5766         aot_module.got_symbol = got_symbol;
5767         aot_module.external_symbols = external_symbols;
5768         aot_module.emit_dwarf = emit_dwarf;
5769         /* The first few entries are reserved */
5770         aot_module.max_got_offset = 16;
5771
5772         add_intrinsics (aot_module.module);
5773         add_types (&aot_module);
5774
5775         /* Add GOT */
5776         /*
5777          * We couldn't compute the type of the LLVM global representing the got because
5778          * its size is only known after all the methods have been emitted. So create
5779          * a dummy variable, and replace all uses it with the real got variable when
5780          * its size is known in mono_llvm_emit_aot_module ().
5781          */
5782         {
5783                 LLVMTypeRef got_type = LLVMArrayType (aot_module.ptr_type, 0);
5784
5785                 aot_module.got_var = LLVMAddGlobal (aot_module.module, got_type, "mono_dummy_got");
5786                 LLVMSetInitializer (aot_module.got_var, LLVMConstNull (got_type));
5787         }
5788
5789         /* Add a dummy personality function */
5790         {
5791                 LLVMBasicBlockRef lbb;
5792                 LLVMBuilderRef lbuilder;
5793                 LLVMValueRef personality;
5794
5795                 personality = LLVMAddFunction (aot_module.module, "mono_aot_personality", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
5796                 LLVMSetLinkage (personality, LLVMInternalLinkage);
5797                 lbb = LLVMAppendBasicBlock (personality, "BB0");
5798                 lbuilder = LLVMCreateBuilder ();
5799                 LLVMPositionBuilderAtEnd (lbuilder, lbb);
5800                 LLVMBuildRetVoid (lbuilder);
5801                 mark_as_used (&aot_module, personality);
5802         }
5803
5804         aot_module.llvm_types = g_hash_table_new (NULL, NULL);
5805         aot_module.plt_entries = g_hash_table_new (g_str_hash, g_str_equal);
5806         aot_module.plt_entries_ji = g_hash_table_new (NULL, NULL);
5807         aot_module.method_to_lmethod = g_hash_table_new (NULL, NULL);
5808 }
5809
5810 /*
5811  * Emit the aot module into the LLVM bitcode file FILENAME.
5812  */
5813 void
5814 mono_llvm_emit_aot_module (const char *filename, const char *cu_name)
5815 {
5816         LLVMTypeRef got_type;
5817         LLVMValueRef real_got;
5818         MonoLLVMModule *module = &aot_module;
5819
5820         /* 
5821          * Create the real got variable and replace all uses of the dummy variable with
5822          * the real one.
5823          */
5824         got_type = LLVMArrayType (aot_module.ptr_type, module->max_got_offset + 1);
5825         real_got = LLVMAddGlobal (aot_module.module, got_type, aot_module.got_symbol);
5826         LLVMSetInitializer (real_got, LLVMConstNull (got_type));
5827         if (module->external_symbols) {
5828                 LLVMSetLinkage (real_got, LLVMExternalLinkage);
5829                 LLVMSetVisibility (real_got, LLVMHiddenVisibility);
5830         } else {
5831                 LLVMSetLinkage (real_got, LLVMInternalLinkage);
5832         }
5833         mono_llvm_replace_uses_of (aot_module.got_var, real_got);
5834
5835         mark_as_used (&aot_module, real_got);
5836
5837         /* Delete the dummy got so it doesn't become a global */
5838         LLVMDeleteGlobal (aot_module.got_var);
5839
5840         emit_llvm_used (&aot_module);
5841         emit_dbg_info (&aot_module, filename, cu_name);
5842
5843         /* Replace PLT entries for directly callable methods with the methods themselves */
5844         {
5845                 GHashTableIter iter;
5846                 MonoJumpInfo *ji;
5847                 LLVMValueRef callee;
5848
5849                 g_hash_table_iter_init (&iter, aot_module.plt_entries_ji);
5850                 while (g_hash_table_iter_next (&iter, (void**)&ji, (void**)&callee)) {
5851                         if (mono_aot_is_direct_callable (ji)) {
5852                                 LLVMValueRef lmethod;
5853
5854                                 lmethod = g_hash_table_lookup (module->method_to_lmethod, ji->data.method);
5855                                 /* The types might not match because the caller might pass an rgctx */
5856                                 if (lmethod && LLVMTypeOf (callee) == LLVMTypeOf (lmethod)) {
5857                                         mono_llvm_replace_uses_of (callee, lmethod);
5858                                         mono_aot_mark_unused_llvm_plt_entry (ji);
5859                                 }
5860                         }
5861                 }
5862         }
5863
5864 #if 0
5865         {
5866                 char *verifier_err;
5867
5868                 if (LLVMVerifyModule (aot_module.module, LLVMReturnStatusAction, &verifier_err)) {
5869                         g_assert_not_reached ();
5870                 }
5871         }
5872 #endif
5873
5874         LLVMWriteBitcodeToFile (aot_module.module, filename);
5875 }
5876
5877
5878 static LLVMValueRef
5879 md_string (const char *s)
5880 {
5881         return LLVMMDString (s, strlen (s));
5882 }
5883
5884 /* Debugging support */
5885
5886 static void
5887 emit_dbg_info (MonoLLVMModule *lmodule, const char *filename, const char *cu_name)
5888 {
5889         LLVMModuleRef module = lmodule->module;
5890         LLVMValueRef args [16], cu_args [16], cu, ver;
5891         int n_cuargs;
5892         char *build_info, *s, *dir;
5893
5894         /*
5895          * This can only be enabled when LLVM code is emitted into a separate object
5896          * file, since the AOT compiler also emits dwarf info,
5897          * and the abbrev indexes will not be correct since llvm has added its own
5898          * abbrevs.
5899          */
5900         if (!lmodule->emit_dwarf)
5901                 return;
5902
5903         /*
5904          * Emit dwarf info in the form of LLVM metadata. There is some
5905          * out-of-date documentation at:
5906          * http://llvm.org/docs/SourceLevelDebugging.html
5907          * but most of this was gathered from the llvm and
5908          * clang sources.
5909          */
5910
5911         n_cuargs = 0;
5912         cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), DW_TAG_compile_unit, FALSE);
5913         /* CU name/compilation dir */
5914         dir = g_path_get_dirname (filename);
5915         args [0] = LLVMMDString (cu_name, strlen (cu_name));
5916         args [1] = LLVMMDString (dir, strlen (dir));
5917         cu_args [n_cuargs ++] = LLVMMDNode (args, 2);
5918         g_free (dir);
5919         /* Language */
5920         cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), DW_LANG_C99, FALSE);
5921         /* Producer */
5922         build_info = mono_get_runtime_build_info ();
5923         s = g_strdup_printf ("Mono AOT Compiler %s (LLVM)", build_info);
5924         cu_args [n_cuargs ++] = LLVMMDString (s, strlen (s));
5925         g_free (build_info);
5926         /* Optimized */
5927         cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
5928         /* Flags */
5929         cu_args [n_cuargs ++] = LLVMMDString ("", strlen (""));
5930         /* Runtime version */
5931         cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
5932         /* Enums */
5933         cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
5934         cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
5935         /* Subprograms */
5936         if (lmodule->subprogram_mds) {
5937                 LLVMValueRef *mds;
5938                 int i;
5939
5940                 mds = g_new0 (LLVMValueRef, lmodule->subprogram_mds->len);
5941                 for (i = 0; i < lmodule->subprogram_mds->len; ++i)
5942                         mds [i] = g_ptr_array_index (lmodule->subprogram_mds, i);
5943                 cu_args [n_cuargs ++] = LLVMMDNode (mds, lmodule->subprogram_mds->len);
5944         } else {
5945                 cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
5946         }
5947         /* GVs */
5948         cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
5949         /* Imported modules */
5950         cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
5951         /* SplitName */
5952         cu_args [n_cuargs ++] = LLVMMDString ("", strlen (""));
5953         /* DebugEmissionKind = FullDebug */
5954         cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
5955         cu = LLVMMDNode (cu_args, n_cuargs);
5956         LLVMAddNamedMetadataOperand (module, "llvm.dbg.cu", cu);
5957
5958         args [0] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
5959         args [1] = LLVMMDString ("Dwarf Version", strlen ("Dwarf Version"));
5960         args [2] = LLVMConstInt (LLVMInt32Type (), 2, FALSE);
5961         ver = LLVMMDNode (args, 3);
5962         LLVMAddNamedMetadataOperand (module, "llvm.module.flags", ver);
5963
5964         args [0] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
5965         args [1] = LLVMMDString ("Debug Info Version", strlen ("Debug Info Version"));
5966         args [2] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
5967         ver = LLVMMDNode (args, 3);
5968         LLVMAddNamedMetadataOperand (module, "llvm.module.flags", ver);
5969 }
5970
5971 static LLVMValueRef
5972 emit_dbg_subprogram (EmitContext *ctx, MonoCompile *cfg, LLVMValueRef method, const char *name)
5973 {
5974         MonoLLVMModule *module = ctx->lmodule;
5975         MonoDebugMethodInfo *minfo = ctx->minfo;
5976         char *source_file, *dir, *filename;
5977         LLVMValueRef md, args [16], ctx_args [16], md_args [64], type_args [16], ctx_md, type_md;
5978         int n_il_offsets;
5979         int *il_offsets;
5980         int *line_numbers;
5981
5982         if (!minfo)
5983                 return NULL;
5984
5985         mono_debug_symfile_get_line_numbers_full (minfo, &source_file, NULL, &n_il_offsets, &il_offsets, &line_numbers, NULL, NULL, NULL, NULL);
5986         if (!source_file)
5987                 source_file = g_strdup ("<unknown>");
5988         dir = g_path_get_dirname (source_file);
5989         filename = g_path_get_basename (source_file);
5990
5991         ctx_args [0] = LLVMConstInt (LLVMInt32Type (), 0x29, FALSE);
5992         args [0] = md_string (filename);
5993         args [1] = md_string (dir);
5994         ctx_args [1] = LLVMMDNode (args, 2);
5995         ctx_md = LLVMMDNode (ctx_args, 2);
5996
5997         type_args [0] = LLVMConstInt (LLVMInt32Type (), DW_TAG_subroutine_type, FALSE);
5998         type_args [1] = NULL;
5999         type_args [2] = NULL;
6000         type_args [3] = LLVMMDString ("", 0);
6001         type_args [4] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
6002         type_args [5] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
6003         type_args [6] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
6004         type_args [7] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
6005         type_args [8] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
6006         type_args [9] = NULL;
6007         type_args [10] = NULL;
6008         type_args [11] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
6009         type_args [12] = NULL;
6010         type_args [13] = NULL;
6011         type_args [14] = NULL;
6012         type_md = LLVMMDNode (type_args, 14);
6013
6014         /* http://llvm.org/docs/SourceLevelDebugging.html#subprogram-descriptors */
6015         md_args [0] = LLVMConstInt (LLVMInt32Type (), DW_TAG_subprogram, FALSE);
6016         /* Source directory + file pair */
6017         args [0] = md_string (filename);
6018         args [1] = md_string (dir);
6019         md_args [1] = LLVMMDNode (args ,2);
6020         md_args [2] = ctx_md;
6021         md_args [3] = md_string (cfg->method->name);
6022         md_args [4] = md_string (name);
6023         md_args [5] = md_string (name);
6024         /* Line number */
6025         if (n_il_offsets)
6026                 md_args [6] = LLVMConstInt (LLVMInt32Type (), line_numbers [0], FALSE);
6027         else
6028                 md_args [6] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
6029         /* Type */
6030         md_args [7] = type_md;
6031         /* static */
6032         md_args [8] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
6033         /* not extern */
6034         md_args [9] = LLVMConstInt (LLVMInt1Type (), 1, FALSE);
6035         /* Virtuality */
6036         md_args [10] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
6037         /* Index into a virtual function */
6038         md_args [11] = NULL;
6039         md_args [12] = NULL;
6040         /* Flags */
6041         md_args [13] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
6042         /* isOptimized */
6043         md_args [14] = LLVMConstInt (LLVMInt1Type (), 1, FALSE);
6044         /* Pointer to LLVM function */
6045         md_args [15] = method;
6046         /* Function template parameter */
6047         md_args [16] = NULL;
6048         /* Function declaration descriptor */
6049         md_args [17] = NULL;
6050         /* List of function variables */
6051         md_args [18] = LLVMMDNode (args, 0);
6052         /* Line number */
6053         md_args [19] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
6054         md = LLVMMDNode (md_args, 20);
6055
6056         if (!module->subprogram_mds)
6057                 module->subprogram_mds = g_ptr_array_new ();
6058         g_ptr_array_add (module->subprogram_mds, md);
6059
6060         g_free (dir);
6061         g_free (filename);
6062         g_free (source_file);
6063         g_free (il_offsets);
6064         g_free (line_numbers);
6065
6066         return md;
6067 }
6068
6069 static void
6070 emit_dbg_loc (EmitContext *ctx, LLVMBuilderRef builder, const unsigned char *cil_code)
6071 {
6072         MonoCompile *cfg = ctx->cfg;
6073
6074         if (ctx->minfo && cil_code && cil_code >= cfg->header->code && cil_code < cfg->header->code + cfg->header->code_size) {
6075                 MonoDebugSourceLocation *loc;
6076                 LLVMValueRef loc_md, md_args [16];
6077                 int nmd_args;
6078
6079                 loc = mono_debug_symfile_lookup_location (ctx->minfo, cil_code - cfg->header->code);
6080
6081                 if (loc) {
6082                         nmd_args = 0;
6083                         md_args [nmd_args ++] = LLVMConstInt (LLVMInt32Type (), loc->row, FALSE);
6084                         md_args [nmd_args ++] = LLVMConstInt (LLVMInt32Type (), loc->column, FALSE);
6085                         md_args [nmd_args ++] = ctx->dbg_md;
6086                         md_args [nmd_args ++] = NULL;
6087                         loc_md = LLVMMDNode (md_args, nmd_args);
6088                         LLVMSetCurrentDebugLocation (builder, loc_md);
6089                         mono_debug_symfile_free_location (loc);
6090                 }
6091         }
6092 }
6093
6094 /*
6095   DESIGN:
6096   - Emit LLVM IR from the mono IR using the LLVM C API.
6097   - The original arch specific code remains, so we can fall back to it if we run
6098     into something we can't handle.
6099 */
6100
6101 /*  
6102   A partial list of issues:
6103   - Handling of opcodes which can throw exceptions.
6104
6105       In the mono JIT, these are implemented using code like this:
6106           method:
6107       <compare>
6108           throw_pos:
6109           b<cond> ex_label
6110           <rest of code>
6111       ex_label:
6112           push throw_pos - method
6113           call <exception trampoline>
6114
6115           The problematic part is push throw_pos - method, which cannot be represented
6116       in the LLVM IR, since it does not support label values.
6117           -> this can be implemented in AOT mode using inline asm + labels, but cannot
6118           be implemented in JIT mode ?
6119           -> a possible but slower implementation would use the normal exception 
6120       throwing code but it would need to control the placement of the throw code
6121       (it needs to be exactly after the compare+branch).
6122           -> perhaps add a PC offset intrinsics ?
6123
6124   - efficient implementation of .ovf opcodes.
6125
6126           These are currently implemented as:
6127           <ins which sets the condition codes>
6128           b<cond> ex_label
6129
6130           Some overflow opcodes are now supported by LLVM SVN.
6131
6132   - exception handling, unwinding.
6133     - SSA is disabled for methods with exception handlers    
6134         - How to obtain unwind info for LLVM compiled methods ?
6135           -> this is now solved by converting the unwind info generated by LLVM
6136              into our format.
6137         - LLVM uses the c++ exception handling framework, while we use our home grown
6138       code, and couldn't use the c++ one:
6139       - its not supported under VC++, other exotic platforms.
6140           - it might be impossible to support filter clauses with it.
6141
6142   - trampolines.
6143   
6144     The trampolines need a predictable call sequence, since they need to disasm
6145     the calling code to obtain register numbers / offsets.
6146
6147     LLVM currently generates this code in non-JIT mode:
6148            mov    -0x98(%rax),%eax
6149            callq  *%rax
6150     Here, the vtable pointer is lost. 
6151     -> solution: use one vtable trampoline per class.
6152
6153   - passing/receiving the IMT pointer/RGCTX.
6154     -> solution: pass them as normal arguments ?
6155
6156   - argument passing.
6157   
6158           LLVM does not allow the specification of argument registers etc. This means
6159       that all calls are made according to the platform ABI.
6160
6161   - passing/receiving vtypes.
6162
6163       Vtypes passed/received in registers are handled by the front end by using
6164           a signature with scalar arguments, and loading the parts of the vtype into those
6165           arguments.
6166
6167           Vtypes passed on the stack are handled using the 'byval' attribute.
6168
6169   - ldaddr.
6170
6171     Supported though alloca, we need to emit the load/store code.
6172
6173   - types.
6174
6175     The mono JIT uses pointer sized iregs/double fregs, while LLVM uses precisely
6176     typed registers, so we have to keep track of the precise LLVM type of each vreg.
6177     This is made easier because the IR is already in SSA form.
6178     An additional problem is that our IR is not consistent with types, i.e. i32/ia64 
6179         types are frequently used incorrectly.
6180 */
6181
6182 /*
6183   AOT SUPPORT:
6184   Emit LLVM bytecode into a .bc file, compile it using llc into a .s file, then link
6185   it with the file containing the methods emitted by the JIT and the AOT data
6186   structures.
6187 */
6188
6189 /* FIXME: Normalize some aspects of the mono IR to allow easier translation, like:
6190  *   - each bblock should end with a branch
6191  *   - setting the return value, making cfg->ret non-volatile
6192  * - avoid some transformations in the JIT which make it harder for us to generate
6193  *   code.
6194  * - use pointer types to help optimizations.
6195  */