| 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 |
#include "ir.h" |
| 25 |
#include "ir_visitor.h" |
| 26 |
#include "ir_unused_structs.h" |
| 27 |
#include "glsl_types.h" |
| 28 |
|
| 29 |
|
| 30 |
class ir_struct_usage_visitor : public ir_hierarchical_visitor { |
| 31 |
public: |
| 32 |
ir_struct_usage_visitor(); |
| 33 |
~ir_struct_usage_visitor(void); |
| 34 |
|
| 35 |
virtual ir_visitor_status visit(ir_dereference_variable *); |
| 36 |
|
| 37 |
bool has_struct_entry(const glsl_type *t) const; |
| 38 |
|
| 39 |
exec_list struct_list; |
| 40 |
void *mem_ctx; |
| 41 |
}; |
| 42 |
|
| 43 |
class ir_decl_removal_visitor : public ir_hierarchical_visitor { |
| 44 |
public: |
| 45 |
ir_decl_removal_visitor(ir_struct_usage_visitor* used_structs) |
| 46 |
: used_structs(used_structs) |
| 47 |
{ |
| 48 |
} |
| 49 |
|
| 50 |
virtual ir_visitor_status visit(ir_typedecl_statement* ir) |
| 51 |
{ |
| 52 |
if (ir->type_decl->is_struct() && !used_structs->has_struct_entry(ir->type_decl)) |
| 53 |
{ |
| 54 |
ir->remove(); |
| 55 |
} |
| 56 |
return visit_continue; |
| 57 |
} |
| 58 |
|
| 59 |
ir_struct_usage_visitor* used_structs; |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
struct struct_entry : public exec_node |
| 64 |
{ |
| 65 |
struct_entry(const glsl_type *type_) : type(type_) { } |
| 66 |
const glsl_type *type; |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
bool |
| 71 |
ir_struct_usage_visitor::has_struct_entry(const glsl_type *t) const |
| 72 |
{ |
| 73 |
assert(t); |
| 74 |
foreach_in_list(struct_entry, entry, &this->struct_list) { |
| 75 |
if (entry->type == t) |
| 76 |
return true; |
| 77 |
} |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
ir_visitor_status |
| 83 |
ir_struct_usage_visitor::visit(ir_dereference_variable *ir) |
| 84 |
{ |
| 85 |
const glsl_type* t = ir->type; |
| 86 |
if (t->base_type == GLSL_TYPE_STRUCT) |
| 87 |
{ |
| 88 |
if (!has_struct_entry (t)) |
| 89 |
{ |
| 90 |
struct_entry *entry = new(mem_ctx) struct_entry(t); |
| 91 |
this->struct_list.push_tail (entry); |
| 92 |
} |
| 93 |
} |
| 94 |
return visit_continue; |
| 95 |
} |
| 96 |
|
| 97 |
static void visit_variable (ir_instruction* ir, void* data) |
| 98 |
{ |
| 99 |
ir_variable* var = ir->as_variable(); |
| 100 |
if (!var) |
| 101 |
return; |
| 102 |
ir_struct_usage_visitor* self = reinterpret_cast<ir_struct_usage_visitor*>(data); |
| 103 |
const glsl_type* t = var->type; |
| 104 |
if (t->base_type == GLSL_TYPE_ARRAY) |
| 105 |
t = t->fields.array; // handle array of structs case |
| 106 |
if (t->base_type == GLSL_TYPE_STRUCT) |
| 107 |
{ |
| 108 |
if (!self->has_struct_entry (t)) |
| 109 |
{ |
| 110 |
struct_entry *entry = new(self->mem_ctx) struct_entry(t); |
| 111 |
self->struct_list.push_tail (entry); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
ir_struct_usage_visitor::ir_struct_usage_visitor() |
| 118 |
{ |
| 119 |
this->mem_ctx = ralloc_context(NULL); |
| 120 |
this->struct_list.make_empty(); |
| 121 |
this->callback_enter = visit_variable; |
| 122 |
this->data_enter = this; |
| 123 |
} |
| 124 |
|
| 125 |
ir_struct_usage_visitor::~ir_struct_usage_visitor(void) |
| 126 |
{ |
| 127 |
ralloc_free(mem_ctx); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
void do_remove_unused_typedecls(exec_list* instructions) |
| 133 |
{ |
| 134 |
ir_struct_usage_visitor v; |
| 135 |
v.run (instructions); |
| 136 |
|
| 137 |
ir_decl_removal_visitor v2(&v); |
| 138 |
v2.run (instructions); |
| 139 |
} |