blob: 851c0dd9f7b1c4c8f75ab48dbd0c055b96e5ba59 [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 Anholt4b6fd392010-06-23 11:37:12 -070061unsigned int hash_func(const void *key)
Eric Anholtcad97662010-04-07 11:46:26 -070062{
Eric Anholt4b6fd392010-06-23 11:37:12 -070063 return (unsigned int)(uintptr_t)key;
Eric Anholtcad97662010-04-07 11:46:26 -070064}
65
Eric Anholt4b6fd392010-06-23 11:37:12 -070066int hash_compare_func(const void *key1, const void *key2)
Eric Anholtcad97662010-04-07 11:46:26 -070067{
Eric Anholt4b6fd392010-06-23 11:37:12 -070068 return key1 == key2 ? 0 : 1;
Eric Anholtcad97662010-04-07 11:46:26 -070069}
70
71bool
Eric Anholt0d423212010-04-16 12:53:46 -070072automatic_inlining_predicate(ir_instruction *ir)
73{
74 ir_call *call = ir->as_call();
75
76 if (call && can_inline(call))
77 return true;
78
79 return false;
80}
81
82bool
Eric Anholtcad97662010-04-07 11:46:26 -070083do_function_inlining(exec_list *instructions)
84{
Ian Romanicke668c2a2010-05-26 18:58:27 -070085 ir_function_inlining_visitor v;
Eric Anholtcad97662010-04-07 11:46:26 -070086
Eric Anholt0d423212010-04-16 12:53:46 -070087 do_expression_flattening(instructions, automatic_inlining_predicate);
88
Ian Romanicke668c2a2010-05-26 18:58:27 -070089 v.run(instructions);
Eric Anholtcad97662010-04-07 11:46:26 -070090
Ian Romanicke668c2a2010-05-26 18:58:27 -070091 return v.progress;
Eric Anholtcad97662010-04-07 11:46:26 -070092}
93
Eric Anholtf66ba4f2010-06-24 08:59:57 -070094static void
95replace_return_with_assignment(ir_instruction *ir, void *data)
96{
97 ir_variable *retval = (ir_variable *)data;
98 ir_return *ret = ir->as_return();
99
100 if (ret) {
101 if (ret->value) {
102 ir_rvalue *lhs = new ir_dereference_variable(retval);
103 ret->insert_before(new ir_assignment(lhs, ret->value, NULL));
104 ret->remove();
105 } else {
106 /* un-valued return has to be the last return, or we shouldn't
107 * have reached here. (see can_inline()).
108 */
109 assert(!ret->next->is_tail_sentinal());
110 }
111 }
112}
113
Eric Anholtcad97662010-04-07 11:46:26 -0700114ir_rvalue *
115ir_call::generate_inline(ir_instruction *next_ir)
116{
117 ir_variable **parameters;
118 int num_parameters;
119 int i;
120 ir_variable *retval = NULL;
Eric Anholt4b6fd392010-06-23 11:37:12 -0700121 struct hash_table *ht;
122
123 ht = hash_table_ctor(0, hash_func, hash_compare_func);
Eric Anholtcad97662010-04-07 11:46:26 -0700124
125 num_parameters = 0;
126 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
127 num_parameters++;
128
129 parameters = new ir_variable *[num_parameters];
130
131 /* Generate storage for the return value. */
132 if (this->callee->return_type) {
133 retval = new ir_variable(this->callee->return_type, "__retval");
134 next_ir->insert_before(retval);
135 }
136
Eric Anholtcad97662010-04-07 11:46:26 -0700137 /* Generate the declarations for the parameters to our inlined code,
138 * and set up the mapping of real function body variables to ours.
139 */
140 i = 0;
141 exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
142 exec_list_iterator param_iter = this->actual_parameters.iterator();
143 for (i = 0; i < num_parameters; i++) {
144 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
145 ir_rvalue *param = (ir_rvalue *) param_iter.get();
146
147 /* Generate a new variable for the parameter. */
Eric Anholt4b6fd392010-06-23 11:37:12 -0700148 parameters[i] = (ir_variable *)sig_param->clone(ht);
Eric Anholt9290e0d2010-06-24 15:03:05 -0700149 parameters[i]->mode = ir_var_auto;
Eric Anholtcad97662010-04-07 11:46:26 -0700150 next_ir->insert_before(parameters[i]);
151
Eric Anholtcad97662010-04-07 11:46:26 -0700152 /* Move the actual param into our param variable if it's an 'in' type. */
Eric Anholt9290e0d2010-06-24 15:03:05 -0700153 if (sig_param->mode == ir_var_in ||
154 sig_param->mode == ir_var_inout) {
Eric Anholtcad97662010-04-07 11:46:26 -0700155 ir_assignment *assign;
156
Ian Romanick70fe8b62010-05-19 11:37:35 +0200157 assign = new ir_assignment(new ir_dereference_variable(parameters[i]),
Eric Anholtcad97662010-04-07 11:46:26 -0700158 param, NULL);
159 next_ir->insert_before(assign);
160 }
161
162 sig_param_iter.next();
163 param_iter.next();
164 }
165
166 /* Generate the inlined body of the function. */
167 foreach_iter(exec_list_iterator, iter, callee->body) {
168 ir_instruction *ir = (ir_instruction *)iter.get();
Eric Anholtf66ba4f2010-06-24 08:59:57 -0700169 ir_instruction *new_ir = ir->clone(ht);
Eric Anholtcad97662010-04-07 11:46:26 -0700170
Eric Anholtf66ba4f2010-06-24 08:59:57 -0700171 next_ir->insert_before(new_ir);
172 visit_tree(new_ir, replace_return_with_assignment, retval);
Eric Anholtcad97662010-04-07 11:46:26 -0700173 }
174
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700175 /* Copy back the value of any 'out' parameters from the function body
176 * variables to our own.
Eric Anholtcad97662010-04-07 11:46:26 -0700177 */
178 i = 0;
179 param_iter = this->actual_parameters.iterator();
180 for (i = 0; i < num_parameters; i++) {
181 ir_instruction *const param = (ir_instruction *) param_iter.get();
182
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700183 /* Move our param variable into the actual param if it's an 'out' type. */
Eric Anholtcad97662010-04-07 11:46:26 -0700184 if (parameters[i]->mode == ir_var_out ||
185 parameters[i]->mode == ir_var_inout) {
186 ir_assignment *assign;
187
188 assign = new ir_assignment(param->as_rvalue(),
Ian Romanick70fe8b62010-05-19 11:37:35 +0200189 new ir_dereference_variable(parameters[i]),
Eric Anholtcad97662010-04-07 11:46:26 -0700190 NULL);
191 next_ir->insert_before(assign);
192 }
193
194 param_iter.next();
195 }
196
Ian Romanick2f8b0432010-06-09 11:00:00 -0700197 delete [] parameters;
Eric Anholtcad97662010-04-07 11:46:26 -0700198
Eric Anholt4b6fd392010-06-23 11:37:12 -0700199 hash_table_dtor(ht);
200
Eric Anholtcad97662010-04-07 11:46:26 -0700201 if (retval)
Ian Romanick70fe8b62010-05-19 11:37:35 +0200202 return new ir_dereference_variable(retval);
Eric Anholtcad97662010-04-07 11:46:26 -0700203 else
204 return NULL;
205}
206
Ian Romanicke668c2a2010-05-26 18:58:27 -0700207
208ir_visitor_status
209ir_function_inlining_visitor::visit_enter(ir_expression *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700210{
211 (void) ir;
Ian Romanicke668c2a2010-05-26 18:58:27 -0700212 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700213}
214
215
Ian Romanicke668c2a2010-05-26 18:58:27 -0700216ir_visitor_status
217ir_function_inlining_visitor::visit_enter(ir_return *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700218{
219 (void) ir;
Ian Romanicke668c2a2010-05-26 18:58:27 -0700220 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700221}
222
223
Ian Romanicke668c2a2010-05-26 18:58:27 -0700224ir_visitor_status
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700225ir_function_inlining_visitor::visit_enter(ir_texture *ir)
226{
227 (void) ir;
228 return visit_continue_with_parent;
229}
230
231
232ir_visitor_status
Ian Romanicke668c2a2010-05-26 18:58:27 -0700233ir_function_inlining_visitor::visit_enter(ir_swizzle *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700234{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700235 (void) ir;
236 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700237}
238
239
Ian Romanicke668c2a2010-05-26 18:58:27 -0700240ir_visitor_status
241ir_function_inlining_visitor::visit_enter(ir_call *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700242{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700243 if (can_inline(ir)) {
244 (void) ir->generate_inline(ir);
245 ir->remove();
246 this->progress = true;
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700247 }
Eric Anholtcad97662010-04-07 11:46:26 -0700248
Ian Romanicke668c2a2010-05-26 18:58:27 -0700249 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700250}
251
252
Ian Romanicke668c2a2010-05-26 18:58:27 -0700253ir_visitor_status
254ir_function_inlining_visitor::visit_enter(ir_assignment *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700255{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700256 ir_call *call = ir->rhs->as_call();
257 if (!call || !can_inline(call))
258 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700259
Ian Romanicke668c2a2010-05-26 18:58:27 -0700260 /* generates the parameter setup, function body, and returns the return
261 * value of the function
262 */
263 ir_rvalue *rhs = call->generate_inline(ir);
264 assert(rhs);
Eric Anholtcad97662010-04-07 11:46:26 -0700265
Ian Romanicke668c2a2010-05-26 18:58:27 -0700266 ir->rhs = rhs;
267 this->progress = true;
Ian Romanickc7b10462010-05-19 13:20:12 +0200268
Ian Romanicke668c2a2010-05-26 18:58:27 -0700269 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700270}