Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / glsl / nir / nir_inline_functions.c
1 /*
2 * Copyright © 2015 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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "nir.h"
25 #include "nir_builder.h"
26 #include "nir_control_flow.h"
27
28 struct inline_functions_state {
29 nir_function_impl *impl;
30 nir_builder builder;
31 bool progress;
32 };
33
34 static bool
35 inline_functions_block(nir_block *block, void *void_state)
36 {
37 struct inline_functions_state *state = void_state;
38
39 nir_builder *b = &state->builder;
40
41 /* This is tricky. We're iterating over instructions in a block but, as
42 * we go, the block and its instruction list are being split into
43 * pieces. However, this *should* be safe since foreach_safe always
44 * stashes the next thing in the iteration. That next thing will
45 * properly get moved to the next block when it gets split, and we
46 * continue iterating there.
47 */
48 nir_foreach_instr_safe(block, instr) {
49 if (instr->type != nir_instr_type_call)
50 continue;
51
52 state->progress = true;
53
54 nir_call_instr *call = nir_instr_as_call(instr);
55 assert(call->callee->impl);
56
57 nir_function_impl *callee_copy =
58 nir_function_impl_clone(call->callee->impl);
59
60 exec_list_append(&state->impl->locals, &callee_copy->locals);
61 exec_list_append(&state->impl->registers, &callee_copy->registers);
62
63 b->cursor = nir_before_instr(&call->instr);
64
65 /* Add copies of all in parameters */
66 assert(call->num_params == callee_copy->num_params);
67 for (unsigned i = 0; i < callee_copy->num_params; i++) {
68 /* Only in or inout parameters */
69 if (call->callee->params[i].param_type == nir_parameter_out)
70 continue;
71
72 nir_copy_deref_var(b, nir_deref_var_create(b->shader,
73 callee_copy->params[i]),
74 call->params[i]);
75 }
76
77 /* Pluck the body out of the function and place it here */
78 nir_cf_list body;
79 nir_cf_list_extract(&body, &callee_copy->body);
80 nir_cf_reinsert(&body, b->cursor);
81
82 b->cursor = nir_before_instr(&call->instr);
83
84 /* Add copies of all out parameters and the return */
85 assert(call->num_params == callee_copy->num_params);
86 for (unsigned i = 0; i < callee_copy->num_params; i++) {
87 /* Only out or inout parameters */
88 if (call->callee->params[i].param_type == nir_parameter_in)
89 continue;
90
91 nir_copy_deref_var(b, call->params[i],
92 nir_deref_var_create(b->shader,
93 callee_copy->params[i]));
94 }
95 if (!glsl_type_is_void(call->callee->return_type)) {
96 nir_copy_deref_var(b, call->return_deref,
97 nir_deref_var_create(b->shader,
98 callee_copy->return_var));
99 }
100
101 nir_instr_remove(&call->instr);
102 }
103
104 return true;
105 }
106
107 bool
108 nir_inline_functions_impl(nir_function_impl *impl)
109 {
110 struct inline_functions_state state;
111
112 state.progress = false;
113 state.impl = impl;
114 nir_builder_init(&state.builder, impl);
115
116 nir_foreach_block(impl, inline_functions_block, &state);
117
118 /* SSA and register indices are completely messed up now */
119 nir_index_ssa_defs(impl);
120 nir_index_local_regs(impl);
121
122 nir_metadata_preserve(impl, nir_metadata_none);
123
124 return state.progress;
125 }
126
127 bool
128 nir_inline_functions(nir_shader *shader)
129 {
130 bool progress = false;
131
132 nir_foreach_function(shader, function) {
133 if (function->impl)
134 progress = nir_inline_functions_impl(function->impl) || progress;
135 }
136
137 return progress;
138 }