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