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 | b3b0cf6 | 2010-08-13 20:39:24 -0700 | [diff] [blame] | 38 | #include "ir_rvalue_visitor.h" |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 39 | #include "glsl_types.h" |
| 40 | |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 41 | static bool debug = false; |
| 42 | |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 43 | class variable_entry : public exec_node |
| 44 | { |
| 45 | public: |
| 46 | variable_entry(ir_variable *var) |
| 47 | { |
| 48 | this->var = var; |
| 49 | this->whole_structure_access = 0; |
| 50 | this->declaration = false; |
| 51 | this->components = NULL; |
| 52 | this->mem_ctx = NULL; |
| 53 | } |
| 54 | |
| 55 | ir_variable *var; /* The key: the variable's pointer. */ |
| 56 | |
| 57 | /** Number of times the variable is referenced, including assignments. */ |
| 58 | unsigned whole_structure_access; |
| 59 | |
| 60 | bool declaration; /* If the variable had a decl in the instruction stream */ |
| 61 | |
| 62 | ir_variable **components; |
| 63 | |
| 64 | /** talloc_parent(this->var) -- the shader's talloc context. */ |
| 65 | void *mem_ctx; |
| 66 | }; |
| 67 | |
| 68 | class ir_structure_reference_visitor : public ir_hierarchical_visitor { |
| 69 | public: |
| 70 | ir_structure_reference_visitor(void) |
| 71 | { |
| 72 | this->mem_ctx = talloc_new(NULL); |
| 73 | this->variable_list.make_empty(); |
| 74 | } |
| 75 | |
| 76 | ~ir_structure_reference_visitor(void) |
| 77 | { |
| 78 | talloc_free(mem_ctx); |
| 79 | } |
| 80 | |
| 81 | virtual ir_visitor_status visit(ir_variable *); |
| 82 | virtual ir_visitor_status visit(ir_dereference_variable *); |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 83 | virtual ir_visitor_status visit_enter(ir_dereference_record *); |
Eric Anholt | f8d2cfe | 2010-08-08 23:29:54 -0700 | [diff] [blame] | 84 | virtual ir_visitor_status visit_enter(ir_assignment *); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 85 | virtual ir_visitor_status visit_enter(ir_function_signature *); |
| 86 | |
| 87 | variable_entry *get_variable_entry(ir_variable *var); |
| 88 | |
| 89 | /* List of variable_entry */ |
| 90 | exec_list variable_list; |
| 91 | |
| 92 | void *mem_ctx; |
| 93 | }; |
| 94 | |
| 95 | variable_entry * |
| 96 | ir_structure_reference_visitor::get_variable_entry(ir_variable *var) |
| 97 | { |
| 98 | assert(var); |
| 99 | |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 100 | if (!var->type->is_record() || var->mode == ir_var_uniform) |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 101 | return NULL; |
| 102 | |
| 103 | foreach_iter(exec_list_iterator, iter, this->variable_list) { |
| 104 | variable_entry *entry = (variable_entry *)iter.get(); |
| 105 | if (entry->var == var) |
| 106 | return entry; |
| 107 | } |
| 108 | |
| 109 | variable_entry *entry = new(mem_ctx) variable_entry(var); |
| 110 | this->variable_list.push_tail(entry); |
| 111 | return entry; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | ir_visitor_status |
| 116 | ir_structure_reference_visitor::visit(ir_variable *ir) |
| 117 | { |
| 118 | variable_entry *entry = this->get_variable_entry(ir); |
| 119 | |
| 120 | if (entry) |
| 121 | entry->declaration = true; |
| 122 | |
| 123 | return visit_continue; |
| 124 | } |
| 125 | |
| 126 | ir_visitor_status |
| 127 | ir_structure_reference_visitor::visit(ir_dereference_variable *ir) |
| 128 | { |
| 129 | ir_variable *const var = ir->variable_referenced(); |
| 130 | variable_entry *entry = this->get_variable_entry(var); |
| 131 | |
| 132 | if (entry) |
| 133 | entry->whole_structure_access++; |
| 134 | |
| 135 | return visit_continue; |
| 136 | } |
| 137 | |
| 138 | ir_visitor_status |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 139 | ir_structure_reference_visitor::visit_enter(ir_dereference_record *ir) |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 140 | { |
Ian Romanick | d0a9cbd | 2010-08-16 13:59:01 -0700 | [diff] [blame] | 141 | (void) ir; |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 142 | /* Don't descend into the ir_dereference_variable below. */ |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 143 | return visit_continue_with_parent; |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | ir_visitor_status |
Eric Anholt | f8d2cfe | 2010-08-08 23:29:54 -0700 | [diff] [blame] | 147 | ir_structure_reference_visitor::visit_enter(ir_assignment *ir) |
| 148 | { |
| 149 | if (ir->lhs->as_dereference_variable() && |
| 150 | ir->rhs->as_dereference_variable() && |
| 151 | !ir->condition) { |
| 152 | /* We'll split copies of a structure to copies of components, so don't |
| 153 | * descend to the ir_dereference_variables. |
| 154 | */ |
| 155 | return visit_continue_with_parent; |
| 156 | } |
Aras Pranckevicius | 5023eda | 2010-08-09 11:50:24 +0300 | [diff] [blame] | 157 | return visit_continue; |
Eric Anholt | f8d2cfe | 2010-08-08 23:29:54 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | ir_visitor_status |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 161 | ir_structure_reference_visitor::visit_enter(ir_function_signature *ir) |
| 162 | { |
| 163 | /* We don't want to descend into the function parameters and |
| 164 | * dead-code eliminate them, so just accept the body here. |
| 165 | */ |
| 166 | visit_list_elements(this, &ir->body); |
| 167 | return visit_continue_with_parent; |
| 168 | } |
| 169 | |
Eric Anholt | b3b0cf6 | 2010-08-13 20:39:24 -0700 | [diff] [blame] | 170 | class ir_structure_splitting_visitor : public ir_rvalue_visitor { |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 171 | public: |
| 172 | ir_structure_splitting_visitor(exec_list *vars) |
| 173 | { |
| 174 | this->variable_list = vars; |
| 175 | } |
| 176 | |
| 177 | virtual ~ir_structure_splitting_visitor() |
| 178 | { |
| 179 | } |
| 180 | |
| 181 | virtual ir_visitor_status visit_leave(ir_assignment *); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 182 | |
| 183 | void split_deref(ir_dereference **deref); |
Eric Anholt | b3b0cf6 | 2010-08-13 20:39:24 -0700 | [diff] [blame] | 184 | void handle_rvalue(ir_rvalue **rvalue); |
José Fonseca | 1cbcf66 | 2010-08-14 15:35:57 +0100 | [diff] [blame] | 185 | variable_entry *get_splitting_entry(ir_variable *var); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 186 | |
| 187 | exec_list *variable_list; |
| 188 | void *mem_ctx; |
| 189 | }; |
| 190 | |
José Fonseca | 1cbcf66 | 2010-08-14 15:35:57 +0100 | [diff] [blame] | 191 | variable_entry * |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 192 | ir_structure_splitting_visitor::get_splitting_entry(ir_variable *var) |
| 193 | { |
| 194 | assert(var); |
| 195 | |
| 196 | if (!var->type->is_record()) |
| 197 | return NULL; |
| 198 | |
| 199 | foreach_iter(exec_list_iterator, iter, *this->variable_list) { |
| 200 | variable_entry *entry = (variable_entry *)iter.get(); |
| 201 | if (entry->var == var) { |
| 202 | return entry; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | void |
| 210 | ir_structure_splitting_visitor::split_deref(ir_dereference **deref) |
| 211 | { |
| 212 | if ((*deref)->ir_type != ir_type_dereference_record) |
| 213 | return; |
| 214 | |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 215 | ir_dereference_record *deref_record = (ir_dereference_record *)*deref; |
| 216 | ir_dereference_variable *deref_var = deref_record->record->as_dereference_variable(); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 217 | if (!deref_var) |
| 218 | return; |
| 219 | |
| 220 | variable_entry *entry = get_splitting_entry(deref_var->var); |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 221 | if (!entry) |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 222 | return; |
| 223 | |
| 224 | unsigned int i; |
| 225 | for (i = 0; i < entry->var->type->length; i++) { |
| 226 | if (strcmp(deref_record->field, |
| 227 | entry->var->type->fields.structure[i].name) == 0) |
| 228 | break; |
| 229 | } |
| 230 | assert(i != entry->var->type->length); |
| 231 | |
| 232 | *deref = new(entry->mem_ctx) ir_dereference_variable(entry->components[i]); |
| 233 | } |
| 234 | |
| 235 | void |
Eric Anholt | b3b0cf6 | 2010-08-13 20:39:24 -0700 | [diff] [blame] | 236 | ir_structure_splitting_visitor::handle_rvalue(ir_rvalue **rvalue) |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 237 | { |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 238 | if (!*rvalue) |
| 239 | return; |
| 240 | |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 241 | ir_dereference *deref = (*rvalue)->as_dereference(); |
| 242 | |
| 243 | if (!deref) |
| 244 | return; |
| 245 | |
| 246 | split_deref(&deref); |
| 247 | *rvalue = deref; |
| 248 | } |
| 249 | |
| 250 | ir_visitor_status |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 251 | ir_structure_splitting_visitor::visit_leave(ir_assignment *ir) |
| 252 | { |
Eric Anholt | f8d2cfe | 2010-08-08 23:29:54 -0700 | [diff] [blame] | 253 | ir_dereference_variable *lhs_deref = ir->lhs->as_dereference_variable(); |
| 254 | ir_dereference_variable *rhs_deref = ir->rhs->as_dereference_variable(); |
| 255 | variable_entry *lhs_entry = lhs_deref ? get_splitting_entry(lhs_deref->var) : NULL; |
| 256 | variable_entry *rhs_entry = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL; |
| 257 | const glsl_type *type = ir->rhs->type; |
| 258 | |
| 259 | if ((lhs_entry || rhs_entry) && !ir->condition) { |
| 260 | for (unsigned int i = 0; i < type->length; i++) { |
| 261 | ir_dereference *new_lhs, *new_rhs; |
| 262 | void *mem_ctx = lhs_entry ? lhs_entry->mem_ctx : rhs_entry->mem_ctx; |
| 263 | |
| 264 | if (lhs_entry) { |
| 265 | new_lhs = new(mem_ctx) ir_dereference_variable(lhs_entry->components[i]); |
| 266 | } else { |
| 267 | new_lhs = new(mem_ctx) |
| 268 | ir_dereference_record(ir->lhs->clone(mem_ctx, NULL), |
| 269 | type->fields.structure[i].name); |
| 270 | } |
| 271 | |
| 272 | if (rhs_entry) { |
| 273 | new_rhs = new(mem_ctx) ir_dereference_variable(rhs_entry->components[i]); |
| 274 | } else { |
| 275 | new_rhs = new(mem_ctx) |
| 276 | ir_dereference_record(ir->rhs->clone(mem_ctx, NULL), |
| 277 | type->fields.structure[i].name); |
| 278 | } |
| 279 | |
| 280 | ir->insert_before(new(mem_ctx) ir_assignment(new_lhs, |
| 281 | new_rhs, |
| 282 | NULL)); |
| 283 | } |
| 284 | ir->remove(); |
| 285 | } else { |
Eric Anholt | b3b0cf6 | 2010-08-13 20:39:24 -0700 | [diff] [blame] | 286 | handle_rvalue(&ir->rhs); |
Eric Anholt | f8d2cfe | 2010-08-08 23:29:54 -0700 | [diff] [blame] | 287 | split_deref(&ir->lhs); |
| 288 | } |
| 289 | |
Eric Anholt | b3b0cf6 | 2010-08-13 20:39:24 -0700 | [diff] [blame] | 290 | handle_rvalue(&ir->condition); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 291 | |
| 292 | return visit_continue; |
| 293 | } |
| 294 | |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 295 | bool |
| 296 | do_structure_splitting(exec_list *instructions) |
| 297 | { |
| 298 | ir_structure_reference_visitor refs; |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 299 | |
| 300 | visit_list_elements(&refs, instructions); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 301 | |
| 302 | /* Trim out variables we can't split. */ |
| 303 | foreach_iter(exec_list_iterator, iter, refs.variable_list) { |
| 304 | variable_entry *entry = (variable_entry *)iter.get(); |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 305 | |
| 306 | if (debug) { |
| 307 | printf("structure %s@%p: decl %d, whole_access %d\n", |
Brian Paul | ffd3f15 | 2010-08-11 14:00:15 -0600 | [diff] [blame] | 308 | entry->var->name, (void *) entry->var, entry->declaration, |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 309 | entry->whole_structure_access); |
| 310 | } |
| 311 | |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 312 | if (!entry->declaration || entry->whole_structure_access) { |
| 313 | entry->remove(); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if (refs.variable_list.is_empty()) |
| 318 | return false; |
| 319 | |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 320 | void *mem_ctx = talloc_new(NULL); |
| 321 | |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 322 | /* Replace the decls of the structures to be split with their split |
| 323 | * components. |
| 324 | */ |
| 325 | foreach_iter(exec_list_iterator, iter, refs.variable_list) { |
| 326 | variable_entry *entry = (variable_entry *)iter.get(); |
| 327 | const struct glsl_type *type = entry->var->type; |
| 328 | |
| 329 | entry->mem_ctx = talloc_parent(entry->var); |
| 330 | |
| 331 | entry->components = talloc_array(mem_ctx, |
| 332 | ir_variable *, |
| 333 | type->length); |
| 334 | |
| 335 | for (unsigned int i = 0; i < entry->var->type->length; i++) { |
| 336 | const char *name = talloc_asprintf(mem_ctx, "%s_%s", |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 337 | entry->var->name, |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 338 | type->fields.structure[i].name); |
| 339 | |
| 340 | entry->components[i] = |
| 341 | new(entry->mem_ctx) ir_variable(type->fields.structure[i].type, |
| 342 | name, |
| 343 | ir_var_temporary); |
| 344 | entry->var->insert_before(entry->components[i]); |
| 345 | } |
| 346 | |
| 347 | entry->var->remove(); |
| 348 | } |
| 349 | |
| 350 | ir_structure_splitting_visitor split(&refs.variable_list); |
| 351 | visit_list_elements(&split, instructions); |
| 352 | |
| 353 | talloc_free(mem_ctx); |
| 354 | |
| 355 | return true; |
| 356 | } |