gallivm: add no-signed-zeros-fp-math option to lp_create_builder (v2)
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_init.c
index 982d1db32ccb25596f4009087ead16e544a27175..ef2580e8263568a472128a7f6563a0ec0f2e2165 100644 (file)
@@ -31,7 +31,8 @@
 #include "util/u_cpu_detect.h"
 #include "util/u_debug.h"
 #include "util/u_memory.h"
-#include "util/u_simple_list.h"
+#include "util/simple_list.h"
+#include "os/os_time.h"
 #include "lp_bld.h"
 #include "lp_bld_debug.h"
 #include "lp_bld_misc.h"
 #include <llvm-c/BitWriter.h>
 
 
-/**
- * AVX is supported in:
- * - standard JIT from LLVM 3.2 onwards
- * - MC-JIT from LLVM 3.1
- *   - MC-JIT supports limited OSes (MacOSX and Linux)
- * - standard JIT in LLVM 3.1, with backports
- */
-#if defined(PIPE_ARCH_PPC_64) || defined(PIPE_ARCH_S390) || defined(PIPE_ARCH_ARM) || defined(PIPE_ARCH_AARCH64)
+/* Only MCJIT is available as of LLVM SVN r216982 */
+#if HAVE_LLVM >= 0x0306
 #  define USE_MCJIT 1
-#  define HAVE_AVX 0
-#elif HAVE_LLVM >= 0x0302 || (HAVE_LLVM == 0x0301 && defined(HAVE_JIT_AVX_SUPPORT))
-#  define USE_MCJIT 0
-#  define HAVE_AVX 1
-#elif HAVE_LLVM == 0x0301 && (defined(PIPE_OS_LINUX) || defined(PIPE_OS_APPLE))
+#elif defined(PIPE_ARCH_PPC_64) || defined(PIPE_ARCH_S390) || defined(PIPE_ARCH_ARM) || defined(PIPE_ARCH_AARCH64)
 #  define USE_MCJIT 1
-#  define HAVE_AVX 1
-#else
-#  define USE_MCJIT 0
-#  define HAVE_AVX 0
 #endif
 
-
-#if USE_MCJIT
-void LLVMLinkInMCJIT();
+#if defined(USE_MCJIT)
+static const bool use_mcjit = USE_MCJIT;
+#else
+static bool use_mcjit = FALSE;
 #endif
 
-/*
- * LLVM has several global caches which pointing/derived from objects
- * owned by the context, so if we freeing contexts causes
- * memory leaks and false cache hits when these objects are destroyed.
- *
- * TODO: For thread safety on multi-threaded OpenGL we should use one LLVM
- * context per thread, and put them in a pool when threads are destroyed.
- */
-#define USE_GLOBAL_CONTEXT 1
-
 
 #ifdef DEBUG
 unsigned gallivm_debug = 0;
@@ -92,6 +70,7 @@ static const struct debug_named_value lp_bld_debug_flags[] = {
    { "no_rho_approx", GALLIVM_DEBUG_NO_RHO_APPROX, NULL },
    { "no_quad_lod", GALLIVM_DEBUG_NO_QUAD_LOD, NULL },
    { "gc",     GALLIVM_DEBUG_GC, NULL },
+   { "dumpbc", GALLIVM_DEBUG_DUMP_BC, NULL },
    DEBUG_NAMED_VALUE_END
 };
 
@@ -135,8 +114,40 @@ create_pass_manager(struct gallivm_state *gallivm)
    gallivm->passmgr = LLVMCreateFunctionPassManagerForModule(gallivm->module);
    if (!gallivm->passmgr)
       return FALSE;
+   /*
+    * TODO: some per module pass manager with IPO passes might be helpful -
+    * the generated texture functions may benefit from inlining if they are
+    * simple, or constant propagation into them, etc.
+    */
 
+#if HAVE_LLVM < 0x0309
+   // Old versions of LLVM get the DataLayout from the pass manager.
    LLVMAddTargetData(gallivm->target, gallivm->passmgr);
+#endif
+
+   /* Setting the module's DataLayout to an empty string will cause the
+    * ExecutionEngine to copy to the DataLayout string from its target
+    * machine to the module.  As of LLVM 3.8 the module and the execution
+    * engine are required to have the same DataLayout.
+    *
+    * TODO: This is just a temporary work-around.  The correct solution is
+    * for gallivm_init_state() to create a TargetMachine and pull the
+    * DataLayout from there.  Currently, the TargetMachine used by llvmpipe
+    * is being implicitly created by the EngineBuilder in
+    * lp_build_create_jit_compiler_for_module()
+    */
+
+#if HAVE_LLVM < 0x0308
+   {
+      char *td_str;
+      // New ones from the Module.
+      td_str = LLVMCopyStringRepOfTargetData(gallivm->target);
+      LLVMSetDataLayout(gallivm->module, td_str);
+      free(td_str);
+   }
+#else
+   LLVMSetDataLayout(gallivm->module, "");
+#endif
 
    if ((gallivm_debug & GALLIVM_DEBUG_NO_OPT) == 0) {
       /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
@@ -147,27 +158,9 @@ create_pass_manager(struct gallivm_state *gallivm)
       LLVMAddLICMPass(gallivm->passmgr);
       LLVMAddCFGSimplificationPass(gallivm->passmgr);
       LLVMAddReassociatePass(gallivm->passmgr);
-
-      if (sizeof(void*) == 4) {
-         /* XXX: For LLVM >= 2.7 and 32-bit build, use this order of passes to
-          * avoid generating bad code.
-          * Test with piglit glsl-vs-sqrt-zero test.
-          */
-         LLVMAddConstantPropagationPass(gallivm->passmgr);
-         LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
-      }
-      else {
-         LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
-         LLVMAddConstantPropagationPass(gallivm->passmgr);
-      }
-
-      if (util_cpu_caps.has_sse4_1) {
-         /* FIXME: There is a bug in this pass, whereby the combination
-          * of fptosi and sitofp (necessary for trunc/floor/ceil/round
-          * implementation) somehow becomes invalid code.
-          */
-         LLVMAddInstructionCombiningPass(gallivm->passmgr);
-      }
+      LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
+      LLVMAddConstantPropagationPass(gallivm->passmgr);
+      LLVMAddInstructionCombiningPass(gallivm->passmgr);
       LLVMAddGVNPass(gallivm->passmgr);
    }
    else {
@@ -182,10 +175,11 @@ create_pass_manager(struct gallivm_state *gallivm)
 
 
 /**
- * Free gallivm object's LLVM allocations, but not the gallivm object itself.
+ * Free gallivm object's LLVM allocations, but not any generated code
+ * nor the gallivm object itself.
  */
-static void
-free_gallivm_state(struct gallivm_state *gallivm)
+void
+gallivm_free_ir(struct gallivm_state *gallivm)
 {
    if (gallivm->passmgr) {
       LLVMDisposePassManager(gallivm->passmgr);
@@ -198,29 +192,43 @@ free_gallivm_state(struct gallivm_state *gallivm)
       LLVMDisposeModule(gallivm->module);
    }
 
-#if !USE_MCJIT
-   /* Don't free the TargetData, it's owned by the exec engine */
-#else
-   if (gallivm->target) {
-      LLVMDisposeTargetData(gallivm->target);
+   FREE(gallivm->module_name);
+
+   if (!use_mcjit) {
+      /* Don't free the TargetData, it's owned by the exec engine */
+   } else {
+      if (gallivm->target) {
+         LLVMDisposeTargetData(gallivm->target);
+      }
    }
-#endif
 
    if (gallivm->builder)
       LLVMDisposeBuilder(gallivm->builder);
 
-   if (!USE_GLOBAL_CONTEXT && gallivm->context)
-      LLVMContextDispose(gallivm->context);
-
-   lp_free_generated_code(gallivm->code);
+   /* The LLVMContext should be owned by the parent of gallivm. */
 
    gallivm->engine = NULL;
    gallivm->target = NULL;
    gallivm->module = NULL;
+   gallivm->module_name = NULL;
    gallivm->passmgr = NULL;
    gallivm->context = NULL;
    gallivm->builder = NULL;
+}
+
+
+/**
+ * Free LLVM-generated code.  Should be done AFTER gallivm_free_ir().
+ */
+static void
+gallivm_free_code(struct gallivm_state *gallivm)
+{
+   assert(!gallivm->module);
+   assert(!gallivm->engine);
+   lp_free_generated_code(gallivm->code);
    gallivm->code = NULL;
+   lp_free_memory_manager(gallivm->memorymgr);
+   gallivm->memorymgr = NULL;
 }
 
 
@@ -242,8 +250,9 @@ init_gallivm_engine(struct gallivm_state *gallivm)
       ret = lp_build_create_jit_compiler_for_module(&gallivm->engine,
                                                     &gallivm->code,
                                                     gallivm->module,
+                                                    gallivm->memorymgr,
                                                     (unsigned) optlevel,
-                                                    USE_MCJIT,
+                                                    use_mcjit,
                                                     &error);
       if (ret) {
          _debug_printf("%s\n", error);
@@ -252,32 +261,32 @@ init_gallivm_engine(struct gallivm_state *gallivm)
       }
    }
 
-#if !USE_MCJIT
-   gallivm->target = LLVMGetExecutionEngineTargetData(gallivm->engine);
-   if (!gallivm->target)
-      goto fail;
-#else
-   if (0) {
-       /*
-        * Dump the data layout strings.
-        */
+   if (!use_mcjit) {
+      gallivm->target = LLVMGetExecutionEngineTargetData(gallivm->engine);
+      if (!gallivm->target)
+         goto fail;
+   } else {
+      if (0) {
+          /*
+           * Dump the data layout strings.
+           */
 
-       LLVMTargetDataRef target = LLVMGetExecutionEngineTargetData(gallivm->engine);
-       char *data_layout;
-       char *engine_data_layout;
+          LLVMTargetDataRef target = LLVMGetExecutionEngineTargetData(gallivm->engine);
+          char *data_layout;
+          char *engine_data_layout;
 
-       data_layout = LLVMCopyStringRepOfTargetData(gallivm->target);
-       engine_data_layout = LLVMCopyStringRepOfTargetData(target);
+          data_layout = LLVMCopyStringRepOfTargetData(gallivm->target);
+          engine_data_layout = LLVMCopyStringRepOfTargetData(target);
 
-       if (1) {
-          debug_printf("module target data = %s\n", data_layout);
-          debug_printf("engine target data = %s\n", engine_data_layout);
-       }
+          if (1) {
+             debug_printf("module target data = %s\n", data_layout);
+             debug_printf("engine target data = %s\n", engine_data_layout);
+          }
 
-       free(data_layout);
-       free(engine_data_layout);
+          free(data_layout);
+          free(engine_data_layout);
+      }
    }
-#endif
 
    return TRUE;
 
@@ -291,22 +300,30 @@ fail:
  * \return  TRUE for success, FALSE for failure
  */
 static boolean
-init_gallivm_state(struct gallivm_state *gallivm)
+init_gallivm_state(struct gallivm_state *gallivm, const char *name,
+                   LLVMContextRef context)
 {
    assert(!gallivm->context);
    assert(!gallivm->module);
 
-   lp_build_init();
+   if (!lp_build_init())
+      return FALSE;
+
+   gallivm->context = context;
 
-   if (USE_GLOBAL_CONTEXT) {
-      gallivm->context = LLVMGetGlobalContext();
-   } else {
-      gallivm->context = LLVMContextCreate();
-   }
    if (!gallivm->context)
       goto fail;
 
-   gallivm->module = LLVMModuleCreateWithNameInContext("gallivm",
+   gallivm->module_name = NULL;
+   if (name) {
+      size_t size = strlen(name) + 1;
+      gallivm->module_name = MALLOC(size);
+      if (gallivm->module_name) {
+         memcpy(gallivm->module_name, name, size);
+      }
+   }
+
+   gallivm->module = LLVMModuleCreateWithNameInContext(name,
                                                        gallivm->context);
    if (!gallivm->module)
       goto fail;
@@ -315,50 +332,54 @@ init_gallivm_state(struct gallivm_state *gallivm)
    if (!gallivm->builder)
       goto fail;
 
+   gallivm->memorymgr = lp_get_default_memory_manager();
+   if (!gallivm->memorymgr)
+      goto fail;
+
    /* FIXME: MC-JIT only allows compiling one module at a time, and it must be
     * complete when MC-JIT is created. So defer the MC-JIT engine creation for
     * now.
     */
-#if !USE_MCJIT
-   if (!init_gallivm_engine(gallivm)) {
-      goto fail;
-   }
-#else
-   /*
-    * MC-JIT engine compiles the module immediately on creation, so we can't
-    * obtain the target data from it.  Instead we create a target data layout
-    * from a string.
-    *
-    * The produced layout strings are not precisely the same, but should make
-    * no difference for the kind of optimization passes we run.
-    *
-    * For reference this is the layout string on x64:
-    *
-    *   e-p:64:64:64-S128-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f16:16:16-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-f128:128:128-n8:16:32:64
-    *
-    * See also:
-    * - http://llvm.org/docs/LangRef.html#datalayout
-    */
+   if (!use_mcjit) {
+      if (!init_gallivm_engine(gallivm)) {
+         goto fail;
+      }
+   } else {
+      /*
+       * MC-JIT engine compiles the module immediately on creation, so we can't
+       * obtain the target data from it.  Instead we create a target data layout
+       * from a string.
+       *
+       * The produced layout strings are not precisely the same, but should make
+       * no difference for the kind of optimization passes we run.
+       *
+       * For reference this is the layout string on x64:
+       *
+       *   e-p:64:64:64-S128-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f16:16:16-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-f128:128:128-n8:16:32:64
+       *
+       * See also:
+       * - http://llvm.org/docs/LangRef.html#datalayout
+       */
 
-   {
-      const unsigned pointer_size = 8 * sizeof(void *);
-      char layout[512];
-      util_snprintf(layout, sizeof layout, "%c-p:%u:%u:%u-i64:64:64-a0:0:%u-s0:%u:%u",
+      {
+         const unsigned pointer_size = 8 * sizeof(void *);
+         char layout[512];
+         util_snprintf(layout, sizeof layout, "%c-p:%u:%u:%u-i64:64:64-a0:0:%u-s0:%u:%u",
 #ifdef PIPE_ARCH_LITTLE_ENDIAN
-                    'e', // little endian
+                       'e', // little endian
 #else
-                    'E', // big endian
+                       'E', // big endian
 #endif
-                    pointer_size, pointer_size, pointer_size, // pointer size, abi alignment, preferred alignment
-                    pointer_size, // aggregate preferred alignment
-                    pointer_size, pointer_size); // stack objects abi alignment, preferred alignment
-
-      gallivm->target = LLVMCreateTargetData(layout);
-      if (!gallivm->target) {
-         return FALSE;
+                       pointer_size, pointer_size, pointer_size, // pointer size, abi alignment, preferred alignment
+                       pointer_size, // aggregate preferred alignment
+                       pointer_size, pointer_size); // stack objects abi alignment, preferred alignment
+
+         gallivm->target = LLVMCreateTargetData(layout);
+         if (!gallivm->target) {
+            return FALSE;
+         }
       }
    }
-#endif
 
    if (!create_pass_manager(gallivm))
       goto fail;
@@ -366,16 +387,34 @@ init_gallivm_state(struct gallivm_state *gallivm)
    return TRUE;
 
 fail:
-   free_gallivm_state(gallivm);
+   gallivm_free_ir(gallivm);
+   gallivm_free_code(gallivm);
    return FALSE;
 }
 
 
-void
+boolean
 lp_build_init(void)
 {
    if (gallivm_initialized)
-      return;
+      return TRUE;
+
+
+   /* LLVMLinkIn* are no-ops at runtime.  They just ensure the respective
+    * component is linked at buildtime, which is sufficient for its static
+    * constructors to be called at load time.
+    */
+#if defined(USE_MCJIT)
+#  if USE_MCJIT
+      LLVMLinkInMCJIT();
+#  else
+      LLVMLinkInJIT();
+#  endif
+#else
+   use_mcjit = debug_get_bool_option("GALLIVM_MCJIT", FALSE);
+   LLVMLinkInJIT();
+   LLVMLinkInMCJIT();
+#endif
 
 #ifdef DEBUG
    gallivm_debug = debug_get_option_gallivm_debug();
@@ -383,14 +422,23 @@ lp_build_init(void)
 
    lp_set_target_options();
 
-#if USE_MCJIT
-   LLVMLinkInMCJIT();
-#else
-   LLVMLinkInJIT();
-#endif
-
    util_cpu_detect();
 
+   /* For simulating less capable machines */
+#ifdef DEBUG
+   if (debug_get_bool_option("LP_FORCE_SSE2", FALSE)) {
+      assert(util_cpu_caps.has_sse2);
+      util_cpu_caps.has_sse3 = 0;
+      util_cpu_caps.has_ssse3 = 0;
+      util_cpu_caps.has_sse4_1 = 0;
+      util_cpu_caps.has_sse4_2 = 0;
+      util_cpu_caps.has_avx = 0;
+      util_cpu_caps.has_avx2 = 0;
+      util_cpu_caps.has_f16c = 0;
+      util_cpu_caps.has_fma = 0;
+   }
+#endif
+
    /* AMD Bulldozer AVX's throughput is the same as SSE2; and because using
     * 8-wide vector needs more floating ops than 4-wide (due to padding), it is
     * actually more efficient to use 4-wide vectors on this processor.
@@ -398,8 +446,7 @@ lp_build_init(void)
     * See also:
     * - http://www.anandtech.com/show/4955/the-bulldozer-review-amd-fx8150-tested/2
     */
-   if (HAVE_AVX &&
-       util_cpu_caps.has_avx &&
+   if (util_cpu_caps.has_avx &&
        util_cpu_caps.has_intel) {
       lp_native_vector_width = 256;
    } else {
@@ -422,16 +469,13 @@ lp_build_init(void)
        */
       util_cpu_caps.has_avx = 0;
       util_cpu_caps.has_avx2 = 0;
-   }
-
-   if (!HAVE_AVX) {
-      /*
-       * note these instructions are VEX-only, so can only emit if we use
-       * avx (don't want to base it on has_avx & has_f16c later as that would
-       * omit it unnecessarily on amd cpus, see above).
-       */
       util_cpu_caps.has_f16c = 0;
-      util_cpu_caps.has_xop = 0;
+      util_cpu_caps.has_fma = 0;
+   }
+   if (HAVE_LLVM < 0x0304 || !use_mcjit) {
+      /* AVX2 support has only been tested with LLVM 3.4, and it requires
+       * MCJIT. */
+      util_cpu_caps.has_avx2 = 0;
    }
 
 #ifdef PIPE_ARCH_PPC_64
@@ -458,30 +502,22 @@ lp_build_init(void)
 
    gallivm_initialized = TRUE;
 
-#if 0
-   /* For simulating less capable machines */
-   util_cpu_caps.has_sse3 = 0;
-   util_cpu_caps.has_ssse3 = 0;
-   util_cpu_caps.has_sse4_1 = 0;
-   util_cpu_caps.has_avx = 0;
-   util_cpu_caps.has_f16c = 0;
-#endif
+   return TRUE;
 }
 
 
 
 /**
  * Create a new gallivm_state object.
- * Note that we return a singleton.
  */
 struct gallivm_state *
-gallivm_create(void)
+gallivm_create(const char *name, LLVMContextRef context)
 {
    struct gallivm_state *gallivm;
 
    gallivm = CALLOC_STRUCT(gallivm_state);
    if (gallivm) {
-      if (!init_gallivm_state(gallivm)) {
+      if (!init_gallivm_state(gallivm, name, context)) {
          FREE(gallivm);
          gallivm = NULL;
       }
@@ -497,13 +533,15 @@ gallivm_create(void)
 void
 gallivm_destroy(struct gallivm_state *gallivm)
 {
-   free_gallivm_state(gallivm);
+   gallivm_free_ir(gallivm);
+   gallivm_free_code(gallivm);
    FREE(gallivm);
 }
 
 
 /**
  * Validate a function.
+ * Verification is only done with debug builds.
  */
 void
 gallivm_verify_function(struct gallivm_state *gallivm,
@@ -526,10 +564,15 @@ gallivm_verify_function(struct gallivm_state *gallivm,
 }
 
 
+/**
+ * Compile a module.
+ * This does IR optimization on all functions in the module.
+ */
 void
 gallivm_compile_module(struct gallivm_state *gallivm)
 {
    LLVMValueRef func;
+   int64_t time_begin = 0;
 
    assert(!gallivm->compiled);
 
@@ -538,34 +581,92 @@ gallivm_compile_module(struct gallivm_state *gallivm)
       gallivm->builder = NULL;
    }
 
+   if (gallivm_debug & GALLIVM_DEBUG_PERF)
+      time_begin = os_time_get();
+
    /* Run optimization passes */
    LLVMInitializeFunctionPassManager(gallivm->passmgr);
    func = LLVMGetFirstFunction(gallivm->module);
    while (func) {
       if (0) {
-        debug_printf("optimizing %s...\n", LLVMGetValueName(func));
+         debug_printf("optimizing func %s...\n", LLVMGetValueName(func));
       }
+
+   /* Disable frame pointer omission on debug/profile builds */
+   /* XXX: And workaround http://llvm.org/PR21435 */
+#if HAVE_LLVM >= 0x0307 && \
+    (defined(DEBUG) || defined(PROFILE) || \
+     defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64))
+      LLVMAddTargetDependentFunctionAttr(func, "no-frame-pointer-elim", "true");
+      LLVMAddTargetDependentFunctionAttr(func, "no-frame-pointer-elim-non-leaf", "true");
+#endif
+
       LLVMRunFunctionPassManager(gallivm->passmgr, func);
       func = LLVMGetNextFunction(func);
    }
    LLVMFinalizeFunctionPassManager(gallivm->passmgr);
 
+   if (gallivm_debug & GALLIVM_DEBUG_PERF) {
+      int64_t time_end = os_time_get();
+      int time_msec = (int)(time_end - time_begin) / 1000;
+      assert(gallivm->module_name);
+      debug_printf("optimizing module %s took %d msec\n",
+                   gallivm->module_name, time_msec);
+   }
+
    /* Dump byte code to a file */
-   if (0) {
-      LLVMWriteBitcodeToFile(gallivm->module, "llvmpipe.bc");
-      debug_printf("llvmpipe.bc written\n");
-      debug_printf("Invoke as \"llc -o - llvmpipe.bc\"\n");
+   if (gallivm_debug & GALLIVM_DEBUG_DUMP_BC) {
+      char filename[256];
+      assert(gallivm->module_name);
+      util_snprintf(filename, sizeof(filename), "ir_%s.bc", gallivm->module_name);
+      LLVMWriteBitcodeToFile(gallivm->module, filename);
+      debug_printf("%s written\n", filename);
+      debug_printf("Invoke as \"llc %s%s -o - %s\"\n",
+                   (HAVE_LLVM >= 0x0305) ? "[-mcpu=<-mcpu option] " : "",
+                   "[-mattr=<-mattr option(s)>]",
+                   filename);
    }
 
-#if USE_MCJIT
-   assert(!gallivm->engine);
-   if (!init_gallivm_engine(gallivm)) {
-      assert(0);
+   if (use_mcjit) {
+      assert(!gallivm->engine);
+      if (!init_gallivm_engine(gallivm)) {
+         assert(0);
+      }
    }
-#endif
    assert(gallivm->engine);
 
    ++gallivm->compiled;
+
+   if (gallivm_debug & GALLIVM_DEBUG_ASM) {
+      LLVMValueRef llvm_func = LLVMGetFirstFunction(gallivm->module);
+
+      while (llvm_func) {
+         /*
+          * Need to filter out functions which don't have an implementation,
+          * such as the intrinsics. May not be sufficient in case of IPO?
+          * LLVMGetPointerToGlobal() will abort otherwise.
+          */
+         if (!LLVMIsDeclaration(llvm_func)) {
+            void *func_code = LLVMGetPointerToGlobal(gallivm->engine, llvm_func);
+            lp_disassemble(llvm_func, func_code);
+         }
+         llvm_func = LLVMGetNextFunction(llvm_func);
+      }
+   }
+
+#if defined(PROFILE)
+   {
+      LLVMValueRef llvm_func = LLVMGetFirstFunction(gallivm->module);
+
+      while (llvm_func) {
+         if (!LLVMIsDeclaration(llvm_func)) {
+            void *func_code = LLVMGetPointerToGlobal(gallivm->engine, llvm_func);
+            lp_profile(llvm_func, func_code);
+         }
+         llvm_func = LLVMGetNextFunction(llvm_func);
+      }
+   }
+#endif
 }
 
 
@@ -576,42 +677,24 @@ gallivm_jit_function(struct gallivm_state *gallivm,
 {
    void *code;
    func_pointer jit_func;
+   int64_t time_begin = 0;
 
    assert(gallivm->compiled);
    assert(gallivm->engine);
 
+   if (gallivm_debug & GALLIVM_DEBUG_PERF)
+      time_begin = os_time_get();
+
    code = LLVMGetPointerToGlobal(gallivm->engine, func);
    assert(code);
    jit_func = pointer_to_func(code);
 
-   if (gallivm_debug & GALLIVM_DEBUG_ASM) {
-      lp_disassemble(func, code);
+   if (gallivm_debug & GALLIVM_DEBUG_PERF) {
+      int64_t time_end = os_time_get();
+      int time_msec = (int)(time_end - time_begin) / 1000;
+      debug_printf("   jitting func %s took %d msec\n",
+                   LLVMGetValueName(func), time_msec);
    }
 
-#if defined(PROFILE)
-   lp_profile(func, code);
-#endif
-
-   /* Free the function body to save memory */
-   lp_func_delete_body(func);
-
    return jit_func;
 }
-
-
-/**
- * Free the function (and its machine code).
- */
-void
-gallivm_free_function(struct gallivm_state *gallivm,
-                      LLVMValueRef func,
-                      const void *code)
-{
-#if !USE_MCJIT
-   if (code) {
-      LLVMFreeMachineCodeForFunction(gallivm->engine, func);
-   }
-
-   LLVMDeleteFunction(func);
-#endif
-}