blob: c5ccd52e5db3aaa001aa4379d752f696153fba8a [file] [log] [blame]
Eric Anholt65122e92010-05-12 12:10:41 -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_constant_variable.cpp
26 *
27 * Marks variables assigned a single constant value over the course
28 * of the program as constant.
29 *
30 * The goal here is to trigger further constant folding and then dead
31 * code elimination. This is common with vector/matrix constructors
32 * and calls to builtin functions.
33 */
34
Eric Anholt65122e92010-05-12 12:10:41 -070035#include "ir.h"
Eric Anholt65122e92010-05-12 12:10:41 -070036#include "ir_visitor.h"
37#include "ir_optimization.h"
38#include "glsl_types.h"
Eric Anholt65122e92010-05-12 12:10:41 -070039
40struct assignment_entry {
Kenneth Graunke332920a2010-06-09 17:05:14 -070041 exec_node link;
Eric Anholt65122e92010-05-12 12:10:41 -070042 int assignment_count;
43 ir_variable *var;
44 ir_constant *constval;
45};
46
47class ir_constant_variable_visitor : public ir_hierarchical_visitor {
48public:
Eric Anholt65122e92010-05-12 12:10:41 -070049 virtual ir_visitor_status visit_enter(ir_assignment *);
Eric Anholt2927c812010-07-20 14:21:43 -070050 virtual ir_visitor_status visit_enter(ir_call *);
Eric Anholt65122e92010-05-12 12:10:41 -070051
Kenneth Graunke332920a2010-06-09 17:05:14 -070052 exec_list list;
Eric Anholt65122e92010-05-12 12:10:41 -070053};
54
55static struct assignment_entry *
Kenneth Graunke332920a2010-06-09 17:05:14 -070056get_assignment_entry(ir_variable *var, exec_list *list)
Eric Anholt65122e92010-05-12 12:10:41 -070057{
58 struct assignment_entry *entry;
59
Kenneth Graunke332920a2010-06-09 17:05:14 -070060 foreach_list_typed(struct assignment_entry, entry, link, list) {
Eric Anholt65122e92010-05-12 12:10:41 -070061 if (entry->var == var)
62 return entry;
63 }
64
65 entry = (struct assignment_entry *)calloc(1, sizeof(*entry));
66 entry->var = var;
Kenneth Graunke332920a2010-06-09 17:05:14 -070067 list->push_head(&entry->link);
Eric Anholt65122e92010-05-12 12:10:41 -070068 return entry;
69}
70
71ir_visitor_status
72ir_constant_variable_visitor::visit_enter(ir_assignment *ir)
73{
74 ir_constant *constval;
75 struct assignment_entry *entry;
76
77 entry = get_assignment_entry(ir->lhs->variable_referenced(), &this->list);
78 assert(entry);
79 entry->assignment_count++;
80
81 /* If it's already constant, don't do the work. */
82 if (entry->var->constant_value)
83 return visit_continue;
84
85 /* OK, now find if we actually have all the right conditions for
86 * this to be a constant value assigned to the var.
87 */
88 if (ir->condition) {
89 constval = ir->condition->constant_expression_value();
90 if (!constval || !constval->value.b[0])
91 return visit_continue;
92 }
93
94 ir_variable *var = ir->lhs->whole_variable_referenced();
95 if (!var)
96 return visit_continue;
97
98 constval = ir->rhs->constant_expression_value();
99 if (!constval)
100 return visit_continue;
101
102 /* Mark this entry as having a constant assignment (if the
103 * assignment count doesn't go >1). do_constant_variable will fix
104 * up the variable with the constant value later.
105 */
106 entry->constval = constval;
107
108 return visit_continue;
109}
110
Eric Anholt2927c812010-07-20 14:21:43 -0700111ir_visitor_status
112ir_constant_variable_visitor::visit_enter(ir_call *ir)
113{
114 exec_list_iterator sig_iter = ir->get_callee()->parameters.iterator();
115 foreach_iter(exec_list_iterator, iter, *ir) {
116 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
117 ir_variable *param = (ir_variable *)sig_iter.get();
118
119 if (param->mode == ir_var_out ||
120 param->mode == ir_var_inout) {
121 ir_variable *var = param_rval->variable_referenced();
122 struct assignment_entry *entry;
123
124 assert(var);
125 entry = get_assignment_entry(var, &this->list);
126 entry->assignment_count++;
127 }
128 sig_iter.next();
129 }
130 return visit_continue;
131}
132
Eric Anholt65122e92010-05-12 12:10:41 -0700133/**
134 * Does a copy propagation pass on the code present in the instruction stream.
135 */
136bool
137do_constant_variable(exec_list *instructions)
138{
139 bool progress = false;
140 ir_constant_variable_visitor v;
141
142 v.run(instructions);
143
Kenneth Graunke332920a2010-06-09 17:05:14 -0700144 while (!v.list.is_empty()) {
Eric Anholt65122e92010-05-12 12:10:41 -0700145
146 struct assignment_entry *entry;
Kenneth Graunke332920a2010-06-09 17:05:14 -0700147 entry = exec_node_data(struct assignment_entry, v.list.head, link);
Eric Anholt65122e92010-05-12 12:10:41 -0700148
149 if (entry->assignment_count == 1 && entry->constval) {
150 entry->var->constant_value = entry->constval;
151 progress = true;
152 }
Kenneth Graunke332920a2010-06-09 17:05:14 -0700153 entry->link.remove();
Eric Anholt65122e92010-05-12 12:10:41 -0700154 free(entry);
155 }
156
157 return progress;
158}
159
160bool
161do_constant_variable_unlinked(exec_list *instructions)
162{
163 bool progress = false;
164
165 foreach_iter(exec_list_iterator, iter, *instructions) {
166 ir_instruction *ir = (ir_instruction *)iter.get();
167 ir_function *f = ir->as_function();
168 if (f) {
169 foreach_iter(exec_list_iterator, sigiter, *f) {
170 ir_function_signature *sig =
171 (ir_function_signature *) sigiter.get();
172 if (do_constant_variable(&sig->body))
173 progress = true;
174 }
175 }
176 }
177
178 return progress;
179}