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 | |
Ian Romanick | a77a6bc | 2010-08-13 16:22:21 -0700 | [diff] [blame] | 30 | #include <stdint.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" |
Aras Pranckevicius | 3174715 | 2010-07-29 12:40:49 +0300 | [diff] [blame] | 36 | #include "program/hash_table.h" |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 37 | |
Eric Anholt | 199c441 | 2010-08-06 00:21:12 -0700 | [diff] [blame] | 38 | static void |
| 39 | do_sampler_replacement(exec_list *instructions, |
| 40 | ir_variable *sampler, |
| 41 | ir_dereference *deref); |
| 42 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 43 | class ir_function_inlining_visitor : public ir_hierarchical_visitor { |
Eric Anholt | bdd9b1f | 2010-05-05 11:45:30 -0700 | [diff] [blame] | 44 | public: |
| 45 | ir_function_inlining_visitor() |
| 46 | { |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 47 | progress = false; |
Eric Anholt | bdd9b1f | 2010-05-05 11:45:30 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | virtual ~ir_function_inlining_visitor() |
| 51 | { |
| 52 | /* empty */ |
| 53 | } |
| 54 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 55 | virtual ir_visitor_status visit_enter(ir_expression *); |
| 56 | virtual ir_visitor_status visit_enter(ir_call *); |
| 57 | virtual ir_visitor_status visit_enter(ir_assignment *); |
| 58 | virtual ir_visitor_status visit_enter(ir_return *); |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 59 | virtual ir_visitor_status visit_enter(ir_texture *); |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 60 | virtual ir_visitor_status visit_enter(ir_swizzle *); |
| 61 | |
| 62 | bool progress; |
Eric Anholt | bdd9b1f | 2010-05-05 11:45:30 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 65 | |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 66 | bool |
Eric Anholt | 0d42321 | 2010-04-16 12:53:46 -0700 | [diff] [blame] | 67 | automatic_inlining_predicate(ir_instruction *ir) |
| 68 | { |
| 69 | ir_call *call = ir->as_call(); |
| 70 | |
| 71 | if (call && can_inline(call)) |
| 72 | return true; |
| 73 | |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | bool |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 78 | do_function_inlining(exec_list *instructions) |
| 79 | { |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 80 | ir_function_inlining_visitor v; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 81 | |
Eric Anholt | 0d42321 | 2010-04-16 12:53:46 -0700 | [diff] [blame] | 82 | do_expression_flattening(instructions, automatic_inlining_predicate); |
| 83 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 84 | v.run(instructions); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 85 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 86 | return v.progress; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Eric Anholt | f66ba4f | 2010-06-24 08:59:57 -0700 | [diff] [blame] | 89 | static void |
| 90 | replace_return_with_assignment(ir_instruction *ir, void *data) |
| 91 | { |
Eric Anholt | e33c103 | 2010-06-24 15:13:03 -0700 | [diff] [blame] | 92 | void *ctx = talloc_parent(ir); |
Eric Anholt | f66ba4f | 2010-06-24 08:59:57 -0700 | [diff] [blame] | 93 | ir_variable *retval = (ir_variable *)data; |
| 94 | ir_return *ret = ir->as_return(); |
| 95 | |
| 96 | if (ret) { |
| 97 | if (ret->value) { |
Eric Anholt | e33c103 | 2010-06-24 15:13:03 -0700 | [diff] [blame] | 98 | ir_rvalue *lhs = new(ctx) ir_dereference_variable(retval); |
Kenneth Graunke | c7a18da | 2010-07-19 21:44:03 -0700 | [diff] [blame] | 99 | ret->replace_with(new(ctx) ir_assignment(lhs, ret->value, NULL)); |
Eric Anholt | f66ba4f | 2010-06-24 08:59:57 -0700 | [diff] [blame] | 100 | } else { |
| 101 | /* un-valued return has to be the last return, or we shouldn't |
| 102 | * have reached here. (see can_inline()). |
| 103 | */ |
Eric Anholt | 199c441 | 2010-08-06 00:21:12 -0700 | [diff] [blame] | 104 | assert(ret->next->is_tail_sentinel()); |
Eric Anholt | 4285247 | 2010-07-29 13:42:39 -0700 | [diff] [blame] | 105 | ret->remove(); |
Eric Anholt | f66ba4f | 2010-06-24 08:59:57 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 110 | ir_rvalue * |
| 111 | ir_call::generate_inline(ir_instruction *next_ir) |
| 112 | { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 113 | void *ctx = talloc_parent(this); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 114 | ir_variable **parameters; |
| 115 | int num_parameters; |
| 116 | int i; |
| 117 | ir_variable *retval = NULL; |
Eric Anholt | 4b6fd39 | 2010-06-23 11:37:12 -0700 | [diff] [blame] | 118 | struct hash_table *ht; |
| 119 | |
Ian Romanick | d1a1ee5 | 2010-07-06 14:49:14 -0700 | [diff] [blame] | 120 | ht = hash_table_ctor(0, hash_table_pointer_hash, hash_table_pointer_compare); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 121 | |
| 122 | num_parameters = 0; |
| 123 | foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters) |
| 124 | num_parameters++; |
| 125 | |
| 126 | parameters = new ir_variable *[num_parameters]; |
| 127 | |
| 128 | /* Generate storage for the return value. */ |
| 129 | if (this->callee->return_type) { |
Aras Pranckevicius | 3adce98 | 2010-08-09 11:17:32 +0300 | [diff] [blame] | 130 | retval = new(ctx) ir_variable(this->callee->return_type, "_ret_val", |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 131 | ir_var_auto); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 132 | next_ir->insert_before(retval); |
| 133 | } |
| 134 | |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 135 | /* Generate the declarations for the parameters to our inlined code, |
| 136 | * and set up the mapping of real function body variables to ours. |
| 137 | */ |
| 138 | i = 0; |
| 139 | exec_list_iterator sig_param_iter = this->callee->parameters.iterator(); |
| 140 | exec_list_iterator param_iter = this->actual_parameters.iterator(); |
| 141 | for (i = 0; i < num_parameters; i++) { |
Eric Anholt | 8ec0b81 | 2010-07-22 13:52:41 -0700 | [diff] [blame] | 142 | ir_variable *sig_param = (ir_variable *) sig_param_iter.get(); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 143 | ir_rvalue *param = (ir_rvalue *) param_iter.get(); |
| 144 | |
| 145 | /* Generate a new variable for the parameter. */ |
Eric Anholt | 8ec0b81 | 2010-07-22 13:52:41 -0700 | [diff] [blame] | 146 | if (sig_param->type->base_type == GLSL_TYPE_SAMPLER) { |
| 147 | /* For samplers, we want the inlined sampler references |
| 148 | * referencing the passed in sampler variable, since that |
| 149 | * will have the location information, which an assignment of |
Eric Anholt | 199c441 | 2010-08-06 00:21:12 -0700 | [diff] [blame] | 150 | * a sampler wouldn't. Fix it up below. |
Eric Anholt | 8ec0b81 | 2010-07-22 13:52:41 -0700 | [diff] [blame] | 151 | */ |
| 152 | parameters[i] = NULL; |
Eric Anholt | 8ec0b81 | 2010-07-22 13:52:41 -0700 | [diff] [blame] | 153 | } else { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 154 | parameters[i] = sig_param->clone(ctx, ht); |
Eric Anholt | 8ec0b81 | 2010-07-22 13:52:41 -0700 | [diff] [blame] | 155 | parameters[i]->mode = ir_var_auto; |
| 156 | next_ir->insert_before(parameters[i]); |
| 157 | } |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 158 | |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 159 | /* Move the actual param into our param variable if it's an 'in' type. */ |
Eric Anholt | 8ec0b81 | 2010-07-22 13:52:41 -0700 | [diff] [blame] | 160 | if (parameters[i] && (sig_param->mode == ir_var_in || |
| 161 | sig_param->mode == ir_var_inout)) { |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 162 | ir_assignment *assign; |
| 163 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 164 | assign = new(ctx) ir_assignment(new(ctx) ir_dereference_variable(parameters[i]), |
| 165 | param, NULL); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 166 | next_ir->insert_before(assign); |
| 167 | } |
| 168 | |
| 169 | sig_param_iter.next(); |
| 170 | param_iter.next(); |
| 171 | } |
| 172 | |
Eric Anholt | 199c441 | 2010-08-06 00:21:12 -0700 | [diff] [blame] | 173 | exec_list new_instructions; |
| 174 | |
| 175 | /* Generate the inlined body of the function to a new list */ |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 176 | foreach_iter(exec_list_iterator, iter, callee->body) { |
| 177 | ir_instruction *ir = (ir_instruction *)iter.get(); |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 178 | ir_instruction *new_ir = ir->clone(ctx, ht); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 179 | |
Eric Anholt | 199c441 | 2010-08-06 00:21:12 -0700 | [diff] [blame] | 180 | new_instructions.push_tail(new_ir); |
Eric Anholt | f66ba4f | 2010-06-24 08:59:57 -0700 | [diff] [blame] | 181 | visit_tree(new_ir, replace_return_with_assignment, retval); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Eric Anholt | 199c441 | 2010-08-06 00:21:12 -0700 | [diff] [blame] | 184 | /* If any samplers were passed in, replace any deref of the sampler |
| 185 | * with a deref of the sampler argument. |
| 186 | */ |
| 187 | param_iter = this->actual_parameters.iterator(); |
| 188 | sig_param_iter = this->callee->parameters.iterator(); |
| 189 | for (i = 0; i < num_parameters; i++) { |
| 190 | ir_instruction *const param = (ir_instruction *) param_iter.get(); |
| 191 | ir_variable *sig_param = (ir_variable *) sig_param_iter.get(); |
| 192 | |
| 193 | if (sig_param->type->base_type == GLSL_TYPE_SAMPLER) { |
| 194 | ir_dereference *deref = param->as_dereference(); |
| 195 | |
| 196 | assert(deref); |
| 197 | do_sampler_replacement(&new_instructions, sig_param, deref); |
| 198 | } |
Aras Pranckevicius | 58f1ffd | 2010-08-06 12:31:56 +0200 | [diff] [blame] | 199 | param_iter.next(); |
| 200 | sig_param_iter.next(); |
Eric Anholt | 199c441 | 2010-08-06 00:21:12 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* Now push those new instructions in. */ |
| 204 | foreach_iter(exec_list_iterator, iter, new_instructions) { |
| 205 | ir_instruction *ir = (ir_instruction *)iter.get(); |
| 206 | next_ir->insert_before(ir); |
| 207 | } |
| 208 | |
Kenneth Graunke | c07fdae | 2010-04-30 23:38:50 -0700 | [diff] [blame] | 209 | /* Copy back the value of any 'out' parameters from the function body |
| 210 | * variables to our own. |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 211 | */ |
| 212 | i = 0; |
| 213 | param_iter = this->actual_parameters.iterator(); |
Eric Anholt | 325a497 | 2010-07-20 16:03:46 -0700 | [diff] [blame] | 214 | sig_param_iter = this->callee->parameters.iterator(); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 215 | for (i = 0; i < num_parameters; i++) { |
| 216 | ir_instruction *const param = (ir_instruction *) param_iter.get(); |
Eric Anholt | 325a497 | 2010-07-20 16:03:46 -0700 | [diff] [blame] | 217 | const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get(); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 218 | |
Kenneth Graunke | c07fdae | 2010-04-30 23:38:50 -0700 | [diff] [blame] | 219 | /* Move our param variable into the actual param if it's an 'out' type. */ |
Eric Anholt | 8ec0b81 | 2010-07-22 13:52:41 -0700 | [diff] [blame] | 220 | if (parameters[i] && (sig_param->mode == ir_var_out || |
| 221 | sig_param->mode == ir_var_inout)) { |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 222 | ir_assignment *assign; |
| 223 | |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 224 | assign = new(ctx) ir_assignment(param->clone(ctx, NULL)->as_rvalue(), |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 225 | new(ctx) ir_dereference_variable(parameters[i]), |
| 226 | NULL); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 227 | next_ir->insert_before(assign); |
| 228 | } |
| 229 | |
| 230 | param_iter.next(); |
Eric Anholt | 325a497 | 2010-07-20 16:03:46 -0700 | [diff] [blame] | 231 | sig_param_iter.next(); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Ian Romanick | 2f8b043 | 2010-06-09 11:00:00 -0700 | [diff] [blame] | 234 | delete [] parameters; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 235 | |
Eric Anholt | 4b6fd39 | 2010-06-23 11:37:12 -0700 | [diff] [blame] | 236 | hash_table_dtor(ht); |
| 237 | |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 238 | if (retval) |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 239 | return new(ctx) ir_dereference_variable(retval); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 240 | else |
| 241 | return NULL; |
| 242 | } |
| 243 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 244 | |
| 245 | ir_visitor_status |
| 246 | ir_function_inlining_visitor::visit_enter(ir_expression *ir) |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 247 | { |
| 248 | (void) ir; |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 249 | return visit_continue_with_parent; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 253 | ir_visitor_status |
| 254 | ir_function_inlining_visitor::visit_enter(ir_return *ir) |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 255 | { |
| 256 | (void) ir; |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 257 | return visit_continue_with_parent; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 261 | ir_visitor_status |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 262 | ir_function_inlining_visitor::visit_enter(ir_texture *ir) |
| 263 | { |
| 264 | (void) ir; |
| 265 | return visit_continue_with_parent; |
| 266 | } |
| 267 | |
| 268 | |
| 269 | ir_visitor_status |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 270 | ir_function_inlining_visitor::visit_enter(ir_swizzle *ir) |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 271 | { |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 272 | (void) ir; |
| 273 | return visit_continue_with_parent; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 277 | ir_visitor_status |
| 278 | ir_function_inlining_visitor::visit_enter(ir_call *ir) |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 279 | { |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 280 | if (can_inline(ir)) { |
Eric Anholt | d2afc87 | 2010-07-12 10:13:20 -0700 | [diff] [blame] | 281 | /* If the call was part of some tree, then it should have been |
| 282 | * flattened out or we shouldn't have seen it because of a |
| 283 | * visit_continue_with_parent in this visitor. |
| 284 | */ |
| 285 | assert(ir == base_ir); |
| 286 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 287 | (void) ir->generate_inline(ir); |
| 288 | ir->remove(); |
| 289 | this->progress = true; |
Kenneth Graunke | 9fa99f3 | 2010-04-21 12:30:22 -0700 | [diff] [blame] | 290 | } |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 291 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 292 | return visit_continue; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 296 | ir_visitor_status |
| 297 | ir_function_inlining_visitor::visit_enter(ir_assignment *ir) |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 298 | { |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 299 | ir_call *call = ir->rhs->as_call(); |
| 300 | if (!call || !can_inline(call)) |
| 301 | return visit_continue; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 302 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 303 | /* generates the parameter setup, function body, and returns the return |
| 304 | * value of the function |
| 305 | */ |
| 306 | ir_rvalue *rhs = call->generate_inline(ir); |
| 307 | assert(rhs); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 308 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 309 | ir->rhs = rhs; |
| 310 | this->progress = true; |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 311 | |
Ian Romanick | e668c2a | 2010-05-26 18:58:27 -0700 | [diff] [blame] | 312 | return visit_continue; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 313 | } |
Eric Anholt | 199c441 | 2010-08-06 00:21:12 -0700 | [diff] [blame] | 314 | |
| 315 | /** |
| 316 | * Replaces references to the "sampler" variable with a clone of "deref." |
| 317 | * |
| 318 | * From the spec, samplers can appear in the tree as function |
| 319 | * (non-out) parameters and as the result of array indexing and |
| 320 | * structure field selection. In our builtin implementation, they |
| 321 | * also appear in the sampler field of an ir_tex instruction. |
| 322 | */ |
| 323 | |
| 324 | class ir_sampler_replacement_visitor : public ir_hierarchical_visitor { |
| 325 | public: |
| 326 | ir_sampler_replacement_visitor(ir_variable *sampler, ir_dereference *deref) |
| 327 | { |
| 328 | this->sampler = sampler; |
| 329 | this->deref = deref; |
| 330 | } |
| 331 | |
| 332 | virtual ~ir_sampler_replacement_visitor() |
| 333 | { |
| 334 | } |
| 335 | |
| 336 | virtual ir_visitor_status visit_leave(ir_call *); |
| 337 | virtual ir_visitor_status visit_leave(ir_dereference_array *); |
| 338 | virtual ir_visitor_status visit_leave(ir_dereference_record *); |
| 339 | virtual ir_visitor_status visit_leave(ir_texture *); |
| 340 | |
| 341 | void replace_deref(ir_dereference **deref); |
| 342 | void replace_rvalue(ir_rvalue **rvalue); |
| 343 | |
| 344 | ir_variable *sampler; |
| 345 | ir_dereference *deref; |
| 346 | }; |
| 347 | |
| 348 | void |
| 349 | ir_sampler_replacement_visitor::replace_deref(ir_dereference **deref) |
| 350 | { |
| 351 | ir_dereference_variable *deref_var = (*deref)->as_dereference_variable(); |
| 352 | if (deref_var && deref_var->var == this->sampler) { |
| 353 | *deref = this->deref->clone(talloc_parent(*deref), NULL); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | void |
| 358 | ir_sampler_replacement_visitor::replace_rvalue(ir_rvalue **rvalue) |
| 359 | { |
| 360 | if (!*rvalue) |
| 361 | return; |
| 362 | |
| 363 | ir_dereference *deref = (*rvalue)->as_dereference(); |
| 364 | |
| 365 | if (!deref) |
| 366 | return; |
| 367 | |
| 368 | replace_deref(&deref); |
| 369 | *rvalue = deref; |
| 370 | } |
| 371 | |
| 372 | ir_visitor_status |
| 373 | ir_sampler_replacement_visitor::visit_leave(ir_texture *ir) |
| 374 | { |
| 375 | replace_deref(&ir->sampler); |
| 376 | |
| 377 | return visit_continue; |
| 378 | } |
| 379 | |
| 380 | ir_visitor_status |
| 381 | ir_sampler_replacement_visitor::visit_leave(ir_dereference_array *ir) |
| 382 | { |
| 383 | replace_rvalue(&ir->array); |
| 384 | return visit_continue; |
| 385 | } |
| 386 | |
| 387 | ir_visitor_status |
| 388 | ir_sampler_replacement_visitor::visit_leave(ir_dereference_record *ir) |
| 389 | { |
| 390 | replace_rvalue(&ir->record); |
| 391 | return visit_continue; |
| 392 | } |
| 393 | |
| 394 | ir_visitor_status |
| 395 | ir_sampler_replacement_visitor::visit_leave(ir_call *ir) |
| 396 | { |
| 397 | foreach_iter(exec_list_iterator, iter, *ir) { |
| 398 | ir_rvalue *param = (ir_rvalue *)iter.get(); |
| 399 | ir_rvalue *new_param = param; |
| 400 | replace_rvalue(&new_param); |
| 401 | |
| 402 | if (new_param != param) { |
| 403 | param->replace_with(new_param); |
| 404 | } |
| 405 | } |
| 406 | return visit_continue; |
| 407 | } |
| 408 | |
| 409 | static void |
| 410 | do_sampler_replacement(exec_list *instructions, |
| 411 | ir_variable *sampler, |
| 412 | ir_dereference *deref) |
| 413 | { |
| 414 | ir_sampler_replacement_visitor v(sampler, deref); |
| 415 | |
| 416 | visit_list_elements(&v, instructions); |
| 417 | } |