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