gallivm: Prevent disassembly debug output from being truncated.
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_intr.c
1 /**************************************************************************
2 *
3 * Copyright 2009 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 above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 /**
30 * @file
31 * Helpers for emiting intrinsic calls.
32 *
33 * LLVM vanilla IR doesn't represent all basic arithmetic operations we care
34 * about, and it is often necessary to resort target-specific intrinsics for
35 * performance, convenience.
36 *
37 * Ideally we would like to stay away from target specific intrinsics and
38 * move all the instruction selection logic into upstream LLVM where it belongs.
39 *
40 * These functions are also used for calling C functions provided by us from
41 * generated LLVM code.
42 *
43 * @author Jose Fonseca <jfonseca@vmware.com>
44 */
45
46
47 #include "util/u_debug.h"
48
49 #include "lp_bld_const.h"
50 #include "lp_bld_intr.h"
51 #include "lp_bld_type.h"
52 #include "lp_bld_pack.h"
53
54
55 LLVMValueRef
56 lp_declare_intrinsic(LLVMModuleRef module,
57 const char *name,
58 LLVMTypeRef ret_type,
59 LLVMTypeRef *arg_types,
60 unsigned num_args)
61 {
62 LLVMTypeRef function_type;
63 LLVMValueRef function;
64
65 assert(!LLVMGetNamedFunction(module, name));
66
67 function_type = LLVMFunctionType(ret_type, arg_types, num_args, 0);
68 function = LLVMAddFunction(module, name, function_type);
69
70 LLVMSetFunctionCallConv(function, LLVMCCallConv);
71 LLVMSetLinkage(function, LLVMExternalLinkage);
72
73 assert(LLVMIsDeclaration(function));
74
75 return function;
76 }
77
78
79 LLVMValueRef
80 lp_build_intrinsic(LLVMBuilderRef builder,
81 const char *name,
82 LLVMTypeRef ret_type,
83 LLVMValueRef *args,
84 unsigned num_args,
85 LLVMAttribute attr)
86 {
87 LLVMModuleRef module = LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder)));
88 LLVMValueRef function;
89
90 function = LLVMGetNamedFunction(module, name);
91 if(!function) {
92 LLVMTypeRef arg_types[LP_MAX_FUNC_ARGS];
93 unsigned i;
94
95 assert(num_args <= LP_MAX_FUNC_ARGS);
96
97 for(i = 0; i < num_args; ++i) {
98 assert(args[i]);
99 arg_types[i] = LLVMTypeOf(args[i]);
100 }
101
102 function = lp_declare_intrinsic(module, name, ret_type, arg_types, num_args);
103
104 if (attr)
105 LLVMAddFunctionAttr(function, attr);
106 }
107
108 return LLVMBuildCall(builder, function, args, num_args, "");
109 }
110
111
112 LLVMValueRef
113 lp_build_intrinsic_unary(LLVMBuilderRef builder,
114 const char *name,
115 LLVMTypeRef ret_type,
116 LLVMValueRef a)
117 {
118 return lp_build_intrinsic(builder, name, ret_type, &a, 1, 0);
119 }
120
121
122 LLVMValueRef
123 lp_build_intrinsic_binary(LLVMBuilderRef builder,
124 const char *name,
125 LLVMTypeRef ret_type,
126 LLVMValueRef a,
127 LLVMValueRef b)
128 {
129 LLVMValueRef args[2];
130
131 args[0] = a;
132 args[1] = b;
133
134 return lp_build_intrinsic(builder, name, ret_type, args, 2, 0);
135 }
136
137
138 /**
139 * Call intrinsic with arguments adapted to intrinsic vector length.
140 *
141 * Split vectors which are too large for the hw, or expand them if they
142 * are too small, so a caller calling a function which might use intrinsics
143 * doesn't need to do splitting/expansion on its own.
144 * This only supports intrinsics where src and dst types match.
145 */
146 LLVMValueRef
147 lp_build_intrinsic_binary_anylength(struct gallivm_state *gallivm,
148 const char *name,
149 struct lp_type src_type,
150 unsigned intr_size,
151 LLVMValueRef a,
152 LLVMValueRef b)
153 {
154 unsigned i;
155 struct lp_type intrin_type = src_type;
156 LLVMBuilderRef builder = gallivm->builder;
157 LLVMValueRef i32undef = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
158 LLVMValueRef anative, bnative;
159 unsigned intrin_length = intr_size / src_type.width;
160
161 intrin_type.length = intrin_length;
162
163 if (intrin_length > src_type.length) {
164 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
165 LLVMValueRef constvec, tmp;
166
167 for (i = 0; i < src_type.length; i++) {
168 elems[i] = lp_build_const_int32(gallivm, i);
169 }
170 for (; i < intrin_length; i++) {
171 elems[i] = i32undef;
172 }
173 if (src_type.length == 1) {
174 LLVMTypeRef elem_type = lp_build_elem_type(gallivm, intrin_type);
175 a = LLVMBuildBitCast(builder, a, LLVMVectorType(elem_type, 1), "");
176 b = LLVMBuildBitCast(builder, b, LLVMVectorType(elem_type, 1), "");
177 }
178 constvec = LLVMConstVector(elems, intrin_length);
179 anative = LLVMBuildShuffleVector(builder, a, a, constvec, "");
180 bnative = LLVMBuildShuffleVector(builder, b, b, constvec, "");
181 tmp = lp_build_intrinsic_binary(builder, name,
182 lp_build_vec_type(gallivm, intrin_type),
183 anative, bnative);
184 if (src_type.length > 1) {
185 constvec = LLVMConstVector(elems, src_type.length);
186 return LLVMBuildShuffleVector(builder, tmp, tmp, constvec, "");
187 }
188 else {
189 return LLVMBuildExtractElement(builder, tmp, elems[0], "");
190 }
191 }
192 else if (intrin_length < src_type.length) {
193 unsigned num_vec = src_type.length / intrin_length;
194 LLVMValueRef tmp[LP_MAX_VECTOR_LENGTH];
195
196 /* don't support arbitrary size here as this is so yuck */
197 if (src_type.length % intrin_length) {
198 /* FIXME: This is something which should be supported
199 * but there doesn't seem to be any need for it currently
200 * so crash and burn.
201 */
202 debug_printf("%s: should handle arbitrary vector size\n",
203 __FUNCTION__);
204 assert(0);
205 return NULL;
206 }
207
208 for (i = 0; i < num_vec; i++) {
209 anative = lp_build_extract_range(gallivm, a, i*intrin_length,
210 intrin_length);
211 bnative = lp_build_extract_range(gallivm, b, i*intrin_length,
212 intrin_length);
213 tmp[i] = lp_build_intrinsic_binary(builder, name,
214 lp_build_vec_type(gallivm, intrin_type),
215 anative, bnative);
216 }
217 return lp_build_concat(gallivm, tmp, intrin_type, num_vec);
218 }
219 else {
220 return lp_build_intrinsic_binary(builder, name,
221 lp_build_vec_type(gallivm, src_type),
222 a, b);
223 }
224 }
225
226
227 LLVMValueRef
228 lp_build_intrinsic_map(struct gallivm_state *gallivm,
229 const char *name,
230 LLVMTypeRef ret_type,
231 LLVMValueRef *args,
232 unsigned num_args)
233 {
234 LLVMBuilderRef builder = gallivm->builder;
235 LLVMTypeRef ret_elem_type = LLVMGetElementType(ret_type);
236 unsigned n = LLVMGetVectorSize(ret_type);
237 unsigned i, j;
238 LLVMValueRef res;
239
240 assert(num_args <= LP_MAX_FUNC_ARGS);
241
242 res = LLVMGetUndef(ret_type);
243 for(i = 0; i < n; ++i) {
244 LLVMValueRef index = lp_build_const_int32(gallivm, i);
245 LLVMValueRef arg_elems[LP_MAX_FUNC_ARGS];
246 LLVMValueRef res_elem;
247 for(j = 0; j < num_args; ++j)
248 arg_elems[j] = LLVMBuildExtractElement(builder, args[j], index, "");
249 res_elem = lp_build_intrinsic(builder, name, ret_elem_type, arg_elems, num_args, 0);
250 res = LLVMBuildInsertElement(builder, res, res_elem, index, "");
251 }
252
253 return res;
254 }
255
256
257 LLVMValueRef
258 lp_build_intrinsic_map_unary(struct gallivm_state *gallivm,
259 const char *name,
260 LLVMTypeRef ret_type,
261 LLVMValueRef a)
262 {
263 return lp_build_intrinsic_map(gallivm, name, ret_type, &a, 1);
264 }
265
266
267 LLVMValueRef
268 lp_build_intrinsic_map_binary(struct gallivm_state *gallivm,
269 const char *name,
270 LLVMTypeRef ret_type,
271 LLVMValueRef a,
272 LLVMValueRef b)
273 {
274 LLVMValueRef args[2];
275
276 args[0] = a;
277 args[1] = b;
278
279 return lp_build_intrinsic_map(gallivm, name, ret_type, args, 2);
280 }
281
282