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_constant_variable.cpp
27 * Marks variables assigned a single constant value over the course
28 * of the program as constant.
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.
38 #include "ir_print_visitor.h"
39 #include "ir_visitor.h"
40 #include "ir_optimization.h"
41 #include "glsl_types.h"
43 struct assignment_entry
{
47 ir_constant
*constval
;
50 class ir_constant_variable_visitor
: public ir_hierarchical_visitor
{
52 virtual ir_visitor_status
visit_enter(ir_assignment
*);
57 static struct assignment_entry
*
58 get_assignment_entry(ir_variable
*var
, exec_list
*list
)
60 struct assignment_entry
*entry
;
62 foreach_list_typed(struct assignment_entry
, entry
, link
, list
) {
63 if (entry
->var
== var
)
67 entry
= (struct assignment_entry
*)calloc(1, sizeof(*entry
));
69 list
->push_head(&entry
->link
);
74 ir_constant_variable_visitor::visit_enter(ir_assignment
*ir
)
76 ir_constant
*constval
;
77 struct assignment_entry
*entry
;
79 entry
= get_assignment_entry(ir
->lhs
->variable_referenced(), &this->list
);
81 entry
->assignment_count
++;
83 /* If it's already constant, don't do the work. */
84 if (entry
->var
->constant_value
)
85 return visit_continue
;
87 /* OK, now find if we actually have all the right conditions for
88 * this to be a constant value assigned to the var.
91 constval
= ir
->condition
->constant_expression_value();
92 if (!constval
|| !constval
->value
.b
[0])
93 return visit_continue
;
96 ir_variable
*var
= ir
->lhs
->whole_variable_referenced();
98 return visit_continue
;
100 constval
= ir
->rhs
->constant_expression_value();
102 return visit_continue
;
104 /* Mark this entry as having a constant assignment (if the
105 * assignment count doesn't go >1). do_constant_variable will fix
106 * up the variable with the constant value later.
108 entry
->constval
= constval
;
110 return visit_continue
;
114 * Does a copy propagation pass on the code present in the instruction stream.
117 do_constant_variable(exec_list
*instructions
)
119 bool progress
= false;
120 ir_constant_variable_visitor v
;
124 while (!v
.list
.is_empty()) {
126 struct assignment_entry
*entry
;
127 entry
= exec_node_data(struct assignment_entry
, v
.list
.head
, link
);
129 if (entry
->assignment_count
== 1 && entry
->constval
) {
130 entry
->var
->constant_value
= entry
->constval
;
133 entry
->link
.remove();
141 do_constant_variable_unlinked(exec_list
*instructions
)
143 bool progress
= false;
145 foreach_iter(exec_list_iterator
, iter
, *instructions
) {
146 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
147 ir_function
*f
= ir
->as_function();
149 foreach_iter(exec_list_iterator
, sigiter
, *f
) {
150 ir_function_signature
*sig
=
151 (ir_function_signature
*) sigiter
.get();
152 if (do_constant_variable(&sig
->body
))