2010-02-15 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini-llvm.c
1 /*
2  * mini-llvm.c: llvm "Backend" for the mono JIT
3  *
4  * (C) 2009 Novell, Inc.
5  */
6
7 #include "mini.h"
8 #include <mono/metadata/debug-helpers.h>
9 #include <mono/metadata/mempool-internals.h>
10
11 #ifndef __STDC_LIMIT_MACROS
12 #define __STDC_LIMIT_MACROS
13 #endif
14 #ifndef __STDC_CONSTANT_MACROS
15 #define __STDC_CONSTANT_MACROS
16 #endif
17
18 #include "llvm-c/Core.h"
19 #include "llvm-c/ExecutionEngine.h"
20 #include "llvm-c/BitWriter.h"
21 #include "llvm-c/Analysis.h"
22
23 #include "mini-llvm-cpp.h"
24
25  /*
26   * Information associated by mono with LLVM modules.
27   */
28 typedef struct {
29         LLVMModuleRef module;
30         LLVMValueRef throw, throw_corlib_exception;
31         GHashTable *llvm_types;
32         LLVMValueRef got_var;
33         const char *got_symbol;
34         GHashTable *plt_entries;
35 } MonoLLVMModule;
36
37 /*
38  * Information associated by the backend with mono basic blocks.
39  */
40 typedef struct {
41         LLVMBasicBlockRef bblock, end_bblock;
42         LLVMValueRef finally_ind;
43         gboolean added, invoke_target;
44         /* 
45          * If this bblock is the start of a finally clause, this is a list of bblocks it
46          * needs to branch to in ENDFINALLY.
47          */
48         GSList *call_handler_return_bbs;
49         LLVMValueRef endfinally_switch;
50         GSList *phi_nodes;
51 } BBInfo;
52
53 /*
54  * Structure containing emit state
55  */
56 typedef struct {
57         MonoMemPool *mempool;
58
59         /* Maps method names to the corresponding LLVMValueRef */
60         GHashTable *emitted_method_decls;
61
62         MonoCompile *cfg;
63         LLVMValueRef lmethod;
64         MonoLLVMModule *lmodule;
65         LLVMModuleRef module;
66         BBInfo *bblocks;
67         int sindex, default_index, ex_index;
68         LLVMBuilderRef builder;
69         LLVMValueRef *values, *addresses;
70         MonoType **vreg_cli_types;
71         LLVMCallInfo *linfo;
72         MonoMethodSignature *sig;
73         GSList *builders;
74         GHashTable *region_to_handler;
75         LLVMBuilderRef alloca_builder;
76         LLVMValueRef last_alloca;
77
78         char temp_name [32];
79 } EmitContext;
80
81 typedef struct {
82         MonoBasicBlock *bb;
83         MonoInst *phi;
84         MonoBasicBlock *in_bb;
85         int sreg;
86 } PhiNode;
87
88 /*
89  * Instruction metadata
90  * This is the same as ins_info, but LREG != IREG.
91  */
92 #ifdef MINI_OP
93 #undef MINI_OP
94 #endif
95 #ifdef MINI_OP3
96 #undef MINI_OP3
97 #endif
98 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
99 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
100 #define NONE ' '
101 #define IREG 'i'
102 #define FREG 'f'
103 #define VREG 'v'
104 #define XREG 'x'
105 #define LREG 'l'
106 /* keep in sync with the enum in mini.h */
107 const char
108 llvm_ins_info[] = {
109 #include "mini-ops.h"
110 };
111 #undef MINI_OP
112 #undef MINI_OP3
113
114 #if SIZEOF_VOID_P == 4
115 #define GET_LONG_IMM(ins) (((guint64)(ins)->inst_ms_word << 32) | (guint64)(guint32)(ins)->inst_ls_word)
116 #else
117 #define GET_LONG_IMM(ins) ((ins)->inst_imm)
118 #endif
119
120 #define LLVM_INS_INFO(opcode) (&llvm_ins_info [((opcode) - OP_START - 1) * 4])
121
122 #if 0
123 #define TRACE_FAILURE(msg) do { printf ("%s\n", msg); } while (0)
124 #else
125 #define TRACE_FAILURE(msg)
126 #endif
127
128 #define LLVM_FAILURE(ctx, reason) do { \
129         TRACE_FAILURE (reason); \
130         (ctx)->cfg->exception_message = g_strdup (reason); \
131         (ctx)->cfg->disable_llvm = TRUE; \
132         goto FAILURE; \
133 } while (0)
134
135 #define CHECK_FAILURE(ctx) do { \
136     if ((ctx)->cfg->disable_llvm) \
137                 goto FAILURE; \
138 } while (0)
139
140 static LLVMIntPredicate cond_to_llvm_cond [] = {
141         LLVMIntEQ,
142         LLVMIntNE,
143         LLVMIntSLE,
144         LLVMIntSGE,
145         LLVMIntSLT,
146         LLVMIntSGT,
147         LLVMIntULE,
148         LLVMIntUGE,
149         LLVMIntULT,
150         LLVMIntUGT,
151 };
152
153 static LLVMRealPredicate fpcond_to_llvm_cond [] = {
154         LLVMRealOEQ,
155         LLVMRealUNE,
156         LLVMRealOLE,
157         LLVMRealOGE,
158         LLVMRealOLT,
159         LLVMRealOGT,
160         LLVMRealULE,
161         LLVMRealUGE,
162         LLVMRealULT,
163         LLVMRealUGT,
164 };
165
166 static LLVMExecutionEngineRef ee;
167 static guint32 current_cfg_tls_id;
168
169 static MonoLLVMModule jit_module, aot_module;
170 static gboolean jit_module_inited;
171
172 static void init_jit_module (void);
173
174 /*
175  * IntPtrType:
176  *
177  *   The LLVM type with width == sizeof (gpointer)
178  */
179 static LLVMTypeRef
180 IntPtrType (void)
181 {
182         return sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type ();
183 }
184
185 /*
186  * get_vtype_size:
187  *
188  *   Return the size of the LLVM representation of the vtype T.
189  */
190 static guint32
191 get_vtype_size (MonoType *t)
192 {
193         int size;
194
195         size = mono_class_value_size (mono_class_from_mono_type (t), NULL);
196
197         while (size < sizeof (gpointer) && mono_is_power_of_two (size) == -1)
198                 size ++;
199
200         return size;
201 }
202
203 /*
204  * simd_class_to_llvm_type:
205  *
206  *   Return the LLVM type corresponding to the Mono.SIMD class KLASS
207  */
208 static LLVMTypeRef
209 simd_class_to_llvm_type (EmitContext *ctx, MonoClass *klass)
210 {
211         if (!strcmp (klass->name, "Vector2d")) {
212                 return LLVMVectorType (LLVMDoubleType (), 2);
213         } else if (!strcmp (klass->name, "Vector2l")) {
214                 return LLVMVectorType (LLVMInt64Type (), 2);
215         } else if (!strcmp (klass->name, "Vector2ul")) {
216                 return LLVMVectorType (LLVMInt64Type (), 2);
217         } else if (!strcmp (klass->name, "Vector4i")) {
218                 return LLVMVectorType (LLVMInt32Type (), 4);
219         } else if (!strcmp (klass->name, "Vector4ui")) {
220                 return LLVMVectorType (LLVMInt32Type (), 4);
221         } else if (!strcmp (klass->name, "Vector4f")) {
222                 return LLVMVectorType (LLVMFloatType (), 4);
223         } else if (!strcmp (klass->name, "Vector8s")) {
224                 return LLVMVectorType (LLVMInt16Type (), 8);
225         } else if (!strcmp (klass->name, "Vector8us")) {
226                 return LLVMVectorType (LLVMInt16Type (), 8);
227         } else if (!strcmp (klass->name, "Vector16sb")) {
228                 return LLVMVectorType (LLVMInt8Type (), 16);
229         } else if (!strcmp (klass->name, "Vector16b")) {
230                 return LLVMVectorType (LLVMInt8Type (), 16);
231         } else {
232                 printf ("%s\n", klass->name);
233                 NOT_IMPLEMENTED;
234                 return NULL;
235         }
236 }
237
238 /*
239  * type_to_llvm_type:
240  *
241  *   Return the LLVM type corresponding to T.
242  */
243 static LLVMTypeRef
244 type_to_llvm_type (EmitContext *ctx, MonoType *t)
245 {
246         if (t->byref)
247                 return LLVMPointerType (LLVMInt8Type (), 0);
248         switch (t->type) {
249         case MONO_TYPE_VOID:
250                 return LLVMVoidType ();
251         case MONO_TYPE_I1:
252                 return LLVMInt8Type ();
253         case MONO_TYPE_I2:
254                 return LLVMInt16Type ();
255         case MONO_TYPE_I4:
256                 return LLVMInt32Type ();
257         case MONO_TYPE_U1:
258                 return LLVMInt8Type ();
259         case MONO_TYPE_U2:
260                 return LLVMInt16Type ();
261         case MONO_TYPE_U4:
262                 return LLVMInt32Type ();
263         case MONO_TYPE_BOOLEAN:
264                 return LLVMInt8Type ();
265         case MONO_TYPE_I8:
266         case MONO_TYPE_U8:
267                 return LLVMInt64Type ();
268         case MONO_TYPE_CHAR:
269                 return LLVMInt16Type ();
270         case MONO_TYPE_R4:
271                 return LLVMFloatType ();
272         case MONO_TYPE_R8:
273                 return LLVMDoubleType ();
274         case MONO_TYPE_I:
275         case MONO_TYPE_U:
276                 return IntPtrType ();
277         case MONO_TYPE_OBJECT:
278         case MONO_TYPE_CLASS:
279         case MONO_TYPE_ARRAY:
280         case MONO_TYPE_SZARRAY:
281         case MONO_TYPE_STRING:
282         case MONO_TYPE_PTR:
283                 return LLVMPointerType (IntPtrType (), 0);
284         case MONO_TYPE_VAR:
285         case MONO_TYPE_MVAR:
286                 /* Because of generic sharing */
287                 return IntPtrType ();
288         case MONO_TYPE_GENERICINST:
289                 if (!mono_type_generic_inst_is_valuetype (t))
290                         return IntPtrType ();
291                 /* Fall through */
292         case MONO_TYPE_VALUETYPE:
293         case MONO_TYPE_TYPEDBYREF: {
294                 MonoClass *klass;
295                 LLVMTypeRef ltype;
296
297                 klass = mono_class_from_mono_type (t);
298
299                 if (MONO_CLASS_IS_SIMD (ctx->cfg, klass))
300                         return simd_class_to_llvm_type (ctx, klass);
301
302                 if (klass->enumtype)
303                         return type_to_llvm_type (ctx, mono_class_enum_basetype (klass));
304                 ltype = g_hash_table_lookup (ctx->lmodule->llvm_types, klass);
305                 if (!ltype) {
306                         int i, size;
307                         LLVMTypeRef *eltypes;
308
309                         size = get_vtype_size (t);
310
311                         eltypes = g_new (LLVMTypeRef, size);
312                         for (i = 0; i < size; ++i)
313                                 eltypes [i] = LLVMInt8Type ();
314
315                         ltype = LLVMStructType (eltypes, size, FALSE);
316                         g_hash_table_insert (ctx->lmodule->llvm_types, klass, ltype);
317                         g_free (eltypes);
318                 }
319                 return ltype;
320         }
321
322         default:
323                 printf ("X: %d\n", t->type);
324                 ctx->cfg->exception_message = g_strdup_printf ("type %s", mono_type_full_name (t));
325                 ctx->cfg->disable_llvm = TRUE;
326                 return NULL;
327         }
328 }
329
330 /*
331  * type_is_unsigned:
332  *
333  *   Return whenever T is an unsigned int type.
334  */
335 static gboolean
336 type_is_unsigned (EmitContext *ctx, MonoType *t)
337 {
338         if (t->byref)
339                 return FALSE;
340         switch (t->type) {
341         case MONO_TYPE_U1:
342         case MONO_TYPE_U2:
343         case MONO_TYPE_U4:
344         case MONO_TYPE_U8:
345                 return TRUE;
346         default:
347                 return FALSE;
348         }
349 }
350
351 /*
352  * type_to_llvm_arg_type:
353  *
354  *   Same as type_to_llvm_type, but treat i8/i16 as i32.
355  */
356 static LLVMTypeRef
357 type_to_llvm_arg_type (EmitContext *ctx, MonoType *t)
358 {
359         LLVMTypeRef ptype = type_to_llvm_type (ctx, t);
360         
361         if (ptype == LLVMInt8Type () || ptype == LLVMInt16Type ()) {
362                 /* 
363                  * LLVM generates code which only sets the lower bits, while JITted
364                  * code expects all the bits to be set.
365                  */
366                 ptype = LLVMInt32Type ();
367         }
368
369         return ptype;
370 }
371
372 /*
373  * llvm_type_to_stack_type:
374  *
375  *   Return the LLVM type which needs to be used when a value of type TYPE is pushed
376  * on the IL stack.
377  */
378 static G_GNUC_UNUSED LLVMTypeRef
379 llvm_type_to_stack_type (LLVMTypeRef type)
380 {
381         if (type == NULL)
382                 return NULL;
383         if (type == LLVMInt8Type ())
384                 return LLVMInt32Type ();
385         else if (type == LLVMInt16Type ())
386                 return LLVMInt32Type ();
387         else if (type == LLVMFloatType ())
388                 return LLVMDoubleType ();
389         else
390                 return type;
391 }
392
393 /*
394  * regtype_to_llvm_type:
395  *
396  *   Return the LLVM type corresponding to the regtype C used in instruction 
397  * descriptions.
398  */
399 static LLVMTypeRef
400 regtype_to_llvm_type (char c)
401 {
402         switch (c) {
403         case 'i':
404                 return LLVMInt32Type ();
405         case 'l':
406                 return LLVMInt64Type ();
407         case 'f':
408                 return LLVMDoubleType ();
409         default:
410                 return NULL;
411         }
412 }
413
414 /*
415  * op_to_llvm_type:
416  *
417  *   Return the LLVM type corresponding to the unary/binary opcode OPCODE.
418  */
419 static LLVMTypeRef
420 op_to_llvm_type (int opcode)
421 {
422         switch (opcode) {
423         case OP_ICONV_TO_I1:
424         case OP_LCONV_TO_I1:
425                 return LLVMInt8Type ();
426         case OP_ICONV_TO_U1:
427         case OP_LCONV_TO_U1:
428                 return LLVMInt8Type ();
429         case OP_ICONV_TO_I2:
430         case OP_LCONV_TO_I2:
431                 return LLVMInt16Type ();
432         case OP_ICONV_TO_U2:
433         case OP_LCONV_TO_U2:
434                 return LLVMInt16Type ();
435         case OP_ICONV_TO_I4:
436         case OP_LCONV_TO_I4:
437                 return LLVMInt32Type ();
438         case OP_ICONV_TO_U4:
439         case OP_LCONV_TO_U4:
440                 return LLVMInt32Type ();
441         case OP_ICONV_TO_I8:
442                 return LLVMInt64Type ();
443         case OP_ICONV_TO_R4:
444                 return LLVMFloatType ();
445         case OP_ICONV_TO_R8:
446                 return LLVMDoubleType ();
447         case OP_ICONV_TO_U8:
448                 return LLVMInt64Type ();
449         case OP_FCONV_TO_I4:
450                 return LLVMInt32Type ();
451         case OP_FCONV_TO_I8:
452                 return LLVMInt64Type ();
453         case OP_FCONV_TO_I1:
454         case OP_FCONV_TO_U1:
455                 return LLVMInt8Type ();
456         case OP_FCONV_TO_I2:
457         case OP_FCONV_TO_U2:
458                 return LLVMInt16Type ();
459         case OP_FCONV_TO_I:
460         case OP_FCONV_TO_U:
461                 return sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type ();
462         case OP_IADD_OVF:
463         case OP_IADD_OVF_UN:
464         case OP_ISUB_OVF:
465         case OP_ISUB_OVF_UN:
466         case OP_IMUL_OVF:
467         case OP_IMUL_OVF_UN:
468                 return LLVMInt32Type ();
469         case OP_LADD_OVF:
470         case OP_LADD_OVF_UN:
471         case OP_LSUB_OVF:
472         case OP_LSUB_OVF_UN:
473         case OP_LMUL_OVF:
474         case OP_LMUL_OVF_UN:
475                 return LLVMInt64Type ();
476         default:
477                 printf ("%s\n", mono_inst_name (opcode));
478                 g_assert_not_reached ();
479                 return NULL;
480         }
481 }               
482
483 /*
484  * load_store_to_llvm_type:
485  *
486  *   Return the size/sign/zero extension corresponding to the load/store opcode
487  * OPCODE.
488  */
489 static LLVMTypeRef
490 load_store_to_llvm_type (int opcode, int *size, gboolean *sext, gboolean *zext)
491 {
492         *sext = FALSE;
493         *zext = FALSE;
494
495         switch (opcode) {
496         case OP_LOADI1_MEMBASE:
497         case OP_STOREI1_MEMBASE_REG:
498         case OP_STOREI1_MEMBASE_IMM:
499                 *size = 1;
500                 *sext = TRUE;
501                 return LLVMInt8Type ();
502         case OP_LOADU1_MEMBASE:
503         case OP_LOADU1_MEM:
504                 *size = 1;
505                 *zext = TRUE;
506                 return LLVMInt8Type ();
507         case OP_LOADI2_MEMBASE:
508         case OP_STOREI2_MEMBASE_REG:
509         case OP_STOREI2_MEMBASE_IMM:
510                 *size = 2;
511                 *sext = TRUE;
512                 return LLVMInt16Type ();
513         case OP_LOADU2_MEMBASE:
514         case OP_LOADU2_MEM:
515                 *size = 2;
516                 *zext = TRUE;
517                 return LLVMInt16Type ();
518         case OP_LOADI4_MEMBASE:
519         case OP_LOADU4_MEMBASE:
520         case OP_LOADI4_MEM:
521         case OP_LOADU4_MEM:
522         case OP_STOREI4_MEMBASE_REG:
523         case OP_STOREI4_MEMBASE_IMM:
524                 *size = 4;
525                 return LLVMInt32Type ();
526         case OP_LOADI8_MEMBASE:
527         case OP_LOADI8_MEM:
528         case OP_STOREI8_MEMBASE_REG:
529         case OP_STOREI8_MEMBASE_IMM:
530                 *size = 8;
531                 return LLVMInt64Type ();
532         case OP_LOADR4_MEMBASE:
533         case OP_STORER4_MEMBASE_REG:
534                 *size = 4;
535                 return LLVMFloatType ();
536         case OP_LOADR8_MEMBASE:
537         case OP_STORER8_MEMBASE_REG:
538                 *size = 8;
539                 return LLVMDoubleType ();
540         case OP_LOAD_MEMBASE:
541         case OP_LOAD_MEM:
542         case OP_STORE_MEMBASE_REG:
543         case OP_STORE_MEMBASE_IMM:
544                 *size = sizeof (gpointer);
545                 return IntPtrType ();
546         default:
547                 g_assert_not_reached ();
548                 return NULL;
549         }
550 }
551
552 /*
553  * ovf_op_to_intrins:
554  *
555  *   Return the LLVM intrinsics corresponding to the overflow opcode OPCODE.
556  */
557 static const char*
558 ovf_op_to_intrins (int opcode)
559 {
560         switch (opcode) {
561         case OP_IADD_OVF:
562                 return "llvm.sadd.with.overflow.i32";
563         case OP_IADD_OVF_UN:
564                 return "llvm.uadd.with.overflow.i32";
565         case OP_ISUB_OVF:
566                 return "llvm.ssub.with.overflow.i32";
567         case OP_ISUB_OVF_UN:
568                 return "llvm.usub.with.overflow.i32";
569         case OP_IMUL_OVF:
570                 return "llvm.smul.with.overflow.i32";
571         case OP_IMUL_OVF_UN:
572                 return "llvm.umul.with.overflow.i32";
573         case OP_LADD_OVF:
574                 return "llvm.sadd.with.overflow.i64";
575         case OP_LADD_OVF_UN:
576                 return "llvm.uadd.with.overflow.i64";
577         case OP_LSUB_OVF:
578                 return "llvm.ssub.with.overflow.i64";
579         case OP_LSUB_OVF_UN:
580                 return "llvm.usub.with.overflow.i64";
581         case OP_LMUL_OVF:
582                 return "llvm.smul.with.overflow.i64";
583         case OP_LMUL_OVF_UN:
584                 return "llvm.umul.with.overflow.i64";
585         default:
586                 g_assert_not_reached ();
587                 return NULL;
588         }
589 }
590
591 static const char*
592 simd_op_to_intrins (int opcode)
593 {
594         switch (opcode) {
595 #if defined(TARGET_X86) || defined(TARGET_AMD64)
596         case OP_MINPD:
597                 return "llvm.x86.sse2.min.pd";
598         case OP_MINPS:
599                 return "llvm.x86.sse2.min.ps";
600         case OP_PMIND_UN:
601                 return "llvm.x86.sse41.pminud";
602         case OP_PMINW_UN:
603                 return "llvm.x86.sse41.pminuw";
604         case OP_PMINB_UN:
605                 return "llvm.x86.sse41.pminub";
606         case OP_MAXPD:
607                 return "llvm.x86.sse2.max.pd";
608         case OP_MAXPS:
609                 return "llvm.x86.sse2.max.ps";
610         case OP_PMAXD_UN:
611                 return "llvm.x86.sse41.pmaxud";
612         case OP_PMAXW_UN:
613                 return "llvm.x86.sse41.pmaxuw";
614         case OP_PMAXB_UN:
615                 return "llvm.x86.sse41.pmaxub";
616 #endif
617         default:
618                 g_assert_not_reached ();
619                 return NULL;
620         }
621 }
622
623 /*
624  * get_bb:
625  *
626  *   Return the LLVM basic block corresponding to BB.
627  */
628 static LLVMBasicBlockRef
629 get_bb (EmitContext *ctx, MonoBasicBlock *bb)
630 {
631         char bb_name [128];
632
633         if (ctx->bblocks [bb->block_num].bblock == NULL) {
634                 sprintf (bb_name, "BB%d", bb->block_num);
635
636                 ctx->bblocks [bb->block_num].bblock = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
637                 ctx->bblocks [bb->block_num].end_bblock = ctx->bblocks [bb->block_num].bblock;
638         }
639
640         return ctx->bblocks [bb->block_num].bblock;
641 }
642
643 /* 
644  * get_end_bb:
645  *
646  *   Return the last LLVM bblock corresponding to BB.
647  * This might not be equal to the bb returned by get_bb () since we need to generate
648  * multiple LLVM bblocks for a mono bblock to handle throwing exceptions.
649  */
650 static LLVMBasicBlockRef
651 get_end_bb (EmitContext *ctx, MonoBasicBlock *bb)
652 {
653         get_bb (ctx, bb);
654         return ctx->bblocks [bb->block_num].end_bblock;
655 }
656
657 static LLVMBasicBlockRef
658 gen_bb (EmitContext *ctx, const char *prefix)
659 {
660         char bb_name [128];
661
662         sprintf (bb_name, "%s%d", prefix, ++ ctx->ex_index);
663         return LLVMAppendBasicBlock (ctx->lmethod, bb_name);
664 }
665
666 /*
667  * resolve_patch:
668  *
669  *   Return the target of the patch identified by TYPE and TARGET.
670  */
671 static gpointer
672 resolve_patch (MonoCompile *cfg, MonoJumpInfoType type, gconstpointer target)
673 {
674         MonoJumpInfo ji;
675
676         memset (&ji, 0, sizeof (ji));
677         ji.type = type;
678         ji.data.target = target;
679
680         return mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &ji, FALSE);
681 }
682
683 /*
684  * convert_full:
685  *
686  *   Emit code to convert the LLVM value V to DTYPE.
687  */
688 static LLVMValueRef
689 convert_full (EmitContext *ctx, LLVMValueRef v, LLVMTypeRef dtype, gboolean is_unsigned)
690 {
691         LLVMTypeRef stype = LLVMTypeOf (v);
692
693         if (stype != dtype) {
694                 gboolean ext = FALSE;
695
696                 /* Extend */
697                 if (dtype == LLVMInt64Type () && (stype == LLVMInt32Type () || stype == LLVMInt16Type () || stype == LLVMInt8Type ()))
698                         ext = TRUE;
699                 else if (dtype == LLVMInt32Type () && (stype == LLVMInt16Type () || stype == LLVMInt8Type ()))
700                         ext = TRUE;
701                 else if (dtype == LLVMInt16Type () && (stype == LLVMInt8Type ()))
702                         ext = TRUE;
703
704                 if (ext)
705                         return is_unsigned ? LLVMBuildZExt (ctx->builder, v, dtype, "") : LLVMBuildSExt (ctx->builder, v, dtype, "");
706
707                 if (dtype == LLVMDoubleType () && stype == LLVMFloatType ())
708                         return LLVMBuildFPExt (ctx->builder, v, dtype, "");
709
710                 /* Trunc */
711                 if (stype == LLVMInt64Type () && (dtype == LLVMInt32Type () || dtype == LLVMInt16Type () || dtype == LLVMInt8Type ()))
712                         return LLVMBuildTrunc (ctx->builder, v, dtype, "");
713                 if (stype == LLVMInt32Type () && (dtype == LLVMInt16Type () || dtype == LLVMInt8Type ()))
714                         return LLVMBuildTrunc (ctx->builder, v, dtype, "");
715                 if (stype == LLVMDoubleType () && dtype == LLVMFloatType ())
716                         return LLVMBuildFPTrunc (ctx->builder, v, dtype, "");
717
718                 if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind && LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
719                         return LLVMBuildBitCast (ctx->builder, v, dtype, "");
720                 if (LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
721                         return LLVMBuildIntToPtr (ctx->builder, v, dtype, "");
722                 if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind)
723                         return LLVMBuildPtrToInt (ctx->builder, v, dtype, "");
724
725 #ifdef MONO_ARCH_SOFT_FLOAT
726                 if (stype == LLVMInt32Type () && dtype == LLVMFloatType ())
727                         return LLVMBuildBitCast (ctx->builder, v, dtype, "");
728                 if (stype == LLVMInt32Type () && dtype == LLVMDoubleType ())
729                         return LLVMBuildBitCast (ctx->builder, LLVMBuildZExt (ctx->builder, v, LLVMInt64Type (), ""), dtype, "");
730 #endif
731
732                 LLVMDumpValue (v);
733                 LLVMDumpValue (LLVMConstNull (dtype));
734                 g_assert_not_reached ();
735                 return NULL;
736         } else {
737                 return v;
738         }
739 }
740
741 static LLVMValueRef
742 convert (EmitContext *ctx, LLVMValueRef v, LLVMTypeRef dtype)
743 {
744         return convert_full (ctx, v, dtype, FALSE);
745 }
746
747 /*
748  * emit_volatile_load:
749  *
750  *   If vreg is volatile, emit a load from its address.
751  */
752 static LLVMValueRef
753 emit_volatile_load (EmitContext *ctx, int vreg)
754 {
755         MonoType *t;
756
757         LLVMValueRef v = LLVMBuildLoad (ctx->builder, ctx->addresses [vreg], "");
758         t = ctx->vreg_cli_types [vreg];
759         if (t && !t->byref) {
760                 /* 
761                  * Might have to zero extend since llvm doesn't have 
762                  * unsigned types.
763                  */
764                 if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_U2)
765                         v = LLVMBuildZExt (ctx->builder, v, LLVMInt32Type (), "");
766                 else if (t->type == MONO_TYPE_U8)
767                         v = LLVMBuildZExt (ctx->builder, v, LLVMInt64Type (), "");
768         }
769
770         return v;
771 }
772
773 /*
774  * emit_volatile_store:
775  *
776  *   If VREG is volatile, emit a store from its value to its address.
777  */
778 static void
779 emit_volatile_store (EmitContext *ctx, int vreg)
780 {
781         MonoInst *var = get_vreg_to_inst (ctx->cfg, vreg);
782
783         if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
784                 g_assert (ctx->addresses [vreg]);
785                 LLVMBuildStore (ctx->builder, convert (ctx, ctx->values [vreg], type_to_llvm_type (ctx, var->inst_vtype)), ctx->addresses [vreg]);
786         }
787 }
788
789 /*
790  * sig_to_llvm_sig:
791  *
792  *   Return the LLVM signature corresponding to the mono signature SIG using the
793  * calling convention information in CINFO.
794  */
795 static LLVMTypeRef
796 sig_to_llvm_sig (EmitContext *ctx, MonoMethodSignature *sig, LLVMCallInfo *cinfo)
797 {
798         LLVMTypeRef ret_type;
799         LLVMTypeRef *param_types = NULL;
800         LLVMTypeRef res;
801         int i, j, pindex;
802         gboolean vretaddr = FALSE;
803
804         ret_type = type_to_llvm_type (ctx, sig->ret);
805         CHECK_FAILURE (ctx);
806
807         if (cinfo && cinfo->ret.storage == LLVMArgVtypeInReg) {
808                 /* LLVM models this by returning an aggregate value */
809                 if (cinfo->ret.pair_storage [0] == LLVMArgInIReg && cinfo->ret.pair_storage [1] == LLVMArgNone) {
810                         LLVMTypeRef members [2];
811
812                         members [0] = IntPtrType ();
813                         ret_type = LLVMStructType (members, 1, FALSE);
814                 } else {
815                         g_assert_not_reached ();
816                 }
817         } else if (cinfo && MONO_TYPE_ISSTRUCT (sig->ret)) {
818                 g_assert (cinfo->ret.storage == LLVMArgVtypeRetAddr);
819                 vretaddr = TRUE;
820         }
821
822         param_types = g_new0 (LLVMTypeRef, (sig->param_count * 2) + 2);
823         pindex = 0;
824         if (vretaddr) {
825                 ret_type = LLVMVoidType ();
826                 param_types [pindex ++] = IntPtrType ();
827         }
828         if (sig->hasthis)
829                 param_types [pindex ++] = IntPtrType ();
830         for (i = 0; i < sig->param_count; ++i) {
831                 if (cinfo && cinfo->args [i + sig->hasthis].storage == LLVMArgVtypeInReg) {
832                         for (j = 0; j < 2; ++j) {
833                                 switch (cinfo->args [i + sig->hasthis].pair_storage [j]) {
834                                 case LLVMArgInIReg:
835                                         param_types [pindex ++] = LLVMIntType (sizeof (gpointer) * 8);
836                                         break;
837                                 case LLVMArgNone:
838                                         break;
839                                 default:
840                                         g_assert_not_reached ();
841                                 }
842                         }
843                 } else if (cinfo && cinfo->args [i + sig->hasthis].storage == LLVMArgVtypeByVal) {
844                         param_types [pindex] = type_to_llvm_arg_type (ctx, sig->params [i]);
845                         CHECK_FAILURE (ctx);
846                         param_types [pindex] = LLVMPointerType (param_types [pindex], 0);
847                         pindex ++;
848                 } else {
849                         param_types [pindex ++] = type_to_llvm_arg_type (ctx, sig->params [i]);
850                 }                       
851         }
852         CHECK_FAILURE (ctx);
853
854         res = LLVMFunctionType (ret_type, param_types, pindex, FALSE);
855         g_free (param_types);
856
857         return res;
858
859  FAILURE:
860         g_free (param_types);
861
862         return NULL;
863 }
864
865 /*
866  * LLVMFunctionType1:
867  *
868  *   Create an LLVM function type from the arguments.
869  */
870 static G_GNUC_UNUSED LLVMTypeRef 
871 LLVMFunctionType1(LLVMTypeRef ReturnType,
872                                   LLVMTypeRef ParamType1,
873                                   int IsVarArg)
874 {
875         LLVMTypeRef param_types [1];
876
877         param_types [0] = ParamType1;
878
879         return LLVMFunctionType (ReturnType, param_types, 1, IsVarArg);
880 }
881
882 /*
883  * LLVMFunctionType2:
884  *
885  *   Create an LLVM function type from the arguments.
886  */
887 static LLVMTypeRef 
888 LLVMFunctionType2(LLVMTypeRef ReturnType,
889                                   LLVMTypeRef ParamType1,
890                                   LLVMTypeRef ParamType2,
891                                   int IsVarArg)
892 {
893         LLVMTypeRef param_types [2];
894
895         param_types [0] = ParamType1;
896         param_types [1] = ParamType2;
897
898         return LLVMFunctionType (ReturnType, param_types, 2, IsVarArg);
899 }
900
901 /*
902  * LLVMFunctionType3:
903  *
904  *   Create an LLVM function type from the arguments.
905  */
906 static LLVMTypeRef 
907 LLVMFunctionType3(LLVMTypeRef ReturnType,
908                                   LLVMTypeRef ParamType1,
909                                   LLVMTypeRef ParamType2,
910                                   LLVMTypeRef ParamType3,
911                                   int IsVarArg)
912 {
913         LLVMTypeRef param_types [3];
914
915         param_types [0] = ParamType1;
916         param_types [1] = ParamType2;
917         param_types [2] = ParamType3;
918
919         return LLVMFunctionType (ReturnType, param_types, 3, IsVarArg);
920 }
921
922 /*
923  * create_builder:
924  *
925  *   Create an LLVM builder and remember it so it can be freed later.
926  */
927 static LLVMBuilderRef
928 create_builder (EmitContext *ctx)
929 {
930         LLVMBuilderRef builder = LLVMCreateBuilder ();
931
932         ctx->builders = g_slist_prepend_mempool (ctx->cfg->mempool, ctx->builders, builder);
933
934         return builder;
935 }
936
937 static LLVMValueRef
938 get_plt_entry (EmitContext *ctx, LLVMTypeRef llvm_sig, MonoJumpInfoType type, gconstpointer data)
939 {
940         char *callee_name = mono_aot_get_plt_symbol (type, data);
941         LLVMValueRef callee;
942
943         if (!callee_name)
944                 return NULL;
945
946         // FIXME: Locking
947         callee = g_hash_table_lookup (ctx->lmodule->plt_entries, callee_name);
948         if (!callee) {
949                 callee = LLVMAddFunction (ctx->module, callee_name, llvm_sig);
950
951                 LLVMSetVisibility (callee, LLVMHiddenVisibility);
952
953                 g_hash_table_insert (ctx->lmodule->plt_entries, (char*)callee_name, callee);
954         }
955
956         return callee;
957 }
958
959 static void
960 emit_cond_throw_pos (EmitContext *ctx)
961 {
962 }
963
964 /*
965  * emit_call:
966  *
967  *   Emit an LLVM call or invoke instruction depending on whenever the call is inside
968  * a try region.
969  */
970 static LLVMValueRef
971 emit_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, LLVMValueRef callee, LLVMValueRef *args, int pindex)
972 {
973         MonoCompile *cfg = ctx->cfg;
974         LLVMValueRef lcall;
975         LLVMBuilderRef builder = *builder_ref;
976
977         // FIXME: Nested clauses
978         if (bb->region != -1 && MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY)) {
979                 MonoMethodHeader *header = mono_method_get_header (cfg->method);
980                 // FIXME: Add a macro for this
981                 int clause_index = (bb->region >> 8) - 1;
982                 MonoExceptionClause *ec = &header->clauses [clause_index];
983                 MonoBasicBlock *tblock;
984                 LLVMBasicBlockRef ex_bb, noex_bb;
985
986                 /*
987                  * Have to use an invoke instead of a call, branching to the
988                  * handler bblock of the clause containing this bblock.
989                  */
990
991                 g_assert (ec->flags == MONO_EXCEPTION_CLAUSE_NONE || ec->flags == MONO_EXCEPTION_CLAUSE_FINALLY);
992
993                 tblock = cfg->cil_offset_to_bb [ec->handler_offset];
994                 g_assert (tblock);
995
996                 ctx->bblocks [tblock->block_num].invoke_target = TRUE;
997
998                 ex_bb = get_bb (ctx, tblock);
999
1000                 noex_bb = gen_bb (ctx, "NOEX_BB");
1001
1002                 /* Use an invoke */
1003                 lcall = LLVMBuildInvoke (builder, callee, args, pindex, noex_bb, ex_bb, "");
1004
1005                 builder = ctx->builder = create_builder (ctx);
1006                 LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
1007
1008                 ctx->bblocks [bb->block_num].end_bblock = noex_bb;
1009         } else {
1010                 lcall = LLVMBuildCall (builder, callee, args, pindex, "");
1011                 ctx->builder = builder;
1012         }
1013
1014         *builder_ref = ctx->builder;
1015
1016         return lcall;
1017 }
1018
1019 /*
1020  * emit_cond_system_exception:
1021  *
1022  *   Emit code to throw the exception EXC_TYPE if the condition CMP is false.
1023  */
1024 static void
1025 emit_cond_system_exception (EmitContext *ctx, MonoBasicBlock *bb, const char *exc_type, LLVMValueRef cmp)
1026 {
1027         LLVMBasicBlockRef ex_bb, noex_bb;
1028         LLVMBuilderRef builder;
1029         MonoClass *exc_class;
1030         LLVMValueRef args [2];
1031
1032         ex_bb = gen_bb (ctx, "EX_BB");
1033         noex_bb = gen_bb (ctx, "NOEX_BB");
1034
1035         LLVMBuildCondBr (ctx->builder, cmp, ex_bb, noex_bb);
1036
1037         exc_class = mono_class_from_name (mono_defaults.corlib, "System", exc_type);
1038         g_assert (exc_class);
1039
1040         /* Emit exception throwing code */
1041         builder = create_builder (ctx);
1042         LLVMPositionBuilderAtEnd (builder, ex_bb);
1043
1044         if (!ctx->lmodule->throw_corlib_exception) {
1045                 LLVMValueRef callee;
1046                 LLVMTypeRef sig;
1047
1048                 MonoMethodSignature *throw_sig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
1049                 throw_sig->ret = &mono_defaults.void_class->byval_arg;
1050                 throw_sig->params [0] = &mono_defaults.int32_class->byval_arg;
1051                 throw_sig->params [1] = &mono_defaults.int32_class->byval_arg;
1052                 sig = sig_to_llvm_sig (ctx, throw_sig, NULL);
1053
1054                 if (ctx->cfg->compile_aot) {
1055                         callee = get_plt_entry (ctx, sig, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception");
1056                 } else {
1057                         callee = LLVMAddFunction (ctx->module, "throw_corlib_exception", sig_to_llvm_sig (ctx, throw_sig, NULL));
1058
1059 #ifdef TARGET_X86 
1060                         /* 
1061                          * LLVM generated code doesn't push the arguments, so we need another
1062                          * throw trampoline.
1063                          */
1064                         LLVMAddGlobalMapping (ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_llvm_throw_corlib_exception"));
1065 #else
1066                         LLVMAddGlobalMapping (ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception"));
1067 #endif
1068                 }
1069
1070                 mono_memory_barrier ();
1071                 ctx->lmodule->throw_corlib_exception = callee;
1072         }
1073
1074 #ifdef TARGET_X86
1075         args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token - MONO_TOKEN_TYPE_DEF, FALSE);
1076 #else
1077         args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token, FALSE);
1078 #endif
1079         /*
1080          * FIXME: The offset is 0, this is not a problem for exception handling
1081          * in general, because we don't llvm compile methods with handlers, its only
1082          * a problem for line numbers in stack traces.
1083          */
1084         args [1] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1085         emit_call (ctx, bb, &builder, ctx->lmodule->throw_corlib_exception, args, 2);
1086
1087         LLVMBuildUnreachable (builder);
1088
1089         ctx->builder = create_builder (ctx);
1090         LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
1091
1092         ctx->bblocks [bb->block_num].end_bblock = noex_bb;
1093
1094         ctx->ex_index ++;
1095 }
1096
1097 /*
1098  * emit_reg_to_vtype:
1099  *
1100  *   Emit code to store the vtype in the registers REGS to the address ADDRESS.
1101  */
1102 static void
1103 emit_reg_to_vtype (EmitContext *ctx, LLVMBuilderRef builder, MonoType *t, LLVMValueRef address, LLVMArgInfo *ainfo, LLVMValueRef *regs)
1104 {
1105         int j, size;
1106
1107         size = get_vtype_size (t);
1108
1109         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
1110                 address = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (LLVMInt8Type (), 0), "");
1111         }
1112
1113         for (j = 0; j < 2; ++j) {
1114                 LLVMValueRef index [2], addr;
1115                 int part_size = size > sizeof (gpointer) ? sizeof (gpointer) : size;
1116                 LLVMTypeRef part_type;
1117
1118                 if (ainfo->pair_storage [j] == LLVMArgNone)
1119                         continue;
1120
1121                 part_type = LLVMIntType (part_size * 8);
1122                 if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
1123                         index [0] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
1124                         addr = LLVMBuildGEP (builder, address, index, 1, "");
1125                 } else {
1126                         index [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1127                         index [1] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
1128                         addr = LLVMBuildGEP (builder, address, index, 2, "");
1129                 }
1130                 switch (ainfo->pair_storage [j]) {
1131                 case LLVMArgInIReg:
1132                         LLVMBuildStore (builder, convert (ctx, regs [j], part_type), LLVMBuildBitCast (ctx->builder, addr, LLVMPointerType (part_type, 0), ""));
1133                         break;
1134                 case LLVMArgNone:
1135                         break;
1136                 default:
1137                         g_assert_not_reached ();
1138                 }
1139
1140                 size -= sizeof (gpointer);
1141         }
1142 }
1143
1144 /*
1145  * emit_vtype_to_reg:
1146  *
1147  *   Emit code to load a vtype at address ADDRESS into registers. Store the registers
1148  * into REGS, and the number of registers into NREGS.
1149  */
1150 static void
1151 emit_vtype_to_reg (EmitContext *ctx, LLVMBuilderRef builder, MonoType *t, LLVMValueRef address, LLVMArgInfo *ainfo, LLVMValueRef *regs, guint32 *nregs)
1152 {
1153         int pindex = 0;
1154         int j, size;
1155
1156         size = get_vtype_size (t);
1157
1158         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
1159                 address = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (LLVMInt8Type (), 0), "");
1160         }
1161
1162         for (j = 0; j < 2; ++j) {
1163                 LLVMValueRef index [2], addr;
1164                 int partsize = size > sizeof (gpointer) ? sizeof (gpointer) : size;
1165
1166                 if (ainfo->pair_storage [j] == LLVMArgNone)
1167                         continue;
1168
1169                 if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
1170                         index [0] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
1171                         addr = LLVMBuildGEP (builder, address, index, 1, "");
1172                 } else {
1173                         index [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1174                         index [1] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);                              
1175                         addr = LLVMBuildGEP (builder, address, index, 2, "");
1176                 }
1177                 switch (ainfo->pair_storage [j]) {
1178                 case LLVMArgInIReg:
1179                         regs [pindex ++] = convert (ctx, LLVMBuildLoad (builder, LLVMBuildBitCast (ctx->builder, addr, LLVMPointerType (LLVMIntType (partsize * 8), 0), ""), ""), IntPtrType ());
1180                         break;
1181                 case LLVMArgNone:
1182                         break;
1183                 default:
1184                         g_assert_not_reached ();
1185                 }
1186                 size -= sizeof (gpointer);
1187         }
1188
1189         *nregs = pindex;
1190 }
1191
1192 static LLVMValueRef
1193 build_alloca (EmitContext *ctx, MonoType *t)
1194 {
1195         MonoClass *k = mono_class_from_mono_type (t);
1196         int align;
1197
1198         if (MONO_CLASS_IS_SIMD (ctx->cfg, k))
1199                 align = 16;
1200         else
1201                 align = mono_class_min_align (k);
1202
1203         /* Sometimes align is not a power of 2 */
1204         while (mono_is_power_of_two (align) == -1)
1205                 align ++;
1206
1207         /*
1208          * Have to place all alloca's at the end of the entry bb, since otherwise they would
1209          * get executed every time control reaches them.
1210          */
1211         LLVMPositionBuilder (ctx->alloca_builder, get_bb (ctx, ctx->cfg->bb_entry), ctx->last_alloca);
1212
1213         ctx->last_alloca = mono_llvm_build_alloca (ctx->alloca_builder, type_to_llvm_type (ctx, t), NULL, align, "");
1214         return ctx->last_alloca;
1215 }
1216
1217 /*
1218  * Put the global into the 'llvm.used' array to prevent it from being optimized away.
1219  */
1220 static void
1221 mark_as_used (LLVMModuleRef module, LLVMValueRef global)
1222 {
1223         LLVMTypeRef used_type;
1224         LLVMValueRef used, used_elem;
1225                 
1226         used_type = LLVMArrayType (LLVMPointerType (LLVMInt8Type (), 0), 1);
1227         used = LLVMAddGlobal (module, used_type, "llvm.used");
1228         used_elem = LLVMConstBitCast (global, LLVMPointerType (LLVMInt8Type (), 0));
1229         LLVMSetInitializer (used, LLVMConstArray (LLVMPointerType (LLVMInt8Type (), 0), &used_elem, 1));
1230         LLVMSetLinkage (used, LLVMAppendingLinkage);
1231         LLVMSetSection (used, "llvm.metadata");
1232 }
1233
1234 /*
1235  * emit_entry_bb:
1236  *
1237  *   Emit code to load/convert arguments.
1238  */
1239 static void
1240 emit_entry_bb (EmitContext *ctx, LLVMBuilderRef builder, int *pindexes)
1241 {
1242         int i, pindex;
1243         MonoCompile *cfg = ctx->cfg;
1244         MonoMethodSignature *sig = ctx->sig;
1245         LLVMCallInfo *linfo = ctx->linfo;
1246         MonoBasicBlock *bb;
1247
1248         ctx->alloca_builder = create_builder (ctx);
1249
1250         /*
1251          * Handle indirect/volatile variables by allocating memory for them
1252          * using 'alloca', and storing their address in a temporary.
1253          */
1254         for (i = 0; i < cfg->num_varinfo; ++i) {
1255                 MonoInst *var = cfg->varinfo [i];
1256                 LLVMTypeRef vtype;
1257
1258                 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || MONO_TYPE_ISSTRUCT (var->inst_vtype)) {
1259                         vtype = type_to_llvm_type (ctx, var->inst_vtype);
1260                         CHECK_FAILURE (ctx);
1261                         /* Could be already created by an OP_VPHI */
1262                         if (!ctx->addresses [var->dreg])
1263                                 ctx->addresses [var->dreg] = build_alloca (ctx, var->inst_vtype);
1264                         ctx->vreg_cli_types [var->dreg] = var->inst_vtype;
1265                 }
1266         }
1267
1268         for (i = 0; i < sig->param_count; ++i) {
1269                 LLVMArgInfo *ainfo = &linfo->args [i + sig->hasthis];
1270                 int reg = cfg->args [i + sig->hasthis]->dreg;
1271
1272                 if (ainfo->storage == LLVMArgVtypeInReg) {
1273                         LLVMValueRef regs [2];
1274
1275                         /* 
1276                          * Emit code to save the argument from the registers to 
1277                          * the real argument.
1278                          */
1279                         pindex = pindexes [i];
1280                         regs [0] = LLVMGetParam (ctx->lmethod, pindex);
1281                         if (ainfo->pair_storage [1] != LLVMArgNone)
1282                                 regs [1] = LLVMGetParam (ctx->lmethod, pindex + 1);
1283                         else
1284                                 regs [1] = NULL;
1285
1286                         ctx->addresses [reg] = build_alloca (ctx, sig->params [i]);
1287
1288                         emit_reg_to_vtype (ctx, builder, sig->params [i], ctx->addresses [reg], ainfo, regs);
1289
1290                         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (sig->params [i]))) {
1291                                 /* Treat these as normal values */
1292                                 ctx->values [reg] = LLVMBuildLoad (builder, ctx->addresses [reg], "");
1293                         }
1294                 } else if (ainfo->storage == LLVMArgVtypeByVal) {
1295                         ctx->addresses [reg] = LLVMGetParam (ctx->lmethod, pindexes [i]);
1296                 } else {
1297                         ctx->values [reg] = convert (ctx, ctx->values [reg], llvm_type_to_stack_type (type_to_llvm_type (ctx, sig->params [i])));
1298                 }
1299         }
1300
1301         if (cfg->vret_addr)
1302                 emit_volatile_store (ctx, cfg->vret_addr->dreg);
1303         if (sig->hasthis)
1304                 emit_volatile_store (ctx, cfg->args [0]->dreg);
1305         for (i = 0; i < sig->param_count; ++i)
1306                 if (!MONO_TYPE_ISSTRUCT (sig->params [i]))
1307                         emit_volatile_store (ctx, cfg->args [i + sig->hasthis]->dreg);
1308
1309         /*
1310          * For finally clauses, create an indicator variable telling OP_ENDFINALLY whenever
1311          * it needs to continue normally, or return back to the exception handling system.
1312          */
1313         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1314                 if (bb->region != -1 && (bb->flags & BB_EXCEPTION_HANDLER))
1315                         g_hash_table_insert (ctx->region_to_handler, GUINT_TO_POINTER (mono_get_block_region_notry (cfg, bb->region)), bb);
1316                 if (bb->region != -1 && (bb->flags & BB_EXCEPTION_HANDLER) && bb->in_scount == 0) {
1317                         LLVMValueRef val = LLVMBuildAlloca (builder, LLVMInt32Type (), "");
1318                         LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), val);
1319
1320                         ctx->bblocks [bb->block_num].finally_ind = val;
1321                 }
1322         }
1323
1324  FAILURE:
1325         ;
1326 }
1327
1328 /* Have to export this for AOT */
1329 void
1330 mono_personality (void);
1331         
1332 void
1333 mono_personality (void)
1334 {
1335         /* Not used */
1336         g_assert_not_reached ();
1337 }
1338
1339 /*
1340  * mono_llvm_emit_method:
1341  *
1342  *   Emit LLVM IL from the mono IL, and compile it to native code using LLVM.
1343  */
1344 void
1345 mono_llvm_emit_method (MonoCompile *cfg)
1346 {
1347         EmitContext *ctx;
1348         MonoMethodSignature *sig;
1349         MonoBasicBlock *bb;
1350         LLVMTypeRef method_type;
1351         LLVMValueRef method = NULL, debug_alias = NULL;
1352         char *method_name, *debug_name = NULL;
1353         LLVMValueRef *values, *addresses;
1354         LLVMTypeRef *vreg_types;
1355         MonoType **vreg_cli_types;
1356         int i, max_block_num, pindex, bb_index;
1357         int *pindexes = NULL;
1358         gboolean last = FALSE;
1359         GPtrArray *phi_values;
1360         LLVMCallInfo *linfo;
1361         GSList *l;
1362         LLVMModuleRef module;
1363         gboolean *is_dead;
1364         gboolean *unreachable;
1365         BBInfo *bblocks;
1366         GPtrArray *bblock_list;
1367         MonoMethodHeader *header;
1368         MonoExceptionClause *clause;
1369
1370         /* The code below might acquire the loader lock, so use it for global locking */
1371         mono_loader_lock ();
1372
1373         /* Used to communicate with the callbacks */
1374         TlsSetValue (current_cfg_tls_id, cfg);
1375
1376         ctx = g_new0 (EmitContext, 1);
1377         ctx->cfg = cfg;
1378         ctx->mempool = cfg->mempool;
1379
1380         /*
1381          * This maps vregs to the LLVM instruction defining them
1382          */
1383         values = g_new0 (LLVMValueRef, cfg->next_vreg);
1384         /*
1385          * This maps vregs for volatile variables to the LLVM instruction defining their
1386          * address.
1387          */
1388         addresses = g_new0 (LLVMValueRef, cfg->next_vreg);
1389         vreg_types = g_new0 (LLVMTypeRef, cfg->next_vreg);
1390         vreg_cli_types = g_new0 (MonoType*, cfg->next_vreg);
1391         phi_values = g_ptr_array_new ();
1392         /* 
1393          * This signals whenever the vreg was defined by a phi node with no input vars
1394          * (i.e. all its input bblocks end with NOT_REACHABLE).
1395          */
1396         is_dead = g_new0 (gboolean, cfg->next_vreg);
1397         /* Whenever the bblock is unreachable */
1398         unreachable = g_new0 (gboolean, cfg->max_block_num);
1399
1400         bblock_list = g_ptr_array_new ();
1401
1402         ctx->values = values;
1403         ctx->addresses = addresses;
1404         ctx->vreg_cli_types = vreg_cli_types;
1405         ctx->region_to_handler = g_hash_table_new (NULL, NULL);
1406  
1407         if (cfg->compile_aot) {
1408                 ctx->lmodule = &aot_module;
1409                 method_name = mono_aot_get_method_name (cfg);
1410                 debug_name = mono_aot_get_method_debug_name (cfg);
1411         } else {
1412                 init_jit_module ();
1413                 ctx->lmodule = &jit_module;
1414                 method_name = mono_method_full_name (cfg->method, TRUE);
1415                 debug_name = NULL;
1416         }
1417         
1418         module = ctx->module = ctx->lmodule->module;
1419
1420 #if 1
1421         {
1422                 static int count = 0;
1423                 count ++;
1424
1425                 if (getenv ("LLVM_COUNT")) {
1426                         if (count == atoi (getenv ("LLVM_COUNT"))) {
1427                                 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
1428                                 last = TRUE;
1429                         }
1430                         if (count > atoi (getenv ("LLVM_COUNT")))
1431                                 LLVM_FAILURE (ctx, "");
1432                 }
1433         }
1434 #endif
1435
1436         sig = mono_method_signature (cfg->method);
1437         ctx->sig = sig;
1438
1439         linfo = mono_arch_get_llvm_call_info (cfg, sig);
1440         ctx->linfo = linfo;
1441         CHECK_FAILURE (ctx);
1442
1443         method_type = sig_to_llvm_sig (ctx, sig, linfo);
1444         CHECK_FAILURE (ctx);
1445
1446         method = LLVMAddFunction (module, method_name, method_type);
1447         ctx->lmethod = method;
1448
1449         LLVMSetLinkage (method, LLVMPrivateLinkage);
1450
1451         if (cfg->method->save_lmf)
1452                 LLVM_FAILURE (ctx, "lmf");
1453
1454         if (sig->pinvoke)
1455                 LLVM_FAILURE (ctx, "pinvoke signature");
1456
1457         header = mono_method_get_header (cfg->method);
1458         for (i = 0; i < header->num_clauses; ++i) {
1459                 clause = &header->clauses [i];
1460                 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_NONE)
1461                         LLVM_FAILURE (ctx, "non-finally/catch clause.");
1462         }
1463
1464         /* 
1465          * This maps parameter indexes in the original signature to the indexes in
1466          * the LLVM signature.
1467          */
1468         pindexes = g_new0 (int, sig->param_count);
1469         pindex = 0;
1470         if (cfg->vret_addr) {
1471                 values [cfg->vret_addr->dreg] = LLVMGetParam (method, pindex);
1472                 pindex ++;
1473         }
1474         if (sig->hasthis) {
1475                 values [cfg->args [0]->dreg] = LLVMGetParam (method, pindex);
1476                 pindex ++;
1477         }
1478         for (i = 0; i < sig->param_count; ++i) {
1479                 values [cfg->args [i + sig->hasthis]->dreg] = LLVMGetParam (method, pindex);
1480                 pindexes [i] = pindex;
1481                 if (linfo->args [i + sig->hasthis].storage == LLVMArgVtypeInReg) {
1482                         if (linfo->args [i + sig->hasthis].pair_storage [0] != LLVMArgNone)
1483                                 pindex ++;
1484                         if (linfo->args [i + sig->hasthis].pair_storage [1] != LLVMArgNone)
1485                                 pindex ++;
1486                 } else if (linfo->args [i + sig->hasthis].storage == LLVMArgVtypeByVal) {
1487                         LLVMAddAttribute (LLVMGetParam (method, pindex), LLVMByValAttribute);
1488                         pindex ++;
1489                 } else {
1490                         pindex ++;
1491                 }
1492         }
1493
1494         max_block_num = 0;
1495         for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
1496                 max_block_num = MAX (max_block_num, bb->block_num);
1497         ctx->bblocks = bblocks = g_new0 (BBInfo, max_block_num + 1);
1498
1499         /* Add branches between non-consecutive bblocks */
1500         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1501                 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
1502                         bb->next_bb != bb->last_ins->inst_false_bb) {
1503                         
1504                         MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
1505                         inst->opcode = OP_BR;
1506                         inst->inst_target_bb = bb->last_ins->inst_false_bb;
1507                         mono_bblock_add_inst (bb, inst);
1508                 }
1509         }
1510
1511         /*
1512          * Make a first pass over the code to precreate PHI nodes.
1513          */
1514         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1515                 MonoInst *ins;
1516                 LLVMBuilderRef builder;
1517                 char *dname;
1518                 char dname_buf[128];
1519
1520                 builder = create_builder (ctx);
1521
1522                 for (ins = bb->code; ins; ins = ins->next) {
1523                         switch (ins->opcode) {
1524                         case OP_PHI:
1525                         case OP_FPHI:
1526                         case OP_VPHI:
1527                         case OP_XPHI: {
1528                                 LLVMTypeRef phi_type = llvm_type_to_stack_type (type_to_llvm_type (ctx, &ins->klass->byval_arg));
1529
1530                                 CHECK_FAILURE (ctx);
1531
1532                                 if (ins->opcode == OP_VPHI) {
1533                                         /* Treat valuetype PHI nodes as operating on the address itself */
1534                                         g_assert (ins->klass);
1535                                         phi_type = LLVMPointerType (type_to_llvm_type (ctx, &ins->klass->byval_arg), 0);
1536                                 }
1537
1538                                 /* 
1539                                  * Have to precreate these, as they can be referenced by
1540                                  * earlier instructions.
1541                                  */
1542                                 sprintf (dname_buf, "t%d", ins->dreg);
1543                                 dname = dname_buf;
1544                                 values [ins->dreg] = LLVMBuildPhi (builder, phi_type, dname);
1545
1546                                 if (ins->opcode == OP_VPHI)
1547                                         addresses [ins->dreg] = values [ins->dreg];
1548
1549                                 g_ptr_array_add (phi_values, values [ins->dreg]);
1550
1551                                 /* 
1552                                  * Set the expected type of the incoming arguments since these have
1553                                  * to have the same type.
1554                                  */
1555                                 for (i = 0; i < ins->inst_phi_args [0]; i++) {
1556                                         int sreg1 = ins->inst_phi_args [i + 1];
1557                                         
1558                                         if (sreg1 != -1)
1559                                                 vreg_types [sreg1] = phi_type;
1560                                 }
1561                                 break;
1562                                 }
1563                         default:
1564                                 break;
1565                         }
1566                 }
1567         }
1568
1569         /* 
1570          * Create an ordering for bblocks, use the depth first order first, then
1571          * put the exception handling bblocks last.
1572          */
1573         for (bb_index = 0; bb_index < cfg->num_bblocks; ++bb_index) {
1574                 bb = cfg->bblocks [bb_index];
1575                 if (!(bb->region != -1 && !MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))) {
1576                         g_ptr_array_add (bblock_list, bb);
1577                         bblocks [bb->block_num].added = TRUE;
1578                 }
1579         }
1580
1581         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1582                 if (!bblocks [bb->block_num].added)
1583                         g_ptr_array_add (bblock_list, bb);
1584         }
1585
1586         /*
1587          * Second pass: generate code.
1588          */
1589         for (bb_index = 0; bb_index < bblock_list->len; ++bb_index) {
1590                 MonoInst *ins;
1591                 LLVMBasicBlockRef cbb;
1592                 LLVMBuilderRef builder;
1593                 gboolean has_terminator;
1594                 LLVMValueRef v;
1595                 LLVMValueRef lhs, rhs;
1596
1597                 bb = g_ptr_array_index (bblock_list, bb_index);
1598
1599                 if (!(bb == cfg->bb_entry || bb->in_count > 0))
1600                         continue;
1601
1602                 cbb = get_bb (ctx, bb);
1603                 builder = create_builder (ctx);
1604                 ctx->builder = builder;
1605                 LLVMPositionBuilderAtEnd (builder, cbb);
1606
1607                 if (bb == cfg->bb_entry)
1608                         emit_entry_bb (ctx, builder, pindexes);
1609                 CHECK_FAILURE (ctx);
1610
1611                 if (bb->flags & BB_EXCEPTION_HANDLER) {
1612                         LLVMTypeRef i8ptr;
1613                         LLVMValueRef eh_selector, eh_exception, personality, args [4];
1614                         MonoInst *exvar;
1615                         static gint32 mapping_inited;
1616                         static int ti_generator;
1617                         char ti_name [128];
1618                         MonoClass **ti;
1619                         LLVMValueRef type_info;
1620                         int clause_index;
1621
1622                         if (!bblocks [bb->block_num].invoke_target) {
1623                                 /*
1624                                  * LLVM asserts if llvm.eh.selector is called from a bblock which
1625                                  * doesn't have an invoke pointing at it.
1626                                  */
1627                                 LLVM_FAILURE (ctx, "handler without invokes");
1628                         }
1629
1630                         eh_selector = LLVMGetNamedFunction (module, "llvm.eh.selector");
1631
1632                         if (cfg->compile_aot) {
1633                                 /* Use a dummy personality function */
1634                                 personality = LLVMGetNamedFunction (module, "mono_aot_personality");
1635                                 g_assert (personality);
1636                         } else {
1637                                 personality = LLVMGetNamedFunction (module, "mono_personality");
1638                                 if (InterlockedCompareExchange (&mapping_inited, 1, 0) == 0)
1639                                         LLVMAddGlobalMapping (ee, personality, mono_personality);
1640                         }
1641
1642                         i8ptr = LLVMPointerType (LLVMInt8Type (), 0);
1643
1644                         clause_index = (mono_get_block_region_notry (cfg, bb->region) >> 8) - 1;
1645
1646                         /*
1647                          * Create the type info
1648                          */
1649                         sprintf (ti_name, "type_info_%d", ti_generator);
1650                         ti_generator ++;
1651
1652                         if (cfg->compile_aot) {
1653                                 /* decode_eh_frame () in aot-runtime.c will decode this */
1654                                 type_info = LLVMAddGlobal (module, LLVMInt32Type (), ti_name);
1655                                 LLVMSetInitializer (type_info, LLVMConstInt (LLVMInt32Type (), clause_index, FALSE));
1656
1657                                 LLVMSetLinkage (type_info, LLVMPrivateLinkage);
1658                                 LLVMSetVisibility (type_info, LLVMHiddenVisibility);
1659
1660                                 /* 
1661                                  * FIXME: llc currently generates incorrect data in the LSDA:
1662                                  *      .byte   0x9B                                        # @TType format (indirect pcrel sdata4)
1663                                  * and later:
1664                                  * .quad        type_info_1                                 # TypeInfo
1665                                  */
1666                                 LLVM_FAILURE (ctx, "aot+clauses");
1667                         } else {
1668                                 /* exception_cb will decode this */
1669                                 ti = g_malloc (sizeof (gint32));
1670                                 *(gint32*)ti = clause_index;
1671
1672                                 type_info = LLVMAddGlobal (module, i8ptr, ti_name);
1673
1674                                 LLVMAddGlobalMapping (ee, type_info, ti);
1675                         }
1676
1677                         args [0] = LLVMConstNull (i8ptr);
1678                         args [1] = LLVMConstBitCast (personality, i8ptr);
1679                         args [2] = type_info;
1680                         LLVMBuildCall (builder, eh_selector, args, 3, "");
1681
1682                         /* Store the exception into the exvar */
1683                         if (bb->in_scount == 1) {
1684                                 g_assert (bb->in_scount == 1);
1685                                 exvar = bb->in_stack [0];
1686
1687                                 eh_exception = LLVMGetNamedFunction (module, "llvm.eh.exception");
1688
1689                                 // FIXME: This is shared with filter clauses ?
1690                                 g_assert (!values [exvar->dreg]);
1691                                 values [exvar->dreg] = LLVMBuildCall (builder, eh_exception, NULL, 0, "");
1692                                 emit_volatile_store (ctx, exvar->dreg);
1693                         }
1694                 }
1695
1696                 has_terminator = FALSE;
1697                 for (ins = bb->code; ins; ins = ins->next) {
1698                         const char *spec = LLVM_INS_INFO (ins->opcode);
1699                         char *dname = NULL;
1700                         char dname_buf [128];
1701
1702                         if (has_terminator)
1703                                 /* There could be instructions after a terminator, skip them */
1704                                 break;
1705
1706                         if (spec [MONO_INST_DEST] != ' ' && !MONO_IS_STORE_MEMBASE (ins)) {
1707                                 sprintf (dname_buf, "t%d", ins->dreg);
1708                                 dname = dname_buf;
1709                         }
1710
1711                         if (spec [MONO_INST_SRC1] != ' ' && spec [MONO_INST_SRC1] != 'v') {
1712                                 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
1713
1714                                 if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
1715                                         lhs = emit_volatile_load (ctx, ins->sreg1);
1716                                 } else {
1717                                         /* It is ok for SETRET to have an uninitialized argument */
1718                                         if (!values [ins->sreg1] && ins->opcode != OP_SETRET)
1719                                                 LLVM_FAILURE (ctx, "sreg1");
1720                                         lhs = values [ins->sreg1];
1721                                 }
1722                         } else {
1723                                 lhs = NULL;
1724                         }
1725
1726                         if (spec [MONO_INST_SRC2] != ' ' && spec [MONO_INST_SRC2] != ' ') {
1727                                 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
1728                                 if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
1729                                         rhs = emit_volatile_load (ctx, ins->sreg2);
1730                                 } else {
1731                                         if (!values [ins->sreg2])
1732                                                 LLVM_FAILURE (ctx, "sreg2");
1733                                         rhs = values [ins->sreg2];
1734                                 }
1735                         } else {
1736                                 rhs = NULL;
1737                         }
1738
1739                         //mono_print_ins (ins);
1740                         switch (ins->opcode) {
1741                         case OP_NOP:
1742                         case OP_NOT_NULL:
1743                         case OP_LIVERANGE_START:
1744                         case OP_LIVERANGE_END:
1745                                 break;
1746                         case OP_ICONST:
1747                                 values [ins->dreg] = LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE);
1748                                 break;
1749                         case OP_I8CONST:
1750 #if SIZEOF_VOID_P == 4
1751                                 values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
1752 #else
1753                                 values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), (gint64)ins->inst_c0, FALSE);
1754 #endif
1755                                 break;
1756                         case OP_R8CONST:
1757                                 values [ins->dreg] = LLVMConstReal (LLVMDoubleType (), *(double*)ins->inst_p0);
1758                                 break;
1759                         case OP_R4CONST:
1760                                 values [ins->dreg] = LLVMConstFPExt (LLVMConstReal (LLVMFloatType (), *(float*)ins->inst_p0), LLVMDoubleType ());
1761                                 break;
1762                         case OP_BR:
1763                                 LLVMBuildBr (builder, get_bb (ctx, ins->inst_target_bb));
1764                                 has_terminator = TRUE;
1765                                 break;
1766                         case OP_SWITCH: {
1767                                 int i;
1768                                 LLVMValueRef v;
1769                                 char bb_name [128];
1770                                 LLVMBasicBlockRef new_bb;
1771                                 LLVMBuilderRef new_builder;
1772
1773                                 // The default branch is already handled
1774                                 // FIXME: Handle it here
1775
1776                                 /* Start new bblock */
1777                                 sprintf (bb_name, "SWITCH_DEFAULT_BB%d", ctx->default_index ++);
1778                                 new_bb = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
1779
1780                                 lhs = convert (ctx, lhs, LLVMInt32Type ());
1781                                 v = LLVMBuildSwitch (builder, lhs, new_bb, GPOINTER_TO_UINT (ins->klass));
1782                                 for (i = 0; i < GPOINTER_TO_UINT (ins->klass); ++i) {
1783                                         MonoBasicBlock *target_bb = ins->inst_many_bb [i];
1784
1785                                         LLVMAddCase (v, LLVMConstInt (LLVMInt32Type (), i, FALSE), get_bb (ctx, target_bb));
1786                                 }
1787
1788                                 new_builder = create_builder (ctx);
1789                                 LLVMPositionBuilderAtEnd (new_builder, new_bb);
1790                                 LLVMBuildUnreachable (new_builder);
1791
1792                                 has_terminator = TRUE;
1793                                 g_assert (!ins->next);
1794                                 
1795                                 break;
1796                         }
1797
1798                         case OP_SETRET:
1799                                 if (linfo->ret.storage == LLVMArgVtypeInReg) {
1800                                         LLVMTypeRef ret_type = LLVMGetReturnType (LLVMGetElementType (LLVMTypeOf (method)));
1801                                         LLVMValueRef part1, retval;
1802                                         int size;
1803
1804                                         size = get_vtype_size (sig->ret);
1805
1806                                         g_assert (addresses [ins->sreg1]);
1807
1808                                         g_assert (linfo->ret.pair_storage [0] == LLVMArgInIReg);
1809                                         g_assert (linfo->ret.pair_storage [1] == LLVMArgNone);
1810                                         
1811                                         part1 = convert (ctx, LLVMBuildLoad (builder, LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMIntType (size * 8), 0), ""), ""), IntPtrType ());
1812
1813                                         retval = LLVMBuildInsertValue (builder, LLVMGetUndef (ret_type), part1, 0, "");
1814
1815                                         LLVMBuildRet (builder, retval);
1816                                         break;
1817                                 }
1818
1819                                 if (linfo->ret.storage == LLVMArgVtypeRetAddr) {
1820                                         LLVMBuildRetVoid (builder);
1821                                         break;
1822                                 }
1823
1824                                 if (!lhs || is_dead [ins->sreg1]) {
1825                                         /* 
1826                                          * The method did not set its return value, probably because it
1827                                          * ends with a throw.
1828                                          */
1829                                         if (cfg->vret_addr)
1830                                                 LLVMBuildRetVoid (builder);
1831                                         else
1832                                                 LLVMBuildRet (builder, LLVMConstNull (type_to_llvm_type (ctx, sig->ret)));
1833                                 } else {
1834                                         LLVMBuildRet (builder, convert (ctx, lhs, type_to_llvm_type (ctx, sig->ret)));
1835                                 }
1836                                 has_terminator = TRUE;
1837                                 break;
1838                         case OP_ICOMPARE:
1839                         case OP_FCOMPARE:
1840                         case OP_LCOMPARE:
1841                         case OP_COMPARE:
1842                         case OP_ICOMPARE_IMM:
1843                         case OP_LCOMPARE_IMM:
1844                         case OP_COMPARE_IMM:
1845 #ifdef TARGET_AMD64
1846                         case OP_AMD64_ICOMPARE_MEMBASE_REG:
1847                         case OP_AMD64_ICOMPARE_MEMBASE_IMM:
1848 #endif
1849 #ifdef TARGET_X86
1850                         case OP_X86_COMPARE_MEMBASE_REG:
1851                         case OP_X86_COMPARE_MEMBASE_IMM:
1852 #endif
1853                         {
1854                                 CompRelation rel;
1855                                 LLVMValueRef cmp;
1856
1857                                 if (ins->next->opcode == OP_NOP)
1858                                         break;
1859
1860                                 if (ins->next->opcode == OP_BR)
1861                                         /* The comparison result is not needed */
1862                                         continue;
1863
1864                                 rel = mono_opcode_to_cond (ins->next->opcode);
1865
1866                                 /* Used for implementing bound checks */
1867 #ifdef TARGET_AMD64
1868                                 if ((ins->opcode == OP_AMD64_ICOMPARE_MEMBASE_REG) || (ins->opcode == OP_AMD64_ICOMPARE_MEMBASE_IMM)) {
1869                                         int size = 4;
1870                                         LLVMValueRef index;
1871                                         LLVMTypeRef t;
1872
1873                                         t = LLVMInt32Type ();
1874
1875                                         g_assert (ins->inst_offset % size == 0);
1876                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
1877
1878                                         lhs = LLVMBuildLoad (builder, LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (t, 0)), &index, 1, ""), "");
1879                                 }
1880                                 if (ins->opcode == OP_AMD64_ICOMPARE_MEMBASE_IMM) {
1881                                         lhs = convert (ctx, lhs, LLVMInt32Type ());
1882                                         rhs = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
1883                                 }
1884                                 if (ins->opcode == OP_AMD64_ICOMPARE_MEMBASE_REG)
1885                                         rhs = convert (ctx, rhs, LLVMInt32Type ());
1886 #endif
1887
1888 #ifdef TARGET_X86
1889                                 if ((ins->opcode == OP_X86_COMPARE_MEMBASE_REG) || (ins->opcode == OP_X86_COMPARE_MEMBASE_IMM)) {
1890                                         int size = 4;
1891                                         LLVMValueRef index;
1892                                         LLVMTypeRef t;
1893
1894                                         t = LLVMInt32Type ();
1895
1896                                         g_assert (ins->inst_offset % size == 0);
1897                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
1898
1899                                         lhs = LLVMBuildLoad (builder, LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (t, 0)), &index, 1, ""), "");
1900                                 }
1901                                 if (ins->opcode == OP_X86_COMPARE_MEMBASE_IMM) {
1902                                         lhs = convert (ctx, lhs, LLVMInt32Type ());
1903                                         rhs = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
1904                                 }
1905                                 if (ins->opcode == OP_X86_COMPARE_MEMBASE_REG)
1906                                         rhs = convert (ctx, rhs, LLVMInt32Type ());
1907 #endif
1908
1909                                 if (ins->opcode == OP_ICOMPARE_IMM) {
1910                                         lhs = convert (ctx, lhs, LLVMInt32Type ());
1911                                         rhs = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
1912                                 }
1913                                 if (ins->opcode == OP_LCOMPARE_IMM) {
1914                                         lhs = convert (ctx, lhs, LLVMInt64Type ());
1915                                         rhs = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
1916                                 }
1917                                 if (ins->opcode == OP_LCOMPARE) {
1918                                         lhs = convert (ctx, lhs, LLVMInt64Type ());
1919                                         rhs = convert (ctx, rhs, LLVMInt64Type ());
1920                                 }
1921                                 if (ins->opcode == OP_ICOMPARE) {
1922                                         lhs = convert (ctx, lhs, LLVMInt32Type ());
1923                                         rhs = convert (ctx, rhs, LLVMInt32Type ());
1924                                 }
1925
1926                                 if (lhs && rhs) {
1927                                         if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
1928                                                 rhs = convert (ctx, rhs, LLVMTypeOf (lhs));
1929                                         else if (LLVMGetTypeKind (LLVMTypeOf (rhs)) == LLVMPointerTypeKind)
1930                                                 lhs = convert (ctx, lhs, LLVMTypeOf (rhs));
1931                                 }
1932
1933                                 /* We use COMPARE+SETcc/Bcc, llvm uses SETcc+br cond */
1934                                 if (ins->opcode == OP_FCOMPARE)
1935                                         cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMDoubleType ()), convert (ctx, rhs, LLVMDoubleType ()), "");
1936                                 else if (ins->opcode == OP_COMPARE_IMM)
1937                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), "");
1938                                 else if (ins->opcode == OP_COMPARE)
1939                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, IntPtrType ()), convert (ctx, rhs, IntPtrType ()), "");
1940                                 else
1941                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], lhs, rhs, "");
1942
1943                                 if (MONO_IS_COND_BRANCH_OP (ins->next)) {
1944                                         LLVMBuildCondBr (builder, cmp, get_bb (ctx, ins->next->inst_true_bb), get_bb (ctx, ins->next->inst_false_bb));
1945                                         has_terminator = TRUE;
1946                                 } else if (MONO_IS_SETCC (ins->next)) {
1947                                         sprintf (dname_buf, "t%d", ins->next->dreg);
1948                                         dname = dname_buf;
1949                                         values [ins->next->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
1950
1951                                         /* Add stores for volatile variables */
1952                                         emit_volatile_store (ctx, ins->next->dreg);
1953                                 } else if (MONO_IS_COND_EXC (ins->next)) {
1954                                         //emit_cond_throw_pos (ctx);
1955                                         emit_cond_system_exception (ctx, bb, ins->next->inst_p1, cmp);
1956                                         builder = ctx->builder;
1957                                 } else {
1958                                         LLVM_FAILURE (ctx, "next");
1959                                 }
1960
1961                                 ins = ins->next;
1962                                 break;
1963                         }
1964                         case OP_FCEQ:
1965                         case OP_FCLT:
1966                         case OP_FCLT_UN:
1967                         case OP_FCGT:
1968                         case OP_FCGT_UN: {
1969                                 CompRelation rel;
1970                                 LLVMValueRef cmp;
1971
1972                                 rel = mono_opcode_to_cond (ins->opcode);
1973
1974                                 cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMDoubleType ()), convert (ctx, rhs, LLVMDoubleType ()), "");
1975                                 values [ins->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
1976                                 break;
1977                         }
1978                         case OP_PHI:
1979                         case OP_FPHI:
1980                         case OP_VPHI:
1981                         case OP_XPHI: {
1982                                 int i;
1983                                 gboolean empty = TRUE;
1984
1985                                 /* Check that all input bblocks really branch to us */
1986                                 for (i = 0; i < bb->in_count; ++i) {
1987                                         if (bb->in_bb [i]->last_ins && bb->in_bb [i]->last_ins->opcode == OP_NOT_REACHED)
1988                                                 ins->inst_phi_args [i + 1] = -1;
1989                                         else
1990                                                 empty = FALSE;
1991                                 }
1992
1993                                 if (empty) {
1994                                         /* LLVM doesn't like phi instructions with zero operands */
1995                                         is_dead [ins->dreg] = TRUE;
1996                                         break;
1997                                 }                                       
1998
1999                                 /* Created earlier, insert it now */
2000                                 LLVMInsertIntoBuilder (builder, values [ins->dreg]);
2001
2002                                 for (i = 0; i < ins->inst_phi_args [0]; i++) {
2003                                         int sreg1 = ins->inst_phi_args [i + 1];
2004                                         int count, j;
2005
2006                                         /* 
2007                                          * Count the number of times the incoming bblock branches to us,
2008                                          * since llvm requires a separate entry for each.
2009                                          */
2010                                         if (bb->in_bb [i]->last_ins && bb->in_bb [i]->last_ins->opcode == OP_SWITCH) {
2011                                                 MonoInst *switch_ins = bb->in_bb [i]->last_ins;
2012
2013                                                 count = 0;
2014                                                 for (j = 0; j < GPOINTER_TO_UINT (switch_ins->klass); ++j) {
2015                                                         if (switch_ins->inst_many_bb [j] == bb)
2016                                                                 count ++;
2017                                                 }
2018                                         } else {
2019                                                 count = 1;
2020                                         }
2021
2022                                         /* Remember for later */
2023                                         for (j = 0; j < count; ++j) {
2024                                                 PhiNode *node = mono_mempool_alloc0 (ctx->mempool, sizeof (PhiNode));
2025                                                 node->bb = bb;
2026                                                 node->phi = ins;
2027                                                 node->in_bb = bb->in_bb [i];
2028                                                 node->sreg = sreg1;
2029                                                 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);
2030                                         }
2031                                 }
2032                                 break;
2033                         }
2034                         case OP_MOVE:
2035                         case OP_LMOVE:
2036                         case OP_XMOVE:
2037                                 g_assert (lhs);
2038                                 values [ins->dreg] = lhs;
2039                                 break;
2040                         case OP_FMOVE: {
2041                                 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
2042                                 
2043                                 g_assert (lhs);
2044                                 values [ins->dreg] = lhs;
2045
2046                                 if (var && var->klass->byval_arg.type == MONO_TYPE_R4) {
2047                                         /* 
2048                                          * This is added by the spilling pass in case of the JIT,
2049                                          * but we have to do it ourselves.
2050                                          */
2051                                         values [ins->dreg] = convert (ctx, values [ins->dreg], LLVMFloatType ());
2052                                 }
2053                                 break;
2054                         }
2055                         case OP_IADD:
2056                         case OP_ISUB:
2057                         case OP_IAND:
2058                         case OP_IMUL:
2059                         case OP_IDIV:
2060                         case OP_IDIV_UN:
2061                         case OP_IREM:
2062                         case OP_IREM_UN:
2063                         case OP_IOR:
2064                         case OP_IXOR:
2065                         case OP_ISHL:
2066                         case OP_ISHR:
2067                         case OP_ISHR_UN:
2068                         case OP_FADD:
2069                         case OP_FSUB:
2070                         case OP_FMUL:
2071                         case OP_FDIV:
2072                         case OP_LADD:
2073                         case OP_LSUB:
2074                         case OP_LMUL:
2075                         case OP_LDIV:
2076                         case OP_LDIV_UN:
2077                         case OP_LREM:
2078                         case OP_LREM_UN:
2079                         case OP_LAND:
2080                         case OP_LOR:
2081                         case OP_LXOR:
2082                         case OP_LSHL:
2083                         case OP_LSHR:
2084                         case OP_LSHR_UN:
2085                                 lhs = convert (ctx, lhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
2086                                 rhs = convert (ctx, rhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
2087
2088                                 switch (ins->opcode) {
2089                                 case OP_IADD:
2090                                 case OP_FADD:
2091                                 case OP_LADD:
2092                                         values [ins->dreg] = LLVMBuildAdd (builder, lhs, rhs, dname);
2093                                         break;
2094                                 case OP_ISUB:
2095                                 case OP_FSUB:
2096                                 case OP_LSUB:
2097                                         values [ins->dreg] = LLVMBuildSub (builder, lhs, rhs, dname);
2098                                         break;
2099                                 case OP_IMUL:
2100                                 case OP_FMUL:
2101                                 case OP_LMUL:
2102                                         values [ins->dreg] = LLVMBuildMul (builder, lhs, rhs, dname);
2103                                         break;
2104                                 case OP_IREM:
2105                                 case OP_LREM:
2106                                         values [ins->dreg] = LLVMBuildSRem (builder, lhs, rhs, dname);
2107                                         break;
2108                                 case OP_IREM_UN:
2109                                 case OP_LREM_UN:
2110                                         values [ins->dreg] = LLVMBuildURem (builder, lhs, rhs, dname);
2111                                         break;
2112                                 case OP_IDIV:
2113                                 case OP_LDIV:
2114                                         values [ins->dreg] = LLVMBuildSDiv (builder, lhs, rhs, dname);
2115                                         break;
2116                                 case OP_IDIV_UN:
2117                                 case OP_LDIV_UN:
2118                                         values [ins->dreg] = LLVMBuildUDiv (builder, lhs, rhs, dname);
2119                                         break;
2120                                 case OP_FDIV:
2121                                         values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, dname);
2122                                         break;
2123                                 case OP_IAND:
2124                                 case OP_LAND:
2125                                         values [ins->dreg] = LLVMBuildAnd (builder, lhs, rhs, dname);
2126                                         break;
2127                                 case OP_IOR:
2128                                 case OP_LOR:
2129                                         values [ins->dreg] = LLVMBuildOr (builder, lhs, rhs, dname);
2130                                         break;
2131                                 case OP_IXOR:
2132                                 case OP_LXOR:
2133                                         values [ins->dreg] = LLVMBuildXor (builder, lhs, rhs, dname);
2134                                         break;
2135                                 case OP_ISHL:
2136                                 case OP_LSHL:
2137                                         values [ins->dreg] = LLVMBuildShl (builder, lhs, rhs, dname);
2138                                         break;
2139                                 case OP_ISHR:
2140                                 case OP_LSHR:
2141                                         values [ins->dreg] = LLVMBuildAShr (builder, lhs, rhs, dname);
2142                                         break;
2143                                 case OP_ISHR_UN:
2144                                 case OP_LSHR_UN:
2145                                         values [ins->dreg] = LLVMBuildLShr (builder, lhs, rhs, dname);
2146                                         break;
2147                                 default:
2148                                         g_assert_not_reached ();
2149                                 }
2150                                 break;
2151                         case OP_IADD_IMM:
2152                         case OP_ISUB_IMM:
2153                         case OP_IMUL_IMM:
2154                         case OP_IREM_IMM:
2155                         case OP_IREM_UN_IMM:
2156                         case OP_IDIV_IMM:
2157                         case OP_IDIV_UN_IMM:
2158                         case OP_IAND_IMM:
2159                         case OP_IOR_IMM:
2160                         case OP_IXOR_IMM:
2161                         case OP_ISHL_IMM:
2162                         case OP_ISHR_IMM:
2163                         case OP_ISHR_UN_IMM:
2164                         case OP_LADD_IMM:
2165                         case OP_LSUB_IMM:
2166                         case OP_LREM_IMM:
2167                         case OP_LAND_IMM:
2168                         case OP_LOR_IMM:
2169                         case OP_LXOR_IMM:
2170                         case OP_LSHL_IMM:
2171                         case OP_LSHR_IMM:
2172                         case OP_LSHR_UN_IMM:
2173                         case OP_ADD_IMM:
2174                         case OP_AND_IMM:
2175                         case OP_MUL_IMM:
2176                         case OP_SHL_IMM:
2177                         case OP_SHR_IMM: {
2178                                 LLVMValueRef imm;
2179
2180                                 if (spec [MONO_INST_SRC1] == 'l') {
2181                                         imm = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
2182                                 } else {
2183                                         imm = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
2184                                 }
2185
2186 #if SIZEOF_VOID_P == 4
2187                                 if (ins->opcode == OP_LSHL_IMM || ins->opcode == OP_LSHR_IMM || ins->opcode == OP_LSHR_UN_IMM)
2188                                         imm = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
2189 #endif
2190
2191                                 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
2192                                         lhs = convert (ctx, lhs, IntPtrType ());
2193                                 imm = convert (ctx, imm, LLVMTypeOf (lhs));
2194                                 switch (ins->opcode) {
2195                                 case OP_IADD_IMM:
2196                                 case OP_LADD_IMM:
2197                                 case OP_ADD_IMM:
2198                                         values [ins->dreg] = LLVMBuildAdd (builder, lhs, imm, dname);
2199                                         break;
2200                                 case OP_ISUB_IMM:
2201                                 case OP_LSUB_IMM:
2202                                         values [ins->dreg] = LLVMBuildSub (builder, lhs, imm, dname);
2203                                         break;
2204                                 case OP_IMUL_IMM:
2205                                 case OP_MUL_IMM:
2206                                         values [ins->dreg] = LLVMBuildMul (builder, lhs, imm, dname);
2207                                         break;
2208                                 case OP_IDIV_IMM:
2209                                 case OP_LDIV_IMM:
2210                                         values [ins->dreg] = LLVMBuildSDiv (builder, lhs, imm, dname);
2211                                         break;
2212                                 case OP_IDIV_UN_IMM:
2213                                 case OP_LDIV_UN_IMM:
2214                                         values [ins->dreg] = LLVMBuildUDiv (builder, lhs, imm, dname);
2215                                         break;
2216                                 case OP_IREM_IMM:
2217                                 case OP_LREM_IMM:
2218                                         values [ins->dreg] = LLVMBuildSRem (builder, lhs, imm, dname);
2219                                         break;
2220                                 case OP_IREM_UN_IMM:
2221                                         values [ins->dreg] = LLVMBuildURem (builder, lhs, imm, dname);
2222                                         break;
2223                                 case OP_IAND_IMM:
2224                                 case OP_LAND_IMM:
2225                                 case OP_AND_IMM:
2226                                         values [ins->dreg] = LLVMBuildAnd (builder, lhs, imm, dname);
2227                                         break;
2228                                 case OP_IOR_IMM:
2229                                 case OP_LOR_IMM:
2230                                         values [ins->dreg] = LLVMBuildOr (builder, lhs, imm, dname);
2231                                         break;
2232                                 case OP_IXOR_IMM:
2233                                 case OP_LXOR_IMM:
2234                                         values [ins->dreg] = LLVMBuildXor (builder, lhs, imm, dname);
2235                                         break;
2236                                 case OP_ISHL_IMM:
2237                                 case OP_LSHL_IMM:
2238                                 case OP_SHL_IMM:
2239                                         values [ins->dreg] = LLVMBuildShl (builder, lhs, imm, dname);
2240                                         break;
2241                                 case OP_ISHR_IMM:
2242                                 case OP_LSHR_IMM:
2243                                 case OP_SHR_IMM:
2244                                         values [ins->dreg] = LLVMBuildAShr (builder, lhs, imm, dname);
2245                                         break;
2246                                 case OP_ISHR_UN_IMM:
2247                                         /* This is used to implement conv.u4, so the lhs could be an i8 */
2248                                         lhs = convert (ctx, lhs, LLVMInt32Type ());
2249                                         imm = convert (ctx, imm, LLVMInt32Type ());
2250                                         values [ins->dreg] = LLVMBuildLShr (builder, lhs, imm, dname);
2251                                         break;
2252                                 case OP_LSHR_UN_IMM:
2253                                         values [ins->dreg] = LLVMBuildLShr (builder, lhs, imm, dname);
2254                                         break;
2255                                 default:
2256                                         g_assert_not_reached ();
2257                                 }
2258                                 break;
2259                         }
2260                         case OP_INEG:
2261                                 values [ins->dreg] = LLVMBuildSub (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), convert (ctx, lhs, LLVMInt32Type ()), dname);
2262                                 break;
2263                         case OP_LNEG:
2264                                 values [ins->dreg] = LLVMBuildSub (builder, LLVMConstInt (LLVMInt64Type (), 0, FALSE), lhs, dname);
2265                                 break;
2266                         case OP_FNEG:
2267                                 lhs = convert (ctx, lhs, LLVMDoubleType ());
2268                                 values [ins->dreg] = LLVMBuildSub (builder, LLVMConstReal (LLVMDoubleType (), 0.0), lhs, dname);
2269                                 break;
2270                         case OP_INOT: {
2271                                 guint32 v = 0xffffffff;
2272                                 values [ins->dreg] = LLVMBuildXor (builder, LLVMConstInt (LLVMInt32Type (), v, FALSE), lhs, dname);
2273                                 break;
2274                         }
2275                         case OP_LNOT: {
2276                                 guint64 v = 0xffffffffffffffffLL;
2277                                 values [ins->dreg] = LLVMBuildXor (builder, LLVMConstInt (LLVMInt64Type (), v, FALSE), lhs, dname);
2278                                 break;
2279                         }
2280 #if defined(TARGET_X86) || defined(TARGET_AMD64)
2281                         case OP_X86_LEA: {
2282                                 LLVMValueRef v1, v2;
2283
2284                                 v1 = LLVMBuildMul (builder, convert (ctx, rhs, IntPtrType ()), LLVMConstInt (IntPtrType (), (1 << ins->backend.shift_amount), FALSE), "");
2285                                 v2 = LLVMBuildAdd (builder, convert (ctx, lhs, IntPtrType ()), v1, "");
2286                                 values [ins->dreg] = LLVMBuildAdd (builder, v2, LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), dname);
2287                                 break;
2288                         }
2289 #endif
2290
2291                         case OP_ICONV_TO_I1:
2292                         case OP_ICONV_TO_I2:
2293                         case OP_ICONV_TO_I4:
2294                         case OP_ICONV_TO_U1:
2295                         case OP_ICONV_TO_U2:
2296                         case OP_ICONV_TO_U4:
2297                         case OP_LCONV_TO_I1:
2298                         case OP_LCONV_TO_I2:
2299                         case OP_LCONV_TO_U1:
2300                         case OP_LCONV_TO_U2:
2301                         case OP_LCONV_TO_U4: {
2302                                 gboolean sign;
2303
2304                                 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);
2305
2306                                 /* Have to do two casts since our vregs have type int */
2307                                 v = LLVMBuildTrunc (builder, lhs, op_to_llvm_type (ins->opcode), "");
2308                                 if (sign)
2309                                         values [ins->dreg] = LLVMBuildSExt (builder, v, LLVMInt32Type (), dname);
2310                                 else
2311                                         values [ins->dreg] = LLVMBuildZExt (builder, v, LLVMInt32Type (), dname);
2312                                 break;
2313                         }
2314                         case OP_ICONV_TO_I8:
2315                                 values [ins->dreg] = LLVMBuildSExt (builder, lhs, LLVMInt64Type (), dname);
2316                                 break;
2317                         case OP_ICONV_TO_U8:
2318                                 values [ins->dreg] = LLVMBuildZExt (builder, lhs, LLVMInt64Type (), dname);
2319                                 break;
2320                         case OP_FCONV_TO_I4:
2321                                 values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, LLVMInt32Type (), dname);
2322                                 break;
2323                         case OP_FCONV_TO_I1:
2324                                 values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildFPToSI (builder, lhs, LLVMInt8Type (), dname), LLVMInt32Type (), "");
2325                                 break;
2326                         case OP_FCONV_TO_U1:
2327                                 values [ins->dreg] = LLVMBuildZExt (builder, LLVMBuildFPToUI (builder, lhs, LLVMInt8Type (), dname), LLVMInt32Type (), "");
2328                                 break;
2329                         case OP_FCONV_TO_I2:
2330                                 values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildFPToSI (builder, lhs, LLVMInt16Type (), dname), LLVMInt32Type (), "");
2331                                 break;
2332                         case OP_FCONV_TO_U2:
2333                                 values [ins->dreg] = LLVMBuildZExt (builder, LLVMBuildFPToUI (builder, lhs, LLVMInt16Type (), dname), LLVMInt32Type (), "");
2334                                 break;
2335                         case OP_FCONV_TO_I8:
2336                                 values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, LLVMInt64Type (), dname);
2337                                 break;
2338                         case OP_FCONV_TO_I:
2339                                 values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, IntPtrType (), dname);
2340                                 break;
2341                         case OP_ICONV_TO_R8:
2342                         case OP_LCONV_TO_R8:
2343                                 values [ins->dreg] = LLVMBuildSIToFP (builder, lhs, LLVMDoubleType (), dname);
2344                                 break;
2345                         case OP_LCONV_TO_R_UN:
2346                                 values [ins->dreg] = LLVMBuildUIToFP (builder, lhs, LLVMDoubleType (), dname);
2347                                 break;
2348 #if SIZEOF_VOID_P == 4
2349                         case OP_LCONV_TO_U:
2350 #endif
2351                         case OP_LCONV_TO_I4:
2352                                 values [ins->dreg] = LLVMBuildTrunc (builder, lhs, LLVMInt32Type (), dname);
2353                                 break;
2354                         case OP_ICONV_TO_R4:
2355                         case OP_LCONV_TO_R4:
2356                                 v = LLVMBuildSIToFP (builder, lhs, LLVMFloatType (), "");
2357                                 values [ins->dreg] = LLVMBuildFPExt (builder, v, LLVMDoubleType (), dname);
2358                                 break;
2359                         case OP_FCONV_TO_R4:
2360                                 v = LLVMBuildFPTrunc (builder, lhs, LLVMFloatType (), "");
2361                                 values [ins->dreg] = LLVMBuildFPExt (builder, v, LLVMDoubleType (), dname);
2362                                 break;
2363                         case OP_SEXT_I4:
2364                                 values [ins->dreg] = LLVMBuildSExt (builder, lhs, LLVMInt64Type (), dname);
2365                                 break;
2366                         case OP_ZEXT_I4:
2367                                 values [ins->dreg] = LLVMBuildZExt (builder, lhs, LLVMInt64Type (), dname);
2368                                 break;
2369                         case OP_TRUNC_I4:
2370                                 values [ins->dreg] = LLVMBuildTrunc (builder, lhs, LLVMInt32Type (), dname);
2371                                 break;
2372                         case OP_LOCALLOC_IMM: {
2373                                 LLVMValueRef v;
2374
2375                                 guint32 size = ins->inst_imm;
2376                                 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
2377
2378                                 v = mono_llvm_build_alloca (builder, LLVMInt8Type (), LLVMConstInt (LLVMInt32Type (), size, FALSE), MONO_ARCH_FRAME_ALIGNMENT, "");
2379
2380                                 if (ins->flags & MONO_INST_INIT) {
2381                                         LLVMValueRef args [4];
2382
2383                                         args [0] = v;
2384                                         args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
2385                                         args [2] = LLVMConstInt (LLVMInt32Type (), size, FALSE);
2386                                         args [3] = LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT, FALSE);
2387                                         LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.memset.i32"), args, 4, "");
2388                                 }
2389
2390                                 values [ins->dreg] = v;
2391                                 break;
2392                         }
2393                         case OP_LOCALLOC: {
2394                                 LLVMValueRef v, size;
2395                                 
2396                                 size = LLVMBuildAnd (builder, LLVMBuildAdd (builder, lhs, LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT - 1, FALSE), ""), LLVMConstInt (LLVMInt32Type (), ~ (MONO_ARCH_FRAME_ALIGNMENT - 1), FALSE), "");
2397
2398                                 v = mono_llvm_build_alloca (builder, LLVMInt8Type (), size, MONO_ARCH_FRAME_ALIGNMENT, "");
2399
2400                                 if (ins->flags & MONO_INST_INIT) {
2401                                         LLVMValueRef args [4];
2402
2403                                         args [0] = v;
2404                                         args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
2405                                         args [2] = size;
2406                                         args [3] = LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT, FALSE);
2407                                         LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.memset.i32"), args, 4, "");
2408                                 }
2409                                 values [ins->dreg] = v;
2410                                 break;
2411                         }
2412
2413                         case OP_LOADI1_MEMBASE:
2414                         case OP_LOADU1_MEMBASE:
2415                         case OP_LOADI2_MEMBASE:
2416                         case OP_LOADU2_MEMBASE:
2417                         case OP_LOADI4_MEMBASE:
2418                         case OP_LOADU4_MEMBASE:
2419                         case OP_LOADI8_MEMBASE:
2420                         case OP_LOADR4_MEMBASE:
2421                         case OP_LOADR8_MEMBASE:
2422                         case OP_LOAD_MEMBASE:
2423                         case OP_LOADI8_MEM:
2424                         case OP_LOADU1_MEM:
2425                         case OP_LOADU2_MEM:
2426                         case OP_LOADI4_MEM:
2427                         case OP_LOADU4_MEM:
2428                         case OP_LOAD_MEM: {
2429                                 int size = 8;
2430                                 LLVMValueRef index, addr;
2431                                 LLVMTypeRef t;
2432                                 gboolean sext = FALSE, zext = FALSE;
2433                                 gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
2434
2435                                 t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
2436
2437                                 if (sext || zext)
2438                                         dname = (char*)"";
2439
2440                                 /* 
2441                                  * We emit volatile loads for loads which can fault, because otherwise
2442                                  * LLVM will generate invalid code when encountering a load from a
2443                                  * NULL address.
2444                                  */
2445                                 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)) {
2446                                         addr = LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE);
2447                                 } else if (ins->inst_offset == 0) {
2448                                         addr = values [ins->inst_basereg];
2449                                 } else if (ins->inst_offset % size != 0) {
2450                                         /* Unaligned load */
2451                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
2452                                         addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
2453                                 } else {
2454                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
2455                                         addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (t, 0)), &index, 1, "");
2456                                 }
2457
2458                                 addr = convert (ctx, addr, LLVMPointerType (t, 0));
2459
2460                                 values [ins->dreg] = mono_llvm_build_load (builder, addr, dname, is_volatile);
2461
2462                                 if (sext)
2463                                         values [ins->dreg] = LLVMBuildSExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
2464                                 else if (zext)
2465                                         values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
2466                                 else if (ins->opcode == OP_LOADR4_MEMBASE)
2467                                         values [ins->dreg] = LLVMBuildFPExt (builder, values [ins->dreg], LLVMDoubleType (), dname);
2468                                 break;
2469                         }
2470                                 
2471                         case OP_STOREI1_MEMBASE_REG:
2472                         case OP_STOREI2_MEMBASE_REG:
2473                         case OP_STOREI4_MEMBASE_REG:
2474                         case OP_STOREI8_MEMBASE_REG:
2475                         case OP_STORER4_MEMBASE_REG:
2476                         case OP_STORER8_MEMBASE_REG:
2477                         case OP_STORE_MEMBASE_REG: {
2478                                 int size = 8;
2479                                 LLVMValueRef index, addr;
2480                                 LLVMTypeRef t;
2481                                 gboolean sext = FALSE, zext = FALSE;
2482
2483                                 t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
2484
2485                                 if (ins->inst_offset % size != 0) {
2486                                         /* Unaligned store */
2487                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
2488                                         addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
2489                                 } else {
2490                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
2491                                         addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, "");
2492                                 }
2493                                 LLVMBuildStore (builder, convert (ctx, values [ins->sreg1], t), convert (ctx, addr, LLVMPointerType (t, 0)));
2494                                 break;
2495                         }
2496
2497                         case OP_STOREI1_MEMBASE_IMM:
2498                         case OP_STOREI2_MEMBASE_IMM:
2499                         case OP_STOREI4_MEMBASE_IMM:
2500                         case OP_STOREI8_MEMBASE_IMM:
2501                         case OP_STORE_MEMBASE_IMM: {
2502                                 int size = 8;
2503                                 LLVMValueRef index, addr;
2504                                 LLVMTypeRef t;
2505                                 gboolean sext = FALSE, zext = FALSE;
2506
2507                                 t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
2508
2509                                 if (ins->inst_offset % size != 0) {
2510                                         /* Unaligned store */
2511                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
2512                                         addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
2513                                 } else {
2514                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
2515                                         addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, "");
2516                                 }
2517                                 LLVMBuildStore (builder, convert (ctx, LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE), t), addr);
2518                                 break;
2519                         }
2520
2521                         case OP_CHECK_THIS:
2522                                 mono_llvm_build_load (builder, convert (ctx, values [ins->sreg1], LLVMPointerType (IntPtrType (), 0)), "", TRUE);
2523                                 break;
2524                         case OP_OUTARG_VTRETADDR:
2525                                 break;
2526                         case OP_VOIDCALL:
2527                         case OP_CALL:
2528                         case OP_LCALL:
2529                         case OP_FCALL:
2530                         case OP_VCALL:
2531                         case OP_VOIDCALL_MEMBASE:
2532                         case OP_CALL_MEMBASE:
2533                         case OP_LCALL_MEMBASE:
2534                         case OP_FCALL_MEMBASE:
2535                         case OP_VCALL_MEMBASE:
2536                         case OP_VOIDCALL_REG:
2537                         case OP_CALL_REG:
2538                         case OP_LCALL_REG:
2539                         case OP_FCALL_REG:
2540                         case OP_VCALL_REG: {
2541                                 MonoCallInst *call = (MonoCallInst*)ins;
2542                                 MonoMethodSignature *sig = call->signature;
2543                                 LLVMValueRef callee, lcall;
2544                                 LLVMValueRef *args;
2545                                 LLVMCallInfo *cinfo;
2546                                 GSList *l;
2547                                 int i, pindex;
2548                                 gboolean vretaddr;
2549                                 LLVMTypeRef llvm_sig;
2550                                 gpointer target;
2551                                 int *pindexes;
2552                                 gboolean virtual, calli;
2553
2554                                 cinfo = call->cinfo;
2555
2556                                 vretaddr = cinfo && cinfo->ret.storage == LLVMArgVtypeRetAddr;
2557
2558                                 llvm_sig = sig_to_llvm_sig (ctx, sig, cinfo);
2559                                 CHECK_FAILURE (ctx);
2560
2561                                 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);
2562                                 calli = (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);
2563
2564                                 pindexes = mono_mempool_alloc0 (cfg->mempool, (sig->param_count + 2) * sizeof (guint32));
2565
2566                                 /* FIXME: Avoid creating duplicate methods */
2567
2568                                 if (ins->flags & MONO_INST_HAS_METHOD) {
2569                                         if (virtual) {
2570                                                 callee = NULL;
2571                                         } else {
2572                                                 if (cfg->compile_aot) {
2573                                                         callee = get_plt_entry (ctx, llvm_sig, MONO_PATCH_INFO_METHOD, call->method);
2574                                                         if (!callee)
2575                                                                 LLVM_FAILURE (ctx, "can't encode patch");
2576                                                 } else {
2577                                                         callee = LLVMAddFunction (module, "", llvm_sig);
2578  
2579                                                         target =
2580                                                                 mono_create_jit_trampoline_in_domain (mono_domain_get (),
2581                                                                                                                                           call->method);
2582                                                         LLVMAddGlobalMapping (ee, callee, target);
2583                                                 }
2584                                         }
2585                                 } else if (calli) {
2586                                 } else {
2587                                         MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
2588
2589                                         if (info) {
2590                                                 /*
2591                                                 MonoJumpInfo ji;
2592
2593                                                 memset (&ji, 0, sizeof (ji));
2594                                                 ji.type = MONO_PATCH_INFO_JIT_ICALL_ADDR;
2595                                                 ji.data.target = info->name;
2596
2597                                                 target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &ji, FALSE);
2598                                                 */
2599                                                 if (cfg->compile_aot) {
2600                                                         callee = get_plt_entry (ctx, llvm_sig, MONO_PATCH_INFO_INTERNAL_METHOD, (char*)info->name);
2601                                                         if (!callee)
2602                                                                 LLVM_FAILURE (ctx, "can't encode patch");
2603                                                 } else {
2604                                                         callee = LLVMAddFunction (module, "", llvm_sig);
2605                                                         target = (gpointer)mono_icall_get_wrapper (info);
2606                                                         LLVMAddGlobalMapping (ee, callee, target);
2607                                                 }
2608                                         } else {
2609                                                 if (cfg->compile_aot) {
2610                                                         callee = NULL;
2611                                                         if (cfg->abs_patches) {
2612                                                                 MonoJumpInfo *abs_ji = g_hash_table_lookup (cfg->abs_patches, call->fptr);
2613                                                                 if (abs_ji) {
2614                                                                         callee = get_plt_entry (ctx, llvm_sig, abs_ji->type, abs_ji->data.target);
2615                                                                         if (!callee)
2616                                                                                 LLVM_FAILURE (ctx, "can't encode patch");
2617                                                                 }
2618                                                         }
2619                                                         if (!callee)
2620                                                                 LLVM_FAILURE (ctx, "aot");
2621                                                 } else {
2622                                                         callee = LLVMAddFunction (module, "", llvm_sig);
2623                                                         target = NULL;
2624                                                         if (cfg->abs_patches) {
2625                                                                 MonoJumpInfo *abs_ji = g_hash_table_lookup (cfg->abs_patches, call->fptr);
2626                                                                 if (abs_ji) {
2627                                                                         /*
2628                                                                          * The monitor entry/exit trampolines might have
2629                                                                          * their own calling convention on some platforms.
2630                                                                          */
2631 #ifndef TARGET_AMD64
2632                                                                         if (abs_ji->type == MONO_PATCH_INFO_MONITOR_ENTER || abs_ji->type == MONO_PATCH_INFO_MONITOR_EXIT)
2633                                                                                 LLVM_FAILURE (ctx, "monitor enter/exit");
2634 #endif
2635                                                                         target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, abs_ji, FALSE);
2636                                                                         LLVMAddGlobalMapping (ee, callee, target);
2637                                                                 }
2638                                                         }
2639                                                         if (!target)
2640                                                                 LLVMAddGlobalMapping (ee, callee, (gpointer)call->fptr);
2641                                                 }
2642                                         }
2643                                 }
2644
2645                                 if (virtual) {
2646                                         int size = sizeof (gpointer);
2647                                         LLVMValueRef index;
2648
2649                                         g_assert (ins->inst_offset % size == 0);
2650                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
2651
2652                                         // FIXME: mono_arch_get_vcall_slot () can't decode the code
2653                                         // generated by LLVM
2654                                         //LLVM_FAILURE (ctx, "virtual call");
2655
2656                                         if (call->method && call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
2657 #ifdef MONO_ARCH_HAVE_LLVM_IMT_TRAMPOLINE
2658                                                 if (cfg->compile_aot) {
2659                                                         MonoJumpInfoImtTramp *imt_tramp = g_new0 (MonoJumpInfoImtTramp, 1);
2660                                                         imt_tramp->method = call->method;
2661                                                         imt_tramp->vt_offset = call->inst.inst_offset;
2662
2663                                                         callee = get_plt_entry (ctx, llvm_sig, MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE, imt_tramp);
2664                                                 } else {
2665                                                         callee = LLVMAddFunction (module, "", llvm_sig);
2666                                                         target = mono_create_llvm_imt_trampoline (cfg->domain, call->method, call->inst.inst_offset);
2667                                                         LLVMAddGlobalMapping (ee, callee, target);
2668                                                 }
2669 #else
2670                                                 /* No support for passing the IMT argument */
2671                                                 LLVM_FAILURE (ctx, "imt");
2672 #endif
2673                                         } else {
2674                                                 callee = convert (ctx, LLVMBuildLoad (builder, LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (LLVMPointerType (IntPtrType (), 0), 0)), &index, 1, ""), ""), LLVMPointerType (llvm_sig, 0));
2675                                         }
2676                                 } else if (calli) {
2677                                         callee = convert (ctx, values [ins->sreg1], LLVMPointerType (llvm_sig, 0));
2678                                 } else {
2679                                         if (ins->flags & MONO_INST_HAS_METHOD) {
2680                                         }
2681                                 }
2682
2683                                 /* 
2684                                  * Collect and convert arguments
2685                                  */
2686
2687                                 args = alloca (sizeof (LLVMValueRef) * ((sig->param_count * 2) + sig->hasthis + vretaddr));
2688                                 l = call->out_ireg_args;
2689                                 pindex = 0;
2690                                 if (vretaddr) {
2691                                         if (!addresses [call->inst.dreg])
2692                                                 addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
2693                                         args [pindex ++] = LLVMBuildPtrToInt (builder, addresses [call->inst.dreg], IntPtrType (), "");
2694                                 }
2695
2696                                 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2697                                         guint32 regpair;
2698                                         int reg;
2699                                         LLVMArgInfo *ainfo = call->cinfo ? &call->cinfo->args [i] : NULL;
2700
2701                                         regpair = (guint32)(gssize)(l->data);
2702                                         reg = regpair & 0xffffff;
2703                                         args [pindex] = values [reg];
2704                                         if (ainfo->storage == LLVMArgVtypeInReg) {
2705                                                 int j;
2706                                                 LLVMValueRef regs [2];
2707                                                 guint32 nregs;
2708
2709                                                 g_assert (ainfo);
2710
2711                                                 g_assert (addresses [reg]);
2712
2713                                                 emit_vtype_to_reg (ctx, builder, sig->params [i - sig->hasthis], addresses [reg], ainfo, regs, &nregs);
2714                                                 for (j = 0; j < nregs; ++j)
2715                                                         args [pindex ++] = regs [j];
2716
2717                                                 // FIXME: alignment
2718                                                 // FIXME: Get rid of the VMOVE
2719                                         } else if (ainfo->storage == LLVMArgVtypeByVal) {
2720                                                 g_assert (addresses [reg]);
2721                                                 args [pindex] = addresses [reg];
2722                                                 pindexes [i] = pindex;
2723                                                 pindex ++;
2724                                         } else {
2725                                                 g_assert (args [pindex]);
2726                                                 if (i == 0 && sig->hasthis)
2727                                                         args [pindex] = convert (ctx, args [pindex], IntPtrType ());
2728                                                 else
2729                                                         args [pindex] = convert (ctx, args [pindex], type_to_llvm_arg_type (ctx, sig->params [i - sig->hasthis]));
2730                                                 pindex ++;
2731                                         }
2732
2733                                         l = l->next;
2734                                 }
2735
2736                                 // FIXME: Align call sites
2737
2738                                 /*
2739                                  * Emit the call
2740                                  */
2741
2742                                 lcall = emit_call (ctx, bb, &builder, callee, args, pindex);
2743
2744                                 /* Add byval attributes if needed */
2745                                 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2746                                         LLVMArgInfo *ainfo = call->cinfo ? &call->cinfo->args [i] : NULL;
2747
2748                                         if (ainfo && ainfo->storage == LLVMArgVtypeByVal) {
2749                                                 LLVMAddInstrAttribute (lcall, pindexes [i] + 1, LLVMByValAttribute);
2750                                         }
2751                                 }
2752
2753                                 /*
2754                                  * Convert the result
2755                                  */
2756                                 if (cinfo && cinfo->ret.storage == LLVMArgVtypeInReg) {
2757                                         LLVMValueRef regs [2];
2758
2759                                         if (!addresses [ins->dreg])
2760                                                 addresses [ins->dreg] = build_alloca (ctx, sig->ret);
2761
2762                                         regs [0] = LLVMBuildExtractValue (builder, lcall, 0, "");
2763                                         if (cinfo->ret.pair_storage [1] != LLVMArgNone)
2764                                                 regs [1] = LLVMBuildExtractValue (builder, lcall, 1, "");
2765                                         
2766                                         emit_reg_to_vtype (ctx, builder, sig->ret, addresses [ins->dreg], &cinfo->ret, regs);
2767                                 } else if (sig->ret->type != MONO_TYPE_VOID && !vretaddr) {
2768                                         /* If the method returns an unsigned value, need to zext it */
2769
2770                                         values [ins->dreg] = convert_full (ctx, lcall, llvm_type_to_stack_type (type_to_llvm_type (ctx, sig->ret)), type_is_unsigned (ctx, sig->ret));
2771                                 }
2772                                 break;
2773                         }
2774                         case OP_AOTCONST: {
2775                                 guint32 got_offset;
2776                                 LLVMValueRef indexes [2];
2777                                 MonoJumpInfo *ji;
2778                                 LLVMValueRef got_entry_addr;
2779
2780                                 /* 
2781                                  * FIXME: Can't allocate from the cfg mempool since that is freed if
2782                                  * the LLVM compile fails.
2783                                  */
2784                                 ji = g_new0 (MonoJumpInfo, 1);
2785                                 ji->type = (MonoJumpInfoType)ins->inst_i1;
2786                                 ji->data.target = ins->inst_p0;
2787
2788                                 ji = mono_aot_patch_info_dup (ji);
2789
2790                                 ji->next = cfg->patch_info;
2791                                 cfg->patch_info = ji;
2792                                    
2793                                 //mono_add_patch_info (cfg, 0, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2794                                 got_offset = mono_aot_get_got_offset (cfg->patch_info);
2795  
2796                                 indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2797                                 indexes [1] = LLVMConstInt (LLVMInt32Type (), (gssize)got_offset, FALSE);
2798                                 got_entry_addr = LLVMBuildGEP (builder, ctx->lmodule->got_var, indexes, 2, "");
2799
2800                                 // FIXME: This doesn't work right now, because it must be
2801                                 // paired with an invariant.end, and even then, its only in effect
2802                                 // inside its basic block
2803 #if 0
2804                                 {
2805                                         LLVMValueRef args [3];
2806                                         LLVMValueRef ptr, val;
2807
2808                                         ptr = LLVMBuildBitCast (builder, got_entry_addr, LLVMPointerType (LLVMInt8Type (), 0), "ptr");
2809
2810                                         args [0] = LLVMConstInt (LLVMInt64Type (), sizeof (gpointer), FALSE);
2811                                         args [1] = ptr;
2812                                         val = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.invariant.start"), args, 2, "");
2813                                 }
2814 #endif
2815
2816                                 values [ins->dreg] = LLVMBuildLoad (builder, got_entry_addr, dname);
2817                                 break;
2818                         }
2819                         case OP_NOT_REACHED:
2820                                 LLVMBuildUnreachable (builder);
2821                                 has_terminator = TRUE;
2822                                 g_assert (bb->block_num < cfg->max_block_num);
2823                                 unreachable [bb->block_num] = TRUE;
2824                                 /* Might have instructions after this */
2825                                 while (ins->next) {
2826                                         MonoInst *next = ins->next;
2827                                         MONO_DELETE_INS (bb, next);
2828                                 }                               
2829                                 break;
2830                         case OP_LDADDR: {
2831                                 MonoInst *var = ins->inst_p0;
2832
2833                                 values [ins->dreg] = addresses [var->dreg];
2834                                 break;
2835                         }
2836                         case OP_SIN: {
2837                                 LLVMValueRef args [1];
2838
2839                                 args [0] = convert (ctx, lhs, LLVMDoubleType ());
2840                                 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.sin.f64"), args, 1, dname);
2841                                 break;
2842                         }
2843                         case OP_COS: {
2844                                 LLVMValueRef args [1];
2845
2846                                 args [0] = convert (ctx, lhs, LLVMDoubleType ());
2847                                 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.cos.f64"), args, 1, dname);
2848                                 break;
2849                         }
2850                                 /* test_0_sqrt_nan fails with LLVM */
2851                                 /*
2852                         case OP_SQRT: {
2853                                 LLVMValueRef args [1];
2854
2855                                 args [0] = lhs;
2856                                 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.sqrt.f64"), args, 1, dname);
2857                                 break;
2858                         }
2859                                 */
2860
2861                         case OP_ABS: {
2862                                 LLVMValueRef args [1];
2863
2864                                 args [0] = lhs;
2865                                 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "fabs"), args, 1, dname);
2866                                 break;
2867                         }
2868
2869                         case OP_IMIN:
2870                         case OP_LMIN: {
2871                                 LLVMValueRef v = LLVMBuildICmp (builder, LLVMIntSLE, lhs, rhs, "");
2872                                 values [ins->dreg] = LLVMBuildSelect (builder, v, lhs, rhs, dname);
2873                                 break;
2874                         }
2875                         case OP_IMAX:
2876                         case OP_LMAX: {
2877                                 LLVMValueRef v = LLVMBuildICmp (builder, LLVMIntSGE, lhs, rhs, "");
2878                                 values [ins->dreg] = LLVMBuildSelect (builder, v, lhs, rhs, dname);
2879                                 break;
2880                         }
2881                         case OP_IMIN_UN:
2882                         case OP_LMIN_UN: {
2883                                 LLVMValueRef v = LLVMBuildICmp (builder, LLVMIntULE, lhs, rhs, "");
2884                                 values [ins->dreg] = LLVMBuildSelect (builder, v, lhs, rhs, dname);
2885                                 break;
2886                         }
2887                         case OP_IMAX_UN:
2888                         case OP_LMAX_UN: {
2889                                 LLVMValueRef v = LLVMBuildICmp (builder, LLVMIntUGE, lhs, rhs, "");
2890                                 values [ins->dreg] = LLVMBuildSelect (builder, v, lhs, rhs, dname);
2891                                 break;
2892                         }
2893                         case OP_ATOMIC_EXCHANGE_I4: {
2894                                 LLVMValueRef args [2];
2895
2896                                 g_assert (ins->inst_offset == 0);
2897
2898                                 args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt32Type (), 0));
2899                                 args [1] = rhs;
2900                                 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.swap.i32.p0i32"), args, 2, dname);
2901                                 break;
2902                         }
2903                         case OP_ATOMIC_EXCHANGE_I8: {
2904                                 LLVMValueRef args [2];
2905
2906                                 g_assert (ins->inst_offset == 0);
2907
2908                                 args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt64Type (), 0));
2909                                 args [1] = rhs;
2910                                 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.swap.i64.p0i64"), args, 2, dname);
2911                                 break;
2912                         }
2913                         case OP_ATOMIC_ADD_NEW_I4: {
2914                                 LLVMValueRef args [2];
2915
2916                                 g_assert (ins->inst_offset == 0);
2917
2918                                 args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt32Type (), 0));
2919                                 args [1] = rhs;
2920                                 values [ins->dreg] = LLVMBuildAdd (builder, LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.load.add.i32.p0i32"), args, 2, ""), args [1], dname);
2921                                 break;
2922                         }
2923                         case OP_ATOMIC_ADD_NEW_I8: {
2924                                 LLVMValueRef args [2];
2925
2926                                 g_assert (ins->inst_offset == 0);
2927
2928                                 args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt64Type (), 0));
2929                                 args [1] = convert (ctx, rhs, LLVMInt64Type ());
2930                                 values [ins->dreg] = LLVMBuildAdd (builder, LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.load.add.i64.p0i64"), args, 2, ""), args [1], dname);
2931                                 break;
2932                         }
2933                         case OP_ATOMIC_CAS_I4:
2934                         case OP_ATOMIC_CAS_I8: {
2935                                 LLVMValueRef args [3];
2936                                 LLVMTypeRef t;
2937                                 const char *intrins;
2938                                 
2939                                 if (ins->opcode == OP_ATOMIC_CAS_I4) {
2940                                         t = LLVMInt32Type ();
2941                                         intrins = "llvm.atomic.cmp.swap.i32.p0i32";
2942                                 } else {
2943                                         t = LLVMInt64Type ();
2944                                         intrins = "llvm.atomic.cmp.swap.i64.p0i64";
2945                                 }
2946
2947                                 args [0] = convert (ctx, lhs, LLVMPointerType (t, 0));
2948                                 /* comparand */
2949                                 args [1] = convert (ctx, values [ins->sreg3], t);
2950                                 /* new value */
2951                                 args [2] = convert (ctx, values [ins->sreg2], t);
2952                                 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, intrins), args, 3, dname);
2953                                 break;
2954                         }
2955                         case OP_MEMORY_BARRIER: {
2956                                 LLVMValueRef args [5];
2957
2958                                 for (i = 0; i < 5; ++i)
2959                                         args [i] = LLVMConstInt (LLVMInt1Type (), TRUE, TRUE);
2960
2961                                 LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.memory.barrier"), args, 5, "");
2962                                 break;
2963                         }
2964                         case OP_RELAXED_NOP: {
2965 #if defined(TARGET_AMD64) || defined(TARGET_X86)
2966                                 /* No way to get LLVM to emit this */
2967                                 LLVM_FAILURE (ctx, "relaxed_nop");
2968 #else
2969                                 break;
2970 #endif
2971                         }
2972                         case OP_TLS_GET: {
2973 #if defined(TARGET_AMD64) || defined(TARGET_X86)
2974 #ifdef TARGET_AMD64
2975                                 // 257 == FS segment register
2976                                 LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 257);
2977 #else
2978                                 // 256 == GS segment register
2979                                 LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 256);
2980 #endif
2981
2982                                 // FIXME: XEN
2983                                 values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildIntToPtr (builder, LLVMConstInt (IntPtrType (), ins->inst_offset, TRUE), ptrtype, ""), "");
2984 #else
2985                                 LLVM_FAILURE (ctx, "opcode tls-get");
2986 #endif
2987
2988                                 break;
2989                         }
2990
2991                         /*
2992                          * Overflow opcodes.
2993                          */
2994                         case OP_IADD_OVF:
2995                         case OP_IADD_OVF_UN:
2996                         case OP_ISUB_OVF:
2997                         case OP_ISUB_OVF_UN:
2998                         case OP_IMUL_OVF:
2999                         case OP_IMUL_OVF_UN:
3000 #if SIZEOF_VOID_P == 8
3001                         case OP_LADD_OVF:
3002                         case OP_LADD_OVF_UN:
3003                         case OP_LSUB_OVF:
3004                         case OP_LSUB_OVF_UN:
3005                         case OP_LMUL_OVF:
3006                         case OP_LMUL_OVF_UN:
3007 #endif
3008                         {
3009                                 LLVMValueRef args [2], val, ovf, func;
3010
3011                                 emit_cond_throw_pos (ctx);
3012
3013                                 args [0] = convert (ctx, lhs, op_to_llvm_type (ins->opcode));
3014                                 args [1] = convert (ctx, rhs, op_to_llvm_type (ins->opcode));
3015                                 func = LLVMGetNamedFunction (module, ovf_op_to_intrins (ins->opcode));
3016                                 g_assert (func);
3017                                 val = LLVMBuildCall (builder, func, args, 2, "");
3018                                 values [ins->dreg] = LLVMBuildExtractValue (builder, val, 0, dname);
3019                                 ovf = LLVMBuildExtractValue (builder, val, 1, "");
3020                                 emit_cond_system_exception (ctx, bb, "OverflowException", ovf);
3021                                 builder = ctx->builder;
3022                                 break;
3023                         }
3024
3025                         /* 
3026                          * Valuetypes.
3027                          *   We currently model them using arrays. Promotion to local vregs is 
3028                          * disabled for them in mono_handle_global_vregs () in the LLVM case, 
3029                          * so we always have an entry in cfg->varinfo for them.
3030                          * FIXME: Is this needed ?
3031                          */
3032                         case OP_VZERO: {
3033                                 MonoClass *klass = ins->klass;
3034                                 LLVMValueRef args [4];
3035
3036                                 if (!klass) {
3037                                         // FIXME:
3038                                         LLVM_FAILURE (ctx, "!klass");
3039                                         break;
3040                                 }
3041
3042                                 if (!addresses [ins->dreg])
3043                                         addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
3044                                 args [0] = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
3045                                 args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
3046                                 args [2] = LLVMConstInt (LLVMInt32Type (), mono_class_value_size (klass, NULL), FALSE);
3047                                 // FIXME: Alignment
3048                                 args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
3049                                 LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.memset.i32"), args, 4, "");
3050                                 break;
3051                         }
3052
3053                         case OP_STOREV_MEMBASE:
3054                         case OP_LOADV_MEMBASE:
3055                         case OP_VMOVE: {
3056                                 MonoClass *klass = ins->klass;
3057                                 LLVMValueRef src, dst, args [4];
3058                                 gboolean done = FALSE;
3059
3060                                 if (!klass) {
3061                                         // FIXME:
3062                                         LLVM_FAILURE (ctx, "!klass");
3063                                         break;
3064                                 }
3065
3066                                 switch (ins->opcode) {
3067                                 case OP_STOREV_MEMBASE:
3068                                         if (!addresses [ins->sreg1]) {
3069                                                 /* SIMD */
3070                                                 g_assert (values [ins->sreg1]);
3071                                                 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));
3072                                                 LLVMBuildStore (builder, values [ins->sreg1], dst);
3073                                                 done = TRUE;
3074                                         } else {
3075                                                 src = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMInt8Type (), 0), "");
3076                                                 dst = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_destbasereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (LLVMInt8Type (), 0));
3077                                         }
3078                                         break;
3079                                 case OP_LOADV_MEMBASE:
3080                                         if (!addresses [ins->dreg])
3081                                                 addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
3082                                         src = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_basereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (LLVMInt8Type (), 0));
3083                                         dst = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
3084                                         break;
3085                                 case OP_VMOVE:
3086                                         if (!addresses [ins->sreg1])
3087                                                 addresses [ins->sreg1] = build_alloca (ctx, &klass->byval_arg);
3088                                         if (!addresses [ins->dreg])
3089                                                 addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
3090                                         src = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMInt8Type (), 0), "");
3091                                         dst = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
3092                                         break;
3093                                 default:
3094                                         g_assert_not_reached ();
3095                                 }
3096
3097                                 if (done)
3098                                         break;
3099
3100                                 args [0] = dst;
3101                                 args [1] = src;
3102                                 args [2] = LLVMConstInt (LLVMInt32Type (), mono_class_value_size (klass, NULL), FALSE);
3103                                 args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
3104                                 // FIXME: Alignment
3105                                 args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
3106                                 LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.memcpy.i32"), args, 4, "");
3107                                 break;
3108                         }
3109                         case OP_LLVM_OUTARG_VT:
3110                                 if (!addresses [ins->sreg1]) {
3111                                         addresses [ins->sreg1] = build_alloca (ctx, &ins->klass->byval_arg);
3112                                         g_assert (values [ins->sreg1]);
3113                                         LLVMBuildStore (builder, values [ins->sreg1], addresses [ins->sreg1]);
3114                                 }
3115                                 addresses [ins->dreg] = addresses [ins->sreg1];
3116                                 break;
3117
3118                         /* 
3119                          * SIMD
3120                          */
3121 #if defined(TARGET_X86) || defined(TARGET_AMD64)
3122                         case OP_XZERO: {
3123                                 values [ins->dreg] = LLVMConstNull (type_to_llvm_type (ctx, &ins->klass->byval_arg));
3124                                 break;
3125                         }
3126                         case OP_LOADX_MEMBASE: {
3127                                 LLVMTypeRef t = type_to_llvm_type (ctx, &ins->klass->byval_arg);
3128                                 LLVMValueRef src;
3129
3130                                 src = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_basereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (t, 0));
3131                                 values [ins->dreg] = LLVMBuildLoad (builder, src, "");
3132                                 break;
3133                         }
3134                         case OP_ADDPD:
3135                         case OP_ADDPS:
3136                         case OP_PADDB:
3137                         case OP_PADDW:
3138                         case OP_PADDD:
3139                         case OP_PADDQ:
3140                                 values [ins->dreg] = LLVMBuildAdd (builder, lhs, rhs, "");
3141                                 break;
3142                         case OP_SUBPD:
3143                         case OP_SUBPS:
3144                         case OP_PSUBB:
3145                         case OP_PSUBW:
3146                         case OP_PSUBD:
3147                         case OP_PSUBQ:
3148                                 values [ins->dreg] = LLVMBuildSub (builder, lhs, rhs, "");
3149                                 break;
3150                         case OP_MULPD:
3151                         case OP_MULPS:
3152                                 values [ins->dreg] = LLVMBuildMul (builder, lhs, rhs, "");
3153                                 break;
3154                         case OP_DIVPD:
3155                         case OP_DIVPS:
3156                                 values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, "");
3157                                 break;
3158                         case OP_PAND:
3159                                 values [ins->dreg] = LLVMBuildAnd (builder, lhs, rhs, "");
3160                                 break;
3161                         case OP_POR:
3162                                 values [ins->dreg] = LLVMBuildOr (builder, lhs, rhs, "");
3163                                 break;
3164                         case OP_PXOR:
3165                                 values [ins->dreg] = LLVMBuildXor (builder, lhs, rhs, "");
3166                                 break;
3167                         case OP_ANDPS:
3168                         case OP_ANDNPS:
3169                         case OP_ORPS:
3170                         case OP_XORPS:
3171                         case OP_ANDPD:
3172                         case OP_ANDNPD:
3173                         case OP_ORPD:
3174                         case OP_XORPD: {
3175                                 LLVMTypeRef t, rt;
3176                                 LLVMValueRef v;
3177
3178                                 switch (ins->opcode) {
3179                                 case OP_ANDPS:
3180                                 case OP_ANDNPS:
3181                                 case OP_ORPS:
3182                                 case OP_XORPS:
3183                                         t = LLVMVectorType (LLVMInt32Type (), 4);
3184                                         rt = LLVMVectorType (LLVMFloatType (), 4);
3185                                         break;
3186                                 case OP_ANDPD:
3187                                 case OP_ANDNPD:
3188                                 case OP_ORPD:
3189                                 case OP_XORPD:
3190                                         t = LLVMVectorType (LLVMInt64Type (), 2);
3191                                         rt = LLVMVectorType (LLVMDoubleType (), 2);
3192                                         break;
3193                                 default:
3194                                         t = LLVMInt32Type ();
3195                                         rt = LLVMInt32Type ();
3196                                         g_assert_not_reached ();
3197                                 }
3198
3199                                 lhs = LLVMBuildBitCast (builder, lhs, t, "");
3200                                 rhs = LLVMBuildBitCast (builder, rhs, t, "");
3201                                 switch (ins->opcode) {
3202                                 case OP_ANDPS:
3203                                 case OP_ANDPD:
3204                                         v = LLVMBuildAnd (builder, lhs, rhs, "");
3205                                         break;
3206                                 case OP_ORPS:
3207                                 case OP_ORPD:
3208                                         v = LLVMBuildOr (builder, lhs, rhs, "");
3209                                         break;
3210                                 case OP_XORPS:
3211                                 case OP_XORPD:
3212                                         v = LLVMBuildXor (builder, lhs, rhs, "");
3213                                         break;
3214                                 case OP_ANDNPS:
3215                                 case OP_ANDNPD:
3216                                         v = LLVMBuildAnd (builder, lhs, LLVMBuildNot (builder, rhs, ""), "");
3217                                         break;
3218                                 }
3219                                 values [ins->dreg] = LLVMBuildBitCast (builder, v, rt, "");
3220                                 break;
3221                         }
3222                         case OP_MINPD:
3223                         case OP_MINPS:
3224                         case OP_MAXPD:
3225                         case OP_MAXPS:
3226                         case OP_PMIND_UN:
3227                         case OP_PMINW_UN:
3228                         case OP_PMINB_UN:
3229                         case OP_PMAXD_UN:
3230                         case OP_PMAXW_UN:
3231                         case OP_PMAXB_UN: {
3232                                 LLVMValueRef args [2];
3233
3234                                 args [0] = lhs;
3235                                 args [1] = rhs;
3236
3237                                 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, simd_op_to_intrins (ins->opcode)), args, 2, dname);
3238                                 break;
3239                         }
3240                         case OP_EXTRACT_R8:
3241                         case OP_EXTRACT_I8:
3242                         case OP_EXTRACT_I4:
3243                         case OP_EXTRACT_I2:
3244                         case OP_EXTRACT_U2:
3245                         case OP_EXTRACT_I1:
3246                         case OP_EXTRACT_U1: {
3247                                 LLVMTypeRef t;
3248
3249                                 switch (ins->opcode) {
3250                                 case OP_EXTRACT_R8:
3251                                         t = LLVMVectorType (LLVMDoubleType (), 2);
3252                                         break;
3253                                 case OP_EXTRACT_I8:
3254                                         t = LLVMVectorType (LLVMInt64Type (), 2);
3255                                         break;
3256                                 case OP_EXTRACT_I4:
3257                                         t = LLVMVectorType (LLVMInt32Type (), 4);
3258                                         break;
3259                                 case OP_EXTRACT_I2:
3260                                 case OP_EXTRACT_U2:
3261                                         t = LLVMVectorType (LLVMInt16Type (), 8);
3262                                         break;
3263                                 case OP_EXTRACT_I1:
3264                                 case OP_EXTRACT_U1:
3265                                         t = LLVMVectorType (LLVMInt8Type (), 16);
3266                                         break;
3267                                 default:
3268                                         t = LLVMInt32Type ();
3269                                         g_assert_not_reached ();
3270                                 }
3271
3272                                 lhs = LLVMBuildBitCast (builder, lhs, t, "");
3273                                 values [ins->dreg] = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), "");
3274                                 break;
3275                         }
3276 #endif
3277
3278                         case OP_DUMMY_USE:
3279                                 break;
3280
3281                         /*
3282                          * EXCEPTION HANDLING
3283                          */
3284                         case OP_IMPLICIT_EXCEPTION:
3285                                 /* This marks a place where an implicit exception can happen */
3286                                 if (bb->region != -1)
3287                                         LLVM_FAILURE (ctx, "implicit-exception");
3288                                 break;
3289                         case OP_THROW: {
3290                                 MonoMethodSignature *throw_sig;
3291                                 LLVMValueRef callee, arg;
3292
3293                                 if (!ctx->lmodule->throw) {
3294                                         throw_sig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
3295                                         throw_sig->ret = &mono_defaults.void_class->byval_arg;
3296                                         throw_sig->params [0] = &mono_defaults.object_class->byval_arg;
3297                                         if (cfg->compile_aot) {
3298                                                 callee = get_plt_entry (ctx, sig_to_llvm_sig (ctx, throw_sig, NULL), MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_exception");
3299                                         } else {
3300                                                 callee = LLVMAddFunction (module, "mono_arch_throw_exception", sig_to_llvm_sig (ctx, throw_sig, NULL));
3301
3302 #ifdef TARGET_X86
3303                                                 /* 
3304                                                  * LLVM doesn't push the exception argument, so we need a different
3305                                                  * trampoline.
3306                                                  */
3307                                                 LLVMAddGlobalMapping (ee, callee, resolve_patch (cfg, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_llvm_throw_exception"));
3308 #else
3309                                                 LLVMAddGlobalMapping (ee, callee, resolve_patch (cfg, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_exception"));
3310 #endif
3311                                         }
3312
3313                                         mono_memory_barrier ();
3314                                         ctx->lmodule->throw = callee;
3315                                 }
3316                                 arg = convert (ctx, values [ins->sreg1], type_to_llvm_type (ctx, &mono_defaults.object_class->byval_arg));
3317                                 emit_call (ctx, bb, &builder, ctx->lmodule->throw, &arg, 1);
3318                                 break;
3319                         }
3320                         case OP_CALL_HANDLER: {
3321                                 /* 
3322                                  * We don't 'call' handlers, but instead simply branch to them.
3323                                  * The code generated by ENDFINALLY will branch back to us.
3324                                  */
3325                                 LLVMBasicBlockRef finally_bb, noex_bb;
3326                                 GSList *bb_list;
3327
3328                                 finally_bb = get_bb (ctx, ins->inst_target_bb);
3329
3330                                 bb_list = bblocks [ins->inst_target_bb->block_num].call_handler_return_bbs;
3331
3332                                 /* 
3333                                  * Set the indicator variable for the finally clause.
3334                                  */
3335                                 lhs = bblocks [ins->inst_target_bb->block_num].finally_ind;
3336                                 g_assert (lhs);
3337                                 LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), g_slist_length (bb_list) + 1, FALSE), lhs);
3338                                 
3339                                 /* Branch to the finally clause */
3340                                 LLVMBuildBr (builder, finally_bb);
3341
3342                                 noex_bb = gen_bb (ctx, "CALL_HANDLER_CONT_BB");
3343                                 // FIXME: Use a mempool
3344                                 bblocks [ins->inst_target_bb->block_num].call_handler_return_bbs = g_slist_append (bblocks [ins->inst_target_bb->block_num].call_handler_return_bbs, noex_bb);
3345
3346                                 builder = ctx->builder = create_builder (ctx);
3347                                 LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
3348
3349                                 bblocks [bb->block_num].end_bblock = noex_bb;
3350                                 break;
3351                         }
3352                         case OP_START_HANDLER: {
3353                                 break;
3354                         }
3355                         case OP_ENDFINALLY: {
3356                                 LLVMBasicBlockRef resume_bb;
3357                                 MonoBasicBlock *handler_bb;
3358                                 LLVMValueRef val, switch_ins;
3359                                 GSList *bb_list;
3360
3361                                 handler_bb = g_hash_table_lookup (ctx->region_to_handler, GUINT_TO_POINTER (mono_get_block_region_notry (cfg, bb->region)));
3362                                 g_assert (handler_bb);
3363                                 lhs = bblocks [handler_bb->block_num].finally_ind;
3364                                 g_assert (lhs);
3365
3366                                 bb_list = bblocks [handler_bb->block_num].call_handler_return_bbs;
3367
3368                                 resume_bb = gen_bb (ctx, "ENDFINALLY_RESUME_BB");
3369
3370                                 /* Load the finally variable */
3371                                 val = LLVMBuildLoad (builder, lhs, "");
3372
3373                                 /* Reset the variable */
3374                                 LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), lhs);
3375
3376                                 /* Branch to either resume_bb, or to the bblocks in bb_list */
3377                                 switch_ins = LLVMBuildSwitch (builder, val, resume_bb, g_slist_length (bb_list));
3378                                 /* 
3379                                  * The other targets are added at the end to handle OP_CALL_HANDLER
3380                                  * opcodes processed later.
3381                                  */
3382                                 bblocks [handler_bb->block_num].endfinally_switch = switch_ins;
3383                                 /*
3384                                 for (i = 0; i < g_slist_length (bb_list); ++i)
3385                                         LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), i + 1, FALSE), g_slist_nth (bb_list, i)->data);
3386                                 */
3387
3388                                 builder = ctx->builder = create_builder (ctx);
3389                                 LLVMPositionBuilderAtEnd (ctx->builder, resume_bb);
3390
3391                                 LLVMBuildCall (builder, LLVMGetNamedFunction (module, "mono_resume_unwind"), NULL, 0, "");
3392                                 LLVMBuildUnreachable (builder);
3393                                 has_terminator = TRUE;
3394                                 break;
3395                         }
3396                         default: {
3397                                 char reason [128];
3398
3399                                 sprintf (reason, "opcode %s", mono_inst_name (ins->opcode));
3400                                 LLVM_FAILURE (ctx, reason);
3401                                 break;
3402                         }
3403                         }
3404
3405                         /* Convert the value to the type required by phi nodes */
3406                         if (spec [MONO_INST_DEST] != ' ' && !MONO_IS_STORE_MEMBASE (ins) && vreg_types [ins->dreg]) {
3407                                 if (!values [ins->dreg])
3408                                         /* vtypes */
3409                                         values [ins->dreg] = addresses [ins->dreg];
3410                                 else
3411                                         values [ins->dreg] = convert (ctx, values [ins->dreg], vreg_types [ins->dreg]);
3412                         }
3413
3414                         /* Add stores for volatile variables */
3415                         if (spec [MONO_INST_DEST] != ' ' && spec [MONO_INST_DEST] != 'v' && !MONO_IS_STORE_MEMBASE (ins))
3416                                 emit_volatile_store (ctx, ins->dreg);
3417                 }
3418
3419                 if (!has_terminator && bb->next_bb && (bb == cfg->bb_entry || bb->in_count > 0))
3420                         LLVMBuildBr (builder, get_bb (ctx, bb->next_bb));
3421
3422                 if (bb == cfg->bb_exit && sig->ret->type == MONO_TYPE_VOID)
3423                         LLVMBuildRetVoid (builder);
3424
3425                 if (bb == cfg->bb_entry)
3426                         ctx->last_alloca = LLVMGetLastInstruction (get_bb (ctx, cfg->bb_entry));
3427         }
3428
3429         /* Add incoming phi values */
3430         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3431                 GSList *l, *ins_list;
3432
3433                 ins_list = bblocks [bb->block_num].phi_nodes;
3434
3435                 for (l = ins_list; l; l = l->next) {
3436                         PhiNode *node = l->data;
3437                         MonoInst *phi = node->phi;
3438                         int sreg1 = node->sreg;
3439                         LLVMBasicBlockRef in_bb;
3440
3441                         if (sreg1 == -1)
3442                                 continue;
3443
3444                         in_bb = get_end_bb (ctx, node->in_bb);
3445
3446                         if (unreachable [node->in_bb->block_num])
3447                                 continue;
3448
3449                         g_assert (values [sreg1]);
3450
3451                         g_assert (LLVMTypeOf (values [sreg1]) == LLVMTypeOf (values [phi->dreg]));
3452                         LLVMAddIncoming (values [phi->dreg], &values [sreg1], &in_bb, 1);
3453                 }
3454         }
3455
3456         /* Create the SWITCH statements for ENDFINALLY instructions */
3457         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3458                 if (bblocks [bb->block_num].endfinally_switch) {
3459                         LLVMValueRef switch_ins = bblocks [bb->block_num].endfinally_switch;
3460                         GSList *bb_list = bblocks [bb->block_num].call_handler_return_bbs;
3461
3462                         for (i = 0; i < g_slist_length (bb_list); ++i)
3463                                 LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), i + 1, FALSE), g_slist_nth (bb_list, i)->data);
3464                 }
3465         }
3466
3467         if (cfg->verbose_level > 1)
3468                 mono_llvm_dump_value (method);
3469
3470         mark_as_used (module, method);
3471
3472         if (cfg->compile_aot) {
3473                 /* Don't generate native code, keep the LLVM IR */
3474
3475                 /* Can't delete the method if it has an alias, so only add it if successful */
3476                 if (debug_name) {
3477                         debug_alias = LLVMAddAlias (module, LLVMTypeOf (method), method, debug_name);
3478                         LLVMSetLinkage (debug_alias, LLVMInternalLinkage);
3479                         LLVMSetVisibility (debug_alias, LLVMHiddenVisibility);
3480                 }
3481
3482                 if (cfg->compile_aot && cfg->verbose_level)
3483                         printf ("%s emitted as %s\n", mono_method_full_name (cfg->method, TRUE), method_name);
3484
3485                 //LLVMVerifyFunction(method, 0);
3486         } else {
3487                 mono_llvm_optimize_method (method);
3488
3489                 if (cfg->verbose_level > 1)
3490                         mono_llvm_dump_value (method);
3491
3492                 cfg->native_code = LLVMGetPointerToGlobal (ee, method);
3493
3494                 /* Set by emit_cb */
3495                 g_assert (cfg->code_len);
3496
3497                 /* FIXME: Free the LLVM IL for the function */
3498         }
3499
3500         goto CLEANUP;
3501
3502  FAILURE:
3503
3504         if (method) {
3505                 /* Need to add unused phi nodes as they can be referenced by other values */
3506                 LLVMBasicBlockRef phi_bb = LLVMAppendBasicBlock (method, "PHI_BB");
3507                 LLVMBuilderRef builder;
3508
3509                 builder = create_builder (ctx);
3510                 LLVMPositionBuilderAtEnd (builder, phi_bb);
3511
3512                 for (i = 0; i < phi_values->len; ++i) {
3513                         LLVMValueRef v = g_ptr_array_index (phi_values, i);
3514                         if (LLVMGetInstructionParent (v) == NULL)
3515                                 LLVMInsertIntoBuilder (builder, v);
3516                 }
3517                 
3518                 LLVMDeleteFunction (method);
3519         }
3520
3521  CLEANUP:
3522         g_free (values);
3523         g_free (addresses);
3524         g_free (vreg_types);
3525         g_free (vreg_cli_types);
3526         g_free (pindexes);
3527         g_free (debug_name);
3528         g_ptr_array_free (phi_values, TRUE);
3529         g_free (ctx->bblocks);
3530         g_hash_table_destroy (ctx->region_to_handler);
3531         g_free (method_name);
3532         g_ptr_array_free (bblock_list, TRUE);
3533
3534         for (l = ctx->builders; l; l = l->next) {
3535                 LLVMBuilderRef builder = l->data;
3536                 LLVMDisposeBuilder (builder);
3537         }
3538
3539         g_free (ctx);
3540
3541         TlsSetValue (current_cfg_tls_id, NULL);
3542
3543         mono_loader_unlock ();
3544 }
3545
3546 /*
3547  * mono_llvm_emit_call:
3548  *
3549  *   Same as mono_arch_emit_call () for LLVM.
3550  */
3551 void
3552 mono_llvm_emit_call (MonoCompile *cfg, MonoCallInst *call)
3553 {
3554         MonoInst *in;
3555         MonoMethodSignature *sig;
3556         int i, n, stack_size;
3557         LLVMArgInfo *ainfo;
3558
3559         stack_size = 0;
3560
3561         sig = call->signature;
3562         n = sig->param_count + sig->hasthis;
3563
3564         call->cinfo = mono_arch_get_llvm_call_info (cfg, sig);
3565
3566         if (cfg->disable_llvm)
3567                 return;
3568
3569         if (sig->call_convention == MONO_CALL_VARARG) {
3570                 cfg->exception_message = g_strdup ("varargs");
3571                 cfg->disable_llvm = TRUE;
3572         }
3573
3574         for (i = 0; i < n; ++i) {
3575                 MonoInst *ins;
3576
3577                 ainfo = call->cinfo->args + i;
3578
3579                 in = call->args [i];
3580                         
3581                 /* Simply remember the arguments */
3582                 switch (ainfo->storage) {
3583                 case LLVMArgInIReg:
3584                         MONO_INST_NEW (cfg, ins, OP_MOVE);
3585                         ins->dreg = mono_alloc_ireg (cfg);
3586                         ins->sreg1 = in->dreg;
3587                         break;
3588                 case LLVMArgInFPReg:
3589                         MONO_INST_NEW (cfg, ins, OP_FMOVE);
3590                         ins->dreg = mono_alloc_freg (cfg);
3591                         ins->sreg1 = in->dreg;
3592                         break;
3593                 case LLVMArgVtypeByVal:
3594                 case LLVMArgVtypeInReg:
3595                         MONO_INST_NEW (cfg, ins, OP_LLVM_OUTARG_VT);
3596                         ins->dreg = mono_alloc_ireg (cfg);
3597                         ins->sreg1 = in->dreg;
3598                         ins->klass = mono_class_from_mono_type (sig->params [i - sig->hasthis]);
3599                         break;
3600                 default:
3601                         call->cinfo = mono_arch_get_llvm_call_info (cfg, sig);
3602                         cfg->exception_message = g_strdup ("ainfo->storage");
3603                         cfg->disable_llvm = TRUE;
3604                         return;
3605                 }
3606
3607                 if (!cfg->disable_llvm) {
3608                         MONO_ADD_INS (cfg->cbb, ins);
3609                         mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, 0, FALSE);
3610                 }
3611         }
3612 }
3613
3614 static unsigned char*
3615 alloc_cb (LLVMValueRef function, int size)
3616 {
3617         MonoCompile *cfg;
3618
3619         cfg = TlsGetValue (current_cfg_tls_id);
3620
3621         if (cfg) {
3622                 // FIXME: dynamic
3623                 return mono_domain_code_reserve (cfg->domain, size);
3624         } else {
3625                 return mono_domain_code_reserve (mono_domain_get (), size);
3626         }
3627 }
3628
3629 static void
3630 emitted_cb (LLVMValueRef function, void *start, void *end)
3631 {
3632         MonoCompile *cfg;
3633
3634         cfg = TlsGetValue (current_cfg_tls_id);
3635         g_assert (cfg);
3636         cfg->code_len = (guint8*)end - (guint8*)start;
3637 }
3638
3639 static void
3640 exception_cb (void *data)
3641 {
3642         MonoCompile *cfg;
3643         MonoJitExceptionInfo *ei;
3644         guint32 ei_len, i;
3645         gpointer *type_info;
3646
3647         cfg = TlsGetValue (current_cfg_tls_id);
3648         g_assert (cfg);
3649
3650         /*
3651          * data points to a DWARF FDE structure, convert it to our unwind format and
3652          * save it.
3653          * An alternative would be to save it directly, and modify our unwinder to work
3654          * with it.
3655          */
3656         cfg->encoded_unwind_ops = mono_unwind_decode_fde ((guint8*)data, &cfg->encoded_unwind_ops_len, NULL, &ei, &ei_len, &type_info);
3657
3658         cfg->llvm_ex_info = mono_mempool_alloc0 (cfg->mempool, ei_len * sizeof (MonoJitExceptionInfo));
3659         cfg->llvm_ex_info_len = ei_len;
3660         memcpy (cfg->llvm_ex_info, ei, ei_len * sizeof (MonoJitExceptionInfo));
3661         /* Fill the rest of the information from the type info */
3662         for (i = 0; i < ei_len; ++i) {
3663                 gint32 clause_index = *(gint32*)type_info [i];
3664                 MonoExceptionClause *clause = &mono_method_get_header (cfg->method)->clauses [clause_index];
3665
3666                 cfg->llvm_ex_info [i].flags = clause->flags;
3667                 cfg->llvm_ex_info [i].data.catch_class = clause->data.catch_class;
3668         }
3669
3670         g_free (ei);
3671 }
3672
3673 static void
3674 add_intrinsics (LLVMModuleRef module)
3675 {
3676         /* Emit declarations of instrinsics */
3677         {
3678                 LLVMTypeRef memset_params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMInt8Type (), LLVMInt32Type (), LLVMInt32Type () };
3679
3680                 LLVMAddFunction (module, "llvm.memset.i32", LLVMFunctionType (LLVMVoidType (), memset_params, 4, FALSE));
3681         }
3682
3683         {
3684                 LLVMTypeRef memcpy_params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMPointerType (LLVMInt8Type (), 0), LLVMInt32Type (), LLVMInt32Type () };
3685
3686                 LLVMAddFunction (module, "llvm.memcpy.i32", LLVMFunctionType (LLVMVoidType (), memcpy_params, 4, FALSE));
3687         }
3688
3689         {
3690                 LLVMTypeRef params [] = { LLVMDoubleType () };
3691
3692                 LLVMAddFunction (module, "llvm.sin.f64", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
3693                 LLVMAddFunction (module, "llvm.cos.f64", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
3694                 LLVMAddFunction (module, "llvm.sqrt.f64", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
3695
3696                 /* This isn't an intrinsic, instead llvm seems to special case it by name */
3697                 LLVMAddFunction (module, "fabs", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
3698         }
3699
3700         {
3701                 LLVMTypeRef membar_params [] = { LLVMInt1Type (), LLVMInt1Type (), LLVMInt1Type (), LLVMInt1Type (), LLVMInt1Type () };
3702
3703                 LLVMAddFunction (module, "llvm.atomic.swap.i32.p0i32", LLVMFunctionType2 (LLVMInt32Type (), LLVMPointerType (LLVMInt32Type (), 0), LLVMInt32Type (), FALSE));
3704                 LLVMAddFunction (module, "llvm.atomic.swap.i64.p0i64", LLVMFunctionType2 (LLVMInt64Type (), LLVMPointerType (LLVMInt64Type (), 0), LLVMInt64Type (), FALSE));
3705                 LLVMAddFunction (module, "llvm.atomic.load.add.i32.p0i32", LLVMFunctionType2 (LLVMInt32Type (), LLVMPointerType (LLVMInt32Type (), 0), LLVMInt32Type (), FALSE));
3706                 LLVMAddFunction (module, "llvm.atomic.load.add.i64.p0i64", LLVMFunctionType2 (LLVMInt64Type (), LLVMPointerType (LLVMInt64Type (), 0), LLVMInt64Type (), FALSE));
3707                 LLVMAddFunction (module, "llvm.atomic.cmp.swap.i32.p0i32", LLVMFunctionType3 (LLVMInt32Type (), LLVMPointerType (LLVMInt32Type (), 0), LLVMInt32Type (), LLVMInt32Type (), FALSE));
3708                 LLVMAddFunction (module, "llvm.atomic.cmp.swap.i64.p0i64", LLVMFunctionType3 (LLVMInt64Type (), LLVMPointerType (LLVMInt64Type (), 0), LLVMInt64Type (), LLVMInt64Type (), FALSE));
3709                 LLVMAddFunction (module, "llvm.memory.barrier", LLVMFunctionType (LLVMVoidType (), membar_params, 5, FALSE));
3710         }
3711
3712         {
3713                 LLVMTypeRef ovf_res_i32 [] = { LLVMInt32Type (), LLVMInt1Type () };
3714                 LLVMTypeRef ovf_params_i32 [] = { LLVMInt32Type (), LLVMInt32Type () };
3715
3716                 LLVMAddFunction (module, "llvm.sadd.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
3717                 LLVMAddFunction (module, "llvm.uadd.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
3718                 LLVMAddFunction (module, "llvm.ssub.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
3719                 LLVMAddFunction (module, "llvm.usub.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
3720                 LLVMAddFunction (module, "llvm.smul.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
3721                 LLVMAddFunction (module, "llvm.umul.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
3722         }
3723
3724         {
3725                 LLVMTypeRef ovf_res_i64 [] = { LLVMInt64Type (), LLVMInt1Type () };
3726                 LLVMTypeRef ovf_params_i64 [] = { LLVMInt64Type (), LLVMInt64Type () };
3727
3728                 LLVMAddFunction (module, "llvm.sadd.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
3729                 LLVMAddFunction (module, "llvm.uadd.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
3730                 LLVMAddFunction (module, "llvm.ssub.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
3731                 LLVMAddFunction (module, "llvm.usub.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
3732                 LLVMAddFunction (module, "llvm.smul.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
3733                 LLVMAddFunction (module, "llvm.umul.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
3734         }
3735
3736         {
3737                 LLVMTypeRef struct_ptr = LLVMPointerType (LLVMStructType (NULL, 0, FALSE), 0);
3738                 LLVMTypeRef invariant_start_params [] = { LLVMInt64Type (), LLVMPointerType (LLVMInt8Type (), 0) };
3739                 LLVMTypeRef invariant_end_params [] = { struct_ptr, LLVMInt64Type (), LLVMPointerType (LLVMInt8Type (), 0) };
3740
3741                 LLVMAddFunction (module, "llvm.invariant.start", LLVMFunctionType (struct_ptr, invariant_start_params, 2, FALSE));
3742
3743                 LLVMAddFunction (module, "llvm.invariant.end", LLVMFunctionType (LLVMVoidType (), invariant_end_params, 3, FALSE));
3744         }
3745
3746         /* EH intrinsics */
3747         {
3748                 LLVMTypeRef arg_types [2];
3749
3750                 arg_types [0] = LLVMPointerType (LLVMInt8Type (), 0);
3751                 arg_types [1] = LLVMPointerType (LLVMInt8Type (), 0);
3752                 LLVMAddFunction (module, "llvm.eh.selector", LLVMFunctionType (LLVMInt32Type (), arg_types, 2, TRUE));
3753
3754                 LLVMAddFunction (module, "llvm.eh.exception", LLVMFunctionType (LLVMPointerType (LLVMInt8Type (), 0), NULL, 0, FALSE));
3755
3756                 LLVMAddFunction (module, "mono_personality", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
3757
3758                 LLVMAddFunction (module, "mono_resume_unwind", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
3759         }
3760
3761         /* SSE intrinsics */
3762         {
3763                 LLVMTypeRef vector_type, arg_types [2];
3764
3765                 vector_type = LLVMVectorType (LLVMInt32Type (), 4);
3766                 arg_types [0] = vector_type;
3767                 arg_types [1] = vector_type;
3768                 LLVMAddFunction (module, "llvm.x86.sse41.pminud", LLVMFunctionType (vector_type, arg_types, 2, FALSE));                                 
3769                 LLVMAddFunction (module, "llvm.x86.sse41.pmaxud", LLVMFunctionType (vector_type, arg_types, 2, FALSE));                                 
3770
3771                 vector_type = LLVMVectorType (LLVMInt16Type (), 8);
3772                 arg_types [0] = vector_type;
3773                 arg_types [1] = vector_type;
3774                 LLVMAddFunction (module, "llvm.x86.sse41.pminuw", LLVMFunctionType (vector_type, arg_types, 2, FALSE));                                 
3775                 LLVMAddFunction (module, "llvm.x86.sse41.pmaxuw", LLVMFunctionType (vector_type, arg_types, 2, FALSE));                                 
3776
3777                 vector_type = LLVMVectorType (LLVMInt8Type (), 16);
3778                 arg_types [0] = vector_type;
3779                 arg_types [1] = vector_type;
3780                 LLVMAddFunction (module, "llvm.x86.sse41.pminub", LLVMFunctionType (vector_type, arg_types, 2, FALSE));                                 
3781                 LLVMAddFunction (module, "llvm.x86.sse41.pmaxub", LLVMFunctionType (vector_type, arg_types, 2, FALSE));                                 
3782
3783                 vector_type = LLVMVectorType (LLVMDoubleType (), 2);
3784                 arg_types [0] = vector_type;
3785                 arg_types [1] = vector_type;
3786                 LLVMAddFunction (module, "llvm.x86.sse2.min.pd", LLVMFunctionType (vector_type, arg_types, 2, FALSE));                                  
3787                 LLVMAddFunction (module, "llvm.x86.sse2.max.pd", LLVMFunctionType (vector_type, arg_types, 2, FALSE));                                  
3788
3789                 vector_type = LLVMVectorType (LLVMFloatType (), 4);
3790                 arg_types [0] = vector_type;
3791                 arg_types [1] = vector_type;
3792                 LLVMAddFunction (module, "llvm.x86.sse2.min.ps", LLVMFunctionType (vector_type, arg_types, 2, FALSE));                                  
3793                 LLVMAddFunction (module, "llvm.x86.sse2.max.ps", LLVMFunctionType (vector_type, arg_types, 2, FALSE));                                  
3794         }
3795 }
3796
3797 void
3798 mono_llvm_init (void)
3799 {
3800         current_cfg_tls_id = TlsAlloc ();
3801 }
3802
3803 static void
3804 init_jit_module (void)
3805 {
3806         if (jit_module_inited)
3807                 return;
3808
3809         mono_loader_lock ();
3810
3811         if (jit_module_inited) {
3812                 mono_loader_unlock ();
3813                 return;
3814         }
3815
3816         jit_module.module = LLVMModuleCreateWithName ("mono");
3817
3818         ee = mono_llvm_create_ee (LLVMCreateModuleProviderForExistingModule (jit_module.module), alloc_cb, emitted_cb, exception_cb);
3819
3820         add_intrinsics (jit_module.module);
3821
3822         jit_module.llvm_types = g_hash_table_new (NULL, NULL);
3823
3824         LLVMAddGlobalMapping (ee, LLVMGetNamedFunction (jit_module.module, "mono_resume_unwind"), mono_resume_unwind);
3825
3826         jit_module_inited = TRUE;
3827
3828         mono_loader_unlock ();
3829 }
3830
3831 void
3832 mono_llvm_cleanup (void)
3833 {
3834         if (ee)
3835                 mono_llvm_dispose_ee (ee);
3836
3837         if (jit_module.llvm_types)
3838                 g_hash_table_destroy (jit_module.llvm_types);
3839 }
3840
3841 void
3842 mono_llvm_create_aot_module (const char *got_symbol)
3843 {
3844         /* Delete previous module */
3845         if (aot_module.plt_entries)
3846                 g_hash_table_destroy (aot_module.plt_entries);
3847
3848         memset (&aot_module, 0, sizeof (aot_module));
3849
3850         aot_module.module = LLVMModuleCreateWithName ("aot");
3851         aot_module.got_symbol = got_symbol;
3852
3853         add_intrinsics (aot_module.module);
3854
3855         /* Add GOT */
3856         /*
3857          * We couldn't compute the type of the LLVM global representing the got because
3858          * its size is only known after all the methods have been emitted. So create
3859          * a dummy variable, and replace all uses it with the real got variable when
3860          * its size is known in mono_llvm_emit_aot_module ().
3861          */
3862         {
3863                 LLVMTypeRef got_type = LLVMArrayType (IntPtrType (), 0);
3864
3865                 aot_module.got_var = LLVMAddGlobal (aot_module.module, got_type, "mono_dummy_got");
3866                 LLVMSetInitializer (aot_module.got_var, LLVMConstNull (got_type));
3867         }
3868
3869         /* Add a dummy personality function */
3870         {
3871                 LLVMBasicBlockRef lbb;
3872                 LLVMBuilderRef lbuilder;
3873                 LLVMValueRef personality;
3874
3875                 personality = LLVMAddFunction (aot_module.module, "mono_aot_personality", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
3876                 LLVMSetLinkage (personality, LLVMPrivateLinkage);
3877                 lbb = LLVMAppendBasicBlock (personality, "BB0");
3878                 lbuilder = LLVMCreateBuilder ();
3879                 LLVMPositionBuilderAtEnd (lbuilder, lbb);
3880                 LLVMBuildRetVoid (lbuilder);
3881         }
3882
3883         aot_module.llvm_types = g_hash_table_new (NULL, NULL);
3884         aot_module.plt_entries = g_hash_table_new (g_str_hash, g_str_equal);
3885 }
3886
3887 /*
3888  * Emit the aot module into the LLVM bitcode file FILENAME.
3889  */
3890 void
3891 mono_llvm_emit_aot_module (const char *filename, int got_size)
3892 {
3893         LLVMTypeRef got_type;
3894         LLVMValueRef real_got;
3895
3896         /* 
3897          * Create the real got variable and replace all uses of the dummy variable with
3898          * the real one.
3899          */
3900         got_type = LLVMArrayType (IntPtrType (), got_size);
3901         real_got = LLVMAddGlobal (aot_module.module, got_type, aot_module.got_symbol);
3902         LLVMSetInitializer (real_got, LLVMConstNull (got_type));
3903         LLVMSetLinkage (real_got, LLVMInternalLinkage);
3904
3905         mono_llvm_replace_uses_of (aot_module.got_var, real_got);
3906
3907         mark_as_used (aot_module.module, real_got);
3908
3909         /* Delete the dummy got so it doesn't become a global */
3910         LLVMDeleteGlobal (aot_module.got_var);
3911
3912 #if 0
3913         {
3914                 char *verifier_err;
3915
3916                 if (LLVMVerifyModule (aot_module.module, LLVMReturnStatusAction, &verifier_err)) {
3917                         g_assert_not_reached ();
3918                 }
3919         }
3920 #endif
3921
3922         LLVMWriteBitcodeToFile (aot_module.module, filename);
3923 }
3924
3925 /*
3926   DESIGN:
3927   - Emit LLVM IR from the mono IR using the LLVM C API.
3928   - The original arch specific code remains, so we can fall back to it if we run
3929     into something we can't handle.
3930   FIXME:
3931   - llvm's PrettyStackTrace class seems to register a signal handler which screws
3932     up our GC. Also, it calls sigaction () a _lot_ of times instead of just once.
3933 */
3934
3935 /*  
3936   A partial list of issues:
3937   - Handling of opcodes which can throw exceptions.
3938
3939       In the mono JIT, these are implemented using code like this:
3940           method:
3941       <compare>
3942           throw_pos:
3943           b<cond> ex_label
3944           <rest of code>
3945       ex_label:
3946           push throw_pos - method
3947           call <exception trampoline>
3948
3949           The problematic part is push throw_pos - method, which cannot be represented
3950       in the LLVM IR, since it does not support label values.
3951           -> this can be implemented in AOT mode using inline asm + labels, but cannot
3952           be implemented in JIT mode ?
3953           -> a possible but slower implementation would use the normal exception 
3954       throwing code but it would need to control the placement of the throw code
3955       (it needs to be exactly after the compare+branch).
3956           -> perhaps add a PC offset intrinsics ?
3957
3958   - efficient implementation of .ovf opcodes.
3959
3960           These are currently implemented as:
3961           <ins which sets the condition codes>
3962           b<cond> ex_label
3963
3964           Some overflow opcodes are now supported by LLVM SVN.
3965
3966   - exception handling, unwinding.
3967     - SSA is disabled for methods with exception handlers    
3968         - How to obtain unwind info for LLVM compiled methods ?
3969           -> this is now solved by converting the unwind info generated by LLVM
3970              into our format.
3971         - LLVM uses the c++ exception handling framework, while we use our home grown
3972       code, and couldn't use the c++ one:
3973       - its not supported under VC++, other exotic platforms.
3974           - it might be impossible to support filter clauses with it.
3975
3976   - trampolines.
3977   
3978     The trampolines need a predictable call sequence, since they need to disasm
3979     the calling code to obtain register numbers / offsets.
3980
3981     LLVM currently generates this code in non-JIT mode:
3982            mov    -0x98(%rax),%eax
3983            callq  *%rax
3984     Here, the vtable pointer is lost. 
3985     -> solution: use one vtable trampoline per class.
3986
3987   - passing/receiving the IMT pointer/RGCTX.
3988     -> solution: pass them as normal arguments ?
3989
3990   - argument passing.
3991   
3992           LLVM does not allow the specification of argument registers etc. This means
3993       that all calls are made according to the platform ABI.
3994
3995   - passing/receiving vtypes.
3996
3997       Vtypes passed/received in registers are handled by the front end by using
3998           a signature with scalar arguments, and loading the parts of the vtype into those
3999           arguments.
4000
4001           Vtypes passed on the stack are handled using the 'byval' attribute.
4002
4003   - ldaddr.
4004
4005     Supported though alloca, we need to emit the load/store code.
4006
4007   - types.
4008
4009     The mono JIT uses pointer sized iregs/double fregs, while LLVM uses precisely
4010     typed registers, so we have to keep track of the precise LLVM type of each vreg.
4011     This is made easier because the IR is already in SSA form.
4012     An additional problem is that our IR is not consistent with types, i.e. i32/ia64 
4013         types are frequently used incorrectly.
4014 */
4015
4016 /*
4017   AOT SUPPORT:
4018   Emit LLVM bytecode into a .bc file, compile it using llc into a .s file, then 
4019   append the AOT data structures to that file. For methods which cannot be
4020   handled by LLVM, the normal JIT compiled versions are used.
4021 */
4022
4023 /* FIXME: Normalize some aspects of the mono IR to allow easier translation, like:
4024  *   - each bblock should end with a branch
4025  *   - setting the return value, making cfg->ret non-volatile
4026  * - merge some changes back to HEAD, to reduce the differences.
4027  * - avoid some transformations in the JIT which make it harder for us to generate
4028  *   code.
4029  * - fix memory leaks.
4030  * - use pointer types to help optimizations.
4031  */