Add hash table helper functions for using pointers as hash keys
[mesa.git] / src / glsl / ir_function_inlining.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file ir_function_inlining.cpp
26 *
27 * Replaces calls to functions with the body of the function.
28 */
29
30 #include <inttypes.h>
31 #include "ir.h"
32 #include "ir_visitor.h"
33 #include "ir_function_inlining.h"
34 #include "ir_expression_flattening.h"
35 #include "glsl_types.h"
36 #include "hash_table.h"
37
38 class ir_function_inlining_visitor : public ir_hierarchical_visitor {
39 public:
40 ir_function_inlining_visitor()
41 {
42 progress = false;
43 }
44
45 virtual ~ir_function_inlining_visitor()
46 {
47 /* empty */
48 }
49
50 virtual ir_visitor_status visit_enter(ir_expression *);
51 virtual ir_visitor_status visit_enter(ir_call *);
52 virtual ir_visitor_status visit_enter(ir_assignment *);
53 virtual ir_visitor_status visit_enter(ir_return *);
54 virtual ir_visitor_status visit_enter(ir_texture *);
55 virtual ir_visitor_status visit_enter(ir_swizzle *);
56
57 bool progress;
58 };
59
60
61 bool
62 automatic_inlining_predicate(ir_instruction *ir)
63 {
64 ir_call *call = ir->as_call();
65
66 if (call && can_inline(call))
67 return true;
68
69 return false;
70 }
71
72 bool
73 do_function_inlining(exec_list *instructions)
74 {
75 ir_function_inlining_visitor v;
76
77 do_expression_flattening(instructions, automatic_inlining_predicate);
78
79 v.run(instructions);
80
81 return v.progress;
82 }
83
84 static void
85 replace_return_with_assignment(ir_instruction *ir, void *data)
86 {
87 void *ctx = talloc_parent(ir);
88 ir_variable *retval = (ir_variable *)data;
89 ir_return *ret = ir->as_return();
90
91 if (ret) {
92 if (ret->value) {
93 ir_rvalue *lhs = new(ctx) ir_dereference_variable(retval);
94 ret->insert_before(new(ctx) ir_assignment(lhs, ret->value, NULL));
95 ret->remove();
96 } else {
97 /* un-valued return has to be the last return, or we shouldn't
98 * have reached here. (see can_inline()).
99 */
100 assert(!ret->next->is_tail_sentinal());
101 }
102 }
103 }
104
105 ir_rvalue *
106 ir_call::generate_inline(ir_instruction *next_ir)
107 {
108 void *ctx = talloc_parent(this);
109 ir_variable **parameters;
110 int num_parameters;
111 int i;
112 ir_variable *retval = NULL;
113 struct hash_table *ht;
114
115 ht = hash_table_ctor(0, hash_table_pointer_hash, hash_table_pointer_compare);
116
117 num_parameters = 0;
118 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
119 num_parameters++;
120
121 parameters = new ir_variable *[num_parameters];
122
123 /* Generate storage for the return value. */
124 if (this->callee->return_type) {
125 retval = new(ctx) ir_variable(this->callee->return_type, "__retval");
126 next_ir->insert_before(retval);
127 }
128
129 /* Generate the declarations for the parameters to our inlined code,
130 * and set up the mapping of real function body variables to ours.
131 */
132 i = 0;
133 exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
134 exec_list_iterator param_iter = this->actual_parameters.iterator();
135 for (i = 0; i < num_parameters; i++) {
136 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
137 ir_rvalue *param = (ir_rvalue *) param_iter.get();
138
139 /* Generate a new variable for the parameter. */
140 parameters[i] = (ir_variable *)sig_param->clone(ht);
141 parameters[i]->mode = ir_var_auto;
142 next_ir->insert_before(parameters[i]);
143
144 /* Move the actual param into our param variable if it's an 'in' type. */
145 if (sig_param->mode == ir_var_in ||
146 sig_param->mode == ir_var_inout) {
147 ir_assignment *assign;
148
149 assign = new(ctx) ir_assignment(new(ctx) ir_dereference_variable(parameters[i]),
150 param, NULL);
151 next_ir->insert_before(assign);
152 }
153
154 sig_param_iter.next();
155 param_iter.next();
156 }
157
158 /* Generate the inlined body of the function. */
159 foreach_iter(exec_list_iterator, iter, callee->body) {
160 ir_instruction *ir = (ir_instruction *)iter.get();
161 ir_instruction *new_ir = ir->clone(ht);
162
163 next_ir->insert_before(new_ir);
164 visit_tree(new_ir, replace_return_with_assignment, retval);
165 }
166
167 /* Copy back the value of any 'out' parameters from the function body
168 * variables to our own.
169 */
170 i = 0;
171 param_iter = this->actual_parameters.iterator();
172 for (i = 0; i < num_parameters; i++) {
173 ir_instruction *const param = (ir_instruction *) param_iter.get();
174
175 /* Move our param variable into the actual param if it's an 'out' type. */
176 if (parameters[i]->mode == ir_var_out ||
177 parameters[i]->mode == ir_var_inout) {
178 ir_assignment *assign;
179
180 assign = new(ctx) ir_assignment(param->as_rvalue(),
181 new(ctx) ir_dereference_variable(parameters[i]),
182 NULL);
183 next_ir->insert_before(assign);
184 }
185
186 param_iter.next();
187 }
188
189 delete [] parameters;
190
191 hash_table_dtor(ht);
192
193 if (retval)
194 return new(ctx) ir_dereference_variable(retval);
195 else
196 return NULL;
197 }
198
199
200 ir_visitor_status
201 ir_function_inlining_visitor::visit_enter(ir_expression *ir)
202 {
203 (void) ir;
204 return visit_continue_with_parent;
205 }
206
207
208 ir_visitor_status
209 ir_function_inlining_visitor::visit_enter(ir_return *ir)
210 {
211 (void) ir;
212 return visit_continue_with_parent;
213 }
214
215
216 ir_visitor_status
217 ir_function_inlining_visitor::visit_enter(ir_texture *ir)
218 {
219 (void) ir;
220 return visit_continue_with_parent;
221 }
222
223
224 ir_visitor_status
225 ir_function_inlining_visitor::visit_enter(ir_swizzle *ir)
226 {
227 (void) ir;
228 return visit_continue_with_parent;
229 }
230
231
232 ir_visitor_status
233 ir_function_inlining_visitor::visit_enter(ir_call *ir)
234 {
235 if (can_inline(ir)) {
236 (void) ir->generate_inline(ir);
237 ir->remove();
238 this->progress = true;
239 }
240
241 return visit_continue;
242 }
243
244
245 ir_visitor_status
246 ir_function_inlining_visitor::visit_enter(ir_assignment *ir)
247 {
248 ir_call *call = ir->rhs->as_call();
249 if (!call || !can_inline(call))
250 return visit_continue;
251
252 /* generates the parameter setup, function body, and returns the return
253 * value of the function
254 */
255 ir_rvalue *rhs = call->generate_inline(ir);
256 assert(rhs);
257
258 ir->rhs = rhs;
259 this->progress = true;
260
261 return visit_continue;
262 }