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