Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 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 | |
Eric Anholt | 4b6fd39 | 2010-06-23 11:37:12 -0700 | [diff] [blame] | 30 | #include <inttypes.h> |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 31 | #include "ir.h" |
| 32 | #include "ir_visitor.h" |
| 33 | #include "ir_function_inlining.h" |
Eric Anholt | 0d42321 | 2010-04-16 12:53:46 -0700 | [diff] [blame] | 34 | #include "ir_expression_flattening.h" |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 35 | #include "glsl_types.h" |
Eric Anholt | 4b6fd39 | 2010-06-23 11:37:12 -0700 | [diff] [blame] | 36 | #include "hash_table.h" |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 37 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 38 | class ir_function_inlining_visitor : public ir_hierarchical_visitor { |
Eric Anholt | bdd9b1f | 2010-05-05 11:45:30 -0700 | [diff] [blame] | 39 | public: |
| 40 | ir_function_inlining_visitor() |
| 41 | { |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 42 | progress = false; |
Eric Anholt | bdd9b1f | 2010-05-05 11:45:30 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | virtual ~ir_function_inlining_visitor() |
| 46 | { |
| 47 | /* empty */ |
| 48 | } |
| 49 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 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 *); |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 54 | virtual ir_visitor_status visit_enter(ir_texture *); |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 55 | virtual ir_visitor_status visit_enter(ir_swizzle *); |
| 56 | |
| 57 | bool progress; |
Eric Anholt | bdd9b1f | 2010-05-05 11:45:30 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 60 | |
Eric Anholt | 4b6fd39 | 2010-06-23 11:37:12 -0700 | [diff] [blame] | 61 | unsigned int hash_func(const void *key) |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 62 | { |
Eric Anholt | 4b6fd39 | 2010-06-23 11:37:12 -0700 | [diff] [blame] | 63 | return (unsigned int)(uintptr_t)key; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Eric Anholt | 4b6fd39 | 2010-06-23 11:37:12 -0700 | [diff] [blame] | 66 | int hash_compare_func(const void *key1, const void *key2) |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 67 | { |
Eric Anholt | 4b6fd39 | 2010-06-23 11:37:12 -0700 | [diff] [blame] | 68 | return key1 == key2 ? 0 : 1; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool |
Eric Anholt | 0d42321 | 2010-04-16 12:53:46 -0700 | [diff] [blame] | 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 |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 83 | do_function_inlining(exec_list *instructions) |
| 84 | { |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 85 | ir_function_inlining_visitor v; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 86 | |
Eric Anholt | 0d42321 | 2010-04-16 12:53:46 -0700 | [diff] [blame] | 87 | do_expression_flattening(instructions, automatic_inlining_predicate); |
| 88 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 89 | v.run(instructions); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 90 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 91 | return v.progress; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | ir_rvalue * |
| 95 | ir_call::generate_inline(ir_instruction *next_ir) |
| 96 | { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame^] | 97 | void *ctx = talloc_parent(this); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 98 | ir_variable **parameters; |
| 99 | int num_parameters; |
| 100 | int i; |
| 101 | ir_variable *retval = NULL; |
Eric Anholt | 4b6fd39 | 2010-06-23 11:37:12 -0700 | [diff] [blame] | 102 | struct hash_table *ht; |
| 103 | |
| 104 | ht = hash_table_ctor(0, hash_func, hash_compare_func); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 105 | |
| 106 | num_parameters = 0; |
| 107 | foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters) |
| 108 | num_parameters++; |
| 109 | |
| 110 | parameters = new ir_variable *[num_parameters]; |
| 111 | |
| 112 | /* Generate storage for the return value. */ |
| 113 | if (this->callee->return_type) { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame^] | 114 | retval = new(ctx) ir_variable(this->callee->return_type, "__retval"); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 115 | next_ir->insert_before(retval); |
| 116 | } |
| 117 | |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 118 | /* Generate the declarations for the parameters to our inlined code, |
| 119 | * and set up the mapping of real function body variables to ours. |
| 120 | */ |
| 121 | i = 0; |
| 122 | exec_list_iterator sig_param_iter = this->callee->parameters.iterator(); |
| 123 | exec_list_iterator param_iter = this->actual_parameters.iterator(); |
| 124 | for (i = 0; i < num_parameters; i++) { |
| 125 | const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get(); |
| 126 | ir_rvalue *param = (ir_rvalue *) param_iter.get(); |
| 127 | |
| 128 | /* Generate a new variable for the parameter. */ |
Eric Anholt | 4b6fd39 | 2010-06-23 11:37:12 -0700 | [diff] [blame] | 129 | parameters[i] = (ir_variable *)sig_param->clone(ht); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 130 | next_ir->insert_before(parameters[i]); |
| 131 | |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 132 | /* Move the actual param into our param variable if it's an 'in' type. */ |
| 133 | if (parameters[i]->mode == ir_var_in || |
| 134 | parameters[i]->mode == ir_var_inout) { |
| 135 | ir_assignment *assign; |
| 136 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame^] | 137 | assign = new(ctx) ir_assignment(new(ctx) ir_dereference_variable(parameters[i]), |
| 138 | param, NULL); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 139 | next_ir->insert_before(assign); |
| 140 | } |
| 141 | |
| 142 | sig_param_iter.next(); |
| 143 | param_iter.next(); |
| 144 | } |
| 145 | |
| 146 | /* Generate the inlined body of the function. */ |
| 147 | foreach_iter(exec_list_iterator, iter, callee->body) { |
| 148 | ir_instruction *ir = (ir_instruction *)iter.get(); |
| 149 | |
Eric Anholt | 4b6fd39 | 2010-06-23 11:37:12 -0700 | [diff] [blame] | 150 | next_ir->insert_before(ir->clone(ht)); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Kenneth Graunke | c07fdae | 2010-04-30 23:38:50 -0700 | [diff] [blame] | 153 | /* Copy back the value of any 'out' parameters from the function body |
| 154 | * variables to our own. |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 155 | */ |
| 156 | i = 0; |
| 157 | param_iter = this->actual_parameters.iterator(); |
| 158 | for (i = 0; i < num_parameters; i++) { |
| 159 | ir_instruction *const param = (ir_instruction *) param_iter.get(); |
| 160 | |
Kenneth Graunke | c07fdae | 2010-04-30 23:38:50 -0700 | [diff] [blame] | 161 | /* Move our param variable into the actual param if it's an 'out' type. */ |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 162 | if (parameters[i]->mode == ir_var_out || |
| 163 | parameters[i]->mode == ir_var_inout) { |
| 164 | ir_assignment *assign; |
| 165 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame^] | 166 | assign = new(ctx) ir_assignment(param->as_rvalue(), |
| 167 | new(ctx) ir_dereference_variable(parameters[i]), |
| 168 | NULL); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 169 | next_ir->insert_before(assign); |
| 170 | } |
| 171 | |
| 172 | param_iter.next(); |
| 173 | } |
| 174 | |
Ian Romanick | 2f8b043 | 2010-06-09 11:00:00 -0700 | [diff] [blame] | 175 | delete [] parameters; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 176 | |
Eric Anholt | 4b6fd39 | 2010-06-23 11:37:12 -0700 | [diff] [blame] | 177 | hash_table_dtor(ht); |
| 178 | |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 179 | if (retval) |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame^] | 180 | return new(ctx) ir_dereference_variable(retval); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 181 | else |
| 182 | return NULL; |
| 183 | } |
| 184 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 185 | |
| 186 | ir_visitor_status |
| 187 | ir_function_inlining_visitor::visit_enter(ir_expression *ir) |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 188 | { |
| 189 | (void) ir; |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 190 | return visit_continue_with_parent; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 194 | ir_visitor_status |
| 195 | ir_function_inlining_visitor::visit_enter(ir_return *ir) |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 196 | { |
| 197 | (void) ir; |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 198 | return visit_continue_with_parent; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 202 | ir_visitor_status |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 203 | ir_function_inlining_visitor::visit_enter(ir_texture *ir) |
| 204 | { |
| 205 | (void) ir; |
| 206 | return visit_continue_with_parent; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | ir_visitor_status |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 211 | ir_function_inlining_visitor::visit_enter(ir_swizzle *ir) |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 212 | { |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 213 | (void) ir; |
| 214 | return visit_continue_with_parent; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 218 | ir_visitor_status |
| 219 | ir_function_inlining_visitor::visit_enter(ir_call *ir) |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 220 | { |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 221 | if (can_inline(ir)) { |
| 222 | (void) ir->generate_inline(ir); |
| 223 | ir->remove(); |
| 224 | this->progress = true; |
Kenneth Graunke | 9fa99f3 | 2010-04-21 12:30:22 -0700 | [diff] [blame] | 225 | } |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 226 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 227 | return visit_continue; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 231 | ir_visitor_status |
| 232 | ir_function_inlining_visitor::visit_enter(ir_assignment *ir) |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 233 | { |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 234 | ir_call *call = ir->rhs->as_call(); |
| 235 | if (!call || !can_inline(call)) |
| 236 | return visit_continue; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 237 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 238 | /* generates the parameter setup, function body, and returns the return |
| 239 | * value of the function |
| 240 | */ |
| 241 | ir_rvalue *rhs = call->generate_inline(ir); |
| 242 | assert(rhs); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 243 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 244 | ir->rhs = rhs; |
| 245 | this->progress = true; |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 246 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 247 | return visit_continue; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 248 | } |