Merge remote branch 'origin/master' into pipe-video
[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_intr.h"
50
51
52 LLVMValueRef
53 lp_declare_intrinsic(LLVMModuleRef module,
54 const char *name,
55 LLVMTypeRef ret_type,
56 LLVMTypeRef *arg_types,
57 unsigned num_args)
58 {
59 LLVMTypeRef function_type;
60 LLVMValueRef function;
61
62 assert(!LLVMGetNamedFunction(module, name));
63
64 function_type = LLVMFunctionType(ret_type, arg_types, num_args, 0);
65 function = LLVMAddFunction(module, name, function_type);
66
67 LLVMSetFunctionCallConv(function, LLVMCCallConv);
68 LLVMSetLinkage(function, LLVMExternalLinkage);
69
70 assert(LLVMIsDeclaration(function));
71
72 if(name[0] == 'l' &&
73 name[1] == 'l' &&
74 name[2] == 'v' &&
75 name[3] == 'm' &&
76 name[4] == '.')
77 assert(LLVMGetIntrinsicID(function));
78
79 return function;
80 }
81
82
83 LLVMValueRef
84 lp_build_intrinsic(LLVMBuilderRef builder,
85 const char *name,
86 LLVMTypeRef ret_type,
87 LLVMValueRef *args,
88 unsigned num_args)
89 {
90 LLVMModuleRef module = LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder)));
91 LLVMValueRef function;
92
93 function = LLVMGetNamedFunction(module, name);
94 if(!function) {
95 LLVMTypeRef arg_types[LP_MAX_FUNC_ARGS];
96 unsigned i;
97
98 assert(num_args <= LP_MAX_FUNC_ARGS);
99
100 for(i = 0; i < num_args; ++i) {
101 assert(args[i]);
102 arg_types[i] = LLVMTypeOf(args[i]);
103 }
104
105 function = lp_declare_intrinsic(module, name, ret_type, arg_types, num_args);
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);
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);
135 }
136
137
138 LLVMValueRef
139 lp_build_intrinsic_map(LLVMBuilderRef builder,
140 const char *name,
141 LLVMTypeRef ret_type,
142 LLVMValueRef *args,
143 unsigned num_args)
144 {
145 LLVMTypeRef ret_elem_type = LLVMGetElementType(ret_type);
146 unsigned n = LLVMGetVectorSize(ret_type);
147 unsigned i, j;
148 LLVMValueRef res;
149
150 assert(num_args <= LP_MAX_FUNC_ARGS);
151
152 res = LLVMGetUndef(ret_type);
153 for(i = 0; i < n; ++i) {
154 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
155 LLVMValueRef arg_elems[LP_MAX_FUNC_ARGS];
156 LLVMValueRef res_elem;
157 for(j = 0; j < num_args; ++j)
158 arg_elems[j] = LLVMBuildExtractElement(builder, args[j], index, "");
159 res_elem = lp_build_intrinsic(builder, name, ret_elem_type, arg_elems, num_args);
160 res = LLVMBuildInsertElement(builder, res, res_elem, index, "");
161 }
162
163 return res;
164 }
165
166
167 LLVMValueRef
168 lp_build_intrinsic_map_unary(LLVMBuilderRef builder,
169 const char *name,
170 LLVMTypeRef ret_type,
171 LLVMValueRef a)
172 {
173 return lp_build_intrinsic_map(builder, name, ret_type, &a, 1);
174 }
175
176
177 LLVMValueRef
178 lp_build_intrinsic_map_binary(LLVMBuilderRef builder,
179 const char *name,
180 LLVMTypeRef ret_type,
181 LLVMValueRef a,
182 LLVMValueRef b)
183 {
184 LLVMValueRef args[2];
185
186 args[0] = a;
187 args[1] = b;
188
189 return lp_build_intrinsic_map(builder, name, ret_type, args, 2);
190 }
191
192