blob: c391f12d883c615c03aa6d2562b8d8415538863d [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 Anholt4b6fd392010-06-23 11:37:12 -070036#include "hash_table.h"
Eric Anholtcad97662010-04-07 11:46:26 -070037
Ian Romanicke668c2a2010-05-26 18:58:27 -070038class ir_function_inlining_visitor : public ir_hierarchical_visitor {
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070039public:
40 ir_function_inlining_visitor()
41 {
Ian Romanicke668c2a2010-05-26 18:58:27 -070042 progress = false;
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070043 }
44
45 virtual ~ir_function_inlining_visitor()
46 {
47 /* empty */
48 }
49
Ian Romanicke668c2a2010-05-26 18:58:27 -070050 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 *);
Kenneth Graunke26d74cd2010-05-26 17:42:03 -070054 virtual ir_visitor_status visit_enter(ir_texture *);
Ian Romanicke668c2a2010-05-26 18:58:27 -070055 virtual ir_visitor_status visit_enter(ir_swizzle *);
56
57 bool progress;
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070058};
59
Eric Anholtcad97662010-04-07 11:46:26 -070060
Eric Anholtcad97662010-04-07 11:46:26 -070061bool
Eric Anholt0d423212010-04-16 12:53:46 -070062automatic_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
72bool
Eric Anholtcad97662010-04-07 11:46:26 -070073do_function_inlining(exec_list *instructions)
74{
Ian Romanicke668c2a2010-05-26 18:58:27 -070075 ir_function_inlining_visitor v;
Eric Anholtcad97662010-04-07 11:46:26 -070076
Eric Anholt0d423212010-04-16 12:53:46 -070077 do_expression_flattening(instructions, automatic_inlining_predicate);
78
Ian Romanicke668c2a2010-05-26 18:58:27 -070079 v.run(instructions);
Eric Anholtcad97662010-04-07 11:46:26 -070080
Ian Romanicke668c2a2010-05-26 18:58:27 -070081 return v.progress;
Eric Anholtcad97662010-04-07 11:46:26 -070082}
83
Eric Anholtf66ba4f2010-06-24 08:59:57 -070084static void
85replace_return_with_assignment(ir_instruction *ir, void *data)
86{
Eric Anholte33c1032010-06-24 15:13:03 -070087 void *ctx = talloc_parent(ir);
Eric Anholtf66ba4f2010-06-24 08:59:57 -070088 ir_variable *retval = (ir_variable *)data;
89 ir_return *ret = ir->as_return();
90
91 if (ret) {
92 if (ret->value) {
Eric Anholte33c1032010-06-24 15:13:03 -070093 ir_rvalue *lhs = new(ctx) ir_dereference_variable(retval);
Kenneth Graunkec7a18da2010-07-19 21:44:03 -070094 ret->replace_with(new(ctx) ir_assignment(lhs, ret->value, NULL));
Eric Anholtf66ba4f2010-06-24 08:59:57 -070095 } else {
96 /* un-valued return has to be the last return, or we shouldn't
97 * have reached here. (see can_inline()).
98 */
99 assert(!ret->next->is_tail_sentinal());
100 }
101 }
102}
103
Eric Anholtcad97662010-04-07 11:46:26 -0700104ir_rvalue *
105ir_call::generate_inline(ir_instruction *next_ir)
106{
Carl Worth1660a292010-06-23 18:11:51 -0700107 void *ctx = talloc_parent(this);
Eric Anholtcad97662010-04-07 11:46:26 -0700108 ir_variable **parameters;
109 int num_parameters;
110 int i;
111 ir_variable *retval = NULL;
Eric Anholt4b6fd392010-06-23 11:37:12 -0700112 struct hash_table *ht;
113
Ian Romanickd1a1ee52010-07-06 14:49:14 -0700114 ht = hash_table_ctor(0, hash_table_pointer_hash, hash_table_pointer_compare);
Eric Anholtcad97662010-04-07 11:46:26 -0700115
116 num_parameters = 0;
117 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
118 num_parameters++;
119
120 parameters = new ir_variable *[num_parameters];
121
122 /* Generate storage for the return value. */
123 if (this->callee->return_type) {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700124 retval = new(ctx) ir_variable(this->callee->return_type, "__retval",
125 ir_var_auto);
Eric Anholtcad97662010-04-07 11:46:26 -0700126 next_ir->insert_before(retval);
127 }
128
Eric Anholtcad97662010-04-07 11:46:26 -0700129 /* 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. */
Ian Romanickca088cc2010-07-06 17:41:02 -0700140 parameters[i] = sig_param->clone(ht);
Eric Anholt9290e0d2010-06-24 15:03:05 -0700141 parameters[i]->mode = ir_var_auto;
Eric Anholtcad97662010-04-07 11:46:26 -0700142 next_ir->insert_before(parameters[i]);
143
Eric Anholtcad97662010-04-07 11:46:26 -0700144 /* Move the actual param into our param variable if it's an 'in' type. */
Eric Anholt9290e0d2010-06-24 15:03:05 -0700145 if (sig_param->mode == ir_var_in ||
146 sig_param->mode == ir_var_inout) {
Eric Anholtcad97662010-04-07 11:46:26 -0700147 ir_assignment *assign;
148
Carl Worth1660a292010-06-23 18:11:51 -0700149 assign = new(ctx) ir_assignment(new(ctx) ir_dereference_variable(parameters[i]),
150 param, NULL);
Eric Anholtcad97662010-04-07 11:46:26 -0700151 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();
Eric Anholtf66ba4f2010-06-24 08:59:57 -0700161 ir_instruction *new_ir = ir->clone(ht);
Eric Anholtcad97662010-04-07 11:46:26 -0700162
Eric Anholtf66ba4f2010-06-24 08:59:57 -0700163 next_ir->insert_before(new_ir);
164 visit_tree(new_ir, replace_return_with_assignment, retval);
Eric Anholtcad97662010-04-07 11:46:26 -0700165 }
166
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700167 /* Copy back the value of any 'out' parameters from the function body
168 * variables to our own.
Eric Anholtcad97662010-04-07 11:46:26 -0700169 */
170 i = 0;
171 param_iter = this->actual_parameters.iterator();
Eric Anholt325a4972010-07-20 16:03:46 -0700172 sig_param_iter = this->callee->parameters.iterator();
Eric Anholtcad97662010-04-07 11:46:26 -0700173 for (i = 0; i < num_parameters; i++) {
174 ir_instruction *const param = (ir_instruction *) param_iter.get();
Eric Anholt325a4972010-07-20 16:03:46 -0700175 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
Eric Anholtcad97662010-04-07 11:46:26 -0700176
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700177 /* Move our param variable into the actual param if it's an 'out' type. */
Eric Anholt325a4972010-07-20 16:03:46 -0700178 if (sig_param->mode == ir_var_out ||
179 sig_param->mode == ir_var_inout) {
Eric Anholtcad97662010-04-07 11:46:26 -0700180 ir_assignment *assign;
181
Eric Anholt325a4972010-07-20 16:03:46 -0700182 assign = new(ctx) ir_assignment(param->clone(NULL)->as_rvalue(),
Carl Worth1660a292010-06-23 18:11:51 -0700183 new(ctx) ir_dereference_variable(parameters[i]),
184 NULL);
Eric Anholtcad97662010-04-07 11:46:26 -0700185 next_ir->insert_before(assign);
186 }
187
188 param_iter.next();
Eric Anholt325a4972010-07-20 16:03:46 -0700189 sig_param_iter.next();
Eric Anholtcad97662010-04-07 11:46:26 -0700190 }
191
Ian Romanick2f8b0432010-06-09 11:00:00 -0700192 delete [] parameters;
Eric Anholtcad97662010-04-07 11:46:26 -0700193
Eric Anholt4b6fd392010-06-23 11:37:12 -0700194 hash_table_dtor(ht);
195
Eric Anholtcad97662010-04-07 11:46:26 -0700196 if (retval)
Carl Worth1660a292010-06-23 18:11:51 -0700197 return new(ctx) ir_dereference_variable(retval);
Eric Anholtcad97662010-04-07 11:46:26 -0700198 else
199 return NULL;
200}
201
Ian Romanicke668c2a2010-05-26 18:58:27 -0700202
203ir_visitor_status
204ir_function_inlining_visitor::visit_enter(ir_expression *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700205{
206 (void) ir;
Ian Romanicke668c2a2010-05-26 18:58:27 -0700207 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700208}
209
210
Ian Romanicke668c2a2010-05-26 18:58:27 -0700211ir_visitor_status
212ir_function_inlining_visitor::visit_enter(ir_return *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700213{
214 (void) ir;
Ian Romanicke668c2a2010-05-26 18:58:27 -0700215 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700216}
217
218
Ian Romanicke668c2a2010-05-26 18:58:27 -0700219ir_visitor_status
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700220ir_function_inlining_visitor::visit_enter(ir_texture *ir)
221{
222 (void) ir;
223 return visit_continue_with_parent;
224}
225
226
227ir_visitor_status
Ian Romanicke668c2a2010-05-26 18:58:27 -0700228ir_function_inlining_visitor::visit_enter(ir_swizzle *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700229{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700230 (void) ir;
231 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700232}
233
234
Ian Romanicke668c2a2010-05-26 18:58:27 -0700235ir_visitor_status
236ir_function_inlining_visitor::visit_enter(ir_call *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700237{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700238 if (can_inline(ir)) {
Eric Anholtd2afc872010-07-12 10:13:20 -0700239 /* If the call was part of some tree, then it should have been
240 * flattened out or we shouldn't have seen it because of a
241 * visit_continue_with_parent in this visitor.
242 */
243 assert(ir == base_ir);
244
Ian Romanicke668c2a2010-05-26 18:58:27 -0700245 (void) ir->generate_inline(ir);
246 ir->remove();
247 this->progress = true;
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700248 }
Eric Anholtcad97662010-04-07 11:46:26 -0700249
Ian Romanicke668c2a2010-05-26 18:58:27 -0700250 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700251}
252
253
Ian Romanicke668c2a2010-05-26 18:58:27 -0700254ir_visitor_status
255ir_function_inlining_visitor::visit_enter(ir_assignment *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700256{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700257 ir_call *call = ir->rhs->as_call();
258 if (!call || !can_inline(call))
259 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700260
Ian Romanicke668c2a2010-05-26 18:58:27 -0700261 /* generates the parameter setup, function body, and returns the return
262 * value of the function
263 */
264 ir_rvalue *rhs = call->generate_inline(ir);
265 assert(rhs);
Eric Anholtcad97662010-04-07 11:46:26 -0700266
Ian Romanicke668c2a2010-05-26 18:58:27 -0700267 ir->rhs = rhs;
268 this->progress = true;
Ian Romanickc7b10462010-05-19 13:20:12 +0200269
Ian Romanicke668c2a2010-05-26 18:58:27 -0700270 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700271}