2009-04-08 Zoltan Varga <vargaz@gmail.com>
authorZoltan Varga <vargaz@gmail.com>
Tue, 7 Apr 2009 22:52:38 +0000 (22:52 -0000)
committerZoltan Varga <vargaz@gmail.com>
Tue, 7 Apr 2009 22:52:38 +0000 (22:52 -0000)
* mini-llvm.c (mono_llvm_emit_method): Fix alignment in the LOCALLOC_IMM
opcode. Use pointer types in more places instead of casting them to
integers.

* mini-llvm-cpp.cpp (mono_llvm_create_ee): Create a pass manager to run
optimizations.
(mono_llvm_optimize_method): New helper function to optimize a method.

* method-to-ir.c (mono_emit_widen_call_res): Extract the call result
widening code so it could be called from more places.
(mono_method_to_ir): Call mono_emit_widne_call_res () in several more
code paths in the call opcodes.

svn path=/trunk/mono/; revision=131265

mono/mini/ChangeLog
mono/mini/method-to-ir.c
mono/mini/mini-llvm-cpp.cpp
mono/mini/mini-llvm-cpp.h
mono/mini/mini-llvm.c
mono/mini/mini.h

index 662f6bffe8b4d640cf7e52f5cdb6954e7947f281..06a5ef33f6e8c9e62628ef06030dca883058b289 100644 (file)
@@ -1,3 +1,17 @@
+2009-04-08  Zoltan Varga  <vargaz@gmail.com>
+
+       * mini-llvm.c (mono_llvm_emit_method): Fix alignment in the LOCALLOC_IMM
+       opcode. Use pointer types in more places instead of casting them to 
+       integers.
+
+       * mini-llvm-cpp.cpp (mono_llvm_create_ee): Create a pass manager to run
+       optimizations.
+       (mono_llvm_optimize_method): New helper function to optimize a method.
+
+       * method-to-ir.c (mono_emit_widen_call_res): Extract the call result 
+       widening code so it could be called from more places.
+       (mono_method_to_ir): Call mono_emit_widne_call_res () in several more
+       code paths in the call opcodes.
 
 Mon Apr 6 14:19:54 CEST 2009 Paolo Molaro <lupus@ximian.com>
 
index df53229fc1276b3c5e2b26eeb615902edc7b379f..7f7abef3d4f739cb32091111c4921e3894ef2ee8 100644 (file)
@@ -2429,6 +2429,48 @@ mono_emit_abs_call (MonoCompile *cfg, MonoJumpInfoType patch_type, gconstpointer
        ((MonoCallInst*)ins)->fptr_is_patch = TRUE;
        return ins;
 }
+static MonoInst*
+mono_emit_widen_call_res (MonoCompile *cfg, MonoInst *ins, MonoMethodSignature *fsig)
+{
+       if (!MONO_TYPE_IS_VOID (fsig->ret)) {
+               if ((fsig->pinvoke || LLVM_ENABLED) && !fsig->ret->byref) {
+                       int widen_op = -1;
+
+                       /* 
+                        * Native code might return non register sized integers 
+                        * without initializing the upper bits.
+                        */
+                       switch (mono_type_to_load_membase (cfg, fsig->ret)) {
+                       case OP_LOADI1_MEMBASE:
+                               widen_op = OP_ICONV_TO_I1;
+                               break;
+                       case OP_LOADU1_MEMBASE:
+                               widen_op = OP_ICONV_TO_U1;
+                               break;
+                       case OP_LOADI2_MEMBASE:
+                               widen_op = OP_ICONV_TO_I2;
+                               break;
+                       case OP_LOADU2_MEMBASE:
+                               widen_op = OP_ICONV_TO_U2;
+                               break;
+                       default:
+                               break;
+                       }
+
+                       if (widen_op != -1) {
+                               int dreg = alloc_preg (cfg);
+                               MonoInst *widen;
+
+                               EMIT_NEW_UNALU (cfg, widen, widen_op, dreg, ins->dreg);
+                               widen->type = ins->type;
+                               ins = widen;
+                       }
+               }
+       }
+
+       return ins;
+}
 
 static MonoMethod*
 get_memcpy_method (void)
@@ -6234,7 +6276,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                }
 
                                if (!MONO_TYPE_IS_VOID (fsig->ret))
-                                       *sp++ = ins;
+                                       *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
 
                                ip += 5;
                                ins_flag = 0;
@@ -6431,43 +6473,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                                ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr);
                                        }
                                }
-                               if (!MONO_TYPE_IS_VOID (fsig->ret)) {
-                                       if (fsig->pinvoke && !fsig->ret->byref) {
-                                               int widen_op = -1;
-
-                                               /* 
-                                                * Native code might return non register sized integers 
-                                                * without initializing the upper bits.
-                                                */
-                                               switch (mono_type_to_load_membase (cfg, fsig->ret)) {
-                                               case OP_LOADI1_MEMBASE:
-                                                       widen_op = OP_ICONV_TO_I1;
-                                                       break;
-                                               case OP_LOADU1_MEMBASE:
-                                                       widen_op = OP_ICONV_TO_U1;
-                                                       break;
-                                               case OP_LOADI2_MEMBASE:
-                                                       widen_op = OP_ICONV_TO_I2;
-                                                       break;
-                                               case OP_LOADU2_MEMBASE:
-                                                       widen_op = OP_ICONV_TO_U2;
-                                                       break;
-                                               default:
-                                                       break;
-                                               }
-
-                                               if (widen_op != -1) {
-                                                       int dreg = alloc_preg (cfg);
-                                                       MonoInst *widen;
-
-                                                       EMIT_NEW_UNALU (cfg, widen, widen_op, dreg, ins->dreg);
-                                                       widen->type = ins->type;
-                                                       ins = widen;
-                                               }
-                                       }
-
-                                       *sp++ = ins;
-                               }
+                               if (!MONO_TYPE_IS_VOID (fsig->ret))
+                                       *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
 
                                ip += 5;
                                ins_flag = 0;
@@ -6515,7 +6522,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        ins = mini_redirect_call (cfg, cmethod, fsig, sp, virtual ? sp [0] : NULL);
                        if (ins) {
                                if (!MONO_TYPE_IS_VOID (fsig->ret))
-                                       *sp++ = ins;
+                                       *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
 
                                ip += 5;
                                ins_flag = 0;
@@ -6534,7 +6541,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        }
 
                        if (!MONO_TYPE_IS_VOID (fsig->ret))
-                               *sp++ = ins;
+                               *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
 
                        ip += 5;
                        ins_flag = 0;
index 6087c0f1a2616105abddbb9cf17fdf2d6b6cc866..59832badfb929648124fd0a1031937e686074e0f 100644 (file)
 
 #include <stdint.h>
 
+#include <llvm/PassManager.h>
 #include <llvm/ExecutionEngine/ExecutionEngine.h>
 #include <llvm/ExecutionEngine/JITMemoryManager.h>
 #include <llvm/Target/TargetOptions.h>
+#include <llvm/Target/TargetData.h>
+#include <llvm/Analysis/Verifier.h>
+#include <llvm/Transforms/Scalar.h>
 
 #include "llvm-c/Core.h"
 #include "llvm-c/ExecutionEngine.h"
@@ -162,7 +166,27 @@ MonoJITMemoryManager::endExceptionTable(const Function *F, unsigned char *TableS
 
 static MonoJITMemoryManager *mono_mm;
 
-extern "C" {
+static FunctionPassManager *fpm;
+
+void
+mono_llvm_optimize_method (LLVMValueRef method)
+{
+       verifyFunction (*(unwrap<Function> (method)));
+       fpm->run (*unwrap<Function> (method));
+}
+
+/* Missing overload for building an alloca with an alignment */
+LLVMValueRef
+mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty, 
+                                               LLVMValueRef ArraySize,
+                                               int alignment, const char *Name)
+{
+       return wrap (unwrap (builder)->Insert (new AllocaInst(unwrap (Ty), unwrap (ArraySize), alignment), Name));
+}
+
+LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef, LLVMTypeRef Ty,
+                                  LLVMValueRef Val, const char *Name);
+
 
 LLVMExecutionEngineRef
 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb)
@@ -178,7 +202,15 @@ mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, Func
   ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, false);
   EE->InstallExceptionTableRegister (exception_cb);
 
-  return wrap(EE);
-}
+  fpm = new FunctionPassManager (unwrap (MP));
+
+  fpm->add(new TargetData(*EE->getTargetData()));
+  /* Add a random set of passes */
+  /* Make this run-time configurable */
+  fpm->add(createInstructionCombiningPass());
+  fpm->add(createReassociatePass());
+  fpm->add(createGVNPass());
+  fpm->add(createCFGSimplificationPass());
 
+  return wrap(EE);
 }
index 9ce0f287b51a63ad572de86c5373d88f06e4fcc3..5080df23093863e16a79ea56d7597e017c27ec87 100644 (file)
@@ -24,6 +24,14 @@ typedef void (ExceptionTableCb) (void *data);
 LLVMExecutionEngineRef
 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb);
 
+void
+mono_llvm_optimize_method (LLVMValueRef method);
+
+LLVMValueRef
+mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty, 
+                                               LLVMValueRef ArraySize,
+                                               int alignment, const char *Name);
+
 G_END_DECLS
 
 #endif /* __MONO_MINI_LLVM_CPP_H__ */  
index 693d3512a3c924aed4f9ce20d65a019fd5914871..9954a7c8f7bdb300d7da8aeccbab76c769fdf2c9 100644 (file)
@@ -155,7 +155,7 @@ type_to_llvm_type (EmitContext *ctx, MonoType *t)
        case MONO_TYPE_SZARRAY:
        case MONO_TYPE_STRING:
        case MONO_TYPE_PTR:
-               return IntPtrType ();
+               return LLVMPointerType (IntPtrType (), 0);
        case MONO_TYPE_VAR:
        case MONO_TYPE_MVAR:
                /* Because of generic sharing */
@@ -430,6 +430,15 @@ convert (EmitContext *ctx, LLVMValueRef v, LLVMTypeRef dtype)
                if (stype == LLVMDoubleType () && dtype == LLVMFloatType ())
                        return LLVMBuildFPTrunc (ctx->builder, v, dtype, get_tempname (ctx));
 
+               if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind && LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
+                       return LLVMBuildBitCast (ctx->builder, v, dtype, get_tempname (ctx));
+               if (LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
+                       return LLVMBuildIntToPtr (ctx->builder, v, dtype, get_tempname (ctx));
+               if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind)
+                       return LLVMBuildPtrToInt (ctx->builder, v, dtype, get_tempname (ctx));
+
+               LLVMDumpValue (v);
+               LLVMDumpValue (LLVMConstNull (dtype));
                g_assert_not_reached ();
                return NULL;
        } else {
@@ -607,20 +616,6 @@ mono_llvm_emit_method (MonoCompile *cfg)
        ctx->cfg = cfg;
        ctx->mempool = cfg->mempool;
 
-#if 0
-       {
-               static int count = 0;
-               count ++;
-
-               if (count == atoi (getenv ("COUNT"))) {
-                       printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
-                       last = TRUE;
-               }
-               if (count > atoi (getenv ("COUNT")))
-                       LLVM_FAILURE (ctx, "");
-       }
-#endif
-
        values = g_new0 (LLVMValueRef, cfg->next_vreg);
        addresses = g_new0 (LLVMValueRef, cfg->next_vreg);
        vreg_types = g_new0 (LLVMTypeRef, cfg->next_vreg);
@@ -630,6 +625,22 @@ mono_llvm_emit_method (MonoCompile *cfg)
        ctx->values = values;
        ctx->addresses = addresses;
 
+#if 1
+       {
+               static int count = 0;
+               count ++;
+
+               if (getenv ("LLVM_COUNT")) {
+                       if (count == atoi (getenv ("LLVM_COUNT"))) {
+                               printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
+                               last = TRUE;
+                       }
+                       if (count > atoi (getenv ("LLVM_COUNT")))
+                               LLVM_FAILURE (ctx, "");
+               }
+       }
+#endif
+
        sig = mono_method_signature (cfg->method);
 
        method_type = sig_to_llvm_sig (ctx, sig, cfg->vret_addr != NULL);
@@ -908,7 +919,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
                                        g_assert (ins->inst_offset % size == 0);
                                        index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
 
-                                       lhs = LLVMBuildLoad (builder, LLVMBuildGEP (builder, LLVMBuildIntToPtr (builder, values [ins->inst_basereg], LLVMPointerType (t, 0), get_tempname (ctx)), &index, 1, get_tempname (ctx)), get_tempname (ctx));
+                                       lhs = LLVMBuildLoad (builder, LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (t, 0)), &index, 1, get_tempname (ctx)), get_tempname (ctx));
                                }
                                if (ins->opcode == OP_AMD64_ICOMPARE_MEMBASE_IMM) {
                                        lhs = convert (ctx, lhs, LLVMInt32Type ());
@@ -924,11 +935,20 @@ mono_llvm_emit_method (MonoCompile *cfg)
                                }
                                if (ins->opcode == OP_LCOMPARE_IMM)
                                        rhs = LLVMConstInt (LLVMInt64Type (), ins->inst_imm, FALSE);
+                               if (ins->opcode == OP_LCOMPARE)
+                                       rhs = convert (ctx, rhs, LLVMInt64Type ());
                                if (ins->opcode == OP_ICOMPARE) {
                                        lhs = convert (ctx, lhs, LLVMInt32Type ());
                                        rhs = convert (ctx, rhs, LLVMInt32Type ());
                                }
 
+                               if (lhs && rhs) {
+                                       if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
+                                               rhs = convert (ctx, rhs, LLVMTypeOf (lhs));
+                                       else if (LLVMGetTypeKind (LLVMTypeOf (rhs)) == LLVMPointerTypeKind)
+                                               lhs = convert (ctx, lhs, LLVMTypeOf (rhs));
+                               }
+
                                /* We use COMPARE+SETcc/Bcc, llvm uses SETcc+br cond */
                                if (ins->opcode == OP_FCOMPARE)
                                        cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMDoubleType ()), convert (ctx, rhs, LLVMDoubleType ()), get_tempname (ctx));
@@ -1150,6 +1170,8 @@ mono_llvm_emit_method (MonoCompile *cfg)
                                        imm = LLVMConstInt (LLVMInt64Type (), ins->inst_imm, FALSE);
                                else
                                        imm = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
+                               if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
+                                       lhs = convert (ctx, lhs, IntPtrType ());
                                imm = convert (ctx, imm, LLVMTypeOf (lhs));
                                switch (ins->opcode) {
                                case OP_IADD_IMM:
@@ -1311,7 +1333,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
                                guint32 size = ins->inst_imm;
                                size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
 
-                               v = LLVMBuildArrayAlloca (builder, LLVMInt8Type (), LLVMConstInt (LLVMInt32Type (), size, FALSE), get_tempname (ctx));
+                               v = mono_llvm_build_alloca (builder, LLVMInt8Type (), LLVMConstInt (LLVMInt32Type (), size, FALSE), MONO_ARCH_FRAME_ALIGNMENT, get_tempname (ctx));
 
                                if (ins->flags & MONO_INST_INIT) {
                                        LLVMValueRef args [4];
@@ -1323,7 +1345,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
                                        LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.memset.i32"), args, 4, "");
                                }
 
-                               values [ins->dreg] = LLVMBuildPtrToInt (builder, v, IntPtrType (), dname);
+                               values [ins->dreg] = v;
                                break;
                        }
                        case OP_LOADI1_MEMBASE:
@@ -1350,12 +1372,12 @@ mono_llvm_emit_method (MonoCompile *cfg)
 
                                g_assert (ins->inst_offset % size == 0);
                                if ((ins->opcode == OP_LOADI8_MEM) || (ins->opcode == OP_LOAD_MEM)) {
-                                       values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildIntToPtr (builder, LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), LLVMPointerType (t, 0), get_tempname (ctx)), dname);
+                                       values [ins->dreg] = LLVMBuildLoad (builder, convert (ctx, LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), LLVMPointerType (t, 0)), dname);
                                } else if (ins->inst_offset == 0) {
-                                       values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildIntToPtr (builder, values [ins->inst_basereg], LLVMPointerType (t, 0), get_tempname (ctx)), dname);
+                                       values [ins->dreg] = LLVMBuildLoad (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (t, 0)), dname);
                                } else {
                                        index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
-                                       values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildGEP (builder, LLVMBuildIntToPtr (builder, values [ins->inst_basereg], LLVMPointerType (t, 0), get_tempname (ctx)), &index, 1, get_tempname (ctx)), dname);
+                                       values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (t, 0)), &index, 1, get_tempname (ctx)), dname);
                                }
                                if (sext)
                                        values [ins->dreg] = LLVMBuildSExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
@@ -1365,7 +1387,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
                                        values [ins->dreg] = LLVMBuildFPExt (builder, values [ins->dreg], LLVMDoubleType (), dname);
                                break;
                        }
-
+                               
                        case OP_STOREI1_MEMBASE_REG:
                        case OP_STOREI2_MEMBASE_REG:
                        case OP_STOREI4_MEMBASE_REG:
@@ -1382,7 +1404,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
 
                                g_assert (ins->inst_offset % size == 0);
                                index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
-                               LLVMBuildStore (builder, convert (ctx, values [ins->sreg1], t), LLVMBuildGEP (builder, LLVMBuildIntToPtr (builder, values [ins->inst_destbasereg], LLVMPointerType (t, 0), get_tempname (ctx)), &index, 1, get_tempname (ctx)));
+                               LLVMBuildStore (builder, convert (ctx, values [ins->sreg1], t), LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, get_tempname (ctx)));
                                break;
                        }
 
@@ -1400,11 +1422,12 @@ mono_llvm_emit_method (MonoCompile *cfg)
 
                                g_assert (ins->inst_offset % size == 0);
                                index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
-                               LLVMBuildStore (builder, convert (ctx, LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE), t), LLVMBuildGEP (builder, LLVMBuildIntToPtr (builder, values [ins->inst_destbasereg], LLVMPointerType (t, 0), get_tempname (ctx)), &index, 1, get_tempname (ctx)));
+                               LLVMBuildStore (builder, convert (ctx, LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE), t), LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, get_tempname (ctx)));
                                break;
                        }
+
                        case OP_CHECK_THIS:
-                               LLVMBuildLoad (builder, LLVMBuildIntToPtr (builder, values [ins->sreg1], LLVMPointerType (IntPtrType (), 0), get_tempname (ctx)), get_tempname (ctx));
+                               LLVMBuildLoad (builder, convert (ctx, values [ins->sreg1], LLVMPointerType (IntPtrType (), 0)), get_tempname (ctx));
                                break;
                        case OP_OUTARG_VTRETADDR:
                                break;
@@ -1481,7 +1504,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
                                        g_assert (ins->inst_offset % size == 0);
                                        index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
 
-                                       callee = LLVMBuildIntToPtr (builder, LLVMBuildLoad (builder, LLVMBuildGEP (builder, LLVMBuildIntToPtr (builder, values [ins->inst_basereg], LLVMPointerType (IntPtrType (), 0), get_tempname (ctx)), &index, 1, get_tempname (ctx)), get_tempname (ctx)), LLVMPointerType (llvm_sig, 0), get_tempname (ctx));
+                                       callee = convert (ctx, LLVMBuildLoad (builder, LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (IntPtrType (), 0)), &index, 1, get_tempname (ctx)), get_tempname (ctx)), LLVMPointerType (llvm_sig, 0));
 
                                        // FIXME: mono_arch_get_vcall_slot () can't decode the code
                                        // generated by LLVM
@@ -1549,7 +1572,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
 
                                throw_sig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
                                throw_sig->ret = &mono_defaults.void_class->byval_arg;
-                               throw_sig->params [0] = &mono_defaults.int_class->byval_arg;
+                               throw_sig->params [0] = &mono_defaults.object_class->byval_arg;
                                // FIXME: Prevent duplicates
                                callee = LLVMAddFunction (module, "mono_arch_throw_exception", sig_to_llvm_sig (ctx, throw_sig, FALSE));
                                LLVMAddGlobalMapping (ee, callee, resolve_patch (cfg, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_exception"));
@@ -1568,7 +1591,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
                        case OP_LDADDR: {
                                MonoInst *var = ins->inst_p0;
 
-                               values [ins->dreg] = LLVMBuildPtrToInt (builder, addresses [var->dreg], IntPtrType (), dname);
+                               values [ins->dreg] = addresses [var->dreg];
                                break;
                        }
                        case OP_SIN: {
@@ -1585,6 +1608,8 @@ mono_llvm_emit_method (MonoCompile *cfg)
                                values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.cos.f64"), args, 1, dname);
                                break;
                        }
+                               /* test_0_sqrt_nan fails with LLVM */
+                               /*
                        case OP_SQRT: {
                                LLVMValueRef args [1];
 
@@ -1592,6 +1617,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
                                values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.sqrt.f64"), args, 1, dname);
                                break;
                        }
+                               */
 
                        case OP_IMIN:
                        case OP_LMIN: {
@@ -1622,7 +1648,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
 
                                g_assert (ins->inst_offset == 0);
 
-                               args [0] = LLVMBuildIntToPtr (builder, lhs, LLVMPointerType (LLVMInt32Type (), 0), get_tempname (ctx));
+                               args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt32Type (), 0));
                                args [1] = rhs;
                                values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.swap.i32.p0i32"), args, 2, dname);
                                break;
@@ -1632,7 +1658,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
 
                                g_assert (ins->inst_offset == 0);
 
-                               args [0] = LLVMBuildIntToPtr (builder, lhs, LLVMPointerType (LLVMInt64Type (), 0), get_tempname (ctx));
+                               args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt64Type (), 0));
                                args [1] = rhs;
                                values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.swap.i64.p0i64"), args, 2, dname);
                                break;
@@ -1642,7 +1668,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
 
                                g_assert (ins->inst_offset == 0);
 
-                               args [0] = LLVMBuildIntToPtr (builder, lhs, LLVMPointerType (LLVMInt32Type (), 0), get_tempname (ctx));
+                               args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt32Type (), 0));
                                args [1] = rhs;
                                values [ins->dreg] = LLVMBuildAdd (builder, LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.load.add.i32.p0i32"), args, 2, get_tempname (ctx)), args [1], dname);
                                break;
@@ -1652,7 +1678,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
 
                                g_assert (ins->inst_offset == 0);
 
-                               args [0] = LLVMBuildIntToPtr (builder, lhs, LLVMPointerType (LLVMInt64Type (), 0), get_tempname (ctx));
+                               args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt64Type (), 0));
                                args [1] = convert (ctx, rhs, LLVMInt64Type ());
                                values [ins->dreg] = LLVMBuildAdd (builder, LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.load.add.i64.p0i64"), args, 2, get_tempname (ctx)), args [1], dname);
                                break;
@@ -1661,7 +1687,7 @@ mono_llvm_emit_method (MonoCompile *cfg)
                        case OP_ATOMIC_CAS_IMM_I4: {
                                LLVMValueRef args [3];
 
-                               args [0] = LLVMBuildIntToPtr (builder, lhs, LLVMPointerType (LLVMInt32Type (), 0), get_tempname (ctx));
+                               args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt32Type (), 0));
                                /* comparand */
                                args [1] = LLVMConstInt (LLVMInt32Type (), GPOINTER_TO_INT (ins->backend.data), FALSE);
                                /* new value */
@@ -1752,12 +1778,12 @@ mono_llvm_emit_method (MonoCompile *cfg)
                                switch (ins->opcode) {
                                case OP_STOREV_MEMBASE:
                                        src = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMInt8Type (), 0), get_tempname (ctx));
-                                       dst = LLVMBuildIntToPtr (builder, LLVMBuildAdd (builder, values [ins->inst_destbasereg], LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), get_tempname (ctx)), LLVMPointerType (LLVMInt8Type (), 0), get_tempname (ctx));
+                                       dst = convert (ctx, LLVMBuildAdd (builder, values [ins->inst_destbasereg], LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), get_tempname (ctx)), LLVMPointerType (LLVMInt8Type (), 0));
                                        break;
                                case OP_LOADV_MEMBASE:
                                        if (!addresses [ins->dreg])
                                                addresses [ins->dreg] = LLVMBuildAlloca (builder, type_to_llvm_type (ctx, &klass->byval_arg), get_tempname (ctx));
-                                       src = LLVMBuildIntToPtr (builder, LLVMBuildAdd (builder, values [ins->inst_basereg], LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), get_tempname (ctx)), LLVMPointerType (LLVMInt8Type (), 0), get_tempname (ctx));
+                                       src = convert (ctx, LLVMBuildAdd (builder, values [ins->inst_basereg], LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), get_tempname (ctx)), LLVMPointerType (LLVMInt8Type (), 0));
                                        dst = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), get_tempname (ctx));
                                        break;
                                case OP_VMOVE:
@@ -1825,8 +1851,10 @@ mono_llvm_emit_method (MonoCompile *cfg)
        if (last)
                LLVMDumpValue (method);
 
-       //if (strstr (cfg->method->name, "test_0_ldfld_stfld_soft_float_remote"))
-       //LLVMDumpValue (method);
+       mono_llvm_optimize_method (method);
+
+       if (cfg->verbose_level > 1)
+               LLVMDumpValue (method);
 
        cfg->native_code = LLVMGetPointerToGlobal (ee, method);
 
index 77500e4147a801a2f3cb5b4d1a0cd4f86e1b42c6..3ae9814361478b89924ce2c7884db5d1b89ce9d3 100644 (file)
@@ -60,8 +60,10 @@ typedef gint64 mgreg_t;
 
 #if ENABLE_LLVM
 #define COMPILE_LLVM(cfg) ((cfg)->compile_llvm)
+#define LLVM_ENABLED TRUE
 #else
 #define COMPILE_LLVM(cfg) (0)
+#define LLVM_ENABLED FALSE
 #endif
 
 #define NOT_IMPLEMENTED do { g_assert_not_reached (); } while (0)