Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2009 VMware, Inc.
  4.  * All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the
  8.  * "Software"), to deal in the Software without restriction, including
  9.  * without limitation the rights to use, copy, modify, merge, publish,
  10.  * distribute, sub license, and/or sell copies of the Software, and to
  11.  * permit persons to whom the Software is furnished to do so, subject to
  12.  * the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice (including the
  15.  * next paragraph) shall be included in all copies or substantial portions
  16.  * of the Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21.  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
  22.  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  *
  26.  **************************************************************************/
  27.  
  28.  
  29. #include "pipe/p_config.h"
  30. #include "pipe/p_compiler.h"
  31. #include "util/u_cpu_detect.h"
  32. #include "util/u_debug.h"
  33. #include "util/u_memory.h"
  34. #include "util/u_simple_list.h"
  35. #include "lp_bld.h"
  36. #include "lp_bld_debug.h"
  37. #include "lp_bld_misc.h"
  38. #include "lp_bld_init.h"
  39.  
  40. #include <llvm-c/Analysis.h>
  41. #include <llvm-c/Transforms/Scalar.h>
  42. #include <llvm-c/BitWriter.h>
  43.  
  44.  
  45. /**
  46.  * AVX is supported in:
  47.  * - standard JIT from LLVM 3.2 onwards
  48.  * - MC-JIT from LLVM 3.1
  49.  *   - MC-JIT supports limited OSes (MacOSX and Linux)
  50.  * - standard JIT in LLVM 3.1, with backports
  51.  */
  52. #if defined(PIPE_ARCH_PPC_64) || defined(PIPE_ARCH_S390) || defined(PIPE_ARCH_ARM) || defined(PIPE_ARCH_AARCH64)
  53. #  define USE_MCJIT 1
  54. #  define HAVE_AVX 0
  55. #elif HAVE_LLVM >= 0x0302 || (HAVE_LLVM == 0x0301 && defined(HAVE_JIT_AVX_SUPPORT))
  56. #  define USE_MCJIT 0
  57. #  define HAVE_AVX 1
  58. #elif HAVE_LLVM == 0x0301 && (defined(PIPE_OS_LINUX) || defined(PIPE_OS_APPLE))
  59. #  define USE_MCJIT 1
  60. #  define HAVE_AVX 1
  61. #else
  62. #  define USE_MCJIT 0
  63. #  define HAVE_AVX 0
  64. #endif
  65.  
  66.  
  67. #if USE_MCJIT
  68. void LLVMLinkInMCJIT();
  69. #endif
  70.  
  71.  
  72. #ifdef DEBUG
  73. unsigned gallivm_debug = 0;
  74.  
  75. static const struct debug_named_value lp_bld_debug_flags[] = {
  76.    { "tgsi",   GALLIVM_DEBUG_TGSI, NULL },
  77.    { "ir",     GALLIVM_DEBUG_IR, NULL },
  78.    { "asm",    GALLIVM_DEBUG_ASM, NULL },
  79.    { "nopt",   GALLIVM_DEBUG_NO_OPT, NULL },
  80.    { "perf",   GALLIVM_DEBUG_PERF, NULL },
  81.    { "no_brilinear", GALLIVM_DEBUG_NO_BRILINEAR, NULL },
  82.    { "no_rho_approx", GALLIVM_DEBUG_NO_RHO_APPROX, NULL },
  83.    { "gc",     GALLIVM_DEBUG_GC, NULL },
  84.    DEBUG_NAMED_VALUE_END
  85. };
  86.  
  87. DEBUG_GET_ONCE_FLAGS_OPTION(gallivm_debug, "GALLIVM_DEBUG", lp_bld_debug_flags, 0)
  88. #endif
  89.  
  90.  
  91. static boolean gallivm_initialized = FALSE;
  92.  
  93. unsigned lp_native_vector_width;
  94.  
  95.  
  96. /*
  97.  * Optimization values are:
  98.  * - 0: None (-O0)
  99.  * - 1: Less (-O1)
  100.  * - 2: Default (-O2, -Os)
  101.  * - 3: Aggressive (-O3)
  102.  *
  103.  * See also CodeGenOpt::Level in llvm/Target/TargetMachine.h
  104.  */
  105. enum LLVM_CodeGenOpt_Level {
  106. #if HAVE_LLVM >= 0x207
  107.    None,        // -O0
  108.    Less,        // -O1
  109.    Default,     // -O2, -Os
  110.    Aggressive   // -O3
  111. #else
  112.    Default,
  113.    None,
  114.    Aggressive
  115. #endif
  116. };
  117.  
  118.  
  119. #if HAVE_LLVM <= 0x0206
  120. /**
  121.  * LLVM 2.6 permits only one ExecutionEngine to be created.  So use the
  122.  * same gallivm state everywhere.
  123.  */
  124. static struct gallivm_state *GlobalGallivm = NULL;
  125. #endif
  126.  
  127.  
  128. /**
  129.  * Create the LLVM (optimization) pass manager and install
  130.  * relevant optimization passes.
  131.  * \return  TRUE for success, FALSE for failure
  132.  */
  133. static boolean
  134. create_pass_manager(struct gallivm_state *gallivm)
  135. {
  136.    assert(!gallivm->passmgr);
  137.    assert(gallivm->target);
  138.  
  139.    gallivm->passmgr = LLVMCreateFunctionPassManager(gallivm->provider);
  140.    if (!gallivm->passmgr)
  141.       return FALSE;
  142.  
  143.    LLVMAddTargetData(gallivm->target, gallivm->passmgr);
  144.  
  145.    if ((gallivm_debug & GALLIVM_DEBUG_NO_OPT) == 0) {
  146.       /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
  147.        * but there are more on SVN.
  148.        * TODO: Add more passes.
  149.        */
  150.       LLVMAddScalarReplAggregatesPass(gallivm->passmgr);
  151.       LLVMAddLICMPass(gallivm->passmgr);
  152.       LLVMAddCFGSimplificationPass(gallivm->passmgr);
  153.       LLVMAddReassociatePass(gallivm->passmgr);
  154.  
  155.       if (HAVE_LLVM >= 0x207 && sizeof(void*) == 4) {
  156.          /* For LLVM >= 2.7 and 32-bit build, use this order of passes to
  157.           * avoid generating bad code.
  158.           * Test with piglit glsl-vs-sqrt-zero test.
  159.           */
  160.          LLVMAddConstantPropagationPass(gallivm->passmgr);
  161.          LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
  162.       }
  163.       else {
  164.          LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
  165.          LLVMAddConstantPropagationPass(gallivm->passmgr);
  166.       }
  167.  
  168.       if (util_cpu_caps.has_sse4_1) {
  169.          /* FIXME: There is a bug in this pass, whereby the combination
  170.           * of fptosi and sitofp (necessary for trunc/floor/ceil/round
  171.           * implementation) somehow becomes invalid code.
  172.           */
  173.          LLVMAddInstructionCombiningPass(gallivm->passmgr);
  174.       }
  175.       LLVMAddGVNPass(gallivm->passmgr);
  176.    }
  177.    else {
  178.       /* We need at least this pass to prevent the backends to fail in
  179.        * unexpected ways.
  180.        */
  181.       LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
  182.    }
  183.  
  184.    return TRUE;
  185. }
  186.  
  187.  
  188. /**
  189.  * Free gallivm object's LLVM allocations, but not the gallivm object itself.
  190.  */
  191. static void
  192. free_gallivm_state(struct gallivm_state *gallivm)
  193. {
  194. #if HAVE_LLVM >= 0x207 /* XXX or 0x208? */
  195.    /* This leads to crashes w/ some versions of LLVM */
  196.    LLVMModuleRef mod;
  197.    char *error;
  198.  
  199.    if (gallivm->engine && gallivm->provider)
  200.       LLVMRemoveModuleProvider(gallivm->engine, gallivm->provider,
  201.                                &mod, &error);
  202. #endif
  203.  
  204.    if (gallivm->passmgr) {
  205.       LLVMDisposePassManager(gallivm->passmgr);
  206.    }
  207.  
  208. #if 0
  209.    /* XXX this seems to crash with all versions of LLVM */
  210.    if (gallivm->provider)
  211.       LLVMDisposeModuleProvider(gallivm->provider);
  212. #endif
  213.  
  214.    if (HAVE_LLVM >= 0x207 && gallivm->engine) {
  215.       /* This will already destroy any associated module */
  216.       LLVMDisposeExecutionEngine(gallivm->engine);
  217.    } else {
  218.       LLVMDisposeModule(gallivm->module);
  219.    }
  220.  
  221. #if !USE_MCJIT
  222.    /* Don't free the TargetData, it's owned by the exec engine */
  223. #else
  224.    if (gallivm->target) {
  225.       LLVMDisposeTargetData(gallivm->target);
  226.    }
  227. #endif
  228.  
  229.    /* Never free the LLVM context.
  230.     */
  231. #if 0
  232.    if (gallivm->context)
  233.       LLVMContextDispose(gallivm->context);
  234. #endif
  235.  
  236.    if (gallivm->builder)
  237.       LLVMDisposeBuilder(gallivm->builder);
  238.  
  239.    gallivm->engine = NULL;
  240.    gallivm->target = NULL;
  241.    gallivm->module = NULL;
  242.    gallivm->provider = NULL;
  243.    gallivm->passmgr = NULL;
  244.    gallivm->context = NULL;
  245.    gallivm->builder = NULL;
  246. }
  247.  
  248.  
  249. static boolean
  250. init_gallivm_engine(struct gallivm_state *gallivm)
  251. {
  252.    if (1) {
  253.       /* We can only create one LLVMExecutionEngine (w/ LLVM 2.6 anyway) */
  254.       enum LLVM_CodeGenOpt_Level optlevel;
  255.       char *error = NULL;
  256.       int ret;
  257.  
  258.       if (gallivm_debug & GALLIVM_DEBUG_NO_OPT) {
  259.          optlevel = None;
  260.       }
  261.       else {
  262.          optlevel = Default;
  263.       }
  264.  
  265. #if HAVE_LLVM >= 0x0301
  266.       ret = lp_build_create_jit_compiler_for_module(&gallivm->engine,
  267.                                                     gallivm->module,
  268.                                                     (unsigned) optlevel,
  269.                                                     USE_MCJIT,
  270.                                                     &error);
  271. #else
  272.       ret = LLVMCreateJITCompiler(&gallivm->engine, gallivm->provider,
  273.                                   (unsigned) optlevel, &error);
  274. #endif
  275.       if (ret) {
  276.          _debug_printf("%s\n", error);
  277.          LLVMDisposeMessage(error);
  278.          goto fail;
  279.       }
  280.    }
  281.  
  282.    LLVMAddModuleProvider(gallivm->engine, gallivm->provider);//new
  283.  
  284. #if !USE_MCJIT
  285.    gallivm->target = LLVMGetExecutionEngineTargetData(gallivm->engine);
  286.    if (!gallivm->target)
  287.       goto fail;
  288. #else
  289.    if (0) {
  290.        /*
  291.         * Dump the data layout strings.
  292.         */
  293.  
  294.        LLVMTargetDataRef target = LLVMGetExecutionEngineTargetData(gallivm->engine);
  295.        char *data_layout;
  296.        char *engine_data_layout;
  297.  
  298.        data_layout = LLVMCopyStringRepOfTargetData(gallivm->target);
  299.        engine_data_layout = LLVMCopyStringRepOfTargetData(target);
  300.  
  301.        if (1) {
  302.           debug_printf("module target data = %s\n", data_layout);
  303.           debug_printf("engine target data = %s\n", engine_data_layout);
  304.        }
  305.  
  306.        free(data_layout);
  307.        free(engine_data_layout);
  308.    }
  309. #endif
  310.  
  311.    return TRUE;
  312.  
  313. fail:
  314.    return FALSE;
  315. }
  316.  
  317.  
  318. /**
  319.  * Singleton
  320.  *
  321.  * We must never free LLVM contexts, because LLVM has several global caches
  322.  * which pointing/derived from objects owned by the context, causing false
  323.  * memory leaks and false cache hits when these objects are destroyed.
  324.  *
  325.  * TODO: For thread safety on multi-threaded OpenGL we should use one LLVM
  326.  * context per thread, and put them in a pool when threads are destroyed.
  327.  */
  328. static LLVMContextRef gallivm_context = NULL;
  329.  
  330.  
  331. /**
  332.  * Allocate gallivm LLVM objects.
  333.  * \return  TRUE for success, FALSE for failure
  334.  */
  335. static boolean
  336. init_gallivm_state(struct gallivm_state *gallivm)
  337. {
  338.    assert(!gallivm->context);
  339.    assert(!gallivm->module);
  340.    assert(!gallivm->provider);
  341.  
  342.    lp_build_init();
  343.  
  344.    if (!gallivm_context) {
  345.       gallivm_context = LLVMContextCreate();
  346.    }
  347.    gallivm->context = gallivm_context;
  348.    if (!gallivm->context)
  349.       goto fail;
  350.  
  351.    gallivm->module = LLVMModuleCreateWithNameInContext("gallivm",
  352.                                                        gallivm->context);
  353.    if (!gallivm->module)
  354.       goto fail;
  355.  
  356.    gallivm->provider =
  357.       LLVMCreateModuleProviderForExistingModule(gallivm->module);
  358.    if (!gallivm->provider)
  359.       goto fail;
  360.  
  361.    gallivm->builder = LLVMCreateBuilderInContext(gallivm->context);
  362.    if (!gallivm->builder)
  363.       goto fail;
  364.  
  365.    /* FIXME: MC-JIT only allows compiling one module at a time, and it must be
  366.     * complete when MC-JIT is created. So defer the MC-JIT engine creation for
  367.     * now.
  368.     */
  369. #if !USE_MCJIT
  370.    if (!init_gallivm_engine(gallivm)) {
  371.       goto fail;
  372.    }
  373. #else
  374.    /*
  375.     * MC-JIT engine compiles the module immediately on creation, so we can't
  376.     * obtain the target data from it.  Instead we create a target data layout
  377.     * from a string.
  378.     *
  379.     * The produced layout strings are not precisely the same, but should make
  380.     * no difference for the kind of optimization passes we run.
  381.     *
  382.     * For reference this is the layout string on x64:
  383.     *
  384.     *   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
  385.     *
  386.     * See also:
  387.     * - http://llvm.org/docs/LangRef.html#datalayout
  388.     */
  389.  
  390.    {
  391.       const unsigned pointer_size = 8 * sizeof(void *);
  392.       char layout[512];
  393.       util_snprintf(layout, sizeof layout, "%c-p:%u:%u:%u-i64:64:64-a0:0:%u-s0:%u:%u",
  394. #ifdef PIPE_ARCH_LITTLE_ENDIAN
  395.                     'e', // little endian
  396. #else
  397.                     'E', // big endian
  398. #endif
  399.                     pointer_size, pointer_size, pointer_size, // pointer size, abi alignment, preferred alignment
  400.                     pointer_size, // aggregate preferred alignment
  401.                     pointer_size, pointer_size); // stack objects abi alignment, preferred alignment
  402.  
  403.       gallivm->target = LLVMCreateTargetData(layout);
  404.       if (!gallivm->target) {
  405.          return FALSE;
  406.       }
  407.    }
  408. #endif
  409.  
  410.    if (!create_pass_manager(gallivm))
  411.       goto fail;
  412.  
  413.    return TRUE;
  414.  
  415. fail:
  416.    free_gallivm_state(gallivm);
  417.    return FALSE;
  418. }
  419.  
  420.  
  421. void
  422. lp_build_init(void)
  423. {
  424.    if (gallivm_initialized)
  425.       return;
  426.  
  427. #ifdef DEBUG
  428.    gallivm_debug = debug_get_option_gallivm_debug();
  429. #endif
  430.  
  431.    lp_set_target_options();
  432.  
  433. #if USE_MCJIT
  434.    LLVMLinkInMCJIT();
  435. #else
  436.    LLVMLinkInJIT();
  437. #endif
  438.  
  439.    util_cpu_detect();
  440.  
  441.    /* AMD Bulldozer AVX's throughput is the same as SSE2; and because using
  442.     * 8-wide vector needs more floating ops than 4-wide (due to padding), it is
  443.     * actually more efficient to use 4-wide vectors on this processor.
  444.     *
  445.     * See also:
  446.     * - http://www.anandtech.com/show/4955/the-bulldozer-review-amd-fx8150-tested/2
  447.     */
  448.    if (HAVE_AVX &&
  449.        util_cpu_caps.has_avx &&
  450.        util_cpu_caps.has_intel) {
  451.       lp_native_vector_width = 256;
  452.    } else {
  453.       /* Leave it at 128, even when no SIMD extensions are available.
  454.        * Really needs to be a multiple of 128 so can fit 4 floats.
  455.        */
  456.       lp_native_vector_width = 128;
  457.    }
  458.  
  459.    lp_native_vector_width = debug_get_num_option("LP_NATIVE_VECTOR_WIDTH",
  460.                                                  lp_native_vector_width);
  461.  
  462.    if (lp_native_vector_width <= 128) {
  463.       /* Hide AVX support, as often LLVM AVX instrinsics are only guarded by
  464.        * "util_cpu_caps.has_avx" predicate, and lack the
  465.        * "lp_native_vector_width > 128" predicate. And also to ensure a more
  466.        * consistent behavior, allowing one to test SSE2 on AVX machines.
  467.        */
  468.       util_cpu_caps.has_avx = 0;
  469.    }
  470.  
  471.    if (!HAVE_AVX) {
  472.       /*
  473.        * note these instructions are VEX-only, so can only emit if we use
  474.        * avx (don't want to base it on has_avx & has_f16c later as that would
  475.        * omit it unnecessarily on amd cpus, see above).
  476.        */
  477.       util_cpu_caps.has_f16c = 0;
  478.    }
  479.  
  480. #ifdef PIPE_ARCH_PPC_64
  481.    /* Set the NJ bit in VSCR to 0 so denormalized values are handled as
  482.     * specified by IEEE standard (PowerISA 2.06 - Section 6.3). This garantees
  483.     * that some rounding and half-float to float handling does not round
  484.     * incorrectly to 0.
  485.     */
  486.    if (util_cpu_caps.has_altivec) {
  487.       unsigned short mask[] = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
  488.                                 0xFFFF, 0xFFFF, 0xFFFE, 0xFFFF };
  489.       __asm (
  490.         "mfvscr %%v1\n"
  491.         "vand   %0,%%v1,%0\n"
  492.         "mtvscr %0"
  493.         :
  494.         : "r" (*mask)
  495.       );
  496.    }
  497. #endif
  498.  
  499.    gallivm_initialized = TRUE;
  500.  
  501. #if 0
  502.    /* For simulating less capable machines */
  503.    util_cpu_caps.has_sse3 = 0;
  504.    util_cpu_caps.has_ssse3 = 0;
  505.    util_cpu_caps.has_sse4_1 = 0;
  506.    util_cpu_caps.has_avx = 0;
  507.    util_cpu_caps.has_f16c = 0;
  508. #endif
  509. }
  510.  
  511.  
  512.  
  513. /**
  514.  * Create a new gallivm_state object.
  515.  * Note that we return a singleton.
  516.  */
  517. struct gallivm_state *
  518. gallivm_create(void)
  519. {
  520.    struct gallivm_state *gallivm;
  521.  
  522. #if HAVE_LLVM <= 0x206
  523.    if (GlobalGallivm) {
  524.       return GlobalGallivm;
  525.    }
  526. #endif
  527.  
  528.    gallivm = CALLOC_STRUCT(gallivm_state);
  529.    if (gallivm) {
  530.       if (!init_gallivm_state(gallivm)) {
  531.          FREE(gallivm);
  532.          gallivm = NULL;
  533.       }
  534.    }
  535.  
  536. #if HAVE_LLVM <= 0x206
  537.    GlobalGallivm = gallivm;
  538. #endif
  539.  
  540.    return gallivm;
  541. }
  542.  
  543.  
  544. /**
  545.  * Destroy a gallivm_state object.
  546.  */
  547. void
  548. gallivm_destroy(struct gallivm_state *gallivm)
  549. {
  550. #if HAVE_LLVM <= 0x0206
  551.    /* No-op: don't destroy the singleton */
  552.    (void) gallivm;
  553. #else
  554.    free_gallivm_state(gallivm);
  555.    FREE(gallivm);
  556. #endif
  557. }
  558.  
  559.  
  560. /**
  561.  * Validate and optimze a function.
  562.  */
  563. static void
  564. gallivm_optimize_function(struct gallivm_state *gallivm,
  565.                           LLVMValueRef func)
  566. {
  567.    if (0) {
  568.       debug_printf("optimizing %s...\n", LLVMGetValueName(func));
  569.    }
  570.  
  571.    assert(gallivm->passmgr);
  572.  
  573.    /* Apply optimizations to LLVM IR */
  574.    LLVMRunFunctionPassManager(gallivm->passmgr, func);
  575.  
  576.    if (0) {
  577.       if (gallivm_debug & GALLIVM_DEBUG_IR) {
  578.          /* Print the LLVM IR to stderr */
  579.          lp_debug_dump_value(func);
  580.          debug_printf("\n");
  581.       }
  582.    }
  583. }
  584.  
  585.  
  586. /**
  587.  * Validate a function.
  588.  */
  589. void
  590. gallivm_verify_function(struct gallivm_state *gallivm,
  591.                         LLVMValueRef func)
  592. {
  593.    /* Verify the LLVM IR.  If invalid, dump and abort */
  594. #ifdef DEBUG
  595.    if (LLVMVerifyFunction(func, LLVMPrintMessageAction)) {
  596.       lp_debug_dump_value(func);
  597.       assert(0);
  598.       return;
  599.    }
  600. #endif
  601.  
  602.    gallivm_optimize_function(gallivm, func);
  603.  
  604.    if (gallivm_debug & GALLIVM_DEBUG_IR) {
  605.       /* Print the LLVM IR to stderr */
  606.       lp_debug_dump_value(func);
  607.       debug_printf("\n");
  608.    }
  609. }
  610.  
  611.  
  612. void
  613. gallivm_compile_module(struct gallivm_state *gallivm)
  614. {
  615. #if HAVE_LLVM > 0x206
  616.    assert(!gallivm->compiled);
  617. #endif
  618.  
  619.    /* Dump byte code to a file */
  620.    if (0) {
  621.       LLVMWriteBitcodeToFile(gallivm->module, "llvmpipe.bc");
  622.       debug_printf("llvmpipe.bc written\n");
  623.       debug_printf("Invoke as \"llc -o - llvmpipe.bc\"\n");
  624.    }
  625.  
  626. #if USE_MCJIT
  627.    assert(!gallivm->engine);
  628.    if (!init_gallivm_engine(gallivm)) {
  629.       assert(0);
  630.    }
  631. #endif
  632.    assert(gallivm->engine);
  633.  
  634.    ++gallivm->compiled;
  635. }
  636.  
  637.  
  638.  
  639. func_pointer
  640. gallivm_jit_function(struct gallivm_state *gallivm,
  641.                      LLVMValueRef func)
  642. {
  643.    void *code;
  644.    func_pointer jit_func;
  645.  
  646.    assert(gallivm->compiled);
  647.    assert(gallivm->engine);
  648.  
  649.    code = LLVMGetPointerToGlobal(gallivm->engine, func);
  650.    assert(code);
  651.    jit_func = pointer_to_func(code);
  652.  
  653.    if (gallivm_debug & GALLIVM_DEBUG_ASM) {
  654.       lp_disassemble(func, code);
  655.    }
  656.  
  657. #if defined(PROFILE)
  658.    lp_profile(func, code);
  659. #endif
  660.  
  661.    /* Free the function body to save memory */
  662.    lp_func_delete_body(func);
  663.  
  664.    return jit_func;
  665. }
  666.  
  667.  
  668. /**
  669.  * Free the function (and its machine code).
  670.  */
  671. void
  672. gallivm_free_function(struct gallivm_state *gallivm,
  673.                       LLVMValueRef func,
  674.                       const void *code)
  675. {
  676. #if !USE_MCJIT
  677.    if (code) {
  678.       LLVMFreeMachineCodeForFunction(gallivm->engine, func);
  679.    }
  680.  
  681.    LLVMDeleteFunction(func);
  682. #endif
  683. }
  684.