2 * mini-llvm.c: llvm "Backend" for the mono JIT
4 * Copyright 2009-2011 Novell Inc (http://www.novell.com)
5 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
9 #include <mono/metadata/debug-helpers.h>
10 #include <mono/metadata/debug-mono-symfile.h>
11 #include <mono/metadata/mempool-internals.h>
12 #include <mono/utils/mono-tls.h>
13 #include <mono/utils/mono-dl.h>
14 #include <mono/utils/mono-time.h>
15 #include <mono/utils/freebsd-dwarf.h>
17 #ifndef __STDC_LIMIT_MACROS
18 #define __STDC_LIMIT_MACROS
20 #ifndef __STDC_CONSTANT_MACROS
21 #define __STDC_CONSTANT_MACROS
24 #include "llvm-c/Core.h"
25 #include "llvm-c/ExecutionEngine.h"
26 #include "llvm-c/BitWriter.h"
27 #include "llvm-c/Analysis.h"
29 #include "mini-llvm-cpp.h"
34 extern void *memset(void *, int, size_t);
35 void bzero (void *to, size_t count) { memset (to, 0, count); }
39 #if LLVM_API_VERSION < 4
40 #error "The version of the mono llvm repository is too old."
44 * Information associated by mono with LLVM modules.
48 LLVMValueRef throw, rethrow, throw_corlib_exception;
49 GHashTable *llvm_types;
51 const char *got_symbol;
52 GHashTable *plt_entries;
53 GHashTable *plt_entries_ji;
54 GHashTable *method_to_lmethod;
59 GPtrArray *subprogram_mds;
61 LLVMExecutionEngineRef ee;
62 gboolean external_symbols;
68 * Information associated by the backend with mono basic blocks.
71 LLVMBasicBlockRef bblock, end_bblock;
72 LLVMValueRef finally_ind;
73 gboolean added, invoke_target;
75 * If this bblock is the start of a finally clause, this is a list of bblocks it
76 * needs to branch to in ENDFINALLY.
78 GSList *call_handler_return_bbs;
80 * If this bblock is the start of a finally clause, this is the bblock that
81 * CALL_HANDLER needs to branch to.
83 LLVMBasicBlockRef call_handler_target_bb;
84 /* The list of switch statements generated by ENDFINALLY instructions */
85 GSList *endfinally_switch_ins_list;
90 * Structure containing emit state
95 /* Maps method names to the corresponding LLVMValueRef */
96 GHashTable *emitted_method_decls;
100 MonoLLVMModule *lmodule;
101 LLVMModuleRef module;
103 int sindex, default_index, ex_index;
104 LLVMBuilderRef builder;
105 LLVMValueRef *values, *addresses;
106 MonoType **vreg_cli_types;
108 MonoMethodSignature *sig;
110 GHashTable *region_to_handler;
111 LLVMBuilderRef alloca_builder;
112 LLVMValueRef last_alloca;
113 LLVMValueRef rgctx_arg;
114 LLVMTypeRef *vreg_types;
116 gboolean *unreachable;
118 LLVMValueRef imt_rgctx_loc;
119 GHashTable *llvm_types;
121 MonoDebugMethodInfo *minfo;
128 MonoBasicBlock *in_bb;
133 * Instruction metadata
134 * This is the same as ins_info, but LREG != IREG.
142 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
143 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
150 /* keep in sync with the enum in mini.h */
153 #include "mini-ops.h"
158 #if SIZEOF_VOID_P == 4
159 #define GET_LONG_IMM(ins) (((guint64)(ins)->inst_ms_word << 32) | (guint64)(guint32)(ins)->inst_ls_word)
161 #define GET_LONG_IMM(ins) ((ins)->inst_imm)
164 #define LLVM_INS_INFO(opcode) (&llvm_ins_info [((opcode) - OP_START - 1) * 4])
167 #define TRACE_FAILURE(msg) do { printf ("%s\n", msg); } while (0)
169 #define TRACE_FAILURE(msg)
173 #define IS_TARGET_X86 1
175 #define IS_TARGET_X86 0
179 #define IS_TARGET_AMD64 1
181 #define IS_TARGET_AMD64 0
184 #define LLVM_FAILURE(ctx, reason) do { \
185 TRACE_FAILURE (reason); \
186 (ctx)->cfg->exception_message = g_strdup (reason); \
187 (ctx)->cfg->disable_llvm = TRUE; \
191 #define CHECK_FAILURE(ctx) do { \
192 if ((ctx)->cfg->disable_llvm) \
196 static LLVMIntPredicate cond_to_llvm_cond [] = {
209 static LLVMRealPredicate fpcond_to_llvm_cond [] = {
222 static MonoNativeTlsKey current_cfg_tls_id;
224 static MonoLLVMModule aot_module;
225 static int memset_param_count, memcpy_param_count;
226 static const char *memset_func_name;
227 static const char *memcpy_func_name;
229 static void init_jit_module (MonoDomain *domain);
231 static void emit_dbg_loc (EmitContext *ctx, LLVMBuilderRef builder, const unsigned char *cil_code);
232 static LLVMValueRef emit_dbg_subprogram (EmitContext *ctx, MonoCompile *cfg, LLVMValueRef method, const char *name);
233 static void emit_dbg_info (MonoLLVMModule *lmodule, const char *filename, const char *cu_name);
238 * The LLVM type with width == sizeof (gpointer)
243 return sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type ();
249 return sizeof (gpointer) == 8 ? LLVMPointerType (LLVMInt64Type (), 0) : LLVMPointerType (LLVMInt32Type (), 0);
255 return sizeof (gpointer) == 8 ? LLVMPointerType (LLVMInt64Type (), 0) : LLVMPointerType (LLVMInt32Type (), 0);
261 * Return the size of the LLVM representation of the vtype T.
264 get_vtype_size (MonoType *t)
268 size = mono_class_value_size (mono_class_from_mono_type (t), NULL);
270 while (size < 2 * sizeof (gpointer) && mono_is_power_of_two (size) == -1)
277 * simd_class_to_llvm_type:
279 * Return the LLVM type corresponding to the Mono.SIMD class KLASS
282 simd_class_to_llvm_type (EmitContext *ctx, MonoClass *klass)
284 if (!strcmp (klass->name, "Vector2d")) {
285 return LLVMVectorType (LLVMDoubleType (), 2);
286 } else if (!strcmp (klass->name, "Vector2l")) {
287 return LLVMVectorType (LLVMInt64Type (), 2);
288 } else if (!strcmp (klass->name, "Vector2ul")) {
289 return LLVMVectorType (LLVMInt64Type (), 2);
290 } else if (!strcmp (klass->name, "Vector4i")) {
291 return LLVMVectorType (LLVMInt32Type (), 4);
292 } else if (!strcmp (klass->name, "Vector4ui")) {
293 return LLVMVectorType (LLVMInt32Type (), 4);
294 } else if (!strcmp (klass->name, "Vector4f")) {
295 return LLVMVectorType (LLVMFloatType (), 4);
296 } else if (!strcmp (klass->name, "Vector8s")) {
297 return LLVMVectorType (LLVMInt16Type (), 8);
298 } else if (!strcmp (klass->name, "Vector8us")) {
299 return LLVMVectorType (LLVMInt16Type (), 8);
300 } else if (!strcmp (klass->name, "Vector16sb")) {
301 return LLVMVectorType (LLVMInt8Type (), 16);
302 } else if (!strcmp (klass->name, "Vector16b")) {
303 return LLVMVectorType (LLVMInt8Type (), 16);
305 printf ("%s\n", klass->name);
311 /* Return the 128 bit SIMD type corresponding to the mono type TYPE */
312 static inline G_GNUC_UNUSED LLVMTypeRef
313 type_to_simd_type (int type)
317 return LLVMVectorType (LLVMInt8Type (), 16);
319 return LLVMVectorType (LLVMInt16Type (), 8);
321 return LLVMVectorType (LLVMInt32Type (), 4);
323 return LLVMVectorType (LLVMInt64Type (), 2);
325 return LLVMVectorType (LLVMDoubleType (), 2);
327 return LLVMVectorType (LLVMFloatType (), 4);
329 g_assert_not_reached ();
335 create_llvm_type_for_type (MonoClass *klass)
337 int i, size, nfields, esize;
338 LLVMTypeRef *eltypes;
343 t = &klass->byval_arg;
345 if (mini_type_is_hfa (t, &nfields, &esize)) {
347 * This is needed on arm64 where HFAs are returned in
351 eltypes = g_new (LLVMTypeRef, size);
352 for (i = 0; i < size; ++i)
353 eltypes [i] = esize == 4 ? LLVMFloatType () : LLVMDoubleType ();
355 size = get_vtype_size (t);
357 eltypes = g_new (LLVMTypeRef, size);
358 for (i = 0; i < size; ++i)
359 eltypes [i] = LLVMInt8Type ();
362 name = mono_type_full_name (&klass->byval_arg);
363 ltype = LLVMStructCreateNamed (LLVMGetGlobalContext (), name);
364 LLVMStructSetBody (ltype, eltypes, size, FALSE);
374 * Return the LLVM type corresponding to T.
377 type_to_llvm_type (EmitContext *ctx, MonoType *t)
380 return LLVMPointerType (LLVMInt8Type (), 0);
382 t = mini_get_underlying_type (ctx->cfg, t);
385 return LLVMVoidType ();
387 return LLVMInt8Type ();
389 return LLVMInt16Type ();
391 return LLVMInt32Type ();
393 return LLVMInt8Type ();
395 return LLVMInt16Type ();
397 return LLVMInt32Type ();
398 case MONO_TYPE_BOOLEAN:
399 return LLVMInt8Type ();
402 return LLVMInt64Type ();
404 return LLVMInt16Type ();
406 return LLVMFloatType ();
408 return LLVMDoubleType ();
411 return IntPtrType ();
412 case MONO_TYPE_OBJECT:
413 case MONO_TYPE_CLASS:
414 case MONO_TYPE_ARRAY:
415 case MONO_TYPE_SZARRAY:
416 case MONO_TYPE_STRING:
418 return ObjRefType ();
421 /* Because of generic sharing */
422 return ObjRefType ();
423 case MONO_TYPE_GENERICINST:
424 if (!mono_type_generic_inst_is_valuetype (t))
425 return ObjRefType ();
427 case MONO_TYPE_VALUETYPE:
428 case MONO_TYPE_TYPEDBYREF: {
432 klass = mono_class_from_mono_type (t);
434 if (MONO_CLASS_IS_SIMD (ctx->cfg, klass))
435 return simd_class_to_llvm_type (ctx, klass);
438 return type_to_llvm_type (ctx, mono_class_enum_basetype (klass));
440 ltype = g_hash_table_lookup (ctx->lmodule->llvm_types, klass);
442 ltype = create_llvm_type_for_type (klass);
443 g_hash_table_insert (ctx->lmodule->llvm_types, klass, ltype);
449 printf ("X: %d\n", t->type);
450 ctx->cfg->exception_message = g_strdup_printf ("type %s", mono_type_full_name (t));
451 ctx->cfg->disable_llvm = TRUE;
459 * Return whenever T is an unsigned int type.
462 type_is_unsigned (EmitContext *ctx, MonoType *t)
479 * type_to_llvm_arg_type:
481 * Same as type_to_llvm_type, but treat i8/i16 as i32.
484 type_to_llvm_arg_type (EmitContext *ctx, MonoType *t)
486 LLVMTypeRef ptype = type_to_llvm_type (ctx, t);
489 * This works on all abis except arm64/ios which passes multiple
490 * arguments in one stack slot.
493 if (ptype == LLVMInt8Type () || ptype == LLVMInt16Type ()) {
495 * LLVM generates code which only sets the lower bits, while JITted
496 * code expects all the bits to be set.
498 ptype = LLVMInt32Type ();
506 * llvm_type_to_stack_type:
508 * Return the LLVM type which needs to be used when a value of type TYPE is pushed
511 static G_GNUC_UNUSED LLVMTypeRef
512 llvm_type_to_stack_type (MonoCompile *cfg, LLVMTypeRef type)
516 if (type == LLVMInt8Type ())
517 return LLVMInt32Type ();
518 else if (type == LLVMInt16Type ())
519 return LLVMInt32Type ();
520 else if (!cfg->r4fp && type == LLVMFloatType ())
521 return LLVMDoubleType ();
527 * regtype_to_llvm_type:
529 * Return the LLVM type corresponding to the regtype C used in instruction
533 regtype_to_llvm_type (char c)
537 return LLVMInt32Type ();
539 return LLVMInt64Type ();
541 return LLVMDoubleType ();
550 * Return the LLVM type corresponding to the unary/binary opcode OPCODE.
553 op_to_llvm_type (int opcode)
558 return LLVMInt8Type ();
561 return LLVMInt8Type ();
564 return LLVMInt16Type ();
567 return LLVMInt16Type ();
570 return LLVMInt32Type ();
573 return LLVMInt32Type ();
575 return LLVMInt64Type ();
577 return LLVMFloatType ();
579 return LLVMDoubleType ();
581 return LLVMInt64Type ();
583 return LLVMInt32Type ();
585 return LLVMInt64Type ();
590 return LLVMInt8Type ();
595 return LLVMInt16Type ();
598 return sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type ();
605 return LLVMInt32Type ();
612 return LLVMInt64Type ();
614 printf ("%s\n", mono_inst_name (opcode));
615 g_assert_not_reached ();
621 * load_store_to_llvm_type:
623 * Return the size/sign/zero extension corresponding to the load/store opcode
627 load_store_to_llvm_type (int opcode, int *size, gboolean *sext, gboolean *zext)
633 case OP_LOADI1_MEMBASE:
634 case OP_STOREI1_MEMBASE_REG:
635 case OP_STOREI1_MEMBASE_IMM:
636 case OP_ATOMIC_LOAD_I1:
637 case OP_ATOMIC_STORE_I1:
640 return LLVMInt8Type ();
641 case OP_LOADU1_MEMBASE:
643 case OP_ATOMIC_LOAD_U1:
644 case OP_ATOMIC_STORE_U1:
647 return LLVMInt8Type ();
648 case OP_LOADI2_MEMBASE:
649 case OP_STOREI2_MEMBASE_REG:
650 case OP_STOREI2_MEMBASE_IMM:
651 case OP_ATOMIC_LOAD_I2:
652 case OP_ATOMIC_STORE_I2:
655 return LLVMInt16Type ();
656 case OP_LOADU2_MEMBASE:
658 case OP_ATOMIC_LOAD_U2:
659 case OP_ATOMIC_STORE_U2:
662 return LLVMInt16Type ();
663 case OP_LOADI4_MEMBASE:
664 case OP_LOADU4_MEMBASE:
667 case OP_STOREI4_MEMBASE_REG:
668 case OP_STOREI4_MEMBASE_IMM:
669 case OP_ATOMIC_LOAD_I4:
670 case OP_ATOMIC_STORE_I4:
671 case OP_ATOMIC_LOAD_U4:
672 case OP_ATOMIC_STORE_U4:
674 return LLVMInt32Type ();
675 case OP_LOADI8_MEMBASE:
677 case OP_STOREI8_MEMBASE_REG:
678 case OP_STOREI8_MEMBASE_IMM:
679 case OP_ATOMIC_LOAD_I8:
680 case OP_ATOMIC_STORE_I8:
681 case OP_ATOMIC_LOAD_U8:
682 case OP_ATOMIC_STORE_U8:
684 return LLVMInt64Type ();
685 case OP_LOADR4_MEMBASE:
686 case OP_STORER4_MEMBASE_REG:
687 case OP_ATOMIC_LOAD_R4:
688 case OP_ATOMIC_STORE_R4:
690 return LLVMFloatType ();
691 case OP_LOADR8_MEMBASE:
692 case OP_STORER8_MEMBASE_REG:
693 case OP_ATOMIC_LOAD_R8:
694 case OP_ATOMIC_STORE_R8:
696 return LLVMDoubleType ();
697 case OP_LOAD_MEMBASE:
699 case OP_STORE_MEMBASE_REG:
700 case OP_STORE_MEMBASE_IMM:
701 *size = sizeof (gpointer);
702 return IntPtrType ();
704 g_assert_not_reached ();
712 * Return the LLVM intrinsics corresponding to the overflow opcode OPCODE.
715 ovf_op_to_intrins (int opcode)
719 return "llvm.sadd.with.overflow.i32";
721 return "llvm.uadd.with.overflow.i32";
723 return "llvm.ssub.with.overflow.i32";
725 return "llvm.usub.with.overflow.i32";
727 return "llvm.smul.with.overflow.i32";
729 return "llvm.umul.with.overflow.i32";
731 return "llvm.sadd.with.overflow.i64";
733 return "llvm.uadd.with.overflow.i64";
735 return "llvm.ssub.with.overflow.i64";
737 return "llvm.usub.with.overflow.i64";
739 return "llvm.smul.with.overflow.i64";
741 return "llvm.umul.with.overflow.i64";
743 g_assert_not_reached ();
749 simd_op_to_intrins (int opcode)
752 #if defined(TARGET_X86) || defined(TARGET_AMD64)
754 return "llvm.x86.sse2.min.pd";
756 return "llvm.x86.sse.min.ps";
758 return "llvm.x86.sse41.pminud";
760 return "llvm.x86.sse41.pminuw";
762 return "llvm.x86.sse2.pminu.b";
764 return "llvm.x86.sse2.pmins.w";
766 return "llvm.x86.sse2.max.pd";
768 return "llvm.x86.sse.max.ps";
770 return "llvm.x86.sse3.hadd.pd";
772 return "llvm.x86.sse3.hadd.ps";
774 return "llvm.x86.sse3.hsub.pd";
776 return "llvm.x86.sse3.hsub.ps";
778 return "llvm.x86.sse41.pmaxud";
780 return "llvm.x86.sse41.pmaxuw";
782 return "llvm.x86.sse2.pmaxu.b";
784 return "llvm.x86.sse3.addsub.ps";
786 return "llvm.x86.sse3.addsub.pd";
787 case OP_EXTRACT_MASK:
788 return "llvm.x86.sse2.pmovmskb.128";
791 return "llvm.x86.sse2.psrli.w";
794 return "llvm.x86.sse2.psrli.d";
797 return "llvm.x86.sse2.psrli.q";
800 return "llvm.x86.sse2.pslli.w";
803 return "llvm.x86.sse2.pslli.d";
806 return "llvm.x86.sse2.pslli.q";
809 return "llvm.x86.sse2.psrai.w";
812 return "llvm.x86.sse2.psrai.d";
814 return "llvm.x86.sse2.padds.b";
816 return "llvm.x86.sse2.padds.w";
818 return "llvm.x86.sse2.psubs.b";
820 return "llvm.x86.sse2.psubs.w";
821 case OP_PADDB_SAT_UN:
822 return "llvm.x86.sse2.paddus.b";
823 case OP_PADDW_SAT_UN:
824 return "llvm.x86.sse2.paddus.w";
825 case OP_PSUBB_SAT_UN:
826 return "llvm.x86.sse2.psubus.b";
827 case OP_PSUBW_SAT_UN:
828 return "llvm.x86.sse2.psubus.w";
830 return "llvm.x86.sse2.pavg.b";
832 return "llvm.x86.sse2.pavg.w";
834 return "llvm.x86.sse.sqrt.ps";
836 return "llvm.x86.sse2.sqrt.pd";
838 return "llvm.x86.sse.rsqrt.ps";
840 return "llvm.x86.sse.rcp.ps";
842 return "llvm.x86.sse2.cvtdq2pd";
844 return "llvm.x86.sse2.cvtdq2ps";
846 return "llvm.x86.sse2.cvtpd2dq";
848 return "llvm.x86.sse2.cvtps2dq";
850 return "llvm.x86.sse2.cvtpd2ps";
852 return "llvm.x86.sse2.cvtps2pd";
854 return "llvm.x86.sse2.cvttpd2dq";
856 return "llvm.x86.sse2.cvttps2dq";
858 return "llvm.x86.sse.cmp.ps";
860 return "llvm.x86.sse2.cmp.pd";
862 return "llvm.x86.sse2.packsswb.128";
864 return "llvm.x86.sse2.packssdw.128";
866 return "llvm.x86.sse2.packuswb.128";
868 return "llvm.x86.sse41.packusdw";
870 return "llvm.x86.sse2.pmulh.w";
871 case OP_PMULW_HIGH_UN:
872 return "llvm.x86.sse2.pmulhu.w";
875 g_assert_not_reached ();
881 simd_op_to_llvm_type (int opcode)
883 #if defined(TARGET_X86) || defined(TARGET_AMD64)
887 return type_to_simd_type (MONO_TYPE_R8);
890 return type_to_simd_type (MONO_TYPE_I8);
893 return type_to_simd_type (MONO_TYPE_I4);
898 return type_to_simd_type (MONO_TYPE_I2);
902 return type_to_simd_type (MONO_TYPE_I1);
904 return type_to_simd_type (MONO_TYPE_R4);
907 return type_to_simd_type (MONO_TYPE_I4);
911 return type_to_simd_type (MONO_TYPE_R8);
915 return type_to_simd_type (MONO_TYPE_R4);
916 case OP_EXTRACT_MASK:
917 return type_to_simd_type (MONO_TYPE_I1);
923 return type_to_simd_type (MONO_TYPE_R4);
926 return type_to_simd_type (MONO_TYPE_R8);
928 g_assert_not_reached ();
939 * Return the LLVM basic block corresponding to BB.
941 static LLVMBasicBlockRef
942 get_bb (EmitContext *ctx, MonoBasicBlock *bb)
944 char bb_name_buf [128];
947 if (ctx->bblocks [bb->block_num].bblock == NULL) {
948 if (bb->flags & BB_EXCEPTION_HANDLER) {
949 int clause_index = (mono_get_block_region_notry (ctx->cfg, bb->region) >> 8) - 1;
950 sprintf (bb_name_buf, "EH_CLAUSE%d_BB%d", clause_index, bb->block_num);
951 bb_name = bb_name_buf;
952 } else if (bb->block_num < 256) {
953 if (!ctx->lmodule->bb_names) {
954 ctx->lmodule->bb_names_len = 256;
955 ctx->lmodule->bb_names = g_new0 (char*, ctx->lmodule->bb_names_len);
957 if (!ctx->lmodule->bb_names [bb->block_num]) {
960 n = g_strdup_printf ("BB%d", bb->block_num);
961 mono_memory_barrier ();
962 ctx->lmodule->bb_names [bb->block_num] = n;
964 bb_name = ctx->lmodule->bb_names [bb->block_num];
966 sprintf (bb_name_buf, "BB%d", bb->block_num);
967 bb_name = bb_name_buf;
970 ctx->bblocks [bb->block_num].bblock = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
971 ctx->bblocks [bb->block_num].end_bblock = ctx->bblocks [bb->block_num].bblock;
974 return ctx->bblocks [bb->block_num].bblock;
980 * Return the last LLVM bblock corresponding to BB.
981 * This might not be equal to the bb returned by get_bb () since we need to generate
982 * multiple LLVM bblocks for a mono bblock to handle throwing exceptions.
984 static LLVMBasicBlockRef
985 get_end_bb (EmitContext *ctx, MonoBasicBlock *bb)
988 return ctx->bblocks [bb->block_num].end_bblock;
991 static LLVMBasicBlockRef
992 gen_bb (EmitContext *ctx, const char *prefix)
996 sprintf (bb_name, "%s%d", prefix, ++ ctx->ex_index);
997 return LLVMAppendBasicBlock (ctx->lmethod, bb_name);
1003 * Return the target of the patch identified by TYPE and TARGET.
1006 resolve_patch (MonoCompile *cfg, MonoJumpInfoType type, gconstpointer target)
1010 memset (&ji, 0, sizeof (ji));
1012 ji.data.target = target;
1014 return mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &ji, FALSE);
1020 * Emit code to convert the LLVM value V to DTYPE.
1023 convert_full (EmitContext *ctx, LLVMValueRef v, LLVMTypeRef dtype, gboolean is_unsigned)
1025 LLVMTypeRef stype = LLVMTypeOf (v);
1027 if (stype != dtype) {
1028 gboolean ext = FALSE;
1031 if (dtype == LLVMInt64Type () && (stype == LLVMInt32Type () || stype == LLVMInt16Type () || stype == LLVMInt8Type ()))
1033 else if (dtype == LLVMInt32Type () && (stype == LLVMInt16Type () || stype == LLVMInt8Type ()))
1035 else if (dtype == LLVMInt16Type () && (stype == LLVMInt8Type ()))
1039 return is_unsigned ? LLVMBuildZExt (ctx->builder, v, dtype, "") : LLVMBuildSExt (ctx->builder, v, dtype, "");
1041 if (dtype == LLVMDoubleType () && stype == LLVMFloatType ())
1042 return LLVMBuildFPExt (ctx->builder, v, dtype, "");
1045 if (stype == LLVMInt64Type () && (dtype == LLVMInt32Type () || dtype == LLVMInt16Type () || dtype == LLVMInt8Type ()))
1046 return LLVMBuildTrunc (ctx->builder, v, dtype, "");
1047 if (stype == LLVMInt32Type () && (dtype == LLVMInt16Type () || dtype == LLVMInt8Type ()))
1048 return LLVMBuildTrunc (ctx->builder, v, dtype, "");
1049 if (stype == LLVMInt16Type () && dtype == LLVMInt8Type ())
1050 return LLVMBuildTrunc (ctx->builder, v, dtype, "");
1051 if (stype == LLVMDoubleType () && dtype == LLVMFloatType ())
1052 return LLVMBuildFPTrunc (ctx->builder, v, dtype, "");
1054 if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind && LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
1055 return LLVMBuildBitCast (ctx->builder, v, dtype, "");
1056 if (LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
1057 return LLVMBuildIntToPtr (ctx->builder, v, dtype, "");
1058 if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind)
1059 return LLVMBuildPtrToInt (ctx->builder, v, dtype, "");
1061 if (mono_arch_is_soft_float ()) {
1062 if (stype == LLVMInt32Type () && dtype == LLVMFloatType ())
1063 return LLVMBuildBitCast (ctx->builder, v, dtype, "");
1064 if (stype == LLVMInt32Type () && dtype == LLVMDoubleType ())
1065 return LLVMBuildBitCast (ctx->builder, LLVMBuildZExt (ctx->builder, v, LLVMInt64Type (), ""), dtype, "");
1068 if (LLVMGetTypeKind (stype) == LLVMVectorTypeKind && LLVMGetTypeKind (dtype) == LLVMVectorTypeKind)
1069 return LLVMBuildBitCast (ctx->builder, v, dtype, "");
1072 LLVMDumpValue (LLVMConstNull (dtype));
1073 g_assert_not_reached ();
1081 convert (EmitContext *ctx, LLVMValueRef v, LLVMTypeRef dtype)
1083 return convert_full (ctx, v, dtype, FALSE);
1087 * emit_volatile_load:
1089 * If vreg is volatile, emit a load from its address.
1092 emit_volatile_load (EmitContext *ctx, int vreg)
1096 LLVMValueRef v = LLVMBuildLoad (ctx->builder, ctx->addresses [vreg], "");
1097 t = ctx->vreg_cli_types [vreg];
1098 if (t && !t->byref) {
1100 * Might have to zero extend since llvm doesn't have
1103 if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_U2 || t->type == MONO_TYPE_CHAR || t->type == MONO_TYPE_BOOLEAN)
1104 v = LLVMBuildZExt (ctx->builder, v, LLVMInt32Type (), "");
1105 else if (t->type == MONO_TYPE_U8)
1106 v = LLVMBuildZExt (ctx->builder, v, LLVMInt64Type (), "");
1113 * emit_volatile_store:
1115 * If VREG is volatile, emit a store from its value to its address.
1118 emit_volatile_store (EmitContext *ctx, int vreg)
1120 MonoInst *var = get_vreg_to_inst (ctx->cfg, vreg);
1122 if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
1123 g_assert (ctx->addresses [vreg]);
1124 LLVMBuildStore (ctx->builder, convert (ctx, ctx->values [vreg], type_to_llvm_type (ctx, var->inst_vtype)), ctx->addresses [vreg]);
1130 * Maps parameter indexes in the original signature to parameter indexes
1131 * in the LLVM signature.
1134 /* The indexes of various special arguments in the LLVM signature */
1135 int vret_arg_pindex, this_arg_pindex, rgctx_arg_pindex, imt_arg_pindex;
1139 * sig_to_llvm_sig_full:
1141 * Return the LLVM signature corresponding to the mono signature SIG using the
1142 * calling convention information in CINFO. Return parameter mapping information in SINFO.
1145 sig_to_llvm_sig_full (EmitContext *ctx, MonoMethodSignature *sig, LLVMCallInfo *cinfo,
1148 LLVMTypeRef ret_type;
1149 LLVMTypeRef *param_types = NULL;
1151 int i, j, pindex, vret_arg_pindex = 0;
1153 gboolean vretaddr = FALSE;
1157 memset (sinfo, 0, sizeof (LLVMSigInfo));
1159 rtype = mini_get_underlying_type (ctx->cfg, sig->ret);
1160 ret_type = type_to_llvm_type (ctx, rtype);
1161 CHECK_FAILURE (ctx);
1164 if (cinfo->ret.storage == LLVMArgVtypeInReg) {
1165 /* LLVM models this by returning an aggregate value */
1166 if (cinfo->ret.pair_storage [0] == LLVMArgInIReg && cinfo->ret.pair_storage [1] == LLVMArgNone) {
1167 LLVMTypeRef members [2];
1169 members [0] = IntPtrType ();
1170 ret_type = LLVMStructType (members, 1, FALSE);
1172 g_assert_not_reached ();
1174 } else if (cinfo->ret.storage == LLVMArgVtypeByVal) {
1175 /* Vtype returned normally by val */
1176 } else if (cinfo->ret.storage == LLVMArgFpStruct) {
1177 /* Vtype returned as a fp struct */
1178 LLVMTypeRef members [16];
1180 /* Have to create our own structure since we don't map fp structures to LLVM fp structures yet */
1181 for (i = 0; i < cinfo->ret.nslots; ++i)
1182 members [i] = cinfo->ret.esize == 8 ? LLVMDoubleType () : LLVMFloatType ();
1183 ret_type = LLVMStructType (members, cinfo->ret.nslots, FALSE);
1184 } else if (mini_type_is_vtype (ctx->cfg, rtype)) {
1185 g_assert (cinfo->ret.storage == LLVMArgVtypeRetAddr);
1187 ret_type = LLVMVoidType ();
1191 pindexes = g_new0 (int, sig->param_count);
1192 param_types = g_new0 (LLVMTypeRef, (sig->param_count * 8) + 3);
1194 if (cinfo && cinfo->rgctx_arg) {
1196 sinfo->rgctx_arg_pindex = pindex;
1197 param_types [pindex] = ctx->lmodule->ptr_type;
1200 if (cinfo && cinfo->imt_arg) {
1202 sinfo->imt_arg_pindex = pindex;
1203 param_types [pindex] = ctx->lmodule->ptr_type;
1207 /* Compute the index in the LLVM signature where the vret arg needs to be passed */
1208 vret_arg_pindex = pindex;
1209 if (cinfo->vret_arg_index == 1) {
1210 /* Add the slots consumed by the first argument */
1211 LLVMArgInfo *ainfo = &cinfo->args [0];
1212 switch (ainfo->storage) {
1213 case LLVMArgVtypeInReg:
1214 for (j = 0; j < 2; ++j) {
1215 if (ainfo->pair_storage [j] == LLVMArgInIReg)
1225 sinfo->vret_arg_pindex = vret_arg_pindex;
1228 if (vretaddr && vret_arg_pindex == pindex)
1229 param_types [pindex ++] = IntPtrType ();
1232 sinfo->this_arg_pindex = pindex;
1233 param_types [pindex ++] = ThisType ();
1235 if (vretaddr && vret_arg_pindex == pindex)
1236 param_types [pindex ++] = IntPtrType ();
1237 for (i = 0; i < sig->param_count; ++i) {
1238 LLVMArgInfo *ainfo = cinfo ? &cinfo->args [i + sig->hasthis] : NULL;
1240 if (vretaddr && vret_arg_pindex == pindex)
1241 param_types [pindex ++] = IntPtrType ();
1242 pindexes [i] = pindex;
1245 param_types [pindex ++] = type_to_llvm_arg_type (ctx, sig->params [i]);
1249 switch (ainfo->storage) {
1250 case LLVMArgVtypeInReg:
1251 for (j = 0; j < 2; ++j) {
1252 switch (ainfo->pair_storage [j]) {
1254 param_types [pindex ++] = LLVMIntType (sizeof (gpointer) * 8);
1259 g_assert_not_reached ();
1263 case LLVMArgVtypeByVal:
1264 param_types [pindex] = type_to_llvm_arg_type (ctx, sig->params [i]);
1265 CHECK_FAILURE (ctx);
1266 param_types [pindex] = LLVMPointerType (param_types [pindex], 0);
1269 case LLVMArgAsIArgs:
1270 param_types [pindex] = LLVMArrayType (IntPtrType (), ainfo->nslots);
1273 case LLVMArgAsFpArgs: {
1276 for (j = 0; j < ainfo->nslots; ++j)
1277 param_types [pindex + j] = ainfo->esize == 8 ? LLVMDoubleType () : LLVMFloatType ();
1278 pindex += ainfo->nslots;
1282 param_types [pindex ++] = type_to_llvm_arg_type (ctx, sig->params [i]);
1286 if (vretaddr && vret_arg_pindex == pindex)
1287 param_types [pindex ++] = IntPtrType ();
1289 CHECK_FAILURE (ctx);
1291 res = LLVMFunctionType (ret_type, param_types, pindex, FALSE);
1292 g_free (param_types);
1295 sinfo->pindexes = pindexes;
1303 g_free (param_types);
1309 sig_to_llvm_sig (EmitContext *ctx, MonoMethodSignature *sig)
1311 return sig_to_llvm_sig_full (ctx, sig, NULL, NULL);
1315 * LLVMFunctionType1:
1317 * Create an LLVM function type from the arguments.
1319 static G_GNUC_UNUSED LLVMTypeRef
1320 LLVMFunctionType1(LLVMTypeRef ReturnType,
1321 LLVMTypeRef ParamType1,
1324 LLVMTypeRef param_types [1];
1326 param_types [0] = ParamType1;
1328 return LLVMFunctionType (ReturnType, param_types, 1, IsVarArg);
1332 * LLVMFunctionType2:
1334 * Create an LLVM function type from the arguments.
1336 static G_GNUC_UNUSED LLVMTypeRef
1337 LLVMFunctionType2(LLVMTypeRef ReturnType,
1338 LLVMTypeRef ParamType1,
1339 LLVMTypeRef ParamType2,
1342 LLVMTypeRef param_types [2];
1344 param_types [0] = ParamType1;
1345 param_types [1] = ParamType2;
1347 return LLVMFunctionType (ReturnType, param_types, 2, IsVarArg);
1351 * LLVMFunctionType3:
1353 * Create an LLVM function type from the arguments.
1355 static G_GNUC_UNUSED LLVMTypeRef
1356 LLVMFunctionType3(LLVMTypeRef ReturnType,
1357 LLVMTypeRef ParamType1,
1358 LLVMTypeRef ParamType2,
1359 LLVMTypeRef ParamType3,
1362 LLVMTypeRef param_types [3];
1364 param_types [0] = ParamType1;
1365 param_types [1] = ParamType2;
1366 param_types [2] = ParamType3;
1368 return LLVMFunctionType (ReturnType, param_types, 3, IsVarArg);
1374 * Create an LLVM builder and remember it so it can be freed later.
1376 static LLVMBuilderRef
1377 create_builder (EmitContext *ctx)
1379 LLVMBuilderRef builder = LLVMCreateBuilder ();
1381 ctx->builders = g_slist_prepend_mempool (ctx->cfg->mempool, ctx->builders, builder);
1387 get_plt_entry (EmitContext *ctx, LLVMTypeRef llvm_sig, MonoJumpInfoType type, gconstpointer data)
1389 char *callee_name = mono_aot_get_plt_symbol (type, data);
1390 LLVMValueRef callee;
1391 MonoJumpInfo *ji = NULL;
1396 if (ctx->cfg->compile_aot)
1397 /* Add a patch so referenced wrappers can be compiled in full aot mode */
1398 mono_add_patch_info (ctx->cfg, 0, type, data);
1401 callee = g_hash_table_lookup (ctx->lmodule->plt_entries, callee_name);
1403 callee = LLVMAddFunction (ctx->module, callee_name, llvm_sig);
1405 LLVMSetVisibility (callee, LLVMHiddenVisibility);
1407 g_hash_table_insert (ctx->lmodule->plt_entries, (char*)callee_name, callee);
1410 if (ctx->cfg->compile_aot) {
1411 ji = g_new0 (MonoJumpInfo, 1);
1413 ji->data.target = data;
1415 g_hash_table_insert (ctx->lmodule->plt_entries_ji, ji, callee);
1422 get_handler_clause (MonoCompile *cfg, MonoBasicBlock *bb)
1424 MonoMethodHeader *header = cfg->header;
1425 MonoExceptionClause *clause;
1429 if (bb->region != -1 && MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))
1430 return (bb->region >> 8) - 1;
1433 for (i = 0; i < header->num_clauses; ++i) {
1434 clause = &header->clauses [i];
1436 if (MONO_OFFSET_IN_CLAUSE (clause, bb->real_offset) && clause->flags == MONO_EXCEPTION_CLAUSE_NONE)
1444 set_metadata_flag (LLVMValueRef v, const char *flag_name)
1446 LLVMValueRef md_arg;
1449 md_kind = LLVMGetMDKindID (flag_name, strlen (flag_name));
1450 md_arg = LLVMMDString ("mono", 4);
1451 LLVMSetMetadata (v, md_kind, LLVMMDNode (&md_arg, 1));
1455 set_invariant_load_flag (LLVMValueRef v)
1457 LLVMValueRef md_arg;
1459 const char *flag_name;
1461 // FIXME: Cache this
1462 flag_name = "invariant.load";
1463 md_kind = LLVMGetMDKindID (flag_name, strlen (flag_name));
1464 md_arg = LLVMMDString ("<index>", strlen ("<index>"));
1465 LLVMSetMetadata (v, md_kind, LLVMMDNode (&md_arg, 1));
1471 * Emit an LLVM call or invoke instruction depending on whenever the call is inside
1475 emit_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, LLVMValueRef callee, LLVMValueRef *args, int pindex)
1477 MonoCompile *cfg = ctx->cfg;
1479 LLVMBuilderRef builder = *builder_ref;
1482 clause_index = get_handler_clause (cfg, bb);
1484 if (clause_index != -1) {
1485 MonoMethodHeader *header = cfg->header;
1486 MonoExceptionClause *ec = &header->clauses [clause_index];
1487 MonoBasicBlock *tblock;
1488 LLVMBasicBlockRef ex_bb, noex_bb;
1491 * Have to use an invoke instead of a call, branching to the
1492 * handler bblock of the clause containing this bblock.
1495 g_assert (ec->flags == MONO_EXCEPTION_CLAUSE_NONE || ec->flags == MONO_EXCEPTION_CLAUSE_FINALLY);
1497 tblock = cfg->cil_offset_to_bb [ec->handler_offset];
1500 ctx->bblocks [tblock->block_num].invoke_target = TRUE;
1502 ex_bb = get_bb (ctx, tblock);
1504 noex_bb = gen_bb (ctx, "NOEX_BB");
1507 lcall = LLVMBuildInvoke (builder, callee, args, pindex, noex_bb, ex_bb, "");
1509 builder = ctx->builder = create_builder (ctx);
1510 LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
1512 ctx->bblocks [bb->block_num].end_bblock = noex_bb;
1514 lcall = LLVMBuildCall (builder, callee, args, pindex, "");
1515 ctx->builder = builder;
1518 *builder_ref = ctx->builder;
1524 emit_load_general (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef addr, const char *name, gboolean is_faulting, BarrierKind barrier)
1526 const char *intrins_name;
1527 LLVMValueRef args [16], res;
1528 LLVMTypeRef addr_type;
1530 if (is_faulting && bb->region != -1) {
1531 LLVMAtomicOrdering ordering;
1534 case LLVM_BARRIER_NONE:
1535 ordering = LLVMAtomicOrderingNotAtomic;
1537 case LLVM_BARRIER_ACQ:
1538 ordering = LLVMAtomicOrderingAcquire;
1540 case LLVM_BARRIER_SEQ:
1541 ordering = LLVMAtomicOrderingSequentiallyConsistent;
1544 g_assert_not_reached ();
1549 * We handle loads which can fault by calling a mono specific intrinsic
1550 * using an invoke, so they are handled properly inside try blocks.
1551 * We can't use this outside clauses, since LLVM optimizes intrinsics which
1552 * are marked with IntrReadArgMem.
1556 intrins_name = "llvm.mono.load.i8.p0i8";
1559 intrins_name = "llvm.mono.load.i16.p0i16";
1562 intrins_name = "llvm.mono.load.i32.p0i32";
1565 intrins_name = "llvm.mono.load.i64.p0i64";
1568 g_assert_not_reached ();
1571 addr_type = LLVMTypeOf (addr);
1572 if (addr_type == LLVMPointerType (LLVMDoubleType (), 0) || addr_type == LLVMPointerType (LLVMFloatType (), 0))
1573 addr = LLVMBuildBitCast (*builder_ref, addr, LLVMPointerType (LLVMIntType (size * 8), 0), "");
1576 args [1] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1577 args [2] = LLVMConstInt (LLVMInt1Type (), TRUE, FALSE);
1578 args [3] = LLVMConstInt (LLVMInt32Type (), ordering, FALSE);
1579 res = emit_call (ctx, bb, builder_ref, LLVMGetNamedFunction (ctx->module, intrins_name), args, 4);
1581 if (addr_type == LLVMPointerType (LLVMDoubleType (), 0))
1582 res = LLVMBuildBitCast (*builder_ref, res, LLVMDoubleType (), "");
1583 else if (addr_type == LLVMPointerType (LLVMFloatType (), 0))
1584 res = LLVMBuildBitCast (*builder_ref, res, LLVMFloatType (), "");
1591 * We emit volatile loads for loads which can fault, because otherwise
1592 * LLVM will generate invalid code when encountering a load from a
1595 res = mono_llvm_build_load (*builder_ref, addr, name, is_faulting, barrier);
1597 /* Mark it with a custom metadata */
1600 set_metadata_flag (res, "mono.faulting.load");
1608 emit_load (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef addr, const char *name, gboolean is_faulting)
1610 return emit_load_general (ctx, bb, builder_ref, size, addr, name, is_faulting, LLVM_BARRIER_NONE);
1614 emit_store_general (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef value, LLVMValueRef addr, gboolean is_faulting, BarrierKind barrier)
1616 const char *intrins_name;
1617 LLVMValueRef args [16];
1619 if (is_faulting && bb->region != -1) {
1620 LLVMAtomicOrdering ordering;
1623 case LLVM_BARRIER_NONE:
1624 ordering = LLVMAtomicOrderingNotAtomic;
1626 case LLVM_BARRIER_REL:
1627 ordering = LLVMAtomicOrderingRelease;
1629 case LLVM_BARRIER_SEQ:
1630 ordering = LLVMAtomicOrderingSequentiallyConsistent;
1633 g_assert_not_reached ();
1639 intrins_name = "llvm.mono.store.i8.p0i8";
1642 intrins_name = "llvm.mono.store.i16.p0i16";
1645 intrins_name = "llvm.mono.store.i32.p0i32";
1648 intrins_name = "llvm.mono.store.i64.p0i64";
1651 g_assert_not_reached ();
1654 if (LLVMTypeOf (value) == LLVMDoubleType () || LLVMTypeOf (value) == LLVMFloatType ()) {
1655 value = LLVMBuildBitCast (*builder_ref, value, LLVMIntType (size * 8), "");
1656 addr = LLVMBuildBitCast (*builder_ref, addr, LLVMPointerType (LLVMIntType (size * 8), 0), "");
1661 args [2] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1662 args [3] = LLVMConstInt (LLVMInt1Type (), TRUE, FALSE);
1663 args [4] = LLVMConstInt (LLVMInt32Type (), ordering, FALSE);
1664 emit_call (ctx, bb, builder_ref, LLVMGetNamedFunction (ctx->module, intrins_name), args, 5);
1666 mono_llvm_build_store (*builder_ref, value, addr, is_faulting, barrier);
1671 emit_store (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef value, LLVMValueRef addr, gboolean is_faulting)
1673 emit_store_general (ctx, bb, builder_ref, size, value, addr, is_faulting, LLVM_BARRIER_NONE);
1677 * emit_cond_system_exception:
1679 * Emit code to throw the exception EXC_TYPE if the condition CMP is false.
1680 * Might set the ctx exception.
1683 emit_cond_system_exception (EmitContext *ctx, MonoBasicBlock *bb, const char *exc_type, LLVMValueRef cmp)
1685 LLVMBasicBlockRef ex_bb, noex_bb;
1686 LLVMBuilderRef builder;
1687 MonoClass *exc_class;
1688 LLVMValueRef args [2];
1690 ex_bb = gen_bb (ctx, "EX_BB");
1691 noex_bb = gen_bb (ctx, "NOEX_BB");
1693 LLVMBuildCondBr (ctx->builder, cmp, ex_bb, noex_bb);
1695 exc_class = mono_class_from_name (mono_get_corlib (), "System", exc_type);
1696 g_assert (exc_class);
1698 /* Emit exception throwing code */
1699 builder = create_builder (ctx);
1700 LLVMPositionBuilderAtEnd (builder, ex_bb);
1702 if (!ctx->lmodule->throw_corlib_exception) {
1703 LLVMValueRef callee;
1705 const char *icall_name;
1707 MonoMethodSignature *throw_sig = mono_metadata_signature_alloc (mono_get_corlib (), 2);
1708 throw_sig->ret = &mono_get_void_class ()->byval_arg;
1709 throw_sig->params [0] = &mono_get_int32_class ()->byval_arg;
1710 icall_name = "llvm_throw_corlib_exception_abs_trampoline";
1711 /* This will become i8* */
1712 throw_sig->params [1] = &mono_get_byte_class ()->this_arg;
1713 sig = sig_to_llvm_sig (ctx, throw_sig);
1715 if (ctx->cfg->compile_aot) {
1716 callee = get_plt_entry (ctx, sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
1718 callee = LLVMAddFunction (ctx->module, "llvm_throw_corlib_exception_trampoline", sig_to_llvm_sig (ctx, throw_sig));
1721 * Differences between the LLVM/non-LLVM throw corlib exception trampoline:
1722 * - On x86, LLVM generated code doesn't push the arguments
1723 * - The trampoline takes the throw address as an arguments, not a pc offset.
1725 LLVMAddGlobalMapping (ctx->lmodule->ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
1728 mono_memory_barrier ();
1729 ctx->lmodule->throw_corlib_exception = callee;
1732 if (IS_TARGET_X86 || IS_TARGET_AMD64)
1733 args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token - MONO_TOKEN_TYPE_DEF, FALSE);
1735 args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token, FALSE);
1738 * The LLVM mono branch contains changes so a block address can be passed as an
1739 * argument to a call.
1741 args [1] = LLVMBlockAddress (ctx->lmethod, ex_bb);
1742 emit_call (ctx, bb, &builder, ctx->lmodule->throw_corlib_exception, args, 2);
1744 LLVMBuildUnreachable (builder);
1746 ctx->builder = create_builder (ctx);
1747 LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
1749 ctx->bblocks [bb->block_num].end_bblock = noex_bb;
1756 * emit_args_to_vtype:
1758 * Emit code to store the vtype in the arguments args to the address ADDRESS.
1761 emit_args_to_vtype (EmitContext *ctx, LLVMBuilderRef builder, MonoType *t, LLVMValueRef address, LLVMArgInfo *ainfo, LLVMValueRef *args)
1763 int j, size, nslots;
1765 size = get_vtype_size (t);
1767 if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
1768 address = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (LLVMInt8Type (), 0), "");
1771 if (ainfo->storage == LLVMArgAsFpArgs)
1772 nslots = ainfo->nslots;
1776 for (j = 0; j < nslots; ++j) {
1777 LLVMValueRef index [2], addr, daddr;
1778 int part_size = size > sizeof (gpointer) ? sizeof (gpointer) : size;
1779 LLVMTypeRef part_type;
1781 if (ainfo->pair_storage [j] == LLVMArgNone)
1784 switch (ainfo->pair_storage [j]) {
1785 case LLVMArgInIReg: {
1786 part_type = LLVMIntType (part_size * 8);
1787 if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
1788 index [0] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
1789 addr = LLVMBuildGEP (builder, address, index, 1, "");
1791 daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (IntPtrType (), 0), "");
1792 index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
1793 addr = LLVMBuildGEP (builder, daddr, index, 1, "");
1795 LLVMBuildStore (builder, convert (ctx, args [j], part_type), LLVMBuildBitCast (ctx->builder, addr, LLVMPointerType (part_type, 0), ""));
1798 case LLVMArgInFPReg: {
1799 LLVMTypeRef arg_type;
1801 if (ainfo->esize == 8)
1802 arg_type = LLVMDoubleType ();
1804 arg_type = LLVMFloatType ();
1806 index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
1807 daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (arg_type, 0), "");
1808 addr = LLVMBuildGEP (builder, daddr, index, 1, "");
1809 LLVMBuildStore (builder, args [j], addr);
1815 g_assert_not_reached ();
1818 size -= sizeof (gpointer);
1823 * emit_vtype_to_args:
1825 * Emit code to load a vtype at address ADDRESS into scalar arguments. Store the arguments
1826 * into ARGS, and the number of arguments into NARGS.
1829 emit_vtype_to_args (EmitContext *ctx, LLVMBuilderRef builder, MonoType *t, LLVMValueRef address, LLVMArgInfo *ainfo, LLVMValueRef *args, guint32 *nargs)
1832 int j, size, nslots;
1833 LLVMTypeRef arg_type;
1835 size = get_vtype_size (t);
1837 if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t)))
1838 address = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (LLVMInt8Type (), 0), "");
1840 if (ainfo->storage == LLVMArgAsFpArgs)
1841 nslots = ainfo->nslots;
1844 for (j = 0; j < nslots; ++j) {
1845 LLVMValueRef index [2], addr, daddr;
1846 int partsize = size > sizeof (gpointer) ? sizeof (gpointer) : size;
1848 if (ainfo->pair_storage [j] == LLVMArgNone)
1851 switch (ainfo->pair_storage [j]) {
1853 if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
1854 index [0] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
1855 addr = LLVMBuildGEP (builder, address, index, 1, "");
1857 daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (IntPtrType (), 0), "");
1858 index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
1859 addr = LLVMBuildGEP (builder, daddr, index, 1, "");
1861 args [pindex ++] = convert (ctx, LLVMBuildLoad (builder, LLVMBuildBitCast (ctx->builder, addr, LLVMPointerType (LLVMIntType (partsize * 8), 0), ""), ""), IntPtrType ());
1863 case LLVMArgInFPReg:
1864 if (ainfo->esize == 8)
1865 arg_type = LLVMDoubleType ();
1867 arg_type = LLVMFloatType ();
1868 daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (arg_type, 0), "");
1869 index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
1870 addr = LLVMBuildGEP (builder, daddr, index, 1, "");
1871 args [pindex ++] = LLVMBuildLoad (builder, addr, "");
1876 g_assert_not_reached ();
1878 size -= sizeof (gpointer);
1885 build_alloca_llvm_type (EmitContext *ctx, LLVMTypeRef t, int align)
1888 * Have to place all alloca's at the end of the entry bb, since otherwise they would
1889 * get executed every time control reaches them.
1891 LLVMPositionBuilder (ctx->alloca_builder, get_bb (ctx, ctx->cfg->bb_entry), ctx->last_alloca);
1893 ctx->last_alloca = mono_llvm_build_alloca (ctx->alloca_builder, t, NULL, align, "");
1894 return ctx->last_alloca;
1898 build_alloca (EmitContext *ctx, MonoType *t)
1900 MonoClass *k = mono_class_from_mono_type (t);
1903 if (MONO_CLASS_IS_SIMD (ctx->cfg, k))
1906 align = mono_class_min_align (k);
1908 /* Sometimes align is not a power of 2 */
1909 while (mono_is_power_of_two (align) == -1)
1912 return build_alloca_llvm_type (ctx, type_to_llvm_type (ctx, t), align);
1916 * Put the global into the 'llvm.used' array to prevent it from being optimized away.
1919 mark_as_used (MonoLLVMModule *lmodule, LLVMValueRef global)
1922 lmodule->used = g_ptr_array_sized_new (16);
1923 g_ptr_array_add (lmodule->used, global);
1927 emit_llvm_used (MonoLLVMModule *lmodule)
1929 LLVMModuleRef module = lmodule->module;
1930 LLVMTypeRef used_type;
1931 LLVMValueRef used, *used_elem;
1937 used_type = LLVMArrayType (LLVMPointerType (LLVMInt8Type (), 0), lmodule->used->len);
1938 used = LLVMAddGlobal (module, used_type, "llvm.used");
1939 used_elem = g_new0 (LLVMValueRef, lmodule->used->len);
1940 for (i = 0; i < lmodule->used->len; ++i)
1941 used_elem [i] = LLVMConstBitCast (g_ptr_array_index (lmodule->used, i), LLVMPointerType (LLVMInt8Type (), 0));
1942 LLVMSetInitializer (used, LLVMConstArray (LLVMPointerType (LLVMInt8Type (), 0), used_elem, lmodule->used->len));
1943 LLVMSetLinkage (used, LLVMAppendingLinkage);
1944 LLVMSetSection (used, "llvm.metadata");
1950 * Emit code to load/convert arguments.
1953 emit_entry_bb (EmitContext *ctx, LLVMBuilderRef builder)
1956 MonoCompile *cfg = ctx->cfg;
1957 MonoMethodSignature *sig = ctx->sig;
1958 LLVMCallInfo *linfo = ctx->linfo;
1961 ctx->alloca_builder = create_builder (ctx);
1964 * Handle indirect/volatile variables by allocating memory for them
1965 * using 'alloca', and storing their address in a temporary.
1967 for (i = 0; i < cfg->num_varinfo; ++i) {
1968 MonoInst *var = cfg->varinfo [i];
1971 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || mini_type_is_vtype (cfg, var->inst_vtype)) {
1972 vtype = type_to_llvm_type (ctx, var->inst_vtype);
1973 CHECK_FAILURE (ctx);
1974 /* Could be already created by an OP_VPHI */
1975 if (!ctx->addresses [var->dreg])
1976 ctx->addresses [var->dreg] = build_alloca (ctx, var->inst_vtype);
1977 ctx->vreg_cli_types [var->dreg] = var->inst_vtype;
1981 for (i = 0; i < sig->param_count; ++i) {
1982 LLVMArgInfo *ainfo = &linfo->args [i + sig->hasthis];
1983 int reg = cfg->args [i + sig->hasthis]->dreg;
1985 switch (ainfo->storage) {
1986 case LLVMArgVtypeInReg:
1987 case LLVMArgAsFpArgs: {
1988 LLVMValueRef args [8];
1991 /* The argument is received as a set of int/fp arguments, store them into the real argument */
1992 memset (args, 0, sizeof (args));
1993 pindex = ctx->pindexes [i];
1994 if (ainfo->storage == LLVMArgVtypeInReg) {
1995 args [0] = LLVMGetParam (ctx->lmethod, pindex);
1996 if (ainfo->pair_storage [1] != LLVMArgNone)
1997 args [1] = LLVMGetParam (ctx->lmethod, pindex + 1);
1999 g_assert (ainfo->nslots <= 8);
2000 for (j = 0; j < ainfo->nslots; ++j)
2001 args [j] = LLVMGetParam (ctx->lmethod, ctx->pindexes [i] + j);
2003 ctx->addresses [reg] = build_alloca (ctx, sig->params [i]);
2005 emit_args_to_vtype (ctx, builder, sig->params [i], ctx->addresses [reg], ainfo, args);
2007 if (ainfo->storage == LLVMArgVtypeInReg && MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (sig->params [i]))) {
2008 /* Treat these as normal values */
2009 ctx->values [reg] = LLVMBuildLoad (builder, ctx->addresses [reg], "");
2013 case LLVMArgVtypeByVal: {
2014 ctx->addresses [reg] = LLVMGetParam (ctx->lmethod, ctx->pindexes [i]);
2016 if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (sig->params [i]))) {
2017 /* Treat these as normal values */
2018 ctx->values [reg] = LLVMBuildLoad (builder, ctx->addresses [reg], "");
2022 case LLVMArgAsIArgs: {
2023 LLVMValueRef arg = LLVMGetParam (ctx->lmethod, ctx->pindexes [i]);
2025 ctx->addresses [reg] = build_alloca (ctx, sig->params [i]);
2027 /* The argument is received as an array of ints, store it into the real argument */
2028 LLVMBuildStore (ctx->builder, arg, convert (ctx, ctx->addresses [reg], LLVMPointerType (LLVMTypeOf (arg), 0)));
2032 ctx->values [reg] = convert_full (ctx, ctx->values [reg], llvm_type_to_stack_type (cfg, type_to_llvm_type (ctx, sig->params [i])), type_is_unsigned (ctx, sig->params [i]));
2038 emit_volatile_store (ctx, cfg->vret_addr->dreg);
2040 emit_volatile_store (ctx, cfg->args [0]->dreg);
2041 for (i = 0; i < sig->param_count; ++i)
2042 if (!mini_type_is_vtype (cfg, sig->params [i]))
2043 emit_volatile_store (ctx, cfg->args [i + sig->hasthis]->dreg);
2045 if (sig->hasthis && !cfg->rgctx_var && cfg->generic_sharing_context) {
2046 LLVMValueRef this_alloc;
2049 * The exception handling code needs the location where the this argument was
2050 * stored for gshared methods. We create a separate alloca to hold it, and mark it
2051 * with the "mono.this" custom metadata to tell llvm that it needs to save its
2052 * location into the LSDA.
2054 this_alloc = mono_llvm_build_alloca (builder, ThisType (), LLVMConstInt (LLVMInt32Type (), 1, FALSE), 0, "");
2055 /* This volatile store will keep the alloca alive */
2056 mono_llvm_build_store (builder, ctx->values [cfg->args [0]->dreg], this_alloc, TRUE, LLVM_BARRIER_NONE);
2058 set_metadata_flag (this_alloc, "mono.this");
2061 if (cfg->rgctx_var) {
2062 LLVMValueRef rgctx_alloc, store;
2065 * We handle the rgctx arg similarly to the this pointer.
2067 g_assert (ctx->addresses [cfg->rgctx_var->dreg]);
2068 rgctx_alloc = ctx->addresses [cfg->rgctx_var->dreg];
2069 /* This volatile store will keep the alloca alive */
2070 store = mono_llvm_build_store (builder, convert (ctx, ctx->rgctx_arg, IntPtrType ()), rgctx_alloc, TRUE, LLVM_BARRIER_NONE);
2072 set_metadata_flag (rgctx_alloc, "mono.this");
2076 * For finally clauses, create an indicator variable telling OP_ENDFINALLY whenever
2077 * it needs to continue normally, or return back to the exception handling system.
2079 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2080 if (bb->region != -1 && (bb->flags & BB_EXCEPTION_HANDLER))
2081 g_hash_table_insert (ctx->region_to_handler, GUINT_TO_POINTER (mono_get_block_region_notry (cfg, bb->region)), bb);
2082 if (bb->region != -1 && (bb->flags & BB_EXCEPTION_HANDLER) && bb->in_scount == 0) {
2086 sprintf (name, "finally_ind_bb%d", bb->block_num);
2087 val = LLVMBuildAlloca (builder, LLVMInt32Type (), name);
2088 LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), val);
2090 ctx->bblocks [bb->block_num].finally_ind = val;
2093 * Create a new bblock which CALL_HANDLER can branch to, because branching to the
2094 * LLVM bblock containing the call to llvm.eh.selector causes problems for the
2095 * LLVM optimizer passes.
2097 sprintf (name, "BB_%d_CALL_HANDLER_TARGET", bb->block_num);
2098 ctx->bblocks [bb->block_num].call_handler_target_bb = LLVMAppendBasicBlock (ctx->lmethod, name);
2106 /* Have to export this for AOT */
2108 mono_personality (void)
2111 g_assert_not_reached ();
2115 process_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, MonoInst *ins)
2117 MonoCompile *cfg = ctx->cfg;
2118 LLVMModuleRef module = ctx->module;
2119 LLVMValueRef *values = ctx->values;
2120 LLVMValueRef *addresses = ctx->addresses;
2121 MonoCallInst *call = (MonoCallInst*)ins;
2122 MonoMethodSignature *sig = call->signature;
2123 LLVMValueRef callee = NULL, lcall;
2125 LLVMCallInfo *cinfo;
2129 LLVMTypeRef llvm_sig;
2131 gboolean virtual, calli;
2132 LLVMBuilderRef builder = *builder_ref;
2135 if (call->signature->call_convention != MONO_CALL_DEFAULT)
2136 LLVM_FAILURE (ctx, "non-default callconv");
2138 cinfo = call->cinfo;
2139 if (call->rgctx_arg_reg)
2140 cinfo->rgctx_arg = TRUE;
2141 if (call->imt_arg_reg)
2142 cinfo->imt_arg = TRUE;
2144 vretaddr = cinfo && cinfo->ret.storage == LLVMArgVtypeRetAddr;
2146 llvm_sig = sig_to_llvm_sig_full (ctx, sig, cinfo, &sinfo);
2147 CHECK_FAILURE (ctx);
2149 virtual = (ins->opcode == OP_VOIDCALL_MEMBASE || ins->opcode == OP_CALL_MEMBASE || ins->opcode == OP_VCALL_MEMBASE || ins->opcode == OP_LCALL_MEMBASE || ins->opcode == OP_FCALL_MEMBASE || ins->opcode == OP_RCALL_MEMBASE);
2150 calli = !call->fptr_is_patch && (ins->opcode == OP_VOIDCALL_REG || ins->opcode == OP_CALL_REG || ins->opcode == OP_VCALL_REG || ins->opcode == OP_LCALL_REG || ins->opcode == OP_FCALL_REG || ins->opcode == OP_RCALL_REG);
2152 /* FIXME: Avoid creating duplicate methods */
2154 if (ins->flags & MONO_INST_HAS_METHOD) {
2158 if (cfg->compile_aot) {
2159 callee = get_plt_entry (ctx, llvm_sig, MONO_PATCH_INFO_METHOD, call->method);
2161 LLVM_FAILURE (ctx, "can't encode patch");
2163 callee = LLVMAddFunction (module, "", llvm_sig);
2166 mono_create_jit_trampoline_in_domain (mono_domain_get (),
2168 LLVMAddGlobalMapping (ctx->lmodule->ee, callee, target);
2172 if (call->method && strstr (call->method->klass->name, "AsyncVoidMethodBuilder"))
2173 /* LLVM miscompiles async methods */
2174 LLVM_FAILURE (ctx, "#13734");
2177 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
2183 memset (&ji, 0, sizeof (ji));
2184 ji.type = MONO_PATCH_INFO_JIT_ICALL_ADDR;
2185 ji.data.target = info->name;
2187 target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &ji, FALSE);
2189 if (cfg->compile_aot) {
2190 callee = get_plt_entry (ctx, llvm_sig, MONO_PATCH_INFO_INTERNAL_METHOD, (char*)info->name);
2192 LLVM_FAILURE (ctx, "can't encode patch");
2194 callee = LLVMAddFunction (module, "", llvm_sig);
2195 target = (gpointer)mono_icall_get_wrapper (info);
2196 LLVMAddGlobalMapping (ctx->lmodule->ee, callee, target);
2199 if (cfg->compile_aot) {
2201 if (cfg->abs_patches) {
2202 MonoJumpInfo *abs_ji = g_hash_table_lookup (cfg->abs_patches, call->fptr);
2204 callee = get_plt_entry (ctx, llvm_sig, abs_ji->type, abs_ji->data.target);
2206 LLVM_FAILURE (ctx, "can't encode patch");
2210 LLVM_FAILURE (ctx, "aot");
2212 callee = LLVMAddFunction (module, "", llvm_sig);
2214 if (cfg->abs_patches) {
2215 MonoJumpInfo *abs_ji = g_hash_table_lookup (cfg->abs_patches, call->fptr);
2218 * FIXME: Some trampolines might have
2219 * their own calling convention on some platforms.
2221 #ifndef TARGET_AMD64
2222 if (abs_ji->type == MONO_PATCH_INFO_MONITOR_ENTER || abs_ji->type == MONO_PATCH_INFO_MONITOR_ENTER_V4 ||
2223 abs_ji->type == MONO_PATCH_INFO_MONITOR_EXIT || abs_ji->type == MONO_PATCH_INFO_GENERIC_CLASS_INIT)
2224 LLVM_FAILURE (ctx, "trampoline with own cconv");
2226 target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, abs_ji, FALSE);
2227 LLVMAddGlobalMapping (ctx->lmodule->ee, callee, target);
2231 LLVMAddGlobalMapping (ctx->lmodule->ee, callee, (gpointer)call->fptr);
2237 int size = sizeof (gpointer);
2240 g_assert (ins->inst_offset % size == 0);
2241 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
2243 callee = convert (ctx, LLVMBuildLoad (builder, LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (LLVMPointerType (IntPtrType (), 0), 0)), &index, 1, ""), ""), LLVMPointerType (llvm_sig, 0));
2245 callee = convert (ctx, values [ins->sreg1], LLVMPointerType (llvm_sig, 0));
2247 if (ins->flags & MONO_INST_HAS_METHOD) {
2252 * Collect and convert arguments
2254 nargs = (sig->param_count * 16) + sig->hasthis + vretaddr + call->rgctx_reg + call->imt_arg_reg;
2255 len = sizeof (LLVMValueRef) * nargs;
2256 args = alloca (len);
2257 memset (args, 0, len);
2258 l = call->out_ireg_args;
2260 if (call->rgctx_arg_reg) {
2261 g_assert (values [call->rgctx_arg_reg]);
2262 g_assert (sinfo.rgctx_arg_pindex < nargs);
2264 * On ARM, the imt/rgctx argument is passed in a caller save register, but some of our trampolines etc. clobber it, leading to
2265 * problems is LLVM moves the arg assignment earlier. To work around this, save the argument into a stack slot and load
2266 * it using a volatile load.
2269 if (!ctx->imt_rgctx_loc)
2270 ctx->imt_rgctx_loc = build_alloca_llvm_type (ctx, ctx->lmodule->ptr_type, sizeof (gpointer));
2271 LLVMBuildStore (builder, convert (ctx, ctx->values [call->rgctx_arg_reg], ctx->lmodule->ptr_type), ctx->imt_rgctx_loc);
2272 args [sinfo.rgctx_arg_pindex] = mono_llvm_build_load (builder, ctx->imt_rgctx_loc, "", TRUE, LLVM_BARRIER_NONE);
2274 args [sinfo.rgctx_arg_pindex] = convert (ctx, values [call->rgctx_arg_reg], ctx->lmodule->ptr_type);
2277 if (call->imt_arg_reg) {
2278 g_assert (values [call->imt_arg_reg]);
2279 g_assert (sinfo.imt_arg_pindex < nargs);
2281 if (!ctx->imt_rgctx_loc)
2282 ctx->imt_rgctx_loc = build_alloca_llvm_type (ctx, ctx->lmodule->ptr_type, sizeof (gpointer));
2283 LLVMBuildStore (builder, convert (ctx, ctx->values [call->imt_arg_reg], ctx->lmodule->ptr_type), ctx->imt_rgctx_loc);
2284 args [sinfo.imt_arg_pindex] = mono_llvm_build_load (builder, ctx->imt_rgctx_loc, "", TRUE, LLVM_BARRIER_NONE);
2286 args [sinfo.imt_arg_pindex] = convert (ctx, values [call->imt_arg_reg], ctx->lmodule->ptr_type);
2291 if (!addresses [call->inst.dreg])
2292 addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
2293 g_assert (sinfo.vret_arg_pindex < nargs);
2294 args [sinfo.vret_arg_pindex] = LLVMBuildPtrToInt (builder, addresses [call->inst.dreg], IntPtrType (), "");
2297 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2300 LLVMArgInfo *ainfo = call->cinfo ? &call->cinfo->args [i] : NULL;
2304 pindex = sinfo.this_arg_pindex;
2306 pindex = sinfo.pindexes [i - 1];
2308 pindex = sinfo.pindexes [i];
2311 regpair = (guint32)(gssize)(l->data);
2312 reg = regpair & 0xffffff;
2313 args [pindex] = values [reg];
2314 switch (ainfo->storage) {
2315 case LLVMArgVtypeInReg:
2316 case LLVMArgAsFpArgs: {
2319 g_assert (addresses [reg]);
2320 emit_vtype_to_args (ctx, builder, sig->params [i - sig->hasthis], addresses [reg], ainfo, args + pindex, &nargs);
2324 // FIXME: Get rid of the VMOVE
2327 case LLVMArgVtypeByVal:
2328 g_assert (addresses [reg]);
2329 args [pindex] = addresses [reg];
2331 case LLVMArgAsIArgs:
2332 g_assert (addresses [reg]);
2333 args [pindex] = LLVMBuildLoad (ctx->builder, convert (ctx, addresses [reg], LLVMPointerType (LLVMArrayType (IntPtrType (), ainfo->nslots), 0)), "");
2336 g_assert (args [pindex]);
2337 if (i == 0 && sig->hasthis)
2338 args [pindex] = convert (ctx, args [pindex], ThisType ());
2340 args [pindex] = convert (ctx, args [pindex], type_to_llvm_arg_type (ctx, sig->params [i - sig->hasthis]));
2343 g_assert (pindex <= nargs);
2348 // FIXME: Align call sites
2354 lcall = emit_call (ctx, bb, &builder, callee, args, LLVMCountParamTypes (llvm_sig));
2357 * Modify cconv and parameter attributes to pass rgctx/imt correctly.
2359 #if defined(MONO_ARCH_IMT_REG) && defined(MONO_ARCH_RGCTX_REG)
2360 g_assert (MONO_ARCH_IMT_REG == MONO_ARCH_RGCTX_REG);
2362 /* The two can't be used together, so use only one LLVM calling conv to pass them */
2363 g_assert (!(call->rgctx_arg_reg && call->imt_arg_reg));
2365 LLVMSetInstructionCallConv (lcall, LLVMMono1CallConv);
2367 if (call->rgctx_arg_reg)
2368 LLVMAddInstrAttribute (lcall, 1 + sinfo.rgctx_arg_pindex, LLVMInRegAttribute);
2369 if (call->imt_arg_reg)
2370 LLVMAddInstrAttribute (lcall, 1 + sinfo.imt_arg_pindex, LLVMInRegAttribute);
2372 /* Add byval attributes if needed */
2373 for (i = 0; i < sig->param_count; ++i) {
2374 LLVMArgInfo *ainfo = call->cinfo ? &call->cinfo->args [i + sig->hasthis] : NULL;
2376 if (ainfo && ainfo->storage == LLVMArgVtypeByVal) {
2377 LLVMAddInstrAttribute (lcall, 1 + sinfo.pindexes [i], LLVMByValAttribute);
2382 * Convert the result
2385 switch (cinfo->ret.storage) {
2386 case LLVMArgVtypeInReg: {
2387 LLVMValueRef regs [2];
2389 if (!addresses [ins->dreg])
2390 addresses [ins->dreg] = build_alloca (ctx, sig->ret);
2392 regs [0] = LLVMBuildExtractValue (builder, lcall, 0, "");
2393 if (cinfo->ret.pair_storage [1] != LLVMArgNone)
2394 regs [1] = LLVMBuildExtractValue (builder, lcall, 1, "");
2395 emit_args_to_vtype (ctx, builder, sig->ret, addresses [ins->dreg], &cinfo->ret, regs);
2398 case LLVMArgVtypeByVal:
2399 if (!addresses [call->inst.dreg])
2400 addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
2401 LLVMBuildStore (builder, lcall, addresses [call->inst.dreg]);
2403 case LLVMArgFpStruct:
2404 if (!addresses [call->inst.dreg])
2405 addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
2406 LLVMBuildStore (builder, lcall, convert_full (ctx, addresses [call->inst.dreg], LLVMPointerType (LLVMTypeOf (lcall), 0), FALSE));
2409 if (sig->ret->type != MONO_TYPE_VOID && !vretaddr)
2410 /* If the method returns an unsigned value, need to zext it */
2411 values [ins->dreg] = convert_full (ctx, lcall, llvm_type_to_stack_type (cfg, type_to_llvm_type (ctx, sig->ret)), type_is_unsigned (ctx, sig->ret));
2415 if (sig->ret->type != MONO_TYPE_VOID && !vretaddr)
2416 /* If the method returns an unsigned value, need to zext it */
2417 values [ins->dreg] = convert_full (ctx, lcall, llvm_type_to_stack_type (cfg, type_to_llvm_type (ctx, sig->ret)), type_is_unsigned (ctx, sig->ret));
2421 if (!addresses [call->inst.dreg])
2422 addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
2423 g_assert (sinfo.vret_arg_pindex < nargs);
2424 args [sinfo.vret_arg_pindex] = LLVMBuildPtrToInt (builder, addresses [call->inst.dreg], IntPtrType (), "");
2427 *builder_ref = ctx->builder;
2429 g_free (sinfo.pindexes);
2437 process_bb (EmitContext *ctx, MonoBasicBlock *bb)
2439 MonoCompile *cfg = ctx->cfg;
2440 MonoMethodSignature *sig = ctx->sig;
2441 LLVMValueRef method = ctx->lmethod;
2442 LLVMValueRef *values = ctx->values;
2443 LLVMValueRef *addresses = ctx->addresses;
2444 LLVMCallInfo *linfo = ctx->linfo;
2445 LLVMModuleRef module = ctx->module;
2446 BBInfo *bblocks = ctx->bblocks;
2448 LLVMBasicBlockRef cbb;
2449 LLVMBuilderRef builder, starting_builder;
2450 gboolean has_terminator;
2452 LLVMValueRef lhs, rhs;
2455 cbb = get_bb (ctx, bb);
2456 builder = create_builder (ctx);
2457 ctx->builder = builder;
2458 LLVMPositionBuilderAtEnd (builder, cbb);
2460 if (bb == cfg->bb_entry)
2461 emit_entry_bb (ctx, builder);
2462 CHECK_FAILURE (ctx);
2464 if (bb->flags & BB_EXCEPTION_HANDLER) {
2466 LLVMValueRef personality;
2467 LLVMBasicBlockRef target_bb;
2469 static gint32 mapping_inited;
2470 static int ti_generator;
2473 LLVMValueRef type_info;
2476 if (!bblocks [bb->block_num].invoke_target) {
2478 * LLVM asserts if llvm.eh.selector is called from a bblock which
2479 * doesn't have an invoke pointing at it.
2480 * Update: LLVM no longer asserts, but some tests in exceptions.exe now fail.
2482 LLVM_FAILURE (ctx, "handler without invokes");
2485 // <resultval> = landingpad <somety> personality <type> <pers_fn> <clause>+
2487 if (cfg->compile_aot) {
2488 /* Use a dummy personality function */
2489 personality = LLVMGetNamedFunction (module, "mono_aot_personality");
2490 g_assert (personality);
2492 personality = LLVMGetNamedFunction (module, "mono_personality");
2493 if (InterlockedCompareExchange (&mapping_inited, 1, 0) == 0)
2494 LLVMAddGlobalMapping (ctx->lmodule->ee, personality, mono_personality);
2497 i8ptr = LLVMPointerType (LLVMInt8Type (), 0);
2499 clause_index = (mono_get_block_region_notry (cfg, bb->region) >> 8) - 1;
2502 * Create the type info
2504 sprintf (ti_name, "type_info_%d", ti_generator);
2507 if (cfg->compile_aot) {
2508 /* decode_eh_frame () in aot-runtime.c will decode this */
2509 type_info = LLVMAddGlobal (module, LLVMInt32Type (), ti_name);
2510 LLVMSetInitializer (type_info, LLVMConstInt (LLVMInt32Type (), clause_index, FALSE));
2513 * These symbols are not really used, the clause_index is embedded into the EH tables generated by DwarfMonoException in LLVM.
2515 LLVMSetLinkage (type_info, LLVMInternalLinkage);
2518 * Enabling this causes llc to crash:
2519 * http://llvm.org/bugs/show_bug.cgi?id=6102
2521 //LLVM_FAILURE (ctx, "aot+clauses");
2523 // test_0_invalid_unbox_arrays () fails
2524 LLVM_FAILURE (ctx, "aot+clauses");
2528 * After the cfg mempool is freed, the type info will point to stale memory,
2529 * but this is not a problem, since we decode it once in exception_cb during
2532 ti = mono_mempool_alloc (cfg->mempool, sizeof (gint32));
2533 *(gint32*)ti = clause_index;
2535 type_info = LLVMAddGlobal (module, i8ptr, ti_name);
2537 LLVMAddGlobalMapping (ctx->lmodule->ee, type_info, ti);
2541 LLVMTypeRef members [2], ret_type;
2542 LLVMValueRef landing_pad;
2544 members [0] = i8ptr;
2545 members [1] = LLVMInt32Type ();
2546 ret_type = LLVMStructType (members, 2, FALSE);
2548 landing_pad = LLVMBuildLandingPad (builder, ret_type, personality, 1, "");
2549 LLVMAddClause (landing_pad, type_info);
2551 /* Store the exception into the exvar */
2552 if (bb->in_scount == 1) {
2553 g_assert (bb->in_scount == 1);
2554 exvar = bb->in_stack [0];
2556 // FIXME: This is shared with filter clauses ?
2557 g_assert (!values [exvar->dreg]);
2559 values [exvar->dreg] = LLVMBuildExtractValue (builder, landing_pad, 0, "ex_obj");
2560 emit_volatile_store (ctx, exvar->dreg);
2564 /* Start a new bblock which CALL_HANDLER can branch to */
2565 target_bb = bblocks [bb->block_num].call_handler_target_bb;
2567 LLVMBuildBr (builder, target_bb);
2569 ctx->builder = builder = create_builder (ctx);
2570 LLVMPositionBuilderAtEnd (ctx->builder, target_bb);
2572 ctx->bblocks [bb->block_num].end_bblock = target_bb;
2576 has_terminator = FALSE;
2577 starting_builder = builder;
2578 for (ins = bb->code; ins; ins = ins->next) {
2579 const char *spec = LLVM_INS_INFO (ins->opcode);
2581 char dname_buf [128];
2583 emit_dbg_loc (ctx, builder, ins->cil_code);
2586 if (nins > 5000 && builder == starting_builder) {
2587 /* some steps in llc are non-linear in the size of basic blocks, see #5714 */
2588 LLVM_FAILURE (ctx, "basic block too long");
2592 /* There could be instructions after a terminator, skip them */
2595 if (spec [MONO_INST_DEST] != ' ' && !MONO_IS_STORE_MEMBASE (ins)) {
2596 sprintf (dname_buf, "t%d", ins->dreg);
2600 if (spec [MONO_INST_SRC1] != ' ' && spec [MONO_INST_SRC1] != 'v') {
2601 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
2603 if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
2604 lhs = emit_volatile_load (ctx, ins->sreg1);
2606 /* It is ok for SETRET to have an uninitialized argument */
2607 if (!values [ins->sreg1] && ins->opcode != OP_SETRET)
2608 LLVM_FAILURE (ctx, "sreg1");
2609 lhs = values [ins->sreg1];
2615 if (spec [MONO_INST_SRC2] != ' ' && spec [MONO_INST_SRC2] != ' ') {
2616 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
2617 if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
2618 rhs = emit_volatile_load (ctx, ins->sreg2);
2620 if (!values [ins->sreg2])
2621 LLVM_FAILURE (ctx, "sreg2");
2622 rhs = values [ins->sreg2];
2628 //mono_print_ins (ins);
2629 switch (ins->opcode) {
2632 case OP_LIVERANGE_START:
2633 case OP_LIVERANGE_END:
2636 values [ins->dreg] = LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE);
2639 #if SIZEOF_VOID_P == 4
2640 values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
2642 values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), (gint64)ins->inst_c0, FALSE);
2646 values [ins->dreg] = LLVMConstReal (LLVMDoubleType (), *(double*)ins->inst_p0);
2650 values [ins->dreg] = LLVMConstReal (LLVMFloatType (), *(float*)ins->inst_p0);
2652 values [ins->dreg] = LLVMConstFPExt (LLVMConstReal (LLVMFloatType (), *(float*)ins->inst_p0), LLVMDoubleType ());
2654 case OP_DUMMY_ICONST:
2655 values [ins->dreg] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2657 case OP_DUMMY_I8CONST:
2658 values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
2660 case OP_DUMMY_R8CONST:
2661 values [ins->dreg] = LLVMConstReal (LLVMDoubleType (), 0.0f);
2664 LLVMBuildBr (builder, get_bb (ctx, ins->inst_target_bb));
2665 has_terminator = TRUE;
2671 LLVMBasicBlockRef new_bb;
2672 LLVMBuilderRef new_builder;
2674 // The default branch is already handled
2675 // FIXME: Handle it here
2677 /* Start new bblock */
2678 sprintf (bb_name, "SWITCH_DEFAULT_BB%d", ctx->default_index ++);
2679 new_bb = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
2681 lhs = convert (ctx, lhs, LLVMInt32Type ());
2682 v = LLVMBuildSwitch (builder, lhs, new_bb, GPOINTER_TO_UINT (ins->klass));
2683 for (i = 0; i < GPOINTER_TO_UINT (ins->klass); ++i) {
2684 MonoBasicBlock *target_bb = ins->inst_many_bb [i];
2686 LLVMAddCase (v, LLVMConstInt (LLVMInt32Type (), i, FALSE), get_bb (ctx, target_bb));
2689 new_builder = create_builder (ctx);
2690 LLVMPositionBuilderAtEnd (new_builder, new_bb);
2691 LLVMBuildUnreachable (new_builder);
2693 has_terminator = TRUE;
2694 g_assert (!ins->next);
2700 if (linfo->ret.storage == LLVMArgVtypeInReg) {
2701 LLVMTypeRef ret_type = LLVMGetReturnType (LLVMGetElementType (LLVMTypeOf (method)));
2702 LLVMValueRef part1, retval;
2705 size = get_vtype_size (sig->ret);
2707 g_assert (addresses [ins->sreg1]);
2709 g_assert (linfo->ret.pair_storage [0] == LLVMArgInIReg);
2710 g_assert (linfo->ret.pair_storage [1] == LLVMArgNone);
2712 part1 = convert (ctx, LLVMBuildLoad (builder, LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMIntType (size * 8), 0), ""), ""), IntPtrType ());
2714 retval = LLVMBuildInsertValue (builder, LLVMGetUndef (ret_type), part1, 0, "");
2716 LLVMBuildRet (builder, retval);
2720 if (linfo->ret.storage == LLVMArgVtypeByVal) {
2721 LLVMValueRef retval;
2723 g_assert (addresses [ins->sreg1]);
2724 retval = LLVMBuildLoad (builder, addresses [ins->sreg1], "");
2725 LLVMBuildRet (builder, retval);
2729 if (linfo->ret.storage == LLVMArgVtypeRetAddr) {
2730 LLVMBuildRetVoid (builder);
2734 if (linfo->ret.storage == LLVMArgFpStruct) {
2735 LLVMTypeRef ret_type = LLVMGetReturnType (LLVMGetElementType (LLVMTypeOf (method)));
2736 LLVMValueRef retval;
2738 g_assert (addresses [ins->sreg1]);
2739 retval = LLVMBuildLoad (builder, convert (ctx, addresses [ins->sreg1], LLVMPointerType (ret_type, 0)), "");
2740 LLVMBuildRet (builder, retval);
2744 if (!lhs || ctx->is_dead [ins->sreg1]) {
2746 * The method did not set its return value, probably because it
2747 * ends with a throw.
2750 LLVMBuildRetVoid (builder);
2752 LLVMBuildRet (builder, LLVMConstNull (type_to_llvm_type (ctx, sig->ret)));
2754 LLVMBuildRet (builder, convert (ctx, lhs, type_to_llvm_type (ctx, sig->ret)));
2756 has_terminator = TRUE;
2763 case OP_ICOMPARE_IMM:
2764 case OP_LCOMPARE_IMM:
2765 case OP_COMPARE_IMM: {
2769 if (ins->next->opcode == OP_NOP)
2772 if (ins->next->opcode == OP_BR)
2773 /* The comparison result is not needed */
2776 rel = mono_opcode_to_cond (ins->next->opcode);
2778 if (ins->opcode == OP_ICOMPARE_IMM) {
2779 lhs = convert (ctx, lhs, LLVMInt32Type ());
2780 rhs = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
2782 if (ins->opcode == OP_LCOMPARE_IMM) {
2783 lhs = convert (ctx, lhs, LLVMInt64Type ());
2784 rhs = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
2786 if (ins->opcode == OP_LCOMPARE) {
2787 lhs = convert (ctx, lhs, LLVMInt64Type ());
2788 rhs = convert (ctx, rhs, LLVMInt64Type ());
2790 if (ins->opcode == OP_ICOMPARE) {
2791 lhs = convert (ctx, lhs, LLVMInt32Type ());
2792 rhs = convert (ctx, rhs, LLVMInt32Type ());
2796 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
2797 rhs = convert (ctx, rhs, LLVMTypeOf (lhs));
2798 else if (LLVMGetTypeKind (LLVMTypeOf (rhs)) == LLVMPointerTypeKind)
2799 lhs = convert (ctx, lhs, LLVMTypeOf (rhs));
2802 /* We use COMPARE+SETcc/Bcc, llvm uses SETcc+br cond */
2803 if (ins->opcode == OP_FCOMPARE) {
2804 cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMDoubleType ()), convert (ctx, rhs, LLVMDoubleType ()), "");
2805 } else if (ins->opcode == OP_RCOMPARE) {
2806 cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMFloatType ()), convert (ctx, rhs, LLVMFloatType ()), "");
2807 } else if (ins->opcode == OP_COMPARE_IMM) {
2808 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind && ins->inst_imm == 0)
2809 cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], lhs, LLVMConstNull (LLVMTypeOf (lhs)), "");
2811 cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), "");
2812 } else if (ins->opcode == OP_LCOMPARE_IMM) {
2813 if (SIZEOF_REGISTER == 4 && COMPILE_LLVM (cfg)) {
2814 /* The immediate is encoded in two fields */
2815 guint64 l = ((guint64)(guint32)ins->inst_offset << 32) | ((guint32)ins->inst_imm);
2816 cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, LLVMInt64Type ()), LLVMConstInt (LLVMInt64Type (), l, FALSE), "");
2818 cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, LLVMInt64Type ()), LLVMConstInt (LLVMInt64Type (), ins->inst_imm, FALSE), "");
2821 else if (ins->opcode == OP_COMPARE) {
2822 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind && LLVMTypeOf (lhs) == LLVMTypeOf (rhs))
2823 cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], lhs, rhs, "");
2825 cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, IntPtrType ()), convert (ctx, rhs, IntPtrType ()), "");
2827 cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], lhs, rhs, "");
2829 if (MONO_IS_COND_BRANCH_OP (ins->next)) {
2830 if (ins->next->inst_true_bb == ins->next->inst_false_bb) {
2832 * If the target bb contains PHI instructions, LLVM requires
2833 * two PHI entries for this bblock, while we only generate one.
2834 * So convert this to an unconditional bblock. (bxc #171).
2836 LLVMBuildBr (builder, get_bb (ctx, ins->next->inst_true_bb));
2838 LLVMBuildCondBr (builder, cmp, get_bb (ctx, ins->next->inst_true_bb), get_bb (ctx, ins->next->inst_false_bb));
2840 has_terminator = TRUE;
2841 } else if (MONO_IS_SETCC (ins->next)) {
2842 sprintf (dname_buf, "t%d", ins->next->dreg);
2844 values [ins->next->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
2846 /* Add stores for volatile variables */
2847 emit_volatile_store (ctx, ins->next->dreg);
2848 } else if (MONO_IS_COND_EXC (ins->next)) {
2849 emit_cond_system_exception (ctx, bb, ins->next->inst_p1, cmp);
2850 CHECK_FAILURE (ctx);
2851 builder = ctx->builder;
2853 LLVM_FAILURE (ctx, "next");
2867 rel = mono_opcode_to_cond (ins->opcode);
2869 cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMDoubleType ()), convert (ctx, rhs, LLVMDoubleType ()), "");
2870 values [ins->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
2881 rel = mono_opcode_to_cond (ins->opcode);
2883 cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMFloatType ()), convert (ctx, rhs, LLVMFloatType ()), "");
2884 values [ins->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
2892 gboolean empty = TRUE;
2894 /* Check that all input bblocks really branch to us */
2895 for (i = 0; i < bb->in_count; ++i) {
2896 if (bb->in_bb [i]->last_ins && bb->in_bb [i]->last_ins->opcode == OP_NOT_REACHED)
2897 ins->inst_phi_args [i + 1] = -1;
2903 /* LLVM doesn't like phi instructions with zero operands */
2904 ctx->is_dead [ins->dreg] = TRUE;
2908 /* Created earlier, insert it now */
2909 LLVMInsertIntoBuilder (builder, values [ins->dreg]);
2911 for (i = 0; i < ins->inst_phi_args [0]; i++) {
2912 int sreg1 = ins->inst_phi_args [i + 1];
2916 * Count the number of times the incoming bblock branches to us,
2917 * since llvm requires a separate entry for each.
2919 if (bb->in_bb [i]->last_ins && bb->in_bb [i]->last_ins->opcode == OP_SWITCH) {
2920 MonoInst *switch_ins = bb->in_bb [i]->last_ins;
2923 for (j = 0; j < GPOINTER_TO_UINT (switch_ins->klass); ++j) {
2924 if (switch_ins->inst_many_bb [j] == bb)
2931 /* Remember for later */
2932 for (j = 0; j < count; ++j) {
2933 PhiNode *node = mono_mempool_alloc0 (ctx->mempool, sizeof (PhiNode));
2936 node->in_bb = bb->in_bb [i];
2938 bblocks [bb->in_bb [i]->block_num].phi_nodes = g_slist_prepend_mempool (ctx->mempool, bblocks [bb->in_bb [i]->block_num].phi_nodes, node);
2948 values [ins->dreg] = lhs;
2952 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
2955 values [ins->dreg] = lhs;
2957 if (var && var->klass->byval_arg.type == MONO_TYPE_R4) {
2959 * This is added by the spilling pass in case of the JIT,
2960 * but we have to do it ourselves.
2962 values [ins->dreg] = convert (ctx, values [ins->dreg], LLVMFloatType ());
2966 case OP_MOVE_F_TO_I4: {
2967 values [ins->dreg] = LLVMBuildBitCast (builder, LLVMBuildFPTrunc (builder, lhs, LLVMFloatType (), ""), LLVMInt32Type (), "");
2970 case OP_MOVE_I4_TO_F: {
2971 values [ins->dreg] = LLVMBuildFPExt (builder, LLVMBuildBitCast (builder, lhs, LLVMFloatType (), ""), LLVMDoubleType (), "");
2974 case OP_MOVE_F_TO_I8: {
2975 values [ins->dreg] = LLVMBuildBitCast (builder, lhs, LLVMInt64Type (), "");
2978 case OP_MOVE_I8_TO_F: {
2979 values [ins->dreg] = LLVMBuildBitCast (builder, lhs, LLVMDoubleType (), "");
3012 lhs = convert (ctx, lhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
3013 rhs = convert (ctx, rhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
3015 #ifdef MONO_ARCH_NEED_DIV_CHECK
3016 switch (ins->opcode) {
3027 cmp = LLVMBuildICmp (builder, LLVMIntEQ, rhs, LLVMConstInt (LLVMTypeOf (rhs), 0, FALSE), "");
3028 emit_cond_system_exception (ctx, bb, "DivideByZeroException", cmp);
3029 CHECK_FAILURE (ctx);
3030 builder = ctx->builder;
3038 switch (ins->opcode) {
3041 values [ins->dreg] = LLVMBuildAdd (builder, lhs, rhs, dname);
3045 values [ins->dreg] = LLVMBuildSub (builder, lhs, rhs, dname);
3049 values [ins->dreg] = LLVMBuildMul (builder, lhs, rhs, dname);
3053 values [ins->dreg] = LLVMBuildSRem (builder, lhs, rhs, dname);
3057 values [ins->dreg] = LLVMBuildURem (builder, lhs, rhs, dname);
3061 values [ins->dreg] = LLVMBuildSDiv (builder, lhs, rhs, dname);
3065 values [ins->dreg] = LLVMBuildUDiv (builder, lhs, rhs, dname);
3069 values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, dname);
3073 values [ins->dreg] = LLVMBuildAnd (builder, lhs, rhs, dname);
3077 values [ins->dreg] = LLVMBuildOr (builder, lhs, rhs, dname);
3081 values [ins->dreg] = LLVMBuildXor (builder, lhs, rhs, dname);
3085 values [ins->dreg] = LLVMBuildShl (builder, lhs, rhs, dname);
3089 values [ins->dreg] = LLVMBuildAShr (builder, lhs, rhs, dname);
3093 values [ins->dreg] = LLVMBuildLShr (builder, lhs, rhs, dname);
3097 values [ins->dreg] = LLVMBuildFAdd (builder, lhs, rhs, dname);
3100 values [ins->dreg] = LLVMBuildFSub (builder, lhs, rhs, dname);
3103 values [ins->dreg] = LLVMBuildFMul (builder, lhs, rhs, dname);
3107 g_assert_not_reached ();
3114 lhs = convert (ctx, lhs, LLVMFloatType ());
3115 rhs = convert (ctx, rhs, LLVMFloatType ());
3116 switch (ins->opcode) {
3118 values [ins->dreg] = LLVMBuildFAdd (builder, lhs, rhs, dname);
3121 values [ins->dreg] = LLVMBuildFSub (builder, lhs, rhs, dname);
3124 values [ins->dreg] = LLVMBuildFMul (builder, lhs, rhs, dname);
3127 values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, dname);
3130 g_assert_not_reached ();
3139 case OP_IREM_UN_IMM:
3141 case OP_IDIV_UN_IMM:
3147 case OP_ISHR_UN_IMM:
3156 case OP_LSHR_UN_IMM:
3162 case OP_SHR_UN_IMM: {
3165 if (spec [MONO_INST_SRC1] == 'l') {
3166 imm = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
3168 imm = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
3171 #if SIZEOF_VOID_P == 4
3172 if (ins->opcode == OP_LSHL_IMM || ins->opcode == OP_LSHR_IMM || ins->opcode == OP_LSHR_UN_IMM)
3173 imm = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
3176 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
3177 lhs = convert (ctx, lhs, IntPtrType ());
3178 imm = convert (ctx, imm, LLVMTypeOf (lhs));
3179 switch (ins->opcode) {
3183 values [ins->dreg] = LLVMBuildAdd (builder, lhs, imm, dname);
3187 values [ins->dreg] = LLVMBuildSub (builder, lhs, imm, dname);
3191 values [ins->dreg] = LLVMBuildMul (builder, lhs, imm, dname);
3195 values [ins->dreg] = LLVMBuildSDiv (builder, lhs, imm, dname);
3197 case OP_IDIV_UN_IMM:
3198 case OP_LDIV_UN_IMM:
3199 values [ins->dreg] = LLVMBuildUDiv (builder, lhs, imm, dname);
3203 values [ins->dreg] = LLVMBuildSRem (builder, lhs, imm, dname);
3205 case OP_IREM_UN_IMM:
3206 values [ins->dreg] = LLVMBuildURem (builder, lhs, imm, dname);
3211 values [ins->dreg] = LLVMBuildAnd (builder, lhs, imm, dname);
3215 values [ins->dreg] = LLVMBuildOr (builder, lhs, imm, dname);
3219 values [ins->dreg] = LLVMBuildXor (builder, lhs, imm, dname);
3224 values [ins->dreg] = LLVMBuildShl (builder, lhs, imm, dname);
3229 values [ins->dreg] = LLVMBuildAShr (builder, lhs, imm, dname);
3231 case OP_ISHR_UN_IMM:
3232 /* This is used to implement conv.u4, so the lhs could be an i8 */
3233 lhs = convert (ctx, lhs, LLVMInt32Type ());
3234 imm = convert (ctx, imm, LLVMInt32Type ());
3235 values [ins->dreg] = LLVMBuildLShr (builder, lhs, imm, dname);
3237 case OP_LSHR_UN_IMM:
3239 values [ins->dreg] = LLVMBuildLShr (builder, lhs, imm, dname);
3242 g_assert_not_reached ();
3247 values [ins->dreg] = LLVMBuildSub (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), convert (ctx, lhs, LLVMInt32Type ()), dname);
3250 values [ins->dreg] = LLVMBuildSub (builder, LLVMConstInt (LLVMInt64Type (), 0, FALSE), lhs, dname);
3253 lhs = convert (ctx, lhs, LLVMDoubleType ());
3254 values [ins->dreg] = LLVMBuildFSub (builder, LLVMConstReal (LLVMDoubleType (), 0.0), lhs, dname);
3257 lhs = convert (ctx, lhs, LLVMFloatType ());
3258 values [ins->dreg] = LLVMBuildFSub (builder, LLVMConstReal (LLVMFloatType (), 0.0), lhs, dname);
3261 guint32 v = 0xffffffff;
3262 values [ins->dreg] = LLVMBuildXor (builder, LLVMConstInt (LLVMInt32Type (), v, FALSE), convert (ctx, lhs, LLVMInt32Type ()), dname);
3266 guint64 v = 0xffffffffffffffffLL;
3267 values [ins->dreg] = LLVMBuildXor (builder, LLVMConstInt (LLVMInt64Type (), v, FALSE), lhs, dname);
3270 #if defined(TARGET_X86) || defined(TARGET_AMD64)
3272 LLVMValueRef v1, v2;
3274 v1 = LLVMBuildMul (builder, convert (ctx, rhs, IntPtrType ()), LLVMConstInt (IntPtrType (), (1 << ins->backend.shift_amount), FALSE), "");
3275 v2 = LLVMBuildAdd (builder, convert (ctx, lhs, IntPtrType ()), v1, "");
3276 values [ins->dreg] = LLVMBuildAdd (builder, v2, LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), dname);
3281 case OP_ICONV_TO_I1:
3282 case OP_ICONV_TO_I2:
3283 case OP_ICONV_TO_I4:
3284 case OP_ICONV_TO_U1:
3285 case OP_ICONV_TO_U2:
3286 case OP_ICONV_TO_U4:
3287 case OP_LCONV_TO_I1:
3288 case OP_LCONV_TO_I2:
3289 case OP_LCONV_TO_U1:
3290 case OP_LCONV_TO_U2:
3291 case OP_LCONV_TO_U4: {
3294 sign = (ins->opcode == OP_ICONV_TO_I1) || (ins->opcode == OP_ICONV_TO_I2) || (ins->opcode == OP_ICONV_TO_I4) || (ins->opcode == OP_LCONV_TO_I1) || (ins->opcode == OP_LCONV_TO_I2);
3296 /* Have to do two casts since our vregs have type int */
3297 v = LLVMBuildTrunc (builder, lhs, op_to_llvm_type (ins->opcode), "");
3299 values [ins->dreg] = LLVMBuildSExt (builder, v, LLVMInt32Type (), dname);
3301 values [ins->dreg] = LLVMBuildZExt (builder, v, LLVMInt32Type (), dname);
3304 case OP_ICONV_TO_I8:
3305 values [ins->dreg] = LLVMBuildSExt (builder, lhs, LLVMInt64Type (), dname);
3307 case OP_ICONV_TO_U8:
3308 values [ins->dreg] = LLVMBuildZExt (builder, lhs, LLVMInt64Type (), dname);
3310 case OP_FCONV_TO_I4:
3311 case OP_RCONV_TO_I4:
3312 values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, LLVMInt32Type (), dname);
3314 case OP_FCONV_TO_I1:
3315 case OP_RCONV_TO_I1:
3316 values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildFPToSI (builder, lhs, LLVMInt8Type (), dname), LLVMInt32Type (), "");
3318 case OP_FCONV_TO_U1:
3319 case OP_RCONV_TO_U1:
3320 values [ins->dreg] = LLVMBuildZExt (builder, LLVMBuildFPToUI (builder, lhs, LLVMInt8Type (), dname), LLVMInt32Type (), "");
3322 case OP_FCONV_TO_I2:
3323 case OP_RCONV_TO_I2:
3324 values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildFPToSI (builder, lhs, LLVMInt16Type (), dname), LLVMInt32Type (), "");
3326 case OP_FCONV_TO_U2:
3327 case OP_RCONV_TO_U2:
3328 values [ins->dreg] = LLVMBuildZExt (builder, LLVMBuildFPToUI (builder, lhs, LLVMInt16Type (), dname), LLVMInt32Type (), "");
3330 case OP_FCONV_TO_I8:
3331 case OP_RCONV_TO_I8:
3332 values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, LLVMInt64Type (), dname);
3335 values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, IntPtrType (), dname);
3337 case OP_ICONV_TO_R8:
3338 case OP_LCONV_TO_R8:
3339 values [ins->dreg] = LLVMBuildSIToFP (builder, lhs, LLVMDoubleType (), dname);
3341 case OP_LCONV_TO_R_UN:
3342 values [ins->dreg] = LLVMBuildUIToFP (builder, lhs, LLVMDoubleType (), dname);
3344 #if SIZEOF_VOID_P == 4
3347 case OP_LCONV_TO_I4:
3348 values [ins->dreg] = LLVMBuildTrunc (builder, lhs, LLVMInt32Type (), dname);
3350 case OP_ICONV_TO_R4:
3351 case OP_LCONV_TO_R4:
3352 v = LLVMBuildSIToFP (builder, lhs, LLVMFloatType (), "");
3354 values [ins->dreg] = v;
3356 values [ins->dreg] = LLVMBuildFPExt (builder, v, LLVMDoubleType (), dname);
3358 case OP_FCONV_TO_R4:
3359 v = LLVMBuildFPTrunc (builder, lhs, LLVMFloatType (), "");
3361 values [ins->dreg] = v;
3363 values [ins->dreg] = LLVMBuildFPExt (builder, v, LLVMDoubleType (), dname);
3365 case OP_RCONV_TO_R8:
3366 values [ins->dreg] = LLVMBuildFPExt (builder, lhs, LLVMDoubleType (), dname);
3368 case OP_RCONV_TO_R4:
3369 values [ins->dreg] = lhs;
3372 values [ins->dreg] = LLVMBuildSExt (builder, convert (ctx, lhs, LLVMInt32Type ()), LLVMInt64Type (), dname);
3375 values [ins->dreg] = LLVMBuildZExt (builder, convert (ctx, lhs, LLVMInt32Type ()), LLVMInt64Type (), dname);
3378 values [ins->dreg] = LLVMBuildTrunc (builder, lhs, LLVMInt32Type (), dname);
3380 case OP_LOCALLOC_IMM: {
3383 guint32 size = ins->inst_imm;
3384 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
3386 v = mono_llvm_build_alloca (builder, LLVMInt8Type (), LLVMConstInt (LLVMInt32Type (), size, FALSE), MONO_ARCH_FRAME_ALIGNMENT, "");
3388 if (ins->flags & MONO_INST_INIT) {
3389 LLVMValueRef args [5];
3392 args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
3393 args [2] = LLVMConstInt (LLVMInt32Type (), size, FALSE);
3394 args [3] = LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT, FALSE);
3395 args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
3396 LLVMBuildCall (builder, LLVMGetNamedFunction (module, memset_func_name), args, memset_param_count, "");
3399 values [ins->dreg] = v;
3403 LLVMValueRef v, size;
3405 size = LLVMBuildAnd (builder, LLVMBuildAdd (builder, convert (ctx, lhs, LLVMInt32Type ()), LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT - 1, FALSE), ""), LLVMConstInt (LLVMInt32Type (), ~ (MONO_ARCH_FRAME_ALIGNMENT - 1), FALSE), "");
3407 v = mono_llvm_build_alloca (builder, LLVMInt8Type (), size, MONO_ARCH_FRAME_ALIGNMENT, "");
3409 if (ins->flags & MONO_INST_INIT) {
3410 LLVMValueRef args [5];
3413 args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
3415 args [3] = LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT, FALSE);
3416 args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
3417 LLVMBuildCall (builder, LLVMGetNamedFunction (module, memset_func_name), args, memset_param_count, "");
3419 values [ins->dreg] = v;
3423 case OP_LOADI1_MEMBASE:
3424 case OP_LOADU1_MEMBASE:
3425 case OP_LOADI2_MEMBASE:
3426 case OP_LOADU2_MEMBASE:
3427 case OP_LOADI4_MEMBASE:
3428 case OP_LOADU4_MEMBASE:
3429 case OP_LOADI8_MEMBASE:
3430 case OP_LOADR4_MEMBASE:
3431 case OP_LOADR8_MEMBASE:
3432 case OP_LOAD_MEMBASE:
3440 LLVMValueRef base, index, addr;
3442 gboolean sext = FALSE, zext = FALSE;
3443 gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
3445 t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
3450 if ((ins->opcode == OP_LOADI8_MEM) || (ins->opcode == OP_LOAD_MEM) || (ins->opcode == OP_LOADI4_MEM) || (ins->opcode == OP_LOADU4_MEM) || (ins->opcode == OP_LOADU1_MEM) || (ins->opcode == OP_LOADU2_MEM)) {
3451 addr = LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE);
3456 if (ins->inst_offset == 0) {
3458 } else if (ins->inst_offset % size != 0) {
3459 /* Unaligned load */
3460 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
3461 addr = LLVMBuildGEP (builder, convert (ctx, base, LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
3463 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
3464 addr = LLVMBuildGEP (builder, convert (ctx, base, LLVMPointerType (t, 0)), &index, 1, "");
3468 addr = convert (ctx, addr, LLVMPointerType (t, 0));
3470 values [ins->dreg] = emit_load (ctx, bb, &builder, size, addr, dname, is_volatile);
3472 if (!is_volatile && (ins->flags & MONO_INST_INVARIANT_LOAD)) {
3474 * These will signal LLVM that these loads do not alias any stores, and
3475 * they can't fail, allowing them to be hoisted out of loops.
3477 set_invariant_load_flag (values [ins->dreg]);
3478 set_metadata_flag (values [ins->dreg], "mono.nofail.load");
3482 values [ins->dreg] = LLVMBuildSExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
3484 values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
3485 else if (!cfg->r4fp && ins->opcode == OP_LOADR4_MEMBASE)
3486 values [ins->dreg] = LLVMBuildFPExt (builder, values [ins->dreg], LLVMDoubleType (), dname);
3490 case OP_STOREI1_MEMBASE_REG:
3491 case OP_STOREI2_MEMBASE_REG:
3492 case OP_STOREI4_MEMBASE_REG:
3493 case OP_STOREI8_MEMBASE_REG:
3494 case OP_STORER4_MEMBASE_REG:
3495 case OP_STORER8_MEMBASE_REG:
3496 case OP_STORE_MEMBASE_REG: {
3498 LLVMValueRef index, addr;
3500 gboolean sext = FALSE, zext = FALSE;
3501 gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
3503 if (!values [ins->inst_destbasereg])
3504 LLVM_FAILURE (ctx, "inst_destbasereg");
3506 t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
3508 if (ins->inst_offset % size != 0) {
3509 /* Unaligned store */
3510 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
3511 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
3513 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
3514 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, "");
3516 emit_store (ctx, bb, &builder, size, convert (ctx, values [ins->sreg1], t), convert (ctx, addr, LLVMPointerType (t, 0)), is_volatile);
3520 case OP_STOREI1_MEMBASE_IMM:
3521 case OP_STOREI2_MEMBASE_IMM:
3522 case OP_STOREI4_MEMBASE_IMM:
3523 case OP_STOREI8_MEMBASE_IMM:
3524 case OP_STORE_MEMBASE_IMM: {
3526 LLVMValueRef index, addr;
3528 gboolean sext = FALSE, zext = FALSE;
3529 gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
3531 t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
3533 if (ins->inst_offset % size != 0) {
3534 /* Unaligned store */
3535 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
3536 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
3538 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
3539 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, "");
3541 emit_store (ctx, bb, &builder, size, convert (ctx, LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), t), convert (ctx, addr, LLVMPointerType (t, 0)), is_volatile);
3546 emit_load (ctx, bb, &builder, sizeof (gpointer), convert (ctx, lhs, LLVMPointerType (IntPtrType (), 0)), "", TRUE);
3548 case OP_OUTARG_VTRETADDR:
3556 case OP_VOIDCALL_MEMBASE:
3557 case OP_CALL_MEMBASE:
3558 case OP_LCALL_MEMBASE:
3559 case OP_FCALL_MEMBASE:
3560 case OP_RCALL_MEMBASE:
3561 case OP_VCALL_MEMBASE:
3562 case OP_VOIDCALL_REG:
3567 case OP_VCALL_REG: {
3568 process_call (ctx, bb, &builder, ins);
3569 CHECK_FAILURE (ctx);
3574 LLVMValueRef indexes [2];
3576 LLVMValueRef got_entry_addr;
3579 * FIXME: Can't allocate from the cfg mempool since that is freed if
3580 * the LLVM compile fails.
3582 ji = g_new0 (MonoJumpInfo, 1);
3583 ji->type = (MonoJumpInfoType)ins->inst_i1;
3584 ji->data.target = ins->inst_p0;
3586 ji = mono_aot_patch_info_dup (ji);
3588 ji->next = cfg->patch_info;
3589 cfg->patch_info = ji;
3591 //mono_add_patch_info (cfg, 0, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3592 got_offset = mono_aot_get_got_offset (cfg->patch_info);
3593 ctx->lmodule->max_got_offset = MAX (ctx->lmodule->max_got_offset, got_offset);
3595 indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
3596 indexes [1] = LLVMConstInt (LLVMInt32Type (), (gssize)got_offset, FALSE);
3597 got_entry_addr = LLVMBuildGEP (builder, ctx->lmodule->got_var, indexes, 2, "");
3599 values [ins->dreg] = LLVMBuildLoad (builder, got_entry_addr, dname);
3600 set_invariant_load_flag (values [ins->dreg]);
3603 case OP_NOT_REACHED:
3604 LLVMBuildUnreachable (builder);
3605 has_terminator = TRUE;
3606 g_assert (bb->block_num < cfg->max_block_num);
3607 ctx->unreachable [bb->block_num] = TRUE;
3608 /* Might have instructions after this */
3610 MonoInst *next = ins->next;
3612 * FIXME: If later code uses the regs defined by these instructions,
3613 * compilation will fail.
3615 MONO_DELETE_INS (bb, next);
3619 MonoInst *var = ins->inst_p0;
3621 values [ins->dreg] = addresses [var->dreg];
3625 LLVMValueRef args [1];
3627 args [0] = convert (ctx, lhs, LLVMDoubleType ());
3628 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.sin.f64"), args, 1, dname);
3632 LLVMValueRef args [1];
3634 args [0] = convert (ctx, lhs, LLVMDoubleType ());
3635 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.cos.f64"), args, 1, dname);
3639 LLVMValueRef args [1];
3642 /* This no longer seems to happen */
3644 * LLVM optimizes sqrt(nan) into undefined in
3645 * lib/Analysis/ConstantFolding.cpp
3646 * Also, sqrt(NegativeInfinity) is optimized into 0.
3648 LLVM_FAILURE (ctx, "sqrt");
3650 args [0] = convert (ctx, lhs, LLVMDoubleType ());
3651 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.sqrt.f64"), args, 1, dname);
3655 LLVMValueRef args [1];
3657 args [0] = convert (ctx, lhs, LLVMDoubleType ());
3658 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "fabs"), args, 1, dname);
3672 lhs = convert (ctx, lhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
3673 rhs = convert (ctx, rhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
3675 switch (ins->opcode) {
3678 v = LLVMBuildICmp (builder, LLVMIntSLE, lhs, rhs, "");
3682 v = LLVMBuildICmp (builder, LLVMIntSGE, lhs, rhs, "");
3686 v = LLVMBuildICmp (builder, LLVMIntULE, lhs, rhs, "");
3690 v = LLVMBuildICmp (builder, LLVMIntUGE, lhs, rhs, "");
3693 g_assert_not_reached ();
3696 values [ins->dreg] = LLVMBuildSelect (builder, v, lhs, rhs, dname);
3699 case OP_ATOMIC_EXCHANGE_I4:
3700 case OP_ATOMIC_EXCHANGE_I8: {
3701 LLVMValueRef args [2];
3704 if (ins->opcode == OP_ATOMIC_EXCHANGE_I4)
3705 t = LLVMInt32Type ();
3707 t = LLVMInt64Type ();
3709 g_assert (ins->inst_offset == 0);
3711 args [0] = convert (ctx, lhs, LLVMPointerType (t, 0));
3712 args [1] = convert (ctx, rhs, t);
3714 values [ins->dreg] = mono_llvm_build_atomic_rmw (builder, LLVM_ATOMICRMW_OP_XCHG, args [0], args [1]);
3717 case OP_ATOMIC_ADD_I4:
3718 case OP_ATOMIC_ADD_I8: {
3719 LLVMValueRef args [2];
3722 if (ins->opcode == OP_ATOMIC_ADD_I4)
3723 t = LLVMInt32Type ();
3725 t = LLVMInt64Type ();
3727 g_assert (ins->inst_offset == 0);
3729 args [0] = convert (ctx, lhs, LLVMPointerType (t, 0));
3730 args [1] = convert (ctx, rhs, t);
3731 values [ins->dreg] = LLVMBuildAdd (builder, mono_llvm_build_atomic_rmw (builder, LLVM_ATOMICRMW_OP_ADD, args [0], args [1]), args [1], dname);
3734 case OP_ATOMIC_CAS_I4:
3735 case OP_ATOMIC_CAS_I8: {
3736 LLVMValueRef args [3], val;
3739 if (ins->opcode == OP_ATOMIC_CAS_I4)
3740 t = LLVMInt32Type ();
3742 t = LLVMInt64Type ();
3744 args [0] = convert (ctx, lhs, LLVMPointerType (t, 0));
3746 args [1] = convert (ctx, values [ins->sreg3], t);
3748 args [2] = convert (ctx, values [ins->sreg2], t);
3749 val = mono_llvm_build_cmpxchg (builder, args [0], args [1], args [2]);
3750 /* cmpxchg returns a pair */
3751 values [ins->dreg] = LLVMBuildExtractValue (builder, val, 0, "");
3754 case OP_MEMORY_BARRIER: {
3755 mono_llvm_build_fence (builder, (BarrierKind) ins->backend.memory_barrier_kind);
3758 case OP_ATOMIC_LOAD_I1:
3759 case OP_ATOMIC_LOAD_I2:
3760 case OP_ATOMIC_LOAD_I4:
3761 case OP_ATOMIC_LOAD_I8:
3762 case OP_ATOMIC_LOAD_U1:
3763 case OP_ATOMIC_LOAD_U2:
3764 case OP_ATOMIC_LOAD_U4:
3765 case OP_ATOMIC_LOAD_U8:
3766 case OP_ATOMIC_LOAD_R4:
3767 case OP_ATOMIC_LOAD_R8: {
3768 LLVM_FAILURE (ctx, "atomic mono.load intrinsic");
3771 gboolean sext, zext;
3773 gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
3774 BarrierKind barrier = (BarrierKind) ins->backend.memory_barrier_kind;
3775 LLVMValueRef index, addr;
3777 t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
3782 if (ins->inst_offset != 0) {
3783 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
3784 addr = LLVMBuildGEP (builder, convert (ctx, lhs, LLVMPointerType (t, 0)), &index, 1, "");
3789 addr = convert (ctx, addr, LLVMPointerType (t, 0));
3791 values [ins->dreg] = emit_load_general (ctx, bb, &builder, size, addr, dname, is_volatile, barrier);
3794 values [ins->dreg] = LLVMBuildSExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
3796 values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
3799 case OP_ATOMIC_STORE_I1:
3800 case OP_ATOMIC_STORE_I2:
3801 case OP_ATOMIC_STORE_I4:
3802 case OP_ATOMIC_STORE_I8:
3803 case OP_ATOMIC_STORE_U1:
3804 case OP_ATOMIC_STORE_U2:
3805 case OP_ATOMIC_STORE_U4:
3806 case OP_ATOMIC_STORE_U8:
3807 case OP_ATOMIC_STORE_R4:
3808 case OP_ATOMIC_STORE_R8: {
3809 LLVM_FAILURE (ctx, "atomic mono.load intrinsic");
3812 gboolean sext, zext;
3814 gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
3815 BarrierKind barrier = (BarrierKind) ins->backend.memory_barrier_kind;
3816 LLVMValueRef index, addr, value;
3818 if (!values [ins->inst_destbasereg])
3819 LLVM_FAILURE (ctx, "inst_destbasereg");
3821 t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
3823 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
3824 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, "");
3825 value = convert (ctx, values [ins->sreg1], t);
3827 emit_store_general (ctx, bb, &builder, size, value, addr, is_volatile, barrier);
3830 case OP_RELAXED_NOP: {
3831 #if defined(TARGET_AMD64) || defined(TARGET_X86)
3832 emit_call (ctx, bb, &builder, LLVMGetNamedFunction (ctx->module, "llvm.x86.sse2.pause"), NULL, 0);
3839 #if (defined(TARGET_AMD64) || defined(TARGET_X86)) && defined(__linux__)
3841 // 257 == FS segment register
3842 LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 257);
3844 // 256 == GS segment register
3845 LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 256);
3848 values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildIntToPtr (builder, LLVMConstInt (IntPtrType (), ins->inst_offset, TRUE), ptrtype, ""), "");
3849 #elif defined(TARGET_AMD64) && defined(TARGET_OSX)
3850 /* See mono_amd64_emit_tls_get () */
3851 int offset = mono_amd64_get_tls_gs_offset () + (ins->inst_offset * 8);
3853 // 256 == GS segment register
3854 LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 256);
3855 values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildIntToPtr (builder, LLVMConstInt (IntPtrType (), offset, TRUE), ptrtype, ""), "");
3857 LLVM_FAILURE (ctx, "opcode tls-get");
3862 case OP_TLS_GET_REG: {
3863 #if defined(TARGET_AMD64) && defined(TARGET_OSX)
3864 /* See emit_tls_get_reg () */
3865 // 256 == GS segment register
3866 LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 256);
3867 values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildIntToPtr (builder, convert (ctx, lhs, LLVMInt32Type ()), ptrtype, ""), "");
3869 LLVM_FAILURE (ctx, "opcode tls-get");
3878 case OP_IADD_OVF_UN:
3880 case OP_ISUB_OVF_UN:
3882 case OP_IMUL_OVF_UN:
3883 #if SIZEOF_VOID_P == 8
3885 case OP_LADD_OVF_UN:
3887 case OP_LSUB_OVF_UN:
3889 case OP_LMUL_OVF_UN:
3892 LLVMValueRef args [2], val, ovf, func;
3894 args [0] = convert (ctx, lhs, op_to_llvm_type (ins->opcode));
3895 args [1] = convert (ctx, rhs, op_to_llvm_type (ins->opcode));
3896 func = LLVMGetNamedFunction (module, ovf_op_to_intrins (ins->opcode));
3898 val = LLVMBuildCall (builder, func, args, 2, "");
3899 values [ins->dreg] = LLVMBuildExtractValue (builder, val, 0, dname);
3900 ovf = LLVMBuildExtractValue (builder, val, 1, "");
3901 emit_cond_system_exception (ctx, bb, "OverflowException", ovf);
3902 CHECK_FAILURE (ctx);
3903 builder = ctx->builder;
3909 * We currently model them using arrays. Promotion to local vregs is
3910 * disabled for them in mono_handle_global_vregs () in the LLVM case,
3911 * so we always have an entry in cfg->varinfo for them.
3912 * FIXME: Is this needed ?
3915 MonoClass *klass = ins->klass;
3916 LLVMValueRef args [5];
3920 LLVM_FAILURE (ctx, "!klass");
3924 if (!addresses [ins->dreg])
3925 addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
3926 args [0] = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
3927 args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
3928 args [2] = LLVMConstInt (LLVMInt32Type (), mono_class_value_size (klass, NULL), FALSE);
3930 args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
3931 args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
3932 LLVMBuildCall (builder, LLVMGetNamedFunction (module, memset_func_name), args, memset_param_count, "");
3935 case OP_DUMMY_VZERO:
3938 case OP_STOREV_MEMBASE:
3939 case OP_LOADV_MEMBASE:
3941 MonoClass *klass = ins->klass;
3942 LLVMValueRef src = NULL, dst, args [5];
3943 gboolean done = FALSE;
3947 LLVM_FAILURE (ctx, "!klass");
3951 if (mini_is_gsharedvt_klass (cfg, klass)) {
3953 LLVM_FAILURE (ctx, "gsharedvt");
3957 switch (ins->opcode) {
3958 case OP_STOREV_MEMBASE:
3959 if (cfg->gen_write_barriers && klass->has_references && ins->inst_destbasereg != cfg->frame_reg &&
3960 LLVMGetInstructionOpcode (values [ins->inst_destbasereg]) != LLVMAlloca) {
3961 /* Decomposed earlier */
3962 g_assert_not_reached ();
3965 if (!addresses [ins->sreg1]) {
3967 g_assert (values [ins->sreg1]);
3968 dst = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_destbasereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (type_to_llvm_type (ctx, &klass->byval_arg), 0));
3969 LLVMBuildStore (builder, values [ins->sreg1], dst);
3972 src = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMInt8Type (), 0), "");
3973 dst = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_destbasereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (LLVMInt8Type (), 0));
3976 case OP_LOADV_MEMBASE:
3977 if (!addresses [ins->dreg])
3978 addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
3979 src = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_basereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (LLVMInt8Type (), 0));
3980 dst = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
3983 if (!addresses [ins->sreg1])
3984 addresses [ins->sreg1] = build_alloca (ctx, &klass->byval_arg);
3985 if (!addresses [ins->dreg])
3986 addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
3987 src = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMInt8Type (), 0), "");
3988 dst = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
3991 g_assert_not_reached ();
3993 CHECK_FAILURE (ctx);
4000 args [2] = LLVMConstInt (LLVMInt32Type (), mono_class_value_size (klass, NULL), FALSE);
4001 args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
4003 args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
4004 args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
4005 LLVMBuildCall (builder, LLVMGetNamedFunction (module, memcpy_func_name), args, memcpy_param_count, "");
4008 case OP_LLVM_OUTARG_VT:
4009 if (!addresses [ins->sreg1]) {
4010 addresses [ins->sreg1] = build_alloca (ctx, &ins->klass->byval_arg);
4011 g_assert (values [ins->sreg1]);
4012 LLVMBuildStore (builder, values [ins->sreg1], addresses [ins->sreg1]);
4014 addresses [ins->dreg] = addresses [ins->sreg1];
4020 #if defined(TARGET_X86) || defined(TARGET_AMD64)
4022 values [ins->dreg] = LLVMConstNull (type_to_llvm_type (ctx, &ins->klass->byval_arg));
4025 case OP_LOADX_MEMBASE: {
4026 LLVMTypeRef t = type_to_llvm_type (ctx, &ins->klass->byval_arg);
4029 src = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_basereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (t, 0));
4030 values [ins->dreg] = mono_llvm_build_aligned_load (builder, src, "", FALSE, 1);
4033 case OP_STOREX_MEMBASE: {
4034 LLVMTypeRef t = LLVMTypeOf (values [ins->sreg1]);
4037 dest = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_destbasereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (t, 0));
4038 mono_llvm_build_aligned_store (builder, values [ins->sreg1], dest, FALSE, 1);
4045 values [ins->dreg] = LLVMBuildAdd (builder, lhs, rhs, "");
4049 values [ins->dreg] = LLVMBuildFAdd (builder, lhs, rhs, "");
4055 values [ins->dreg] = LLVMBuildSub (builder, lhs, rhs, "");
4059 values [ins->dreg] = LLVMBuildFSub (builder, lhs, rhs, "");
4063 values [ins->dreg] = LLVMBuildFMul (builder, lhs, rhs, "");
4067 values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, "");
4070 values [ins->dreg] = LLVMBuildAnd (builder, lhs, rhs, "");
4073 values [ins->dreg] = LLVMBuildOr (builder, lhs, rhs, "");
4076 values [ins->dreg] = LLVMBuildXor (builder, lhs, rhs, "");
4080 values [ins->dreg] = LLVMBuildMul (builder, lhs, rhs, "");
4091 LLVMValueRef v = NULL;
4093 switch (ins->opcode) {
4098 t = LLVMVectorType (LLVMInt32Type (), 4);
4099 rt = LLVMVectorType (LLVMFloatType (), 4);
4105 t = LLVMVectorType (LLVMInt64Type (), 2);
4106 rt = LLVMVectorType (LLVMDoubleType (), 2);
4109 t = LLVMInt32Type ();
4110 rt = LLVMInt32Type ();
4111 g_assert_not_reached ();
4114 lhs = LLVMBuildBitCast (builder, lhs, t, "");
4115 rhs = LLVMBuildBitCast (builder, rhs, t, "");
4116 switch (ins->opcode) {
4119 v = LLVMBuildAnd (builder, lhs, rhs, "");
4123 v = LLVMBuildOr (builder, lhs, rhs, "");
4127 v = LLVMBuildXor (builder, lhs, rhs, "");
4131 v = LLVMBuildAnd (builder, rhs, LLVMBuildNot (builder, lhs, ""), "");
4134 values [ins->dreg] = LLVMBuildBitCast (builder, v, rt, "");
4158 case OP_PADDB_SAT_UN:
4159 case OP_PADDW_SAT_UN:
4160 case OP_PSUBB_SAT_UN:
4161 case OP_PSUBW_SAT_UN:
4169 case OP_PMULW_HIGH_UN: {
4170 LLVMValueRef args [2];
4175 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, simd_op_to_intrins (ins->opcode)), args, 2, dname);
4182 values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildICmp (builder, LLVMIntEQ, lhs, rhs, ""), LLVMTypeOf (lhs), "");
4186 values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildICmp (builder, LLVMIntSGT, lhs, rhs, ""), LLVMTypeOf (lhs), "");
4194 case OP_EXTRACTX_U2:
4196 case OP_EXTRACT_U1: {
4198 gboolean zext = FALSE;
4200 t = simd_op_to_llvm_type (ins->opcode);
4202 switch (ins->opcode) {
4210 case OP_EXTRACTX_U2:
4215 t = LLVMInt32Type ();
4216 g_assert_not_reached ();
4219 lhs = LLVMBuildBitCast (builder, lhs, t, "");
4220 values [ins->dreg] = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), "");
4222 values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), "");
4231 case OP_EXPAND_R8: {
4232 LLVMTypeRef t = simd_op_to_llvm_type (ins->opcode);
4233 LLVMValueRef mask [16], v;
4236 for (i = 0; i < 16; ++i)
4237 mask [i] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
4239 v = convert (ctx, values [ins->sreg1], LLVMGetElementType (t));
4241 values [ins->dreg] = LLVMBuildInsertElement (builder, LLVMConstNull (t), v, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
4242 values [ins->dreg] = LLVMBuildShuffleVector (builder, values [ins->dreg], LLVMGetUndef (t), LLVMConstVector (mask, LLVMGetVectorSize (t)), "");
4247 values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt8Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
4250 values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt16Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
4253 values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt32Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
4256 values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt64Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
4259 values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMFloatType ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
4262 values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMDoubleType ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
4273 case OP_EXTRACT_MASK:
4280 v = convert (ctx, values [ins->sreg1], simd_op_to_llvm_type (ins->opcode));
4282 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, simd_op_to_intrins (ins->opcode)), &v, 1, dname);
4288 LLVMValueRef args [3];
4292 args [2] = LLVMConstInt (LLVMInt8Type (), ins->inst_c0, FALSE);
4294 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, simd_op_to_intrins (ins->opcode)), args, 3, dname);
4299 /* This is only used for implementing shifts by non-immediate */
4300 values [ins->dreg] = lhs;
4311 LLVMValueRef args [3];
4314 args [1] = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
4316 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, simd_op_to_intrins (ins->opcode)), args, 2, dname);
4327 case OP_PSHLQ_REG: {
4328 LLVMValueRef args [3];
4331 args [1] = values [ins->sreg2];
4333 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, simd_op_to_intrins (ins->opcode)), args, 2, dname);
4340 case OP_PSHUFLEW_LOW:
4341 case OP_PSHUFLEW_HIGH: {
4343 LLVMValueRef v1 = NULL, v2 = NULL, mask_values [16];
4344 int i, mask_size = 0;
4345 int imask = ins->inst_c0;
4347 /* Convert the x86 shuffle mask to LLVM's */
4348 switch (ins->opcode) {
4351 mask [0] = ((imask >> 0) & 3);
4352 mask [1] = ((imask >> 2) & 3);
4353 mask [2] = ((imask >> 4) & 3) + 4;
4354 mask [3] = ((imask >> 6) & 3) + 4;
4355 v1 = values [ins->sreg1];
4356 v2 = values [ins->sreg2];
4360 mask [0] = ((imask >> 0) & 1);
4361 mask [1] = ((imask >> 1) & 1) + 2;
4362 v1 = values [ins->sreg1];
4363 v2 = values [ins->sreg2];
4365 case OP_PSHUFLEW_LOW:
4367 mask [0] = ((imask >> 0) & 3);
4368 mask [1] = ((imask >> 2) & 3);
4369 mask [2] = ((imask >> 4) & 3);
4370 mask [3] = ((imask >> 6) & 3);
4375 v1 = values [ins->sreg1];
4376 v2 = LLVMGetUndef (LLVMTypeOf (v1));
4378 case OP_PSHUFLEW_HIGH:
4384 mask [4] = 4 + ((imask >> 0) & 3);
4385 mask [5] = 4 + ((imask >> 2) & 3);
4386 mask [6] = 4 + ((imask >> 4) & 3);
4387 mask [7] = 4 + ((imask >> 6) & 3);
4388 v1 = values [ins->sreg1];
4389 v2 = LLVMGetUndef (LLVMTypeOf (v1));
4393 mask [0] = ((imask >> 0) & 3);
4394 mask [1] = ((imask >> 2) & 3);
4395 mask [2] = ((imask >> 4) & 3);
4396 mask [3] = ((imask >> 6) & 3);
4397 v1 = values [ins->sreg1];
4398 v2 = LLVMGetUndef (LLVMTypeOf (v1));
4401 g_assert_not_reached ();
4403 for (i = 0; i < mask_size; ++i)
4404 mask_values [i] = LLVMConstInt (LLVMInt32Type (), mask [i], FALSE);
4406 values [ins->dreg] =
4407 LLVMBuildShuffleVector (builder, v1, v2,
4408 LLVMConstVector (mask_values, mask_size), dname);
4412 case OP_UNPACK_LOWB:
4413 case OP_UNPACK_LOWW:
4414 case OP_UNPACK_LOWD:
4415 case OP_UNPACK_LOWQ:
4416 case OP_UNPACK_LOWPS:
4417 case OP_UNPACK_LOWPD:
4418 case OP_UNPACK_HIGHB:
4419 case OP_UNPACK_HIGHW:
4420 case OP_UNPACK_HIGHD:
4421 case OP_UNPACK_HIGHQ:
4422 case OP_UNPACK_HIGHPS:
4423 case OP_UNPACK_HIGHPD: {
4425 LLVMValueRef mask_values [16];
4426 int i, mask_size = 0;
4427 gboolean low = FALSE;
4429 switch (ins->opcode) {
4430 case OP_UNPACK_LOWB:
4434 case OP_UNPACK_LOWW:
4438 case OP_UNPACK_LOWD:
4439 case OP_UNPACK_LOWPS:
4443 case OP_UNPACK_LOWQ:
4444 case OP_UNPACK_LOWPD:
4448 case OP_UNPACK_HIGHB:
4451 case OP_UNPACK_HIGHW:
4454 case OP_UNPACK_HIGHD:
4455 case OP_UNPACK_HIGHPS:
4458 case OP_UNPACK_HIGHQ:
4459 case OP_UNPACK_HIGHPD:
4463 g_assert_not_reached ();
4467 for (i = 0; i < (mask_size / 2); ++i) {
4469 mask [(i * 2) + 1] = mask_size + i;
4472 for (i = 0; i < (mask_size / 2); ++i) {
4473 mask [(i * 2)] = (mask_size / 2) + i;
4474 mask [(i * 2) + 1] = mask_size + (mask_size / 2) + i;
4478 for (i = 0; i < mask_size; ++i)
4479 mask_values [i] = LLVMConstInt (LLVMInt32Type (), mask [i], FALSE);
4481 values [ins->dreg] =
4482 LLVMBuildShuffleVector (builder, values [ins->sreg1], values [ins->sreg2],
4483 LLVMConstVector (mask_values, mask_size), dname);
4488 LLVMTypeRef t = simd_op_to_llvm_type (ins->opcode);
4489 LLVMValueRef v, val;
4491 v = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
4492 val = LLVMConstNull (t);
4493 val = LLVMBuildInsertElement (builder, val, v, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
4494 val = LLVMBuildInsertElement (builder, val, v, LLVMConstInt (LLVMInt32Type (), 1, FALSE), dname);
4496 values [ins->dreg] = val;
4500 case OP_DUPPS_HIGH: {
4501 LLVMTypeRef t = simd_op_to_llvm_type (ins->opcode);
4502 LLVMValueRef v1, v2, val;
4505 if (ins->opcode == OP_DUPPS_LOW) {
4506 v1 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
4507 v2 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 2, FALSE), "");
4509 v1 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 1, FALSE), "");
4510 v2 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 3, FALSE), "");
4512 val = LLVMConstNull (t);
4513 val = LLVMBuildInsertElement (builder, val, v1, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
4514 val = LLVMBuildInsertElement (builder, val, v1, LLVMConstInt (LLVMInt32Type (), 1, FALSE), "");
4515 val = LLVMBuildInsertElement (builder, val, v2, LLVMConstInt (LLVMInt32Type (), 2, FALSE), "");
4516 val = LLVMBuildInsertElement (builder, val, v2, LLVMConstInt (LLVMInt32Type (), 3, FALSE), "");
4518 values [ins->dreg] = val;
4528 * EXCEPTION HANDLING
4530 case OP_IMPLICIT_EXCEPTION:
4531 /* This marks a place where an implicit exception can happen */
4532 if (bb->region != -1)
4533 LLVM_FAILURE (ctx, "implicit-exception");
4537 MonoMethodSignature *throw_sig;
4538 LLVMValueRef callee, arg;
4539 gboolean rethrow = (ins->opcode == OP_RETHROW);
4540 const char *icall_name;
4542 callee = rethrow ? ctx->lmodule->rethrow : ctx->lmodule->throw;
4543 icall_name = rethrow ? "mono_arch_rethrow_exception" : "mono_arch_throw_exception";
4546 throw_sig = mono_metadata_signature_alloc (mono_get_corlib (), 1);
4547 throw_sig->ret = &mono_get_void_class ()->byval_arg;
4548 throw_sig->params [0] = &mono_get_object_class ()->byval_arg;
4549 if (cfg->compile_aot) {
4550 callee = get_plt_entry (ctx, sig_to_llvm_sig (ctx, throw_sig), MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
4552 callee = LLVMAddFunction (module, icall_name, sig_to_llvm_sig (ctx, throw_sig));
4556 * LLVM doesn't push the exception argument, so we need a different
4559 LLVMAddGlobalMapping (ctx->lmodule->ee, callee, resolve_patch (cfg, MONO_PATCH_INFO_INTERNAL_METHOD, rethrow ? "llvm_rethrow_exception_trampoline" : "llvm_throw_exception_trampoline"));
4561 LLVMAddGlobalMapping (ctx->lmodule->ee, callee, resolve_patch (cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
4565 mono_memory_barrier ();
4567 ctx->lmodule->rethrow = callee;
4569 ctx->lmodule->throw = callee;
4571 arg = convert (ctx, lhs, type_to_llvm_type (ctx, &mono_get_object_class ()->byval_arg));
4572 emit_call (ctx, bb, &builder, callee, &arg, 1);
4575 case OP_CALL_HANDLER: {
4577 * We don't 'call' handlers, but instead simply branch to them.
4578 * The code generated by ENDFINALLY will branch back to us.
4580 LLVMBasicBlockRef noex_bb;
4582 BBInfo *info = &bblocks [ins->inst_target_bb->block_num];
4584 bb_list = info->call_handler_return_bbs;
4587 * Set the indicator variable for the finally clause.
4589 lhs = info->finally_ind;
4591 LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), g_slist_length (bb_list) + 1, FALSE), lhs);
4593 /* Branch to the finally clause */
4594 LLVMBuildBr (builder, info->call_handler_target_bb);
4596 noex_bb = gen_bb (ctx, "CALL_HANDLER_CONT_BB");
4597 info->call_handler_return_bbs = g_slist_append_mempool (cfg->mempool, info->call_handler_return_bbs, noex_bb);
4599 builder = ctx->builder = create_builder (ctx);
4600 LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
4602 bblocks [bb->block_num].end_bblock = noex_bb;
4605 case OP_START_HANDLER: {
4608 case OP_ENDFINALLY: {
4609 LLVMBasicBlockRef resume_bb;
4610 MonoBasicBlock *handler_bb;
4611 LLVMValueRef val, switch_ins, callee;
4615 handler_bb = g_hash_table_lookup (ctx->region_to_handler, GUINT_TO_POINTER (mono_get_block_region_notry (cfg, bb->region)));
4616 g_assert (handler_bb);
4617 info = &bblocks [handler_bb->block_num];
4618 lhs = info->finally_ind;
4621 bb_list = info->call_handler_return_bbs;
4623 resume_bb = gen_bb (ctx, "ENDFINALLY_RESUME_BB");
4625 /* Load the finally variable */
4626 val = LLVMBuildLoad (builder, lhs, "");
4628 /* Reset the variable */
4629 LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), lhs);
4631 /* Branch to either resume_bb, or to the bblocks in bb_list */
4632 switch_ins = LLVMBuildSwitch (builder, val, resume_bb, g_slist_length (bb_list));
4634 * The other targets are added at the end to handle OP_CALL_HANDLER
4635 * opcodes processed later.
4637 info->endfinally_switch_ins_list = g_slist_append_mempool (cfg->mempool, info->endfinally_switch_ins_list, switch_ins);
4639 builder = ctx->builder = create_builder (ctx);
4640 LLVMPositionBuilderAtEnd (ctx->builder, resume_bb);
4642 if (ctx->cfg->compile_aot) {
4643 callee = get_plt_entry (ctx, LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE), MONO_PATCH_INFO_INTERNAL_METHOD, "llvm_resume_unwind_trampoline");
4645 callee = LLVMGetNamedFunction (module, "llvm_resume_unwind_trampoline");
4647 LLVMBuildCall (builder, callee, NULL, 0, "");
4649 LLVMBuildUnreachable (builder);
4650 has_terminator = TRUE;
4656 sprintf (reason, "opcode %s", mono_inst_name (ins->opcode));
4657 LLVM_FAILURE (ctx, reason);
4662 /* Convert the value to the type required by phi nodes */
4663 if (spec [MONO_INST_DEST] != ' ' && !MONO_IS_STORE_MEMBASE (ins) && ctx->vreg_types [ins->dreg]) {
4664 if (!values [ins->dreg])
4666 values [ins->dreg] = addresses [ins->dreg];
4668 values [ins->dreg] = convert (ctx, values [ins->dreg], ctx->vreg_types [ins->dreg]);
4671 /* Add stores for volatile variables */
4672 if (spec [MONO_INST_DEST] != ' ' && spec [MONO_INST_DEST] != 'v' && !MONO_IS_STORE_MEMBASE (ins))
4673 emit_volatile_store (ctx, ins->dreg);
4676 if (!has_terminator && bb->next_bb && (bb == cfg->bb_entry || bb->in_count > 0))
4677 LLVMBuildBr (builder, get_bb (ctx, bb->next_bb));
4679 if (bb == cfg->bb_exit && sig->ret->type == MONO_TYPE_VOID) {
4680 emit_dbg_loc (ctx, builder, cfg->header->code + cfg->header->code_size - 1);
4681 LLVMBuildRetVoid (builder);
4684 if (bb == cfg->bb_entry)
4685 ctx->last_alloca = LLVMGetLastInstruction (get_bb (ctx, cfg->bb_entry));
4694 * mono_llvm_check_method_supported:
4696 * Do some quick checks to decide whenever cfg->method can be compiled by LLVM, to avoid
4697 * compiling a method twice.
4700 mono_llvm_check_method_supported (MonoCompile *cfg)
4702 MonoMethodHeader *header = cfg->header;
4703 MonoExceptionClause *clause;
4706 if (cfg->method->save_lmf) {
4707 cfg->exception_message = g_strdup ("lmf");
4708 cfg->disable_llvm = TRUE;
4710 if (cfg->disable_llvm)
4714 for (i = 0; i < header->num_clauses; ++i) {
4715 clause = &header->clauses [i];
4717 if (i > 0 && clause->try_offset <= header->clauses [i - 1].handler_offset + header->clauses [i - 1].handler_len) {
4719 * FIXME: Some tests still fail with nested clauses.
4721 cfg->exception_message = g_strdup ("nested clauses");
4722 cfg->disable_llvm = TRUE;
4726 if (cfg->disable_llvm)
4731 if (cfg->method->dynamic) {
4732 cfg->exception_message = g_strdup ("dynamic.");
4733 cfg->disable_llvm = TRUE;
4735 if (cfg->disable_llvm)
4740 * mono_llvm_emit_method:
4742 * Emit LLVM IL from the mono IL, and compile it to native code using LLVM.
4745 mono_llvm_emit_method (MonoCompile *cfg)
4748 MonoMethodSignature *sig;
4750 LLVMTypeRef method_type;
4751 LLVMValueRef method = NULL;
4753 LLVMValueRef *values;
4754 int i, max_block_num, bb_index;
4755 gboolean last = FALSE;
4756 GPtrArray *phi_values;
4757 LLVMCallInfo *linfo;
4759 LLVMModuleRef module;
4761 GPtrArray *bblock_list;
4762 MonoMethodHeader *header;
4763 MonoExceptionClause *clause;
4767 /* The code below might acquire the loader lock, so use it for global locking */
4768 mono_loader_lock ();
4770 /* Used to communicate with the callbacks */
4771 mono_native_tls_set_value (current_cfg_tls_id, cfg);
4773 ctx = g_new0 (EmitContext, 1);
4775 ctx->mempool = cfg->mempool;
4778 * This maps vregs to the LLVM instruction defining them
4780 values = g_new0 (LLVMValueRef, cfg->next_vreg);
4782 * This maps vregs for volatile variables to the LLVM instruction defining their
4785 ctx->addresses = g_new0 (LLVMValueRef, cfg->next_vreg);
4786 ctx->vreg_types = g_new0 (LLVMTypeRef, cfg->next_vreg);
4787 ctx->vreg_cli_types = g_new0 (MonoType*, cfg->next_vreg);
4788 phi_values = g_ptr_array_sized_new (256);
4790 * This signals whenever the vreg was defined by a phi node with no input vars
4791 * (i.e. all its input bblocks end with NOT_REACHABLE).
4793 ctx->is_dead = g_new0 (gboolean, cfg->next_vreg);
4794 /* Whenever the bblock is unreachable */
4795 ctx->unreachable = g_new0 (gboolean, cfg->max_block_num);
4797 bblock_list = g_ptr_array_sized_new (256);
4799 ctx->values = values;
4800 ctx->region_to_handler = g_hash_table_new (NULL, NULL);
4802 if (cfg->compile_aot) {
4803 ctx->lmodule = &aot_module;
4804 method_name = mono_aot_get_method_name (cfg);
4805 cfg->llvm_method_name = g_strdup (method_name);
4807 init_jit_module (cfg->domain);
4808 ctx->lmodule = domain_jit_info (cfg->domain)->llvm_module;
4809 method_name = mono_method_full_name (cfg->method, TRUE);
4812 module = ctx->module = ctx->lmodule->module;
4815 LLVM_FAILURE (ctx, "gsharedvt");
4819 static int count = 0;
4822 if (g_getenv ("LLVM_COUNT")) {
4823 if (count == atoi (g_getenv ("LLVM_COUNT"))) {
4824 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
4828 if (count > atoi (g_getenv ("LLVM_COUNT")))
4829 LLVM_FAILURE (ctx, "");
4834 sig = mono_method_signature (cfg->method);
4837 linfo = mono_arch_get_llvm_call_info (cfg, sig);
4839 CHECK_FAILURE (ctx);
4842 linfo->rgctx_arg = TRUE;
4843 method_type = sig_to_llvm_sig_full (ctx, sig, linfo, &sinfo);
4844 CHECK_FAILURE (ctx);
4847 * This maps parameter indexes in the original signature to the indexes in
4848 * the LLVM signature.
4850 ctx->pindexes = sinfo.pindexes;
4852 method = LLVMAddFunction (module, method_name, method_type);
4853 ctx->lmethod = method;
4855 LLVMSetFunctionCallConv (method, LLVMMono1CallConv);
4856 LLVMSetLinkage (method, LLVMPrivateLinkage);
4858 LLVMAddFunctionAttr (method, LLVMUWTable);
4860 if (cfg->compile_aot) {
4861 LLVMSetLinkage (method, LLVMInternalLinkage);
4862 if (ctx->lmodule->external_symbols) {
4863 LLVMSetLinkage (method, LLVMExternalLinkage);
4864 LLVMSetVisibility (method, LLVMHiddenVisibility);
4867 LLVMSetLinkage (method, LLVMPrivateLinkage);
4870 if (cfg->method->save_lmf)
4871 LLVM_FAILURE (ctx, "lmf");
4873 if (sig->pinvoke && cfg->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE)
4874 LLVM_FAILURE (ctx, "pinvoke signature");
4876 header = cfg->header;
4877 for (i = 0; i < header->num_clauses; ++i) {
4878 clause = &header->clauses [i];
4879 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_NONE)
4880 LLVM_FAILURE (ctx, "non-finally/catch clause.");
4882 if (header->num_clauses || (cfg->method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING))
4883 /* We can't handle inlined methods with clauses */
4884 LLVMAddFunctionAttr (method, LLVMNoInlineAttribute);
4886 if (linfo->rgctx_arg) {
4887 ctx->rgctx_arg = LLVMGetParam (method, sinfo.rgctx_arg_pindex);
4889 * We mark the rgctx parameter with the inreg attribute, which is mapped to
4890 * MONO_ARCH_RGCTX_REG in the Mono calling convention in llvm, i.e.
4891 * CC_X86_64_Mono in X86CallingConv.td.
4893 LLVMAddAttribute (ctx->rgctx_arg, LLVMInRegAttribute);
4894 LLVMSetValueName (ctx->rgctx_arg, "rgctx");
4896 if (cfg->vret_addr) {
4897 values [cfg->vret_addr->dreg] = LLVMGetParam (method, sinfo.vret_arg_pindex);
4898 LLVMSetValueName (values [cfg->vret_addr->dreg], "vret");
4901 values [cfg->args [0]->dreg] = LLVMGetParam (method, sinfo.this_arg_pindex);
4902 LLVMSetValueName (values [cfg->args [0]->dreg], "this");
4905 names = g_new (char *, sig->param_count);
4906 mono_method_get_param_names (cfg->method, (const char **) names);
4908 for (i = 0; i < sig->param_count; ++i) {
4911 values [cfg->args [i + sig->hasthis]->dreg] = LLVMGetParam (method, sinfo.pindexes [i]);
4912 if (names [i] && names [i][0] != '\0')
4913 name = g_strdup_printf ("arg_%s", names [i]);
4915 name = g_strdup_printf ("arg_%d", i);
4916 LLVMSetValueName (values [cfg->args [i + sig->hasthis]->dreg], name);
4918 if (linfo->args [i + sig->hasthis].storage == LLVMArgVtypeByVal)
4919 LLVMAddAttribute (LLVMGetParam (method, sinfo.pindexes [i]), LLVMByValAttribute);
4923 if (ctx->lmodule->emit_dwarf && cfg->compile_aot && mono_debug_enabled ()) {
4924 ctx->minfo = mono_debug_lookup_method (cfg->method);
4925 ctx->dbg_md = emit_dbg_subprogram (ctx, cfg, method, method_name);
4929 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
4930 max_block_num = MAX (max_block_num, bb->block_num);
4931 ctx->bblocks = bblocks = g_new0 (BBInfo, max_block_num + 1);
4933 /* Add branches between non-consecutive bblocks */
4934 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4935 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
4936 bb->next_bb != bb->last_ins->inst_false_bb) {
4938 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
4939 inst->opcode = OP_BR;
4940 inst->inst_target_bb = bb->last_ins->inst_false_bb;
4941 mono_bblock_add_inst (bb, inst);
4946 * The INDIRECT flag added by OP_LDADDR inhibits optimizations, even if the LDADDR
4947 * was later optimized away, so clear these flags, and add them back for the still
4948 * present OP_LDADDR instructions.
4950 for (i = 0; i < cfg->next_vreg; ++i) {
4953 ins = get_vreg_to_inst (cfg, i);
4954 if (ins && ins != cfg->rgctx_var)
4955 ins->flags &= ~MONO_INST_INDIRECT;
4959 * Make a first pass over the code to precreate PHI nodes/set INDIRECT flags.
4961 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4963 LLVMBuilderRef builder;
4965 char dname_buf[128];
4967 builder = create_builder (ctx);
4969 for (ins = bb->code; ins; ins = ins->next) {
4970 switch (ins->opcode) {
4975 LLVMTypeRef phi_type = llvm_type_to_stack_type (cfg, type_to_llvm_type (ctx, &ins->klass->byval_arg));
4977 CHECK_FAILURE (ctx);
4979 if (ins->opcode == OP_VPHI) {
4980 /* Treat valuetype PHI nodes as operating on the address itself */
4981 g_assert (ins->klass);
4982 phi_type = LLVMPointerType (type_to_llvm_type (ctx, &ins->klass->byval_arg), 0);
4986 * Have to precreate these, as they can be referenced by
4987 * earlier instructions.
4989 sprintf (dname_buf, "t%d", ins->dreg);
4991 values [ins->dreg] = LLVMBuildPhi (builder, phi_type, dname);
4993 if (ins->opcode == OP_VPHI)
4994 ctx->addresses [ins->dreg] = values [ins->dreg];
4996 g_ptr_array_add (phi_values, values [ins->dreg]);
4999 * Set the expected type of the incoming arguments since these have
5000 * to have the same type.
5002 for (i = 0; i < ins->inst_phi_args [0]; i++) {
5003 int sreg1 = ins->inst_phi_args [i + 1];
5006 ctx->vreg_types [sreg1] = phi_type;
5011 ((MonoInst*)ins->inst_p0)->flags |= MONO_INST_INDIRECT;
5020 * Create an ordering for bblocks, use the depth first order first, then
5021 * put the exception handling bblocks last.
5023 for (bb_index = 0; bb_index < cfg->num_bblocks; ++bb_index) {
5024 bb = cfg->bblocks [bb_index];
5025 if (!(bb->region != -1 && !MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))) {
5026 g_ptr_array_add (bblock_list, bb);
5027 bblocks [bb->block_num].added = TRUE;
5031 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5032 if (!bblocks [bb->block_num].added)
5033 g_ptr_array_add (bblock_list, bb);
5037 * Second pass: generate code.
5039 for (bb_index = 0; bb_index < bblock_list->len; ++bb_index) {
5040 bb = g_ptr_array_index (bblock_list, bb_index);
5042 if (!(bb == cfg->bb_entry || bb->in_count > 0))
5045 process_bb (ctx, bb);
5046 CHECK_FAILURE (ctx);
5049 /* Add incoming phi values */
5050 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5051 GSList *l, *ins_list;
5053 ins_list = bblocks [bb->block_num].phi_nodes;
5055 for (l = ins_list; l; l = l->next) {
5056 PhiNode *node = l->data;
5057 MonoInst *phi = node->phi;
5058 int sreg1 = node->sreg;
5059 LLVMBasicBlockRef in_bb;
5064 in_bb = get_end_bb (ctx, node->in_bb);
5066 if (ctx->unreachable [node->in_bb->block_num])
5069 if (!values [sreg1])
5070 /* Can happen with values in EH clauses */
5071 LLVM_FAILURE (ctx, "incoming phi sreg1");
5073 if (phi->opcode == OP_VPHI) {
5074 g_assert (LLVMTypeOf (ctx->addresses [sreg1]) == LLVMTypeOf (values [phi->dreg]));
5075 LLVMAddIncoming (values [phi->dreg], &ctx->addresses [sreg1], &in_bb, 1);
5077 if (LLVMTypeOf (values [sreg1]) != LLVMTypeOf (values [phi->dreg]))
5079 LLVM_FAILURE (ctx, "incoming phi arg type mismatch");
5080 g_assert (LLVMTypeOf (values [sreg1]) == LLVMTypeOf (values [phi->dreg]));
5081 LLVMAddIncoming (values [phi->dreg], &values [sreg1], &in_bb, 1);
5086 /* Create the SWITCH statements for ENDFINALLY instructions */
5087 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5088 BBInfo *info = &bblocks [bb->block_num];
5090 for (l = info->endfinally_switch_ins_list; l; l = l->next) {
5091 LLVMValueRef switch_ins = l->data;
5092 GSList *bb_list = info->call_handler_return_bbs;
5094 for (i = 0; i < g_slist_length (bb_list); ++i)
5095 LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), i + 1, FALSE), g_slist_nth (bb_list, i)->data);
5099 if (cfg->verbose_level > 1)
5100 mono_llvm_dump_value (method);
5102 if (cfg->compile_aot)
5103 mark_as_used (ctx->lmodule, method);
5105 if (cfg->compile_aot) {
5106 LLVMValueRef md_args [16];
5107 LLVMValueRef md_node;
5110 method_index = mono_aot_get_method_index (cfg->orig_method);
5111 md_args [0] = LLVMMDString (method_name, strlen (method_name));
5112 md_args [1] = LLVMConstInt (LLVMInt32Type (), method_index, FALSE);
5113 md_node = LLVMMDNode (md_args, 2);
5114 LLVMAddNamedMetadataOperand (module, "mono.function_indexes", md_node);
5115 //LLVMSetMetadata (method, md_kind, LLVMMDNode (&md_arg, 1));
5118 if (cfg->compile_aot) {
5119 /* Don't generate native code, keep the LLVM IR */
5120 if (cfg->compile_aot && cfg->verbose_level)
5121 printf ("%s emitted as %s\n", mono_method_full_name (cfg->method, TRUE), method_name);
5123 //LLVMVerifyFunction(method, 0);
5125 mono_llvm_optimize_method (ctx->lmodule->mono_ee, method);
5127 if (cfg->verbose_level > 1)
5128 mono_llvm_dump_value (method);
5130 cfg->native_code = LLVMGetPointerToGlobal (ctx->lmodule->ee, method);
5132 /* Set by emit_cb */
5133 g_assert (cfg->code_len);
5135 /* FIXME: Free the LLVM IL for the function */
5138 if (ctx->lmodule->method_to_lmethod)
5139 g_hash_table_insert (ctx->lmodule->method_to_lmethod, cfg->method, method);
5146 /* Need to add unused phi nodes as they can be referenced by other values */
5147 LLVMBasicBlockRef phi_bb = LLVMAppendBasicBlock (method, "PHI_BB");
5148 LLVMBuilderRef builder;
5150 builder = create_builder (ctx);
5151 LLVMPositionBuilderAtEnd (builder, phi_bb);
5153 for (i = 0; i < phi_values->len; ++i) {
5154 LLVMValueRef v = g_ptr_array_index (phi_values, i);
5155 if (LLVMGetInstructionParent (v) == NULL)
5156 LLVMInsertIntoBuilder (builder, v);
5159 LLVMDeleteFunction (method);
5164 g_free (ctx->addresses);
5165 g_free (ctx->vreg_types);
5166 g_free (ctx->vreg_cli_types);
5167 g_free (ctx->pindexes);
5168 g_free (ctx->is_dead);
5169 g_free (ctx->unreachable);
5170 g_ptr_array_free (phi_values, TRUE);
5171 g_free (ctx->bblocks);
5172 g_hash_table_destroy (ctx->region_to_handler);
5173 g_free (method_name);
5174 g_ptr_array_free (bblock_list, TRUE);
5176 for (l = ctx->builders; l; l = l->next) {
5177 LLVMBuilderRef builder = l->data;
5178 LLVMDisposeBuilder (builder);
5183 mono_native_tls_set_value (current_cfg_tls_id, NULL);
5185 mono_loader_unlock ();
5189 * mono_llvm_emit_call:
5191 * Same as mono_arch_emit_call () for LLVM.
5194 mono_llvm_emit_call (MonoCompile *cfg, MonoCallInst *call)
5197 MonoMethodSignature *sig;
5198 int i, n, stack_size;
5203 sig = call->signature;
5204 n = sig->param_count + sig->hasthis;
5206 call->cinfo = mono_arch_get_llvm_call_info (cfg, sig);
5208 if (cfg->disable_llvm)
5211 if (sig->call_convention == MONO_CALL_VARARG) {
5212 cfg->exception_message = g_strdup ("varargs");
5213 cfg->disable_llvm = TRUE;
5216 for (i = 0; i < n; ++i) {
5219 ainfo = call->cinfo->args + i;
5221 in = call->args [i];
5223 /* Simply remember the arguments */
5224 switch (ainfo->storage) {
5226 case LLVMArgInFPReg: {
5227 MonoType *t = (sig->hasthis && i == 0) ? &mono_get_intptr_class ()->byval_arg : sig->params [i - sig->hasthis];
5230 opcode = mono_type_to_regmove (cfg, t);
5231 if (opcode == OP_FMOVE) {
5232 MONO_INST_NEW (cfg, ins, OP_FMOVE);
5233 ins->dreg = mono_alloc_freg (cfg);
5234 } else if (opcode == OP_LMOVE) {
5235 MONO_INST_NEW (cfg, ins, OP_LMOVE);
5236 ins->dreg = mono_alloc_lreg (cfg);
5238 MONO_INST_NEW (cfg, ins, OP_MOVE);
5239 ins->dreg = mono_alloc_ireg (cfg);
5241 ins->sreg1 = in->dreg;
5244 case LLVMArgVtypeByVal:
5245 case LLVMArgVtypeInReg:
5246 case LLVMArgAsIArgs:
5247 case LLVMArgAsFpArgs:
5248 MONO_INST_NEW (cfg, ins, OP_LLVM_OUTARG_VT);
5249 ins->dreg = mono_alloc_ireg (cfg);
5250 ins->sreg1 = in->dreg;
5251 ins->klass = mono_class_from_mono_type (sig->params [i - sig->hasthis]);
5254 call->cinfo = mono_arch_get_llvm_call_info (cfg, sig);
5255 cfg->exception_message = g_strdup ("ainfo->storage");
5256 cfg->disable_llvm = TRUE;
5260 if (!cfg->disable_llvm) {
5261 MONO_ADD_INS (cfg->cbb, ins);
5262 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, 0, FALSE);
5267 static unsigned char*
5268 alloc_cb (LLVMValueRef function, int size)
5272 cfg = mono_native_tls_get_value (current_cfg_tls_id);
5276 return mono_domain_code_reserve (cfg->domain, size);
5278 return mono_domain_code_reserve (mono_domain_get (), size);
5283 emitted_cb (LLVMValueRef function, void *start, void *end)
5287 cfg = mono_native_tls_get_value (current_cfg_tls_id);
5289 cfg->code_len = (guint8*)end - (guint8*)start;
5293 exception_cb (void *data)
5296 MonoJitExceptionInfo *ei;
5297 guint32 ei_len, i, j, nested_len, nindex;
5298 gpointer *type_info;
5299 int this_reg, this_offset;
5301 cfg = mono_native_tls_get_value (current_cfg_tls_id);
5305 * data points to a DWARF FDE structure, convert it to our unwind format and
5307 * An alternative would be to save it directly, and modify our unwinder to work
5310 cfg->encoded_unwind_ops = mono_unwind_decode_fde ((guint8*)data, &cfg->encoded_unwind_ops_len, NULL, &ei, &ei_len, &type_info, &this_reg, &this_offset);
5311 if (cfg->verbose_level > 1)
5312 mono_print_unwind_info (cfg->encoded_unwind_ops, cfg->encoded_unwind_ops_len);
5314 /* Count nested clauses */
5316 for (i = 0; i < ei_len; ++i) {
5317 for (j = 0; j < ei_len; ++j) {
5318 gint32 cindex1 = *(gint32*)type_info [i];
5319 MonoExceptionClause *clause1 = &cfg->header->clauses [cindex1];
5320 gint32 cindex2 = *(gint32*)type_info [j];
5321 MonoExceptionClause *clause2 = &cfg->header->clauses [cindex2];
5323 if (cindex1 != cindex2 && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset) {
5329 cfg->llvm_ex_info = mono_mempool_alloc0 (cfg->mempool, (ei_len + nested_len) * sizeof (MonoJitExceptionInfo));
5330 cfg->llvm_ex_info_len = ei_len + nested_len;
5331 memcpy (cfg->llvm_ex_info, ei, ei_len * sizeof (MonoJitExceptionInfo));
5332 /* Fill the rest of the information from the type info */
5333 for (i = 0; i < ei_len; ++i) {
5334 gint32 clause_index = *(gint32*)type_info [i];
5335 MonoExceptionClause *clause = &cfg->header->clauses [clause_index];
5337 cfg->llvm_ex_info [i].flags = clause->flags;
5338 cfg->llvm_ex_info [i].data.catch_class = clause->data.catch_class;
5342 * For nested clauses, the LLVM produced exception info associates the try interval with
5343 * the innermost handler, while mono expects it to be associated with all nesting clauses.
5345 /* FIXME: These should be order with the normal clauses */
5347 for (i = 0; i < ei_len; ++i) {
5348 for (j = 0; j < ei_len; ++j) {
5349 gint32 cindex1 = *(gint32*)type_info [i];
5350 MonoExceptionClause *clause1 = &cfg->header->clauses [cindex1];
5351 gint32 cindex2 = *(gint32*)type_info [j];
5352 MonoExceptionClause *clause2 = &cfg->header->clauses [cindex2];
5354 if (cindex1 != cindex2 && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset) {
5356 * The try interval comes from the nested clause, everything else from the
5359 memcpy (&cfg->llvm_ex_info [nindex], &cfg->llvm_ex_info [j], sizeof (MonoJitExceptionInfo));
5360 cfg->llvm_ex_info [nindex].try_start = cfg->llvm_ex_info [i].try_start;
5361 cfg->llvm_ex_info [nindex].try_end = cfg->llvm_ex_info [i].try_end;
5366 g_assert (nindex == ei_len + nested_len);
5367 cfg->llvm_this_reg = this_reg;
5368 cfg->llvm_this_offset = this_offset;
5370 /* type_info [i] is cfg mempool allocated, no need to free it */
5377 dlsym_cb (const char *name, void **symbol)
5383 if (!strcmp (name, "__bzero")) {
5384 *symbol = (void*)bzero;
5386 current = mono_dl_open (NULL, 0, NULL);
5389 err = mono_dl_symbol (current, name, symbol);
5391 mono_dl_close (current);
5393 #ifdef MONO_ARCH_HAVE_CREATE_LLVM_NATIVE_THUNK
5394 *symbol = (char*)mono_arch_create_llvm_native_thunk (mono_domain_get (), (guint8*)(*symbol));
5400 AddFunc (LLVMModuleRef module, const char *name, LLVMTypeRef ret_type, LLVMTypeRef *param_types, int nparams)
5402 LLVMAddFunction (module, name, LLVMFunctionType (ret_type, param_types, nparams, FALSE));
5406 AddFunc2 (LLVMModuleRef module, const char *name, LLVMTypeRef ret_type, LLVMTypeRef param_type1, LLVMTypeRef param_type2)
5408 LLVMTypeRef param_types [4];
5410 param_types [0] = param_type1;
5411 param_types [1] = param_type2;
5413 AddFunc (module, name, ret_type, param_types, 2);
5417 add_intrinsics (LLVMModuleRef module)
5419 /* Emit declarations of instrinsics */
5421 * It would be nicer to emit only the intrinsics actually used, but LLVM's Module
5422 * type doesn't seem to do any locking.
5425 LLVMTypeRef memset_params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMInt8Type (), LLVMInt32Type (), LLVMInt32Type (), LLVMInt1Type () };
5427 memset_param_count = 5;
5428 memset_func_name = "llvm.memset.p0i8.i32";
5430 LLVMAddFunction (module, memset_func_name, LLVMFunctionType (LLVMVoidType (), memset_params, memset_param_count, FALSE));
5434 LLVMTypeRef memcpy_params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMPointerType (LLVMInt8Type (), 0), LLVMInt32Type (), LLVMInt32Type (), LLVMInt1Type () };
5436 memcpy_param_count = 5;
5437 memcpy_func_name = "llvm.memcpy.p0i8.p0i8.i32";
5439 LLVMAddFunction (module, memcpy_func_name, LLVMFunctionType (LLVMVoidType (), memcpy_params, memcpy_param_count, FALSE));
5443 LLVMTypeRef params [] = { LLVMDoubleType () };
5445 LLVMAddFunction (module, "llvm.sin.f64", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
5446 LLVMAddFunction (module, "llvm.cos.f64", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
5447 LLVMAddFunction (module, "llvm.sqrt.f64", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
5449 /* This isn't an intrinsic, instead llvm seems to special case it by name */
5450 LLVMAddFunction (module, "fabs", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
5454 LLVMTypeRef ovf_res_i32 [] = { LLVMInt32Type (), LLVMInt1Type () };
5455 LLVMTypeRef ovf_params_i32 [] = { LLVMInt32Type (), LLVMInt32Type () };
5457 LLVMAddFunction (module, "llvm.sadd.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
5458 LLVMAddFunction (module, "llvm.uadd.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
5459 LLVMAddFunction (module, "llvm.ssub.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
5460 LLVMAddFunction (module, "llvm.usub.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
5461 LLVMAddFunction (module, "llvm.smul.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
5462 LLVMAddFunction (module, "llvm.umul.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
5466 LLVMTypeRef ovf_res_i64 [] = { LLVMInt64Type (), LLVMInt1Type () };
5467 LLVMTypeRef ovf_params_i64 [] = { LLVMInt64Type (), LLVMInt64Type () };
5469 LLVMAddFunction (module, "llvm.sadd.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
5470 LLVMAddFunction (module, "llvm.uadd.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
5471 LLVMAddFunction (module, "llvm.ssub.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
5472 LLVMAddFunction (module, "llvm.usub.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
5473 LLVMAddFunction (module, "llvm.smul.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
5474 LLVMAddFunction (module, "llvm.umul.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
5479 LLVMTypeRef arg_types [2];
5480 LLVMTypeRef ret_type;
5482 arg_types [0] = LLVMPointerType (LLVMInt8Type (), 0);
5483 arg_types [1] = LLVMPointerType (LLVMInt8Type (), 0);
5484 ret_type = LLVMInt32Type ();
5486 LLVMAddFunction (module, "mono_personality", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
5488 LLVMAddFunction (module, "llvm_resume_unwind_trampoline", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
5491 /* SSE intrinsics */
5492 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5494 LLVMTypeRef ret_type, arg_types [16];
5497 ret_type = type_to_simd_type (MONO_TYPE_I4);
5498 arg_types [0] = ret_type;
5499 arg_types [1] = ret_type;
5500 AddFunc (module, "llvm.x86.sse41.pminud", ret_type, arg_types, 2);
5501 AddFunc (module, "llvm.x86.sse41.pmaxud", ret_type, arg_types, 2);
5503 ret_type = type_to_simd_type (MONO_TYPE_I2);
5504 arg_types [0] = ret_type;
5505 arg_types [1] = ret_type;
5506 AddFunc (module, "llvm.x86.sse41.pminuw", ret_type, arg_types, 2);
5507 AddFunc (module, "llvm.x86.sse2.pmins.w", ret_type, arg_types, 2);
5508 AddFunc (module, "llvm.x86.sse41.pmaxuw", ret_type, arg_types, 2);
5509 AddFunc (module, "llvm.x86.sse2.padds.w", ret_type, arg_types, 2);
5510 AddFunc (module, "llvm.x86.sse2.psubs.w", ret_type, arg_types, 2);
5511 AddFunc (module, "llvm.x86.sse2.paddus.w", ret_type, arg_types, 2);
5512 AddFunc (module, "llvm.x86.sse2.psubus.w", ret_type, arg_types, 2);
5513 AddFunc (module, "llvm.x86.sse2.pavg.w", ret_type, arg_types, 2);
5514 AddFunc (module, "llvm.x86.sse2.pmulh.w", ret_type, arg_types, 2);
5515 AddFunc (module, "llvm.x86.sse2.pmulhu.w", ret_type, arg_types, 2);
5517 ret_type = type_to_simd_type (MONO_TYPE_I1);
5518 arg_types [0] = ret_type;
5519 arg_types [1] = ret_type;
5520 AddFunc (module, "llvm.x86.sse2.pminu.b", ret_type, arg_types, 2);
5521 AddFunc (module, "llvm.x86.sse2.pmaxu.b", ret_type, arg_types, 2);
5522 AddFunc (module, "llvm.x86.sse2.padds.b", ret_type, arg_types, 2);
5523 AddFunc (module, "llvm.x86.sse2.psubs.b", ret_type, arg_types, 2);
5524 AddFunc (module, "llvm.x86.sse2.paddus.b", ret_type, arg_types, 2);
5525 AddFunc (module, "llvm.x86.sse2.psubus.b", ret_type, arg_types, 2);
5526 AddFunc (module, "llvm.x86.sse2.pavg.b", ret_type, arg_types, 2);
5528 ret_type = type_to_simd_type (MONO_TYPE_R8);
5529 arg_types [0] = ret_type;
5530 arg_types [1] = ret_type;
5531 AddFunc (module, "llvm.x86.sse2.min.pd", ret_type, arg_types, 2);
5532 AddFunc (module, "llvm.x86.sse2.max.pd", ret_type, arg_types, 2);
5533 AddFunc (module, "llvm.x86.sse3.hadd.pd", ret_type, arg_types, 2);
5534 AddFunc (module, "llvm.x86.sse3.hsub.pd", ret_type, arg_types, 2);
5535 AddFunc (module, "llvm.x86.sse3.addsub.pd", ret_type, arg_types, 2);
5537 ret_type = type_to_simd_type (MONO_TYPE_R4);
5538 arg_types [0] = ret_type;
5539 arg_types [1] = ret_type;
5540 AddFunc (module, "llvm.x86.sse.min.ps", ret_type, arg_types, 2);
5541 AddFunc (module, "llvm.x86.sse.max.ps", ret_type, arg_types, 2);
5542 AddFunc (module, "llvm.x86.sse3.hadd.ps", ret_type, arg_types, 2);
5543 AddFunc (module, "llvm.x86.sse3.hsub.ps", ret_type, arg_types, 2);
5544 AddFunc (module, "llvm.x86.sse3.addsub.ps", ret_type, arg_types, 2);
5547 ret_type = type_to_simd_type (MONO_TYPE_I1);
5548 arg_types [0] = type_to_simd_type (MONO_TYPE_I2);
5549 arg_types [1] = type_to_simd_type (MONO_TYPE_I2);
5550 AddFunc (module, "llvm.x86.sse2.packsswb.128", ret_type, arg_types, 2);
5551 AddFunc (module, "llvm.x86.sse2.packuswb.128", ret_type, arg_types, 2);
5552 ret_type = type_to_simd_type (MONO_TYPE_I2);
5553 arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
5554 arg_types [1] = type_to_simd_type (MONO_TYPE_I4);
5555 AddFunc (module, "llvm.x86.sse2.packssdw.128", ret_type, arg_types, 2);
5556 AddFunc (module, "llvm.x86.sse41.packusdw", ret_type, arg_types, 2);
5559 ret_type = type_to_simd_type (MONO_TYPE_R8);
5560 arg_types [0] = ret_type;
5561 arg_types [1] = ret_type;
5562 arg_types [2] = LLVMInt8Type ();
5563 AddFunc (module, "llvm.x86.sse2.cmp.pd", ret_type, arg_types, 3);
5564 ret_type = type_to_simd_type (MONO_TYPE_R4);
5565 arg_types [0] = ret_type;
5566 arg_types [1] = ret_type;
5567 arg_types [2] = LLVMInt8Type ();
5568 AddFunc (module, "llvm.x86.sse.cmp.ps", ret_type, arg_types, 3);
5570 /* Conversion ops */
5571 ret_type = type_to_simd_type (MONO_TYPE_R8);
5572 arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
5573 AddFunc (module, "llvm.x86.sse2.cvtdq2pd", ret_type, arg_types, 1);
5574 ret_type = type_to_simd_type (MONO_TYPE_R4);
5575 arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
5576 AddFunc (module, "llvm.x86.sse2.cvtdq2ps", ret_type, arg_types, 1);
5577 ret_type = type_to_simd_type (MONO_TYPE_I4);
5578 arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
5579 AddFunc (module, "llvm.x86.sse2.cvtpd2dq", ret_type, arg_types, 1);
5580 ret_type = type_to_simd_type (MONO_TYPE_I4);
5581 arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
5582 AddFunc (module, "llvm.x86.sse2.cvtps2dq", ret_type, arg_types, 1);
5583 ret_type = type_to_simd_type (MONO_TYPE_R4);
5584 arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
5585 AddFunc (module, "llvm.x86.sse2.cvtpd2ps", ret_type, arg_types, 1);
5586 ret_type = type_to_simd_type (MONO_TYPE_R8);
5587 arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
5588 AddFunc (module, "llvm.x86.sse2.cvtps2pd", ret_type, arg_types, 1);
5590 ret_type = type_to_simd_type (MONO_TYPE_I4);
5591 arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
5592 AddFunc (module, "llvm.x86.sse2.cvttpd2dq", ret_type, arg_types, 1);
5593 ret_type = type_to_simd_type (MONO_TYPE_I4);
5594 arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
5595 AddFunc (module, "llvm.x86.sse2.cvttps2dq", ret_type, arg_types, 1);
5598 ret_type = type_to_simd_type (MONO_TYPE_R8);
5599 arg_types [0] = ret_type;
5600 AddFunc (module, "llvm.x86.sse2.sqrt.pd", ret_type, arg_types, 1);
5601 ret_type = type_to_simd_type (MONO_TYPE_R4);
5602 arg_types [0] = ret_type;
5603 AddFunc (module, "llvm.x86.sse.sqrt.ps", ret_type, arg_types, 1);
5604 ret_type = type_to_simd_type (MONO_TYPE_R4);
5605 arg_types [0] = ret_type;
5606 AddFunc (module, "llvm.x86.sse.rsqrt.ps", ret_type, arg_types, 1);
5607 ret_type = type_to_simd_type (MONO_TYPE_R4);
5608 arg_types [0] = ret_type;
5609 AddFunc (module, "llvm.x86.sse.rcp.ps", ret_type, arg_types, 1);
5612 ret_type = type_to_simd_type (MONO_TYPE_I2);
5613 arg_types [0] = ret_type;
5614 arg_types [1] = LLVMInt32Type ();
5615 AddFunc (module, "llvm.x86.sse2.psrli.w", ret_type, arg_types, 2);
5616 AddFunc (module, "llvm.x86.sse2.psrai.w", ret_type, arg_types, 2);
5617 AddFunc (module, "llvm.x86.sse2.pslli.w", ret_type, arg_types, 2);
5618 ret_type = type_to_simd_type (MONO_TYPE_I4);
5619 arg_types [0] = ret_type;
5620 arg_types [1] = LLVMInt32Type ();
5621 AddFunc (module, "llvm.x86.sse2.psrli.d", ret_type, arg_types, 2);
5622 AddFunc (module, "llvm.x86.sse2.psrai.d", ret_type, arg_types, 2);
5623 AddFunc (module, "llvm.x86.sse2.pslli.d", ret_type, arg_types, 2);
5624 ret_type = type_to_simd_type (MONO_TYPE_I8);
5625 arg_types [0] = ret_type;
5626 arg_types [1] = LLVMInt32Type ();
5627 AddFunc (module, "llvm.x86.sse2.psrli.q", ret_type, arg_types, 2);
5628 AddFunc (module, "llvm.x86.sse2.pslli.q", ret_type, arg_types, 2);
5631 ret_type = LLVMInt32Type ();
5632 arg_types [0] = type_to_simd_type (MONO_TYPE_I1);
5633 AddFunc (module, "llvm.x86.sse2.pmovmskb.128", ret_type, arg_types, 1);
5636 AddFunc (module, "llvm.x86.sse2.pause", LLVMVoidType (), NULL, 0);
5639 /* Load/Store intrinsics */
5641 LLVMTypeRef arg_types [5];
5645 for (i = 1; i <= 8; i *= 2) {
5646 arg_types [0] = LLVMPointerType (LLVMIntType (i * 8), 0);
5647 arg_types [1] = LLVMInt32Type ();
5648 arg_types [2] = LLVMInt1Type ();
5649 arg_types [3] = LLVMInt32Type ();
5650 sprintf (name, "llvm.mono.load.i%d.p0i%d", i * 8, i * 8);
5651 LLVMAddFunction (module, name, LLVMFunctionType (LLVMIntType (i * 8), arg_types, 4, FALSE));
5653 arg_types [0] = LLVMIntType (i * 8);
5654 arg_types [1] = LLVMPointerType (LLVMIntType (i * 8), 0);
5655 arg_types [2] = LLVMInt32Type ();
5656 arg_types [3] = LLVMInt1Type ();
5657 arg_types [4] = LLVMInt32Type ();
5658 sprintf (name, "llvm.mono.store.i%d.p0i%d", i * 8, i * 8);
5659 LLVMAddFunction (module, name, LLVMFunctionType (LLVMVoidType (), arg_types, 5, FALSE));
5665 add_types (MonoLLVMModule *lmodule)
5667 lmodule->ptr_type = LLVMPointerType (sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type (), 0);
5671 mono_llvm_init (void)
5673 mono_native_tls_alloc (¤t_cfg_tls_id, NULL);
5677 init_jit_module (MonoDomain *domain)
5679 MonoJitICallInfo *info;
5680 MonoJitDomainInfo *dinfo;
5681 MonoLLVMModule *module;
5684 dinfo = domain_jit_info (domain);
5685 if (dinfo->llvm_module)
5688 mono_loader_lock ();
5690 if (dinfo->llvm_module) {
5691 mono_loader_unlock ();
5695 module = g_new0 (MonoLLVMModule, 1);
5697 name = g_strdup_printf ("mono-%s", domain->friendly_name);
5698 module->module = LLVMModuleCreateWithName (name);
5700 module->mono_ee = mono_llvm_create_ee (LLVMCreateModuleProviderForExistingModule (module->module), alloc_cb, emitted_cb, exception_cb, dlsym_cb, &module->ee);
5702 add_intrinsics (module->module);
5705 module->llvm_types = g_hash_table_new (NULL, NULL);
5707 info = mono_find_jit_icall_by_name ("llvm_resume_unwind_trampoline");
5709 LLVMAddGlobalMapping (module->ee, LLVMGetNamedFunction (module->module, "llvm_resume_unwind_trampoline"), (void*)info->func);
5711 mono_memory_barrier ();
5713 dinfo->llvm_module = module;
5715 mono_loader_unlock ();
5719 mono_llvm_cleanup (void)
5721 if (aot_module.module)
5722 LLVMDisposeModule (aot_module.module);
5724 LLVMContextDispose (LLVMGetGlobalContext ());
5728 mono_llvm_free_domain_info (MonoDomain *domain)
5730 MonoJitDomainInfo *info = domain_jit_info (domain);
5731 MonoLLVMModule *module = info->llvm_module;
5737 if (module->llvm_types)
5738 g_hash_table_destroy (module->llvm_types);
5740 mono_llvm_dispose_ee (module->mono_ee);
5742 if (module->bb_names) {
5743 for (i = 0; i < module->bb_names_len; ++i)
5744 g_free (module->bb_names [i]);
5745 g_free (module->bb_names);
5747 //LLVMDisposeModule (module->module);
5751 info->llvm_module = NULL;
5755 mono_llvm_create_aot_module (const char *got_symbol, gboolean external_symbols, gboolean emit_dwarf)
5757 /* Delete previous module */
5758 if (aot_module.plt_entries)
5759 g_hash_table_destroy (aot_module.plt_entries);
5760 if (aot_module.module)
5761 LLVMDisposeModule (aot_module.module);
5763 memset (&aot_module, 0, sizeof (aot_module));
5765 aot_module.module = LLVMModuleCreateWithName ("aot");
5766 aot_module.got_symbol = got_symbol;
5767 aot_module.external_symbols = external_symbols;
5768 aot_module.emit_dwarf = emit_dwarf;
5769 /* The first few entries are reserved */
5770 aot_module.max_got_offset = 16;
5772 add_intrinsics (aot_module.module);
5773 add_types (&aot_module);
5777 * We couldn't compute the type of the LLVM global representing the got because
5778 * its size is only known after all the methods have been emitted. So create
5779 * a dummy variable, and replace all uses it with the real got variable when
5780 * its size is known in mono_llvm_emit_aot_module ().
5783 LLVMTypeRef got_type = LLVMArrayType (aot_module.ptr_type, 0);
5785 aot_module.got_var = LLVMAddGlobal (aot_module.module, got_type, "mono_dummy_got");
5786 LLVMSetInitializer (aot_module.got_var, LLVMConstNull (got_type));
5789 /* Add a dummy personality function */
5791 LLVMBasicBlockRef lbb;
5792 LLVMBuilderRef lbuilder;
5793 LLVMValueRef personality;
5795 personality = LLVMAddFunction (aot_module.module, "mono_aot_personality", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
5796 LLVMSetLinkage (personality, LLVMInternalLinkage);
5797 lbb = LLVMAppendBasicBlock (personality, "BB0");
5798 lbuilder = LLVMCreateBuilder ();
5799 LLVMPositionBuilderAtEnd (lbuilder, lbb);
5800 LLVMBuildRetVoid (lbuilder);
5801 mark_as_used (&aot_module, personality);
5804 aot_module.llvm_types = g_hash_table_new (NULL, NULL);
5805 aot_module.plt_entries = g_hash_table_new (g_str_hash, g_str_equal);
5806 aot_module.plt_entries_ji = g_hash_table_new (NULL, NULL);
5807 aot_module.method_to_lmethod = g_hash_table_new (NULL, NULL);
5811 * Emit the aot module into the LLVM bitcode file FILENAME.
5814 mono_llvm_emit_aot_module (const char *filename, const char *cu_name)
5816 LLVMTypeRef got_type;
5817 LLVMValueRef real_got;
5818 MonoLLVMModule *module = &aot_module;
5821 * Create the real got variable and replace all uses of the dummy variable with
5824 got_type = LLVMArrayType (aot_module.ptr_type, module->max_got_offset + 1);
5825 real_got = LLVMAddGlobal (aot_module.module, got_type, aot_module.got_symbol);
5826 LLVMSetInitializer (real_got, LLVMConstNull (got_type));
5827 if (module->external_symbols) {
5828 LLVMSetLinkage (real_got, LLVMExternalLinkage);
5829 LLVMSetVisibility (real_got, LLVMHiddenVisibility);
5831 LLVMSetLinkage (real_got, LLVMInternalLinkage);
5833 mono_llvm_replace_uses_of (aot_module.got_var, real_got);
5835 mark_as_used (&aot_module, real_got);
5837 /* Delete the dummy got so it doesn't become a global */
5838 LLVMDeleteGlobal (aot_module.got_var);
5840 emit_llvm_used (&aot_module);
5841 emit_dbg_info (&aot_module, filename, cu_name);
5843 /* Replace PLT entries for directly callable methods with the methods themselves */
5845 GHashTableIter iter;
5847 LLVMValueRef callee;
5849 g_hash_table_iter_init (&iter, aot_module.plt_entries_ji);
5850 while (g_hash_table_iter_next (&iter, (void**)&ji, (void**)&callee)) {
5851 if (mono_aot_is_direct_callable (ji)) {
5852 LLVMValueRef lmethod;
5854 lmethod = g_hash_table_lookup (module->method_to_lmethod, ji->data.method);
5855 /* The types might not match because the caller might pass an rgctx */
5856 if (lmethod && LLVMTypeOf (callee) == LLVMTypeOf (lmethod)) {
5857 mono_llvm_replace_uses_of (callee, lmethod);
5858 mono_aot_mark_unused_llvm_plt_entry (ji);
5868 if (LLVMVerifyModule (aot_module.module, LLVMReturnStatusAction, &verifier_err)) {
5869 g_assert_not_reached ();
5874 LLVMWriteBitcodeToFile (aot_module.module, filename);
5879 md_string (const char *s)
5881 return LLVMMDString (s, strlen (s));
5884 /* Debugging support */
5887 emit_dbg_info (MonoLLVMModule *lmodule, const char *filename, const char *cu_name)
5889 LLVMModuleRef module = lmodule->module;
5890 LLVMValueRef args [16], cu_args [16], cu, ver;
5892 char *build_info, *s, *dir;
5895 * This can only be enabled when LLVM code is emitted into a separate object
5896 * file, since the AOT compiler also emits dwarf info,
5897 * and the abbrev indexes will not be correct since llvm has added its own
5900 if (!lmodule->emit_dwarf)
5904 * Emit dwarf info in the form of LLVM metadata. There is some
5905 * out-of-date documentation at:
5906 * http://llvm.org/docs/SourceLevelDebugging.html
5907 * but most of this was gathered from the llvm and
5912 cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), DW_TAG_compile_unit, FALSE);
5913 /* CU name/compilation dir */
5914 dir = g_path_get_dirname (filename);
5915 args [0] = LLVMMDString (cu_name, strlen (cu_name));
5916 args [1] = LLVMMDString (dir, strlen (dir));
5917 cu_args [n_cuargs ++] = LLVMMDNode (args, 2);
5920 cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), DW_LANG_C99, FALSE);
5922 build_info = mono_get_runtime_build_info ();
5923 s = g_strdup_printf ("Mono AOT Compiler %s (LLVM)", build_info);
5924 cu_args [n_cuargs ++] = LLVMMDString (s, strlen (s));
5925 g_free (build_info);
5927 cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
5929 cu_args [n_cuargs ++] = LLVMMDString ("", strlen (""));
5930 /* Runtime version */
5931 cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
5933 cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
5934 cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
5936 if (lmodule->subprogram_mds) {
5940 mds = g_new0 (LLVMValueRef, lmodule->subprogram_mds->len);
5941 for (i = 0; i < lmodule->subprogram_mds->len; ++i)
5942 mds [i] = g_ptr_array_index (lmodule->subprogram_mds, i);
5943 cu_args [n_cuargs ++] = LLVMMDNode (mds, lmodule->subprogram_mds->len);
5945 cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
5948 cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
5949 /* Imported modules */
5950 cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
5952 cu_args [n_cuargs ++] = LLVMMDString ("", strlen (""));
5953 /* DebugEmissionKind = FullDebug */
5954 cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
5955 cu = LLVMMDNode (cu_args, n_cuargs);
5956 LLVMAddNamedMetadataOperand (module, "llvm.dbg.cu", cu);
5958 args [0] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
5959 args [1] = LLVMMDString ("Dwarf Version", strlen ("Dwarf Version"));
5960 args [2] = LLVMConstInt (LLVMInt32Type (), 2, FALSE);
5961 ver = LLVMMDNode (args, 3);
5962 LLVMAddNamedMetadataOperand (module, "llvm.module.flags", ver);
5964 args [0] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
5965 args [1] = LLVMMDString ("Debug Info Version", strlen ("Debug Info Version"));
5966 args [2] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
5967 ver = LLVMMDNode (args, 3);
5968 LLVMAddNamedMetadataOperand (module, "llvm.module.flags", ver);
5972 emit_dbg_subprogram (EmitContext *ctx, MonoCompile *cfg, LLVMValueRef method, const char *name)
5974 MonoLLVMModule *module = ctx->lmodule;
5975 MonoDebugMethodInfo *minfo = ctx->minfo;
5976 char *source_file, *dir, *filename;
5977 LLVMValueRef md, args [16], ctx_args [16], md_args [64], type_args [16], ctx_md, type_md;
5985 mono_debug_symfile_get_line_numbers_full (minfo, &source_file, NULL, &n_il_offsets, &il_offsets, &line_numbers, NULL, NULL, NULL, NULL);
5987 source_file = g_strdup ("<unknown>");
5988 dir = g_path_get_dirname (source_file);
5989 filename = g_path_get_basename (source_file);
5991 ctx_args [0] = LLVMConstInt (LLVMInt32Type (), 0x29, FALSE);
5992 args [0] = md_string (filename);
5993 args [1] = md_string (dir);
5994 ctx_args [1] = LLVMMDNode (args, 2);
5995 ctx_md = LLVMMDNode (ctx_args, 2);
5997 type_args [0] = LLVMConstInt (LLVMInt32Type (), DW_TAG_subroutine_type, FALSE);
5998 type_args [1] = NULL;
5999 type_args [2] = NULL;
6000 type_args [3] = LLVMMDString ("", 0);
6001 type_args [4] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
6002 type_args [5] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
6003 type_args [6] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
6004 type_args [7] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
6005 type_args [8] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
6006 type_args [9] = NULL;
6007 type_args [10] = NULL;
6008 type_args [11] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
6009 type_args [12] = NULL;
6010 type_args [13] = NULL;
6011 type_args [14] = NULL;
6012 type_md = LLVMMDNode (type_args, 14);
6014 /* http://llvm.org/docs/SourceLevelDebugging.html#subprogram-descriptors */
6015 md_args [0] = LLVMConstInt (LLVMInt32Type (), DW_TAG_subprogram, FALSE);
6016 /* Source directory + file pair */
6017 args [0] = md_string (filename);
6018 args [1] = md_string (dir);
6019 md_args [1] = LLVMMDNode (args ,2);
6020 md_args [2] = ctx_md;
6021 md_args [3] = md_string (cfg->method->name);
6022 md_args [4] = md_string (name);
6023 md_args [5] = md_string (name);
6026 md_args [6] = LLVMConstInt (LLVMInt32Type (), line_numbers [0], FALSE);
6028 md_args [6] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
6030 md_args [7] = type_md;
6032 md_args [8] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
6034 md_args [9] = LLVMConstInt (LLVMInt1Type (), 1, FALSE);
6036 md_args [10] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
6037 /* Index into a virtual function */
6038 md_args [11] = NULL;
6039 md_args [12] = NULL;
6041 md_args [13] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
6043 md_args [14] = LLVMConstInt (LLVMInt1Type (), 1, FALSE);
6044 /* Pointer to LLVM function */
6045 md_args [15] = method;
6046 /* Function template parameter */
6047 md_args [16] = NULL;
6048 /* Function declaration descriptor */
6049 md_args [17] = NULL;
6050 /* List of function variables */
6051 md_args [18] = LLVMMDNode (args, 0);
6053 md_args [19] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
6054 md = LLVMMDNode (md_args, 20);
6056 if (!module->subprogram_mds)
6057 module->subprogram_mds = g_ptr_array_new ();
6058 g_ptr_array_add (module->subprogram_mds, md);
6062 g_free (source_file);
6063 g_free (il_offsets);
6064 g_free (line_numbers);
6070 emit_dbg_loc (EmitContext *ctx, LLVMBuilderRef builder, const unsigned char *cil_code)
6072 MonoCompile *cfg = ctx->cfg;
6074 if (ctx->minfo && cil_code && cil_code >= cfg->header->code && cil_code < cfg->header->code + cfg->header->code_size) {
6075 MonoDebugSourceLocation *loc;
6076 LLVMValueRef loc_md, md_args [16];
6079 loc = mono_debug_symfile_lookup_location (ctx->minfo, cil_code - cfg->header->code);
6083 md_args [nmd_args ++] = LLVMConstInt (LLVMInt32Type (), loc->row, FALSE);
6084 md_args [nmd_args ++] = LLVMConstInt (LLVMInt32Type (), loc->column, FALSE);
6085 md_args [nmd_args ++] = ctx->dbg_md;
6086 md_args [nmd_args ++] = NULL;
6087 loc_md = LLVMMDNode (md_args, nmd_args);
6088 LLVMSetCurrentDebugLocation (builder, loc_md);
6089 mono_debug_symfile_free_location (loc);
6096 - Emit LLVM IR from the mono IR using the LLVM C API.
6097 - The original arch specific code remains, so we can fall back to it if we run
6098 into something we can't handle.
6102 A partial list of issues:
6103 - Handling of opcodes which can throw exceptions.
6105 In the mono JIT, these are implemented using code like this:
6112 push throw_pos - method
6113 call <exception trampoline>
6115 The problematic part is push throw_pos - method, which cannot be represented
6116 in the LLVM IR, since it does not support label values.
6117 -> this can be implemented in AOT mode using inline asm + labels, but cannot
6118 be implemented in JIT mode ?
6119 -> a possible but slower implementation would use the normal exception
6120 throwing code but it would need to control the placement of the throw code
6121 (it needs to be exactly after the compare+branch).
6122 -> perhaps add a PC offset intrinsics ?
6124 - efficient implementation of .ovf opcodes.
6126 These are currently implemented as:
6127 <ins which sets the condition codes>
6130 Some overflow opcodes are now supported by LLVM SVN.
6132 - exception handling, unwinding.
6133 - SSA is disabled for methods with exception handlers
6134 - How to obtain unwind info for LLVM compiled methods ?
6135 -> this is now solved by converting the unwind info generated by LLVM
6137 - LLVM uses the c++ exception handling framework, while we use our home grown
6138 code, and couldn't use the c++ one:
6139 - its not supported under VC++, other exotic platforms.
6140 - it might be impossible to support filter clauses with it.
6144 The trampolines need a predictable call sequence, since they need to disasm
6145 the calling code to obtain register numbers / offsets.
6147 LLVM currently generates this code in non-JIT mode:
6148 mov -0x98(%rax),%eax
6150 Here, the vtable pointer is lost.
6151 -> solution: use one vtable trampoline per class.
6153 - passing/receiving the IMT pointer/RGCTX.
6154 -> solution: pass them as normal arguments ?
6158 LLVM does not allow the specification of argument registers etc. This means
6159 that all calls are made according to the platform ABI.
6161 - passing/receiving vtypes.
6163 Vtypes passed/received in registers are handled by the front end by using
6164 a signature with scalar arguments, and loading the parts of the vtype into those
6167 Vtypes passed on the stack are handled using the 'byval' attribute.
6171 Supported though alloca, we need to emit the load/store code.
6175 The mono JIT uses pointer sized iregs/double fregs, while LLVM uses precisely
6176 typed registers, so we have to keep track of the precise LLVM type of each vreg.
6177 This is made easier because the IR is already in SSA form.
6178 An additional problem is that our IR is not consistent with types, i.e. i32/ia64
6179 types are frequently used incorrectly.
6184 Emit LLVM bytecode into a .bc file, compile it using llc into a .s file, then link
6185 it with the file containing the methods emitted by the JIT and the AOT data
6189 /* FIXME: Normalize some aspects of the mono IR to allow easier translation, like:
6190 * - each bblock should end with a branch
6191 * - setting the return value, making cfg->ret non-volatile
6192 * - avoid some transformations in the JIT which make it harder for us to generate
6194 * - use pointer types to help optimizations.