2 * mini-llvm.c: llvm "Backend" for the mono JIT
4 * (C) 2009 Novell, Inc.
8 #include <mono/metadata/debug-helpers.h>
9 #include <mono/metadata/mempool-internals.h>
11 #ifndef __STDC_LIMIT_MACROS
12 #define __STDC_LIMIT_MACROS
14 #ifndef __STDC_CONSTANT_MACROS
15 #define __STDC_CONSTANT_MACROS
18 #include "llvm-c/Core.h"
19 #include "llvm-c/ExecutionEngine.h"
20 #include "llvm-c/BitWriter.h"
21 #include "llvm-c/Analysis.h"
23 #include "mini-llvm-cpp.h"
26 * Information associated by mono with LLVM modules.
30 LLVMValueRef throw, rethrow, throw_corlib_exception;
31 GHashTable *llvm_types;
33 const char *got_symbol;
34 GHashTable *plt_entries;
38 * Information associated by the backend with mono basic blocks.
41 LLVMBasicBlockRef bblock, end_bblock;
42 LLVMValueRef finally_ind;
43 gboolean added, invoke_target;
45 * If this bblock is the start of a finally clause, this is a list of bblocks it
46 * needs to branch to in ENDFINALLY.
48 GSList *call_handler_return_bbs;
50 * If this bblock is the start of a finally clause, this is the bblock that
51 * CALL_HANDLER needs to branch to.
53 LLVMBasicBlockRef call_handler_target_bb;
54 /* The list of switch statements generated by ENDFINALLY instructions */
55 GSList *endfinally_switch_ins_list;
60 * Structure containing emit state
65 /* Maps method names to the corresponding LLVMValueRef */
66 GHashTable *emitted_method_decls;
70 MonoLLVMModule *lmodule;
73 int sindex, default_index, ex_index;
74 LLVMBuilderRef builder;
75 LLVMValueRef *values, *addresses;
76 MonoType **vreg_cli_types;
78 MonoMethodSignature *sig;
80 GHashTable *region_to_handler;
81 LLVMBuilderRef alloca_builder;
82 LLVMValueRef last_alloca;
83 LLVMValueRef rgctx_arg;
84 LLVMTypeRef *vreg_types;
86 gboolean *unreachable;
95 MonoBasicBlock *in_bb;
100 * Instruction metadata
101 * This is the same as ins_info, but LREG != IREG.
109 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
110 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
117 /* keep in sync with the enum in mini.h */
120 #include "mini-ops.h"
125 #if SIZEOF_VOID_P == 4
126 #define GET_LONG_IMM(ins) (((guint64)(ins)->inst_ms_word << 32) | (guint64)(guint32)(ins)->inst_ls_word)
128 #define GET_LONG_IMM(ins) ((ins)->inst_imm)
131 #define LLVM_INS_INFO(opcode) (&llvm_ins_info [((opcode) - OP_START - 1) * 4])
134 #define TRACE_FAILURE(msg) do { printf ("%s\n", msg); } while (0)
136 #define TRACE_FAILURE(msg)
140 #define IS_TARGET_X86 1
142 #define IS_TARGET_X86 0
145 #define LLVM_FAILURE(ctx, reason) do { \
146 TRACE_FAILURE (reason); \
147 (ctx)->cfg->exception_message = g_strdup (reason); \
148 (ctx)->cfg->disable_llvm = TRUE; \
152 #define CHECK_FAILURE(ctx) do { \
153 if ((ctx)->cfg->disable_llvm) \
157 static LLVMIntPredicate cond_to_llvm_cond [] = {
170 static LLVMRealPredicate fpcond_to_llvm_cond [] = {
183 static LLVMExecutionEngineRef ee;
184 static guint32 current_cfg_tls_id;
186 static MonoLLVMModule jit_module, aot_module;
187 static gboolean jit_module_inited;
188 static int memset_param_count, memcpy_param_count;
189 static const char *memset_func_name;
190 static const char *memcpy_func_name;
191 static const char *eh_selector_name;
193 static void init_jit_module (void);
198 * The LLVM type with width == sizeof (gpointer)
203 return sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type ();
209 * Return the size of the LLVM representation of the vtype T.
212 get_vtype_size (MonoType *t)
216 size = mono_class_value_size (mono_class_from_mono_type (t), NULL);
218 while (size < sizeof (gpointer) && mono_is_power_of_two (size) == -1)
225 * simd_class_to_llvm_type:
227 * Return the LLVM type corresponding to the Mono.SIMD class KLASS
230 simd_class_to_llvm_type (EmitContext *ctx, MonoClass *klass)
232 if (!strcmp (klass->name, "Vector2d")) {
233 return LLVMVectorType (LLVMDoubleType (), 2);
234 } else if (!strcmp (klass->name, "Vector2l")) {
235 return LLVMVectorType (LLVMInt64Type (), 2);
236 } else if (!strcmp (klass->name, "Vector2ul")) {
237 return LLVMVectorType (LLVMInt64Type (), 2);
238 } else if (!strcmp (klass->name, "Vector4i")) {
239 return LLVMVectorType (LLVMInt32Type (), 4);
240 } else if (!strcmp (klass->name, "Vector4ui")) {
241 return LLVMVectorType (LLVMInt32Type (), 4);
242 } else if (!strcmp (klass->name, "Vector4f")) {
243 return LLVMVectorType (LLVMFloatType (), 4);
244 } else if (!strcmp (klass->name, "Vector8s")) {
245 return LLVMVectorType (LLVMInt16Type (), 8);
246 } else if (!strcmp (klass->name, "Vector8us")) {
247 return LLVMVectorType (LLVMInt16Type (), 8);
248 } else if (!strcmp (klass->name, "Vector16sb")) {
249 return LLVMVectorType (LLVMInt8Type (), 16);
250 } else if (!strcmp (klass->name, "Vector16b")) {
251 return LLVMVectorType (LLVMInt8Type (), 16);
253 printf ("%s\n", klass->name);
262 * Return the LLVM type corresponding to T.
265 type_to_llvm_type (EmitContext *ctx, MonoType *t)
268 return LLVMPointerType (LLVMInt8Type (), 0);
271 return LLVMVoidType ();
273 return LLVMInt8Type ();
275 return LLVMInt16Type ();
277 return LLVMInt32Type ();
279 return LLVMInt8Type ();
281 return LLVMInt16Type ();
283 return LLVMInt32Type ();
284 case MONO_TYPE_BOOLEAN:
285 return LLVMInt8Type ();
288 return LLVMInt64Type ();
290 return LLVMInt16Type ();
292 return LLVMFloatType ();
294 return LLVMDoubleType ();
297 return IntPtrType ();
298 case MONO_TYPE_OBJECT:
299 case MONO_TYPE_CLASS:
300 case MONO_TYPE_ARRAY:
301 case MONO_TYPE_SZARRAY:
302 case MONO_TYPE_STRING:
304 return LLVMPointerType (IntPtrType (), 0);
307 /* Because of generic sharing */
308 return IntPtrType ();
309 case MONO_TYPE_GENERICINST:
310 if (!mono_type_generic_inst_is_valuetype (t))
311 return IntPtrType ();
313 case MONO_TYPE_VALUETYPE:
314 case MONO_TYPE_TYPEDBYREF: {
318 klass = mono_class_from_mono_type (t);
320 if (MONO_CLASS_IS_SIMD (ctx->cfg, klass))
321 return simd_class_to_llvm_type (ctx, klass);
324 return type_to_llvm_type (ctx, mono_class_enum_basetype (klass));
325 ltype = g_hash_table_lookup (ctx->lmodule->llvm_types, klass);
328 LLVMTypeRef *eltypes;
330 size = get_vtype_size (t);
332 eltypes = g_new (LLVMTypeRef, size);
333 for (i = 0; i < size; ++i)
334 eltypes [i] = LLVMInt8Type ();
336 ltype = LLVMStructType (eltypes, size, FALSE);
337 g_hash_table_insert (ctx->lmodule->llvm_types, klass, ltype);
344 printf ("X: %d\n", t->type);
345 ctx->cfg->exception_message = g_strdup_printf ("type %s", mono_type_full_name (t));
346 ctx->cfg->disable_llvm = TRUE;
354 * Return whenever T is an unsigned int type.
357 type_is_unsigned (EmitContext *ctx, MonoType *t)
373 * type_to_llvm_arg_type:
375 * Same as type_to_llvm_type, but treat i8/i16 as i32.
378 type_to_llvm_arg_type (EmitContext *ctx, MonoType *t)
380 LLVMTypeRef ptype = type_to_llvm_type (ctx, t);
382 if (ptype == LLVMInt8Type () || ptype == LLVMInt16Type ()) {
384 * LLVM generates code which only sets the lower bits, while JITted
385 * code expects all the bits to be set.
387 ptype = LLVMInt32Type ();
394 * llvm_type_to_stack_type:
396 * Return the LLVM type which needs to be used when a value of type TYPE is pushed
399 static G_GNUC_UNUSED LLVMTypeRef
400 llvm_type_to_stack_type (LLVMTypeRef type)
404 if (type == LLVMInt8Type ())
405 return LLVMInt32Type ();
406 else if (type == LLVMInt16Type ())
407 return LLVMInt32Type ();
408 else if (type == LLVMFloatType ())
409 return LLVMDoubleType ();
415 * regtype_to_llvm_type:
417 * Return the LLVM type corresponding to the regtype C used in instruction
421 regtype_to_llvm_type (char c)
425 return LLVMInt32Type ();
427 return LLVMInt64Type ();
429 return LLVMDoubleType ();
438 * Return the LLVM type corresponding to the unary/binary opcode OPCODE.
441 op_to_llvm_type (int opcode)
446 return LLVMInt8Type ();
449 return LLVMInt8Type ();
452 return LLVMInt16Type ();
455 return LLVMInt16Type ();
458 return LLVMInt32Type ();
461 return LLVMInt32Type ();
463 return LLVMInt64Type ();
465 return LLVMFloatType ();
467 return LLVMDoubleType ();
469 return LLVMInt64Type ();
471 return LLVMInt32Type ();
473 return LLVMInt64Type ();
476 return LLVMInt8Type ();
479 return LLVMInt16Type ();
482 return sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type ();
489 return LLVMInt32Type ();
496 return LLVMInt64Type ();
498 printf ("%s\n", mono_inst_name (opcode));
499 g_assert_not_reached ();
505 * load_store_to_llvm_type:
507 * Return the size/sign/zero extension corresponding to the load/store opcode
511 load_store_to_llvm_type (int opcode, int *size, gboolean *sext, gboolean *zext)
517 case OP_LOADI1_MEMBASE:
518 case OP_STOREI1_MEMBASE_REG:
519 case OP_STOREI1_MEMBASE_IMM:
522 return LLVMInt8Type ();
523 case OP_LOADU1_MEMBASE:
527 return LLVMInt8Type ();
528 case OP_LOADI2_MEMBASE:
529 case OP_STOREI2_MEMBASE_REG:
530 case OP_STOREI2_MEMBASE_IMM:
533 return LLVMInt16Type ();
534 case OP_LOADU2_MEMBASE:
538 return LLVMInt16Type ();
539 case OP_LOADI4_MEMBASE:
540 case OP_LOADU4_MEMBASE:
543 case OP_STOREI4_MEMBASE_REG:
544 case OP_STOREI4_MEMBASE_IMM:
546 return LLVMInt32Type ();
547 case OP_LOADI8_MEMBASE:
549 case OP_STOREI8_MEMBASE_REG:
550 case OP_STOREI8_MEMBASE_IMM:
552 return LLVMInt64Type ();
553 case OP_LOADR4_MEMBASE:
554 case OP_STORER4_MEMBASE_REG:
556 return LLVMFloatType ();
557 case OP_LOADR8_MEMBASE:
558 case OP_STORER8_MEMBASE_REG:
560 return LLVMDoubleType ();
561 case OP_LOAD_MEMBASE:
563 case OP_STORE_MEMBASE_REG:
564 case OP_STORE_MEMBASE_IMM:
565 *size = sizeof (gpointer);
566 return IntPtrType ();
568 g_assert_not_reached ();
576 * Return the LLVM intrinsics corresponding to the overflow opcode OPCODE.
579 ovf_op_to_intrins (int opcode)
583 return "llvm.sadd.with.overflow.i32";
585 return "llvm.uadd.with.overflow.i32";
587 return "llvm.ssub.with.overflow.i32";
589 return "llvm.usub.with.overflow.i32";
591 return "llvm.smul.with.overflow.i32";
593 return "llvm.umul.with.overflow.i32";
595 return "llvm.sadd.with.overflow.i64";
597 return "llvm.uadd.with.overflow.i64";
599 return "llvm.ssub.with.overflow.i64";
601 return "llvm.usub.with.overflow.i64";
603 return "llvm.smul.with.overflow.i64";
605 return "llvm.umul.with.overflow.i64";
607 g_assert_not_reached ();
613 simd_op_to_intrins (int opcode)
616 #if defined(TARGET_X86) || defined(TARGET_AMD64)
618 return "llvm.x86.sse2.min.pd";
620 return "llvm.x86.sse2.min.ps";
622 return "llvm.x86.sse41.pminud";
624 return "llvm.x86.sse41.pminuw";
626 return "llvm.x86.sse2.pminu.b";
628 return "llvm.x86.sse2.pmins.w";
630 return "llvm.x86.sse2.max.pd";
632 return "llvm.x86.sse2.max.ps";
634 return "llvm.x86.sse41.pmaxud";
636 return "llvm.x86.sse41.pmaxuw";
638 return "llvm.x86.sse2.pmaxu.b";
640 return "llvm.x86.sse2.pcmpeq.b";
642 return "llvm.x86.sse2.pcmpeq.w";
644 return "llvm.x86.sse2.pcmpeq.d";
646 return "llvm.x86.sse41.pcmpeqq";
648 return "llvm.x86.sse2.pcmpgt.b";
651 g_assert_not_reached ();
657 simd_op_to_llvm_type (int opcode)
659 #if defined(TARGET_X86) || defined(TARGET_AMD64)
663 return LLVMVectorType (LLVMDoubleType (), 2);
666 return LLVMVectorType (LLVMInt64Type (), 2);
669 return LLVMVectorType (LLVMInt32Type (), 4);
673 return LLVMVectorType (LLVMInt16Type (), 8);
677 return LLVMVectorType (LLVMInt8Type (), 16);
679 return LLVMVectorType (LLVMFloatType (), 4);
681 g_assert_not_reached ();
692 * Return the LLVM basic block corresponding to BB.
694 static LLVMBasicBlockRef
695 get_bb (EmitContext *ctx, MonoBasicBlock *bb)
699 if (ctx->bblocks [bb->block_num].bblock == NULL) {
700 sprintf (bb_name, "BB%d", bb->block_num);
702 ctx->bblocks [bb->block_num].bblock = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
703 ctx->bblocks [bb->block_num].end_bblock = ctx->bblocks [bb->block_num].bblock;
706 return ctx->bblocks [bb->block_num].bblock;
712 * Return the last LLVM bblock corresponding to BB.
713 * This might not be equal to the bb returned by get_bb () since we need to generate
714 * multiple LLVM bblocks for a mono bblock to handle throwing exceptions.
716 static LLVMBasicBlockRef
717 get_end_bb (EmitContext *ctx, MonoBasicBlock *bb)
720 return ctx->bblocks [bb->block_num].end_bblock;
723 static LLVMBasicBlockRef
724 gen_bb (EmitContext *ctx, const char *prefix)
728 sprintf (bb_name, "%s%d", prefix, ++ ctx->ex_index);
729 return LLVMAppendBasicBlock (ctx->lmethod, bb_name);
735 * Return the target of the patch identified by TYPE and TARGET.
738 resolve_patch (MonoCompile *cfg, MonoJumpInfoType type, gconstpointer target)
742 memset (&ji, 0, sizeof (ji));
744 ji.data.target = target;
746 return mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &ji, FALSE);
752 * Emit code to convert the LLVM value V to DTYPE.
755 convert_full (EmitContext *ctx, LLVMValueRef v, LLVMTypeRef dtype, gboolean is_unsigned)
757 LLVMTypeRef stype = LLVMTypeOf (v);
759 if (stype != dtype) {
760 gboolean ext = FALSE;
763 if (dtype == LLVMInt64Type () && (stype == LLVMInt32Type () || stype == LLVMInt16Type () || stype == LLVMInt8Type ()))
765 else if (dtype == LLVMInt32Type () && (stype == LLVMInt16Type () || stype == LLVMInt8Type ()))
767 else if (dtype == LLVMInt16Type () && (stype == LLVMInt8Type ()))
771 return is_unsigned ? LLVMBuildZExt (ctx->builder, v, dtype, "") : LLVMBuildSExt (ctx->builder, v, dtype, "");
773 if (dtype == LLVMDoubleType () && stype == LLVMFloatType ())
774 return LLVMBuildFPExt (ctx->builder, v, dtype, "");
777 if (stype == LLVMInt64Type () && (dtype == LLVMInt32Type () || dtype == LLVMInt16Type () || dtype == LLVMInt8Type ()))
778 return LLVMBuildTrunc (ctx->builder, v, dtype, "");
779 if (stype == LLVMInt32Type () && (dtype == LLVMInt16Type () || dtype == LLVMInt8Type ()))
780 return LLVMBuildTrunc (ctx->builder, v, dtype, "");
781 if (stype == LLVMInt16Type () && dtype == LLVMInt8Type ())
782 return LLVMBuildTrunc (ctx->builder, v, dtype, "");
783 if (stype == LLVMDoubleType () && dtype == LLVMFloatType ())
784 return LLVMBuildFPTrunc (ctx->builder, v, dtype, "");
786 if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind && LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
787 return LLVMBuildBitCast (ctx->builder, v, dtype, "");
788 if (LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
789 return LLVMBuildIntToPtr (ctx->builder, v, dtype, "");
790 if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind)
791 return LLVMBuildPtrToInt (ctx->builder, v, dtype, "");
793 #ifdef MONO_ARCH_SOFT_FLOAT
794 if (stype == LLVMInt32Type () && dtype == LLVMFloatType ())
795 return LLVMBuildBitCast (ctx->builder, v, dtype, "");
796 if (stype == LLVMInt32Type () && dtype == LLVMDoubleType ())
797 return LLVMBuildBitCast (ctx->builder, LLVMBuildZExt (ctx->builder, v, LLVMInt64Type (), ""), dtype, "");
801 LLVMDumpValue (LLVMConstNull (dtype));
802 g_assert_not_reached ();
810 convert (EmitContext *ctx, LLVMValueRef v, LLVMTypeRef dtype)
812 return convert_full (ctx, v, dtype, FALSE);
816 * emit_volatile_load:
818 * If vreg is volatile, emit a load from its address.
821 emit_volatile_load (EmitContext *ctx, int vreg)
825 LLVMValueRef v = LLVMBuildLoad (ctx->builder, ctx->addresses [vreg], "");
826 t = ctx->vreg_cli_types [vreg];
827 if (t && !t->byref) {
829 * Might have to zero extend since llvm doesn't have
832 if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_U2 || t->type == MONO_TYPE_CHAR || t->type == MONO_TYPE_BOOLEAN)
833 v = LLVMBuildZExt (ctx->builder, v, LLVMInt32Type (), "");
834 else if (t->type == MONO_TYPE_U8)
835 v = LLVMBuildZExt (ctx->builder, v, LLVMInt64Type (), "");
842 * emit_volatile_store:
844 * If VREG is volatile, emit a store from its value to its address.
847 emit_volatile_store (EmitContext *ctx, int vreg)
849 MonoInst *var = get_vreg_to_inst (ctx->cfg, vreg);
851 if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
852 g_assert (ctx->addresses [vreg]);
853 LLVMBuildStore (ctx->builder, convert (ctx, ctx->values [vreg], type_to_llvm_type (ctx, var->inst_vtype)), ctx->addresses [vreg]);
859 * Maps parameter indexes in the original signature to parameter indexes
860 * in the LLVM signature.
863 /* The indexes of various special arguments in the LLVM signature */
864 int vret_arg_pindex, this_arg_pindex, rgctx_arg_pindex, imt_arg_pindex;
868 * sig_to_llvm_sig_full:
870 * Return the LLVM signature corresponding to the mono signature SIG using the
871 * calling convention information in CINFO. Return parameter mapping information in SINFO.
874 sig_to_llvm_sig_full (EmitContext *ctx, MonoMethodSignature *sig, LLVMCallInfo *cinfo,
877 LLVMTypeRef ret_type;
878 LLVMTypeRef *param_types = NULL;
880 int i, j, pindex, vret_arg_pindex = 0;
882 gboolean vretaddr = FALSE;
885 memset (sinfo, 0, sizeof (LLVMSigInfo));
887 ret_type = type_to_llvm_type (ctx, sig->ret);
890 if (cinfo && cinfo->ret.storage == LLVMArgVtypeInReg) {
891 /* LLVM models this by returning an aggregate value */
892 if (cinfo->ret.pair_storage [0] == LLVMArgInIReg && cinfo->ret.pair_storage [1] == LLVMArgNone) {
893 LLVMTypeRef members [2];
895 members [0] = IntPtrType ();
896 ret_type = LLVMStructType (members, 1, FALSE);
898 g_assert_not_reached ();
900 } else if (cinfo && MONO_TYPE_ISSTRUCT (sig->ret)) {
901 g_assert (cinfo->ret.storage == LLVMArgVtypeRetAddr);
903 ret_type = LLVMVoidType ();
906 pindexes = g_new0 (int, sig->param_count);
907 param_types = g_new0 (LLVMTypeRef, (sig->param_count * 2) + 3);
909 if (cinfo && cinfo->rgctx_arg) {
911 sinfo->rgctx_arg_pindex = pindex;
912 param_types [pindex] = IntPtrType ();
915 if (cinfo && cinfo->imt_arg && IS_LLVM_MONO_BRANCH) {
917 sinfo->imt_arg_pindex = pindex;
918 param_types [pindex] = IntPtrType ();
922 /* Compute the index in the LLVM signature where the vret arg needs to be passed */
923 vret_arg_pindex = pindex;
924 if (cinfo->vret_arg_index == 1) {
925 /* Add the slots consumed by the first argument */
926 LLVMArgInfo *ainfo = &cinfo->args [0];
927 switch (ainfo->storage) {
928 case LLVMArgVtypeInReg:
929 for (j = 0; j < 2; ++j) {
930 if (ainfo->pair_storage [j] == LLVMArgInIReg)
940 sinfo->vret_arg_pindex = vret_arg_pindex;
943 if (vretaddr && vret_arg_pindex == pindex)
944 param_types [pindex ++] = IntPtrType ();
947 sinfo->this_arg_pindex = pindex;
948 param_types [pindex ++] = IntPtrType ();
950 if (vretaddr && vret_arg_pindex == pindex)
951 param_types [pindex ++] = IntPtrType ();
952 for (i = 0; i < sig->param_count; ++i) {
953 if (vretaddr && vret_arg_pindex == pindex)
954 param_types [pindex ++] = IntPtrType ();
955 pindexes [i] = pindex;
956 if (cinfo && cinfo->args [i + sig->hasthis].storage == LLVMArgVtypeInReg) {
957 for (j = 0; j < 2; ++j) {
958 switch (cinfo->args [i + sig->hasthis].pair_storage [j]) {
960 param_types [pindex ++] = LLVMIntType (sizeof (gpointer) * 8);
965 g_assert_not_reached ();
968 } else if (cinfo && cinfo->args [i + sig->hasthis].storage == LLVMArgVtypeByVal) {
969 param_types [pindex] = type_to_llvm_arg_type (ctx, sig->params [i]);
971 param_types [pindex] = LLVMPointerType (param_types [pindex], 0);
974 param_types [pindex ++] = type_to_llvm_arg_type (ctx, sig->params [i]);
977 if (vretaddr && vret_arg_pindex == pindex)
978 param_types [pindex ++] = IntPtrType ();
982 res = LLVMFunctionType (ret_type, param_types, pindex, FALSE);
983 g_free (param_types);
986 sinfo->pindexes = pindexes;
994 g_free (param_types);
1000 sig_to_llvm_sig (EmitContext *ctx, MonoMethodSignature *sig)
1002 return sig_to_llvm_sig_full (ctx, sig, NULL, NULL);
1006 * LLVMFunctionType1:
1008 * Create an LLVM function type from the arguments.
1010 static G_GNUC_UNUSED LLVMTypeRef
1011 LLVMFunctionType1(LLVMTypeRef ReturnType,
1012 LLVMTypeRef ParamType1,
1015 LLVMTypeRef param_types [1];
1017 param_types [0] = ParamType1;
1019 return LLVMFunctionType (ReturnType, param_types, 1, IsVarArg);
1023 * LLVMFunctionType2:
1025 * Create an LLVM function type from the arguments.
1028 LLVMFunctionType2(LLVMTypeRef ReturnType,
1029 LLVMTypeRef ParamType1,
1030 LLVMTypeRef ParamType2,
1033 LLVMTypeRef param_types [2];
1035 param_types [0] = ParamType1;
1036 param_types [1] = ParamType2;
1038 return LLVMFunctionType (ReturnType, param_types, 2, IsVarArg);
1042 * LLVMFunctionType3:
1044 * Create an LLVM function type from the arguments.
1047 LLVMFunctionType3(LLVMTypeRef ReturnType,
1048 LLVMTypeRef ParamType1,
1049 LLVMTypeRef ParamType2,
1050 LLVMTypeRef ParamType3,
1053 LLVMTypeRef param_types [3];
1055 param_types [0] = ParamType1;
1056 param_types [1] = ParamType2;
1057 param_types [2] = ParamType3;
1059 return LLVMFunctionType (ReturnType, param_types, 3, IsVarArg);
1065 * Create an LLVM builder and remember it so it can be freed later.
1067 static LLVMBuilderRef
1068 create_builder (EmitContext *ctx)
1070 LLVMBuilderRef builder = LLVMCreateBuilder ();
1072 ctx->builders = g_slist_prepend_mempool (ctx->cfg->mempool, ctx->builders, builder);
1078 get_plt_entry (EmitContext *ctx, LLVMTypeRef llvm_sig, MonoJumpInfoType type, gconstpointer data)
1080 char *callee_name = mono_aot_get_plt_symbol (type, data);
1081 LLVMValueRef callee;
1087 callee = g_hash_table_lookup (ctx->lmodule->plt_entries, callee_name);
1089 callee = LLVMAddFunction (ctx->module, callee_name, llvm_sig);
1091 LLVMSetVisibility (callee, LLVMHiddenVisibility);
1093 g_hash_table_insert (ctx->lmodule->plt_entries, (char*)callee_name, callee);
1100 get_handler_clause (MonoCompile *cfg, MonoBasicBlock *bb)
1102 MonoMethodHeader *header = cfg->header;
1103 MonoExceptionClause *clause;
1107 if (bb->region != -1 && MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))
1108 return (bb->region >> 8) - 1;
1111 for (i = 0; i < header->num_clauses; ++i) {
1112 clause = &header->clauses [i];
1114 if (MONO_OFFSET_IN_CLAUSE (clause, bb->real_offset) && clause->flags == MONO_EXCEPTION_CLAUSE_NONE)
1122 set_metadata_flag (LLVMValueRef v, const char *flag_name)
1124 #if LLVM_CHECK_VERSION (2, 8)
1125 LLVMValueRef md_arg;
1128 if (!IS_LLVM_MONO_BRANCH)
1131 md_kind = LLVMGetMDKindID (flag_name, strlen (flag_name));
1132 md_arg = LLVMMDString ("mono", 4);
1133 LLVMSetMetadata (v, md_kind, LLVMMDNode (&md_arg, 1));
1140 * Emit an LLVM call or invoke instruction depending on whenever the call is inside
1144 emit_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, LLVMValueRef callee, LLVMValueRef *args, int pindex)
1146 MonoCompile *cfg = ctx->cfg;
1148 LLVMBuilderRef builder = *builder_ref;
1151 clause_index = get_handler_clause (cfg, bb);
1153 if (clause_index != -1) {
1154 MonoMethodHeader *header = cfg->header;
1155 MonoExceptionClause *ec = &header->clauses [clause_index];
1156 MonoBasicBlock *tblock;
1157 LLVMBasicBlockRef ex_bb, noex_bb;
1160 * Have to use an invoke instead of a call, branching to the
1161 * handler bblock of the clause containing this bblock.
1164 g_assert (ec->flags == MONO_EXCEPTION_CLAUSE_NONE || ec->flags == MONO_EXCEPTION_CLAUSE_FINALLY);
1166 tblock = cfg->cil_offset_to_bb [ec->handler_offset];
1169 ctx->bblocks [tblock->block_num].invoke_target = TRUE;
1171 ex_bb = get_bb (ctx, tblock);
1173 noex_bb = gen_bb (ctx, "NOEX_BB");
1176 lcall = LLVMBuildInvoke (builder, callee, args, pindex, noex_bb, ex_bb, "");
1178 builder = ctx->builder = create_builder (ctx);
1179 LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
1181 ctx->bblocks [bb->block_num].end_bblock = noex_bb;
1183 lcall = LLVMBuildCall (builder, callee, args, pindex, "");
1184 ctx->builder = builder;
1187 *builder_ref = ctx->builder;
1193 emit_load (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef addr, const char *name, gboolean is_faulting)
1195 const char *intrins_name;
1196 LLVMValueRef args [16], res;
1197 LLVMTypeRef addr_type;
1199 if (is_faulting && bb->region != -1 && IS_LLVM_MONO_BRANCH) {
1201 * We handle loads which can fault by calling a mono specific intrinsic
1202 * using an invoke, so they are handled properly inside try blocks.
1203 * We can't use this outside clauses, since LLVM optimizes intrinsics which
1204 * are marked with IntrReadArgMem.
1208 intrins_name = "llvm.mono.load.i8.p0i8";
1211 intrins_name = "llvm.mono.load.i16.p0i16";
1214 intrins_name = "llvm.mono.load.i32.p0i32";
1217 intrins_name = "llvm.mono.load.i64.p0i64";
1220 g_assert_not_reached ();
1223 addr_type = LLVMTypeOf (addr);
1224 if (addr_type == LLVMPointerType (LLVMDoubleType (), 0) || addr_type == LLVMPointerType (LLVMFloatType (), 0))
1225 addr = LLVMBuildBitCast (*builder_ref, addr, LLVMPointerType (LLVMIntType (size * 8), 0), "");
1228 args [1] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1229 args [2] = LLVMConstInt (LLVMInt1Type (), TRUE, FALSE);
1230 res = emit_call (ctx, bb, builder_ref, LLVMGetNamedFunction (ctx->module, intrins_name), args, 3);
1232 if (addr_type == LLVMPointerType (LLVMDoubleType (), 0))
1233 res = LLVMBuildBitCast (*builder_ref, res, LLVMDoubleType (), "");
1234 else if (addr_type == LLVMPointerType (LLVMFloatType (), 0))
1235 res = LLVMBuildBitCast (*builder_ref, res, LLVMFloatType (), "");
1242 * We emit volatile loads for loads which can fault, because otherwise
1243 * LLVM will generate invalid code when encountering a load from a
1246 res = mono_llvm_build_load (*builder_ref, addr, name, is_faulting);
1248 /* Mark it with a custom metadata */
1251 set_metadata_flag (res, "mono.faulting.load");
1259 emit_store (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef value, LLVMValueRef addr, gboolean is_faulting)
1261 const char *intrins_name;
1262 LLVMValueRef args [16];
1264 if (is_faulting && bb->region != -1 && IS_LLVM_MONO_BRANCH) {
1267 intrins_name = "llvm.mono.store.i8.p0i8";
1270 intrins_name = "llvm.mono.store.i16.p0i16";
1273 intrins_name = "llvm.mono.store.i32.p0i32";
1276 intrins_name = "llvm.mono.store.i64.p0i64";
1279 g_assert_not_reached ();
1282 if (LLVMTypeOf (value) == LLVMDoubleType () || LLVMTypeOf (value) == LLVMFloatType ()) {
1283 value = LLVMBuildBitCast (*builder_ref, value, LLVMIntType (size * 8), "");
1284 addr = LLVMBuildBitCast (*builder_ref, addr, LLVMPointerType (LLVMIntType (size * 8), 0), "");
1289 args [2] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1290 args [3] = LLVMConstInt (LLVMInt1Type (), TRUE, FALSE);
1291 emit_call (ctx, bb, builder_ref, LLVMGetNamedFunction (ctx->module, intrins_name), args, 4);
1293 LLVMBuildStore (*builder_ref, value, addr);
1298 * emit_cond_system_exception:
1300 * Emit code to throw the exception EXC_TYPE if the condition CMP is false.
1301 * Might set the ctx exception.
1304 emit_cond_system_exception (EmitContext *ctx, MonoBasicBlock *bb, const char *exc_type, LLVMValueRef cmp)
1306 LLVMBasicBlockRef ex_bb, noex_bb;
1307 LLVMBuilderRef builder;
1308 MonoClass *exc_class;
1309 LLVMValueRef args [2];
1311 ex_bb = gen_bb (ctx, "EX_BB");
1312 noex_bb = gen_bb (ctx, "NOEX_BB");
1314 LLVMBuildCondBr (ctx->builder, cmp, ex_bb, noex_bb);
1316 exc_class = mono_class_from_name (mono_get_corlib (), "System", exc_type);
1317 g_assert (exc_class);
1319 /* Emit exception throwing code */
1320 builder = create_builder (ctx);
1321 LLVMPositionBuilderAtEnd (builder, ex_bb);
1323 if (!ctx->lmodule->throw_corlib_exception) {
1324 LLVMValueRef callee;
1326 const char *icall_name;
1328 MonoMethodSignature *throw_sig = mono_metadata_signature_alloc (mono_get_corlib (), 2);
1329 throw_sig->ret = &mono_get_void_class ()->byval_arg;
1330 throw_sig->params [0] = &mono_get_int32_class ()->byval_arg;
1331 if (IS_LLVM_MONO_BRANCH) {
1332 icall_name = "llvm_throw_corlib_exception_abs_trampoline";
1333 throw_sig->params [1] = &mono_get_intptr_class ()->byval_arg;
1335 icall_name = "llvm_throw_corlib_exception_trampoline";
1336 throw_sig->params [1] = &mono_get_int32_class ()->byval_arg;
1338 sig = sig_to_llvm_sig (ctx, throw_sig);
1340 if (ctx->cfg->compile_aot) {
1341 callee = get_plt_entry (ctx, sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
1343 callee = LLVMAddFunction (ctx->module, "llvm_throw_corlib_exception_trampoline", sig_to_llvm_sig (ctx, throw_sig));
1346 * Differences between the LLVM/non-LLVM throw corlib exception trampoline:
1347 * - On x86, LLVM generated code doesn't push the arguments
1348 * - When using the LLVM mono branch, the trampoline takes the throw address as an
1349 * arguments, not a pc offset.
1351 LLVMAddGlobalMapping (ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
1354 mono_memory_barrier ();
1355 ctx->lmodule->throw_corlib_exception = callee;
1359 args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token - MONO_TOKEN_TYPE_DEF, FALSE);
1361 args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token, FALSE);
1363 if (IS_LLVM_MONO_BRANCH) {
1365 * The LLVM mono branch contains changes so a block address can be passed as an
1366 * argument to a call.
1368 args [1] = LLVMBuildPtrToInt (builder, LLVMBlockAddress (ctx->lmethod, ex_bb), IntPtrType (), "");
1369 emit_call (ctx, bb, &builder, ctx->lmodule->throw_corlib_exception, args, 2);
1372 * FIXME: The offset is 0, this is only a problem if the code is inside a clause,
1373 * otherwise only the line numbers in stack traces are incorrect.
1375 if (bb->region != -1 && !IS_LLVM_MONO_BRANCH)
1376 LLVM_FAILURE (ctx, "system-ex-in-region");
1378 args [1] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1379 emit_call (ctx, bb, &builder, ctx->lmodule->throw_corlib_exception, args, 2);
1382 LLVMBuildUnreachable (builder);
1384 ctx->builder = create_builder (ctx);
1385 LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
1387 ctx->bblocks [bb->block_num].end_bblock = noex_bb;
1397 * emit_reg_to_vtype:
1399 * Emit code to store the vtype in the registers REGS to the address ADDRESS.
1402 emit_reg_to_vtype (EmitContext *ctx, LLVMBuilderRef builder, MonoType *t, LLVMValueRef address, LLVMArgInfo *ainfo, LLVMValueRef *regs)
1406 size = get_vtype_size (t);
1408 if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
1409 address = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (LLVMInt8Type (), 0), "");
1412 for (j = 0; j < 2; ++j) {
1413 LLVMValueRef index [2], addr;
1414 int part_size = size > sizeof (gpointer) ? sizeof (gpointer) : size;
1415 LLVMTypeRef part_type;
1417 if (ainfo->pair_storage [j] == LLVMArgNone)
1420 part_type = LLVMIntType (part_size * 8);
1421 if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
1422 index [0] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
1423 addr = LLVMBuildGEP (builder, address, index, 1, "");
1425 index [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1426 index [1] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
1427 addr = LLVMBuildGEP (builder, address, index, 2, "");
1429 switch (ainfo->pair_storage [j]) {
1431 LLVMBuildStore (builder, convert (ctx, regs [j], part_type), LLVMBuildBitCast (ctx->builder, addr, LLVMPointerType (part_type, 0), ""));
1436 g_assert_not_reached ();
1439 size -= sizeof (gpointer);
1444 * emit_vtype_to_reg:
1446 * Emit code to load a vtype at address ADDRESS into registers. Store the registers
1447 * into REGS, and the number of registers into NREGS.
1450 emit_vtype_to_reg (EmitContext *ctx, LLVMBuilderRef builder, MonoType *t, LLVMValueRef address, LLVMArgInfo *ainfo, LLVMValueRef *regs, guint32 *nregs)
1455 size = get_vtype_size (t);
1457 if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
1458 address = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (LLVMInt8Type (), 0), "");
1461 for (j = 0; j < 2; ++j) {
1462 LLVMValueRef index [2], addr;
1463 int partsize = size > sizeof (gpointer) ? sizeof (gpointer) : size;
1465 if (ainfo->pair_storage [j] == LLVMArgNone)
1468 if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
1469 index [0] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
1470 addr = LLVMBuildGEP (builder, address, index, 1, "");
1472 index [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1473 index [1] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
1474 addr = LLVMBuildGEP (builder, address, index, 2, "");
1476 switch (ainfo->pair_storage [j]) {
1478 regs [pindex ++] = convert (ctx, LLVMBuildLoad (builder, LLVMBuildBitCast (ctx->builder, addr, LLVMPointerType (LLVMIntType (partsize * 8), 0), ""), ""), IntPtrType ());
1483 g_assert_not_reached ();
1485 size -= sizeof (gpointer);
1492 build_alloca (EmitContext *ctx, MonoType *t)
1494 MonoClass *k = mono_class_from_mono_type (t);
1497 if (MONO_CLASS_IS_SIMD (ctx->cfg, k))
1500 align = mono_class_min_align (k);
1502 /* Sometimes align is not a power of 2 */
1503 while (mono_is_power_of_two (align) == -1)
1507 * Have to place all alloca's at the end of the entry bb, since otherwise they would
1508 * get executed every time control reaches them.
1510 LLVMPositionBuilder (ctx->alloca_builder, get_bb (ctx, ctx->cfg->bb_entry), ctx->last_alloca);
1512 ctx->last_alloca = mono_llvm_build_alloca (ctx->alloca_builder, type_to_llvm_type (ctx, t), NULL, align, "");
1513 return ctx->last_alloca;
1517 * Put the global into the 'llvm.used' array to prevent it from being optimized away.
1520 mark_as_used (LLVMModuleRef module, LLVMValueRef global)
1522 LLVMTypeRef used_type;
1523 LLVMValueRef used, used_elem;
1525 used_type = LLVMArrayType (LLVMPointerType (LLVMInt8Type (), 0), 1);
1526 used = LLVMAddGlobal (module, used_type, "llvm.used");
1527 used_elem = LLVMConstBitCast (global, LLVMPointerType (LLVMInt8Type (), 0));
1528 LLVMSetInitializer (used, LLVMConstArray (LLVMPointerType (LLVMInt8Type (), 0), &used_elem, 1));
1529 LLVMSetLinkage (used, LLVMAppendingLinkage);
1530 LLVMSetSection (used, "llvm.metadata");
1536 * Emit code to load/convert arguments.
1539 emit_entry_bb (EmitContext *ctx, LLVMBuilderRef builder)
1542 MonoCompile *cfg = ctx->cfg;
1543 MonoMethodSignature *sig = ctx->sig;
1544 LLVMCallInfo *linfo = ctx->linfo;
1547 ctx->alloca_builder = create_builder (ctx);
1550 * Handle indirect/volatile variables by allocating memory for them
1551 * using 'alloca', and storing their address in a temporary.
1553 for (i = 0; i < cfg->num_varinfo; ++i) {
1554 MonoInst *var = cfg->varinfo [i];
1557 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || MONO_TYPE_ISSTRUCT (var->inst_vtype)) {
1558 vtype = type_to_llvm_type (ctx, var->inst_vtype);
1559 CHECK_FAILURE (ctx);
1560 /* Could be already created by an OP_VPHI */
1561 if (!ctx->addresses [var->dreg])
1562 ctx->addresses [var->dreg] = build_alloca (ctx, var->inst_vtype);
1563 ctx->vreg_cli_types [var->dreg] = var->inst_vtype;
1567 for (i = 0; i < sig->param_count; ++i) {
1568 LLVMArgInfo *ainfo = &linfo->args [i + sig->hasthis];
1569 int reg = cfg->args [i + sig->hasthis]->dreg;
1571 if (ainfo->storage == LLVMArgVtypeInReg) {
1572 LLVMValueRef regs [2];
1575 * Emit code to save the argument from the registers to
1576 * the real argument.
1578 pindex = ctx->pindexes [i];
1579 regs [0] = LLVMGetParam (ctx->lmethod, pindex);
1580 if (ainfo->pair_storage [1] != LLVMArgNone)
1581 regs [1] = LLVMGetParam (ctx->lmethod, pindex + 1);
1585 ctx->addresses [reg] = build_alloca (ctx, sig->params [i]);
1587 emit_reg_to_vtype (ctx, builder, sig->params [i], ctx->addresses [reg], ainfo, regs);
1589 if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (sig->params [i]))) {
1590 /* Treat these as normal values */
1591 ctx->values [reg] = LLVMBuildLoad (builder, ctx->addresses [reg], "");
1593 } else if (ainfo->storage == LLVMArgVtypeByVal) {
1594 ctx->addresses [reg] = LLVMGetParam (ctx->lmethod, ctx->pindexes [i]);
1596 if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (sig->params [i]))) {
1597 /* Treat these as normal values */
1598 ctx->values [reg] = LLVMBuildLoad (builder, ctx->addresses [reg], "");
1601 ctx->values [reg] = convert (ctx, ctx->values [reg], llvm_type_to_stack_type (type_to_llvm_type (ctx, sig->params [i])));
1606 emit_volatile_store (ctx, cfg->vret_addr->dreg);
1608 emit_volatile_store (ctx, cfg->args [0]->dreg);
1609 for (i = 0; i < sig->param_count; ++i)
1610 if (!MONO_TYPE_ISSTRUCT (sig->params [i]))
1611 emit_volatile_store (ctx, cfg->args [i + sig->hasthis]->dreg);
1613 if (sig->hasthis && !cfg->rgctx_var && cfg->generic_sharing_context) {
1614 LLVMValueRef this_alloc;
1617 * The exception handling code needs the location where the this argument was
1618 * stored for gshared methods. We create a separate alloca to hold it, and mark it
1619 * with the "mono.this" custom metadata to tell llvm that it needs to save its
1620 * location into the LSDA.
1622 this_alloc = mono_llvm_build_alloca (builder, IntPtrType (), LLVMConstInt (LLVMInt32Type (), 1, FALSE), 0, "");
1623 /* This volatile store will keep the alloca alive */
1624 mono_llvm_build_store (builder, ctx->values [cfg->args [0]->dreg], this_alloc, TRUE);
1626 set_metadata_flag (this_alloc, "mono.this");
1629 if (cfg->rgctx_var) {
1630 LLVMValueRef rgctx_alloc, store;
1633 * We handle the rgctx arg similarly to the this pointer.
1635 g_assert (ctx->addresses [cfg->rgctx_var->dreg]);
1636 rgctx_alloc = ctx->addresses [cfg->rgctx_var->dreg];
1637 /* This volatile store will keep the alloca alive */
1638 store = mono_llvm_build_store (builder, ctx->rgctx_arg, rgctx_alloc, TRUE);
1640 set_metadata_flag (rgctx_alloc, "mono.this");
1644 * For finally clauses, create an indicator variable telling OP_ENDFINALLY whenever
1645 * it needs to continue normally, or return back to the exception handling system.
1647 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1648 if (bb->region != -1 && (bb->flags & BB_EXCEPTION_HANDLER))
1649 g_hash_table_insert (ctx->region_to_handler, GUINT_TO_POINTER (mono_get_block_region_notry (cfg, bb->region)), bb);
1650 if (bb->region != -1 && (bb->flags & BB_EXCEPTION_HANDLER) && bb->in_scount == 0) {
1654 sprintf (name, "finally_ind_bb%d", bb->block_num);
1655 val = LLVMBuildAlloca (builder, LLVMInt32Type (), name);
1656 LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), val);
1658 ctx->bblocks [bb->block_num].finally_ind = val;
1661 * Create a new bblock which CALL_HANDLER can branch to, because branching to the
1662 * LLVM bblock containing the call to llvm.eh.selector causes problems for the
1663 * LLVM optimizer passes.
1665 sprintf (name, "BB_%d_CALL_HANDLER_TARGET", bb->block_num);
1666 ctx->bblocks [bb->block_num].call_handler_target_bb = LLVMAppendBasicBlock (ctx->lmethod, name);
1674 /* Have to export this for AOT */
1676 mono_personality (void);
1679 mono_personality (void)
1682 g_assert_not_reached ();
1686 process_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, MonoInst *ins)
1688 MonoCompile *cfg = ctx->cfg;
1689 LLVMModuleRef module = ctx->module;
1690 LLVMValueRef *values = ctx->values;
1691 LLVMValueRef *addresses = ctx->addresses;
1692 MonoCallInst *call = (MonoCallInst*)ins;
1693 MonoMethodSignature *sig = call->signature;
1694 LLVMValueRef callee, lcall;
1696 LLVMCallInfo *cinfo;
1700 LLVMTypeRef llvm_sig;
1702 gboolean virtual, calli;
1703 LLVMBuilderRef builder = *builder_ref;
1706 if (call->signature->call_convention != MONO_CALL_DEFAULT)
1707 LLVM_FAILURE (ctx, "non-default callconv");
1709 if (call->rgctx_arg_reg && !IS_LLVM_MONO_BRANCH)
1710 LLVM_FAILURE (ctx, "rgctx reg in call");
1712 if (call->rgctx_reg && !IS_LLVM_MONO_BRANCH) {
1714 * It might be possible to support this by creating a static rgctx trampoline, but
1715 * common_call_trampoline () would patch callsites to call the trampoline, which
1716 * would be incorrect if the rgctx arg is computed dynamically.
1718 LLVM_FAILURE (ctx, "rgctx reg");
1721 cinfo = call->cinfo;
1722 if (call->rgctx_arg_reg)
1723 cinfo->rgctx_arg = TRUE;
1724 if (call->imt_arg_reg)
1725 cinfo->imt_arg = TRUE;
1727 vretaddr = cinfo && cinfo->ret.storage == LLVMArgVtypeRetAddr;
1729 llvm_sig = sig_to_llvm_sig_full (ctx, sig, cinfo, &sinfo);
1730 CHECK_FAILURE (ctx);
1732 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);
1733 calli = (ins->opcode == OP_VOIDCALL_REG || ins->opcode == OP_CALL_REG || ins->opcode == OP_VCALL_REG || ins->opcode == OP_LCALL_REG || ins->opcode == OP_FCALL_REG);
1735 /* FIXME: Avoid creating duplicate methods */
1737 if (ins->flags & MONO_INST_HAS_METHOD) {
1741 if (cfg->compile_aot) {
1742 callee = get_plt_entry (ctx, llvm_sig, MONO_PATCH_INFO_METHOD, call->method);
1744 LLVM_FAILURE (ctx, "can't encode patch");
1746 callee = LLVMAddFunction (module, "", llvm_sig);
1749 mono_create_jit_trampoline_in_domain (mono_domain_get (),
1751 LLVMAddGlobalMapping (ee, callee, target);
1756 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
1762 memset (&ji, 0, sizeof (ji));
1763 ji.type = MONO_PATCH_INFO_JIT_ICALL_ADDR;
1764 ji.data.target = info->name;
1766 target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &ji, FALSE);
1768 if (cfg->compile_aot) {
1769 callee = get_plt_entry (ctx, llvm_sig, MONO_PATCH_INFO_INTERNAL_METHOD, (char*)info->name);
1771 LLVM_FAILURE (ctx, "can't encode patch");
1773 callee = LLVMAddFunction (module, "", llvm_sig);
1774 target = (gpointer)mono_icall_get_wrapper (info);
1775 LLVMAddGlobalMapping (ee, callee, target);
1778 if (cfg->compile_aot) {
1780 if (cfg->abs_patches) {
1781 MonoJumpInfo *abs_ji = g_hash_table_lookup (cfg->abs_patches, call->fptr);
1783 callee = get_plt_entry (ctx, llvm_sig, abs_ji->type, abs_ji->data.target);
1785 LLVM_FAILURE (ctx, "can't encode patch");
1789 LLVM_FAILURE (ctx, "aot");
1791 callee = LLVMAddFunction (module, "", llvm_sig);
1793 if (cfg->abs_patches) {
1794 MonoJumpInfo *abs_ji = g_hash_table_lookup (cfg->abs_patches, call->fptr);
1797 * FIXME: Some trampolines might have
1798 * their own calling convention on some platforms.
1800 #ifndef TARGET_AMD64
1801 if (abs_ji->type == MONO_PATCH_INFO_MONITOR_ENTER || abs_ji->type == MONO_PATCH_INFO_MONITOR_EXIT || abs_ji->type == MONO_PATCH_INFO_GENERIC_CLASS_INIT)
1802 LLVM_FAILURE (ctx, "trampoline with own cconv");
1804 target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, abs_ji, FALSE);
1805 LLVMAddGlobalMapping (ee, callee, target);
1809 LLVMAddGlobalMapping (ee, callee, (gpointer)call->fptr);
1815 int size = sizeof (gpointer);
1818 g_assert (ins->inst_offset % size == 0);
1819 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
1822 * When using the llvm mono branch, we can support IMT directly, otherwise
1823 * we need to call a trampoline.
1825 if (call->method && call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE && !IS_LLVM_MONO_BRANCH) {
1826 #ifdef MONO_ARCH_HAVE_LLVM_IMT_TRAMPOLINE
1827 if (cfg->compile_aot) {
1828 MonoJumpInfoImtTramp *imt_tramp = g_new0 (MonoJumpInfoImtTramp, 1);
1829 imt_tramp->method = call->method;
1830 imt_tramp->vt_offset = call->inst.inst_offset;
1832 callee = get_plt_entry (ctx, llvm_sig, MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE, imt_tramp);
1834 callee = LLVMAddFunction (module, "", llvm_sig);
1835 target = mono_create_llvm_imt_trampoline (cfg->domain, call->method, call->inst.inst_offset);
1836 LLVMAddGlobalMapping (ee, callee, target);
1839 /* No support for passing the IMT argument */
1840 LLVM_FAILURE (ctx, "imt");
1843 callee = convert (ctx, LLVMBuildLoad (builder, LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (LLVMPointerType (IntPtrType (), 0), 0)), &index, 1, ""), ""), LLVMPointerType (llvm_sig, 0));
1846 callee = convert (ctx, values [ins->sreg1], LLVMPointerType (llvm_sig, 0));
1848 if (ins->flags & MONO_INST_HAS_METHOD) {
1853 * Collect and convert arguments
1855 len = sizeof (LLVMValueRef) * ((sig->param_count * 2) + sig->hasthis + vretaddr + call->rgctx_reg);
1856 args = alloca (len);
1857 memset (args, 0, len);
1858 l = call->out_ireg_args;
1860 if (IS_LLVM_MONO_BRANCH) {
1861 if (call->rgctx_arg_reg) {
1862 g_assert (values [call->rgctx_arg_reg]);
1863 args [sinfo.rgctx_arg_pindex] = values [call->rgctx_arg_reg];
1865 if (call->imt_arg_reg) {
1866 g_assert (values [call->imt_arg_reg]);
1867 args [sinfo.imt_arg_pindex] = values [call->imt_arg_reg];
1872 if (!addresses [call->inst.dreg])
1873 addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
1874 args [sinfo.vret_arg_pindex] = LLVMBuildPtrToInt (builder, addresses [call->inst.dreg], IntPtrType (), "");
1877 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1880 LLVMArgInfo *ainfo = call->cinfo ? &call->cinfo->args [i] : NULL;
1884 pindex = sinfo.this_arg_pindex;
1886 pindex = sinfo.pindexes [i - 1];
1888 pindex = sinfo.pindexes [i];
1891 regpair = (guint32)(gssize)(l->data);
1892 reg = regpair & 0xffffff;
1893 args [pindex] = values [reg];
1894 if (ainfo->storage == LLVMArgVtypeInReg) {
1896 LLVMValueRef regs [2];
1901 g_assert (addresses [reg]);
1903 emit_vtype_to_reg (ctx, builder, sig->params [i - sig->hasthis], addresses [reg], ainfo, regs, &nregs);
1904 for (j = 0; j < nregs; ++j)
1905 args [pindex ++] = regs [j];
1908 // FIXME: Get rid of the VMOVE
1909 } else if (ainfo->storage == LLVMArgVtypeByVal) {
1910 g_assert (addresses [reg]);
1911 args [pindex] = addresses [reg];
1913 g_assert (args [pindex]);
1914 if (i == 0 && sig->hasthis)
1915 args [pindex] = convert (ctx, args [pindex], IntPtrType ());
1917 args [pindex] = convert (ctx, args [pindex], type_to_llvm_arg_type (ctx, sig->params [i - sig->hasthis]));
1923 // FIXME: Align call sites
1929 lcall = emit_call (ctx, bb, &builder, callee, args, LLVMCountParamTypes (llvm_sig));
1931 #ifdef LLVM_MONO_BRANCH
1933 * Modify cconv and parameter attributes to pass rgctx/imt correctly.
1935 #if defined(MONO_ARCH_IMT_REG) && defined(MONO_ARCH_RGCTX_REG)
1936 g_assert (MONO_ARCH_IMT_REG == MONO_ARCH_RGCTX_REG);
1938 /* The two can't be used together, so use only one LLVM calling conv to pass them */
1939 g_assert (!(call->rgctx_arg_reg && call->imt_arg_reg));
1940 if (call->rgctx_arg_reg || call->imt_arg_reg)
1941 LLVMSetInstructionCallConv (lcall, LLVMMono1CallConv);
1943 if (call->rgctx_arg_reg)
1944 LLVMAddInstrAttribute (lcall, 1 + sinfo.rgctx_arg_pindex, LLVMInRegAttribute);
1945 if (call->imt_arg_reg)
1946 LLVMAddInstrAttribute (lcall, 1 + sinfo.imt_arg_pindex, LLVMInRegAttribute);
1949 /* Add byval attributes if needed */
1950 for (i = 0; i < sig->param_count; ++i) {
1951 LLVMArgInfo *ainfo = call->cinfo ? &call->cinfo->args [i + sig->hasthis] : NULL;
1953 if (ainfo && ainfo->storage == LLVMArgVtypeByVal) {
1954 LLVMAddInstrAttribute (lcall, 1 + sinfo.pindexes [i], LLVMByValAttribute);
1959 * Convert the result
1961 if (cinfo && cinfo->ret.storage == LLVMArgVtypeInReg) {
1962 LLVMValueRef regs [2];
1964 if (!addresses [ins->dreg])
1965 addresses [ins->dreg] = build_alloca (ctx, sig->ret);
1967 regs [0] = LLVMBuildExtractValue (builder, lcall, 0, "");
1968 if (cinfo->ret.pair_storage [1] != LLVMArgNone)
1969 regs [1] = LLVMBuildExtractValue (builder, lcall, 1, "");
1971 emit_reg_to_vtype (ctx, builder, sig->ret, addresses [ins->dreg], &cinfo->ret, regs);
1972 } else if (sig->ret->type != MONO_TYPE_VOID && !vretaddr) {
1973 /* If the method returns an unsigned value, need to zext it */
1975 values [ins->dreg] = convert_full (ctx, lcall, llvm_type_to_stack_type (type_to_llvm_type (ctx, sig->ret)), type_is_unsigned (ctx, sig->ret));
1978 *builder_ref = ctx->builder;
1980 g_free (sinfo.pindexes);
1988 process_bb (EmitContext *ctx, MonoBasicBlock *bb)
1990 MonoCompile *cfg = ctx->cfg;
1991 MonoMethodSignature *sig = ctx->sig;
1992 LLVMValueRef method = ctx->lmethod;
1993 LLVMValueRef *values = ctx->values;
1994 LLVMValueRef *addresses = ctx->addresses;
1996 LLVMCallInfo *linfo = ctx->linfo;
1997 LLVMModuleRef module = ctx->module;
1998 BBInfo *bblocks = ctx->bblocks;
2000 LLVMBasicBlockRef cbb;
2001 LLVMBuilderRef builder;
2002 gboolean has_terminator;
2004 LLVMValueRef lhs, rhs;
2006 cbb = get_bb (ctx, bb);
2007 builder = create_builder (ctx);
2008 ctx->builder = builder;
2009 LLVMPositionBuilderAtEnd (builder, cbb);
2011 if (bb == cfg->bb_entry)
2012 emit_entry_bb (ctx, builder);
2013 CHECK_FAILURE (ctx);
2015 if (bb->flags & BB_EXCEPTION_HANDLER) {
2017 LLVMValueRef eh_selector, eh_exception, personality, args [4];
2018 LLVMBasicBlockRef target_bb;
2020 static gint32 mapping_inited;
2021 static int ti_generator;
2024 LLVMValueRef type_info;
2027 if (!bblocks [bb->block_num].invoke_target) {
2029 * LLVM asserts if llvm.eh.selector is called from a bblock which
2030 * doesn't have an invoke pointing at it.
2032 LLVM_FAILURE (ctx, "handler without invokes");
2035 eh_selector = LLVMGetNamedFunction (module, eh_selector_name);
2037 if (cfg->compile_aot) {
2038 /* Use a dummy personality function */
2039 personality = LLVMGetNamedFunction (module, "mono_aot_personality");
2040 g_assert (personality);
2042 personality = LLVMGetNamedFunction (module, "mono_personality");
2043 if (InterlockedCompareExchange (&mapping_inited, 1, 0) == 0)
2044 LLVMAddGlobalMapping (ee, personality, mono_personality);
2047 i8ptr = LLVMPointerType (LLVMInt8Type (), 0);
2049 clause_index = (mono_get_block_region_notry (cfg, bb->region) >> 8) - 1;
2052 * Create the type info
2054 sprintf (ti_name, "type_info_%d", ti_generator);
2057 if (cfg->compile_aot) {
2058 /* decode_eh_frame () in aot-runtime.c will decode this */
2059 type_info = LLVMAddGlobal (module, LLVMInt32Type (), ti_name);
2060 LLVMSetInitializer (type_info, LLVMConstInt (LLVMInt32Type (), clause_index, FALSE));
2062 LLVMSetLinkage (type_info, LLVMPrivateLinkage);
2063 LLVMSetVisibility (type_info, LLVMHiddenVisibility);
2066 * Enabling this causes llc to crash:
2067 * http://llvm.org/bugs/show_bug.cgi?id=6102
2069 //LLVM_FAILURE (ctx, "aot+clauses");
2072 * After the cfg mempool is freed, the type info will point to stale memory,
2073 * but this is not a problem, since we decode it once in exception_cb during
2076 ti = mono_mempool_alloc (cfg->mempool, sizeof (gint32));
2077 *(gint32*)ti = clause_index;
2079 type_info = LLVMAddGlobal (module, i8ptr, ti_name);
2081 LLVMAddGlobalMapping (ee, type_info, ti);
2084 args [0] = LLVMConstNull (i8ptr);
2085 args [1] = LLVMConstBitCast (personality, i8ptr);
2086 args [2] = type_info;
2087 LLVMBuildCall (builder, eh_selector, args, 3, "");
2089 /* Store the exception into the exvar */
2090 if (bb->in_scount == 1) {
2091 g_assert (bb->in_scount == 1);
2092 exvar = bb->in_stack [0];
2094 eh_exception = LLVMGetNamedFunction (module, "llvm.eh.exception");
2096 // FIXME: This is shared with filter clauses ?
2097 g_assert (!values [exvar->dreg]);
2098 values [exvar->dreg] = LLVMBuildCall (builder, eh_exception, NULL, 0, "");
2099 emit_volatile_store (ctx, exvar->dreg);
2102 /* Start a new bblock which CALL_HANDLER can branch to */
2103 target_bb = bblocks [bb->block_num].call_handler_target_bb;
2105 LLVMBuildBr (builder, target_bb);
2107 ctx->builder = builder = create_builder (ctx);
2108 LLVMPositionBuilderAtEnd (ctx->builder, target_bb);
2110 ctx->bblocks [bb->block_num].end_bblock = target_bb;
2114 has_terminator = FALSE;
2115 for (ins = bb->code; ins; ins = ins->next) {
2116 const char *spec = LLVM_INS_INFO (ins->opcode);
2118 char dname_buf [128];
2121 /* There could be instructions after a terminator, skip them */
2124 if (spec [MONO_INST_DEST] != ' ' && !MONO_IS_STORE_MEMBASE (ins)) {
2125 sprintf (dname_buf, "t%d", ins->dreg);
2129 if (spec [MONO_INST_SRC1] != ' ' && spec [MONO_INST_SRC1] != 'v') {
2130 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
2132 if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
2133 lhs = emit_volatile_load (ctx, ins->sreg1);
2135 /* It is ok for SETRET to have an uninitialized argument */
2136 if (!values [ins->sreg1] && ins->opcode != OP_SETRET)
2137 LLVM_FAILURE (ctx, "sreg1");
2138 lhs = values [ins->sreg1];
2144 if (spec [MONO_INST_SRC2] != ' ' && spec [MONO_INST_SRC2] != ' ') {
2145 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
2146 if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
2147 rhs = emit_volatile_load (ctx, ins->sreg2);
2149 if (!values [ins->sreg2])
2150 LLVM_FAILURE (ctx, "sreg2");
2151 rhs = values [ins->sreg2];
2157 //mono_print_ins (ins);
2158 switch (ins->opcode) {
2161 case OP_LIVERANGE_START:
2162 case OP_LIVERANGE_END:
2165 values [ins->dreg] = LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE);
2168 #if SIZEOF_VOID_P == 4
2169 values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
2171 values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), (gint64)ins->inst_c0, FALSE);
2175 values [ins->dreg] = LLVMConstReal (LLVMDoubleType (), *(double*)ins->inst_p0);
2178 values [ins->dreg] = LLVMConstFPExt (LLVMConstReal (LLVMFloatType (), *(float*)ins->inst_p0), LLVMDoubleType ());
2181 LLVMBuildBr (builder, get_bb (ctx, ins->inst_target_bb));
2182 has_terminator = TRUE;
2188 LLVMBasicBlockRef new_bb;
2189 LLVMBuilderRef new_builder;
2191 // The default branch is already handled
2192 // FIXME: Handle it here
2194 /* Start new bblock */
2195 sprintf (bb_name, "SWITCH_DEFAULT_BB%d", ctx->default_index ++);
2196 new_bb = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
2198 lhs = convert (ctx, lhs, LLVMInt32Type ());
2199 v = LLVMBuildSwitch (builder, lhs, new_bb, GPOINTER_TO_UINT (ins->klass));
2200 for (i = 0; i < GPOINTER_TO_UINT (ins->klass); ++i) {
2201 MonoBasicBlock *target_bb = ins->inst_many_bb [i];
2203 LLVMAddCase (v, LLVMConstInt (LLVMInt32Type (), i, FALSE), get_bb (ctx, target_bb));
2206 new_builder = create_builder (ctx);
2207 LLVMPositionBuilderAtEnd (new_builder, new_bb);
2208 LLVMBuildUnreachable (new_builder);
2210 has_terminator = TRUE;
2211 g_assert (!ins->next);
2217 if (linfo->ret.storage == LLVMArgVtypeInReg) {
2218 LLVMTypeRef ret_type = LLVMGetReturnType (LLVMGetElementType (LLVMTypeOf (method)));
2219 LLVMValueRef part1, retval;
2222 size = get_vtype_size (sig->ret);
2224 g_assert (addresses [ins->sreg1]);
2226 g_assert (linfo->ret.pair_storage [0] == LLVMArgInIReg);
2227 g_assert (linfo->ret.pair_storage [1] == LLVMArgNone);
2229 part1 = convert (ctx, LLVMBuildLoad (builder, LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMIntType (size * 8), 0), ""), ""), IntPtrType ());
2231 retval = LLVMBuildInsertValue (builder, LLVMGetUndef (ret_type), part1, 0, "");
2233 LLVMBuildRet (builder, retval);
2237 if (linfo->ret.storage == LLVMArgVtypeRetAddr) {
2238 LLVMBuildRetVoid (builder);
2242 if (!lhs || ctx->is_dead [ins->sreg1]) {
2244 * The method did not set its return value, probably because it
2245 * ends with a throw.
2248 LLVMBuildRetVoid (builder);
2250 LLVMBuildRet (builder, LLVMConstNull (type_to_llvm_type (ctx, sig->ret)));
2252 LLVMBuildRet (builder, convert (ctx, lhs, type_to_llvm_type (ctx, sig->ret)));
2254 has_terminator = TRUE;
2260 case OP_ICOMPARE_IMM:
2261 case OP_LCOMPARE_IMM:
2262 case OP_COMPARE_IMM: {
2266 if (ins->next->opcode == OP_NOP)
2269 if (ins->next->opcode == OP_BR)
2270 /* The comparison result is not needed */
2273 rel = mono_opcode_to_cond (ins->next->opcode);
2275 if (ins->opcode == OP_ICOMPARE_IMM) {
2276 lhs = convert (ctx, lhs, LLVMInt32Type ());
2277 rhs = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
2279 if (ins->opcode == OP_LCOMPARE_IMM) {
2280 lhs = convert (ctx, lhs, LLVMInt64Type ());
2281 rhs = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
2283 if (ins->opcode == OP_LCOMPARE) {
2284 lhs = convert (ctx, lhs, LLVMInt64Type ());
2285 rhs = convert (ctx, rhs, LLVMInt64Type ());
2287 if (ins->opcode == OP_ICOMPARE) {
2288 lhs = convert (ctx, lhs, LLVMInt32Type ());
2289 rhs = convert (ctx, rhs, LLVMInt32Type ());
2293 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
2294 rhs = convert (ctx, rhs, LLVMTypeOf (lhs));
2295 else if (LLVMGetTypeKind (LLVMTypeOf (rhs)) == LLVMPointerTypeKind)
2296 lhs = convert (ctx, lhs, LLVMTypeOf (rhs));
2299 /* We use COMPARE+SETcc/Bcc, llvm uses SETcc+br cond */
2300 if (ins->opcode == OP_FCOMPARE)
2301 cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMDoubleType ()), convert (ctx, rhs, LLVMDoubleType ()), "");
2302 else if (ins->opcode == OP_COMPARE_IMM)
2303 cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), "");
2304 else if (ins->opcode == OP_COMPARE)
2305 cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, IntPtrType ()), convert (ctx, rhs, IntPtrType ()), "");
2307 cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], lhs, rhs, "");
2309 if (MONO_IS_COND_BRANCH_OP (ins->next)) {
2310 LLVMBuildCondBr (builder, cmp, get_bb (ctx, ins->next->inst_true_bb), get_bb (ctx, ins->next->inst_false_bb));
2311 has_terminator = TRUE;
2312 } else if (MONO_IS_SETCC (ins->next)) {
2313 sprintf (dname_buf, "t%d", ins->next->dreg);
2315 values [ins->next->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
2317 /* Add stores for volatile variables */
2318 emit_volatile_store (ctx, ins->next->dreg);
2319 } else if (MONO_IS_COND_EXC (ins->next)) {
2320 emit_cond_system_exception (ctx, bb, ins->next->inst_p1, cmp);
2321 CHECK_FAILURE (ctx);
2322 builder = ctx->builder;
2324 LLVM_FAILURE (ctx, "next");
2338 rel = mono_opcode_to_cond (ins->opcode);
2340 cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMDoubleType ()), convert (ctx, rhs, LLVMDoubleType ()), "");
2341 values [ins->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
2349 gboolean empty = TRUE;
2351 /* Check that all input bblocks really branch to us */
2352 for (i = 0; i < bb->in_count; ++i) {
2353 if (bb->in_bb [i]->last_ins && bb->in_bb [i]->last_ins->opcode == OP_NOT_REACHED)
2354 ins->inst_phi_args [i + 1] = -1;
2360 /* LLVM doesn't like phi instructions with zero operands */
2361 ctx->is_dead [ins->dreg] = TRUE;
2365 /* Created earlier, insert it now */
2366 LLVMInsertIntoBuilder (builder, values [ins->dreg]);
2368 for (i = 0; i < ins->inst_phi_args [0]; i++) {
2369 int sreg1 = ins->inst_phi_args [i + 1];
2373 * Count the number of times the incoming bblock branches to us,
2374 * since llvm requires a separate entry for each.
2376 if (bb->in_bb [i]->last_ins && bb->in_bb [i]->last_ins->opcode == OP_SWITCH) {
2377 MonoInst *switch_ins = bb->in_bb [i]->last_ins;
2380 for (j = 0; j < GPOINTER_TO_UINT (switch_ins->klass); ++j) {
2381 if (switch_ins->inst_many_bb [j] == bb)
2388 /* Remember for later */
2389 for (j = 0; j < count; ++j) {
2390 PhiNode *node = mono_mempool_alloc0 (ctx->mempool, sizeof (PhiNode));
2393 node->in_bb = bb->in_bb [i];
2395 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);
2404 values [ins->dreg] = lhs;
2407 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
2410 values [ins->dreg] = lhs;
2412 if (var && var->klass->byval_arg.type == MONO_TYPE_R4) {
2414 * This is added by the spilling pass in case of the JIT,
2415 * but we have to do it ourselves.
2417 values [ins->dreg] = convert (ctx, values [ins->dreg], LLVMFloatType ());
2451 lhs = convert (ctx, lhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
2452 rhs = convert (ctx, rhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
2454 switch (ins->opcode) {
2457 values [ins->dreg] = LLVMBuildAdd (builder, lhs, rhs, dname);
2461 values [ins->dreg] = LLVMBuildSub (builder, lhs, rhs, dname);
2465 values [ins->dreg] = LLVMBuildMul (builder, lhs, rhs, dname);
2469 values [ins->dreg] = LLVMBuildSRem (builder, lhs, rhs, dname);
2473 values [ins->dreg] = LLVMBuildURem (builder, lhs, rhs, dname);
2477 values [ins->dreg] = LLVMBuildSDiv (builder, lhs, rhs, dname);
2481 values [ins->dreg] = LLVMBuildUDiv (builder, lhs, rhs, dname);
2484 values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, dname);
2488 values [ins->dreg] = LLVMBuildAnd (builder, lhs, rhs, dname);
2492 values [ins->dreg] = LLVMBuildOr (builder, lhs, rhs, dname);
2496 values [ins->dreg] = LLVMBuildXor (builder, lhs, rhs, dname);
2500 values [ins->dreg] = LLVMBuildShl (builder, lhs, rhs, dname);
2504 values [ins->dreg] = LLVMBuildAShr (builder, lhs, rhs, dname);
2508 values [ins->dreg] = LLVMBuildLShr (builder, lhs, rhs, dname);
2512 values [ins->dreg] = LLVMBuildFAdd (builder, lhs, rhs, dname);
2515 values [ins->dreg] = LLVMBuildFSub (builder, lhs, rhs, dname);
2518 values [ins->dreg] = LLVMBuildFMul (builder, lhs, rhs, dname);
2522 g_assert_not_reached ();
2529 case OP_IREM_UN_IMM:
2531 case OP_IDIV_UN_IMM:
2537 case OP_ISHR_UN_IMM:
2546 case OP_LSHR_UN_IMM:
2554 if (spec [MONO_INST_SRC1] == 'l') {
2555 imm = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
2557 imm = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
2560 #if SIZEOF_VOID_P == 4
2561 if (ins->opcode == OP_LSHL_IMM || ins->opcode == OP_LSHR_IMM || ins->opcode == OP_LSHR_UN_IMM)
2562 imm = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
2565 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
2566 lhs = convert (ctx, lhs, IntPtrType ());
2567 imm = convert (ctx, imm, LLVMTypeOf (lhs));
2568 switch (ins->opcode) {
2572 values [ins->dreg] = LLVMBuildAdd (builder, lhs, imm, dname);
2576 values [ins->dreg] = LLVMBuildSub (builder, lhs, imm, dname);
2580 values [ins->dreg] = LLVMBuildMul (builder, lhs, imm, dname);
2584 values [ins->dreg] = LLVMBuildSDiv (builder, lhs, imm, dname);
2586 case OP_IDIV_UN_IMM:
2587 case OP_LDIV_UN_IMM:
2588 values [ins->dreg] = LLVMBuildUDiv (builder, lhs, imm, dname);
2592 values [ins->dreg] = LLVMBuildSRem (builder, lhs, imm, dname);
2594 case OP_IREM_UN_IMM:
2595 values [ins->dreg] = LLVMBuildURem (builder, lhs, imm, dname);
2600 values [ins->dreg] = LLVMBuildAnd (builder, lhs, imm, dname);
2604 values [ins->dreg] = LLVMBuildOr (builder, lhs, imm, dname);
2608 values [ins->dreg] = LLVMBuildXor (builder, lhs, imm, dname);
2613 values [ins->dreg] = LLVMBuildShl (builder, lhs, imm, dname);
2618 values [ins->dreg] = LLVMBuildAShr (builder, lhs, imm, dname);
2620 case OP_ISHR_UN_IMM:
2621 /* This is used to implement conv.u4, so the lhs could be an i8 */
2622 lhs = convert (ctx, lhs, LLVMInt32Type ());
2623 imm = convert (ctx, imm, LLVMInt32Type ());
2624 values [ins->dreg] = LLVMBuildLShr (builder, lhs, imm, dname);
2626 case OP_LSHR_UN_IMM:
2627 values [ins->dreg] = LLVMBuildLShr (builder, lhs, imm, dname);
2630 g_assert_not_reached ();
2635 values [ins->dreg] = LLVMBuildSub (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), convert (ctx, lhs, LLVMInt32Type ()), dname);
2638 values [ins->dreg] = LLVMBuildSub (builder, LLVMConstInt (LLVMInt64Type (), 0, FALSE), lhs, dname);
2641 lhs = convert (ctx, lhs, LLVMDoubleType ());
2642 values [ins->dreg] = LLVMBuildFSub (builder, LLVMConstReal (LLVMDoubleType (), 0.0), lhs, dname);
2645 guint32 v = 0xffffffff;
2646 values [ins->dreg] = LLVMBuildXor (builder, LLVMConstInt (LLVMInt32Type (), v, FALSE), lhs, dname);
2650 guint64 v = 0xffffffffffffffffLL;
2651 values [ins->dreg] = LLVMBuildXor (builder, LLVMConstInt (LLVMInt64Type (), v, FALSE), lhs, dname);
2654 #if defined(TARGET_X86) || defined(TARGET_AMD64)
2656 LLVMValueRef v1, v2;
2658 v1 = LLVMBuildMul (builder, convert (ctx, rhs, IntPtrType ()), LLVMConstInt (IntPtrType (), (1 << ins->backend.shift_amount), FALSE), "");
2659 v2 = LLVMBuildAdd (builder, convert (ctx, lhs, IntPtrType ()), v1, "");
2660 values [ins->dreg] = LLVMBuildAdd (builder, v2, LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), dname);
2665 case OP_ICONV_TO_I1:
2666 case OP_ICONV_TO_I2:
2667 case OP_ICONV_TO_I4:
2668 case OP_ICONV_TO_U1:
2669 case OP_ICONV_TO_U2:
2670 case OP_ICONV_TO_U4:
2671 case OP_LCONV_TO_I1:
2672 case OP_LCONV_TO_I2:
2673 case OP_LCONV_TO_U1:
2674 case OP_LCONV_TO_U2:
2675 case OP_LCONV_TO_U4: {
2678 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);
2680 /* Have to do two casts since our vregs have type int */
2681 v = LLVMBuildTrunc (builder, lhs, op_to_llvm_type (ins->opcode), "");
2683 values [ins->dreg] = LLVMBuildSExt (builder, v, LLVMInt32Type (), dname);
2685 values [ins->dreg] = LLVMBuildZExt (builder, v, LLVMInt32Type (), dname);
2688 case OP_ICONV_TO_I8:
2689 values [ins->dreg] = LLVMBuildSExt (builder, lhs, LLVMInt64Type (), dname);
2691 case OP_ICONV_TO_U8:
2692 values [ins->dreg] = LLVMBuildZExt (builder, lhs, LLVMInt64Type (), dname);
2694 case OP_FCONV_TO_I4:
2695 values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, LLVMInt32Type (), dname);
2697 case OP_FCONV_TO_I1:
2698 values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildFPToSI (builder, lhs, LLVMInt8Type (), dname), LLVMInt32Type (), "");
2700 case OP_FCONV_TO_U1:
2701 values [ins->dreg] = LLVMBuildZExt (builder, LLVMBuildFPToUI (builder, lhs, LLVMInt8Type (), dname), LLVMInt32Type (), "");
2703 case OP_FCONV_TO_I2:
2704 values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildFPToSI (builder, lhs, LLVMInt16Type (), dname), LLVMInt32Type (), "");
2706 case OP_FCONV_TO_U2:
2707 values [ins->dreg] = LLVMBuildZExt (builder, LLVMBuildFPToUI (builder, lhs, LLVMInt16Type (), dname), LLVMInt32Type (), "");
2709 case OP_FCONV_TO_I8:
2710 values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, LLVMInt64Type (), dname);
2713 values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, IntPtrType (), dname);
2715 case OP_ICONV_TO_R8:
2716 case OP_LCONV_TO_R8:
2717 values [ins->dreg] = LLVMBuildSIToFP (builder, lhs, LLVMDoubleType (), dname);
2719 case OP_LCONV_TO_R_UN:
2720 values [ins->dreg] = LLVMBuildUIToFP (builder, lhs, LLVMDoubleType (), dname);
2722 #if SIZEOF_VOID_P == 4
2725 case OP_LCONV_TO_I4:
2726 values [ins->dreg] = LLVMBuildTrunc (builder, lhs, LLVMInt32Type (), dname);
2728 case OP_ICONV_TO_R4:
2729 case OP_LCONV_TO_R4:
2730 v = LLVMBuildSIToFP (builder, lhs, LLVMFloatType (), "");
2731 values [ins->dreg] = LLVMBuildFPExt (builder, v, LLVMDoubleType (), dname);
2733 case OP_FCONV_TO_R4:
2734 v = LLVMBuildFPTrunc (builder, lhs, LLVMFloatType (), "");
2735 values [ins->dreg] = LLVMBuildFPExt (builder, v, LLVMDoubleType (), dname);
2738 values [ins->dreg] = LLVMBuildSExt (builder, lhs, LLVMInt64Type (), dname);
2741 values [ins->dreg] = LLVMBuildZExt (builder, lhs, LLVMInt64Type (), dname);
2744 values [ins->dreg] = LLVMBuildTrunc (builder, lhs, LLVMInt32Type (), dname);
2746 case OP_LOCALLOC_IMM: {
2749 guint32 size = ins->inst_imm;
2750 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
2752 v = mono_llvm_build_alloca (builder, LLVMInt8Type (), LLVMConstInt (LLVMInt32Type (), size, FALSE), MONO_ARCH_FRAME_ALIGNMENT, "");
2754 if (ins->flags & MONO_INST_INIT) {
2755 LLVMValueRef args [5];
2758 args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
2759 args [2] = LLVMConstInt (LLVMInt32Type (), size, FALSE);
2760 args [3] = LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT, FALSE);
2761 args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
2762 LLVMBuildCall (builder, LLVMGetNamedFunction (module, memset_func_name), args, memset_param_count, "");
2765 values [ins->dreg] = v;
2769 LLVMValueRef v, size;
2771 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), "");
2773 v = mono_llvm_build_alloca (builder, LLVMInt8Type (), size, MONO_ARCH_FRAME_ALIGNMENT, "");
2775 if (ins->flags & MONO_INST_INIT) {
2776 LLVMValueRef args [5];
2779 args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
2781 args [3] = LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT, FALSE);
2782 args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
2783 LLVMBuildCall (builder, LLVMGetNamedFunction (module, memset_func_name), args, memset_param_count, "");
2785 values [ins->dreg] = v;
2789 case OP_LOADI1_MEMBASE:
2790 case OP_LOADU1_MEMBASE:
2791 case OP_LOADI2_MEMBASE:
2792 case OP_LOADU2_MEMBASE:
2793 case OP_LOADI4_MEMBASE:
2794 case OP_LOADU4_MEMBASE:
2795 case OP_LOADI8_MEMBASE:
2796 case OP_LOADR4_MEMBASE:
2797 case OP_LOADR8_MEMBASE:
2798 case OP_LOAD_MEMBASE:
2806 LLVMValueRef index, addr;
2808 gboolean sext = FALSE, zext = FALSE;
2809 gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
2811 t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
2816 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)) {
2817 addr = LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE);
2818 } else if (ins->inst_offset == 0) {
2819 addr = values [ins->inst_basereg];
2820 } else if (ins->inst_offset % size != 0) {
2821 /* Unaligned load */
2822 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
2823 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
2825 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
2826 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (t, 0)), &index, 1, "");
2829 addr = convert (ctx, addr, LLVMPointerType (t, 0));
2831 values [ins->dreg] = emit_load (ctx, bb, &builder, size, addr, dname, is_volatile);
2833 if (!is_volatile && (ins->flags & MONO_INST_CONSTANT_LOAD)) {
2835 * These will signal LLVM that these loads do not alias any stores, and
2836 * they can't fail, allowing them to be hoisted out of loops.
2838 set_metadata_flag (values [ins->dreg], "mono.noalias");
2839 set_metadata_flag (values [ins->dreg], "mono.nofail.load");
2843 values [ins->dreg] = LLVMBuildSExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
2845 values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
2846 else if (ins->opcode == OP_LOADR4_MEMBASE)
2847 values [ins->dreg] = LLVMBuildFPExt (builder, values [ins->dreg], LLVMDoubleType (), dname);
2851 case OP_STOREI1_MEMBASE_REG:
2852 case OP_STOREI2_MEMBASE_REG:
2853 case OP_STOREI4_MEMBASE_REG:
2854 case OP_STOREI8_MEMBASE_REG:
2855 case OP_STORER4_MEMBASE_REG:
2856 case OP_STORER8_MEMBASE_REG:
2857 case OP_STORE_MEMBASE_REG: {
2859 LLVMValueRef index, addr;
2861 gboolean sext = FALSE, zext = FALSE;
2862 gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
2864 if (!values [ins->inst_destbasereg])
2865 LLVM_FAILURE (ctx, "inst_destbasereg");
2867 t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
2869 if (ins->inst_offset % size != 0) {
2870 /* Unaligned store */
2871 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
2872 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
2874 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
2875 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, "");
2877 emit_store (ctx, bb, &builder, size, convert (ctx, values [ins->sreg1], t), convert (ctx, addr, LLVMPointerType (t, 0)), is_volatile);
2881 case OP_STOREI1_MEMBASE_IMM:
2882 case OP_STOREI2_MEMBASE_IMM:
2883 case OP_STOREI4_MEMBASE_IMM:
2884 case OP_STOREI8_MEMBASE_IMM:
2885 case OP_STORE_MEMBASE_IMM: {
2887 LLVMValueRef index, addr;
2889 gboolean sext = FALSE, zext = FALSE;
2890 gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
2892 t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
2894 if (ins->inst_offset % size != 0) {
2895 /* Unaligned store */
2896 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
2897 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
2899 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
2900 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, "");
2902 emit_store (ctx, bb, &builder, size, convert (ctx, LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), t), addr, is_volatile);
2907 emit_load (ctx, bb, &builder, sizeof (gpointer), convert (ctx, values [ins->sreg1], LLVMPointerType (IntPtrType (), 0)), "", TRUE);
2909 case OP_OUTARG_VTRETADDR:
2916 case OP_VOIDCALL_MEMBASE:
2917 case OP_CALL_MEMBASE:
2918 case OP_LCALL_MEMBASE:
2919 case OP_FCALL_MEMBASE:
2920 case OP_VCALL_MEMBASE:
2921 case OP_VOIDCALL_REG:
2925 case OP_VCALL_REG: {
2926 process_call (ctx, bb, &builder, ins);
2927 CHECK_FAILURE (ctx);
2932 LLVMValueRef indexes [2];
2934 LLVMValueRef got_entry_addr;
2937 * FIXME: Can't allocate from the cfg mempool since that is freed if
2938 * the LLVM compile fails.
2940 ji = g_new0 (MonoJumpInfo, 1);
2941 ji->type = (MonoJumpInfoType)ins->inst_i1;
2942 ji->data.target = ins->inst_p0;
2944 ji = mono_aot_patch_info_dup (ji);
2946 ji->next = cfg->patch_info;
2947 cfg->patch_info = ji;
2949 //mono_add_patch_info (cfg, 0, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2950 got_offset = mono_aot_get_got_offset (cfg->patch_info);
2952 indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2953 indexes [1] = LLVMConstInt (LLVMInt32Type (), (gssize)got_offset, FALSE);
2954 got_entry_addr = LLVMBuildGEP (builder, ctx->lmodule->got_var, indexes, 2, "");
2956 // FIXME: This doesn't work right now, because it must be
2957 // paired with an invariant.end, and even then, its only in effect
2958 // inside its basic block
2961 LLVMValueRef args [3];
2962 LLVMValueRef ptr, val;
2964 ptr = LLVMBuildBitCast (builder, got_entry_addr, LLVMPointerType (LLVMInt8Type (), 0), "ptr");
2966 args [0] = LLVMConstInt (LLVMInt64Type (), sizeof (gpointer), FALSE);
2968 val = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.invariant.start"), args, 2, "");
2972 values [ins->dreg] = LLVMBuildLoad (builder, got_entry_addr, dname);
2975 case OP_NOT_REACHED:
2976 LLVMBuildUnreachable (builder);
2977 has_terminator = TRUE;
2978 g_assert (bb->block_num < cfg->max_block_num);
2979 ctx->unreachable [bb->block_num] = TRUE;
2980 /* Might have instructions after this */
2982 MonoInst *next = ins->next;
2984 * FIXME: If later code uses the regs defined by these instructions,
2985 * compilation will fail.
2987 MONO_DELETE_INS (bb, next);
2991 MonoInst *var = ins->inst_p0;
2993 values [ins->dreg] = addresses [var->dreg];
2997 LLVMValueRef args [1];
2999 args [0] = convert (ctx, lhs, LLVMDoubleType ());
3000 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.sin.f64"), args, 1, dname);
3004 LLVMValueRef args [1];
3006 args [0] = convert (ctx, lhs, LLVMDoubleType ());
3007 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.cos.f64"), args, 1, dname);
3011 LLVMValueRef args [1];
3014 * LLVM optimizes sqrt(nan) into undefined in
3015 * lib/Analysis/ConstantFolding.cpp
3016 * Also, sqrt(NegativeInfinity) is optimized into 0.
3018 LLVM_FAILURE (ctx, "sqrt");
3020 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.sqrt.f64"), args, 1, dname);
3024 LLVMValueRef args [1];
3027 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "fabs"), args, 1, dname);
3041 lhs = convert (ctx, lhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
3042 rhs = convert (ctx, rhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
3044 switch (ins->opcode) {
3047 v = LLVMBuildICmp (builder, LLVMIntSLE, lhs, rhs, "");
3051 v = LLVMBuildICmp (builder, LLVMIntSGE, lhs, rhs, "");
3055 v = LLVMBuildICmp (builder, LLVMIntULE, lhs, rhs, "");
3059 v = LLVMBuildICmp (builder, LLVMIntUGE, lhs, rhs, "");
3062 g_assert_not_reached ();
3065 values [ins->dreg] = LLVMBuildSelect (builder, v, lhs, rhs, dname);
3068 case OP_ATOMIC_EXCHANGE_I4: {
3069 LLVMValueRef args [2];
3071 g_assert (ins->inst_offset == 0);
3073 args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt32Type (), 0));
3075 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.swap.i32.p0i32"), args, 2, dname);
3078 case OP_ATOMIC_EXCHANGE_I8: {
3079 LLVMValueRef args [2];
3081 g_assert (ins->inst_offset == 0);
3083 args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt64Type (), 0));
3084 args [1] = convert (ctx, rhs, LLVMInt64Type ());
3085 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.swap.i64.p0i64"), args, 2, dname);
3088 case OP_ATOMIC_ADD_NEW_I4: {
3089 LLVMValueRef args [2];
3091 g_assert (ins->inst_offset == 0);
3093 args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt32Type (), 0));
3095 values [ins->dreg] = LLVMBuildAdd (builder, LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.load.add.i32.p0i32"), args, 2, ""), args [1], dname);
3098 case OP_ATOMIC_ADD_NEW_I8: {
3099 LLVMValueRef args [2];
3101 g_assert (ins->inst_offset == 0);
3103 args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt64Type (), 0));
3104 args [1] = convert (ctx, rhs, LLVMInt64Type ());
3105 values [ins->dreg] = LLVMBuildAdd (builder, LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.load.add.i64.p0i64"), args, 2, ""), args [1], dname);
3108 case OP_ATOMIC_CAS_I4:
3109 case OP_ATOMIC_CAS_I8: {
3110 LLVMValueRef args [3];
3112 const char *intrins;
3114 if (ins->opcode == OP_ATOMIC_CAS_I4) {
3115 t = LLVMInt32Type ();
3116 intrins = "llvm.atomic.cmp.swap.i32.p0i32";
3118 t = LLVMInt64Type ();
3119 intrins = "llvm.atomic.cmp.swap.i64.p0i64";
3122 args [0] = convert (ctx, lhs, LLVMPointerType (t, 0));
3124 args [1] = convert (ctx, values [ins->sreg3], t);
3126 args [2] = convert (ctx, values [ins->sreg2], t);
3127 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, intrins), args, 3, dname);
3130 case OP_MEMORY_BARRIER: {
3131 LLVMValueRef args [5];
3134 /* Not yet supported by llc on arm */
3135 LLVM_FAILURE (ctx, "memory-barrier+arm");
3138 for (i = 0; i < 5; ++i)
3139 args [i] = LLVMConstInt (LLVMInt1Type (), TRUE, TRUE);
3141 LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.memory.barrier"), args, 5, "");
3144 case OP_RELAXED_NOP: {
3145 #if defined(TARGET_AMD64) || defined(TARGET_X86)
3146 /* No way to get LLVM to emit this */
3147 LLVM_FAILURE (ctx, "relaxed_nop");
3153 #if defined(TARGET_AMD64) || defined(TARGET_X86)
3155 // 257 == FS segment register
3156 LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 257);
3158 // 256 == GS segment register
3159 LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 256);
3163 values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildIntToPtr (builder, LLVMConstInt (IntPtrType (), ins->inst_offset, TRUE), ptrtype, ""), "");
3165 LLVM_FAILURE (ctx, "opcode tls-get");
3175 case OP_IADD_OVF_UN:
3177 case OP_ISUB_OVF_UN:
3179 case OP_IMUL_OVF_UN:
3180 #if SIZEOF_VOID_P == 8
3182 case OP_LADD_OVF_UN:
3184 case OP_LSUB_OVF_UN:
3186 case OP_LMUL_OVF_UN:
3189 LLVMValueRef args [2], val, ovf, func;
3191 args [0] = convert (ctx, lhs, op_to_llvm_type (ins->opcode));
3192 args [1] = convert (ctx, rhs, op_to_llvm_type (ins->opcode));
3193 func = LLVMGetNamedFunction (module, ovf_op_to_intrins (ins->opcode));
3195 val = LLVMBuildCall (builder, func, args, 2, "");
3196 values [ins->dreg] = LLVMBuildExtractValue (builder, val, 0, dname);
3197 ovf = LLVMBuildExtractValue (builder, val, 1, "");
3198 emit_cond_system_exception (ctx, bb, "OverflowException", ovf);
3199 CHECK_FAILURE (ctx);
3200 builder = ctx->builder;
3206 * We currently model them using arrays. Promotion to local vregs is
3207 * disabled for them in mono_handle_global_vregs () in the LLVM case,
3208 * so we always have an entry in cfg->varinfo for them.
3209 * FIXME: Is this needed ?
3212 MonoClass *klass = ins->klass;
3213 LLVMValueRef args [5];
3217 LLVM_FAILURE (ctx, "!klass");
3221 if (!addresses [ins->dreg])
3222 addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
3223 args [0] = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
3224 args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
3225 args [2] = LLVMConstInt (LLVMInt32Type (), mono_class_value_size (klass, NULL), FALSE);
3227 args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
3228 args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
3229 LLVMBuildCall (builder, LLVMGetNamedFunction (module, memset_func_name), args, memset_param_count, "");
3233 case OP_STOREV_MEMBASE:
3234 case OP_LOADV_MEMBASE:
3236 MonoClass *klass = ins->klass;
3237 LLVMValueRef src, dst, args [5];
3238 gboolean done = FALSE;
3242 LLVM_FAILURE (ctx, "!klass");
3246 switch (ins->opcode) {
3247 case OP_STOREV_MEMBASE:
3248 if (cfg->gen_write_barriers && klass->has_references && ins->inst_destbasereg != cfg->frame_reg) {
3249 /* FIXME: Emit write barriers like in mini_emit_stobj () */
3250 LLVM_FAILURE (ctx, "storev_membase + write barriers");
3253 if (!addresses [ins->sreg1]) {
3255 g_assert (values [ins->sreg1]);
3256 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));
3257 LLVMBuildStore (builder, values [ins->sreg1], dst);
3260 src = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMInt8Type (), 0), "");
3261 dst = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_destbasereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (LLVMInt8Type (), 0));
3264 case OP_LOADV_MEMBASE:
3265 if (!addresses [ins->dreg])
3266 addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
3267 src = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_basereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (LLVMInt8Type (), 0));
3268 dst = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
3271 if (!addresses [ins->sreg1])
3272 addresses [ins->sreg1] = build_alloca (ctx, &klass->byval_arg);
3273 if (!addresses [ins->dreg])
3274 addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
3275 src = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMInt8Type (), 0), "");
3276 dst = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
3279 g_assert_not_reached ();
3281 CHECK_FAILURE (ctx);
3288 args [2] = LLVMConstInt (LLVMInt32Type (), mono_class_value_size (klass, NULL), FALSE);
3289 args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
3291 args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
3292 args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
3293 LLVMBuildCall (builder, LLVMGetNamedFunction (module, memcpy_func_name), args, memcpy_param_count, "");
3296 case OP_LLVM_OUTARG_VT:
3297 if (!addresses [ins->sreg1]) {
3298 addresses [ins->sreg1] = build_alloca (ctx, &ins->klass->byval_arg);
3299 g_assert (values [ins->sreg1]);
3300 LLVMBuildStore (builder, values [ins->sreg1], addresses [ins->sreg1]);
3302 addresses [ins->dreg] = addresses [ins->sreg1];
3308 #if defined(TARGET_X86) || defined(TARGET_AMD64)
3310 values [ins->dreg] = LLVMConstNull (type_to_llvm_type (ctx, &ins->klass->byval_arg));
3313 case OP_LOADX_MEMBASE: {
3314 LLVMTypeRef t = type_to_llvm_type (ctx, &ins->klass->byval_arg);
3317 src = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_basereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (t, 0));
3318 values [ins->dreg] = mono_llvm_build_aligned_load (builder, src, "", FALSE, 1);
3325 values [ins->dreg] = LLVMBuildAdd (builder, lhs, rhs, "");
3329 values [ins->dreg] = LLVMBuildFAdd (builder, lhs, rhs, "");
3335 values [ins->dreg] = LLVMBuildSub (builder, lhs, rhs, "");
3339 values [ins->dreg] = LLVMBuildFSub (builder, lhs, rhs, "");
3343 values [ins->dreg] = LLVMBuildFMul (builder, lhs, rhs, "");
3347 values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, "");
3350 values [ins->dreg] = LLVMBuildAnd (builder, lhs, rhs, "");
3353 values [ins->dreg] = LLVMBuildOr (builder, lhs, rhs, "");
3356 values [ins->dreg] = LLVMBuildXor (builder, lhs, rhs, "");
3369 switch (ins->opcode) {
3374 t = LLVMVectorType (LLVMInt32Type (), 4);
3375 rt = LLVMVectorType (LLVMFloatType (), 4);
3381 t = LLVMVectorType (LLVMInt64Type (), 2);
3382 rt = LLVMVectorType (LLVMDoubleType (), 2);
3385 t = LLVMInt32Type ();
3386 rt = LLVMInt32Type ();
3387 g_assert_not_reached ();
3390 lhs = LLVMBuildBitCast (builder, lhs, t, "");
3391 rhs = LLVMBuildBitCast (builder, rhs, t, "");
3392 switch (ins->opcode) {
3395 v = LLVMBuildAnd (builder, lhs, rhs, "");
3399 v = LLVMBuildOr (builder, lhs, rhs, "");
3403 v = LLVMBuildXor (builder, lhs, rhs, "");
3407 v = LLVMBuildAnd (builder, rhs, LLVMBuildNot (builder, lhs, ""), "");
3410 values [ins->dreg] = LLVMBuildBitCast (builder, v, rt, "");
3429 LLVMValueRef args [2];
3434 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, simd_op_to_intrins (ins->opcode)), args, 2, dname);
3443 case OP_EXTRACT_U1: {
3445 gboolean zext = FALSE;
3447 t = simd_op_to_llvm_type (ins->opcode);
3449 switch (ins->opcode) {
3461 t = LLVMInt32Type ();
3462 g_assert_not_reached ();
3465 lhs = LLVMBuildBitCast (builder, lhs, t, "");
3466 values [ins->dreg] = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), "");
3468 values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), "");
3477 case OP_EXPAND_R8: {
3478 LLVMTypeRef t = simd_op_to_llvm_type (ins->opcode);
3479 LLVMValueRef mask [16], v;
3481 for (i = 0; i < 16; ++i)
3482 mask [i] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
3484 v = convert (ctx, values [ins->sreg1], LLVMGetElementType (t));
3486 values [ins->dreg] = LLVMBuildInsertElement (builder, LLVMConstNull (t), v, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
3487 values [ins->dreg] = LLVMBuildShuffleVector (builder, values [ins->dreg], LLVMGetUndef (t), LLVMConstVector (mask, LLVMGetVectorSize (t)), "");
3496 * EXCEPTION HANDLING
3498 case OP_IMPLICIT_EXCEPTION:
3499 /* This marks a place where an implicit exception can happen */
3500 if (bb->region != -1)
3501 LLVM_FAILURE (ctx, "implicit-exception");
3505 MonoMethodSignature *throw_sig;
3506 LLVMValueRef callee, arg;
3507 gboolean rethrow = (ins->opcode == OP_RETHROW);
3508 const char *icall_name;
3510 callee = rethrow ? ctx->lmodule->rethrow : ctx->lmodule->throw;
3511 icall_name = rethrow ? "mono_arch_rethrow_exception" : "mono_arch_throw_exception";
3514 throw_sig = mono_metadata_signature_alloc (mono_get_corlib (), 1);
3515 throw_sig->ret = &mono_get_void_class ()->byval_arg;
3516 throw_sig->params [0] = &mono_get_object_class ()->byval_arg;
3517 if (cfg->compile_aot) {
3518 callee = get_plt_entry (ctx, sig_to_llvm_sig (ctx, throw_sig), MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
3520 callee = LLVMAddFunction (module, icall_name, sig_to_llvm_sig (ctx, throw_sig));
3524 * LLVM doesn't push the exception argument, so we need a different
3527 LLVMAddGlobalMapping (ee, callee, resolve_patch (cfg, MONO_PATCH_INFO_INTERNAL_METHOD, rethrow ? "llvm_rethrow_exception_trampoline" : "llvm_throw_exception_trampoline"));
3529 LLVMAddGlobalMapping (ee, callee, resolve_patch (cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
3533 mono_memory_barrier ();
3535 ctx->lmodule->rethrow = callee;
3537 ctx->lmodule->throw = callee;
3539 arg = convert (ctx, values [ins->sreg1], type_to_llvm_type (ctx, &mono_get_object_class ()->byval_arg));
3540 emit_call (ctx, bb, &builder, callee, &arg, 1);
3543 case OP_CALL_HANDLER: {
3545 * We don't 'call' handlers, but instead simply branch to them.
3546 * The code generated by ENDFINALLY will branch back to us.
3548 LLVMBasicBlockRef noex_bb;
3550 BBInfo *info = &bblocks [ins->inst_target_bb->block_num];
3552 bb_list = info->call_handler_return_bbs;
3555 * Set the indicator variable for the finally clause.
3557 lhs = info->finally_ind;
3559 LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), g_slist_length (bb_list) + 1, FALSE), lhs);
3561 /* Branch to the finally clause */
3562 LLVMBuildBr (builder, info->call_handler_target_bb);
3564 noex_bb = gen_bb (ctx, "CALL_HANDLER_CONT_BB");
3565 info->call_handler_return_bbs = g_slist_append_mempool (cfg->mempool, info->call_handler_return_bbs, noex_bb);
3567 builder = ctx->builder = create_builder (ctx);
3568 LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
3570 bblocks [bb->block_num].end_bblock = noex_bb;
3573 case OP_START_HANDLER: {
3576 case OP_ENDFINALLY: {
3577 LLVMBasicBlockRef resume_bb;
3578 MonoBasicBlock *handler_bb;
3579 LLVMValueRef val, switch_ins, callee;
3583 handler_bb = g_hash_table_lookup (ctx->region_to_handler, GUINT_TO_POINTER (mono_get_block_region_notry (cfg, bb->region)));
3584 g_assert (handler_bb);
3585 info = &bblocks [handler_bb->block_num];
3586 lhs = info->finally_ind;
3589 bb_list = info->call_handler_return_bbs;
3591 resume_bb = gen_bb (ctx, "ENDFINALLY_RESUME_BB");
3593 /* Load the finally variable */
3594 val = LLVMBuildLoad (builder, lhs, "");
3596 /* Reset the variable */
3597 LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), lhs);
3599 /* Branch to either resume_bb, or to the bblocks in bb_list */
3600 switch_ins = LLVMBuildSwitch (builder, val, resume_bb, g_slist_length (bb_list));
3602 * The other targets are added at the end to handle OP_CALL_HANDLER
3603 * opcodes processed later.
3605 info->endfinally_switch_ins_list = g_slist_append_mempool (cfg->mempool, info->endfinally_switch_ins_list, switch_ins);
3607 builder = ctx->builder = create_builder (ctx);
3608 LLVMPositionBuilderAtEnd (ctx->builder, resume_bb);
3610 if (ctx->cfg->compile_aot) {
3611 callee = get_plt_entry (ctx, LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE), MONO_PATCH_INFO_INTERNAL_METHOD, "llvm_resume_unwind_trampoline");
3613 callee = LLVMGetNamedFunction (module, "llvm_resume_unwind_trampoline");
3615 LLVMBuildCall (builder, callee, NULL, 0, "");
3617 LLVMBuildUnreachable (builder);
3618 has_terminator = TRUE;
3624 sprintf (reason, "opcode %s", mono_inst_name (ins->opcode));
3625 LLVM_FAILURE (ctx, reason);
3630 /* Convert the value to the type required by phi nodes */
3631 if (spec [MONO_INST_DEST] != ' ' && !MONO_IS_STORE_MEMBASE (ins) && ctx->vreg_types [ins->dreg]) {
3632 if (!values [ins->dreg])
3634 values [ins->dreg] = addresses [ins->dreg];
3636 values [ins->dreg] = convert (ctx, values [ins->dreg], ctx->vreg_types [ins->dreg]);
3639 /* Add stores for volatile variables */
3640 if (spec [MONO_INST_DEST] != ' ' && spec [MONO_INST_DEST] != 'v' && !MONO_IS_STORE_MEMBASE (ins))
3641 emit_volatile_store (ctx, ins->dreg);
3644 if (!has_terminator && bb->next_bb && (bb == cfg->bb_entry || bb->in_count > 0))
3645 LLVMBuildBr (builder, get_bb (ctx, bb->next_bb));
3647 if (bb == cfg->bb_exit && sig->ret->type == MONO_TYPE_VOID)
3648 LLVMBuildRetVoid (builder);
3650 if (bb == cfg->bb_entry)
3651 ctx->last_alloca = LLVMGetLastInstruction (get_bb (ctx, cfg->bb_entry));
3660 * mono_llvm_check_method_supported:
3662 * Do some quick checks to decide whenever cfg->method can be compiled by LLVM, to avoid
3663 * compiling a method twice.
3666 mono_llvm_check_method_supported (MonoCompile *cfg)
3669 MonoMethodHeader *header = cfg->header;
3670 MonoExceptionClause *clause;
3674 if (cfg->generic_sharing_context && !IS_LLVM_MONO_BRANCH) {
3675 /* No way to obtain location info for this/rgctx */
3676 cfg->exception_message = g_strdup ("gshared");
3677 cfg->disable_llvm = TRUE;
3680 if (cfg->method->save_lmf) {
3681 cfg->exception_message = g_strdup ("lmf");
3682 cfg->disable_llvm = TRUE;
3685 if (!LLVM_CHECK_VERSION (2, 8)) {
3687 * FIXME: LLLVM 2.6 no longer seems to generate correct exception info
3690 cfg->exception_message = g_strdup ("clauses");
3691 cfg->disable_llvm = TRUE;
3695 for (i = 0; i < header->num_clauses; ++i) {
3696 clause = &header->clauses [i];
3698 if (i > 0 && clause->try_offset <= header->clauses [i - 1].handler_offset + header->clauses [i - 1].handler_len) {
3700 * FIXME: Some tests still fail with nested clauses.
3702 cfg->exception_message = g_strdup ("nested clauses");
3703 cfg->disable_llvm = TRUE;
3709 if (cfg->method->dynamic) {
3710 cfg->exception_message = g_strdup ("dynamic.");
3711 cfg->disable_llvm = TRUE;
3716 * mono_llvm_emit_method:
3718 * Emit LLVM IL from the mono IL, and compile it to native code using LLVM.
3721 mono_llvm_emit_method (MonoCompile *cfg)
3724 MonoMethodSignature *sig;
3726 LLVMTypeRef method_type;
3727 LLVMValueRef method = NULL, debug_alias = NULL;
3728 char *method_name, *debug_name = NULL;
3729 LLVMValueRef *values;
3730 int i, max_block_num, bb_index;
3731 gboolean last = FALSE;
3732 GPtrArray *phi_values;
3733 LLVMCallInfo *linfo;
3735 LLVMModuleRef module;
3737 GPtrArray *bblock_list;
3738 MonoMethodHeader *header;
3739 MonoExceptionClause *clause;
3743 /* The code below might acquire the loader lock, so use it for global locking */
3744 mono_loader_lock ();
3746 /* Used to communicate with the callbacks */
3747 TlsSetValue (current_cfg_tls_id, cfg);
3749 ctx = g_new0 (EmitContext, 1);
3751 ctx->mempool = cfg->mempool;
3754 * This maps vregs to the LLVM instruction defining them
3756 values = g_new0 (LLVMValueRef, cfg->next_vreg);
3758 * This maps vregs for volatile variables to the LLVM instruction defining their
3761 ctx->addresses = g_new0 (LLVMValueRef, cfg->next_vreg);
3762 ctx->vreg_types = g_new0 (LLVMTypeRef, cfg->next_vreg);
3763 ctx->vreg_cli_types = g_new0 (MonoType*, cfg->next_vreg);
3764 phi_values = g_ptr_array_new ();
3766 * This signals whenever the vreg was defined by a phi node with no input vars
3767 * (i.e. all its input bblocks end with NOT_REACHABLE).
3769 ctx->is_dead = g_new0 (gboolean, cfg->next_vreg);
3770 /* Whenever the bblock is unreachable */
3771 ctx->unreachable = g_new0 (gboolean, cfg->max_block_num);
3773 bblock_list = g_ptr_array_new ();
3775 ctx->values = values;
3776 ctx->region_to_handler = g_hash_table_new (NULL, NULL);
3778 if (cfg->compile_aot) {
3779 ctx->lmodule = &aot_module;
3780 method_name = mono_aot_get_method_name (cfg);
3781 debug_name = mono_aot_get_method_debug_name (cfg);
3784 ctx->lmodule = &jit_module;
3785 method_name = mono_method_full_name (cfg->method, TRUE);
3789 module = ctx->module = ctx->lmodule->module;
3793 static int count = 0;
3796 if (getenv ("LLVM_COUNT")) {
3797 if (count == atoi (getenv ("LLVM_COUNT"))) {
3798 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
3802 if (count > atoi (getenv ("LLVM_COUNT")))
3803 LLVM_FAILURE (ctx, "");
3808 sig = mono_method_signature (cfg->method);
3811 linfo = mono_arch_get_llvm_call_info (cfg, sig);
3813 CHECK_FAILURE (ctx);
3815 if (cfg->rgctx_var) {
3816 if (IS_LLVM_MONO_BRANCH)
3817 linfo->rgctx_arg = TRUE;
3819 LLVM_FAILURE (ctx, "rgctx arg");
3821 method_type = sig_to_llvm_sig_full (ctx, sig, linfo, &sinfo);
3822 CHECK_FAILURE (ctx);
3825 * This maps parameter indexes in the original signature to the indexes in
3826 * the LLVM signature.
3828 ctx->pindexes = sinfo.pindexes;
3830 method = LLVMAddFunction (module, method_name, method_type);
3831 ctx->lmethod = method;
3833 #ifdef LLVM_MONO_BRANCH
3834 if (linfo->rgctx_arg)
3835 LLVMSetFunctionCallConv (method, LLVMMono1CallConv);
3837 LLVMSetLinkage (method, LLVMPrivateLinkage);
3839 if (cfg->method->save_lmf)
3840 LLVM_FAILURE (ctx, "lmf");
3843 LLVM_FAILURE (ctx, "pinvoke signature");
3845 header = cfg->header;
3846 for (i = 0; i < header->num_clauses; ++i) {
3847 clause = &header->clauses [i];
3848 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_NONE)
3849 LLVM_FAILURE (ctx, "non-finally/catch clause.");
3852 if (linfo->rgctx_arg) {
3853 ctx->rgctx_arg = LLVMGetParam (method, sinfo.rgctx_arg_pindex);
3855 * We mark the rgctx parameter with the inreg attribute, which is mapped to
3856 * MONO_ARCH_RGCTX_REG in the Mono calling convention in llvm, i.e.
3857 * CC_X86_64_Mono in X86CallingConv.td.
3859 LLVMAddAttribute (ctx->rgctx_arg, LLVMInRegAttribute);
3860 LLVMSetValueName (ctx->rgctx_arg, "rgctx");
3862 if (cfg->vret_addr) {
3863 values [cfg->vret_addr->dreg] = LLVMGetParam (method, sinfo.vret_arg_pindex);
3864 LLVMSetValueName (values [cfg->vret_addr->dreg], "vret");
3867 values [cfg->args [0]->dreg] = LLVMGetParam (method, sinfo.this_arg_pindex);
3868 LLVMSetValueName (values [cfg->args [0]->dreg], "this");
3871 names = g_new (char *, sig->param_count);
3872 mono_method_get_param_names (cfg->method, (const char **) names);
3874 for (i = 0; i < sig->param_count; ++i) {
3877 values [cfg->args [i + sig->hasthis]->dreg] = LLVMGetParam (method, sinfo.pindexes [i]);
3878 if (names [i] && names [i][0] != '\0')
3879 name = g_strdup_printf ("arg_%s", names [i]);
3881 name = g_strdup_printf ("arg_%d", i);
3882 LLVMSetValueName (values [cfg->args [i + sig->hasthis]->dreg], name);
3884 if (linfo->args [i + sig->hasthis].storage == LLVMArgVtypeByVal)
3885 LLVMAddAttribute (LLVMGetParam (method, sinfo.pindexes [i]), LLVMByValAttribute);
3890 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
3891 max_block_num = MAX (max_block_num, bb->block_num);
3892 ctx->bblocks = bblocks = g_new0 (BBInfo, max_block_num + 1);
3894 /* Add branches between non-consecutive bblocks */
3895 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3896 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
3897 bb->next_bb != bb->last_ins->inst_false_bb) {
3899 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
3900 inst->opcode = OP_BR;
3901 inst->inst_target_bb = bb->last_ins->inst_false_bb;
3902 mono_bblock_add_inst (bb, inst);
3907 * The INDIRECT flag added by OP_LDADDR inhibits optimizations, even if the LDADDR
3908 * was later optimized away, so clear these flags, and add them back for the still
3909 * present OP_LDADDR instructions.
3911 for (i = 0; i < cfg->next_vreg; ++i) {
3914 ins = get_vreg_to_inst (cfg, i);
3915 if (ins && ins != cfg->rgctx_var)
3916 ins->flags &= ~MONO_INST_INDIRECT;
3920 * Make a first pass over the code to precreate PHI nodes/set INDIRECT flags.
3922 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3924 LLVMBuilderRef builder;
3926 char dname_buf[128];
3928 builder = create_builder (ctx);
3930 for (ins = bb->code; ins; ins = ins->next) {
3931 switch (ins->opcode) {
3936 LLVMTypeRef phi_type = llvm_type_to_stack_type (type_to_llvm_type (ctx, &ins->klass->byval_arg));
3938 CHECK_FAILURE (ctx);
3940 if (ins->opcode == OP_VPHI) {
3941 /* Treat valuetype PHI nodes as operating on the address itself */
3942 g_assert (ins->klass);
3943 phi_type = LLVMPointerType (type_to_llvm_type (ctx, &ins->klass->byval_arg), 0);
3947 * Have to precreate these, as they can be referenced by
3948 * earlier instructions.
3950 sprintf (dname_buf, "t%d", ins->dreg);
3952 values [ins->dreg] = LLVMBuildPhi (builder, phi_type, dname);
3954 if (ins->opcode == OP_VPHI)
3955 ctx->addresses [ins->dreg] = values [ins->dreg];
3957 g_ptr_array_add (phi_values, values [ins->dreg]);
3960 * Set the expected type of the incoming arguments since these have
3961 * to have the same type.
3963 for (i = 0; i < ins->inst_phi_args [0]; i++) {
3964 int sreg1 = ins->inst_phi_args [i + 1];
3967 ctx->vreg_types [sreg1] = phi_type;
3972 ((MonoInst*)ins->inst_p0)->flags |= MONO_INST_INDIRECT;
3981 * Create an ordering for bblocks, use the depth first order first, then
3982 * put the exception handling bblocks last.
3984 for (bb_index = 0; bb_index < cfg->num_bblocks; ++bb_index) {
3985 bb = cfg->bblocks [bb_index];
3986 if (!(bb->region != -1 && !MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))) {
3987 g_ptr_array_add (bblock_list, bb);
3988 bblocks [bb->block_num].added = TRUE;
3992 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3993 if (!bblocks [bb->block_num].added)
3994 g_ptr_array_add (bblock_list, bb);
3998 * Second pass: generate code.
4000 for (bb_index = 0; bb_index < bblock_list->len; ++bb_index) {
4001 bb = g_ptr_array_index (bblock_list, bb_index);
4003 if (!(bb == cfg->bb_entry || bb->in_count > 0))
4006 process_bb (ctx, bb);
4007 CHECK_FAILURE (ctx);
4010 /* Add incoming phi values */
4011 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4012 GSList *l, *ins_list;
4014 ins_list = bblocks [bb->block_num].phi_nodes;
4016 for (l = ins_list; l; l = l->next) {
4017 PhiNode *node = l->data;
4018 MonoInst *phi = node->phi;
4019 int sreg1 = node->sreg;
4020 LLVMBasicBlockRef in_bb;
4025 in_bb = get_end_bb (ctx, node->in_bb);
4027 if (ctx->unreachable [node->in_bb->block_num])
4030 g_assert (values [sreg1]);
4032 if (phi->opcode == OP_VPHI) {
4033 g_assert (LLVMTypeOf (ctx->addresses [sreg1]) == LLVMTypeOf (values [phi->dreg]));
4034 LLVMAddIncoming (values [phi->dreg], &ctx->addresses [sreg1], &in_bb, 1);
4036 g_assert (LLVMTypeOf (values [sreg1]) == LLVMTypeOf (values [phi->dreg]));
4037 LLVMAddIncoming (values [phi->dreg], &values [sreg1], &in_bb, 1);
4042 /* Create the SWITCH statements for ENDFINALLY instructions */
4043 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4044 BBInfo *info = &bblocks [bb->block_num];
4046 for (l = info->endfinally_switch_ins_list; l; l = l->next) {
4047 LLVMValueRef switch_ins = l->data;
4048 GSList *bb_list = info->call_handler_return_bbs;
4050 for (i = 0; i < g_slist_length (bb_list); ++i)
4051 LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), i + 1, FALSE), g_slist_nth (bb_list, i)->data);
4055 if (cfg->verbose_level > 1)
4056 mono_llvm_dump_value (method);
4058 mark_as_used (module, method);
4060 if (cfg->compile_aot) {
4061 /* Don't generate native code, keep the LLVM IR */
4063 /* Can't delete the method if it has an alias, so only add it if successful */
4065 debug_alias = LLVMAddAlias (module, LLVMTypeOf (method), method, debug_name);
4066 LLVMSetLinkage (debug_alias, LLVMInternalLinkage);
4067 LLVMSetVisibility (debug_alias, LLVMHiddenVisibility);
4070 if (cfg->compile_aot && cfg->verbose_level)
4071 printf ("%s emitted as %s\n", mono_method_full_name (cfg->method, TRUE), method_name);
4073 //LLVMVerifyFunction(method, 0);
4075 mono_llvm_optimize_method (method);
4077 if (cfg->verbose_level > 1)
4078 mono_llvm_dump_value (method);
4080 cfg->native_code = LLVMGetPointerToGlobal (ee, method);
4082 /* Set by emit_cb */
4083 g_assert (cfg->code_len);
4085 /* FIXME: Free the LLVM IL for the function */
4093 /* Need to add unused phi nodes as they can be referenced by other values */
4094 LLVMBasicBlockRef phi_bb = LLVMAppendBasicBlock (method, "PHI_BB");
4095 LLVMBuilderRef builder;
4097 builder = create_builder (ctx);
4098 LLVMPositionBuilderAtEnd (builder, phi_bb);
4100 for (i = 0; i < phi_values->len; ++i) {
4101 LLVMValueRef v = g_ptr_array_index (phi_values, i);
4102 if (LLVMGetInstructionParent (v) == NULL)
4103 LLVMInsertIntoBuilder (builder, v);
4106 LLVMDeleteFunction (method);
4111 g_free (ctx->addresses);
4112 g_free (ctx->vreg_types);
4113 g_free (ctx->vreg_cli_types);
4114 g_free (ctx->pindexes);
4115 g_free (ctx->is_dead);
4116 g_free (ctx->unreachable);
4117 g_free (debug_name);
4118 g_ptr_array_free (phi_values, TRUE);
4119 g_free (ctx->bblocks);
4120 g_hash_table_destroy (ctx->region_to_handler);
4121 g_free (method_name);
4122 g_ptr_array_free (bblock_list, TRUE);
4124 for (l = ctx->builders; l; l = l->next) {
4125 LLVMBuilderRef builder = l->data;
4126 LLVMDisposeBuilder (builder);
4131 TlsSetValue (current_cfg_tls_id, NULL);
4133 mono_loader_unlock ();
4137 * mono_llvm_emit_call:
4139 * Same as mono_arch_emit_call () for LLVM.
4142 mono_llvm_emit_call (MonoCompile *cfg, MonoCallInst *call)
4145 MonoMethodSignature *sig;
4146 int i, n, stack_size;
4151 sig = call->signature;
4152 n = sig->param_count + sig->hasthis;
4154 call->cinfo = mono_arch_get_llvm_call_info (cfg, sig);
4156 if (cfg->disable_llvm)
4159 if (sig->call_convention == MONO_CALL_VARARG) {
4160 cfg->exception_message = g_strdup ("varargs");
4161 cfg->disable_llvm = TRUE;
4164 for (i = 0; i < n; ++i) {
4167 ainfo = call->cinfo->args + i;
4169 in = call->args [i];
4171 /* Simply remember the arguments */
4172 switch (ainfo->storage) {
4174 MONO_INST_NEW (cfg, ins, OP_MOVE);
4175 ins->dreg = mono_alloc_ireg (cfg);
4176 ins->sreg1 = in->dreg;
4178 case LLVMArgInFPReg:
4179 MONO_INST_NEW (cfg, ins, OP_FMOVE);
4180 ins->dreg = mono_alloc_freg (cfg);
4181 ins->sreg1 = in->dreg;
4183 case LLVMArgVtypeByVal:
4184 case LLVMArgVtypeInReg:
4185 MONO_INST_NEW (cfg, ins, OP_LLVM_OUTARG_VT);
4186 ins->dreg = mono_alloc_ireg (cfg);
4187 ins->sreg1 = in->dreg;
4188 ins->klass = mono_class_from_mono_type (sig->params [i - sig->hasthis]);
4191 call->cinfo = mono_arch_get_llvm_call_info (cfg, sig);
4192 cfg->exception_message = g_strdup ("ainfo->storage");
4193 cfg->disable_llvm = TRUE;
4197 if (!cfg->disable_llvm) {
4198 MONO_ADD_INS (cfg->cbb, ins);
4199 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, 0, FALSE);
4204 static unsigned char*
4205 alloc_cb (LLVMValueRef function, int size)
4209 cfg = TlsGetValue (current_cfg_tls_id);
4213 return mono_domain_code_reserve (cfg->domain, size);
4215 return mono_domain_code_reserve (mono_domain_get (), size);
4220 emitted_cb (LLVMValueRef function, void *start, void *end)
4224 cfg = TlsGetValue (current_cfg_tls_id);
4226 cfg->code_len = (guint8*)end - (guint8*)start;
4230 exception_cb (void *data)
4233 MonoJitExceptionInfo *ei;
4234 guint32 ei_len, i, j, nested_len, nindex;
4235 gpointer *type_info;
4236 int this_reg, this_offset;
4238 cfg = TlsGetValue (current_cfg_tls_id);
4242 * data points to a DWARF FDE structure, convert it to our unwind format and
4244 * An alternative would be to save it directly, and modify our unwinder to work
4247 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);
4249 /* Count nested clauses */
4251 for (i = 0; i < ei_len; ++i) {
4252 for (j = 0; j < ei_len; ++j) {
4253 gint32 cindex1 = *(gint32*)type_info [i];
4254 MonoExceptionClause *clause1 = &cfg->header->clauses [cindex1];
4255 gint32 cindex2 = *(gint32*)type_info [j];
4256 MonoExceptionClause *clause2 = &cfg->header->clauses [cindex2];
4258 if (cindex1 != cindex2 && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset) {
4264 cfg->llvm_ex_info = mono_mempool_alloc0 (cfg->mempool, (ei_len + nested_len) * sizeof (MonoJitExceptionInfo));
4265 cfg->llvm_ex_info_len = ei_len + nested_len;
4266 memcpy (cfg->llvm_ex_info, ei, ei_len * sizeof (MonoJitExceptionInfo));
4267 /* Fill the rest of the information from the type info */
4268 for (i = 0; i < ei_len; ++i) {
4269 gint32 clause_index = *(gint32*)type_info [i];
4270 MonoExceptionClause *clause = &cfg->header->clauses [clause_index];
4272 cfg->llvm_ex_info [i].flags = clause->flags;
4273 cfg->llvm_ex_info [i].data.catch_class = clause->data.catch_class;
4277 * For nested clauses, the LLVM produced exception info associates the try interval with
4278 * the innermost handler, while mono expects it to be associated with all nesting clauses.
4280 /* FIXME: These should be order with the normal clauses */
4282 for (i = 0; i < ei_len; ++i) {
4283 for (j = 0; j < ei_len; ++j) {
4284 gint32 cindex1 = *(gint32*)type_info [i];
4285 MonoExceptionClause *clause1 = &cfg->header->clauses [cindex1];
4286 gint32 cindex2 = *(gint32*)type_info [j];
4287 MonoExceptionClause *clause2 = &cfg->header->clauses [cindex2];
4289 if (cindex1 != cindex2 && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset) {
4291 * The try interval comes from the nested clause, everything else from the
4294 memcpy (&cfg->llvm_ex_info [nindex], &cfg->llvm_ex_info [j], sizeof (MonoJitExceptionInfo));
4295 cfg->llvm_ex_info [nindex].try_start = cfg->llvm_ex_info [i].try_start;
4296 cfg->llvm_ex_info [nindex].try_end = cfg->llvm_ex_info [i].try_end;
4301 g_assert (nindex == ei_len + nested_len);
4302 cfg->llvm_this_reg = this_reg;
4303 cfg->llvm_this_offset = this_offset;
4305 /* type_info [i] is cfg mempool allocated, no need to free it */
4312 add_intrinsics (LLVMModuleRef module)
4314 /* Emit declarations of instrinsics */
4316 LLVMTypeRef memset_params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMInt8Type (), LLVMInt32Type (), LLVMInt32Type (), LLVMInt1Type () };
4318 if (LLVM_CHECK_VERSION(2, 8)) {
4319 memset_param_count = 5;
4320 memset_func_name = "llvm.memset.p0i8.i32";
4322 memset_param_count = 4;
4323 memset_func_name = "llvm.memset.i32";
4325 LLVMAddFunction (module, memset_func_name, LLVMFunctionType (LLVMVoidType (), memset_params, memset_param_count, FALSE));
4329 LLVMTypeRef memcpy_params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMPointerType (LLVMInt8Type (), 0), LLVMInt32Type (), LLVMInt32Type (), LLVMInt1Type () };
4331 if (LLVM_CHECK_VERSION(2, 8)) {
4332 memcpy_param_count = 5;
4333 memcpy_func_name = "llvm.memcpy.p0i8.p0i8.i32";
4335 memcpy_param_count = 4;
4336 memcpy_func_name = "llvm.memcpy.i32";
4339 LLVMAddFunction (module, memcpy_func_name, LLVMFunctionType (LLVMVoidType (), memcpy_params, memcpy_param_count, FALSE));
4343 LLVMTypeRef params [] = { LLVMDoubleType () };
4345 LLVMAddFunction (module, "llvm.sin.f64", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
4346 LLVMAddFunction (module, "llvm.cos.f64", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
4347 LLVMAddFunction (module, "llvm.sqrt.f64", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
4349 /* This isn't an intrinsic, instead llvm seems to special case it by name */
4350 LLVMAddFunction (module, "fabs", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
4354 LLVMTypeRef membar_params [] = { LLVMInt1Type (), LLVMInt1Type (), LLVMInt1Type (), LLVMInt1Type (), LLVMInt1Type () };
4356 LLVMAddFunction (module, "llvm.atomic.swap.i32.p0i32", LLVMFunctionType2 (LLVMInt32Type (), LLVMPointerType (LLVMInt32Type (), 0), LLVMInt32Type (), FALSE));
4357 LLVMAddFunction (module, "llvm.atomic.swap.i64.p0i64", LLVMFunctionType2 (LLVMInt64Type (), LLVMPointerType (LLVMInt64Type (), 0), LLVMInt64Type (), FALSE));
4358 LLVMAddFunction (module, "llvm.atomic.load.add.i32.p0i32", LLVMFunctionType2 (LLVMInt32Type (), LLVMPointerType (LLVMInt32Type (), 0), LLVMInt32Type (), FALSE));
4359 LLVMAddFunction (module, "llvm.atomic.load.add.i64.p0i64", LLVMFunctionType2 (LLVMInt64Type (), LLVMPointerType (LLVMInt64Type (), 0), LLVMInt64Type (), FALSE));
4360 LLVMAddFunction (module, "llvm.atomic.cmp.swap.i32.p0i32", LLVMFunctionType3 (LLVMInt32Type (), LLVMPointerType (LLVMInt32Type (), 0), LLVMInt32Type (), LLVMInt32Type (), FALSE));
4361 LLVMAddFunction (module, "llvm.atomic.cmp.swap.i64.p0i64", LLVMFunctionType3 (LLVMInt64Type (), LLVMPointerType (LLVMInt64Type (), 0), LLVMInt64Type (), LLVMInt64Type (), FALSE));
4362 LLVMAddFunction (module, "llvm.memory.barrier", LLVMFunctionType (LLVMVoidType (), membar_params, 5, FALSE));
4366 LLVMTypeRef ovf_res_i32 [] = { LLVMInt32Type (), LLVMInt1Type () };
4367 LLVMTypeRef ovf_params_i32 [] = { LLVMInt32Type (), LLVMInt32Type () };
4369 LLVMAddFunction (module, "llvm.sadd.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
4370 LLVMAddFunction (module, "llvm.uadd.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
4371 LLVMAddFunction (module, "llvm.ssub.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
4372 LLVMAddFunction (module, "llvm.usub.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
4373 LLVMAddFunction (module, "llvm.smul.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
4374 LLVMAddFunction (module, "llvm.umul.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
4378 LLVMTypeRef ovf_res_i64 [] = { LLVMInt64Type (), LLVMInt1Type () };
4379 LLVMTypeRef ovf_params_i64 [] = { LLVMInt64Type (), LLVMInt64Type () };
4381 LLVMAddFunction (module, "llvm.sadd.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
4382 LLVMAddFunction (module, "llvm.uadd.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
4383 LLVMAddFunction (module, "llvm.ssub.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
4384 LLVMAddFunction (module, "llvm.usub.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
4385 LLVMAddFunction (module, "llvm.smul.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
4386 LLVMAddFunction (module, "llvm.umul.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
4390 LLVMTypeRef struct_ptr = LLVMPointerType (LLVMStructType (NULL, 0, FALSE), 0);
4391 LLVMTypeRef invariant_start_params [] = { LLVMInt64Type (), LLVMPointerType (LLVMInt8Type (), 0) };
4392 LLVMTypeRef invariant_end_params [] = { struct_ptr, LLVMInt64Type (), LLVMPointerType (LLVMInt8Type (), 0) };
4394 LLVMAddFunction (module, "llvm.invariant.start", LLVMFunctionType (struct_ptr, invariant_start_params, 2, FALSE));
4396 LLVMAddFunction (module, "llvm.invariant.end", LLVMFunctionType (LLVMVoidType (), invariant_end_params, 3, FALSE));
4401 LLVMTypeRef arg_types [2];
4402 LLVMTypeRef ret_type;
4404 arg_types [0] = LLVMPointerType (LLVMInt8Type (), 0);
4405 arg_types [1] = LLVMPointerType (LLVMInt8Type (), 0);
4406 if (LLVM_CHECK_VERSION(2, 8)) {
4407 eh_selector_name = "llvm.eh.selector";
4408 ret_type = LLVMInt32Type ();
4410 if (SIZEOF_VOID_P == 8) {
4411 eh_selector_name = "llvm.eh.selector.i64";
4412 ret_type = LLVMInt64Type ();
4414 eh_selector_name = "llvm.eh.selector.i32";
4415 ret_type = LLVMInt32Type ();
4418 LLVMAddFunction (module, eh_selector_name, LLVMFunctionType (ret_type, arg_types, 2, TRUE));
4420 LLVMAddFunction (module, "llvm.eh.exception", LLVMFunctionType (LLVMPointerType (LLVMInt8Type (), 0), NULL, 0, FALSE));
4422 LLVMAddFunction (module, "mono_personality", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
4424 LLVMAddFunction (module, "llvm_resume_unwind_trampoline", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
4427 /* SSE intrinsics */
4429 LLVMTypeRef vector_type, arg_types [2];
4431 vector_type = LLVMVectorType (LLVMInt32Type (), 4);
4432 arg_types [0] = vector_type;
4433 arg_types [1] = vector_type;
4434 LLVMAddFunction (module, "llvm.x86.sse41.pminud", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4435 LLVMAddFunction (module, "llvm.x86.sse41.pmaxud", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4436 LLVMAddFunction (module, "llvm.x86.sse2.pcmpeq.d", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4438 vector_type = LLVMVectorType (LLVMInt16Type (), 8);
4439 arg_types [0] = vector_type;
4440 arg_types [1] = vector_type;
4441 LLVMAddFunction (module, "llvm.x86.sse41.pminuw", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4442 LLVMAddFunction (module, "llvm.x86.sse2.pmins.w", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4443 LLVMAddFunction (module, "llvm.x86.sse41.pmaxuw", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4444 LLVMAddFunction (module, "llvm.x86.sse2.pcmpeq.w", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4446 vector_type = LLVMVectorType (LLVMInt8Type (), 16);
4447 arg_types [0] = vector_type;
4448 arg_types [1] = vector_type;
4449 LLVMAddFunction (module, "llvm.x86.sse2.pminu.b", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4450 LLVMAddFunction (module, "llvm.x86.sse2.pmaxu.b", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4451 LLVMAddFunction (module, "llvm.x86.sse2.pcmpeq.b", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4452 LLVMAddFunction (module, "llvm.x86.sse2.pcmpgt.b", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4454 vector_type = LLVMVectorType (LLVMInt64Type (), 2);
4455 arg_types [0] = vector_type;
4456 arg_types [1] = vector_type;
4457 LLVMAddFunction (module, "llvm.x86.sse41.pcmpeqq", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4459 vector_type = LLVMVectorType (LLVMDoubleType (), 2);
4460 arg_types [0] = vector_type;
4461 arg_types [1] = vector_type;
4462 LLVMAddFunction (module, "llvm.x86.sse2.min.pd", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4463 LLVMAddFunction (module, "llvm.x86.sse2.max.pd", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4465 vector_type = LLVMVectorType (LLVMFloatType (), 4);
4466 arg_types [0] = vector_type;
4467 arg_types [1] = vector_type;
4468 LLVMAddFunction (module, "llvm.x86.sse2.min.ps", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4469 LLVMAddFunction (module, "llvm.x86.sse2.max.ps", LLVMFunctionType (vector_type, arg_types, 2, FALSE));
4472 /* Load/Store intrinsics */
4473 if (IS_LLVM_MONO_BRANCH) {
4474 LLVMTypeRef arg_types [5];
4478 for (i = 1; i <= 8; i *= 2) {
4479 arg_types [0] = LLVMPointerType (LLVMIntType (i * 8), 0);
4480 arg_types [1] = LLVMInt32Type ();
4481 arg_types [2] = LLVMInt1Type ();
4482 sprintf (name, "llvm.mono.load.i%d.p0i%d", i * 8, i * 8);
4483 LLVMAddFunction (module, name, LLVMFunctionType (LLVMIntType (i * 8), arg_types, 3, FALSE));
4485 arg_types [0] = LLVMIntType (i * 8);
4486 arg_types [1] = LLVMPointerType (LLVMIntType (i * 8), 0);
4487 arg_types [2] = LLVMInt32Type ();
4488 arg_types [3] = LLVMInt1Type ();
4489 sprintf (name, "llvm.mono.store.i%d.p0i%d", i * 8, i * 8);
4490 LLVMAddFunction (module, name, LLVMFunctionType (LLVMVoidType (), arg_types, 4, FALSE));
4496 mono_llvm_init (void)
4498 current_cfg_tls_id = TlsAlloc ();
4502 init_jit_module (void)
4504 MonoJitICallInfo *info;
4506 if (jit_module_inited)
4509 mono_loader_lock ();
4511 if (jit_module_inited) {
4512 mono_loader_unlock ();
4516 jit_module.module = LLVMModuleCreateWithName ("mono");
4518 ee = mono_llvm_create_ee (LLVMCreateModuleProviderForExistingModule (jit_module.module), alloc_cb, emitted_cb, exception_cb);
4520 add_intrinsics (jit_module.module);
4522 jit_module.llvm_types = g_hash_table_new (NULL, NULL);
4524 info = mono_find_jit_icall_by_name ("llvm_resume_unwind_trampoline");
4526 LLVMAddGlobalMapping (ee, LLVMGetNamedFunction (jit_module.module, "llvm_resume_unwind_trampoline"), (void*)info->func);
4528 jit_module_inited = TRUE;
4530 mono_loader_unlock ();
4534 mono_llvm_cleanup (void)
4537 mono_llvm_dispose_ee (ee);
4539 if (jit_module.llvm_types)
4540 g_hash_table_destroy (jit_module.llvm_types);
4542 if (aot_module.module)
4543 LLVMDisposeModule (aot_module.module);
4545 LLVMContextDispose (LLVMGetGlobalContext ());
4549 mono_llvm_create_aot_module (const char *got_symbol)
4551 /* Delete previous module */
4552 if (aot_module.plt_entries)
4553 g_hash_table_destroy (aot_module.plt_entries);
4554 if (aot_module.module)
4555 LLVMDisposeModule (aot_module.module);
4557 memset (&aot_module, 0, sizeof (aot_module));
4559 aot_module.module = LLVMModuleCreateWithName ("aot");
4560 aot_module.got_symbol = got_symbol;
4562 add_intrinsics (aot_module.module);
4566 * We couldn't compute the type of the LLVM global representing the got because
4567 * its size is only known after all the methods have been emitted. So create
4568 * a dummy variable, and replace all uses it with the real got variable when
4569 * its size is known in mono_llvm_emit_aot_module ().
4572 LLVMTypeRef got_type = LLVMArrayType (IntPtrType (), 0);
4574 aot_module.got_var = LLVMAddGlobal (aot_module.module, got_type, "mono_dummy_got");
4575 LLVMSetInitializer (aot_module.got_var, LLVMConstNull (got_type));
4578 /* Add a dummy personality function */
4580 LLVMBasicBlockRef lbb;
4581 LLVMBuilderRef lbuilder;
4582 LLVMValueRef personality;
4584 personality = LLVMAddFunction (aot_module.module, "mono_aot_personality", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
4585 LLVMSetLinkage (personality, LLVMPrivateLinkage);
4586 lbb = LLVMAppendBasicBlock (personality, "BB0");
4587 lbuilder = LLVMCreateBuilder ();
4588 LLVMPositionBuilderAtEnd (lbuilder, lbb);
4589 LLVMBuildRetVoid (lbuilder);
4592 aot_module.llvm_types = g_hash_table_new (NULL, NULL);
4593 aot_module.plt_entries = g_hash_table_new (g_str_hash, g_str_equal);
4597 * Emit the aot module into the LLVM bitcode file FILENAME.
4600 mono_llvm_emit_aot_module (const char *filename, int got_size)
4602 LLVMTypeRef got_type;
4603 LLVMValueRef real_got;
4606 * Create the real got variable and replace all uses of the dummy variable with
4609 got_type = LLVMArrayType (IntPtrType (), got_size);
4610 real_got = LLVMAddGlobal (aot_module.module, got_type, aot_module.got_symbol);
4611 LLVMSetInitializer (real_got, LLVMConstNull (got_type));
4612 LLVMSetLinkage (real_got, LLVMInternalLinkage);
4614 mono_llvm_replace_uses_of (aot_module.got_var, real_got);
4616 mark_as_used (aot_module.module, real_got);
4618 /* Delete the dummy got so it doesn't become a global */
4619 LLVMDeleteGlobal (aot_module.got_var);
4625 if (LLVMVerifyModule (aot_module.module, LLVMReturnStatusAction, &verifier_err)) {
4626 g_assert_not_reached ();
4631 LLVMWriteBitcodeToFile (aot_module.module, filename);
4636 - Emit LLVM IR from the mono IR using the LLVM C API.
4637 - The original arch specific code remains, so we can fall back to it if we run
4638 into something we can't handle.
4642 A partial list of issues:
4643 - Handling of opcodes which can throw exceptions.
4645 In the mono JIT, these are implemented using code like this:
4652 push throw_pos - method
4653 call <exception trampoline>
4655 The problematic part is push throw_pos - method, which cannot be represented
4656 in the LLVM IR, since it does not support label values.
4657 -> this can be implemented in AOT mode using inline asm + labels, but cannot
4658 be implemented in JIT mode ?
4659 -> a possible but slower implementation would use the normal exception
4660 throwing code but it would need to control the placement of the throw code
4661 (it needs to be exactly after the compare+branch).
4662 -> perhaps add a PC offset intrinsics ?
4664 - efficient implementation of .ovf opcodes.
4666 These are currently implemented as:
4667 <ins which sets the condition codes>
4670 Some overflow opcodes are now supported by LLVM SVN.
4672 - exception handling, unwinding.
4673 - SSA is disabled for methods with exception handlers
4674 - How to obtain unwind info for LLVM compiled methods ?
4675 -> this is now solved by converting the unwind info generated by LLVM
4677 - LLVM uses the c++ exception handling framework, while we use our home grown
4678 code, and couldn't use the c++ one:
4679 - its not supported under VC++, other exotic platforms.
4680 - it might be impossible to support filter clauses with it.
4684 The trampolines need a predictable call sequence, since they need to disasm
4685 the calling code to obtain register numbers / offsets.
4687 LLVM currently generates this code in non-JIT mode:
4688 mov -0x98(%rax),%eax
4690 Here, the vtable pointer is lost.
4691 -> solution: use one vtable trampoline per class.
4693 - passing/receiving the IMT pointer/RGCTX.
4694 -> solution: pass them as normal arguments ?
4698 LLVM does not allow the specification of argument registers etc. This means
4699 that all calls are made according to the platform ABI.
4701 - passing/receiving vtypes.
4703 Vtypes passed/received in registers are handled by the front end by using
4704 a signature with scalar arguments, and loading the parts of the vtype into those
4707 Vtypes passed on the stack are handled using the 'byval' attribute.
4711 Supported though alloca, we need to emit the load/store code.
4715 The mono JIT uses pointer sized iregs/double fregs, while LLVM uses precisely
4716 typed registers, so we have to keep track of the precise LLVM type of each vreg.
4717 This is made easier because the IR is already in SSA form.
4718 An additional problem is that our IR is not consistent with types, i.e. i32/ia64
4719 types are frequently used incorrectly.
4724 Emit LLVM bytecode into a .bc file, compile it using llc into a .s file, then
4725 append the AOT data structures to that file. For methods which cannot be
4726 handled by LLVM, the normal JIT compiled versions are used.
4729 /* FIXME: Normalize some aspects of the mono IR to allow easier translation, like:
4730 * - each bblock should end with a branch
4731 * - setting the return value, making cfg->ret non-volatile
4732 * - avoid some transformations in the JIT which make it harder for us to generate
4734 * - use pointer types to help optimizations.