Merge branch 'gallium-newclear'
[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/ExecutionEngine/ExecutionEngine.h>
40 #include <llvm/ExecutionEngine/JITEventListener.h>
41
42 #include "pipe/p_config.h"
43 #include "util/u_debug.h"
44
45
46 #if (defined(PIPE_OS_WINDOWS) && !defined(PIPE_CC_MSVC)) || defined(PIPE_OS_EMBDDED)
47
48 #include "llvm/Support/raw_ostream.h"
49
50 class raw_debug_ostream :
51 public llvm::raw_ostream
52 {
53 uint64_t pos;
54
55 void write_impl(const char *Ptr, size_t Size);
56 uint64_t current_pos() { return pos; }
57 uint64_t current_pos() const { return pos; }
58
59 #if HAVE_LLVM >= 0x207
60 uint64_t preferred_buffer_size() { return 512; }
61 #else
62 size_t preferred_buffer_size() { return 512; }
63 #endif
64 };
65
66
67 void
68 raw_debug_ostream::write_impl(const char *Ptr, size_t Size)
69 {
70 if (Size > 0) {
71 char *lastPtr = (char *)&Ptr[Size];
72 char last = *lastPtr;
73 *lastPtr = 0;
74 _debug_printf("%*s", Size, Ptr);
75 *lastPtr = last;
76 pos += Size;
77 }
78 }
79
80
81 /**
82 * Same as LLVMDumpValue, but through our debugging channels.
83 */
84 extern "C" void
85 lp_debug_dump_value(LLVMValueRef value)
86 {
87 raw_debug_ostream os;
88 llvm::unwrap(value)->print(os);
89 os.flush();
90 }
91
92
93 #else
94
95
96 extern "C" void
97 lp_debug_dump_value(LLVMValueRef value)
98 {
99 LLVMDumpValue(value);
100 }
101
102
103 #endif
104
105
106 /**
107 * Register the engine with oprofile.
108 *
109 * This allows to see the LLVM IR function names in oprofile output.
110 *
111 * To actually work LLVM needs to be built with the --with-oprofile configure
112 * option.
113 *
114 * Also a oprofile:oprofile user:group is necessary. Which is not created by
115 * default on some distributions.
116 */
117 extern "C" void
118 lp_register_oprofile_jit_event_listener(LLVMExecutionEngineRef EE)
119 {
120 llvm::unwrap(EE)->RegisterJITEventListener(llvm::createOProfileJITEventListener());
121 }