e55780c940eb4a06f4b4c042c58a3a29beab624d
[platform/upstream/mesa.git] / 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 unsigned int hash_func(const void *key)
62 {
63    return (unsigned int)(uintptr_t)key;
64 }
65
66 int hash_compare_func(const void *key1, const void *key2)
67 {
68    return key1 == key2 ? 0 : 1;
69 }
70
71 bool
72 automatic_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
82 bool
83 do_function_inlining(exec_list *instructions)
84 {
85    ir_function_inlining_visitor v;
86
87    do_expression_flattening(instructions, automatic_inlining_predicate);
88
89    v.run(instructions);
90
91    return v.progress;
92 }
93
94 static void
95 replace_return_with_assignment(ir_instruction *ir, void *data)
96 {
97    void *ctx = talloc_parent(ir);
98    ir_variable *retval = (ir_variable *)data;
99    ir_return *ret = ir->as_return();
100
101    if (ret) {
102       if (ret->value) {
103          ir_rvalue *lhs = new(ctx) ir_dereference_variable(retval);
104          ret->insert_before(new(ctx) ir_assignment(lhs, ret->value, NULL));
105          ret->remove();
106       } else {
107          /* un-valued return has to be the last return, or we shouldn't
108           * have reached here. (see can_inline()).
109           */
110          assert(!ret->next->is_tail_sentinal());
111       }
112    }
113 }
114
115 ir_rvalue *
116 ir_call::generate_inline(ir_instruction *next_ir)
117 {
118    void *ctx = talloc_parent(this);
119    ir_variable **parameters;
120    int num_parameters;
121    int i;
122    ir_variable *retval = NULL;
123    struct hash_table *ht;
124
125    ht = hash_table_ctor(0, hash_func, hash_compare_func);
126
127    num_parameters = 0;
128    foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
129       num_parameters++;
130
131    parameters = new ir_variable *[num_parameters];
132
133    /* Generate storage for the return value. */
134    if (this->callee->return_type) {
135       retval = new(ctx) ir_variable(this->callee->return_type, "__retval");
136       next_ir->insert_before(retval);
137    }
138
139    /* Generate the declarations for the parameters to our inlined code,
140     * and set up the mapping of real function body variables to ours.
141     */
142    i = 0;
143    exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
144    exec_list_iterator param_iter = this->actual_parameters.iterator();
145    for (i = 0; i < num_parameters; i++) {
146       const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
147       ir_rvalue *param = (ir_rvalue *) param_iter.get();
148
149       /* Generate a new variable for the parameter. */
150       parameters[i] = (ir_variable *)sig_param->clone(ht);
151       parameters[i]->mode = ir_var_auto;
152       next_ir->insert_before(parameters[i]);
153
154       /* Move the actual param into our param variable if it's an 'in' type. */
155       if (sig_param->mode == ir_var_in ||
156           sig_param->mode == ir_var_inout) {
157          ir_assignment *assign;
158
159          assign = new(ctx) ir_assignment(new(ctx) ir_dereference_variable(parameters[i]),
160                                          param, NULL);
161          next_ir->insert_before(assign);
162       }
163
164       sig_param_iter.next();
165       param_iter.next();
166    }
167
168    /* Generate the inlined body of the function. */
169    foreach_iter(exec_list_iterator, iter, callee->body) {
170       ir_instruction *ir = (ir_instruction *)iter.get();
171       ir_instruction *new_ir = ir->clone(ht);
172
173       next_ir->insert_before(new_ir);
174       visit_tree(new_ir, replace_return_with_assignment, retval);
175    }
176
177    /* Copy back the value of any 'out' parameters from the function body
178     * variables to our own.
179     */
180    i = 0;
181    param_iter = this->actual_parameters.iterator();
182    for (i = 0; i < num_parameters; i++) {
183       ir_instruction *const param = (ir_instruction *) param_iter.get();
184
185       /* Move our param variable into the actual param if it's an 'out' type. */
186       if (parameters[i]->mode == ir_var_out ||
187           parameters[i]->mode == ir_var_inout) {
188          ir_assignment *assign;
189
190          assign = new(ctx) ir_assignment(param->as_rvalue(),
191                                          new(ctx) ir_dereference_variable(parameters[i]),
192                                          NULL);
193          next_ir->insert_before(assign);
194       }
195
196       param_iter.next();
197    }
198
199    delete [] parameters;
200
201    hash_table_dtor(ht);
202
203    if (retval)
204       return new(ctx) ir_dereference_variable(retval);
205    else
206       return NULL;
207 }
208
209
210 ir_visitor_status
211 ir_function_inlining_visitor::visit_enter(ir_expression *ir)
212 {
213    (void) ir;
214    return visit_continue_with_parent;
215 }
216
217
218 ir_visitor_status
219 ir_function_inlining_visitor::visit_enter(ir_return *ir)
220 {
221    (void) ir;
222    return visit_continue_with_parent;
223 }
224
225
226 ir_visitor_status
227 ir_function_inlining_visitor::visit_enter(ir_texture *ir)
228 {
229    (void) ir;
230    return visit_continue_with_parent;
231 }
232
233
234 ir_visitor_status
235 ir_function_inlining_visitor::visit_enter(ir_swizzle *ir)
236 {
237    (void) ir;
238    return visit_continue_with_parent;
239 }
240
241
242 ir_visitor_status
243 ir_function_inlining_visitor::visit_enter(ir_call *ir)
244 {
245    if (can_inline(ir)) {
246       (void) ir->generate_inline(ir);
247       ir->remove();
248       this->progress = true;
249    }
250
251    return visit_continue;
252 }
253
254
255 ir_visitor_status
256 ir_function_inlining_visitor::visit_enter(ir_assignment *ir)
257 {
258    ir_call *call = ir->rhs->as_call();
259    if (!call || !can_inline(call))
260       return visit_continue;
261
262    /* generates the parameter setup, function body, and returns the return
263     * value of the function
264     */
265    ir_rvalue *rhs = call->generate_inline(ir);
266    assert(rhs);
267
268    ir->rhs = rhs;
269    this->progress = true;
270
271    return visit_continue;
272 }