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 | { |
| 141 | /* Don't descend into the ir_dereference_variable below. */ |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 142 | return visit_continue_with_parent; |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | ir_visitor_status |
Eric Anholt | f8d2cfe | 2010-08-08 23:29:54 -0700 | [diff] [blame] | 146 | ir_structure_reference_visitor::visit_enter(ir_assignment *ir) |
| 147 | { |
| 148 | if (ir->lhs->as_dereference_variable() && |
| 149 | ir->rhs->as_dereference_variable() && |
| 150 | !ir->condition) { |
| 151 | /* We'll split copies of a structure to copies of components, so don't |
| 152 | * descend to the ir_dereference_variables. |
| 153 | */ |
| 154 | return visit_continue_with_parent; |
| 155 | } |
Aras Pranckevicius | 5023eda | 2010-08-09 11:50:24 +0300 | [diff] [blame] | 156 | return visit_continue; |
Eric Anholt | f8d2cfe | 2010-08-08 23:29:54 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | ir_visitor_status |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 160 | ir_structure_reference_visitor::visit_enter(ir_function_signature *ir) |
| 161 | { |
| 162 | /* We don't want to descend into the function parameters and |
| 163 | * dead-code eliminate them, so just accept the body here. |
| 164 | */ |
| 165 | visit_list_elements(this, &ir->body); |
| 166 | return visit_continue_with_parent; |
| 167 | } |
| 168 | |
Eric Anholt | b3b0cf6 | 2010-08-13 20:39:24 -0700 | [diff] [blame^] | 169 | class ir_structure_splitting_visitor : public ir_rvalue_visitor { |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 170 | public: |
| 171 | ir_structure_splitting_visitor(exec_list *vars) |
| 172 | { |
| 173 | this->variable_list = vars; |
| 174 | } |
| 175 | |
| 176 | virtual ~ir_structure_splitting_visitor() |
| 177 | { |
| 178 | } |
| 179 | |
| 180 | virtual ir_visitor_status visit_leave(ir_assignment *); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 181 | |
| 182 | void split_deref(ir_dereference **deref); |
Eric Anholt | b3b0cf6 | 2010-08-13 20:39:24 -0700 | [diff] [blame^] | 183 | void handle_rvalue(ir_rvalue **rvalue); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 184 | struct variable_entry *get_splitting_entry(ir_variable *var); |
| 185 | |
| 186 | exec_list *variable_list; |
| 187 | void *mem_ctx; |
| 188 | }; |
| 189 | |
| 190 | struct variable_entry * |
| 191 | ir_structure_splitting_visitor::get_splitting_entry(ir_variable *var) |
| 192 | { |
| 193 | assert(var); |
| 194 | |
| 195 | if (!var->type->is_record()) |
| 196 | return NULL; |
| 197 | |
| 198 | foreach_iter(exec_list_iterator, iter, *this->variable_list) { |
| 199 | variable_entry *entry = (variable_entry *)iter.get(); |
| 200 | if (entry->var == var) { |
| 201 | return entry; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | ir_structure_splitting_visitor::split_deref(ir_dereference **deref) |
| 210 | { |
| 211 | if ((*deref)->ir_type != ir_type_dereference_record) |
| 212 | return; |
| 213 | |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 214 | ir_dereference_record *deref_record = (ir_dereference_record *)*deref; |
| 215 | ir_dereference_variable *deref_var = deref_record->record->as_dereference_variable(); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 216 | if (!deref_var) |
| 217 | return; |
| 218 | |
| 219 | variable_entry *entry = get_splitting_entry(deref_var->var); |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 220 | if (!entry) |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 221 | return; |
| 222 | |
| 223 | unsigned int i; |
| 224 | for (i = 0; i < entry->var->type->length; i++) { |
| 225 | if (strcmp(deref_record->field, |
| 226 | entry->var->type->fields.structure[i].name) == 0) |
| 227 | break; |
| 228 | } |
| 229 | assert(i != entry->var->type->length); |
| 230 | |
| 231 | *deref = new(entry->mem_ctx) ir_dereference_variable(entry->components[i]); |
| 232 | } |
| 233 | |
| 234 | void |
Eric Anholt | b3b0cf6 | 2010-08-13 20:39:24 -0700 | [diff] [blame^] | 235 | ir_structure_splitting_visitor::handle_rvalue(ir_rvalue **rvalue) |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 236 | { |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 237 | if (!*rvalue) |
| 238 | return; |
| 239 | |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 240 | ir_dereference *deref = (*rvalue)->as_dereference(); |
| 241 | |
| 242 | if (!deref) |
| 243 | return; |
| 244 | |
| 245 | split_deref(&deref); |
| 246 | *rvalue = deref; |
| 247 | } |
| 248 | |
| 249 | ir_visitor_status |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 250 | ir_structure_splitting_visitor::visit_leave(ir_assignment *ir) |
| 251 | { |
Eric Anholt | f8d2cfe | 2010-08-08 23:29:54 -0700 | [diff] [blame] | 252 | ir_dereference_variable *lhs_deref = ir->lhs->as_dereference_variable(); |
| 253 | ir_dereference_variable *rhs_deref = ir->rhs->as_dereference_variable(); |
| 254 | variable_entry *lhs_entry = lhs_deref ? get_splitting_entry(lhs_deref->var) : NULL; |
| 255 | variable_entry *rhs_entry = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL; |
| 256 | const glsl_type *type = ir->rhs->type; |
| 257 | |
| 258 | if ((lhs_entry || rhs_entry) && !ir->condition) { |
| 259 | for (unsigned int i = 0; i < type->length; i++) { |
| 260 | ir_dereference *new_lhs, *new_rhs; |
| 261 | void *mem_ctx = lhs_entry ? lhs_entry->mem_ctx : rhs_entry->mem_ctx; |
| 262 | |
| 263 | if (lhs_entry) { |
| 264 | new_lhs = new(mem_ctx) ir_dereference_variable(lhs_entry->components[i]); |
| 265 | } else { |
| 266 | new_lhs = new(mem_ctx) |
| 267 | ir_dereference_record(ir->lhs->clone(mem_ctx, NULL), |
| 268 | type->fields.structure[i].name); |
| 269 | } |
| 270 | |
| 271 | if (rhs_entry) { |
| 272 | new_rhs = new(mem_ctx) ir_dereference_variable(rhs_entry->components[i]); |
| 273 | } else { |
| 274 | new_rhs = new(mem_ctx) |
| 275 | ir_dereference_record(ir->rhs->clone(mem_ctx, NULL), |
| 276 | type->fields.structure[i].name); |
| 277 | } |
| 278 | |
| 279 | ir->insert_before(new(mem_ctx) ir_assignment(new_lhs, |
| 280 | new_rhs, |
| 281 | NULL)); |
| 282 | } |
| 283 | ir->remove(); |
| 284 | } else { |
Eric Anholt | b3b0cf6 | 2010-08-13 20:39:24 -0700 | [diff] [blame^] | 285 | handle_rvalue(&ir->rhs); |
Eric Anholt | f8d2cfe | 2010-08-08 23:29:54 -0700 | [diff] [blame] | 286 | split_deref(&ir->lhs); |
| 287 | } |
| 288 | |
Eric Anholt | b3b0cf6 | 2010-08-13 20:39:24 -0700 | [diff] [blame^] | 289 | handle_rvalue(&ir->condition); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 290 | |
| 291 | return visit_continue; |
| 292 | } |
| 293 | |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 294 | bool |
| 295 | do_structure_splitting(exec_list *instructions) |
| 296 | { |
| 297 | ir_structure_reference_visitor refs; |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 298 | |
| 299 | visit_list_elements(&refs, instructions); |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 300 | |
| 301 | /* Trim out variables we can't split. */ |
| 302 | foreach_iter(exec_list_iterator, iter, refs.variable_list) { |
| 303 | variable_entry *entry = (variable_entry *)iter.get(); |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 304 | |
| 305 | if (debug) { |
| 306 | printf("structure %s@%p: decl %d, whole_access %d\n", |
Brian Paul | ffd3f15 | 2010-08-11 14:00:15 -0600 | [diff] [blame] | 307 | entry->var->name, (void *) entry->var, entry->declaration, |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 308 | entry->whole_structure_access); |
| 309 | } |
| 310 | |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 311 | if (!entry->declaration || entry->whole_structure_access) { |
| 312 | entry->remove(); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | if (refs.variable_list.is_empty()) |
| 317 | return false; |
| 318 | |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 319 | void *mem_ctx = talloc_new(NULL); |
| 320 | |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 321 | /* Replace the decls of the structures to be split with their split |
| 322 | * components. |
| 323 | */ |
| 324 | foreach_iter(exec_list_iterator, iter, refs.variable_list) { |
| 325 | variable_entry *entry = (variable_entry *)iter.get(); |
| 326 | const struct glsl_type *type = entry->var->type; |
| 327 | |
| 328 | entry->mem_ctx = talloc_parent(entry->var); |
| 329 | |
| 330 | entry->components = talloc_array(mem_ctx, |
| 331 | ir_variable *, |
| 332 | type->length); |
| 333 | |
| 334 | for (unsigned int i = 0; i < entry->var->type->length; i++) { |
| 335 | const char *name = talloc_asprintf(mem_ctx, "%s_%s", |
Eric Anholt | 0a0ab12 | 2010-08-05 13:25:39 -0700 | [diff] [blame] | 336 | entry->var->name, |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 337 | type->fields.structure[i].name); |
| 338 | |
| 339 | entry->components[i] = |
| 340 | new(entry->mem_ctx) ir_variable(type->fields.structure[i].type, |
| 341 | name, |
| 342 | ir_var_temporary); |
| 343 | entry->var->insert_before(entry->components[i]); |
| 344 | } |
| 345 | |
| 346 | entry->var->remove(); |
| 347 | } |
| 348 | |
| 349 | ir_structure_splitting_visitor split(&refs.variable_list); |
| 350 | visit_list_elements(&split, instructions); |
| 351 | |
| 352 | talloc_free(mem_ctx); |
| 353 | |
| 354 | return true; |
| 355 | } |