lp_bld_misc.cpp revision 85d09d1c61d3e5ab2c2ae6fc74a30ea6a572f25e
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 <stddef.h>
38
39#include <llvm-c/Core.h>
40#include <llvm-c/ExecutionEngine.h>
41#include <llvm/Target/TargetOptions.h>
42#include <llvm/ExecutionEngine/ExecutionEngine.h>
43#include <llvm/ExecutionEngine/JITEventListener.h>
44#include <llvm/Support/CommandLine.h>
45#include <llvm/Support/PrettyStackTrace.h>
46
47#include "pipe/p_config.h"
48#include "util/u_debug.h"
49
50
51/**
52 * Register the engine with oprofile.
53 *
54 * This allows to see the LLVM IR function names in oprofile output.
55 *
56 * To actually work LLVM needs to be built with the --with-oprofile configure
57 * option.
58 *
59 * Also a oprofile:oprofile user:group is necessary. Which is not created by
60 * default on some distributions.
61 */
62extern "C" void
63lp_register_oprofile_jit_event_listener(LLVMExecutionEngineRef EE)
64{
65#if HAVE_LLVM >= 0x0301
66   llvm::unwrap(EE)->RegisterJITEventListener(llvm::JITEventListener::createOProfileJITEventListener());
67#else
68   llvm::unwrap(EE)->RegisterJITEventListener(llvm::createOProfileJITEventListener());
69#endif
70}
71
72
73extern "C" void
74lp_set_target_options(void)
75{
76#if HAVE_LLVM <= 0x0300
77#if defined(DEBUG)
78#if HAVE_LLVM >= 0x0207
79   llvm::JITEmitDebugInfo = true;
80#endif
81#endif
82
83   /*
84    * LLVM revision 123367 switched the default stack alignment to 16 bytes on
85    * Linux (and several other Unices in later revisions), to match recent gcc
86    * versions.
87    *
88    * However our drivers can be loaded by old binary applications, still
89    * maintaining a 4 bytes stack alignment.  Therefore we must tell LLVM here
90    * to only assume a 4 bytes alignment for backwards compatibility.
91    */
92#if defined(PIPE_ARCH_X86)
93#if HAVE_LLVM >= 0x0300
94   llvm::StackAlignmentOverride = 4;
95#else
96   llvm::StackAlignment = 4;
97#endif
98#endif
99
100#if defined(DEBUG) || defined(PROFILE)
101   llvm::NoFramePointerElim = true;
102#endif
103
104   llvm::NoExcessFPPrecision = false;
105
106   /* XXX: Investigate this */
107#if 0
108   llvm::UnsafeFPMath = true;
109#endif
110#endif  /* HAVE_LLVM <= 0x0300 */
111
112#if HAVE_LLVM < 0x0209
113   /*
114    * LLVM will generate MMX instructions for vectors <= 64 bits, leading to
115    * innefficient code, and in 32bit systems, to the corruption of the FPU
116    * stack given that it expects the user to generate the EMMS instructions.
117    *
118    * See also:
119    * - http://llvm.org/bugs/show_bug.cgi?id=3287
120    * - http://l4.me.uk/post/2009/06/07/llvm-wrinkle-3-configuration-what-configuration/
121    *
122    * The -disable-mmx global option can be specified only once  since we
123    * dynamically link against LLVM it will reside in a separate shared object,
124    * which may or not be delete when this shared object is, so we use the
125    * llvm::DisablePrettyStackTrace variable (which we set below and should
126    * reside in the same shared library) to determine whether the -disable-mmx
127    * option has been set or not.
128    *
129    * Thankfully this ugly hack is not necessary on LLVM 2.9 onwards.
130    */
131   if (!llvm::DisablePrettyStackTrace) {
132      static boolean first = TRUE;
133      static const char* options[] = {
134         "prog",
135         "-disable-mmx"
136      };
137      assert(first);
138      llvm::cl::ParseCommandLineOptions(2, const_cast<char**>(options));
139      first = FALSE;
140   }
141#endif
142
143   /*
144    * By default LLVM adds a signal handler to output a pretty stack trace.
145    * This signal handler is never removed, causing problems when unloading the
146    * shared object where the gallium driver resides.
147    */
148   llvm::DisablePrettyStackTrace = true;
149}
150
151
152extern "C" void
153lp_func_delete_body(LLVMValueRef FF)
154{
155   llvm::Function *func = llvm::unwrap<llvm::Function>(FF);
156   func->deleteBody();
157}
158
159
160extern "C"
161LLVMValueRef
162lp_build_load_volatile(LLVMBuilderRef B, LLVMValueRef PointerVal,
163                       const char *Name)
164{
165   return llvm::wrap(llvm::unwrap(B)->CreateLoad(llvm::unwrap(PointerVal), true, Name));
166}
167
168extern "C"
169void
170lp_set_load_alignment(LLVMValueRef Inst,
171                       unsigned Align)
172{
173   llvm::unwrap<llvm::LoadInst>(Inst)->setAlignment(Align);
174}
175
176extern "C"
177void
178lp_set_store_alignment(LLVMValueRef Inst,
179		       unsigned Align)
180{
181   llvm::unwrap<llvm::StoreInst>(Inst)->setAlignment(Align);
182}
183