blob: 2b971b7aaac586f59ca92892b1e997f2829a98ef [file] [log] [blame]
Eric Anholt7d211042010-04-16 16:43:47 -07001/*
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_dead_code.cpp
26 *
27 * Eliminates dead assignments and variable declarations from the code.
28 */
29
Eric Anholt7d211042010-04-16 16:43:47 -070030#include "ir.h"
31#include "ir_visitor.h"
Eric Anholtd72edc42010-07-30 16:05:27 -070032#include "ir_variable_refcount.h"
Eric Anholt7d211042010-04-16 16:43:47 -070033#include "glsl_types.h"
34
Eric Anholt7d211042010-04-16 16:43:47 -070035/**
36 * Do a dead code pass over instructions and everything that instructions
37 * references.
38 *
39 * Note that this will remove assignments to globals, so it is not suitable
40 * for usage on an unlinked instruction stream.
41 */
42bool
Eric Anholt66d4c652010-07-27 11:28:26 -070043do_dead_code(exec_list *instructions)
Eric Anholt7d211042010-04-16 16:43:47 -070044{
Eric Anholtd72edc42010-07-30 16:05:27 -070045 ir_variable_refcount_visitor v;
Eric Anholt7d211042010-04-16 16:43:47 -070046 bool progress = false;
47
Ian Romanickb5a7cf92010-05-14 12:41:22 -070048 v.run(instructions);
Eric Anholt7d211042010-04-16 16:43:47 -070049
50 foreach_iter(exec_list_iterator, iter, v.variable_list) {
51 variable_entry *entry = (variable_entry *)iter.get();
52
Ian Romanickb5a7cf92010-05-14 12:41:22 -070053 /* Since each assignment is a reference, the refereneced count must be
54 * greater than or equal to the assignment count. If they are equal,
55 * then all of the references are assignments, and the variable is
56 * dead.
57 *
58 * Note that if the variable is neither assigned nor referenced, both
59 * counts will be zero and will be caught by the equality test.
60 */
61 assert(entry->referenced_count >= entry->assigned_count);
62
63 if ((entry->referenced_count > entry->assigned_count)
64 || !entry->declaration)
Eric Anholt7d211042010-04-16 16:43:47 -070065 continue;
66
67 if (entry->assign) {
68 /* Remove a single dead assignment to the variable we found.
69 * Don't do so if it's a shader output, though.
70 */
71 if (!entry->var->shader_out) {
72 entry->assign->remove();
73 progress = true;
74 }
75 } else {
76 /* If there are no assignments or references to the variable left,
77 * then we can remove its declaration.
78 */
79 entry->var->remove();
80 progress = true;
81 }
82 }
Eric Anholt66d4c652010-07-27 11:28:26 -070083 talloc_free(v.mem_ctx);
84
Eric Anholt7d211042010-04-16 16:43:47 -070085 return progress;
86}
87
88/**
89 * Does a dead code pass on the functions present in the instruction stream.
90 *
91 * This is suitable for use while the program is not linked, as it will
92 * ignore variable declarations (and the assignments to them) for variables
93 * with global scope.
94 */
95bool
Eric Anholt66d4c652010-07-27 11:28:26 -070096do_dead_code_unlinked(exec_list *instructions)
Eric Anholt7d211042010-04-16 16:43:47 -070097{
98 bool progress = false;
99
100 foreach_iter(exec_list_iterator, iter, *instructions) {
101 ir_instruction *ir = (ir_instruction *)iter.get();
Kenneth Graunke6202cbf2010-04-21 16:02:15 -0700102 ir_function *f = ir->as_function();
103 if (f) {
104 foreach_iter(exec_list_iterator, sigiter, *f) {
105 ir_function_signature *sig =
106 (ir_function_signature *) sigiter.get();
Eric Anholt66d4c652010-07-27 11:28:26 -0700107 if (do_dead_code(&sig->body))
Kenneth Graunke6202cbf2010-04-21 16:02:15 -0700108 progress = true;
109 }
Eric Anholt7d211042010-04-16 16:43:47 -0700110 }
111 }
112
113 return progress;
114}