Merge remote branch 'origin/master' into lp-setup-llvm
[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 #if (defined(PIPE_OS_WINDOWS) && !defined(PIPE_CC_MSVC)) || defined(PIPE_OS_EMBDDED)
50
51 #include "llvm/Support/raw_ostream.h"
52
53 class raw_debug_ostream :
54 public llvm::raw_ostream
55 {
56 uint64_t pos;
57
58 void write_impl(const char *Ptr, size_t Size);
59 uint64_t current_pos() { return pos; }
60 uint64_t current_pos() const { return pos; }
61
62 #if HAVE_LLVM >= 0x207
63 uint64_t preferred_buffer_size() { return 512; }
64 #else
65 size_t preferred_buffer_size() { return 512; }
66 #endif
67 };
68
69
70 void
71 raw_debug_ostream::write_impl(const char *Ptr, size_t Size)
72 {
73 if (Size > 0) {
74 char *lastPtr = (char *)&Ptr[Size];
75 char last = *lastPtr;
76 *lastPtr = 0;
77 _debug_printf("%*s", Size, Ptr);
78 *lastPtr = last;
79 pos += Size;
80 }
81 }
82
83
84 /**
85 * Same as LLVMDumpValue, but through our debugging channels.
86 */
87 extern "C" void
88 lp_debug_dump_value(LLVMValueRef value)
89 {
90 raw_debug_ostream os;
91 llvm::unwrap(value)->print(os);
92 os.flush();
93 }
94
95
96 #else
97
98
99 extern "C" void
100 lp_debug_dump_value(LLVMValueRef value)
101 {
102 LLVMDumpValue(value);
103 }
104
105
106 #endif
107
108
109 /**
110 * Register the engine with oprofile.
111 *
112 * This allows to see the LLVM IR function names in oprofile output.
113 *
114 * To actually work LLVM needs to be built with the --with-oprofile configure
115 * option.
116 *
117 * Also a oprofile:oprofile user:group is necessary. Which is not created by
118 * default on some distributions.
119 */
120 extern "C" void
121 lp_register_oprofile_jit_event_listener(LLVMExecutionEngineRef EE)
122 {
123 llvm::unwrap(EE)->RegisterJITEventListener(llvm::createOProfileJITEventListener());
124 }
125
126
127 extern "C" void
128 lp_set_target_options(void)
129 {
130 #if defined(DEBUG)
131 #if HAVE_LLVM >= 0x0207
132 llvm::JITEmitDebugInfo = true;
133 #endif
134 #endif
135
136 #if defined(DEBUG) || defined(PROFILE)
137 llvm::NoFramePointerElim = true;
138 #endif
139
140 llvm::NoExcessFPPrecision = false;
141
142 /* XXX: Investigate this */
143 #if 0
144 llvm::UnsafeFPMath = true;
145 #endif
146
147 /*
148 * LLVM will generate MMX instructions for vectors <= 64 bits, leading to
149 * innefficient code, and in 32bit systems, to the corruption of the FPU
150 * stack given that it expects the user to generate the EMMS instructions.
151 *
152 * See also:
153 * - http://llvm.org/bugs/show_bug.cgi?id=3287
154 * - http://l4.me.uk/post/2009/06/07/llvm-wrinkle-3-configuration-what-configuration/
155 */
156 static boolean first = TRUE;
157 if (first) {
158 static const char* options[] = {
159 "prog",
160 "-disable-mmx"
161 };
162 llvm::cl::ParseCommandLineOptions(2, const_cast<char**>(options));
163 first = FALSE;
164 }
165
166 /*
167 * By default LLVM adds a signal handler to output a pretty stack trace.
168 * This signal handler is never removed, causing problems when unloading the
169 * shared object where the gallium driver resides.
170 */
171 llvm::DisablePrettyStackTrace = true;
172 }
173
174
175 extern "C" void
176 lp_func_delete_body(LLVMValueRef FF)
177 {
178 llvm::Function *func = llvm::unwrap<llvm::Function>(FF);
179 func->deleteBody();
180 }
181
182
183 extern "C"
184 LLVMValueRef
185 lp_build_load_volatile(LLVMBuilderRef B, LLVMValueRef PointerVal,
186 const char *Name)
187 {
188 return llvm::wrap(llvm::unwrap(B)->CreateLoad(llvm::unwrap(PointerVal), true, Name));
189 }
190