Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / mini / mini-llvm.c
index f072ac56c1eb5f3561fbd7802ffbf85b31643771..0b996be7f1838ee8a684725105b03d1d60afe7b5 100644 (file)
@@ -467,6 +467,9 @@ create_llvm_type_for_type (MonoLLVMModule *module, MonoClass *klass)
 static LLVMTypeRef
 type_to_llvm_type (EmitContext *ctx, MonoType *t)
 {
+       if (t->byref)
+               return ThisType ();
+
        t = mini_get_underlying_type (t);
 
        switch (t->type) {
@@ -1226,10 +1229,10 @@ sig_to_llvm_sig_no_cinfo (EmitContext *ctx, MonoMethodSignature *sig)
        int i, pindex;
        MonoType *rtype;
 
-       rtype = mini_get_underlying_type (sig->ret);
-       ret_type = type_to_llvm_type (ctx, rtype);
+       ret_type = type_to_llvm_type (ctx, sig->ret);
        if (!ctx_ok (ctx))
                return NULL;
+       rtype = mini_get_underlying_type (sig->ret);
 
        param_types = g_new0 (LLVMTypeRef, (sig->param_count * 8) + 3);
        pindex = 0;
@@ -1269,10 +1272,10 @@ sig_to_llvm_sig_full (EmitContext *ctx, MonoMethodSignature *sig, LLVMCallInfo *
        if (!cinfo)
                return sig_to_llvm_sig_no_cinfo (ctx, sig);
 
-       rtype = mini_get_underlying_type (sig->ret);
-       ret_type = type_to_llvm_type (ctx, rtype);
+       ret_type = type_to_llvm_type (ctx, sig->ret);
        if (!ctx_ok (ctx))
                return NULL;
+       rtype = mini_get_underlying_type (sig->ret);
 
        switch (cinfo->ret.storage) {
        case LLVMArgVtypeInReg:
@@ -2465,7 +2468,7 @@ emit_get_method (MonoLLVMModule *module)
        func = LLVMAddFunction (lmodule, module->get_method_symbol, LLVMFunctionType1 (rtype, LLVMInt32Type (), FALSE));
        LLVMSetLinkage (func, LLVMExternalLinkage);
        LLVMSetVisibility (func, LLVMHiddenVisibility);
-       LLVMAddFunctionAttr (func, LLVMNoUnwindAttribute);
+       mono_llvm_add_func_attr (func, LLVM_ATTR_NO_UNWIND);
        module->get_method = func;
 
        entry_bb = LLVMAppendBasicBlock (func, "ENTRY");
@@ -2545,7 +2548,7 @@ emit_get_unbox_tramp (MonoLLVMModule *module)
        func = LLVMAddFunction (lmodule, module->get_unbox_tramp_symbol, LLVMFunctionType1 (rtype, LLVMInt32Type (), FALSE));
        LLVMSetLinkage (func, LLVMExternalLinkage);
        LLVMSetVisibility (func, LLVMHiddenVisibility);
-       LLVMAddFunctionAttr (func, LLVMNoUnwindAttribute);
+       mono_llvm_add_func_attr (func, LLVM_ATTR_NO_UNWIND);
        module->get_unbox_tramp = func;
 
        entry_bb = LLVMAppendBasicBlock (func, "ENTRY");
@@ -2596,7 +2599,7 @@ emit_llvm_code_start (MonoLLVMModule *module)
 
        func = LLVMAddFunction (lmodule, "llvm_code_start", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
        LLVMSetLinkage (func, LLVMInternalLinkage);
-       LLVMAddFunctionAttr (func, LLVMNoUnwindAttribute);
+       mono_llvm_add_func_attr (func, LLVM_ATTR_NO_UNWIND);
        module->code_start = func;
        entry_bb = LLVMAppendBasicBlock (func, "ENTRY");
        builder = LLVMCreateBuilder ();
@@ -2635,7 +2638,7 @@ emit_init_icall_wrapper (MonoLLVMModule *module, const char *name, const char *i
                g_assert_not_reached ();
        }
        LLVMSetLinkage (func, LLVMInternalLinkage);
-       LLVMAddFunctionAttr (func, LLVMNoInlineAttribute);
+       mono_llvm_add_func_attr (func, LLVM_ATTR_NO_INLINE);
        mono_llvm_set_preserveall_cc (func);
        entry_bb = LLVMAppendBasicBlock (func, "ENTRY");
        builder = LLVMCreateBuilder ();
@@ -2704,7 +2707,7 @@ emit_llvm_code_end (MonoLLVMModule *module)
 
        func = LLVMAddFunction (lmodule, "llvm_code_end", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
        LLVMSetLinkage (func, LLVMInternalLinkage);
-       LLVMAddFunctionAttr (func, LLVMNoUnwindAttribute);
+       mono_llvm_add_func_attr (func, LLVM_ATTR_NO_UNWIND);
        module->code_end = func;
        entry_bb = LLVMAppendBasicBlock (func, "ENTRY");
        builder = LLVMCreateBuilder ();
@@ -2857,17 +2860,17 @@ emit_unbox_tramp (EmitContext *ctx, const char *method_name, LLVMTypeRef method_
        tramp_name = g_strdup_printf ("ut_%s", method_name);
        tramp = LLVMAddFunction (ctx->module->lmodule, tramp_name, method_type);
        LLVMSetLinkage (tramp, LLVMInternalLinkage);
-       LLVMAddFunctionAttr (tramp, LLVMOptimizeForSizeAttribute);
-       //LLVMAddFunctionAttr (tramp, LLVMNoUnwindAttribute);
+       mono_llvm_add_func_attr (tramp, LLVM_ATTR_OPTIMIZE_FOR_SIZE);
+       //mono_llvm_add_func_attr (tramp, LLVM_ATTR_NO_UNWIND);
        linfo = ctx->linfo;
        // FIXME: Reduce code duplication with mono_llvm_compile_method () etc.
        if (!ctx->llvm_only && ctx->rgctx_arg_pindex != -1)
-               LLVMAddAttribute (LLVMGetParam (tramp, ctx->rgctx_arg_pindex), LLVMInRegAttribute);
+               mono_llvm_add_param_attr (LLVMGetParam (tramp, ctx->rgctx_arg_pindex), LLVM_ATTR_IN_REG);
        if (ctx->cfg->vret_addr) {
                LLVMSetValueName (LLVMGetParam (tramp, linfo->vret_arg_pindex), "vret");
                if (linfo->ret.storage == LLVMArgVtypeByRef) {
-                       LLVMAddAttribute (LLVMGetParam (tramp, linfo->vret_arg_pindex), LLVMStructRetAttribute);
-                       LLVMAddAttribute (LLVMGetParam (tramp, linfo->vret_arg_pindex), LLVMNoAliasAttribute);
+                       mono_llvm_add_param_attr (LLVMGetParam (tramp, linfo->vret_arg_pindex), LLVM_ATTR_STRUCT_RET);
+                       mono_llvm_add_param_attr (LLVMGetParam (tramp, linfo->vret_arg_pindex), LLVM_ATTR_NO_ALIAS);
                }
        }
 
@@ -2889,9 +2892,9 @@ emit_unbox_tramp (EmitContext *ctx, const char *method_name, LLVMTypeRef method_
        }
        call = LLVMBuildCall (builder, method, args, nargs, "");
        if (!ctx->llvm_only && ctx->rgctx_arg_pindex != -1)
-               LLVMAddInstrAttribute (call, 1 + ctx->rgctx_arg_pindex, LLVMInRegAttribute);
+               mono_llvm_add_instr_attr (call, 1 + ctx->rgctx_arg_pindex, LLVM_ATTR_IN_REG);
        if (linfo->ret.storage == LLVMArgVtypeByRef)
-               LLVMAddInstrAttribute (call, 1 + linfo->vret_arg_pindex, LLVMStructRetAttribute);
+               mono_llvm_add_instr_attr (call, 1 + linfo->vret_arg_pindex, LLVM_ATTR_STRUCT_RET);
 
        // FIXME: This causes assertions in clang
        //mono_llvm_set_must_tail (call);
@@ -3541,18 +3544,18 @@ process_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref,
                mono_llvm_set_call_preserveall_cc (lcall);
 
        if (cinfo->ret.storage == LLVMArgVtypeByRef)
-               LLVMAddInstrAttribute (lcall, 1 + cinfo->vret_arg_pindex, LLVMStructRetAttribute);
+               mono_llvm_add_instr_attr (lcall, 1 + cinfo->vret_arg_pindex, LLVM_ATTR_STRUCT_RET);
        if (!ctx->llvm_only && call->rgctx_arg_reg)
-               LLVMAddInstrAttribute (lcall, 1 + cinfo->rgctx_arg_pindex, LLVMInRegAttribute);
+               mono_llvm_add_instr_attr (lcall, 1 + cinfo->rgctx_arg_pindex, LLVM_ATTR_IN_REG);
        if (call->imt_arg_reg)
-               LLVMAddInstrAttribute (lcall, 1 + cinfo->imt_arg_pindex, LLVMInRegAttribute);
+               mono_llvm_add_instr_attr (lcall, 1 + cinfo->imt_arg_pindex, LLVM_ATTR_IN_REG);
 
        /* Add byval attributes if needed */
        for (i = 0; i < sig->param_count; ++i) {
                LLVMArgInfo *ainfo = &call->cinfo->args [i + sig->hasthis];
 
                if (ainfo && ainfo->storage == LLVMArgVtypeByVal)
-                       LLVMAddInstrAttribute (lcall, 1 + ainfo->pindex, LLVMByValAttribute);
+                       mono_llvm_add_instr_attr (lcall, 1 + ainfo->pindex, LLVM_ATTR_BY_VAL);
        }
 
        /*
@@ -4011,18 +4014,15 @@ emit_handler_start (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef builder
                g_assert (personality);
        } else {
 #if LLVM_API_VERSION > 100
-               personality = ctx->module->personality;
-               if (!personality) {
-                       LLVMTypeRef personality_type = LLVMFunctionType (LLVMInt32Type (), NULL, 0, TRUE);
-                       personality = LLVMAddFunction (ctx->lmodule, "mono_personality", personality_type);
-                       LLVMAddFunctionAttr (personality, LLVMNoUnwindAttribute);
-                       LLVMBasicBlockRef entry_bb = LLVMAppendBasicBlock (personality, "ENTRY");
-                       LLVMBuilderRef builder2 = LLVMCreateBuilder ();
-                       LLVMPositionBuilderAtEnd (builder2, entry_bb);
-                       LLVMBuildRet (builder2, LLVMConstInt (LLVMInt32Type (), 0, FALSE));
-                       ctx->module->personality = personality;
-                       LLVMDisposeBuilder (builder2);
-               }
+               /* Can't cache this as each method is in its own llvm module */
+               LLVMTypeRef personality_type = LLVMFunctionType (LLVMInt32Type (), NULL, 0, TRUE);
+               personality = LLVMAddFunction (ctx->lmodule, "mono_personality", personality_type);
+               mono_llvm_add_func_attr (personality, LLVM_ATTR_NO_UNWIND);
+               LLVMBasicBlockRef entry_bb = LLVMAppendBasicBlock (personality, "ENTRY");
+               LLVMBuilderRef builder2 = LLVMCreateBuilder ();
+               LLVMPositionBuilderAtEnd (builder2, entry_bb);
+               LLVMBuildRet (builder2, LLVMConstInt (LLVMInt32Type (), 0, FALSE));
+               LLVMDisposeBuilder (builder2);
 #else
                static gint32 mapping_inited;
 
@@ -6466,7 +6466,11 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
                        args [0] = lhs;
                        args [1] = rhs;
                        /* 0xf1 == multiply all 4 elements, add them together, and store the result to the lowest element */
+#if LLVM_API_VERSION >= 500
+                       args [2] = LLVMConstInt (LLVMInt8Type (), 0xf1, FALSE);
+#else
                        args [2] = LLVMConstInt (LLVMInt32Type (), 0xf1, FALSE);
+#endif
 
                        values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, simd_op_to_intrins (ins->opcode)), args, 3, dname);
                        break;
@@ -6875,7 +6879,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
        if (cfg->compile_aot)
                ctx->lmodule = ctx->module->lmodule;
        else
-               ctx->lmodule = LLVMModuleCreateWithName ("jit-module");
+               ctx->lmodule = LLVMModuleCreateWithName (g_strdup_printf ("jit-module-%s", cfg->method->name));
 #else
        ctx->lmodule = ctx->module->lmodule;
 #endif
@@ -6976,7 +6980,7 @@ emit_method_inner (EmitContext *ctx)
                LLVMSetFunctionCallConv (method, LLVMMono1CallConv);
        LLVMSetLinkage (method, LLVMPrivateLinkage);
 
-       LLVMAddFunctionAttr (method, LLVMUWTable);
+       mono_llvm_add_func_attr (method, LLVM_ATTR_UW_TABLE);
 
        if (cfg->compile_aot) {
                LLVMSetLinkage (method, LLVMInternalLinkage);
@@ -7016,7 +7020,7 @@ emit_method_inner (EmitContext *ctx)
        }
        if (header->num_clauses || (cfg->method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) || cfg->no_inline)
                /* We can't handle inlined methods with clauses */
-               LLVMAddFunctionAttr (method, LLVMNoInlineAttribute);
+               mono_llvm_add_func_attr (method, LLVM_ATTR_NO_INLINE);
 
        if (linfo->rgctx_arg) {
                ctx->rgctx_arg = LLVMGetParam (method, linfo->rgctx_arg_pindex);
@@ -7027,7 +7031,7 @@ emit_method_inner (EmitContext *ctx)
                 * CC_X86_64_Mono in X86CallingConv.td.
                 */
                if (!ctx->llvm_only)
-                       LLVMAddAttribute (ctx->rgctx_arg, LLVMInRegAttribute);
+                       mono_llvm_add_param_attr (ctx->rgctx_arg, LLVM_ATTR_IN_REG);
                LLVMSetValueName (ctx->rgctx_arg, "rgctx");
        } else {
                ctx->rgctx_arg_pindex = -1;
@@ -7036,8 +7040,8 @@ emit_method_inner (EmitContext *ctx)
                values [cfg->vret_addr->dreg] = LLVMGetParam (method, linfo->vret_arg_pindex);
                LLVMSetValueName (values [cfg->vret_addr->dreg], "vret");
                if (linfo->ret.storage == LLVMArgVtypeByRef) {
-                       LLVMAddAttribute (LLVMGetParam (method, linfo->vret_arg_pindex), LLVMStructRetAttribute);
-                       LLVMAddAttribute (LLVMGetParam (method, linfo->vret_arg_pindex), LLVMNoAliasAttribute);
+                       mono_llvm_add_param_attr (LLVMGetParam (method, linfo->vret_arg_pindex), LLVM_ATTR_STRUCT_RET);
+                       mono_llvm_add_param_attr (LLVMGetParam (method, linfo->vret_arg_pindex), LLVM_ATTR_NO_ALIAS);
                }
        }
 
@@ -7082,7 +7086,7 @@ emit_method_inner (EmitContext *ctx)
                LLVMSetValueName (values [cfg->args [i + sig->hasthis]->dreg], name);
                g_free (name);
                if (ainfo->storage == LLVMArgVtypeByVal)
-                       LLVMAddAttribute (LLVMGetParam (method, pindex), LLVMByValAttribute);
+                       mono_llvm_add_param_attr (LLVMGetParam (method, pindex), LLVM_ATTR_BY_VAL);
 
                if (ainfo->storage == LLVMArgVtypeByRef) {
                        /* For OP_LDADDR */
@@ -8215,7 +8219,11 @@ add_intrinsic (LLVMModuleRef module, int id)
                ret_type = type_to_simd_type (MONO_TYPE_R4);
                arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
                arg_types [1] = type_to_simd_type (MONO_TYPE_R4);
+#if LLVM_API_VERSION >= 500
+               arg_types [2] = LLVMInt8Type ();
+#else
                arg_types [2] = LLVMInt32Type ();
+#endif
                AddFunc (module, name, ret_type, arg_types, 3);
                break;
 #endif