2
* Copyright © 2010 Intel Corporation
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:
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
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.
25
* \file ir_dead_functions.cpp
27
* Eliminates unused functions from the linked program.
31
#include "ir_visitor.h"
32
#include "ir_expression_flattening.h"
33
#include "glsl_types.h"
35
class signature_entry : public exec_node
38
signature_entry(ir_function_signature *sig)
40
this->signature = sig;
44
ir_function_signature *signature;
48
class ir_dead_functions_visitor : public ir_hierarchical_visitor {
50
ir_dead_functions_visitor()
52
this->mem_ctx = talloc_new(NULL);
55
~ir_dead_functions_visitor()
57
talloc_free(this->mem_ctx);
60
virtual ir_visitor_status visit_enter(ir_function_signature *);
61
virtual ir_visitor_status visit_enter(ir_call *);
63
signature_entry *get_signature_entry(ir_function_signature *var);
65
bool (*predicate)(ir_instruction *ir);
67
/* List of signature_entry */
68
exec_list signature_list;
74
ir_dead_functions_visitor::get_signature_entry(ir_function_signature *sig)
76
foreach_iter(exec_list_iterator, iter, this->signature_list) {
77
signature_entry *entry = (signature_entry *)iter.get();
78
if (entry->signature == sig)
82
signature_entry *entry = new(mem_ctx) signature_entry(sig);
83
this->signature_list.push_tail(entry);
89
ir_dead_functions_visitor::visit_enter(ir_function_signature *ir)
91
signature_entry *entry = this->get_signature_entry(ir);
93
if (strcmp(ir->function_name(), "main") == 0) {
97
return visit_continue;
102
ir_dead_functions_visitor::visit_enter(ir_call *ir)
104
signature_entry *entry = this->get_signature_entry(ir->get_callee());
108
return visit_continue;
112
do_dead_functions(exec_list *instructions)
114
ir_dead_functions_visitor v;
115
bool progress = false;
117
visit_list_elements(&v, instructions);
119
/* Now that we've figured out which function signatures are used, remove
120
* the unused ones, and remove function definitions that have no more
123
foreach_iter(exec_list_iterator, iter, v.signature_list) {
124
signature_entry *entry = (signature_entry *)iter.get();
127
entry->signature->remove();
133
/* We don't just do this above when we nuked a signature because of
136
foreach_iter(exec_list_iterator, iter, *instructions) {
137
ir_instruction *ir = (ir_instruction *)iter.get();
138
ir_function *func = ir->as_function();
140
if (func && func->signatures.is_empty()) {
141
/* At this point (post-linking), the symbol table is no
142
* longer in use, so not removing the function from the
143
* symbol table should be OK.