Merge pull request #2820 from kumpera/license-change-rebased
[mono.git] / mono / mini / mini-llvm.c
index ec9d3b6445fdc1dcbe0b7d1c9ab17c1973a11300..adec297e3c3ebfaff926f66a07fea1c05851de76 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright 2009-2011 Novell Inc (http://www.novell.com)
  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 #include "mini.h"
 #define __STDC_CONSTANT_MACROS
 #endif
 
-#include "llvm-c/Core.h"
-#include "llvm-c/ExecutionEngine.h"
 #include "llvm-c/BitWriter.h"
 #include "llvm-c/Analysis.h"
 
 #include "mini-llvm-cpp.h"
+#include "llvm-jit.h"
 #include "aot-compiler.h"
 #include "mini-llvm.h"
 
@@ -45,6 +45,8 @@ void bzero (void *to, size_t count) { memset (to, 0, count); }
 #error "The version of the mono llvm repository is too old."
 #endif
 
+#define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
+
  /*
   * Information associated by mono with LLVM modules.
   */
@@ -165,6 +167,7 @@ typedef struct {
        GPtrArray *phi_values;
        GPtrArray *bblock_list;
        char *method_name;
+       GHashTable *jit_callees;
 } EmitContext;
 
 typedef struct {
@@ -257,9 +260,9 @@ static LLVMRealPredicate fpcond_to_llvm_cond [] = {
 static MonoNativeTlsKey current_cfg_tls_id;
 
 static MonoLLVMModule aot_module;
-static int memset_param_count, memcpy_param_count;
-static const char *memset_func_name;
-static const char *memcpy_func_name;
+
+static GHashTable *intrins_id_to_name;
+static GHashTable *intrins_name_to_id;
 
 static void init_jit_module (MonoDomain *domain);
 
@@ -267,6 +270,8 @@ static void emit_dbg_loc (EmitContext *ctx, LLVMBuilderRef builder, const unsign
 static LLVMValueRef emit_dbg_subprogram (EmitContext *ctx, MonoCompile *cfg, LLVMValueRef method, const char *name);
 static void emit_dbg_info (MonoLLVMModule *module, const char *filename, const char *cu_name);
 static void emit_cond_system_exception (EmitContext *ctx, MonoBasicBlock *bb, const char *exc_type, LLVMValueRef cmp);
+static LLVMValueRef get_intrinsic (EmitContext *ctx, const char *name);
+static void decode_llvm_eh_info (EmitContext *ctx, gpointer eh_frame);
 
 static inline void
 set_failure (EmitContext *ctx, const char *message)
@@ -1696,6 +1701,22 @@ get_callee (EmitContext *ctx, LLVMTypeRef llvm_sig, MonoJumpInfoType type, gcons
        }
 }
 
+static LLVMValueRef
+emit_jit_callee (EmitContext *ctx, const char *name, LLVMTypeRef llvm_sig, gpointer target)
+{
+#if LLVM_API_VERSION > 100
+       LLVMValueRef tramp_var = LLVMAddGlobal (ctx->lmodule, LLVMPointerType (llvm_sig, 0), name);
+       LLVMSetInitializer (tramp_var, LLVMConstIntToPtr (LLVMConstInt (LLVMInt64Type (), (guint64)(size_t)target, FALSE), LLVMPointerType (llvm_sig, 0)));
+       LLVMSetLinkage (tramp_var, LLVMExternalLinkage);
+       LLVMValueRef callee = LLVMBuildLoad (ctx->builder, tramp_var, "");
+       return callee;
+#else
+       LLVMValueRef callee = LLVMAddFunction (ctx->lmodule, "", llvm_sig);
+       LLVMAddGlobalMapping (ctx->module->ee, callee, target);
+       return callee;
+#endif
+}
+
 static int
 get_handler_clause (MonoCompile *cfg, MonoBasicBlock *bb)
 {
@@ -1926,7 +1947,7 @@ emit_load_general (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder
                args [1] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
                args [2] = LLVMConstInt (LLVMInt1Type (), TRUE, FALSE);
                args [3] = LLVMConstInt (LLVMInt32Type (), ordering, FALSE);
-               res = emit_call (ctx, bb, builder_ref, LLVMGetNamedFunction (ctx->lmodule, intrins_name), args, 4);
+               res = emit_call (ctx, bb, builder_ref, get_intrinsic (ctx, intrins_name), args, 4);
 
                if (addr_type == LLVMPointerType (LLVMDoubleType (), 0))
                        res = LLVMBuildBitCast (*builder_ref, res, LLVMDoubleType (), "");
@@ -2022,7 +2043,7 @@ emit_store_general (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builde
                args [2] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
                args [3] = LLVMConstInt (LLVMInt1Type (), TRUE, FALSE);
                args [4] = LLVMConstInt (LLVMInt32Type (), ordering, FALSE);
-               emit_call (ctx, bb, builder_ref, LLVMGetNamedFunction (ctx->lmodule, intrins_name), args, 5);
+               emit_call (ctx, bb, builder_ref, get_intrinsic (ctx, intrins_name), args, 5);
        } else {
                mono_llvm_build_store (*builder_ref, value, addr, is_faulting, barrier);
        }
@@ -2048,6 +2069,11 @@ emit_cond_system_exception (EmitContext *ctx, MonoBasicBlock *bb, const char *ex
        MonoClass *exc_class;
        LLVMValueRef args [2];
        LLVMValueRef callee;
+       gboolean no_pc = FALSE;
+
+       if (IS_TARGET_AMD64)
+               /* Some platforms don't require the pc argument */
+               no_pc = TRUE;
        
        ex_bb = gen_bb (ctx, "EX_BB");
        if (ctx->llvm_only)
@@ -2092,37 +2118,53 @@ emit_cond_system_exception (EmitContext *ctx, MonoBasicBlock *bb, const char *ex
                LLVMTypeRef sig;
                const char *icall_name;
 
-               sig = LLVMFunctionType2 (LLVMVoidType (), LLVMInt32Type (), LLVMPointerType (LLVMInt8Type (), 0), FALSE);
+               if (no_pc)
+                       sig = LLVMFunctionType1 (LLVMVoidType (), LLVMInt32Type (), FALSE);
+               else
+                       sig = LLVMFunctionType2 (LLVMVoidType (), LLVMInt32Type (), LLVMPointerType (LLVMInt8Type (), 0), FALSE);
                icall_name = "llvm_throw_corlib_exception_abs_trampoline";
 
                if (ctx->cfg->compile_aot) {
                        callee = get_callee (ctx, sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
                } else {
-                       callee = LLVMAddFunction (ctx->lmodule, "llvm_throw_corlib_exception_trampoline", sig);
-
                        /*
                         * Differences between the LLVM/non-LLVM throw corlib exception trampoline:
                         * - On x86, LLVM generated code doesn't push the arguments
                         * - The trampoline takes the throw address as an arguments, not a pc offset.
                         */
-                       LLVMAddGlobalMapping (ctx->module->ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
+                       gpointer target = resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
+                       callee = emit_jit_callee (ctx, "llvm_throw_corlib_exception_trampoline", sig, target);
 
+#if LLVM_API_VERSION > 100
+                       /*
+                        * Make sure that ex_bb starts with the invoke, so the block address points to it, and not to the load 
+                        * added by emit_jit_callee ().
+                        */
+                       ex2_bb = gen_bb (ctx, "EX2_BB");
+                       LLVMBuildBr (builder, ex2_bb);
+                       ex_bb = ex2_bb;
+
+                       ctx->builder = builder = create_builder (ctx);
+                       LLVMPositionBuilderAtEnd (ctx->builder, ex2_bb);
+#else
                        mono_memory_barrier ();
                        ctx->module->throw_corlib_exception = callee;
+#endif
                }
        }
 
-       if (IS_TARGET_X86 || IS_TARGET_AMD64)
-               args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token - MONO_TOKEN_TYPE_DEF, FALSE);
-       else
-               args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token, FALSE);
+       args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token - MONO_TOKEN_TYPE_DEF, FALSE);
 
        /*
         * The LLVM mono branch contains changes so a block address can be passed as an
         * argument to a call.
         */
-       args [1] = LLVMBlockAddress (ctx->lmethod, ex_bb);
-       emit_call (ctx, bb, &builder, callee, args, 2);
+       if (no_pc) {
+               emit_call (ctx, bb, &builder, callee, args, 1);
+       } else {
+               args [1] = LLVMBlockAddress (ctx->lmethod, ex_bb);
+               emit_call (ctx, bb, &builder, callee, args, 2);
+       }
 
        LLVMBuildUnreachable (builder);
 
@@ -2722,7 +2764,7 @@ emit_init_method (EmitContext *ctx)
 
        args [0] = inited_var;
        args [1] = LLVMConstInt (LLVMInt8Type (), 1, FALSE);
-       inited_var = LLVMBuildCall (ctx->builder, LLVMGetNamedFunction (ctx->lmodule, "llvm.expect.i8"), args, 2, "");
+       inited_var = LLVMBuildCall (ctx->builder, get_intrinsic (ctx, "llvm.expect.i8"), args, 2, "");
 
        cmp = LLVMBuildICmp (builder, LLVMIntEQ, inited_var, LLVMConstInt (LLVMTypeOf (inited_var), 0, FALSE), "");
 
@@ -3091,7 +3133,6 @@ static void
 process_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, MonoInst *ins)
 {
        MonoCompile *cfg = ctx->cfg;
-       LLVMModuleRef lmodule = ctx->lmodule;
        LLVMValueRef *values = ctx->values;
        LLVMValueRef *addresses = ctx->addresses;
        MonoCallInst *call = (MonoCallInst*)ins;
@@ -3154,15 +3195,46 @@ process_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref,
                                }
                        } else {
                                MonoError error;
+                               static int tramp_index;
+                               char *name;
 
-                               callee = LLVMAddFunction (lmodule, "", llvm_sig);
+                               name = g_strdup_printf ("tramp_%d", tramp_index);
+                               tramp_index ++;
+
+#if LLVM_API_VERSION > 100
+                               /*
+                                * Use our trampoline infrastructure for lazy compilation instead of llvm's.
+                                * Make all calls through a global. The address of the global will be saved in
+                                * MonoJitDomainInfo.llvm_jit_callees and updated when the method it refers to is
+                                * compiled.
+                                */
+                               LLVMValueRef tramp_var = g_hash_table_lookup (ctx->jit_callees, call->method);
+                               if (!tramp_var) {
+                                       target =
+                                               mono_create_jit_trampoline (mono_domain_get (),
+                                                                                                       call->method, &error);
+                                       if (!mono_error_ok (&error))
+                                               mono_error_raise_exception (&error); /* FIXME: Don't raise here */
+                                       tramp_var = LLVMAddGlobal (ctx->lmodule, LLVMPointerType (llvm_sig, 0), name);
+                                       LLVMSetInitializer (tramp_var, LLVMConstIntToPtr (LLVMConstInt (LLVMInt64Type (), (guint64)(size_t)target, FALSE), LLVMPointerType (llvm_sig, 0)));
+                                       LLVMSetLinkage (tramp_var, LLVMExternalLinkage);
+                                       g_hash_table_insert (ctx->jit_callees, call->method, tramp_var);
+                               }
+                               callee = LLVMBuildLoad (builder, tramp_var, "");
+#else
                                target =
                                        mono_create_jit_trampoline (mono_domain_get (),
                                                                                                call->method, &error);
+                               if (!mono_error_ok (&error))
+                                       mono_error_raise_exception (&error); /* FIXME: Don't raise here */
+
+                               callee = LLVMAddFunction (ctx->lmodule, name, llvm_sig);
+                               g_free (name);
+
                                if (!mono_error_ok (&error))
                                        mono_error_raise_exception (&error); /* FIXME: Don't raise here */
                                LLVMAddGlobalMapping (ctx->module->ee, callee, target);
+#endif
                        }
                }
 
@@ -3192,9 +3264,8 @@ process_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref,
                                        return;
                                }
                        } else {
-                               callee = LLVMAddFunction (lmodule, "", llvm_sig);
                                target = (gpointer)mono_icall_get_wrapper (info);
-                               LLVMAddGlobalMapping (ctx->module->ee, callee, target);
+                               callee = emit_jit_callee (ctx, "", llvm_sig, target);
                        }
                } else {
                        if (cfg->compile_aot) {
@@ -3214,7 +3285,23 @@ process_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref,
                                        return;
                                }
                        } else {
-                               callee = LLVMAddFunction (lmodule, "", llvm_sig);
+#if LLVM_API_VERSION > 100
+                               if (cfg->abs_patches) {
+                                       MonoJumpInfo *abs_ji = (MonoJumpInfo*)g_hash_table_lookup (cfg->abs_patches, call->fptr);
+                                       if (abs_ji) {
+                                               MonoError error;
+
+                                               target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, abs_ji, FALSE, &error);
+                                               mono_error_assert_ok (&error);
+                                               callee = emit_jit_callee (ctx, "", llvm_sig, target);
+                                       } else {
+                                               g_assert_not_reached ();
+                                       }
+                               } else {
+                                       g_assert_not_reached ();
+                               }
+#else
+                               callee = LLVMAddFunction (ctx->lmodule, "", llvm_sig);
                                target = NULL;
                                if (cfg->abs_patches) {
                                        MonoJumpInfo *abs_ji = (MonoJumpInfo*)g_hash_table_lookup (cfg->abs_patches, call->fptr);
@@ -3232,6 +3319,7 @@ process_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref,
                                }
                                if (!target)
                                        LLVMAddGlobalMapping (ctx->module->ee, callee, (gpointer)call->fptr);
+#endif
                        }
                }
        }
@@ -3536,24 +3624,26 @@ emit_throw (EmitContext *ctx, MonoBasicBlock *bb, gboolean rethrow, LLVMValueRef
                if (ctx->cfg->compile_aot) {
                        callee = get_callee (ctx, sig_to_llvm_sig (ctx, throw_sig), MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
                } else {
-                       callee = LLVMAddFunction (ctx->lmodule, icall_name, sig_to_llvm_sig (ctx, throw_sig));
-
+                       gpointer target;
 #ifdef TARGET_X86
                        /* 
                         * LLVM doesn't push the exception argument, so we need a different
                         * trampoline.
                         */
-                       LLVMAddGlobalMapping (ctx->module->ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, rethrow ? "llvm_rethrow_exception_trampoline" : "llvm_throw_exception_trampoline"));
+                       target = resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, rethrow ? "llvm_rethrow_exception_trampoline" : "llvm_throw_exception_trampoline");
 #else
-                       LLVMAddGlobalMapping (ctx->module->ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
+                       target = resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
 #endif
+                       callee = emit_jit_callee (ctx, icall_name, sig_to_llvm_sig (ctx, throw_sig), target);
                }
 
                mono_memory_barrier ();
+#if LLVM_API_VERSION < 100
                if (rethrow)
                        ctx->module->rethrow = callee;
                else
                        ctx->module->throw_icall = callee;
+#endif
        }
        arg = convert (ctx, exc, type_to_llvm_type (ctx, &mono_get_object_class ()->byval_arg));
        emit_call (ctx, bb, &ctx->builder, callee, &arg, 1);
@@ -3716,7 +3806,7 @@ get_mono_personality (EmitContext *ctx)
 
        if (!use_debug_personality) {
                if (ctx->cfg->compile_aot) {
-                               personality = LLVMGetNamedFunction (ctx->lmodule, default_personality_name);
+                               personality = get_intrinsic (ctx, default_personality_name);
                } else if (InterlockedCompareExchange (&mapping_inited, 1, 0) == 0) {
                                personality = LLVMAddFunction (ctx->lmodule, default_personality_name, personality_type);
                                LLVMAddGlobalMapping (ctx->module->ee, personality, personality);
@@ -3864,10 +3954,8 @@ emit_handler_start (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef builder
        LLVMValueRef landing_pad;
        LLVMBasicBlockRef target_bb;
        MonoInst *exvar;
-       static gint32 mapping_inited;
        static int ti_generator;
        char ti_name [128];
-       gint32 *ti;
        LLVMValueRef type_info;
        int clause_index;
        GSList *l;
@@ -3879,9 +3967,22 @@ emit_handler_start (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef builder
                personality = LLVMGetNamedFunction (lmodule, "mono_personality");
                g_assert (personality);
        } else {
+#if LLVM_API_VERSION > 100
+               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));
+#else
+               static gint32 mapping_inited;
+
                personality = LLVMGetNamedFunction (lmodule, "mono_personality");
+
                if (InterlockedCompareExchange (&mapping_inited, 1, 0) == 0)
                        LLVMAddGlobalMapping (ctx->module->ee, personality, (gpointer)mono_personality);
+#endif
        }
 
        i8ptr = LLVMPointerType (LLVMInt8Type (), 0);
@@ -3904,6 +4005,12 @@ emit_handler_start (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef builder
                 */
                LLVMSetLinkage (type_info, LLVMInternalLinkage);
        } else {
+#if LLVM_API_VERSION > 100
+               type_info = LLVMAddGlobal (lmodule, LLVMInt32Type (), ti_name);
+               LLVMSetInitializer (type_info, LLVMConstInt (LLVMInt32Type (), clause_index, FALSE));
+#else
+               gint32 *ti;
+
                /*
                 * After the cfg mempool is freed, the type info will point to stale memory,
                 * but this is not a problem, since we decode it once in exception_cb during
@@ -3915,6 +4022,7 @@ emit_handler_start (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef builder
                type_info = LLVMAddGlobal (lmodule, i8ptr, ti_name);
 
                LLVMAddGlobalMapping (ctx->module->ee, type_info, ti);
+#endif
        }
 
        {
@@ -3992,7 +4100,6 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
        LLVMValueRef *values = ctx->values;
        LLVMValueRef *addresses = ctx->addresses;
        LLVMCallInfo *linfo = ctx->linfo;
-       LLVMModuleRef lmodule = ctx->lmodule;
        BBInfo *bblocks = ctx->bblocks;
        MonoInst *ins;
        LLVMBasicBlockRef cbb;
@@ -4364,7 +4471,7 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
                        if (likely) {
                                args [0] = cmp;
                                args [1] = LLVMConstInt (LLVMInt1Type (), 1, FALSE);
-                               cmp = LLVMBuildCall (ctx->builder, LLVMGetNamedFunction (ctx->lmodule, "llvm.expect.i1"), args, 2, "");
+                               cmp = LLVMBuildCall (ctx->builder, get_intrinsic (ctx, "llvm.expect.i1"), args, 2, "");
                        }
 
                        if (MONO_IS_COND_BRANCH_OP (ins->next)) {
@@ -4932,7 +5039,7 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
                                args [2] = LLVMConstInt (LLVMInt32Type (), size, FALSE);
                                args [3] = LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT, FALSE);
                                args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
-                               LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, memset_func_name), args, memset_param_count, "");
+                               LLVMBuildCall (builder, get_intrinsic (ctx, "llvm.memset.p0i8.i32"), args, 5, "");
                        }
 
                        values [ins->dreg] = v;
@@ -4953,7 +5060,7 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
                                args [2] = size;
                                args [3] = LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT, FALSE);
                                args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
-                               LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, memset_func_name), args, memset_param_count, "");
+                               LLVMBuildCall (builder, get_intrinsic (ctx, "llvm.memset.p0i8.i32"), args, 5, "");
                        }
                        values [ins->dreg] = v;
                        break;
@@ -5185,28 +5292,28 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
                        LLVMValueRef args [1];
 
                        args [0] = convert (ctx, lhs, LLVMDoubleType ());
-                       values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, "llvm.sin.f64"), args, 1, dname);
+                       values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, "llvm.sin.f64"), args, 1, dname);
                        break;
                }
                case OP_COS: {
                        LLVMValueRef args [1];
 
                        args [0] = convert (ctx, lhs, LLVMDoubleType ());
-                       values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, "llvm.cos.f64"), args, 1, dname);
+                       values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, "llvm.cos.f64"), args, 1, dname);
                        break;
                }
                case OP_SQRT: {
                        LLVMValueRef args [1];
 
                        args [0] = convert (ctx, lhs, LLVMDoubleType ());
-                       values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, "llvm.sqrt.f64"), args, 1, dname);
+                       values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, "llvm.sqrt.f64"), args, 1, dname);
                        break;
                }
                case OP_ABS: {
                        LLVMValueRef args [1];
 
                        args [0] = convert (ctx, lhs, LLVMDoubleType ());
-                       values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, "fabs"), args, 1, dname);
+                       values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, "fabs"), args, 1, dname);
                        break;
                }
 
@@ -5386,7 +5493,7 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
                }
                case OP_RELAXED_NOP: {
 #if defined(TARGET_AMD64) || defined(TARGET_X86)
-                       emit_call (ctx, bb, &builder, LLVMGetNamedFunction (ctx->lmodule, "llvm.x86.sse2.pause"), NULL, 0);
+                       emit_call (ctx, bb, &builder, get_intrinsic (ctx, "llvm.x86.sse2.pause"), NULL, 0);
                        break;
 #else
                        break;
@@ -5465,7 +5572,7 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
 
                                args [0] = convert (ctx, lhs, op_to_llvm_type (ins->opcode));
                                args [1] = convert (ctx, rhs, op_to_llvm_type (ins->opcode));
-                               func = LLVMGetNamedFunction (lmodule, ovf_op_to_intrins (ins->opcode));
+                               func = get_intrinsic (ctx, ovf_op_to_intrins (ins->opcode));
                                g_assert (func);
                                val = LLVMBuildCall (builder, func, args, 2, "");
                                values [ins->dreg] = LLVMBuildExtractValue (builder, val, 0, dname);
@@ -5502,7 +5609,7 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
                        // FIXME: Alignment
                        args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
                        args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
-                       LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, memset_func_name), args, memset_param_count, "");
+                       LLVMBuildCall (builder, get_intrinsic (ctx, "llvm.memset.p0i8.i32"), args, 5, "");
                        break;
                }
                case OP_DUMMY_VZERO:
@@ -5576,7 +5683,7 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
                        // FIXME: Alignment
                        args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
                        args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
-                       LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, memcpy_func_name), args, memcpy_param_count, "");
+                       LLVMBuildCall (builder, get_intrinsic (ctx, "llvm.memcpy.p0i8.p0i8.i32"), args, 5, "");
                        break;
                }
                case OP_LLVM_OUTARG_VT: {
@@ -5768,7 +5875,7 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
                        args [0] = lhs;
                        args [1] = rhs;
 
-                       values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, simd_op_to_intrins (ins->opcode)), args, 2, dname);
+                       values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, simd_op_to_intrins (ins->opcode)), args, 2, dname);
                        break;
                }
                case OP_PCMPEQB:
@@ -5875,7 +5982,7 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
 
                        v = convert (ctx, values [ins->sreg1], simd_op_to_llvm_type (ins->opcode));
 
-                       values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, simd_op_to_intrins (ins->opcode)), &v, 1, dname);
+                       values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, simd_op_to_intrins (ins->opcode)), &v, 1, dname);
                        break;
                }
 
@@ -5887,7 +5994,7 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
                        args [1] = rhs;
                        args [2] = LLVMConstInt (LLVMInt8Type (), ins->inst_c0, FALSE);
 
-                       values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, simd_op_to_intrins (ins->opcode)), args, 3, dname);
+                       values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, simd_op_to_intrins (ins->opcode)), args, 3, dname);
                        break;
                }
 
@@ -5909,7 +6016,7 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
                        args [0] = lhs;
                        args [1] = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
 
-                       values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, simd_op_to_intrins (ins->opcode)), args, 2, dname);
+                       values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, simd_op_to_intrins (ins->opcode)), args, 2, dname);
                        break;
                }
 
@@ -5926,7 +6033,7 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
                        args [0] = lhs;
                        args [1] = values [ins->sreg2];
 
-                       values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, simd_op_to_intrins (ins->opcode)), args, 2, dname);
+                       values [ins->dreg] = LLVMBuildCall (builder, get_intrinsic (ctx, simd_op_to_intrins (ins->opcode)), args, 2, dname);
                        break;
                }
 
@@ -6214,7 +6321,17 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
                                if (ctx->cfg->compile_aot) {
                                        callee = get_callee (ctx, LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE), MONO_PATCH_INFO_INTERNAL_METHOD, "llvm_resume_unwind_trampoline");
                                } else {
-                                       callee = LLVMGetNamedFunction (lmodule, "llvm_resume_unwind_trampoline");
+#if LLVM_API_VERSION > 100
+                                       MonoJitICallInfo *info;
+
+                                       info = mono_find_jit_icall_by_name ("llvm_resume_unwind_trampoline");
+                                       g_assert (info);
+                                       gpointer target = (void*)info->func;
+                                       LLVMTypeRef icall_sig = LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE);
+                                       callee = emit_jit_callee (ctx, "llvm_resume_unwind_trampoline", icall_sig, target);
+#else
+                                       callee = LLVMGetNamedFunction (ctx->lmodule, "llvm_resume_unwind_trampoline");
+#endif
                                }
                                LLVMBuildCall (builder, callee, NULL, 0, "");
                                LLVMBuildUnreachable (builder);
@@ -6395,6 +6512,7 @@ free_ctx (EmitContext *ctx)
        g_free (ctx->bblocks);
        g_hash_table_destroy (ctx->region_to_handler);
        g_hash_table_destroy (ctx->clause_to_handler);
+       g_hash_table_destroy (ctx->jit_callees);
        g_free (ctx->method_name);
        g_ptr_array_free (ctx->bblock_list, TRUE);
 
@@ -6453,6 +6571,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
        ctx->region_to_handler = g_hash_table_new (NULL, NULL);
        ctx->clause_to_handler = g_hash_table_new (NULL, NULL);
        ctx->method_to_callers = g_hash_table_new (NULL, NULL);
+       ctx->jit_callees = g_hash_table_new (NULL, NULL);
        if (cfg->compile_aot) {
                ctx->module = &aot_module;
 
@@ -6486,7 +6605,11 @@ mono_llvm_emit_method (MonoCompile *cfg)
        ctx->method_name = method_name;
        ctx->is_linkonce = is_linkonce;
 
+#if LLVM_API_VERSION > 100
+       ctx->lmodule = LLVMModuleCreateWithName ("jit-module");
+#else
        ctx->lmodule = ctx->module->lmodule;
+#endif
        ctx->llvm_only = ctx->module->llvm_only;
 
        emit_method_inner (ctx);
@@ -6594,7 +6717,11 @@ emit_method_inner (EmitContext *ctx)
                        LLVMSetVisibility (method, LLVMDefaultVisibility);
                }
        } else {
+#if LLVM_API_VERSION > 100
+               LLVMSetLinkage (method, LLVMExternalLinkage);
+#else
                LLVMSetLinkage (method, LLVMPrivateLinkage);
+#endif
        }
 
        if (cfg->method->save_lmf && !cfg->llvm_only) {
@@ -6993,6 +7120,46 @@ emit_method_inner (EmitContext *ctx)
 #endif
        } else {
                //LLVMVerifyFunction(method, 0);
+#if LLVM_API_VERSION > 100
+               MonoDomain *domain = mono_domain_get ();
+               MonoJitDomainInfo *domain_info;
+               int nvars = g_hash_table_size (ctx->jit_callees);
+               LLVMValueRef *callee_vars = g_new0 (LLVMValueRef, nvars); 
+               gpointer *callee_addrs = g_new0 (gpointer, nvars);
+               GHashTableIter iter;
+               LLVMValueRef var;
+               MonoMethod *callee;
+               gpointer eh_frame;
+
+               /*
+                * Compute the addresses of the LLVM globals pointing to the
+                * methods called by the current method. Pass it to the trampoline
+                * code so it can update them after their corresponding method was
+                * compiled.
+                */
+               g_hash_table_iter_init (&iter, ctx->jit_callees);
+               i = 0;
+               while (g_hash_table_iter_next (&iter, NULL, (void**)&var))
+                       callee_vars [i ++] = var;
+
+               cfg->native_code = mono_llvm_compile_method (ctx->module->mono_ee, ctx->lmethod, nvars, callee_vars, callee_addrs, &eh_frame);
+
+               decode_llvm_eh_info (ctx, eh_frame);
+
+               mono_domain_lock (domain);
+               domain_info = domain_jit_info (domain);
+               if (!domain_info->llvm_jit_callees)
+                       domain_info->llvm_jit_callees = g_hash_table_new (NULL, NULL);
+               g_hash_table_iter_init (&iter, ctx->jit_callees);
+               i = 0;
+               while (g_hash_table_iter_next (&iter, (void**)&callee, (void**)&var)) {
+                       GSList *addrs = g_hash_table_lookup (domain_info->llvm_jit_callees, callee);
+                       addrs = g_slist_prepend (addrs, callee_addrs [i]);
+                       g_hash_table_insert (domain_info->llvm_jit_callees, callee, addrs);
+                       i ++;
+               }
+               mono_domain_unlock (domain);
+#else
                mono_llvm_optimize_method (ctx->module->mono_ee, ctx->lmethod);
 
                if (cfg->verbose_level > 1)
@@ -7002,6 +7169,7 @@ emit_method_inner (EmitContext *ctx)
 
                /* Set by emit_cb */
                g_assert (cfg->code_len);
+#endif
        }
 
        if (ctx->module->method_to_lmethod)
@@ -7241,6 +7409,90 @@ exception_cb (void *data)
        g_free (type_info);
 }
 
+#if LLVM_API_VERSION > 100
+/*
+ * decode_llvm_eh_info:
+ *
+ *   Decode the EH table emitted by llvm in jit mode, and store
+ * the result into cfg.
+ */
+static void
+decode_llvm_eh_info (EmitContext *ctx, gpointer eh_frame)
+{
+       MonoCompile *cfg = ctx->cfg;
+       guint8 *cie, *fde;
+       int fde_len;
+       MonoLLVMFDEInfo info;
+       MonoJitExceptionInfo *ei;
+       guint8 *p = eh_frame;
+       int version, fde_count, fde_offset;
+       guint32 ei_len, i, nested_len;
+       gpointer *type_info;
+       gint32 *table;
+
+       /*
+        * Decode the one element EH table emitted by the MonoException class
+        * in llvm.
+        */
+
+       /* Similar to decode_llvm_mono_eh_frame () in aot-runtime.c */
+
+       version = *p;
+       g_assert (version == 3);
+       p ++;
+       p ++;
+       p = (guint8 *)ALIGN_PTR_TO (p, 4);
+
+       fde_count = *(guint32*)p;
+       p += 4;
+       table = (gint32*)p;
+
+       g_assert (fde_count == 1);
+
+       /* The only table entry */
+       fde_offset = table [1];
+       table += 2;
+       /* Extra entry */
+       cfg->code_len = table [0];
+       fde_len = table [1] - fde_offset;
+       table += 2;
+
+       fde = (guint8*)eh_frame + fde_offset;
+       cie = (guint8*)table;
+
+       mono_unwind_decode_llvm_mono_fde (fde, fde_len, cie, cfg->native_code, &info);
+
+       cfg->encoded_unwind_ops = info.unw_info;
+       cfg->encoded_unwind_ops_len = info.unw_info_len;
+       if (cfg->verbose_level > 1)
+               mono_print_unwind_info (cfg->encoded_unwind_ops, cfg->encoded_unwind_ops_len);
+       if (info.this_reg != -1) {
+               cfg->llvm_this_reg = info.this_reg;
+               cfg->llvm_this_offset = info.this_offset;
+       }
+
+       ei = info.ex_info;
+       ei_len = info.ex_info_len;
+       type_info = info.type_info;
+
+       // Nested clauses are currently disabled
+       nested_len = 0;
+
+       cfg->llvm_ex_info = (MonoJitExceptionInfo*)mono_mempool_alloc0 (cfg->mempool, (ei_len + nested_len) * sizeof (MonoJitExceptionInfo));
+       cfg->llvm_ex_info_len = ei_len + nested_len;
+       memcpy (cfg->llvm_ex_info, ei, ei_len * sizeof (MonoJitExceptionInfo));
+       /* Fill the rest of the information from the type info */
+       for (i = 0; i < ei_len; ++i) {
+               gint32 clause_index = *(gint32*)type_info [i];
+               MonoExceptionClause *clause = &cfg->header->clauses [clause_index];
+
+               cfg->llvm_ex_info [i].flags = clause->flags;
+               cfg->llvm_ex_info [i].data.catch_class = clause->data.catch_class;
+               cfg->llvm_ex_info [i].clause_index = clause_index;
+       }
+}
+#endif
+
 static char*
 dlsym_cb (const char *name, void **symbol)
 {
@@ -7281,226 +7533,478 @@ AddFunc2 (LLVMModuleRef module, const char *name, LLVMTypeRef ret_type, LLVMType
        AddFunc (module, name, ret_type, param_types, 2);
 }
 
+typedef enum {
+       INTRINS_MEMSET,
+       INTRINS_MEMCPY,
+       INTRINS_SADD_OVF_I32,
+       INTRINS_UADD_OVF_I32,
+       INTRINS_SSUB_OVF_I32,
+       INTRINS_USUB_OVF_I32,
+       INTRINS_SMUL_OVF_I32,
+       INTRINS_UMUL_OVF_I32,
+       INTRINS_SADD_OVF_I64,
+       INTRINS_UADD_OVF_I64,
+       INTRINS_SSUB_OVF_I64,
+       INTRINS_USUB_OVF_I64,
+       INTRINS_SMUL_OVF_I64,
+       INTRINS_UMUL_OVF_I64,
+       INTRINS_SIN,
+       INTRINS_COS,
+       INTRINS_SQRT,
+       INTRINS_FABS,
+       INTRINS_EXPECT_I8,
+       INTRINS_EXPECT_I1,
+#if defined(TARGET_AMD64) || defined(TARGET_X86)
+       INTRINS_SSE_PMOVMSKB,
+       INTRINS_SSE_PSRLI_W,
+       INTRINS_SSE_PSRAI_W,
+       INTRINS_SSE_PSLLI_W,
+       INTRINS_SSE_PSRLI_D,
+       INTRINS_SSE_PSRAI_D,
+       INTRINS_SSE_PSLLI_D,
+       INTRINS_SSE_PSRLI_Q,
+       INTRINS_SSE_PSLLI_Q,
+       INTRINS_SSE_SQRT_PD,
+       INTRINS_SSE_SQRT_PS,
+       INTRINS_SSE_RSQRT_PS,
+       INTRINS_SSE_RCP_PS,
+       INTRINS_SSE_CVTTPD2DQ,
+       INTRINS_SSE_CVTTPS2DQ,
+       INTRINS_SSE_CVTDQ2PD,
+       INTRINS_SSE_CVTDQ2PS,
+       INTRINS_SSE_CVTPD2DQ,
+       INTRINS_SSE_CVTPS2DQ,
+       INTRINS_SSE_CVTPD2PS,
+       INTRINS_SSE_CVTPS2PD,
+       INTRINS_SSE_CMPPD,
+       INTRINS_SSE_CMPPS,
+       INTRINS_SSE_PACKSSWB,
+       INTRINS_SSE_PACKUSWB,
+       INTRINS_SSE_PACKSSDW,
+       INTRINS_SSE_PACKUSDW,
+       INTRINS_SSE_MINPS,
+       INTRINS_SSE_MAXPS,
+       INTRINS_SSE_HADDPS,
+       INTRINS_SSE_HSUBPS,
+       INTRINS_SSE_ADDSUBPS,
+       INTRINS_SSE_MINPD,
+       INTRINS_SSE_MAXPD,
+       INTRINS_SSE_HADDPD,
+       INTRINS_SSE_HSUBPD,
+       INTRINS_SSE_ADDSUBPD,
+       INTRINS_SSE_PMINUD,
+       INTRINS_SSE_PMAXUD,
+       INTRINS_SSE_PMINUW,
+       INTRINS_SSE_PMINSW,
+       INTRINS_SSE_PMAXUW,
+       INTRINS_SSE_PADDSW,
+       INTRINS_SSE_PSUBSW,
+       INTRINS_SSE_PADDUSW,
+       INTRINS_SSE_PSUBUSW,
+       INTRINS_SSE_PAVGW,
+       INTRINS_SSE_PMULHW,
+       INTRINS_SSE_PMULHU,
+       INTRINS_SSE_PMINUB,
+       INTRINS_SSE_PMAXUB,
+       INTRINS_SE_PADDSB,
+       INTRINS_SSE_PSUBSB,
+       INTRINS_SSE_PADDUSB,
+       INTRINS_SSE_PSUBUSB,
+       INTRINS_SSE_PAVGB,
+       INTRINS_SSE_PAUSE,
+#endif
+       INTRINS_NUM
+} IntrinsicId;
+
+typedef struct {
+       IntrinsicId id;
+       const char *name;
+} IntrinsicDesc;
+
+static IntrinsicDesc intrinsics[] = {
+       {INTRINS_MEMSET, "llvm.memset.p0i8.i32"},
+       {INTRINS_MEMCPY, "llvm.memcpy.p0i8.p0i8.i32"},
+       {INTRINS_SADD_OVF_I32, "llvm.sadd.with.overflow.i32"},
+       {INTRINS_UADD_OVF_I32, "llvm.uadd.with.overflow.i32"},
+       {INTRINS_SSUB_OVF_I32, "llvm.ssub.with.overflow.i32"},
+       {INTRINS_USUB_OVF_I32, "llvm.usub.with.overflow.i32"},
+       {INTRINS_SMUL_OVF_I32, "llvm.smul.with.overflow.i32"},
+       {INTRINS_UMUL_OVF_I32, "llvm.umul.with.overflow.i32"},
+       {INTRINS_SADD_OVF_I64, "llvm.sadd.with.overflow.i64"},
+       {INTRINS_UADD_OVF_I64, "llvm.uadd.with.overflow.i64"},
+       {INTRINS_SSUB_OVF_I64, "llvm.ssub.with.overflow.i64"},
+       {INTRINS_USUB_OVF_I64, "llvm.usub.with.overflow.i64"},
+       {INTRINS_SMUL_OVF_I64, "llvm.smul.with.overflow.i64"},
+       {INTRINS_UMUL_OVF_I64, "llvm.umul.with.overflow.i64"},
+       {INTRINS_SIN, "llvm.sin.f64"},
+       {INTRINS_COS, "llvm.cos.f64"},
+       {INTRINS_SQRT, "llvm.sqrt.f64"},
+       /* This isn't an intrinsic, instead llvm seems to special case it by name */
+       {INTRINS_FABS, "fabs"},
+       {INTRINS_EXPECT_I8, "llvm.expect.i8"},
+       {INTRINS_EXPECT_I1, "llvm.expect.i1"},
+#if defined(TARGET_AMD64) || defined(TARGET_X86)
+       {INTRINS_SSE_PMOVMSKB, "llvm.x86.sse2.pmovmskb.128"},
+       {INTRINS_SSE_PSRLI_W, "llvm.x86.sse2.psrli.w"},
+       {INTRINS_SSE_PSRAI_W, "llvm.x86.sse2.psrai.w"},
+       {INTRINS_SSE_PSLLI_W, "llvm.x86.sse2.pslli.w"},
+       {INTRINS_SSE_PSRLI_D, "llvm.x86.sse2.psrli.d"},
+       {INTRINS_SSE_PSRAI_D, "llvm.x86.sse2.psrai.d"},
+       {INTRINS_SSE_PSLLI_D, "llvm.x86.sse2.pslli.d"},
+       {INTRINS_SSE_PSRLI_Q, "llvm.x86.sse2.psrli.q"},
+       {INTRINS_SSE_PSLLI_Q, "llvm.x86.sse2.pslli.q"},
+       {INTRINS_SSE_SQRT_PD, "llvm.x86.sse2.sqrt.pd"},
+       {INTRINS_SSE_SQRT_PS, "llvm.x86.sse.sqrt.ps"},
+       {INTRINS_SSE_RSQRT_PS, "llvm.x86.sse.rsqrt.ps"},
+       {INTRINS_SSE_RCP_PS, "llvm.x86.sse.rcp.ps"},
+       {INTRINS_SSE_CVTTPD2DQ, "llvm.x86.sse2.cvttpd2dq"},
+       {INTRINS_SSE_CVTTPS2DQ, "llvm.x86.sse2.cvttps2dq"},
+       {INTRINS_SSE_CVTDQ2PD, "llvm.x86.sse2.cvtdq2pd"},
+       {INTRINS_SSE_CVTDQ2PS, "llvm.x86.sse2.cvtdq2ps"},
+       {INTRINS_SSE_CVTPD2DQ, "llvm.x86.sse2.cvtpd2dq"},
+       {INTRINS_SSE_CVTPS2DQ, "llvm.x86.sse2.cvtps2dq"},
+       {INTRINS_SSE_CVTPD2PS, "llvm.x86.sse2.cvtpd2ps"},
+       {INTRINS_SSE_CVTPS2PD, "llvm.x86.sse2.cvtps2pd"},
+       {INTRINS_SSE_CMPPD, "llvm.x86.sse2.cmp.pd"},
+       {INTRINS_SSE_CMPPS, "llvm.x86.sse.cmp.ps"},
+       {INTRINS_SSE_PACKSSWB, "llvm.x86.sse2.packsswb.128"},
+       {INTRINS_SSE_PACKUSWB, "llvm.x86.sse2.packuswb.128"},
+       {INTRINS_SSE_PACKSSDW, "llvm.x86.sse2.packssdw.128"},
+       {INTRINS_SSE_PACKUSDW, "llvm.x86.sse41.packusdw"},
+       {INTRINS_SSE_MINPS, "llvm.x86.sse.min.ps"},
+       {INTRINS_SSE_MAXPS, "llvm.x86.sse.max.ps"},
+       {INTRINS_SSE_HADDPS, "llvm.x86.sse3.hadd.ps"},
+       {INTRINS_SSE_HSUBPS, "llvm.x86.sse3.hsub.ps"},
+       {INTRINS_SSE_ADDSUBPS, "llvm.x86.sse3.addsub.ps"},
+       {INTRINS_SSE_MINPD, "llvm.x86.sse2.min.pd"},
+       {INTRINS_SSE_MAXPD, "llvm.x86.sse2.max.pd"},
+       {INTRINS_SSE_HADDPD, "llvm.x86.sse3.hadd.pd"},
+       {INTRINS_SSE_HSUBPD, "llvm.x86.sse3.hsub.pd"},
+       {INTRINS_SSE_ADDSUBPD, "llvm.x86.sse3.addsub.pd"},
+       {INTRINS_SSE_PMINUD, "llvm.x86.sse41.pminud"},
+       {INTRINS_SSE_PMAXUD, "llvm.x86.sse41.pmaxud"},
+       {INTRINS_SSE_PMINUW, "llvm.x86.sse41.pminuw"},
+       {INTRINS_SSE_PMINSW, "llvm.x86.sse2.pmins.w"},
+       {INTRINS_SSE_PMAXUW, "llvm.x86.sse41.pmaxuw"},
+       {INTRINS_SSE_PADDSW, "llvm.x86.sse2.padds.w"},
+       {INTRINS_SSE_PSUBSW, "llvm.x86.sse2.psubs.w"},
+       {INTRINS_SSE_PADDUSW, "llvm.x86.sse2.paddus.w"},
+       {INTRINS_SSE_PSUBUSW, "llvm.x86.sse2.psubus.w"},
+       {INTRINS_SSE_PAVGW, "llvm.x86.sse2.pavg.w"},
+       {INTRINS_SSE_PMULHW, "llvm.x86.sse2.pmulh.w"},
+       {INTRINS_SSE_PMULHU, "llvm.x86.sse2.pmulhu.w"},
+       {INTRINS_SSE_PMINUB, "llvm.x86.sse2.pminu.b"},
+       {INTRINS_SSE_PMAXUB, "llvm.x86.sse2.pmaxu.b"},
+       {INTRINS_SE_PADDSB, "llvm.x86.sse2.padds.b"},
+       {INTRINS_SSE_PSUBSB, "llvm.x86.sse2.psubs.b"},
+       {INTRINS_SSE_PADDUSB, "llvm.x86.sse2.paddus.b"},
+       {INTRINS_SSE_PSUBUSB, "llvm.x86.sse2.psubus.b"},
+       {INTRINS_SSE_PAVGB, "llvm.x86.sse2.pavg.b"},
+       {INTRINS_SSE_PAUSE, "llvm.x86.sse2.pause"}
+#endif
+};
+
 static void
-add_intrinsics (LLVMModuleRef module)
+add_sse_binary (LLVMModuleRef module, const char *name, int type)
 {
-       /* Emit declarations of instrinsics */
-       /*
-        * It would be nicer to emit only the intrinsics actually used, but LLVM's Module
-        * type doesn't seem to do any locking.
-        */
-       {
-               LLVMTypeRef params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMInt8Type (), LLVMInt32Type (), LLVMInt32Type (), LLVMInt1Type () };
-
-               memset_param_count = 5;
-               memset_func_name = "llvm.memset.p0i8.i32";
+       LLVMTypeRef ret_type = type_to_simd_type (type);
+       AddFunc2 (module, name, ret_type, ret_type, ret_type);
+}
 
-               AddFunc (module, memset_func_name, LLVMVoidType (), params, memset_param_count);
-       }
+static void
+add_intrinsic (LLVMModuleRef module, int id)
+{
+       const char *name;
+       LLVMTypeRef ret_type, arg_types [16];
 
-       {
-               LLVMTypeRef params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMPointerType (LLVMInt8Type (), 0), LLVMInt32Type (), LLVMInt32Type (), LLVMInt1Type () };
+       name = g_hash_table_lookup (intrins_id_to_name, GINT_TO_POINTER (id));
+       g_assert (name);
 
-               memcpy_param_count = 5;
-               memcpy_func_name = "llvm.memcpy.p0i8.p0i8.i32";
+       switch (id) {
+       case INTRINS_MEMSET: {
+               LLVMTypeRef params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMInt8Type (), LLVMInt32Type (), LLVMInt32Type (), LLVMInt1Type () };
 
-               AddFunc (module, memcpy_func_name, LLVMVoidType (), params, memcpy_param_count);
+               AddFunc (module, name, LLVMVoidType (), params, 5);
+               break;
        }
+       case INTRINS_MEMCPY: {
+               LLVMTypeRef params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMPointerType (LLVMInt8Type (), 0), LLVMInt32Type (), LLVMInt32Type (), LLVMInt1Type () };
 
-       {
-               LLVMTypeRef params [] = { LLVMDoubleType () };
-
-               AddFunc (module, "llvm.sin.f64", LLVMDoubleType (), params, 1);
-               AddFunc (module, "llvm.cos.f64", LLVMDoubleType (), params, 1);
-               AddFunc (module, "llvm.sqrt.f64", LLVMDoubleType (), params, 1);
-
-               /* This isn't an intrinsic, instead llvm seems to special case it by name */
-               AddFunc (module, "fabs", LLVMDoubleType (), params, 1);
+               AddFunc (module, name, LLVMVoidType (), params, 5);
+               break;
        }
-
-       {
+       case INTRINS_SADD_OVF_I32:
+       case INTRINS_UADD_OVF_I32:
+       case INTRINS_SSUB_OVF_I32:
+       case INTRINS_USUB_OVF_I32:
+       case INTRINS_SMUL_OVF_I32:
+       case INTRINS_UMUL_OVF_I32: {
                LLVMTypeRef ovf_res_i32 [] = { LLVMInt32Type (), LLVMInt1Type () };
                LLVMTypeRef params [] = { LLVMInt32Type (), LLVMInt32Type () };
                LLVMTypeRef ret_type = LLVMStructType (ovf_res_i32, 2, FALSE);
 
-               AddFunc (module, "llvm.sadd.with.overflow.i32", ret_type, params, 2);
-               AddFunc (module, "llvm.sadd.with.overflow.i32", ret_type, params, 2);
-               AddFunc (module, "llvm.uadd.with.overflow.i32", ret_type, params, 2);
-               AddFunc (module, "llvm.ssub.with.overflow.i32", ret_type, params, 2);
-               AddFunc (module, "llvm.usub.with.overflow.i32", ret_type, params, 2);
-               AddFunc (module, "llvm.smul.with.overflow.i32", ret_type, params, 2);
-               AddFunc (module, "llvm.umul.with.overflow.i32", ret_type, params, 2);
+               AddFunc (module, name, ret_type, params, 2);
+               break;
        }
-
-       {
+       case INTRINS_SADD_OVF_I64:
+       case INTRINS_UADD_OVF_I64:
+       case INTRINS_SSUB_OVF_I64:
+       case INTRINS_USUB_OVF_I64:
+       case INTRINS_SMUL_OVF_I64:
+       case INTRINS_UMUL_OVF_I64: {
                LLVMTypeRef ovf_res_i64 [] = { LLVMInt64Type (), LLVMInt1Type () };
                LLVMTypeRef params [] = { LLVMInt64Type (), LLVMInt64Type () };
                LLVMTypeRef ret_type = LLVMStructType (ovf_res_i64, 2, FALSE);
 
-               AddFunc (module, "llvm.sadd.with.overflow.i64", ret_type, params, 2);
-               AddFunc (module, "llvm.uadd.with.overflow.i64", ret_type, params, 2);
-               AddFunc (module, "llvm.ssub.with.overflow.i64", ret_type, params, 2);
-               AddFunc (module, "llvm.usub.with.overflow.i64", ret_type, params, 2);
-               AddFunc (module, "llvm.smul.with.overflow.i64", ret_type, params, 2);
-               AddFunc (module, "llvm.umul.with.overflow.i64", ret_type, params, 2);
+               AddFunc (module, name, ret_type, params, 2);
+               break;
        }
+       case INTRINS_SIN:
+       case INTRINS_COS:
+       case INTRINS_SQRT:
+       case INTRINS_FABS: {
+               LLVMTypeRef params [] = { LLVMDoubleType () };
 
-       AddFunc2 (module, "llvm.expect.i8", LLVMInt8Type (), LLVMInt8Type (), LLVMInt8Type ());
-       AddFunc2 (module, "llvm.expect.i1", LLVMInt1Type (), LLVMInt1Type (), LLVMInt1Type ());
-
-       /* EH intrinsics */
-       {
-               AddFunc (module, "mono_personality", LLVMVoidType (), NULL, 0);
-
-               AddFunc (module, "llvm_resume_unwind_trampoline", LLVMVoidType (), NULL, 0);
+               AddFunc (module, name, LLVMDoubleType (), params, 1);
+               break;
        }
-
-       /* SSE intrinsics */
-#if defined(TARGET_X86) || defined(TARGET_AMD64)
-       {
-               LLVMTypeRef ret_type, arg_types [16];
-
-               /* Binary ops */
-               ret_type = type_to_simd_type (MONO_TYPE_I4);
-               arg_types [0] = ret_type;
-               arg_types [1] = ret_type;
-               AddFunc (module, "llvm.x86.sse41.pminud", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse41.pmaxud", ret_type, arg_types, 2);
-
+       case INTRINS_EXPECT_I8:
+               AddFunc2 (module, name, LLVMInt8Type (), LLVMInt8Type (), LLVMInt8Type ());
+               break;
+       case INTRINS_EXPECT_I1:
+               AddFunc2 (module, name, LLVMInt1Type (), LLVMInt1Type (), LLVMInt1Type ());
+               break;
+#if defined(TARGET_AMD64) || defined(TARGET_X86)
+       case INTRINS_SSE_PMOVMSKB:
+               /* pmovmskb */
+               ret_type = LLVMInt32Type ();
+               arg_types [0] = type_to_simd_type (MONO_TYPE_I1);
+               AddFunc (module, name, ret_type, arg_types, 1);
+               break;
+       case INTRINS_SSE_PSRLI_W:
+       case INTRINS_SSE_PSRAI_W:
+       case INTRINS_SSE_PSLLI_W:
+               /* shifts */
                ret_type = type_to_simd_type (MONO_TYPE_I2);
                arg_types [0] = ret_type;
-               arg_types [1] = ret_type;
-               AddFunc (module, "llvm.x86.sse41.pminuw", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.pmins.w", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse41.pmaxuw", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.padds.w", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.psubs.w", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.paddus.w", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.psubus.w", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.pavg.w", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.pmulh.w", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.pmulhu.w", ret_type, arg_types, 2);
-
-               ret_type = type_to_simd_type (MONO_TYPE_I1);
+               arg_types [1] = LLVMInt32Type ();
+               AddFunc (module, name, ret_type, arg_types, 2);
+               break;
+       case INTRINS_SSE_PSRLI_D:
+       case INTRINS_SSE_PSRAI_D:
+       case INTRINS_SSE_PSLLI_D:
+               ret_type = type_to_simd_type (MONO_TYPE_I4);
                arg_types [0] = ret_type;
-               arg_types [1] = ret_type;
-               AddFunc (module, "llvm.x86.sse2.pminu.b", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.pmaxu.b", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.padds.b", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.psubs.b", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.paddus.b", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.psubus.b", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.pavg.b", ret_type, arg_types, 2);
-
+               arg_types [1] = LLVMInt32Type ();
+               AddFunc (module, name, ret_type, arg_types, 2);
+               break;
+       case INTRINS_SSE_PSRLI_Q:
+       case INTRINS_SSE_PSLLI_Q:
+               ret_type = type_to_simd_type (MONO_TYPE_I8);
+               arg_types [0] = ret_type;
+               arg_types [1] = LLVMInt32Type ();
+               AddFunc (module, name, ret_type, arg_types, 2);
+               break;
+       case INTRINS_SSE_SQRT_PD:
+               /* Unary ops */
                ret_type = type_to_simd_type (MONO_TYPE_R8);
                arg_types [0] = ret_type;
-               arg_types [1] = ret_type;
-               AddFunc (module, "llvm.x86.sse2.min.pd", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.max.pd", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse3.hadd.pd", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse3.hsub.pd", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse3.addsub.pd", ret_type, arg_types, 2);
-
+               AddFunc (module, name, ret_type, arg_types, 1);
+               break;
+       case INTRINS_SSE_SQRT_PS:
                ret_type = type_to_simd_type (MONO_TYPE_R4);
                arg_types [0] = ret_type;
-               arg_types [1] = ret_type;
-               AddFunc (module, "llvm.x86.sse.min.ps", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse.max.ps", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse3.hadd.ps", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse3.hsub.ps", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse3.addsub.ps", ret_type, arg_types, 2);
-
-               /* pack */
-               ret_type = type_to_simd_type (MONO_TYPE_I1);
-               arg_types [0] = type_to_simd_type (MONO_TYPE_I2);
-               arg_types [1] = type_to_simd_type (MONO_TYPE_I2);
-               AddFunc (module, "llvm.x86.sse2.packsswb.128", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.packuswb.128", ret_type, arg_types, 2);
-               ret_type = type_to_simd_type (MONO_TYPE_I2);
-               arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
-               arg_types [1] = type_to_simd_type (MONO_TYPE_I4);
-               AddFunc (module, "llvm.x86.sse2.packssdw.128", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse41.packusdw", ret_type, arg_types, 2);
-
-               /* cmp pd/ps */
-               ret_type = type_to_simd_type (MONO_TYPE_R8);
+               AddFunc (module, name, ret_type, arg_types, 1);
+               break;
+       case INTRINS_SSE_RSQRT_PS:
+               ret_type = type_to_simd_type (MONO_TYPE_R4);
                arg_types [0] = ret_type;
-               arg_types [1] = ret_type;
-               arg_types [2] = LLVMInt8Type ();
-               AddFunc (module, "llvm.x86.sse2.cmp.pd", ret_type, arg_types, 3);
+               AddFunc (module, name, ret_type, arg_types, 1);
+               break;
+       case INTRINS_SSE_RCP_PS:
                ret_type = type_to_simd_type (MONO_TYPE_R4);
                arg_types [0] = ret_type;
-               arg_types [1] = ret_type;
-               arg_types [2] = LLVMInt8Type ();
-               AddFunc (module, "llvm.x86.sse.cmp.ps", ret_type, arg_types, 3);
-
+               AddFunc (module, name, ret_type, arg_types, 1);
+               break;
+       case INTRINS_SSE_CVTTPD2DQ:
+               ret_type = type_to_simd_type (MONO_TYPE_I4);
+               arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
+               AddFunc (module, name, ret_type, arg_types, 1);
+               break;
+       case INTRINS_SSE_CVTTPS2DQ:
+               ret_type = type_to_simd_type (MONO_TYPE_I4);
+               arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
+               AddFunc (module, name, ret_type, arg_types, 1);
+               break;
+       case INTRINS_SSE_CVTDQ2PD:
                /* Conversion ops */
                ret_type = type_to_simd_type (MONO_TYPE_R8);
                arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
-               AddFunc (module, "llvm.x86.sse2.cvtdq2pd", ret_type, arg_types, 1);
+               AddFunc (module, name, ret_type, arg_types, 1);
+               break;
+       case INTRINS_SSE_CVTDQ2PS:
                ret_type = type_to_simd_type (MONO_TYPE_R4);
                arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
-               AddFunc (module, "llvm.x86.sse2.cvtdq2ps", ret_type, arg_types, 1);
+               AddFunc (module, name, ret_type, arg_types, 1);
+               break;
+       case INTRINS_SSE_CVTPD2DQ:
                ret_type = type_to_simd_type (MONO_TYPE_I4);
                arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
-               AddFunc (module, "llvm.x86.sse2.cvtpd2dq", ret_type, arg_types, 1);
+               AddFunc (module, name, ret_type, arg_types, 1);
+               break;
+       case INTRINS_SSE_CVTPS2DQ:
                ret_type = type_to_simd_type (MONO_TYPE_I4);
                arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
-               AddFunc (module, "llvm.x86.sse2.cvtps2dq", ret_type, arg_types, 1);
+               AddFunc (module, name, ret_type, arg_types, 1);
+               break;
+       case INTRINS_SSE_CVTPD2PS:
                ret_type = type_to_simd_type (MONO_TYPE_R4);
                arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
-               AddFunc (module, "llvm.x86.sse2.cvtpd2ps", ret_type, arg_types, 1);
+               AddFunc (module, name, ret_type, arg_types, 1);
+               break;
+       case INTRINS_SSE_CVTPS2PD:
                ret_type = type_to_simd_type (MONO_TYPE_R8);
                arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
-               AddFunc (module, "llvm.x86.sse2.cvtps2pd", ret_type, arg_types, 1);
-
-               ret_type = type_to_simd_type (MONO_TYPE_I4);
-               arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
-               AddFunc (module, "llvm.x86.sse2.cvttpd2dq", ret_type, arg_types, 1);
-               ret_type = type_to_simd_type (MONO_TYPE_I4);
-               arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
-               AddFunc (module, "llvm.x86.sse2.cvttps2dq", ret_type, arg_types, 1);
-
-               /* Unary ops */
+               AddFunc (module, name, ret_type, arg_types, 1);
+               break;
+       case INTRINS_SSE_CMPPD:
+               /* cmp pd/ps */
                ret_type = type_to_simd_type (MONO_TYPE_R8);
                arg_types [0] = ret_type;
-               AddFunc (module, "llvm.x86.sse2.sqrt.pd", ret_type, arg_types, 1);
-               ret_type = type_to_simd_type (MONO_TYPE_R4);
-               arg_types [0] = ret_type;
-               AddFunc (module, "llvm.x86.sse.sqrt.ps", ret_type, arg_types, 1);
-               ret_type = type_to_simd_type (MONO_TYPE_R4);
-               arg_types [0] = ret_type;
-               AddFunc (module, "llvm.x86.sse.rsqrt.ps", ret_type, arg_types, 1);
+               arg_types [1] = ret_type;
+               arg_types [2] = LLVMInt8Type ();
+               AddFunc (module, name, ret_type, arg_types, 3);
+               break;
+       case INTRINS_SSE_CMPPS:
                ret_type = type_to_simd_type (MONO_TYPE_R4);
                arg_types [0] = ret_type;
-               AddFunc (module, "llvm.x86.sse.rcp.ps", ret_type, arg_types, 1);
-
-               /* shifts */
+               arg_types [1] = ret_type;
+               arg_types [2] = LLVMInt8Type ();
+               AddFunc (module, name, ret_type, arg_types, 3);
+               break;
+       case INTRINS_SSE_PACKSSWB:
+       case INTRINS_SSE_PACKUSWB:
+       case INTRINS_SSE_PACKSSDW:
+               /* pack */
+               ret_type = type_to_simd_type (MONO_TYPE_I1);
+               arg_types [0] = type_to_simd_type (MONO_TYPE_I2);
+               arg_types [1] = type_to_simd_type (MONO_TYPE_I2);
+               AddFunc (module, name, ret_type, arg_types, 2);
+               break;
+       case INTRINS_SSE_PACKUSDW:
                ret_type = type_to_simd_type (MONO_TYPE_I2);
-               arg_types [0] = ret_type;
-               arg_types [1] = LLVMInt32Type ();
-               AddFunc (module, "llvm.x86.sse2.psrli.w", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.psrai.w", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.pslli.w", ret_type, arg_types, 2);
-               ret_type = type_to_simd_type (MONO_TYPE_I4);
-               arg_types [0] = ret_type;
-               arg_types [1] = LLVMInt32Type ();
-               AddFunc (module, "llvm.x86.sse2.psrli.d", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.psrai.d", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.pslli.d", ret_type, arg_types, 2);
-               ret_type = type_to_simd_type (MONO_TYPE_I8);
-               arg_types [0] = ret_type;
-               arg_types [1] = LLVMInt32Type ();
-               AddFunc (module, "llvm.x86.sse2.psrli.q", ret_type, arg_types, 2);
-               AddFunc (module, "llvm.x86.sse2.pslli.q", ret_type, arg_types, 2);
+               arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
+               arg_types [1] = type_to_simd_type (MONO_TYPE_I4);
+               AddFunc (module, name, ret_type, arg_types, 2);
+               break;
+               /* SSE Binary ops */
+       case INTRINS_SSE_PMINUD:
+       case INTRINS_SSE_PMAXUD:
+               add_sse_binary (module, name, MONO_TYPE_I4);
+               break;
+       case INTRINS_SSE_PMINUW:
+       case INTRINS_SSE_PMINSW:
+       case INTRINS_SSE_PMAXUW:
+       case INTRINS_SSE_PADDSW:
+       case INTRINS_SSE_PSUBSW:
+       case INTRINS_SSE_PADDUSW:
+       case INTRINS_SSE_PSUBUSW:
+       case INTRINS_SSE_PAVGW:
+       case INTRINS_SSE_PMULHW:
+       case INTRINS_SSE_PMULHU:
+               add_sse_binary (module, name, MONO_TYPE_I2);
+               break;
+       case INTRINS_SSE_MINPS:
+       case INTRINS_SSE_MAXPS:
+       case INTRINS_SSE_HADDPS:
+       case INTRINS_SSE_HSUBPS:
+       case INTRINS_SSE_ADDSUBPS:
+               add_sse_binary (module, name, MONO_TYPE_R4);
+               break;
+       case INTRINS_SSE_MINPD:
+       case INTRINS_SSE_MAXPD:
+       case INTRINS_SSE_HADDPD:
+       case INTRINS_SSE_HSUBPD:
+       case INTRINS_SSE_ADDSUBPD:
+               add_sse_binary (module, name, MONO_TYPE_R8);
+               break;
+       case INTRINS_SSE_PMINUB:
+       case INTRINS_SSE_PMAXUB:
+       case INTRINS_SE_PADDSB:
+       case INTRINS_SSE_PSUBSB:
+       case INTRINS_SSE_PADDUSB:
+       case INTRINS_SSE_PSUBUSB:
+       case INTRINS_SSE_PAVGB:
+               add_sse_binary (module, name, MONO_TYPE_I1);
+               break;
+       case INTRINS_SSE_PAUSE:
+               AddFunc (module, "llvm.x86.sse2.pause", LLVMVoidType (), NULL, 0);
+               break;
+#endif
+       default:
+               g_assert_not_reached ();
+               break;
+       }
+}
 
-               /* pmovmskb */
-               ret_type = LLVMInt32Type ();
-               arg_types [0] = type_to_simd_type (MONO_TYPE_I1);
-               AddFunc (module, "llvm.x86.sse2.pmovmskb.128", ret_type, arg_types, 1);
+static LLVMValueRef
+get_intrinsic (EmitContext *ctx, const char *name)
+{
+#if LLVM_API_VERSION > 100
+       LLVMValueRef res;
+
+       /*
+        * Every method is emitted into its own module so
+        * we can add intrinsics on demand.
+        */
+       res = LLVMGetNamedFunction (ctx->lmodule, name);
+       if (!res) {
+               int id = -1;
+
+               /* No locking needed */
+               id = GPOINTER_TO_INT (g_hash_table_lookup (intrins_name_to_id, name));
+               id --;
+               if (id == -1)
+                       printf ("%s\n", name);
+               g_assert (id != -1);
+               add_intrinsic (ctx->lmodule, id);
+               res = LLVMGetNamedFunction (ctx->lmodule, name);
+               g_assert (res);
+       }
+
+       return res;
+#else
+       LLVMValueRef res;
+
+       res = LLVMGetNamedFunction (ctx->lmodule, name);
+       g_assert (res);
+       return res;
+#endif
+}
+
+static void
+add_intrinsics (LLVMModuleRef module)
+{
+       int i;
+
+       /* Emit declarations of instrinsics */
+       /*
+        * It would be nicer to emit only the intrinsics actually used, but LLVM's Module
+        * type doesn't seem to do any locking.
+        */
+       for (i = 0; i < INTRINS_NUM; ++i)
+               add_intrinsic (module, i);
+
+       /* EH intrinsics */
+       {
+               AddFunc (module, "mono_personality", LLVMVoidType (), NULL, 0);
+
+               AddFunc (module, "llvm_resume_unwind_trampoline", LLVMVoidType (), NULL, 0);
        }
 
-       AddFunc (module, "llvm.x86.sse2.pause", LLVMVoidType (), NULL, 0);
+       /* SSE intrinsics */
+#if defined(TARGET_X86) || defined(TARGET_AMD64)
+
 #endif
 
        /* Load/Store intrinsics */
@@ -7537,13 +8041,25 @@ add_types (MonoLLVMModule *module)
 void
 mono_llvm_init (void)
 {
+       GHashTable *h;
+       int i;
+
        mono_native_tls_alloc (&current_cfg_tls_id, NULL);
+
+       h = g_hash_table_new (NULL, NULL);
+       for (i = 0; i < INTRINS_NUM; ++i)
+               g_hash_table_insert (h, GINT_TO_POINTER (intrinsics [i].id), (gpointer)intrinsics [i].name);
+       intrins_id_to_name = h;
+
+       h = g_hash_table_new (g_str_hash, g_str_equal);
+       for (i = 0; i < INTRINS_NUM; ++i)
+               g_hash_table_insert (h, (gpointer)intrinsics [i].name, GINT_TO_POINTER (intrinsics [i].id + 1));
+       intrins_name_to_id = h;
 }
 
 static void
 init_jit_module (MonoDomain *domain)
 {
-       MonoJitICallInfo *info;
        MonoJitDomainInfo *dinfo;
        MonoLLVMModule *module;
        char *name;
@@ -7572,9 +8088,13 @@ init_jit_module (MonoDomain *domain)
 
        module->llvm_types = g_hash_table_new (NULL, NULL);
 
+#if LLVM_API_VERSION < 100
+       MonoJitICallInfo *info;
+
        info = mono_find_jit_icall_by_name ("llvm_resume_unwind_trampoline");
        g_assert (info);
        LLVMAddGlobalMapping (module->ee, LLVMGetNamedFunction (module->lmodule, "llvm_resume_unwind_trampoline"), (void*)info->func);
+#endif
 
        mono_memory_barrier ();