Merge remote branch 'origin/master' into pipe-video
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_misc.cpp
1 /**************************************************************************
2 *
3 * Copyright 2010 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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28
29 #ifndef __STDC_LIMIT_MACROS
30 #define __STDC_LIMIT_MACROS
31 #endif
32
33 #ifndef __STDC_CONSTANT_MACROS
34 #define __STDC_CONSTANT_MACROS
35 #endif
36
37 #include <llvm-c/Core.h>
38 #include <llvm-c/ExecutionEngine.h>
39 #include <llvm/Target/TargetOptions.h>
40 #include <llvm/ExecutionEngine/ExecutionEngine.h>
41 #include <llvm/ExecutionEngine/JITEventListener.h>
42 #include <llvm/Support/CommandLine.h>
43 #include <llvm/Support/PrettyStackTrace.h>
44
45 #include "pipe/p_config.h"
46 #include "util/u_debug.h"
47
48
49 /**
50 * Register the engine with oprofile.
51 *
52 * This allows to see the LLVM IR function names in oprofile output.
53 *
54 * To actually work LLVM needs to be built with the --with-oprofile configure
55 * option.
56 *
57 * Also a oprofile:oprofile user:group is necessary. Which is not created by
58 * default on some distributions.
59 */
60 extern "C" void
61 lp_register_oprofile_jit_event_listener(LLVMExecutionEngineRef EE)
62 {
63 llvm::unwrap(EE)->RegisterJITEventListener(llvm::createOProfileJITEventListener());
64 }
65
66
67 extern "C" void
68 lp_set_target_options(void)
69 {
70 #if defined(DEBUG)
71 #if HAVE_LLVM >= 0x0207
72 llvm::JITEmitDebugInfo = true;
73 #endif
74 #endif
75
76 #if defined(DEBUG) || defined(PROFILE)
77 llvm::NoFramePointerElim = true;
78 #endif
79
80 llvm::NoExcessFPPrecision = false;
81
82 /* XXX: Investigate this */
83 #if 0
84 llvm::UnsafeFPMath = true;
85 #endif
86
87 #if HAVE_LLVM < 0x0209
88 /*
89 * LLVM will generate MMX instructions for vectors <= 64 bits, leading to
90 * innefficient code, and in 32bit systems, to the corruption of the FPU
91 * stack given that it expects the user to generate the EMMS instructions.
92 *
93 * See also:
94 * - http://llvm.org/bugs/show_bug.cgi?id=3287
95 * - http://l4.me.uk/post/2009/06/07/llvm-wrinkle-3-configuration-what-configuration/
96 */
97 static boolean first = TRUE;
98 if (first) {
99 static const char* options[] = {
100 "prog",
101 "-disable-mmx"
102 };
103 llvm::cl::ParseCommandLineOptions(2, const_cast<char**>(options));
104 first = FALSE;
105 }
106 #endif
107
108 /*
109 * By default LLVM adds a signal handler to output a pretty stack trace.
110 * This signal handler is never removed, causing problems when unloading the
111 * shared object where the gallium driver resides.
112 */
113 llvm::DisablePrettyStackTrace = true;
114 }
115
116
117 extern "C" void
118 lp_func_delete_body(LLVMValueRef FF)
119 {
120 llvm::Function *func = llvm::unwrap<llvm::Function>(FF);
121 func->deleteBody();
122 }
123
124
125 extern "C"
126 LLVMValueRef
127 lp_build_load_volatile(LLVMBuilderRef B, LLVMValueRef PointerVal,
128 const char *Name)
129 {
130 return llvm::wrap(llvm::unwrap(B)->CreateLoad(llvm::unwrap(PointerVal), true, Name));
131 }
132