blob: 1adf67868ee4b0ef97386202c7ddc6bedca5609f [file] [log] [blame]
Eric Anholtcad97662010-04-07 11:46:26 -07001/*
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
Eric Anholt4b6fd392010-06-23 11:37:12 -070030#include <inttypes.h>
Eric Anholtcad97662010-04-07 11:46:26 -070031#include "ir.h"
32#include "ir_visitor.h"
33#include "ir_function_inlining.h"
Eric Anholt0d423212010-04-16 12:53:46 -070034#include "ir_expression_flattening.h"
Eric Anholtcad97662010-04-07 11:46:26 -070035#include "glsl_types.h"
Eric Anholt3d601232010-06-24 17:08:53 -070036extern "C" {
Eric Anholt4b6fd392010-06-23 11:37:12 -070037#include "hash_table.h"
Eric Anholt3d601232010-06-24 17:08:53 -070038}
Eric Anholtcad97662010-04-07 11:46:26 -070039
Ian Romanicke668c2a2010-05-26 18:58:27 -070040class ir_function_inlining_visitor : public ir_hierarchical_visitor {
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070041public:
42 ir_function_inlining_visitor()
43 {
Ian Romanicke668c2a2010-05-26 18:58:27 -070044 progress = false;
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070045 }
46
47 virtual ~ir_function_inlining_visitor()
48 {
49 /* empty */
50 }
51
Ian Romanicke668c2a2010-05-26 18:58:27 -070052 virtual ir_visitor_status visit_enter(ir_expression *);
53 virtual ir_visitor_status visit_enter(ir_call *);
54 virtual ir_visitor_status visit_enter(ir_assignment *);
55 virtual ir_visitor_status visit_enter(ir_return *);
Kenneth Graunke26d74cd2010-05-26 17:42:03 -070056 virtual ir_visitor_status visit_enter(ir_texture *);
Ian Romanicke668c2a2010-05-26 18:58:27 -070057 virtual ir_visitor_status visit_enter(ir_swizzle *);
58
59 bool progress;
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070060};
61
Eric Anholtcad97662010-04-07 11:46:26 -070062
Eric Anholt4b6fd392010-06-23 11:37:12 -070063unsigned int hash_func(const void *key)
Eric Anholtcad97662010-04-07 11:46:26 -070064{
Eric Anholt4b6fd392010-06-23 11:37:12 -070065 return (unsigned int)(uintptr_t)key;
Eric Anholtcad97662010-04-07 11:46:26 -070066}
67
Eric Anholt4b6fd392010-06-23 11:37:12 -070068int hash_compare_func(const void *key1, const void *key2)
Eric Anholtcad97662010-04-07 11:46:26 -070069{
Eric Anholt4b6fd392010-06-23 11:37:12 -070070 return key1 == key2 ? 0 : 1;
Eric Anholtcad97662010-04-07 11:46:26 -070071}
72
73bool
Eric Anholt0d423212010-04-16 12:53:46 -070074automatic_inlining_predicate(ir_instruction *ir)
75{
76 ir_call *call = ir->as_call();
77
78 if (call && can_inline(call))
79 return true;
80
81 return false;
82}
83
84bool
Eric Anholtcad97662010-04-07 11:46:26 -070085do_function_inlining(exec_list *instructions)
86{
Ian Romanicke668c2a2010-05-26 18:58:27 -070087 ir_function_inlining_visitor v;
Eric Anholtcad97662010-04-07 11:46:26 -070088
Eric Anholt0d423212010-04-16 12:53:46 -070089 do_expression_flattening(instructions, automatic_inlining_predicate);
90
Ian Romanicke668c2a2010-05-26 18:58:27 -070091 v.run(instructions);
Eric Anholtcad97662010-04-07 11:46:26 -070092
Ian Romanicke668c2a2010-05-26 18:58:27 -070093 return v.progress;
Eric Anholtcad97662010-04-07 11:46:26 -070094}
95
Eric Anholtf66ba4f2010-06-24 08:59:57 -070096static void
97replace_return_with_assignment(ir_instruction *ir, void *data)
98{
Eric Anholte33c1032010-06-24 15:13:03 -070099 void *ctx = talloc_parent(ir);
Eric Anholtf66ba4f2010-06-24 08:59:57 -0700100 ir_variable *retval = (ir_variable *)data;
101 ir_return *ret = ir->as_return();
102
103 if (ret) {
104 if (ret->value) {
Eric Anholte33c1032010-06-24 15:13:03 -0700105 ir_rvalue *lhs = new(ctx) ir_dereference_variable(retval);
106 ret->insert_before(new(ctx) ir_assignment(lhs, ret->value, NULL));
Eric Anholtf66ba4f2010-06-24 08:59:57 -0700107 ret->remove();
108 } else {
109 /* un-valued return has to be the last return, or we shouldn't
110 * have reached here. (see can_inline()).
111 */
112 assert(!ret->next->is_tail_sentinal());
113 }
114 }
115}
116
Eric Anholtcad97662010-04-07 11:46:26 -0700117ir_rvalue *
118ir_call::generate_inline(ir_instruction *next_ir)
119{
Carl Worth1660a292010-06-23 18:11:51 -0700120 void *ctx = talloc_parent(this);
Eric Anholtcad97662010-04-07 11:46:26 -0700121 ir_variable **parameters;
122 int num_parameters;
123 int i;
124 ir_variable *retval = NULL;
Eric Anholt4b6fd392010-06-23 11:37:12 -0700125 struct hash_table *ht;
126
127 ht = hash_table_ctor(0, hash_func, hash_compare_func);
Eric Anholtcad97662010-04-07 11:46:26 -0700128
129 num_parameters = 0;
130 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
131 num_parameters++;
132
133 parameters = new ir_variable *[num_parameters];
134
135 /* Generate storage for the return value. */
136 if (this->callee->return_type) {
Carl Worth1660a292010-06-23 18:11:51 -0700137 retval = new(ctx) ir_variable(this->callee->return_type, "__retval");
Eric Anholtcad97662010-04-07 11:46:26 -0700138 next_ir->insert_before(retval);
139 }
140
Eric Anholtcad97662010-04-07 11:46:26 -0700141 /* Generate the declarations for the parameters to our inlined code,
142 * and set up the mapping of real function body variables to ours.
143 */
144 i = 0;
145 exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
146 exec_list_iterator param_iter = this->actual_parameters.iterator();
147 for (i = 0; i < num_parameters; i++) {
148 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
149 ir_rvalue *param = (ir_rvalue *) param_iter.get();
150
151 /* Generate a new variable for the parameter. */
Eric Anholt4b6fd392010-06-23 11:37:12 -0700152 parameters[i] = (ir_variable *)sig_param->clone(ht);
Eric Anholt9290e0d2010-06-24 15:03:05 -0700153 parameters[i]->mode = ir_var_auto;
Eric Anholtcad97662010-04-07 11:46:26 -0700154 next_ir->insert_before(parameters[i]);
155
Eric Anholtcad97662010-04-07 11:46:26 -0700156 /* Move the actual param into our param variable if it's an 'in' type. */
Eric Anholt9290e0d2010-06-24 15:03:05 -0700157 if (sig_param->mode == ir_var_in ||
158 sig_param->mode == ir_var_inout) {
Eric Anholtcad97662010-04-07 11:46:26 -0700159 ir_assignment *assign;
160
Carl Worth1660a292010-06-23 18:11:51 -0700161 assign = new(ctx) ir_assignment(new(ctx) ir_dereference_variable(parameters[i]),
162 param, NULL);
Eric Anholtcad97662010-04-07 11:46:26 -0700163 next_ir->insert_before(assign);
164 }
165
166 sig_param_iter.next();
167 param_iter.next();
168 }
169
170 /* Generate the inlined body of the function. */
171 foreach_iter(exec_list_iterator, iter, callee->body) {
172 ir_instruction *ir = (ir_instruction *)iter.get();
Eric Anholtf66ba4f2010-06-24 08:59:57 -0700173 ir_instruction *new_ir = ir->clone(ht);
Eric Anholtcad97662010-04-07 11:46:26 -0700174
Eric Anholtf66ba4f2010-06-24 08:59:57 -0700175 next_ir->insert_before(new_ir);
176 visit_tree(new_ir, replace_return_with_assignment, retval);
Eric Anholtcad97662010-04-07 11:46:26 -0700177 }
178
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700179 /* Copy back the value of any 'out' parameters from the function body
180 * variables to our own.
Eric Anholtcad97662010-04-07 11:46:26 -0700181 */
182 i = 0;
183 param_iter = this->actual_parameters.iterator();
184 for (i = 0; i < num_parameters; i++) {
185 ir_instruction *const param = (ir_instruction *) param_iter.get();
186
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700187 /* Move our param variable into the actual param if it's an 'out' type. */
Eric Anholtcad97662010-04-07 11:46:26 -0700188 if (parameters[i]->mode == ir_var_out ||
189 parameters[i]->mode == ir_var_inout) {
190 ir_assignment *assign;
191
Carl Worth1660a292010-06-23 18:11:51 -0700192 assign = new(ctx) ir_assignment(param->as_rvalue(),
193 new(ctx) ir_dereference_variable(parameters[i]),
194 NULL);
Eric Anholtcad97662010-04-07 11:46:26 -0700195 next_ir->insert_before(assign);
196 }
197
198 param_iter.next();
199 }
200
Ian Romanick2f8b0432010-06-09 11:00:00 -0700201 delete [] parameters;
Eric Anholtcad97662010-04-07 11:46:26 -0700202
Eric Anholt4b6fd392010-06-23 11:37:12 -0700203 hash_table_dtor(ht);
204
Eric Anholtcad97662010-04-07 11:46:26 -0700205 if (retval)
Carl Worth1660a292010-06-23 18:11:51 -0700206 return new(ctx) ir_dereference_variable(retval);
Eric Anholtcad97662010-04-07 11:46:26 -0700207 else
208 return NULL;
209}
210
Ian Romanicke668c2a2010-05-26 18:58:27 -0700211
212ir_visitor_status
213ir_function_inlining_visitor::visit_enter(ir_expression *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700214{
215 (void) ir;
Ian Romanicke668c2a2010-05-26 18:58:27 -0700216 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700217}
218
219
Ian Romanicke668c2a2010-05-26 18:58:27 -0700220ir_visitor_status
221ir_function_inlining_visitor::visit_enter(ir_return *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700222{
223 (void) ir;
Ian Romanicke668c2a2010-05-26 18:58:27 -0700224 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700225}
226
227
Ian Romanicke668c2a2010-05-26 18:58:27 -0700228ir_visitor_status
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700229ir_function_inlining_visitor::visit_enter(ir_texture *ir)
230{
231 (void) ir;
232 return visit_continue_with_parent;
233}
234
235
236ir_visitor_status
Ian Romanicke668c2a2010-05-26 18:58:27 -0700237ir_function_inlining_visitor::visit_enter(ir_swizzle *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700238{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700239 (void) ir;
240 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700241}
242
243
Ian Romanicke668c2a2010-05-26 18:58:27 -0700244ir_visitor_status
245ir_function_inlining_visitor::visit_enter(ir_call *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700246{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700247 if (can_inline(ir)) {
248 (void) ir->generate_inline(ir);
249 ir->remove();
250 this->progress = true;
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700251 }
Eric Anholtcad97662010-04-07 11:46:26 -0700252
Ian Romanicke668c2a2010-05-26 18:58:27 -0700253 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700254}
255
256
Ian Romanicke668c2a2010-05-26 18:58:27 -0700257ir_visitor_status
258ir_function_inlining_visitor::visit_enter(ir_assignment *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700259{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700260 ir_call *call = ir->rhs->as_call();
261 if (!call || !can_inline(call))
262 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700263
Ian Romanicke668c2a2010-05-26 18:58:27 -0700264 /* generates the parameter setup, function body, and returns the return
265 * value of the function
266 */
267 ir_rvalue *rhs = call->generate_inline(ir);
268 assert(rhs);
Eric Anholtcad97662010-04-07 11:46:26 -0700269
Ian Romanicke668c2a2010-05-26 18:58:27 -0700270 ir->rhs = rhs;
271 this->progress = true;
Ian Romanickc7b10462010-05-19 13:20:12 +0200272
Ian Romanicke668c2a2010-05-26 18:58:27 -0700273 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700274}