Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -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_structure_splitting.cpp |
| 26 | * |
| 27 | * If a structure is only ever referenced by its components, then |
| 28 | * split those components out to individual variables so they can be |
| 29 | * handled normally by other optimization passes. |
| 30 | * |
| 31 | * This skips structures like uniforms, which need to be accessible as |
| 32 | * structures for their access by the GL. |
| 33 | */ |
| 34 | |
| 35 | #include "ir.h" |
| 36 | #include "ir_visitor.h" |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 37 | #include "ir_print_visitor.h" |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 38 | #include "glsl_types.h" |
| 39 | |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 40 | static bool debug = false; |
| 41 | |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 42 | class variable_entry : public exec_node |
| 43 | { |
| 44 | public: |
| 45 | variable_entry(ir_variable *var) |
| 46 | { |
| 47 | this->var = var; |
| 48 | this->whole_structure_access = 0; |
| 49 | this->declaration = false; |
| 50 | this->components = NULL; |
| 51 | this->mem_ctx = NULL; |
| 52 | } |
| 53 | |
| 54 | ir_variable *var; /* The key: the variable's pointer. */ |
| 55 | |
| 56 | /** Number of times the variable is referenced, including assignments. */ |
| 57 | unsigned whole_structure_access; |
| 58 | |
| 59 | bool declaration; /* If the variable had a decl in the instruction stream */ |
| 60 | |
| 61 | ir_variable **components; |
| 62 | |
| 63 | /** talloc_parent(this->var) -- the shader's talloc context. */ |
| 64 | void *mem_ctx; |
| 65 | }; |
| 66 | |
| 67 | class ir_structure_reference_visitor : public ir_hierarchical_visitor { |
| 68 | public: |
| 69 | ir_structure_reference_visitor(void) |
| 70 | { |
| 71 | this->mem_ctx = talloc_new(NULL); |
| 72 | this->variable_list.make_empty(); |
| 73 | } |
| 74 | |
| 75 | ~ir_structure_reference_visitor(void) |
| 76 | { |
| 77 | talloc_free(mem_ctx); |
| 78 | } |
| 79 | |
| 80 | virtual ir_visitor_status visit(ir_variable *); |
| 81 | virtual ir_visitor_status visit(ir_dereference_variable *); |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 82 | virtual ir_visitor_status visit_enter(ir_dereference_record *); |
Eric Anholt | f8d2cfe | 2010-08-08 23:29:54 -0700 | [diff] [blame^] | 83 | virtual ir_visitor_status visit_enter(ir_assignment *); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 84 | virtual ir_visitor_status visit_enter(ir_function_signature *); |
| 85 | |
| 86 | variable_entry *get_variable_entry(ir_variable *var); |
| 87 | |
| 88 | /* List of variable_entry */ |
| 89 | exec_list variable_list; |
| 90 | |
| 91 | void *mem_ctx; |
| 92 | }; |
| 93 | |
| 94 | variable_entry * |
| 95 | ir_structure_reference_visitor::get_variable_entry(ir_variable *var) |
| 96 | { |
| 97 | assert(var); |
| 98 | |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 99 | if (!var->type->is_record() || var->mode == ir_var_uniform) |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 100 | return NULL; |
| 101 | |
| 102 | foreach_iter(exec_list_iterator, iter, this->variable_list) { |
| 103 | variable_entry *entry = (variable_entry *)iter.get(); |
| 104 | if (entry->var == var) |
| 105 | return entry; |
| 106 | } |
| 107 | |
| 108 | variable_entry *entry = new(mem_ctx) variable_entry(var); |
| 109 | this->variable_list.push_tail(entry); |
| 110 | return entry; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | ir_visitor_status |
| 115 | ir_structure_reference_visitor::visit(ir_variable *ir) |
| 116 | { |
| 117 | variable_entry *entry = this->get_variable_entry(ir); |
| 118 | |
| 119 | if (entry) |
| 120 | entry->declaration = true; |
| 121 | |
| 122 | return visit_continue; |
| 123 | } |
| 124 | |
| 125 | ir_visitor_status |
| 126 | ir_structure_reference_visitor::visit(ir_dereference_variable *ir) |
| 127 | { |
| 128 | ir_variable *const var = ir->variable_referenced(); |
| 129 | variable_entry *entry = this->get_variable_entry(var); |
| 130 | |
| 131 | if (entry) |
| 132 | entry->whole_structure_access++; |
| 133 | |
| 134 | return visit_continue; |
| 135 | } |
| 136 | |
| 137 | ir_visitor_status |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 138 | ir_structure_reference_visitor::visit_enter(ir_dereference_record *ir) |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 139 | { |
| 140 | /* Don't descend into the ir_dereference_variable below. */ |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 141 | return visit_continue_with_parent; |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | ir_visitor_status |
Eric Anholt | f8d2cfe | 2010-08-08 23:29:54 -0700 | [diff] [blame^] | 145 | ir_structure_reference_visitor::visit_enter(ir_assignment *ir) |
| 146 | { |
| 147 | if (ir->lhs->as_dereference_variable() && |
| 148 | ir->rhs->as_dereference_variable() && |
| 149 | !ir->condition) { |
| 150 | /* We'll split copies of a structure to copies of components, so don't |
| 151 | * descend to the ir_dereference_variables. |
| 152 | */ |
| 153 | return visit_continue_with_parent; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | ir_visitor_status |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 158 | ir_structure_reference_visitor::visit_enter(ir_function_signature *ir) |
| 159 | { |
| 160 | /* We don't want to descend into the function parameters and |
| 161 | * dead-code eliminate them, so just accept the body here. |
| 162 | */ |
| 163 | visit_list_elements(this, &ir->body); |
| 164 | return visit_continue_with_parent; |
| 165 | } |
| 166 | |
| 167 | class ir_structure_splitting_visitor : public ir_hierarchical_visitor { |
| 168 | public: |
| 169 | ir_structure_splitting_visitor(exec_list *vars) |
| 170 | { |
| 171 | this->variable_list = vars; |
| 172 | } |
| 173 | |
| 174 | virtual ~ir_structure_splitting_visitor() |
| 175 | { |
| 176 | } |
| 177 | |
| 178 | virtual ir_visitor_status visit_leave(ir_assignment *); |
| 179 | virtual ir_visitor_status visit_leave(ir_call *); |
| 180 | virtual ir_visitor_status visit_leave(ir_dereference_array *); |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 181 | virtual ir_visitor_status visit_leave(ir_dereference_record *); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 182 | virtual ir_visitor_status visit_leave(ir_expression *); |
| 183 | virtual ir_visitor_status visit_leave(ir_if *); |
| 184 | virtual ir_visitor_status visit_leave(ir_return *); |
| 185 | virtual ir_visitor_status visit_leave(ir_swizzle *); |
| 186 | virtual ir_visitor_status visit_leave(ir_texture *); |
| 187 | |
| 188 | void split_deref(ir_dereference **deref); |
| 189 | void split_rvalue(ir_rvalue **rvalue); |
| 190 | struct variable_entry *get_splitting_entry(ir_variable *var); |
| 191 | |
| 192 | exec_list *variable_list; |
| 193 | void *mem_ctx; |
| 194 | }; |
| 195 | |
| 196 | struct variable_entry * |
| 197 | ir_structure_splitting_visitor::get_splitting_entry(ir_variable *var) |
| 198 | { |
| 199 | assert(var); |
| 200 | |
| 201 | if (!var->type->is_record()) |
| 202 | return NULL; |
| 203 | |
| 204 | foreach_iter(exec_list_iterator, iter, *this->variable_list) { |
| 205 | variable_entry *entry = (variable_entry *)iter.get(); |
| 206 | if (entry->var == var) { |
| 207 | return entry; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | void |
| 215 | ir_structure_splitting_visitor::split_deref(ir_dereference **deref) |
| 216 | { |
| 217 | if ((*deref)->ir_type != ir_type_dereference_record) |
| 218 | return; |
| 219 | |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 220 | ir_dereference_record *deref_record = (ir_dereference_record *)*deref; |
| 221 | ir_dereference_variable *deref_var = deref_record->record->as_dereference_variable(); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 222 | if (!deref_var) |
| 223 | return; |
| 224 | |
| 225 | variable_entry *entry = get_splitting_entry(deref_var->var); |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 226 | if (!entry) |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 227 | return; |
| 228 | |
| 229 | unsigned int i; |
| 230 | for (i = 0; i < entry->var->type->length; i++) { |
| 231 | if (strcmp(deref_record->field, |
| 232 | entry->var->type->fields.structure[i].name) == 0) |
| 233 | break; |
| 234 | } |
| 235 | assert(i != entry->var->type->length); |
| 236 | |
| 237 | *deref = new(entry->mem_ctx) ir_dereference_variable(entry->components[i]); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | ir_structure_splitting_visitor::split_rvalue(ir_rvalue **rvalue) |
| 242 | { |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 243 | if (!*rvalue) |
| 244 | return; |
| 245 | |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 246 | ir_dereference *deref = (*rvalue)->as_dereference(); |
| 247 | |
| 248 | if (!deref) |
| 249 | return; |
| 250 | |
| 251 | split_deref(&deref); |
| 252 | *rvalue = deref; |
| 253 | } |
| 254 | |
| 255 | ir_visitor_status |
| 256 | ir_structure_splitting_visitor::visit_leave(ir_expression *ir) |
| 257 | { |
| 258 | unsigned int operand; |
| 259 | |
| 260 | for (operand = 0; operand < ir->get_num_operands(); operand++) { |
| 261 | split_rvalue(&ir->operands[operand]); |
| 262 | } |
| 263 | |
| 264 | return visit_continue; |
| 265 | } |
| 266 | |
| 267 | ir_visitor_status |
| 268 | ir_structure_splitting_visitor::visit_leave(ir_texture *ir) |
| 269 | { |
| 270 | split_rvalue(&ir->coordinate); |
| 271 | split_rvalue(&ir->projector); |
| 272 | split_rvalue(&ir->shadow_comparitor); |
| 273 | |
| 274 | switch (ir->op) { |
| 275 | case ir_tex: |
| 276 | break; |
| 277 | case ir_txb: |
| 278 | split_rvalue(&ir->lod_info.bias); |
| 279 | break; |
| 280 | case ir_txf: |
| 281 | case ir_txl: |
| 282 | split_rvalue(&ir->lod_info.lod); |
| 283 | break; |
| 284 | case ir_txd: |
| 285 | split_rvalue(&ir->lod_info.grad.dPdx); |
| 286 | split_rvalue(&ir->lod_info.grad.dPdy); |
| 287 | break; |
| 288 | } |
| 289 | |
| 290 | return visit_continue; |
| 291 | } |
| 292 | |
| 293 | ir_visitor_status |
| 294 | ir_structure_splitting_visitor::visit_leave(ir_swizzle *ir) |
| 295 | { |
| 296 | split_rvalue(&ir->val); |
| 297 | return visit_continue; |
| 298 | } |
| 299 | |
| 300 | ir_visitor_status |
| 301 | ir_structure_splitting_visitor::visit_leave(ir_dereference_array *ir) |
| 302 | { |
| 303 | split_rvalue(&ir->array_index); |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 304 | split_rvalue(&ir->array); |
| 305 | return visit_continue; |
| 306 | } |
| 307 | |
| 308 | ir_visitor_status |
| 309 | ir_structure_splitting_visitor::visit_leave(ir_dereference_record *ir) |
| 310 | { |
| 311 | split_rvalue(&ir->record); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 312 | return visit_continue; |
| 313 | } |
| 314 | |
| 315 | ir_visitor_status |
| 316 | ir_structure_splitting_visitor::visit_leave(ir_assignment *ir) |
| 317 | { |
Eric Anholt | f8d2cfe | 2010-08-08 23:29:54 -0700 | [diff] [blame^] | 318 | ir_dereference_variable *lhs_deref = ir->lhs->as_dereference_variable(); |
| 319 | ir_dereference_variable *rhs_deref = ir->rhs->as_dereference_variable(); |
| 320 | variable_entry *lhs_entry = lhs_deref ? get_splitting_entry(lhs_deref->var) : NULL; |
| 321 | variable_entry *rhs_entry = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL; |
| 322 | const glsl_type *type = ir->rhs->type; |
| 323 | |
| 324 | if ((lhs_entry || rhs_entry) && !ir->condition) { |
| 325 | for (unsigned int i = 0; i < type->length; i++) { |
| 326 | ir_dereference *new_lhs, *new_rhs; |
| 327 | void *mem_ctx = lhs_entry ? lhs_entry->mem_ctx : rhs_entry->mem_ctx; |
| 328 | |
| 329 | if (lhs_entry) { |
| 330 | new_lhs = new(mem_ctx) ir_dereference_variable(lhs_entry->components[i]); |
| 331 | } else { |
| 332 | new_lhs = new(mem_ctx) |
| 333 | ir_dereference_record(ir->lhs->clone(mem_ctx, NULL), |
| 334 | type->fields.structure[i].name); |
| 335 | } |
| 336 | |
| 337 | if (rhs_entry) { |
| 338 | new_rhs = new(mem_ctx) ir_dereference_variable(rhs_entry->components[i]); |
| 339 | } else { |
| 340 | new_rhs = new(mem_ctx) |
| 341 | ir_dereference_record(ir->rhs->clone(mem_ctx, NULL), |
| 342 | type->fields.structure[i].name); |
| 343 | } |
| 344 | |
| 345 | ir->insert_before(new(mem_ctx) ir_assignment(new_lhs, |
| 346 | new_rhs, |
| 347 | NULL)); |
| 348 | } |
| 349 | ir->remove(); |
| 350 | } else { |
| 351 | split_rvalue(&ir->rhs); |
| 352 | split_deref(&ir->lhs); |
| 353 | } |
| 354 | |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 355 | split_rvalue(&ir->condition); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 356 | |
| 357 | return visit_continue; |
| 358 | } |
| 359 | |
| 360 | ir_visitor_status |
| 361 | ir_structure_splitting_visitor::visit_leave(ir_call *ir) |
| 362 | { |
| 363 | foreach_iter(exec_list_iterator, iter, *ir) { |
| 364 | ir_rvalue *param = (ir_rvalue *)iter.get(); |
| 365 | ir_rvalue *new_param = param; |
| 366 | split_rvalue(&new_param); |
| 367 | |
| 368 | if (new_param != param) { |
| 369 | param->replace_with(new_param); |
| 370 | } |
| 371 | } |
| 372 | return visit_continue; |
| 373 | } |
| 374 | |
| 375 | ir_visitor_status |
| 376 | ir_structure_splitting_visitor::visit_leave(ir_return *ir) |
| 377 | { |
| 378 | split_rvalue(&ir->value);; |
| 379 | return visit_continue; |
| 380 | } |
| 381 | |
| 382 | ir_visitor_status |
| 383 | ir_structure_splitting_visitor::visit_leave(ir_if *ir) |
| 384 | { |
| 385 | split_rvalue(&ir->condition); |
| 386 | return visit_continue; |
| 387 | } |
| 388 | |
| 389 | |
| 390 | bool |
| 391 | do_structure_splitting(exec_list *instructions) |
| 392 | { |
| 393 | ir_structure_reference_visitor refs; |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 394 | |
| 395 | visit_list_elements(&refs, instructions); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 396 | |
| 397 | /* Trim out variables we can't split. */ |
| 398 | foreach_iter(exec_list_iterator, iter, refs.variable_list) { |
| 399 | variable_entry *entry = (variable_entry *)iter.get(); |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 400 | |
| 401 | if (debug) { |
| 402 | printf("structure %s@%p: decl %d, whole_access %d\n", |
| 403 | entry->var->name, entry->var, entry->declaration, |
| 404 | entry->whole_structure_access); |
| 405 | } |
| 406 | |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 407 | if (!entry->declaration || entry->whole_structure_access) { |
| 408 | entry->remove(); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | if (refs.variable_list.is_empty()) |
| 413 | return false; |
| 414 | |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 415 | void *mem_ctx = talloc_new(NULL); |
| 416 | |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 417 | /* Replace the decls of the structures to be split with their split |
| 418 | * components. |
| 419 | */ |
| 420 | foreach_iter(exec_list_iterator, iter, refs.variable_list) { |
| 421 | variable_entry *entry = (variable_entry *)iter.get(); |
| 422 | const struct glsl_type *type = entry->var->type; |
| 423 | |
| 424 | entry->mem_ctx = talloc_parent(entry->var); |
| 425 | |
| 426 | entry->components = talloc_array(mem_ctx, |
| 427 | ir_variable *, |
| 428 | type->length); |
| 429 | |
| 430 | for (unsigned int i = 0; i < entry->var->type->length; i++) { |
| 431 | const char *name = talloc_asprintf(mem_ctx, "%s_%s", |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 432 | entry->var->name, |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 433 | type->fields.structure[i].name); |
| 434 | |
| 435 | entry->components[i] = |
| 436 | new(entry->mem_ctx) ir_variable(type->fields.structure[i].type, |
| 437 | name, |
| 438 | ir_var_temporary); |
| 439 | entry->var->insert_before(entry->components[i]); |
| 440 | } |
| 441 | |
| 442 | entry->var->remove(); |
| 443 | } |
| 444 | |
| 445 | ir_structure_splitting_visitor split(&refs.variable_list); |
| 446 | visit_list_elements(&split, instructions); |
| 447 | |
| 448 | talloc_free(mem_ctx); |
| 449 | |
| 450 | return true; |
| 451 | } |