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 opt_dead_functions.cpp
26 *
27 * Eliminates unused functions from the linked program.
28 */
29
30 #include "ir.h"
31 #include "ir_visitor.h"
32 #include "ir_expression_flattening.h"
33 #include "glsl_types.h"
34
35 namespace {
36
37 class signature_entry : public exec_node
38 {
39 public:
signature_entry(ir_function_signature * sig)40 signature_entry(ir_function_signature *sig)
41 {
42 this->signature = sig;
43 this->used = false;
44 }
45
46 ir_function_signature *signature;
47 bool used;
48 };
49
50 class ir_dead_functions_visitor : public ir_hierarchical_visitor {
51 public:
ir_dead_functions_visitor()52 ir_dead_functions_visitor()
53 {
54 this->mem_ctx = ralloc_context(NULL);
55 }
56
~ir_dead_functions_visitor()57 ~ir_dead_functions_visitor()
58 {
59 ralloc_free(this->mem_ctx);
60 }
61
62 virtual ir_visitor_status visit_enter(ir_function_signature *);
63 virtual ir_visitor_status visit_enter(ir_call *);
64
65 signature_entry *get_signature_entry(ir_function_signature *var);
66
67 /* List of signature_entry */
68 exec_list signature_list;
69 void *mem_ctx;
70 };
71
72 } /* unnamed namespace */
73
74 signature_entry *
get_signature_entry(ir_function_signature * sig)75 ir_dead_functions_visitor::get_signature_entry(ir_function_signature *sig)
76 {
77 foreach_iter(exec_list_iterator, iter, this->signature_list) {
78 signature_entry *entry = (signature_entry *)iter.get();
79 if (entry->signature == sig)
80 return entry;
81 }
82
83 signature_entry *entry = new(mem_ctx) signature_entry(sig);
84 this->signature_list.push_tail(entry);
85 return entry;
86 }
87
88
89 ir_visitor_status
visit_enter(ir_function_signature * ir)90 ir_dead_functions_visitor::visit_enter(ir_function_signature *ir)
91 {
92 signature_entry *entry = this->get_signature_entry(ir);
93
94 if (strcmp(ir->function_name(), "main") == 0) {
95 entry->used = true;
96 }
97
98
99
100 return visit_continue;
101 }
102
103
104 ir_visitor_status
visit_enter(ir_call * ir)105 ir_dead_functions_visitor::visit_enter(ir_call *ir)
106 {
107 signature_entry *entry = this->get_signature_entry(ir->callee);
108
109 entry->used = true;
110
111 return visit_continue;
112 }
113
114 bool
do_dead_functions(exec_list * instructions)115 do_dead_functions(exec_list *instructions)
116 {
117 ir_dead_functions_visitor v;
118 bool progress = false;
119
120 visit_list_elements(&v, instructions);
121
122 /* Now that we've figured out which function signatures are used, remove
123 * the unused ones, and remove function definitions that have no more
124 * signatures.
125 */
126 foreach_iter(exec_list_iterator, iter, v.signature_list) {
127 signature_entry *entry = (signature_entry *)iter.get();
128
129 if (!entry->used) {
130 entry->signature->remove();
131 delete entry->signature;
132 progress = true;
133 }
134 delete(entry);
135 }
136
137 /* We don't just do this above when we nuked a signature because of
138 * const pointers.
139 */
140 foreach_iter(exec_list_iterator, iter, *instructions) {
141 ir_instruction *ir = (ir_instruction *)iter.get();
142 ir_function *func = ir->as_function();
143
144 if (func && func->signatures.is_empty()) {
145 /* At this point (post-linking), the symbol table is no
146 * longer in use, so not removing the function from the
147 * symbol table should be OK.
148 */
149 func->remove();
150 delete func;
151 progress = true;
152 }
153 }
154
155 return progress;
156 }
157